" Max's .vimrc
" Honourary mention to Gary Bernhardt


""""""""""""""""""""""
" Plugins
""""""""""""""""""""""

execute pathogen#infect()

""""""""""""""""""""""
" Basic editing config
""""""""""""""""""""""

set term=xterm-256color

set nocompatible
colorscheme maxbucknell

" Line numbering
set relativenumber
set number

" Set editor shell to bash, for Syntastic compatibility
set shell=bash

" Tab config options
set expandtab
set tabstop=2
set shiftwidth=2
set softtabstop=2
set autoindent

" So done with this
set nobackup
set nowritebackup
set noswapfile

" I am lazy and I don't like holding shift.
noremap ; :
noremap ;; ;

" Paste!
noremap <leader>v "+gp
noremap <leader>V "+gP
noremap <leader>c "+y
noremap <leader>C "+Y

" Quick exit insert mode
inoremap hh <esc>
inoremap <esc> <nop>

" Bad arrow keys
inoremap <up> <nop>
inoremap <down> <nop>
inoremap <left> <nop>
inoremap <right> <nop>
noremap <up> <nop>
noremap <down> <nop>
noremap <left> <nop>
noremap <right> <nop>

" Move lines up and down
noremap - ddp
noremap _ ddkP

" Uppercase an entire word with <c-u>
inoremap <c-u> <esc>viwUa
nnoremap <c-u> viwU

" Show me when my lines are too long
call matchadd('ColorColumn', '\%81v', 100)

" Prevent K from being annoying
noremap K <nop>

" Edit and Reload .vimrc files
nmap <silent> <Leader>ev :e $MYVIMRC<CR>
nmap <silent> <Leader>es :so $MYVIMRC<CR>

" Search options
" Show partial matches while searching
set incsearch

" Highlight other matches in the file
set hlsearch

" Show the next search result.
" By Damian Conway.
"
" This rewires n and N to do the highlighing...
nnoremap <silent> n   n:call HLNext(0.2)<cr>
nnoremap <silent> N   N:call HLNext(0.2)<cr>

" Blink the next match
function! HLNext (blinktime)
    let [bufnum, lnum, col, off] = getpos('.')
    let matchlen = strlen(matchstr(strpart(getline('.'),col-1),@/))
    let target_pat = '\c\%#'.@/
    let ring = matchadd('MBSearchNext', target_pat, 101)
    redraw
    exec 'sleep ' . float2nr(a:blinktime * 1000) . 'm'
    call matchdelete(ring)
    redraw
endfunction

" Disable match-paren
" Re-enabled for now, to remember why I turned it off
" let loaded_matchparen = 1
set showmatch

" Make searches case sensitive only if an upper case character has been typed
set ignorecase smartcase

" Prevent Vim from clobbering the scrollback buffer. See
" http://www.shallowsky.com/linux/noaltscreen.html
set t_ti= t_te=

" Highlight current line
set cursorline

" Ensure that the cursor never touches top or bottom of screen
set scrolloff=10

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

" display incomplete commands
set showcmd

" Allow hidden buffers
set hidden

" Enable highlighting for syntax
syntax on

" 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

" Insert only one space when joining lines that contain sentence-terminating
" punctuation like `.`.
set nojoinspaces

" If a file is changed outside of vim, automatically reload it without asking
set autoread

" Show trailing whitespace, since it's a crime
set list
set listchars=trail:·,tab:‣\ 

" Turn off code folding
set foldmethod=manual
set nofoldenable
let g:vim_markdown_folding_disabled=1

" Always show status bar
set laststatus=2

" Pastetoggle to let Vim paste things without auto stuff
set pastetoggle=<F2>

"""""""""""""""""
" Custom autocmds
"""""""""""""""""

augroup vimrcEx
  " Clear all autocmds in the group
  autocmd!
  autocmd FileType text setlocal textwidth=78
  " Jump to last cursor position unless it's invalid or in an event handler
  autocmd BufReadPost *
    \ if line("'\"") > 0 && line("'\"") <= line("$") |
    \   exe "normal g`\"" |
    \ endif

  " Language whitespace settings
  autocmd FileType json,c,xml,java,php,python setlocal shiftwidth=4 softtabstop=4
  autocmd FileType ruby,haml,eruby,yaml,html,javascript,sass,cucumber setlocal ai sw=2 sts=2 et
  autocmd FileType make setlocal noet sw=8 sts=8 ts=8

  " Create files when opened
  autocmd BufNewFile * write
augroup END


"""""""""""""
" STATUS LINE
"""""""""""""
" set statusline=%<[%n]:\ %f\ %5l,%3c\ (%{&ft})
set statusline=[%f] " filename
set statusline+=\ [%l,\ %c] "line and column number
set statusline+=\ %y " filetype
set statusline+=\ %{SyntasticStatuslineFlag()} " syntastic errors


"""""""""""""""""""""
" Configure Syntastic
"""""""""""""""""""""

let g:syntastic_enable_signs = 0
let g:syntastic_check_on_open = 1
let g:syntastic_javascript_checkers = ['eslint']
let g:syntastic_stl_format='[%t errors, first: %F]'
nnoremap <space> :SyntasticCheck<cr>

"""""""""""""""
" MISC KEY MAPS
"""""""""""""""

noremap <leader>y "*y
nnoremap <leader>. <c-^>

" Move around splits with <c-hjkl>
nnoremap <c-j> <c-w>j
nnoremap <c-k> <c-w>k
nnoremap <c-h> <c-w>h
nnoremap <c-l> <c-w>l


""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" MULTIPURPOSE TAB KEY
" Indent if we're at the beginning of a line. Else, do completion.
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
function! InsertTabWrapper()
  let col = col('.') - 1
  if !col || getline('.')[col - 1] !~ '\k'
    return "\<tab>"
  else
    return "\<c-p>"
  endif
endfunction

inoremap <tab> <c-r>=InsertTabWrapper()<cr>
inoremap <s-tab> <c-n>

" I don't like the quote concealing
let g:vim_json_syntax_conceal = 0

" Show syntax highlighting groups for word under cursor
nmap <leader>\ :call <SID>SynStack()<CR>
function! <SID>SynStack()
  if !exists("*synstack")
    return
  endif
  echo map(synstack(line('.'), col('.')), 'synIDattr(v:val,"name")')
endfunc

