Skip to content

Commit

Permalink
improve init mode detecting, remove useless items from `meow-mode-sta…
Browse files Browse the repository at this point in the history
…te-list`
  • Loading branch information
tianshu committed Dec 15, 2021
1 parent 65c9fba commit a39509d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
46 changes: 40 additions & 6 deletions meow-core.el
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,46 @@ We have to remember previous state, so that we can restore it."
(defun meow--enable ()
"Enable Meow.
before activate any state.
then SPC will be bound to LEADER."
(when (meow--init-motion-p)
(meow-normal-mode -1)
(meow--save-origin-commands)
(meow-motion-mode 1)))
This function will switch to the proper state for current
major mode. Firstly, the variable `meow-mode-state-list' will be used.
If current major mode derived from any mode from the list, specified
state will be used. When no result is found, give a test on the command
bound to key A. if the command name contains \"self-insert\", then NORMAL
state will be used. Otherwise, MOTION state will be used.
Before turning on MOTION state, the original commands will be remap.
The new keybinding is generated by prepend `meow-motion-remap-prefix' to
the original keybinding.
Note: When this function is called, NORMAL state is already enabled.
NORMAL state is enabled globally when `meow-global-mode' is used.
because in `fundamental-mode', there's no chance for meow to call
an init function."
(let ((state-to-modes (seq-group-by #'cdr meow-mode-state-list)))
(cond
;; if MOTION is specified
((apply #'derived-mode-p (mapcar #'car (alist-get 'motion state-to-modes)))
(meow-normal-mode -1)
(meow--save-origin-commands)
(meow-motion-mode 1))

;; if NORMAL is specified
((apply #'derived-mode-p (mapcar #'car (alist-get 'normal state-to-modes)))
nil)

;; if key A is bound to a self-insert command
((progn
;; Disable meow-normal-mode, we need test the command name bound to a single letter key.
(meow-normal-mode -1)
(let ((meow-normal-mode nil)
(cmd (key-binding "a")))
(string-match-p "\\`.*self-insert.*\\'" (symbol-name cmd))))
(meow-normal-mode 1))

;; fallback to MOTION state
(t
(meow--save-origin-commands)
(meow-motion-mode 1)))))

(defun meow--disable ()
"Disable Meow."
Expand Down
7 changes: 0 additions & 7 deletions meow-util.el
Original file line number Diff line number Diff line change
Expand Up @@ -553,13 +553,6 @@ that bound to DEF. Otherwise, return DEF."
(undo-amalgamate-change-group ,handle))
(cancel-change-group ,handle))))))

(defun meow--init-motion-p ()
(let ((state-to-modes (seq-group-by #'cdr meow-mode-state-list)))
(or (apply #'derived-mode-p
(mapcar #'car (alist-get 'motion state-to-modes)))
(not (apply #'derived-mode-p
(mapcar #'car (alist-get 'normal state-to-modes)))))))

(defun meow--highlight-pre-command ()
(unless (member this-command '(meow-search))
(meow--remove-match-highlights))
Expand Down
8 changes: 1 addition & 7 deletions meow-var.el
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,8 @@ This option will affect the color of position hint and fake region cursor."
(cider-browse-spec-view-mode . motion)
(cargo-process-mode . normal)
(conf-mode . normal)
(comint-mode . normal)
(deadgrep-edit-mode . normal)
(deft-mode . normal)
(eshell-mode . normal)
(edmacro-mode . normal)
(fundamental-mode . normal)
(gud-mode . normal)
(haskell-interactive-mode . normal)
(help-mode . normal)
Expand All @@ -187,13 +183,11 @@ This option will affect the color of position hint and fake region cursor."
(prog-mode . normal)
(py-shell-mode . normal)
(restclient-mode . normal)
(shell-mode . normal)
(telega-chat-mode . normal)
(term-mode . normal)
(text-mode . normal)
(vterm-mode . normal)
(Custom-mode . normal)
)
(Custom-mode . normal))
"A list of rules, each is (major-mode . init-state).
The init-state can only be `motion' or `normal',
Expand Down

0 comments on commit a39509d

Please sign in to comment.