Files
vim-denote/autoload/denote/notes.vim
2026-02-28 15:16:47 +01:00

171 lines
5.9 KiB
VimL

" 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:Gfun = function('denote#notes#list', [a:search])
call denote#loclist#fill(l:title, l:files, l:Gfun)
endfunction
" Put all notes of the given tag to the location list. The tag argument is
" mandatory.
function denote#notes#bytag(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:Gfun = function('denote#notes#bytag', [a:tag])
call denote#loclist#fill(l:title, l:files, l:Gfun)
endfunction
" Search in denote notes
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()
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
" 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 l:identifier = g:Denote_identifier_fun()
let l:fn = denote#meta#filename(a:ft, l:identifier, a:title)
" Jump to window this location list belongs to
call denote#loclist#jumptowindow()
" Open file and write front matter
exe 'edit ' l:fn
call setline(1, denote#frontmatter#new(a:ft, l:identifier, a:title))
endfunction
" Function to set the title of the selected entry
function denote#notes#settitle(linenr, title)
" Get file first!
let l:items = getloclist(0, {'items': 1})['items']
if empty(l:items)
return
endif
let l:item = l:items[a:linenr-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:notesignature = denote#meta#noteSignatureFromFile(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
" file. Otherwise, we rename the file only.
if index(g:denote_note_file_extensions, l:ext) >= 0
" Handle front matter
call denote#frontmatter#setTitle(l:filename, a:title)
call denote#loclist#jumptowindow()
exe l:bufnr .. 'buf'
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
endif
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, l:idx)
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
" Add file to denote directory
function denote#notes#copy(origfile)
if !filereadable(a:origfile)
echohl WarningMsg
echom 'Cannot copy specified file to denote directory.'
return
endif
" Derive title from origfile
let l:title = fnamemodify(a:origfile, ':t:r')
let l:ext = fnamemodify(a:origfile, ':e')
let l:identifier = g:Denote_identifier_fun()
let l:filename = denote#meta#filename(l:ext, l:identifier, l:title)
call system('cp ' .. shellescape(a:origfile) .. ' ' .. shellescape(l:filename))
" Write front matter, if this is supported
if index(g:denote_note_file_extensions, l:ext) >= 0
call denote#loclist#jumptowindow()
exe 'edit ' .. l:filename
call denote#frontmatter#prepend(l:filename, l:identifier, l:title)
exe 'w'
endif
endfunction
" Delete notes from denote directory
function denote#notes#rm(line1, line2)
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:title = denote#meta#noteTitleFromFile(l:filename)
let l:answer = confirm('Are you sure you want to delete the note "' .. l:title .. '"?', "&Yes\n&No\n", 2, 'Question')
if l:answer != 1
continue
endif
" Wipe buffer, if it exists
if bufexists(l:filename)
exe 'silent bwipe ' .. fnameescape(l:filename)
endif
" Delete file
call delete(l:filename)
endfor
endfunction