Skip to content

Running some setup after injecting the target

You can customize what happens after the target is inserted at the minibuffer prompt of an action. There are ‘embark-target-injection-hooks’, that are run by default after injecting the target into the minibuffer. The variable ‘embark-target-injection-hooks’ is an alist associating commands to their setup hooks. There are two special keys: if no setup hook is specified for a given action, the hook associated to ‘t’ is run; and the hook associated to ‘:always’ is run regardless of the action. (This variable used to have the less explicit name of ‘embark-setup-action-hooks’, so please update your configuration.)

For example, consider using ‘shell-command’ as an action during file completion. It would be useful to insert a space before the target file name and to leave the point at the beginning, so you can immediately type the shell command to run on that file. That’s why in Embark’s default configuration there is an entry in ‘embark-target-injection-hooks’ associating ‘shell-command’ to a hook that includes ‘embark--shell-prep’, a simple helper function that quotes all the spaces in the file name, inserts an extra space at the beginning of the line and leaves point to the left of it.

Now, the preparation that ‘embark--shell-prep’ does would be useless if Embark did what it normally does after it inserts the target of the action at the minibuffer prompt, which is to "press ‘RET’" for you, accepting the target as is; if Embark did that for ‘shell-command’ you wouldn’t get a chance to type in the command to execute! That is why in Embark’s default configuration the entry for ‘shell-command’ in ‘embark-target-injection-hooks’ also contains the function ‘embark--allow-edit’.

Embark used to have a dedicated variable ‘embark-allow-edit-actions’ to which you could add commands for which Embark should forgo pressing ‘RET’ for you after inserting the target. Since its effect can also be achieved via the general ‘embark-target-injection-hooks’ mechanism, that variable has been removed to simplify Embark. Be sure to update your configuration; if you had something like:

emacs-lisp
(add-to-list 'embark-allow-edit-actions 'my-command)

you should replace it with:

emacs-lisp
(push 'embark--allow-edit
      (alist-get 'my-command embark-target-injection-hooks))

Also note that while you could abuse ‘embark--allow-edit’ so that you have to confirm "dangerous" actions such as ‘delete-file’, it is better to implement confirmation by adding the ‘embark--confirm’ function to the appropriate entry of a different hook alist, namely, ‘embark-pre-action-hooks’.

Besides ‘embark--allow-edit’, Embark comes with another function that is of general utility in action setup hooks: ‘embark--ignore-target’. Use it for commands that do prompt you in the minibuffer but for which inserting the target would be inappropriate. This is not a common situation but does occasionally arise. For example it is used by default for ‘shell-command-on-region’: that command is used as an action for region targets, and it prompts you for a shell command; you typically do not want the target, that is the contents of the region, to be entered at that prompt!