1

I would like to write a function that starts a search (evil-search-forward specifically), with some arbitrary text already in the minibuffer.

Following directions in this link I came up with the following:

  (defun my/insert-headline-string ()
    (insert "foobar"))

  (minibuffer-with-setup-hook
      (:append 'my/insert-headline-string)
    (call-interactively #'evil-search-forward))

But it doesn't work for me: evil-search-forward runs just fine, but there is no text in the mini-buffer. If I use a different function (eg find-file) it prefills mini-buffer as expected, like so:

  (minibuffer-with-setup-hook
      (:append 'my/insert-headline-string)
    (call-interactively #'find-file))

Is there a reason this won't work for the search minibuffer?

Drew
  • 75,699
  • 9
  • 109
  • 225
randouser
  • 91
  • 7
  • 1
    I don't use Evil, but did you try just using `insert` instead of `:append`? (Dunno what `:append` does.) And does `evil-search-forward` actually use the minibuffer? Isearch does not, for example, even though it looks like it does. – Drew Jul 09 '22 at 15:42
  • Thanks for the tip! Looking into `evil-search-forward`, it's basically a wrapper for isearch, which explains why editting the minibuffer doesn't do what I want. Would there be a way to prefill the isearch minibuffer I wonder? (Also, played with `insert` instead of `:append` but could not get that to work). – randouser Jul 10 '22 at 01:44
  • 1
    You ask how to *"prefill the isearch minibuffer"*. There is no Isearch minibuffer - that's what I said. But you can predefine ("prefill") the initial search string: `(defun the () (setq isearch-string "the")) (defun find-the ()(interactive) (add-hook 'isearch-mode-hook 'the) (isearch-mode t))` – Drew Jul 10 '22 at 04:11
  • That did it, thanks! I just added a `remove-hook` after the search to clean things up and it's just what I wanted. I can post the final code I'm using as an answer? Or do you want to? I don't know SE etiquette. – randouser Jul 12 '22 at 09:47
  • Sure. Please post your answer. (And you can accept your own answer.) Thx. – Drew Jul 12 '22 at 15:34

1 Answers1

0

Following @Drew 's suggestions, I managed to achieve this with the following code:


  (defun my/set-isearch-string ()
    (setq isearch-string "^\*+[[:space:]].*"))

  (defun my/search-headlines-only ()
    (interactive)
    (add-hook 'isearch-mode-hook 'my/set-isearch-string)
    (evil-search-forward) 
    (remove-hook 'isearch-mode-hook 'my/set-isearch-string))

randouser
  • 91
  • 7