" .vimrc
" cayla fauver <[email protected]>
" Created: Sat Sep 17, 2005  08:02PM

syntax on

set background=dark
set backspace=indent,eol,start
set directory=~/tmp         " temp file directory for vim
set expandtab               " expands tabs to space, ctrl-v <tab> to get a real tab
set formatoptions+=roc      " r = comment leader after enter, o = comment leader after o, c = auto wrap comments
set formatoptions-=t        " t = disable line wrapping on normal text.
set gdefault                " set all substitutes to g by default
set history=50              " none mapped commands to remember.  default is 20
set hlsearch                " highlight search terms.  temp disable with :nohlsearch
set ignorecase              " ignore case in patterns
set incsearch               " match as search pattern is typed
set laststatus=2            " status bar always shows last two lines
set nowrap                  " don't wrap text
set number
set relativenumber          " line counts above and below from current position
set shiftwidth=4            " how much to indent on >>, cindent, etc
set showcmd                 " show command feedback in last line
set showmatch               " show matching brackets on cursor over
set showmode                " show mode in last line, eg. insert, visual, etc
set smartcase               " case insensitive if all lower case, sensitive if mixed; this works with ignorecase
set statusline=%F%m%r%w\ [f=%{&ff}]\ [t=%Y]\ [l=%04l,c=%04v]
set tabstop=4               " tab = n spaces
set textwidth=72            " needed for wrapping comments
set title
set titlestring=%F%(\ %M%)
set undolevels=5000         " max # of undos
set visualbell

let g:netrw_altv = 1
let g:netrw_banner = 0
let g:netrw_browse_split = 4
let g:netrw_liststyle = 3
let g:netrw_winsize = 25
map <silent> <F2> :call ToggleVExplorer()<CR>

" try to exlude pattens that might not be real words.  i.e. programming
syn match myExCapitalWords +\<[A-Z]\w*\>+ contains=@NoSpell
syn match myExNonWords +\<\p*[^A-Za-z \t]\p*\>+ contains=@NoSpell

" bindings function keys
" clear search
map <silent> <F1> :let @/ = ""<CR>
" don't bork idents on paste
noremap <F6> :set invpaste paste?<CR>
set pastetoggle=<F6>
map <F9> :setlocal spell! spelllang=en_us<CR>

" trim trailing whitespace
nnoremap <silent> <Leader>tt :let _s=@/<Bar>:%s/\s\+$//e<Bar>:let @/=_s<Bar>:nohl<CR>
" format json
noremap <Leader>fj :%!python -m json.tool<CR>
" sudo :w :)
cmap w!! w !sudo tee > /dev/null %

filetype plugin indent on
filetype on
augroup filetype
  au BufNewFile,BufRead *.txt,README*,mutt*[0-9] set filetype=human
augroup END

au FileType human               set noautoindent nosmartindent formatoptions-=r textwidth=72
au FileType c,cpp,java          let b:comment_leader = '// '
au FileType sh,make,perl,python let b:comment_leader = '# '
au FileType vim                 let b:comment_leader = '" '
au FileType gitcommit           set wrap textwidth=72 fo+=t
au FileType gitcommit           setlocal spell spelllang=en_us spellcapcheck=
au FileType gitcommit           call setpos('.', [0, 1, 1, 0])

" try to clear search on load and exit.  on exit is pre writing viminfo
au VimEnter,GUIEnter,BufNewFile,BufRead,VimLeavePre * let @/ = ""

" lets do it
inoremap  <Up>     <NOP>
inoremap  <Down>   <NOP>
inoremap  <Left>   <NOP>
inoremap  <Right>  <NOP>
noremap   <Up>     <NOP>
noremap   <Down>   <NOP>
noremap   <Left>   <NOP>
noremap   <Right>  <NOP>

" FUNCTIONS
" https://www.reddit.com/r/vim/comments/62s3fl/tmux_breaks_vim_colors/dfou3u7/
if &term =~ '256color'
  " Disable Background Color Erase (BCE) so that color schemes work
  " properly within 256-color terminals
  set t_ut=
endif

autocmd BufReadPost *
\ if line("'\"") > 0 && line("'\"") <= line("$") |
\   exe "normal! g`\"" |
\ endif

autocmd BufEnter * let &titlestring = hostname() . "-vim %F%(\ %M%) "
" http://vim.wikia.com/wiki/Automatically_quit_Vim_if_quickfix_window_is_the_last
au BufEnter * call MyLastWindow()
function! MyLastWindow()
  " if the window is quickfix go on
  if &buftype=="quickfix"
    " if this window is last on screen quit without warning
    if winbufnr(2) == -1
      quit!
    endif
  endif
endfunction

" https://jordanelver.co.uk/blog/2015/05/27/working-with-vim-colorschemes/
nmap <leader>sp :call <SID>SynStack()<CR>
function! <SID>SynStack()
  if !exists("*synstack")
    return
  endif
  echo map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")')
endfunc

function! Customize()
    if filereadable(expand("~/.vim/vimrc.personal"))
        source ~/.vim/vimrc.personal
    endif
endfunction
call Customize()