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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
"=============================================================================
" sonictemplate.vim
" Author: Yasuhiro Matsumoto <mattn.jp@gmail.com>
" Last Change: 11-Jun-2012.
let s:save_cpo = &cpo
set cpo&vim
let s:tmpldir = []
if exists('g:sonictemplate_vim_template_dir')
if type(g:sonictemplate_vim_template_dir) == 3
let s:tmpldir += map(g:sonictemplate_vim_template_dir, 'fnamemodify(expand(v:val), ":p")')
else
call add(s:tmpldir, fnamemodify(expand(g:sonictemplate_vim_template_dir), ":p"))
endif
endif
call add(s:tmpldir, expand('<sfile>:p:h:h') . '/template/')
function! sonictemplate#select(mode) abort
let name = input(':Template ', '', 'customlist,sonictemplate#complete')
if name == ''
return ''
endif
call sonictemplate#apply(name, a:mode, 0)
return ''
endfunction
function! sonictemplate#select_intelligent(mode) abort
let name = input(':Template ', '', 'customlist,sonictemplate#complete_intelligent')
if name == ''
return ''
endif
call sonictemplate#apply(name, a:mode, 1)
return ''
endfunction
function! sonictemplate#get_filetype()
let c = col('.')
if c == col('$')
let c -= 1
endif
let ft = tolower(synIDattr(synID(line("."), c, 1), "name"))
if ft =~ '^css\w'
let ft = 'css'
elseif ft =~ '^html\w'
let ft = 'html'
elseif ft =~ '^javaScript'
let ft = 'javascript'
elseif ft =~ '^xml\w'
let ft = 'xml'
endif
return ft
endfunction
function! sonictemplate#complete(lead, cmdline, curpos) abort
let ft = &ft
let tmp = []
let fts = [&ft, sonictemplate#get_filetype()]
for tmpldir in s:tmpldir
for ft in fts
let tmp += map(split(globpath(join([tmpldir, ft], '/'), (search('[^ \t]', 'wn') ? 'snip-' : 'base-') . a:lead . '*.*'), "\n"), 'fnamemodify(v:val, ":t:r")[5:]')
if len(tmp) > 0
break
endif
endfor
endfor
for tmpldir in s:tmpldir
let tmp += map(split(globpath(join([tmpldir, '_'], '/'), (search('[^ \t]', 'wn') ? 'snip-' : 'base-') . a:lead . '*.*'), "\n"), 'fnamemodify(v:val, ":t:r")[5:]')
endfor
let candidate = []
for c in tmp
if index(candidate, c) == -1
call add(candidate, c)
endif
endfor
return candidate
endfunction
function! sonictemplate#complete_intelligent(lead, cmdline, curpos) abort
let ft = &ft
let tmp = []
let fts = [tolower(sonictemplate#get_filetype()), &ft]
for tmpldir in s:tmpldir
for ft in fts
let tmp += map(split(globpath(join([tmpldir, ft], '/'), (search('[^ \t]', 'wn') ? 'snip-' : 'base-') . a:lead . '*.*'), "\n"), 'fnamemodify(v:val, ":t:r")[5:]')
if len(tmp) > 0
break
endif
endfor
endfor
for tmpldir in s:tmpldir
let tmp += map(split(globpath(join([tmpldir, '_'], '/'), (search('[^ \t]', 'wn') ? 'snip-' : 'base-') . a:lead . '*.*'), "\n"), 'fnamemodify(v:val, ":t:r")[5:]')
endfor
let candidate = []
for c in tmp
if index(candidate, c) == -1
call add(candidate, c)
endif
endfor
return candidate
endfunction
function! sonictemplate#apply(name, mode, ...) abort
let name = matchstr(a:name, '\S\+')
let buffer_is_not_empty = search('[^ \t]', 'wn')
let fs = []
if get(a:000, 0, 0)
let fts = [sonictemplate#get_filetype(), &ft, '_']
else
let fts = [&ft, sonictemplate#get_filetype(), '_']
endif
let prefix = search('[^ \t]', 'wn') ? 'snip-' : 'base-'
for tmpldir in s:tmpldir
for ft in fts
let fs += split(globpath(join([tmpldir, ft], '/'), prefix . name . '.*'), "\n")
endfor
endfor
if len(fs) == 0
echomsg 'Template '.name.' is not exists.'
return
endif
let f = fs[0]
if !filereadable(f)
echomsg 'Template '.name.' is not exists.'
return
endif
let c = join(readfile(f), "\n")
let c = substitute(c, '{{_name_}}', expand('%:t:r:'), 'g')
let tmp = c
let mx = '{{_input_:\(.\{-}\)}}'
let vars = []
while 1
let match = matchstr(tmp, mx)
if len(match) == 0
break
endif
let var = substitute(match, mx, '\1', 'ig')
if index(vars, var) == -1
call add(vars, var)
endif
let tmp = tmp[stridx(tmp, match) + len(match):]
endwhile
for var in vars
let val = input(var . ":")
let c = substitute(c, '\V{{_input_:'.var.'}}', '\=val', 'g')
endfor
sandbox let c = substitute(c, '{{_if_:\(.\{-}\);\(.\{-}\)\(;\(.\{-}\)\)\{-}}}', '\=eval(submatch(1))?submatch(2):submatch(4)', 'g')
sandbox let c = substitute(c, '{{_expr_:\(.\{-}\)}}', '\=eval(submatch(1))', 'g')
if len(c) == 0
return
endif
if !buffer_is_not_empty
let c = substitute(c, '{{_inline_}}', '', 'g')
if &expandtab || &tabstop != &shiftwidth
let c = substitute(c, "\t", repeat(' ', &shiftwidth), 'g')
endif
silent! %d _
silent! put = c
silent! normal! ggdd
else
if c[len(c)-1] == "\n"
let c = c[:-2]
endif
if stridx(c, '{{_inline_}}') != -1
let c = substitute(c, '{{_inline_}}', '', 'g')
let c = join(split(c, "\n"), "")
silent! exe "normal! a\<c-r>=c\<cr>"
return
else
let line = getline('.')
let indent = matchstr(line, '^\(\s*\)')
if line !~ '^\s*$'
let lhs = col('.') > 1 ? line[:col('.')-2] : ""
let rhs = line[len(lhs):]
let lhs = lhs[len(indent):]
let c = lhs . c . rhs
endif
silent! normal dd
let c = indent . substitute(substitute(c, "\n", "\n".indent, 'g'), "\n".indent."\n", "\n\n", 'g')
if len(indent) && (&expandtab || &tabstop != &shiftwidth || indent =~ '^ \+$')
let c = substitute(c, "\t", repeat(' ', min([len(indent), &shiftwidth])), 'g')
endif
silent! put! =c
endif
endif
if stridx(c, '{{_cursor_}}') != -1
if a:mode == 'n'
silent! call search('\zs{{_cursor_}}', 'w')
silent! foldopen
silent! exe "normal ".repeat("x", 12)
else
silent! call search('{{_cursor_}}\zs', 'w')
silent! foldopen
call feedkeys(repeat("\<bs>", 12))
endif
endif
endfunction
let &cpo = s:save_cpo
unlet s:save_cpo
" vim:set et:
|