From: Jim Porter Date: Wed, 2 Nov 2022 16:22:43 +0000 (-0700) Subject: Enable/disable 'server-mode' when starting/stopping the server X-Git-Tag: emacs-29.0.90~1616^2~137 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=0147e1ed831151dddac65727886d5a70bbab9f02;p=emacs.git Enable/disable 'server-mode' when starting/stopping the server * lisp/server.el (server-mode-map): New keymap... (server-mode): ... use it. (server-start): Update the 'server-mode' variable (and sync to 'global-minor-modes') when starting/stopping the server. * test/lisp/server-tests.el: New file (bug#58909). --- diff --git a/lisp/server.el b/lisp/server.el index 90d97c1538e..553890ce299 100644 --- a/lisp/server.el +++ b/lisp/server.el @@ -670,7 +670,6 @@ the `server-process' variable." "/tmp/") (ignore-errors (delete-directory (file-name-directory server-file)))))) - (setq server-mode nil) ;; already set by the minor mode code (display-warning 'server (concat "Unable to start the Emacs server.\n" @@ -688,7 +687,9 @@ server or call `\\[server-force-delete]' to forcibly disconnect it.")) (if leave-dead (progn (unless (eq t leave-dead) (server-log (message "Server stopped"))) - (setq server-process nil)) + (setq server-mode nil + global-minor-modes (delq 'server-mode global-minor-modes) + server-process nil)) ;; Make sure there is a safe directory in which to place the socket. (server-ensure-safe-dir server-dir) (when server-process @@ -728,6 +729,8 @@ server or call `\\[server-force-delete]' to forcibly disconnect it.")) :plist '(:authenticated t))))) (unless server-process (error "Could not start server process")) (process-put server-process :server-file server-file) + (setq server-mode t) + (push 'server-mode global-minor-modes) (when server-use-tcp (let ((auth-key (server-get-auth-key))) (process-put server-process :auth-key auth-key) @@ -796,6 +799,10 @@ by the current Emacs process, use the `server-process' variable." t) (file-error nil))) +;; This keymap is empty, but allows users to define keybindings to use +;; when `server-mode' is active. +(defvar-keymap server-mode-map) + ;;;###autoload (define-minor-mode server-mode "Toggle Server mode. @@ -805,6 +812,7 @@ Server mode runs a process that accepts commands from the `server-start' for details." :global t :version "22.1" + :keymap server-mode-map ;; Fixme: Should this check for an existing server socket and do ;; nothing if there is one (for multiple Emacs sessions)? (server-start (not server-mode))) diff --git a/test/lisp/server-tests.el b/test/lisp/server-tests.el new file mode 100644 index 00000000000..351b8ef8d12 --- /dev/null +++ b/test/lisp/server-tests.el @@ -0,0 +1,41 @@ +;;; server-tests.el --- Emacs server test suite -*- lexical-binding:t -*- + +;; Copyright (C) 2022 Free Software Foundation, Inc. + +;; This file is part of GNU Emacs. + +;; GNU Emacs is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; GNU Emacs is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs. If not, see . + +;;; Code: + +(require 'ert) +(require 'server) + +;;; Tests: + +(ert-deftest server-test/server-start-sets-minor-mode () + "Ensure that calling `server-start' also sets `server-mode' properly." + (server-start) + (unwind-protect + (progn + ;; Make sure starting the server activates the minor mode. + (should (eq server-mode t)) + (should (memq 'server-mode global-minor-modes))) + ;; Always stop the server, even if the above checks fail. + (server-start t)) + ;; Make sure stopping the server deactivates the minor mode. + (should (eq server-mode nil)) + (should-not (memq 'server-mode global-minor-modes))) + +;;; server-tests.el ends here