" Use pathogen to use search for plug-ins
call pathogen#infect()

" Basics
set t_Co=256        " Use 256 colors
set encoding=utf8   " Use utf-8 encoding
set nocompatible    " Get out of vi-compatible mode
set background=dark " I am using a dark background
syntax on           " Syntax highlighting on

" General
"colorscheme solarized
colorscheme wombat
" setlocal spell spelllang=en_us
set fileformats=unix,dos,mac    " support all three, in this order
set nostartofline               " leave my cursor where it was
set history=1000
set autoread        " Set to auto read when a file is changed from the outside
set autochdir       " Change current directory to be directory of current file
set colorcolumn=80
set foldmethod=syntax

if $TMUX == ''
    set clipboard+=unnamed
endif

" Have tab complete work more like bash
set wildmenu
set wildmode=list:longest

set backspace=start,eol,indent
set whichwrap+=<,>,[,]

set magic       "Set magic on, for regular expressions

" Turn on omni completion. Must have `filetype plugin on` to use.
" To use, in insert mode press ctrl+x ctrl+o
set ofu=syntaxcomplete#Complete

" Mappings
let mapleader=","
let g:mapleader=","
map <leader>rl :source $MYVIMRC<CR>
map <leader>nt :NERDTreeToggle<CR>
map <leader>ss :setlocal spell!<CR>
map <leader>ct :TlistToggle<CR>     " taglist shortcut
map <leader>ts :%s/\s\s*$//g<CR>    " trim trailing whitespace
map <leader>cc :CoffeeCompile<CR>   " Complie CoffeeScript
nnoremap <leader>rc :split $MYVIMRC<cr>
" Use space to toggle folding
nnoremap <space> za

if bufwinnr(1)
    map <leader>+ <C-W><
    map <leader>- <C-W>>
endif

map <F12> ggVGg?  " apply rot13 for people snooping over shoulder, good fun

" Nerdtree settings
let NERDTreeDirArrows=1

" Prep some variables for use with syntastic syntax checker
let g:syntastic_phpcs_disable=1
let g:syntastic_auto_loc_list=1
let g:syntastic_stl_format = '[%E{Err: %fe #%e}%B{, }%W{Warn: %fw #%w}]'
"let g:syntastic_json_checker = "jsonlint"
let g:syntastic_scss_checkers = []
let g:syntastic_css_checkers = []

" Close all open buffers on entering a window if the only
" buffer that's left is the NERDTree buffer
function! s:CloseIfOnlyNerdTreeLeft()
    if exists("t:NERDTreeBufName")
        if bufwinnr(t:NERDTreeBufName) != -1
            if winnr("$") == 1
                q
            endif
        endif
    endif
endfunction

function! FoldText()
    let nl = v:foldend - v:foldstart + 1
    let linetext = getline(v:foldstart)
    let txt = linetext . ' ' . nl . ' lines '
    return txt
endfunction
set foldtext=FoldText()

" Vim Tabs
map <leader>tt :tabnew<cr>
map <leader>te :tabedit
map <leader>tc :tabclose<cr>
map <leader>to :tabonly<cr>
map <leader>tn :tabNext<cr>
map <leader>tp :tabprevious<cr>
map <leader>tf :tabfirst<cr>
map <leader>tl :tablast<cr>
map <leader>tm :tabmove

" Tabs
set expandtab       " expandtab converts tabs to spaces. For tabs, set noexpandtab
set shiftwidth=4    " unify
set softtabstop=4   " Pressing backspace works like tabs
set tabstop=4       " real tabs should be 4, but they will show with set list on

" Indent
set autoindent      "Auto indent
set smartindent     "Smart indent
set copyindent      " above all -- follow the conventions laid before us
set preserveindent  " above all -- follow the conventions laid before us
set shiftround      " when at 3 spaces, and I hit '>', go to 4, not 5
filetype plugin on   " load filetype plug-ins and indent settings
filetype indent on   " load filetype plug-ins and indent settings

" UI
set showcmd         " show the command being typed
set ruler           " Always show current positions along the bottom
set relativenumber  " Use relative line numbers
set numberwidth=4   " If we have over 9999 lines
set hidden          " you can change buffer without saving
"set mouse=a         " use mouse everywhere

" No sound on errors
set noerrorbells    " don't make noise
set novisualbell    " don't blink
set t_vb=
set tm=500

" Visual Cues
set showmatch       " show matching brackets
set matchtime=2     " how many tenths of a second to blink matching brackets for
set nohlsearch      " do not highlight searched for phrases
set incsearch       " BUT do highlight as you type you search phrase
set scrolloff=5     " Keep 5 lines (top/bottom) for scope
set sidescrolloff=5 " Keep 5 lines at the size
set list        " we do what to show tabs, to ensure we get them out of my files
"set listchars=tab:𝅔\ ,trail:… " show tabs and trailing whitespace
set listchars=tab:ˍ\ ,trail:… " show tabs and trailing whitespace

" ~\file[+] [type] [line,column] [number of lines]
set statusline=%F%m%r%h%w\ [%Y]\ [%03l,%03v]\ [%L]
"set statusline+=%#warningmsg#
"set statusline+=%{SyntasticStatuslineFlag()}
"set statusline+=%*

set laststatus=2 " always show the status line

" Text Formatting/Layout
set nowrap      " do not wrap line
set ignorecase  " case insensitive by default
set smartcase   " if there are caps, go case-sensitive
set completeopt=menu,longest,preview " improve the way autocomplete works

autocmd WinEnter * call s:CloseIfOnlyNerdTreeLeft()

autocmd FileType go autocmd BufWritePre <buffer> Fmt

augroup jade
    au! BufRead,BufNewFile *.jade set syntax=jade
augroup END

augroup styl
    au! BufRead,BufNewFile *.styl set syntax=stylus
augroup END

augroup conkyrc
    au! BufRead,BufNewFile *.conkyrc set syntax=conkyrc
augroup END

augroup vcl
    au! BufRead,BufNewFile *.vcl set filetype=vcl
augroup END

augroup mustache
    au! BufRead,BufNewFile *.mustache set filetype=mustache
    au! BufRead,BufNewFile *.mu set filetype=mustache
    au! BufRead,BufNewFile *.hjs set filetype=mustache
augroup END

augroup coffee
    au! BufRead,BufNewFile *.coffee set filetype=coffee
augroup END

augroup markdown
    au! BufRead,BufNewFile *.md set filetype=markdown
augroup END

augroup COMMIT_EDITMSG
    au! BufRead,BufNewFile COMMIT_EDITMSG set filetype=gitcommit
augroup END

autocmd FileType go autocmd BufWritePre <buffer> Fmt