0

I know that there are many ways to logically separate concerns by project in a single Emacs instance. However, what I'd like to do is to get one Emacs instance per project and one instance for any file that is not bound to any project. For checking the project of a file, I'd like to refer to project.el facilities.

Opening a file from my Linux-based OS file manager may result in either re-using a frame of an already existing instance or spawning a frame of a newly created instance, depending of whether the file's project (if existing) is opened or not. None of these instances should be opened as headless daemons, as I'd like the "Buffer changed" dialog to appear when closing the last window. How can I achieve something like this?

4
  • Use emacsclient and start emacs-server for each project separately? (Dunno. I don't do this kind of thing anymore.) Commented Jan 21 at 21:04
  • Mmm. I visualize two cases. Case 1 - no instance running: create new instance as server and bind it to project which the file belongs to; Case 2 - one or more instances running: delegate one of the running servers into checking whether the opening project already has a running instance and decide where to open the file, or create a new instance if needed. Commented Jan 21 at 21:42
  • This is specifically a question about what happens when you double-click files with the mouse in your desktop environment, yes? Commented Jan 21 at 21:42
  • 1
    @phils yes, basically xdg-open. My request does not affect opening a file through C-x C-f Commented Jan 21 at 21:45

1 Answer 1

1

emacsclient --socket-name="$PROJECT" --alternate-editor='emacs' will connect a client to the named server if it exists, and run a new instance of Emacs if no such server exists.

So then you:

  1. Write yourself a wrapper shell script for that, which figures out what the $PROJECT should be for any given invocation (for whatever your criteria for "a project" is). Make XDG use this wrapper when it wants to run Emacs.

  2. Have your wrapper script export PROJECT as an environment variable.

  3. In your init file:

    (when-let ((project (getenv "PROJECT"))) (setq server-name project) (server-start)) 
3
  • Thank you for the quick reply! Sorry, I ought to clarify before, I just edited my question to specify that I'd like project.el to check the project of a file, in the same way that it can assess the project and make it appear in the mode line as I open a file. Commented Jan 21 at 21:56
  • I guess my idea still works if you have a separate named daemon running to which you can make project.el enquiries. Something like emacsclient --socket-name="project-el" --eval "(lisp goes here)" and obtain the project name that way. Commented Jan 21 at 22:01
  • The "shell script" could be all be done with another Emacs instance, but I won't look into that right now. Commented Jan 21 at 22:03

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.