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.

2
  • 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. Feb 22, 2016 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. Feb 22, 2016 at 21:42

1 Answer 1

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))))
7
  • Any inhibitions about adding this behavior as an optional argument to swiper? Feb 22, 2016 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, 2016 at 7: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. Feb 23, 2016 at 9: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, 2016 at 9:31
  • 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, 2016 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.