add and remove tags

This commit is contained in:
2026-02-28 14:21:39 +01:00
parent 55bbeecb0a
commit 5d631a5506
4 changed files with 68 additions and 7 deletions

View File

@@ -76,3 +76,12 @@ function denote#frontmatter#setTitle(filename, title)
call map(l:frontmatter, { _, v -> substitute(v, '^#\?+\?title:\?\s*"\?\zs.\{-\}\ze\"\?$', a:title, '')})
call setbufline(a:filename, 1, l:frontmatter)
endfunction
" Set tags in front matter
function denote#frontmatter#setTags(filename, tags)
let l:frontmatter = s:getFrontmatter(a:filename)
let l:tagline = denote#frontmatter#new(fnamemodify(a:filename, ':t'), '', '', a:tags)
\ ->filter('v:val =~ "^\\(#+file\\)\\?tags"')[0]
call map(l:frontmatter, { _, v -> v =~ '^\(#+file\)\?tags' ? l:tagline : v})
call setbufline(a:filename, 1, l:frontmatter)
endfunction

View File

@@ -43,5 +43,7 @@ function denote#ft#qf()
" Denote-list specific configuration
if l:context['denote'] == 'list'
command! -nargs=1 -range -buffer DenoteSetTitle :call denote#notes#settitle(<line1>, <q-args>)
command! -nargs=1 -range -buffer DenoteTagAdd :call denote#notes#tagmod(<line1>, <line2>, <q-args>, v:true)
command! -nargs=1 -range -buffer DenoteTagRm :call denote#notes#tagmod(<line1>, <line2>, <q-args>, v:false)
endif
endfunction

View File

@@ -81,9 +81,9 @@ endfunction
function denote#loclist#reload()
let l:context = getloclist(0, {'context': 1})['context']
if has_key(l:context, 'denote') && has_key(l:context, 'gfun')
execute ':lclose'
exe 'lclose'
call l:context['gfun']()
execute ':lwindow'
exe 'lwindow'
endif
endfunction

View File

@@ -21,7 +21,7 @@ endfunction
function denote#notes#grep(re)
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
exe 'silent! lvimgrep ' .. a:re .. ' ' .. l:fpat
let l:Gfun = function('denote#notes#grep', [a:re])
call denote#loclist#setgrep(l:title, l:Gfun)
endfunction
@@ -34,7 +34,7 @@ function denote#notes#new(title, ft=g:denote_new_ft)
" Jump to window this location list belongs to
call denote#loclist#jumptowindow()
" Open file and write front matter
execute 'edit ' l:fn
exe 'edit ' l:fn
call setline(1, denote#frontmatter#new(a:ft, l:identifier, a:title))
endfunction
@@ -64,7 +64,9 @@ function denote#notes#settitle(linenr, title)
exe 'silent file ' .. l:newfilename
exe 'silent w'
exe 'lopen'
if fnamemodify(l:filename, ':t') != fnamemodify(l:newfilename, ':t')
call delete(l:filename)
endif
else
if fnamemodify(l:filename, ':t') == fnamemodify(l:newfilename, ':t')
return
@@ -72,3 +74,51 @@ function denote#notes#settitle(linenr, title)
call rename(l:filename, l:newfilename)
endif
endfunction
" Function to add or remove a tag to the selected entries. The last argument
" a:add, is set to v:true to add, and v:false to remove.
function denote#notes#tagmod(line1, line2, tag, add)
" Get file first!
let l:items = getloclist(0, {'items': 1})['items']
if empty(l:items)
return
endif
for i in range(a:line1, a:line2)
let l:item = l:items[i-1]
let l:bufnr = l:item['bufnr']
let l:filename = bufname(l:bufnr)
let l:noteid = denote#meta#noteIdFromFile(l:filename)
let l:notetags = denote#meta#noteTagsFromFile(l:filename)
let l:idx = index(l:notetags, a:tag)
if a:add
if l:idx >= 0
continue
endif
call add(l:notetags, a:tag)
else
if l:idx == -1
continue
endif
call remove(l:notetags, a:tag)
endif
let l:notesignature = denote#meta#noteSignatureFromFile(l:filename)
let l:title = denote#meta#noteTitleFromFile(l:filename)
let l:ext = fnamemodify(l:filename, ':e')
let l:newfilename = denote#meta#filename(l:ext, l:noteid, l:title, l:notetags, l:notesignature)
if index(g:denote_note_file_extensions, l:ext) >= 0
" Handle front matter
call denote#frontmatter#setTags(l:filename, l:notetags)
call denote#loclist#jumptowindow()
exe l:bufnr .. 'buf'
exe 'silent file ' .. l:newfilename
exe 'silent w'
exe 'lopen'
call delete(l:filename)
else
if fnamemodify(l:filename, ':t') == fnamemodify(l:newfilename, ':t')
return
endif
call rename(l:filename, l:newfilename)
endif
endfor
endfunction