-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
embark.texi
1563 lines (1321 loc) · 67.4 KB
/
embark.texi
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
\input texinfo @c -*- texinfo -*-
@c %**start of header
@setfilename embark.info
@settitle Embark: Emacs Mini-Buffer Actions Rooted in Keymaps
@documentencoding UTF-8
@documentlanguage en
@c %**end of header
@dircategory Emacs misc features
@direntry
* Embark: (embark). Emacs Mini-Buffer Actions Rooted in Keymaps.
@end direntry
@finalout
@titlepage
@title Embark: Emacs Mini-Buffer Actions Rooted in Keymaps
@author Omar Antolín Camarena
@end titlepage
@contents
@ifnottex
@node Top
@top Embark: Emacs Mini-Buffer Actions Rooted in Keymaps
@end ifnottex
@menu
* Overview::
* Quick start::
* Advanced configuration::
* How does Embark call the actions?::
* Embark, Marginalia and Consult: Embark Marginalia and Consult.
* Related Packages::
* Resources::
* Contributions::
* Acknowledgments::
@detailmenu
--- The Detailed Node Listing ---
Overview
* Acting on targets::
* The default action on a target::
* Working with sets of possible targets::
* Switching to a different command without losing what you've typed::
Working with sets of possible targets
* Selecting some targets to make an ad hoc candidate set::
* @samp{embark-live} a live-updating variant of @samp{embark-collect}::
Advanced configuration
* Showing information about available targets and actions::
* Selecting commands via completions instead of key bindings::
* Quitting the minibuffer after an action::
* Running some setup after injecting the target::
* Running hooks before, after or around an action: Running hooks before after or around an action.
* Creating your own keymaps::
* Defining actions for new categories of targets::
Selecting commands via completions instead of key bindings
* Selecting commands via completion outside of Embark::
Defining actions for new categories of targets
* New minibuffer target example - tab-bar tabs::
* New target example in regular buffers - short Wikipedia links::
How does Embark call the actions?
* Non-interactive functions as actions::
Embark, Marginalia and Consult
* Marginalia::
* Consult::
@end detailmenu
@end menu
@node Overview
@chapter Overview
Embark makes it easy to choose a command to run based on what is near
point, both during a minibuffer completion session (in a way familiar
to Helm or Counsel users) and in normal buffers. Bind the command
@samp{embark-act} to a key and it acts like prefix-key for a keymap of
@emph{actions} (commands) relevant to the @emph{target} around point. With point on
an URL in a buffer you can open the URL in a browser or eww or
download the file it points to. If while switching buffers you spot an
old one, you can kill it right there and continue to select another.
Embark comes preconfigured with over a hundred actions for common
types of targets such as files, buffers, identifiers, s-expressions,
sentences; and it is easy to add more actions and more target types.
Embark can also collect all the candidates in a minibuffer to an
occur-like buffer or export them to a buffer in a major-mode specific
to the type of candidates, such as dired for a set of files, ibuffer
for a set of buffers, or customize for a set of variables.
@menu
* Acting on targets::
* The default action on a target::
* Working with sets of possible targets::
* Switching to a different command without losing what you've typed::
@end menu
@node Acting on targets
@section Acting on targets
You can think of @samp{embark-act} as a keyboard-based version of a
right-click contextual menu. The @samp{embark-act} command (which you should
bind to a convenient key), acts as a prefix for a keymap offering you
relevant @emph{actions} to use on a @emph{target} determined by the context:
@itemize
@item
In the minibuffer, the target is the current top completion
candidate.
@item
In the @samp{*Completions*} buffer the target is the completion at point.
@item
In a regular buffer, the target is the region if active, or else the
file, symbol, URL, s-expression or defun at point.
@end itemize
Multiple targets can be present at the same location and you can cycle
between them by repeating the @samp{embark-act} key binding. The type of
actions offered depend on the type of the target. Here is a sample of
a few of the actions offered in the default configuration:
@itemize
@item
For files you get offered actions like deleting, copying,
renaming, visiting in another window, running a shell command on the
file, etc.
@item
For buffers the actions include switching to or killing the buffer.
@item
For package names the actions include installing, removing or
visiting the homepage.
@item
For Emacs Lisp symbols the actions include finding the definition,
looking up documentation, evaluating (which for a variable
immediately shows the value, but for a function lets you pass it
some arguments first). There are some actions specific to variables,
such as setting the value directly or though the customize system,
and some actions specific to commands, such as binding it to a key.
@end itemize
By default when you use @samp{embark-act} if you don't immediately select an
action, after a short delay Embark will pop up a buffer showing a list
of actions and their corresponding key bindings. If you are using
@samp{embark-act} outside the minibuffer, Embark will also highlight the
current target. These behaviors are configurable via the variable
@samp{embark-indicators}. Instead of selecting an action via its key binding,
you can select it by name with completion by typing @samp{C-h} after
@samp{embark-act}.
Everything is easily configurable: determining the current target,
classifying it, and deciding which 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 variable @samp{embark-keymap-alist}
associates target types with variables containing keymaps, and those
keymaps containing bindings for the actions. (To examine the available
categories and their associated keymaps, you can use @samp{C-h v
embark-keymap-alist} or customize that variable.) For example, in the
default configuration the type @samp{file} is associated with the symbol
@samp{embark-file-map}. That symbol names a keymap with single-letter key
bindings for common Emacs file commands, for instance @samp{c} is bound to
@samp{copy-file}. This means that if you are in the minibuffer after running
a command that prompts for a file, such as @samp{find-file} or @samp{rename-file},
you can copy a file by running @samp{embark-act} and then pressing @samp{c}.
These action keymaps are very convenient but not strictly necessary
when using @samp{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 @samp{embark-act} all of your
key bindings and even @samp{execute-extended-command} can be used to run a
command. For example, if you want to replace all occurrences of the
symbol at point, just use @samp{M-%} as the action, there is no need to bind
@samp{query-replace} in one of Embark's keymaps. Also, those action keymaps
are normal Emacs keymaps and you should feel free to bind in them
whatever commands you find useful as actions and want to be available
through convenient bindings.
The actions in @samp{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).
Emacs's minibuffer completion system includes metadata indicating the
@emph{category} of what is being completed. For example, @samp{find-file}'s
metadata indicates a category of @samp{file} and @samp{switch-to-buffer}'s metadata
indicates a category of @samp{buffer}. Embark has the related notion of the
@emph{type} of a target for actions, and by default when category metadata
is present it is taken to be the type of minibuffer completion
candidates when used as targets. Emacs commands often do not set
useful category metadata so the @uref{https://github.com/minad/marginalia, Marginalia} package, which supplies
this missing metadata, is highly recommended for use with Embark.
Embark's default configuration has actions for the following target
types: files, buffers, symbols, packages, URLs, bookmarks, and as a
somewhat special case, actions for when the region is active. You can
read about the @uref{https://github.com/oantolin/embark/wiki/Default-Actions, default actions and their key bindings} on the GitHub
project wiki.
@node The default action on a target
@section The default action on a target
Embark has a notion of default action for a target:
@itemize
@item
If the target is a minibuffer completion candidate, then the default
action is whatever command opened the minibuffer in the first place.
For example if you run @samp{kill-buffer}, then the default action will be
to kill buffers.
@item
If the target comes from a regular buffer (i.e., not a minibuffer),
then the default action is whatever is bound to @samp{RET} in the keymap of
actions for that type of target. For example, in Embark's default
configuration for a URL found at point the default action is
@samp{browse-url}, because @samp{RET} is bound to @samp{browse-url} in the @samp{embark-url-map}
keymap.
@end itemize
To run the default action you can press @samp{RET} after running @samp{embark-act}.
Note that if there are several different targets at a given location,
each has its own default action, so first cycle to the target you want
and then press @samp{RET} to run the corresponding default action.
There is also @samp{embark-dwim} which runs the default action for the first
target found. It's pretty handy in non-minibuffer buffers: with
Embark's default configuration it will:
@itemize
@item
Open the file at point.
@item
Open the URL at point in a web browser (using the @samp{browse-url}
command).
@item
Compose a new email to the email address at point.
@item
In an Emacs Lisp buffer, if point is on an opening parenthesis or
right after a closing one, it will evaluate the corresponding
expression.
@item
Go to the definition of an Emacs Lisp function, variable or macro at
point.
@item
Find the file corresponding to an Emacs Lisp library at point.
@end itemize
@node Working with sets of possible targets
@section Working with sets of possible targets
Besides acting individually on targets, Embark lets you work
collectively on a set of target @emph{candidates}. For example, while you are
in the minibuffer the candidates are simply the possible completions
of your input. Embark provides three main commands to work on candidate
sets:
@itemize
@item
The @samp{embark-act-all} command runs the same action on each of the
current candidates. It is just like using @samp{embark-act} on each
candidate in turn. (Because you can easily act on many more
candidates than you meant to, by default Embark asks you to confirm
uses of @samp{embark-act-all}; you can turn this off by setting the user
option @samp{embark-confirm-act-all} to @samp{nil}.)
@item
The @samp{embark-collect} command produces a buffer listing all the current
candidates, for you to peruse and run actions on at your leisure.
The candidates are displayed as a list showing additional
annotations. If any of the candidates contain newlines, then
horizontal lines are used to separate candidates.
The Embark Collect buffer is somewhat ``dired-like'': you can select
and deselect candidates through @samp{embark-select} (available as an
action in @samp{embark-act}, bound to @samp{SPC}; but you could also give it a
global key binding). In an Embark Collect buffer @samp{embark-act} is bound
to @samp{a} and @samp{embark-act-all} is bound to @samp{A}; @samp{embark-act-all} will act on
all currently marked candidates if there any, and will act on all
candidates if none are marked. In particular, this means that @samp{a SPC}
will toggle whether the candidate at point is selected, and @samp{A SPC}
will select all candidates if none are selected, or deselect all
selected candidates if there are some.
@item
The @samp{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.
If you use the grepping commands from the @uref{https://github.com/minad/consult/, Consult} package,
@samp{consult-grep}, @samp{consult-git-grep} or @samp{consult-ripgrep}, then you should
install the @samp{embark-consult} package, which adds support for exporting a
list of grep results to an honest grep-mode buffer, on which you can
even use @uref{https://github.com/mhayashi1120/Emacs-wgrep, wgrep} if you wish.
@end itemize
When in doubt choosing between exporting and collecting, a good rule
of thumb is to always prefer @samp{embark-export} since when an exporter to a
special major mode is available for a given type of target, it will be
more featureful than an Embark collect buffer, and if no such exporter
is configured the @samp{embark-export} command falls back to the generic
@samp{embark-collect}.
These commands are always available as ``actions'' (although they do not
act on just the current target but on all candidates) for @samp{embark-act}
and are bound to @samp{A}, @samp{S} (for ``snapshot''), and @samp{E}, respectively, in
@samp{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 @samp{embark-act}.
In Embark Collect or Embark Export buffers that were obtained by
running @samp{embark-collect} or @samp{embark-export} from within a minibuffer
completion session, @samp{g} is bound to a command that restarts the
completion session, that is, the command that opened the minibuffer is
run again and the minibuffer contents restored. You can then interact
normally with the command, perhaps editing the minibuffer contents,
and, if you wish, you can rerun @samp{embark-collect} or @samp{embark-export} to get
an updated buffer.
@menu
* Selecting some targets to make an ad hoc candidate set::
* @samp{embark-live} a live-updating variant of @samp{embark-collect}::
@end menu
@node Selecting some targets to make an ad hoc candidate set
@subsection Selecting some targets to make an ad hoc candidate set
The commands for working with sets of candidates just described,
namely @samp{embark-act-all}, @samp{embark-export} and @samp{embark-collect} by default
work with all candidates defined in the current context. For example,
in the minibuffer they operate on all currently completion candidates,
or in a dired buffer they work on all marked files (or all files if
none are marked). Embark also has a notion of @emph{selection}, where you can
accumulate an ad hoc list of targets for these commands to work on.
The selection is controlled by using the @samp{embark-select} action, bound
to @samp{SPC} in @samp{embark-general-map} so that it is always available (you can
also give @samp{embark-select} a global key binding if you wish; when called
directly, not as an action for @samp{embark-act}, it will select the first
target at point). Calling this action on a target toggles its
membership in the current buffer's Embark selection; that is, it adds
it to selection if not selected and removes it from the selection if
it was selected. Whenever the selection for a buffer is non-empty, the
commands @samp{embark-act-all}, @samp{embark-export} and @samp{embark-collect} will act on
the selection.
To deselect all selected targets, you can use the @samp{embark-select} action
through @samp{embark-act-all}, since this will run @samp{embark-select} on each
member of the current selection. Similarly if no targets are selected
and you are in a minibuffer completion session, running @samp{embark-select}
from @samp{embark-act-all} will select all the current completion candidates.
By default, whenever some targets are selected in the current buffer,
a count of selected targets appears in the mode line. This can be
turned off or customized through the @samp{embark-selection-indicator} user
option.
The selection functionality is supported in every buffer:
@itemize
@item
In the minibuffer this gives a convenient way to act on several
completion candidates that don't follow any simple pattern: just go
through the completions selecting the ones you want, then use
@samp{embark-act-all}. For example, you could attach several files at once
to an email.
@item
For Embark Collect buffers this functionality enables a dired-like
workflow, in which you mark various candidates and apply an action
to all at once. (It supersedes a previous ad hoc dired-like
interface that was implemented only in Embark Collect buffers, with
a slightly different interface.)
@item
In a eww buffer you could use this to select various links you wish
to follow up on, and then collect them into a buffer. Similarly,
while reading Emacs's info manual you could select some symbols you
want to read more about and export them to an @samp{apropos-mode} buffer.
@item
You can use selections in regular text or programming buffers to do
complex editing operations. For example, if you have three
paragraphs scattered over a file and you want to bring them
together, you can select each one, insert them all somewhere and
finally delete all of them (from their original locations).
@end itemize
@node @samp{embark-live} a live-updating variant of @samp{embark-collect}
@subsection @samp{embark-live} a live-updating variant of @samp{embark-collect}
Finally, there is also an @samp{embark-live} variant of the @samp{embark-collect}
command which automatically updates the collection after each change
in the source buffer. Users of a completion UI that automatically
updates and displays the candidate list (such as Vertico, Icomplete,
Fido-mode, or MCT) will probably not want to use
@samp{embark-live} from the minibuffer as they will then have two live
updating displays of the completion candidates!
A more likely use of @samp{embark-live} is to be called from a regular buffer
to display a sort of live updating ``table of contents'' for the buffer.
This depends on having appropriate candidate collectors configured in
@samp{embark-candidate-collectors}. There are not many in Embark's default
configuration, but you can try this experiment: open a dired buffer in
a directory that has very many files, mark a few, and run @samp{embark-live}.
You'll get an Embark Collect buffer containing only the marked files,
which updates as you mark or unmark files in dired. To make
@samp{embark-live} genuinely useful other candidate collectors are required.
The @samp{embark-consult} package (documented near the end of this manual)
contains a few: one for imenu items and one for outline headings as
used by @samp{outline-minor-mode}. Those collectors really do give
@samp{embark-live} a table-of-contents feel.
@node Switching to a different command without losing what you've typed
@section Switching to a different command without losing what you've typed
Embark also has the @samp{embark-become} command which is useful for when
you run a command, start typing at the minibuffer and realize you
meant a different command. The most common case for me is that I run
@samp{switch-to-buffer}, start typing a buffer name and realize I haven't
opened the file I had in mind yet! I'll use this situation as a
running example to illustrate @samp{embark-become}. When this happens I can,
of course, press @samp{C-g} and then run @samp{find-file} and open the file, but
this requires retyping the portion of the file name you already
typed. This process can be streamlined with @samp{embark-become}: while still
in the @samp{switch-to-buffer} you can run @samp{embark-become} and effectively
make the @samp{switch-to-buffer} command become @samp{find-file} for this run.
You can bind @samp{embark-become} to a key in @samp{minibuffer-local-map}, but it is
also available as an action under the letter @samp{B} (uppercase), so you
don't need a binding if you already have one for @samp{embark-act}. So,
assuming I have @samp{embark-act} bound to, say, @samp{C-.}, once I realize I
haven't open the file I can type @samp{C-. B C-x C-f} to have
@samp{switch-to-buffer} become @samp{find-file} without losing what I have already
typed in the minibuffer.
But for even more convenience, @samp{embark-become} offers shorter key
bindings for commands you are likely to want the current command to
become. When you use @samp{embark-become} it looks for the current command in
all keymaps named in the list @samp{embark-become-keymaps} and then activates
all keymaps that contain it. For example, the default value of
@samp{embark-become-keymaps} contains a keymap @samp{embark-become-file+buffer-map}
with bindings for several commands related to files and buffers, in
particular, it binds @samp{switch-to-buffer} to @samp{b} and @samp{find-file} to @samp{f}. So when
I accidentally try to switch to a buffer for a file I haven't opened
yet, @samp{embark-become} finds that the command I ran, @samp{switch-to-buffer}, is
in the keymap @samp{embark-become-file+buffer-map}, so it activates that
keymap (and any others that also contain a binding for
@samp{switch-to-buffer}). The end result is that I can type @samp{C-. B f} to
switch to @samp{find-file}.
@node Quick start
@chapter Quick start
The easiest way to install Embark is from GNU ELPA, just run @samp{M-x
package-install RET embark RET}. (It is also available on MELPA@.) It is
highly recommended to also install @uref{https://github.com/minad/marginalia, Marginalia} (also available on GNU
ELPA), so that Embark can offer you preconfigured actions in more
contexts. For @samp{use-package} users, the following is a very reasonable
starting configuration:
@lisp
(use-package marginalia
:ensure t
:config
(marginalia-mode))
(use-package embark
:ensure t
:bind
(("C-." . embark-act) ;; pick some comfortable binding
("C-;" . embark-dwim) ;; good alternative: M-.
("C-h B" . embark-bindings)) ;; alternative for `describe-bindings'
:init
;; Optionally replace the key help with a completing-read interface
(setq prefix-help-command #'embark-prefix-help-command)
;; Show the Embark target at point via Eldoc. You may adjust the
;; Eldoc strategy, if you want to see the documentation from
;; multiple providers. Beware that using this can be a little
;; jarring since the message shown in the minibuffer can be more
;; than one line, causing the modeline to move up and down:
;; (add-hook 'eldoc-documentation-functions #'embark-eldoc-first-target)
;; (setq eldoc-documentation-strategy #'eldoc-documentation-compose-eagerly)
:config
;; Hide the mode line of the Embark live/completions buffers
(add-to-list 'display-buffer-alist
'("\\`\\*Embark Collect \\(Live\\|Completions\\)\\*"
nil
(window-parameters (mode-line-format . none)))))
;; Consult users will also want the embark-consult package.
(use-package embark-consult
:ensure t ; only need to install it, embark loads it after consult if found
:hook
(embark-collect-mode . consult-preview-at-point-mode))
@end lisp
About the suggested key bindings for @samp{embark-act} and @samp{embark-dwim}:
@itemize
@item
Those key bindings are unlikely to work in the terminal, but
terminal users are probably well aware of this and will know to
select different bindings.
@item
The suggested @samp{C-.} binding is used by default in (at least some
installations of) GNOME to input emojis, and Emacs doesn't even get
a chance to respond to the binding. You can select a different key
binding for @samp{embark-act} or use @samp{ibus-setup} to change the shortcut for
emoji insertion (Emacs 29 uses @samp{C-x 8 e e}, in case you want to set
the same one system-wide).
@item
The suggested alternative of @samp{M-.} for @samp{embark-dwim} is bound by default
to @samp{xref-find-definitions}. That is a very useful command but
overwriting it with @samp{embark-dwim} is sensible since in Embark's
default configuration, @samp{embark-dwim} will also find the definition of
the identifier at point. (Note that @samp{xref-find-definitions} with a
prefix argument prompts you for an identifier, @samp{embark-dwim} does not
cover this case).
@end itemize
Other Embark commands such as @samp{embark-act-all}, @samp{embark-become},
@samp{embark-collect}, and @samp{embark-export} can be run through @samp{embark-act} as
actions bound to @samp{A}, @samp{B}, @samp{S} (for ``snapshot''), and @samp{E} respectively, and
thus don't really need a dedicated key binding, but feel free to bind
them directly if you so wish. If you do choose to bind them directly,
you'll probably want to bind them in @samp{minibuffer-local-map}, since they
are most useful in the minibuffer (in fact, @samp{embark-become} only works
in the minibuffer).
The command @samp{embark-dwim} executes the default action at point. Another good
keybinding for @samp{embark-dwim} is @samp{M-.} since @samp{embark-dwim} acts like
@samp{xref-find-definitions} on the symbol at point. @samp{C-.} can be seen as a
right-click context menu at point and @samp{M-.} acts like left-click. The
keybindings are mnemonic, both act at the point (@samp{.}).
Embark needs to know what your minibuffer completion system considers
to be the list of candidates and which one is the current candidate.
Embark works out of the box if you use Emacs's default tab completion,
the built-in @samp{icomplete-mode} or @samp{fido-mode}, or the third-party packages
@uref{https://github.com/minad/vertico, Vertico} or @uref{https://github.com/abo-abo/swiper, Ivy}.
If you are a @uref{https://emacs-helm.github.io/helm/, Helm} or @uref{https://github.com/abo-abo/swiper, Ivy} user you are unlikely to want Embark since
those packages include comprehensive functionality for acting on
minibuffer completion candidates. (Embark does come with Ivy
integration despite this.)
@node Advanced configuration
@chapter Advanced configuration
@menu
* Showing information about available targets and actions::
* Selecting commands via completions instead of key bindings::
* Quitting the minibuffer after an action::
* Running some setup after injecting the target::
* Running hooks before, after or around an action: Running hooks before after or around an action.
* Creating your own keymaps::
* Defining actions for new categories of targets::
@end menu
@node Showing information about available targets and actions
@section Showing information about available targets and actions
By default, if you run @samp{embark-act} and do not immediately select an
action, after a short delay Embark will pop up a buffer called @samp{*Embark
Actions*} containing a list of available actions with their key
bindings. You can scroll that buffer with the mouse of with the usual
commands @samp{scroll-other-window} and @samp{scroll-other-window-down} (bound by
default to @samp{C-M-v} and @samp{C-M-S-v}).
That functionality is provided by the @samp{embark-mixed-indicator}, but
Embark has other indicators that can provide information about the
target and its type, what other targets you can cycle to, and which
actions have key bindings in the action map for the current type of
target. Any number of indicators can be active at once and the user
option @samp{embark-indicators} should be set to a list of the desired
indicators.
Embark comes with the following indicators:
@itemize
@item
@samp{embark-minimal-indicator}: shows a messages in the echo area or
minibuffer prompt showing the current target and the types of all
targets starting with the current one.
@item
@samp{embark-highlight-indicator}: highlights the target at point; on by
default.
@item
@samp{embark-verbose-indicator}: displays a table of actions and their key
bindings in a buffer; this is not on by default, in favor of the
mixed indicator described next.
@item
@samp{embark-mixed-indicator}: starts out by behaving as the minimal
indicator but after a short delay acts as the verbose indicator;
this is on by default.
@item
@samp{embark-isearch-highlight-indicator}: this only does something when
the current target is the symbol at point, in which case it
lazily highlights all occurrences of that symbol in the current
buffer, like isearch; also on by default.
@end itemize
Users of the popular @uref{https://github.com/justbur/emacs-which-key, which-key} package may prefer to use the
@samp{embark-which-key-indicator} from the @uref{https://github.com/oantolin/embark/wiki/Additional-Configuration#use-which-key-like-a-key-menu-prompt, Embark wiki}. Just copy its
definition from the wiki into your configuration and customize the
@samp{embark-indicators} user option to exclude the mixed and verbose
indicators and to include @samp{embark-which-key-indicator}.
If you use @uref{https://github.com/minad/vertico, Vertico}, there is an even easier way to get a
@samp{which-key}-like display that also lets you use completion to narrow
down the list of alternatives, described at the end of the next
section.
@node Selecting commands via completions instead of key bindings
@section Selecting commands via completions instead of key bindings
As an alternative to reading the list of actions in the verbose or
mixed indicators (see the previous section for a description of
these), you can press the @samp{embark-help-key}, which is @samp{C-h} by default
(but you may prefer @samp{?} to free up @samp{C-h} for use as a prefix) after
running @samp{embark-act}. Pressing the help key will prompt you for the name
of an action with completion (but feel free to enter a command that is
not among the offered candidates!), and will also remind you of the
key bindings. You can press @samp{embark-keymap-prompter-key}, which is @samp{@@} by
default, at the prompt and then one of the key bindings to enter the
name of the corresponding action.
You may think that with the @samp{*Embark Actions*} buffer popping up to
remind you of the key bindings you'd never want to use completion to
select an action by name, but personally I find that typing a small
portion of the action name to narrow down the list of candidates feels
significantly faster than visually scanning the entire list of actions.
If you find you prefer selecting actions that way, you can configure
embark to always prompt you for actions by setting the variable
@samp{embark-prompter} to @samp{embark-completing-read-prompter}.
On the other hand, you may wish to continue using key bindings for the
actions you perform most often, and to use completion only to explore
what further actions are available or when you've forgotten a key
binding. In that case, you may prefer to use the minimal indicator,
which does not pop-up an @samp{*Embark Actions*} buffer at all, and to use
the @samp{embark-help-key} whenever you need help. This unobtrusive setup is
achieved with the following configuration:
@lisp
(setq embark-indicators
'(embark-minimal-indicator ; default is embark-mixed-indicator
embark-highlight-indicator
embark-isearch-highlight-indicator))
@end lisp
@uref{https://github.com/minad/vertico, Vertico} users may wish to configure a grid display for the actions and
key-bindings, reminiscent of the popular package @uref{https://github.com/justbur/emacs-which-key, which-key}, but, of
course, enhanced by the use of completion to narrow the list of
commands. In order to get the grid display, put the following in your
Vertico configuration:
@lisp
(add-to-list 'vertico-multiform-categories '(embark-keybinding grid))
(vertico-multiform-mode)
@end lisp
This will make the available keys be shown in a compact grid like in
@samp{which-key}. The @samp{vertico-multiform-mode} also enables keys such as @samp{M-V},
@samp{M-G}, @samp{M-B}, and @samp{M-U} for manually switching between layouts in Vertico
buffers.
@menu
* Selecting commands via completion outside of Embark::
@end menu
@node Selecting commands via completion outside of Embark
@subsection Selecting commands via completion outside of Embark
If you like this completion interface for exploring key bindings for
Embark actions, you may want to use it elsewhere in Emacs. You can use
Embark's completion-based command prompter to list:
@itemize
@item
key bindings under a prefix,
@item
local key bindings, or
@item
all key bindings.
@end itemize
To use it for key bindings under a prefix (you can use this to replace
the @samp{which-key} package, for example), use this configuration:
@lisp
(setq prefix-help-command #'embark-prefix-help-command)
@end lisp
Now, when you have started on a prefix sequence such as @samp{C-x} or @samp{C-c},
pressing @samp{C-h} will bring up the Embark version of the built-in
@samp{prefix-help-command}, which will list the keys under that prefix and
their bindings, and lets you select the one you wanted with completion,
or by key binding if you press @samp{embark-keymap-prompter-key}.
To list local or global key bindings, use the command @samp{embark-bindings}.
You can bind that to @samp{C-h b}, which is the default key binding for the
built-in @samp{describe-bindings} command, which this command can replace. By
default, @samp{embark-bindings} lists local key bindings, typically those
bound in the major mode keymap; to get global bindings as well, call
it with a @samp{C-u} prefix argument.
@node Quitting the minibuffer after an action
@section Quitting the minibuffer after an action
By default, if you call @samp{embark-act} from the minibuffer it quits the
minibuffer after performing the action. You can change this by setting
the user option @samp{embark-quit-after-action} to @samp{nil}. Having @samp{embark-act} @emph{not}
quit the minibuffer can be useful to turn commands into little ``thing
managers''. For example, you can use @samp{find-file} as a little file manager
or @samp{describe-package} as a little package manager: you can run those
commands, perform a series of actions, and then quit the command.
If you want to control the quitting behavior in a fine-grained manner
depending on the action, you can set @samp{embark-quit-after-action} to an
alist, associating commands to either @samp{t} for quitting or @samp{nil} for not
quitting. When using an alist, you can use the special key @samp{t} to
specify the default behavior. For example, to specify that by default
actions should not quit the minibuffer but that using @samp{kill-buffer} as
an action should quit, you can use the following configuration:
@lisp
(setq embark-quit-after-action '((kill-buffer . t) (t . nil)))
@end lisp
The variable @samp{embark-quit-after-action} only specifies a default, that
is, it only controls whether or not @samp{embark-act} quits the minibuffer
when you call it without a prefix argument, and you can select the
opposite behavior to what the variable says by calling @samp{embark-act} with
@samp{C-u}. Also note that both the variable @samp{embark-quit-after-action} and @samp{C-u}
have no effect when you call @samp{embark-act} outside the minibuffer.
If you find yourself using the quitting and non-quitting variants of
@samp{embark-act} about equally often, independently of the action, you may
prefer to simply have separate commands for them instead of a single
command that you call with @samp{C-u} half the time. You could, for example,
keep the default exiting behavior of @samp{embark-act} and define a
non-quitting version as follows:
@lisp
(defun embark-act-noquit ()
"Run action but don't quit the minibuffer afterwards."
(interactive)
(let ((embark-quit-after-action nil))
(embark-act)))
@end lisp
@node Running some setup after injecting the target
@section Running some setup after injecting the target
You can customize what happens after the target is inserted at the
minibuffer prompt of an action. There are
@samp{embark-target-injection-hooks}, that are run by default after injecting
the target into the minibuffer. The variable
@samp{embark-target-injection-hooks} is an alist associating commands to
their setup hooks. There are two special keys: if no setup hook is
specified for a given action, the hook associated to @samp{t} is run; and the
hook associated to @samp{:always} is run regardless of the action. (This
variable used to have the less explicit name of
@samp{embark-setup-action-hooks}, so please update your configuration.)
For example, consider using @samp{shell-command} as an action during file
completion. It would be useful to insert a space before the target
file name and to leave the point at the beginning, so you can
immediately type the shell command to run on that file. That's why in
Embark's default configuration there is an entry in
@samp{embark-target-injection-hooks} associating @samp{shell-command} to a hook that
includes @samp{embark--shell-prep}, a simple helper function that quotes all
the spaces in the file name, inserts an extra space at the beginning
of the line and leaves point to the left of it.
Now, the preparation that @samp{embark--shell-prep} does would be useless if
Embark did what it normally does after it inserts the target of the
action at the minibuffer prompt, which is to ``press @samp{RET}'' for you,
accepting the target as is; if Embark did that for @samp{shell-command} you
wouldn't get a chance to type in the command to execute! That is why
in Embark's default configuration the entry for @samp{shell-command} in
@samp{embark-target-injection-hooks} also contains the function
@samp{embark--allow-edit}.
Embark used to have a dedicated variable @samp{embark-allow-edit-actions} to
which you could add commands for which Embark should forgo pressing
@samp{RET} for you after inserting the target. Since its effect can also be
achieved via the general @samp{embark-target-injection-hooks} mechanism, that
variable has been removed to simplify Embark. Be sure to update your
configuration; if you had something like:
@lisp
(add-to-list 'embark-allow-edit-actions 'my-command)
@end lisp
you should replace it with:
@lisp
(push 'embark--allow-edit
(alist-get 'my-command embark-target-injection-hooks))
@end lisp
Also note that while you could abuse @samp{embark--allow-edit} so that you
have to confirm ``dangerous'' actions such as @samp{delete-file}, it is better
to implement confirmation by adding the @samp{embark--confirm} function to
the appropriate entry of a different hook alist, namely,
@samp{embark-pre-action-hooks}.
Besides @samp{embark--allow-edit}, Embark comes with another function that is
of general utility in action setup hooks: @samp{embark--ignore-target}. Use
it for commands that do prompt you in the minibuffer but for which
inserting the target would be inappropriate. This is not a common
situation but does occasionally arise. For example it is used by
default for @samp{shell-command-on-region}: that command is used as an action
for region targets, and it prompts you for a shell command; you
typically do @emph{not} want the target, that is the contents of the region,
to be entered at that prompt!
@node Running hooks before after or around an action
@section Running hooks before, after or around an action
Embark has three variables, @samp{embark-pre-action-hooks},
@samp{embark-post-action-hooks} and @samp{embark-around-action-hooks}, which are
alists associating commands to hooks that should run before or after
or as around advice for the command when used as an action. As with
@samp{embark-target-injection-hooks}, there are two special keys for the
alists: @samp{t} designates the default hook to run when no specific hook is
specified for a command; and the hook associated to @samp{:always} runs
regardless.
The default values of those variables are fairly extensive, adding
creature comforts to make running actions a smooth experience. Embark
comes with several functions intended to be added to these hooks, and
used in the default values of @samp{embark-pre-action-hooks},
@samp{embark-post-action-hooks} and @samp{embark-around-action-hooks}.
For pre-action hooks:
@table @asis
@item @samp{embark--confirm}
Prompt the user for confirmation before executing
the action. This is used be default for commands deemed ``dangerous'',
or, more accurately, hard to undo, such as @samp{delete-file} and
@samp{kill-buffer}.
@item @samp{embark--unmark-target}
Unmark the active region. Use this for
commands you want to act on the region contents but without the
region being active. The default configuration uses this function as
a pre-action hook for @samp{occur} and @samp{query-replace}, for example, so that
you can use them as actions with region targets to search the whole
buffer for the text contained in the region. Without this pre-action
hook using @samp{occur} as an action for a region target would be
pointless: it would search for the the region contents @emph{in the
region}, (typically, due to the details of regexps) finding only one
match!
@item @samp{embark--beginning-of-target}
Move to the beginning of the target
(for targets that report bounds). This is used by default for
backward motion commands such as @samp{backward-sexp}, so that they don't
accidentally leave you on the current target.
@item @samp{embark--end-of-target}
Move to the end of the target. This is used
similarly to the previous function, but also for commands that act
on the last s-expression like @samp{eval-last-sexp}. This allow you to act
on an s-expression from anywhere inside it and still use
@samp{eval-last-sexp} as an action.
@item @samp{embark--xref-push-markers}
Push the current location on the xref
marker stack. Use this for commands that take you somewhere and for
which you'd like to be able to come back to where you were using
@samp{xref-pop-marker-stack}. This is used by default for @samp{find-library}.
@end table
For post-action hooks:
@table @asis
@item @samp{embark--restart}
Restart the command currently prompting in the
minibuffer, so that the list of completion candidates is updated.
This is useful as a post action hook for commands that delete or
rename a completion candidate; for example the default value of
@samp{embark-post-action-hooks} uses it for @samp{delete-file}, @samp{kill-buffer},
@samp{rename-file}, @samp{rename-buffer}, etc.
@end table
For around-action hooks:
@table @asis
@item @samp{embark--mark-target}
Save existing mark and point location, mark
the target and run the action. Most targets at point outside the
minibuffer report which region of the buffer they correspond to
(this is the information used by @samp{embark-highlight-indicator} to
know what portion of the buffer to highlight); this function marks
that region. It is useful as an around action hook for commands that
expect a region to be marked, for example, it is used by default for
@samp{indent-region} so that it works on s-expression targets, or for
@samp{fill-region} so that it works on paragraph targets.
@item @samp{embark--cd}
Run the action with @samp{default-directory} set to the
directory associated to the current target. The target should be of
type @samp{file}, @samp{buffer}, @samp{bookmark} or @samp{library}, and the associated directory
is what you'd expect in each case.
@item @samp{embark--narrow-to-target}
Run the action with buffer narrowed to
current target. Use this as an around hook to localize the effect of
actions that don't already work on just the region. In the default
configuration it is used for @samp{repunctuate-sentences}.
@item @samp{embark--save-excursion}
Run the action restoring point at the end.
The current default configuration doesn't use this but it is
available for users.
@end table
@node Creating your own keymaps
@section Creating your own keymaps
All internal keymaps are defined with the standard helper macro
@samp{defvar-keymap}. For example a simple version of the file action keymap
could be defined as follows:
@lisp
(defvar-keymap embark-file-map
:doc "Example keymap with a few file actions"
:parent embark-general-map
"d" #'delete-file
"r" #'rename-file
"c" #'copy-file)
@end lisp
These action keymaps are perfectly normal Emacs
keymaps. You may want to inherit from the @samp{embark-general-map} if you
want to access the default Embark actions. Note that @samp{embark-collect}
and @samp{embark-export} are also made available via @samp{embark-general-map}.
@node Defining actions for new categories of targets
@section Defining actions for new categories of targets
It is easy to configure Embark to provide actions for new types of
targets, either in the minibuffer or outside it. I present below two
very detailed examples of how to do this. At several points I'll
explain more than one way to proceed, typically with the easiest
option first. I include the alternative options since there will be
similar situations where the easiest option is not available.
@menu
* New minibuffer target example - tab-bar tabs::
* New target example in regular buffers - short Wikipedia links::
@end menu
@node New minibuffer target example - tab-bar tabs
@subsection New minibuffer target example - tab-bar tabs
As an example let us take a look at the @uref{https://www.gnu.org/software/emacs/manual/html_node/emacs/Tab-Bars.html, tab bars}. I'll explain how
to configure Embark to offer tab-specific actions when you use the
tab-bar-mode commands that mention tabs by name. The configuration
explained here is now built-in to Embark (and Marginalia), but it's
still a good self-contained example. In order to setup up tab actions
you would need to: (1) make sure Embark knows those commands deal with
tabs, (2) define a keymap for tab actions and configure Embark so it
knows that's the keymap you want.
@enumerate
@item
@anchor{Telling Embark about commands that prompt for tabs by name}Telling Embark about commands that prompt for tabs by name