/
AutoLoadTemplate.vim
88 lines (75 loc) · 2.38 KB
/
AutoLoadTemplate.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
" Vim plugin file
" Maintainer: janus_wel <janus.wel.3@gmail.com>
" Last Change: 2009/03/06 20:34:27.
" Version: 0.22
" Remark: load template along with ext automatically.
" the position of template files can be seted
" by g:autoloadtemplate_path. default is
" UNIX / Linux: ~/.vim/template
" Windows : %HOME%\vimfiles\template
if has('autocmd')
" path to template files
" default
let s:templatepath = split(&runtimepath, ',')[0] . '/template/*'
" load setting from global variable
if exists('g:autoloadtemplate_path')
let s:templatepath = g:templateautoloader_path . '/*'
endif
" return bool
" 0 : buffer has something
" not 0: buffer is empty
function! IsBufferEmpty()
if !(line('$') == 1 && getline(1) == '')
return 1
endif
return 0
endfunction
" return list
" get absolute path of template files
function! GetTemplateFiles()
return split(expand(s:templatepath), '\n')
endfunction
" return none
" read specified file to buffer
function! ReadTemplateFile(file)
execute 'read ' . a:file
1delete
endfunction
" along with ext
function! AutoLoadTemplateExt()
if IsBufferEmpty()
return 1
endif
" get extension name of buffer
let l:ext = fnamemodify(bufname(''), ':e')
" load template if ext has matched
for l:t in GetTemplateFiles()
if l:ext == fnamemodify(l:t, ':e')
call ReadTemplateFile(l:t)
endif
endfor
endfunction
" along with filetype
function! AutoLoadTemplateFileType()
if IsBufferEmpty()
return 1
endif
" get filetype of buffer
let l:ft = &l:filetype
" load template if ext has matched
for l:t in GetTemplateFiles()
if l:ft == fnamemodify(l:t, ':t:r')
call ReadTemplateFile(l:t)
endif
endfor
" why 'modified' is set by calling this function only...?
setlocal nomodified
endfunction
" autocmd
augroup AutoLoadTemplate
autocmd! AutoLoadTemplate
autocmd BufNewFile * call AutoLoadTemplateExt()
autocmd FileType * call AutoLoadTemplateFileType()
augroup END
endif
" vim: ts=4 sw=4 sts=0 et