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 нажимать эту самую <F11>. 
" Прощайте, тесты лесенкой.
set pastetoggle=<F11>

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 <rg_root>/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() . "\<CR>"

"====================================================================
"						*mappings*
" Comfortable PageUp PageDown
nnoremap <PageUp> <C-U><C-U>
nnoremap <PageDown> <C-D><C-D>
inoremap <PageUp> <Esc><C-U><C-U>a
inoremap <PageDown> <Esc><C-D><C-D>a
vnoremap <PageDown> <C-D><C-D>
vnoremap <PageUp> <C-U><C-U>
" Navigating windows by C-j C-k C-h C-l
noremap <C-H> <C-W>h
noremap <C-J> <C-W>j
noremap <C-K> <C-W>k
noremap <C-L> <C-W>l
inoremap <C-H> <Esc><C-W>h
inoremap <C-J> <Esc><C-W>j
inoremap <C-K> <Esc><C-W>k
inoremap <C-L> <Esc><C-W>l

" vmap / y/<C-R>"<CR>

runtime ftplugin/man.vim

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

nnoremap <F9> :call DistClean()<CR>
inoremap <F9> <Esc>:call DistClean()<CR>
vnoremap <F9> <Esc>:call DistClean()<CR>

function! DistClean()
    sil exe "normal \:wa\<CR>"
    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\<CR>"
    endif
endfunction

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

" 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 . "\<CR>"
    exe "normal " . a:lin . "G"
    exe "normal " . a:col . "|"
endfunction

" Converts <name>.c to <name>.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 <name>.c file to <name>.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"))<CR>

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


    map <F1> :sil exe ':!cvsdiff ' . TokenUnderCursor(1) . '&'<CR>

    " 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 ) <Esc>:windo exe ":call CloseScratch()"<CR>a)