-
Notifications
You must be signed in to change notification settings - Fork 0
/
mckak.el
182 lines (155 loc) · 6.15 KB
/
mckak.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
;; -*- lexical-binding: t -*-
(require 'multiple-cursors)
(require 'cl)
(require 'mk-macros)
(require 'phi-search)
(require 'evil-core)
(mk/defmotion to-next-word-start ()
"word start"
(select extend)
(skip-syntax-forward "w")
(skip-syntax-forward "^w"))
(mk/defmotion to-next-word-end ()
"word end"
(select extend)
(skip-syntax-forward "^w")
(skip-syntax-forward "w"))
(mk/defmotion to-previous-word-start ()
"previous word start"
(select extend)
(skip-syntax-backward "^w")
(skip-syntax-backward "w"))
(mk/defmotion to-previous-word-end ()
"previous word end"
(select extend)
(skip-syntax-backward "w")
(skip-syntax-backward "^w"))
(mk/defmotion to-next-char ()
"character"
(move extend)
(save-restriction
(narrow-to-region (point) (line-end-position))
(ignore-errors (forward-char))))
(mk/defmotion to-previous-char ()
"previous character"
(move extend)
(save-restriction
(narrow-to-region (point) (line-beginning-position))
(ignore-errors (backward-char))))
(mk/defmotion to-next-line ()
"line"
(move extend)
(next-line))
(mk/defmotion to-previous-line ()
"previous line"
(move extend)
(previous-line))
(defun mk--generate-select-to-next-found-char (char count)
(lambda ()
(interactive)
(dotimes (_ count)
(mk/create-selection
(let ((start (point)) case-fold-search)
(condition-case nil
(progn
(mk/move-to-next-char)
(search-forward (char-to-string char) (line-end-position)))
(error (goto-char start))))))))
(defun mk/select-to-next-found-char (char &optional count)
"Selects COUNT'th found character"
(interactive "cChar:\np")
(setq count (or count 1))
(let ((cmd (mk--generate-select-to-next-found-char char count)))
(call-interactively cmd)
(setq this-command cmd
this-original-command cmd
mc--this-command cmd)))
(add-to-list 'mc/cmds-to-run-for-all 'mk/select-to-next-found-char)
(add-to-list 'mc/cmds-to-run-for-all 'mk/extend-to-next-found-char)
(evil-define-state mckak
"McKak state."
:tag " <K> "
:suppress-keymap t
(progn
;; TODO with the setq evil-default-state lower evil starts with mckak state
;; however this remove-hook only gets called after re-entering it.
;; This makes the first e.g. `w' call flip into visual state,
;; which is exactly what we are trying to avoid.
(remove-hook 'activate-mark-hook 'evil-visual-activate-hook t)))
(add-to-list 'mc/cmds-to-run-once 'evil-mckak-state)
(setq evil-default-state 'mckak)
(define-key evil-insert-state-map [escape] 'evil-mckak-state)
(setq evil-mckak-state-map (copy-keymap evil-normal-state-map))
(define-key evil-mckak-state-map "w" 'mk/select-to-next-word-start)
(define-key evil-mckak-state-map "e" 'mk/select-to-next-word-end)
(define-key evil-mckak-state-map "j" 'mk/move-to-next-line)
(define-key evil-mckak-state-map "k" 'mk/move-to-previous-line)
(define-key evil-mckak-state-map "h" 'mk/move-to-previous-char)
(define-key evil-mckak-state-map "l" 'mk/move-to-next-char)
(define-key evil-mckak-state-map "b" 'mk/select-to-previous-word-start)
(define-key evil-mckak-state-map "v" 'mk/select-to-previous-word-end)
(define-key evil-mckak-state-map "f" 'mk/select-to-next-found-char)
(define-key evil-mckak-state-map "N" 'mc/mark-next-like-this)
(define-key evil-mckak-state-map (kbd "M-s") 'mc/edit-lines)
(define-key evil-mckak-state-map "s" 'mc/mark-all-in-region-regexp)
(define-key evil-mckak-state-map "&" 'mc/vertical-align-with-space)
(define-key evil-mckak-state-map "W" 'mk/extend-to-next-word-start)
(define-key evil-mckak-state-map "E" 'mk/extend-to-next-word-end)
(define-key evil-mckak-state-map "J" 'mk/extend-to-next-line)
(define-key evil-mckak-state-map "K" 'mk/extend-to-previous-line)
(define-key evil-mckak-state-map "H" 'mk/extend-to-previous-char)
(define-key evil-mckak-state-map "L" 'mk/extend-to-next-char)
(define-key evil-mckak-state-map "B" 'mk/extend-to-previous-word-start)
(define-key evil-mckak-state-map "V" 'mk/extend-to-previous-word-end)
(define-key evil-mckak-state-map "F" 'mk/extend-to-next-found-char)
(define-key evil-mckak-state-map (kbd "M-f") 'mk/select-to-previous-found-char)
(define-key evil-mckak-state-map (kbd "M-F") 'mk/extend-to-previous-found-char)
(define-key evil-mckak-state-map "/" 'phi-search)
(define-key evil-mckak-state-map "?" 'phi-search-backward)
(define-key evil-mckak-state-map "%" 'mark-whole-buffer)
(defun mk/kill-region ()
(interactive)
(if (region-active-p)
(call-interactively 'kill-region)
(call-interactively 'delete-char)))
(defun mk/change-region ()
(interactive)
(if (region-active-p)
(call-interactively 'kill-region)
(call-interactively 'delete-char))
(evil-insert-state 1))
;; TODO keep the regions untouched
;; or expand them, still to be decided.
(defun mk/insert ()
(interactive)
(when (region-active-p)
(goto-char (region-beginning)))
(evil-insert-state 1))
;; TODO keep and expand the region.
(defun mk/append ()
(interactive)
(if (region-active-p)
(goto-char (region-end))
(goto-char (1+ (point))))
(evil-insert-state 1))
(define-key evil-mckak-state-map "d" 'mk/kill-region)
(define-key evil-mckak-state-map "c" 'mk/change-region)
(define-key evil-mckak-state-map "i" 'mk/insert)
(define-key evil-mckak-state-map "a" 'mk/append)
(add-to-list 'mc/cmds-to-run-for-all 'mk/kill-region)
(add-to-list 'mc/cmds-to-run-for-all 'mk/change-region)
(add-to-list 'mc/cmds-to-run-for-all 'mk/insert)
(add-to-list 'mc/cmds-to-run-for-all 'mk/append)
(dotimes (i 10)
(define-key evil-mckak-state-map (number-to-string i) 'digit-argument))
;; (global-set-key [escape] 'mckak-local-mode)
;; (define-minor-mode mckak-local-mode
;; "McKak mode - vim meets multiple cursors."
;; :keymap evil-mckak-state-map)
;; (defun mckak-initialize ()
;; (unless (minibufferp)
;; (mckak-local-mode 1)))
;; ;;;### autoload
;; (define-globalized-minor-mode mckak-mode
;; mckak-local-mode mckak-initialize)
(provide 'mckak)