-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
embark.el
4591 lines (4153 loc) · 187 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-2023 Free Software Foundation, Inc.
;; Author: Omar Antolín Camarena <[email protected]>
;; Maintainer: Omar Antolín Camarena <[email protected]>
;; Keywords: convenience
;; Version: 1.1
;; Homepage: https://github.com/oantolin/embark
;; Package-Requires: ((emacs "28.1") (compat "30"))
;; 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-map'. 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:
(require 'compat)
(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."
:link '(info-link :tag "Info Manual" "(embark)")
:link '(url-link :tag "Homepage" "https://github.com/oantolin/embark")
:link '(emacs-library-link :tag "Library Source" "embark.el")
:group 'minibuffer
:prefix "embark-")
(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)
(flymake embark-flymake-map)
(smerge smerge-basic-map embark-general-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-candidate
embark-target-active-region
embark-target-collect-candidate
embark-target-completion-list-candidate
embark-target-text-heading-at-point
embark-target-bug-reference-at-point
embark-target-flymake-at-point
embark-target-smerge-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-guess-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 one of:
1. a cons (TYPE . TARGET) where TARGET is a string and TYPE is a
symbol (which is looked up in `embark-keymap-alist' to
determine which additional keybindings for actions to setup);
2. a dotted list of the form (TYPE TARGET START . END), where
START and END are the buffer positions bounding TARGET, used
for highlighting; or
3. a possibly empty list of targets, each of type 1 or 2 (in
particular if a target finder does not find any targets, it
should return nil)."
:type 'hook)
(defcustom embark-transformer-alist
'((minor-mode . embark--lookup-lighter-minor-mode)
(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 nil or a string. The
string must be accepted by `key-valid-p'."
:type '(choice key (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 a string which is accepted by
`key-valid-p'."
:type '(choice key (const :tag "Use embark-act key" nil)))
(defcustom embark-help-key "C-h"
"Key used for help.
The key must be either nil or a string. The
string must be accepted by `key-valid-p'."
:type '(choice (const "C-h")
(const "?")
(const :tag "None" nil)
key))
(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 (const :tag "Always quit" t)
(const :tag "Never quit" nil)
(alist :tag "Configure per action"
:key-type (choice (function :tag "Action")
(const :tag "All other actions" t))
:value-type (choice (const :tag "Quit" t)
(const :tag "Do not quit" nil)))))
(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
were 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)
(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)
(append-to-buffer 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 arguments. The :action command, 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)
(embark-insert embark--end-of-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-forward embark--unmark-target)
(embark-isearch-backward 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)
(replace-string embark--beginning-of-target embark--unmark-target)
(replace-regexp embark--beginning-of-target embark--unmark-target)
;; mark pseudo-action
(mark embark--mark-target)
;; shells in new buffers
(shell embark--universal-argument)
(eshell 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-around-action-hooks
'(;; use directory of target as default-directory
(shell embark--cd)
(eshell embark--cd)
;; mark the target preserving point and previous mark
(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)
(count-words embark--mark-target)
(delete-duplicate-lines 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)
(append-to-buffer embark--mark-target)
(shell-command-on-region embark--mark-target)
(embark-eval-replace embark--mark-target)
(delete-indentation embark--mark-target)
(comment-dwim embark--mark-target)
(insert-parentheses embark--mark-target)
(insert-pair embark--mark-target)
(org-emphasize embark--mark-target)
;; do the actual work of selecting & deselecting targets
(embark-select embark--select))
"Alist associating commands with post-action hooks.
The hooks are run instead of the embarked upon action. The hook
can decide whether or not to run the action or it can run it
in some special environment, like inside a let-binding or inside
`save-excursion'. Each hook is called with keyword argument :run
providing a function encapsulating the following around hooks and
the action; the hook additionally receives the keyword arguments
used for other types of action hooks, for more details see
`embark-target-injection-hooks'."
:type '(alist :key-type
(choice symbol
(const :tag "Default" t)
(const :tag "Always" :always))
:value-type hook))
(static-if (< emacs-major-version 29)
;; narrow to target for duration of action
(setf (alist-get 'repunctuate-sentences embark-around-action-hooks)
'(embark--narrow-to-target)))
(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 candidate
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
;; navigation commands
flymake-goto-next-error flymake-goto-prev-error
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
;; smerge commands
smerge-refine smerge-combine-with-next smerge-prev smerge-next)
"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 prioritize 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")))))
;;; Overlay properties
;; high priority to override both bug reference and the lazy
;; isearch highlights in embark-isearch-highlight-indicator
(put 'embark-target-overlay 'face 'embark-target)
(put 'embark-target-overlay 'priority 1001)
(put 'embark-selected-overlay 'face 'embark-selected)
(put 'embark-selected-overlay 'priority 1001)
;;; 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-completion-table (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-completion-table (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)
(with-current-buffer standard-output
(when (minibufferp completion-reference-buffer)
(setq embark--type
(completion-metadata-get
(with-current-buffer completion-reference-buffer
(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-guess-file-at-point ()
"Target the file guessed by `ffap' at point."
(when-let ((tap-file (thing-at-point 'filename))
((not (ffap-url-p tap-file))) ; no URLs, those have a target finder
(bounds (bounds-of-thing-at-point 'filename))
(file (ffap-file-at-point)))
;; ffap doesn't make bounds available, so we use
;; thingatpt bounds, which might be a little off
;; adjust bounds if thingatpt gobbled punctuation around file
(when (or (string-match (regexp-quote file) tap-file)
(string-match (regexp-quote (file-name-base file)) tap-file))
(setq bounds (cons (+ (car bounds) (match-beginning 0))
(- (cdr bounds) (- (length tap-file)
(match-end 0))))))
`(file ,(abbreviate-file-name (expand-file-name file)) ,@bounds)))
(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."
(let (file bounds)
(or (and (derived-mode-p 'dired-mode)
(setq file (dired-get-filename t 'no-error-if-not-filep))
(setq bounds
(cons
(save-excursion (dired-move-to-filename) (point))
(save-excursion (dired-move-to-end-of-filename) (point)))))
(and (derived-mode-p 'image-dired-thumbnail-mode)
(setq file (image-dired-original-file-name))
(setq bounds (cons (point) (1+ (point)))))
(when-let ((tap-file (thing-at-point 'filename))
((not (equal (file-name-base tap-file) tap-file)))
(guess (embark-target-guess-file-at-point)))
(setq file (cadr guess) bounds (cddr guess))))
(when file
`(file ,(abbreviate-file-name (expand-file-name file)) ,@bounds))))
(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-overlay-target (name prop &optional pred type target)
"Define a target finder for NAME that targets overlays with property PROP.
The function defined is named embark-target-NAME-at-point and it
returns Embark targets based on the overlays around point. An
overlay provides a target if its property named PROP is non-nil.
If the optional PRED argument is given, it should be an
expression and it further restricts the targets to only those
overlays for which PRED evaluates to non-nil.
The target finder returns target type NAME or optional symbol
TYPE if given.
The target finder returns the substring of the buffer covered by
the overlay as the target string or the result of evaluating the
optional TARGET expression if given.
PRED and TARGET are expressions (not functions) and when evaluated the
symbols `%o' and `%p' are bound to the overlay and the overlay's
property respectively."
`(defun ,(intern (format "embark-target-%s-at-point" name)) ()
,(format "Target %s at point." name)
(when-let ((%o (seq-find
(lambda (%o)
(when-let ((%p (overlay-get %o ',prop)))
(ignore %p)
,(or pred t)))
(overlays-in (max (point-min) (1- (point)))
(min (point-max) (1+ (point))))))
(%p (overlay-get %o ',prop)))
(ignore %p)
(cons ',(or type name)
(cons ,(or target `(buffer-substring-no-properties
(overlay-start %o) (overlay-end %o)))
(cons (overlay-start %o) (overlay-end %o)))))))
(embark-define-overlay-target flymake flymake-diagnostic)
(embark-define-overlay-target bug-reference bug-reference-url nil url %p)
(embark-define-overlay-target smerge smerge (eq %p 'conflict))
(defmacro embark-define-thingatpt-target (thing &rest modes)
"Define a target finder for THING using the thingatpt library.
The function defined is named embark-target-NAME-at-point and it
uses (thing-at-point \\='THING) to find its targets.
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)
(defmacro embark-define-regexp-target
(name regexp &optional type target bounds limit)
"Define a target finder for matches of REGEXP around point.
The function defined is named embark-target-NAME-at-point and it
uses (thing-at-point-looking-at REGEXP) to find its targets.
The target finder returns target type NAME or optional symbol
TYPE if given.
The target finder returns the substring of the buffer matched by
REGEXP as the target string or the result of evaluating the
optional TARGET expression if given. In the expression TARGET
you can use `match-string' to recover the match of the REGEXP or
of any sub-expressions it has.
BOUNDS is an optional expression to compute the bounds of the
target and defaults to (cons (match-beginning 0) (match-end 0)).
The optional LIMIT is the number of characters before and after
point to limit the search to. If LIMIT is nil, search a little
more than the current line (more precisely, the smallest interval
centered at point that includes the current line)."
`(defun ,(intern (format "embark-target-%s-at-point" name)) ()
,(format "Target %s at point." name)
(save-match-data
(when (thing-at-point-looking-at
,regexp
,(or limit '(max (- (pos-eol) (point)) (- (point) (pos-bol)))))
(cons ',(or type name)
(cons ,(or target '(match-string 0))
,(or bounds
'(cons (match-beginning 0) (match-end 0)))))))))
(defun embark--identifier-types (identifier)
"Return list of target types appropriate for IDENTIFIER."
(let ((symbol (intern-soft identifier)))
(if (not
(or (derived-mode-p 'emacs-lisp-mode 'inferior-emacs-lisp-mode)
(and (not (derived-mode-p 'prog-mode))
symbol
(or (boundp symbol) (fboundp symbol) (symbol-plist symbol)))))
'(identifier)
(let* ((library (ffap-el-mode identifier))
(types
(append
(and (commandp symbol) '(command))
(and symbol (boundp symbol) (not (keywordp symbol)) '(variable))
(and (fboundp symbol) (not (commandp symbol)) '(function))
(and (facep symbol) '(face))
(and library '(library))
(and (featurep 'package) (embark--package-desc symbol)
'(package)))))
(when (and library
(looking-back "\\(?:require\\|use-package\\).*"
(line-beginning-position)))
(setq types (embark--rotate types (cl-position 'library types))))
(or types '(symbol))))))
(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)))))
(mapcar (lambda (type) `(,type ,name . ,bounds))
(embark--identifier-types name)))))
(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-candidate ()
"Target the top completion candidate in the minibuffer.