|
| 1 | +-- Shortcuts for quickfix |
| 2 | + |
| 3 | +local function clamp(target, a, b) |
| 4 | + if target <= a then |
| 5 | + return a |
| 6 | + end |
| 7 | + if target >= b then |
| 8 | + return b |
| 9 | + end |
| 10 | + return target |
| 11 | +end |
| 12 | + |
| 13 | +local function clamp_linecount(target) |
| 14 | + local count = vim.api.nvim_buf_line_count(0) |
| 15 | + return clamp(target, 1, count) |
| 16 | +end |
| 17 | + |
| 18 | +vim.api.nvim_create_autocmd('BufWinEnter', { |
| 19 | + callback = function(ev) |
| 20 | + if vim.bo[ev.buf].buftype ~= 'quickfix' then |
| 21 | + return |
| 22 | + end |
| 23 | + |
| 24 | + vim.keymap.set('n', 'dd', function() |
| 25 | + local cursor = vim.api.nvim_win_get_cursor(0) |
| 26 | + |
| 27 | + local entries = vim.fn.getqflist() |
| 28 | + local rm_index = cursor[1] |
| 29 | + table.remove(entries, rm_index) |
| 30 | + vim.fn.setqflist(entries) |
| 31 | + |
| 32 | + vim.api.nvim_win_set_cursor(0, { clamp_linecount(cursor[1]), cursor[2] }) |
| 33 | + end, { buffer = ev.buf }) |
| 34 | + |
| 35 | + vim.keymap.set('v', 'd', function() |
| 36 | + local cursor = vim.api.nvim_win_get_cursor(0) |
| 37 | + |
| 38 | + local from, to = vim.fn.line 'v', vim.fn.line '.' |
| 39 | + local qf = {} |
| 40 | + for i, v in ipairs(vim.fn.getqflist()) do |
| 41 | + if i < from or i > to then |
| 42 | + table.insert(qf, v) |
| 43 | + end |
| 44 | + end |
| 45 | + vim.fn.setqflist(qf) |
| 46 | + vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('<esc>', true, true, true), 'nv', false) |
| 47 | + |
| 48 | + vim.api.nvim_win_set_cursor(0, { clamp_linecount(from), cursor[2] }) |
| 49 | + end, { buffer = ev.buf }) |
| 50 | + end, |
| 51 | +}) |
0 commit comments