diff --git a/autoload/denote/frontmatter.vim b/autoload/denote/frontmatter.vim new file mode 100644 index 0000000..78d7860 --- /dev/null +++ b/autoload/denote/frontmatter.vim @@ -0,0 +1,59 @@ +" Functions to create the front matter {{{1 +" Helper function to put string in double quotes, and remove inside double +" quotes. +function s:escapeDQ(s) + return '"' .. substitute(a:s, '"', '', 'g') .. '"' +endfunction + +" Create front matter (yaml) +function s:md_new_yaml(id, title, tags) + return [ + \ '---', + \ 'title: ' .. s:escapeDQ(a:title), + \ 'date: ' .. strftime("%FT%T%z"), + \ 'tags: [' .. map(copy(a:tags), {_, t -> s:escapeDQ(t) })->join(', ') .. ']', + \ 'identifier: ' .. s:escapeDQ(a:id), + \ '---' + \ ] +endfunction + +" Create front matter (toml) +function s:md_new_toml(id, title, tags) + return [ + \ '+++', + \ 'title ' .. s:escapeDQ(a:title), + \ 'date ' .. strftime("%FT%T%z"), + \ 'tags [' .. map(copy(a:tags), {_, t -> s:escapeDQ(t) })->join(', ') .. ']', + \ 'identifier ' .. s:escapeDQ(a:id), + \ '+++' + \ ] +endfunction + +" Create front matter (plain) +function s:plain_new(id, title, tags) + return [ + \ 'title: ' .. a:title, + \ 'date: ' .. strftime("%F"), + \ 'tags: ' .. a:tags->join(' '), + \ 'identifier: ' .. a:id, + \ '---------------------------', + \ ] +endfunction + +" Create front matter (org) +function s:org_new(id, title, tags) + return [ + \ '#+title: ' .. a:title, + \ '#+date: [' .. strftime("%F %a %R") .. ']', + \ '#+filetags: :' .. map(copy(a:tags), {_, t -> substitute(t, ':', '', 'g') })->join(':') .. ':', + \ '#+identifier: ' .. a:id, + \ ] +endfunction + +" Create front matter +function denote#frontmatter#new(ft, id, title, tags=[]) + return a:ft == 'org' ? s:org_new(a:id, a:title, a:tags) + \ : a:ft == 'plain' ? s:plain_new(a:id, a:title, a:tags) + \ : g:denote_fm_md_type == 'toml' ? s:md_new_toml(a:id, a:title, a:tags) + \ : s:md_new_yaml(a:id, a:title, a:tags) +endfunction diff --git a/autoload/denote/meta.vim b/autoload/denote/meta.vim index 07a2d7a..41eb41e 100644 --- a/autoload/denote/meta.vim +++ b/autoload/denote/meta.vim @@ -32,5 +32,14 @@ endfunction " Return the note tags from the filename as a list. function denote#meta#noteTagsFromFile(filename) return a:filename->matchstr("__\\zs.\\{-\\}\\ze\\(==\\|@@\\|--\\|\\..\\)")->split("_") - \ ?? [] +endfunction + +" Identifier creation +function denote#meta#identifier_generate() + if g:denote_identifier_fun + return execute "call " .. g:denote_identifier_fun .. "()" + endif + return exists("*strftime") + \ ? strftime("%Y%m%dT%H%M%S") + \ : rand() endfunction diff --git a/plugin/denote.vim b/plugin/denote.vim index bef7d42..ffb1242 100644 --- a/plugin/denote.vim +++ b/plugin/denote.vim @@ -10,6 +10,22 @@ if !exists('g:denote_loc_title_columns') let g:denote_loc_title_columns=40 endif +" Default filetype for newly created denote entries +if !exists('g:denote_new_ft') + let g:denote_new_ft='md' +endif + +" Default front-matter type for markdown notes, may be one of 'yaml' or 'toml' +if !exists('g:denote_fm_md_type') + let g:denote_fm_md_type='yaml' +endif + +" By using the following global variable, the user may specify a custom +" function for creating identifiers. +if !exists('g:denote_identifier_fun') + let g:denote_identifier_fun='' +endif + " Local functions {{{1 " Put all notes of the given tag to the location list. The tag argument is " mandatory. @@ -82,10 +98,24 @@ function s:tagList(ArgLead, cmdLine, CursorPos) return uniq(sort(tags))->join("\n") endfunction +" This creates a new denote entry with the given title and of the given +" filetype. The title may be empty. +function s:DenoteNew(title, ft=g:denote_new_ft) + let identifier=denote#meta#identifier_generate() + let fn=identifier .. '--' .. a:title + \ ->tolower() + \ ->substitute('[^[:fname:]]\|/', '-', 'g') + \ ->substitute('-\+', '-', 'g') + \ ->trim('-') .. '.' .. a:ft + execute "edit " .. fn + call setline(1, denote#frontmatter#new(a:ft, identifier, a:title)) +endfunction + " Public commands and key mappings {{{1 command -nargs=* Denote :call DenoteNotes() command -nargs=1 -complete=custom,tagList DenoteTag :call DenoteNotesByTag() command -nargs=+ DenoteGrep :call DenoteGrep() +command -nargs=1 DenoteNew :call DenoteNew() " Useful key mappings nnoremap DenoteList :Denote:lclose:lopen:resize 20