Skip to content

Commit 14659f6

Browse files
committed
change viper to use derived-mode-p
* lisp/subr.el (provided-mode-derived-p): New function. (derived-mode-p): Use it. * lisp/emulation/viper.el (viper-mode): Use derived-mode-p. (this-major-mode-requires-vi-state): Use provided-mode-derived-p. (set-viper-state-in-major-mode): Use derived-mode-p.
1 parent 95aba61 commit 14659f6

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

lisp/emulation/viper.el

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -592,8 +592,10 @@ This startup message appears whenever you load Viper, unless you type `y' now."
592592
))
593593
(viper-set-expert-level 'dont-change-unless)))
594594

595-
(or (memq major-mode viper-emacs-state-mode-list) ; don't switch to Vi
596-
(memq major-mode viper-insert-state-mode-list) ; don't switch
595+
(or (cl-member-if #'derived-mode-p
596+
viper-emacs-state-mode-list) ; don't switch to Vi
597+
(cl-member-if #'derived-mode-p
598+
viper-insert-state-mode-list) ; don't switch
597599
(viper-change-state-to-vi))
598600
))
599601

@@ -605,11 +607,15 @@ This startup message appears whenever you load Viper, unless you type `y' now."
605607
;; Apply a little heuristic to invoke vi state on major-modes
606608
;; that are not listed in viper-vi-state-mode-list
607609
(defun this-major-mode-requires-vi-state (mode)
608-
(cond ((memq mode viper-vi-state-mode-list) t)
609-
((memq mode viper-emacs-state-mode-list) nil)
610-
((memq mode viper-insert-state-mode-list) nil)
611-
(t (and (eq (key-binding "a") 'self-insert-command)
612-
(eq (key-binding " ") 'self-insert-command)))))
610+
(let ((check (lambda (one-mode)
611+
(provided-mode-derived-p mode one-mode))))
612+
(cond ((cl-member-if check viper-vi-state-mode-list) t)
613+
((cl-member-if check viper-emacs-state-mode-list)
614+
nil)
615+
((cl-member-if check viper-insert-state-mode-list)
616+
nil)
617+
(t (and (eq (key-binding "a") 'self-insert-command)
618+
(eq (key-binding " ") 'self-insert-command))))))
613619

614620

615621
;; This hook designed to enable Vi-style editing in comint-based modes."
@@ -802,13 +808,14 @@ It also can't undo some Viper settings."
802808
(cond ((and (this-major-mode-requires-vi-state major-mode)
803809
(eq viper-current-state 'emacs-state))
804810
(viper-mode))
805-
((memq major-mode viper-emacs-state-mode-list)
811+
((cl-member-if #'derived-mode-p viper-emacs-state-mode-list)
806812
;; not checking (eq viper-current-state 'emacs-state)
807813
;; because viper-current-state could have gotten it by
808814
;; default. We need viper-change-state-to-emacs here to have
809815
;; the keymaps take effect.
810816
(viper-change-state-to-emacs))
811-
((and (memq major-mode viper-insert-state-mode-list)
817+
((and (cl-member-if #'derived-mode-p
818+
viper-insert-state-mode-list)
812819
(not (eq viper-current-state 'insert-state)))
813820
(viper-change-state-to-insert))
814821
)) ; with-current-buffer

lisp/subr.el

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,13 +1872,18 @@ Only affects hooks run in the current buffer."
18721872

18731873
;; PUBLIC: find if the current mode derives from another.
18741874

1875+
(defun provided-mode-derived-p (mode &rest modes)
1876+
"Non-nil if MODE is derived from one of MODES.
1877+
Uses the `derived-mode-parent' property of the symbol to trace backwards.
1878+
If you just want to check `major-mode', use `derived-mode-p'."
1879+
(while (and (not (memq mode modes))
1880+
(setq mode (get mode 'derived-mode-parent))))
1881+
mode)
1882+
18751883
(defun derived-mode-p (&rest modes)
18761884
"Non-nil if the current major mode is derived from one of MODES.
18771885
Uses the `derived-mode-parent' property of the symbol to trace backwards."
1878-
(let ((parent major-mode))
1879-
(while (and (not (memq parent modes))
1880-
(setq parent (get parent 'derived-mode-parent))))
1881-
parent))
1886+
(apply #'provided-mode-derived-p major-mode modes))
18821887

18831888
;;;; Minor modes.
18841889

0 commit comments

Comments
 (0)