summaryrefslogtreecommitdiff
path: root/plugin/qf-dd.vim
blob: 385c0c7f966bec034242afc51cfab24f4ceee7ed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
" https://stackoverflow.com/questions/42905008/quickfix-list-how-to-add-and-remove-entries
" When using `dd` in the quickfix list, remove the item from the quickfix list.
function! RemoveQFItem(mode) range abort
  let l:qf_list = getqflist()

  " distinguish mode for getting delete index and delete count
  if a:mode == 'v'
    let l:del_qf_idx = getpos("'<")[1] - 1
    let l:del_ct = getpos("'>")[1] - l:del_qf_idx
  else
    let l:del_qf_idx = line('.') - 1
    let l:del_ct = v:count > 1 ? v:count : 1
  endif

  " delete lines and update quickfix
  for item in range(l:del_ct)
    call remove(l:qf_list, l:del_qf_idx)
  endfor
  call setqflist(l:qf_list, 'r')

  if len(l:qf_list) > 0
    execute l:del_qf_idx + 1 . 'cfirst'
    copen
  else
    cclose
  endif
endfunction

autocmd FileType qf nmap <buffer> dd :call RemoveQFItem('n')<cr>
autocmd FileType qf vmap <buffer> dd :call RemoveQFItem('v')<cr>