1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-06 22:41:06 -08:00

(define-minor-mode): Update global-minor-modes if init-value is non-nil

When a global minor mode is enabled by init-value rather than by
calling the major mode function, we failed to register it in
`global-minor-modes` (bug#79518).

* lisp/emacs-lisp/easy-mmode.el (define-minor-mode): Register
ourselves in `global-minor-modes` at top-level for global modes
with a non-nil init-value.
Also in the `modefun`, simply the code with `not` and `add-to-list`
and consolidate the local/global paths to update `*-minor-modes`.

* lisp/simple.el (global-minor-modes): Move to...
* lisp/subr.el (global-minor-modes): ...here so it's defined early enough
for `auto-compression-mode` to register itself.
This commit is contained in:
Stefan Monnier 2025-10-14 11:21:29 -04:00
parent 3aae55acfc
commit 171c7fd6df
3 changed files with 28 additions and 33 deletions

View file

@ -332,12 +332,16 @@ for a description of this minor mode."
Setting this variable directly does not take effect;
either customize it (see the info node `Easy Customization')
or call the function `%s'."))))
`(defcustom ,mode ,init-value
,(format base-doc-string pretty-name mode mode)
,@set
,@initialize
,@type
,@(nreverse extra-keywords)))))
`(progn
(defcustom ,mode ,init-value
,(format base-doc-string pretty-name mode mode)
,@set
,@initialize
,@type
,@(nreverse extra-keywords))
,(when init-value
`(when (bound-and-true-p ,mode)
(add-to-list 'global-minor-modes ',modefun)))))))
;; The actual function.
,(funcall
@ -360,30 +364,21 @@ or call the function `%s'."))))
'toggle)))))
(let ((,last-message (current-message)))
(,@setter
(cond ((eq arg 'toggle)
(not ,getter))
((and (numberp arg)
(< arg 1))
nil)
(t
t)))
(cond ((eq arg 'toggle) (not ,getter))
(t (not (and (numberp arg) (< arg 1))))))
;; Keep minor modes list up to date.
,@(if globalp
;; When running this byte-compiled code in earlier
;; Emacs versions, these variables may not be defined
;; there. So check defensively, even if they're
;; always defined in Emacs 28 and up.
`((when (boundp 'global-minor-modes)
(setq global-minor-modes
(delq ',modefun global-minor-modes))
(when ,getter
(push ',modefun global-minor-modes))))
;; Ditto check.
`((when (boundp 'local-minor-modes)
(setq local-minor-modes
(delq ',modefun local-minor-modes))
(when ,getter
(push ',modefun local-minor-modes)))))
,(let ((minor-modes-var (if globalp
'global-minor-modes
'local-minor-modes)))
;; When running this byte-compiled code in earlier
;; Emacs versions, these variables may not be defined
;; there. So check defensively, even if they're
;; always defined in Emacs 28 and up.
`(when (boundp ',minor-modes-var)
(if ,getter
(add-to-list ',minor-modes-var ',modefun)
(setq ,minor-modes-var
(delq ',modefun ,minor-modes-var)))))
,@body
;; The on/off hooks are here for backward compatibility only.
(run-hooks ',hook (if ,getter ',hook-on ',hook-off))