5

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?

James Sulak
  • 31,389
  • 11
  • 53
  • 57
  • 1
    This is a duplicate of http://stackoverflow.com/questions/268088/, which has more answers. – Glenn Nov 25 '09 at 17:09

6 Answers6

7

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.]

  • This will break things horribly. `server-edit` will switch to a server buffer if you are not currently in one, which means you can only kill buffers if you've killed all server buffers. (Also, M-w is a pretty important binding, you never use it?) – jrockway Jan 13 '09 at 03:57
  • server-buffer-clients is only non-nil if I'm editing if I'm already in a buffer being waited on by a client, unless the documentation is wrong. And no, I don't use kill-ring-save bound to M-w. I must admit I probably rebind too many keys, but... –  Jan 13 '09 at 04:34
  • OK, you're right. I would use server-kill-buffer instead of server-edit though. – jrockway Jan 13 '09 at 07:47
  • I stopped using CUA mode early on -- M-w, FTW. \n using (server-kill-buffer) in the above function (bound to (kbd "C-x k") requires me to enter C-x k TWICE. ??? – Michael Paulukonis Jan 13 '09 at 16:34
7

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).

p4bl0
  • 3,846
  • 1
  • 22
  • 21
5

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).

Joe Casadonte
  • 15,888
  • 11
  • 45
  • 57
  • hrm - can't get that to work with the FireFox "It's all Text" plugin. "Can't find path – Michael Paulukonis Jan 14 '09 at 18:49
  • I use It's All Text, too, and just have D:\product\emacs\bin\emacsclientw.exe in it. I don't have a problem killing buffers visited using IAT. I can't remember ever changing kill-buffer to accomodate this (and a quick check through my .emacs confirms this). – Joe Casadonte Jan 15 '09 at 13:29
4

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))))
jrockway
  • 42,082
  • 9
  • 61
  • 86
  • I get a Debugger entered--Lisp error: (wrong-number-of-arguments #[nil " I'm running GNU Emacs 23.0.60.1 (i386-mingw-nt5.1.2600) of 2008-08-19 on LENNART-69DE564 (patched) – Michael Paulukonis Jan 13 '09 at 16:25
2

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"...).

Michael Paulukonis
  • 9,020
  • 5
  • 48
  • 68
0

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.

elmarco
  • 31,633
  • 21
  • 64
  • 68