set autoindent
set modeline
set background=dark
set encoding=utf-8
set history=1000
set showcmd
set laststatus=2
set virtualedit=block
set hidden
set suffixes+=.pdf
set listchars=eol:$,tab:⊫—,trail:,extends:»,precedes:«,nbsp:·
if has("syntax")
  set statusline=%<[#%n]\ %f%y%H%M%R%W\ %=U+%04B\ %{synIDattr(synID(line('.'),col('.'),1),'name')}\ %-10.(%l,%c%V%)\ %P
else
  set statusline=%<[#%n]\ %f%y%H%M%R%W\ %=%-14.(%l,%c%V%)\ %P
endif

if 1
  let c_comment_strings=1
  " <Leader>c in insert mode is a bit much
  let g:EnhCommentifyUserBindings='Yes'
endif


if has("syntax")
  syntax on
  filetype plugin indent on
  " Use Posix syntax for the 'sh' filetype.
  let g:is_posix = 1
endif

" Set palette for godlygeek's cterm-hex patch.
if exists("+termpalette")
  set termpalette=xterm
endif

" Incrementally-highlighted search.  Use Ctrl-L to suppress highlight.
if has("extra_search")
  set hlsearch
  set incsearch
  nnoremap <C-l> :nohl<CR><C-l>
  if !&insertmode
    inoremap <C-l> <C-O>:nohl<CR>
  endif
endif

highlight Tab ctermbg=Blue
if has("syntax")
  match Tab /\t/
endif

" Use sudo to write a file, if we opened it without appropriate permissions
if 1
  command! SW exe "w !sudo dd of=%" | e!
endif

" :NN and :PP (with optional !): like :n/:N, but quit after the last (before
"   the first) file.
if 1
  command! -bar -bang -count=1 NN if argidx() < argc()-<count> | <count>n<bang> | else | q<bang> | endif
  command! -bar -bang -count=1 PP if argidx() >= <count> | <count>N<bang> | else | q<bang> | endif
endif

" :Run <shellcmd>: run a shell command, placing its output in a new window.
if 1
  command! -nargs=+ -complete=shellcmd Run new | set buftype=nofile | r ! <args>
endif

if has("insert_expand")
  " Syntax-complete if that's the best we can do
  if has("autocmd")
    autocmd Filetype * if &omnifunc == "" | setlocal omnifunc=syntaxcomplete#Complete | endif
  endif

  " Omni-complete with ctrl-space
  set cot+=longest
  inoremap <expr> <C-Space> pumvisible() ? "\<C-n>" : "\<C-x>\<C-o>"
endif

" Ctrl-Up and Ctrl-Down: scroll through the quickfix list.
nnoremap <C-Up> :cp<CR>
nnoremap <C-Down> :cn<CR>
inoremap <C-Up> <C-o>:cp<CR>
inoremap <C-Down> <C-o>:cn<CR>

" :Man command.
runtime! ftplugin/man.vim

" I never write plaintex.
if 1
  let tex_flavor="latex"
endif


" <F7> to save and build, <F12> to rebuild tags
" First, the versions for vim.tiny
nnoremap <F7> :wa<CR>:!make<CR>
nnoremap <silent> <F12> :!ctags -R .<CR>:redr!<CR>

" And the improved versions
if 1
" Compile without prompting and open the quickfix window if necessary.
  command! -bar -complete=file -nargs=* Make silent! make <args> | cw | redr!
  nnoremap <F7> :wa<CR>:Make<CR>
  nnoremap <F12> :silent! !ctags -R .<CR>:redr!<CR>
endif

if 1
  " Execute the current line.
  nnoremap @;; "pyy:@p<CR>
  " Operator to execute a command from the buffer.  Uses "p and '', sorry.
  nnoremap @; m':silent set opfunc=ExecRegion<CR>g@
  vnoremap @; :<C-u>call ExecRegion('vis')<CR>
  function! ExecRegion(type, ...)
    if a:type == "vis"
      " Yank and execute the visual region.
      :'<,'> y p
      @p
    else
      " remember the buffer
      let l:origbuf = bufnr("%")

      " Yank and execute the operator region.
      :'[,'] y p
      @p

      " If we're in the same buffer as before, keep our position
      if bufnr("%") == l:origbuf && has("ex_extra")
        norm g``
      endif
    endif
  endfun
endif

if 1
  nnoremap g\j :silent! set opfunc=JoinOp<CR>g@
  vnoremap g\j J
  function! JoinOp(type)
    '[,'] j
  endfun
endif

if $VIM_PAGER_MODE != ""
  nnoremap q :q<CR>
  nnoremap Q q<CR>
  nnoremap <C-Up> ?^commit \x\+$<CR>zt
  nnoremap <C-Down> /^commit \x\+$<CR>zt
endif

" C-c prefix for VCS commands.  Normal mode only.
" The abbreviations differ from those with \c prefix.
nnoremap <C-c>a :VCSAdd<CR>
nnoremap <C-c>A :VCSAnnotate<CR>
nnoremap <C-c>c :VCSCommit<CR>
nnoremap <C-c>d :VCSDiff<CR>
nnoremap <C-c>D :VCSVimDiff<CR>
nnoremap <C-c>l :VCSLog<CR>
nnoremap <C-c>o :VCSGotoOriginal<CR>
nnoremap <C-c>R :VCSRevert<CR>
nnoremap <C-c>s :VCSStatus<CR>
nnoremap <C-c>u :VCSUpdate<CR>
nnoremap <C-c><C-c> :VCSGotoOriginal!<CR>

if has("textobjects")
  " iT to select an inner tagblock in visual mode without the leading/trailing
  " newline.  This is useful for XML/HTML/etc like the following:
  "    <tag>
  "    content paragraph that should be selected
  "    </tag>
  vnoremap iT itk$oj^O

  " Format the current iT tagblock: join, indent, and re-split.
  nmap gqP viTJ==Vgq

  " Pseudo-textobject for An Indented region (all the surrounding lines with
  " at least as much indent as the current one).  Needs much work.
  vnoremap ai :<C-u>. <bar> let ai_re='[^[:space:]].*\%' . virtcol(".") . 'v'<CR>gv?<C-r>=ai_re<CR><CR>j^o/<C-r>=ai_re<CR><CR>k$
endif

if has("digraphs")
  " Digraphs for schwa.
  digr ee 601 " ə
  digr Ee 399 " Ə
  digr EE 398 " Ǝ
endif

if 1
  " Open a line indented to the current column.
  nnoremap <Leader>o :let b:ospaces=virtcol(".")-1<CR>o<Esc>:exe "norm " . b:ospaces . "i "<CR>:.retab!<CR>A
  nnoremap <Leader>O :let b:ospaces=virtcol(".")-1<CR>O<Esc>:exe "norm " . b:ospaces . "i "<CR>:.retab!<CR>A
endif

if 1
  let g:csapprox_no_warn=1
  let g:CSApprox_verbose_level=0
endif