88 lines
2.7 KiB
VimL
88 lines
2.7 KiB
VimL
" Run this plugin only if the denote package has been setup
|
|
if !exists('g:denote_directory')
|
|
finish
|
|
endif
|
|
|
|
" ... and if this filetype is specified as denote-note file
|
|
if index(g:denote_note_file_extensions, expand('%:e')) == -1
|
|
finish
|
|
endif
|
|
|
|
" ... and if the file is in the denote directory
|
|
if g:denote_directory != expand('%:p:h')
|
|
finish
|
|
endif
|
|
|
|
" Load only once per buffer
|
|
if exists('b:loaded_denote_ftplugin_notes')
|
|
finish
|
|
endif
|
|
let b:loaded_denote_ftplugin_notes = 1
|
|
|
|
" Link completion
|
|
" This works by using the functions s:column(), s:suggestions(),
|
|
" s:complete_link(), and by setting 'omnifunc'.
|
|
|
|
" Find the column where the completion starts. This must be between 1 and
|
|
" col('.'). Denote links are of this form: `denote:<identifier>`.
|
|
function DenoteNoteCompleteLinkColumn()
|
|
" Get the substring from the start of the line until col('.')
|
|
let l:x = max([1, col('.')-2])
|
|
let l:l = getline('.')[:l:x]
|
|
" Take the shortest prefix of a denote link. This may be any of
|
|
" \<d$
|
|
" ..
|
|
" \<denote:$
|
|
" \<denote:\f$
|
|
" \<denote:\f\f$
|
|
" etc.
|
|
let l:res = l:l->matchstrpos('\<denote:\f*$')
|
|
if l:res[1] >= 0
|
|
return l:res[1]
|
|
endif
|
|
let l:res = l:l->matchstrpos('\<d\f*$')
|
|
if l:res[1] == -1
|
|
return -3
|
|
endif
|
|
return 'denote:' =~ '^' .. l:res[0] ? l:res[1] : -3
|
|
endfunction
|
|
|
|
" Return completion items given by the base
|
|
function DenoteNoteCompleteLinkSuggestions(base)
|
|
let l:prefix = a:base->matchstr('^denote:\zs.*$')
|
|
let l:flist = glob(g:denote_directory .. '/' .. (l:prefix ? '*' .. l:prefix .. '*' : '*'), 0, v:true)
|
|
let l:res = []
|
|
for filename in l:flist
|
|
let l:meta = libdenote#scheme_metadata(filename)
|
|
if l:meta.id == v:false || (l:meta.id !~ '^' .. l:prefix && l:meta.title !~ l:prefix)
|
|
continue
|
|
endif
|
|
let l:meta.title = l:meta.title ?? '(no title)'
|
|
call add(l:res, {
|
|
\ 'word' : 'denote:' .. l:meta.id,
|
|
\ 'abbr' : l:meta.title,
|
|
\ 'menu' : l:meta.tags->join(', ')
|
|
\ })
|
|
endfor
|
|
return l:res
|
|
endfunction
|
|
|
|
" Completion function for denote links
|
|
function DenoteNoteCompleteLink(findstart, base)
|
|
return a:findstart == 1 ? DenoteNoteCompleteLinkColumn() : DenoteNoteCompleteLinkSuggestions(a:base)
|
|
endfunction
|
|
setlocal omnifunc=DenoteNoteCompleteLink
|
|
" Denote links are of the form 'denote:<note id>'; we require the column.
|
|
setlocal isfname+=:
|
|
" Set the function to resolve the filename under the cursor (see |gf|).
|
|
function DenoteNoteGotoFile()
|
|
return v:fname !~ '^denote:'
|
|
\ ? v:fname
|
|
\ : libdenote#scheme_find(g:denote_directory, v:fname[7:]) ?? v:fname
|
|
endfunction
|
|
setlocal includeexpr=DenoteNoteGotoFile()
|
|
|
|
" Back references command
|
|
let b:meta = libdenote#scheme_metadata(expand('%:t'))
|
|
exe 'command! -buffer DenoteBackReferences DenoteGrep /\<denote:' .. b:meta.id .. '\>/gj'
|