commit 9ef0496968ae3e246d29343741183ac5ddf544ce Author: Baco Date: Sat Oct 23 15:16:30 2021 +0200 Initial Commit of stdheader for 42lausanne diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..30d5c42 --- /dev/null +++ b/LICENSE @@ -0,0 +1,33 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# LICENSE :+: :+: :+: # +# +:+ +:+ +:+ # +# By: +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# 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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..df262b2 --- /dev/null +++ b/README.md @@ -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 F1. + +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 F1 + +### **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)**. diff --git a/img/42header.png b/img/42header.png new file mode 100644 index 0000000..6279725 Binary files /dev/null and b/img/42header.png differ diff --git a/set_header.sh b/set_header.sh new file mode 100755 index 0000000..f9263ac --- /dev/null +++ b/set_header.sh @@ -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 diff --git a/vim/stdheader.vim b/vim/stdheader.vim new file mode 100644 index 0000000..fd9065a --- /dev/null +++ b/vim/stdheader.vim @@ -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 :Stdheader +autocmd BufWritePre * call s:update ()