I'm working on a plugin that allows avy to act from a distance and immediately return to its starting position, which mostly works. However, there is a problem in that an action, such as the deletion of a word, often leaves a superfluous whitespace, and sometimes an action deletes a whitespace it should have left. I have a solution for this: when avy acts on a position at a distance, I'd like to save this position as a variable, and after avy returned to its starting point I would like to set a transient keymap that allows for certain actions, like the insertion of a whitespace, to the part of the buffer that was acted on, and otherwise continues with point where it was. So, in code, something like this:
(defun avy-act-on-position (cmd func size)
"This function asks for a command and an avy function as chosen
using the avy-function-map, then uses the chosen avy function to
choose a position, and then apply the input command to it. Input
commands are chosen through typing a key combination that is either in
the currently active maps or in the avy-position-command-map, which
overrides the others. Afterwards, spaces at the position that was acted
on can be added or removed through a command in avy-post-action-map.
Through this method, simple editing of areas before or after point
can be done without having to move point."
(interactive "kCommand: \nkAvy function: \nkSelection size: ")
(call-interactively (lookup-key avy-function-map func))
(call-interactively (lookup-key avy-position-selection-map size))
(call-interactively (lookup-key (make-composed-keymap avy-selection-command-map (make-composed-keymap (current-active-maps t))) cmd))
(let ((pos (point)))
(call-interactively #'avy-pop-mark)
(let ((pos2 (point)))
(set-transient-map avy-post-action-map)
(goto-char (point))))
)
(defvar-keymap avy-post-action-map
:doc "Map of actions that can be taken post avy action commands like avy-act-by-same-function and avy-act-on-position."
"," (progn (goto-char pos)
(call-interactively #'delete-char)))
The problem is that when I evaluate this keymap, Emacs returns an error because pos is undefined (as it would be set as a local variable in avy-act-on-position). Furthermore, I need all commands in avy-post-action-map to return to the position which was acted on, so I'd like all of them to be precomposed with something like (goto-char pos). Is there some way I can do this, for instance by adding a hook that executes goto-char to a command that is called from avy-post-action-map?