Magically setup emacsclient

2019-05-25, updated 2019-08-02 Blogging, past and not blogging rantAutomatic Latex Fragment Toggling in org-mode

This is a magical way of automatically setting up emacsclient, by northrupthebandgeek. If you start emacs for the first time, it will start a server. If the server has already been started, it will delegate to emacsclient.

One tiny improvement over the function defined on the reddit post, is that this doesn't guess what is the name of the emacs server file. It instead generates the nam e in the same ways as done by the server package.

;; from:
;; Automagical EmacsClient functionality
;; Basically, if Emacs is already running, this shunts things over to
;; the existing Emacs; otherwise, it readies itself to accept said
;; shunting.
;; This operates by detecting the existence of an Emacs server socket
;; file.  If a socket is found, Emacs will
;; spin up emacsclient and immediately exit itself.  Otherwise, Emacs
;; will start a new server.

(defun server-already-running-p ()
  "Is Emacs already running?
Gets name based on server-force-delete."
       (let ((file (expand-file-name
                    (if server-use-tcp
         (file-exists-p file)))

(defun server-shunt ()
  "Shunts to emacsclient"
       (let ((args (append '("emacsclient" "-a" "\"\"" "-c" "-n")
                           (cdr command-line-args))))
         (shell-command (substring (format "%S" args) 1 -1))

(unless (featurep 'server)
  (unless (boundp 'server-process)
    (require 'server)
    (if (server-already-running-p) (server-shunt) (server-start))))

Author: Ivan Tadeu Ferreira Antunes Filho

Date: 2019-11-27 Wed 01:29


Made with Emacs 27.0.50 (Org mode 9.1.9) and Org export head