LightTable / Clojure

Light Table Clojure language plugin
MIT License
99 stars 47 forks source link

change default-content of instarepl #47

Closed Knotschi closed 9 years ago

Knotschi commented 9 years ago

I would like to change the default-content of my instarepl in the user.behaviours. Is that possible?

kenny-evitt commented 9 years ago

I learned a lot figuring out the answer to this question – thanks!

TLDR

Add the following to the vector in your user.behaviors file:

 [:editor.clj.instarepl :lt.plugins.clojure.instarepl/start-content ";; Blah blah blah

"]

Save the user.behaviors file – Light Table should reload it automatically – and then open a new Clojure InstaREPL tab – you should see ;; Blah blah blah at the top of the tab contents.

Details

I'm guessing you included "default-content" in your question because you found these lines in the Clojure plugin source:

(def default-content ";; Anything you type in here will be executed
;; immediately with the results shown on the
;; right.
")

If you search that same source file (src/lt/plugins/clojure/instarepl.cljs) for default-content you'll find a single reference in the instarepl Light Table object definition:

(object/object* ::instarepl
                :tags #{:instarepl}
                :name "Instarepl"
                :live true
                :init (fn [this]
                        (let [main (-> (pool/create {:mime "text/x-clojure" :content "" :ns "user"})
                                       (object/remove-tags [:editor.clj])
                                       (object/add-tags [:editor.clj.instarepl :editor.transient]))]
                          (object/merge! main {:frame this})
                          (editor/set-val main (or (object/raise-reduce main :start-content+) default-content))
                          (editor/clear-history main)
                          (object/merge! main {:dirty false
                                               :editor.generation (editor/->generation main)})
                          (object/merge! this {:main main
                                               :dirty false})
                          (editor/+class main :main)
                          (editor/move-cursor main {:line 10000 :ch 0})
                          [:div#instarepl
                           (live-toggle this)
                           (object/->content main)
                           [:p.error (bound this :error)]
                           ]
                          )))

In particular, this line references default-content:

(editor/set-val main (or (object/raise-reduce main :start-content+) default-content))

So a Clojure InstaREPL tab will use the result of evaluating (object/raise-reduce main :start-content+) if the return value isn't false or nil – otherwise it will use default-content.

So how do we affect the value of (object/raise-reduce main :start-content+)?

The raise-reduce function raises the trigger :start-content+ for the object main, main being a new Clojure 'editor pool' (buffer).

If we search this same source file for the trigger we find the creation of a behavior named start-content:

(behavior ::start-content
          :triggers #{:start-content+}
          :type :user
          :desc "Instarepl: Set start content"
          :params [{:label "content"}]
          :reaction (fn [this res content]
                      content))

So we need to add the start-content behavior to the main object but how do we do that? Instead of adding the behavior directly to that object, we can add the behavior to one of the tags on that object.

Based on the code for creating main we know that it has at least two tags: :editor.clj.instarepl and :editor.transient. :editor.clj.instarepl is probably a more appropriate tag to use as we probably don't want to change the start content of every 'transient editor (tab)'.

Because the various .behaviors files are for doing exactly this, we just need to add the following to the vector in our user.behaviors file:

 [:editor.clj.instarepl :lt.plugins.clojure.instarepl/start-content ";; Blah blah blah

"]
Knotschi commented 9 years ago

thanx a lot for that awesome detailed answer! I understand lt plugins now a lot better :+1: