Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to not open first found file automatically #21

Closed
cpixl opened this issue Sep 26, 2015 · 10 comments
Closed

Option to not open first found file automatically #21

cpixl opened this issue Sep 26, 2015 · 10 comments

Comments

@cpixl
Copy link
Contributor

cpixl commented Sep 26, 2015

As asked in #13, I'm filling a new issue for this topic.
Maybe it would be better to have on option to not open the file when searching (and maybe it could be the default), as the user may be only looking for the existence of some search term on the project, and don't intend to lose focus from the current code (yeah, it's possible to go back to where it was by pressing <c-o>, but sometimes it's inconvenient to use it everytime when using ferret).

@sergei-dyshel
Copy link

Makes sense. Had to move to another plugin just because of this option.

@cpixl
Copy link
Contributor Author

cpixl commented Mar 16, 2016

Recently I switched to CtrlSF. It gives me more options and Edit Mode (which is more user-friendly in most of the cases).

@wincent
Copy link
Owner

wincent commented Mar 16, 2016

Wow, CtrlSF is pretty impressive.

I'm going to close this for now as it's not a pain point that I feel. If anybody wants to fix it, I think these are the options:

  • Use dispatch.vim, which doesn't jump to the first match by default (this is what I do).
  • If you don't want to use dispatch.vim, you could patch the cexpr call in Ferret to be cgetexpr instead (make it a configuration option if you want), and then use g:FerretQFHandler to implement any custom behavior that you want (like copen | wincmd p or whatever).

I'd be open to pull requests.

@wincent wincent closed this as completed Mar 16, 2016
@ntnn
Copy link
Contributor

ntnn commented Mar 16, 2016

I don't remember the code well, but I guess you set 'grepprg' and 'grepformat' - if so you'd just have to put an exclamation mark after the final grep.

@wincent
Copy link
Owner

wincent commented Mar 16, 2016

Yeah @ntnn, something like that. I quickly hacked a sample up in 47ca7cd to show one way of doing it (too flakey to actually merge). We're using cexpr under the covers but switching to :grep would indeed enable a pretty straightforward switch to :grep!.

wincent added a commit that referenced this issue Mar 16, 2016
There is a little bit of fragility here because the user can override
the handler behavior with `g:FerretQFHandler` (for example, my use of
`wincmd p` here assumes you've switched focus with `copen` rather than
`cwindow`, but short of removing that functionality I am not sure how
one would avoid this). As it is, the `wincmd` can do the wrong thing, so
this feature is decidedly fragile. eg:

- run a search, and leaving focus in the quickfix listing
- let g:FerretDispatch=0
- let g:FerretAutojump=0
- our wincmd here rotates us out of the quickfix window

Related #21
@sergei-dyshel
Copy link

Thanks for quick response!

@kriansa
Copy link

kriansa commented Jun 13, 2017

Hello @wincent,

Sorry to bump this closed issue, but is there something we could do to help to get 47ca7cd merged? Sometimes it's annoying to close the current buffer every time I do an :Ack and the result is not the first one listed.

Thanks for this great project.

@wincent
Copy link
Owner

wincent commented Jun 13, 2017

@kriansa: yeah, I think we can merge something like that (will need to redo it it no longer applies cleanly). Lots of edge cases to deal with, but it will have to be something like this:

diff --git a/autoload/ferret/private/shared.vim b/autoload/ferret/private/shared.vim
index 14ad5a1..ff1d929 100644
--- a/autoload/ferret/private/shared.vim
+++ b/autoload/ferret/private/shared.vim
@@ -1,19 +1,43 @@
 " Copyright 2015-present Greg Hurrell. All rights reserved.
 " Licensed under the terms of the BSD 2-clause license.
 
+" Convenience function to get/sanitize the current g:FerretAutojump setting.
+function! s:autojump()
+  let l:autojump=get(g:, 'FerretAutojump', 1)
+  if l:autojump != 0 && l:autojump != 1 && l:autojump != 2
+    let l:autojump=0
+  endif
+  return l:autojump
+endfunction
+
 function! ferret#private#shared#finalize_search(output, ack)
   let l:original_errorformat=&errorformat
+  let l:autojump=s:autojump()
+  if a:ack
+    let l:prefix='c' " Will use cexpr, cgetexpr.
+    let l:handler=get(g:, 'FerretQFHandler', 'botright copen')
+    let l:post='qf'
+  else
+    let l:prefix='l' " Will use lexpr, lgetexpr.
+    let l:handler=get(g:, 'FerretLLHandler', 'lopen')
+    let l:post='location'
+  endif
   try
     let &errorformat=g:FerretFormat
-    if a:ack
-      call s:swallow('cexpr a:1', a:output)
-      execute get(g:, 'FerretQFHandler', 'botright cwindow')
-      call ferret#private#post('qf')
+    if l:autojump == 2 " Show listing and jump to first result.
+      call s:swallow(l:prefix . 'expr a:1', a:output)
     else
-      call s:swallow('lexpr a:1', a:output)
-      execute get(g:, 'FerretLLHandler', 'lwindow')
-      call ferret#private#post('location')
+      call s:swallow(l:prefix . 'getexpr a:1', a:output)
     endif
+    let l:before=winnr()
+    execute l:handler
+    if l:autojump != 1 " Show listing, but don't jump anywhere.
+      let l:after=winnr()
+      if l:before != l:after
+        execute l:before . 'wincmd w'
+      end
+    endif
+    call ferret#private#post(l:post)
   finally
     let &errorformat=l:original_errorformat
   endtry

wincent added a commit that referenced this issue Jun 13, 2017
This is a fairly invasive change, so I'll keep my finger poised above
the revert button. For more context, see:

#21

Notable changes since I last explored this:

- Using `winnr()` to avoid blindly relying on `wincmd p`.
- Switched to `:copen` (from `:cwindow`) to get consistent focusing
  behavior.
- Slight change of `post()` callback ordering so that we can decide
  whether or not to open the listing at all; this allows us to avoid
  showing an empty listing with `:copen`.
@wincent
Copy link
Owner

wincent commented Jun 13, 2017

Ok, so I pushed something to the "next" branch. Will let it bake there for a while before merging to master.

@kriansa
Copy link

kriansa commented Jun 15, 2017

Thank you for your quick response!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants