                                    (str "(resolve '" (current-word ed) ")")
                                    :open-grimoire)))})
 
-;; (def url "http://grimoire.arrdem.com/1.5.0/clojure.string/blank_QMARK/examples")
 (defn GET [url cb]
   (let [req (.get (js/require "http") url
                   (fn [resp]

            [clojure.string :as s]
            [lt.objs.editor.pool :as pool]
            [lt.plugins.clojure :as clojure]
            [lt.objs.tabs :as tabs]
            [lt.objs.notifos :as notifos]
            [lt.objs.command :as cmd])
  (:require-macros [lt.macros :refer [defui behavior]]))

    (.on req "error" (fn [err]
                       (println "Request" url "failed with:" (.-message err))))))

(def ->grimoire-examples-url (comp #(str % "/examples") ->grimoire-url))

(defn fetch-and-open-grimoire-examples* [{:keys [ns var url]}]
  (notifos/working)
  (GET url (fn [body]
             (let [path (tempfile (str ns "-" var) ".clj")]
               (files/save path body)
               (cmd/exec! :open-path path))
             (notifos/done-working))))

(def fetch-and-open-grimoire-examples
  (partial for-resolved-var
           (fn [ns var]
             (fetch-and-open-grimoire-examples*
              {:ns ns :var var :url (->grimoire-examples-url ns var)}))))

(cmd/command {:command :inc-clojure.open-grimoire-examples
              :desc "IncClojure: Open grimoire examples locally for current symbol"
              :exec (fn []
                      (let [ed (pool/last-active)]
                        (eval-code ed
                                   (str "(resolve '" (current-word ed) ")")
                                   :fetch-and-open-grimoire-examples)))})

(comment
  (object/raise editor :editor.eval.clj.result.callback "OK?")
  (-> @editor :client :default deref)
