Skip to content

Commit 60059dd

Browse files
committed
Add parameters to control wrap
1 parent 42deb4f commit 60059dd

File tree

3 files changed

+69
-42
lines changed

3 files changed

+69
-42
lines changed

README.md

+10-6
Original file line numberDiff line numberDiff line change
@@ -96,19 +96,23 @@ require('qf').clear(list, name)
9696
-- If entry is further away than limit, the entry will not be selected. This is to prevent recentering of cursor caused by setpos. There is no way to select an entry without jumping, so the cursor position is saved and restored instead.
9797
require('qf').follow(list, strategy)
9898

99-
-- Wrapping version of [lc]next
100-
require('qf').next(list)
99+
-- Wrapping version of [lc]next. Also takes into account valid entries.
100+
-- If wrap is nil or true, it will wrap around the list
101+
require('qf').next(list, wrap = true)
101102

102-
-- Wrapping version of [lc]prev
103-
require('qf').prev(list)
103+
-- Wrapping version of [lc]prev. Also takes into account valid entries.
104+
-- If wrap is nil or true, it will wrap around the list
105+
require('qf').prev(list, wrap = true)
104106

105107
-- Wrapping version of [lc]above
106108
-- Will switch buffer
107-
require('qf').above(list)
109+
-- If wrap is nil or true, it will wrap around the list
110+
require('qf').above(list, wrap = true)
108111

109112
-- Wrapping version of [lc]below
110113
-- Will switch buffer
111-
require('qf').below(list)
114+
-- If wrap is nil or true, it will wrap around the list
115+
require('qf').below(list, wrap = true)
112116

113117
-- Save quickfix or location list with name
114118
require('qf').save(list, name)

after/ftplugin/qf.vim

-15
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,2 @@
11
" Setup window local variables for quickfix and location list windows
2-
3-
42
lua require'qf'.on_ft()
5-
6-
" setlocal nobuflisted
7-
" setlocal nonumber
8-
" setlocal norelativenumber
9-
10-
" " Determine if current window is location list or quickfix list
11-
" let b:list = get(get(getwininfo(win_getid()), 0, {}), 'loclist', 0) ? 'l' : 'c'
12-
13-
" if luaeval("require'qf'.config." . b:list . ".wide_bottom") == v:true
14-
" wincmd J
15-
" endif
16-
17-
" let height = luaeval("require'qf'.get_height('" . b:list . "')")

lua/qf.lua

+59-21
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ local function setup_autocmds(config)
9999
cmd('autocmd QuickFixCmdPost ' .. qf_post_commands() .. ' :lua require"qf".open("c", true)')
100100
end
101101

102-
cmd('autocmd WinLeave * :lua require"qf".reopen_all()')
102+
cmd('autocmd WinNew * :lua require"qf".reopen_all()')
103103

104104
cmd('autocmd QuitPre * :lua require"qf".close("loc")')
105105

@@ -192,15 +192,21 @@ end
192192
-- Close and opens list if already open.
193193
-- This is to fix the list stretching bottom of a new vertical split.
194194
function M.reopen(list)
195+
if vim.o.ft == 'qf' then
196+
return
197+
end
198+
195199
list = fix_list(list)
196200
local num_items = #list_items(list)
197201

198202
if not M.list_visible(list) then
199203
return
200204
end
201205

202-
print("Reopening ", list)
203-
cmd('noau ' .. list .. 'close | ' .. list .. 'open ' .. get_height(list, num_items))
206+
print("Reopening " .. list)
207+
cmd('noau wincmd p | noau ' .. list .. 'close | ' .. list .. 'open ' .. get_height(list, num_items))
208+
209+
M.on_ft()
204210
end
205211

206212
function M.reopen_all()
@@ -210,14 +216,18 @@ function M.reopen_all()
210216
end
211217

212218
-- Setup qf filetype specific options
213-
function M.on_ft()
214-
local wininfo = fn.getwininfo(fn.win_getid()) or {}
219+
function M.on_ft(winid)
220+
winid = winid or fn.win_getid()
221+
local wininfo = fn.getwininfo(winid) or {}
215222
local list = nil
216223

217224
if not wininfo or not wininfo[1] then
225+
print("Not a quickfix list")
218226
return
219227
end
220228

229+
-- print(vim.inspect(wininfo))
230+
221231
if wininfo[1].quickfix == 1 then
222232
list = 'c'
223233
end
@@ -268,7 +278,7 @@ function M.resize(list, stay, num_items)
268278
cmd(list .. "open " .. height )
269279

270280
if stay then
271-
cmd "wincmd p"
281+
cmd "noau wincmd p"
272282
end
273283
end
274284

@@ -297,7 +307,7 @@ function M.open(list, stay, verbose)
297307
cmd(list .. 'open ' .. get_height(list, num_items))
298308

299309
if stay then
300-
cmd "wincmd p"
310+
cmd "noau wincmd p"
301311
end
302312

303313
if opts.close_other then
@@ -423,6 +433,10 @@ local strategy_lookup = {
423433
-- (optional) limit, don't select entry further away than limit.
424434
-- If entry is further away than limit, the entry will not be selected. This is to prevent recentering of cursor caused by setpos. There is no way to select an entry without jumping, so the cursor position is saved and restored instead.
425435
function M.follow(list, strategy, limit)
436+
if api.nvim_get_mode().mode ~= 'n' then
437+
return
438+
end
439+
426440
list = fix_list(list)
427441
local opts = M.config[list]
428442

@@ -476,33 +490,41 @@ function M.follow(list, strategy, limit)
476490
fn.setpos('.', pos)
477491
end
478492

479-
-- Wrapping version of [lc]next
480-
function M.next(list, verbose)
493+
-- Wrapping version of [lc]next. Also takes into account valid entries.
494+
-- If wrap is nil or true, it will wrap around the list
495+
function M.next(list, verbose, wrap)
496+
if wrap == nil then
497+
wrap = true
498+
end
481499
list = fix_list(list)
482500

483501
if not check_empty(list, #list_items(list), verbose) then
484502
return
485503
end
486504

487-
if list == 'c' then
488-
cmd "try | :cnext | catch | cfirst | endtry"
505+
if wrap then
506+
cmd ("try | :" .. list .. "next | catch | " .. list .. "first | endtry")
489507
else
490-
cmd "try | :lnext | catch | lfirst | endtry"
508+
cmd ("try | :" .. list .. "next | catch | call nvim_err_writeln('No More Items') | endtry")
491509
end
492510
end
493511

494-
-- Wrapping version of [lc]prev
495-
function M.prev(list, verbose)
512+
-- Wrapping version of [lc]prev. Also takes into account valid entries.
513+
-- If wrap is nil or true, it will wrap around the list
514+
function M.prev(list, verbose, wrap)
515+
if wrap == nil then
516+
wrap = true
517+
end
496518
list = fix_list(list)
497519

498520
if not check_empty(list, #list_items(list), verbose) then
499521
return
500522
end
501523

502-
if list == 'c' then
503-
cmd "try | :cprev | catch | clast | endtry"
524+
if wrap then
525+
cmd ("try | :" .. list .. "prev | catch | " .. list .. "last | endtry")
504526
else
505-
cmd "try | :lprev | catch | llast | endtry"
527+
cmd ("try | :" .. list .. "prev | catch | call nvim_err_writeln('No More Items') | endtry")
506528
end
507529
end
508530

@@ -535,7 +557,11 @@ end
535557

536558
-- Wrapping version of [lc]above
537559
-- Will switch buffer
538-
function M.above(list, verbose)
560+
function M.above(list, verbose, wrap)
561+
if wrap == nil then
562+
wrap = true
563+
end
564+
539565
list = fix_list(list)
540566

541567
local items = list_items(list)
@@ -551,7 +577,12 @@ function M.above(list, verbose)
551577

552578
-- Go to last valid entry
553579
if idx == 0 then
554-
idx = prev_valid(items, #items)
580+
if wrap then
581+
idx = prev_valid(items, #items)
582+
else
583+
api.nvim_err_writeln("No more items")
584+
return
585+
end
555586
end
556587

557588
-- No valid entries, go to first.
@@ -568,7 +599,10 @@ end
568599

569600
-- Wrapping version of [lc]below
570601
-- Will switch buffer
571-
function M.below(list, verbose)
602+
function M.below(list, verbose, wrap)
603+
if wrap == nil then
604+
wrap = true
605+
end
572606
list = fix_list(list)
573607

574608
local items = list_items(list)
@@ -584,7 +618,11 @@ function M.below(list, verbose)
584618

585619
-- Go to first valid entry
586620
if not idx or idx > #items then
587-
idx = next_valid(items, 1)
621+
if wrap then
622+
idx = next_valid(items, 1)
623+
else
624+
api.nvim_err_writeln("No more items")
625+
end
588626
end
589627
if list == 'c' then
590628
cmd('cc ' .. idx)

0 commit comments

Comments
 (0)