Skip to content

Commit

Permalink
When the default action is embark--command, run it on original target
Browse files Browse the repository at this point in the history
This fixes #180.

Background: the default action is given, in order of priority, by:
(a): embark-default-action-overrides, (b) embark--command, (c) the
binding of RET in the relevant action keymap.

This change means that when the default action is given by
embark--command, then it recieves as argument the untransformed
target. This is meant for commands that prompt in the minibuffer for
candidates that are somehow "mangled", like several Consult commands
which prefix their candidates with unicode tofu. The tofu is necessary
for that command but should be removed for other actions.
  • Loading branch information
oantolin committed Mar 8, 2021
1 parent 4bb320a commit 0abac69
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions embark.el
Original file line number Diff line number Diff line change
Expand Up @@ -812,15 +812,19 @@ returns a non-nil result. Each function should either a pair of
a type symbol and a target string, or nil.
The initial type is then looked up as a key in the variable
`embark-transformer-alist'. If there is a transformer for
the type, it is called with the initial target, and must return a
`cons' of the transformed type and target."
`embark-transformer-alist'. If there is a transformer for the
type, it is called with the initial target, and must return a
`cons' of the transformed type and target.
The return value is 3-element list of the possibly transformed
type, the possibly transformed target and the original target."
(pcase-let* ((`(,type . ,target)
(run-hook-with-args-until-success 'embark-target-finders))
(transformer (alist-get type embark-transformer-alist)))
(if transformer
(funcall transformer target)
(cons type target))))
(pcase-let ((`(,new-type . ,new-target) (funcall transformer target)))
(list new-type new-target target))
(list type target target))))

(defun embark--default-action (type)
"Return default action for the given TYPE of target.
Expand Down Expand Up @@ -857,16 +861,22 @@ whether calling `embark-act' with nil ARG quits the minibuffer,
and if ARG is non-nil it will do the opposite. Interactively,
ARG is the prefix argument."
(interactive "P")
(pcase-let* ((`(,type . ,target) (embark--target)))
(pcase-let* ((`(,type ,target ,original) (embark--target)))
(if (and (null type) (null target))
(user-error "No target found")
(if-let ((action (embark--with-indicator embark-action-indicator
embark-prompter
(embark--action-keymap type)
target)))
(embark--act action target
(if embark-quit-after-action (not arg) arg))
(user-error "Canceled")))))
(let ((action (embark--with-indicator embark-action-indicator
embark-prompter
(embark--action-keymap type)
target))
(default-action (embark--default-action type)))
(if action
(embark--act action
(if (and (eq action default-action)
(eq action embark--command))
original
target)
(if embark-quit-after-action (not arg) arg))
(user-error "Canceled"))))))

;;;###autoload
(defun embark-default-action ()
Expand All @@ -883,9 +893,12 @@ type is not listed in `embark-default-action-overrides', the
default action is given by whatever binding RET has in the action
keymap for the target's type."
(interactive)
(pcase-let ((`(,type . ,target) (embark--target)))
(if (or type target)
(embark--act (embark--default-action type) target)
(pcase-let* ((`(,type ,target ,original) (embark--target))
(default-action (embark--default-action type)))
(if original
(embark--act default-action (if (eq default-action embark--command)
original
target))
(user-error "No target found"))))

(defun embark--become-keymap ()
Expand Down Expand Up @@ -1178,7 +1191,7 @@ Returns the name of the command."
(embark--command-name action)))))
(fset name (lambda ()
(interactive)
(embark--act action (cdr (embark--target)))))
(embark--act action (cadr (embark--target)))))
(put name 'function-documentation (documentation action))
name))

Expand Down

0 comments on commit 0abac69

Please sign in to comment.