unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#67034: 30.0.50; Make `derived-mode-p` take a single arg
@ 2023-11-10  3:58 Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2023-11-10 12:18 ` Daniel Mendler
  2023-11-16 22:28 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 2 replies; 9+ messages in thread
From: Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2023-11-10  3:58 UTC (permalink / raw)
  To: 67034

Package: Emacs
Version: 30.0.50


Looking at uses of `derived-mode-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.

So making `derived-mode-p` take a single arg (which we'd allow to be
either a mode or a list of modes) would not make any real difference to
the callers (it would even be more convenient since it could often avoid
the use of `apply`), and in return we'd save allocating the
`&rest` list.

Same for `provided-mode-derived-p`.

And yes, I plead guilty for the `&rest` of `derived-mode-p`.
Seemed like a good idea at the time :-(

Draft patch below.


        Stefan


diff --git a/lisp/subr.el b/lisp/subr.el
index d4173b4daba..cd6407ef4b2 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -2678,11 +2678,17 @@ while-let
 
 ;; PUBLIC: find if the current mode derives from another.
 
-(defun provided-mode-derived-p (mode &rest modes)
+(defun provided-mode-derived-p (mode &optional parent &rest modes)
   "Non-nil if MODE is derived from one of MODES.
 Uses the `derived-mode-parent' property of the symbol to trace backwards.
 If you just want to check `major-mode', use `derived-mode-p'."
-  (declare (side-effect-free t))
+  (declare (side-effect-free t)
+           (advertised-calling-convention (mode parent) "30.1"))
+  (setq modes (if (not (listp parent))
+                  (cons parent modes)
+                ;; New calling convention can't use MODES at the same time.
+                (cl-assert (null modes))
+                parent))
   (while
       (and
        (not (memq mode modes))
@@ -2693,11 +2699,19 @@ provided-mode-derived-p
                          (and (symbolp alias) alias)))))))
   mode)
 
-(defun derived-mode-p (&rest modes)
-  "Non-nil if the current major mode is derived from one of MODES.
-Uses the `derived-mode-parent' property of the symbol to trace backwards."
-  (declare (side-effect-free t))
-  (apply #'provided-mode-derived-p major-mode modes))
+(defun derived-mode-p (&optional mode &rest modes)
+  "Non-nil if the current major mode is derived from MODE.
+MODE can also be a list of modes, in which case we check if major mode
+is derived from one of them.
+It also supports an obsolete `&rest MODES' calling convention."
+  (declare (side-effect-free t)
+           (advertised-calling-convention (mode) "30.1"))
+  (provided-mode-derived-p major-mode
+                           (if (not (listp mode)) (cons mode modes)
+                             ;; New calling convention can't use MODES
+                             ;; at the same time.
+                             (cl-assert (null modes))
+                             mode)))
 
 (defvar-local major-mode--suspended nil)
 (put 'major-mode--suspended 'permanent-local t)






^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2023-11-23 17:00 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-11-10  3:58 bug#67034: 30.0.50; Make `derived-mode-p` take a single arg Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-11-10 12:18 ` Daniel Mendler
2023-11-11  0:04   ` Dmitry Gutov
2023-11-11  9:08     ` Daniel Mendler
2023-11-16 22:38   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-11-16 22:28 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-11-17  8:24   ` Eli Zaretskii
2023-11-17 21:18     ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-11-23 17:00       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).