aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--nvim/lua/keymap.lua80
1 files changed, 64 insertions, 16 deletions
diff --git a/nvim/lua/keymap.lua b/nvim/lua/keymap.lua
index 2e0ae76..017237a 100644
--- a/nvim/lua/keymap.lua
+++ b/nvim/lua/keymap.lua
@@ -8,7 +8,8 @@ local cmd_pcall = function(string)
end
end
-local function send_to_qf(bufnr, lines, line_nums)
+local function send_to_qf(lines, line_nums)
+ local bufnr = vim.api.nvim_get_current_buf()
local qf_items = {}
for i, line in ipairs(lines) do
if line:match("^%s*$") == nil then
@@ -22,29 +23,76 @@ vim.keymap.set('n', 'gf', function() cmd_pcall(':e <cfile>') end, { noremap = tr
vim.keymap.set('n', "<Leader>q", function() vim.cmd('botright cope') end)
vim.keymap.set('n', "<Leader>l", function() cmd_pcall(':aboveleft lope') end)
vim.keymap.set('n', "<Leader>dk", function() vim.diagnostic.open_float() end)
+vim.keymap.set('n', "[q", "<cmd>cprev<cr>", { noremap = true })
+vim.keymap.set('n', "]q", "<cmd>cnext<cr>", { noremap = true })
+-- Send visual selection to quickfix list
vim.keymap.set('v', "<C-q>", function()
- -- return to normal mode to correctly set '< and '> marks
- vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<ESC>", true, false, true), 'x', false)
+ -- return to normal mode to correctly set '< and '> marks
+ vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<ESC>", 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 range = { start = vim.api.nvim_buf_get_mark(0, "<")[1], stop = vim.api.nvim_buf_get_mark(0, ">")[1] }
- local line_nums = {}
- local lines = vim.api.nvim_buf_get_lines(bufnr, start_line, end_line+1, false)
+ local line_nums = {}
+ local lines = vim.api.nvim_buf_get_lines(0, range.start - 1, range.stop, false)
- for i = start_line, end_line, 1 do
- table.insert(line_nums, i)
- end
+ for i = range.start, range.stop, 1 do
+ table.insert(line_nums, i)
+ end
+
+ send_to_qf(lines, line_nums)
+ vim.cmd('botright cope')
+end, { noremap = true, desc = "Send visual selection to quickfix list" })
+
+-- Shift the visual selection by amount, clamping to buffer size
+--- @param amount integer
+local function vert_shift_selection(amount)
+ vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<ESC>", true, false, true), 'x', false)
- send_to_qf(bufnr, lines, line_nums)
- vim.cmd('botright cope')
- end,
- { noremap = true, desc = "Send visual selection to quickfix list" })
+ local init = { start = vim.api.nvim_buf_get_mark(0, '<'), stop = vim.api.nvim_buf_get_mark(0, '>') }
+ local target = {
+ start = { init.start[1] + amount, init.start[2] },
+ stop = { init.stop[1] + amount, init.stop[2] }
+ }
+
+ local linecount = vim.api.nvim_buf_line_count(0)
+ if target.start[1] < 1 then
+ target.start[1] = 1
+ target.stop[1] = 1 + (init.stop[1] - init.start[1])
+ elseif target.stop[1] > linecount then
+ target.stop[1] = linecount
+ target.start[1] = linecount - (target.stop[1] - target.start[1])
+ end
+
+ -- HACK: should be shifted into target calculation
+ if target.stop[1] > linecount or target.start[1] < 1 then
+ return
+ end
+
+ -- shift lines
+ if amount < 0 then
+ vim.cmd("'<,'>mo '<" .. amount-1)
+ elseif amount > 0 then
+ vim.cmd("'<,'>mo '>+" .. amount)
+ end
+
+ -- update marks
+ local bufnr = vim.api.nvim_get_current_buf()
+ vim.api.nvim_buf_set_mark(bufnr, '<', target.start[1], target.start[2], {})
+ vim.api.nvim_buf_set_mark(bufnr, '>', target.stop[1], target.stop[2], {})
+
+ vim.cmd("normal! gv")
+end
+
+-- Move visual selection with J and K
+vim.keymap.set('v', "J", function()
+ vert_shift_selection(1)
+end, { noremap = true, desc = "shift visual selection down" })
+vim.keymap.set('v', "K", function()
+ vert_shift_selection(-1)
+end, { noremap = true, desc = "shift visual selection up" })
vim.keymap.set('n', "<Leader>k", function() vim.cmd("make") end)
vim.keymap.set('t', "<ESC><ESC>", "<c-\\><c-n>")
-
return M