forked from cosmicexplorer/helm-rg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelm-rg.el
1911 lines (1659 loc) · 82.2 KB
/
helm-rg.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
;;; helm-rg.el --- a helm interface to ripgrep -*- lexical-binding: t -*-
;; This file 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 file 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/>.
;; Author: Danny McClanahan
;; Version: 0.1
;; URL: https://github.com/cosmicexplorer/helm-rg
;; Package-Requires: ((emacs "25") (helm "2.8.8") (cl-lib "0.5") (dash "2.13.0"))
;; Keywords: find, file, files, helm, fast, rg, ripgrep, grep, search
;;; Commentary:
;; The below is generated from a README at
;; https://github.com/cosmicexplorer/helm-rg.
;; MELPA: https://melpa.org/#/helm-rg
;; !`helm-rg' example usage (./emacs-helm-rg.png)
;; Search massive codebases extremely fast, using `ripgrep'
;; (https://github.com/BurntSushi/ripgrep) and `helm'
;; (https://github.com/emacs-helm/helm). Inspired by `helm-ag'
;; (https://github.com/syohex/emacs-helm-ag) and `f3'
;; (https://github.com/cosmicexplorer/f3).
;; Also check out rg.el (https://github.com/dajva/rg.el), which I haven't used
;; much but seems pretty cool.
;; Usage:
;; *See the `ripgrep' whirlwind tour
;; (https://github.com/BurntSushi/ripgrep#whirlwind-tour) for further
;; information on invoking `ripgrep'.*
;; - Invoke the interactive function `helm-rg' to start a search with `ripgrep'
;; in the current directory.
;; - `helm' is used to browse the results and update the output as you
;; type.
;; - Each line has the file path, the line number, and the column number of
;; the start of the match, and each part is highlighted differently.
;; - Use `TAB' to invoke the helm persistent action, which previews the
;; result and highlights the matched text in the preview.
;; - Use `RET' to visit the file containing the result, move point to the
;; start of the match, and recenter.
;; - The result's buffer is displayed with
;; `helm-rg-display-buffer-normal-method' (which defaults to
;; `switch-to-buffer').
;; - Use a prefix argument (`C-u RET') to open the buffer with
;; `helm-rg-display-buffer-alternate-method' (which defaults to
;; `pop-to-buffer').
;; - The text entered into the minibuffer is interpreted into a PCRE
;; (https://pcre.org) regexp to pass to `ripgrep'.
;; - `helm-rg''s pattern syntax is basically PCRE, but single spaces
;; basically act as a more powerful conjunction operator.
;; - For example, the pattern `a b' in the minibuffer is transformed
;; into `a.*b|b.*a'.
;; - The single space can be used to find lines with any
;; permutation of the regexps on either side of the space.
;; - Two spaces in a row will search for a literal single space.
;; - `ripgrep''s `--smart-case' option is used so that case-sensitive
;; search is only on if any of the characters in the pattern are capitalized.
;; - For example, `ab' (conceptually) searches `[Aa][bB]', but `Ab'
;; in the minibuffer will only search for the pattern `Ab' with `ripgrep',
;; because it has at least one uppercase letter.
;; - Use `M-d' to select a new directory to search from.
;; - Use `M-g' to input a glob pattern to filter files by, e.g. `*.py'.
;; - The glob pattern defaults to the value of
;; `helm-rg-default-glob-string', which is an empty string (matches every file)
;; unless you customize it.
;; - Pressing `M-g' again shows the same minibuffer prompt for the glob
;; pattern, with the string that was previously input.
;; - Use `<left>' and `<right>' to go up and down by files in the results.
;; - `<up>' and `<down>' simply go up and down by match result, and there
;; may be many matches for your pattern in a single file, even multiple on a
;; single line (which `ripgrep' reports as multiple separate results).
;; - The `<left>' and `<right>' keys will move up or down until it lands on
;; a result from a different file than it started on.
;; - When moving by file, `helm-rg' will cycle around the results list,
;; but it will print a harmless error message instead of looping infinitely if
;; all results are from the same file.
;; - Use the interactive autoloaded function `helm-rg-display-help' to see the
;; ripgrep command's usage info.
;; TODO:
;; *items checked completed here are ready to be added to the docs above*
;; - [x] make a keybinding to drop into an "edit mode" and edit file content
;; inline in results like `helm-ag' (https://github.com/syohex/emacs-helm-ag)
;; - *currently called "bounce mode"* in the alpha stage
;; - [x] needs to dedup results from the same line
;; - [x] should also merge the colorations
;; - [x] this might be easier without using the `--vimgrep' flag (!!!)
;; - [x] can insert markers on either side of each line to find the text
;; added or removed
;; - [x] can change the filename by editing the file line
;; - [x] needs to reset all the file data for each entry if the file
;; name is being changed!!!
;; - [x] can expand the windows of text beyond single lines at a time
;; - using `helm-rg--expand-match-context' and/or
;; `helm-rg--spread-match-context'
;; - [x] and pop into another buffer for a quick view if you want
;; - can use `helm-rg--visit-current-file-for-bounce'
;; - [ ] can expand up and down from file header lines to add lines
;; from the top or bottom of the file!
;; - [ ] can use newlines in inserted text
;; - not for file names -- newlines are still removed there
;; - would need to use text properties to move by match results
;; then, for everything that uses `helm-rg--apply-matches-with-file-for-bounce'
;; basically
;; - [x] visiting the file should go to the appropriate line of the file!
;; - [x] color all results in the file in the async action!
;; - [x] don't recolor when switching to a different result in the same
;; file!
;; - [x] don't color matches whenever file path matches
;; `helm-rg-shallow-highlight-files-regexp'
;; - [ ] use `ripgrep' file types instead of flattening globbing out into
;; `helm-rg-default-glob-string'
;; - user defines file types in a `defcustom', and can interactively toggle
;; the accepted file types
;; - user can also set the default set of file types
;; - as a dir-local variable!!
;; - [ ] add testing
;; - [ ] should be testing all of our interactive functions
;; - in all configurations (for all permutations of `defcustom' values)
;; - [ ] also everything that's called by helm
;; - does helm have any frameworks to make integration testing easier?
;; - [ ] publish `update-commentary.el' and the associated machinery
;; - as an npm package, MELPA package, pandoc writer, *???*
;; - [ ] make a keybinding for running `helm-rg' on dired marked files
;; - then you could do an `f3' search, bounce to dired, then immediately
;; `helm-rg' on just the file paths from the `f3' search, *which would be
;; sick*
;; License:
;; GPL 3.0+ (./LICENSE)
;; End Commentary
;;; Code:
(require 'ansi-color)
(require 'cl-lib)
(require 'dash)
(require 'font-lock)
(require 'helm)
(require 'helm-grep)
(require 'helm-lib)
(require 'pcase)
(require 'rx)
;; Customization Helpers
(defun helm-rg--always-safe-local (_)
"Use as a :safe predicate in a `defcustom' form to accept any local override."
t)
(defun helm-rg--gen-defcustom-form-from-alist (name alist doc args)
`(defcustom ,name ',(car (helm-rg--alist-keys (symbol-value alist)))
,doc
:type `(radio ,@(--map `(const ,it) (helm-rg--alist-keys ,alist)))
:group 'helm-rg
,@args))
(defmacro helm-rg--defcustom-from-alist (name alist doc &rest args)
"Create a `defcustom' named NAME which allows the keys of ALIST as values.
The default value for the `defcustom' is the `car' of the first element of ALIST. ALIST must be the
unquoted name of a variable containing an alist."
(declare (indent 2))
(helm-rg--gen-defcustom-form-from-alist name alist doc args))
;; CL deftypes
(cl-deftype helm-rg--existing-file ()
`(and string
(satisfies file-exists-p)))
(cl-deftype helm-rg--existing-directory ()
`(and helm-rg--existing-file
(satisfies file-directory-p)))
;; Public error types
(define-error 'helm-rg-error "Error invoking `helm-rg'")
;; Customization
(defgroup helm-rg nil
"Group for `helm-rg' customizations."
:group 'helm-grep)
(defcustom helm-rg-ripgrep-executable (executable-find "rg")
"The location of the ripgrep binary executable."
:type 'string
:group 'helm-rg)
(defcustom helm-rg-default-glob-string ""
"The glob pattern used for the '-g' argument to ripgrep.
Set to the empty string to match every file."
:type 'string
:safe #'helm-rg--always-safe-local
:group 'helm-rg)
(defcustom helm-rg-default-directory 'default
"Specification for starting directory to invoke ripgrep in.
Used in `helm-rg--interpret-starting-dir'. Possible values:
'default => Use `default-directory'.
'git-root => Use \"git rev-parse --show-toplevel\" (see
`helm-rg-git-executable').
<string> => Use the directory at path <string>."
:type '(choice symbol string)
:safe #'helm-rg--always-safe-local
:group 'helm-rg)
(defcustom helm-rg-git-executable (executable-find "git")
"Location of git executable."
:type 'string
:group 'helm-rg)
(defcustom helm-rg-thing-at-point 'symbol
"Type of object at point to initialize the `helm-rg' minibuffer input with."
:type 'symbol
:safe #'helm-rg--always-safe-local
:group 'helm-rg)
(defcustom helm-rg-input-min-search-chars 2
"Ripgrep will not be invoked unless the input is at least this many chars.
See `helm-rg--make-process' and `helm-rg--make-dummy-process' if interested."
;; FIXME: this should be a *positive* integer!
:type 'integer
:safe #'helm-rg--always-safe-local
:group 'helm-rg)
(defcustom helm-rg-display-buffer-normal-method #'switch-to-buffer
"A function accepting a single argument BUF and displaying the buffer.
The default function to invoke to display a visited buffer in some window in
`helm-rg'."
:type 'function
:group 'helm-rg)
(defcustom helm-rg-display-buffer-alternate-method #'pop-to-buffer
"A function accepting a single argument BUF and displaying the buffer.
The function will be invoked if a prefix argument is used when visiting a result
in `helm-rg'."
:type 'function
:group 'helm-rg)
(defcustom helm-rg-shallow-highlight-files-regexp nil
"Regexp describing file paths to only partially highlight, for performance reasons.
By default, `helm-rg' will create overlays to highlight all the matches from ripgrep in a file when
previewing a result. This is done each time a match is selected, even for buffers already
previewed. Creating these overlays can be slow for files with lots of matches in some search. If
this variable is set to an elisp regexp and some file path matches it, `helm-rg' will only highlight
the current line of the file and the matches in that line when previewing that file."
:type 'regexp
:safe #'helm-rg--always-safe-local
:group 'helm-rg)
(defcustom helm-rg-prepend-file-name-line-at-top-of-matches t
"Whether to put the file path as a separate line in `helm-rg' output above the file's matches.
The file can be visited as if it was a match on the first line of the file (without any matched
text).
FIXME: if this is nil and `helm-rg-include-file-on-every-match-line' is t, you get a stream of just
line numbers and content, without any file names. We should unify these two boolean options somehow
to get all three allowable states."
:type 'boolean
:group 'helm-rg)
(defcustom helm-rg-include-file-on-every-match-line nil
"Whether to include the file path on every line of `helm-rg' output.
This is purely an interface change, and does not affect anything else."
:type 'boolean
:group 'helm-rg)
(defcustom helm-rg--default-expand-match-lines-for-bounce 3
"???"
;; FIXME: this should be a *positive* integer!
:type 'integer
:group 'helm-rg)
;; Faces
(defface helm-rg-preview-line-highlight
'((t (:background "green" :foreground "black")))
"Face for the line of text matched by the ripgrep process."
:group 'helm-rg)
(defface helm-rg-base-rg-cmd-face
'((t (:foreground "gray" :weight normal)))
"Face for the ripgrep executable in the ripgrep invocation."
:group 'helm-rg)
(defface helm-rg-inactive-arg-face
'((t (:foreground "gray" :weight normal)))
"Face for non-essential arguments in the ripgrep invocation."
:group 'helm-rg)
(defface helm-rg-active-arg-face
'((t (:foreground "green")))
"Face for arguments in the ripgrep invocation which affect the results."
:group 'helm-rg)
(defface helm-rg-directory-cmd-face
'((t (:foreground "brown" :background "black" :weight normal)))
"Face for any directories provided as paths to the ripgrep invocation.")
(defface helm-rg-error-message
'((t (:foreground "red")))
"Face for error text displayed in the `helm-buffer' for `helm-rg'."
:group 'helm-rg)
(defface helm-rg-title-face
'((t (:foreground "purple" :background "black" :weight bold)))
"Face for the title of the ripgrep async helm source."
:group 'helm-rg)
(defface helm-rg-directory-header-face
'((t (:foreground "white" :background "black" :weight bold)))
"Face for the current directory in the header of the `helm-buffer' for `helm-rg'."
:group 'helm-rg)
(defface helm-rg-file-match-face
'((t (:foreground "#0ff" :underline t)))
"Face for the file name when displaying matches in the `helm-buffer' for `helm-rg'."
:group 'helm-rg)
(defface helm-rg-colon-separator-ripgrep-output-face
'((t (:foreground "white")))
"Face for the separator between file, line, and match text in ripgrep output."
:group 'helm-rg)
(defface helm-rg-line-number-match-face
'((t (:foreground "orange" :underline t)))
"Face for line numbers when displaying matches in the `helm-buffer' for `helm-rg'."
:group 'helm-rg)
(defface helm-rg-match-text-face
'((t (:foreground "white" :background "purple")))
"Face for displaying matches in the `helm-buffer' and in file previews for `helm-rg'."
:group 'helm-rg)
;; Constants
(defconst helm-rg--color-format-argument-alist
'((red :cmd-line "red" :text-property "red3"))
"Alist mapping (a symbol named after a color) -> (strings to describe that symbol on the ripgrep
command line and in an emacs text property). This allows `helm-rg' to identify matched text using
ripgrep's highlighted output directly instead of doing it ourselves, by telling ripgrep to highlight
matches a specific color, then searching for that specific color as a text property in the output.")
(defconst helm-rg--style-format-argument-alist
'((bold :cmd-line "bold" :text-property bold))
"Very similar to `helm-rg--color-format-argument-alist', but for non-color styling.")
(defconst helm-rg--case-sensitive-argument-alist
'((smart-case "--smart-case")
(case-sensitive "--case-sensitive")
(case-insensitive "--ignore-case"))
"Alist of methods of treating case-sensitivity when invoking ripgrep.
The value is the ripgrep command-line argument which enforces the specified type of
case-sensitivity.")
(defconst helm-rg--ripgrep-argv-format-alist
`((helm-rg-ripgrep-executable :face helm-rg-base-rg-cmd-face)
((->> helm-rg--case-sensitive-argument-alist
(helm-rg--alist-get-exhaustive helm-rg--case-sensitivity))
:face helm-rg-active-arg-face)
("--color=ansi" :face helm-rg-inactive-arg-face)
((helm-rg--construct-match-color-format-arguments)
:face helm-rg-inactive-arg-face)
((unless (helm-rg--empty-glob-p helm-rg--glob-string)
(list "-g" helm-rg--glob-string))
:face helm-rg-active-arg-face)
(it
:face font-lock-string-face)
((helm-rg--process-paths-to-search helm-rg--paths-to-search)
:face helm-rg-directory-cmd-face))
"Alist mapping (sexp -> face) describing how to generate and propertize the argv for ripgrep.")
(defconst helm-rg--helm-buffer-name "*helm-rg*")
(defconst helm-rg--process-name "*helm-rg--rg*")
(defconst helm-rg--process-buffer-name "*helm-rg--rg-output*")
(defconst helm-rg--error-process-name "*helm-rg--error-process*")
(defconst helm-rg--error-buffer-name "*helm-rg--errors*")
(defconst helm-rg--ripgrep-help-buffer-name "helm-rg-usage-help")
(defconst helm-rg--bounce-buffer-name "helm-rg-bounce-buf")
(defconst helm-rg--output-new-file-line-regexp
(rx (: bos (group (+? (not (any 0)))) eos))
"Regexp for ripgrep output which marks the start of results for a new file.
See `helm-rg--process-transition' for usage.")
(defconst helm-rg--numbered-text-line-regexp
(rx (: bos
(: (group (+ digit)) ":"
(group (*? anything)))
eos))
"Regexp for ripgrep output which marks a matched line, with he line number and content.
See `helm-rg--process-transition' for usage.")
(defconst helm-rg--persistent-action-display-buffer-method #'switch-to-buffer
"A function accepting a single argument BUF and displaying the buffer.
Let-bound to `helm-rg--display-buffer-method' in `helm-rg--async-persistent-action'.")
(defconst helm-rg--loop-input-pattern-regexp
(rx
(:
(* (char ? ))
;; group 1 = single entire element
(group
(+
(|
(not (in ? ))
(= 2 ? ))))))
"Regexp applied iteratively to split the input interpreted by `helm-rg'.")
(defconst helm-rg--all-whitespace-regexp
(rx (: bos (zero-or-more space) eos)))
(defconst helm-rg--jump-location-text-property 'helm-rg-jump-to
"Name of a text property attached to the colorized ripgrep output.
This text property contains location and match info. See `helm-rg--process-transition' for usage.")
(defconst helm-rg--helm-header-property-name 'helm-header
"Property used for the \"header\" of the `helm-buffer' displayed in `helm-rg'.
This header is generated by helm, and is separate from the process output.")
;; Variables
(defvar helm-rg--append-persistent-buffers nil
"Whether to record buffers opened during an `helm-rg' session.")
(defvar helm-rg--cur-persistent-bufs nil
"List of buffers opened temporarily during an `helm-rg' session.")
(defvar helm-rg--matches-in-current-file-overlays nil
"List of overlays used to highlight matches in `helm-rg'.")
(defvar helm-rg--current-line-overlay nil
"Overlay for highlighting the selected matching line in a file in `helm-rg'.")
(defvar helm-rg--current-dir nil
"Working directory for the current `helm-rg' session.")
(defvar helm-rg--glob-string nil
"Glob string used for the current `helm-rg' session.")
(defvar helm-rg--glob-string-history nil
"History variable for the selection of `helm-rg--glob-string'.")
(defvar helm-rg--input-history nil
"History variable for the pattern input to the ripgrep process.")
(defvar helm-rg--display-buffer-method nil
"The method to use to display a buffer visiting a result.
Should accept one argument BUF, the buffer to display.")
(defvar helm-rg--paths-to-search nil
"List of paths to use in the ripgrep command.
All paths are interpreted relative to the directory ripgrep is invoked from.
When nil, searches from the directory ripgrep is invoked from.
See the documentation for `helm-rg-default-directory'.")
(defvar helm-rg--case-sensitivity nil
"Key of `helm-rg--case-sensitive-argument-alist' to use in a `helm-rg' session.")
(defvar helm-rg--previously-highlighted-buffer nil
"Previous buffer visited in between async actions of a `helm-rg' session.
Used to cache the overlays drawn for matches within a file when visiting matches in the same file
using `helm-rg--async-persistent-action'.")
(defvar helm-rg--last-argv nil
"Argument list for the most recent ripgrep invocation.
Used for the command-line header in `helm-rg--bounce-mode'.")
;; Buffer-local Variables
(defvar-local helm-rg--process-output-parse-state
(list :cur-file nil)
"Contains state which is updated as the ripgrep output is processed.
This is buffer-local because it is specific to a single process invocation and is manipulated in
that process's buffer. See `helm-rg--parse-process-output' for usage.")
(defvar-local helm-rg--beginning-of-bounce-content-mark nil
"Contains a marker pointing to the beginning of the match results in a `helm-rg--bounce' buffer.")
(defvar-local helm-rg--do-font-locking nil
"If t, colorize the file text as it would be in an editor.
This may be expensive for larger files, so it is turned off if
`helm-rg-shallow-highlight-files-regexp' is a regexp matching the file's path.")
;; Utilities
(defun helm-rg--alist-get-exhaustive (key alist)
"Get KEY from ALIST, or throw an error."
(or (alist-get key alist)
(error "key '%s' was not found in alist '%S' during exhaustive check"
key alist)))
(defun helm-rg--alist-keys (alist)
"Get all keys of ALIST."
(cl-mapcar #'car alist))
(defmacro helm-rg--get-optional-typed (type-name obj &rest body)
"If OBJ is non-nil, check its type against TYPE-NAME, then bind it to `it' and execute BODY."
(declare (indent 2))
`(let ((it ,obj))
(when it
(cl-check-type it ,type-name)
,@body)))
(defmacro helm-rg--into-temp-buffer (to-insert &rest body)
"Execute BODY at the beginning of a `with-temp-buffer' containing TO-INSERT."
(declare (indent 1))
`(with-temp-buffer
(insert ,to-insert)
(goto-char (point-min))
,@body))
(defmacro helm-rg--with-named-temp-buffer (name &rest body)
"Execute BODY after binding the result of a `with-temp-buffer' to NAME.
BODY is executed in the original buffer, not the new temp buffer."
(declare (indent 1))
(let ((cur-buf (gensym "helm-rg--with-named-temp-buffer")))
`(let ((,cur-buf (current-buffer)))
(with-temp-buffer
(let ((,name (current-buffer)))
(with-current-buffer ,cur-buf
,@body))))))
;; Logic
(defun helm-rg--make-dummy-process (input err-msg)
"Make a process that immediately exits to display just a title."
(let* ((dummy-proc (make-process
:name helm-rg--process-name
:buffer helm-rg--process-buffer-name
:command '("echo")
:noquery t))
(input-repr
(cond
((string= input "")
"<empty string>")
((string-match-p helm-rg--all-whitespace-regexp input)
"<whitespace>")
(t input)))
(helm-src-name
(format "%s %s: %s"
(helm-rg--make-face 'helm-rg-error-message "no results for input")
(helm-rg--make-face 'font-lock-string-face input-repr)
(helm-rg--make-face 'helm-rg-error-message err-msg))))
(helm-attrset 'name helm-src-name)
dummy-proc))
(defun helm-rg--validate-or-make-dummy-process (input)
(cond
((< (length input) helm-rg-input-min-search-chars)
(helm-rg--make-dummy-process
input
(format "must be at least %d characters" helm-rg-input-min-search-chars)))
(t t)))
(defun helm-rg--join (sep seq)
(mapconcat #'identity seq sep))
(defun helm-rg--props (props str)
(apply #'propertize (append (list str) props)))
(defun helm-rg--make-face (face str)
(helm-rg--props `(face ,face) str))
(defun helm-rg--process-paths-to-search (paths)
(cl-check-type helm-rg--current-dir helm-rg--existing-directory)
(cl-loop
for path in paths
for expanded = (expand-file-name path helm-rg--current-dir)
unless (file-exists-p expanded)
do (error (concat "Error: expanded path '%s' does not exist. "
"The cwd was '%s', and the paths provided were %S.")
expanded
helm-rg--current-dir
paths)
collect (file-relative-name expanded helm-rg--current-dir)))
(defun helm-rg--empty-glob-p (glob-str)
(or (null glob-str)
(string-blank-p glob-str)))
(defun helm-rg--construct-argv (pattern)
"Create an argument list for the ripgrep command.
This argument list is propertized for display in the `helm-buffer' header when using `helm-rg', and
is used directly to invoke ripgrep. It uses `defcustom' values, and `defvar' values bound in other
functions."
;; TODO: document these pcase deconstructions in the docstring for
;; `helm-rg--ripgrep-argv-format-alist'!
(cl-loop
for el in helm-rg--ripgrep-argv-format-alist
append (pcase-exhaustive el
(`(,(or (and `it (let expr pattern)) expr) :face ,face-sym)
(pcase-exhaustive (eval expr)
((and (pred listp) args)
(--map (helm-rg--make-face face-sym it) args))
(arg
(list (helm-rg--make-face face-sym arg))))))))
(defun helm-rg--make-process-from-argv (argv)
(let* ((real-proc (make-process
:name helm-rg--process-name
:buffer helm-rg--process-buffer-name
:command argv
:noquery t))
(helm-src-name
(format "argv: %s" (helm-rg--join " " argv))))
(helm-attrset 'name helm-src-name)
(set-process-query-on-exit-flag real-proc nil)
real-proc))
(defun helm-rg--make-process ()
"Invoke ripgrep in `helm-rg--current-dir' with `helm-pattern'.
Make a dummy process if the input is empty with a clear message to the user."
(let* ((default-directory helm-rg--current-dir)
(input helm-pattern))
(pcase-exhaustive (helm-rg--validate-or-make-dummy-process input)
((and x (pred processp))
(setq helm-rg--last-argv nil)
x)
(`t
(let* ((rg-regexp (helm-rg--helm-pattern-to-ripgrep-regexp input))
(argv (helm-rg--construct-argv rg-regexp))
(real-proc (helm-rg--make-process-from-argv argv)))
(setq helm-rg--last-argv argv)
real-proc)))))
(defun helm-rg--make-overlay-with-face (beg end face)
"Generate an overlay in region BEG to END with face FACE."
(let ((olay (make-overlay beg end)))
(overlay-put olay 'face face)
olay))
(defun helm-rg--delete-match-overlays ()
"Delete all cached overlays in `helm-rg--matches-in-current-file-overlays', and clear it."
(mapc #'delete-overlay helm-rg--matches-in-current-file-overlays)
(setq helm-rg--matches-in-current-file-overlays nil))
(defun helm-rg--delete-line-overlay ()
"Delete the cached overlay `helm-rg--current-line-overlay', if it exists, and clear it."
(helm-rg--get-optional-typed overlay helm-rg--current-line-overlay
(delete-overlay it))
(setq helm-rg--current-line-overlay nil))
(defun helm-rg--collect-lines-matches-current-file (orig-line-parsed file-abs-path)
"Collect all matches from ripgrep's highlighted output from from FILE-ABS-PATH."
;; If we are on a file's line, stay where we are, otherwise back up to the closest file line above
;; the current line (this is the file that "owns" the entry).
(cl-destructuring-bind (&key
((:file orig-file))
((:line-num orig-line-num))
((:match-results orig-match-results)))
orig-line-parsed
;; Collect all the results on all matching lines of the file.
(with-helm-window
(helm-rg--file-backward t)
(let ((all-match-results nil))
;; Process the first line (`helm-rg--iterate-results' will advance
;; past the initial element).
(cl-destructuring-bind (&key file line-num match-results) (helm-rg--current-jump-location)
(when (and line-num match-results)
(push (list :match-line-num line-num
:line-match-results match-results)
all-match-results)))
(helm-rg--iterate-results
'forward
:success-fn (lambda (cur-line-parsed)
(cl-destructuring-bind (&key file line-num match-results)
cur-line-parsed
(cl-check-type orig-file string)
(cl-check-type file string)
(if (not (string= orig-file file))
;; We have reached the results from a different file, so done.
t
(progn
;; In filename lines, these are nil.
(when (and line-num match-results)
(push (list :match-line-num line-num
:line-match-results match-results)
all-match-results))
;; We loop forever if there's only one file in
;; the results unless we return this as success.
(helm-end-of-source-p)))))
:failure-fn (lambda (cur-line-parsed)
(helm-rg--different-file-line orig-line-parsed cur-line-parsed)))
(helm-rg--iterate-results
'backward
:success-fn (lambda (cur-line-parsed)
(helm-rg--on-same-entry orig-line-parsed cur-line-parsed))
:failure-fn #'ignore)
(reverse all-match-results)))))
(defun helm-rg--convert-lines-matches-to-overlays (line-match-results)
(beginning-of-line)
(--map (cl-destructuring-bind (&key beg end) it
(helm-rg--make-overlay-with-face
(+ (point) beg) (+ (point) end)
'helm-rg-match-text-face))
line-match-results))
(defun helm-rg--make-match-overlays-for-result (cur-file-matches)
(save-excursion
(goto-char (point-min))
(cl-loop
with cur-line = 1
for line-match-set in cur-file-matches
append (cl-destructuring-bind (&key match-line-num line-match-results)
line-match-set
(let ((lines-diff (- match-line-num cur-line)))
(cl-assert (>= lines-diff 0))
(forward-line lines-diff)
(incf cur-line lines-diff)
(cl-assert (not (eobp)))
(helm-rg--convert-lines-matches-to-overlays line-match-results))))))
(defun helm-rg--async-action (parsed-output &optional highlight-matches)
"Visit the file at the line and column specified by CAND.
The match is highlighted in its buffer."
(let ((default-directory helm-rg--current-dir)
(helm-rg--display-buffer-method
(or helm-rg--display-buffer-method
;; If a prefix arg is given for the async action or persistent action, use the
;; alternate buffer display method (which by default is `pop-to-buffer').
(if helm-current-prefix-arg helm-rg-display-buffer-alternate-method
helm-rg-display-buffer-normal-method))))
;; We always want to delete the line overlay if it exists, no matter what.
(helm-rg--delete-line-overlay)
(cl-destructuring-bind (&key file line-num match-results) parsed-output
(let* ((file-abs-path (expand-file-name file))
(buffer-to-display
(or (when-let ((visiting-buf (find-buffer-visiting file-abs-path)))
;; TODO: prompt to save the buffer if modified? something?
visiting-buf)
(let ((new-buf (find-file-noselect file-abs-path)))
(when helm-rg--append-persistent-buffers
(push new-buf helm-rg--cur-persistent-bufs))
new-buf)))
(cur-file-matches
;; Clear the old matches and make new ones, if this is a different file than the last
;; one we visited in this session.
(cond
;; We don't highlight any matches, probably because we are the async action and just
;; want to jump to a file location.
((not highlight-matches)
nil)
;; If the file path matches `helm-rg-shallow-highlight-files-regexp', just
;; highlight the matches for the current line, if any. We need to do this again, even
;; if it is the same file, because the single line number to draw may change.
((and (stringp helm-rg-shallow-highlight-files-regexp)
(string-match-p helm-rg-shallow-highlight-files-regexp file-abs-path))
;; Delete the overlay for the previous line.
(helm-rg--delete-match-overlays)
(list (list :match-line-num line-num
:line-match-results match-results)))
;; This is the same buffer as last time, so do nothing.
((eq helm-rg--previously-highlighted-buffer buffer-to-display)
nil)
(t
;; This is a different buffer, so record that.
(setq helm-rg--previously-highlighted-buffer buffer-to-display)
;; Clear the old lines (from the previous buffer) and make new ones.
(helm-rg--delete-match-overlays)
(helm-rg--collect-lines-matches-current-file parsed-output file-abs-path)))))
;; Display the buffer visiting the file with the matches.
(funcall helm-rg--display-buffer-method buffer-to-display)
;; Make overlays highlighting all the matches (unless we are in the same file as
;; before, or highlight-matches is nil).
(when cur-file-matches
(setq helm-rg--matches-in-current-file-overlays
(helm-rg--make-match-overlays-for-result cur-file-matches)))
;; Advance in the file to the given line.
(goto-char (point-min))
(helm-rg--get-optional-typed natnum line-num
(forward-line (1- it)))
;; Make a line overlay, if requested.
(when highlight-matches
(let ((line-olay
(helm-rg--make-overlay-with-face (line-beginning-position) (line-end-position)
'helm-rg-preview-line-highlight)))
(setq helm-rg--current-line-overlay line-olay)))
;; Move to the first match in the line (all lines have >= 1 match because ripgrep only
;; outputs matching lines).
(let ((first-match-beginning (plist-get (car match-results) :beg)))
(helm-rg--get-optional-typed natnum first-match-beginning
(forward-char it)))
(recenter)))))
(defun helm-rg--async-persistent-action (parsed-output)
"Visit the file at the line and column specified by CAND.
Call `helm-rg--async-action', but push the buffer corresponding to CAND to
`helm-rg--matches-in-current-file-overlays', if there was no buffer visiting it already."
(let ((helm-rg--append-persistent-buffers t)
(helm-rg--display-buffer-method helm-rg--persistent-action-display-buffer-method))
(helm-rg--async-action parsed-output t)))
(defun helm-rg--kill-proc-if-live (proc-name)
"Delete the process named PROC-NAME, if it is alive."
(let ((proc (get-process proc-name)))
(when (process-live-p proc)
(delete-process proc))))
(defun helm-rg--kill-bufs-if-live (&rest bufs)
"Kill any live buffers in BUFS."
(mapc
(lambda (buf)
(when (buffer-live-p (get-buffer buf))
(kill-buffer buf)))
bufs))
(defun helm-rg--unwind-cleanup ()
"Reset all the temporary state in `defvar's in this package."
(helm-rg--delete-match-overlays)
(helm-rg--delete-line-overlay)
(cl-loop
for opened-buf in helm-rg--cur-persistent-bufs
unless (eq (current-buffer) opened-buf)
do (kill-buffer opened-buf)
finally (setq helm-rg--cur-persistent-bufs nil))
(helm-rg--kill-proc-if-live helm-rg--process-name)
;; Don't delete `helm-rg--helm-buffer-name' to support using e.g. `helm-resume'.
;; TODO: add testing for this use case.
(helm-rg--kill-bufs-if-live helm-rg--process-buffer-name
helm-rg--error-buffer-name)
(setq helm-rg--glob-string nil
helm-rg--paths-to-search nil
helm-rg--case-sensitivity nil
helm-rg--previously-highlighted-buffer nil
helm-rg--last-argv nil))
(defun helm-rg--do-helm-rg (rg-pattern)
"Invoke ripgrep to search for RG-PATTERN, using `helm'."
(helm :sources '(helm-rg-process-source)
:buffer helm-rg--helm-buffer-name
:input rg-pattern
:prompt "rg pattern: "))
(defun helm-rg--get-thing-at-pt ()
"Get the object surrounding point, or the empty string."
(helm-aif (thing-at-point helm-rg-thing-at-point)
(substring-no-properties it)
""))
(defun helm-rg--header-name (src-name)
(format "%s %s @ %s"
(helm-rg--make-face 'helm-rg-title-face "rg")
src-name
(helm-rg--make-face 'helm-rg-directory-header-face helm-rg--current-dir)))
(defun helm-rg--current-jump-location (&optional object)
(get-text-property (line-beginning-position) helm-rg--jump-location-text-property object))
(defun helm-rg--get-jump-location-from-line (line)
"Get the value of `helm-rg--jump-location-text-property' at the start of LINE."
;; When there is an empty pattern, the argument can be nil due to the way helm handles our dummy
;; process. There may be a way to avoid having to do this check.
(when line
(get-text-property 0 helm-rg--jump-location-text-property line)))
(defun helm-rg--display-to-real (_)
"Extract the information from the process filter stored in the current entry's text properties.
Note that this doesn't use the argument at all. I don't think you can get the currently selected
line without the text properties scrubbed using helm without doing this."
(helm-rg--get-jump-location-from-line (helm-get-selection nil 'withprop)))
(defun helm-rg--collect-matches (regexp)
(cl-loop while (re-search-forward regexp nil t)
collect (match-string 1)))
(defun helm-rg--helm-pattern-to-ripgrep-regexp (pattern)
"Transform PATTERN (the `helm-input') into a Perl-compatible regular expression.
TODO: add ert testing for this function!"
;; For example: "a b c" => "a b.*c|c.*a b".
(->>
;; Split the pattern into our definition of "components". Suppose PATTERN is "a b c". Then:
;; "a b c" => '("a b" "c")
(helm-rg--into-temp-buffer pattern
(helm-rg--collect-matches helm-rg--loop-input-pattern-regexp))
;; Two spaces in a row becomes a single space in the output regexp. Each component is now a
;; regexp.
;; '("a b" "c") => '("a b" "c")
(--map (replace-regexp-in-string (rx (= 2 ? )) " " it))
;; All permutations of all component regexps.
;; '("a b" "c") => '(("a b" "c") ("c" "a b"))
(-permutations)
;; Each permutation is converted into a regexp which matches a line containing each regexp in
;; the permutation in order, each separated by 0 or more non-newline characters.
;; '(("a b" "c") ("c" "a b")) => '("a b.*c" "c.*a b")
(--map (helm-rg--join ".*" it))
;; Return a regexp which matches any of the resulting regexps.
;; '("a b.*c" "c.*a b") => "a b.*c|c.*a b"
(helm-rg--join "|")))
(defun helm-rg--advance-forward ()
(interactive)
(let ((helm-move-to-line-cycle-in-source t))
(if (helm-end-of-source-p)
(helm-beginning-of-buffer)
(helm-next-line))))
(defun helm-rg--advance-backward ()
(interactive)
(let ((helm-move-to-line-cycle-in-source t))
(if (helm-beginning-of-source-p)
(helm-end-of-buffer)
(helm-previous-line))))
(define-error 'helm-rg--helm-buffer-iteration-error
"Iterating over ripgrep match results in the helm buffer failed."
'helm-rg-error)
(cl-defun helm-rg--iterate-results (direction &key success-fn failure-fn)
(with-helm-buffer
(let ((move-fn
(pcase-exhaustive direction
(`forward #'helm-rg--advance-forward)
(`backward #'helm-rg--advance-backward))))
(call-interactively move-fn)
(cl-loop
for cur-line-parsed = (helm-rg--current-jump-location)
until (funcall success-fn cur-line-parsed)
if (funcall failure-fn cur-line-parsed)
return (signal 'helm-rg--helm-buffer-iteration-error "could not cycle to the next entry")
else do (call-interactively move-fn)))))
(defun helm-rg--current-line-contents ()
"`helm-current-line-contents' doesn't keep text properties."
(buffer-substring (point-at-bol) (point-at-eol)))
(cl-defun helm-rg--nullable-states-different (a b &key (cmp #'eq))
"Compare A and B respecting nullability using CMP.
When CMP is `string=', the following results:
(A=nil, B=nil) => nil
(A=\"a\", B=nil) => t
(A=nil, B=\"a\") => t
(A=\"a\", B=\"a\") => nil
(A=\"a\", B=\"b\") => t
TODO: throw the above into an ert test!"
(if a
(not (and b (funcall cmp a b)))
b))
(defun helm-rg--on-same-entry (orig-line-parsed cur-line-parsed)
(cl-destructuring-bind (&key ((:file orig-file)) ((:line-num orig-line-num)) ((:match-results _)))
orig-line-parsed
(cl-check-type orig-file string)
(cl-destructuring-bind (&key ((:file cur-file)) ((:line-num cur-line-num)) ((:match-results _)))
cur-line-parsed
(cl-check-type cur-file string)
(and (string= orig-file cur-file)
(not (helm-rg--nullable-states-different orig-line-num cur-line-num :cmp #'=))))))