Emacs - emacsclient or new frame?
Asked Answered
L

2

10

this is a rather rudimentary question, but what is the practical difference between opening a new file in a separate frame (make-new-frame) from emacs or opening the file in an instance of emacsclient? I can see that if you are working through a terminal, the difference is clear... but can emacsclient additionally restrict the list of buffers accessed by (buffer-menu) or ido-mode to buffers opened in that particular emacsclient instance?

Liu answered 8/10, 2009 at 21:24 Comment(0)
C
6

There's really no difference between the two situations, other than the fact that the server sets up some buffer-local state to enable C-x # (aka server-edit).

You can limit M-x list-buffers behavior like you're asking with the following advice:

(defadvice list-buffers-noselect (before list-buffers-noselect-limit-to-those-for-emacsclient activate)
  "When the current buffer is a being viewed by an emacclient, restrict the buffers to those associated with the emacsclient"
  (when (and (null (ad-get-arg 1)) server-buffer-clients)
    (let ((blist (delete-dups (reduce 'append
                                       (mapcar (lambda (proc) (process-get proc 'buffers))
                                               server-buffer-clients)))))
      (ad-set-arg 1 blist))))

Now when you do M-x buffer-menu in a buffer visited by emacsclient, you only see other buffers visited by the same client(s). It works as normal when the buffer is not visited by an emacsclient.

I don't use ido, but I imagine the customization would be similar (if this advice doesn't work as is).

The details are that when you run emacsclient, the buffers that get opened up are associated with the server process (it can be more than one because you can open up the same file via multiple invocations of emacsclient). A buffer's server clients are stored in the buffer local variable server-buffer-clients.

To find out which buffers are associated with a particular invocation of emacsclient, find the process for that emacsclient, and do: (process-get proc 'buffers) (where proc is the particular emacsclient process - one of the elements of the list found in server-buffer-clients).

That's all the advice does.

Camara answered 8/10, 2009 at 23:8 Comment(1)
Thanks! As Jed mentions, there appears to be no difference when a new frame is opened, but the advice above works when I open a new file with emacsclient -t. Interesting... I will look into this for ido-mode and possibly ibuffer. Much appreciated!Liu
H
3

There is no difference in emacs 23, as long as emacsserver is running. The buffer list will be the same in each.

Herv answered 8/10, 2009 at 21:42 Comment(1)
Thanks - yes it appears mostly correct, but there appears to be some awareness on the part of emacs as to which client is accessing a particular file, as Trey points out... but thanks for your input!Liu

© 2022 - 2024 — McMap. All rights reserved.