[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/compat db9321e222 1/3: Add provided-mode-derived-p from
From: |
ELPA Syncer |
Subject: |
[elpa] externals/compat db9321e222 1/3: Add provided-mode-derived-p from Emacs 27 |
Date: |
Wed, 10 Aug 2022 15:57:24 -0400 (EDT) |
branch: externals/compat
commit db9321e222352c5c276c5df60b921999034264bf
Author: Philip Kaludercic <philipk@posteo.net>
Commit: Philip Kaludercic <philipk@posteo.net>
Add provided-mode-derived-p from Emacs 27
---
compat-27.el | 17 +++++++++++++++++
compat-tests.el | 23 +++++++++++++++++++++++
compat.texi | 7 +++++++
3 files changed, 47 insertions(+)
diff --git a/compat-27.el b/compat-27.el
index fdc81c93c8..89ed286954 100644
--- a/compat-27.el
+++ b/compat-27.el
@@ -399,6 +399,23 @@ where USER is a valid login name."
body)))
(cons 'progn (nreverse body))))
+(compat-defun provided-mode-derived-p (mode &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'."
+ :realname compat--provided-mode-derived-p
+ ;; If MODE is an alias, then look up the real mode function first.
+ (when-let ((alias (symbol-function mode)))
+ (when (symbolp alias)
+ (setq mode alias)))
+ (while
+ (and
+ (not (memq mode modes))
+ (let* ((parent (get mode 'derived-mode-parent))
+ (parentfn (symbol-function parent)))
+ (setq mode (if (and parentfn (symbolp parentfn)) parentfn parent)))))
+ mode)
+
;;* UNTESTED
(compat-defmacro ignore-error (condition &rest body)
"Execute BODY; if the error CONDITION occurs, return nil.
diff --git a/compat-tests.el b/compat-tests.el
index b2940cfbd8..0794b76972 100644
--- a/compat-tests.el
+++ b/compat-tests.el
@@ -1838,5 +1838,28 @@ being compared against."
(ought t "~root/")
(ought t "~root/file"))
+(let ((one (make-symbol "1"))
+ (two (make-symbol "2"))
+ (three (make-symbol "3"))
+ (one.5 (make-symbol "1.5"))
+ (eins (make-symbol "𝟙")))
+ (put two 'derived-mode-parent one)
+ (put one.5 'derived-mode-parent one)
+ (put three 'derived-mode-parent two)
+ (compat-deftests provided-mode-derived-p
+ (ought one one one)
+ (ought one two one)
+ (ought one three one)
+ (ought nil one eins)
+ (ought nil two eins)
+ (ought nil two one.5)
+ (ought one two one.5 one)
+ (ought two two one.5 two)
+ (ought one three one.5 one)
+ (ought two three one.5 one two)
+ (ought two three one.5 two one)
+ (ought three three one.5 two one three)
+ (ought three three one.5 three two one)))
+
(provide 'compat-tests)
;;; compat-tests.el ends here
diff --git a/compat.texi b/compat.texi
index 7e70fd9d61..27b00337c4 100644
--- a/compat.texi
+++ b/compat.texi
@@ -1468,6 +1468,13 @@ of @env{PATH} can lead to confusing results.
@xref{Subprocess Creation,,,elisp}.
@end defopt
+@c based on lisp/modes.el
+@defun provided-mode-derived-p mode &rest modes
+This function returns non-@code{nil} if @var{mode} is derived from any
+of the major modes given by the symbols @var{modes}.
+@end defun
+
+
@subsection Prefixed Definitions
These functions are prefixed with @code{compat} prefix, and are only
loaded when @code{compat-27} is required: