Initial Commit of stdheader for 42lausanne

This commit is contained in:
Baco
2021-10-23 15:16:30 +02:00
commit 9ef0496968
5 changed files with 247 additions and 0 deletions

33
LICENSE Normal file
View File

@@ -0,0 +1,33 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# LICENSE :+: :+: :+: #
# +:+ +:+ +:+ #
# By: <gbaconni@student.42lausanne.ch> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2021/10/23 14:49:14 by gbaconni #+# #+# #
# Updated: 2021/10/23 14:49:55 by gbaconni ### lausanne.ch #
# #
# **************************************************************************** #
This is a free work delivered for educational purposes during the main course
at 42Lausanne <42lausanne.ch>.
Feel free to use it, change it, give advices, send me a pull request or
just slack me if you want to share some thoughts.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

45
README.md Normal file
View File

@@ -0,0 +1,45 @@
# **42 Header**
42Lausanne (Switzerland)
### **Description**
42 standard header for vim editor.
![42 header](img/42header.png)
### **UNIX Setup**
Add in `~/.zshrc` your:
+ `USER`
+ `MAIL`
Copy `stdheader.vim` in your `~/.vim/plugin`.
### **Usage**
In **NORMAL** mode you can use `:Stdheader` or simply press the shortcut <kbd>F1</kbd>.
Under **Linux** you eventually need to disable the **help** shortcut of your **terminal** :
For **Terminator**, right click -> Preferences -> Shortcuts -> change help with something other than <kbd>F1</kbd>
### **Note**
Inside the **42 clusters** you can easily run:
`$ ./set_header.sh`
The location of stdheader.vim in 42lausanne's clusters:
- /usr/share/vim/vim81/plugin/stdheader.vim
- /usr/share/vim/vim80/plugin/stdheader.vim
### **Credits**
[@42Paris](https://github.com/42Paris)
[@zazard](https://github.com/zazard)
### **License**
This work is published under the terms of **[42 Unlicense](https://github.com/gcamerli/42unlicense)**.

BIN
img/42header.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 876 KiB

26
set_header.sh Executable file
View File

@@ -0,0 +1,26 @@
#!/bin/bash
# ./set_header.sh
# Set variables
if [ ! -z "$USER" ]
then
echo 'export USER=`/usr/bin/whoami`' >> ~/.zshrc
fi
if [ ! -z "$GROUP" ]
then
echo 'export GROUP=`/usr/bin/id -gn ${USER}`' >> ~/.zshrc
fi
if [ ! -z "$MAIL" ]
then
echo 'export MAIL="${USER}@student.42lausanne.ch"' >> ~/.zshrc
fi
test -d ~/.vim/plugin || mkdir -p ~/.vim/plugin
# Add stdheader to vim plugins
cp vim/stdheader.vim ~/.vim/plugin/
source ~/.zshrc

143
vim/stdheader.vim Normal file
View File

@@ -0,0 +1,143 @@
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$':
\['!', '!', '/']
\}
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: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
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 ()