dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

embark.texi (65610B)


      1 \input texinfo    @c -*- texinfo -*-
      2 @c %**start of header
      3 @setfilename embark.info
      4 @settitle Embark: Emacs Mini-Buffer Actions Rooted in Keymaps
      5 @documentencoding UTF-8
      6 @documentlanguage en
      7 @c %**end of header
      8 
      9 @dircategory Emacs misc features
     10 @direntry
     11 * Embark: (embark).     Emacs Mini-Buffer Actions Rooted in Keymaps.
     12 @end direntry
     13 
     14 @finalout
     15 @titlepage
     16 @title Embark: Emacs Mini-Buffer Actions Rooted in Keymaps
     17 @author Omar Antolín Camarena
     18 @end titlepage
     19 
     20 @contents
     21 
     22 @ifnottex
     23 @node Top
     24 @top Embark: Emacs Mini-Buffer Actions Rooted in Keymaps
     25 @end ifnottex
     26 
     27 @menu
     28 * Overview::
     29 * Quick start::
     30 * Advanced configuration::
     31 * How does Embark call the actions?::
     32 * Embark, Marginalia and Consult: Embark Marginalia and Consult. 
     33 * Related Packages::
     34 * Resources::
     35 * Contributions::
     36 * Acknowledgments::
     37 
     38 @detailmenu
     39 --- The Detailed Node Listing ---
     40 
     41 Overview
     42 
     43 * Acting on targets::
     44 * The default action on a target::
     45 * Working with sets of possible targets::
     46 * Switching to a different command without losing what you've typed::
     47 
     48 Working with sets of possible targets
     49 
     50 * Selecting some targets to make an ad hoc candidate set::
     51 * @samp{embark-live} a live-updating variant of @samp{embark-collect}::
     52 
     53 Advanced configuration
     54 
     55 * Showing information about available targets and actions::
     56 * Selecting commands via completions instead of key bindings::
     57 * Quitting the minibuffer after an action::
     58 * Running some setup after injecting the target::
     59 * Running hooks before, after or around an action: Running hooks before after or around an action. 
     60 * Creating your own keymaps::
     61 * Defining actions for new categories of targets::
     62 
     63 Defining actions for new categories of targets
     64 
     65 * New minibuffer target example - tab-bar tabs::
     66 * New target example in regular buffers - short Wikipedia links::
     67 
     68 How does Embark call the actions?
     69 
     70 * Non-interactive functions as actions::
     71 
     72 Embark, Marginalia and Consult
     73 
     74 * Marginalia::
     75 * Consult::
     76 
     77 @end detailmenu
     78 @end menu
     79 
     80 @node Overview
     81 @chapter Overview
     82 
     83 Embark makes it easy to choose a command to run based on what is near
     84 point, both during a minibuffer completion session (in a way familiar
     85 to Helm or Counsel users) and in normal buffers. Bind the command
     86 @samp{embark-act} to a key and it acts like prefix-key for a keymap of
     87 @emph{actions} (commands) relevant to the @emph{target} around point. With point on
     88 an URL in a buffer you can open the URL in a browser or eww or
     89 download the file it points to. If while switching buffers you spot an
     90 old one, you can kill it right there and continue to select another.
     91 Embark comes preconfigured with over a hundred actions for common
     92 types of targets such as files, buffers, identifiers, s-expressions,
     93 sentences; and it is easy to add more actions and more target types.
     94 Embark can also collect all the candidates in a minibuffer to an
     95 occur-like buffer or export them to a buffer in a major-mode specific
     96 to the type of candidates, such as dired for a set of files, ibuffer
     97 for a set of buffers, or customize for a set of variables.
     98 
     99 @menu
    100 * Acting on targets::
    101 * The default action on a target::
    102 * Working with sets of possible targets::
    103 * Switching to a different command without losing what you've typed::
    104 @end menu
    105 
    106 @node Acting on targets
    107 @section Acting on targets
    108 
    109 You can think of @samp{embark-act} as a keyboard-based version of a
    110 right-click contextual menu. The @samp{embark-act} command (which you should
    111 bind to a convenient key), acts as a prefix for a keymap offering you
    112 relevant @emph{actions} to use on a @emph{target} determined by the context:
    113 
    114 @itemize
    115 @item
    116 In the minibuffer, the target is the current top completion
    117 candidate.
    118 @item
    119 In the @samp{*Completions*} buffer the target is the completion at point.
    120 @item
    121 In a regular buffer, the target is the region if active, or else the
    122 file, symbol, URL, s-expression or defun at point.
    123 @end itemize
    124 
    125 Multiple targets can be present at the same location and you can cycle
    126 between them by repeating the @samp{embark-act} key binding. The type of
    127 actions offered depend on the type of the target. Here is a sample of
    128 a few of the actions offered in the default configuration:
    129 
    130 @itemize
    131 @item
    132 For files you get offered actions like deleting, copying,
    133 renaming, visiting in another window, running a shell command on the
    134 file, etc.
    135 @item
    136 For buffers the actions include switching to or killing the buffer.
    137 @item
    138 For package names the actions include installing, removing or
    139 visiting the homepage.
    140 @item
    141 For Emacs Lisp symbols the actions include finding the definition,
    142 looking up documentation, evaluating (which for a variable
    143 immediately shows the value, but for a function lets you pass it
    144 some arguments first). There are some actions specific to variables,
    145 such as setting the value directly or though the customize system,
    146 and some actions specific to commands, such as binding it to a key.
    147 @end itemize
    148 
    149 By default when you use @samp{embark-act} if you don't immediately select an
    150 action, after a short delay Embark will pop up a buffer showing a list
    151 of actions and their corresponding key bindings. If you are using
    152 @samp{embark-act} outside the minibuffer, Embark will also highlight the
    153 current target. These behaviors are configurable via the variable
    154 @samp{embark-indicators}. Instead of selecting an action via its key binding,
    155 you can select it by name with completion by typing @samp{C-h} after
    156 @samp{embark-act}.
    157 
    158 Everything is easily configurable: determining the current target,
    159 classifying it, and deciding which actions are offered for each type
    160 in the classification. The above introduction just mentions part of
    161 the default configuration.
    162 
    163 Configuring which actions are offered for a type is particularly easy
    164 and requires no programming: the variable @samp{embark-keymap-alist}
    165 associates target types with variables containing keymaps, and those
    166 keymaps containing bindings for the actions. (To examine the available
    167 categories and their associated keymaps, you can use @samp{C-h v
    168 embark-keymap-alist} or customize that variable.) For example, in the
    169 default configuration the type @samp{file} is associated with the symbol
    170 @samp{embark-file-map}. That symbol names a keymap with single-letter key
    171 bindings for common Emacs file commands, for instance @samp{c} is bound to
    172 @samp{copy-file}. This means that if you are in the minibuffer after running
    173 a command that prompts for a file, such as @samp{find-file} or @samp{rename-file},
    174 you can copy a file by running @samp{embark-act} and then pressing @samp{c}.
    175 
    176 These action keymaps are very convenient but not strictly necessary
    177 when using @samp{embark-act}: you can use any command that reads from the
    178 minibuffer as an action and the target of the action will be inserted
    179 at the first minibuffer prompt. After running @samp{embark-act} all of your
    180 key bindings and even @samp{execute-extended-command} can be used to run a
    181 command. For example, if you want to replace all occurrences of the
    182 symbol at point, just use @samp{M-%} as the action, there is no need to bind
    183 @samp{query-replace} in one of Embark's keymaps. Also, those action keymaps
    184 are normal Emacs keymaps and you should feel free to bind in them
    185 whatever commands you find useful as actions and want to be available
    186 through convenient bindings.
    187 
    188 The actions in @samp{embark-general-map} are available no matter what type
    189 of completion you are in the middle of. By default this includes
    190 bindings to save the current candidate in the kill ring and to insert
    191 the current candidate in the previously selected buffer (the buffer
    192 that was current when you executed a command that opened up the
    193 minibuffer).
    194 
    195 Emacs's minibuffer completion system includes metadata indicating the
    196 @emph{category} of what is being completed. For example, @samp{find-file}'s
    197 metadata indicates a category of @samp{file} and @samp{switch-to-buffer}'s metadata
    198 indicates a category of @samp{buffer}. Embark has the related notion of the
    199 @emph{type} of a target for actions, and by default when category metadata
    200 is present it is taken to be the type of minibuffer completion
    201 candidates when used as targets. Emacs commands often do not set
    202 useful category metadata so the @uref{https://github.com/minad/marginalia, Marginalia} package, which supplies
    203 this missing metadata, is highly recommended for use with Embark.
    204 
    205 Embark's default configuration has actions for the following target
    206 types: files, buffers, symbols, packages, URLs, bookmarks, and as a
    207 somewhat special case, actions for when the region is active. You can
    208 read about the @uref{https://github.com/oantolin/embark/wiki/Default-Actions, default actions and their key bindings} on the GitHub
    209 project wiki.
    210 
    211 @node The default action on a target
    212 @section The default action on a target
    213 
    214 Embark has a notion of default action for a target:
    215 
    216 @itemize
    217 @item
    218 If the target is a minibuffer completion candidate, then the default
    219 action is whatever command opened the minibuffer in the first place.
    220 For example if you run @samp{kill-buffer}, then the default action will be
    221 to kill buffers.
    222 @item
    223 If the target comes from a regular buffer (i.e., not a minibuffer),
    224 then the default action is whatever is bound to @samp{RET} in the keymap of
    225 actions for that type of target. For example, in Embark's default
    226 configuration for a URL found at point the default action is
    227 @samp{browse-url}, because @samp{RET} is bound to @samp{browse-url} in the @samp{embark-url-map}
    228 keymap.
    229 @end itemize
    230 
    231 To run the default action you can press @samp{RET} after running @samp{embark-act}.
    232 Note that if there are several different targets at a given location,
    233 each has its own default action, so first cycle to the target you want
    234 and then press @samp{RET} to run the corresponding default action.
    235 
    236 There is also @samp{embark-dwim} which runs the default action for the first
    237 target found. It's pretty handy in non-minibuffer buffers: with
    238 Embark's default configuration it will:
    239 
    240 @itemize
    241 @item
    242 Open the file at point.
    243 @item
    244 Open the URL at point in a web browser (using the @samp{browse-url}
    245 command).
    246 @item
    247 Compose a new email to the email address at point.
    248 @item
    249 In an Emacs Lisp buffer, if point is on an opening parenthesis or
    250 right after a closing one, it will evaluate the corresponding
    251 expression.
    252 @item
    253 Go to the definition of an Emacs Lisp function, variable or macro at
    254 point.
    255 @item
    256 Find the file corresponding to an Emacs Lisp library at point.
    257 @end itemize
    258 
    259 @node Working with sets of possible targets
    260 @section Working with sets of possible targets
    261 
    262 Besides acting individually on targets, Embark lets you work
    263 collectively on a set of target @emph{candidates}. For example, while you are
    264 in the minibuffer the candidates are simply the possible completions
    265 of your input. Embark provides three main commands to work on candidate
    266 sets:
    267 
    268 @itemize
    269 @item
    270 The @samp{embark-act-all} command runs the same action on each of the
    271 current candidates. It is just like using @samp{embark-act} on each
    272 candidate in turn. (Because you can easily act on many more
    273 candidates than you meant to, by default Embark asks you to confirm
    274 uses of @samp{embark-act-all}; you can turn this off by setting the user
    275 option @samp{embark-confirm-act-all} to @samp{nil}.)
    276 
    277 @item
    278 The @samp{embark-collect} command produces a buffer listing all the current
    279 candidates, for you to peruse and run actions on at your leisure.
    280 The candidates are displayed as a list showing additional
    281 annotations. If any of the candidates contain newlines, then
    282 horizontal lines are used to separate candidates.
    283 
    284 The Embark Collect buffer is somewhat ``dired-like'': you can select
    285 and deselect candidates through @samp{embark-select} (available as an
    286 action in @samp{embark-act}, bound to @samp{SPC}; but you could also give it a
    287 global key binding). In an Embark Collect buffer @samp{embark-act} is bound
    288 to @samp{a} and @samp{embark-act-all} is bound to @samp{A}; @samp{embark-act-all} will act on
    289 all currently marked candidates if there any, and will act on all
    290 candidates if none are marked. In particular, this means that @samp{a SPC}
    291 will toggle whether the candidate at point is selected, and @samp{A SPC}
    292 will select all candidates if none are selected, or deselect all
    293 selected candidates if there are some.
    294 
    295 @item
    296 The @samp{embark-export} command tries to open a buffer in an appropriate
    297 major mode for the set of candidates. If the candidates are files
    298 export produces a Dired buffer; if they are buffers, you get an
    299 Ibuffer buffer; and if they are packages you get a buffer in
    300 package menu mode.
    301 
    302 If you use the grepping commands from the @uref{https://github.com/minad/consult/, Consult} package,
    303 @samp{consult-grep}, @samp{consult-git-grep} or @samp{consult-ripgrep}, then you should
    304 install the @samp{embark-consult} package, which adds support for exporting a
    305 list of grep results to an honest grep-mode buffer, on which you can
    306 even use @uref{https://github.com/mhayashi1120/Emacs-wgrep, wgrep} if you wish.
    307 @end itemize
    308 
    309 When in doubt choosing between exporting and collecting, a good rule
    310 of thumb is to always prefer @samp{embark-export} since when an exporter to a
    311 special major mode is available for a given type of target, it will be
    312 more featureful than an Embark collect buffer, and if no such exporter
    313 is configured the @samp{embark-export} command falls back to the generic
    314 @samp{embark-collect}.
    315 
    316 These commands are always available as ``actions'' (although they do not
    317 act on just the current target but on all candidates) for @samp{embark-act}
    318 and are bound to @samp{A}, @samp{S} (for ``snapshot''), and @samp{E}, respectively, in
    319 @samp{embark-general-map}. This means that you do not have to bind your own
    320 key bindings for these (although you can, of course!), just a key
    321 binding for @samp{embark-act}.
    322 
    323 In Embark Collect or Embark Export buffers that were obtained by
    324 running @samp{embark-collect} or @samp{embark-export} from within a minibuffer
    325 completion session, @samp{g} is bound to a command that restarts the
    326 completion session, that is, the command that opened the minibuffer is
    327 run again and the minibuffer contents restored. You can then interact
    328 normally with the command, perhaps editing the minibuffer contents,
    329 and, if you wish, you can rerun @samp{embark-collect} or @samp{embark-export} to get
    330 an updated buffer.
    331 
    332 @menu
    333 * Selecting some targets to make an ad hoc candidate set::
    334 * @samp{embark-live} a live-updating variant of @samp{embark-collect}::
    335 @end menu
    336 
    337 @node Selecting some targets to make an ad hoc candidate set
    338 @subsection Selecting some targets to make an ad hoc candidate set
    339 
    340 The commands for working with sets of candidates just described,
    341 namely @samp{embark-act-all}, @samp{embark-export} and @samp{embark-collect} by default
    342 work with all candidates defined in the current context. For example,
    343 in the minibuffer they operate on all currently completion candidates,
    344 or in a dired buffer they work on all marked files (or all files if
    345 none are marked). Embark also has a notion of @emph{selection}, where you can
    346 accumulate an ad hoc list of targets for these commands to work on.
    347 
    348 The selection is controlled by using the @samp{embark-select} action, bound
    349 to @samp{SPC} in @samp{embark-general-map} so that it is always available (you can
    350 also give @samp{embark-select} a global key binding if you wish; when called
    351 directly, not as an action for @samp{embark-act}, it will select the first
    352 target at point). Calling this action on a target toggles its
    353 membership in the current buffer's Embark selection; that is, it adds
    354 it to selection if not selected and removes it from the selection if
    355 it was selected. Whenever the selection for a buffer is non-empty, the
    356 commands @samp{embark-act-all}, @samp{embark-export} and @samp{embark-collect} will act on
    357 the selection.
    358 
    359 To deselect all selected targets, you can use the @samp{embark-select} action
    360 through @samp{embark-act-all}, since this will run @samp{embark-select} on each
    361 member of the current selection. Similarly if no targets are selected
    362 and you are in a minibuffer completion session, running @samp{embark-select}
    363 from @samp{embark-act-all} will select all the current completion candidates.
    364 
    365 By default, whenever some targets are selected in the current buffer,
    366 a count of selected targets appears in the mode line. This can be
    367 turned off or customized through the @samp{embark-selection-indicator} user
    368 option.
    369 
    370 The selection functionality is supported in every buffer:
    371 
    372 @itemize
    373 @item
    374 In the minibuffer this gives a convenient way to act on several
    375 completion candidates that don't follow any simple pattern: just go
    376 through the completions selecting the ones you want, then use
    377 @samp{embark-act-all}. For example, you could attach several files at once
    378 to an email.
    379 @item
    380 For Embark Collect buffers this functionality enables a dired-like
    381 workflow, in which you mark various candidates and apply an action
    382 to all at once. (It supersedes a previous ad hoc dired-like
    383 interface that was implemented only in Embark Collect buffers, with
    384 a slightly different interface.)
    385 @item
    386 In a eww buffer you could use this to select various links you wish
    387 to follow up on, and then collect them into a buffer. Similarly,
    388 while reading Emacs's info manual you could select some symbols you
    389 want to read more about and export them to an @samp{apropos-mode} buffer.
    390 @item
    391 You can use selections in regular text or programming buffers to do
    392 complex editing operations. For example, if you have three
    393 paragraphs scattered over a file and you want to bring them
    394 together, you can select each one, insert them all somewhere and
    395 finally delete all of them (from their original locations).
    396 @end itemize
    397 
    398 @node @samp{embark-live} a live-updating variant of @samp{embark-collect}
    399 @subsection @samp{embark-live} a live-updating variant of @samp{embark-collect}
    400 
    401 Finally, there is also an @samp{embark-live} variant of the @samp{embark-collect}
    402 command which automatically updates the collection after each change
    403 in the source buffer. Users of a completion UI that automatically
    404 updates and displays the candidate list (such as Vertico, Icomplete,
    405 Fido-mode, or MCT) will probably not want to use
    406 @samp{embark-live} from the minibuffer as they will then have two live
    407 updating displays of the completion candidates!
    408 
    409 A more likely use of @samp{embark-live} is to be called from a regular buffer
    410 to display a sort of live updating ``table of contents'' for the buffer.
    411 This depends on having appropriate candidate collectors configured in
    412 @samp{embark-candidate-collectors}. There are not many in Embark's default
    413 configuration, but you can try this experiment: open a dired buffer in
    414 a directory that has very many files, mark a few, and run @samp{embark-live}.
    415 You'll get an Embark Collect buffer containing only the marked files,
    416 which updates as you mark or unmark files in dired. To make
    417 @samp{embark-live} genuinely useful other candidate collectors are required.
    418 The @samp{embark-consult} package (documented near the end of this manual)
    419 contains a few: one for imenu items and one for outline headings as
    420 used by @samp{outline-minor-mode}. Those collectors really do give
    421 @samp{embark-live} a table-of-contents feel.
    422 
    423 @node Switching to a different command without losing what you've typed
    424 @section Switching to a different command without losing what you've typed
    425 
    426 Embark also has the @samp{embark-become} command which is useful for when
    427 you run a command, start typing at the minibuffer and realize you
    428 meant a different command. The most common case for me is that I run
    429 @samp{switch-to-buffer}, start typing a buffer name and realize I haven't
    430 opened the file I had in mind yet! I'll use this situation as a
    431 running example to illustrate @samp{embark-become}. When this happens I can,
    432 of course, press @samp{C-g} and then run @samp{find-file} and open the file, but
    433 this requires retyping the portion of the file name you already
    434 typed. This process can be streamlined with @samp{embark-become}: while still
    435 in the @samp{switch-to-buffer} you can run @samp{embark-become} and effectively
    436 make the @samp{switch-to-buffer} command become @samp{find-file} for this run.
    437 
    438 You can bind @samp{embark-become} to a key in @samp{minibuffer-local-map}, but it is
    439 also available as an action under the letter @samp{B} (uppercase), so you
    440 don't need a binding if you already have one for @samp{embark-act}. So,
    441 assuming I have @samp{embark-act} bound to, say, @samp{C-.}, once I realize I
    442 haven't open the file I can type @samp{C-. B C-x C-f} to have
    443 @samp{switch-to-buffer} become @samp{find-file} without losing what I have already
    444 typed in the minibuffer.
    445 
    446 But for even more convenience, @samp{embark-become} offers shorter key
    447 bindings for commands you are likely to want the current command to
    448 become. When you use @samp{embark-become} it looks for the current command in
    449 all keymaps named in the list @samp{embark-become-keymaps} and then activates
    450 all keymaps that contain it. For example, the default value of
    451 @samp{embark-become-keymaps} contains a keymap @samp{embark-become-file+buffer-map}
    452 with bindings for several commands related to files and buffers, in
    453 particular, it binds @samp{switch-to-buffer} to @samp{b} and @samp{find-file} to @samp{f}. So when
    454 I accidentally try to switch to a buffer for a file I haven't opened
    455 yet, @samp{embark-become} finds that the command I ran, @samp{switch-to-buffer}, is
    456 in the keymap @samp{embark-become-file+buffer-map}, so it activates that
    457 keymap (and any others that also contain a binding for
    458 @samp{switch-to-buffer}). The end result is that I can type @samp{C-. B f} to
    459 switch to @samp{find-file}.
    460 
    461 @node Quick start
    462 @chapter Quick start
    463 
    464 The easiest way to install Embark is from GNU ELPA, just run @samp{M-x
    465 package-install RET embark RET}. (It is also available on MELPA@.) It is
    466 highly recommended to also install @uref{https://github.com/minad/marginalia, Marginalia} (also available on GNU
    467 ELPA), so that Embark can offer you preconfigured actions in more
    468 contexts. For @samp{use-package} users, the following is a very reasonable
    469 starting configuration:
    470 
    471 @lisp
    472 (use-package marginalia
    473   :ensure t
    474   :config
    475   (marginalia-mode))
    476 
    477 (use-package embark
    478   :ensure t
    479 
    480   :bind
    481   (("C-." . embark-act)         ;; pick some comfortable binding
    482    ("C-;" . embark-dwim)        ;; good alternative: M-.
    483    ("C-h B" . embark-bindings)) ;; alternative for `describe-bindings'
    484 
    485   :init
    486 
    487   ;; Optionally replace the key help with a completing-read interface
    488   (setq prefix-help-command #'embark-prefix-help-command)
    489 
    490   ;; Show the Embark target at point via Eldoc.  You may adjust the Eldoc
    491   ;; strategy, if you want to see the documentation from multiple providers.
    492   (add-hook 'eldoc-documentation-functions #'embark-eldoc-first-target)
    493   ;; (setq eldoc-documentation-strategy #'eldoc-documentation-compose-eagerly)
    494 
    495   :config
    496 
    497   ;; Hide the mode line of the Embark live/completions buffers
    498   (add-to-list 'display-buffer-alist
    499                '("\\`\\*Embark Collect \\(Live\\|Completions\\)\\*"
    500                  nil
    501                  (window-parameters (mode-line-format . none)))))
    502 
    503 ;; Consult users will also want the embark-consult package.
    504 (use-package embark-consult
    505   :ensure t ; only need to install it, embark loads it after consult if found
    506   :hook
    507   (embark-collect-mode . consult-preview-at-point-mode))
    508 @end lisp
    509 
    510 About the suggested key bindings for @samp{embark-act} and @samp{embark-dwim}:
    511 @itemize
    512 @item
    513 Those key bindings are unlikely to work in the terminal, but
    514 terminal users are probably well aware of this and will know to
    515 select different bindings.
    516 @item
    517 The suggested @samp{C-.} binding is used by default in (at least some
    518 installations of) GNOME to input emojis, and Emacs doesn't even get
    519 a chance to respond to the binding. You can select a different key
    520 binding for @samp{embark-act} or use @samp{ibus-setup} to change the shortcut for
    521 emoji insertion (Emacs 29 will likely use @samp{C-x 8 e e}, in case you
    522 want to set the same one system-wide).
    523 @item
    524 The suggested alternative of @samp{M-.} for @samp{embark-dwim} is bound by default
    525 to @samp{xref-find-definitions}. That is a very useful command but
    526 overwriting it with @samp{embark-dwim} is sensible since in Embark's
    527 default configuration, @samp{embark-dwim} will also find the definition of
    528 the identifier at point. (Note that @samp{xref-find-definitions} with a
    529 prefix argument prompts you for an identifier, @samp{embark-dwim} does not
    530 cover this case).
    531 @end itemize
    532 
    533 Other Embark commands such as @samp{embark-act-all}, @samp{embark-become},
    534 @samp{embark-collect}, and @samp{embark-export} can be run through @samp{embark-act} as
    535 actions bound to @samp{A}, @samp{B}, @samp{S} (for ``snapshot''), and @samp{E} respectively, and
    536 thus don't really need a dedicated key binding, but feel free to bind
    537 them directly if you so wish. If you do choose to bind them directly,
    538 you'll probably want to bind them in @samp{minibuffer-local-map}, since they
    539 are most useful in the minibuffer (in fact, @samp{embark-become} only works
    540 in the minibuffer).
    541 
    542 The command @samp{embark-dwim} executes the default action at point. Another good
    543 keybinding for @samp{embark-dwim} is @samp{M-.} since @samp{embark-dwim} acts like
    544 @samp{xref-find-definitions} on the symbol at point. @samp{C-.} can be seen as a
    545 right-click context menu at point and @samp{M-.} acts like left-click. The
    546 keybindings are mnemonic, both act at the point (@samp{.}).
    547 
    548 Embark needs to know what your minibuffer completion system considers
    549 to be the list of candidates and which one is the current candidate.
    550 Embark works out of the box if you use Emacs's default tab completion,
    551 the built-in @samp{icomplete-mode} or @samp{fido-mode}, or the third-party packages
    552 @uref{https://github.com/minad/vertico, Vertico} or @uref{https://github.com/abo-abo/swiper, Ivy}.
    553 
    554 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
    555 those packages include comprehensive functionality for acting on
    556 minibuffer completion candidates. (Embark does come with Ivy
    557 integration despite this.)
    558 
    559 @node Advanced configuration
    560 @chapter Advanced configuration
    561 
    562 @menu
    563 * Showing information about available targets and actions::
    564 * Selecting commands via completions instead of key bindings::
    565 * Quitting the minibuffer after an action::
    566 * Running some setup after injecting the target::
    567 * Running hooks before, after or around an action: Running hooks before after or around an action. 
    568 * Creating your own keymaps::
    569 * Defining actions for new categories of targets::
    570 @end menu
    571 
    572 @node Showing information about available targets and actions
    573 @section Showing information about available targets and actions
    574 
    575 By default, if you run @samp{embark-act} and do not immediately select an
    576 action, after a short delay Embark will pop up a buffer called @samp{*Embark
    577 Actions*} containing a list of available actions with their key
    578 bindings. You can scroll that buffer with the mouse of with the usual
    579 commands @samp{scroll-other-window} and @samp{scroll-other-window-down} (bound by
    580 default to @samp{C-M-v} and @samp{C-M-S-v}).
    581 
    582 That functionality is provided by the @samp{embark-mixed-indicator}, but
    583 Embark has other indicators that can provide information about the
    584 target and its type, what other targets you can cycle to, and which
    585 actions have key bindings in the action map for the current type of
    586 target. Any number of indicators can be active at once and the user
    587 option @samp{embark-indicators} should be set to a list of the desired
    588 indicators.
    589 
    590 Embark comes with the following indicators:
    591 
    592 @itemize
    593 @item
    594 @samp{embark-minimal-indicator}: shows a messages in the echo area or
    595 minibuffer prompt showing the current target and the types of all
    596 targets starting with the current one; this one is on by default.
    597 
    598 @item
    599 @samp{embark-highlight-indicator}: highlights the target at point;
    600 also on by default.
    601 
    602 @item
    603 @samp{embark-verbose-indicator}: displays a table of actions and their key
    604 bindings in a buffer; this is not on by default, in favor of the
    605 mixed indicator described next.
    606 
    607 @item
    608 @samp{embark-mixed-indicator}: starts out by behaving as the minimal
    609 indicator but after a short delay acts as the verbose indicator;
    610 this is on by default.
    611 
    612 @item
    613 @samp{embark-isearch-highlight-indicator}: this only does something when
    614 the current target is the symbol at point, in which case it
    615 lazily highlights all occurrences of that symbol in the current
    616 buffer, like isearch; also on by default.
    617 @end itemize
    618 
    619 Users of the popular @uref{https://github.com/justbur/emacs-which-key, which-key} package may prefer to use the
    620 @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
    621 definition from the wiki into your configuration and customize the
    622 @samp{embark-indicators} user option to exclude the mixed and verbose
    623 indicators and to include @samp{embark-which-key-indicator}.
    624 
    625 @node Selecting commands via completions instead of key bindings
    626 @section Selecting commands via completions instead of key bindings
    627 
    628 As an alternative to reading the list of actions in the verbose or
    629 mixed indicators (see the previous section for a description of
    630 these), you can press the @samp{embark-help-key}, which is @samp{C-h} by default
    631 (but you may prefer @samp{?} to free up @samp{C-h} for use as a prefix) after
    632 running @samp{embark-act}. Pressing the help key will prompt you for the name
    633 of an action with completion (but feel free to enter a command that is
    634 not among the offered candidates!), and will also remind you of the
    635 key bindings. You can press @samp{embark-keymap-prompter-key}, which is @samp{@@} by
    636 default, at the prompt and then one of the key bindings to enter the
    637 name of the corresponding action.
    638 
    639 You may think that with the @samp{*Embark Actions*} buffer popping up to
    640 remind you of the key bindings you'd never want to use completion to
    641 select an action by name, but personally I find that typing a small
    642 portion of the action name to narrow down the list of candidates feels
    643 significantly faster than visually scanning the entire list of actions.
    644 
    645 If you find you prefer entering actions that way, you can configure
    646 embark to always prompt you for actions by setting the variable
    647 @samp{embark-prompter} to @samp{embark-completing-read-prompter}.
    648 
    649 @node Quitting the minibuffer after an action
    650 @section Quitting the minibuffer after an action
    651 
    652 By default, if you call @samp{embark-act} from the minibuffer it quits the
    653 minibuffer after performing the action. You can change this by setting
    654 the user option @samp{embark-quit-after-action} to @samp{nil}. Having @samp{embark-act} @emph{not}
    655 quit the minibuffer can be useful to turn commands into little ``thing
    656 managers''. For example, you can use @samp{find-file} as a little file manager
    657 or @samp{describe-package} as a little package manager: you can run those
    658 commands, perform a series of actions, and then quit the command.
    659 
    660 If you want to control the quitting behavior in a fine-grained manner
    661 depending on the action, you can set @samp{embark-quit-after-action} to an
    662 alist, associating commands to either @samp{t} for quitting or @samp{nil} for not
    663 quitting. When using an alist, you can use the special key @samp{t} to
    664 specify the default behavior. For example, to specify that by default
    665 actions should not quit the minibuffer but that using @samp{kill-buffer} as
    666 an action should quit, you can use the following configuration:
    667 
    668 @lisp
    669 (setq embark-quit-after-action '((kill-buffer . t) (t . nil)))
    670 @end lisp
    671 
    672 The variable @samp{embark-quit-after-action} only specifies a default, that
    673 is, it only controls whether or not @samp{embark-act} quits the minibuffer
    674 when you call it without a prefix argument, and you can select the
    675 opposite behavior to what the variable says by calling @samp{embark-act} with
    676 @samp{C-u}. Also note that both the variable @samp{embark-quit-after-action} and @samp{C-u}
    677 have no effect when you call @samp{embark-act} outside the minibuffer.
    678 
    679 If you find yourself using the quitting and non-quitting variants of
    680 @samp{embark-act} about equally often, independently of the action, you may
    681 prefer to simply have separate commands for them instead of a single
    682 command that you call with @samp{C-u} half the time. You could, for example,
    683 keep the default exiting behavior of @samp{embark-act} and define a
    684 non-quitting version as follows:
    685 
    686 @lisp
    687 (defun embark-act-noquit ()
    688   "Run action but don't quit the minibuffer afterwards."
    689   (interactive)
    690   (let ((embark-quit-after-action nil))
    691     (embark-act)))
    692 @end lisp
    693 
    694 @node Running some setup after injecting the target
    695 @section Running some setup after injecting the target
    696 
    697 You can customize what happens after the target is inserted at the
    698 minibuffer prompt of an action. There are
    699 @samp{embark-target-injection-hooks}, that are run by default after injecting
    700 the target into the minibuffer. The variable
    701 @samp{embark-target-injection-hooks} is an alist associating commands to
    702 their setup hooks. There are two special keys: if no setup hook is
    703 specified for a given action, the hook associated to @samp{t} is run; and the
    704 hook associated to @samp{:always} is run regardless of the action. (This
    705 variable used to have the less explicit name of
    706 @samp{embark-setup-action-hooks}, so please update your configuration.)
    707 
    708 For example, consider using @samp{shell-command} as an action during file
    709 completion. It would be useful to insert a space before the target
    710 file name and to leave the point at the beginning, so you can
    711 immediately type the shell command to run on that file. That's why in
    712 Embark's default configuration there is an entry in
    713 @samp{embark-target-injection-hooks} associating @samp{shell-command} to a hook that
    714 includes @samp{embark--shell-prep}, a simple helper function that quotes all
    715 the spaces in the file name, inserts an extra space at the beginning
    716 of the line and leaves point to the left of it.
    717 
    718 Now, the preparation that @samp{embark--shell-prep} does would be useless if
    719 Embark did what it normally does after it inserts the target of the
    720 action at the minibuffer prompt, which is to ``press @samp{RET}'' for you,
    721 accepting the target as is; if Embark did that for @samp{shell-command} you
    722 wouldn't get a chance to type in the command to execute! That is why
    723 in Embark's default configuration the entry for @samp{shell-command} in
    724 @samp{embark-target-injection-hooks} also contains the function
    725 @samp{embark--allow-edit}.
    726 
    727 Embark used to have a dedicated variable @samp{embark-allow-edit-actions} to
    728 which you could add commands for which Embark should forgo pressing
    729 @samp{RET} for you after inserting the target. Since its effect can also be
    730 achieved via the general @samp{embark-target-injection-hooks} mechanism, that
    731 variable has been removed to simplify Embark. Be sure to update your
    732 configuration; if you had something like:
    733 
    734 @lisp
    735 (add-to-list 'embark-allow-edit-actions 'my-command)
    736 @end lisp
    737 
    738 you should replace it with:
    739 
    740 @lisp
    741 (push 'embark--allow-edit
    742       (alist-get 'my-command embark-target-injection-hooks))
    743 @end lisp
    744 
    745 
    746 Also note that while you could abuse @samp{embark--allow-edit} so that you
    747 have to confirm ``dangerous'' actions such as @samp{delete-file}, it is better
    748 to implement confirmation by adding the @samp{embark--confirm} function to
    749 the appropriate entry of a different hook alist, namely,
    750 @samp{embark-pre-action-hooks}.
    751 
    752 Besides @samp{embark--allow-edit}, Embark comes with another function that is
    753 of general utility in action setup hooks: @samp{embark--ignore-target}. Use
    754 it for commands that do prompt you in the minibuffer but for which
    755 inserting the target would be inappropriate. This is not a common
    756 situation but does occasionally arise. For example it is used by
    757 default for @samp{shell-command-on-region}: that command is used as an action
    758 for region targets, and it prompts you for a shell command; you
    759 typically do @emph{not} want the target, that is the contents of the region,
    760 to be entered at that prompt!
    761 
    762 @node Running hooks before after or around an action
    763 @section Running hooks before, after or around an action
    764 
    765 Embark has three variables, @samp{embark-pre-action-hooks},
    766 @samp{embark-post-action-hooks} and @samp{embark-around-action-hooks}, which are
    767 alists associating commands to hooks that should run before or after
    768 or as around advice for the command when used as an action. As with
    769 @samp{embark-target-injection-hooks}, there are two special keys for the
    770 alists: @samp{t} designates the default hook to run when no specific hook is
    771 specified for a command; and the hook associated to @samp{:always} runs
    772 regardless.
    773 
    774 The default values of those variables are fairly extensive, adding
    775 creature comforts to make running actions a smooth experience. Embark
    776 comes with several functions intended to be added to these hooks, and
    777 used in the default values of @samp{embark-pre-action-hooks},
    778 @samp{embark-post-action-hooks} and @samp{embark-around-action-hooks}.
    779 
    780 For pre-action hooks:
    781 
    782 @table @asis
    783 @item @samp{embark--confirm}
    784 Prompt the user for confirmation before executing
    785 the action. This is used be default for commands deemed ``dangerous'',
    786 or, more accurately, hard to undo, such as @samp{delete-file} and
    787 @samp{kill-buffer}.
    788 
    789 @item @samp{embark--unmark-target}
    790 Unmark the active region. Use this for
    791 commands you want to act on the region contents but without the
    792 region being active. The default configuration uses this function as
    793 a pre-action hook for @samp{occur} and @samp{query-replace}, for example, so that
    794 you can use them as actions with region targets to search the whole
    795 buffer for the text contained in the region. Without this pre-action
    796 hook using @samp{occur} as an action for a region target would be
    797 pointless: it would search for the the region contents @emph{in the
    798 region}, (typically, due to the details of regexps) finding only one
    799 match!
    800 
    801 @item @samp{embark--beginning-of-target}
    802 Move to the beginning of the target
    803 (for targets that report bounds). This is used by default for
    804 backward motion commands such as @samp{backward-sexp}, so that they don't
    805 accidentally leave you on the current target.
    806 
    807 @item @samp{embark--end-of-target}
    808 Move to the end of the target. This is used
    809 similarly to the previous function, but also for commands that act
    810 on the last s-expression like @samp{eval-last-sexp}. This allow you to act
    811 on an s-expression from anywhere inside it and still use
    812 @samp{eval-last-sexp} as an action.
    813 
    814 @item @samp{embark--xref-push-markers}
    815 Push the current location on the xref
    816 marker stack. Use this for commands that take you somewhere and for
    817 which you'd like to be able to come back to where you were using
    818 @samp{xref-pop-marker-stack}. This is used by default for @samp{find-library}.
    819 @end table
    820 
    821 For post-action hooks:
    822 
    823 @table @asis
    824 @item @samp{embark--restart}
    825 Restart the command currently prompting in the
    826 minibuffer, so that the list of completion candidates is updated.
    827 This is useful as a post action hook for commands that delete or
    828 rename a completion candidate; for example the default value of
    829 @samp{embark-post-action-hooks} uses it for @samp{delete-file}, @samp{kill-buffer},
    830 @samp{rename-file}, @samp{rename-buffer}, etc.
    831 @end table
    832 
    833 For around-action hooks:
    834 
    835 @table @asis
    836 @item @samp{embark--mark-target}
    837 Save existing mark and point location, mark
    838 the target and run the action. Most targets at point outside the
    839 minibuffer report which region of the buffer they correspond to
    840 (this is the information used by @samp{embark-highlight-indicator} to
    841 know what portion of the buffer to highlight); this function marks
    842 that region. It is useful as an around action hook for commands that
    843 expect a region to be marked, for example, it is used by default for
    844 @samp{indent-region} so that it works on s-expression targets, or for
    845 @samp{fill-region} so that it works on paragraph targets.
    846 
    847 @item @samp{embark--cd}
    848 Run the action with @samp{default-directory} set to the
    849 directory associated to the current target. The target should be of
    850 type @samp{file}, @samp{buffer}, @samp{bookmark} or @samp{library}, and the associated directory
    851 is what you'd expect in each case.
    852 
    853 @item @samp{embark--narrow-to-target}
    854 Run the action with buffer narrowed to
    855 current target. Use this as an around hook to localize the effect of
    856 actions that don't already work on just the region. In the default
    857 configuration it is used for @samp{repunctuate-sentences}.
    858 
    859 @item @samp{embark--save-excursion}
    860 Run the action restoring point at the end.
    861 The current default configuration doesn't use this but it is
    862 available for users.
    863 @end table
    864 
    865 @node Creating your own keymaps
    866 @section Creating your own keymaps
    867 
    868 All internal keymaps are defined with the standard helper macro
    869 @samp{defvar-keymap}. For example a simple version of the file action keymap
    870 could be defined as follows:
    871 
    872 @lisp
    873 (defvar-keymap embark-file-map
    874   :doc "Example keymap with a few file actions"
    875   :parent embark-general-map
    876   "d" #'delete-file
    877   "r" #'rename-file
    878   "c" #'copy-file)
    879 @end lisp
    880 
    881 These action keymaps are perfectly normal Emacs
    882 keymaps.  You may want to inherit from the @samp{embark-general-map} if you
    883 want to access the default Embark actions. Note that @samp{embark-collect}
    884 and @samp{embark-export} are also made available via @samp{embark-general-map}.
    885 
    886 @node Defining actions for new categories of targets
    887 @section Defining actions for new categories of targets
    888 
    889 It is easy to configure Embark to provide actions for new types of
    890 targets, either in the minibuffer or outside it. I present below two
    891 very detailed examples of how to do this. At several points I'll
    892 explain more than one way to proceed, typically with the easiest
    893 option first. I include the alternative options since there will be
    894 similar situations where the easiest option is not available.
    895 
    896 @menu
    897 * New minibuffer target example - tab-bar tabs::
    898 * New target example in regular buffers - short Wikipedia links::
    899 @end menu
    900 
    901 @node New minibuffer target example - tab-bar tabs
    902 @subsection New minibuffer target example - tab-bar tabs
    903 
    904 As an example, take the new @uref{https://www.gnu.org/software/emacs/manual/html_node/emacs/Tab-Bars.html, tab bars} from Emacs 27. I'll explain how
    905 to configure Embark to offer tab-specific actions when you use the
    906 tab-bar-mode commands that mention tabs by name. The configuration
    907 explained here is now built-in to Embark (and Marginalia), but it's
    908 still a good self-contained example. In order to setup up tab actions
    909 you would need to: (1) make sure Embark knows those commands deal with
    910 tabs, (2) define a keymap for tab actions and configure Embark so it
    911 knows that's the keymap you want.
    912 
    913 @enumerate
    914 @item
    915 @anchor{Telling Embark about commands that prompt for tabs by name}Telling Embark about commands that prompt for tabs by name
    916 
    917 
    918 For step (1), it would be great if the @samp{tab-bar-mode} commands reported
    919 the completion category @samp{tab} when asking you for a tab with
    920 completion. (All built-in Emacs commands that prompt for file names,
    921 for example, do have metadata indicating that they want a @samp{file}.) They
    922 do not, unfortunately, and I will describe a couple of ways to deal
    923 with this.
    924 
    925 Maybe the easiest thing is to configure @uref{https://github.com/minad/marginalia, Marginalia} to enhance those
    926 commands. All of the @samp{tab-bar-*-tab-by-name} commands have the words
    927 ``tab by name'' in the minibuffer prompt, so you can use:
    928 
    929 @lisp
    930 (add-to-list 'marginalia-prompt-categories '("tab by name" . tab))
    931 @end lisp
    932 
    933 That's it! But in case you are ever in a situation where you don't
    934 already have commands that prompt for the targets you want, I'll
    935 describe how writing your own command with appropriate @samp{category}
    936 metadata looks:
    937 
    938 @lisp
    939 (defun my-select-tab-by-name (tab)
    940   (interactive
    941    (list
    942     (let ((tab-list (or (mapcar (lambda (tab) (cdr (assq 'name tab)))
    943                                 (tab-bar-tabs))
    944                         (user-error "No tabs found"))))
    945       (completing-read
    946        "Tabs: "
    947        (lambda (string predicate action)
    948          (if (eq action 'metadata)
    949              '(metadata (category . tab))
    950            (complete-with-action
    951             action tab-list string predicate)))))))
    952   (tab-bar-select-tab-by-name tab))
    953 @end lisp
    954 
    955 As you can see, the built-in support for setting the category
    956 meta-datum is not very easy to use or pretty to look at. To help with
    957 this I recommend the @samp{consult--read} function from the excellent
    958 @uref{https://github.com/minad/consult/, Consult} package. With that function we can rewrite the command as
    959 follows:
    960 
    961 @lisp
    962 (defun my-select-tab-by-name (tab)
    963   (interactive
    964    (list
    965     (let ((tab-list (or (mapcar (lambda (tab) (cdr (assq 'name tab)))
    966                                 (tab-bar-tabs))
    967                         (user-error "No tabs found"))))
    968       (consult--read tab-list
    969                      :prompt "Tabs: "
    970                      :category 'tab))))
    971   (tab-bar-select-tab-by-name tab))
    972 @end lisp
    973 
    974 Much nicer! No matter how you define the @samp{my-select-tab-by-name}
    975 command, the first approach with Marginalia and prompt detection has
    976 the following advantages: you get the @samp{tab} category for all the
    977 @samp{tab-bar-*-bar-by-name} commands at once, also, you enhance built-in
    978 commands, instead of defining new ones.
    979 
    980 @item
    981 @anchor{Defining and configuring a keymap for tab actions}Defining and configuring a keymap for tab actions
    982 
    983 
    984 Let's say we want to offer select, rename and close actions for tabs
    985 (in addition to Embark general actions, such as saving the tab name to
    986 the kill-ring, which you get for free). Then this will do:
    987 
    988 @lisp
    989 (defvar-keymap embark-tab-actions
    990   :doc "Keymap for actions for tab-bar tabs (when mentioned by name)."
    991   :parent embark-general-map
    992   "s" #'tab-bar-select-tab-by-name
    993   "r" #'tab-bar-rename-tab-by-name
    994   "k" #'tab-bar-close-tab-by-name)
    995 
    996 (add-to-list 'embark-keymap-alist '(tab . embark-tab-actions))
    997 @end lisp
    998 
    999 What if after using this for a while you feel closing the tab
   1000 without confirmation is dangerous? You have a couple of options:
   1001 
   1002 @enumerate
   1003 @item
   1004 You can keep using the @samp{tab-bar-close-tab-by-name} command, but have
   1005 Embark ask you for confirmation:
   1006 @lisp
   1007 (push #'embark--confirm
   1008       (alist-get 'tab-bar-close-tab-by-name
   1009                  embark-pre-action-hooks))
   1010 @end lisp
   1011 
   1012 @item
   1013 You can write your own command that prompts for confirmation and
   1014 use that instead of @samp{tab-bar-close-tab-by-name} in the above keymap:
   1015 @lisp
   1016 (defun my-confirm-close-tab-by-name (tab)
   1017   (interactive "sTab to close: ")
   1018   (when (y-or-n-p (format "Close tab '%s'? " tab))
   1019     (tab-bar-close-tab-by-name tab)))
   1020 @end lisp
   1021 
   1022 Notice that this is a command you can also use directly from @samp{M-x}
   1023 independently of Embark. Using it from @samp{M-x} leaves something to be
   1024 desired, though, since you don't get completion for the tab names.
   1025 You can fix this if you wish as described in the previous section.
   1026 @end enumerate
   1027 @end enumerate
   1028 
   1029 @node New target example in regular buffers - short Wikipedia links
   1030 @subsection New target example in regular buffers - short Wikipedia links
   1031 
   1032 Say you want to teach Embark to treat text of the form
   1033 @samp{wikipedia:Garry_Kasparov} in any regular buffer as a link to Wikipedia,
   1034 with actions to open the Wikipedia page in eww or an external browser
   1035 or to save the URL of the page in the kill-ring. We can take advantage
   1036 of the actions that Embark has preconfigured for URLs, so all we need
   1037 to do is teach Embark that @samp{wikipedia:Garry_Kasparov} stands for the URL
   1038 @samp{https://en.wikipedia.org/wiki/Garry_Kasparov}.
   1039 
   1040 You can be as fancy as you want with the recognized syntax. Here, to
   1041 keep the example simple, I'll assume the link matches the regexp
   1042 @samp{wikipedia:[[:alnum:]_]+}. We will write a function that looks for a
   1043 match surrounding point, and returns a dotted list of the form @samp{'(url
   1044 URL-OF-THE-PAGE START . END)} where @samp{START} and @samp{END} are the buffer
   1045 positions bounding the target, and are used by Embark to highlight it
   1046 if you have @samp{embark-highlight-indicator} included in the list
   1047 @samp{embark-indicators}. (There are a couple of other options for the return
   1048 value of a target finder: the bounding positions are optional and a
   1049 single target finder is allowed to return multiple targets; see the
   1050 documentation for @samp{embark-target-finders} for details.)
   1051 
   1052 @lisp
   1053 (defun my-short-wikipedia-link ()
   1054   "Target a link at point of the form wikipedia:Page_Name."
   1055   (save-excursion
   1056     (let* ((start (progn (skip-chars-backward "[:alnum:]_:") (point)))
   1057            (end (progn (skip-chars-forward "[:alnum:]_:") (point)))
   1058            (str (buffer-substring-no-properties start end)))
   1059       (save-match-data
   1060         (when (string-match "wikipedia:\\([[:alnum:]_]+\\)" str)
   1061           `(url
   1062             ,(format "https://en.wikipedia.org/wiki/%s"
   1063                      (match-string 1 str))
   1064             ,start . ,end))))))
   1065 
   1066 (add-to-list 'embark-target-finders 'my-short-wikipedia-link)
   1067 @end lisp
   1068 
   1069 @node How does Embark call the actions?
   1070 @chapter How does Embark call the actions?
   1071 
   1072 Embark actions are normal Emacs commands, that is, functions with an
   1073 interactive specification. In order to execute an action, Embark
   1074 calls the command with @samp{call-interactively}, so the command reads user
   1075 input exactly as if run directly by the user. For example the
   1076 command may open a minibuffer and read a string
   1077 (@samp{read-from-minibuffer}) or open a completion interface
   1078 (@samp{completing-read}). If this happens, Embark takes the target string
   1079 and inserts it automatically into the minibuffer, simulating user
   1080 input this way. After inserting the string, Embark exits the
   1081 minibuffer, submitting the input. (The immediate minibuffer exit can
   1082 be disabled for specific actions in order to allow editing the
   1083 input; this is done by adding the @samp{embark--allow-edit} function to the
   1084 appropriate entry of @samp{embark-target-injection-hooks}). Embark inserts
   1085 the target string at the first minibuffer opened by the action
   1086 command, and if the command happens to prompt the user for input
   1087 more than once, the user still interacts with the second and further
   1088 prompts in the normal fashion. Note that if a command does not
   1089 prompt the user for input in the minibuffer, Embark still allows you
   1090 to use it as an action, but of course, never inserts the target
   1091 anywhere. (There are plenty of examples in the default configuration
   1092 of commands that do not prompt the user bound to keys in the action
   1093 maps, most of the region actions, for instance.)
   1094 
   1095 This is how Embark manages to reuse normal commands as actions. The
   1096 mechanism allows you to use as Embark actions commands that were not
   1097 written with Embark in mind (and indeed almost all actions that are
   1098 bound by default in Embark's action keymaps are standard Emacs
   1099 commands). It also allows you to write new custom actions in such a
   1100 way that they are useful even without Embark.
   1101 
   1102 Staring from version 28.1, Emacs has a variable
   1103 @samp{y-or-n-p-use-read-key}, which when set to @samp{t} causes @samp{y-or-n-p} to use
   1104 @samp{read-key} instead of @samp{read-from-minibuffer}. Setting
   1105 @samp{y-or-n-p-use-read-key} to @samp{t} is recommended for Embark users because
   1106 it keeps Embark from attempting to insert the target at a @samp{y-or-n-p}
   1107 prompt, which would almost never be sensible. Also consider this as
   1108 a warning to structure your own action commands so that if they use
   1109 @samp{y-or-n-p}, they do so only after the prompting for the target.
   1110 
   1111 Here is a simple example illustrating the various ways of reading
   1112 input from the user mentioned above. Bind the following commands to
   1113 the @samp{embark-symbol-map} to be used as actions, then put the point on
   1114 some symbol and run them with @samp{embark-act}:
   1115 
   1116 @lisp
   1117 (defun example-action-command1 ()
   1118   (interactive)
   1119   (message "The input was `%s'." (read-from-minibuffer "Input: ")))
   1120 
   1121 (defun example-action-command2 (arg input1 input2)
   1122   (interactive "P\nsInput 1: \nsInput 2: ")
   1123   (message "The first input %swas `%s', and the second was `%s'."
   1124            (if arg "truly " "")
   1125            input1
   1126            input2))
   1127 
   1128 (defun example-action-command3 ()
   1129   (interactive)
   1130   (message "Your selection was `%s'."
   1131            (completing-read "Select: " '("E" "M" "B" "A" "R" "K"))))
   1132 
   1133 (defun example-action-command4 ()
   1134   (interactive)
   1135   (message "I don't prompt you for input and thus ignore the target!"))
   1136 
   1137 (keymap-set embark-symbol-map "X 1" #'example-action-command1)
   1138 (keymap-set embark-symbol-map "X 2" #'example-action-command2)
   1139 (keymap-set embark-symbol-map "X 3" #'example-action-command3)
   1140 (keymap-set embark-symbol-map "X 4" #'example-action-command4)
   1141 @end lisp
   1142 
   1143 Also note that if you are using the key bindings to call actions,
   1144 you can pass prefix arguments to actions in the normal way. For
   1145 example, you can use @samp{C-u X2} with the above demonstration actions to
   1146 make the message printed by @samp{example-action-command2} more emphatic.
   1147 This ability to pass prefix arguments to actions is useful for some
   1148 actions in the default configuration, such as
   1149 @samp{embark-shell-command-on-buffer}.
   1150 
   1151 @menu
   1152 * Non-interactive functions as actions::
   1153 @end menu
   1154 
   1155 @node Non-interactive functions as actions
   1156 @section Non-interactive functions as actions
   1157 
   1158 Alternatively, Embark does support one other type of action: a
   1159 non-interactive function of a single argument. The target is passed
   1160 as argument to the function. For example:
   1161 
   1162 @lisp
   1163 (defun example-action-function (target)
   1164   (message "The target was `%s'." target))
   1165 
   1166 (keymap-set embark-symbol-map "X 4" #'example-action-function)
   1167 @end lisp
   1168 
   1169 Note that normally binding non-interactive functions in a keymap is
   1170 useless, since when attempting to run them using the key binding you
   1171 get an error message similar to ``Wrong type argument: commandp,
   1172 example-action-function''. In general it is more flexible to write
   1173 any new Embark actions as commands, that is, as interactive
   1174 functions, because that way you can also run them directly, without
   1175 Embark. But there are a couple of reasons to use non-interactive
   1176 functions as actions:
   1177 
   1178 @enumerate
   1179 @item
   1180 You may already have the function lying around, and it is
   1181 convenient to simply reuse it.
   1182 
   1183 @item
   1184 For command actions the targets can only be simple string, with
   1185 no text properties. For certain advanced uses you may want the
   1186 action to receive a string @emph{with} some text properties, or even a
   1187 non-string target.
   1188 @end enumerate
   1189 
   1190 @node Embark Marginalia and Consult
   1191 @chapter Embark, Marginalia and Consult
   1192 
   1193 Embark cooperates well with the @uref{https://github.com/minad/marginalia, Marginalia} and @uref{https://github.com/minad/consult, Consult} packages.
   1194 Neither of those packages is a dependency of Embark, but both are
   1195 highly recommended companions to Embark, for opposite reasons:
   1196 Marginalia greatly enhances Embark's usefulness, while Embark can help
   1197 enhance Consult.
   1198 
   1199 In the remainder of this section I'll explain what exactly Marginalia
   1200 does for Embark, and what Embark can do for Consult.
   1201 
   1202 @menu
   1203 * Marginalia::
   1204 * Consult::
   1205 @end menu
   1206 
   1207 @node Marginalia
   1208 @section Marginalia
   1209 
   1210 Embark comes with actions for symbols (commands, functions, variables
   1211 with actions such as finding the definition, looking up the
   1212 documentation, evaluating, etc.) in the @samp{embark-symbol-map} keymap, and
   1213 for packages (actions like install, delete, browse url, etc.) in the
   1214 @samp{embark-package-keymap}.
   1215 
   1216 Unfortunately Embark does not automatically offers you these keymaps
   1217 when relevant, because many built-in Emacs commands don't report
   1218 accurate category metadata. For example, a command like
   1219 @samp{describe-package}, which reads a package name from the minibuffer,
   1220 does not have metadata indicating this fact.
   1221 
   1222 In an earlier Embark version, there were functions to supply this
   1223 missing metadata, but they have been moved to Marginalia, which
   1224 augments many Emacs command to report accurate category metadata.
   1225 Simply activating @samp{marginalia-mode} allows Embark to offer you the
   1226 package and symbol actions when appropriate again. Candidate
   1227 annotations in the Embark collect buffer are also provided by the
   1228 Marginalia package:
   1229 
   1230 @itemize
   1231 @item
   1232 If you install Marginalia and activate @samp{marginalia-mode}, Embark
   1233 Collect buffers will use the Marginalia annotations automatically.
   1234 
   1235 @item
   1236 If you don't install Marginalia, you will see only the annotations
   1237 that come with Emacs (such as key bindings in @samp{M-x}, or the unicode
   1238 characters in @samp{C-x 8 RET}).
   1239 @end itemize
   1240 
   1241 @node Consult
   1242 @section Consult
   1243 
   1244 The excellent Consult package provides many commands that use
   1245 minibuffer completion, via the @samp{completing-read} function; plenty of its
   1246 commands can be considered enhanced versions of built-in Emacs
   1247 commands, and some are completely new functionality. One common
   1248 enhancement provided in all commands for which it makes sense is
   1249 preview functionality, for example @samp{consult-buffer} will show you a
   1250 quick preview of a buffer before you actually switch to it.
   1251 
   1252 If you use both Consult and Embark you should install the
   1253 @samp{embark-consult} package which provides integration between the two. It
   1254 provides exporters for several Consult commands and also tweaks the
   1255 behavior of many Consult commands when used as actions with @samp{embark-act}
   1256 in subtle ways that you may not even notice, but make for a smoother
   1257 experience. You need only install it to get these benefits: Embark
   1258 will automatically load it after Consult if found.
   1259 
   1260 The @samp{embark-consult} package provides the following exporters:
   1261 
   1262 @itemize
   1263 @item
   1264 You can use @samp{embark-export} from @samp{consult-line}, @samp{consult-outline}, or
   1265 @samp{consult-mark} to obtain an @samp{occur-mode} buffer. As with the built-in
   1266 @samp{occur} command you use that buffer to jump to a match and after that,
   1267 you can then use @samp{next-error} and @samp{previous-error} to navigate to other
   1268 matches. You can also press @samp{e} to activate @samp{occur-edit-mode} and edit
   1269 the matches in place!
   1270 
   1271 @item
   1272 You can export from any of the Consult asynchronous search commands,
   1273 @samp{consult-grep}, @samp{consult-git-grep}, or @samp{consult-ripgrep} to get a
   1274 @samp{grep-mode} buffer. Here too you can use @samp{next-error} and @samp{previous-error}
   1275 to navigate among matches, and, if you install the @uref{http://github.com/mhayashi1120/Emacs-wgrep/raw/master/wgrep.el , wgrep} package,
   1276 you can use it to edit the matches in place.
   1277 @end itemize
   1278 
   1279 In both cases, pressing @samp{g} will rerun the Consult command you had
   1280 exported from and re-enter the input you had typed (which is similar
   1281 to reverting but a little more flexible). You can then proceed to
   1282 re-export if that's what you want, but you can also edit the input
   1283 changing the search terms or simply cancel if you see you are done
   1284 with that search.
   1285 
   1286 The @samp{embark-consult} also contains some candidates collectors that allow
   1287 you to run @samp{embark-live} to get a live-updating table of contents for
   1288 your buffer:
   1289 
   1290 @itemize
   1291 @item
   1292 @samp{embark-consult-outline-candidates} produces the outline headings of
   1293 the current buffer, using @samp{consult-outline}.
   1294 @item
   1295 @samp{embark-consult-imenu-candidates} produces the imenu items of
   1296 the current buffer, using @samp{consult-imenu}.
   1297 @item
   1298 @samp{embark-consult-imenu-or-outline-candidates} is a simple combination
   1299 of the two previous functions: it produces imenu items in buffers
   1300 deriving from @samp{prog-mode} and otherwise outline headings.
   1301 @end itemize
   1302 
   1303 The way to configure @samp{embark-live} (or @samp{embark-collect} and @samp{embark-export}
   1304 for that matter) to use one of these function is to add it at the end
   1305 of the @samp{embark-candidate-collectors} list. The @samp{embark-consult} package by
   1306 default adds the last one, which seems to be the most sensible
   1307 default.
   1308 
   1309 Besides those exporters and candidate collectors, the @samp{embark-consult}
   1310 package provides many subtle tweaks and small integrations between
   1311 Embark and Consult. Some examples are:
   1312 
   1313 @itemize
   1314 @item
   1315 When used as actions, the asynchronous search commands will search
   1316 only the files associated to the targets: if the targets @emph{are} files,
   1317 it searches those files; for buffers it will search either the
   1318 associated file if there is one, else all files in the buffer's
   1319 @samp{default-directory}; for bookmarks it will search the file they point
   1320 to, same for Emacs Lisp libraries. This is particularly powerful
   1321 when using @samp{embark-act-all} to act on multiple files at once, for
   1322 example you can use @samp{consult-find} to search among file @emph{names} and then
   1323 @samp{embark-act-all} and @samp{consult-grep} to search within the matching files.
   1324 
   1325 @itemize
   1326 @item
   1327 For all other target types, those that do not have a sensible
   1328 notion of associated file, a Consult search command (asynchronous
   1329 or not) will search for the text of the target but leave the
   1330 minibuffer open so you can interact with the Consult command.
   1331 @end itemize
   1332 
   1333 @item
   1334 @samp{consult-imenu} will search for the target and take you directly to
   1335 the location if it matches a unique imenu entry, otherwise it will
   1336 leave the minibuffer open so you can navigate among the matches.
   1337 @end itemize
   1338 
   1339 @node Related Packages
   1340 @chapter Related Packages
   1341 
   1342 There are several packages that offer functionality similar
   1343 to Embark's. 
   1344 
   1345 @table @asis
   1346 @item Acting on minibuffer completion candidates
   1347 The popular Ivy and
   1348 Helm packages have support for acting on the completion candidates
   1349 of commands written using their APIs, and there is an extensive
   1350 ecosystem of packages meant for Helm and for Ivy (the Ivy ones
   1351 usually have ``counsel'' in the name) providing commands and
   1352 appropriate actions.
   1353 @item Acting on things at point
   1354 The built-in @samp{context-menu-mode} provides
   1355 a mouse-driven context-sensitive configurable menu. The @samp{do-at-point}
   1356 package by Philip Kaludercic (available on GNU ELPA), on the other
   1357 hand is keyboard-driven.
   1358 @item Collecting completion candidates into a buffer
   1359 The Ivy package
   1360 has the command @samp{ivy-occur} which is similar to @samp{embark-collect}. As
   1361 with Ivy actions, @samp{ivy-occur} only works for commands written using
   1362 the Ivy API@.
   1363 @end table
   1364 
   1365 @node Resources
   1366 @chapter Resources
   1367 
   1368 If you want to learn more about how others have used Embark here are
   1369 some links to read:
   1370 
   1371 @itemize
   1372 @item
   1373 @uref{https://karthinks.com/software/fifteen-ways-to-use-embark/, Fifteen ways to use Embark}, a blog post by Karthik Chikmagalur.
   1374 @item
   1375 @uref{https://protesilaos.com/dotemacs/, Protesilaos Stavrou's dotemacs}, look for the section called
   1376 ``Extended minibuffer actions and more (embark.el and
   1377 prot-embark.el)''
   1378 @end itemize
   1379 
   1380 And some videos to watch:
   1381 
   1382 @itemize
   1383 @item
   1384 @uref{https://protesilaos.com/codelog/2021-01-09-emacs-embark-extras/, Embark and my extras} by Protesilaos Stavrou.
   1385 @item
   1386 @uref{https://youtu.be/qpoQiiinCtY, Embark -- Key features and tweaks} by Raoul Comninos on the
   1387 Emacs-Elements YouTube channel.
   1388 @item
   1389 @uref{https://youtu.be/WsxXr1ncukY, Livestreamed: Adding an Embark context action to send a stream
   1390 message} by Sacha Chua.
   1391 @item
   1392 @uref{https://youtu.be/qk2Is_sC8Lk, System Crafters Live! - The Many Uses of Embark} by David Wilson.
   1393 @item
   1394 @uref{https://youtu.be/5ffb2at2d7w, Using Emacs Episode 80 - Vertico, Marginalia, Consult and Embark} by
   1395 Mike Zamansky.
   1396 @end itemize
   1397 
   1398 @node Contributions
   1399 @chapter Contributions
   1400 
   1401 Contributions to Embark are very welcome. There is a @uref{https://github.com/oantolin/embark/issues/95, wish list} for
   1402 actions, target finders, candidate collectors and exporters. For other
   1403 ideas you have for Embark, feel free to open an issue on the @uref{https://github.com/oantolin/embark/issues, issue
   1404 tracker}. Any neat configuration tricks you find might be a good fit
   1405 for the @uref{https://github.com/oantolin/embark/wiki, wiki}.
   1406 
   1407 Code contributions are very welcome too, but since Embark is now on
   1408 GNU ELPA, copyright assignment to the FSF is required before you can
   1409 contribute code.
   1410 
   1411 @node Acknowledgments
   1412 @chapter Acknowledgments
   1413 
   1414 While I, Omar Antolín Camarena, have written most of the Embark code
   1415 and remain very stubborn about some of the design decisions, Embark
   1416 has received substantial help from a number of other people which this
   1417 document has neglected to mention for far too long. In particular,
   1418 Daniel Mendler has been absolutely invaluable, implementing several
   1419 important features, and providing a lot of useful advice.
   1420 
   1421 Code contributions:
   1422 
   1423 @itemize
   1424 @item
   1425 @uref{https://github.com/minad, Daniel Mendler}
   1426 @item
   1427 @uref{https://github.com/clemera/, Clemens Radermacher}
   1428 @item
   1429 @uref{https://codeberg.org/jao/, José Antonio Ortega Ruiz}
   1430 @item
   1431 @uref{https://github.com/iyefrat, Itai Y@. Efrat}
   1432 @item
   1433 @uref{https://github.com/a13, a13}
   1434 @item
   1435 @uref{https://github.com/jakanakaevangeli, jakanakaevangeli}
   1436 @item
   1437 @uref{https://github.com/mihakam, mihakam}
   1438 @item
   1439 @uref{https://github.com/leungbk, Brian Leung}
   1440 @item
   1441 @uref{https://github.com/karthink, Karthik Chikmagalur}
   1442 @item
   1443 @uref{https://github.com/roshanshariff, Roshan Shariff}
   1444 @item
   1445 @uref{https://github.com/condy0919, condy0919}
   1446 @item
   1447 @uref{https://github.com/DamienCassou, Damien Cassou}
   1448 @item
   1449 @uref{https://github.com/JimDBh, JimDBh}
   1450 @end itemize
   1451 
   1452 Advice and useful discussions:
   1453 
   1454 @itemize
   1455 @item
   1456 @uref{https://github.com/minad, Daniel Mendler}
   1457 @item
   1458 @uref{https://gitlab.com/protesilaos/, Protesilaos Stavrou}
   1459 @item
   1460 @uref{https://github.com/clemera/, Clemens Radermacher}
   1461 @item
   1462 @uref{https://github.com/hmelman/, Howard Melman}
   1463 @item
   1464 @uref{https://github.com/astoff, Augusto Stoffel}
   1465 @item
   1466 @uref{https://github.com/bdarcus, Bruce d'Arcus}
   1467 @item
   1468 @uref{https://github.com/jdtsmith, JD Smith}
   1469 @item
   1470 @uref{https://github.com/karthink, Karthik Chikmagalur}
   1471 @item
   1472 @uref{https://github.com/jakanakaevangeli, jakanakaevangeli}
   1473 @item
   1474 @uref{https://github.com/iyefrat, Itai Y@. Efrat}
   1475 @item
   1476 @uref{https://github.com/mohkale, Mohsin Kaleem}
   1477 @end itemize
   1478 
   1479 @bye