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.
Related
I want to read the output of a buffer after process started by comint finishes.
(comint-redirect-send-command-to-process
command-string ;; tested to work in the commint buffer
output-buffer-name ;; random text
buffer-process ;; (get-buffer-process (current-buffer))
nil ;; don't echo input back
t) ;; don't show output buffer immediatly
This sexp is evaluated in a buffer running a comint process. I want to read all the text of output-buffer-name once the process has finished.
I have tried applying the solution posted to this question: sleep in emacs lisp by adding this below the process start command:
(defun on-status-change (process status)
(message "status done"))
(set-process-sentinel buffer-process 'on-status-change)
This message does not appear in *Messages*.
There is no prompt in the output-buffer text, but I can write a function that returns t when the output is finished based on the full output text.
How can I react to the buffer finishing/changing, or how can I force comint to run this function synchronously.
The source for comint-redirect-send-command-to-process is here on line 3717
If anyone else has a similar issue, I ended up "solving" this.
(while (not comint-redirect-completed)
(sleep-for 0.01))
comint-redirect-completed’s value is nil
Documentation:
Non-nil if redirection has completed in the current buffer.
Obviously not a great solution, but I couldn't get anything else working.
How to kill an internal process in Emacs? For example I run M-x shell.
I can check running processes with M-x list-processes but how can I kill a process from this list?
There is no default key binding for this; however see pjammer's answer -- list-processes+ includes (among other things) a kill binding on C-k -- and also Joao Tavora's answer -- which provides just a kill binding (for the same key).
event_jr points out in the comments that you can use M-: (kill-process) RET to kill the current buffer's process.
More generally: You can use M-: (kill-process PROCESS) RET, where PROCESS "may be a process, a buffer, or the name of a process or buffer", with those names being as they appear in the output of list-processes. Process names take precedence over buffer names, should you happen to have a conflict; so it's probably best to be in the habit of supplying the process name.
Alternatively, Emacs 23+ has a general system process manager (M-x proced) which is more akin to running top, and which does have a default binding for sending (arbitrary) signals (k). Of course it may be far less obvious in that listing which process you're interested in.
Edit: Better late than never :) The following enables M-x kill-process RET to be used (tested in Emacs 26.1):
;; Enable M-x kill-process (to kill the current buffer's process).
(put 'kill-process 'interactive-form
'(interactive
(let ((proc (get-buffer-process (current-buffer))))
(if (process-live-p proc)
(unless (yes-or-no-p (format "Kill %S? " proc))
(error "Process not killed"))
(error (format "Buffer %s has no process" (buffer-name))))
nil)))
This thread is ancient but here's a very quick hack that works perfectly for me
(define-key process-menu-mode-map (kbd "C-k") 'joaot/delete-process-at-point)
(defun joaot/delete-process-at-point ()
(interactive)
(let ((process (get-text-property (point) 'tabulated-list-id)))
(cond ((and process
(processp process))
(delete-process process)
(revert-buffer))
(t
(error "no process at point!")))))
An alternative way:
You can use M-x eval-expression RET
Then type: (delete-process "<name-of-the-process>") RET
(where "name-of-the-process" was previously obtained from M-x list-processes RET).
Confirm that the process was killed by repeating M-x list-processes RET).
And that's it.
it looks like there is a new mode or add on you can use instead called list process +
If you use counsel you can run M-x counsel-list-processes. You can then type M-o to bring up actions, one of which is kill.
I'm writing an Emacs minor mode that has some Emacs commands which invoke shell commands. I'm using the following code:
(let ((output (get-buffer-create "*Foo Output*")))
(start-process "Foo Process" output argv0)
(display-buffer output))
I'd like the buffer containing those shell commands to automatically scroll to the bottom any time output is inserted, or at the very least when the command is finished executing. How can I do that?
You can do this by using a process filter function.
A process filter function is a function that receives the standard
output from the associated process. If a process has a filter, then
all output from that process is passed to the filter. The process
buffer is used directly for output from the process only when there is
no filter.
[...]
Many filter functions sometimes (or always) insert the output in the
process's buffer, mimicking the actions of Emacs when there is no
filter.
start-process returns a process object that stands for the new subprocess in Lisp which you can store in a variable, say proc. You can write a simple filter function that just inserts the output of the process into the associated output buffer, thereby moving point to the end of the buffer.
(defun my-insertion-filter (proc string)
(when (buffer-live-p (process-buffer proc))
(with-current-buffer (process-buffer proc)
;; Insert the text, advancing the process marker.
(goto-char (process-mark proc))
(insert string)
(set-marker (process-mark proc) (point)))))
Use set-process-filter to assign that filter function to your process.
(set-process-filter proc 'my-insertion-filter)
Alternatively, if it's sufficient to only jump to the end of the buffer once the process has terminated, you might want to use a sentinel.
A process sentinel is a function that is called whenever the
associated process changes status for any reason, including signals
(whether sent by Emacs or caused by the process's own actions) that
terminate, stop, or continue the process. The process sentinel is also
called if the process exits.
(defun my-sentinel (proc event)
(when (buffer-live-p (process-buffer proc))
(with-current-buffer (process-buffer proc)
(end-of-buffer))))
(Note that this function scrolls to the end of the process buffer every time it is called which may happen not only at the end of the process. If you really only want it to do that when the process has terminated, check if event is the string "finished\n".)
Use set-process-sentinel to assign that sentinel to your process.
(set-process-sentinel proc 'my-sentinel)
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.
I'm using EmacsW32 (patched) on windows. Emacs is running in server mode so that subsequent calls to emacsclientw open files in the same server session.
I have C-x C-c mapped to make the current frame invisible and not kill the emacs server process. I'd like clicking the window's X (close) button to also just hide the frame & not terminate the server process as it currently does.
Any ideas ? Thanks!
Sure, I have a method of doing this. There may be refinements possible, but this is a good starting place.
First, I setup a variable and advise the kill-emacs function
(defvar bnb/really-kill-emacs nil)
(defadvice kill-emacs (around bnb/really-exit activate)
"Only kill emacs if the variable is true"
(if bnb/really-kill-emacs
ad-do-it)
(bnb/exit))
The bnb/exit function just makes the frame invisible like what you have bound to C-x C-c.
I then have an additional function to properly exit emacs if that is ever necessary. That will set the variable and call kill-emacs as follows.
(defun bnb/really-kill-emacs ()
(interactive)
(setq bnb/really-kill-emacs t)
(kill-emacs))