From 69f8f5b24254072abbb3b44e533b3b623ddca80a Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Wed, 15 Sep 2021 01:20:31 +0200 Subject: [PATCH] feat(default): narrow to multi-line region in +default/search-buffer Since multi-line inputs are useless to consult-line and swiper, I've changed +default/search-buffer to be more DWIM (do-what-I-mean). i.e. if a selection is active when invoking it, and... - The selection is multi-line: restrict the search to that region. - The selection is *not* multi-line: use the selection as the initial input. - Otherwise, search the whole buffer as normal. --- modules/config/default/autoload/search.el | 48 ++++++++++++++--------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/modules/config/default/autoload/search.el b/modules/config/default/autoload/search.el index 66ecb9758..20faf7cd3 100644 --- a/modules/config/default/autoload/search.el +++ b/modules/config/default/autoload/search.el @@ -24,25 +24,37 @@ If prefix ARG is set, prompt for a directory to search from." ;;;###autoload (defun +default/search-buffer () "Conduct a text search on the current buffer. -If a selection is active, pre-fill the prompt with it." + +If a selection is active and multi-line, perform a search restricted to that +region. + +If a selection is active and not multi-line, use the selection as the initial +input and search the whole buffer for it." (interactive) - (cond ((or (featurep! :completion helm) - (featurep! :completion ivy)) - (call-interactively - (if (region-active-p) - #'swiper-isearch-thing-at-point - #'swiper-isearch))) - ((featurep! :completion vertico) - (if (region-active-p) - (let ((start (region-beginning)) - (end (region-end))) - (deactivate-mark) - (consult-line - (replace-regexp-in-string - " " "\\\\ " - (rxt-quote-pcre - (buffer-substring-no-properties start end))))) - (call-interactively #'consult-line))))) + (let (start end multiline-p) + (save-restriction + (when (region-active-p) + (setq start (region-beginning) + end (region-end) + multiline-p (/= (line-number-at-pos start) + (line-number-at-pos end))) + (deactivate-mark) + (when multiline-p + (narrow-to-region start end))) + (cond ((or (featurep! :completion helm) + (featurep! :completion ivy)) + (call-interactively + (if (and start end (not multiline-p)) + #'swiper-isearch-thing-at-point + #'swiper-isearch))) + ((featurep! :completion vertico) + (if (and start end (not multiline-p)) + (consult-line + (replace-regexp-in-string + " " "\\\\ " + (rxt-quote-pcre + (buffer-substring-no-properties start end)))) + (call-interactively #'consult-line))))))) ;;;###autoload (defun +default/search-project (&optional arg)