/
templatefile.vim
165 lines (154 loc) · 6.75 KB
/
templatefile.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"=============================================================================
" Vim global plugin for autoload template files
" File: templatefile.vim
" Maintainer: Lubomir Host <host8@kepler.fmph.uniba.sk>
" Last Change: 2002/02/05
" Version: $Id: templatefile.vim,v 1.3 2002/02/06 01:13:40 host8 Exp $
" Thanks:
" Scott Urban : First version of templatefile.vim
" http://vim.sourceforge.net/scripts/
" script.php?script_id=198
"
" Description:
" Plugin load template file for new files
" Templates for new files aren't loaded, if g:load_templates == "no"
" if g:load_templates == "ask" you are asked before loading template
" If exists enviroment variable $VIMTEMPLATE, templates are loaded from
" this directory.
augroup TemplateSystem
autocmd!
au BufNewFile * call LoadTemplateFile()
augroup END
command! -nargs=0 LoadTemplateFile call LoadTemplateFile()
command! -nargs=1 LoadFile call LoadFile(<args>)
" template file loaded
fun! LoadTemplateFile()
if exists("g:load_templates")
if g:load_templates == "no"
return
endif
endif
let extension = expand ("%:e")
let prefix = expand ("%")
"Special Handling for stored Procedures
if matchstr(prefix, '^sp_') == "sp_"
let extension = "sp.sql"
endif
if matchstr(prefix, '^tr_') == "tr_"
let extension = "tr.sql"
endif
if matchstr(prefix, '^tr_i') == "tr_i"
let event = "insert"
elseif matchstr(prefix, '^tr_d') == "tr_d"
let event="delete"
else
let event="update"
endif
let event=toupper(event)
let basetable = substitute(prefix, "tr_._", "", "g")
let basetable = substitute(basetable, ".sql", "", "g")
if extension == ""
let template_file = "templates/" . expand("%:t")
let template_func = "TemplateFileFunc_noext_" . expand("%:t")
else
let template_file = "templates/skel." . extension
let template_func = "TemplateFileFunc_" . extension
endif
if filereadable(expand($VIMTEMPLATE . template_file))
call LoadTemplateFileConfirm($VIMTEMPLATE . template_file)
elseif filereadable(expand($HOME . "/.vim/" . template_file))
call LoadTemplateFileConfirm($HOME . "/.vim/" . template_file)
elseif filereadable(expand($VIM . template_file))
call LoadTemplateFileConfirm($VIM . template_file)
elseif filereadable(expand($VIMRUNTIME . template_file))
call LoadTemplateFileConfirm($VIMRUNTIME . template_file)
else
" Template not found
endif
let date = strftime("%c")
let year = strftime("%Y")
let cwd = getcwd()
let sp_version = strftime("%y%j")
let sp_version = "1.01." . substitute(sp_version, "^0", "", "g")
let lastdir = substitute(cwd, ".*/", "", "g")
let myfile = expand("%:t:r")
let myfile_ext = expand("%")
let inc_gaurd = substitute(myfile, "\\.", "_", "g")
let inc_gaurd = toupper(inc_gaurd)
silent! execute "%s/@AUTHOR@/" . b:author ."/g"
silent! execute "%s/@EMAIL@/" . b:email ."/g"
silent! execute "%s/@URL@/" . b:url ."/g"
silent! execute "%s/@VERSION@/" . sp_version . "/g"
silent! execute "%s/@DATE@/" . date . "/g"
silent! execute "%s/@YEAR@/" . year . "/g"
silent! execute "%s/@LASTDIR@/" . lastdir . "/g"
silent! execute "%s/@FILE@/" . myfile . "/g"
silent! execute "%s/@EVENT@/" . event . "/g"
silent! execute "%s/@BASETABLE@/" . basetable . "/g"
silent! execute "%s/@FILE_EXT@/" . myfile_ext . "/g"
silent! execute "%s/@INCLUDE_GAURD@/" . inc_gaurd . "/g"
if exists ("*" . template_func)
if exists("g:load_templates")
if g:load_templates == "ask"
let choice = confirm("Call function " . template_func . "() ?:",
\ "&yes\n" .
\ "&no\n")
if choice == 1
silent! execute ":call " . template_func . "()"
endif
elseif g:load_templates == "yes"
silent! execute ":call " . template_func . "()"
endif
else
silent! execute ":call " . template_func . "()"
endif
endif
endfun
fun! LoadTemplateFileConfirm(filename)
if filereadable(expand(a:filename))
if exists("g:load_templates")
if g:load_templates == "ask"
let choice = confirm("NEW FILE! Load template file " .
\ expand(a:filename) . " ?:",
\ "&yes\n" .
\ "&no\n")
if choice == 1
execute "0r " . a:filename
endif
elseif g:load_templates == "yes"
execute "0r " . a:filename
endif
else
execute "0r " . a:filename
endif
endif
endfun
fun! LoadFile(filename)
if filereadable(expand(a:filename))
if exists("g:load_templates")
if g:load_templates == "ask"
let choice = confirm("Load file " .
\ expand(a:filename) . " ?:",
\ "&yes\n" .
\ "&no\n")
if choice == 1
execute "0r " . a:filename
endif
elseif g:load_templates == "yes"
execute "0r " . a:filename
endif
else
execute "0r " . a:filename
endif
else
echo "File not found!"
endif
endfun
" example for no-extension file specific template processing
fun! TemplateFileFunc_noext_makefile()
let save_r = @r
let @r = "all:\n\techo your template files need work"
normal G
put r
let @r = save_r
endfun