Skip to content

Commit df87398

Browse files
committed
Massive refactoring of the whole filter/restore mechanism.
1 parent f25957d commit df87398

File tree

5 files changed

+195
-80
lines changed

5 files changed

+195
-80
lines changed

README.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44

55
## Features
66

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

4746
## TODO
4847

48+
* Ask #vim's opinion.
4949
* Export more options?
5050
* Add a gifcast to the README?
5151

@@ -54,4 +54,7 @@ on Windows.
5454
* Use `<Plug>` mappings.
5555
* Add proper attribution for a few features.
5656
* Write a proper `help` file.
57+
`<Plug>` mappings.
58+
* Add proper attribution for a few features.
59+
* Write a proper `help` file.
5760
* Ask #vim's opinion.

after/ftplugin/qf.vim

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
" vim-qf - Tame the quickfix window
22
" Maintainer: romainl <[email protected]>
3-
" Version: 0.0.3
3+
" Version: 0.0.2
44
" License: Vim License (see :help license)
55
" Location: after/ftplugin/qf.vim
66
" Website: https://github.com/romainl/vim-qf
@@ -24,14 +24,14 @@ setlocal number
2424
" we don't want quickfix buffers to pop up when doing :bn or :bp
2525
set nobuflisted
2626

27+
" are we in a location list or a quickfix list?
28+
let b:isLoc = len(getloclist(0)) > 0 ? 1 : 0
29+
2730
" customize the statusline
2831
if exists("g:qf_statusline")
29-
execute "setlocal statusline=" . g:qf_statusline.before . "%{w:quickfix_title}" . g:qf_statusline.after
32+
execute "setlocal statusline=" . g:qf_statusline.before . "%{qf#SetStatusline()}" . g:qf_statusline.after
3033
endif
3134

32-
" are we in a location list or a quickfix list?
33-
let b:isLoc = len(getloclist(0)) > 0 ? 1 : 0
34-
3535
" force the quickfix window to be opened at the bottom
3636
" of the screen and take the full width
3737
wincmd J
@@ -56,25 +56,26 @@ endif
5656

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

6262
" restore the location/quickfix list
6363
" usage:
64-
" :Restore
64+
" :Restore
6565
command! -buffer Restore call qf#RestoreList()
6666

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

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

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

8081
let &cpo = s:save_cpo

autoload/qf.vim

Lines changed: 158 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
" vim-qf - Tame the quickfix window
22
" Maintainer: romainl <[email protected]>
3-
" Version: 0.0.3
3+
" Version: 0.0.2
44
" License: Vim License (see :help license)
55
" Location: autoload/qf.vim
66
" Website: https://github.com/romainl/vim-qf
@@ -51,33 +51,174 @@ endfunction
5151

5252
" filter the current list
5353
function qf#FilterList(pat)
54+
call qf#AddList()
55+
call qf#AddTitle(w:quickfix_title)
56+
57+
call qf#SetList(a:pat)
58+
59+
call qf#SetTitle(a:pat)
60+
call qf#AddTitle(w:quickfix_title)
61+
endfunction
62+
63+
" restore the original list
64+
function qf#RestoreList()
5465
if exists("b:isLoc")
5566
if b:isLoc == 1
56-
if !exists("b:locl")
57-
let b:locl = getloclist(0)
58-
let w:qf_title = w:quickfix_title
67+
let lists = getwinvar(winnr("#"), "qf_location_lists")
68+
if len(lists) > 0
69+
call setloclist(0, getwinvar(winnr("#"), "qf_location_lists")[0], "r")
70+
let w:quickfix_title = getwinvar(winnr("#"), "qf_location_titles")[0]
71+
else
72+
echo "No filter applied. Nothing to restore."
5973
endif
60-
call setloclist(0, filter(getloclist(0), "bufname(v:val['bufnr']) =~ a:pat || v:val['text'] =~ a:pat"))
6174
else
62-
if !exists("b:qfl")
63-
let b:qfl = getqflist()
64-
let w:qf_title = w:quickfix_title
75+
if exists("g:qf_quickfix_lists")
76+
if len(g:qf_quickfix_lists) > 0
77+
call setqflist(g:qf_quickfix_lists[0], "r")
78+
let w:quickfix_title = g:qf_quickfix_titles[0]
79+
else
80+
echo "No filter applied. Nothing to restore."
81+
endif
6582
endif
66-
call setqflist(filter(getqflist(), "bufname(v:val['bufnr']) =~ a:pat || v:val['text'] =~ a:pat"))
6783
endif
68-
let w:quickfix_title = w:qf_title . "[filtered]"
6984
endif
85+
call qf#ResetLists()
7086
endfunction
7187

72-
" restore the original list
73-
function qf#RestoreList()
88+
function qf#ResetLists()
89+
if exists("b:isLoc")
90+
if b:isLoc == 1
91+
call setwinvar(winnr("#"), "qf_location_lists", [])
92+
call setwinvar(winnr("#"), "qf_location_titles", [])
93+
else
94+
let g:qf_quickfix_lists = []
95+
let g:qf_quickfix_titles = []
96+
endif
97+
endif
98+
endfunction
99+
100+
function qf#SetStatusline()
101+
if exists("b:isLoc")
102+
if b:isLoc == 1
103+
let titles = getwinvar(winnr("#"), "qf_location_titles")
104+
if len(titles) > 0
105+
return titles[-1]
106+
else
107+
if exists("w:quickfix_title")
108+
return w:quickfix_title
109+
else
110+
return ""
111+
endif
112+
endif
113+
else
114+
if exists("g:qf_quickfix_titles")
115+
if len(g:qf_quickfix_titles) > 0
116+
return g:qf_quickfix_titles[-1]
117+
else
118+
if exists("w:quickfix_title")
119+
return w:quickfix_title
120+
else
121+
return ""
122+
endif
123+
endif
124+
else
125+
if exists("w:quickfix_title")
126+
return w:quickfix_title
127+
else
128+
return ""
129+
endif
130+
endif
131+
endif
132+
endif
133+
endfunction
134+
135+
function qf#SetList(pat)
136+
if exists("b:isLoc")
137+
if b:isLoc == 1
138+
call setloclist(0, filter(getloclist(0), "bufname(v:val['bufnr']) =~ a:pat || v:val['text'] =~ a:pat"), "r")
139+
else
140+
call setqflist(filter(getqflist(), "bufname(v:val['bufnr']) =~ a:pat || v:val['text'] =~ a:pat"), "r")
141+
endif
142+
endif
143+
endfunction
144+
145+
function qf#AddList()
146+
if exists("b:isLoc")
147+
if b:isLoc == 1
148+
let locations = getwinvar(winnr("#"), "qf_location_lists")
149+
if len(locations) > 0
150+
call add(locations, getloclist(0))
151+
call setwinvar(winnr("#"), "qf_location_lists", locations)
152+
else
153+
call setwinvar(winnr("#"), "qf_location_lists", [getloclist(0)])
154+
endif
155+
else
156+
if exists("g:qf_quickfix_lists")
157+
let g:qf_quickfix_lists = add(g:qf_quickfix_lists, getqflist())
158+
else
159+
let g:qf_quickfix_lists = [getqflist()]
160+
endif
161+
endif
162+
endif
163+
endfunction
164+
165+
function qf#SetTitle(pat)
166+
if exists("b:isLoc")
167+
if b:isLoc == 1
168+
let w:quickfix_title = getwinvar(winnr("#"), "qf_location_titles")[0] . " [filter: '" . a:pat . "']"
169+
else
170+
if len(g:qf_quickfix_titles) > 0
171+
let w:quickfix_title = g:qf_quickfix_titles[0] . " [filter: '" . a:pat . "']"
172+
else
173+
let w:quickfix_title = w:quickfix_title . " [filter: '" . a:pat . "']"
174+
endif
175+
endif
176+
endif
177+
endfunction
178+
179+
function qf#AddTitle(title)
180+
if exists("b:isLoc")
181+
if b:isLoc == 1
182+
let titles = getwinvar(winnr("#"), "qf_location_titles")
183+
if len(titles) > 0
184+
call add(titles, a:title)
185+
call setwinvar(winnr("#"), "qf_location_titles", titles)
186+
else
187+
call setwinvar(winnr("#"), "qf_location_titles", [a:title])
188+
endif
189+
else
190+
if exists("g:qf_quickfix_titles")
191+
let g:qf_quickfix_titles = add(g:qf_quickfix_titles, a:title)
192+
else
193+
let g:qf_quickfix_titles = [a:title]
194+
endif
195+
endif
196+
endif
197+
endfunction
198+
199+
function qf#ReuseTitle()
74200
if exists("b:isLoc")
75-
if b:isLoc == 1 && exists("b:locl")
76-
call setloclist(0, b:locl)
77-
elseif b:isLoc != 1 && !exists("b:locl")
78-
call setqflist(b:qfl)
201+
if b:isLoc == 1
202+
let titles = getwinvar(winnr("#"), "qf_location_titles")
203+
if len(titles) > 0
204+
let w:quickfix_title = getwinvar(winnr("#"), "qf_location_titles")[0]"
205+
endif
206+
else
207+
if exists("g:qf_quickfix_titles")
208+
let w:quickfix_title = g:qf_quickfix_titles[0]
209+
endif
210+
endif
211+
endif
212+
endfunction
213+
214+
" template
215+
function qf#FunctionName(argument)
216+
if exists("b:isLoc")
217+
if b:isLoc == 1
218+
" do something
219+
else
220+
" do something else
79221
endif
80-
let w:quickfix_title = w:qf_title
81222
endif
82223
endfunction
83224

doc/qf.txt

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ Help on using vim-qf *qf* *vim-qf
1010
2. Installation ............................... |qf-installation|
1111
3. Configuration .............................. |qf-configuration|
1212
4. Usage ...................................... |qf-usage|
13-
5. Sample config .............................. |qf-sample|
14-
6. Acknowledgements ........................... |qf-acknowledgements|
15-
7. TODO ....................................... |qf-todo|
16-
8. DONE ....................................... |qf-done|
13+
5. Acknowledgements ........................... |qf-acknowledgements|
14+
6. TODO ....................................... |qf-todo|
15+
7. DONE ....................................... |qf-done|
1716

1817
==============================================================================
1918
1. INTRODUCTION *qf-intro*
@@ -153,7 +152,8 @@ is focused:
153152
*:Filter*
154153

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

158158
Example: >
159159
@@ -180,37 +180,7 @@ is focused:
180180
:Dofile norm @q
181181
<
182182
==============================================================================
183-
5. SAMPLE CONFIG *qf-sample*
184-
185-
Here is a sample "all-inclusive" congig: >
186-
187-
" use Ack.vim mappings
188-
let g:qf_mapping_ack_style = 1
189-
190-
" 'synchronize' the statusline with our own
191-
let g:qf_statusline = {}
192-
let g:qf_statusline.before = '%<\ '
193-
let g:qf_statusline.after = '\ %f%=%l\/%-6L\ \ \ \ \ '
194-
195-
" wrapping mappings for convenience
196-
nmap <Home> <Plug>QfCprevious
197-
nmap <End> <Plug>QfCnext
198-
nmap <C-Home> <Plug>QfLprevious
199-
nmap <C-end> <Plug>QfLnext
200-
201-
" switch to/from the location/quickfix window
202-
nmap ç <Plug>QfSwitch
203-
204-
" press '§' to filter the list with the filename under the cursor
205-
" press '<F5>' to restore the list
206-
augroup vimqf
207-
autocmd!
208-
autocmd FileType qf nnoremap § :Filter <C-r><C-f><CR>
209-
autocmd FileType qf nnoremap <F5> :Restore<CR>
210-
augroup END
211-
<
212-
==============================================================================
213-
6. ACKNOWLEGEMENTS *qf-acknowledgements*
183+
5. ACKNOWLEGEMENTS *qf-acknowledgements*
214184

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

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

236206
==============================================================================
237-
7. TODO *qf-todo*
207+
6. TODO *qf-todo*
238208

239209
- Export more options?
240210
- Add a gifcast to the README?
241211

242212
==============================================================================
243-
8. DONE *qf-done*
213+
7. DONE *qf-done*
244214

245215
- Use <Plug> mappings.
246216
- Add proper attribution for a few features.

0 commit comments

Comments
 (0)