forked from meow-edit/meow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meow-core.el
226 lines (196 loc) · 7.66 KB
/
meow-core.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
;;; meow-core.el --- Mode definitions for Meow -*- lexical-binding: t; -*-
;; This file is not part of GNU Emacs.
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License
;; as published by the Free Software Foundation; either version 3
;; of the License, or (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;;; Modes definition in Meow.
;;; Code:
(require 'cl-lib)
(require 'subr-x)
(require 'meow-util)
(require 'meow-command)
(require 'meow-keypad)
(require 'meow-var)
(require 'meow-esc)
(require 'meow-shims)
(require 'meow-beacon)
(require 'meow-helpers)
(meow-define-state insert
"Meow INSERT state minor mode."
:lighter " [I]"
:keymap meow-insert-state-keymap
:face meow-insert-cursor
(if meow-insert-mode
(run-hooks 'meow-insert-enter-hook)
(when (and meow--insert-pos
meow-select-on-change
(not (= (point) meow--insert-pos)))
(thread-first
(meow--make-selection '(select . transient) meow--insert-pos (point))
(meow--select)))
(run-hooks 'meow-insert-exit-hook)
(setq-local meow--insert-pos nil)))
(meow-define-state normal
"Meow NORMAL state minor mode."
:lighter " [N]"
:keymap meow-normal-state-keymap
:face meow-normal-cursor)
(meow-define-state motion
"Meow MOTION state minor mode."
:lighter " [M]"
:keymap meow-motion-state-keymap
:face meow-motion-cursor)
(meow-define-state keypad
"Meow KEYPAD state minor mode."
:lighter " [K]"
:keymap meow-keypad-state-keymap
:face meow-keypad-cursor
(when meow-keypad-mode
(setq meow--prefix-arg current-prefix-arg
meow--keypad-keymap-description-activated nil
meow--keypad-allow-quick-dispatch t
meow--keypad-keys nil
meow--use-literal nil
meow--use-meta nil
meow--use-both nil)))
(meow-define-state beacon
"Meow BEACON state minor mode."
:lighter " [B]"
:keymap meow-beacon-state-keymap
:face meow-beacon-cursor
(if meow-beacon-mode
(progn
(setq meow--beacon-backup-hl-line (bound-and-true-p hl-line-mode))
(meow--cancel-selection)
(hl-line-mode -1))
(when meow--beacon-backup-hl-line
(hl-line-mode 1))))
;;;###autoload
(define-minor-mode meow-mode
"Meow minor mode.
This minor mode is used by meow-global-mode, should not be enabled directly."
:init-value nil
:interactive nil
:global nil
:keymap meow-keymap
(if meow-mode
(meow--enable)
(meow--disable)))
;;;###autoload
(defun meow-indicator ()
"Indicator showing current mode."
(or meow--indicator (meow--update-indicator)))
;;;###autoload
(define-global-minor-mode meow-global-mode meow-mode
(lambda ()
(unless (minibufferp)
(meow-mode 1)))
:group 'meow
(if meow-mode
(meow--global-enable)
(meow--global-disable)))
(defun meow--enable ()
"Enable Meow.
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)
(setq meow--current-state nil)
(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)
(setq meow--current-state nil)
(let ((cmd (key-binding "a")))
(and
(commandp cmd)
(symbolp cmd)
(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."
(mapc (lambda (state-mode) (funcall (cdr state-mode) -1)) meow-state-mode-alist))
(defun meow--global-enable ()
"Enable meow globally."
(setq-default meow-normal-mode t)
(meow--init-buffers)
(add-hook 'window-state-change-functions #'meow--on-window-state-change)
(add-hook 'minibuffer-setup-hook #'meow--minibuffer-setup)
(add-hook 'pre-command-hook 'meow--highlight-pre-command)
(add-hook 'post-command-hook 'meow--maybe-toggle-beacon-state)
(add-hook 'suspend-hook 'meow--on-exit)
(add-hook 'suspend-resume-hook 'meow--update-cursor)
(add-hook 'kill-emacs-hook 'meow--on-exit)
(add-hook 'desktop-after-read-hook 'meow--init-buffers)
(meow--enable-shims)
;; meow-esc-mode fix ESC in TUI
(unless window-system
(meow-esc-mode 1))
;; raise Meow keymap priority
(add-to-ordered-list 'emulation-mode-map-alists
`((meow-motion-mode . ,meow-motion-state-keymap)))
(add-to-ordered-list 'emulation-mode-map-alists
`((meow-normal-mode . ,meow-normal-state-keymap)))
(add-to-ordered-list 'emulation-mode-map-alists
`((meow-keypad-mode . ,meow-keypad-state-keymap)))
(add-to-ordered-list 'emulation-mode-map-alists
`((meow-beacon-mode . ,meow-beacon-state-keymap)))
(when meow-use-cursor-position-hack
(setq redisplay-highlight-region-function #'meow--redisplay-highlight-region-function)
(setq redisplay-unhighlight-region-function #'meow--redisplay-unhighlight-region-function))
(meow--prepare-face)
(advice-add 'load-theme :after 'meow--prepare-face))
(defun meow--global-disable ()
"Disable Meow globally."
(setq-default meow-normal-mode nil)
(remove-hook 'window-state-change-functions #'meow--on-window-state-change)
(remove-hook 'minibuffer-setup-hook #'meow--minibuffer-setup)
(remove-hook 'pre-command-hook 'meow--highlight-pre-command)
(remove-hook 'post-command-hook 'meow--maybe-toggle-beacon-state)
(remove-hook 'suspend-hook 'meow--on-exit)
(remove-hook 'suspend-resume-hook 'meow--update-cursor)
(remove-hook 'kill-emacs-hook 'meow--on-exit)
(remove-hook 'desktop-after-read-hook 'meow--init-buffers)
(meow--disable-shims)
(meow--remove-modeline-indicator)
(when meow-use-cursor-position-hack
(setq redisplay-highlight-region-function meow--backup-redisplay-highlight-region-function)
(setq redisplay-unhighlight-region-function meow--backup-redisplay-unhighlight-region-function))
(unless window-system
(meow-esc-mode -1))
(advice-remove 'load-theme 'meow--prepare-face))
(provide 'meow-core)
;;; meow-core.el ends here