-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisl.el
1251 lines (1149 loc) · 50 KB
/
isl.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
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; isl.el --- Simple incremental search in current-buffer -*- lexical-binding: t -*-
;; Author: Thierry Volpiatto <[email protected]>
;; Copyright (C) 2021~2024 Thierry Volpiatto <[email protected]>
;; Version: 1.0
;; URL: https://github.com/thierryvolpiatto/isearch-light
;; Compatibility: GNU Emacs 25.1+
;; Package-Requires: ((emacs "25.1"))
;; 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 of the License, 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, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; ISL like Incremental-Search-Light. Search regexp(s) incrementaly
;; in current-buffer using multimatch like Helm. By default
;; multimatch is done on 'symbols', but can be toggled to 'lines' with
;; `C-j', see README file for more infos on this and other features.
;; `C-h m' is available at any moment during session for help to see
;; common commands. The starting point is M-x isl-search, bind it to
;; a convenient keybinding of your choice.
;;; Code:
(eval-when-compile (require 'cl-lib))
;; Compatibility
(unless (and (fboundp 'pos-bol) (fboundp 'pos-eol))
(defalias 'pos-bol 'line-beginning-position)
(defalias 'pos-eol 'line-end-position))
(defvar iedit-aborting)
(defvar iedit-read-only-occurrences-overlays)
(defvar iedit-read-only-occurrences-overlays)
(defvar iedit-case-sensitive)
(defvar iedit-occurrences-overlays)
(defvar iedit-mode)
(defvar helm-occur-always-search-in-current)
(defvar hs-minor-mode)
(defvar hs-show-hook)
(declare-function iedit-make-read-only-occurrence-overlay "ext:iedit-lib.el")
(declare-function iedit-make-occurrence-overlay "ext:iedit-lib.el")
(declare-function iedit-update-index "ext:iedit-lib.el")
(declare-function iedit-lib-cleanup "ext:iedit-lib.el")
(declare-function iedit-start "ext:iedit.el")
(declare-function iedit-done "ext:iedit.el")
(declare-function outline-show-entry "outline.el")
(declare-function org-reveal "org.el")
(declare-function helm-multi-occur-1 "ext:helm-occur.el")
(declare-function hs-show-block "hideshow.el")
(declare-function markdown-show-entry "ext:markdown-mode.el")
(declare-function bm-toggle "ext:bm.el")
;; Internals
(defvar isl-pattern "")
(defvar-local isl-last-query nil)
(put 'isl-last-query 'permanent-local t)
(defvar-local isl-last-object nil)
(put 'isl-last-object 'permanent-local t)
(defvar isl-visited-buffers nil)
(defvar isl-current-buffer nil)
(defvar isl--item-overlays nil)
(defvar isl--iterator nil)
(defvar isl--last-overlay nil)
(defvar isl--direction nil)
(defvar isl-initial-pos nil)
(defvar isl--number-results 0)
(defvar isl-history nil)
(defvar isl--yank-point nil)
(defvar isl--quit nil)
(defvar isl--invalid nil)
(defvar isl--window-start nil)
(defvar-local isl--buffer-invisibility-spec nil)
(defconst isl-space-regexp "\\s\\\\s-"
"Match a quoted space in a string.")
(defconst isl--case-fold-choices '(smart nil t))
(defvar isl--case-fold-choices-iterator nil)
(defvar isl-help-buffer-name "*isl help*")
(defvar isl--hidding nil)
(defvar isl--point-min nil)
(defvar isl--point-max nil)
(defvar isl--extra-items-overlays nil)
(defvar isl-search-invisible t)
;; User vars
(defvar isl-timer-delay 0.01)
(defvar isl-help-string
"* ISL help\n
Incremental search in current buffer with multi match (InLine/InSymbol).
** Commands
\\<isl-map>
\\[isl-display-or-quit-help]\t\tDisplay or quit this help buffer
\\[isl-help-quit]\t\tQuit this help buffer
\\[isl-goto-next]\t\tGoto next occurence
\\[isl-goto-prev]\t\tGoto previous occurence
\\[isl-scroll-down]\t\tScroll down
\\[isl-scroll-up]\t\tScroll up
\\[isl-exit-at-point]\t\tExit at current position
\\[abort-recursive-edit]\t\tQuit and restore initial position
\\[isl-yank-word-at-point]\t\tYank word at point
\\[isl-yank-symbol-at-point]\t\tYank symbol at point
\\[isl-recenter]\t\tRecenter current buffer
\\[isl-change-matching-style]\t\tToggle matching style (regexp/litteral)
\\[isl-select-case-fold-search]\t\tChange case fold search (cycle: *=smart, 1=t, 0=nil)
\\[isl-goto-first]\t\tGoto first occurence
\\[isl-goto-last]\t\tGoto last occurence
\\[isl-goto-closest-from-start]\t\tGoto closest occurence from start
\\[isl-jump-to-helm-occur]\t\tJump to helm-occur
\\[isl-jump-to-iedit-mode]\t\tJump to iedit-mode
\\[isl-query-replace]\t\tJump to query replace
\\[isl-show-or-hide-context-lines]\t\tHide or show non matching lines
\\[isl-toggle-multi-search-in-line]\t\tToggle multi search style (InLine/InSymbol)
\\[isl-toggle-invisible-search]\t\tToggle searching in invisible text
\\[isl-bm-this-pos]\t\tAdd bookmark BM to current pos")
(defgroup isl nil
"Search buffers with `isl-search'."
:prefix "isl-"
:group 'matching)
(defcustom isl-search-function #'isl--re-search-forward
"The search function that will be used by default when starting `isl-search'.
Possible values are `isl--re-search-forward' and `search-forward', the
first use regexp matching while the second is using literal matching.
Its value can be changed during `isl-search' session with `\\<isl-map>\\[isl-change-matching-style]'."
:type '(choice
(function :tag "Regexp matching" isl--re-search-forward)
(function :tag "Literal matching" search-forward)))
(defcustom isl-case-fold-search 'smart
"The `case-fold-search' value.
Possible value are nil, t or smart.
Value smart means use `case-fold-search' when upcase chars are detected
in pattern."
:type 'symbol)
(defcustom isl-after-position-string ">"
"The string used to notify in mode-line when position is above initial pos."
:type 'string)
(defcustom isl-before-position-string "<"
"The string used to notify in mode-line when position is below initial pos."
:type 'string)
(defcustom isl-direction-down-string "↓"
"The string used in mode-line to notify search direction."
:type 'string)
(defcustom isl-direction-up-string "↑"
"The string used in mode-line to notify search direction."
:type 'string)
(defcustom isl-warning-char "⚠"
"Used to signal invalid regexp in mode-line."
:type 'string)
(defcustom isl-save-pos-to-mark-ring t
"Save initial position to `mark-ring' on exit when non nil."
:type 'boolean)
(defcustom isl-requires-pattern 1
"Start updating after this number of chars."
:type 'integer)
(defcustom isl-visible-context-lines 1
"Number of lines to show around line when hiding non matching lines."
:type 'integer)
(defcustom isl-noresume-buffers '("*Helm Help*")
"Prevent resuming in these buffers."
:type '(repeat string))
(defcustom isl-multi-search-in-line nil
"Startup with multi search in line by default when non nil.
Otherwise multi search only in symbols.
In buffers containing huge lines or sometimes only one huge line, you
should multi search only in symbols and not in whole line which is
really costly and may take ages or crash Emacs.
You can toggle this at any time with \\<isl-map>\\[isl-toggle-multi-search-in-line]."
:type 'boolean
:initialize 'custom-initialize-changed
:set (lambda (var val)
(set var val)
(if val
(set-face-attribute 'isl-on nil :extend t)
(set-face-attribute 'isl-on nil :extend nil))))
(defcustom isl-no-invisible-search-in-modes nil
"Do not search invisible text in these modes."
:type '(repeat symbol))
(defface isl-match
'((t :background "Brown4"))
"Face used to highlight the items matched.")
(defface isl-match-items
'((t :background "SaddleBrown" :foreground "black"))
"Face used to highlight the items matched inside line.")
(defface isl-on
'((t :background "SandyBrown"
:foreground "black"))
"Face used to highlight the item where point is.")
(defface isl-line
'((t :background "Darkgoldenrod1" :extend t))
"Face used to flash line on exit.")
(defface isl-number
'((t :foreground "green"))
"Face used to highlight number in mode-line.")
(defface isl-string
'((t :foreground "Lightgoldenrod1" :bold t))
"Face used to highlight pattern in mode-line.")
(defface isl-case-fold
'((t :inherit isl-string))
"Face used to highlight case sensitivity string in mode-line.")
(defvar isl-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map minibuffer-local-map)
(define-key map (kbd "C-s") 'isl-goto-next)
(define-key map (kbd "C-r") 'isl-goto-prev)
(define-key map (kbd "C-n") 'isl-goto-next)
(define-key map (kbd "C-p") 'isl-goto-prev)
(define-key map (kbd "<down>") 'isl-goto-next)
(define-key map (kbd "<up>") 'isl-goto-prev)
(define-key map (kbd "RET") 'isl-exit-at-point)
(define-key map (kbd "C-w") 'isl-yank-word-at-point)
(define-key map (kbd "C-z") 'isl-yank-symbol-at-point)
(define-key map (kbd "M-r") 'isl-change-matching-style)
(define-key map (kbd "M-c") 'isl-select-case-fold-search)
(define-key map (kbd "M-<") 'isl-goto-first)
(define-key map (kbd "M->") 'isl-goto-last)
(define-key map (kbd "M-=") 'isl-goto-closest-from-start)
(define-key map (kbd "M-s") 'isl-jump-to-helm-occur)
(define-key map (kbd "M-i") 'isl-toggle-invisible-search)
(define-key map (kbd "C-;") 'isl-jump-to-iedit-mode)
(define-key map (kbd "M-%") 'isl-query-replace)
(define-key map (kbd "C-h m") 'isl-display-or-quit-help)
(define-key map (kbd "C-q") 'isl-help-quit)
(define-key map (kbd "C-'") 'isl-show-or-hide-context-lines)
(define-key map (kbd "C-l") 'isl-recenter)
(define-key map (kbd "C-v") 'isl-scroll-up)
(define-key map (kbd "M-v") 'isl-scroll-down)
(define-key map (kbd "C-k") 'isl-delete-minibuffer-contents)
(define-key map (kbd "C-j") 'isl-toggle-multi-search-in-line)
(define-key map (kbd "C-!") 'isl-bm-this-pos)
map)
"The map used when `isl-search' is running.
Don't forget to modify `isl-mini-map' accordingly to fit with kmacros
when modifying keybindings here.")
;;; Actions
;;
(defun isl--goto-overlay (overlay)
"Goto OVERLAY."
(let ((pos (and overlay (overlay-end overlay))))
(when (and overlay pos)
(setq isl--last-overlay overlay)
(overlay-put overlay 'face 'isl-on)
(goto-char pos)
(setq isl--yank-point pos))))
(defun isl-bm-this-pos ()
"Toggle bookmark on current position with `bm'.
Need to have the `bm' package installed."
(interactive)
(cl-assert (require 'bm nil t) nil "Please install `bm' package")
(with-selected-window (minibuffer-selected-window)
(when isl--last-overlay
(save-excursion
;; Use overlay-start otherwise with inline we are one line too far.
(goto-char (overlay-start isl--last-overlay))
(bm-toggle)))))
(defun isl--highlight-last-overlay (face)
"Highlight `isl--last-overlay' with FACE."
(when (overlayp isl--last-overlay)
(overlay-put isl--last-overlay 'face face)))
(defun isl-goto-next-1 (arg)
"Main function that allow moving from one to ARG overlay.
It put overlay on current position, move to next overlay using
`isl--iterator', set `isl--yank-point' and then setup mode-line."
(with-selected-window (minibuffer-selected-window)
(isl--highlight-last-overlay 'isl-match)
(when isl--iterator
;; This is a noop when ARG==1 i.e. (1- 1) == 0.
(cl-loop repeat (1- arg) do (isl-iter-next isl--iterator))
(isl--goto-overlay (isl-iter-next isl--iterator)))
(isl-setup-mode-line)))
(defun isl-scroll-1 (arg)
"Scroll up if ARG is positive, down if it is negative."
(let (ov)
(with-selected-window (minibuffer-selected-window)
(when isl--iterator
(setq ov (if (> arg 0)
(isl--first-ov-after-pos (window-end))
(isl--first-ov-before-pos (window-start))))))
(when ov
(isl--find-and-goto-overlay ov))))
(defun isl--first-ov-after-pos (pos)
"Move to next overlay from POS."
(cl-loop for ov in isl--item-overlays
when (> (overlay-start ov) pos)
return ov))
(defun isl--first-ov-before-pos (pos)
"Move to previous overlay from POS."
(cl-loop for ov in (reverse isl--item-overlays)
when (< (overlay-start ov) pos)
return ov))
(defun isl-scroll-up ()
"Scroll up to closest overlay in next screen."
(interactive)
(isl-scroll-1 1))
(put 'isl-scroll-up 'no-helm-mx t)
(defun isl-scroll-down ()
"Scroll down to closest overlay in previous screen."
(interactive)
(isl-scroll-1 -1))
(put 'isl-scroll-down 'no-helm-mx t)
(defun isl-delete-minibuffer-contents ()
"No docstring."
(interactive)
(with-selected-window (minibuffer-window)
(if (eolp)
(delete-region (minibuffer-prompt-end) (point))
(delete-region (point) (point-max)))))
(put 'isl-delete-minibuffer-contents 'no-helm-mx t)
(defun isl--find-and-goto-overlay (overlay)
"Consume iterators up to OVERLAY and jump to it."
(with-selected-window (minibuffer-selected-window)
(let (ov)
(while (not (eql (setq ov (isl-iter-next isl--iterator))
overlay)))
(isl--highlight-last-overlay 'isl-match)
(and ov (isl--goto-overlay ov)))
(isl-setup-mode-line)))
(defun isl-goto-first ()
"Goto first match."
(interactive)
(let ((ov (car isl--item-overlays)))
(if (eql ov isl--last-overlay)
(user-error "Already at first occurence")
(isl--find-and-goto-overlay ov))))
(put 'isl-goto-first 'no-helm-mx t)
(defun isl-goto-last ()
"Goto last match."
(interactive)
(let ((ov (car (last isl--item-overlays))))
(if (eql ov isl--last-overlay)
(user-error "Already at last occurence")
(isl--find-and-goto-overlay ov))))
(put 'isl-goto-last 'no-helm-mx t)
(defun isl-goto-closest-from-start ()
"Goto closest match from start."
(interactive)
(let ((ov (isl-closest-overlay
isl-initial-pos isl--item-overlays)))
(if (eql ov isl--last-overlay)
(user-error "Already at closest occurence from start")
(isl--find-and-goto-overlay ov))))
(put 'isl-goto-closest-from-start 'no-helm-mx t)
(defun isl-goto-next (&optional arg)
"Go to next ARG match."
(interactive "p")
(when (eq isl--direction 'backward)
(setq isl--direction 'forward)
(isl-set-iterator t))
(isl-goto-next-1 arg))
(put 'isl-goto-next 'no-helm-mx t)
(defun isl-goto-prev (&optional arg)
"Go to previous ARG matches."
(interactive "p")
(when (eq isl--direction 'forward)
(setq isl--direction 'backward)
(isl-set-iterator t))
(isl-goto-next-1 arg))
(put 'isl-goto-prev 'no-helm-mx t)
(defun isl-exit-at-point ()
"Exit minibuffer and jump at current position."
(interactive)
(with-selected-window (minibuffer-selected-window)
;; Ensure user haven't scrolled to another place.
(let ((end (overlay-end isl--last-overlay)))
(goto-char (if isl-multi-search-in-line
(1- end) end)))
(when isl-multi-search-in-line
(let* ((ovs (overlays-in (pos-bol) (pos-eol)))
(matches (cl-loop for ov in ovs
when (overlay-get ov 'isl-matches)
collect ov)))
(when matches
(goto-char (overlay-end (car matches))))))
(when isl-save-pos-to-mark-ring
(set-marker (mark-marker) isl-initial-pos)
(push-mark isl-initial-pos 'nomsg))
(let ((ov (make-overlay (pos-bol) (1+ (pos-eol)))))
(overlay-put ov 'face 'isl-line)
(sit-for 0.2)
(delete-overlay ov))
;; Save `window-start' position before exiting minibuffer and
;; restore it after in `isl-cleanup' to avoid a moving screen.
(setq isl--window-start (window-start)))
;; Call `exit-minibuffer' out of the `with-selected-window' block to
;; avoid error with the emacs-28 version.
(exit-minibuffer))
(put 'isl-exit-at-point 'no-helm-mx t)
(defun isl-yank-word-at-point ()
"Yank word at point in minibuffer.
The word at point is relative to the current position in buffer, not
the initial position i.e. the position before launching `isl-search'."
(interactive)
(let (str)
(with-current-buffer isl-current-buffer
(when (eolp) (error "End of line"))
(when (or (memq (char-syntax (or (char-after) 0)) '(?w ?_ ? ))
(memq (char-syntax (or (char-after (1+ (point))) 0))
'(?w ?_ ? )))
(setq str (buffer-substring-no-properties (or isl--yank-point (point))
(save-excursion
(forward-word)
(point))))
(when (string-match "\\` " str)
(setq str (replace-match "\\\\ " nil nil str)))
(with-selected-window (minibuffer-window)
(insert str))))))
(put 'isl-yank-word-at-point 'no-helm-mx t)
(defun isl-yank-symbol-at-point ()
"Yank symbol at point in minibuffer.
The symbol at point is relative to the current position in buffer, not
the initial position i.e. the position before launching `isl-search'."
(interactive)
(let (str)
(with-current-buffer isl-current-buffer
(when (setq str (thing-at-point 'symbol t))
(with-selected-window (minibuffer-window)
(delete-minibuffer-contents)
(insert str))))))
(put 'isl-yank-symbol-at-point 'no-helm-mx t)
(defun isl-recenter ()
"Recenter from isl."
(interactive)
(with-selected-window (minibuffer-selected-window)
(recenter)))
(put 'isl-recenter 'no-helm-mx t)
(defun isl-matching-style ()
"Return current matching style as a string."
(cl-ecase isl-search-function
(isl--re-search-forward "Regex")
(search-forward "Literal")))
(defun isl-change-matching-style ()
"Toggle style matching in `isl-search' i.e. regexp/literal."
(interactive)
(with-current-buffer isl-current-buffer
(setq-local isl-search-function
(cl-ecase isl-search-function
(isl--re-search-forward #'search-forward)
(search-forward #'isl--re-search-forward)))
(unless executing-kbd-macro
(when (string= isl-pattern "")
(let* ((style (isl-matching-style))
(mode-line-format (format " Switching to %s searching" style)))
(force-mode-line-update)
(sit-for 1)))
(isl-update))))
(put 'isl-change-matching-style 'no-helm-mx t)
(defun isl-toggle-invisible-search ()
"Toggle searching in invisible text."
(interactive)
(with-current-buffer isl-current-buffer
(setq-local isl-search-invisible (not isl-search-invisible))
(unless isl-search-invisible
(setq buffer-invisibility-spec isl--buffer-invisibility-spec))
(message "Invisible search turned %s" (if isl-search-invisible "on" "off"))
(isl-update)))
(put 'isl-toggle-invisible-search 'no-helm-mx t)
(defun isl-jump-to-helm-occur ()
"Invoke `helm-occur' from `isl-search'."
(interactive)
(cl-assert (require 'helm-occur nil t) nil "Please install Helm package")
(let ((input isl-pattern)
(bufs (list isl-current-buffer)))
(run-at-time 0.1 nil
(lambda ()
;; Use `helm-occur-always-search-in-current' as a
;; flag for `helm-occur--select-closest-candidate'.
(let ((helm-occur-always-search-in-current t))
(helm-multi-occur-1 bufs input))))
(abort-recursive-edit)))
(put 'isl-jump-to-helm-occur 'no-helm-mx t)
(defun isl-query-replace (&optional arg)
"Launch `query-replace' from isl.
Argument ARG have same meaning as in `query-replace'.
When it is launched from the last occurence, replacement is done
automatically backward."
(interactive "P")
(cl-assert isl--item-overlays nil "Nothing yet to replace")
(let* ((style (isl-matching-style))
(regexp isl-pattern)
(start (overlay-start isl--last-overlay))
(end (overlay-end isl--last-overlay))
(bottom (overlay-start (car (last isl--item-overlays))))
(pnv (prefix-numeric-value arg))
(backward (or (and arg (< pnv 0))
(and start bottom (= start bottom))))
(wr (and arg (> pnv 0))))
(run-at-time
0.1 nil
(lambda ()
(let* ((regexp-flag (string= style "Regex"))
(prompt1 (if backward "Query replace backward" "Query replace"))
(prompt (concat prompt1 (if regexp-flag "%s regexp" "%s")))
(args (list
regexp
(query-replace-read-to
regexp
(format prompt (if wr " word" ""))
regexp-flag)
(and wr arg))))
(with-current-buffer isl-current-buffer
(save-excursion
(let ((case-fold-search t))
(goto-char (if backward end start))
(apply #'perform-replace
(list (nth 0 args) (nth 1 args)
t regexp-flag (nth 2 args) nil
multi-query-replace-map nil nil backward))))))))
(abort-recursive-edit)))
(put 'isl-query-replace 'no-helm-mx t)
;; Iedit
;;
(defun isl--advice-iedit-start (old--fn &rest args)
"Allow iedit matching multi pattern.
Use `isl--iedit-make-occurrences-overlays' instead of
`iedit-make-occurrences-overlays' then apply OLD--FN on ARGS.
This is an around advice."
(cl-letf (((symbol-function 'iedit-make-occurrences-overlays)
#'isl--iedit-make-occurrences-overlays))
(apply old--fn args)))
(defun isl--iedit-make-occurrences-overlays (occurrence-regexp beg end)
"Same as `iedit-make-occurrences-overlays' but handle multiple regexps.
Arguments OCCURRENCE-REGEXP, BEG and END have same meaning as in
`iedit-make-occurrences-overlays'."
(setq iedit-aborting nil)
(setq iedit-occurrences-overlays nil)
(setq iedit-read-only-occurrences-overlays nil)
;; Find and record each occurrence's markers and add the overlay to the occurrences
(let ((counter 0)
(case-fold-search (not iedit-case-sensitive))
(length 0)
bounds)
(save-excursion
(save-selected-window
(goto-char beg)
(while (setq bounds (isl-multi-search-fwd occurrence-regexp end t))
(let ((beginning (car bounds))
(ending (cdr bounds)))
(if (and (> length 0) (/= (- ending beginning) length))
(throw 'not-same-length 'not-same-length)
(setq length (- ending beginning)))
(if (text-property-not-all beginning ending 'read-only nil)
(push (iedit-make-read-only-occurrence-overlay beginning ending)
iedit-read-only-occurrences-overlays)
(push (iedit-make-occurrence-overlay beginning ending)
iedit-occurrences-overlays))
(setq counter (1+ counter))))))
(iedit-update-index)
counter))
(defun isl-jump-to-iedit-mode ()
"Start Iedit mode from `isl' using last search string as the regexp."
(interactive)
(cl-assert (require 'iedit nil t))
(cl-assert isl--item-overlays nil "Nothing yet to replace")
(let ((regexp (if (eq isl-search-function 'search-forward)
(regexp-quote isl-pattern)
isl-pattern))
(pos (with-current-buffer isl-current-buffer
(overlay-end isl--last-overlay))))
(run-at-time
0.1 nil
(lambda ()
(save-restriction
(when (and isl--point-min isl--point-max)
(narrow-to-region isl--point-min isl--point-max))
(let ((case-fold-search (isl-set-case-fold-search regexp))
result)
(setq mark-active nil)
(run-hooks 'deactivate-mark-hook)
(when iedit-mode
(iedit-lib-cleanup))
(advice-add 'iedit-start :around #'isl--advice-iedit-start)
(unwind-protect
(progn
(setq result
(catch 'not-same-length
(iedit-start regexp (point-min) (point-max))))
(cond ((not iedit-occurrences-overlays)
(message "No matches found for %s" regexp)
(iedit-done))
((equal result 'not-same-length)
(message "Matches are not the same length.")
(iedit-done)))
(goto-char pos))
(advice-remove 'iedit-start #'isl--advice-iedit-start))))))
(abort-recursive-edit)))
(put 'isl-jump-to-iedit-mode 'no-helm-mx t)
(defun isl-display-or-quit-help ()
"Display or quit isl help buffer."
(interactive)
(if (get-buffer-window isl-help-buffer-name 'visible)
(progn
(switch-to-buffer isl-help-buffer-name)
(quit-window t))
(with-current-buffer-window
(get-buffer-create isl-help-buffer-name)
(cons 'display-buffer-below-selected
'((window-height . fit-window-to-buffer)
(preserve-size . (nil . t))))
nil
(with-current-buffer standard-output
(insert
(substitute-command-keys
isl-help-string)))
(outline-mode)
(setq cursor-type nil)
(setq buffer-read-only t)
(local-set-key (kbd "q") 'quit-window))))
(put 'isl-display-or-quit-help 'no-helm-mx t)
(defun isl-help-quit ()
"Quit isl help buffer."
(interactive)
(let ((win (get-buffer-window isl-help-buffer-name 'visible)))
(if win
(with-selected-window win
(quit-window))
(user-error "No help buffer found"))))
(put 'isl-help-quit 'no-helm-mx t)
(defun isl-show-or-hide-context-lines ()
"Hide or show non matching lines."
(interactive)
(when isl--item-overlays
(with-selected-window (minibuffer-selected-window)
(if (setq isl--hidding (not isl--hidding))
(let ((start 1) ; start at point-min.
ov-end bol)
(save-excursion
(goto-char (overlay-end (car isl--item-overlays)))
(setq ov-end (point))
(set (make-local-variable 'line-move-ignore-invisible) t)
(add-to-invisibility-spec '(isl-invisible . t))
(while (not (eobp))
(forward-line (- isl-visible-context-lines))
;; Store position from n lines before
;; this overlay and bol and move to next overlay.
(when (> (setq bol (pos-bol)) start)
(isl--put-invisible-overlay start (1- bol)))
(goto-char ov-end)
;; Go to n lines after last overlay found and jump to
;; next overlay from there.
(forward-line isl-visible-context-lines)
(setq start (1+ (pos-eol)))
(goto-char (next-single-char-property-change ov-end 'isl))
(setq ov-end (point)))
;; Store maybe remaining lines up to eob.
(when (< start (point-max))
(isl--put-invisible-overlay start (point-max)))))
(remove-overlays nil nil 'isl-invisible t)
(remove-from-invisibility-spec '(isl-invisible . t))))))
(put 'isl-show-or-hide-context-lines 'no-helm-mx t)
(defun isl--put-invisible-overlay (beg end)
"Make an invisible overlay from BEG to END."
(let ((ol (make-overlay beg end)))
(overlay-put ol 'isl-invisible t)
(overlay-put ol 'invisible 'isl-invisible)))
(defun isl-iter-circular (seq)
"Infinite iteration on SEQ."
(let ((lis seq))
(lambda ()
(let ((elm (car lis)))
(setq lis (pcase lis (`(,_ . ,ll) (or ll seq))))
elm))))
(defun isl-iter-next (iterator)
"Return next elm of ITERATOR."
(and iterator (funcall iterator)))
(defun isl-delete-overlays ()
"Cleanup ovelays."
(when isl--item-overlays
(remove-overlays nil nil 'isl t)
(setq isl--item-overlays nil
isl--extra-items-overlays nil)))
(cl-defun isl-set-case-fold-search (&optional (pattern isl-pattern))
"Return a suitable value for `case-fold-search'.
This is done according to `isl-case-fold-search'.
Optional argument PATTERN default to `isl-pattern'."
(cl-case isl-case-fold-search
(smart (let ((case-fold-search nil))
(if (string-match "[[:upper:]]" pattern) nil t)))
(t isl-case-fold-search)))
(defun isl-select-case-fold-search ()
"Set `case-fold-search' from `isl-search' session."
(interactive)
(with-current-buffer isl-current-buffer
(if (eq last-command 'isl-select-case-fold-search)
(setq-local isl-case-fold-search
(isl-iter-next isl--case-fold-choices-iterator))
(setq isl--case-fold-choices-iterator
(isl-iter-circular
(append (remove isl-case-fold-search isl--case-fold-choices)
(list isl-case-fold-search))))
(setq-local isl-case-fold-search
(isl-iter-next isl--case-fold-choices-iterator)))
(unless executing-kbd-macro
(isl-update))))
(put 'isl-select-case-fold-search 'no-helm-mx t)
(defun isl-split-string (str)
"Split string STR at non quoted spaces."
(split-string
(replace-regexp-in-string
isl-space-regexp "\\s-" str nil t)))
(defun isl-patterns (str)
"Return an alist of (pred . regexp) elements from STR."
(cl-loop for s in (isl-split-string str)
collect (if (char-equal ?! (aref s 0))
(cons 'not (substring s 1))
(cons 'identity s))))
(defsubst isl--re-search-forward (regexp &optional bound noerror count)
"Same as `re-search-forward' but return nil when point doesn't move.
This avoid possible infloop when a wrong regexp is entered in
minibuffer. Arguments REGEXP, BOUND, NOERROR and COUNT have same
meaning as in `re-search-forward'."
(let ((pos (point)))
(pcase (re-search-forward regexp bound noerror count)
((pred (eql pos)) nil)
(it it))))
(defun isl-multi-search-fwd (str &optional _bound _noerror _count)
"Return position of symbol or line matched by STR.
When arg STR contains spaces, it is converted in patterns with
`isl-patterns' , when first pattern of list match a symbol
subsequent patterns are used to check if all patterns match this
symbol. The return value is a cons cell (beg . end) denoting
symbol or line position according to `isl-multi-search-in-line'."
;; Prevent infloop crashing Emacs with incorrect configuration.
(cl-assert (memq isl-search-function '(isl--re-search-forward search-forward)))
(let* ((pattern (isl-patterns str))
(initial (or (assq 'identity pattern)
'(identity . "")))
;; In REST we search for all patterns but INITIAL we already
;; initially searched. We were previously using (cdr
;; pattern) which was fine as long as first pattern was not a
;; negation.
(rest (remove initial pattern))
(case-fold-search (isl-set-case-fold-search)))
(cl-loop while (condition-case _err
(funcall isl-search-function (cdr initial) nil t)
(invalid-regexp
(setq isl--invalid t) nil))
for bounds = (cond ((and rest isl-multi-search-in-line)
(cons (pos-bol) (1+ (pos-eol))))
(rest
(bounds-of-thing-at-point
(if (and (derived-mode-p 'prog-mode)
(not (nth 3 (syntax-ppss))))
'symbol 'filename)))
(t (cons (match-beginning 0) (match-end 0))))
unless bounds return nil
if (or (not rest)
(cl-loop for (pred . re) in rest
always (funcall pred
(progn
(goto-char (car bounds))
(condition-case _err
(funcall isl-search-function
re (cdr bounds) t)
(invalid-regexp
(setq isl--invalid t) nil))))))
;; When executing kbd macros, behave as the interactive
;; isl-search and leave point on last match in line
;; instead of jumping on eol.
do (unless executing-kbd-macro (goto-char (cdr bounds)))
and return bounds
else do (goto-char (cdr bounds))
finally return nil)))
(defun isl-toggle-multi-search-in-line ()
"Toggle multi-search in lines/symbols."
(interactive)
(with-current-buffer isl-current-buffer
(setq-local isl-multi-search-in-line (not isl-multi-search-in-line))
(unless executing-kbd-macro
(if isl-multi-search-in-line
(set-face-attribute 'isl-on nil :extend t)
(set-face-attribute 'isl-on nil :extend nil))
(isl-update))))
(defun isl-maybe-update (str)
"Decide starting to update according to STR length.
See `isl-requires-pattern'."
(> (length str) isl-requires-pattern))
(defun isl-update ()
"Update `current-buffer' when `isl-pattern' change."
(with-selected-window (minibuffer-selected-window)
(while-no-input
(when isl--hidding
(remove-overlays nil nil 'isl-invisible t)
(remove-from-invisibility-spec '(isl-invisible . t))
(setq isl--hidding nil))
(isl-delete-overlays)
(setq isl--invalid nil)
;; We don't use the isearch-invisible mechanism which is heavy
;; and don't behave as we want, instead remove invisibility in
;; all buffer and on exit restore it and unhide only the place
;; where point is with appropriate functions belonging to
;; major-mode e.g. org => org-reveal etc...
(when (and isl-search-invisible
buffer-invisibility-spec
(listp buffer-invisibility-spec))
(mapc 'remove-from-invisibility-spec buffer-invisibility-spec))
(let ((count 1)
ov
bounds)
(unless (string= isl-pattern "")
(save-excursion
(goto-char (point-min))
(condition-case-unless-debug nil
(while (setq bounds (isl-multi-search-fwd isl-pattern nil t))
(unless (and (not isl-search-invisible)
(invisible-p (cdr bounds)))
(setq ov (make-overlay (car bounds) (cdr bounds)))
(push ov isl--item-overlays)
(overlay-put ov 'isl t)
(overlay-put ov 'pos count)
(overlay-put ov 'face 'isl-match)
(when isl-multi-search-in-line
(isl--highlight-items-in-line (car bounds) (cdr bounds)))
(cl-incf count)))
(invalid-regexp (setq isl--invalid t) nil))
(setq isl--item-overlays (reverse isl--item-overlays)))
(if (null isl--item-overlays)
(progn (setq isl--number-results 0)
(and isl-initial-pos (goto-char isl-initial-pos)))
(setq isl--last-overlay
(isl-closest-overlay
(or isl-initial-pos 0) isl--item-overlays)
isl--number-results (max (length isl--item-overlays) 0))
(isl--highlight-last-overlay 'isl-on)
(isl-set-iterator)
(goto-char (overlay-end (isl-iter-next isl--iterator)))
(setq isl--yank-point (point)))))
(isl-setup-mode-line))))
(defun isl--highlight-items-in-line (beg end)
"Highlight items inside a matched line from BEG to END."
;; When this is called we are at eol.
(save-excursion
(goto-char beg)
(cl-loop with ov2
for p in (isl-split-string isl-pattern)
unless (string-match "\\`!" p)
do (save-excursion
(while (isl--re-search-forward p end t)
(setq ov2 (make-overlay (match-beginning 0) (match-end 0)))
(push ov2 isl--extra-items-overlays)
(overlay-put ov2 'face 'isl-match-items)
(overlay-put ov2 'isl t)
(overlay-put ov2 'isl-matches t)
(overlay-put ov2 'priority 1))))))
(defun isl-setup-mode-line ()
"Setup `mode-line-format' for `isl-search'."
(let ((style (isl-matching-style))
(search (if isl-multi-search-in-line
(propertize "Inline" 'help-echo "Search in line")
(propertize "Insym" 'help-echo "Search in symbol")))
(position (with-current-buffer isl-current-buffer
(if (and isl-initial-pos
(> (point) isl-initial-pos))
isl-after-position-string
isl-before-position-string)))
(direction (if (eq isl--direction 'forward)
isl-direction-down-string
isl-direction-up-string)))
(when (numberp isl--number-results)
(setq mode-line-format
(cond ((or (string= isl-pattern "")
(<= (length isl-pattern)
isl-requires-pattern))
(default-value 'mode-line-format))
((zerop isl--number-results)
`(" " mode-line-buffer-identification " "
(:eval ,(format "%s `%s' [%s %s %s %s]"
(if isl--invalid
(propertize
(format "%s Invalid regexp:"
isl-warning-char)
'face 'font-lock-warning-face)
"No results found for pattern")
(propertize isl-pattern
'face 'isl-string)
style
search
(if isl-search-invisible
(propertize
" Sinv" 'help-echo
"Search in invisible text")
"")
direction))
" " mode-line-position))
(t `(" " mode-line-buffer-identification " "
(:eval ,(format
"[%s/%s] result(s) found [%s %s%s %s %s %s]"
(propertize
(number-to-string
(overlay-get isl--last-overlay 'pos))
'face 'isl-number)
(propertize (number-to-string
isl--number-results)
'face 'isl-number)
style
search
(if isl-search-invisible
(propertize
" Sinv" 'help-echo
"Search in invisible text")
"")
direction
position
(propertize (pcase isl-case-fold-search
('smart "*")
('t "1")
('nil "0"))
'face 'isl-case-fold
'help-echo "case-fold-search")))
" " mode-line-position)))))))
(defun isl-closest-overlay (pos overlays)
"Return closest overlay from POS in OVERLAYS list."
(cl-loop for ov in overlays
for ovpos = (overlay-start ov)
for diff = (if (> pos ovpos) (- pos ovpos) (- ovpos pos))
collect (cons diff ov) into res
minimize diff into min