aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Monnier2023-11-16 17:21:18 -0500
committerStefan Monnier2023-11-23 11:59:38 -0500
commite6556db4200ccf28bd9bb033be0d5ce3cd2316a9 (patch)
tree41411919440c61b34f8223f7f17431c48ffd1c8f
parent9bda21ad0dddc5d84b6fca269626adeaa608b7a1 (diff)
downloademacs-e6556db4200ccf28bd9bb033be0d5ce3cd2316a9.tar.gz
emacs-e6556db4200ccf28bd9bb033be0d5ce3cd2316a9.zip
(derived-mode-p): Take MODES as a single argument
Looking at uses of `derived-mode-p` and `provide-mode-derived-p`, I can't find a single use case where it wouldn't be preferable for it to take a single argument instead of `&rest`: all the calls are either passing a single argument anyway, or passing a fixed list of modes. The use of `&rest` just makes the code less efficient and sometimes more clunky (because of the need for `apply`). So let's change that (while preserving backward compatibility, of course). * doc/lispref/modes.texi (Derived Modes): Adjust accordingly. * lisp/subr.el (provided-mode-derived-p, derived-mode-p): Take the `modes` as a single argument.
-rw-r--r--doc/lispref/modes.texi9
-rw-r--r--etc/NEWS11
-rw-r--r--lisp/subr.el28
3 files changed, 36 insertions, 12 deletions
diff --git a/doc/lispref/modes.texi b/doc/lispref/modes.texi
index 8670807cbdf..13090a13d71 100644
--- a/doc/lispref/modes.texi
+++ b/doc/lispref/modes.texi
@@ -932,9 +932,14 @@ Do not write an @code{interactive} spec in the definition;
932@code{define-derived-mode} does that automatically. 932@code{define-derived-mode} does that automatically.
933@end defmac 933@end defmac
934 934
935@defun derived-mode-p &rest modes 935@defun derived-mode-p modes
936This function returns non-@code{nil} if the current major mode is 936This function returns non-@code{nil} if the current major mode is
937derived from any of the major modes given by the symbols @var{modes}. 937derived from any of the major modes given by the list of symbols
938in @var{modes}.
939Instead of a list, @var{modes} can also be a single mode symbol.
940
941Furthermore, we still support a deprecated calling convention where the
942@var{modes} were passed as separate arguments.
938@end defun 943@end defun
939 944
940The graph of major modes is accessed with the following lower-level 945The graph of major modes is accessed with the following lower-level
diff --git a/etc/NEWS b/etc/NEWS
index 8794239c148..c0f76ed052b 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1228,8 +1228,15 @@ values.
1228Mostly used internally to do a kind of topological sort of 1228Mostly used internally to do a kind of topological sort of
1229inheritance hierarchies. 1229inheritance hierarchies.
1230 1230
1231** New API to control the graph of major modes. 1231** New API for 'derived-mode-p' and control of the graph of major modes.
1232While 'define-derived-mode' still only support single inheritance, 1232
1233*** 'derived-mode-p' now takes the list of modes as a single argument.
1234The same holds for `provided-mode-derived-p`.
1235The old calling convention where multiple modes are passed as
1236separate arguments is deprecated.
1237
1238*** New functions to access the graph of major modes.
1239While 'define-derived-mode' still only supports single inheritance,
1233modes can declare additional parents (for tests like 'derived-mode-p') 1240modes can declare additional parents (for tests like 'derived-mode-p')
1234with `derived-mode-add-parents`. 1241with `derived-mode-add-parents`.
1235Accessing the 'derived-mode-parent' property directly is now 1242Accessing the 'derived-mode-parent' property directly is now
diff --git a/lisp/subr.el b/lisp/subr.el
index dcf49509177..304b71e6168 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -2782,19 +2782,31 @@ The returned list is not fresh, don't modify it.
2782 (cons mode (remq mode all-parents)) 2782 (cons mode (remq mode all-parents))
2783 (put mode 'derived-mode--all-parents (cons mode all-parents)))))))) 2783 (put mode 'derived-mode--all-parents (cons mode all-parents))))))))
2784 2784
2785(defun provided-mode-derived-p (mode &rest modes) 2785(defun provided-mode-derived-p (mode &optional modes &rest old-modes)
2786 "Non-nil if MODE is derived from one of MODES. 2786 "Non-nil if MODE is derived from a mode that is a member of the list MODES.
2787If you just want to check `major-mode', use `derived-mode-p'." 2787MODES can also be a single mode instead of a list.
2788 (declare (side-effect-free t)) 2788If you just want to check `major-mode', use `derived-mode-p'.
2789We also still support the deprecated calling convention:
2790\(provided-mode-derived-p MODE &rest MODES)."
2791 (declare (side-effect-free t)
2792 (advertised-calling-convention (mode modes) "30.1"))
2793 (cond
2794 (old-modes (setq modes (cons modes old-modes)))
2795 ((not (listp modes)) (setq modes (list modes))))
2789 (let ((ps (derived-mode-all-parents mode))) 2796 (let ((ps (derived-mode-all-parents mode)))
2790 (while (and modes (not (memq (car modes) ps))) 2797 (while (and modes (not (memq (car modes) ps)))
2791 (setq modes (cdr modes))) 2798 (setq modes (cdr modes)))
2792 (car modes))) 2799 (car modes)))
2793 2800
2794(defun derived-mode-p (&rest modes) 2801(defun derived-mode-p (&optional modes &rest old-modes)
2795 "Non-nil if the current major mode is derived from one of MODES." 2802 "Non-nil if the current major mode is derived from one of MODES.
2796 (declare (side-effect-free t)) 2803MODES should be a list of symbols or a single mode symbol instead of a list.
2797 (apply #'provided-mode-derived-p major-mode modes)) 2804We also still support the deprecated calling convention:
2805\(derived-mode-p &rest MODES)."
2806 (declare (side-effect-free t)
2807 (advertised-calling-convention (modes) "30.1"))
2808 (provided-mode-derived-p major-mode (if old-modes (cons modes old-modes)
2809 modes)))
2798 2810
2799(defun derived-mode-set-parent (mode parent) 2811(defun derived-mode-set-parent (mode parent)
2800 "Declare PARENT to be the parent of MODE." 2812 "Declare PARENT to be the parent of MODE."