Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions plugin/templates.vim
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ endfunction
" Makes a single [variable] expansion, using [value] as replacement.
"
function <SID>TExpand(variable, value)
silent! execute "%s/\\V%" . <SID>EscapeRegex(a:variable) . "%/" . <SID>EscapeRegex(a:value) . "/g"
silent! execute "%s/\\V%" . <SID>EscapeRegex(a:variable) . "%/" . <SID>EscapeRegex(a:value) . "/ge"
endfunction

" Performs variable expansion in a template once it was loaded {{{2
Expand Down Expand Up @@ -480,25 +480,25 @@ endfunction
" Load the given file as a template
function <SID>TLoadTemplate(template, position)
if a:template != ""
let l:deleteLastLine = 0
if line('$') == 1 && getline(1) == ''
let l:deleteLastLine = 1
endif
let l:templateLines = readfile(a:template)
let l:emptyBuffer = line('$') == 1 && getline(1) == ''

" Read template file and expand variables in it.
let l:safeFileName = <SID>NeuterFileName(a:template)
if a:position == 0 || l:deleteLastLine == 1
execute "keepalt 0r " . l:safeFileName
if empty(l:templateLines)
if l:emptyBuffer
call setline(1, '')
endif
else
execute "keepalt r " . l:safeFileName
endif
call <SID>TExpandVars()

if l:deleteLastLine == 1
" Loading a template into an empty buffer leaves an extra blank line at the bottom, delete it
execute line('$') . "d _"
if l:emptyBuffer
call append(0, l:templateLines)
call deletebufline('', line('$'))
elseif a:position == 0
call append(0, l:templateLines)
else
call append(line('.'), l:templateLines)
endif
endif

call <SID>TExpandVars()
call <SID>TPutCursor()
setlocal nomodified
endif
Expand Down
48 changes: 48 additions & 0 deletions tests/test_preserve_last_lines.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
let g:templates_no_autocmd = 1
let g:templates_directory = []

execute 'set rtp^=' . fnameescape(fnamemodify(expand('<sfile>:p'), ':h:h'))
runtime plugin/templates.vim

function! s:RunCase(file_name, template_lines, expected_lines) abort
let l:root = tempname()
let l:templates_dir = l:root . '/templates'
call mkdir(l:templates_dir, 'p')
call writefile(a:template_lines, l:templates_dir . '/=template=.' . fnamemodify(a:file_name, ':e'))
let g:templates_directory = [l:templates_dir]

silent execute 'edit ' . fnameescape(l:root . '/' . a:file_name)
silent Template

call assert_equal(a:expected_lines, getline(1, '$'))
bwipe!
endfunction

function! s:RunFoldExprCase(file_name, template_lines, expected_lines) abort
let l:root = tempname()
let l:templates_dir = l:root . '/templates'
call mkdir(l:templates_dir, 'p')
call writefile(a:template_lines, l:templates_dir . '/=template=.' . fnamemodify(a:file_name, ':e'))
let g:templates_directory = [l:templates_dir]

silent enew
execute 'file ' . fnameescape(l:root . '/' . a:file_name)
setlocal foldmethod=expr foldexpr=1
silent Template

call assert_equal(a:expected_lines, getline(1, '$'))
bwipe!
endfunction

call s:RunCase('keep.txt', ['alpha', 'beta', 'gamma'], ['alpha', 'beta', 'gamma'])
call s:RunCase('cursor.md', ['alpha', 'omega', '%HERE%', ''], ['alpha', 'omega', '', ''])
call s:RunFoldExprCase('keep.md', ['alpha', 'beta', 'gamma'], ['alpha', 'beta', 'gamma'])

if empty(v:errors)
quit
endif

for s:error in v:errors
echom s:error
endfor
cquit 1