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.
function s:tagList(ArgLead, cmdLine, CursorPos)
let files=glob(g:denote_directory .. "/*", 0, v:true)
let tags=[]
for f in files
let tags=extend(tags, denote#meta#noteTagsFromFile(f))
let l:files=glob(g:denote_directory .. '/*', 0, v:true)
let l:tags=[]
for f in l:files
let l:tags=extend(l:tags, denote#meta#noteTagsFromFile(f))
endfor
return uniq(sort(tags))->join("\n")
return uniq(sort(l:tags))->join("\n")
endfunction

View File

@@ -2,7 +2,7 @@
" 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 = getline(".")[:col('.')]
let l:l = getline('.')[:col('.')]
" Take the shortest prefix of a denote link. This may be any of
" \<d$
" ..
@@ -10,37 +10,37 @@ function s:column()
" \<denote:\f$
" \<denote:\f\f$
" etc.
let res = l->matchstrpos('\<denote:\f*$')
if res[1] >= 0
return res[1]
let l:res = l:l->matchstrpos('\<denote:\f*$')
if l:res[1] >= 0
return l:res[1]
endif
let res = l->matchstrpos('\<d\f*$')
if res[1] == -1
let l:res = l:l->matchstrpos('\<d\f*$')
if l:res[1] == -1
return -3
endif
return 'denote:' =~ '^' .. res[0] ? res[1] : -3
return 'denote:' =~ '^' .. l:res[0] ? l:res[1] : -3
endfunction
" Return completion items given by the base
function s:suggestions(base)
let prefix = a:base->matchstr('^denote:\zs.*$')
let flist = glob(g:denote_directory .. "/" .. (prefix ? "*" .. prefix .. "*" : "*"), 0, v:true)
let res = []
for filename in flist
let noteId = denote#meta#noteIdFromFile(filename)
let noteTitle = denote#meta#noteTitleFromFile(filename)
if noteId == v:false || (noteId !~ '^' .. prefix && noteTitle !~ prefix)
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:noteId = denote#meta#noteIdFromFile(filename)
let l:noteTitle = denote#meta#noteTitleFromFile(filename)
if l:noteId == v:false || (l:noteId !~ '^' .. l:prefix && l:noteTitle !~ l:prefix)
continue
endif
let noteTitle = noteTitle ?? '(no title)'
let noteTags = denote#meta#noteTagsFromFile(filename)
call add(res, {
\ 'word' : 'denote:' .. noteId,
\ 'abbr' : noteTitle,
\ 'menu' : noteTags->join(', ')
let l:noteTitle = l:noteTitle ?? '(no title)'
let l:noteTags = denote#meta#noteTagsFromFile(filename)
call add(l:res, {
\ 'word' : 'denote:' .. l:noteId,
\ 'abbr' : l:noteTitle,
\ 'menu' : l:noteTags->join(', ')
\ })
endfor
return res
return l:res
endfunction
" 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
" quotes.
function s:escapeDQ(s)
@@ -10,7 +10,7 @@ function s:md_new_yaml(id, title, tags)
return [
\ '---',
\ '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(', ') .. ']',
\ 'identifier: ' .. s:escapeDQ(a:id),
\ '---'
@@ -22,7 +22,7 @@ function s:md_new_toml(id, title, tags)
return [
\ '+++',
\ '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(', ') .. ']',
\ 'identifier ' .. s:escapeDQ(a:id),
\ '+++'
@@ -33,7 +33,7 @@ endfunction
function s:plain_new(id, title, tags)
return [
\ 'title: ' .. a:title,
\ 'date: ' .. strftime("%F"),
\ 'date: ' .. strftime('%F'),
\ 'tags: ' .. a:tags->join(' '),
\ 'identifier: ' .. a:id,
\ '---------------------------',
@@ -44,7 +44,7 @@ endfunction
function s:org_new(id, title, tags)
return [
\ '#+title: ' .. a:title,
\ '#+date: [' .. strftime("%F %a %R") .. ']',
\ '#+date: [' .. strftime('%F %a %R') .. ']',
\ '#+filetags: :' .. map(copy(a:tags), {_, t -> substitute(t, ':', '', 'g') })->join(':') .. ':',
\ '#+identifier: ' .. a:id,
\ ]

View File

@@ -2,7 +2,7 @@
" This resolves denote links. The function has access to the variable v:fname,
" which corresponds to the filename under the cursor.
function s:gotofile()
return v:fname !~ "^denote:"
return v:fname !~ '^denote:'
\ ? v:fname
\ : denote#meta#fileFromNoteId(v:fname[7:]) ?? v:fname
endfunction
@@ -19,7 +19,7 @@ function denote#ft#denote()
" Set the function to resolve the filename under the cursor (see |gf|).
setlocal includeexpr=s:gotofile()
" 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'
endfunction
@@ -32,7 +32,6 @@ function denote#ft#qf()
" Clear settings
set spell<
nmapclear <buffer>
" nunmap <buffer> q
return
endif
setlocal nospell

View File

@@ -5,10 +5,10 @@ endfunction
" Local helper function to retrieve and format the title.
function s:titleFromBuf(buf)
let name=denote#meta#noteTitleFromFile(bufname(a:buf))
return strchars(name, 1) <= g:denote_loc_title_columns
\ ? printf('%' .. g:denote_loc_title_columns .. 's', name)
\ : printf('%.' .. (g:denote_loc_title_columns - 1) .. 's', name) .. '…'
let l:name=denote#meta#noteTitleFromFile(bufname(a:buf))
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
@@ -21,44 +21,32 @@ endfunction
" This modifies the location list for pretty display.
function denote#loclist#textReferences(info)
let items=getloclist(a:info.winid)
let l=[]
let width=winwidth(0) - g:denote_loc_title_columns - 19
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 e=items[idx]
let name=s:titleFromBuf(e.bufnr)
let lnum=printf('%5d', e.lnum)
let col=printf('%3d', e.col)
call add(l, name ..
\ ' | ' .. lnum .. ' col ' .. col ..
\ ' | ' .. s:formatText(items[idx].text, col, width))
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
return l:l
endfunction
" This modifies the location list for pretty display.
function denote#loclist#textNoteList(info)
let items=getloclist(a:info.winid)
let l=[]
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 e=items[idx]
let name=s:titleFromBuf(e.bufnr)
let ntags=denote#meta#noteTagsFromFile(bufname(e.bufnr))->join()
call add(l, name .. ' | ' .. ntags)
let l:e=l:items[idx]
let l:name=s:titleFromBuf(l:e.bufnr)
let l:ntags=denote#meta#noteTagsFromFile(bufname(l:e.bufnr))->join()
call add(l:l, l:name .. ' | ' .. l:ntags)
endfor
return 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'})
return l:l
endfunction
" 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.
" (B) Then we ensure that the note id is followed by another field or by the
" 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 .. '\\|@@' .. a:noteId .. '"')
return empty(files) ? v:false : files[0]
return empty(l:files) ? v:false : l:files[0]
endfunction
" Return the note id from the filename. On failure, v:false is returned.
function denote#meta#noteIdFromFile(file)
return a:file->fnamemodify(':t')->matchstr("@@\\zs.\\{-\\}\\ze\\(==\\|--\\|__\\|\\..\\)")
\ ?? a:file->fnamemodify(':t')->matchstr("^.\\{-\\}\\ze\\(==\\|--\\|__\\|\\..\\)")
return a:file->fnamemodify(':t')->matchstr('@@\zs.\{-\}\ze\(==\|--\|__\|\..\)')
\ ?? a:file->fnamemodify(':t')->matchstr('^.\{-\}\ze\(==\|--\|__\|\..\)')
\ ?? v:false
endfunction
" Return the note title from the filename.
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
" Return the note tags from the filename as a list.
function denote#meta#noteTagsFromFile(file)
return a:file->fnamemodify(':t')->matchstr("__\\zs.\\{-\\}\\ze\\(==\\|@@\\|--\\|\\..\\)")->split("_")
return a:file->fnamemodify(':t')->matchstr('__\zs.\{-\}\ze\(==\|@@\|--\|\..\)')->split('_')
endfunction
" Identifier creation
function denote#meta#identifier_generate()
if g:denote_identifier_fun
return execute "call " .. g:denote_identifier_fun .. "()"
return execute 'call ' .. g:denote_identifier_fun .. '()'
endif
return exists("*strftime")
\ ? strftime("%Y%m%dT%H%M%S")
return exists('*strftime')
\ ? strftime('%Y%m%dT%H%M%S')
\ : rand()
endfunction

View File

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