How to do use C-x k to kill an Emacs buffer opened in server mode? - emacs

I use windows batch file to open files in an already-running instance of Emacs using emacsclientw.exe. However, any file opened that way is opened in server mode, which means that I have to use C-x # to kill it, instead of the usual C-x k. How do I change this behavior?

My solution was to rebind it (well M-w actually) to:
(lambda ()
(interactive)
(if server-buffer-clients
(server-edit)
(kill-this-buffer)))
[edit: having now read the code for server-edit, it might be better to use server-done (depending on what you want). server-edit will switch you to a server-edited buffer (if any still exist) but server-done will simply switch you to the next buffer. You could also use the output of server-done to see if the buffer was actually killed (not the case if the file was open before invoking emacsclient) and then kill it if not. Or use server-kill-buffer as suggested elsewhere.]

Here is what i put in my .emacs to do this :
(add-hook 'server-switch-hook
(lambda ()
(local-set-key (kbd "C-x k") '(lambda ()
(interactive)
(if server-buffer-clients
(server-edit)
(ido-kill-buffer))))))
Like this C-x k work the usual way when i'm not finding a file from emacsclient (which for me is ido-kill-buffer), and if i'm using emacsclient, C-x k does server-edit if the client is waiting, or run ido-kill-buffer otherwise (if i used emacsclient -n).

use:
D:\> emacsclientw -n foo.txt
the -n says "no wait". This is in GNU Emacs 22.2.1 (i386-mingw-nt5.1.2600) of 2008-03-26 on RELEASE (and many prior versions, IIRC).

You know, I hate to suggest workarounds instead of a real solution... but after reading the server code, I am confused as to how emacs even determines that a buffer is a server buffer.
With that in mind, why not open up files like:
emacsclient --eval '(find-file "/path/to/file")'
This way emacs doesn't know that your buffer was opened via emacsclient, which sounds like what you really want.
Edit:
I'm not really happy with this, but it seems to work:
(global-set-key (kbd "C-x k") (lambda () (interactive) (server-kill-buffer (current-buffer))))

Okay, THIS did work for me:
(global-set-key (kbd "C-x k") '(lambda ()
(interactive)
(if server-buffer-clients
(server-done)
(kill-this-buffer))))
(this is the code from IvanAndrus's answer with the explicit changes from edits and comments, and using jrockway's keybinding.)
And, yes -- I am rebinding a standard keybinding. BUT it's a functional replacement NOT something completely different (eg, replacing kill-ring-save with kill-buffer stuff).
BTW, EmacsWiki has a couple of pages on this topic-- KillKey and KillingBuffer -- neither of which shed better light for me than the above (although the first-function on KillKey also used "server-edit"...).

I am not sure if that will work on windows, but on linux, emacs -daemon is just great.
With it, you don't have to run a different program, and your bindings are the same. I guess there are other advantages, but since I could never learn those emacsclient bindings, I never really used it, and can't say.
I don't think -daemon had been released yet, I am using 23.0.60.1 from CVS.

Related

Strange Emacs behavior (produces garbage on the screen)

I'm trying to learn Emacs and eLisp by writing some simple macros. Here is one of them:
(global-set-key (kbd "C-c d") 'local-delete-line)
(defun local-delete-line ()
"deletes 1 line"
(interactive)
(beginning-of-line)
(set-mark-command)
(next-line)
(delete-region))
Unfortunately, after triggering C-c d (or any other hotkey that I set up by global-set-key), Emacs responds with this:
Any ideas what can cause this?
To troubleshoot, I've removed my whole .emacs file, created an empty one, and I've put only the definition of local-delete-line function, together with global-set-key command. Emacs still produces garbage when invoking the function.
Start by reading the doc of set-mark-command (C-h f set-mark-command). See what it says about not setting the mark in code you write. See how many arguments it requires.
Read the doc of delete-region: it requires two arguments.
Do M-: (setq debug-on-error t), and then try your recipe. The backtrace buffer will tell you what you have done wrong.
State what it is that you are trying to do. So far, it seems like you just want to delete or kill a line. If so, what's wrong with C-k?

Prevent Emacs from exiting once the exit procedure has initiated?

Is there a way to prevent Emacs from exiting once I initiate the exit process?
I occasionally fat finger C-xC-s as C-xC-c. It isn't an awful process to get back up and running but I am curious if there is a way I can stop the exit process so that I can continue uninterrupted with all my files open.
Using GNU Emacs 24.3.1. Running on Cygwin under Window 7.
There is a built-in variable you can set to a function like so:
(setq confirm-kill-emacs 'y-or-n-p)
scottfrazer's answer's the more appropriate, to me, than what follows.
Enable Emacs Lock minor mode (emacs-lock-mode) on any of the buffers, to prevent Emacs from exiting in case you accidentally hit C-xC-c.
From the Emacs Wiki page:
Emacs cannot exit until the buffer is killed or unlocked
Add (emacs-lock-mode) to your .emacs/init.el file so that this lock is enabled in every Emacs session. Adding this will lock the *scratch* buffer which will have to be unlocked in case you really want to exit Emacs.
Another way/hack of doing this is to start a process in Emacs e.g. M-xshell or have an unsaved file associated to a buffer, doing this will prompt you for confirmations when Emacs is exiting.
Yes one more, unset C-xC-c using global-unset-key. And then if you want to exit Emacs M-xkill-emacs.
Using confirm-kill-emacs, as #scottfrazer suggested, is one approach.
More generally, you can use kill-emacs-query-functions to do whatever you want in this regard. (There was no real need for them to add confirm-kill-emacs, but they did.)
You probably do not want to use kill-emacs-hook in this regard (that's what kill-emacs-query-functions is for), but be aware of it, in case you come across it using apropos etc.
One advantage of kill-emacs-query-functions over justconfirm-kill-emacs is that you can require a better confirmation: yes instead of just hitting key y. For example:
(add-hook 'kill-emacs-query-functions
(lambda () (y-or-n-p "Do you really want to exit Emacs? "))
'append)
That is what I do. It is too easy to be hitting keys and accidentally hit C-x C-c y, especially since I have similar keys bound (e.g., C-x c, C-x C-x, C-x C-y).
If you're looking for a shorter answer, I've had this line at the bottom of all my .emacs files since the last century:
(shell)
I've added the following to my emacs configuration to prevent accidental closes. I didn't like having to confirm close emacs for something like a one off commit, but I hate losing my emacs session accidentally while deep in a problem.
This adds a global state flag to emacs describing whether or not it's locked. This flag is set either automatically after emacs is open for 5 minutes, or manually using the lock-emacs command. The lock can later be removed manually by using the unlock-emacs command.
If emacs is locked, and you attempt to close it (presumably accidentally), emacs will instead give you a message saying that emacs has been locked, and cannot be closed. If it's unlocked, close behaves exactly as it does by default.
;; don't close emacs on accident
(setq emacs-locked nil)
(setq confirm-kill-emacs
(lambda (&rest args)
(if emacs-locked
(progn
(message "%s" "Emacs is locked, and cannot be closed.")
nil)
t)
))
(defun lock-emacs-silently ()
(progn
(setq emacs-locked t))
)
(defun lock-emacs ()
"Prevent emacs from being closed."
(interactive)
(progn
(lock-emacs-silently)
(message "%s" "Emacs is now locked."))
)
(defun unlock-emacs ()
"Allow emacs to be closed."
(interactive)
(progn
(setq emacs-locked 'nil)
(message "%s" "Emacs can now be closed."))
)
(run-at-time "5 minutes" nil 'lock-emacs-silently)
(Open to suggestions on how to make the confirm-kill-emacs portion nicer, I'm a lisp novice :) ).
After using this for a couple of years, I ended up going to something much simpler:
;; Unbind the normal close
(global-unset-key (kbd "C-x C-c"))
;; Require C-c 3 times before closing
(global-set-key (kbd "C-x C-c C-c C-c") 'save-buffers-kill-terminal)

function similar to other-buffer but with filtering

I'm looking for a way to include some filtering in the other-buffer method in emacs.
Currently calling other-buffer pulls up the last most recent buffer, but the problem with this is that buffers that get modified by external processes keep coming up as other-buffer. I would like to implement some sort of filtering in other-buffer.
Currently I use evil with C-^ bound to other-buffer, and I have some tail.el buffers active, and when I try to switch bufffers the tail buffers keep popping up.
Is there some alternative to other-buffer or could someone scratch up some code to implement this, Thanks.
What has worked for me is winner-mode - it's like an undo, but for window configurations.
Here's my setup:
(winner-mode)
(global-set-key (kbd "<f7>") 'winner-undo)
(global-set-key (kbd "C-<f7>") 'winner-redo)
Also I'd recommend other-window on some very cheap shortcut, since it's
a command that's used a lot.
I've put it on C-p, since I didn't appreciate the inconsistency
that one of the direction keys is so far away from others.
I've got previous-line on C-h instead, so now
my direction keys are n h f b - they're almost together!
And I didn't really miss the defaults on C-h, since f1
has the same functionality.
Ok so I got some workable solution but its not perfect it using bits from this answer:
emacs lisp, how to get buffer major mode?
(defun buffer-mode (buffer-or-string)
"Returns the major mode associated with a buffer."
(with-current-buffer buffer-or-string (format "%s" major-mode)))
(defun other-buffer-ex ()
(interactive)
(switch-to-buffer
(if (string-equal (buffer-mode (other-buffer)) "comint-mode")
(next-buffer) (other-buffer))))

How can I set up two, parallel buffers in emacs to edit Python files in one and execute in an IPython shell in the other?

I'm trying to setup ipython.el in emacs23. I've successfully installed it (after putting python-mode.el in my load-path to supplant python.el which comes pre-installed with emacs). And I can even get it to run via M-x py-shell, etc.
The interface seems to be pretty poorly setup, and I was wondering if I was doing it wrong, or if I need to customize it to make it work the way I'd like.
In short, the workflow I'd like to have:
in one or more buffers, edit Python code
When I hit C-c C-c in that buffer, either execute the Python code in that buffer in the open IPython shell buffer (if there is one) or open up another buffer to do that.
But what happens right now is:
With the IPython shell in one buffer and the Python file in the other, if I hit C-c C-c in the Python file buffer, the file buffer switches to the IPython buffer (meaning I now have two, duplicated iPython buffers) and the file is executed.
This is annoying.
I'm pretty new to elisp, but my understanding of defadvice is that I could advise around python-execute-buffer to take note of the existing file buffer, run python-execute-buffer, and then switch back to the original file buffer as a workaround.
This seems pretty silly. Any suggestions for better ways to accomplish this would be appreciated!
If it matters: I'm on OS X 10.6.8 with IPython 0.10.1 running Emacs 24.0.50.
Thanks in advance!
Turns out that simply installing python-mode.el and anything-ipython.el and putting
(require 'python-mode)
(require 'ipython)
(require 'anything-ipython)
(add-hook 'python-mode-hook #'(lambda ()
(define-key py-mode-map (kbd "C-<tab>") 'anything-ipython-complete)))
(add-hook 'ipython-shell-hook #'(lambda ()
(define-key py-mode-map (kbd "C-<tab>")
'anything-ipython-complete)))
in my .emacs made everything work just how I wanted if py-shell is executed before py-execute-buffer (so that C-c C-c) executes the code in the shell instead of just opening up the *Python Output* buffer.

emacs ( recompile -y )

Is it possible to pass a "-yes" flag to the 'recompile' command in emacs?
Excuse my complete lack of (e)lisp know-how. I got sick of going outside Emacs to compile my latex code, so i added the following key binding to my .emacs:
(global-set-key (kbd "<f12>") 'recompile);
Is it possible to automatically answer 'yes' to the following prompt that might appear:
"A compilation process is running; kill it? (yes or no)."
Also, is it possible to make the window that opens and shows the output to scroll to the bottom automatically. The interesting stuff is typically down there. Maybe its possible to chain the following command after recompile: "C-x o, end-of-buffer".
Thanks!
Here's some code to solve your first problem (interrupting the current compilation):
(defun interrupt-and-recompile ()
"Interrupt old compilation, if any, and recompile."
(interactive)
(ignore-errors (kill-compilation))
(recompile))
For your second problem (scrolling the compilation output), just customize the user setting compilation-scroll-output.
This behaviour is governed by the compilation-always-kill global variable. Customize it via customize-variable and set it to t.
Not sure which version of emacs first had this, but 26 and newer certainly does.
I somehow need to put kill-compilation into a ignore-errors with Emacs 23.2 to get it to work when no process is running. Otherwise works great.
(defun interrupt-and-recompile ()
"Interrupt old compilation, if any, and recompile."
(interactive)
(ignore-errors
(kill-compilation))
(recompile)
)
Whenever I tried using kill-compilation with latex/pdflatex it did not work. I assume it is because latex does not respond to SIGINT.
Instead I am using the following hack, which first sets the process-kill-without-query bit of the compilation-buffer and then closes it (which kills the running process).
(defun interrupt-and-recompile ()
"Interrupt old compilation, if any, and recompile."
(interactive)
(ignore-errors
(process-kill-without-query
(get-buffer-process
(get-buffer "*compilation*"))))
(ignore-errors
(kill-buffer "*compilation*"))
(recompile)
)
The other solutions didn't work for me for the same reason as sfeuz, but I didn't like the nuclear option of killing the hardcoded buffer by name.
Here's a short solution that autoanswers yes to that specific question by advising yes-or-no-p:
ftp://download.tuxfamily.org/user42/compilation-always-kill.el
(source: http://www.emacswiki.org/CompilationMode)