From 5d631a5506b2625536eeeefc630014d04feac04e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=84min=20Baumeler?= Date: Sat, 28 Feb 2026 14:21:39 +0100 Subject: [PATCH] add and remove tags --- autoload/denote/frontmatter.vim | 11 ++++++- autoload/denote/ft.vim | 2 ++ autoload/denote/loclist.vim | 4 +-- autoload/denote/notes.vim | 58 ++++++++++++++++++++++++++++++--- 4 files changed, 68 insertions(+), 7 deletions(-) diff --git a/autoload/denote/frontmatter.vim b/autoload/denote/frontmatter.vim index fcd10e2..73ae334 100644 --- a/autoload/denote/frontmatter.vim +++ b/autoload/denote/frontmatter.vim @@ -35,7 +35,7 @@ function s:plain_new(id, title, tags) return [ \ 'title: ' .. a:title, \ 'date: ' .. strftime('%F'), - \ 'tags: ' .. a:tags->join(' '), + \ 'tags: ' .. a:tags->join(' '), \ 'identifier: ' .. a:id, \ '---------------------------', \ ] @@ -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 diff --git a/autoload/denote/ft.vim b/autoload/denote/ft.vim index da42768..09599f8 100644 --- a/autoload/denote/ft.vim +++ b/autoload/denote/ft.vim @@ -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(, ) + command! -nargs=1 -range -buffer DenoteTagAdd :call denote#notes#tagmod(, , , v:true) + command! -nargs=1 -range -buffer DenoteTagRm :call denote#notes#tagmod(, , , v:false) endif endfunction diff --git a/autoload/denote/loclist.vim b/autoload/denote/loclist.vim index 4609517..e8a7883 100644 --- a/autoload/denote/loclist.vim +++ b/autoload/denote/loclist.vim @@ -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 diff --git a/autoload/denote/notes.vim b/autoload/denote/notes.vim index 886e0ad..02c8e37 100644 --- a/autoload/denote/notes.vim +++ b/autoload/denote/notes.vim @@ -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 @@ -51,7 +51,7 @@ function denote#notes#settitle(linenr, title) let l:noteid = denote#meta#noteIdFromFile(l:filename) let l:notetags = denote#meta#noteTagsFromFile(l:filename) let l:notesignature = denote#meta#noteSignatureFromFile(l:filename) - let l:oldtitle = denote#meta#noteTitleFromFile (l:filename) + let l:oldtitle = denote#meta#noteTitleFromFile(l:filename) let l:ext = fnamemodify(l:filename, ':e') let l:newfilename = denote#meta#filename(l:ext, l:noteid, a:title, l:notetags, l:notesignature) " If this note has a front matter, we rewrite the front matter and rename the @@ -64,7 +64,9 @@ function denote#notes#settitle(linenr, title) exe 'silent file ' .. l:newfilename exe 'silent w' exe 'lopen' - call delete(l:filename) + 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