7

The server-running-p predicate will evaluate to t if the Emacs server is running, irrespective of which Emacs session currently "owns" the server process.

Therefore, when there are two or more independent Emacs sessions running simultaneously, server-running-p does not really test whether the current Emacs session is running the server.

I'm looking for a more specific test, one that will evaluate to t if and only if the current session (i.e. the session performing the test) is running the Emacs server.

  • 1
    Cannot test right now but maybe you can test if server-mode is non-nil. – YoungFrog Mar 4 '17 at 20:27
  • @YoungFrog: thanks for the suggestion, but in every situation I tested (namely, after server-start and after server-force-delete), server-mode was always nil. – kjo Mar 4 '17 at 20:32
  • 1
    I think YoungFrog's suggestion is a good one, but you need to call (server-mode 1) rather than calling (server-start). The former invokes the latter, and server-force-delete also checks and disables this mode, so it rather looks like sever-mode is the intended interface, and we shouldn't be calling server-start directly. – phils Mar 4 '17 at 23:10
  • 1
    That said, using server-mode still doesn't account for the same server being started and deleted via multiple Emacs instances, as deleting the server from one instance has no effect on the value of server-mode in another. – phils Mar 4 '17 at 23:24
5

You might check:

(and (boundp 'server-process)
     (processp server-process)
     (server-running-p))

That should work provided that you've avoided starting a server in the other instances.

Assuming a socket-based server, if you were to start a second Emacs instance, and forcibly delete and then restart the server process from that second instance, then the above code will still return non-nil in the original instance (as the same socket directory will be in use), leaving you in the original situation.

(Obviously this isn't ideal, so there may well be a better solution.)

server-running-p tells you whether a particular named server (defaulting to server-name) is running; so if your server-name is the same in all of your instances, then you can avoid starting an already-running server like so:

;; Start server (but don't restart).
(require 'server)
(unless (server-running-p)
  (server-start))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.