From 4bfccb24e8342896081cdfaf17a55f01e81b4766 Mon Sep 17 00:00:00 2001 From: JP Appel Date: Sun, 22 Sep 2024 14:24:34 -0400 Subject: Add bind to send selection to qf list --- nvim/lua/keymap.lua | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) (limited to 'nvim') diff --git a/nvim/lua/keymap.lua b/nvim/lua/keymap.lua index 5fff17b..2e0ae76 100644 --- a/nvim/lua/keymap.lua +++ b/nvim/lua/keymap.lua @@ -1,6 +1,6 @@ local M = {} -local cmd_pcall = function (string) +local cmd_pcall = function(string) local success, msg = pcall(function() vim.cmd(string) end) if not success then msg = string.match(tostring(msg), "E%d+.*") @@ -8,12 +8,42 @@ local cmd_pcall = function (string) end end -vim.keymap.set('n', 'gf', function() cmd_pcall(':e ') end, {noremap = true}) -vim.keymap.set('n', "q", function() vim.cmd(':botright cope') end) +local function send_to_qf(bufnr, lines, line_nums) + local qf_items = {} + for i, line in ipairs(lines) do + if line:match("^%s*$") == nil then + table.insert(qf_items, { bufnr = bufnr, lnum = line_nums[i], text = line }) + end + end + vim.fn.setqflist(qf_items, 'r') +end + +vim.keymap.set('n', 'gf', function() cmd_pcall(':e ') end, { noremap = true }) +vim.keymap.set('n', "q", function() vim.cmd('botright cope') end) vim.keymap.set('n', "l", function() cmd_pcall(':aboveleft lope') end) -vim.keymap.set('n', "dk", function () vim.diagnostic.open_float() end) +vim.keymap.set('n', "dk", function() vim.diagnostic.open_float() end) + +vim.keymap.set('v', "", function() + -- return to normal mode to correctly set '< and '> marks + vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("", true, false, true), 'x', false) + + local bufnr = vim.api.nvim_get_current_buf() + local start_line = vim.fn.getpos("'<")[2] + local end_line = vim.fn.getpos("'>")[2] + + local line_nums = {} + local lines = vim.api.nvim_buf_get_lines(bufnr, start_line, end_line+1, false) + + for i = start_line, end_line, 1 do + table.insert(line_nums, i) + end + + send_to_qf(bufnr, lines, line_nums) + vim.cmd('botright cope') + end, + { noremap = true, desc = "Send visual selection to quickfix list" }) -vim.keymap.set('n',"k", function() vim.cmd("make") end) +vim.keymap.set('n', "k", function() vim.cmd("make") end) vim.keymap.set('t', "", "") -- cgit v1.2.3