all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Nicolas Graves via "Development of GNU Guix and the GNU System distribution." <guix-devel@gnu.org>
To: guix-devel@gnu.org, emacs-devel@gnu.org
Cc: Andrew Tropin <andrew@trop.in>,
	Stefan Monnier <monnier@iro.umontreal.ca>
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	[thread overview]
Message-ID: <87zfu0m9ps.fsf@ngraves.fr> (raw)
In-Reply-To: 875xwotg35.fsf@trop.in

[-- Attachment #1: Type: text/plain, Size: 5146 bytes --]


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


[-- Attachment #2.1: Type: text/plain, Size: 1261 bytes --]

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 #2.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 832 bytes --]

[-- Attachment #3: Type: text/plain, Size: 101 bytes --]

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

-- 
Best regards,
Nicolas Graves

       reply	other threads:[~2024-04-11 11:15 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20240410234923.29319-2-ngraves@ngraves.fr>
     [not found] ` <875xwotg35.fsf@trop.in>
2024-04-11 11:15   ` Nicolas Graves via Development of GNU Guix and the GNU System distribution. [this message]
2024-04-12 20:38     ` [Nicolas Graves] [PATCH v6 01/10] rde: emacs: Start emacs in --daemon mode, with shepherd and pid-file Ludovic Courtès
2024-04-13 14:20       ` Nicolas Graves via Development of GNU Guix and the GNU System distribution.
2024-04-13 15:09         ` Nicolas Graves via Development of GNU Guix and the GNU System distribution.
2024-04-13 15:16         ` Stefan Monnier
2024-04-14 19:11           ` Björn Bidar
2024-04-14 19:11           ` Björn Bidar
     [not found]           ` <87le5f2202.fsf@>
2024-04-14 20:52             ` Stefan Monnier
2024-04-19 14:19               ` Ludovic Courtès
2024-04-19 14:36                 ` Rudolf Schlatte
2024-04-20  2:31                   ` Stefan Monnier
2024-04-19 14:17             ` Ludovic Courtès
2024-05-11 20:15           ` Nicolas Graves via Development of GNU Guix and the GNU System distribution.
2024-05-11 23:07             ` Nicolas Graves via Development of GNU Guix and the GNU System distribution.
2024-05-12  6:29               ` Eli Zaretskii
2024-05-12  7:50                 ` Nicolas Graves via Development of GNU Guix and the GNU System distribution.
2024-05-12  7:54                   ` Nicolas Graves via Development of GNU Guix and the GNU System distribution.
2024-05-12  9:36                   ` Eli Zaretskii
2024-05-12 11:11                     ` Nicolas Graves via Development of GNU Guix and the GNU System distribution.
2024-05-12 15:01                       ` Nicolas Graves via Development of GNU Guix and the GNU System distribution.
2024-04-13 16:50       ` Nicolas Graves via Development of GNU Guix and the GNU System distribution.
2024-04-19 14:25         ` Ludovic Courtès
2024-04-14 16:51       ` Felix Lechner via Development of GNU Guix and the GNU System distribution.

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87zfu0m9ps.fsf@ngraves.fr \
    --to=guix-devel@gnu.org \
    --cc=andrew@trop.in \
    --cc=emacs-devel@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    --cc=ngraves@ngraves.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.