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.
This commit is contained in:
Henrik Lissner 2021-09-15 01:20:31 +02:00
parent 35f701a999
commit 69f8f5b242

View file

@ -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)
(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 (region-active-p)
(if (and start end (not multiline-p))
#'swiper-isearch-thing-at-point
#'swiper-isearch)))
((featurep! :completion vertico)
(if (region-active-p)
(let ((start (region-beginning))
(end (region-end)))
(deactivate-mark)
(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)))))
(buffer-substring-no-properties start end))))
(call-interactively #'consult-line)))))))
;;;###autoload
(defun +default/search-project (&optional arg)