I'm trying to open a background buffer with a comint erlang-shell, and once it's up, run a call a function in emacs (using distel to send it's binaries to the erlang node).
ie:
...
(let ((args (append (list "-sname" node-name "-pa") path)))
(get-buffer-create buffer-name)
(apply #'make-comint-in-buffer node-name buffer-name "erl" nil args)
(erl-check-backend (make-node-name node-name))
...
The problem is that when I call distel, the node is not yet up (epmd has no registered names) so it fails. I'm guessing this is because the inferior process has not had the chance to run yet. Is there any way to wait until the comint-buffer has finished its setup?
I tried accept-process-output on the buffer-process of the buffer sent in as argument to the function above, but that just hung.
Any help appreciated :)
Thomas
in python.el authored by Dave Love the following was used:
(while (progn
(accept-process-output proc 5)
(null python-preoutput-result)))
in python-mode.el the check for an running process is done that way
(or (get-buffer-process (py-buffer-name-prepare pyshellname))
(get-buffer-process
(py-shell nil dedicated pyshellname
switch sepchar py-buffer-name t)))))
i.e. if a Python shell doesn't exist, its start will return the process-symbol.
Related
I want such feature in org-mode: before exiting emacs (while org-mode is running) it asks me: "Do you want to run function vc-dir before exit?"
I tried this:
(add-hook 'kill-emacs-hook 'vc-dir)
But it errors: "wrong number of arguments"
also tried as found here:
(defadvice save-buffers-kill-emacs (before update-mod-flag activate)
(vc-dir))
The same error.
So how to make it work in easy way: vc-dir runs always on exit.
Or how to make it work with warning message (the best way)?
Thanks!
vc-dir takes an argument (the "dir").
So you can do:
(add-hook 'kill-emacs-hook (lambda () (vc-dir "your-dir-here")))
Of course this won't stop emacs from exiting: vc-dir opens a buffer but does not "wait" for user input. For the interactive approach you want you can do this:
(add-hook 'kill-emacs-query-functions
(lambda ()
(if (y-or-n-p "Do you want to run function vc-dir before exit?")
(progn
(vc-dir "your-directory")
nil)
t)))
Change "your-directory" by default-directory if you want to use the last visited buffer as vc-directory.
How about trying that function, which asks for confirmation before running vc-dir:
(defun my-vc-check-onexit ()
(interactive)
(let ((doquit (read-from-minibuffer "Do you want to run vcs? ")))
(if (string-equal doquit "y") (vc-dir "~/my/dir"))
))
and bound it to the hook.
note: it may not be good elisp ;)
I'd like to have a function that asks for a number n and executes the default compile command n-times afterwards. That is to say unlike C-c C-c (i.e. TeX-command-master) I don't want to be asked which command to run, it should select the default compile command based on the AUCTeX settings. Naturally if any error occurs the execution should stop.
I know about TeX-texify, however, this doesn't statisfy my needs because sometimes I just want emacs to run pdflatex five times indepent of what the AUCTeX parser thinks is adequate.
Any help is much appreciated!
Edit: I have looked into this a little further and using code from the above reference I have started writing a function that does this. However, it has one major flaw. Let me first give you the code:
(defcustom TeX-MultiTeX-Command "LaTeX" "Default MultiTeX command" :type 'string :group 'TeX-command)
(defun TeX-MultiTeX (n)
"Run TeX-command n-times"
(interactive "nRun TeX/LaTeX how many times: ")
(while (> n 0)
(TeX-command TeX-MultiTeX-Command 'TeX-master-file)
(setq n (- n 1))))
As you can see, I have implemented a config variable for selecting the correct compilation command. Now let me present the problem:
The compilation of the LaTeX document takes some time, however, my function instantly calls the second (and following) executions of the compile command. Maybe someone can provide help in finding a solution that checks whether compilation has finished successfully prior to executing (TeX-command TeX-MultiTeX-Command 'TeX-master-file), then executes said function or prints some error message if compilation finished with an error.
With the help of the code of the TeX-texify function I have developed a function that does what I want, the code is given below.
I'd like to thank user4815162342; although this solution is not based on his suggestion, I think his solution might be of use for a different problem. Also I'd like to thank TN, the author of TeX-texify, I shamelessly took and adapted his code for my problem. ;)
(defcustom TeX-MultiTeX-Command "LaTeX"
"Default MultiTeX command"
:type 'string :group 'TeX-command)
(defun TeX-MultiTeX-sentinel (&optional proc sentinel)
"Non-interactive! Call the standard-sentinel of the current LaTeX-process.
If there is still something left do do start the next latex-command."
(set-buffer (process-buffer proc))
(funcall TeX-MultiTeX-sentinel proc sentinel)
(let ((case-fold-search nil))
(when (string-match "\\(finished\\|exited\\)" sentinel)
(set-buffer TeX-command-buffer)
(unless (plist-get TeX-error-report-switches (intern (TeX-master-file)))
(TeX-MultiTeX TeX-MultiTeX-num-left)))))
(defun TeX-MultiTeX (n)
"Run TeX-command n-times"
(interactive "nRun TeX/LaTeX how many times: ")
(when (or (called-interactively-p 'any)
(null (boundp 'TeX-MultiTeX-num-left)))
(setq TeX-MultiTeX-num-left n))
(if (>= TeX-MultiTeX-num-left 1)
(progn
(TeX-command TeX-MultiTeX-Command 'TeX-master-file)
(setq TeX-MultiTeX-num-left (- TeX-MultiTeX-num-left 1))
(setq proc (get-buffer-process (current-buffer)))
(setq TeX-MultiTeX-sentinel (process-sentinel proc))
(set-process-sentinel proc 'TeX-MultiTeX-sentinel))))
It seems that you need a synchronous way to run TeX-command. I haven't word with TeX-command, but if it uses the compilation API, it can be made to wait for the compilation to finish, although it's not exactly obvious how to do that. Here is an example that uses compilation-finish-functions to achieve the desired effect:
(require 'cl) ; for lexical-let
(defun compile-and-wait (compilefun)
(interactive)
(lexical-let ((done nil) finish-callback)
(setq finish-callback
;; when the compilation is done, remove the callback from
;; compilation-finish-functions and interrupt the wait
(lambda (buf msg)
(setq compilation-finish-functions
(delq finish-callback compilation-finish-functions))
(setq done t)))
(push finish-callback compilation-finish-functions)
(funcall compilefun)
(while (not done)
(sleep-for .1))))
EDIT
AUC TeX is not using compilation mode to spawn TeX, so the above cannot work. Since it's still useful for other compilation buffers, I'm leaving it in the answer. Another way to implement TeX-MultiTeX is by binding TeX-process-asynchronous to nil, which should ensure that AUC TeX waits for the command to finish.
My ~/.emacs contains the following settings for opening certain files with certain applications (Ubuntu 12.10; Emacs 24):
(setq dired-guess-shell-alist-user
'(("\\.pdf\\'" "okular ? &")
("\\.djvu\\'" "okular ? &")
("\\.mp3\\'" "vlc ? &")
("\\.mp4\\'" "vlc ? &")
))
When I navigate to a .pdf in dired-mode and hit !, it opens the .pdf in Okular, but the dired-buffer is split into two parts, the second one now being a useless *Async Shell Command* buffer containing content like
okular(25393)/kdecore (KConfigSkeleton) KCoreConfigSkeleton::writeConfig:
okular(25393)/kdecore (KConfigSkeleton) KCoreConfigSkeleton::writeConfig:
okular(25393)/kdecore (KConfigSkeleton) KCoreConfigSkeleton::writeConfig:
okular(25393)/kdecore (KConfigSkeleton) KCoreConfigSkeleton::writeConfig:
How can I prevent this buffer from being opened? (except for, maybe, if there was an error and this information is useful).
I found related questions here and here, but they seem to deal with specific commands executed asynchronously, instead of the *Async Shell Command* in general (if possible, I would like to change the behaviour in general for asynchronous processes, not only for certain file types)
Found this here:
(call-process-shell-command "okular&" nil 0)
Works for me. No stderr gobbledygook.
The question was asked in 2012, and at the time of my writing, the most recent answer is dated 2015. Now, in 2017, I can say that the answer is simple:
(add-to-list 'display-buffer-alist
(cons "\\*Async Shell Command\\*.*" (cons #'display-buffer-no-window nil)))
I am piggybacking off of user1404316's answer, but here is another generic way to achieve the desired outcome.
(defun async-shell-command-no-window
(command)
(interactive)
(let
((display-buffer-alist
(list
(cons
"\\*Async Shell Command\\*.*"
(cons #'display-buffer-no-window nil)))))
(async-shell-command
command)))
I'm not entirely sure about doing it for asynchronous processes in general, but for anything that goes through async-shell-command, this should work:
(defadvice async-shell-command (around hide-async-windows activate)
(save-window-excursion
ad-do-it))
Sadly there is no good way to avoid this buffer as it's called directly by 'shell-command' function ('async-shell-command' is just a wrapper).
So, a much better way is to replace 'async-shell-command' with 'start-process'.
You should start process with 'set-process-sentinel' to detect the moment when process emits 'exit signal. Then kill process.
A slightly more complicated incantation should get you what you want. Just use a shell command like: (okular ? >& /dev/null &).
I haven't tested this with okular, but I can do M-! ((echo foo; sleep 10; echo bar) >& /dev/null &) and Emacs returns immediately without creating a new buffer.
I solved the problem, using this method:
;list of programs, corresponding to extensions
(setq alist-programs
'(("pdf" ."okular")
("djvu" . "okular")
("mp3" . "xmms")))
(defun my-run-async-command (command file)
"Run a command COMMAND on the file asynchronously.
No buffers are created"
(interactive
(let ((file (car (dired-get-marked-files t current-prefix-arg))))
(list
;last element of alist-programs, contains COMMAND
(cdr
(assoc
(file-name-extension file)
alist-programs))
file)))
;begin of function body
(if command ;command if not nil?
(start-process "command" nil command file)
)
)
;attach function to <f2> key
(add-hook 'dired-mode-hook
(lambda ()
(define-key dired-mode-map (kbd "<f2>") 'my-run-async-command)))
The suggestions to use start-process are ok if he is running a distinct program on the path of course. But if you want run some shell command in a specific directory (eg your project directory) then simply quell the popup - you frequently want the buffer but just dont want it jumping up in your face. eg I run a webserver and I want to see the output, just not now...
(use-package php-mode
:config
(add-to-list 'display-buffer-alist
(cons "\\*Symfony Web Server\\*.*" (cons #'display-buffer-no-window nil)))
(defun php-mode-webserver-hook ()
(if (projectile-project-root) (let ((default-directory (projectile-project-root)))
(unless (get-buffer "*Symfony Web Server*" )
(async-shell-command "bin/console server:run" "*Symfony Web Server*")))))
:hook (php-mode . php-mode-webserver-hook))
At this moment in time, I am using nrepl primarily to talk to Clojurescript apps. I like to use nrepl from within emacs. I start nrepl by typing M-x nrepl-jack-in.
Unfortunately, my nrepl session often gets completely hung. When this happens, I dutifully kill the 3 buffers related to nrepl. These buffers are:
*nrepl*
*nrepl-connection*
*nrepl-server*
*nrepl-server* also has an active process, it ask me if I want to close it, and I say yes.
I then type M-x nrepl-jack-in again.
This is a pain.
I would like to overload nrepl-jack-in so that it automatically checks if any of these 3 buffers exist. If any of them do exist, it will kill these buffers and any active processes associated with these bufers. After doing this, the overloaded nrepl-jack-in will proceed as usual. I would like this because then, whenever I detect that nrepl has decided to hang itself again, I could just type M-X nrepl-jack-in and restart what I was doing.
This should get the job done:
(defun my-nrepl-jack-in ()
(interactive)
(dolist (buffer (buffer-list))
(when (string-prefix-p "*nrepl" (buffer-name buffer))
(kill-buffer buffer)))
(nrepl-jack-in nil))
The chosen answer didn't quite work for me... The nrepl process sentinel threw an error, preventing it from restarting. I played with it a bit and came up with the following (which also gives a separate kill-nrepl function)
;; Disable prompt on killing buffer with a process
(setq kill-buffer-query-functions
(remq 'process-kill-buffer-query-function
kill-buffer-query-functions))
(defun nrepl-kill ()
"Kill all nrepl buffers and processes"
(interactive)
(when (get-process "nrepl-server")
(set-process-sentinel (get-process "nrepl-server")
(lambda (proc evt) t)))
(dolist (buffer (buffer-list))
(when (string-prefix-p "*nrepl" (buffer-name buffer))
(kill-buffer buffer))))
(defun nrepl-me ()
(interactive)
(nrepl-kill)
(nrepl-jack-in nil))
I'm currently running an asynchronous emacs command with a fair degree of regularity like this:
(save-window-excursion
(async-shell-command
cmd
(generate-new-buffer "async")))
This works well and all, but it clutters up my emacs instance with a whole ton of async<5> and async<11> buffers. How can I automatically kill these buffers when their corresponding asynchronous command finishes executing?
While it won't kill them when the command completes, you can have the buffers killed after a period of time - this assumes that the async commands are shotr-lived ( or have a fairly-known runtime). Something like:
(save-window-excursion
(let ((buf (generate-new-buffer "async")))
(async-shell-command cmd buf)
(run-with-timer 10 nil (lambda (buf) (kill-buffer buf)) buf)))
Please have a look at
http://news.gmane.org/find-root.php?message_id=%3cloom.20120517T145957%2d51%40post.gmane.org%3e
The second proposal there starts a sentinel along with the shell process.
When this sentinel detects the process status 'exit you can kill the process buffer right away or you can start dying-mode for the process buffer as it is proposed in the cited posting.
Within the dying time you can inspect the process output, cancel dying or prolong the life-time of the buffer.
Best regards,
Tobias
I'm assuming that (for this particular use-case) you are rarely interested in looking at the output that is put in the "async" buffer and that you just want to prevent the creation of the extraneous buffers. If so, you could do:
(save-window-excursion
(when (get-buffer "async")
(kill-buffer "async"))
(async-shell-command
cmd
(generate-new-buffer "async")))
This will kill the "async" buffer prior to running the "async-shell-command" and thus prevent the additional "async" buffers from being created.