forked from oantolin/embark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
embark-consult.el
391 lines (318 loc) · 14.8 KB
/
embark-consult.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
;;; embark-consult.el --- Consult integration for Embark -*- lexical-binding: t; -*-
;; Copyright (C) 2021, 2022 Free Software Foundation, Inc.
;; Author: Omar Antolín Camarena <[email protected]>
;; Maintainer: Omar Antolín Camarena <[email protected]>
;; Keywords: convenience
;; Version: 0.6
;; Homepage: https://github.com/oantolin/embark
;; Package-Requires: ((emacs "27.1") (embark "0.17") (consult "0.17"))
;; 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:
;; This package provides integration between Embark and Consult. The package
;; will be loaded automatically by Embark.
;; Some of the functionality here was previously contained in Embark
;; itself:
;; - Support for consult-buffer, so that you get the correct actions
;; for each type of entry in consult-buffer's list.
;; - Support for consult-line, consult-outline, consult-mark and
;; consult-global-mark, so that the insert and save actions don't
;; include a weird unicode character at the start of the line, and so
;; you can export from them to an occur buffer (where occur-edit-mode
;; works!).
;; Just load this package to get the above functionality, no further
;; configuration is necessary.
;; Additionally this package contains some functionality that has
;; never been in Embark: access to Consult preview from auto-updating
;; Embark Collect buffer that is associated to an active minibuffer
;; for a Consult command. For information on Consult preview, see
;; Consult's info manual or its readme on GitHub.
;; If you always want the minor mode enabled whenever it possible use:
;; (add-hook 'embark-collect-mode-hook #'consult-preview-at-point-mode)
;; If you don't want the minor mode automatically on and prefer to
;; trigger the consult previews manually use this instead:
;; (define-key embark-collect-mode-map (kbd "C-j")
;; #'consult-preview-at-point)
;;; Code:
(require 'embark)
(require 'consult)
(eval-when-compile
(require 'cl-lib))
;;; Consult preview from Embark Collect buffers
(defun embark-consult--collect-candidate ()
"Return candidate at point in collect buffer."
(cadr (embark-target-collect-candidate)))
(add-hook 'consult--completion-candidate-hook #'embark-consult--collect-candidate)
;;; Support for consult-location
(defun embark-consult--strip (string)
"Strip substrings marked with the `consult-strip' property from STRING."
(if (text-property-not-all 0 (length string) 'consult-strip nil string)
(let ((end (length string)) (pos 0) (chunks))
(while (< pos end)
(let ((next (next-single-property-change pos 'consult-strip string end)))
(unless (get-text-property pos 'consult-strip string)
(push (substring string pos next) chunks))
(setq pos next)))
(apply #'concat (nreverse chunks)))
string))
(defun embark-consult--target-strip (type target)
"Remove the unicode suffix character from a TARGET of TYPE."
(cons type (embark-consult--strip target)))
(setf (alist-get 'consult-location embark-transformer-alist)
#'embark-consult--target-strip)
(defun embark-consult-goto-location (target)
"Jump to consult location TARGET."
(consult--jump (car (consult--get-location target)))
(pulse-momentary-highlight-one-line (point)))
(setf (alist-get 'consult-location embark-default-action-overrides)
#'embark-consult-goto-location)
(defun embark-consult-export-occur (lines)
"Create an occur mode buffer listing LINES.
The elements of LINES are assumed to be values of category `consult-line'."
(let ((buf (generate-new-buffer "*Embark Export Occur*"))
(mouse-msg "mouse-2: go to this occurrence")
last-buf)
(with-current-buffer buf
(dolist (line lines)
(pcase-let*
((`(,loc . ,num) (consult--get-location line))
;; the text properties added to the following strings are
;; taken from occur-engine
(lineno (propertize (format "%7d:" num)
'occur-prefix t
;; Allow insertion of text at the end
;; of the prefix (for Occur Edit mode).
'front-sticky t
'rear-nonsticky t
'occur-target loc
'follow-link t
'help-echo mouse-msg))
(contents (propertize (embark-consult--strip line)
'occur-target loc
'occur-match t
'follow-link t
'help-echo mouse-msg))
(nl (propertize "\n" 'occur-target loc))
(this-buf (marker-buffer loc)))
(unless (eq this-buf last-buf)
(insert (propertize
(format "lines from buffer: %s\n" this-buf)
'face list-matching-lines-buffer-name-face))
(setq last-buf this-buf))
(insert (concat lineno contents nl))))
(goto-char (point-min))
(occur-mode))
(pop-to-buffer buf)))
(defun embark-consult--upgrade-markers ()
"Upgrade consult-location cheap markers to real markers.
This function is meant to be added to `embark-collect-mode-hook'."
(when (eq embark--type 'consult-location)
(mapc (lambda (entry) (consult--get-location (car entry)))
tabulated-list-entries)))
(setf (alist-get 'consult-location embark-exporters-alist)
#'embark-consult-export-occur)
(cl-pushnew #'embark-consult--upgrade-markers embark-collect-mode-hook)
;;; Support for consult-grep
(defvar wgrep-header/footer-parser)
(declare-function wgrep-setup "ext:wgrep")
(embark-define-keymap embark-consult-revert-map
"A keymap with a binding for revert-buffer."
:parent nil
("g" revert-buffer))
(defun embark-consult-export-grep (lines)
"Create a grep mode buffer listing LINES."
(let ((buf (generate-new-buffer "*Embark Export Grep*")))
(with-current-buffer buf
(insert (propertize "Exported grep results:\n\n" 'wgrep-header t))
(dolist (line lines) (insert line "\n"))
(goto-char (point-min))
(grep-mode)
;; Make this buffer current for next/previous-error
(setq next-error-last-buffer buf)
;; Set up keymap before possible wgrep-setup, so that wgrep
;; restores our binding too when the user finishes editing.
(use-local-map (make-composed-keymap
embark-consult-revert-map
(current-local-map)))
(setq-local wgrep-header/footer-parser #'ignore)
(when (fboundp 'wgrep-setup) (wgrep-setup)))
(pop-to-buffer buf)))
(defun embark-consult-goto-grep (location)
"Go to LOCATION, which should be a string with a grep match."
;; Actions are run in the target window, so in this case whatever
;; window was selected when the command that produced the
;; xref-location candidates ran. In particular, we inherit the
;; default-directory of the buffer in that window, but we really
;; want the default-directory of the minibuffer or collect window we
;; call the action from, which is the previous window, since the
;; location is given relative to that directory.
(let ((default-directory (with-selected-window (previous-window)
default-directory)))
(consult--jump (consult--grep-position location))
(pulse-momentary-highlight-one-line (point))))
(setf (alist-get 'consult-grep embark-default-action-overrides)
#'embark-consult-goto-grep)
(setf (alist-get 'consult-grep embark-exporters-alist)
#'embark-consult-export-grep)
;;; Support for consult-xref
(declare-function xref--show-xref-buffer "ext:xref")
(declare-function consult-xref "ext:consult-xref")
(defvar xref-auto-jump-to-first-xref)
(defvar consult-xref--fetcher)
(defun embark-consult-export-xref (items)
"Create an xref buffer listing ITEMS."
(cl-flet ((xref-items (items)
(mapcar (lambda (item) (get-text-property 0 'consult-xref item))
items)))
(let ((fetcher consult-xref--fetcher)
(input (minibuffer-contents)))
(set-buffer
(xref--show-xref-buffer
(lambda ()
(let ((candidates (funcall fetcher)))
(if (null (cdr candidates))
candidates
(catch 'xref-items
(minibuffer-with-setup-hook
(lambda ()
(insert input)
(add-hook
'minibuffer-exit-hook
(lambda ()
(throw 'xref-items
(xref-items
(or
(plist-get
(embark--maybe-transform-candidates)
:candidates)
(user-error "No candidates for export")))))
nil t))
(consult-xref fetcher))))))
`((fetched-xrefs . ,(xref-items items))
(window . ,(embark--target-window))
(auto-jump . ,xref-auto-jump-to-first-xref)
(display-action)))))))
(setf (alist-get 'consult-xref embark-exporters-alist)
#'embark-consult-export-xref)
;;; Support for consult-find and consult-locate
(setf (alist-get '(file . consult-find) embark-default-action-overrides)
#'find-file)
(setf (alist-get '(file . consult-locate) embark-default-action-overrides)
#'find-file)
;;; Support for consult-isearch
(setf (alist-get 'consult-isearch embark-transformer-alist)
#'embark-consult--target-strip)
;;; Bindings for consult commands in embark keymaps
(define-key embark-file-map "x" #'consult-file-externally)
(define-key embark-become-file+buffer-map "Cb" #'consult-buffer)
(define-key embark-become-file+buffer-map "C4b" #'consult-buffer-other-window)
;;; Support for Consult search commands
(embark-define-keymap embark-consult-sync-search-map
"Keymap for Consult sync search commands"
:parent nil
("o" consult-outline)
("i" 'consult-imenu)
("I" 'consult-imenu-multi)
("l" consult-line)
("L" consult-line-multi))
(embark-define-keymap embark-consult-async-search-map
"Keymap for Consult async search commands"
:parent nil
("g" consult-grep)
("r" consult-ripgrep)
("G" consult-git-grep)
("f" consult-find)
("F" consult-locate))
(defvar embark-consult-search-map
(keymap-canonicalize
(make-composed-keymap embark-consult-sync-search-map
embark-consult-async-search-map))
"Keymap for all Consult search commands.")
(fset 'embark-consult-sync-search-map embark-consult-sync-search-map)
(define-key embark-become-match-map "C" 'embark-consult-sync-search-map)
(cl-pushnew 'embark-consult-async-search-map embark-become-keymaps)
(fset 'embark-consult-search-map embark-consult-search-map)
(define-key embark-general-map "C" 'embark-consult-search-map)
(map-keymap
(lambda (_key cmd)
(cl-pushnew 'embark--allow-edit
(alist-get cmd embark-target-injection-hooks)))
embark-consult-search-map)
(defun embark-consult--unique-match (&rest _)
"If there is a unique matching candidate, accept it.
This is intended to be used in `embark-target-injection-hooks'."
(let ((candidates (cdr (embark-minibuffer-candidates))))
(if (or (null candidates) (cdr candidates))
(embark--allow-edit)
(delete-minibuffer-contents)
(insert (car candidates)))))
(dolist (cmd '(consult-outline consult-imenu consult-imenu-multi))
(setf (alist-get cmd embark-target-injection-hooks)
(remq 'embark--allow-edit
(alist-get cmd embark-target-injection-hooks)))
(cl-pushnew #'embark-consult--unique-match
(alist-get cmd embark-target-injection-hooks)))
(cl-defun embark-consult--prep-async (&key type target &allow-other-keys)
"Either add Consult's async separator or ignore the TARGET depending on TYPE.
If the TARGET of the given TYPE has an associated notion of
directory, we don't want to search for the text of target, but
rather just start a search in the associated directory.
This is intended to be used in `embark-target-injection-hooks'
for any action that is a Consult async command."
(let* ((style (alist-get consult-async-split-style
consult-async-split-styles-alist))
(initial (plist-get style :initial))
(separator (plist-get style :separator))
(directory (embark--associated-directory target type)))
(when directory
(delete-minibuffer-contents))
(when initial
(goto-char (minibuffer-prompt-end))
(insert initial)
(goto-char (point-max)))
(when (and separator (null directory))
(goto-char (point-max))
(insert separator))))
(map-keymap
(lambda (_key cmd)
(cl-pushnew #'embark--cd (alist-get cmd embark-pre-action-hooks))
(cl-pushnew #'embark-consult--prep-async
(alist-get cmd embark-target-injection-hooks)))
embark-consult-async-search-map)
;;; Tables of contents for buffers: imenu and outline candidate collectors
(defun embark-consult-outline-candidates ()
"Collect all outline headings in the current buffer."
(cons 'consult-location (consult--outline-candidates)))
(autoload 'consult-imenu--items "consult-imenu")
(defun embark-consult-imenu-candidates ()
"Collect all imenu items in the current buffer."
(cons 'imenu (mapcar #'car (consult-imenu--items))))
(declare-function consult-imenu--group "ext:consult-imenu")
(defun embark-consult--imenu-group-function (type prop)
"Return a suitable group-function for imenu.
TYPE is the completion category.
PROP is the metadata property.
Meant as :after-until advice for `embark-collect--metadatum'."
(when (and (eq type 'imenu) (eq prop 'group-function))
(consult-imenu--group)))
(advice-add #'embark-collect--metadatum :after-until
#'embark-consult--imenu-group-function)
(defun embark-consult-imenu-or-outline-candidates ()
"Collect imenu items in prog modes buffer or outline headings otherwise."
(if (derived-mode-p 'prog-mode)
(embark-consult-imenu-candidates)
(embark-consult-outline-candidates)))
(setf (alist-get 'imenu embark-default-action-overrides) 'consult-imenu)
(add-to-list 'embark-candidate-collectors
#'embark-consult-imenu-or-outline-candidates
'append)
(provide 'embark-consult)
;;; embark-consult.el ends here