From 01b027b5acee3dc5874dde29e71cd8162e922508 Mon Sep 17 00:00:00 2001 From: "Kenneth Benzie (Benie)" Date: Sun, 31 Mar 2019 15:04:08 +0100 Subject: [PATCH] Support snippet templates and add help snippets Add `autocmd` to automatically expand the `_template` snippet on the `BufNewFile` event, if the expansion fails because the `_template` snippet does not exist for the current `filetype` undo the changes. Do the same thing when the `filetype` changes to `help`, since these are `text` files to begin with the first `autocmd` has no effect. --- UltiSnips/help.snippets | 24 ++++++++++++++++++++++++ autoload/snippet.vim | 16 ++++++++++++++++ plugin/autocmds.vim | 11 +++++++---- 3 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 UltiSnips/help.snippets create mode 100644 autoload/snippet.vim diff --git a/UltiSnips/help.snippets b/UltiSnips/help.snippets new file mode 100644 index 0000000..f4551ce --- /dev/null +++ b/UltiSnips/help.snippets @@ -0,0 +1,24 @@ +snippet _template "help file template" +*`!p snip.rv = snip.fn`* For Vim version 8.0 Last change: `!p +from datetime import datetime +snip.rv = datetime.now().strftime('%B %d, %Y')` + +$0 + + vim:tw=78:ts=8:ft=help:norl: +endsnippet + +snippet s "help section" +============================================================================== +${1:1}. ${2:Section}`!p +spaces = 78 - len(t[1]) - len(snip.basename) - (2 * len(t[2])) - 3 +snip.rv = spaces * ' ' + '*' + snip.basename + '-' + t[2].lower() + '*'` + +$0 +endsnippet + +snippet d "help detail" +`!p spaces = 78 - len(t[1]) +snip.rv = spaces * ' '`*${1:}* +$0 +endsnippet diff --git a/autoload/snippet.vim b/autoload/snippet.vim new file mode 100644 index 0000000..90f163f --- /dev/null +++ b/autoload/snippet.vim @@ -0,0 +1,16 @@ +" Description: Expand snippet on file creation. + +" Attempt to expand the _template snippet if this is a new file. +" https://noahfrederick.com/log/vim-templates-with-ultisnips-and-projectionist +function! snippet#template() abort + " Return if non-empty buffer or file exists. + if !(line('$') == 1 && getline('$') ==# '') || filereadable(expand('%')) + return + endif + " Attempt to expand the _template snippet. + execute "normal! i_template\=UltiSnips#ExpandSnippet()\" + if g:ulti_expand_res == 0 + " Expansions failed, undo insert. + silent! undo + endif +endfunction diff --git a/plugin/autocmds.vim b/plugin/autocmds.vim index 360921d..4386d00 100644 --- a/plugin/autocmds.vim +++ b/plugin/autocmds.vim @@ -1,14 +1,17 @@ augroup benieAugroup - " Clear all autocmd's in this group autocmd! " Reopening a file at last curson position au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$") \ | exe "normal! g'\"" | endif - " Highlight conflict markers in any filefile - au FileType * :call matchadd('Todo', '^\(<<<<<<<\||||||||\|=======\|>>>>>>>\)\s\ze.*$') + " Highlight conflict markers in any filetype + au FileType * call matchadd('Todo', '^\(<<<<<<<\||||||||\|=======\|>>>>>>>\)\s\ze.*$') - " Read template into buffer and send line 1 to the black hold register + " Read template into buffer then send line 1 to the black hold register au BufNewFile todo.md read ~/.vim/templates/skeleton.todo.md | 1delete _ + " Attempt to expand snippet named `_template` if it exists + au BufNewFile * silent! call snippet#template() + " Do the same when filetype changes to help + au FileType help silent! call snippet#template() augroup END