sanel / monroe

Clojure nREPL client for Emacs
161 stars 21 forks source link

Simple function to launch nrepl server via Leiningen #27

Closed lukaszkorecki closed 6 years ago

lukaszkorecki commented 6 years ago

I had this function in my Emacs config for a while now and I thought I'll share it. It's quite naive in the implementation, it offers no help in finding the project's root path so my usual flow is:

Now, it's probably not a good idea for Monroe to become CIDER's equivalent, but even simpler Clojure integration modes like inf-clojure have some way of launching the REPL server in the background.

Let me know what you think!

sanel commented 6 years ago

Good idea!

Can you please rename function to monroe-nrepl-server-start and put cmd as global variable, like monroe-nrepl-server-cmd and monroe-nrepl-server-args?

This way it will not be dependable on leiningen and can be easily replaced with boot or other language implementations

technomancy commented 6 years ago

What if this knew how to tell when the server launched (by calling monroe-locate-running-nrepl-host) and could actually make the connection?

lukaszkorecki commented 6 years ago

@sanel Will do!

@technomancy I like that 👍 - and locate-dominating-file can be used to set the workdir of terminal buffer.

technomancy commented 6 years ago

Using a terminal buffer here when the server is running headlessly actually struck me as a bit odd; usually that's for situations when you're interacting over stdio. Using async-shell-command might be simpler?

lukaszkorecki commented 6 years ago

Ah, that's a leftover from the original version of the function when it didn't use the :headless option. I'll have a look at the async command 👍

lukaszkorecki commented 6 years ago

@sanel @technomancy Pushed the following updates:

sanel commented 6 years ago

Looks very good. Thanks @lukaszkorecki !

lukaszkorecki commented 6 years ago

🙌

sanel commented 6 years ago

@lukaszkorecki sorry for re-opening this, from where you used async-start-process? My emacs version 25.3.1 doesn't have it... Did you mean to use async-shell-command instead?

lukaszkorecki commented 6 years ago

@sanel I'm running Emacs 26 and have 25 laying around as well. Let me check if 25 has an equivalent function

sanel commented 6 years ago

Here is implementation with async-shell-command:

diff --git a/monroe.el b/monroe.el
index 9135f00..0d655d3 100644
--- a/monroe.el
+++ b/monroe.el
@@ -544,24 +544,17 @@ as path can be remote location. For remote paths, use absolute path."
 (defun monroe-nrepl-server-start ()
   "Starts nrepl server. Uses monroe-nrepl-server-cmd + monroe-nrepl-server-cmd-args as the command. Finds project root by locatin monroe-nrepl-server-project-file"
   (interactive)
-  (let* ((cmd monroe-nrepl-server-cmd)
-         (switches (split-string-and-unquote monroe-nrepl-server-cmd-args))
-         (nrepl-buf-name (concat "*" monroe-nrepl-server-buffer-name "*"))
+  (let* ((nrepl-buf-name (concat "*" monroe-nrepl-server-buffer-name "*"))
          (repl-started-dir (monroe-locate-port-file)))
     (if repl-started-dir
-        (message (concat "Monroe: nREPL server already running in " repl-started-dir))
+        (message "nREPL server already running in %s" repl-started-dir)
       (progn
         (lexical-let ((default-directory
                         (locate-dominating-file default-directory
                                                 monroe-nrepl-server-project-file)))
-          (message (concat
-                    "Monroe: Starting nREPL server in " default-directory))
-          (apply 'async-start-process
-                 monroe-nrepl-server-buffer-name
-                 cmd
-                 nil
-                 switches))
-        (switch-to-buffer nrepl-buf-name)))))
+          (message "Starting nREPL server in %s" default-directory)
+          (async-shell-command (concat monroe-nrepl-server-cmd " " monroe-nrepl-server-cmd-args)
+                               nrepl-buf-name))))))

 (defun monroe-extract-keys (htable)
   "Get all keys from hashtable."

Will this work for you? (Emacs 26 and 25)

lukaszkorecki commented 6 years ago

@sanel checking 👍

lukaszkorecki commented 6 years ago

@sanel yep confirmed working in Emacs 26.1 and Emacs 25.3.1 (both OSX)

lukaszkorecki commented 6 years ago

@sanel Sorry for that - I didn't realize that async-start-process is not a built-in (it comes with async.el, which is probably pulled in by something else in my config)

sanel commented 6 years ago

No worry @lukaszkorecki :) I guess you have https://github.com/jwiegley/emacs-async package automatically loaded.