-
Notifications
You must be signed in to change notification settings - Fork 4
/
macrursors.el
607 lines (552 loc) · 20.9 KB
/
macrursors.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
;;; macrursors.el --- Macro visualizer -*- lexical-binding: t; -*-
;;; Commentary:
;; visualization as to where the macros will take place
;; and reduce the brainwork needed to create the macro.
;; just does it for you
;; 1. Select all like something (selection, word, sexp, line, etc)
;; 2. Place fake cursor at every point to indicate where the macros
;; will be executed
;; 3. Automatically starts defining the macro.
;; 4. When done execute the macro at every point.
;; Heavily insired by meow's beacon-mode.
;; There are quite a few code snippets from meow's beacon-mode.
;; Thanks to meow's devs for doing a lot of the heavy lifting,
;; both conceptually and physically.
;; The faces were inspired by multiple-cursors.
;; TODO:
;; - Support for add cursor on click
;; - DONE Cursors on lines directly above/below the point
;; - Documentation
;;; Code:
(require 'cl-lib)
(require 'mouse)
(require 'thingatpt)
(defvar-local macrursors--overlays nil)
(defvar-local macrursors--insert-enter-key nil)
(defgroup macrursors nil
"Macrursors, a multi-edit tool for GNU Emacs."
:group 'editing)
(defface macrursors-cursor-face
'((t (:inverse-video t)))
"The face used for fake cursors."
:group 'macrursors)
(defface macrursors-cursor-bar-face
`((t (:height 1 :background ,(face-attribute 'cursor :background))))
"The face used for fake cursors if the `cursor-type' is bar."
:group 'macrursors)
(defcustom macrursors-match-cursor-style t
"If non-nil, attempt to match the cursor style that the user has selected.
Namely, use vertical bars if the user has configured Emacs to use that cursor.
If nil, just use standard rectangle cursors for all fake cursors.
In some modes/themes, the bar fake cursors are either not
rendered or shift text."
:type '(boolean)
:group 'macrursors)
(defface macrursors-region-face
'((t :inherit region))
"The face used for fake regions."
:group 'macrursors)
(defcustom macrursors-pre-finish-hook nil
"Hook run before macros are applied.
Useful for optizationing the speed of the macro application.
A simple solution is to disable all minor modes that are purely
aesthetic in `macrursors-pre-finish-hook'
and re-enable them in `macrursors-post-finish-hook'."
:type 'hook
:group 'macrursors)
(defcustom macrursors-post-finish-hook nil
"Hook run after macros are applied.
Useful for optizationing the speed of the macro application.
A simple solution is to disable all minor modes that are purely
aesthetic in `macrursors-pre-finish-hook'
and re-enable them in `macrursors-post-finish-hook'."
:type 'hook
:group 'macrursors)
(defcustom macrursors-apply-keys "C-;"
"The bind to end and apply the macro recorded."
:type 'key-sequence
:group 'macrursors)
(defvar-local macrursors--instance nil
"The thing last used to create macrursors.")
(define-minor-mode macrursors-mode
"Minor mode for when macrursors in active."
:lighter macrursors-mode-line
:keymap
(let ((map (make-sparse-keymap)))
(define-key map (kbd macrursors-apply-keys) #'macrursors-end)
(define-key map (kbd "C-g") #'macrursors-early-quit)
map))
(defcustom macrursors-mode-line
'(" MAC:" (:eval (if macrursors--overlays
(format (propertize "%d/%d" 'face 'font-lock-warning-face)
(1+ (cl-count-if (lambda (p) (< p (point))) macrursors--overlays
:key #'overlay-start))
(1+ (length macrursors--overlays)))
(propertize "1/1" 'face 'font-lock-warning-face))))
"Mode-line format for Macrursors."
:type 'string
:risky t
:group 'macrursors)
(defun macrursors--inside-secondary-selection ()
(and-let*
((buf (overlay-buffer mouse-secondary-overlay))
((eq buf (current-buffer))))
(<= (overlay-start mouse-secondary-overlay)
(point)
(overlay-end mouse-secondary-overlay))))
;;;; Macrursor overlay manipulation functions
;; TODO maybe add support for multiple types of cursor types
(defun macrursors--add-overlay-at-point (pos)
"Create an overlay to draw a fake cursor at POS."
(let* ((cursor-type (if (eq cursor-type t)
(frame-parameter nil 'cursor-type)
cursor-type))
(ov (make-overlay pos (if (eq cursor-type 'bar)
pos
(1+ pos)))))
(overlay-put ov 'face
(cond
((not macrursors-match-cursor-style) 'macrursors-cursor-face)
((or (eq cursor-type 'bar)
(and (listp cursor-type)
(eq (car cursor-type) 'bar))) 'macrursors-cursor-bar-face)
(t 'macrursors-cursor-face)))
(overlay-put ov 'macrursors-type 'cursor)
(push ov macrursors--overlays)))
(defun macrursors--remove-overlays ()
"Remove all overlays from current buffer."
(mapc #'delete-overlay macrursors--overlays)
(setq macrursors--overlays nil))
(defun macrursors--get-overlay-positions (&optional overlays)
"Return a list with the position of all the cursors in `macrursors--overlays'.
If OVERLAYS in non-nil, return a list with the positions of OVERLAYS."
(mapcar
#'overlay-start
(or overlays macrursors--overlays)))
;;;; Generic functions and commands for creating macrursors.
(defun macrursors--instance-with-bounds (&optional regexp)
"Return an appropriate string or object-type (symbol) for
creating macrursors.
The returned format is a list of the string/symbol and its
beginning and ending positions."
(cond
;; Provided regexp -- Use as instance
(regexp
(list regexp (point) (point)))
;; Region active -- Mark string from region
((use-region-p)
(when (< (point) (mark))
(exchange-point-and-mark))
(list (regexp-quote
(buffer-substring-no-properties
(region-beginning) (region-end)))
(region-beginning)
(region-end)))
;; Cursors active -- reuse instance
(macrursors-mode
(let ((region macrursors--instance))
(list region
(cond
((stringp region) (- (point) (length region)))
((symbolp region) (car (bounds-of-thing-at-point region)))
(t end))
(point))))
;; Mark symbol at point
((when-let* ((symb (thing-at-point 'symbol))
(bounds (bounds-of-thing-at-point 'symbol))
(symb-beg (car bounds))
(symb-end (cdr bounds)))
(goto-char symb-end)
(list (concat "\\_<" (regexp-quote (substring-no-properties symb)) "\\_>")
symb-beg symb-end)))))
(defun macrursors--mark-all-instances-of (string orig-point &optional end)
(while (re-search-forward string end t)
(unless (= (point) orig-point)
(macrursors--add-overlay-at-point (point)))))
;;;###autoload
(defun macrursors-mark-all-instances-of (&optional regexp)
(interactive)
(pcase-let* ((selection-p (macrursors--inside-secondary-selection))
(start (if selection-p
(overlay-start mouse-secondary-overlay)
0))
(end (and selection-p
(overlay-end mouse-secondary-overlay)))
(`(,region ,_ ,orig-point) (macrursors--instance-with-bounds regexp)))
(goto-char orig-point)
(save-excursion
(goto-char start)
(cond
((stringp region)
(macrursors--mark-all-instances-of region orig-point end))
((symbolp region)
(funcall (intern (concat "macrursors-mark-all-"
(symbol-name region) "s"))))))
(when (use-region-p) (deactivate-mark))
(setq macrursors--instance region)
(macrursors-start)))
(defun macrursors--mark-next-instance-of (string &optional end)
(let ((cursor-positions (macrursors--get-overlay-positions))
(matched-p))
(while (and (setq matched-p
(re-search-forward string end t 1))
(member (point) cursor-positions)))
(if (or (not matched-p)
(> (point) (or end (point-max)))
(member (point) cursor-positions))
(message "No more matches.")
(macrursors--add-overlay-at-point (point)))))
;;;###autoload
(defun macrursors-mark-next-instance-of (&optional arg)
(interactive "p")
(when defining-kbd-macro (end-kbd-macro))
(pcase-let ((search-end (and (macrursors--inside-secondary-selection)
(overlay-end mouse-secondary-overlay)))
(`(,region ,_ ,end) (macrursors--instance-with-bounds)))
(save-excursion
(cond
((< arg 0) ; Remove cursors
(dotimes (_ (- arg))
(and macrursors--overlays
(delete-overlay (car macrursors--overlays)))
(cl-callf cdr macrursors--overlays)))
((stringp region) ; Mark next instance of some string
(goto-char end)
(dotimes (_ arg)
(macrursors--mark-next-instance-of region search-end))
(setq macrursors--instance region)
(macrursors-start))
; No region or symbol-name, mark line
(t (macrursors-mark-next-line arg search-end))))))
(defun macrursors--mark-previous-instance-of (string &optional start)
(let ((cursor-positions (macrursors--get-overlay-positions))
(matched))
(while (and (setq matched
(re-search-forward string start t -1))
(member (match-end 0) cursor-positions)))
(if (or (not matched)
(<= (point) (or start (point-min)))
(member (match-end 0) cursor-positions))
(message "No more matches.")
(macrursors--add-overlay-at-point (match-end 0)))))
;;;###autoload
(defun macrursors-mark-previous-instance-of (&optional arg)
(interactive "p")
(when defining-kbd-macro (end-kbd-macro))
(pcase-let ((search-start (if (macrursors--inside-secondary-selection)
(overlay-start mouse-secondary-overlay)
0))
(`(,region ,beg ,end) (macrursors--instance-with-bounds)))
(save-excursion
(cond
((< arg 0) ; Remove cursors
(dotimes (_ (- arg))
(and macrursors--overlays
(delete-overlay (car macrursors--overlays)))
(cl-callf cdr macrursors--overlays)))
((stringp region) ; Mark next instance of some string
(goto-char beg)
(dotimes (_ arg)
(macrursors--mark-previous-instance-of region search-start))
(setq macrursors--instance region)
(macrursors-start))
; No region or symbol-name, mark line
(t (macrursors-mark-previous-line arg search-start))))))
(defun macrursors--forward-number ()
(interactive)
(let ((closest-ahead (save-excursion (search-forward-regexp "[0-9]" nil t))))
(when closest-ahead
(push-mark)
(goto-char closest-ahead))))
;;;; Commands to create macrursors from Isearch
(defun macrursors--isearch-regexp ()
(or isearch-success (user-error "Nothing to match."))
(prog1
(cond
((functionp isearch-regexp-function)
(funcall isearch-regexp-function isearch-string))
(isearch-regexp-function (word-search-regexp isearch-string))
(isearch-regexp isearch-string)
(t (regexp-quote isearch-string)))))
;;;###autoload
(defun macrursors-mark-from-isearch ()
(interactive)
(let* ((regexp (macrursors--isearch-regexp))
(selection-p (macrursors--inside-secondary-selection))
(search-start (if selection-p
(overlay-start mouse-secondary-overlay)
0))
(search-end (and selection-p
(overlay-end mouse-secondary-overlay)))
orig-point)
(goto-char (max (point) isearch-other-end))
(isearch-exit)
(setq orig-point (point))
(save-excursion
(goto-char search-start)
(macrursors--mark-all-instances-of regexp orig-point search-end))
(setq macrursors--instance regexp)
(macrursors-start)))
;;;###autoload
(defun macrursors-mark-next-from-isearch (&optional arg)
(interactive "p")
(let* ((regexp (macrursors--isearch-regexp))
(search-end (and (macrursors--inside-secondary-selection)
(overlay-end mouse-secondary-overlay))))
(goto-char (max (point) isearch-other-end))
(isearch-exit)
(save-excursion
(dotimes (_ arg)
(macrursors--mark-next-instance-of regexp search-end)))
(setq macrursors--instance regexp)
(macrursors-start)))
;;;###autoload
(defun macrursors-mark-previous-from-isearch (&optional arg)
(interactive "p")
(let* ((regexp (macrursors--isearch-regexp))
(search-start (and (macrursors--inside-secondary-selection)
(overlay-start mouse-secondary-overlay)))
orig-point)
(setq orig-point (min (point) isearch-other-end))
(goto-char (max (point) isearch-other-end))
(isearch-exit)
(save-excursion
(goto-char orig-point)
(dotimes (_ arg)
(macrursors--mark-previous-instance-of regexp search-start)))
(setq macrursors--instance regexp)
(macrursors-start)))
;;;; Commands to create macrursors at syntactic units ("things")
;;;###autoload
(defmacro macrursors--defun-mark-all (name thing func)
`(defun ,name ()
(interactive)
(when mark-active (deactivate-mark))
(let ((end-of-thing (cdr (bounds-of-thing-at-point ,thing))))
(if end-of-thing
(goto-char end-of-thing)
(funcall ,func)))
(let ((orig-point (point))
(start (if (macrursors--inside-secondary-selection)
(overlay-start mouse-secondary-overlay)
0))
(end (if (macrursors--inside-secondary-selection)
(overlay-end mouse-secondary-overlay)
(point-max))))
(save-excursion
(goto-char start)
(while (and (let ((curr (point)))
(funcall ,func)
(not (= (point) curr)))
(<= (point) end))
(unless (= (point) orig-point)
(macrursors--add-overlay-at-point (point)))))
(setq macrursors--instance ,thing)
(macrursors-start))))
;;;###autoload
(macrursors--defun-mark-all macrursors-mark-all-words
'word
#'forward-word)
;;;###autoload
(macrursors--defun-mark-all macrursors-mark-all-symbols
'symbol
(lambda ()
(call-interactively #'forward-symbol)))
;;;###autoload
(macrursors--defun-mark-all macrursors-mark-all-lists
'list
#'forward-list)
;;;###autoload
(macrursors--defun-mark-all macrursors-mark-all-sexps
'sexp
#'forward-sexp)
;;;###autoload
(macrursors--defun-mark-all macrursors-mark-all-defuns
'defun
#'end-of-defun)
;;;###autoload
(macrursors--defun-mark-all macrursors-mark-all-numbers
'number
#'macrursors--forward-number)
;;;###autoload
(macrursors--defun-mark-all macrursors-mark-all-sentences
'sentence
#'forward-sentence)
;; FIXME there is no forward-url function
;; (macrursors--defun-mark-all macrursors-mark-all-urls
;; 'url
;; #'forward-url)
;; FIXME there is no forward-email function
;; (macrursors--defun-mark-all macrursors-mark-all-sexp
;; 'word
;; #'forward-sexp)
;;;###autoload
(defun macrursors-mark-next-line (arg &optional search-end)
(interactive (list (prefix-numeric-value current-prefix-arg)
(and (macrursors--inside-secondary-selection)
(overlay-end mouse-secondary-overlay))))
(when defining-kbd-macro (end-kbd-macro))
(let ((col (current-column))
bounded)
(save-excursion
(if (< arg 0)
(dotimes (_ (- arg))
(and macrursors--overlays
(delete-overlay (car macrursors--overlays)))
(cl-callf cdr macrursors--overlays))
(dotimes (_ arg)
(while (and (setq bounded
(and (line-move-1 1 'no-error)
(move-to-column col)
(<= (point) (or search-end (point-max)))))
(member (point) (macrursors--get-overlay-positions))))
(if bounded
(macrursors--add-overlay-at-point (point))
(message "No more lines below."))))
(when bounded
(setq macrursors--instance 'line)
(macrursors-start)))))
;;;###autoload
(defun macrursors-mark-previous-line (arg &optional search-beg)
(interactive (list (prefix-numeric-value current-prefix-arg)
(and (macrursors--inside-secondary-selection)
(overlay-start mouse-secondary-overlay))))
(when defining-kbd-macro (end-kbd-macro))
(let ((col (current-column))
bounded)
(save-excursion
(if (< arg 0)
(dotimes (_ (- arg))
(and macrursors--overlays
(delete-overlay (car macrursors--overlays)))
(cl-callf cdr macrursors--overlays))
(dotimes (_ arg)
(while (and (setq bounded
(and (line-move-1 -1 'no-error)
(move-to-column col)
(>= (point) (or search-beg (point-min)))))
(member (point) (macrursors--get-overlay-positions))))
(if bounded
(macrursors--add-overlay-at-point (point))
(message "No more lines above."))))
(when bounded
(setq macrursors--instance 'line)
(macrursors-start)))))
;;;###autoload
(defun macrursors-mark-all-lines ()
(interactive)
(when mark-active (deactivate-mark))
(let ((start (if (macrursors--inside-secondary-selection)
(overlay-start mouse-secondary-overlay)
0))
(end (if (macrursors--inside-secondary-selection)
(overlay-end mouse-secondary-overlay)
(point-max)))
(col (current-column)))
(save-excursion
(while (and (let ((curr (point)))
(forward-line -1)
(move-to-column col)
(not (= (point) curr)))
(>= (point) start))
(macrursors--add-overlay-at-point (point))))
(save-excursion
(while (and (let ((curr (point)))
(forward-line 1)
(move-to-column col)
(not (= (point) curr)))
(<= (point) end))
(macrursors--add-overlay-at-point (point))))
(setq macrursors--instance 'line)
(macrursors-start)))
;;;###autoload
(defun macrursors-mark-all-lines-or-instances ()
"If a selection exists, mark all instances of the selection.
Else, mark all lines."
(interactive)
(if (and transient-mark-mode mark-active (not (eq (mark) (point))))
(macrursors-mark-all-instances-of)
(macrursors-mark-all-lines)))
;;;; Functions that apply the defined macro across all macrursors
(defun macrursors-start ()
"Start kmacro recording, apply to all cursors when terminate."
(interactive)
(macrursors-mode 1)
(call-interactively #'kmacro-start-macro))
(defmacro macrursors--wrap-collapse-undo (&rest body)
"Like `progn' but perform BODY with undo collapsed."
(declare (indent 0) (debug t))
(let ((handle (make-symbol "--change-group-handle--"))
(success (make-symbol "--change-group-success--")))
`(let ((,handle (prepare-change-group))
;; Don't truncate any undo data in the middle of this.
(undo-outer-limit nil)
(undo-limit most-positive-fixnum)
(undo-strong-limit most-positive-fixnum)
(,success nil))
(unwind-protect
(progn
(activate-change-group ,handle)
(prog1 ,(macroexp-progn body)
(setq ,success t)))
(if ,success
(progn
(accept-change-group ,handle)
(undo-amalgamate-change-group ,handle))
(cancel-change-group ,handle))))))
(defun macrursors--apply-command (overlays cmd &optional args)
(when overlays
(save-excursion
(dolist (ov overlays)
(goto-char (overlay-start ov))
(if (commandp cmd)
(call-interactively cmd)
(apply cmd args))))))
(defun macrursors-apply-command (cmd &rest args)
(macrursors--wrap-collapse-undo
(macrursors--apply-command
macrursors--overlays
cmd args)))
(defun macrursors--apply-kmacros ()
"Apply kmacros."
(interactive)
(macrursors-apply-command #'execute-kbd-macro
last-kbd-macro))
(defun macrursors--toggle-modes (func &rest args)
(cond
((not (symbolp func)) (funcall func))
((and (fboundp func)
(string-suffix-p "-mode" (symbol-name func)))
(apply func args)))
;; Return nil to continue hook processing.
nil)
;; NOTE DOES NOT WORK WHEN CALLED FROM M-x!!!
;; FIXME applying time
;;;###autoload
(defun macrursors-end ()
(interactive)
(if (not defining-kbd-macro)
(error "Not defining a macro")
(end-kbd-macro)
(run-hook-wrapped 'macrursors-pre-finish-hook
#'macrursors--toggle-modes -1)
(macrursors--apply-kmacros)
(run-hook-wrapped 'macrursors-post-finish-hook
#'macrursors--toggle-modes +1)
(macrursors--remove-overlays)
(macrursors-mode -1)))
;;;###autoload
(defun macrursors-early-quit ()
"Used to quit out of recording the macro for the cursors."
(interactive)
(if (region-active-p)
(progn
(deactivate-mark)
(when defining-kbd-macro
(end-kbd-macro)
(macrursors-start)))
(when defining-kbd-macro (end-kbd-macro))
(macrursors--remove-overlays)
(macrursors-mode -1)))
(provide 'macrursors)
;;; macrursors.el ends here