I use Python in Emacs, with Elpy and iPython. I run code from a file with C-c C-c, but sometimes I would like to terminate the process (keyboard interrupt). I use the same shortcut to try to terminate the process (as per this question).
My goal would be to try to distinguish between killing the whole Python process or just interrupting the kernel. With the above shortcut, it sometimes does the first and sometimes does the second. I am yet to find any clues, how to generalize it.
So, the question is: How to stop the Python process and how to interrupt the kernel?
I would just define a function to send an interrupt to the process. I use anaconda-mode not elpy, so I am not sure what elpy uses to get the associated process-buffer, but python-mode has python-shell-get-process-or-error. Then you could bind the key C-c C-a, for example, in the python-mode-map to the following function.
(defun my-interrupt ()
"Send an interrupt signal to python process"
(interactive)
(let ((proc (ignore-errors
(python-shell-get-process-or-error))))
(when proc
(interrupt-process proc))))
Related
When using M-x list-processes I get a list of processes.
Is there a way to kill them interactively ? For instance by selecting one in the list, then pressing q or something similar ?
In case it matters, I am using emacs 24.5.1 on osx with emacs prelude.
Note : it is different from this question I want to do it interactively, not from the mini-buffer (as understood by #legoscia already).
In Emacs 25, you can do what you'd expect: in the process list, hit d to "delete" the process under point.
For earlier Emacs versions, I've been using this little function that works using the minibuffer, not the process list:
(defun delete-process-i(p)
(interactive `(,(completing-read"Kill proc: "(mapcar 'process-name(process-list))()t)))
(delete-process p))
After defining it, you can type M-x delete-process-i, and type the name of the process you want to kill, with tab completion.
(I originally wrote it to fit in 140 characters; thus the non-standard layout.)
I have installed Emacs under Windows 7 and want to use it in my everyday work. Unfortunately Emacs world and other text editors world are completely different and I am getting stuck on every third sequence of keys pressed on keyboard - it's doing something that I don't expect it would do.
I want to make a panic command - when I press ESC ESC ESC it stops doing everything, quitting from minibuffer, stops entering command, unhighlight regexps, etc. It already does what I want, except it killing buffers, the layout of my workspace. So I modified keyboard-escape-quit function in simple.el file (found it by C-h k ESC ESC ESC)
(defun keyboard-escape-quit ()
"Exit the current \"mode\" (in a generalized sense of the word).
This command can exit an interactive command such as `query-replace',
can clear out a prefix argument or a region,
can get out of the minibuffer or other recursive edit,
cancel the use of the current buffer (for special-purpose buffers),
or go back to just one window (by deleting all but the selected window)."
(interactive)
; Stop highlighting regexp
(unhighlight-regexp)
(cond ((eq last-command 'mode-exited) nil)
((region-active-p)
(deactivate-mark))
((> (minibuffer-depth) 0)
(abort-recursive-edit))
(current-prefix-arg
nil)
((> (recursion-depth) 0)
(exit-recursive-edit))
(buffer-quit-function
(funcall buffer-quit-function))
;((not (one-window-p t))
; (delete-other-windows))
((string-match "^ \\*" (buffer-name (current-buffer)))
(bury-buffer))))
I have byte-compiled and loaded this file and it works ok. But I can't figure out why it is not loading at startup.
You cannot modify some special built-in libraries, including simple.el. Emacs never actually loads these special libraries from their source or byte code files. Their byte code is directly included in the Emacs executable at build time, by a process called “dumping”. Emacs loads these libraries from its own binary.
Generally, should not modify any built-in libraries anyway. Your risk breakage, and your customizations are lost when you update Emacs.
Instead, do what you are supposed to do: Add custom functions to your init.el.
Hence, instead of modifying the built-in keyboard-escape-quit, create your own function, e.g. my-emergency-quit, in your init.el, and bind it to a global key, e.g. C-c q, with
(global-set-key (kbd "C-c q") #'my-emergency-quit)
Some final words of advice: I do not think that such a panic command does any good. The first rule of Emacs is: Don't panic. If you are stuck, don't try to quit and kill everything. Rather, try to find out why you are stuck, and how to get “un-stuck” by normal means. You'll learn Emacs better this way, imho.
Is it possible to configure emacs term to send everything (maybe exception M-x) as raw commands. This will allow for instance run emacs -nw inside terminal and every command will work for emacs inside terminal now the one outside.
I want something like this because I sometimes run nano from terminal or screen, I also use ssh and this will alow me to run emacs on the server. Right now when I run nano I need to call C-c x that send C-x.
I'd first suggest using tramp to edit remote files. I prefer it to opening an editor on the remote machine. If you try to run emacs inside a term-mode buffer, you're going to be fighting it all the time.
If you must run emacs inside a term-mode buffer, you can use term-send-raw and term-send-raw-string. For example:
(defun term-send-backward-word ()
"Move backward word in term mode."
(interactive)
(term-send-raw-string "\eb"))
<Escape> b is what the terminal (which is eterm-color) expects when you press C-<left>. This is not necessarily the same as binding C-<left> to term-send-raw. The best thing to do is probably to try binding whatever key to term-send-raw, and if that doesn't work, make a function with term-send-raw-string and bind that. You can figure out what the string should be if you have a shell in the term-mode buffer, send a quote, and then type the key. You can send a quote with
(defun term-send-quote ()
"Quote the next character in term-mode.
Similar to how `quoted-insert' works in a regular buffer."
(interactive)
(term-send-raw-string "\C-v"))
It's just like typing C-v in a normal terminal.
Finally, I'll mention multi-term. It's available in melpa. It provides the functions I listed above, and has better defaults than term-mode IMO. But it's probably further from what you want, because it tries to integrate term-mode with the rest of emacs instead of just passing things through.
I am trying to stop the simple-httpd server with its command httpd-stop when I type the C-x C-c command (save-buffers-kill-terminal to quit emacs), cause I would like to get rid of the "Active processes exist; kill them and exit anyway? (yes or no)" message every time I type C-x C-c.
To achieve my aim, I was thinking about writing a hook like:
(add-hook 'save-buffers-kill-terminal
(lambda () (httpd-stop)
))
But it did not work. I continue to get the message "Active processes exist; kill them and exit anyway? (yes or no)" when I type C-x C-c.
How can I achieve my goal?
Thanks!
There is no hook named save-buffers-kill-terminal.
There's a function by that name, but hooks don't automatically exist for every function (the advice mechanism does facilitate that in effect, but you don't need that here (edit: or maybe you do; see below)).
A hook only exists if there is code which explicitly runs it with run-hooks, or similar (e.g. C-u C-h a run.*hook RET). Or more specifically, you can add-hook with any arbitrary variable, but it will never function as a hook unless something runs it.
In general you would use either of kill-emacs-hook (if the callback does not need to interact with the user), or kill-emacs-query-functions if interaction could be necessary. See the help of each of those variables for details.
Edit:
In the case of active processes, a different mechanism is needed, as these queries happen before either of those hooks are run.
If you just want to avoid the query and let the process be killed, you can use set-process-query-on-exit-flag. This sets a per-process flag which determines whether or not this query will happen:
set-process-query-on-exit-flag is a built-in function in `process.c'.
(set-process-query-on-exit-flag PROCESS FLAG)
Specify if query is needed for PROCESS when Emacs is exited.
If the second argument FLAG is non-nil, Emacs will query the user before
exiting or killing a buffer if PROCESS is running. This function
returns FLAG.
You would probably arrange this when you started the process (assuming you have some code which starts httpd), obtaining PROCESS with either (get-process NAME) or (get-buffer-process BUFFER). I'm not using this httpd library, so I don't know specifically what you'll need here.
If you definitely want to call a graceful shut-down function like httpd-stop, I think you'll need to do something custom.
(defadvice save-buffers-kill-emacs (before my-httpd-auto-stop)
(when (httpd-is-running-p) ;; not a real predicate
(httpd-stop)))
(ad-activate 'save-buffers-kill-emacs)
You'll have to figure out what to use in place of my hypothetical httpd-is-running-p, to establish whether or not to call the stop function.
Note that this would run before any other queries, so you might stop httpd this way but then decide not to kill emacs after all (but that sort of thing is possible regardless).
Edit: Alternatively, as it is apparently safe to call this stop function whether or not httpd is running, you could use the following (including an added test to see whether or not httpd-stop is defined, as calling it will be an error otherwise, and I'm not sure how you load the library):
(defadvice save-buffers-kill-emacs (before my-httpd-auto-stop)
(when (fboundp 'httpd-stop)
(httpd-stop)))
(ad-activate 'save-buffers-kill-emacs)
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.