set nocompatible "Don't have to try to be compatible with old vi

""""""""
"" Plugin Loading with Pathogen
"""""""""""""""""""""""""""""""""""""""""""""""""""
call pathogen#infect()


""""""""
"" Start Up
"""""""""""""""""""""""""""""""""""""""""""""""""""
function! StartUp()
    " Stuff in here will be called by autocmd below
    if 0 == argc()
        NERDTree
    end
endfunction


""""""""
"" General Behaviours
"""""""""""""""""""""""""""""""""""""""""""""""""""

set autoread "Read a file if it's changed from outside of vim
set splitbelow "New splits appear below current window instead of above
set splitright "New splits appear right of current window
set ttyfast "Smooth movement

"if version >= 700
"    set mouse=a "mouse support for 7.x, but don't use this if we use screen
"    because it has no effect, and then just becomes annoying for copy/paste
"endif 

if has("autocmd")
    " Enable filetype specific features
    filetype plugin indent on
    " Clear existing autocmd
    autocmd!
    " When editing a file, always jump to the last cursor position
    autocmd BufReadPost *
    \ if line("'\"") > 0 && line ("'\"") <= line("$") |
    \   exe "normal! g'\"" |
    \ endif
    " Source the vimrc file after saving it
    autocmd bufwritepost .vimrc source $MYVIMRC
    " so far, this startup just opens NERDTree when there are no arguments
    autocmd VimEnter * call StartUp()
else
    set autoindent on
endif

""""""""
"" Tabs and Text Formatting
""""""""""""""""""""""""""""""""""""""""""""""""""""""

set expandtab       " change to single spaces
set tabstop=4       " actual tab press distance
set shiftround      " indent to nearest tabstops
set shiftwidth=4    " amount to indent with > and <
set smarttab        " backspace tabs where appropriate even if spaces
set softtabstop=4   " let backspace delete indent
set wrap lbr        " wrap long lines of text
set backspace=eol,start,indent "backspace over everything
set textwidth=80


""""""""
"" UI - Colours
""""""""""""""""""""""""""""""""""""""""""""""""""""""

syntax enable
set t_Co=16
set background=dark
colorscheme solarized
hi Folded ctermfg=darkred "set colour for folded lines
set colorcolumn=+1


""""""""
"" UI - Numbering
""""""""""""""""""""""""""""""""""""""""""""""""""""""""

set number "show line numbers
set ruler "show row,col count in status line
set rulerformat=%55(%{strftime('%a\ %b\ %e\ %I:%M\ %p')}\ %5l,%-6(%c%V%)\ %P%)
nnoremap <silent> <expr> $ ScreenMovement("$")
set laststatus=2 "always show a status line
"set relativenumber "current line always 0 (requires 7.3 and up)


""""""""
"" UI - Code Folding
""""""""""""""""""""""""""""""""""""""""""""""""""""""""

set foldmethod=indent
set foldlevel=7
set foldtext=MyFoldText()

function! MyFoldText()
    let line = getline(v:foldstart)
    let indent = indent(v:foldstart)
    let indentOnly = strpart(line, 0, indent-1)
    let linecount = v:foldend+1 - v:foldstart
    let plural = ""
    if linecount != 1
        let plural = "s"
    endif
    let foldtext = '+'.indentOnly.'... ('.linecount.' More lines)'
    return foldtext
endfunction


""""""""
"" UI - Search
""""""""""""""""""""""""""""""""""""""""""""""""""""""""

set hlsearch    " make searches highlighted
set incsearch   " vim will search as you type!
set ignorecase  " ignore case for searches
set smartcase   " well, unless a user puts in uppercase search characters
set magic       " enables wildcard searching


""""""""
"" Key Remaps and Shortcuts 
""""""""""""""""""""""""""""""""""""""""""""""""""""""""

let mapleader = "," "Leader key lets you make more kinds of shortcuts!

" More convenient escape
imap ii <Esc>
imap II <Esc>

" Add extra lines up and down
nmap <leader>j o<Esc>k
nmap <leader>k O<Esc>j

" Toggle NERDTree instead of the normal dir browser
nmap <silent> <leader>d   :NERDTreeToggle<CR>

" Comment out lines with NERDCommenter

" Edit .vimrc
map <leader>v   :e $MYVIMRC<CR>

""""""""
"" Key Remaps - Movement and Windows
""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" Smart way to move btw. windows
map <C-j> <C-W>j
map <C-k> <C-W>k
map <C-h> <C-W>h
map <C-l> <C-W>l

" window resizing
noremap + <C-w>10+
noremap - <C-w>10-

" mapping to make movements operate on 1 screen line in wrap mode
function! ScreenMovement(movement)
    if &wrap
        return "g" . a:movement
    else
        return a:movement
    endif
endfunction

onoremap <silent> <expr> j ScreenMovement("j")
onoremap <silent> <expr> k ScreenMovement("k")
onoremap <silent> <expr> 0 ScreenMovement("0")
onoremap <silent> <expr> ^ ScreenMovement("^")
onoremap <silent> <expr> $ ScreenMovement("$")
nnoremap <silent> <expr> j ScreenMovement("j")
nnoremap <silent> <expr> k ScreenMovement("k")
nnoremap <silent> <expr> 0 ScreenMovement("0")
nnoremap <silent> <expr> ^ ScreenMovement("^")


""""""""
"" Plugin options
""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" let g:NERDTreeQuitOnOpen = 1
let g:SuperTabDefaultCompletionType = "context"
