Running 'lein swank' (calling clojure server) with elisp - emacs

As is asked and answered here. I could use 'lein swank' to run clojure on Aquamacs.
I need to automate running 'lein swank', before running slime/clojure.
Q : Is there a way to this automatically? I mean how can I run the command 'lein swank' automatically when slime/clojure (M-x slime-connect) is called.
Q : If I have to come up with elisp code to run 'lein swank', how can I do that?
Added
Based on Jürgen Hötzel's answer, I modified the elisp as follows.
(defun lein-swank ()
(interactive)
(let ((default-directory (locate-dominating-file default-directory "/Users/smcho/bin/leiningen")))
(when (not default-directory)
(error "Not in a Leiningen project."))
;; you can customize slime-port using .dir-locals.el
(let ((proc (start-process "lein-swank" nil "/Users/smcho/bin/leiningen/bin/lein" "swank" (number-to-string 4005))))
(when proc
(process-put proc :output nil)
(set-process-sentinel proc (lambda (proc event)
(message "%s%s: `%S'"
(process-get proc :output)
proc (replace-regexp-in-string "\n" "" event))))
(set-process-filter proc
(lambda (proc output)
;; record last line of output until connected (possible error message)
(process-put proc :output (concat (process-get proc :output) output))
(when (string-match "Connection opened on" output)
(slime-connect "localhost" 4005)
;; no need to further process output
(set-process-filter proc nil))))
(message "Starting swank server...")))))
But, I got this error.
No project.clj found in this directory. lein-swank: `"exited abnormally with code 1"'.
What I found was that I should change pwd to ~/bin/leiningen to run 'lein swank'. Just put lein binary inside the PATH string doesn't make it run.

I made a gist for this job:
http://gist.github.com/419364
Just use the interactive command "M-x lein-swank", which will spawn the command in the current directory and connect to it.
I made several improvements to lein-swank:
lein-swank-command is now customizable: You can use Leiningen, if its bin directory is not part of your PATH environment.
Added directory as interactive argument: If project.clj can not be found in a dominating location of your current directory, you can specify a location.

Related

Spawing ctags from Emacs with start-process

I have a function that is supposed to create a ctags file and load it into Emacs asynchronously. ctags can take a while to run if invoked on really big files, and I don't want my function to do any blocking, thus I use start-process. This is what it all looks like:
(defun temp-tags-file-for-file (file)
"Generate a temporary tags file for FILE.
Add the file to tags list and return the name of the file."
(if (not (boundp 'ctags-command))
(setq ctags-command "/usr/bin/ctags"))
(let* ((temp-file (make-temp-file "EMACS_TAGS"))
(proc (start-process "temp-tags-proc" nil ctags-command
"-f" temp-file file)))
(set-process-sentinel proc
(lambda (proc msg)
(when (eq (process-status proc) 'exit)
(if (boundp 'temp-tags-file)
(progn
(add-to-list 'tags-table-list
temp-tags-file)
(makunbound 'temp-tags-file))))))
(setq temp-tags-file temp-file)
temp-file))
For some reason, the tags file is always blank. Calling ctags with the exact same parameters from the shell generates a non-blank, working tags file. How do I get ctags to print its output properly?
If ctags wants the shell, just give it to the shell:
(start-process "temp-tags-proc" nil shell-file-name shell-command-switch
(format "/usr/bin/ctags ~/Dropbox/source/c/*.c -f %s"
(make-temp-file "EMACS_TAGS")))

emacs shell-mode follows its default-directory to current-file

When I enter cd c:/dir/to/path in shell mode, shell mode follows its default-directory to c:/dir/to/path and that's good. But while visiting a certain file(c:/another/dir/file.ext), how can I let the existing shell directory to that one without entering cd c:/antoher/dir/file.ext ?
Is there any pre-exisiting function for that in emacs? searched quite a lot but not found unfortunately.
I'm using Emacs 24.2.1 in Win7.
EDIT:
I've written a not-good-looking function like followings. Any proposal/advice will be appreciated(I'm a novice in elisp).
(With prefixed interactive call, it will show shell buffer with current directory. I'm repeatedly thinking there might be something already invented one better than this).
(defun my-shell-with-current-directory (&optional arg)
(interactive "P")
(let* ((sp (get-process "shell"))
(spbuf (and sp (process-buffer sp)))
(dir (if buffer-file-name (file-name-directory buffer-file-name) default-directory)))
(if (and arg sp spbuf dir)
(progn
(comint-simple-send sp (concat "cd /d " dir))
(display-buffer spbuf)
(save-excursion
(set-buffer spbuf)
(cd dir)
)
)
(progn
(shell)
(comint-simple-send sp "setlocal enableextensions")
)
)
)
)
You may set default-directory using setq at any time.
You may add that code as a hook onto find-file-hooks.
Also you may advice find-file command with the setting wished.
So far just my thoughts...

Update multi-term buffer name based on PWD

If I use konsole or other terminal, the terminal tag name can change based on PWD. But in multi-term, the buffer name is *terminal<number>*. This is not very nice. Because when I switch between them, the name is not very informative. So I want to rename it based on PWD.
I find that the Enter key is bind to term-send-raw, so I write a function
(defadvice term-send-raw (around rename-term-name activate)
(progn
(rename-buffer
(concat "⇒ "
(shell-command-to-string "pwd | xargs basename | tr -d '\n'")
(format-time-string " [%M ∞ %S]")))
ad-do-it))
But the problem is pwd command return the PWD of the terminal buffer, while it is not the PWD of the SHELL in that terminal.
The PWD of the terminal buffer is set by defcustom multi-term-default-dir. And it does not change when the PWD change in the SHELL.
(defcustom multi-term-default-dir "~/"
"The default directory for terms if current directory doesn't exist."
:type 'string
:group 'multi-term)
How can I get the PWD of the SHELL in the terminal?
Regards.
AFAIK there is no easy way to retrieve information from a running process.
But if you want to get the current directory you could:
ask the shell to print it
parse and trace the command-line for functions like cd, pushd, popd…
poll /proc/PID/cwd
The first method is described in the header of term.el (M-xfind-libraryRETtermRET).
And now, thank you for your question, you gave me the opportunity to do this:
(defadvice term-send-input (after update-current-directory)
(let* ((pid (process-id (get-buffer-process (current-buffer))))
(cwd (file-truename (format "/proc/%d/cwd" pid))))
(cd cwd)))
(ad-activate 'term-send-input)
It's a naive implementation of the third method and it doesn't work if the user uses su or ssh. However, I don't know if it's possible withouth using the first or the second method.
In your case, you can just replace the cd command with whatever you want.
Building off of Daimrod's answer for polling /proc/PID/cwd, I found a way get around the problem that Reed pointed out where the advice doesn't pick up the updated CWD immediately and you have to hit Enter twice.
If you move the CWD update code to its own function and use run-at-time to call it from the advice at a later time, it will pick up the updated CWD correctly. Unfortunately I don't know enough about Emacs' scheduling to explain why this works (any enlightenment would be appreciated).
Here's my code based on Daimrod's. Note I advised term-send-input for line-mode and term-send-return for char-mode. I tested this using multi-term on Emacs 24.3.1:
(defadvice term-send-input (after update-current-directory)
(run-at-time "0.1 sec" nil 'term-update-dir)
)
(ad-activate 'term-send-input)
(defadvice term-send-return (after update-current-directory)
(run-at-time "0.1 sec" nil 'term-update-dir)
)
(ad-activate 'term-send-return)
(defun term-update-dir ()
(let* ((pid (process-id (get-buffer-process (current-buffer))))
(cwd (file-truename (format "/proc/%d/cwd" pid))))
(unless (equal (file-name-as-directory cwd) default-directory)
(message (concat "Switching dir to " cwd))
(cd cwd)))
)
Most terminals get their window name from the command echo -en. In zsh you can put this in your ~/.zshenv
precmd() { echo -en "\e]0;`basename ${PWD}`\a" }
and that will get the basename of your PWD environment variable. Ideally multi-term would do something similar and put it in multi-term-buffer-name, which is the variable which holds its buffer name.
Yes, this is not a complete solution. I'm hoping for one too!
Try this:
(defun open-or-jump-to-multi-term ()
(interactive)
(if (string-prefix-p "*terminal<" (buffer-name))
(delete-window)
(progn
(setq bufname (concat "*terminal<" (directory-file-name (file-name-directory (buffer-file-name))) ">"))
(if (get-buffer-process bufname)
(switch-to-buffer-other-window bufname)
(progn
(split-window-right)
(other-window 1)
(multi-term)
(rename-buffer bufname)
)
)))
)
(global-set-key (kbd "C-`") 'open-or-jump-to-multi-term)

Using Emacs, is it possible to pin the compilation command to a specific buffer/directory?

Right now I am using the following to compile, when I'm in for example main.cpp
C-x b Makefile RET M-x compile RET RET
I actually have M-x compile as a keyboard shortcut, but the problem is I would really like not having to go through all that trouble to simply run my Makefile.
I need to visit Makefile to make sure the compile command is executed using the same directory. Is there any way to pin the directory so I can simply go M-x compile RET RET?
Best regards
Use recompile instead. C-u M-x recompile will let you edit the compile command first. Either way the compile will work out of the directory the last compile was done in.
See my answer here
Directory local variables provide an easy way to trigger the compile from a parent directory of any source file in a subdirectory.
I run emacs primarily on windows.
When I have a makefile that is in a parent directory of a C module, I use this as the compile command:
cd .. && nmake <arguments here>
for example:
cd .. && nmake CONFIG=Debug PLATFORM=x64 target
Beyond that, I find that specifying the make command line that I want to run for various modules is sort of a pain. I wanted a way to attach the default compile command to the buffer being edited. So I wrote a little elisp to handle that job. I figured to insert into the header comments of each buffer a line that would stipulate my preferred compile command, like this:
compile: cd .. && nmake CONFIG=Debug PLATFORM=x64 target
And then have a piece of elisp run, before I invoke M-x compile that grabs the line and proposes it as the compile command I would like to run.
This defun pulls a line out of the header comments:
(defun cheeso-c-get-value-from-comments (marker-string line-limit)
"gets a string from the header comments in the current buffer.
This is used to extract the compile command from the comments. It
could be used for other purposes too.
It looks for \"marker-string:\" and returns the string that
follows it, or returns nil if that string is not found.
eg, when marker-string is \"compile\", and the following
string is found at the top of the buffer:
compile: cl.exe /I uthash
...then this command will return the string
\"cl.exe /I uthash\"
It's ok to have whitespace between the marker and the following
colon.
"
(let (start search-limit found)
;; determine what lines to look in
(save-excursion
(save-restriction
(widen)
(cond ((> line-limit 0)
(goto-char (setq start (point-min)))
(forward-line line-limit)
(setq search-limit (point)))
((< line-limit 0)
(goto-char (setq search-limit (point-max)))
(forward-line line-limit)
(setq start (point)))
(t ;0 => no limit (use with care!)
(setq start (point-min))
(setq search-limit (point-max))))))
;; look in those lines
(save-excursion
(save-restriction
(widen)
(let ((re-string
(concat "\\b" marker-string "[ \t]*:[ \t]*\\(.+\\)$")))
(if (and start
(< (goto-char start) search-limit)
(re-search-forward re-string search-limit 'move))
(buffer-substring-no-properties
(match-beginning 1)
(match-end 1))))))))
Ok, now I need something to invoke that before I invoke compile.
(defun cheeso-invoke-compile-interactively ()
"fn to wrap the `compile' function. This simply
checks to see if `compile-command' has been previously set, and
if not, invokes `cheeso-guess-compile-command' to set the value.
Then it invokes the `compile' function, interactively."
(interactive)
(cond
((not (boundp 'cheeso-local-compile-command-has-been-set))
(cheeso-guess-compile-command)
(set (make-local-variable 'cheeso-local-compile-command-has-been-set) t)))
;; local compile command has now been set
(call-interactively 'compile))
Then of course, the defun that guesses the compile command:
(defun cheeso-guess-compile-command ()
"set `compile-command' intelligently depending on the
current buffer, or the contents of the current directory."
(interactive)
(set (make-local-variable 'compile-command)
(cond
(buffer-file-name
(let ((filename (file-name-nondirectory buffer-file-name)))
(cond
;; editing a C-language source file - check for an
;; explicitly-specified command
((string-equal (substring buffer-file-name -2) ".c")
(let ((explicit-compile-command
(cheeso-c-get-value-from-comments "compile" 34)))
(or explicit-compile-command
(concat "nmake " ;; assume a makefile exists
(file-name-sans-extension filename)
".exe"))))
;; editing a makefile - just run nmake
((string-equal (substring buffer-file-name -8) "makefile")
"nmake ")
;; something else - do a typical .exe build
(t
(concat "nmake "
(file-name-sans-extension filename)
".exe")))))
(t
;; punt
"nmake "))))
The final bit is to bind C-x C-e , normally bound to compile, to the wrapper defun:
(global-set-key "\C-x\C-e" 'cheeso-invoke-compile-interactively)
Now, when I do C-x C-e in the buffer, it searches for the compile command, and proposes to me the command that it finds. I can edit the proposed compile command, then press ENTER and run it.

emacs sqlplus disconnected

I started using sqlplus for emacs. It works fine except one thing - very often I get a message "Buffer ... is not talking to anybody". In a file sqlplus.el there is the following code which verifies (get-buffer-process process-buffer-name). How can I keep alive the sql process?
(defun sqlplus-verify-buffer (connect-string)
(let ((output-buffer-name (sqlplus-get-output-buffer-name connect-string))
(process-buffer-name (sqlplus-get-process-buffer-name connect-string)))
(when (not (get-buffer process-buffer-name))
(sqlplus-shutdown connect-string)
(error "No SQL*Plus session! Use 'M-x sqlplus' to start the SQL*Plus interpreter"))
(unless (get-buffer-process process-buffer-name)
(sqlplus-shutdown connect-string)
(error "Buffer '%s' is not talking to anybody!" output-buffer-name)))
t)
One possible solution is the following
(defadvice sqlplus-verify-buffer (before sqlplus-verify-buffer-and-reconnect activate)
(unless (get-buffer-process (sqlplus-get-process-buffer-name connect-string))
(sqlplus connect-string)))