Skip to content

Commit f8900fe

Browse files
committed
Add deadgrep-file-type-alist for user defined file types
1 parent baa352b commit f8900fe

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

deadgrep.el

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,24 @@ display.")
137137
(defvar-local deadgrep--file-type 'all)
138138
(put 'deadgrep--file-type 'permanent-local t)
139139

140+
(defcustom deadgrep-file-type-alist
141+
nil
142+
"Alist defining predetermined file types.
143+
The key is a symbol which will be used as the name as the value
144+
of `deadgrep--file-type' and as the name of a button in the
145+
*deadgreap* buffer. The value is a list of arguments to pass to rg.
146+
147+
For example, to only search in test files you might add the
148+
following key-value pair.
149+
150+
(tests . (\"--type-add=TEST:*_test.cpp\"
151+
\"--type=TEST\"))
152+
153+
The types of `all', `type', and `glob' will always be available."
154+
:type '(alist :key-type symbol
155+
:value-type (repeat :tag "Arguments for rg" string))
156+
:group 'deadgrep)
157+
140158
(defvar-local deadgrep--context nil
141159
"When set, also show context of results.
142160
This is stored as a cons cell of integers (lines-before . lines-after).")
@@ -589,6 +607,8 @@ with a text face property `deadgrep-match-face'."
589607
(t
590608
"*")))))
591609
(setq deadgrep--file-type (cons 'glob glob))))
610+
((alist-get button-type deadgrep-file-type-alist)
611+
(setq deadgrep--file-type button-type))
592612
(t
593613
(error "Unknown button type: %S" button-type))))
594614
(deadgrep-restart))
@@ -642,12 +662,21 @@ to obtain ripgrep results."
642662
(error "Unknown case: %s" case)))
643663

644664
(cond
645-
((eq deadgrep--file-type 'all))
646665
((eq (car-safe deadgrep--file-type) 'type)
647666
(push (format "--type=%s" (cdr deadgrep--file-type)) args))
648667
((eq (car-safe deadgrep--file-type) 'glob)
649668
(push (format "--type-add=custom:%s" (cdr deadgrep--file-type)) args)
650669
(push "--type=custom" args))
670+
((alist-get deadgrep--file-type deadgrep-file-type-alist)
671+
(let ((a (alist-get deadgrep--file-type deadgrep-file-type-alist)))
672+
(cond ((stringp a)
673+
(push a args))
674+
((listp a)
675+
(dolist (aa a)
676+
(push aa args)))
677+
(t
678+
(error "unknown argument type in deadgrep-file-type-alist: %s" a)))))
679+
((eq deadgrep--file-type 'all))
651680
(t
652681
(error "Unknown file-type: %S" deadgrep--file-type)))
653682

@@ -758,6 +787,13 @@ search settings."
758787
(if (eq (car-safe deadgrep--file-type) 'glob)
759788
(format ":%s" (cdr deadgrep--file-type))
760789
"")
790+
(cl-loop for alist-item in deadgrep-file-type-alist
791+
concat (concat " "
792+
(if (eq deadgrep--file-type (car alist-item))
793+
(symbol-name (car alist-item))
794+
(deadgrep--button (symbol-name (car alist-item))
795+
'deadgrep-file-type
796+
'file-type (car alist-item)))))
761797
"\n\n")
762798
(put-text-property
763799
start-pos (point)

0 commit comments

Comments
 (0)