emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Nicolas Graves] [PATCH v6 01/10] rde: emacs: Start emacs in --daemon mo


From: Nicolas Graves
Subject: [Nicolas Graves] [PATCH v6 01/10] rde: emacs: Start emacs in --daemon mode, with shepherd and pid-file
Date: Thu, 11 Apr 2024 13:15:27 +0200

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 <ngraves@ngraves.fr>
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 <andrew@trop.in>
To: Nicolas Graves <ngraves@ngraves.fr>, ~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

On 2024-04-11 01:48, Nicolas Graves wrote:

> v6 of the previous patch series, rebased and squased
>
> Nicolas Graves (10):
>   rde: emacs: Start emacs in --daemon mode, with shepherd and pid-file
>   rde: emacs: Add smart emacs-client-alternate-editor
>   rde: emacs: Make minibuffer programs fail without emacs-server
>   rde: sway: Add shepherd value
>   rde: emacs: Use absolute path for herd binary
>   rde: wm: Sort package modules
>   rde: swaynotificationcenter: Add value libnotify
>   rde: emacs: Move emacs-minibuffer-program to feature value
>   rde: emacs: Make emacs-minibuffer-program depend on config
>   rde: emacs: Propagate herd and libnotify paths
>
>  src/rde/features/emacs-xyz.scm      |   6 +-
>  src/rde/features/emacs.scm          | 168 +++++++++++++++++++++-------
>  src/rde/features/password-utils.scm |   3 +-
>  src/rde/features/wm.scm             |  38 ++++---
>  src/rde/features/xdisorg.scm        |   4 +-
>  src/rde/home/services/emacs.scm     |   9 +-
>  6 files changed, 165 insertions(+), 63 deletions(-)

Very nice, works flawlessly so far and notifications, when server is
starting are great.  No more half-way loaded emacsclient.

Thank you very much for you work!  Great job, Nicolas!

-- 
Best regards,
Andrew Tropin

Attachment: signature.asc
Description: PGP signature

-------------------- End of forwarded message --------------------

-- 
Best regards,
Nicolas Graves

reply via email to

[Prev in Thread] Current Thread [Next in Thread]