Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs list of parent modes

Tags:

emacs

mode

elisp

In emacs is it possible to get a list of parent modes for a derived mode?

I found derived-mode-p, but this only allows me to test if a mode is derived from another.

like image 912
Thayne Avatar asked Feb 27 '15 05:02

Thayne


1 Answers

Every symbol has a property list and for derived modes, this list includes the component derived-mode-parent which indicates which mode it was derived from. It seems to be just a single symbol.

(define-derived-mode nst-mode text-mode "Testing")
=> nst-mode

(get 'nst-mode 'derived-mode-parent)
=> text-mode

I don't see how there could be more than one parent, ever.

Here is a demo of a transitive chain of parents.

(define-derived-mode rope nil "Victim of mouse")
(define-derived-mode mouse rope "Victim of cat")
(define-derived-mode cat mouse "Victim of dog")
(define-derived-mode dog cat "Enough already")

(let ((mode 'dog) parents)
  (while mode
    (setq parents (cons mode parents)
          mode (get mode 'derived-mode-parent)))
  (reverse parents))

=> (dog cat mouse rope)

... or, just for fun, a recursive version:

(defun derived-mode-parents (mode)
  (and mode
    (cons mode (derived-mode-parents
          (get mode 'derived-mode-parent)))))

(derived-mode-parents 'dog)
=> (dog cat mouse rope)
like image 151
tripleee Avatar answered Oct 02 '22 09:10

tripleee