" When started as "evim", evim.vim will already have done these settings.
if v:progname =~? "evim"
  finish
endif

" Use Vim settings, rather then Vi settings (much better!).
" This must be first, because it changes other options as a side effect.
set nocompatible

" allow backspacing over everything in insert mode
set backspace=indent,eol,start

set nobackup		" do not keep a backup file, use versions instead
set nofsync     " Don't call fsync() on save. Stops vim from blocking on busy disks
set history=999		" keep 999 lines of command line history
set ruler		" show the cursor position all the time
set showcmd		" display incomplete commands
set incsearch		" do incremental searching
set ignorecase " ignore case in searches
set nowrap
"set number
":set laststatus=2

" Show possible command completions when pressing 'Tab' 
set wmnu
set wim=longest:full

" Don't use Ex mode, use Q for formatting
map Q gq

" This is an alternative that also works in block mode, but the deleted
" text is lost and it only works for putting the current register.
"vnoremap p "_dp

" 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

" 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).
  autocmd BufReadPost *
    \ if line("'\"") > 0 && line("'\"") <= line("$") |
    \   exe "normal g`\"" |
    \ endif

  augroup END

else

  set autoindent		" always set autoindenting on

endif " has("autocmd")


" colors

" PIBE
set tabstop=4
set shiftwidth=4
set expandtab
"set tabstop=8
"set noexpandtab

filetype on
:nnoremap <silent> <F8> :Tlist <CR>
:vnoremap * y/<C-R>"<CR>

:set foldmethod=manual
:map + v%zf

" make errors
:set errorformat ^=
\%-G%f:%l:\ (Each\ undeclared\ identifier\ is\ reported\ only\ once,
\%-G%f:%l:\ for\ each\ function\ it\ appears\ in.)

:set background=dark

" Visual queues
set showmatch " show matching brackets
set noerrorbells

" My own stuff:
" Improve C-n autocompletion: (vim tip 1386)
set completeopt=longest,menuone,preview,longest
inoremap <expr> <cr> pumvisible() ? "\<c-y>" : "\<c-g>u\<cr>"
inoremap <expr> <c-n> pumvisible() ? "\<lt>c-n>" : "\<lt>c-n>\<lt>c-r>=pumvisible() ? \"\\<lt>down>\" : \"\"\<lt>cr>"
inoremap <expr> <m-;> pumvisible() ? "\<lt>c-n>" : "\<lt>c-x>\<lt>c-o>\<lt>c-n>\<lt>c-p>\<lt>c-r>=pumvisible() ? \"\\<lt>down>\" : \"\"\<lt>cr>"
autocmd CursorMovedI * if pumvisible() == 0|pclose|endif
autocmd InsertLeave * if pumvisible() == 0|pclose|endif
" Also try C-X C-o, when a tags file is set!

let OmniCpp_NamespaceSearch = 1
let OmniCpp_GlobalScopeSearch = 1
let OmniCpp_ShowAccess = 1
let OmniCpp_MayCompleteDot = 1
let OmniCpp_MayCompleteArrow = 1
let OmniCpp_MayCompleteScope = 1
let OmniCpp_DefaultNamespaces = ["std", "_GLIBCXX_STD"]

" Folding:
set foldmethod=indent
set foldlevelstart=99 "Make sure we start with everything opened.

" Write .swp to /tmp
set backupdir=~/.vim/backup,/tmp
set directory=~/.vim/backup,/tmp

" Enable mouse
set mouse=n

" Jump left & right between windows
map <C-H> <C-W>h
map <C-L> <C-W>l

" have the usual indentation keystrokes still work in visual mode:
vnoremap <C-T> >
vnoremap <C-D> <LT>
vmap <Tab> <C-T>
vmap <S-Tab> <C-D>

" Mutt settings
au BufRead /tmp/mutt* :set et noautoindent nosmartindent spell

" Needed to set/reset rxvt titles
if !has("X11") && $TERM =~ "xterm" || $TERM =~ "screen" || $TERM =~ "rxvt"
	" Set the window title
	let hostname = substitute(hostname(), "\\..*$", "", "")
	au BufEnter * let &titlestring = hostname.':'."%f %r%m - Vim"
	au BufEnter * let &titleold = $USER.'@'.hostname.':'.getcwd()
	set title
	set t_ts=^[]0;
	set t_fs=^G
endif

" Abreviations
"
"Commentify one word with C-style comment markers
nmap <F9> gewi/*<ESC>wwgea*/<ESC>2<Left>
imap <F9> <ESC>gewi/*<ESC>wwgea*/<ESC>2<Left>

" Unbind the cursor keys in insert, normal and visual modes.
for prefix in ['i', 'n', 'v']
  for key in ['<Up>', '<Down>', '<Left>', '<Right>']
    exe prefix . "noremap " . key . " <Nop>"
  endfor
endfor

let freebsdfile = "/usr/src/tools/tools/editing/freebsd.vim"
if filereadable(freebsdfile)
  :source freebsdfile
  :call FreeBSD_Style()
endif

set cscopetag

" Allow the user of a per-project .vimrc / _vimrc /...
set exrc
set secure "Disallow potentially dangerous commands

" Open a quickfix window (full width) after a call to 'make'
" (But only if there are errors)
function OpenQuickFix()
  :bo cw
endfunction

au QuickfixCmdPost make call OpenQuickFix()
set makeprg=make\ -j\ 3
" Save before building
set autowriteall

" Persistent undo
set undodir=/tmp/
set undofile