From 511efe89ed084bc922ae142d32e08427859198f4 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Mon, 10 Jun 2024 23:07:28 +0200 Subject: nvim: Completely rewrite my configuration --- .config/nvim/lua/pipe.lua | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 .config/nvim/lua/pipe.lua (limited to '.config/nvim/lua/pipe.lua') diff --git a/.config/nvim/lua/pipe.lua b/.config/nvim/lua/pipe.lua new file mode 100644 index 0000000..f09a030 --- /dev/null +++ b/.config/nvim/lua/pipe.lua @@ -0,0 +1,42 @@ +function pipe_visual() + pipe_internal({'<', '>'}, vim.fn.visualmode() ~= 'v') +end + +function pipe_normal(arg) + pipe_internal({'[', ']'}, arg == 'line') +end + +function pipe_internal(marks, linesp) + local mode = vim.fn.visualmode() + + local sr, sc = unpack(vim.api.nvim_buf_get_mark(0, marks[1])) + local er, ec = unpack(vim.api.nvim_buf_get_mark(0, marks[2])) + + local lines = linesp + and vim.api.nvim_buf_get_lines(0, sr - 1, er, true) + or vim.api.nvim_buf_get_text(0, sr - 1, sc, er - 1, ec + 1, {}) + + local ok, cmd = pcall(vim.fn.input, { + prompt = '… | ', + cancelreturn = vim.NIL, + }) + + if not ok or cmd == vim.NIL then + return + end + + local out = vim.fn.systemlist(cmd, lines) + if linesp then + vim.api.nvim_buf_set_lines(0, sr - 1, er, true, out) + else + vim.api.nvim_buf_set_text(0, sr - 1, sc, er - 1, ec + 1, out) + end +end + +vim.keymap.set('n', 'PipeNormal', ':set opfunc=v:lua.pipe_normalg@', + { silent = true }) +vim.keymap.set('x', 'PipeVisual', ':lua pipe_visual()', + { silent = true }) + +vim.keymap.set('n', '|', 'PipeNormal') +vim.keymap.set('x', '|', 'PipeVisual') -- cgit v1.2.3