"
" Vim configuration file (~/.vimrc)
" 2009, kolter <kolter@openics.org>
"
"  This file is distributed in the hope that it will be useful,
"  but WITHOUT ANY WARRANTY
"

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" General Settings
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

set nocompatible "no vi compatibility
set history=200 "command history size
set undolevels=120 "undo levels count

set autoread "notify file changes
set autochdir "switch to the current file directory
set clipboard+=unnamed "share system clipboard
set backup "enable backups
set backupdir=~/.vim/backups "backup directory
set directory=~/.vim/tmp "directory to place swap files in

"Encoding settings
if has("multi_byte")
	if &termencoding == ""
		let &termencoding = &encoding
	endif
	set encoding=utf-8
	setglobal fileencoding=utf-8 bomb
	set fileencodings=ucs-bom,utf-8,default,latin1
endif

"Syntax highlight
syntax on
set t_Co=256
set bg=dark
colorscheme xoria256


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Interface Settings
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

set title "show title
set number "show line numbers
set ruler "statusline for each window
set visualbell t_vb= "visual beep

set showcmd "show incomplete commands
set confirm "raise a dialog asking what to do instead of failing

"set mouse=a "enable mouse support
set lazyredraw "do not redraw when running macros
set ttyfast "fast terminal connection support

set shortmess=a "abbreviations on status line
set laststatus=2 "always statusline
set statusline=%<%F\ %h%m%r%y%=%{\"[\".(&fenc==\"\"?&enc:&fenc).((exists(\"+bomb\")\ &&\ &bomb)?\",B\":\"\").\"]\ \"}%k\ %-14.(%l,%c%V%)\ %P

set wildmenu "display completion menu
set wildmode=list:longest,list:full "content of completion menu
"ignore some extensions while completing filename
"set wildignore=*.o,*.so,,*.gz,*.bz2,*.tar,*.tgz,*.tbz2,*.png,*.jpg,*.jpeg,*.gif
set wildchar=<TAB> "wildcar expansion character

set modeline "allow last lines of documents set vim mode
set modelines=3 "number of lines to check for modelines


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Keyboard Settings
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" keep ctrl-a/ctrl-e for begining/end of line
nmap <C-a> <C-o>I
imap <C-a> <C-o>I
vmap <C-a> <C-o>I

nmap <C-e> <C-o>A
imap <C-e> <C-o>A
vmap <C-e> <C-o>A

"reopen current file
map <F5> :e!<CR>
"using redraw to disable highlighted search patterns
nnoremap <C-L> :nohl<CR><C-L>

"map F1 to open previous buffer
nmap <F1> :bp<CR>
imap <F1> <ESC>:bp<CR>a
"map F2 to open next buffer
nmap <F2> :bn<CR>
imap <F2> <ESC>:bn<CR>a

"imap <C-Space> <C-X><C-O> "smart completion
nmap <C-u> :u<CR>
imap <C-u> <ESC>:u<CR>

nmap <C-i> :tabnext<CR>
"nmap <C-S-i> :tabprevious<CR>
nmap <C-t> :tabnew<CR>
"nmap <C-M-w> :tabclose<CR>

nmap <C-q> :call CleanClose()<CR>

nmap <F6> :vsplit<CR>
nmap <F7> :split<CR>

"ctrl-$arrow no cycle around splitted windows
map <C-left> <C-W>h
map <C-right> <C-W>l
map <C-Up> <C-W>k
map <C-Down> <C-W>j

"ident in visual mode keep selection
vmap > >gv
vmap < <gv


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Editor Settings
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

set fileformats=unix,dos,mac "support all three, in this order
set hidden "make possible to change buffer without saving
set backspace=indent,eol,start "enhance backspacing
set report=0 "agressive reporting
set pastetoggle=<F11> "use <F11> to toggle between 'paste' and 'nopaste'
set nostartofline "leave the cursor where it is

"indent settings
set autoindent
set smartindent
set cindent

"tabs and spaces settings
set tabstop=4
set shiftwidth=4
set shiftround "round indent to shiftwidth
set softtabstop=4

set showmatch "brace/parenthese/bracket matching
set nowrap "wrap long lines
if has("multi_byte")
	set showbreak="…"
else
	set showbreak="+++"
endif

"show some not printable chars
set list
if has("multi_byte")
	set listchars=trail:·,extends:>,precedes:<,nbsp:•,tab:→\ 
else
	set listchars=tab:>.,trail:·,extends:>,precedes:<,nbsp:%
endif

"search settings
set hlsearch "highlight searches
set incsearch "incremental searches
set ignorecase "ignore case in search patterns
set infercase "smart case support

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Email Settings
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
if has('autocmd')
	autocmd BufRead ~/.mutt/tmp/mutt-* set filetype=mail
	autocmd FileType mail set autoindent expandtab formatoptions=tcqn
	autocmd FileType mail vmap D dO[...]<CR>
	autocmd FileType mail call EnableSpellChecking()
endif

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Filetypes Settings
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
filetype plugin indent on

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Global Triggers
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
if has('autocmd')
	"reload Vim config file after saving
	autocmd BufWritePost $MYVIMRC :source $MYVIMRC
	"always limit the width for text files
	autocmd BufRead *.txt set tw=78
	autocmd QuickFixCmdPre * botright cwindow 5
endif

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Spell Checking
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"set spellfile=~/.vim/spell/words
function! EnableSpellChecking()
	setlocal spell spelllang=fr,en_us
	let b:spell = 1
endfunction

function! DisableSpellChecking()
	setlocal nospell
	unlet b:spell
endfunction

function! ToggleSpell()
	if !exists("b:spell")
		execute 'call EnableSpellChecking()'
	else
		execute 'call DisableSpellChecking()'
	endif
endfunction

nmap <F4> :call ToggleSpell()<CR>
imap <F4> <Esc>:call ToggleSpell()<CR>a


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" TagList setup
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Settings for taglist.vim
let Tlist_Use_Right_Window=1
let Tlist_Auto_Open=0
"let Tlist_Enable_Fold_Column=0
let Tlist_Compact_Format=1
"let Tlist_WinWidth=28
let Tlist_Exit_OnlyWindow=1
let Tlist_File_Fold_Auto_Close=1
let Tlist_Process_File_Always=1
"dip F1 to open previous buffersplay tags only for the current buffer
let Tlist_Show_One_File=1
let Tlist_Use_SingleClick=1
let Tlist_Ctags_Cmd="~/.vim/bin/ctags-exuberant"
nnoremap <silent> <F9> :Tlist<CR>


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Useful functions
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
function! CleanClose()
	let todelbufNr = bufnr("%")
	let newbufNr = bufnr("#")
	if ((newbufNr != -1) && (newbufNr != todelbufNr) && buflisted(newbufNr))
		exe "b".newbufNr
	else
		bnext
	endif
	if (bufnr("%") == todelbufNr)
		new
	endif
	exe "bd".todelbufNr
endfunction