dotemacs

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

embark.info (71429B)


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