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