functionality to add new notes
This commit is contained in:
59
autoload/denote/frontmatter.vim
Normal file
59
autoload/denote/frontmatter.vim
Normal file
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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 <SID>DenoteNotes(<q-args>)
|
||||
command -nargs=1 -complete=custom,<SID>tagList DenoteTag :call <SID>DenoteNotesByTag(<q-args>)
|
||||
command -nargs=+ DenoteGrep :call <SID>DenoteGrep(<q-args>)
|
||||
command -nargs=1 DenoteNew :call <SID>DenoteNew(<q-args>)
|
||||
|
||||
" Useful key mappings
|
||||
nnoremap <silent> <Plug>DenoteList :Denote<CR>:lclose<CR>:lopen<CR>:resize 20<CR>
|
||||
|
||||
Reference in New Issue
Block a user