3

I'd like to start swiper with a filter pre-set on the search -- as if I'd entered a search expression and then used S-SPC (ivy-restrict-to-matches) to clear the query space to narrow the results down further.

I can use

(swiper "something to search on here")

but I still see something to search on here in the minibuffer. How can I get that to go away programmatically?


My use-case is a simple way to navigate functions defined in a file. I defined a regexp that matches the beginning of functions up to the function name -- I'd like to match on this name.

Sean Allred
  • 6,861
  • 16
  • 85
  • I failed to understand the question. What is the application you have in mind? By the way, by default, `ivy-restrict-to-matches` is bound to `S-SPC`. – Kaushal Modi Feb 22 '16 at 21:26
  • @KaushalModi Whoops, fixed. Thanks. My use-case is a simple way to navigate functions defined in a file. I defined a regexp that matches the beginning of functions up to the function name -- I'd like to match on this name. – Sean Allred Feb 22 '16 at 21:42

1 Answers1

3

It's a bit tricky, since you can't call anything after read-from-minibuffer (called by ivy-read called by swiper) until it returns. However, there's a visible function swiper--update-input-ivy that's called in post-command-hook. You can advice this function. It could also be possible to advice ivy--exhibit (it's always in the minibuffer's post-command-hook), but swiper--update-input-ivy will have less side-effects.

(defun swiper-ivy-restrict-to-matches-once (&rest r)
  (ivy-restrict-to-matches)
  (advice-remove
   'swiper--update-input-ivy
   'swiper-ivy-restrict-to-matches-once))

(defun swiper-for-defun ()
  (interactive)
  (advice-add
   'swiper--update-input-ivy
   :after 'swiper-ivy-restrict-to-matches-once)
  (swiper "defun"))

New solution due to an update in API

(defun swiper-for-defun ()
  (interactive)
  (swiper--ivy
   (cl-remove-if-not
    (lambda (x)
      (string-match "^ (defun" x))
    (swiper--candidates))))
abo-abo
  • 13,943
  • 1
  • 29
  • 43
  • Any inhibitions about adding this behavior as an optional argument to `swiper`? – Sean Allred Feb 22 '16 at 22:43
  • It could be done. In the source, it would look much more elegant - just a `cl-remove-if-not` over `swiper--candidates`. But why? What's the use case? – abo-abo Feb 23 '16 at 07:11
  • I describe my use case above – that's why I ask. Since this doesn't exist, I'm not convinced I'm taking the right approach. – Sean Allred Feb 23 '16 at 09:26
  • That's not a full use case. Surely, you don't `M-:` the piece of code you posted. There's probably some defun and some binding and something you're trying to automate. – abo-abo Feb 23 '16 at 09:31
  • I've edited my comment into the original question. Is that still not a use-case? – Sean Allred Feb 23 '16 at 09:33
  • 1
    Now it is:) I would recommend to use `semantic` or `imenu` to navigate tags, but it's your choice. I've updated `swiper--ivy` to suit you more. – abo-abo Feb 23 '16 at 09:42
  • Believe it or not, today I learned about `imenu` (and `counsel-imenu`) :-) Still, +1 to the API change. I think it will prove useful in the future. Thanks for your help! – Sean Allred Feb 23 '16 at 10:15