Connecting consult-line with isearch history
I've recently jumped on the not-so-new hotness of Consult, with its friends Vertico and Orderless, replacing the still strong but slightly slower Counsel, Ivy, Swiper and Flx. There's been a few bumps along the way to adapt these new packages to my own whims, but its all working out so far.
One such issue is that after using consult-line to search in a buffer, I'd often like to continue my search, preferring the evil-mode keys n and N (evil-search-next and evil-search-previous, respectively). Unfortunately, once consult-line is closed, it can't be resumed without starting it again anew, unlike the swiper behavior I've come to rely on.
Now I did come across a 2021 GitHub thread about this issue, but the chosen solution only works for evil-search as the search module. This is not the default and, though I'm sure many love it, I prefer the isearch way of highlighting. So, taking inspiration from that thread, I've made my own advice for consult-line which instead works by connecting consult's consult--line-history with the regexp-search-ring that isearch uses.
(defun consult-line-isearch-history (&rest _)
"Add latest `consult-line' search pattern to the isearch history.
This allows n and N to continue the search after `consult-line' exits."
(when (and (bound-and-true-p evil-mode)
(eq evil-search-module 'isearch)
consult--line-history)
(let* ((pattern (car consult--line-history))
(regexp (if (string-prefix-p "\\_" pattern)
(substring pattern 2)
pattern)))
(add-to-history 'regexp-search-ring regexp)
(setq evil-ex-search-pattern (evil-ex-pattern regexp t nil nil))
(setq evil-ex-search-direction 'forward))))
;; Now tell consult-line to run the function after a search
(advice-add #'consult-line :after #'consult-line-isearch-history)
Check out my full Emacs configuration if you'd like to see how else I bend Emacs to my will.