Skip to content

Commit 48ff4c0

Browse files
andyleejordanEli-Zaretskii
authored andcommitted
Support mode aliases in 'provided-mode-derived-p'
* lisp/subr.el (provided-mode-derived-p): Check aliases of MODES as well as MODES themselves. (Bug#32795) * test/lisp/subr-tests.el (provided-mode-derived-p): New test. Copyright-paperwork-exempt: yes
1 parent c973a0f commit 48ff4c0

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

etc/NEWS

+5
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,11 @@ This works like 'dolist', but reports progress similar to
982982
This works like 'delete-frame-functions', but runs after the frame to
983983
be deleted has been made dead and removed from the frame list.
984984

985+
---
986+
** The function 'provided-mode-derived-p' was extended to support aliases.
987+
The function now returns non-nil when the argument MODE is derived
988+
from any alias of any of MODES.
989+
985990
+++
986991
** New frame focus state inspection interface.
987992
The hooks 'focus-in-hook' and 'focus-out-hook' are now obsolete.

lisp/subr.el

+7-3
Original file line numberDiff line numberDiff line change
@@ -1918,11 +1918,15 @@ Only affects hooks run in the current buffer."
19181918
;; PUBLIC: find if the current mode derives from another.
19191919

19201920
(defun provided-mode-derived-p (mode &rest modes)
1921-
"Non-nil if MODE is derived from one of MODES.
1921+
"Non-nil if MODE is derived from one of MODES or their aliases.
19221922
Uses the `derived-mode-parent' property of the symbol to trace backwards.
19231923
If you just want to check `major-mode', use `derived-mode-p'."
1924-
(while (and (not (memq mode modes))
1925-
(setq mode (get mode 'derived-mode-parent))))
1924+
(while
1925+
(and
1926+
(not (memq mode modes))
1927+
(let* ((parent (get mode 'derived-mode-parent))
1928+
(parentfn (symbol-function parent)))
1929+
(setq mode (if (and parentfn (symbolp parentfn)) parentfn parent)))))
19261930
mode)
19271931

19281932
(defun derived-mode-p (&rest modes)

test/lisp/subr-tests.el

+12
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@
6161
(quote
6262
(0 font-lock-keyword-face))))))))
6363

64+
(ert-deftest provided-mode-derived-p ()
65+
;; base case: `derived-mode' directly derives `prog-mode'
66+
(should (progn
67+
(define-derived-mode derived-mode prog-mode "test")
68+
(provided-mode-derived-p 'derived-mode 'prog-mode)))
69+
;; edge case: `derived-mode' derives an alias of `prog-mode'
70+
(should (progn
71+
(defalias 'parent-mode
72+
(if (fboundp 'prog-mode) 'prog-mode 'fundamental-mode))
73+
(define-derived-mode derived-mode parent-mode "test")
74+
(provided-mode-derived-p 'derived-mode 'prog-mode))))
75+
6476
(ert-deftest number-sequence-test ()
6577
(should (= (length
6678
(number-sequence (1- most-positive-fixnum) most-positive-fixnum))

0 commit comments

Comments
 (0)