branch: externals/embark commit 958d158f00919e17fcaedc08a811f4ee9cf0a059 Author: Omar Antolín <omar.anto...@gmail.com> Commit: Omar Antolín <omar.anto...@gmail.com>
DWIM behavior for consult-async commands (fix #252) Now when the consult-async search commands like consult-grep are used as actions the behavior now depends on the type of the target: for files, buffers, bookmarks and libraries, the search starts in the directory associated to the target with no initial search term; for any other type of target the initial serch term will be the text of the target. --- embark-consult.el | 27 +++++++++++++++++---------- embark.el | 25 +++++++++++++++---------- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/embark-consult.el b/embark-consult.el index 5a92032cf9..d4de0e0992 100644 --- a/embark-consult.el +++ b/embark-consult.el @@ -288,26 +288,33 @@ This is intended to be used in `embark-target-injection-hooks'." (cl-pushnew #'embark-consult--unique-match (alist-get cmd embark-target-injection-hooks))) -(defun embark-consult--add-async-separator (&rest _) - "Add Consult's async separator at the beginning. -This is intended to be used in `embark-target-injection-hooks' for any action -that is a Consult async command." +(cl-defun embark-consult--prep-async (&key type target &allow-other-keys) + "Either add Consult's async separator or ignore the TARGET depending on TYPE. +If the TARGET of the given TYPE has an associated notion of +directory, we don't want to search for the text of target, but +rather just start a search in the associated directory. + +This is intended to be used in `embark-target-injection-hooks' +for any action that is a Consult async command." (let* ((style (alist-get consult-async-split-style consult-async-split-styles-alist)) (initial (plist-get style :initial)) - (separator (plist-get style :separator))) - (cond - (initial + (separator (plist-get style :separator)) + (directory (embark--associated-directory target type))) + (when directory + (delete-minibuffer-contents)) + (when initial (goto-char (minibuffer-prompt-end)) (insert initial) (goto-char (point-max))) - (separator + (when (and separator (null directory)) (goto-char (point-max)) - (insert separator))))) + (insert separator)))) (map-keymap (lambda (_key cmd) - (cl-pushnew #'embark-consult--add-async-separator + (cl-pushnew #'embark--cd (alist-get cmd embark-pre-action-hooks)) + (cl-pushnew #'embark-consult--prep-async (alist-get cmd embark-target-injection-hooks))) embark-consult-async-search-map) diff --git a/embark.el b/embark.el index 1d6bf419f4..4cc4f55222 100644 --- a/embark.el +++ b/embark.el @@ -3673,22 +3673,27 @@ The advice is self-removing so it only affects ACTION once." (unless (y-or-n-p (format "Run %s on %s? " action target)) (user-error "Cancelled"))) +(defun embark--associated-directory (target type) + "Return directory associated to TARGET of given TYPE. +The supported values of TYPE are file, buffer, bookmark and +library, which have an obvious notion of associated directory." + (pcase type + ('file + (file-name-directory target)) + ('buffer + (buffer-local-value 'default-directory (get-buffer target))) + ('bookmark + (file-name-directory (bookmark-location target))) + ('library + (file-name-directory (locate-library target))))) + (autoload 'bookmark-location "bookmark") (cl-defun embark--cd (&key action target type &allow-other-keys) "Run ACTION with `default-directory' set to the directory of TARGET. The supported values of TYPE are file, buffer, bookmark and library, which have an obvious notion of associated directory." (when-let (((symbolp action)) - (directory - (pcase type - ('file - (file-name-directory target)) - ('buffer - (buffer-local-value 'default-directory (get-buffer target))) - ('bookmark - (file-name-directory (bookmark-location target))) - ('library - (file-name-directory (locate-library target)))))) + (directory (embark--associated-directory target type))) (cl-labels ((in-directory (fn &rest args) (advice-remove action #'in-directory) (let ((default-directory directory))