-
Notifications
You must be signed in to change notification settings - Fork 2
/
consult-todo.el
290 lines (260 loc) · 11.8 KB
/
consult-todo.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
;;; consult-todo.el --- Search hl-todo keywords in consult -*- lexical-binding: t -*-
;; Copyright (C) 2023 liuyinz
;; Author: liuyinz <liuyinz@gmail.com>
;; Maintainer: liuyinz <liuyinz@gmail.com>
;; Created: 2021-10-03 03:44:36
;; Version: 0.4.1
;; Package-Requires: ((emacs "27.1") (consult "0.35") (hl-todo "3.1.2"))
;; Homepage: https://github.com/liuyinz/consult-todo
;; License: GPL-3.0-or-later
;; This file is not a part of GNU Emacs.
;; 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Provide commands `consult-todo' to search, filter, jump to hl-todo keywords.
;;; Code:
(eval-when-compile
(require 'cl-lib)
(require 'pcase)
(require 'subr-x))
(require 'consult)
(require 'hl-todo)
(require 'compile)
(require 'grep)
(declare-function project-root "project")
(defgroup consult-todo nil
"Search hl-todo keywords in consult."
:group 'consult-todo)
(defcustom consult-todo-narrow nil
"Alist of (NARROW . KEYWORD) to display."
:type '(repeat (cons (character :tag "Narrow")
(string :tag "Keyword")))
:group 'consult-todo)
(defcustom consult-todo-other (cons ?. "OTHER")
"Cons mapping for narrow and missing keywords."
:type '(cons character string)
:group 'consult-todo)
(defcustom consult-todo-only-comment nil
"If non-nil, only search todo keywords in comments.
Only effective on buffers."
:type 'boolean
:group 'consult-todo)
(defcustom consult-todo-dir-preview-key nil
"Preview trigger keys for `consult-todo-dir' related command.
Value can be nil, `any', a single key or a list of keys."
:type '(choice (const :tag "Any key" any)
(list :tag "Debounced"
(const :debounce)
(float :tag "Seconds" 0.1)
(const any))
(const :tag "No preview" nil)
(key :tag "Key")
(repeat :tag "List of keys" key))
:group 'consult-todo)
(defconst consult-todo--narrow
'((?t . "TODO")
(?f . "FIXME")
(?b . "BUG")
(?h . "HACK"))
"Default mapping of narrow and keywords.")
(defvar consult-todo--narrow-extend nil
"Default mapping of narrow and keywords include OTHER if exists.")
(defun consult-todo--narrow ()
"Return narrow alist."
(or consult-todo-narrow consult-todo--narrow))
(defun consult-todo--narrow-extend ()
"Return narrow alist include `consult-todo-other' if it's non-nil."
(or consult-todo--narrow-extend
(if-let* (((consp consult-todo-other))
(narrow (car consult-todo-other))
(group (cdr consult-todo-other))
((and (characterp narrow) (not (assoc narrow (consult-todo--narrow)))))
((and (stringp group) (not (rassoc group (consult-todo--narrow))))))
(setq consult-todo--narrow-extend
(cons consult-todo-other (consult-todo--narrow)))
(user-error
"Consult-todo-other: format error or conflicts with consult-todo-narrow")
(setq consult-todo--narrow-extend nil))))
(defun consult-todo--format (candidates)
"Return formatted string according to CANDIDATES."
(if candidates
(mapcar
(pcase-lambda (`(,name ,line ,type ,pos ,narrow ,text))
(propertize
(format (apply #'format "%%-%ds %%-%ds %%-%ds %%s"
(cl-loop for i to 2
collect (seq-max (mapcar
(lambda(x) (length (nth i x)))
candidates))))
(propertize name 'face 'consult-file)
(propertize line 'face 'consult-line-number)
;; WONTFIX don't support regexp keywords face
(propertize type 'face (hl-todo--combine-face
(cdr (assoc type hl-todo-keyword-faces))))
text)
'consult-location (cons pos line)
'consult--type narrow))
candidates)
(user-error "No hl-todo keywords")))
(defun consult-todo-grep-state ()
"Lookup SELECTED in CANDIDATES list of `consult-location' category.
Return the location marker."
(let ((open (consult--temporary-files))
(jump (consult--jump-state)))
(lambda (action cand)
(unless cand (funcall open))
(when cand
(setq cand (car (get-text-property 0 'consult-location cand)))
(funcall jump action (consult--marker-from-line-column
(ignore-errors
(funcall (or (and (not (eq action 'return)) open)
#'find-file-noselect)
(nth 0 cand)))
(nth 1 cand) (nth 2 cand)))))))
(defun consult-todo--candidates (buffers)
"Return list of hl-todo keywords in current buffer.
If optional argument BUFFERS is non-nil, operate on list of them."
(cl-loop for buf in (or buffers (list (current-buffer)))
append
(with-current-buffer buf
(save-excursion
(save-restriction
(widen)
(goto-char (point-min))
(cl-loop while (hl-todo--search)
when (or (null consult-todo-only-comment)
(nth 4 (syntax-ppss)))
collect
;; HACK when buffer is too large, match-string return nil
;; in one match, use thing-at-point to get the match-string
;; as type. At this failed match, point is still accurate,
;; match-begin and match-end is limit to accessible portion
;; of buffer
(let ((type (or (match-string-no-properties 2)
(save-excursion
(backward-to-word)
(substring-no-properties
(save-match-data
(thing-at-point 'word)))))))
(list (buffer-name)
(number-to-string (line-number-at-pos))
type
(copy-marker (point))
(car (or (rassoc type (consult-todo--narrow))
consult-todo-other))
(string-trim (buffer-substring-no-properties
(point)
(line-end-position)))))))))))
(defun consult-todo--candidates-rgrep (buffer message)
"Return list of hl-todo keywords in directory where rgrep running.
BUFFER is the rgrep buffer, MESSAGE is the status of rgrep."
(consult--forbid-minibuffer)
(consult--read
(consult-todo--format
(unwind-protect
(when (and (string-match-p "^finished" message)
(string-prefix-p "*consult-todo-" (buffer-name buffer)))
(with-current-buffer buffer
(goto-char (point-min))
(cl-loop while (and (null (eobp))
(condition-case nil
(progn
(compilation-next-error 1)
t)
(user-error nil)))
when (save-excursion
(save-match-data
(text-property-search-forward 'font-lock-face 'match t)))
collect
(let* ((msg (get-text-property (point) 'compilation-message))
(loc (compilation--message->loc msg))
(line (compilation--loc->line loc))
(col (compilation--loc->col loc))
(file (caar (compilation--loc->file-struct loc)))
(type (buffer-substring-no-properties
(prop-match-beginning it)
(prop-match-end it))))
(list (file-name-nondirectory file)
(number-to-string line)
type
(list (expand-file-name file compilation-directory)
line col)
(car (or (rassoc type (consult-todo--narrow))
consult-todo-other))
(string-trim
(buffer-substring-no-properties
(prop-match-end it)
(line-end-position))))))))
(kill-buffer buffer)
(remove-hook 'compilation-finish-functions #'consult-todo--candidates-rgrep)))
:prompt "Go to hl-todo in dir: "
:category 'consult-grep
:require-match t
:sort nil
:preview-key consult-todo-dir-preview-key
:group (consult--type-group (consult-todo--narrow-extend))
:narrow (consult--type-narrow (consult-todo--narrow-extend))
:lookup #'consult--lookup-member
:state (consult-todo-grep-state)))
;;;###autoload
(defun consult-todo (&optional buffers)
"Jump to hl-todo keywords.
If BUFFERS is non-nil, prompt with hl-todo keywords in them instead."
(interactive "P")
(consult--forbid-minibuffer)
(consult--read
(consult-todo--format
(consult-todo--candidates buffers))
:prompt "Go to hl-todo: "
:category 'consult-location
:require-match t
:sort nil
:group (consult--type-group (consult-todo--narrow-extend))
:narrow (consult--type-narrow (consult-todo--narrow-extend))
:lookup #'consult--lookup-location
:state (consult--jump-state)))
;; FIXME return "error in process sentinel: No hl-todo keywords/Quit" if
;; candidates list is empty or cancel jumping. Preparing to rewrite it totally.
;; Use it as little as possible
;;;###autoload
(defun consult-todo-dir (&optional directory files)
"Jump to hl-todo keywords in FILES in DIRECTORY.
If optinal arg FILES is nil, search in all files.
If optional arg DIRECTORY is nil, rgrep in default directory."
(interactive)
(let* ((files (or files "* .*"))
(directory (or directory default-directory)))
(add-hook 'compilation-finish-functions #'consult-todo--candidates-rgrep)
(cl-letf ((compilation-buffer-name-function
(lambda (&rest _) (format "*consult-todo-%s*" directory))))
(save-window-excursion
(let ((grep-command "grep --color=auto -nH --null -I -e "))
(rgrep (hl-todo--regexp) files directory))))))
;;;###autoload
(defun consult-todo-all ()
"Jump to hl-todo keywords in all `hl-todo-mode' enabled buffers."
(interactive)
(consult-todo (seq-filter (lambda (x)
(buffer-local-value 'hl-todo-mode x))
(buffer-list))))
;;;###autoload
(defun consult-todo-project ()
"Jump to hl-todo keywords in current project."
(interactive)
(consult-todo-dir
(when-let ((project (project-current)))
(expand-file-name
(if (fboundp 'project-root)
(project-root project)
(car (with-no-warnings
(project-roots project))))))))
(provide 'consult-todo)
;;; consult-todo.el ends here