" Use vim settings, instead of vi
set nocompatible

set ts=4
set sw=4
set bs=2

set nobackup
set noswapfile
set modeline
set ruler
set history=50
set nowrap

" Change terminal title
set title
" No annoying error noises
set noerrorbells
" Make backspace delete lots of things
set backspace=indent,eol,start
" Show us the command we're typing
set showcmd
" Highlight matching parens
set showmatch
" Search options: incremental search, highlight search
set hlsearch
set incsearch
" Selective case insensitivity
set smartcase
" Show full tags when doing search completion
set showfulltag
" Speed up macros
set lazyredraw
" No annoying error noises
set noerrorbells
" Wrap on these
set whichwrap+=<,>,[,]
" Use the cool tab complete menu
set wildmenu
set wildmode=longest,full
set wildignore+=*.o,*~
set suffixes+=.in,.a
" Allow edit buffers to be hidden
set hidden
" 1 height windows
set winminheight=1
" misc
set autowrite
set autochdir
set ttyfast
set smartcase

set complete-=i

if has("autocmd")
	filetype plugin indent on
else
	set ai
endif

" Activer la coloration
if &t_Co > 2 || has("gui_running")
	syntax on
	set nohls
endif

if has("gui_running")
	colorscheme slate

	set guifont=Monospace\ 8
	set guioptions-=T
	set guioptions-=m
	set guioptions-=l
	set guioptions-=L
	set guioptions-=r
	set guioptions-=R
	set mouse=ar
	set mousemodel=extend
	set mousefocus
	set mousehide
	set noguipty
	set guicursor=a:blinkon0

	highlight Normal     gui=NONE guibg=Black guifg=White
	highlight NonText    gui=NONE guibg=Black
	highlight Pmenu      gui=NONE guifg=Black guibg=LightGrey
	highlight PmenuSel   gui=NONE guifg=LightGrey guibg=Black
	highlight PmenuSbar  gui=NONE guifg=LightGrey guibg=Black
	highlight PmenuThumb gui=NONE guifg=Black guibg=LightGrey 
endif

function! CleverTab(direction)
	let idx = col('.') - 1
	let str = getline('.')

	if a:direction == "forward" && idx >= 2 && str[idx - 1] == ' ' && str[idx - 2] =~? '[a-z]'
		if &softtabstop && idx % &softtabstop == 0
			return "\<BS>\<Tab>\<Tab>"
		else
			return "\<BS>\<Tab>"
		endif
	elseif idx == 0 || str[idx - 1] !~? '[a-z]'
		return "\<Tab>"
	elseif "backward" == a:direction
		return "\<C-P>"
	else
		return "\<C-N>"
	endif
endf

function! ClosePair(char)
	if getline('.')[col('.') - 1] == a:char
		return "\<Right>"
	else
		return a:char
	endif
endf

inoremap <Tab> <C-R>=CleverTab("forward")<CR>
inoremap <S-Tab> <C-R>=CleverTab("backward")<CR>


if has("gui_running")
	inoremap <expr> <Esc>      pumvisible()?"\<C-E>":"\<Esc>"
	inoremap <expr> <CR>       pumvisible()?"\<C-Y>":"\<CR>"
	inoremap <expr> <Down>     pumvisible()?"\<C-N>":"\<Down>"
	inoremap <expr> <Up>       pumvisible()?"\<C-P>":"\<Up>"
	inoremap <expr> <PageDown> pumvisible()?"\<PageDown>\<C-P>\<C-N>":"\<PageDown>"
	inoremap <expr> <PageUp>   pumvisible()?"\<PageUp>\<C-P>\<C-N>":"\<PageUp>"
endif

inoremap <C-Space> <C-X><C-O>

inoremap ( ()<ESC>i
inoremap ) <c-r>=ClosePair(')')<CR>
inoremap { {}<ESC>i
inoremap } <c-r>=ClosePair('}')<CR>
inoremap [ []<ESC>i
inoremap ] <c-r>=ClosePair(']')<CR>

" Paste
map <S-Insert> <MiddleMouse>
map! <S-Insert> <MiddleMouse>

" Change buffer
map <C-N> :bn<CR>
map <C-P> :bp<CR>

" Shell like Home / End
cnoremap <C-A>    <Home>
cnoremap <C-E>    <End>

" Hide coloration of found words
map <C-C> :nohlsearch<CR>

" Tabs
map <C-t> :tabnew<CR>
map <C-Tab> :tabnext<CR>
map <C-S-Tab> :tabprevious<CR>