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).
This commit is contained in:
Jim Porter 2022-11-02 09:22:43 -07:00
parent 7781121c44
commit 0147e1ed83
2 changed files with 51 additions and 2 deletions

View file

@ -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)))

41
test/lisp/server-tests.el Normal file
View file

@ -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 <https://www.gnu.org/licenses/>.
;;; 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