Rebase the version of stdheader.vim from 42paris to 42lausanne

This commit is contained in:
Guy Baconniere
2021-10-25 11:10:26 +02:00
parent 8b3f090412
commit c3929bf5ed

View File

@@ -1,3 +1,16 @@
" **************************************************************************** "
" "
" ::: :::::::: "
" stdheader.vim :+: :+: :+: "
" +:+ +:+ +:+ "
" By: <gbaconni@student.42lausanne.ch> +#+ +:+ +#+ "
" +#+#+#+#+#+ +#+ "
" Created: 2013/06/15 12:45:56 by zaz #+# #+# "
" Updated: 2021/10/25 09:58:04 by gbaconni ### ########.fr "
" "
" **************************************************************************** "
let s:asciiart = [
\" ::: ::::::::",
\" :+: :+: :+:",
@@ -8,136 +21,175 @@ let s:asciiart = [
\" ### lausanne.ch "
\]
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$':
\['!', '!', '/']
let s:styles = [
\{
\'extensions': ['\.c$', '\.h$', '\.cc$', '\.hh$', '\.cpp$', '\.hpp$'],
\'start': '/*', 'end': '*/', 'fill': '*'
\},
\{
\'extensions': ['\.htm$', '\.html$', '\.xml$'],
\'start': '<!--', 'end': '-->', 'fill': '*'
\},
\{
\'extensions': ['\.js$'],
\'start': '//', 'end': '//', 'fill': '*'
\},
\{
\'extensions': ['\.tex$'],
\'start': '%', 'end': '%', 'fill': '*'
\},
\{
\'extensions': ['\.ml$', '\.mli$', '\.mll$', '\.mly$'],
\'start': '(*', 'end': '*)', 'fill': '*'
\},
\{
\'extensions': ['\.vim$', 'vimrc$', '\.myvimrc$', 'vimrc$'],
\'start': '"', 'end': '"', 'fill': '*'
\},
\{
\'extensions': ['\.el$', '\.emacs$', '\.myemacs$'],
\'start': ';', 'end': ';', 'fill': '*'
\},
\{
\'extensions': ['\.f90$', '\.f95$', '\.f03$', '\.f$', '\.for$'],
\'start': '!', 'end': '!', 'fill': '/'
\}
\]
function! s:filetype()
let l:f = s:filename()
let s:linelen = 80
let s:marginlen = 5
let s:contentlen = s:linelen - (3 * s:marginlen - 1) - strlen(s:asciiart[0])
function! s:trimlogin ()
let l:trimlogin = strpart($USER, 0, 9)
if strlen(l:trimlogin) == 0
let l:trimlogin = "rfederer"
endif
return l:trimlogin
endfunction
function! s:trimemail ()
let l:trimemail = strpart($MAIL, 0, s:contentlen - 16)
if strlen(l:trimemail) == 0
let l:trimemail = "rfederer@student.42lausanne.ch"
endif
return l:trimemail
endfunction
function! s:midgap ()
return repeat(' ', s:marginlen - 1)
endfunction
function! s:lmargin ()
return repeat(' ', s:marginlen - strlen(s:start))
endfunction
function! s:rmargin ()
return repeat(' ', s:marginlen - strlen(s:end))
endfunction
function! s:empty_content ()
return repeat(' ', s:contentlen)
endfunction
function! s:left ()
return s:start . s:lmargin()
endfunction
function! s:right ()
return s:rmargin() . s:end
endfunction
function! s:bigline ()
return s:start . ' ' . repeat(s:fill, s:linelen - 2 - strlen(s:start) - strlen(s:end)) . ' ' . s:end
endfunction
function! s:logo1 ()
return s:left() . s:empty_content() . s:midgap() . s:asciiart[0] . s:right()
endfunction
function! s:fileline ()
let l:trimfile = strpart(fnamemodify(bufname('%'), ':t'), 0, s:contentlen)
return s:left() . l:trimfile . repeat(' ', s:contentlen - strlen(l:trimfile)) . s:midgap() . s:asciiart[1] . s:right()
endfunction
function! s:logo2 ()
return s:left() . s:empty_content() . s:midgap() .s:asciiart[2] . s:right()
endfunction
function! s:coderline ()
let l:contentline = "By: ". s:trimlogin () . ' <' . s:trimemail () . '>'
return s:left() . l:contentline . repeat(' ', s:contentlen - strlen(l:contentline)) . s:midgap() . s:asciiart[3] . s:right()
endfunction
function! s:logo3 ()
return s:left() . s:empty_content() . s:midgap() .s:asciiart[4] . s:right()
endfunction
function! s:dateline (prefix, logo)
let l:date = strftime("%Y/%m/%d %H:%M:%S")
let l:contentline = a:prefix . ": " . l:date . " by " . s:trimlogin ()
return s:left() . l:contentline . repeat(' ', s:contentlen - strlen(l:contentline)) . s:midgap() . s:asciiart[a:logo] . s:right()
endfunction
function! s:createline ()
return s:dateline("Created", 5)
endfunction
function! s:updateline ()
return s:dateline("Updated", 6)
endfunction
function! s:emptyline ()
return s:start . repeat(' ', s:linelen - strlen(s:start) - strlen(s:end)) . s:end
endfunction
function! s:filetype ()
let l:file = fnamemodify(bufname("%"), ':t')
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]
for l:style in s:styles
for l:ext in l:style['extensions']
if l:file =~ l:ext
let s:start = l:style['start']
let s:end = l:style['end']
let s:fill = l:style['fill']
endif
endfor
endfor
endfunction
function! s:ascii(n)
return s:asciiart[a:n - 3]
endfunction
function! s:insert ()
call s:filetype ()
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: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 = "rfederer"
endif
return l:user
endfunction
function! s:mail()
let l:mail = $MAIL
if strlen(l:mail) == 0
let l:mail = "rfederer@student.42lausanne.ch"
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
call append (0, s:bigline())
call append (0, s:emptyline())
call append (0, s:updateline())
call append (0, s:createline())
call append (0, s:logo3())
call append (0, s:coderline())
call append (0, s:logo2())
call append (0, s:fileline())
call append (0, s:logo1())
call append (0, s:emptyline())
call append (0, s:bigline())
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:update ()
call s:filetype ()
function! s:stdheader()
if s:update()
call s:insert()
let l:pattern = s:start . repeat(' ', 5 - strlen(s:start)) . "Updated: [0-9]"
let l:line = getline (9)
if l:line =~ l:pattern
call setline(9, s:updateline())
endif
endfunction
" Bind command and shortcut
command! Stdheader call s:stdheader ()
map <F1> :Stdheader<CR>
command! Stdheader call s:insert ()
nmap <F1> :Stdheader<CR>
autocmd BufWritePre * call s:update ()