Skip to content

Instantly share code, notes, and snippets.

@andschwa
Last active September 21, 2018 06:46
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Make `provided-mode-derived-p` support `defalias` parents
;; Fixed to support `(defalias 'my-parent-mode ...)`
(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'."
(while (and (not (memq mode modes))
(let ((parent (get mode 'derived-mode-parent)))
(let ((parentfn (symbol-function parent)))
(setq mode (if (and parentfn (symbolp parentfn))
parentfn
parent))))))
mode)
;; Broken behavior (seen in cmake-mode.el and groovy-mode.el):
(defalias 'alias-parent-mode
(if (fboundp 'prog-mode) 'prog-mode 'fundamental-mode))
(define-derived-mode alias-mode alias-parent-mode "Test")
(message "Aliased derived: %s"
(provided-mode-derived-p 'alias-mode 'prog-mode))
;; Existing working behavior:
(define-derived-mode test-mode prog-mode "Test")
(message "Directly derived: %s"
(provided-mode-derived-p 'test-mode 'prog-mode))
;; Original from subr.el:
;; (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'."
;; (while (and (not (memq mode modes))
;; (setq mode (get mode 'derived-mode-parent))))
;; mode)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment