This repository was archived by the owner on Sep 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathisolate.el
808 lines (680 loc) · 27.4 KB
/
isolate.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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
;;; isolate.el --- Surrounding tool with flexible customizations.
;; Author: Yuan Fu <[email protected]>
;; URL: https://github.com/casouri/isolate
;; Version: 1.2
;; Keywords: convenience
;; Package-Requires: ((emacs "25"))
;;; Commentary:
;;
;; This is a surrounding tool.
;; It features powerful and easy customization system.
;; You can create complex regexp rules easily.
;;
;; More information in README.org.
;;; Code:
;;
;;; Custom
(defgroup isolate
'((isolate-separator custom-variable))
"Isolate (surround) region with pairs."
:group 'convenience
:prefix "isolate-")
(defcustom isolate-separator ","
"The separator that splits left string into segments.
It can be a regexp expression but I suggest to use a single character.
By \"character\" I mean \"a string that has a length of 1\"."
:group 'isolate
:type 'string)
;;; Variable
;;
;; Not all variables, some are under Delete and Change heading.
;;;; Public
(defvar isolate--left-beg nil
"Mark of beginning of left segments.")
(defvar isolate--left-end nil
"Mark of end of left segments.")
(defvar isolate--right-beg nil
"Mark of beginning of right segments.")
(defvar isolate--right-end nil
"Mark of end of right segments.")
(defvar isolate-quick-shortcut-list
'((:from "]" :to "[, ")
(:from ")" :to "(, ")
(:from "}" :to "{, ")
(:from ">" :to "<, "))
"Shortcuts for `isolate-quick-xxx' functions.
For example, by default \"]\" is mapped to \"[ \", etc.
Each element is an plist representing a shortcut.
Each shortcut have three possible keys: :from, :to and :condition.
:from and :to are strings \(not regexp\),
:condition is a function that takes user input as argument.
:condition is optional.
If :condition exists and returns nil, the shortcut will be ignored.")
(defvar isolate-pair-list
'((:to-left "`" :to-right "'" :no-regexp t :condition (lambda (_) (if (equal major-mode 'emacs-lisp-mode) t nil)))
(:to-left "(" :to-right ")")
(:to-left "[" :to-right "]" :no-regexp t)
(:to-left "{" :to-right "}")
(:to-left "<" :to-right ">")
(:from "<\\([^ ]+\\).*>" :to-right (lambda (left) (format "</%s>" (match-string 1 left))))
(:to-left "\\{begin}" :to-right "\\{end}")
(:from "org-src" :to-left "#+BEGIN_SRC\n" :to-right "#+END_SRC\n" :no-regexp t))
"Matching pairs.
Each element is an plist with five possible keys: :from, :to-left, :to-right, :no-regexp and :condition.
Only (:from or :to-left) and :to-right are required.
1. If only :to-left, and it equal to user input,
and matches and condition passes,
:to-left is used as left of pair,
:to-right is used as right of pair.
2. If only :from, and the regexp of :from matches user input,
user-input is used as left of pair
and :to-right is used as right of pair.
3. If both :from and :to-left exists,
:from as regexp is used to match user-input,
if it matches, :to-left is used as left of pair
and :to-right is used as right of pair.
In addition, :to-left and :to-right can be a function
that takes user input as argument and return a string.
If they are functions, and you have a regexp :from,
you can use (match-string num user-input) to get
regexp matched groups.
:condition, if exist, should be a function
that takes user input as argument and return a boolean.
You can use it to check major modes, etc.
:no-regexp only affects delete commands,
if you want to search the matched pair plainly by text
rather than by regexp, add :no-regexp t.
This is especially important for pairs that contains
regexp keywords such as [, \\, +, etc.
A word of :from:
\"^\" and \"$\" are added automatically to from before matching.
Also don't forget regexp escapes.")
(defvar isolate-setup-hook nil
"This hook is ran after `isolate--add-setup'.
Which moves the point to the left of the region
and enters `evil-insert-state' if necessary.")
;;;; Private
(defvar isolate--left-overlay nil
"Overlay used for highlighting.")
(defvar isolate--right-overlay nil
"Overlay used for highlighting.")
;;; Base function
;;;; Essential
(defun isolate--match-pair (left-segment pair-list &optional search)
"Find matching left and right segment for LEFT-SEGMENT.
Return a cons of (left . right).
LEFT-SEGMENT is not everything left to region,
but one of the segments separated by `isolate-separator'.
PAIR-LIST is the list in which this function looks for match.
SEARCH means this function is called by delete command.
Return (LEFT-SEGMENT . LEFT-SEGMENT) if nothing matches.
This function never returns nil."
(catch 'return
(dolist (pair pair-list)
(let* ((from (plist-get pair :from))
(to-left (plist-get pair :to-left))
(to-right (plist-get pair :to-right))
(condition (plist-get pair :condition))
(no-regexp (plist-get pair :no-regexp))
(quote-func (if (and search no-regexp) 'regexp-quote 'eval)))
(when (and (or (not condition) ; no condition
(and condition (funcall condition left-segment))) ; condition passes
(or (and to-left (equal to-left left-segment)) ; to-left exists & exactly equal
(string-match (format "^%s$" from) left-segment))) ; from matches
(setq to-left (or to-left left-segment))
(throw 'return
(cons (funcall quote-func
(if (functionp to-left)
(funcall to-left left-segment)
to-left))
(funcall quote-func
(if (functionp to-right)
(funcall to-right left-segment)
to-right)))))))
;; if no one matches, return left itself
(cons left-segment left-segment)))
;;;; Helper
(defun isolate--translate-quick-shortcut (left-segment)
"Translate user input LEFT-SEGMENT if a predefined shortcut exists.
Return LEFT-SEGMENT itself if not."
(catch 'return
(dolist (shortcut isolate-quick-shortcut-list)
(let ((from (plist-get shortcut :from))
(to (plist-get shortcut :to))
(condition (plist-get shortcut :condition)))
(when (and (equal from left-segment)
(or (not condition)
(funcall condition left-segment)))
(throw 'return to))))
left-segment))
(defmacro isolate--setup-marker (left-beg left-end right-beg righ-end)
"Helper for setting up markers."
`(save-excursion
;; I changed goto-char to set-mark,
;; because evil's line selection
;; prevents point from moving...
(set-marker (setq isolate--left-beg (point-marker))
,left-beg)
(set-marker (setq isolate--left-end (point-marker))
,left-end)
(set-marker-insertion-type isolate--left-end t)
(set-marker (setq isolate--right-beg (point-marker))
,right-beg)
(set-marker (setq isolate--right-end (point-marker))
,righ-end)
(set-marker-insertion-type isolate--right-end t)))
;;;; Fundamental helper
(define-inline isolate--append (seq elt)
"Append ELT to SEQ destructively."
`(if ,seq
(nconc ,seq (list ,elt))
(setq ,seq (list ,elt))))
(define-inline isolate--push (elt seq)
"Push ELT to SEQ destructively."
`(if ,seq
(push ,elt ,seq)
(setq ,seq (list ,elt))))
(defun isolate--replace-with (str start end)
"Replace strings between START and END with STR.
This function doesn't move point."
(save-excursion
(delete-region start end)
(goto-char start)
(insert str)))
;;; Add
;;;; Command
(defun isolate-quick-add (left-segment)
"Surround region. LEFT-SEGMENT is the left matching pair."
(interactive "cEnter left segment")
;; setup
(isolate--add-setup-marker)
(isolate--add-setup)
(save-excursion
(let* ((left (isolate--translate-quick-shortcut
(char-to-string left-segment)))
(con (isolate--add left))
(actual-left (car con))
(right (cdr con)))
;; insert left
(goto-char isolate--left-beg)
(insert actual-left)
;; insert right
(goto-char isolate--right-beg)
(insert right)))
;; cleanup
(isolate--add-cleanup))
(defalias 'isolate-long-add #'isolate-add-mode)
(defun isolate-add-finish ()
"Finished adding pairs. IOW exit `isolate-add-mode'."
(interactive)
(isolate-add-mode -1))
(defun isolate-add-abort ()
"Abort adding pairs."
(interactive)
(isolate--replace-with "" isolate--left-beg isolate--left-end)
(isolate--replace-with "" isolate--right-beg isolate--right-end)
(isolate-add-mode -1))
(define-minor-mode isolate-add-mode
"Enter this mode for surrounding interactivly.
Hit C-c C-c when finished."
:lighter "ADD"
:keymap (let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-c") #'isolate-add-finish)
(define-key map (kbd "C-c C-q") #'isolate-add-abort)
(define-key map (kbd "C-c C-a") #'isolate-goto-beginning)
(define-key map (kbd "C-c C-e") #'isolate-goto-end)
map)
(if isolate-add-mode
(progn
(isolate--add-setup-marker)
(isolate--add-setup)
(add-hook 'post-command-hook #'isolate--add-hook t t)
(advice-add 'message :around #'isolate--add-echo-advice)
(isolate--disable-autoparens))
(isolate--add-cleanup)
(remove-hook 'post-command-hook #'isolate--add-hook t)
(advice-remove 'message #'isolate--add-echo-advice)
(isolate--disable-autoparens t)))
(defun isolate-goto-beginning ()
"Go to beginning of left."
(interactive)
(goto-char isolate--left-beg))
(defun isolate-goto-end ()
"Go to end of left."
(interactive)
(goto-char isolate--left-end))
;;;; Function
(defun isolate--add (left)
"Return (left . right) base on LEFT."
(let* ((left-list nil)
(actual-left nil)
(right nil)
(right-list nil))
;; match left and construct right
(dolist (segment (split-string left isolate-separator))
(let* ((con (isolate--match-pair segment isolate-pair-list))
(left-segment (car con))
(right-segment (cdr con)))
(isolate--append left-list left-segment)
(isolate--push right-segment right-list)))
;; replace left and insert right
(setq actual-left (funcall 'string-join left-list isolate-separator))
(setq right (apply 'concat right-list))
(cons actual-left right)))
(defvar isolate--left-old-length 0
"Length of left before process.")
(defvar isolate--old-cursor-pos 0
"Cursor position before process.")
(defun isolate--add-hook ()
"Add isolation to region. This function is used in `isolate-add-mode'.
For actual command use `isolate-quick-add' or `isolate-add-mode'."
(let* ((left (buffer-substring-no-properties
isolate--left-beg isolate--left-end))
(con (isolate--add left))
(actual-left (car con))
(right (cdr con)))
(unless (equal left actual-left)
(isolate--replace-with actual-left isolate--left-beg isolate--left-end)
(goto-char isolate--left-end))
(isolate--replace-with right isolate--right-beg isolate--right-end)))
;;;; Helper
(defun isolate--add-echo-advice (old-func &rest arg)
"Indicate user that add mode is active. OLD-FUNC is `message'. ARG are args."
(funcall old-func "[ ADD ] [ Abort: C-c q ] [ Finish: C-c C-c ] [ Jump BEG: C-c a ] [ Jump END: C-c e ]\n\n%s" (apply 'format arg)))
(defun isolate--add-setup-marker ()
"Setup the markers."
(unless isolate--changing
(isolate--setup-marker (region-beginning)
(region-beginning)
(region-end)
(region-end))))
(defun isolate--add-setup ()
"Setup highlight and insert. Have to run after `isolate--xxx-setup-marker'."
;; highlight
(overlay-put
(setq isolate--left-overlay
(make-overlay isolate--left-beg isolate--left-end nil nil t))
'face 'highlight)
(overlay-put
(setq isolate--right-overlay
(make-overlay isolate--right-beg isolate--right-end nil nil t))
'face 'highlight)
;; insert
(goto-char isolate--left-beg)
(deactivate-mark)
(run-hooks 'isolate-setup-insert-hook))
(defun isolate--add-cleanup ()
"Clean up highlight, separator and insert setup."
(save-excursion
;; highlight
(delete-overlay isolate--left-overlay)
(delete-overlay isolate--right-overlay)
;; separator
(goto-char isolate--left-beg)
(while (search-forward isolate-separator isolate--left-end t)
(replace-match ""))))
(defun isolate--disable-autoparens (&optional enable)
"Disable paren auto-completion in `isolate-add-mode'.
If ENABLE non-nil, reenable them.
It also disable evil."
(if (not enable)
(progn
(setq isolate--evil
(if (bound-and-true-p evil-mode)
(progn (evil-mode -1) t)
(if (bound-and-true-p evil-local-mode)
(progn (evil-local-mode -1) t)
nil)))
(setq isolate--smartparens
(if (bound-and-true-p smartparens-mode)
(progn (smartparens-mode -1) t)
nil))
(setq isolate--paredit
(if (bound-and-true-p paredit-mode)
(progn (paredit-mode -1) t)
nil))
(setq isolate--electric-pair
(if (bound-and-true-p electric-pair-mode)
(progn (electric-pair-mode -1) t)
nil)))
(when (bound-and-true-p isolate--evil)
(evil-mode))
(when (bound-and-true-p isolate--smartparens)
(smartparens-mode))
(when (bound-and-true-p isolate--paredit)
(paredit-mode))
(when (bound-and-true-p isolate--electric-pair)
(electric-pair-mode))))
;;; Delete
;;;; Variable
(defvar isolate-delete-extended-pair-list
'((:to-left "\\" :to-right "\\" :no-regexp t)
(:to-left "+" :to-right "+" :no-regexp t)
(:to-left "." :to-right "." :no-regexp t)
(:to-left "*" :to-right "*" :no-regexp t)
(:to-left "?" :to-right "?" :no-regexp t)
(:to-left "^" :to-right "^" :no-regexp t)
(:to-left "$" :to-right "$" :no-regexp t)
(:from "<t>" :to-left "<[^/]+?>" :to-right "</.+?>")
(:from "<\\([^ ]+\\)[^<>]*>"
:to-left (lambda (user-input) (format "<%s *.*?>" (match-string 1 user-input)))
:to-right (lambda (user-input) (format "< *?/%s *?>" (match-string 1 user-input)))))
"Rule list.
Detail see `isolate-pair-list'.")
(defvar isolate--search-level 1
"The level of matching pairs that isolate searches.
Starts from 1, passed to search function as COUNT.
Don't forget to reset to 1 when exit `isolate-delete-mode'.")
(defvar isolate--search-history nil
"History of long delete(change) search.")
(defvar isolate--search-success nil
"If nil, don't delete.")
;;;; Command
;;;;; Quick delete
(defun isolate-quick-delete (left-segment)
"Delete surrouding matched by LEFT-SEGMENT.
Return t if match, nil if no match."
(interactive "cEnter left segment")
;; match & delete
(let ((result (if (isolate--search-pair
(isolate--translate-quick-shortcut
(char-to-string left-segment)))
(progn
(isolate--replace-with "" isolate--left-beg isolate--left-end)
(isolate--replace-with "" isolate--right-beg isolate--right-end)
t)
(message "No balanced match")
nil)))
;; cleanup
(isolate--delete-cleanup-overlay)
result))
;;;;; Long delete
(defvar isolate--delete-minibuffer-map (let ((map (make-composed-keymap
(make-sparse-keymap)
minibuffer-local-map)))
(define-key map (kbd "C-p") #'isolate-search-up)
(define-key map (kbd "C-n") #'isolate-search-down)
map)
"The keymap used in `isolate-long-delete'.")
(defvar isolate--delete-main-buffer nil
"The buffer that edit should apply to.")
(defmacro isolate--delete-with-left (var &rest form)
"Evaluate FORM with VAR as the user inserted regexp."
`(when (>= (point-max) (length "Left regexp: "))
(let ((,var (buffer-substring-no-properties (1+ (length "Left regexp: ")) (point-max))))
,@form)))
(defun isolate--delete-hook ()
"Function run in `post-command-hook'."
;; TODO add check for minibuffer window
(isolate--delete-with-left
left
(with-current-buffer isolate--delete-main-buffer
(when (setq isolate--search-success
(isolate--search-pair left isolate--search-level))
(isolate--delete-update-highlight)))))
(defvar isolate--delete-minibuffer-hook-stage 0
"Recode stage for `isolate--delete-minibuffer-hook'.")
(defun isolate--delete-minibuffer-hook ()
"Stage 0: add self to `minibuffer-setup-hook'.
Stage 1: remove self from `minibuffer-setup-hook', add self to `post-self-insert-hook'.
Stage 2: remove self from `post-self-insert-hook', add `isolate--delete-hook' to `post-command-hook'.
Stage 3: remove `isolate--delete-hook' from `post-command-hook'."
(pcase isolate--delete-minibuffer-hook-stage
(0 (setq isolate--delete-minibuffer-hook-stage 1)
(add-hook 'minibuffer-setup-hook #'isolate--delete-minibuffer-hook))
(1 (setq isolate--delete-minibuffer-hook-stage 2)
(remove-hook 'minibuffer-setup-hook #'isolate--delete-minibuffer-hook)
(add-hook 'post-command-hook #'isolate--delete-hook))
(2 (setq isolate--delete-minibuffer-hook-stage 0)
(remove-hook 'post-command-hook #'isolate--delete-hook))))
(defun isolate-long-delete ()
"Delete surrounding by entering regexp."
(interactive)
(setq isolate--delete-main-buffer (current-buffer))
(let ((minibuffer-local-map isolate--delete-minibuffer-map))
(condition-case nil
(progn
(setq isolate--delete-minibuffer-hook-stage 0)
(isolate--delete-minibuffer-hook)
(isolate--disable-autoparens)
(read-string "Left regexp: ")
(isolate--delete-minibuffer-hook) ; cleanup
(isolate--disable-autoparens t)
(isolate-delete-finish)
(isolate--delete-cleanup))
((quit error)
(isolate--delete-minibuffer-hook) ;cleanup
(isolate--disable-autoparens t)
(isolate--delete-cleanup)))))
(defun isolate-delete-finish ()
"Apply edit."
(interactive)
(when isolate--search-success
(isolate--replace-with "" isolate--left-beg isolate--left-end)
(isolate--replace-with "" isolate--right-beg isolate--right-end)))
(defun isolate--delete-cleanup ()
"Cleanup."
(isolate--delete-cleanup-overlay)
(setq isolate--search-level 1
isolate--search-success (if isolate--changing
isolate--search-success
nil)))
(defun isolate-search-up ()
"Search one level of matching pairs up."
(interactive)
(setq isolate--search-level
(isolate--delete-with-left
left
(with-current-buffer isolate--delete-main-buffer
(setq isolate--search-level (1+ isolate--search-level))
(if (isolate--search-pair left isolate--search-level)
(progn
(isolate--delete-update-highlight)
(message "Up one level"))
;; failed, set level back
(setq isolate--search-level (1- isolate--search-level))
(message "No further match"))
isolate--search-level))))
(defun isolate-search-down ()
"Search one level of matching pairs down."
(interactive)
(setq isolate--search-level
(isolate--delete-with-left
left
(with-current-buffer isolate--delete-main-buffer
(setq isolate--search-level (1- isolate--search-level))
(if (and (>= isolate--search-level 1)
(isolate--search-pair left isolate--search-level))
(progn
(isolate--delete-update-highlight)
(message "Down one level"))
;; failed, set level back
(setq isolate--search-level (1+ isolate--search-level))
(message "No furher match"))
isolate--search-level))))
;;;; Function
(defun isolate--count-match (regexp beg end)
"Count number of occurences of REGEXP between BEG and END in current buffer."
(save-excursion
(goto-char beg)
(let ((count 0))
(while (re-search-forward regexp end t)
(setq count (1+ count)))
count)))
(defmacro isolate--jump-backward ()
"Used in `isolate--find-balance-pair'."
`(progn
(unless (re-search-backward left-regexp nil t)
(throw 'return nil))
(setq left-beg (match-beginning 0)
left-end (match-end 0))))
(defmacro isolate--jump-forward ()
"Used in `isolate--find-balance-pair'."
`(progn
(unless (re-search-forward right-regexp nil t)
(throw 'return nil))
(setq right-beg (match-beginning 0)
right-end (match-end 0))))
(defun isolate--simple-balance-pair (left-regexp right-regexp &optional point count)
"Find the COUNT'th balanced matching pair (LEFT-REGEXP & RIGHT-REGEXP)
centered around POINT.
Count starts from 1.
Nil or omitted COUNT means 0.
Nil or omitted POINT means (point).
Return t if successed, nil if failed.
This is simple version because left-regexp = right-regexp."
(save-excursion
(catch 'return
(let ((count (or count 1))
left-beg
left-end
right-beg
right-end
origin-point)
(setq origin-point (goto-char (or point (point))))
(dotimes (var count)
(isolate--jump-backward))
(goto-char origin-point)
(dotimes (var count)
(isolate--jump-forward))
(isolate--setup-marker left-beg
left-end
right-beg
right-end)
;; return t
t))))
(defun isolate--find-balance-pair (left-regexp right-regexp &optional point count)
"Find the COUNT'th balanced matching pair (LEFT-REGEXP & RIGHT-REGEXP)
centered around POINT.
Count starts from 1.
Nil or omitted COUNT means 0.
Nil or omitted POINT means (point).
Return t if successed, nil if failed."
(if (equal left-regexp right-regexp)
(isolate--simple-balance-pair left-regexp right-regexp point count)
(save-excursion
(catch 'return
(let ((count (or count 1))
left-beg
left-end
right-beg
right-end
origin-point
;; used in step 2 when handels COUNT
old-right-count
new-right-count)
(setq origin-point (goto-char (or point (point))))
(isolate--jump-backward)
;; left count is 1, right-count is 0
;; now point is at the beginning of the first left match
;; jump left until left-count = 1 + right-count
(while (let ((left-count (isolate--count-match left-regexp left-beg origin-point))
(right-count (isolate--count-match right-regexp left-beg origin-point)))
(< (- left-count right-count) 1))
(isolate--jump-backward))
;; point is at the beginning of correct left match
;; now handle COUNT
;; nothing hapends if count <= 1.
(setq old-right-count (isolate--count-match right-regexp left-beg origin-point))
(setq new-right-count old-right-count)
(while (> count 1)
(isolate--jump-backward)
(setq new-right-count (isolate--count-match right-regexp left-beg origin-point))
(setq count (+ count (- new-right-count old-right-count 1)))
(setq old-right-count new-right-count))
;; it's time to find right match.
;; setup right-beg and righ-end
(isolate--jump-forward)
;; jump right until balanced
(while (let ((left-count (isolate--count-match left-regexp left-beg right-beg))
(right-count (isolate--count-match right-regexp left-end right-end)))
(> left-count right-count))
(isolate--jump-forward))
(when (< right-beg origin-point)
(throw 'return nil))
;; done! Now it's balanced!
(isolate--setup-marker left-beg
left-end
right-beg
right-end)
;; return t
t)))))
(defun isolate--search-pair (left &optional count)
"Seach COUNT'th LEFT and matching right segment.
Return t if matched, nil if not.
Note that COUNT starts from 1, not 0. 0 and nil are treated as 1.
The function sets `isolate--left-beg', `isolate--left-end',
`isolate--right-beg' and `isolate--right-end'.
COUNT is like COUNT in `search-backward-regexp'."
(catch 'return
(let ((count (if (or (equal count nil) (equal count 0)) 1 count))
;; special treatment for `isolate-quick-delete'
left-list
left-regexp
right-list
right-regexp
(left-count 0)
(right-count 0))
;; construct left and right regexp
(dolist (segment (split-string left isolate-separator))
(let* ((con (isolate--match-pair
segment
(append isolate-delete-extended-pair-list isolate-pair-list)
t))
(left-pattern (car con))
(right-pattern (cdr con)))
(isolate--append left-list left-pattern)
(isolate--push right-pattern right-list)))
;; construct
(setq left-regexp (apply 'concat left-list))
(setq right-regexp (apply 'concat right-list))
;; search & return
(isolate--find-balance-pair left-regexp right-regexp nil count))))
;;;; Helper
(defun isolate--delete-echo-advice (old-func &rest arg)
"Indicate user that delete mode is active. OLD-FUNC is `message'. ARG are args."
(funcall old-func "[ DELETE ] [ Abort: C-c q ] [ Finish: C-c C-c ] [ New match: C-c s ] [ Up: C-c p] [ Down: C-c n ]\n\n%s" (apply 'format arg)))
(defun isolate--delete-cleanup-overlay ()
"Cleanup overlay."
(when isolate--left-overlay
(delete-overlay isolate--left-overlay))
(when isolate--right-overlay
(delete-overlay isolate--right-overlay)))
(defun isolate--delete-update-highlight ()
"Update highlight."
(isolate--delete-cleanup-overlay)
(overlay-put
(setq isolate--left-overlay (make-overlay isolate--left-beg isolate--left-end))
'face 'highlight)
(overlay-put
(setq isolate--right-overlay (make-overlay isolate--right-beg isolate--right-end))
'face 'highlight))
;;; Change
;;;; Variable
(defvar isolate--changing nil
"If this is t, `isolate--add-setup-marker' doesn't alter markers.")
;;;; Command
(defun isolate-quick-change (from to)
"Change FROM to TO."
(interactive "cChange from\ncChange to")
(let ((isolate--changing t))
(when (isolate-quick-delete from)
(isolate-quick-add to))))
(defun isolate--change-delete-advice (&rest _)
"Start adding. Advice to `isolate-delete-finish'."
(interactive)
(setq isolate--changing nil)
(when isolate--search-success
(setq isolate--search-success nil)
(let ((isolate--changing t))
(advice-remove 'isolate-delete-finish #'isolate--change-delete-advice)
(isolate-add-mode))))
(defun isolate-long-change ()
"Change matching pair of user entered regexp to user enterd pair."
(interactive)
(setq isolate--changing t)
(advice-add 'isolate-delete-finish :after #'isolate--change-delete-advice)
(isolate-long-delete))
;;; Provide
(provide 'isolate)
;;; isolate.el ends here