Hi Guix, Emacs, As promised to Stefan a few months ago, here's a use case of Shepherd/Emacs implementation that we developped in RDE. We're using the --daemon option on the Shepherd side to launch the server in the background, include code in Emacs configuration to make it create a pid-file as soon as the server has started, and redefine kill-emacs to be managed by the Shepherd. Originally I asked Stefan because I wasn't happy with the current Emacs/Shepherd interaction possibilities, as Emacs doesn't provide a native --pid-file option. He advised me to share once we found a solution to highlight the situation, here it is. Cheers, Nicolas -------------------- Start of forwarded message -------------------- From: Nicolas Graves To: ~abcdw/rde-devel@lists.sr.ht Cc: ngraves@ngraves.fr Subject: [PATCH v6 01/10] rde: emacs: Start emacs in --daemon mode, with shepherd and pid-file Date: Thu, 11 Apr 2024 01:48:13 +0200 --- src/rde/features/emacs.scm | 42 ++++++++++++++++++++++++++++++++- src/rde/home/services/emacs.scm | 9 ++++--- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/rde/features/emacs.scm b/src/rde/features/emacs.scm index 7d055377..74ba306e 100644 --- a/src/rde/features/emacs.scm +++ b/src/rde/features/emacs.scm @@ -440,7 +440,47 @@ Prefix argument can be used to kill a few words." ;; Configure ediff for window manager. (setq ediff-diff-options "-w" ediff-split-window-function 'split-window-horizontally - ediff-window-setup-function 'ediff-setup-windows-plain)) + ediff-window-setup-function 'ediff-setup-windows-plain) + + ;; Configure emacs background server. + ,@(if (get-value 'emacs-server-mode? config) + `((add-hook + 'emacs-startup-hook + (lambda () + (when server-mode + (advice-add + 'kill-emacs :override + (lambda (&optional arg restart) + "\ +Make GNU Shepherd kill the Emacs server. + +If RESTART is non-nil, instead of just exiting at the end, herd will restart +an Emacs server. ARG is ignored. + +Note: This is added through RDE. Redefining a primitive is not advised in +Emacs, but this one is high-level (present in few other functions), and +tested." + (interactive) + (if restart + (call-process-shell-command + (concat "herd restart emacs-" server-name) + nil 0) + (call-process-shell-command + (concat "herd stop emacs-" server-name) + nil 0)))) + ;; Tell shepherd that emacs is started through pid-file. + (with-temp-file (concat (getenv "XDG_RUNTIME_DIR") + "/emacs/" server-name ".pid") + (insert (number-to-string (emacs-pid))))))) + (add-hook + 'kill-emacs-hook + (lambda () + (when server-mode + (let ((pidfile (concat (getenv "XDG_RUNTIME_DIR") + "/emacs/" server-name ".pid"))) + (when (file-exists-p pidfile) + (delete-file pidfile))))))) + '())) #:summary "General settings, better defaults" #:commentary "\ It can contain settings not yet moved to separate features." diff --git a/src/rde/home/services/emacs.scm b/src/rde/home/services/emacs.scm index 9b0b37af..95156a4d 100644 --- a/src/rde/home/services/emacs.scm +++ b/src/rde/home/services/emacs.scm @@ -188,11 +188,14 @@ Same as @code{init-el}, but result will go to @file{early-init.el}.")) (start #~(make-forkexec-constructor (list #$(file-append (home-emacs-configuration-emacs config) - "/bin/emacs") #$(format #f "--fg-daemon=~a" name)) + "/bin/emacs") #$(format #f "--daemon=~a" name)) #:log-file (string-append (getenv "XDG_STATE_HOME") "/log" - "/emacs-" #$(symbol->string name) ".log"))) - (stop #~(make-kill-destructor)))) + "/emacs-" #$(symbol->string name) ".log") + #:pid-file (string-append (getenv "XDG_RUNTIME_DIR") "/emacs/" + #$(symbol->string name) ".pid"))) + (stop #~(make-kill-destructor)) + (respawn? #f))) (define (add-emacs-shepherd-service config) (if (not (null? (home-emacs-configuration-emacs-servers config))) -- 2.41.0 -------------------- End of forwarded message -------------------- -------------------- Start of forwarded message -------------------- From: Andrew Tropin To: Nicolas Graves , ~abcdw/rde-devel@lists.sr.ht Cc: ngraves@ngraves.fr Subject: Re: [PATCH v6 00/10] Emacs server background mode. Date: Thu, 11 Apr 2024 12:15:58 +0300