tags:

views:

428

answers:

6

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?

+4  A: 

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
I get a Debugger entered--Lisp error: (wrong-number-of-arguments #[nil " <etc etc etc>I'm running GNU Emacs 23.0.60.1 (i386-mingw-nt5.1.2600) of 2008-08-19 on LENNART-69DE564 (patched)
Michael Paulukonis
+6  A: 

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
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...
OK, you're right. I would use server-kill-buffer instead of server-edit though.
jrockway
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
+2  A: 

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
+3  A: 

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
hrm - can't get that to work with the FireFox "It's all Text" plugin. "Can't find path <blah blah -n" -- same results under various permutations with trailing space and enclosing in quotes. BUT the question-at-hand is for batch-files. so.
Michael Paulukonis
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
+1  A: 

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
Oh, nice! Works like a charm, and removes that whole rebinding issue!
Michael Paulukonis
A: 

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