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.
This commit is contained in:
Kenneth Benzie 2019-03-31 15:04:08 +01:00
parent 65c219c98d
commit 01b027b5ac
3 changed files with 47 additions and 4 deletions

24
UltiSnips/help.snippets Normal file
View File

@ -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

16
autoload/snippet.vim Normal file
View File

@ -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\<C-r>=UltiSnips#ExpandSnippet()\<CR>"
if g:ulti_expand_res == 0
" Expansions failed, undo insert.
silent! undo
endif
endfunction

View File

@ -1,14 +1,17 @@
augroup benieAugroup augroup benieAugroup
" Clear all autocmd's in this group
autocmd! autocmd!
" Reopening a file at last curson position " Reopening a file at last curson position
au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$") au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$")
\ | exe "normal! g'\"" | endif \ | exe "normal! g'\"" | endif
" Highlight conflict markers in any filefile " Highlight conflict markers in any filetype
au FileType * :call matchadd('Todo', '^\(<<<<<<<\||||||||\|=======\|>>>>>>>\)\s\ze.*$') 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 _ 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 augroup END