vimrc and the helper scripts

This commit is contained in:
Vahagn Khachatryan
2013-06-19 00:01:26 +05:00
commit 0fe6aada11
3 changed files with 514 additions and 0 deletions

84
vim/cf5-compiler.vim Executable file
View File

@@ -0,0 +1,84 @@
"
" Functions to compile and link a single c/cpp file.
"
"if exists("g:loaded_vahagn_compiler") | finish | endif
let g:loaded_vahagn_compiler = 1
"
" Make sure let-modeline.vim is loaded.
"
if !exists('*FirstModeLine')
runtime plugin/let-modeline.vim
endif
"
" Windows
"
let g:cppflags=" "
let g:wcppflags="/O2 /EHsc"
function! s:CompileMSVC()
let exename=expand("%:p:r:s,$,.exe,")
let srcname=expand("%")
" compile it
let ccline="cl ".g:cppflags." ".g:wcppflags." ".srcname." /Fe".exename
echo ccline
let cout = system( ccline )
if v:shell_error
echo cout
return
endif
" run it
echo exename
let eout = system( exename )
echo eout
endfunction
function! s:CompileWindows()
call s:CompileMSVC()
endfunction
"
" Linux
"
let g:lcppflags="-O2"
function! s:CompileGCC()
let exename=expand("%:p:r:s,$,.exe,")
let srcname=expand("%")
" compile it
let ccline="g++ ".g:cppflags." ".g:lcppflags." ".srcname." -o".exename
echo ccline
let cout = system( ccline )
if v:shell_error
echo cout
return
endif
echo cout
" run it
let cmdline="LD_LIBRARY_PATH=".g:ldlibrarypath.":".$LD_LIBRARY_PATH." ".exename
echo cmdline
let eout = system( cmdline )
echo eout
endfunction
function! s:CompileLinux()
call s:CompileGCC()
endfunction
"
" Load compile instructions and call window or linux compiler.
"
function! CF5Compile()
source expand("%:p:h")."/compile-opt.vim"
call FirstModeLine()
if exists("g:cf5script")
execute g:cf5script
endif
if has("win32") || has("win64")
call s:CompileWindows()
else
call s:CompileLinux()
endif
endfunction

234
vim/let-modeline.vim Executable file
View File

@@ -0,0 +1,234 @@
" example -> VIM: let b:toto="foo" g:tata=4 g:egal="t=y".&tw
" ===========================================================================
" File: let-modeline.vim {{{1
" Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
" <URL:http://hermitte.free.fr/vim/>
" URL: http://hermitte.free.fr/vim/ressources/dollar_VIM/plugin/let-modeline.vim
" Version: 1.6
" Last Update: 23rd Mar 2004
"
" Purpose: {{{2
" Defines the function : FirstModeLine() that extends the VIM modeline
" feature to variables. In VIM, it is possible to set options in the
" first and last lines. -> :h modeline
" The function proposed extends it to 'let {var}={val}' affectations.
"
" Exemples Of Useful Aplications: {{{2
" Typical Example: {{{3
" When editing a LaTeX document composed of several files, it is very
" practical to know the name of the main file whichever file is edited --
" TKlatex does this thanks the global variable g:TeXfile. Hence it knows
" that latex should be called on this main file ; aux2tags.vim could also
" be told to compute the associated .aux file.
" Anyway. Defining (through menus or a let command) g:TeXfile each time is
" really boring. It bored me so much that I programmed a first version of
" this script. In every file of one of my projects I added the line :
" % VIM: let g:TeXfile=main.tex
" [main.tex is the name of the main file of the project]
" Thus, I can very simply call LaTeX from within VIM without having to
" wonder which file is the main one nor having to specify g:TeXfile each
" time.
"
" Using Callback Functions: {{{3
" Actually, in order to affect g:TeXfile, I have to call another function.
" Hence, I define a callback function (in my (La)TeX ftplugin) that checks
" whether I want to set g:TeXfile. In that case, the callback function
" calls the right function and return true. Otherwise, it returns false.
" You will find the code of this precise callback function as an example at
" the end of this file.
"
" Tune C Compilations: {{{3
" An easy way to tune the parameters of the compilation of simple programs
" without having to maintain a makefile:
" // VIM: let $CPPFLAGS='-I../../libs':
"
" ---------------------------------------------------------------------------
" Format: {{{2
" On the _first_ line of any file, the extended modeline format is:
" {line} ::= [text]{white}VIM:[white]let{affectations}
" {affectations} ::= {sgl_affect.}
" {affectations} ::= {sgl_affect.}{white}{affectations}
" {sgl_affect.} ::= {variable}[white]=[white]{value}
" {variable} ::= cf. vim variable format ; beware simple
" variables (other than global-, buffer-,
" or window-variables) are not exported.
" Can also be an environment variable -> $VAR.
" {value} ::= string or numeral value : no function
" call allowed.
"
" Options: {{2
" (*) 'modeline' : vim-option that must be set to 1
" (*) 'modelines': vim-option corrsponding to the number of lines
" searched.
" (*) b:ModeLine_CallBack(var,val) : callback function
" Enable to define callback functions when needed. cf. lhlatex.vim
"
" Installation: {{{2
" (*) Drop the file into your $$/plugin/ or $$/macros/ folder.
" (*) Source it from your .vimrc and add the autocommand:
" " Loads FirstModeLine()
" if !exists('*FirstModeLine')
" " :Runtime emules :runtime with VIM 5.x
" Runtime plugin/let-modeline.vim
" endif
" if exists('*FirstModeLine')
" aug ALL
" au!
" " To not interfer with Templates loaders
" au BufNewFile * :let b:this_is_new_buffer=1
" " Modeline interpretation
" au BufEnter * :call FirstModeLine()
" aug END
" endif
"
"
" Remarks: {{{2
" (*) The only way to call a function is through the callback feature.
" Affectation like 'let g:foo="abc".DEF()' are recognized and
" forbiden.
" (*) The modeline is recognized thanks to "VIM" in that *must* be in
" uppercase letters
"
" Changes: {{{2
" v1.6: Support for environment variables.
" vim 6.x only
" Doesn't check into folded lines anymore
" v1.5: Check that the format of the variables and values is correct
" before it tries to set the variables -> no more error messages
" when using 2html.vim.
" v1.4: With Vim 6.x, it doesn't mess anymore with the search history
" v1.3: Parse several lines according to &modelines and &modeline
" v1.2: no-reinclusion mecanism
" v1.1b: extend variable names to accept underscores
"
" Todo: {{{2
" (*) Enforce the patterns and the resulting errors
" (*) Permit to have comments ending characters at the end of the line.
" (*) Simplify the regexps
"
" }}}1
" ===========================================================================
" Definitions: {{{1
if exists("g:loaded_let_modeline") | finish | endif
let g:loaded_let_modeline = 1
" Internal function dedicated to the recognition of function calls {{{2
function! s:FoundFunctionCall(value_str)
let str = substitute(a:value_str, '"[^"]*"', '', 'g')
let str = substitute(str, "'[^']*'", '', 'g')
return match(str, '(.*)') != -1
endfunction
" Internal function dedicated to the parsing of a line {{{2
function! FML_parse_line(mtch)
" call confirm('Find:'.a:mtch, '&ok', 1)
if a:mtch !=""
let mtch = a:mtch
let re_var = '\s\+\([[:alnum:]:_$]\+\)'
" beware the comments ending characters
let re_val = '\(\%(' . "'[^']*'" . '\|"[^"]*"\|[-a-zA-Z0-9:_.&$]\)\+\)$'
let re_other = '^\(.\{-}\)'
let re_sub = re_other . re_var . '\s*=\s*' . re_val
while strlen(mtch) != 0
let vari = substitute( mtch, re_sub, '\2', '' )
let valu = substitute( mtch, re_sub, '\3', '' )
" call confirm('regex: '.re_sub."\nmtch: <<".mtch.">>\nvar: ".vari."\nval: ".valu, '&ok', 1)
if (vari !~ '^[[:alnum:]:_$]\+$') || (valu !~ re_val)
return
endif
" Check : no function !
if s:FoundFunctionCall(valu)
echohl ErrorMsg
echo "Find a function call in the affectation : let " . vari . " = " . valu
echohl None
return
endif
let mtch = substitute( mtch, re_sub, '\1', '' )
""echo vari . " = " . valu . " --- " . mtch . "\n"
" call confirm('vari: '.vari.' = '.valu." --- " . mtch, '&Ok', 1)
if exists("b:ModeLine_CallBack")
exe 'let res = '. b:ModeLine_CallBack . '("'.vari.'","'.valu.'")'
if res == 1 | return | endif
endif
" Else
execute "let " . vari . " = " . valu
endwhile
endif
endfunction
" Internal function dedicated searching the matching lines {{{2
function! s:Do_it_on_range(first, last)
" let modeline_pat = '[vV][iI][mM]\d*:\s*let\s*\zs.*$'
let modeline_pat = '[vV][iI][mM]\d*:\s*let\zs.*$'
if &verbose >= 2 " {{{
echo "\n->"a:first.','.a:last. 'g/'.modeline_pat.
\ '/:call FML_parse_line(matchstr(getline("."),"'.
\ escape(modeline_pat, '\\') .'"))'
endif " }}}
let s:save_fold_enable= &foldenable
set nofoldenable
if exists(':try')
try
silent execute a:first.','.a:last. 'g/'.modeline_pat.
\ '/:call FML_parse_line(matchstr(getline("."),"'.
\ escape(modeline_pat, '\\') .'"))'
" Purge the history for the search pattern just used.
call histdel('search', -1)
finally
let &foldenable = s:save_fold_enable
endtry
else " Older versions of Vim
silent execute a:first.','.a:last. 'g/'.modeline_pat.
\ '/:call FML_parse_line(matchstr(getline("."),"'.
\ escape(modeline_pat, '\\') .'"))'
" Purge the history for the search pattern just used.
call histdel('search', -1)
let &foldenable = s:save_fold_enable
endif
endfunction
" The main function {{{2
function! FirstModeLine()
if !&modeline | return | endif
let pos = line('.') . 'normal! ' . virtcol('.') . '|'
let e1 = 1+&modelines-1
let b2 = line('$') - &modelines+1
" call confirm('e1='.e1."\nb2=".b2, '&ok', 1)
if e1 >= b2
call s:Do_it_on_range(1, line('$'))
else
call s:Do_it_on_range(1, e1)
call s:Do_it_on_range(b2, line('$'))
endif
if !exists('b:this_is_new_buffer')
exe pos
else
unlet b:this_is_new_buffer
endif
" call confirm('fini!', '&ok', 1)
endfunction
" }}}2
" }}}1
" ===========================================================================
" Example of a callback function {{{1
" Version I use in my (La)TeX ftplugin
if 0
let b:ModeLine_CallBack = "TeXModeLine_CallBack"
function! TeXModeLine_CallBack(var,val)
if match(a:var, "g:TeXfile") != -1
" restore quotes around the file name
"let valu = substitute( valu, '^"\=\([[:alnum:].]\+\)"\=$', '"\1"', '' )
call TKSetTeXfileName( 2, a:val )
return 1
else
return 0
endif
endfunction
endif
" }}}1
" ===========================================================================
" vim600: set fdm=marker:

196
vim/vimrc Executable file
View File

@@ -0,0 +1,196 @@
"source $VIMRUNTIME/vimrc_example.vim
"source $VIMRUNTIME/mswin.vim
"behave mswin
if v:lang =~ "utf8$" || v:lang =~ "UTF-8$"
set fileencodings=utf-8,latin1
endif
set nocompatible " Use Vim defaults (much better!)
set backspace=indent,eol,start " allow backspacing over everything in insert mode
"set bs=2 " allow backspacing over everything in insert mode
"set backup " keep a backup file
set viminfo='20,\"50 " read/write a .viminfo file, don't store more
" than 50 lines of registers
set history=50 " keep 50 lines of command line history
set ruler " show the cursor position all the time
set tabstop=4 " make tabes to be equal to 4 space chars.
set shiftwidth=4 " for shifting by 4 when pressing tab.
set fileformat=unix " line ending is unix
set textwidth=80 " 80 char text
let g:netrw_preview = 1 " netrw open window to the right
let g:netrw_browse_split = 3 " open in a tab
" In many terminal emulators the mouse works just fine, thus enable it.
if has('mouse')
set mouse=a
endif
" a copy-past from my linux vimrc
if &term=="xterm"
set t_Co=8
set t_Sb=[4%dm
set t_Sf=[3%dm
endif
" Switch syntax highlighting on, when the terminal has colors
" Also switch on highlighting the last used search pattern.
if &t_Co > 2 || has("gui_running")
syntax on
set hlsearch
endif
" One such option is the 'hidden' option, which allows you to re-use the same
" window and switch from an unsaved buffer without saving it first. Also allows
" you to keep an undo history for multiple files when re-using the same window
" in this way. Note that using persistent undo also lets you undo in multiple
" files even in the same window, but is less efficient and is actually designed
" for keeping undo history after closing Vim entirely. Vim will complain if you
" try to quit without saving, and swap files will keep you safe if your computer
" crashes.
if has('hidden')
set hidden
endif
" Instead of failing a command because of unsaved changes, instead raise a
" dialogue asking if you wish to save changed files.
if has('confirm')
set confirm
endif
" Use visual bell instead of beeping when doing something wrong
if has('visualbell')
set visualbell
endif
" Display line numbers on the left
if has('number')
set number
endif
" Modelined are used to configure files.
set modeline
" Only do this part when compiled with support for autocommands.
if has("autocmd")
" Enable file type detection.
" Use the default filetype settings, so that mail gets 'tw' set to 72,
" 'cindent' is on in C files, etc.
" Also load indent files, to automatically do language-dependent indenting.
filetype plugin indent on
" Put these in an autocmd group, so that we can delete them easily.
augroup vimrcEx
au!
" For all text files set 'textwidth' to 78 characters.
autocmd FileType text setlocal textwidth=78
" When editing a file, always jump to the last known cursor position.
" Don't do it when the position is invalid or when inside an event handler
" (happens when dropping a file on gvim).
" Also don't do it when the mark is in the first line, that is the default
" position when opening a file.
autocmd BufReadPost *
\ if line("'\"") > 1 && line("'\"") <= line("$") |
\ exe "normal! g`\"" |
\ endif
augroup END
else
set autoindent " always set autoindenting on
endif " has("autocmd")
" Convenient command to see the difference between the current buffer and the
" file it was loaded from, thus the changes you made.
" Only define it when not defined already.
if !exists(":DiffOrig")
command DiffOrig vert new | set bt=nofile | r # | 0d_ | diffthis
\ | wincmd p | diffthis
endif
set diffexpr=MyDiff()
function MyDiff()
let opt = '-a --binary '
if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
let arg1 = v:fname_in
if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif
let arg2 = v:fname_new
if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif
let arg3 = v:fname_out
if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif
let eq = ''
if $VIMRUNTIME =~ ' '
if &sh =~ '\<cmd'
let cmd = '""' . $VIMRUNTIME . '\diff"'
let eq = '"'
else
let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
endif
else
let cmd = $VIMRUNTIME . '\diff'
endif
silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3 . eq
endfunction
"
" Loads CF5Compile
" Compile and run file if Ctrl-F5 is pressed.
"
if !exists('*CF5Compile')
runtime plugin/cf5-compiler.vim
endif
map <silent> <C-F5> :call CF5Compile()<CR>
"
" CTRL-U in insert mode deletes a lot. Use CTRL-G u to first break undo,
" so that you can undo CTRL-U after inserting a line break.
"
inoremap <C-U> <C-G>u<C-U>
"
" Move through wrapped lines instead of buffer lines.
"
noremap <Up> gk
noremap <Down> gj
imap <Up> <C-O>gk
imap <Down> <C-O>gj
"
" .gvimrc content here.
"
if has("gui_running")
colors darkblue " set color scheme
"set guifont=FreeMono:h12
set lines=50
set columns=80
if exists("+spell")
set spell
endif
set encoding=utf-8
"set fileencodings=utf-8
if exists("+colorcolumn")
set colorcolumn=+1 " color textwidth+1-th line
endif
if has("win32") || has("win64")
set guifont=Courier\ AM:h12
endif
":tab drop {file}
" Hide show menu and toolbar.
map <silent> <C-F2> :if &guioptions =~# 'm' <Bar>
\set guioptions-=m <Bar>
\set guioptions-=T <Bar>
\set showtabline=0 <Bar>
\else <Bar>
\set guioptions+=m <Bar>
\set guioptions+=T <Bar>
\set showtabline=1 <Bar>
\endif <CR>
" Open fine in a new tab.
map <silent> <C-F3> :browse tabnew %:p:h<CR>
endif