-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstdheader.vim
143 lines (123 loc) · 3.25 KB
/
stdheader.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
let s:asciiart = [
\" ::: ::::::::",
\" :+: :+: :+:",
\" +:+ +:+ +:+ ",
\" +#+ +:+ +#+ ",
\"+#+#+#+#+#+ +#+ ",
\" #+# #+# ",
\" ### ########.fr "
\]
let s:start = '/*'
let s:end = '*/'
let s:fill = '*'
let s:length = 80
let s:margin = 5
let s:types = {
\'\.c$\|\.h$\|\.cc$\|\.hh$\|\.cpp$\|\.hpp$\|\.php':
\['/*', '*/', '*'],
\'\.htm$\|\.html$\|\.xml$':
\['<!--', '-->', '*'],
\'\.js$':
\['//', '//', '*'],
\'\.tex$':
\['%', '%', '*'],
\'\.ml$\|\.mli$\|\.mll$\|\.mly$':
\['(*', '*)', '*'],
\'\.vim$\|\vimrc$':
\['"', '"', '*'],
\'\.el$\|\emacs$':
\[';', ';', '*'],
\'\.f90$\|\.f95$\|\.f03$\|\.f$\|\.for$':
\['!', '!', '/']
\}
function! s:filetype()
let l:f = s:filename()
let s:start = '#'
let s:end = '#'
let s:fill = '*'
for type in keys(s:types)
if l:f =~ type
let s:start = s:types[type][0]
let s:end = s:types[type][1]
let s:fill = s:types[type][2]
endif
endfor
endfunction
function! s:ascii(n)
return s:asciiart[a:n - 3]
endfunction
function! s:textline(left, right)
let l:left = strpart(a:left, 0, s:length - s:margin * 3 - strlen(a:right) + 1)
return s:start . repeat(' ', s:margin - strlen(s:start)) . l:left . repeat(' ', s:length - s:margin * 2 - strlen(l:left) - strlen(a:right)) . a:right . repeat(' ', s:margin - strlen(s:end)) . s:end
endfunction
function! s:line(n)
if a:n == 1 || a:n == 11 " top and bottom line
return s:start . ' ' . repeat(s:fill, s:length - strlen(s:start) - strlen(s:end) - 2) . ' ' . s:end
elseif a:n == 2 || a:n == 10 " blank line
return s:textline('', '')
elseif a:n == 3 || a:n == 5 || a:n == 7 " empty with ascii
return s:textline('', s:ascii(a:n))
elseif a:n == 4 " filename
return s:textline(s:filename(), s:ascii(a:n))
elseif a:n == 6 " author
return s:textline("By: " . s:user() . " <" . s:mail() . ">", s:ascii(a:n))
elseif a:n == 8 " created
return s:textline("Created: " . s:date() . " by " . s:user(), s:ascii(a:n))
elseif a:n == 9 " updated
return s:textline("Updated: " . s:date() . " by " . s:user(), s:ascii(a:n))
endif
endfunction
function! s:user()
let l:user = $USER
if strlen(l:user) == 0
let l:user = "marvin"
endif
return l:user
endfunction
function! s:mail()
let l:mail = $MAIL
if strlen(l:mail) == 0
let l:mail = "[email protected]"
endif
return l:mail
endfunction
function! s:filename()
let l:filename = expand("%:t")
if strlen(l:filename) == 0
let l:filename = "< new >"
endif
return l:filename
endfunction
function! s:date()
return strftime("%Y/%m/%d %H:%M:%S")
endfunction
function! s:insert()
let l:line = 11
" empty line after header
call append(0, "")
" loop over lines
while l:line > 0
call append(0, s:line(l:line))
let l:line = l:line - 1
endwhile
endfunction
function! s:update()
call s:filetype()
if getline(9) =~ s:start . repeat(' ', s:margin - strlen(s:start)) . "Updated: "
if &mod
call setline(9, s:line(9))
endif
call setline(4, s:line(4))
return 0
endif
return 1
endfunction
function! s:stdheader()
if s:update()
call s:insert()
endif
endfunction
" Bind command and shortcut
command! Stdheader call s:stdheader ()
map <F1> :Stdheader<CR>
autocmd BufWritePre * call s:update ()