code: cleaned up

This commit is contained in:
2026-02-25 16:11:35 +01:00
parent ba939661c7
commit 2015c0e32c
7 changed files with 76 additions and 89 deletions

View File

@@ -1,11 +1,11 @@
" This function is used to autocomplete the tags in user commands. " This function is used to autocomplete the tags in user commands.
function s:tagList(ArgLead, cmdLine, CursorPos) function s:tagList(ArgLead, cmdLine, CursorPos)
let files=glob(g:denote_directory .. "/*", 0, v:true) let l:files=glob(g:denote_directory .. '/*', 0, v:true)
let tags=[] let l:tags=[]
for f in files for f in l:files
let tags=extend(tags, denote#meta#noteTagsFromFile(f)) let l:tags=extend(l:tags, denote#meta#noteTagsFromFile(f))
endfor endfor
return uniq(sort(tags))->join("\n") return uniq(sort(l:tags))->join("\n")
endfunction endfunction

View File

@@ -2,7 +2,7 @@
" col('.'). Denote links are of this form: `denote:<identifier>`. " col('.'). Denote links are of this form: `denote:<identifier>`.
function s:column() function s:column()
" Get the substring from the start of the line until col('.') " Get the substring from the start of the line until col('.')
let l = getline(".")[:col('.')] let l:l = getline('.')[:col('.')]
" Take the shortest prefix of a denote link. This may be any of " Take the shortest prefix of a denote link. This may be any of
" \<d$ " \<d$
" .. " ..
@@ -10,37 +10,37 @@ function s:column()
" \<denote:\f$ " \<denote:\f$
" \<denote:\f\f$ " \<denote:\f\f$
" etc. " etc.
let res = l->matchstrpos('\<denote:\f*$') let l:res = l:l->matchstrpos('\<denote:\f*$')
if res[1] >= 0 if l:res[1] >= 0
return res[1] return l:res[1]
endif endif
let res = l->matchstrpos('\<d\f*$') let l:res = l:l->matchstrpos('\<d\f*$')
if res[1] == -1 if l:res[1] == -1
return -3 return -3
endif endif
return 'denote:' =~ '^' .. res[0] ? res[1] : -3 return 'denote:' =~ '^' .. l:res[0] ? l:res[1] : -3
endfunction endfunction
" Return completion items given by the base " Return completion items given by the base
function s:suggestions(base) function s:suggestions(base)
let prefix = a:base->matchstr('^denote:\zs.*$') let l:prefix = a:base->matchstr('^denote:\zs.*$')
let flist = glob(g:denote_directory .. "/" .. (prefix ? "*" .. prefix .. "*" : "*"), 0, v:true) let l:flist = glob(g:denote_directory .. '/' .. (l:prefix ? '*' .. l:prefix .. '*' : '*'), 0, v:true)
let res = [] let l:res = []
for filename in flist for filename in l:flist
let noteId = denote#meta#noteIdFromFile(filename) let l:noteId = denote#meta#noteIdFromFile(filename)
let noteTitle = denote#meta#noteTitleFromFile(filename) let l:noteTitle = denote#meta#noteTitleFromFile(filename)
if noteId == v:false || (noteId !~ '^' .. prefix && noteTitle !~ prefix) if l:noteId == v:false || (l:noteId !~ '^' .. l:prefix && l:noteTitle !~ l:prefix)
continue continue
endif endif
let noteTitle = noteTitle ?? '(no title)' let l:noteTitle = l:noteTitle ?? '(no title)'
let noteTags = denote#meta#noteTagsFromFile(filename) let l:noteTags = denote#meta#noteTagsFromFile(filename)
call add(res, { call add(l:res, {
\ 'word' : 'denote:' .. noteId, \ 'word' : 'denote:' .. l:noteId,
\ 'abbr' : noteTitle, \ 'abbr' : l:noteTitle,
\ 'menu' : noteTags->join(', ') \ 'menu' : l:noteTags->join(', ')
\ }) \ })
endfor endfor
return res return l:res
endfunction endfunction
" Completion function for denote links " Completion function for denote links

View File

@@ -1,4 +1,4 @@
" Functions to create the front matter {{{1 " Functions to create the front matter
" Helper function to put string in double quotes, and remove inside double " Helper function to put string in double quotes, and remove inside double
" quotes. " quotes.
function s:escapeDQ(s) function s:escapeDQ(s)
@@ -10,7 +10,7 @@ function s:md_new_yaml(id, title, tags)
return [ return [
\ '---', \ '---',
\ 'title: ' .. s:escapeDQ(a:title), \ 'title: ' .. s:escapeDQ(a:title),
\ 'date: ' .. strftime("%FT%T%z"), \ 'date: ' .. strftime('%FT%T%z'),
\ 'tags: [' .. map(copy(a:tags), {_, t -> s:escapeDQ(t) })->join(', ') .. ']', \ 'tags: [' .. map(copy(a:tags), {_, t -> s:escapeDQ(t) })->join(', ') .. ']',
\ 'identifier: ' .. s:escapeDQ(a:id), \ 'identifier: ' .. s:escapeDQ(a:id),
\ '---' \ '---'
@@ -22,7 +22,7 @@ function s:md_new_toml(id, title, tags)
return [ return [
\ '+++', \ '+++',
\ 'title ' .. s:escapeDQ(a:title), \ 'title ' .. s:escapeDQ(a:title),
\ 'date ' .. strftime("%FT%T%z"), \ 'date ' .. strftime('%FT%T%z'),
\ 'tags [' .. map(copy(a:tags), {_, t -> s:escapeDQ(t) })->join(', ') .. ']', \ 'tags [' .. map(copy(a:tags), {_, t -> s:escapeDQ(t) })->join(', ') .. ']',
\ 'identifier ' .. s:escapeDQ(a:id), \ 'identifier ' .. s:escapeDQ(a:id),
\ '+++' \ '+++'
@@ -33,7 +33,7 @@ endfunction
function s:plain_new(id, title, tags) function s:plain_new(id, title, tags)
return [ return [
\ 'title: ' .. a:title, \ 'title: ' .. a:title,
\ 'date: ' .. strftime("%F"), \ 'date: ' .. strftime('%F'),
\ 'tags: ' .. a:tags->join(' '), \ 'tags: ' .. a:tags->join(' '),
\ 'identifier: ' .. a:id, \ 'identifier: ' .. a:id,
\ '---------------------------', \ '---------------------------',
@@ -44,7 +44,7 @@ endfunction
function s:org_new(id, title, tags) function s:org_new(id, title, tags)
return [ return [
\ '#+title: ' .. a:title, \ '#+title: ' .. a:title,
\ '#+date: [' .. strftime("%F %a %R") .. ']', \ '#+date: [' .. strftime('%F %a %R') .. ']',
\ '#+filetags: :' .. map(copy(a:tags), {_, t -> substitute(t, ':', '', 'g') })->join(':') .. ':', \ '#+filetags: :' .. map(copy(a:tags), {_, t -> substitute(t, ':', '', 'g') })->join(':') .. ':',
\ '#+identifier: ' .. a:id, \ '#+identifier: ' .. a:id,
\ ] \ ]

View File

@@ -2,7 +2,7 @@
" This resolves denote links. The function has access to the variable v:fname, " This resolves denote links. The function has access to the variable v:fname,
" which corresponds to the filename under the cursor. " which corresponds to the filename under the cursor.
function s:gotofile() function s:gotofile()
return v:fname !~ "^denote:" return v:fname !~ '^denote:'
\ ? v:fname \ ? v:fname
\ : denote#meta#fileFromNoteId(v:fname[7:]) ?? v:fname \ : denote#meta#fileFromNoteId(v:fname[7:]) ?? v:fname
endfunction endfunction
@@ -19,7 +19,7 @@ function denote#ft#denote()
" Set the function to resolve the filename under the cursor (see |gf|). " Set the function to resolve the filename under the cursor (see |gf|).
setlocal includeexpr=s:gotofile() setlocal includeexpr=s:gotofile()
" Back references command " Back references command
let l:noteid = denote#meta#noteIdFromFile(expand("%:t")) let l:noteid = denote#meta#noteIdFromFile(expand('%:t'))
exe 'command! DenoteBackReferences DenoteGrep /\<denote:' .. l:noteid .. '\>/gj' exe 'command! DenoteBackReferences DenoteGrep /\<denote:' .. l:noteid .. '\>/gj'
endfunction endfunction
@@ -32,7 +32,6 @@ function denote#ft#qf()
" Clear settings " Clear settings
set spell< set spell<
nmapclear <buffer> nmapclear <buffer>
" nunmap <buffer> q
return return
endif endif
setlocal nospell setlocal nospell

View File

@@ -5,10 +5,10 @@ endfunction
" Local helper function to retrieve and format the title. " Local helper function to retrieve and format the title.
function s:titleFromBuf(buf) function s:titleFromBuf(buf)
let name=denote#meta#noteTitleFromFile(bufname(a:buf)) let l:name=denote#meta#noteTitleFromFile(bufname(a:buf))
return strchars(name, 1) <= g:denote_loc_title_columns return strchars(l:name, 1) <= g:denote_loc_title_columns
\ ? printf('%' .. g:denote_loc_title_columns .. 's', name) \ ? printf('%' .. g:denote_loc_title_columns .. 's', l:name)
\ : printf('%.' .. (g:denote_loc_title_columns - 1) .. 's', name) .. '…' \ : printf('%.' .. (g:denote_loc_title_columns - 1) .. 's', l:name) .. '…'
endfunction endfunction
" Local helper function to truncate text with match at the given column as a " Local helper function to truncate text with match at the given column as a
@@ -21,44 +21,32 @@ endfunction
" This modifies the location list for pretty display. " This modifies the location list for pretty display.
function denote#loclist#textReferences(info) function denote#loclist#textReferences(info)
let items=getloclist(a:info.winid) let l:items=getloclist(a:info.winid)
let l=[] let l:l=[]
let width=winwidth(0) - g:denote_loc_title_columns - 19 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) for idx in range(a:info.start_idx - 1, a:info.end_idx - 1)
let e=items[idx] let l:e=l:items[idx]
let name=s:titleFromBuf(e.bufnr) let l:name=s:titleFromBuf(l:e.bufnr)
let lnum=printf('%5d', e.lnum) let l:lnum=printf('%5d', l:e.lnum)
let col=printf('%3d', e.col) let l:col=printf('%3d', l:e.col)
call add(l, name .. call add(l:l, l:name ..
\ ' | ' .. lnum .. ' col ' .. col .. \ ' | ' .. l:lnum .. ' l:col ' .. l:col ..
\ ' | ' .. s:formatText(items[idx].text, col, width)) \ ' | ' .. s:formatText(l:items[idx].text, l:col, l:width))
endfor endfor
return l return l:l
endfunction endfunction
" This modifies the location list for pretty display. " This modifies the location list for pretty display.
function denote#loclist#textNoteList(info) function denote#loclist#textNoteList(info)
let items=getloclist(a:info.winid) let l:items=getloclist(a:info.winid)
let l=[] let l:l=[]
for idx in range(a:info.start_idx - 1, a:info.end_idx - 1) for idx in range(a:info.start_idx - 1, a:info.end_idx - 1)
let e=items[idx] let l:e=l:items[idx]
let name=s:titleFromBuf(e.bufnr) let l:name=s:titleFromBuf(l:e.bufnr)
let ntags=denote#meta#noteTagsFromFile(bufname(e.bufnr))->join() let l:ntags=denote#meta#noteTagsFromFile(bufname(l:e.bufnr))->join()
call add(l, name .. ' | ' .. ntags) call add(l:l, l:name .. ' | ' .. l:ntags)
endfor endfor
return l return l:l
endfunction
" Load all references to the given note into the location list.
function denote#loclist#references(noteId)
" Populate location list
silent! execute "lvimgrep /\\<denote:" .. a:noteId .. "\\>/gj " .. g:denote_directory .. "/*"
" Adjust location list: set title and specify display function
let file=denote#meta#fileFromNoteId(a:noteId)
let noteTitle=denote#meta#noteTitleFromFile(file)
call setloclist(0, [], 'r',
\ {'title': 'References to ' .. noteTitle .. ' (' .. a:noteId .. ')',
\ 'quickfixtextfunc' : 'denote#loclist#textReferences'})
endfunction endfunction
" Re-populate location list with denote entries " Re-populate location list with denote entries

View File

@@ -10,35 +10,35 @@ function denote#meta#fileFromNoteId(noteId)
" (A) First, we get all files that contain the note id as substring. " (A) First, we get all files that contain the note id as substring.
" (B) Then we ensure that the note id is followed by another field or by the " (B) Then we ensure that the note id is followed by another field or by the
" file extension. " file extension.
let files = glob(g:denote_directory .. "/*" .. a:noteId .. "*", 0, v:true) let l:files = glob(g:denote_directory .. '/*' .. a:noteId .. '*', 0, v:true)
\ ->filter('v:val->split("/")[-1] =~ "' .. a:noteId .. '\\(==\\|--\\|__\\|\\.\\)"') \ ->filter('v:val->split("/")[-1] =~ "' .. a:noteId .. '\\(==\\|--\\|__\\|\\.\\)"')
\ ->filter('v:val->split("/")[-1] =~ "^' .. a:noteId .. '\\|@@' .. a:noteId .. '"') \ ->filter('v:val->split("/")[-1] =~ "^' .. a:noteId .. '\\|@@' .. a:noteId .. '"')
return empty(files) ? v:false : files[0] return empty(l:files) ? v:false : l:files[0]
endfunction endfunction
" Return the note id from the filename. On failure, v:false is returned. " Return the note id from the filename. On failure, v:false is returned.
function denote#meta#noteIdFromFile(file) function denote#meta#noteIdFromFile(file)
return a:file->fnamemodify(':t')->matchstr("@@\\zs.\\{-\\}\\ze\\(==\\|--\\|__\\|\\..\\)") return a:file->fnamemodify(':t')->matchstr('@@\zs.\{-\}\ze\(==\|--\|__\|\..\)')
\ ?? a:file->fnamemodify(':t')->matchstr("^.\\{-\\}\\ze\\(==\\|--\\|__\\|\\..\\)") \ ?? a:file->fnamemodify(':t')->matchstr('^.\{-\}\ze\(==\|--\|__\|\..\)')
\ ?? v:false \ ?? v:false
endfunction endfunction
" Return the note title from the filename. " Return the note title from the filename.
function denote#meta#noteTitleFromFile(file) function denote#meta#noteTitleFromFile(file)
return a:file->fnamemodify(':t')->matchstr("--\\zs.\\{-\\}\\ze\\(==\\|@@\\|__\\|\\..\\)")->substitute("-", " ", "g") return a:file->fnamemodify(':t')->matchstr('--\zs.\{-\}\ze\(==\|@@\|__\|\..\)')->substitute('-', ' ', 'g')
endfunction endfunction
" Return the note tags from the filename as a list. " Return the note tags from the filename as a list.
function denote#meta#noteTagsFromFile(file) function denote#meta#noteTagsFromFile(file)
return a:file->fnamemodify(':t')->matchstr("__\\zs.\\{-\\}\\ze\\(==\\|@@\\|--\\|\\..\\)")->split("_") return a:file->fnamemodify(':t')->matchstr('__\zs.\{-\}\ze\(==\|@@\|--\|\..\)')->split('_')
endfunction endfunction
" Identifier creation " Identifier creation
function denote#meta#identifier_generate() function denote#meta#identifier_generate()
if g:denote_identifier_fun if g:denote_identifier_fun
return execute "call " .. g:denote_identifier_fun .. "()" return execute 'call ' .. g:denote_identifier_fun .. '()'
endif endif
return exists("*strftime") return exists('*strftime')
\ ? strftime("%Y%m%dT%H%M%S") \ ? strftime('%Y%m%dT%H%M%S')
\ : rand() \ : rand()
endfunction endfunction

View File

@@ -1,33 +1,33 @@
" Put all notes of the given tag to the location list. The search argument may be " Put all notes of the given tag to the location list. The search argument may be
" empty. For improving search, white spaces are replaced by the * |wildcard|. " empty. For improving search, white spaces are replaced by the * |wildcard|.
function denote#notes#list(search) function denote#notes#list(search)
let l:s = substitute(" " .. a:search .. " ", " ", "*", "g") let l:s = substitute(' ' .. a:search .. ' ', ' ', '*', 'g')
let l:files = glob(g:denote_directory .. "/" .. l:s, 0, v:true) let l:files = glob(g:denote_directory .. '/' .. l:s, 0, v:true)
let l:title = "Denote notes search:" .. a:search let l:title = 'Denote notes search:' .. a:search
call denote#loclist#fill(l:title, l:files) call denote#loclist#fill(l:title, l:files)
endfunction endfunction
" Put all notes of the given tag to the location list. The tag argument is " Put all notes of the given tag to the location list. The tag argument is
" mandatory. " mandatory.
function denote#notes#bytag(tag) function denote#notes#bytag(tag)
let files = glob(g:denote_directory .. "/*_" .. a:tag .. "*", 0, v:true)->filter('v:val->split("/")[-1] =~ "_' .. a:tag .. '\\(==\\|@@\\|__\\|_\\|\\.\\)"') let l:files = glob(g:denote_directory .. '/*_' .. a:tag .. '*', 0, v:true)->filter('v:val->split("/")[-1] =~ "_' .. a:tag .. '\\(==\\|@@\\|__\\|_\\|\\.\\)"')
let l:title = "Denote notes: " .. a:tag let l:title = 'Denote notes: ' .. a:tag
call denote#loclist#fill(l:title, l:files) call denote#loclist#fill(l:title, l:files)
endfunction endfunction
" Search in denote notes " Search in denote notes
function denote#notes#grep(re) function denote#notes#grep(re)
let l:title = "Grep results for: " .. a:re let l:title = 'Grep results for: ' .. a:re
let fpat=map(copy(g:denote_note_file_extensions), {_, e -> g:denote_directory .. "/*." .. e})->join() let l:fpat=map(copy(g:denote_note_file_extensions), {_, e -> g:denote_directory .. '/*.' .. e})->join()
execute "silent! lvimgrep " .. a:re .. " " .. fpat execute 'silent! lvimgrep ' .. a:re .. ' ' .. l:fpat
call denote#loclist#setgrep(l:title) call denote#loclist#setgrep(l:title)
endfunction endfunction
" This creates a new denote entry with the given title and of the given " This creates a new denote entry with the given title and of the given
" filetype. The title may be empty. " filetype. The title may be empty.
function denote#notes#new(title, ft=g:denote_new_ft) function denote#notes#new(title, ft=g:denote_new_ft)
let identifier=denote#meta#identifier_generate() let l:identifier=denote#meta#identifier_generate()
let fn=identifier .. '--' .. a:title let l:fn=l:identifier .. '--' .. a:title
\ ->tolower() \ ->tolower()
\ ->substitute('[^[:fname:]]\|/', '-', 'g') \ ->substitute('[^[:fname:]]\|/', '-', 'g')
\ ->substitute('-\+', '-', 'g') \ ->substitute('-\+', '-', 'g')
@@ -35,6 +35,6 @@ function denote#notes#new(title, ft=g:denote_new_ft)
\ ->substitute('=\+', '=', 'g') \ ->substitute('=\+', '=', 'g')
\ ->substitute('@\+', '@', 'g') \ ->substitute('@\+', '@', 'g')
\ ->trim('-_@=') .. '.' .. a:ft \ ->trim('-_@=') .. '.' .. a:ft
execute "edit " .. g:denote_directory .. "/" .. fn execute 'edit ' .. g:denote_directory .. '/' .. l:fn
call setline(1, denote#frontmatter#new(a:ft, identifier, a:title)) call setline(1, denote#frontmatter#new(a:ft, l:identifier, a:title))
endfunction endfunction