From 6ccf21f1e3137012d0335fac05dfc476cc9ebec2 Mon Sep 17 00:00:00 2001 From: Stefan Monnier Date: Thu, 16 Nov 2023 17:21:18 -0500 Subject: [PATCH 1/2] (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. --- doc/lispref/modes.texi | 6 +++++- lisp/subr.el | 26 +++++++++++++++++++------- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/doc/lispref/modes.texi b/doc/lispref/modes.texi index 130bc10cd59..1d60c3a01e0 100644 --- a/doc/lispref/modes.texi +++ b/doc/lispref/modes.texi @@ -932,9 +932,13 @@ Derived Modes @code{define-derived-mode} does that automatically. @end defmac -@defun derived-mode-p &rest modes +@defun derived-mode-p modes This function returns non-@code{nil} if the current major mode is derived from any of the major modes given by the symbols @var{modes}. +Instead of a list, @var{modes} can also be a single symbol. + +Furthermore, we still support a deprecated calling convention where the +@var{modes} were passed as separate arguments. @end defun The graph of major modes is accessed with the following lower-level diff --git a/lisp/subr.el b/lisp/subr.el index dcf49509177..dd01d79b1d0 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -2782,19 +2782,31 @@ derived-mode-all-parents (cons mode (remq mode all-parents)) (put mode 'derived-mode--all-parents (cons mode all-parents)))))))) -(defun provided-mode-derived-p (mode &rest modes) +(defun provided-mode-derived-p (mode &optional modes &rest old-modes) "Non-nil if MODE is derived from one of MODES. -If you just want to check `major-mode', use `derived-mode-p'." - (declare (side-effect-free t)) +MODES can also be a single mode instead of a list. +If you just want to check `major-mode', use `derived-mode-p'. +We also still support the deprecated calling convention: +\(provided-mode-derived-p MODE &rest MODES)." + (declare (side-effect-free t) + (advertised-calling-convention (mode modes) "30.1")) + (cond + (old-modes (setq modes (cons modes old-modes))) + ((not (listp modes)) (setq modes (list modes)))) (let ((ps (derived-mode-all-parents mode))) (while (and modes (not (memq (car modes) ps))) (setq modes (cdr modes))) (car modes))) -(defun derived-mode-p (&rest modes) - "Non-nil if the current major mode is derived from one of MODES." - (declare (side-effect-free t)) - (apply #'provided-mode-derived-p major-mode modes)) +(defun derived-mode-p (&optional modes &rest old-modes) + "Non-nil if the current major mode is derived from one of MODES. +MODES can also be a single mode instead of a list. +We also still support the deprecated calling convention: +\(derived-mode-p &rest MODES)." + (declare (side-effect-free t) + (advertised-calling-convention (modes) "30.1")) + (provided-mode-derived-p major-mode (if old-modes (cons modes old-modes) + modes))) (defun derived-mode-set-parent (mode parent) "Declare PARENT to be the parent of MODE." -- 2.42.0