From 24a51f080f0e790b0681a1745fd32f7d967437e1 Mon Sep 17 00:00:00 2001 From: Jim Porter Date: Wed, 2 Nov 2022 09:22:43 -0700 Subject: [PATCH 1/2] 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. --- lisp/server.el | 12 ++++++++++-- test/lisp/server-tests.el | 41 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 test/lisp/server-tests.el diff --git a/lisp/server.el b/lisp/server.el index 90d97c1538..553890ce29 100644 --- a/lisp/server.el +++ b/lisp/server.el @@ -670,7 +670,6 @@ server-start "/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-start (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-start :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 @@ server-running-p 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 `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 0000000000..351b8ef8d1 --- /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 -- 2.25.1