From 7ca91ddcc352f8d7db0f8394b52a4c3db6cb7b69 Mon Sep 17 00:00:00 2001 From: n3wborn Date: Tue, 17 Sep 2024 22:34:07 +0200 Subject: [PATCH] feat(autocmd): preview qflist files using --- lua/custom/autocommands.lua | 67 +++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/lua/custom/autocommands.lua b/lua/custom/autocommands.lua index ec25f90..ac9cf02 100644 --- a/lua/custom/autocommands.lua +++ b/lua/custom/autocommands.lua @@ -133,4 +133,71 @@ vim.api.nvim_create_autocmd({ 'LspAttach' }, { end, }) +-- https://www.reddit.com/r/neovim/comments/1fhy2xi/comment/lnea46c/ +vim.api.nvim_create_autocmd('FileType', { + pattern = 'qf', + desc = 'navigate and preview qf-list results using and ', + callback = function(event) + local opts = { buffer = event.buf, silent = true } + local init_bufnr = vim.fn.bufnr('#') + vim.keymap.set('n', '', function() + if vim.fn.line('.') == vim.fn.line('$') then + vim.notify('E553: No more items', vim.log.levels.ERROR) + return + end + vim.cmd('wincmd p') -- jump to current displayed file + vim.cmd( + (vim.fn.bufnr('%') ~= init_bufnr and vim.bo.filetype ~= 'qf') + and ('bd | wincmd p | cn | res %d'):format( + math.floor( + ( + vim.o.lines + - vim.o.cmdheight + - (vim.o.laststatus == 0 and 0 or 1) + - (vim.o.tabline == '' and 0 or 1) + ) + / 3 + * 2 + + 0.5 + ) - 1 + ) + or 'cn' + ) + vim.cmd('execute "normal! zz"') + if vim.bo.filetype ~= 'qf' then + vim.cmd('wincmd p') + end + end, opts) + + vim.keymap.set('n', '', function() + if vim.fn.line('.') == 1 then + vim.notify('E553: No more items', vim.log.levels.ERROR) + return + end + vim.cmd('wincmd p') -- jump to current displayed file + vim.cmd( + (vim.fn.bufnr('%') ~= init_bufnr and vim.bo.filetype ~= 'qf') + and ('bd | wincmd p | cN | res %d'):format( + math.floor( + ( + vim.o.lines + - vim.o.cmdheight + - (vim.o.laststatus == 0 and 0 or 1) + - (vim.o.tabline == '' and 0 or 1) + ) + / 3 + * 2 + + 0.5 + ) - 1 + ) + or 'cN' + ) + vim.cmd('execute "normal! zz"') + if vim.bo.filetype ~= 'qf' then + vim.cmd('wincmd p') + end + end, opts) + end, +}) + vim.opt.updatetime = 100