Skip to content

Commit

Permalink
Massive refactoring of the whole filter/restore mechanism.
Browse files Browse the repository at this point in the history
  • Loading branch information
romainl committed Aug 2, 2015
1 parent f25957d commit df87398
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 80 deletions.
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@

## Features

* no softwrapping in the quickfix window,
* no relative numbers in the quickfix window,
* quickfix buffers are hidden from `:ls` and buffer navigation,
* optional Ack.vim inspired mappings,
* optional mappings for `:cnext`, `:cprevious`, `:lnext`, `:lprevious`
that wrap around the beginning an end of the list,
* optional mapping for jumping to and from the location/quickfix window,
* filter/restore the list,
* perform commands on each line in the list,
* perform commands on each file in the list,
* no soft-wrapping,
* no relative numbers,
* quickfix buffers hidden from `:ls` and buffer navigation,
* Ack.vim-inspired mappings,
* wrapping mappings for `:cnext`, `:cprevious`, `:lnext`, `:lprevious`,
* jump to and from the location/quickfix window with a single mapping
* filter/restore the list
* perform commands on each line in the list
* perform commands on each file in the list
* open the location/quickfix window automatically after `:make`, `:grep`,
`:lvimgrep` and friends if there are valid locations/errors
* quit Vim if the last window is a location/quickfix window
Expand Down Expand Up @@ -46,6 +45,7 @@ on Windows.

## TODO

* Ask #vim's opinion.
* Export more options?
* Add a gifcast to the README?

Expand All @@ -54,4 +54,7 @@ on Windows.
* Use `<Plug>` mappings.
* Add proper attribution for a few features.
* Write a proper `help` file.
`<Plug>` mappings.
* Add proper attribution for a few features.
* Write a proper `help` file.
* Ask #vim's opinion.
21 changes: 11 additions & 10 deletions after/ftplugin/qf.vim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
" vim-qf - Tame the quickfix window
" Maintainer: romainl <[email protected]>
" Version: 0.0.3
" Version: 0.0.2
" License: Vim License (see :help license)
" Location: after/ftplugin/qf.vim
" Website: https://github.com/romainl/vim-qf
Expand All @@ -24,14 +24,14 @@ setlocal number
" we don't want quickfix buffers to pop up when doing :bn or :bp
set nobuflisted

" are we in a location list or a quickfix list?
let b:isLoc = len(getloclist(0)) > 0 ? 1 : 0

" customize the statusline
if exists("g:qf_statusline")
execute "setlocal statusline=" . g:qf_statusline.before . "%{w:quickfix_title}" . g:qf_statusline.after
execute "setlocal statusline=" . g:qf_statusline.before . "%{qf#SetStatusline()}" . g:qf_statusline.after
endif

" are we in a location list or a quickfix list?
let b:isLoc = len(getloclist(0)) > 0 ? 1 : 0

" force the quickfix window to be opened at the bottom
" of the screen and take the full width
wincmd J
Expand All @@ -56,25 +56,26 @@ endif

" filter the location/quickfix list
" usage:
" :Filter foo
command! -buffer -nargs=* Filter call qf#FilterList(<q-args>)
" :Filter foo
command! -buffer -nargs=1 Filter call qf#FilterList(<q-args>)

" restore the location/quickfix list
" usage:
" :Restore
" :Restore
command! -buffer Restore call qf#RestoreList()

" do something on each line in the location/quickfix list
" usage:
" :Doline s/^/---
" :Doline s/^/---
command! -buffer -nargs=1 Doline call qf#DoList(1, <q-args>)

" do something on each file in the location/quickfix list
" usage:
" :Dofile %s/^/---
" :Dofile %s/^/---
command! -buffer -nargs=1 Dofile call qf#DoList(0, <q-args>)

" quit Vim if the last window is a quickfix window
autocmd qf BufEnter <buffer> if winnr('$') < 2 | q | endif
autocmd qf BufWinEnter <buffer> call qf#ReuseTitle()

let &cpo = s:save_cpo
175 changes: 158 additions & 17 deletions autoload/qf.vim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
" vim-qf - Tame the quickfix window
" Maintainer: romainl <[email protected]>
" Version: 0.0.3
" Version: 0.0.2
" License: Vim License (see :help license)
" Location: autoload/qf.vim
" Website: https://github.com/romainl/vim-qf
Expand Down Expand Up @@ -51,33 +51,174 @@ endfunction

" filter the current list
function qf#FilterList(pat)
call qf#AddList()
call qf#AddTitle(w:quickfix_title)

call qf#SetList(a:pat)

call qf#SetTitle(a:pat)
call qf#AddTitle(w:quickfix_title)
endfunction

" restore the original list
function qf#RestoreList()
if exists("b:isLoc")
if b:isLoc == 1
if !exists("b:locl")
let b:locl = getloclist(0)
let w:qf_title = w:quickfix_title
let lists = getwinvar(winnr("#"), "qf_location_lists")
if len(lists) > 0
call setloclist(0, getwinvar(winnr("#"), "qf_location_lists")[0], "r")
let w:quickfix_title = getwinvar(winnr("#"), "qf_location_titles")[0]
else
echo "No filter applied. Nothing to restore."
endif
call setloclist(0, filter(getloclist(0), "bufname(v:val['bufnr']) =~ a:pat || v:val['text'] =~ a:pat"))
else
if !exists("b:qfl")
let b:qfl = getqflist()
let w:qf_title = w:quickfix_title
if exists("g:qf_quickfix_lists")
if len(g:qf_quickfix_lists) > 0
call setqflist(g:qf_quickfix_lists[0], "r")
let w:quickfix_title = g:qf_quickfix_titles[0]
else
echo "No filter applied. Nothing to restore."
endif
endif
call setqflist(filter(getqflist(), "bufname(v:val['bufnr']) =~ a:pat || v:val['text'] =~ a:pat"))
endif
let w:quickfix_title = w:qf_title . "[filtered]"
endif
call qf#ResetLists()
endfunction

" restore the original list
function qf#RestoreList()
function qf#ResetLists()
if exists("b:isLoc")
if b:isLoc == 1
call setwinvar(winnr("#"), "qf_location_lists", [])
call setwinvar(winnr("#"), "qf_location_titles", [])
else
let g:qf_quickfix_lists = []
let g:qf_quickfix_titles = []
endif
endif
endfunction

function qf#SetStatusline()
if exists("b:isLoc")
if b:isLoc == 1
let titles = getwinvar(winnr("#"), "qf_location_titles")
if len(titles) > 0
return titles[-1]
else
if exists("w:quickfix_title")
return w:quickfix_title
else
return ""
endif
endif
else
if exists("g:qf_quickfix_titles")
if len(g:qf_quickfix_titles) > 0
return g:qf_quickfix_titles[-1]
else
if exists("w:quickfix_title")
return w:quickfix_title
else
return ""
endif
endif
else
if exists("w:quickfix_title")
return w:quickfix_title
else
return ""
endif
endif
endif
endif
endfunction

function qf#SetList(pat)
if exists("b:isLoc")
if b:isLoc == 1
call setloclist(0, filter(getloclist(0), "bufname(v:val['bufnr']) =~ a:pat || v:val['text'] =~ a:pat"), "r")
else
call setqflist(filter(getqflist(), "bufname(v:val['bufnr']) =~ a:pat || v:val['text'] =~ a:pat"), "r")
endif
endif
endfunction

function qf#AddList()
if exists("b:isLoc")
if b:isLoc == 1
let locations = getwinvar(winnr("#"), "qf_location_lists")
if len(locations) > 0
call add(locations, getloclist(0))
call setwinvar(winnr("#"), "qf_location_lists", locations)
else
call setwinvar(winnr("#"), "qf_location_lists", [getloclist(0)])
endif
else
if exists("g:qf_quickfix_lists")
let g:qf_quickfix_lists = add(g:qf_quickfix_lists, getqflist())
else
let g:qf_quickfix_lists = [getqflist()]
endif
endif
endif
endfunction

function qf#SetTitle(pat)
if exists("b:isLoc")
if b:isLoc == 1
let w:quickfix_title = getwinvar(winnr("#"), "qf_location_titles")[0] . " [filter: '" . a:pat . "']"
else
if len(g:qf_quickfix_titles) > 0
let w:quickfix_title = g:qf_quickfix_titles[0] . " [filter: '" . a:pat . "']"
else
let w:quickfix_title = w:quickfix_title . " [filter: '" . a:pat . "']"
endif
endif
endif
endfunction

function qf#AddTitle(title)
if exists("b:isLoc")
if b:isLoc == 1
let titles = getwinvar(winnr("#"), "qf_location_titles")
if len(titles) > 0
call add(titles, a:title)
call setwinvar(winnr("#"), "qf_location_titles", titles)
else
call setwinvar(winnr("#"), "qf_location_titles", [a:title])
endif
else
if exists("g:qf_quickfix_titles")
let g:qf_quickfix_titles = add(g:qf_quickfix_titles, a:title)
else
let g:qf_quickfix_titles = [a:title]
endif
endif
endif
endfunction

function qf#ReuseTitle()
if exists("b:isLoc")
if b:isLoc == 1 && exists("b:locl")
call setloclist(0, b:locl)
elseif b:isLoc != 1 && !exists("b:locl")
call setqflist(b:qfl)
if b:isLoc == 1
let titles = getwinvar(winnr("#"), "qf_location_titles")
if len(titles) > 0
let w:quickfix_title = getwinvar(winnr("#"), "qf_location_titles")[0]"
endif
else
if exists("g:qf_quickfix_titles")
let w:quickfix_title = g:qf_quickfix_titles[0]
endif
endif
endif
endfunction

" template
function qf#FunctionName(argument)
if exists("b:isLoc")
if b:isLoc == 1
" do something
else
" do something else
endif
let w:quickfix_title = w:qf_title
endif
endfunction

Expand Down
46 changes: 8 additions & 38 deletions doc/qf.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ Help on using vim-qf *qf* *vim-qf
2. Installation ............................... |qf-installation|
3. Configuration .............................. |qf-configuration|
4. Usage ...................................... |qf-usage|
5. Sample config .............................. |qf-sample|
6. Acknowledgements ........................... |qf-acknowledgements|
7. TODO ....................................... |qf-todo|
8. DONE ....................................... |qf-done|
5. Acknowledgements ........................... |qf-acknowledgements|
6. TODO ....................................... |qf-todo|
7. DONE ....................................... |qf-done|

==============================================================================
1. INTRODUCTION *qf-intro*
Expand Down Expand Up @@ -153,7 +152,8 @@ is focused:
*:Filter*

Removes every item that doesn't match with the supplied argument,
either in the filename or in the description.
either in the filename or in the description. Note: no regular
expressions.

Example: >
Expand All @@ -180,37 +180,7 @@ is focused:
:Dofile norm @q
<
==============================================================================
5. SAMPLE CONFIG *qf-sample*

Here is a sample "all-inclusive" congig: >
" use Ack.vim mappings
let g:qf_mapping_ack_style = 1
" 'synchronize' the statusline with our own
let g:qf_statusline = {}
let g:qf_statusline.before = '%<\ '
let g:qf_statusline.after = '\ %f%=%l\/%-6L\ \ \ \ \ '
" wrapping mappings for convenience
nmap <Home> <Plug>QfCprevious
nmap <End> <Plug>QfCnext
nmap <C-Home> <Plug>QfLprevious
nmap <C-end> <Plug>QfLnext
" switch to/from the location/quickfix window
nmap ç <Plug>QfSwitch
" press '§' to filter the list with the filename under the cursor
" press '<F5>' to restore the list
augroup vimqf
autocmd!
autocmd FileType qf nnoremap § :Filter <C-r><C-f><CR>
autocmd FileType qf nnoremap <F5> :Restore<CR>
augroup END
<
==============================================================================
6. ACKNOWLEGEMENTS *qf-acknowledgements*
5. ACKNOWLEGEMENTS *qf-acknowledgements*

The "Ack.vim-inspired mappings" are adapted from Ack.vim:

Expand All @@ -234,13 +204,13 @@ a proper plugin come from Barry Arthur's Area 41 plugin:
- https://github.com/dahu/Area-41

==============================================================================
7. TODO *qf-todo*
6. TODO *qf-todo*

- Export more options?
- Add a gifcast to the README?

==============================================================================
8. DONE *qf-done*
7. DONE *qf-done*

- Use <Plug> mappings.
- Add proper attribution for a few features.
Expand Down
Loading

0 comments on commit df87398

Please sign in to comment.