korya /.vimrc

" *vimrc*
"
" 1 |common-settings|
" 2 |jsettings|
" 3 |tags|
" 4 |mappings|
" 5 |gui_running|
" 6 |omni-completion|

"====================================================================
"						*common-settings*
filetype plugin on
" Using mouse as in gvim
" set mouse=a
set nocompatible
set wildmenu
set ttyfast
set nu
set autoindent
set smartindent
set incsearch
set nohlsearch
set nowrap
set backspace=indent,eol,start
set autoread
set shiftwidth=4
set tabstop=8
syntax on

" перед вставкой из внешнего clipboard нажимать эту самую . 
" Прощайте, тесты лесенкой.
set pastetoggle=

au! BufNewFile,BufRead *.h if &ft == 'cpp' | set ft=c | endif
if &ft == 'c'
    set cindent
endif

"====================================================================
"						*jsettings*
" Highlighting over 80 chars
" set textwidth=80
" au! BufNewFile,BufRead * exec 'match Todo /\%>' . &textwidth . 'v.\+/'

" Taking the dynamic highlighting one step further
set textwidth=80
au BufNewFile,BufRead *.c,*.h,*.cpp exec 'match Todo /\%>' . &textwidth . 'v.\+\|.,[^ ]\|while(\| ,\| )\|( \|[|&]  \|  [|&]\|\n\n\n\|;* \n\|[^ ][!=]=[^ ]\|=[^ =]\|[^ !=<>|&^+]=\|for(\| ;\n/'

" Is this an openrg
let g:is_openrg = 0
function! IsOpenRG()
    if stridx(expand("%:p"), $HOME."/rg/") == 0
	let g:is_openrg = 1
    else
	let g:is_openrg = 0
    endif
endfunction
au BufNewFile,BufRead  *.c,*.h,*.cpp call IsOpenRG()

"====================================================================
"						*tags*
function! TagsFile()
    if g:is_openrg == 1
	" tags file is /pkg/tags
	let &tags = substitute(expand("%:p:h"), "/pkg.*$", "", "") . "/tags"
    else
	set tags=tags
	set tags+=/usr/include/tags
    endif
endfunction
au BufNewFile,BufRead  *.c,*.h,*.cpp call TagsFile()

function! MyCtagsProg()
    let prog = "ctags -R ."
    if g:is_openrg == 1
	let prog = "true"
"         let prog = ". ~/.bashrc; jctags " . expand("%")
    endif
    return prog
endfunction
au BufWritePost,FileWritePost *.c,*.h sil exe "normal \:! " . MyCtagsProg() . "\"

"====================================================================
"						*mappings*
" Comfortable PageUp PageDown
nnoremap  
nnoremap  
inoremap  a
inoremap  a
vnoremap  
vnoremap  
" Navigating windows by C-j C-k C-h C-l
noremap  h
noremap  j
noremap  k
noremap  l
inoremap  h
inoremap  j
inoremap  k
inoremap  l

" vmap / y/"

runtime ftplugin/man.vim

let g:EnhCommentifyPretty = 'Yes' 
let g:EnhCommentifyMultiPartBlocks = 'Yes'
let g:EnhCommentifyCommentsOp = 'Yes'
nnoremap  :call EnhancedCommentify('', 'guess')j
inoremap  :call EnhancedCommentify('', 'guess')ji
vnoremap  :call EnhancedCommentify('', 'guess', 
		\ line("'<"), line("'>"))

nnoremap  :call DistClean()
inoremap  :call DistClean()
vnoremap  :call DistClean()

function! DistClean()
    sil exe "normal \:wa\"
    set makeprg=make\ distclean\ &&\ BUILD=UML-24\ make\ config\ DIST=UML\ &&\ make
    let choise = confirm("Are you sure? Do you really want to make DISTCLEAN?!", "\&Yes.\n\&no, just kidding.", 2)
    if choise == 1
	exe "normal \:make\"
    endif
endfunction

au BufRead,BufNewFile * let curr_dir=expand("%:p")
nnoremap  :wa:set makeprg=.\ ~/.bashrc;\ jqmake\ %:make
inoremap  :wa:set makeprg=make\ $(echo\ %\\\|grep\ -o\ 'pkg/[^/]*')\ &&\ make\ -C\ pkg/main\ &&\ make\ ramdisk\ &&\ make\ -C\ os:make
vnoremap  :wa:set makeprg=make\ $(echo\ %\\\|grep\ -o\ 'pkg/[^/]*')\ &&\ make\ -C\ pkg/main\ &&\ make\ ramdisk\ &&\ make\ -C\ os:make

" Opens file in current buffer, and goes to lin line and col column
" TODO: Check whether vim has a such function
function! OpenFileOnPos(fname, lin, col)
    exe "normal \:e " . a:fname . "\"
    exe "normal " . a:lin . "G"
    exe "normal " . a:col . "|"
endfunction

" Converts .c to .h and reverse
" TODO: Find more correct way to get extension
function! Source2HeaderAndReverse(fname)
    let ext = a:fname[-2:]
    if ext == '.c' || ext == '.h'
	return substitute(a:fname, ext . "$", ext == '.c' ? '.h' : '.c', "")
    endif
    return ""
endfunction

" Toggles from .c file to .h and reverse. Stores the last locations
" into g:ToggleHeaderLastLine and g:ToggleHeaderLastPos.
" NOTE: I think that g:ToggleHeaderLastLine and g:ToggleHeaderLastPos will work
" only if we will not work with this func for other files.
" TODO: Check whether arrays exist in vim, or hash, to store there these values
" for corresponding files.
let g:ToggleHeaderLastLine = 0
let g:ToggleHeaderLastPos = 0
function! ToggleHeader(fname)
    let req_line = g:ToggleHeaderLastLine
    let req_col = g:ToggleHeaderLastPos
    let g:ToggleHeaderLastLine = line(".")
    let g:ToggleHeaderLastPos = col(".")

    call OpenFileOnPos(Source2HeaderAndReverse(a:fname), req_line, req_col)
endfunction

nmap \hh :call ToggleHeader(expand("%:p"))

"====================================================================
"						*gui_running*
if has("gui_running")
    set guioptions=aegit


    map  :sil exe ':!cvsdiff ' . TokenUnderCursor(1) . '&'

    " functions
    function! IsTokenChar(is_fname, char)
	if a:char == "_"
	    return 1
	endif

	if a:char >= "0" && a:char <= "9"
	    return 1
	endif

	if a:char >= "A" && a:char <= "Z"
	    return 1
	endif

	if a:char >= "a" && a:char <= "z"
	    return 1
	endif

	if a:is_fname && (a:char == "/" || a:char == "." || a:char == "-")
	    return 1
	endif
	return 0
    endfunction

    function! TokenUnderCursor(is_fname)
	echo ""
	let line = getline(".")
	let pos = col(".") - 1

	let begpos = pos -1
	while IsTokenChar(a:is_fname, strpart(line, begpos, 1)) == 1
	    let begpos = begpos - 1
	endwhile

	let endpos = pos + 1
	while IsTokenChar(a:is_fname, strpart(line, endpos, 1)) == 1
	    let endpos = endpos + 1
	endwhile

	return strpart(line, begpos+1, endpos-begpos-1)
    endfunction

endif " if has("gui_running")

"====================================================================
"						*omni-completion*
" let OmniCpp_NamespaceSearch = 0
autocmd FileType c set omnifunc=ccomplete#Complete

function! CloseScratch()
    if bufname("%")==""
	:exe ":bdelete ".bufnr("%")
    endif
endfunc
" inoremap ) :windo exe ":call CloseScratch()"a)