-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathevil-nerd-commenter-operator.el
402 lines (349 loc) · 13.8 KB
/
evil-nerd-commenter-operator.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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
;;; evil-nerd-commenter-operator.el --- evil operator for this program -*- lexical-binding: t -*-
;; Author: Chen Bin <[email protected]>
;; This file is not part of GNU Emacs.
;;; License:
;; 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, 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 this program; if not, write to the Free Software
;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
;;; Commentary:
;; Provides operators for evil-mode.
;;; Code:
(require 'evil)
(require 'evil-nerd-commenter-sdk)
(defvar evilnc-c-style-comment-modes
'(awk-mode
c++-mode
c-mode
css-mode
dart-mode
ess-mode
go-mode
java-mode
javascript-mode
js-mode
js2-mode
perl-mode
php-mode
swift-mode
web-mode)
"Major modes using C comment syntax.")
(defvar evilnc-current-text-object nil
"Internal variable to detect current object working on.")
(defvar evilnc-whole-line-text-objects
'(evil-forward-paragraph
evil-backward-paragraph)
"Comment operator operates on wholes when dealing with these text objects.")
(defvar evilnc-temporary-goal-column 0
"Value of`temporary-goal-column' specifying right edge of rectangle yank.")
(defadvice evil-visual-highlight-block (around evil-visual-highlight-block-hack activate)
"Show overlay over inner comment text object."
ad-do-it
(when (eq this-command 'evilnc-inner-commenter)
(dolist (overlay evil-visual-block-overlays)
(let* ((b (overlay-start overlay))
(e (save-excursion
(goto-char (overlay-end overlay))
(line-end-position))))
(move-overlay overlay b e)))))
(defadvice evil-apply-on-block (around evil-apply-on-block-around-hack activate)
"Yank correct region of inner comment text object."
(let* ((tmp-command last-command))
;; force `evil-apply-on-block' use our temporary-goal-column
(when (> evilnc-temporary-goal-column 0)
(setq temporary-goal-column (max temporary-goal-column
evilnc-temporary-goal-column))
;; Read `evil-apply-on-block'. Note `temporary-goal-column' is used
;; if and only if `last-command' is `next-line' or `previous-line'
(setq last-command 'next-line))
ad-do-it
;; restore last command
(setq last-command tmp-command)
(setq evilnc-temporary-goal-column 0)))
(defun evilnc-expand-to-whole-comment-or-line (start end)
"Expand the comment region defined by START and END.
Make sure all comment is included.
Or expand the region to contain whole lines."
(cond
((evilnc-pure-comment-p start)
(save-excursion
(let* ((newstart start)
(newend end))
;; expand the beginning
(goto-char newstart)
(while (and (>= (1- newstart) (line-beginning-position)) (evilnc-pure-comment-p (1- newstart)))
(setq newstart (1- newstart)))
;; expand the end
(goto-char newend)
(while (and (<= newend (line-end-position)) (evilnc-pure-comment-p newend))
(setq newend (1+ newend)))
(cons newstart newend))))
;; try to expand region to contain whole line if,
;; - currently more than one line text in the region,
;; - a specific text object is touched just before the operator
((and (not (evilnc-sdk-inside-one-line-p start end))
evilnc-current-text-object
(member (car evilnc-current-text-object) evilnc-whole-line-text-objects)
;; 0.5 second
(< (- (float-time (current-time)) (cdr evilnc-current-text-object)) 0.5))
(evilnc-sdk-expand-to-contain-whole-lines start end))
(t
(cons start end))))
;; {{ know text object type to operate on
(defun evilnc-set-current-text-object (text-object)
"Set current TEXT-OBJECT."
(setq evilnc-current-text-object
(cons text-object (float-time (current-time)))))
(defadvice evil-select-an-object (before evilnc-evil-select-an-object-hack activate)
"Figure out text object type."
(let* ((thing (car (ad-get-args 0))))
;; record the thing and timestamp `evil-select-an-object' is called
(evilnc-set-current-text-object thing)))
(defadvice evil-backward-paragraph (before evilnc-evil-backward-paragraph-hack activate)
"Record current text object."
(evilnc-set-current-text-object 'evil-backward-paragraph))
(defadvice evil-forward-paragraph (before evilnc-evil-forward-paragraph-hack activate)
"Record current text object."
(evilnc-set-current-text-object 'evil-forward-paragraph))
;; }}
(evil-define-operator evilnc-comment-operator (start end type)
"Comments text from START to END with TYPE."
(interactive "<R>")
(cond
((eq type 'block)
(let* ((newpos (evilnc-expand-to-whole-comment-or-line start end) ))
(evil-apply-on-block #'evilnc-comment-or-uncomment-region
(car newpos)
(cdr newpos)
nil)))
((and (eq type 'line)
(= end (point-max))
(or (= start end)
(/= (char-before end) ?\n))
(/= start (point-min))
(= (char-before start) ?\n))
(evilnc-comment-or-uncomment-region (1- start) end))
((eq type 'line)
;; comment whole line, for now
(evilnc-comment-or-uncomment-region start
(save-excursion
(goto-char (1- end))
(line-end-position))))
(t
(when (and start end)
(let* ((newpos (evilnc-expand-to-whole-comment-or-line start end)))
(evilnc-comment-or-uncomment-region (car newpos) (cdr newpos))))))
;; place cursor on beginning of line
(if (and (called-interactively-p 'any) (eq type 'line))
(evil-first-non-blank)))
(defun evilnc-comment-or-uncomment-region-then-action (start end commenter &optional action)
"Comment/uncomment between START and END using COMMENTER, then take ACTION."
(evil-with-single-undo
;; yank original text
(evil-yank-lines start end nil 'lines)
(when (evil-visual-state-p)
;; `evil-paste-before' does not work in visual state.
(evil-normal-state))
(cond
(evilnc-original-above-comment-when-copy-and-comment
(let* ((p (point)))
(funcall commenter start end)
(goto-char start)
(when action (funcall action))
(goto-char p)))
(t
(goto-char end)
(when action (funcall action))
;; actual comment operation should happen at last
;; or else "(start end)" is screwed up
(funcall commenter start end)))))
(evil-define-operator evilnc-yank-and-comment-operator (start end)
"(Un)comment and yank the text from START to END."
:move-point (not evilnc-original-above-comment-when-copy-and-comment)
(interactive "<r>")
(evilnc-comment-or-uncomment-region-then-action start
end
evilnc-comment-or-uncomment-region-function))
(evil-define-operator evilnc-copy-and-comment-operator (start end)
"Inserts a commented copy of the text from START to END."
:move-point (not evilnc-original-above-comment-when-copy-and-comment)
(interactive "<r>")
(evilnc-comment-or-uncomment-region-then-action start
end
'comment-region
(lambda () (evil-paste-before 1))))
(defun evilnc-one-line-comment-p (start end)
"Test if text between START and END is one line comment."
(save-excursion
(goto-char start)
(and (<= (line-beginning-position) start)
;; end is the upper limit great than (line-end-position)
(<= end (1+ (line-end-position))))))
(defun evilnc-get-comment-bounds ()
"Return bounds like (cons start end)."
(let* ((b (point))
(e (point))
(col 0)
rlt)
;; decrease start position
(while (evilnc-comment-p (- b 1))
(setq b (- b 1)))
;; increase end
(while (evilnc-comment-p (+ e 1))
(setq e (+ e 1)))
;; we could select extra spaces at the end of comment
;; so we need go back
(let* ((str (save-excursion
(goto-char e)
(evilnc-sdk-cur-line e)))
(empty-line-p (string-match "^[ \t]*$" str)))
(when empty-line-p
;; empty line plus line feed
(setq e (- e (length str) 1))))
(cond
((>= b e)
(setq rlt nil))
((evilnc-one-line-comment-p b e)
;; contract from start position
(while (not (evilnc-pure-comment-p b))
(setq b (+ b 1)))
;; contract from end position
(while (not (evilnc-pure-comment-p e))
(setq e (- e 1)))
(if (< b e) (setq rlt (cons b (1+ e)))))
(t
;; multi-line comment
(setq rlt (cons b (1+ e)))))
rlt))
(defun evilnc-adjusted-comment-end (b e)
"Adjust comment end of region between B and E."
(let* ((next-end-char (evilnc-get-char (- e 2)))
(end-char (evilnc-get-char (- e 1))))
;; avoid selecting CR/LF at the end of comment
(while (and (< b e)
(memq (evilnc-get-char (- e 1)) '(10 13)))
(setq e (- e 1)))
;; avoid selecting comment limiter
(cond
((and (memq major-mode evilnc-c-style-comment-modes)
(= end-char ?/)
(= next-end-char ?*))
;; avoid selecting the ending comment limiter "*/"
(setq e (- e 2))
(while (and (> e b)
(= (evilnc-get-char (- e 1)) ?*))
(setq e (- e 1))))
(t
;; other languages we can safely use font face
(while (and (> e b)
(evilnc-comment-delimiter-p (- e 1)))
(setq e (- e 1)))))
e))
(defun evilnc-c-style-comment-p (pos)
"Is C style comment at POS?"
(and (memq major-mode evilnc-c-style-comment-modes)
(= (evilnc-get-char pos) ?/)
(= (memq (evilnc-get-char (1+ pos)) '(?/ ?*)))))
(defun evilnc-comment-column-bounds (start end &optional c-style)
"From START to END find column bounds of rectangle selection.
Return (cons col-min col-max) or nil. If C-STYLE is t,
we are processing C like language."
(let* ((col-min most-positive-fixnum)
(col-max 0))
(while (< start end)
(when (and (not (evilnc-whitespace-p start))
(evilnc-pure-comment-p start)
(not (or (evilnc-comment-delimiter-p start)
(and c-style
(memq (evilnc-get-char start) '(?/ ?*))))))
(let* ((col (evil-column start)))
(if (< col col-min)
(setq col-min col))
(if (> col col-max)
(setq col-max col))))
(setq start (1+ start)))
(if (< col-min col-max)
(cons col-min col-max))))
(evil-define-text-object evilnc-inner-commenter (&optional count start end type)
"An inner comment text object."
(let* ((bounds (evilnc-get-comment-bounds))
b
e
c-style)
(cond
(bounds
(setq b (car bounds))
(setq e (cdr bounds))
(cond
((setq c-style (evilnc-c-style-comment-p b))
(while (and (< b e)
(or (evilnc-whitespace-p b)
(evilnc-line-end-p b)
(memq (evilnc-get-char b) '(?/ ?*))))
(setq b (1+ b)))
(while (and (< b e)
(or (evilnc-whitespace-p e)
(evilnc-line-end-p e)
(memq (evilnc-get-char e) '(?/ ?*))))
(setq e (1- e)))
(setq e (1+ e))
(setq b (save-excursion
(goto-char b)
(forward-word 1)
(forward-word -1)
(point))))
(t
(setq b (save-excursion
(goto-char (car bounds))
(forward-word 1)
(forward-word -1)
(point)))
(setq e (save-excursion
(goto-char (cdr bounds))
(goto-char (evilnc-adjusted-comment-end b (line-end-position)))
(point)))))
(cond
((evilnc-one-line-comment-p b e)
;; keep move e to the end of comment
(evil-range b ;; (if c-style (1+ e) e)
e))
(t
;; multi-line comment
(let* ((col-b (evil-column b))
(col-bounds (evilnc-comment-column-bounds b e c-style)))
(cond
(col-bounds
(if (> col-b (car col-bounds))
(setq b (- b (- col-b (car col-bounds)))))
(setq evilnc-temporary-goal-column (cdr col-bounds)))
(t
(setq evilnc-temporary-goal-column (evil-column e)))))
(evil-range b e 'block :expanded t))))
(t
(error "Not inside a comment")))))
(evil-define-text-object evilnc-outer-commenter (&optional count start end type)
"An outer comment text object."
(let* ((bounds (evilnc-get-comment-bounds)))
(cond
(bounds
(evil-range (car bounds) (cdr bounds) 'exclusive :expanded t))
(t
(error "Not inside a comment")))))
(evil-define-text-object evilnc-inner-comment (&optional count start end type)
"An inner comment text object."
(evilnc-inner-commenter count start end type))
(evil-define-text-object evilnc-outer-comment (&optional count start end type)
"An outer comment text object."
(evilnc-outer-commenter count start end type))
(provide 'evil-nerd-commenter-operator)
;;; evil-nerd-commenter-operator.el ends here
;; Local Variables:
;; no-byte-compile: t
;; End: