From b7f614b82b3d1fcbe0ca4814b8e06ac769ce1be6 Mon Sep 17 00:00:00 2001 From: JP Appel Date: Wed, 25 Dec 2024 00:38:35 -0500 Subject: Move nonkeymap functions to separate file --- nvim/lua/keymap.lua | 80 +++++++---------------------------------------------- nvim/lua/shared.lua | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 70 deletions(-) create mode 100644 nvim/lua/shared.lua (limited to 'nvim/lua') diff --git a/nvim/lua/keymap.lua b/nvim/lua/keymap.lua index 4ad1bc6..56f5491 100644 --- a/nvim/lua/keymap.lua +++ b/nvim/lua/keymap.lua @@ -1,25 +1,8 @@ -local M = {} - -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+.*") - vim.api.nvim_err_writeln(msg) - end -end +local shared = require('shared') -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 - table.insert(qf_items, { bufnr = bufnr, lnum = line_nums[i], text = line }) - end - end - vim.fn.setqflist(qf_items, 'r') -end +local M = {} -vim.keymap.set('n', 'gf', function() cmd_pcall(':e ') end, { noremap = true }) +vim.keymap.set('n', 'gf', function() shared.cmd_pcall(':e ') end, { noremap = true }) vim.keymap.set('n', "q", function() if vim.o.ft == "qf" then vim.cmd('ccl') @@ -28,78 +11,35 @@ vim.keymap.set('n', "q", function() end end, { noremap = true, desc = "Toggle bottom quickfix window" }) -vim.keymap.set('n', "l", function() cmd_pcall(':aboveleft lope') end) +vim.keymap.set('n', "l", function() shared.cmd_pcall(':aboveleft lope') end) vim.keymap.set('n', "dk", function() vim.diagnostic.open_float() end) vim.keymap.set('n', "[q", "cprev", { noremap = true }) vim.keymap.set('n', "]q", "cnext", { noremap = true }) -- Send visual selection to quickfix list 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 range = { start = vim.api.nvim_buf_get_mark(0, "<")[1], stop = vim.api.nvim_buf_get_mark(0, ">")[1] } + local visual = shared.get_visual() local line_nums = {} - local lines = vim.api.nvim_buf_get_lines(0, range.start - 1, range.stop, false) - - for i = range.start, range.stop, 1 do + for i = visual.range.start, visual.range.stop, 1 do table.insert(line_nums, i) end - send_to_qf(lines, line_nums) + shared.send_to_qf(visual.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("", true, false, true), 'x', false) - - 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) + shared.vert_shift_selection(1) end, { noremap = true, desc = "shift visual selection down" }) vim.keymap.set('v', "K", function() - vert_shift_selection(-1) + shared.vert_shift_selection(-1) end, { noremap = true, desc = "shift visual selection up" }) vim.keymap.set('n', "k", "make") vim.keymap.set('t', "", "") + return M diff --git a/nvim/lua/shared.lua b/nvim/lua/shared.lua new file mode 100644 index 0000000..ae97cdd --- /dev/null +++ b/nvim/lua/shared.lua @@ -0,0 +1,79 @@ +local M = {} + +-- pcall a vim command +function M.cmd_pcall(string) + local success, msg = pcall(function() vim.cmd(string) end) + if not success then + msg = string.match(tostring(msg), "E%d+.*") + vim.api.nvim_err_writeln(msg) + end +end + +--- Get the lines of a visual selection +--- @return { lines : string[], range : { start : integer, stop : integer } } +M.get_visual = 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 range = { start = vim.api.nvim_buf_get_mark(0, "<")[1], stop = vim.api.nvim_buf_get_mark(0, ">")[1] } + + local lines = vim.api.nvim_buf_get_lines(0, range.start - 1, range.stop, false) + + return { lines = lines, range = range } +end + +-- send lines from the current buffer to quickfix +--- @param lines string[] +--- @param line_nums integer[] +function M.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 + table.insert(qf_items, { bufnr = bufnr, lnum = line_nums[i], text = line }) + end + end + vim.fn.setqflist(qf_items, 'r') +end + +-- Shift the visual selection by amount, clamping to buffer size +--- @param amount integer +function M.vert_shift_selection(amount) + vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("", true, false, true), 'x', false) + + 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 + +return M -- cgit v1.2.3