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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
;;; bespoke vish modal -*- lexical-binding: t; -*-
(require 's)
(require 'dash)
(defun mm/do-nothing () (interactive))
(defmacro mm/compose-command (first cmd then)
"Return the name of a new named function that evaluates all of the forms
in FIRST (a list of forms), calls CMD interactively with no arguments,
then evaluates all of the forms in THEN. The return value of the named
function is the return value of CMD."
(let* ((prefix (sxhash first))
(suffix (sxhash then))
(name (make-symbol (format "%s--f/%s--t/%s" cmd prefix suffix))))
`(progn
(defun ,name ()
,(format "First do %S then run %s then do %S"
first
cmd
then)
(interactive)
(let ((ret nil))
,@first
(setq ret (call-interactively ,cmd))
,@then
ret))
(quote ,name))))
(defmacro mm/post-effect-enabler (until effect local)
"Returns a function that installs a post-command hook that
runs EFFECT. It will remove itself on the first invocation for which
UNTIL is false. Note that this final iteration will still trigger
EFFECT. Note that the command that calls the returned function will NOT
trigger EFFECT or consult UNTIL."
(setq local (eval local))
(let ((name (make-symbol
(format "post-effect--%s--%s"
(sxhash effect) (sxhash until)))))
`(let ((grace-period t))
(defun ,name ()
,(format "Run %S. If not %S then remove from post command hook."
effect until)
(if grace-period
(setq grace-period nil)
(progn
(unless ,until
(remove-hook 'post-command-hook ',name ,local))
,@effect)))
(lambda ()
(add-hook 'post-command-hook ',name 0 ,local)))))
(defconst mm/moves '(("h" . backward-char)
("j" . next-line)
("k" . previous-line)
("l" . forward-char)
("e" . forward-word)
("b" . backward-word)
("E" . forward-sexp)
("B" . backward-sexp)
("0" . move-beginning-of-line)
("$" . move-end-of-line)
("[" . beginning-of-defun)
("]" . end-of-defun)
("{" . backward-paragraph)
("}" . forward-paragraph)
))
(defconst mm/digits '(("1" . digit-argument)
("2" . digit-argument)
("3" . digit-argument)
("4" . digit-argument)
("5" . digit-argument)
("6" . digit-argument)
("7" . digit-argument)
("8" . digit-argument)
("9" . digit-argument)
))
(defconst mm/no-ops `(
,(kbd "<escape>")
"`" "-" "+"
"q" "w" "r" "t" "\\"
"s" "f" ";" "'"
"z" "n" "," "."
"~" "!" "@" "#" "%" "^" "&" "*" "(" ")" "_" "+"
"Q" "W" "R" "T" "Y" "U" "|"
"S" "D" "F" "H" "J" "K" "L" ":" "\"" ,(kbd "<return>")
"Z" "X" "C" "V" "N" "M" "<" ">"
))
(defvar-local mm/state 'mm/normal-state-mode)
(defvar mm/all-states '() )
(defmacro mm/transition-state (to)
`(lambda ()
(interactive)
(funcall mm/state -1)
(funcall ,to 1)))
(defmacro mm/define-state (name lighter color keymap enable disable)
`(progn
(define-minor-mode ,name
"TODO This is a modal state."
:init-value nil
:interactive t
:lighter ,lighter
:keymap ,keymap
(if ,name
(progn ;; enable logic
,@enable
(set-face-background 'cursor ,color)
(setq mm/state ',name))
(progn ;; disable logic
nil ;nonempty?
,@disable)
)
)
(add-to-list 'mm/all-states ',name)
)
)
;; TODO consider an entry action that indents the line? maybe controlled by a variable?
(mm/define-state mm/base-state-mode
" <B>"
"turquoise"
`((,(kbd "<escape>") . ,(mm/transition-state 'mm/normal-state-mode)))
nil
nil)
(mm/define-state mm/normal-state-mode
" <N>"
"lime"
`(,@mm/moves
,@mm/digits
,@(mapcar (lambda (key) (cons key 'keyboard-quit))
mm/no-ops)
("m" . set-mark-command)
(,(kbd "SPC") . set-mark-command)
("u" . undo)
("p" . yank)
;; mixing vi/emacs meanings of yank
("y" . ,(mm/transition-state 'mm/yank-state-mode))
("i" . ,(mm/transition-state 'mm/base-state-mode))
("a" . ,(mm/compose-command ((unless (eq (point) (line-end-position))
(forward-char nil)))
(mm/transition-state 'mm/base-state-mode)
nil))
("I" . ,(mm/compose-command ((beginning-of-line-text nil))
(mm/transition-state 'mm/base-state-mode)
nil))
("A" . ,(mm/compose-command ((move-end-of-line nil))
(mm/transition-state 'mm/base-state-mode)
nil))
("d" . ,(mm/transition-state 'mm/delete-state-mode))
("c" . ,(mm/transition-state 'mm/change-state-mode))
("o" . ,(mm/compose-command ((move-end-of-line nil)
(newline)
(funcall indent-line-function))
(mm/transition-state 'mm/base-state-mode)
nil))
("O" . ,(mm/compose-command ((move-beginning-of-line nil)
(open-line 1)
(funcall indent-line-function))
(mm/transition-state 'mm/base-state-mode)
nil)))
nil
nil)
(mm/define-state mm/delete-state-mode
" <D>"
"red"
`(,@mm/moves
,@mm/digits
,@(mapcar (lambda (key) (cons key 'keyboard-quit))
mm/no-ops)
("m" . mm/do-nothing)
("d" . ,(mm/compose-command ((pop-mark)
(move-beginning-of-line nil)
(push-mark)
(next-line)
(move-beginning-of-line nil))
'mm/do-nothing
nil))
)
;; enable
((let* ((transition (mm/transition-state 'mm/normal-state-mode))
(disable-handler
(mm/post-effect-enabler
;; hack to check if it was a prefix command
(internal-echo-keystrokes-prefix)
((unless (internal-echo-keystrokes-prefix)
(funcall transition)))
t)))
(push-mark)
(setq-local mm/disable-delete nil)
;; TODO do these trigger before or after post command
(add-hook 'window-selection-change-functions
(lambda (window)
(setq-local mm/disable-delete t))
0 t)
(add-hook 'window-buffer-change-functions
(lambda (window)
(setq-local mm/disable-delete t))
0 t)
(funcall disable-handler)))
((progn ;; disable
;; do deletion on mode exit
(unless (or mm/disable-delete
(eq this-command 'keyboard-quit))
(kill-region nil nil t))
)))
;; should be a copy of delete mode, but switches to base after
(mm/define-state mm/change-state-mode
" <C>"
"red"
`(,@mm/moves
,@mm/digits
,@(mapcar (lambda (key) (cons key 'keyboard-quit))
mm/no-ops)
("m" . mm/do-nothing)
("c" . ,(mm/compose-command ((pop-mark)
(move-beginning-of-line nil)
(push-mark)
(move-end-of-line nil))
'mm/do-nothing
nil)))
;; enable
((let* ((transition-n (mm/transition-state 'mm/normal-state-mode))
(transition-b (mm/transition-state 'mm/base-state-mode))
(disable-handler
(mm/post-effect-enabler
;; hack to check if it was a prefix command
(internal-echo-keystrokes-prefix)
((unless (internal-echo-keystrokes-prefix)
(if (eq this-command 'keyboard-quit)
(funcall transition-n)
(funcall transition-b))))
t)))
(push-mark)
(setq-local mm/disable-delete nil)
;; TODO do these trigger before or after post command
(add-hook 'window-selection-change-functions
(lambda (window)
(setq-local mm/disable-delete t))
0 t)
(add-hook 'window-buffer-change-functions
(lambda (window)
(setq-local mm/disable-delete t))
0 t)
(funcall disable-handler)))
((progn ;; disable
;; do deletion on mode exit
(unless (or mm/disable-delete
(eq this-command 'keyboard-quit))
(kill-region nil nil t))
)))
(mm/define-state mm/yank-state-mode
" <Y>"
"purple"
`(,@mm/moves
,@mm/digits
,@(mapcar (lambda (key) (cons key 'keyboard-quit))
mm/no-ops)
("m" . ,(mm/compose-command ((pop-mark))
'mm/do-nothing
nil))
("y" . ,(mm/compose-command ((pop-mark)
(move-beginning-of-line nil)
(push-mark)
(move-end-of-line nil))
'mm/do-nothing
nil))
)
;; enable
((let* ((transition (mm/transition-state 'mm/normal-state-mode))
(disable-handler
(mm/post-effect-enabler
;; hack to check if it was a prefix command
(internal-echo-keystrokes-prefix)
((unless (internal-echo-keystrokes-prefix)
(funcall transition)))
t)))
(push-mark)
(setq-local mm/disable-delete nil)
(funcall disable-handler)))
((progn ;; disable
(unless (or mm/disable-delete
(eq this-command 'keyboard-quit))
(copy-region-as-kill nil nil t))
)))
(setq minions-prominent-modes (append mm/all-states minions-prominent-modes))
(defun mm/enable-normal-state ()
(interactive)
(unless (minibufferp)
(mm/normal-state-mode 1)))
(define-minor-mode mm/global-modal-mode
"Makes buffers start in normal state."
:global t
:interactive t
(if mm/global-modal-mode
(progn (add-hook 'after-change-major-mode-hook 'mm/enable-normal-state)
(mm/enable-normal-state))
(remove-hook 'after-change-major-mode-hook 'mm/enable-normal-state)))
;; And we're off!
(mm/global-modal-mode 1)
|