Arry
Arry

Reputation: 1035

Cider: how to run a comand on startup?

I have a web app project that uses boot and reloaded.repl.
It has the following piece of configuration:

(require
<...>
 '[reloaded.repl :refer [init start stop go reset]]
<...>)

I start it as boot dev, and do a (cider-connect <...>) to the instance - after a while a *cider-repl <stuff>* buffer appears.

The first command I want to run and always run is (go) in the Repl so that the dev server is ready. How can I configure Cider to invoke a command when the connection is ready?

I couldn't find anything in the documentation and searching for "cider startup", "cider run command when repl is ready" etc.

Upvotes: 1

Views: 384

Answers (2)

Arry
Arry

Reputation: 1035

Combined with the idea of Rulle's answer, this works:


(add-hook 'cider-connected-hook (lambda ()
                                  (cider-interactive-eval "(go)")))

When evaluated in the scratch buffer, the (cider-interactive-eval "(go)") unhelpfully errors out with "No linked session", but when put in a hook, it works.

Upvotes: 0

Rulle
Rulle

Reputation: 4901

I am not quite sure how to run a command when the REPL is ready, but would it be acceptable to have a keybinding, e.g. Ctrl + F12, in Emacs for running the command? If that is an acceptable solution, you could have the following code in your ~/.emacs.d/init.el file:

(defun initialize-reloaded ()
  (interactive)
  (cider-interactive-eval
   "(do (require '[reloaded.repl :refer [init start stop go reset]])
        (go))"))

(add-hook 'cider-mode-hook 
          (lambda ()
            (define-key clojure-mode-map (kbd "<C-f12>") 'initialize-reloaded)))

In the above code, I have included the code to require reloaded but you can omit that if that code is already required by the source file.

Then all you have to do is to press Ctrl + F12 to start the server.

Upvotes: 0

Related Questions