forked from oantolin/embark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
embark.el
4300 lines (3878 loc) · 174 KB
/
embark.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
;;; embark.el --- Conveniently act on minibuffer completions -*- 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.17
;; Homepage: https://github.com/oantolin/embark
;; Package-Requires: ((emacs "26.1"))
;; This file is 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:
;; This package provides a sort of right-click contextual menu for
;; Emacs, accessed through the `embark-act' command (which you should
;; bind to a convenient key), offering you relevant actions to use on
;; a target determined by the context:
;; - In the minibuffer, the target is the current best completion
;; candidate.
;; - In the `*Completions*' buffer the target is the completion at point.
;; - In a regular buffer, the target is the region if active, or else the
;; file, symbol or url at point.
;; The type of actions offered depend on the type of the target:
;; - For files you get offered actions like deleting, copying,
;; renaming, visiting in another window, running a shell command on the
;; file, etc.
;; - For buffers the actions include switching to or killing the buffer.
;; - For package names the actions include installing, removing or
;; visiting the homepage.
;; Everything is easily configurable: determining the current target,
;; classifying it, and deciding with actions are offered for each type
;; in the classification. The above introduction just mentions part of
;; the default configuration.
;; Configuring which actions are offered for a type is particularly
;; easy and requires no programming: the `embark-keymap-alist'
;; variable associates target types with variable containing keymaps,
;; and those keymaps containing binds for the actions. For example,
;; in the default configuration the type `file' is associated with the
;; symbol `embark-file-keymap'. That symbol names a keymap with
;; single-letter key bindings for common Emacs file commands, for
;; instance `c' is bound to `copy-file'. This means that if while you
;; are in the minibuffer after running a command that prompts for a
;; file, such as `find-file' or `rename-file', you can copy a file by
;; running `embark-act' and then pressing `c'.
;; These action keymaps are very convenient but not strictly necessary
;; when using `embark-act': you can use any command that reads from the
;; minibuffer as an action and the target of the action will be inserted
;; at the first minibuffer prompt. After running `embark-act' all of your
;; key bindings and even `execute-extended-command' can be used to run a
;; command. The action keymaps are normal Emacs keymaps and you should
;; feel free to bind in them whatever commands you find useful as actions.
;; The actions in `embark-general-map' are available no matter what
;; type of completion you are in the middle of. By default this
;; includes bindings to save the current candidate in the kill ring
;; and to insert the current candidate in the previously selected
;; buffer (the buffer that was current when you executed a command
;; that opened up the minibuffer).
;; You can read about the Embark GitHub project wiki:
;; https://github.com/oantolin/embark/wiki/Default-Actions
;; Besides acting individually on targets, Embark lets you work
;; collectively on a set of target candidates. For example, while
;; you are in the minibuffer the candidates are simply the possible
;; completions of your input. Embark provides three commands to work
;; on candidate sets:
;; - The `embark-act-all' command runs the same action on each of the
;; current candidates. It is just like using `embark-act' on each
;; candidate in turn.
;; - The `embark-collect' command produces a buffer listing all
;; candidates, for you to peruse and run actions on at your leisure.
;; The candidates are displayed as a list showing additional
;; annotations.
;; - The `embark-export' command tries to open a buffer in an
;; appropriate major mode for the set of candidates. If the
;; candidates are files export produces a Dired buffer; if they are
;; buffers, you get an Ibuffer buffer; and if they are packages you
;; get a buffer in package menu mode.
;; These are always available as "actions" (although they do not act
;; on just the current target but on all candidates) for embark-act
;; and are bound to A, S (for "snapshot") and E, respectively, in
;; embark-general-map. This means that you do not have to bind your
;; own key bindings for these (although you can, of course), just a
;; key binding for `embark-act'.
;;; Code:
(eval-when-compile (require 'subr-x))
(require 'ffap) ; used to recognize file and url targets
;;; User facing options
(defgroup embark nil
"Emacs Mini-Buffer Actions Rooted in Keymaps."
:group 'minibuffer)
(defcustom embark-keymap-alist
'((file embark-file-map)
(library embark-library-map)
(environment-variables embark-file-map) ; they come up in file completion
(url embark-url-map)
(email embark-email-map)
(buffer embark-buffer-map)
(tab embark-tab-map)
(expression embark-expression-map)
(identifier embark-identifier-map)
(defun embark-defun-map)
(symbol embark-symbol-map)
(face embark-face-map)
(command embark-command-map)
(variable embark-variable-map)
(function embark-function-map)
(minor-mode embark-command-map)
(unicode-name embark-unicode-name-map)
(package embark-package-map)
(bookmark embark-bookmark-map)
(region embark-region-map)
(sentence embark-sentence-map)
(paragraph embark-paragraph-map)
(kill-ring embark-kill-ring-map)
(heading embark-heading-map)
(t embark-general-map))
"Alist of action types and corresponding keymaps.
The special key t is associated with the default keymap to use.
Each value can be either a single symbol whose value is a keymap,
or a list of such symbols."
:type '(alist :key-type (symbol :tag "Target type")
:value-type (choice (variable :tag "Keymap")
(repeat :tag "Keymaps" variable))))
(defcustom embark-target-finders
'(embark-target-top-minibuffer-completion
embark-target-active-region
embark-target-text-heading-at-point
embark-target-collect-candidate
embark-target-completion-at-point
embark-target-bug-reference-at-point
embark-target-package-at-point
embark-target-email-at-point
embark-target-url-at-point
embark-target-file-at-point
embark-target-custom-variable-at-point
embark-target-identifier-at-point
embark-target-library-file-at-point
embark-target-expression-at-point
embark-target-sentence-at-point
embark-target-paragraph-at-point
embark-target-defun-at-point
embark-target-prog-heading-at-point)
"List of functions to determine the target in current context.
Each function should take no arguments and return either nil to
indicate that no target has been found, a cons (type . target)
where type is a symbol and target is a string, or a triple of the
form (type target . bounds), where bounds is the (beg . end)
bounds pair of the target at point for highlighting."
:type 'hook)
(defcustom embark-transformer-alist
'((minor-mode . embark--lookup-lighter-minor-mode)
(symbol . embark--refine-symbol-type)
(embark-keybinding . embark--keybinding-command)
(project-file . embark--project-file-full-path)
(package . embark--remove-package-version)
(multi-category . embark--refine-multi-category)
(file . embark--simplify-path))
"Alist associating type to functions for transforming targets.
Each function should take a type and a target string and return a
pair of the form a `cons' of the new type and the new target."
:type '(alist :key-type symbol :value-type function))
(defcustom embark-become-keymaps
'(embark-become-help-map
embark-become-file+buffer-map
embark-become-shell-command-map
embark-become-match-map)
"List of keymaps for `embark-become'.
Each keymap groups a set of related commands that can
conveniently become one another."
:type '(repeat variable))
(defcustom embark-prompter 'embark-keymap-prompter
"Function used to prompt the user for actions.
This should be set to a function that prompts the use for an
action and returns the symbol naming the action command. The
default value, `embark-keymap-prompter' activates the type
specific action keymap given in `embark-keymap-alist'.
There is also `embark-completing-read-prompter' which
prompts for an action with completion."
:type '(choice (const :tag "Use action keymaps" embark-keymap-prompter)
(const :tag "Read action with completion"
embark-completing-read-prompter)
(function :tag "Other")))
(defcustom embark-keymap-prompter-key "@"
"Key to switch to the keymap prompter from `embark-completing-read-prompter'.
The key must be either a string or a vector.
This is the key representation accepted by `define-key'."
:type '(choice key-sequence (const :tag "None" nil)))
(defcustom embark-cycle-key nil
"Key used for `embark-cycle'.
If the key is set to nil it defaults to the global binding of
`embark-act'. The key must be either a string or a vector. This
is the key representation accepted by `define-key'."
:type '(choice key-sequence (const :tag "Use embark-act key" nil)))
(defcustom embark-help-key "\C-h"
"Key used for help.
The key must be either nil, a string or a vector. This
is the key representation accepted by `define-key'."
:type '(choice (const :tag "Use 'C-h'" "\C-h")
(const :tag "Use '?'" "?")
(const :tag "None" nil)
key-sequence))
(defcustom embark-keybinding-repeat
(propertize "*" 'face 'embark-keybinding-repeat)
"Indicator string for repeatable keybindings.
Keybindings are formatted by the `completing-read' prompter and
the verbose indicator."
:type 'string)
(defface embark-keybinding-repeat
'((t :inherit font-lock-builtin-face))
"Face used to indicate keybindings as repeatable.")
(defface embark-keybinding '((t :inherit success))
"Face used to display key bindings.
Used by `embark-completing-read-prompter' and `embark-keymap-help'.")
(defface embark-keymap '((t :slant italic))
"Face used to display keymaps.
Used by `embark-completing-read-prompter' and `embark-keymap-help'.")
(defface embark-target '((t :inherit highlight))
"Face used to highlight the target at point during `embark-act'.")
(defcustom embark-quit-after-action t
"Should `embark-act' quit the minibuffer?
This controls whether calling `embark-act' without a prefix
argument quits the minibuffer or not. You can always get the
opposite behavior to that indicated by this variable by calling
`embark-act' with \\[universal-argument].
Note that `embark-act' can also be called from outside the
minibuffer and this variable is irrelevant in that case.
In addition to t or nil this variable can also be set to an
alist to specify the minibuffer quitting behavior per command.
In the alist case one can additionally use the key t to
prescribe a default for commands not used as alist keys."
:type '(choice boolean
(alist :key-type (choice function (const t))
:value-type boolean)))
(defcustom embark-confirm-act-all t
"Should `embark-act-all' prompt the user for confirmation?
Even if this variable is nil you may still be prompted to confirm
some uses of `embark-act-all', namely, for those actions whose
entry in `embark-pre-action-hooks' includes `embark--confirm'."
:type 'boolean)
(defcustom embark-default-action-overrides nil
"Alist associating target types with overriding default actions.
When the source of a target is minibuffer completion, the default
action for it is usually the command that opened the minibuffer
in the first place but this can be overridden for a given type by
an entry in this list.
For example, if you run `delete-file' the default action for its
completion candidates is `delete-file' itself. You may prefer to
make `find-file' the default action for all files, even if they
wre obtained from a `delete-file' prompt. In that case you can
configure that by adding an entry to this variable pairing `file'
with `find-file'.
In addition to target types, you can also use as keys in this alist,
pairs of a target type and a command name. Such a pair indicates that
the override only applies if the target was obtained from minibuffer
completion from that command. For example adding an
entry (cons (cons \\='file \\='delete-file) \\='find-file) to this alist would
indicate that for files at the prompt of the `delete-file' command,
`find-file' should be used as the default action."
:type '(alist :key-type (choice (symbol :tag "Type")
(cons (symbol :tag "Type")
(symbol :tag "Command")))
:value-type (function :tag "Default action")))
(defcustom embark-target-injection-hooks
'((async-shell-command embark--allow-edit embark--shell-prep)
(shell-command embark--allow-edit embark--shell-prep)
(pp-eval-expression embark--eval-prep)
(package-delete embark--force-complete)
;; commands evaluating code found in the buffer, which may in turn prompt
(embark-pp-eval-defun embark--ignore-target)
(eval-defun embark--ignore-target)
(eval-last-sexp embark--ignore-target)
(embark-eval-replace embark--ignore-target)
;; commands which prompt for something that is *not* the target
(write-region embark--ignore-target)
(append-to-file embark--ignore-target)
(shell-command-on-region embark--ignore-target)
(format-encode-region embark--ignore-target)
(format-decode-region embark--ignore-target)
(xref-find-definitions embark--ignore-target)
(xref-find-references embark--ignore-target)
(sort-regexp-fields embark--ignore-target)
(align-regexp embark--ignore-target))
"Alist associating commands with post-injection setup hooks.
For commands appearing as keys in this alist, run the
corresponding value as a setup hook after injecting the target
into in the minibuffer and before acting on it. The hooks must
accept arbitrary keyword argument. The :action symbol, the
:target string and target :type are always present. For actions
at point the target bounds are passed too. The default pre-action
hook is specified by the entry with key t. Furthermore, hooks with
the key :always are executed always."
:type '(alist :key-type
(choice symbol
(const :tag "Default" t)
(const :tag "Always" :always))
:value-type hook))
(defcustom embark-pre-action-hooks
`(;; commands that need to position point at the beginning or end
(eval-last-sexp embark--end-of-target)
(indent-pp-sexp embark--beginning-of-target)
(backward-up-list embark--beginning-of-target)
(backward-list embark--beginning-of-target)
(forward-list embark--end-of-target)
(forward-sexp embark--end-of-target)
(backward-sexp embark--beginning-of-target)
(raise-sexp embark--beginning-of-target)
(kill-sexp embark--beginning-of-target)
(mark-sexp embark--beginning-of-target)
(transpose-sexps embark--end-of-target)
(transpose-sentences embark--end-of-target)
(transpose-paragraphs embark--end-of-target)
(forward-sentence embark--end-of-target)
(backward-sentence embark--beginning-of-target)
(backward-paragraph embark--beginning-of-target)
;; region commands
(mark embark--mark-target)
(kill-region embark--mark-target)
(kill-ring-save embark--mark-target)
(indent-region embark--mark-target)
(ispell-region embark--mark-target)
(fill-region embark--mark-target)
(upcase-region embark--mark-target)
(downcase-region embark--mark-target)
(capitalize-region embark--mark-target)
(count-words-region embark--mark-target)
(shell-command-on-region embark--mark-target)
(delete-region embark--mark-target)
(format-encode-region embark--mark-target)
(format-decode-region embark--mark-target)
(write-region embark--mark-target)
(append-to-file embark--mark-target)
(shell-command-on-region embark--mark-target)
(embark-eval-replace embark--mark-target)
;; commands we want to be able to jump back from
;; (embark-find-definition achieves this by calling
;; xref-find-definitions which pushes the markers itself)
(find-library embark--xref-push-marker)
;; commands which prompt the user for confirmation before running
(delete-file embark--confirm)
(delete-directory embark--confirm)
(kill-buffer embark--confirm)
(embark-kill-buffer-and-window embark--confirm)
(bookmark-delete embark--confirm)
(package-delete embark--confirm)
(,'tab-bar-close-tab-by-name embark--confirm) ;; Avoid package-lint warning
;; search for region contents outside said region
(embark-isearch embark--unmark-target)
(occur embark--unmark-target)
(query-replace embark--beginning-of-target embark--unmark-target)
(query-replace-regexp embark--beginning-of-target embark--unmark-target)
;; narrow to target for duration of action
(repunctuate-sentences embark--narrow-to-target)
;; use directory of target as default-directory
(shell embark--cd embark--universal-argument)
(eshell embark--cd embark--universal-argument))
"Alist associating commands with pre-action hooks.
The hooks are run right before an action is embarked upon. See
`embark-target-injection-hooks' for information about the hook
arguments and more details."
:type '(alist :key-type
(choice symbol
(const :tag "Default" t)
(const :tag "Always" :always))
:value-type hook))
(defcustom embark-post-action-hooks
`((bookmark-delete embark--restart)
(bookmark-rename embark--restart)
(delete-file embark--restart)
(embark-kill-ring-remove embark--restart)
(embark-recentf-remove embark--restart)
(embark-history-remove embark--restart)
(rename-file embark--restart)
(copy-file embark--restart)
(delete-directory embark--restart)
(make-directory embark--restart)
(kill-buffer embark--restart)
(embark-rename-buffer embark--restart)
(,'tab-bar-rename-tab-by-name embark--restart) ;; Avoid package-lint warning
(,'tab-bar-close-tab-by-name embark--restart)
(package-delete embark--restart))
"Alist associating commands with post-action hooks.
The hooks are run after an embarked upon action concludes. See
`embark-target-injection-hooks' for information about the hook
arguments and more details."
:type '(alist :key-type
(choice symbol
(const :tag "Default" t)
(const :tag "Always" :always))
:value-type hook))
(defcustom embark-multitarget-actions '(embark-insert embark-copy-as-kill)
"Commands for which `embark-act-all' should pass a list of targets.
Normally `embark-act-all' runs the same action on each candiate
separately, but when a command included in this variable's value
is used as an action, `embark-act-all' will instead call it
non-interactively with a single argument: the list of all
candidates. For commands on this list `embark-act' behaves
similarly: it calls them non-interactively with a single
argument: a one element list containing the target."
:type '(repeat function))
(defcustom embark-repeat-actions
'((mark . region)
;; outline commands
outline-next-visible-heading outline-previous-visible-heading
outline-forward-same-level outline-backward-same-level
outline-demote outline-promote
outline-show-subtree (outline-mark-subtree . region)
outline-move-subtree-up outline-move-subtree-down
outline-up-heading outline-hide-subtree outline-cycle
;; org commands (remapped outline commands)
org-forward-heading-same-level org-backward-heading-same-level
org-next-visible-heading org-previous-visible-heading
org-demote-subtree org-promote-subtree
org-show-subtree (org-mark-subtree . region)
org-move-subtree-up org-move-subtree-down
;; transpose commands
transpose-sexps transpose-sentences transpose-paragraphs
;; movement
embark-next-symbol embark-previous-symbol
backward-up-list backward-list forward-list forward-sexp
backward-sexp forward-sentence backward-sentence
forward-paragraph backward-paragraph)
"List of repeatable actions.
When you use a command on this list as an Embark action from
outside the minibuffer, `embark-act' does not exit but instead
lets you act again on the possibly new target you reach.
By default, after using one of these actions, when `embark-act'
looks for targets again, it will start the target cycle at the
same type as the previously acted upon target; that is, you
\"don't loose your place in the target cycle\".
Sometimes, however, you'll want to prioritze a different type of
target to continue acting on. The main example of this that if
you use a marking command as an action, you almost always want to
act on the region next. For those cases, in addition to
commands, you can also place on this list a pair of a command and
the desired starting type for the target cycle for the next
action."
:type '(repeat (choice function
(cons function
(symbol :tag "Next target type")))))
;;; Stashing information for actions in buffer local variables
(defvar-local embark--type nil
"Cache for the completion type, meant to be set buffer-locally.")
(defvar-local embark--target-buffer nil
"Cache for the previous buffer, meant to be set buffer-locally.")
(defvar-local embark--target-window nil
"Cache for the previous window, meant to be set buffer-locally.
Since windows can be reused to display different buffers, this
window should only be used if it displays the buffer stored in
the variable `embark--target-buffer'.")
(defvar-local embark--command nil
"Command that started the completion session.")
(defvar-local embark--toggle-quit nil
"Should we toggle the default quitting behavior for the next action?")
(defun embark--minibuffer-point ()
"Return length of minibuffer contents."
(max 0 (- (point) (minibuffer-prompt-end))))
(defun embark--default-directory ()
"Guess a reasonable default directory for the current candidates."
(if (and (minibufferp) minibuffer-completing-file-name)
(let ((end (minibuffer-prompt-end))
(contents (minibuffer-contents)))
(expand-file-name
(substitute-in-file-name
(buffer-substring
end
(+ end
(or (cdr
(last
(completion-all-completions
contents
minibuffer-completion-table
minibuffer-completion-predicate
(embark--minibuffer-point))))
(cl-position ?/ contents :from-end t)
0))))))
default-directory))
(defun embark--target-buffer ()
"Return buffer that should be targeted by Embark actions."
(cond
((and (minibufferp) (minibuffer-selected-window))
(window-buffer (minibuffer-selected-window)))
((and embark--target-buffer (buffer-live-p embark--target-buffer))
embark--target-buffer)
(t (current-buffer))))
(defun embark--target-window (&optional display)
"Return window which should be selected when Embark actions run.
If DISPLAY is non-nil, call `display-buffer' to produce the
window if necessary."
(cond
((and (minibufferp) (minibuffer-selected-window))
(minibuffer-selected-window))
((and embark--target-window
(window-live-p embark--target-window)
(or (not (buffer-live-p embark--target-buffer))
(eq (window-buffer embark--target-window) embark--target-buffer)))
embark--target-window)
((and embark--target-buffer (buffer-live-p embark--target-buffer))
(or (get-buffer-window embark--target-buffer)
(when display (display-buffer embark--target-buffer))))
(display (selected-window))))
(defun embark--cache-info (buffer)
"Cache information needed for actions in variables local to BUFFER.
BUFFER defaults to the current buffer."
(let ((cmd embark--command)
(dir (embark--default-directory))
(target-buffer (embark--target-buffer))
(target-window (embark--target-window)))
(with-current-buffer buffer
(setq embark--command cmd
default-directory dir
embark--target-buffer target-buffer
embark--target-window target-window))))
(defun embark--cache-info--completion-list ()
"Cache information needed for actions in a *Completions* buffer.
Meant to be be added to `completion-setup-hook'."
;; when completion-setup-hook hook runs, the *Completions* buffer is
;; available in the variable standard-output
(embark--cache-info standard-output)
(when (minibufferp completion-reference-buffer)
(with-current-buffer standard-output
(setq embark--type
(completion-metadata-get (embark--metadata) 'category)))))
;; We have to add this *after* completion-setup-function because that's
;; when the buffer is put in completion-list-mode and turning the mode
;; on kills all local variables! So we use a depth of 5.
(add-hook 'completion-setup-hook #'embark--cache-info--completion-list 5)
;;;###autoload
(progn
(defun embark--record-this-command ()
"Record command which opened the minibuffer.
We record this because it will be the default action.
This function is meant to be added to `minibuffer-setup-hook'."
(setq-local embark--command this-command))
(add-hook 'minibuffer-setup-hook #'embark--record-this-command))
;;; Internal variables
(defvar embark--prompter-history nil
"History used by the `embark-completing-read-prompter'.")
;;; Core functionality
(defconst embark--verbose-indicator-buffer " *Embark Actions*")
(defvar embark--minimal-indicator-overlay nil)
(defun embark--metadata ()
"Return current minibuffer completion metadata."
(completion-metadata
(buffer-substring-no-properties
(minibuffer-prompt-end)
(max (minibuffer-prompt-end) (point)))
minibuffer-completion-table
minibuffer-completion-predicate))
(defun embark-target-active-region ()
"Target the region if active."
(when (use-region-p)
(let ((start (region-beginning))
(end (region-end)))
`(region ,(buffer-substring start end) . (,start . ,end)))))
(autoload 'dired-get-filename "dired")
(declare-function image-dired-original-file-name "image-dired")
(defun embark-target-file-at-point ()
"Target file at point.
This function mostly relies on `ffap-file-at-point', with the
following exceptions:
- In `dired-mode', it uses `dired-get-filename' instead.
- In `imaged-dired-thumbnail-mode', it uses
`image-dired-original-file-name' instead."
(if-let (file (or (and (derived-mode-p 'dired-mode)
(dired-get-filename t 'no-error-if-not-filep))
(and (derived-mode-p 'image-dired-thumbnail-mode)
(image-dired-original-file-name))))
(save-excursion
(end-of-line)
`(file ,(abbreviate-file-name (expand-file-name file))
,(save-excursion
(re-search-backward " " (line-beginning-position) 'noerror)
(1+ (point)))
. ,(point)))
(when-let* ((ffap-file (ffap-file-at-point))
(tap-file (thing-at-point 'filename))
((not (or (ffap-url-p tap-file) (ffap-el-mode tap-file)))))
`(file ,(abbreviate-file-name (expand-file-name ffap-file))
;; TODO the boundaries may be wrong, this should be generalized.
;; Unfortunately ffap does not make the bounds available.
. ,(bounds-of-thing-at-point 'filename)))))
(defun embark-target-library-file-at-point ()
"Target the file of the Emacs Lisp library at point.
The function `embark-target-file-at-point' could also easily
target Emacs Lisp library files, the only reason it doesn't is so
that library files and other types of file targets can be given
different priorities in `embark-target-finders'."
(when-let* ((name (thing-at-point 'filename))
(lib (ffap-el-mode name)))
`(file ,lib . ,(bounds-of-thing-at-point 'filename))))
(defun embark-target-bug-reference-at-point ()
"Target a bug reference at point."
(when-let ((ov (seq-find (lambda (ov) (overlay-get ov 'bug-reference-url))
(overlays-at (point)))))
`(url ,(overlay-get ov 'bug-reference-url)
,(overlay-start ov) . ,(overlay-end ov))))
(defun embark-target-package-at-point ()
"Target the package on the current line in a packages buffer."
(when (derived-mode-p 'package-menu-mode)
(when-let ((pkg (get-text-property (point) 'tabulated-list-id)))
`(package ,(symbol-name (package-desc-name pkg))
,(line-beginning-position) . ,(line-end-position)))))
(defun embark-target-email-at-point ()
"Target the email address at point."
(when-let ((email (thing-at-point 'email)))
(when (string-prefix-p "mailto:" email)
(setq email (string-remove-prefix "mailto:" email)))
`(email ,email . ,(bounds-of-thing-at-point 'email))))
(defun embark-target-url-at-point ()
"Target the URL at point."
(if-let ((url (or (get-text-property (point) 'shr-url)
(get-text-property (point) 'image-url))))
`(url ,url
,(previous-single-property-change
(min (1+ (point)) (point-max)) 'mouse-face nil (point-min))
. ,(next-single-property-change
(point) 'mouse-face nil (point-max)))
(when-let ((url (thing-at-point 'url)))
`(url ,url . ,(thing-at-point-bounds-of-url-at-point t)))))
(declare-function widget-at "wid-edit")
(defun embark-target-custom-variable-at-point ()
"Target the variable corresponding to the customize widget at point."
(when (derived-mode-p 'Custom-mode)
(save-excursion
(beginning-of-line)
(when-let* ((widget (widget-at (point)))
(var (and (eq (car widget) 'custom-visibility)
(plist-get (cdr widget) :parent)))
(sym (and (eq (car var) 'custom-variable)
(plist-get (cdr var) :value))))
`(variable
,(symbol-name sym)
,(point)
. ,(progn
(re-search-forward ":" (line-end-position) 'noerror)
(point)))))))
;; NOTE: There is also (thing-at-point 'list), however it does
;; not work on strings and requires the point to be inside the
;; parentheses. This version here is slightly more general.
(defun embark-target-expression-at-point ()
"Target expression at point."
(cl-flet ((syntax-p (class &optional (delta 0))
(and (<= (point-min) (+ (point) delta) (point-max))
(eq (pcase class
('open 4) ('close 5) ('prefix 6) ('string 7))
(syntax-class (syntax-after (+ (point) delta)))))))
(when-let
((start
(pcase-let ((`(_ ,open _ ,string _ _ _ _ ,start _ _) (syntax-ppss)))
(ignore-errors ; set start=nil if delimiters are unbalanced
(cond
(string start)
((or (syntax-p 'open) (syntax-p 'prefix))
(save-excursion (backward-prefix-chars) (point)))
((syntax-p 'close -1)
(save-excursion
(backward-sexp) (backward-prefix-chars) (point)))
((syntax-p 'string) (point))
((syntax-p 'string -1) (scan-sexps (point) -1))
(t open)))))
(end (ignore-errors (scan-sexps start 1))))
(unless (eq start (car (bounds-of-thing-at-point 'defun)))
`(expression ,(buffer-substring start end) ,start . ,end)))))
(defmacro embark-define-thingatpt-target (thing &rest modes)
"Define a target finder for THING using the thingatpt library.
If any MODES are given, the target finder only applies to buffers
in one of those major modes."
(declare (indent 1))
`(defun ,(intern (format "embark-target-%s-at-point" thing)) ()
,(format "Target %s at point." thing)
(when ,(if modes `(derived-mode-p ,@(mapcar (lambda (m) `',m) modes)) t)
(when-let (bounds (bounds-of-thing-at-point ',thing))
(cons ',thing (cons
(buffer-substring (car bounds) (cdr bounds))
bounds))))))
(embark-define-thingatpt-target defun)
(embark-define-thingatpt-target sentence
text-mode help-mode Info-mode man-common)
(embark-define-thingatpt-target paragraph
text-mode help-mode Info-mode man-common)
(defun embark-target-identifier-at-point ()
"Target identifier at point.
In Emacs Lisp and IELM buffers the identifier is promoted to a
symbol, for which more actions are available. Identifiers are
also promoted to symbols if they are interned Emacs Lisp symbols
and found in a buffer in a major mode that is not derived from
`prog-mode' (this is intended for when you might be reading or
writing about Emacs).
As a convenience, in Org Mode an initial ' or surrounding == or
~~ are removed."
(when-let (bounds (bounds-of-thing-at-point 'symbol))
(let ((name (buffer-substring (car bounds) (cdr bounds))))
(when (derived-mode-p 'org-mode)
(cond ((string-prefix-p "'" name)
(setq name (substring name 1))
(cl-incf (car bounds)))
((string-match-p "^\\([=~]\\).*\\1$" name)
(setq name (substring name 1 -1))
(cl-incf (car bounds))
(cl-decf (cdr bounds)))))
`(,(if (or (derived-mode-p 'emacs-lisp-mode 'inferior-emacs-lisp-mode)
(and
(not (derived-mode-p 'prog-mode))
(when-let ((sym (intern-soft name)))
(or (boundp sym) (fboundp sym) (symbol-plist sym)))))
'symbol
'identifier)
,name
. ,bounds))))
(defun embark-target-heading-at-point ()
"Target the outline heading at point."
(let ((beg (line-beginning-position))
(end (line-end-position)))
(when (save-excursion
(goto-char beg)
(and (bolp)
(looking-at
;; default definition from outline.el
(or (bound-and-true-p outline-regexp) "[*\^L]+"))))
(require 'outline) ;; Ensure that outline commands are available
`(heading ,(buffer-substring beg end) ,beg . ,end))))
(defun embark-target-text-heading-at-point ()
"Target the outline heading at point in text modes."
(when (derived-mode-p 'text-mode)
(embark-target-heading-at-point)))
(defun embark-target-prog-heading-at-point ()
"Target the outline heading at point in programming modes."
(when (derived-mode-p 'prog-mode)
(embark-target-heading-at-point)))
(defun embark-target-top-minibuffer-completion ()
"Target the top completion candidate in the minibuffer.
Return the category metadatum as the type of the target.
This target finder is meant for the default completion UI and
completion UI highly compatible with it, like Icomplete.
Many completion UIs can still work with Embark but will need
their own target finder. See for example
`embark--vertico-selected' or `embark--selectrum-selected'."
(when (and (minibufferp) minibuffer-completion-table)
(pcase-let* ((`(,category . ,candidates) (embark-minibuffer-candidates))
(contents (minibuffer-contents))
(top (if (test-completion contents
minibuffer-completion-table
minibuffer-completion-predicate)
contents
(let ((completions (completion-all-sorted-completions)))
(if (null completions)
contents
(concat
(substring contents
0 (or (cdr (last completions)) 0))
(car completions)))))))
(cons category (or (car (member top candidates)) top)))))
(defun embark-target-collect-candidate ()
"Target the collect candidate at point."
(when (derived-mode-p 'embark-collect-mode)
(when-let ((button
(pcase (get-text-property (point) 'tabulated-list-column-name)
("Candidate" (button-at (point)))
("Annotation" (previous-button (point)))))
(start (button-start button))
(end (button-end button))
(candidate (tabulated-list-get-id)))
`(,embark--type
,(if (eq embark--type 'file)
(abbreviate-file-name (expand-file-name candidate))
candidate)
,start . ,end))))
(defun embark-target-completion-at-point ()
"Return the completion candidate at point in a completions buffer."
(when (derived-mode-p 'completion-list-mode)
(if (not (get-text-property (point) 'mouse-face))
(user-error "No completion here")
;; this fairly delicate logic is taken from `choose-completion'
(let (beg end)
(cond
((and (not (eobp)) (get-text-property (point) 'mouse-face))
(setq end (point) beg (1+ (point))))
((and (not (bobp))
(get-text-property (1- (point)) 'mouse-face))
(setq end (1- (point)) beg (point)))
(t (user-error "No completion here")))
(setq beg (previous-single-property-change beg 'mouse-face))
(setq end (or (next-single-property-change end 'mouse-face)
(point-max)))
(let ((raw (or (get-text-property beg 'completion--string)
(buffer-substring beg end))))
`(,embark--type
,(if (eq embark--type 'file)
(abbreviate-file-name (expand-file-name raw))
raw)
,beg . ,end))))))
(defun embark--cycle-key ()
"Return the key to use for `embark-cycle'."
(or embark-cycle-key (car (where-is-internal #'embark-act))))
(defun embark--raw-action-keymap (type)
"Return raw action map for targets of given TYPE.
This does not take into account the default action, help key or
cycling bindings, just what's registered in
`embark-keymap-alist'."
(make-composed-keymap
(mapcar #'symbol-value
(let ((actions (or (alist-get type embark-keymap-alist)
(alist-get t embark-keymap-alist))))
(if (consp actions) actions (list actions))))))
(defun embark--action-keymap (type cycle)
"Return action keymap for targets of given TYPE.
If CYCLE is non-nil bind `embark-cycle'."
(make-composed-keymap
(let ((map (make-sparse-keymap))
(default-action (embark--default-action type)))
(define-key map [13] default-action)
(when-let ((cycle-key (and cycle (embark--cycle-key))))
(define-key map cycle-key #'embark-cycle))
(when embark-help-key
(define-key map embark-help-key #'embark-keymap-help))
map)
(embark--raw-action-keymap type)))
(defun embark--truncate-target (target)
"Truncate TARGET string."
(unless (stringp target)
(setq target (format "%s" target)))
(if-let (pos (string-match-p "\n" target))
(concat (car (split-string target "\n" 'omit-nulls "\\s-*")) "…")
target))
(defun embark--format-targets (target shadowed-targets rep)
"Return a formatted string indicating the TARGET of an action.
This is used internally by the minimal indicator and for the
targets section of the verbose indicator. The string will also
mention any SHADOWED-TARGETS. A non-nil REP indicates we are in
a repeating sequence of actions."
(let ((act (propertize
(cond
((plist-get target :multi) "∀ct")
(rep "Rep")
(t "Act"))
'face 'highlight)))
(cond
((eq (plist-get target :type) 'embark-become)
(propertize "Become" 'face 'highlight))
((and (minibufferp)
(not (eq 'embark-keybinding
(completion-metadata-get
(embark--metadata)
'category))))
;; we are in a minibuffer but not from the
;; completing-read prompter, use just "Act"
act)
((plist-get target :multi)
(format "%s on %s %ss"
act
(plist-get target :multi)
(plist-get target :type)))
(t (format
"%s on %s%s '%s'"
act
(plist-get target :type)
(if shadowed-targets
(format (propertize "(%s)" 'face 'shadow)
(string-join
(mapcar (lambda (x)
(symbol-name (plist-get x :type)))
shadowed-targets)
", "))
"")
(embark--truncate-target (plist-get target :target)))))))
(defun embark-minimal-indicator ()
"Minimal indicator, appearing in the minibuffer prompt or echo area.
This indicator displays a message showing the types of all
targets, starting with the current target, and the value of the
current target. The message is displayed in the echo area, or if
the minibuffer is open, the message is added to the prompt."
(lambda (&optional keymap targets _prefix)
(if (null keymap)
(when embark--minimal-indicator-overlay
(delete-overlay embark--minimal-indicator-overlay)
(setq-local embark--minimal-indicator-overlay nil))
(let ((indicator (embark--format-targets
(car targets) (cdr targets)
(eq (lookup-key keymap [13]) #'embark-done))))
(if (not (minibufferp))
(message "%s" indicator)
(unless embark--minimal-indicator-overlay
(setq-local embark--minimal-indicator-overlay
(make-overlay (point-min) (point-min)
(current-buffer) t t)))
(overlay-put embark--minimal-indicator-overlay