cleaned
This commit is contained in:
87
after/ftplugin/markdown.vim
Normal file
87
after/ftplugin/markdown.vim
Normal file
@@ -0,0 +1,87 @@
|
||||
" 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 s:column()
|
||||
" 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 s:suggestions(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 s:complete_link(findstart, base)
|
||||
return a:findstart == 1 ? s:column() : s:suggestions(a:base)
|
||||
endfunction
|
||||
setlocal omnifunc=s:complete_link()
|
||||
" 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 s:gotofile()
|
||||
return v:fname !~ '^denote:'
|
||||
\ ? v:fname
|
||||
\ : libdenote#scheme_find(g:denote_directory, v:fname[7:]) ?? v:fname
|
||||
endfunction
|
||||
setlocal includeexpr=s:gotofile()
|
||||
|
||||
" Back references command
|
||||
let b:meta = libdenote#scheme_metadata(expand('%:t'))
|
||||
exe 'command! -buffer DenoteBackReferences DenoteGrep /\<denote:' .. b:meta.id .. '\>/gj'
|
||||
Reference in New Issue
Block a user