How to wait for / capture aysnchronous shell command output in emacs lisp? - elisp

If I execute a shell command asynchronously in emacs lisp like so:
(shell-command "mycommand &")
Is there a way to wait for the command to generate output before continuing? For my current application, it is probably sufficient to wait until the command generates any output at all, but ideally I'd like to capture the output for additional processing. Is this possible?

You should use comint-output-filter-functions variable that contains function to call after output is inserted into the buffer.
For example, you can do :
(add-hook 'comint-output-filter-functions '(lambda (txt) (message "hello")))
N.B. : From Emacs 23.2, you have the new command async-shell-command, bound globally to M-&.
This executes your command asynchronously without requiring an ampersand. The output of your command is sent to the buffer
*Async Shell Command*.

Perhaps you need to register a Process Filter to give you the callback timing you need? See 37.9 Receiving Output from Processes in the Elisp manual (I see this in my copy for Emacs 22.3).
Here is an example of running a callback when you get the first process output and also storing it into an "associated buffer". Copy it to your *scratch* buffer and eval-region it, but make sure to split-window and show the *Messages* buffer visible so that you can see what's going on.
;; this is emacs lisp (and a comment line)
(defvar my-callback-got-some-already nil)
(defun my-callback ()
(message "callback ran at %s" (current-time-string)))
(defun my-filter-waits-for-first-time-input (proc string)
(unless my-callback-got-some-already
(setq my-callback-got-some-already t)
;; do your one-time thing
(my-callback))
;; insert into the associated buffer as if no process filter was
;; registered
(with-current-buffer (process-buffer proc)
(let ((moving (= (point) (process-mark proc))))
(save-excursion
;; Insert the text, advancing the process marker.
(goto-char (process-mark proc))
(insert string)
(set-marker (process-mark proc) (point)))
(if moving (goto-char (process-mark proc))))))
(defun async-callback-test-harness ()
(interactive)
(let ((process-handle "async-callback-test")
(associated-process-buffer "*async-callback-test*")
(command "ls")
(busy-loop-var ""))
(setq my-callback-got-some-already nil)
(message "start test %s" (current-time-string))
(start-process process-handle associated-process-buffer command)
;; Supposedly async but Emacs doesn't get the input until
;; "Emacs is waiting" so the following set-process-filter
;; can be registered in time.
;; To prove the point, make emacs busy loop to show that the
;; emacs doesn't drop its input and
;; the callback will get the unskipped input.
(switch-to-buffer associated-process-buffer)
(dotimes (k 2000) ; about two seconds on my machine
(setq busy-loop-var (concat busy-loop-var "busy looping...")))
(message "done busy waiting %s" (current-time-string))
(set-process-filter (get-process process-handle)
'my-filter-waits-for-first-time-input)
nil))
;; run it!
(async-callback-test-harness)

Related

calling org-content from lisp not working

When I call org-content from the active buffer I get the outline I want. However if I use it in a lisp function like this
(split-window-right (truncate (* W 0.75)))
(if (get-buffer "inbox.org")
(set-window-buffer nil "inbox.org")
(progn
(find-file "~/Documents/GTD/inbox.org")
(text-scale-set -1)))
(org-content)
The windows splits and the right buffer gets loaded but the org-content bit doesn't seem to do anything.
Any ideas of what I do wrong ?
Thanks,
Jouke
Here is a reproducible example, open a new buffer named test.org and define the following function in the *scratch* buffer:
(defun test ()
(let ((buffer (get-buffer "test.org")))
(when buffer
(set-window-buffer nil buffer)
(message "%s" (current-buffer)))))
The message being outputted is *scratch*: only the buffer associated with the window was changed, but what Emacs considers the current buffer did not.
If instead you use switch-to-buffer, as follows, the message displays the selected buffer:
(defun test ()
(let ((buffer (get-buffer "test.org")))
(when buffer
(switch-to-buffer buffer)
(message "%s" (current-buffer)))))
Applying the same change to your code makes (org-content) happy.

Getting the output of a process

I am trying to create a function which takes as input a path and returns the output of the ls terminal command as a string. I'm using a process and sentinel since I'll eventually want to create other functions which will take some time to execute, and I want them to run asynchronously.
(defun ls-to-string (path)
(let (ls-proc
ls-output)
(progn (setq ls-proc (start-process "" "ls-buffer" "ls" path))
(set-process-sentinel ls-proc (lambda (p e)
(if (string= e "finished\n")
(progn (set-buffer "ls-buffer")
(setq ls-output (buffer-string))
(kill-buffer "ls-buffer")
(message ls-output))))) <---- (1)
ls-output))) <---- (2)
(ls-to-string "/home")
I have (temporarily) added (message ls-output) just to show that ls-output does contain the string (1). However the return value is nil (2).

Emacs : how to load file content in scratch screen

I would like to load "~/todo.org" file content in scratch buffer at startup.
I have tried:
(setq initial-buffer-choice "~/todo.org")
But it opens the file in a new buffer (not scratch).
I have also tried:
(setq initial-scratch-message "~/todo.org")
But it prints the file path in the scratch buffer and i would like it's content.
I also would like to change the mode of the scratch buffer to org-mode.
I have tried:
(setq initial-major-mode org-mode)
But i have an initialisation error
Symbol's value as variable is void: org-mode
You can achieve the desired effect with a little bit of Lisp code that you put in your init file:
(condition-case err
(with-current-buffer "*scratch*"
(let ((min (point-min))
(max (point-max))
(goto-char max)
(insert-file-contents "~/todo.org")
(delete-region min max)
(org-mode)))
(error (message "%s" error-message-string err)))
But as #phils pointed out in a comment to your question, the *scratch* buffer might not be the best buffer to use for this functionality. Thus, I suggest to consider the following alternative:
(condition-case err
(let ((buffer (get-buffer-create "*todo*")))
(with-current-buffer buffer
(insert-file-contents "~/todo.org")
(org-mode))
(setq initial-buffer-choice buffer))
(error (message "%s" error-message-string err)))
By using this version, you leave the *scratch* buffer alone. Your .org file will be inserted in a separate buffer by the name of *todo*. This buffer is not associated with your ~/todo.org file, so when you first try save it, you will have to specify a file name.
Finally, i'll go for this:
(condition-case err
(when (get-buffer "*scratch*")
(with-current-buffer "*scratch*"
(erase-buffer)
(insert-file-contents "~/todo.org")
(org-mode)
)
)
(error (message "%s" error-message-string err)))

How can I map an unknown list of args to start-process in elisp?

I'm finally trying to learn elisp but haven't wrapped my head around how to map an unknown list of arguments to variables dynamically.
Here's a working function that passes up to three arguments to start-process. But I would like to pass an infinite number of args to the function.
(defun create-drush-buffer (command &rest a)
(if (locate-dominating-file default-directory "includes/bootstrap.inc")
(progn
(setq opt1 (car a))
(setq opt2 (cadr a))
(setq opt3 (caddr a))
(setq allopt (concat opt1 " " opt2 " " opt3))
(setq b-name (concat "*drush " command " " allopt "*"))
(if (buffer-live-p b-name)
(switch-to-buffer b-name)
(setq d-buffer (get-buffer-create b-name))
(with-current-buffer d-buffer
(goto-char (point-min))
(view-mode 1)
(hl-line-mode 1)
(if opt3
(start-process "drush" (current-buffer) drupal-drush-program
command
opt1
opt2
opt3)
(if opt2
(start-process "drush" (current-buffer)
drupal-drush-program
command
opt1
opt2)
(if opt1
(start-process "drush" (current-buffer)
drupal-drush-program
command
opt1)
(start-process "drush" (current-buffer)
drupal-drush-program
command))))
(shrink-window-if-larger-than-buffer))
(switch-to-buffer d-buffer)))
(message (concat default-directory " is not a drupal project"))))
Here's an example of a calling function I would like to work with create-drush-buffer.
(defun drush-sql-sync ()
(interactive)
(create-drush-buffer
"sql-sync"
"-y"
"-d"
"-v"
"#cu.wstage1-education"
"#cu.local-education"))
How can I achieve this and make my code less redundant? Any help with code or even steering me to relevant documentation appreciated.
You can use apply:
(apply FUNCTION &rest ARGUMENTS)
Call FUNCTION with our remaining args, using our last arg as list of
args. Then return the value FUNCTION returns. Thus, (apply '+ 1 2 '(3
4)) returns 10.
This way, you could call start-process using something like:
(apply 'start-process "drush" (current-buffer)
drupal-drush-program
command
a)
As a side note, you should not create temporary variables using setq, as this creates or modified global variables (if no local ones with the name exists). Instead, use let.
Good luck with your elisp projects!

Open a file at line with "filename:line" syntax

Very often, compilations errors are displayed with the file:line syntax.
It would be nice to copy-paste this directly to open the file at the right line.
Emacs already has some mode to handle this in buffers (compile-mode, iirc), but I would like to have this available from the shell command line, since I use the standard shell most of the time outside of emacs.
Any idea how to tweak emacs to learn file:line syntax to open file at line line? (obviously, if file:line really exists on disk, it should be opened preferably)
You can do this using emacsclient. e.g. to open FILE at line 4, column 3 in a new frame:
emacsclient +4:3 FILE
Leave off the :3 to simply open the file at line 4.
I have the following in my .emacs, but I haven't found it as useful as I thought it would be.
;; Open files and goto lines like we see from g++ etc. i.e. file:line#
;; (to-do "make `find-file-line-number' work for emacsclient as well")
;; (to-do "make `find-file-line-number' check if the file exists")
(defadvice find-file (around find-file-line-number
(filename &optional wildcards)
activate)
"Turn files like file.cpp:14 into file.cpp and going to the 14-th line."
(save-match-data
(let* ((matched (string-match "^\\(.*\\):\\([0-9]+\\):?$" filename))
(line-number (and matched
(match-string 2 filename)
(string-to-number (match-string 2 filename))))
(filename (if matched (match-string 1 filename) filename)))
ad-do-it
(when line-number
;; goto-line is for interactive use
(goto-char (point-min))
(forward-line (1- line-number))))))
I suggest to add following code in your emacs config:
(defadvice server-visit-files (before parse-numbers-in-lines (files proc &optional nowait) activate)
"looks for filenames like file:line or file:line:position and reparses name in such manner that position in file"
(ad-set-arg 0
(mapcar (lambda (fn)
(let ((name (car fn)))
(if (string-match "^\\(.*?\\):\\([0-9]+\\)\\(?::\\([0-9]+\\)\\)?$" name)
(cons
(match-string 1 name)
(cons (string-to-number (match-string 2 name))
(string-to-number (or (match-string 3 name) "")))
)
fn))) files))
)
by now you can open file with a line number right from command line like this:
emacsclient filename:linenumber:position
P.S. I hope i'm not too late with my answer.
And here is my go at it. Calls the original find-file-at-point
(defun find-file-at-point-with-line()
"if file has an attached line num goto that line, ie boom.rb:12"
(interactive)
(setq line-num 0)
(save-excursion
(search-forward-regexp "[^ ]:" (point-max) t)
(if (looking-at "[0-9]+")
(setq line-num (string-to-number (buffer-substring (match-beginning 0) (match-end 0))))))
(find-file-at-point)
(if (not (equal line-num 0))
(goto-line line-num)))
You can use a bash script:
#! /bin/bash
file=$(awk '{sub(/:[0-9]*$/,"")}1' <<< "$1")
line=$(awk '{sub(/^.*:/,"")}1' <<< "$1")
emacs --no-splash "+$line" "$file" &
If you call this script for openline and you get an error message, e.g.
Error: file.cpp:1046
you can do
openline file.cpp:1046
to open the file.cpp in Emacs at line 1046..
Another version of Ivan Andrus' nice find-file advice which does both line + optional column number, as you see in node and coffeescript errors:
;; Open files and go places like we see from error messages, i e: path:line:col
;; (to-do "make `find-file-line-number' work for emacsclient as well")
;; (to-do "make `find-file-line-number' check if the file exists")
(defadvice find-file (around find-file-line-number
(path &optional wildcards)
activate)
"Turn files like file.js:14:10 into file.js and going to line 14, col 10."
(save-match-data
(let* ((match (string-match "^\\(.*?\\):\\([0-9]+\\):?\\([0-9]*\\)$" path))
(line-no (and match
(match-string 2 path)
(string-to-number (match-string 2 path))))
(col-no (and match
(match-string 3 path)
(string-to-number (match-string 3 path))))
(path (if match (match-string 1 path) path)))
ad-do-it
(when line-no
;; goto-line is for interactive use
(goto-char (point-min))
(forward-line (1- line-no))
(when (> col-no 0)
(forward-char (1- col-no)))))))
Emacs 25 does not use defadvice anymore. Refs.
So here is the version updated to the new syntax:
(defun find-file--line-number (orig-fun filename &optional wildcards)
"Turn files like file.cpp:14 into file.cpp and going to the 14-th line."
(save-match-data
(let* ((matched (string-match "^\\(.*\\):\\([0-9]+\\):?$" filename))
(line-number (and matched
(match-string 2 filename)
(string-to-number (match-string 2 filename))))
(filename (if matched (match-string 1 filename) filename)))
(apply orig-fun (list filename wildcards))
(when line-number
;; goto-line is for interactive use
(goto-char (point-min))
(forward-line (1- line-number))))))
(advice-add 'find-file :around #'find-file--line-number)
This works if you call open file from inside emacs (C-x C-f), but not works anymore from command line, it seems that emacs 25 is not using find-file when you call it from command line and I don't know how to debug this kind of thing.
You talk about pasting to open a file (I assume you mean at a find file prompt inside of emacs) and also doing something from the command line. If you want to copy & paste then you need to do something like what Ivan showed with the defadvice. If you want something from the command line you can do the following. I've adapted this from something I did a year ago with an emacs:// URI handler (for use from within Firefox):
Put this in your .emacs file:
(defun emacs-uri-handler (uri)
"Handles emacs URIs in the form: emacs:///path/to/file/LINENUM"
(save-match-data
(if (string-match "emacs://\\(.*\\)/\\([0-9]+\\)$" uri)
(let ((filename (match-string 1 uri))
(linenum (match-string 2 uri)))
(while (string-match "\\(%20\\)" filename)
(setq filename (replace-match " " nil t filename 1)))
(with-current-buffer (find-file filename)
(goto-line (string-to-number linenum))))
(beep)
(message "Unable to parse the URI <%s>" uri))))
and then create a shell script in your path (I called mine 'emacsat'):
#!/bin/bash
emacsclient --no-wait -e "(emacs-uri-handler \"emacs://$1/${2:-1}\")"
A DOS batch script would look similar, but I don't know how to do default values (though I'm pretty sure you can do it).
See How to configure firefox to run emacsclientw on certain links? for further instructions if you want to integrate with Firefox, too.
To return42's code, added column number support, and cleaned up case where column number is present, and line number is sought.
;; find file at point, jump to line no.
;; ====================================
(require 'ffap)
(defun find-file-at-point-with-line (&optional filename)
"Opens file at point and moves point to line specified next to file name."
(interactive)
(let* ((filename (or filename (if current-prefix-arg (ffap-prompter) (ffap-guesser))))
(line-number
(and (or (looking-at ".* line \\(\[0-9\]+\\)")
(looking-at "[^:]*:\\(\[0-9\]+\\)"))
(string-to-number (match-string-no-properties 1))))
(column-number
(or
(and (looking-at "[^:]*:\[0-9\]+:\\(\[0-9\]+\\)")
(string-to-number (match-string-no-properties 1)))
(let 'column-number 0))))
(message "%s --> %s:%s" filename line-number column-number)
(cond ((ffap-url-p filename)
(let (current-prefix-arg)
(funcall ffap-url-fetcher filename)))
((and line-number
(file-exists-p filename))
(progn (find-file-other-window filename)
;; goto-line is for interactive use
(goto-char (point-min))
(forward-line (1- line-number))
(forward-char column-number)))
((and ffap-pass-wildcards-to-dired
ffap-dired-wildcards
(string-match ffap-dired-wildcards filename))
(funcall ffap-directory-finder filename))
((and ffap-dired-wildcards
(string-match ffap-dired-wildcards filename)
find-file-wildcards
;; Check if it's find-file that supports wildcards arg
(memq ffap-file-finder '(find-file find-alternate-file)))
(funcall ffap-file-finder (expand-file-name filename) t))
((or (not ffap-newfile-prompt)
(file-exists-p filename)
(y-or-n-p "File does not exist, create buffer? "))
(funcall ffap-file-finder
;; expand-file-name fixes "~/~/.emacs" bug sent by CHUCKR.
(expand-file-name filename)))
;; User does not want to find a non-existent file:
((signal 'file-error (list "Opening file buffer"
"no such file or directory"
filename))))))
I made a little rewrite of the find-file-at-point function.
If there is a line number match, the file will be opened within an other window and the courser will be placed in this line. If no line number match, do what ffap normally does ...
;; find file at point, jump to line no.
;; ====================================
(require 'ffap)
(defun find-file-at-point-with-line (&optional filename)
"Opens file at point and moves point to line specified next to file name."
(interactive)
(let* ((filename (or filename (ffap-prompter)))
(line-number
(and (or (looking-at ".* line \\(\[0-9\]+\\)")
(looking-at ".*:\\(\[0-9\]+\\):"))
(string-to-number (match-string-no-properties 1)))))
(message "%s --> %s" filename line-number)
(cond ((ffap-url-p filename)
(let (current-prefix-arg)
(funcall ffap-url-fetcher filename)))
((and line-number
(file-exists-p filename))
(progn (find-file-other-window filename)
(goto-line line-number)))
((and ffap-pass-wildcards-to-dired
ffap-dired-wildcards
(string-match ffap-dired-wildcards filename))
(funcall ffap-directory-finder filename))
((and ffap-dired-wildcards
(string-match ffap-dired-wildcards filename)
find-file-wildcards
;; Check if it's find-file that supports wildcards arg
(memq ffap-file-finder '(find-file find-alternate-file)))
(funcall ffap-file-finder (expand-file-name filename) t))
((or (not ffap-newfile-prompt)
(file-exists-p filename)
(y-or-n-p "File does not exist, create buffer? "))
(funcall ffap-file-finder
;; expand-file-name fixes "~/~/.emacs" bug sent by CHUCKR.
(expand-file-name filename)))
;; User does not want to find a non-existent file:
((signal 'file-error (list "Opening file buffer"
"no such file or directory"
filename))))))
If you have an old version of the ffap (2008) you should update your emacs or apply
an other small patch ...
--- Emacs/lisp/ffap.el
+++ Emacs/lisp/ffap.el
## -1170,7 +1170,7 ## which may actually result in an url rather than a filename."
;; remote, you probably already have a connection.
((and (not abs) (ffap-file-exists-string name)))
;; Try stripping off line numbers; good for compilation/grep output.
- ((and (not abs) (string-match ":[0-9]" name)
+ ((and (string-match ":[0-9]" name)
(ffap-file-exists-string (substring name 0 (match-beginning 0)))))
;; Try stripping off prominent (non-root - #) shell prompts
Here is a zsh function that works if you put it into your .zshrc file.
Since I'm running my code in zsh usually, and this is where i see the errors. Kudos to #sanityinc for the emacs part. Just thought this should be on google.
emn () {
blah=$1
filen=(${(s.:.)blah})
/Applications/Emacs.app/Contents/MacOS/Emacs +$filen[2] $filen[1] &
}
Use like emn /long/stack/error.rb:123
I've modified ivan-andrus defadvice so it works with emacsclient:
(defadvice find-file-noselect (around find-file-noselect-at-line
(filename &optional nowarn rawfile wildcards)
activate)
"Turn files like file.cpp:14 into file.cpp and going to the 14-th line."
(save-match-data
(let* ((matched (string-match "^\\(.*\\):\\([0-9]+\\):?$" filename))
(line-number (and matched
(match-string 2 filename)
(string-to-number (match-string 2 filename))))
(filename (if matched (match-string 1 filename) filename))
(buffer-name ad-do-it))
(when line-number
(with-current-buffer buffer-name
(goto-char (point-min))
(forward-line (1- line-number)))))))
I use this a lot, I made it with command-line-1:
;;;; Open files and goto lines like we see from g++ etc. i.e. file:line#
(defun command-line-1--line-number (orig-fun args)
(setq new-args ())
(dolist (f args)
(setq new-args
(append new-args
(if (string-match "^\\(.*\\):\\([0-9]+\\):?$" f)
(list (concat "+" (match-string 2 f))
(match-string 1 f))
(list f)))))
(apply orig-fun (list new-args)))
(advice-add 'command-line-1 :around #'command-line-1--line-number)

Resources