Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs - emacsclient or new frame?

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?

like image 633
hatmatrix Avatar asked Oct 08 '09 21:10

hatmatrix


People also ask

Why use EmacsClient?

EmacsClient allows one to open a file for editing in an already running Emacs. Because it doesn't start a new Emacs instance at each invocation, you can set up EmacsClient as the default editor, e.g. in the environment variable EDITOR or VISUAL. EmacsClient is part of and works only in conjunction with GNU Emacs.

What is Emacs NOX?

Nox is a lightweight, high-performance LSP client for Emacs. 204 stars 18 forks.

How do I stop Emacs daemon?

Stopping the Emacs Daemon The simplest way to stop the emacs daemon from within emacs is to use the kill-emacs or save-buffers-kill-emacs commands. Here is a more advanced function will ask if you want to save any modified buffers, quit your session, and shutdown the associated emacs server instance.


1 Answers

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.

like image 169
Trey Jackson Avatar answered Sep 21 '22 17:09

Trey Jackson