How to remove the prompt for killing emacsclient buffers?
Asked Answered
R

5

24

After I open something with emacsclient, when I kill that buffer (C-x k) I get a confirmation dialog:

Buffer `blah' still has clients; kill it? (yes or no)

But when I kill buffers opened directly from Emacs I don't. Is there a way not to get them when emacsclient opened them?

Rubella answered 6/11, 2008 at 9:39 Comment(1)
Note that when you use emacsclient to edit a file, Emacs tells you to use C-x # to tell the server when you've finished with the file. If you do that (rather than C-x k), you won't be asked the question.Mccluskey
P
21

This worked for me:

(remove-hook 'kill-buffer-query-functions 'server-kill-buffer-query-function)

There's more information on Using Emacsclient blog entry.

Paramorphism answered 6/11, 2008 at 10:19 Comment(3)
I don't know if it's appropriate for me to upvote this answer, but to the best of my knowledge it's correct, so :-)Oversubtle
This won't work if emacs was started using emacsclient's "ALTERNATE_EDITOR" Behavior... in that case you can do (defun server-remove-kill-buffer-hook () (remove-hook 'kill-buffer-query-functions 'server-kill-buffer-query-function)) (add-hook 'server-visit-hook 'server-remove-kill-buffer-hook)Solberg
I'd be a bit careful with removing the hook. From the file "server.el": Ask before killing a server buffer. It was suggested to release its client instead, but I think that is dangerous -- the client would proceed using whatever is on disk in that file. -- rms.Ciliary
I
22

The other option is to use the -n option with emacsclient so that it doesn't wait for the file to be edited before exiting.

For example:

emacsclient -n myfile.txt
Imperfect answered 6/11, 2008 at 14:26 Comment(2)
Thanks! It has the benefit (for me) over the other solution not to close the frame when buffer is killed, in the case emacsclient created a new frame with the '-c' option.Publus
Note: -n (--no-wait) is not what you want if you have a program waiting for the file to be finished editing.Frear
P
21

This worked for me:

(remove-hook 'kill-buffer-query-functions 'server-kill-buffer-query-function)

There's more information on Using Emacsclient blog entry.

Paramorphism answered 6/11, 2008 at 10:19 Comment(3)
I don't know if it's appropriate for me to upvote this answer, but to the best of my knowledge it's correct, so :-)Oversubtle
This won't work if emacs was started using emacsclient's "ALTERNATE_EDITOR" Behavior... in that case you can do (defun server-remove-kill-buffer-hook () (remove-hook 'kill-buffer-query-functions 'server-kill-buffer-query-function)) (add-hook 'server-visit-hook 'server-remove-kill-buffer-hook)Solberg
I'd be a bit careful with removing the hook. From the file "server.el": Ask before killing a server buffer. It was suggested to release its client instead, but I think that is dangerous -- the client would proceed using whatever is on disk in that file. -- rms.Ciliary
C
1

You may set the keyboard command C-x k so that it marks client buffers as done and kills normal buffers.

I shamelessly stole this code snippet from the Emacs Client entry in the Emacs Wiki:

(add-hook 'server-switch-hook
      (lambda ()
        (when (current-local-map)
          (use-local-map (copy-keymap (current-local-map))))
        (when server-buffer-clients
          (local-set-key (kbd "C-x k") 'server-edit))))

While this does not help with other ways of killing buffers (such as M-x list-buffers), it should be on the safe side by respecting the Emacs client behavior that some shell scripts expect.

Here is an excerpt from the file server.el in your Emacs distribution that might shed a little light on what I mean:

;; When you finish editing a Server buffer, again call server-edit
;; to mark that buffer as done for the client and switch to the next
;; Server buffer.  When all the buffers for a client have been edited
;; and exited with server-edit, the client "editor" will return
;; to the program that invoked it.

Later on, there is an explicit warning that a buffer shouldn't be killed, but released (at least this is how I interpret it):

;; Ask before killing a server buffer.
;; It was suggested to release its client instead,
;; but I think that is dangerous--the client would proceed
;; using whatever is on disk in that file. -- rms.
Ciliary answered 17/1, 2015 at 18:14 Comment(0)
H
0

For whatever reason, I have to manually launch the remove-hook solution on emacs23, perhaps because certain parts of the server are loaded after the .emacs is loaded. Adding a dummy (server-start) line to my .emacs before the (remove-hook ...) did not help. So I have opted for the following, less principled solution:

(defalias 'server-kill-buffer-query-function '(lambda () t))
Hitchcock answered 28/5, 2013 at 0:47 Comment(3)
You should be able to use (eval-after-load "server" '(remove-hook ...)) to handle load order issues (although if the server library had not yet loaded, then your alias should get clobbered once it loads, so perhaps something else was messing with the hook after you had??)Mccluskey
@Mccluskey it seems no using, if I start emacs by emacsclient -t (export ALTERNATE_EDITOR=).Christalchristalle
Hmm, true. I see that (certainly in 24.5) that function is only added during server-start, so it's not there to remove immediately after the library loads. It looks like you could either use after-advice on server-start, or else use server-visit-hook (in which case the code would likely run repeatedly, but that's not a problem in this case).Mccluskey
B
0

If you are using C-x # to exit client to go back to terminal, I have some tricks to avoid prompt:

(defun cxa/yes-never-no (&rest _args)
  "Override `y-or-n-p' to always retrun t."
  t)

(defun cxa/yes-then-restore (orig-fun &rest args)
  "Make `y-or-n-p' always return t before ORIG-FUN, and restore after applied ARGS."
  (advice-add 'y-or-n-p :override 'cxa/yes-never-no)
  (apply orig-fun args)
  (advice-remove 'y-or-n-p 'cxa/yes-never-no))

(advice-add 'server-done :around 'cxa/yes-then-restore)
Battik answered 3/2, 2023 at 10:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.