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 ;;;###autoload
(defun +default/search-buffer () (defun +default/search-buffer ()
"Conduct a text search on the current 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) (interactive)
(cond ((or (featurep! :completion helm) (let (start end multiline-p)
(featurep! :completion ivy)) (save-restriction
(call-interactively (when (region-active-p)
(if (region-active-p) (setq start (region-beginning)
#'swiper-isearch-thing-at-point end (region-end)
#'swiper-isearch))) multiline-p (/= (line-number-at-pos start)
((featurep! :completion vertico) (line-number-at-pos end)))
(if (region-active-p) (deactivate-mark)
(let ((start (region-beginning)) (when multiline-p
(end (region-end))) (narrow-to-region start end)))
(deactivate-mark) (cond ((or (featurep! :completion helm)
(consult-line (featurep! :completion ivy))
(replace-regexp-in-string (call-interactively
" " "\\\\ " (if (and start end (not multiline-p))
(rxt-quote-pcre #'swiper-isearch-thing-at-point
(buffer-substring-no-properties start end))))) #'swiper-isearch)))
(call-interactively #'consult-line))))) ((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 ;;;###autoload
(defun +default/search-project (&optional arg) (defun +default/search-project (&optional arg)