Files
vim-denote/autoload/denote/loclist.vim
2026-03-03 23:03:34 +01:00

105 lines
3.0 KiB
VimL

" Clear location list
function denote#loclist#clear()
call setloclist(0, [], ' ')
endfunction
" Local helper function to retrieve and format the title.
function s:titleFromBuf(buf)
let l:name = libdenote#scheme_metadata(bufname(a:buf)).title
return strchars(l:name, 1) <= g:denote_loc_title_columns
\ ? printf('%' .. g:denote_loc_title_columns .. 's', l:name)
\ : printf('%.' .. (g:denote_loc_title_columns - 1) .. 's', l:name) .. '…'
endfunction
" Local helper function to truncate text with match at the given column as a
" string of at most `width` columns.
function s:formatText(text, col, width)
return a:col <= a:width
\ ? a:text
\ : a:text[a:col-float2nr(a:width*0.3):]
endfunction
" This modifies the location list for pretty display.
function s:textReferences(info)
let l:items=getloclist(a:info.winid)
let l:l=[]
let l:width=winwidth(0) - g:denote_loc_title_columns - 19
for idx in range(a:info.start_idx - 1, a:info.end_idx - 1)
let l:e=l:items[idx]
let l:name=s:titleFromBuf(l:e.bufnr)
let l:lnum=printf('%5d', l:e.lnum)
let l:col=printf('%3d', l:e.col)
call add(l:l, l:name ..
\ ' | ' .. l:lnum .. ' l:col ' .. l:col ..
\ ' | ' .. s:formatText(l:items[idx].text, l:col, l:width))
endfor
return l:l
endfunction
" This modifies the location list for pretty display.
function s:textNoteList(info)
let l:items=getloclist(a:info.winid)
let l:l=[]
for idx in range(a:info.start_idx - 1, a:info.end_idx - 1)
let l:e=l:items[idx]
let l:name=s:titleFromBuf(l:e.bufnr)
let l:ntags = libdenote#scheme_metadata(bufname(l:e.bufnr)).tags->join()
call add(l:l, l:name .. ' | ' .. l:ntags)
endfor
return l:l
endfunction
" Re-populate location list with denote entries
function denote#loclist#fill(title, files, Gfun)
" Clear first
call setloclist(0, [], ' ')
" Set properties
call setloclist(0, [], 'r',
\ {'title': a:title,
\ 'quickfixtextfunc' : 's:textNoteList',
\ 'context' : {'denote': 'list', 'gfun': a:Gfun}})
" Populate
let l:notes=[]
for f in a:files
call add(l:notes, {
\ 'filename' : f,
\ 'lnum' : 1
\ })
endfor
call setloclist(0, l:notes, 'a')
endfunction
" Specify location list as denote-grep list
function denote#loclist#setgrep(title, Gfun)
call setloclist(0, [], 'r',
\ {'title': a:title,
\ 'quickfixtextfunc' : 's:textReferences',
\ 'context' : {'denote': 'grep', 'gfun': a:Gfun}})
endfunction
" Reload location list
function denote#loclist#reload()
let l:context = getloclist(0, {'context': 1})['context']
if has_key(l:context, 'denote') && has_key(l:context, 'gfun')
call denote#loclist#jumptowindow()
exe 'lclose'
call l:context['gfun']()
exe 'lwindow'
endif
endfunction
" Jump to window this location list belongs to
function denote#loclist#jumptowindow()
let l:locprop = getloclist(0, {'filewinid': 0})
if type(l:locprop) != v:t_dict || !has_key(l:locprop, 'filewinid')
return
endif
let l:winid = l:locprop['filewinid']
if l:winid == 0
exe 'new'
exe 'wincmd K'
else
call win_gotoid(l:winid)
endif
endfunction