From 9b884163c3ca5779346bad81061035f9d189afe6 Mon Sep 17 00:00:00 2001 From: Ches Martin Date: Sat, 7 Feb 2015 04:51:33 +0700 Subject: [PATCH 1/5] Don't clobber global 'grepprg' value 'grepprg' is a buffer-local setting. We should only use the `&l` version, or else when we restore it after an `:Ag` command, we're disrupting the setting in other buffers. It's surprising that 'grepprg' is local and global, and 'grepformat' is only global, so we have no choice but to clobber it. But this could have it's uses: suppose you have a C program with some tests in shell (like ag!), and you always use `ag` for 'grepprg'. The same 'grepformat' can work project-wide, but you might have an autocommand that makes 'grepprg' use `ag --shell` for all files under the tests directory. --- autoload/ag.vim | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/autoload/ag.vim b/autoload/ag.vim index a5139d4f..2cee9be8 100644 --- a/autoload/ag.vim +++ b/autoload/ag.vim @@ -84,21 +84,22 @@ function! ag#Ag(cmd, args) let g:ag_format="%f:%l:%c:%m" endif - let l:grepprg_bak=&grepprg - let l:grepformat_bak=&grepformat - let l:t_ti_bak=&t_ti - let l:t_te_bak=&t_te + let l:grepprg_bak = &l:grepprg + let l:grepformat_bak = &grepformat + let l:t_ti_bak = &t_ti + let l:t_te_bak = &t_te + try - let &grepprg=g:ag_prg - let &grepformat=g:ag_format + let &l:grepprg = g:ag_prg + let &grepformat = g:ag_format set t_ti= set t_te= silent! execute a:cmd . " " . escape(l:grepargs, '|') finally - let &grepprg=l:grepprg_bak - let &grepformat=l:grepformat_bak - let &t_ti=l:t_ti_bak - let &t_te=l:t_te_bak + let &l:grepprg = l:grepprg_bak + let &grepformat = l:grepformat_bak + let &t_ti = l:t_ti_bak + let &t_te = l:t_te_bak endtry if a:cmd =~# '^l' From 586353a81c1eac8bfe56147906b763b8f7a3414e Mon Sep 17 00:00:00 2001 From: Ches Martin Date: Sat, 7 Feb 2015 16:14:52 +0700 Subject: [PATCH 2/5] Add autoload guard, factor out an expressive var --- autoload/ag.vim | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/autoload/ag.vim b/autoload/ag.vim index 2cee9be8..dc5ee0b7 100644 --- a/autoload/ag.vim +++ b/autoload/ag.vim @@ -1,5 +1,10 @@ " NOTE: You must, of course, install ag / the_silver_searcher +if exists('g:autoloaded_ag') + finish +endif +let g:autoloaded_ag = 1 + " FIXME: Delete deprecated options below on or after 15-7 (6 months from when they were changed) {{{ if exists("g:agprg") @@ -61,6 +66,12 @@ endfunction function! ag#Ag(cmd, args) let l:ag_executable = get(split(g:ag_prg, " "), 0) + if a:cmd =~# '^l' + let l:using_loclist = 1 + else + let l:using_loclist = 0 + endif + " Ensure that `ag` is installed if !executable(l:ag_executable) echoe "Ag command '" . l:ag_executable . "' was not found. Is the silver searcher installed and on your $PATH?" @@ -102,13 +113,13 @@ function! ag#Ag(cmd, args) let &t_te = l:t_te_bak endtry - if a:cmd =~# '^l' + if l:using_loclist let l:match_count = len(getloclist(winnr())) else let l:match_count = len(getqflist()) endif - if a:cmd =~# '^l' && l:match_count + if l:using_loclist && l:match_count exe g:ag_lhandler let l:apply_mappings = g:ag_apply_lmappings let l:matches_window_prefix = 'l' " we're using the location list From dd068d1437ed9c4fe57c8d75a5a2d7a336d4616f Mon Sep 17 00:00:00 2001 From: Ches Martin Date: Sat, 7 Feb 2015 18:42:32 +0700 Subject: [PATCH 3/5] Fix buffer locality of mappings :scream: `` *must* come first. This was leaving the mappings set in all normal quickfix windows, which is not our place to impose on users. --- autoload/ag.vim | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/autoload/ag.vim b/autoload/ag.vim index dc5ee0b7..7c2a2ed9 100644 --- a/autoload/ag.vim +++ b/autoload/ag.vim @@ -139,18 +139,18 @@ function! ag#Ag(cmd, args) if l:match_count if l:apply_mappings - nnoremap h K - nnoremap H Kb - nnoremap o - nnoremap t T - nnoremap T TgT - nnoremap v HbJt - - exe 'nnoremap e :' . l:matches_window_prefix .'close' - exe 'nnoremap go :' . l:matches_window_prefix . 'open' - exe 'nnoremap q :' . l:matches_window_prefix . 'close' - - exe 'nnoremap gv :let b:height=winheight(0)H:' . l:matches_window_prefix . 'openJ:exe printf(":normal %d\c-w>_", b:height)' + nnoremap h K + nnoremap H Kb + nnoremap o + nnoremap t T + nnoremap T TgT + nnoremap v HbJt + + exe 'nnoremap e :' . l:matches_window_prefix .'close' + exe 'nnoremap go :' . l:matches_window_prefix . 'open' + exe 'nnoremap q :' . l:matches_window_prefix . 'close' + + exe 'nnoremap gv :let b:height=winheight(0)H:' . l:matches_window_prefix . 'openJ:exe printf(":normal %d\c-w>_", b:height)' " Interpretation: " :let b:height=winheight(0) Get the height of the quickfix/location list window " Open the current item in a new split From 986cbbcc61b4ae4fa1318c6abd8b6ed4c06a65e0 Mon Sep 17 00:00:00 2001 From: Ches Martin Date: Sun, 8 Feb 2015 01:15:29 +0700 Subject: [PATCH 4/5] Refactor the big ugly gv mapping to a function Looks much better as code than a one-line string, doesn't it? --- autoload/ag.vim | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/autoload/ag.vim b/autoload/ag.vim index 7c2a2ed9..e764fea4 100644 --- a/autoload/ag.vim +++ b/autoload/ag.vim @@ -146,18 +146,14 @@ function! ag#Ag(cmd, args) nnoremap T TgT nnoremap v HbJt - exe 'nnoremap e :' . l:matches_window_prefix .'close' - exe 'nnoremap go :' . l:matches_window_prefix . 'open' - exe 'nnoremap q :' . l:matches_window_prefix . 'close' - - exe 'nnoremap gv :let b:height=winheight(0)H:' . l:matches_window_prefix . 'openJ:exe printf(":normal %d\c-w>_", b:height)' - " Interpretation: - " :let b:height=winheight(0) Get the height of the quickfix/location list window - " Open the current item in a new split - " H Slam the newly opened window against the left edge - " :copen -or- :lopen Open either the quickfix window or the location list (whichever we were using) - " J Slam the quickfix/location list window against the bottom edge - " :exe printf(":normal %d\c-w>_", b:height) Restore the quickfix/location list window's height from before we opened the match + let l:closecmd = l:matches_window_prefix . 'close' + let l:opencmd = l:matches_window_prefix . 'open' + + exe 'nnoremap e ' . l:closecmd . '' + exe 'nnoremap go :' . l:opencmd . '' + exe 'nnoremap q ' . l:closecmd . '' + + exe 'nnoremap gv :call PreviewVertical("' . l:opencmd . '")' if g:ag_mapping_message && l:apply_mappings echom "ag.vim keys: q=quit /e/t/h/v=enter/edit/tab/split/vsplit go/T/H/gv=preview versions of same" @@ -175,7 +171,16 @@ function! ag#AgFromSearch(cmd, args) call ag#Ag(a:cmd, '"' . search .'" '. a:args) endfunction -function! ag#GetDocLocations() +function! ag#AgHelp(cmd,args) + let args = a:args.' '.s:GetDocLocations() + call ag#Ag(a:cmd,args) +endfunction + +"----------------------------------------------------------------------------- +" Private API +"----------------------------------------------------------------------------- + +function! s:GetDocLocations() let dp = '' for p in split(&runtimepath,',') let p = p.'/doc/' @@ -186,7 +191,13 @@ function! ag#GetDocLocations() return dp endfunction -function! ag#AgHelp(cmd,args) - let args = a:args.' '.ag#GetDocLocations() - call ag#Ag(a:cmd,args) +" Called from within a list window, preserves its height after shuffling vsplit. +" The parameter indicates whether list was opened as copen or lopen. +function! s:PreviewVertical(opencmd) + let b:height = winheight(0) " Get the height of list window + exec "normal! \\" | " Open current item in a new split + wincmd H " Slam newly opened window against the left edge + exec a:opencmd | " Move back to the list window + wincmd J " Slam the list window against the bottom edge + exec 'resize' b:height | " Restore the list window's height endfunction From 5bae241f87f6d1f4dd94988f5da038f7fd3b0a60 Mon Sep 17 00:00:00 2001 From: Ches Martin Date: Fri, 13 Feb 2015 22:03:15 +0700 Subject: [PATCH 5/5] fixup! Refactor the big ugly gv mapping to a function --- autoload/ag.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/autoload/ag.vim b/autoload/ag.vim index e764fea4..bd46ed60 100644 --- a/autoload/ag.vim +++ b/autoload/ag.vim @@ -149,9 +149,9 @@ function! ag#Ag(cmd, args) let l:closecmd = l:matches_window_prefix . 'close' let l:opencmd = l:matches_window_prefix . 'open' - exe 'nnoremap e ' . l:closecmd . '' + exe 'nnoremap e :' . l:closecmd . '' exe 'nnoremap go :' . l:opencmd . '' - exe 'nnoremap q ' . l:closecmd . '' + exe 'nnoremap q :' . l:closecmd . '' exe 'nnoremap gv :call PreviewVertical("' . l:opencmd . '")'