Vim tip 99.
[profile.git] / .vimrc
diff --git a/.vimrc b/.vimrc
old mode 100755 (executable)
new mode 100644 (file)
index 5a3622a..2b6167a
--- a/.vimrc
+++ b/.vimrc
+""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
 " $Id$
+""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
+" Multi-version vimrc compatible with version 4 and above.
+""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
+
+" Note that "if <condition> | call Something() | endif" syntax is unsupported 
+" in Vim 4 so we write all our functions out the long way.  It does work in 
+" autocommand definitions, however.
+
+" Vim 4 complains if version isn't set in the configuration file.
+version 4.0
+
+""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
+" Handle options safe to use in version 4.  Vim 4 parses but ignores the 
+" "if version" syntax used later in this file so we don't use it.  No attempt 
+" is made to make this configuration compatible with Vim 3.
+""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
+" No compatibility mode.
+se nocp
+
+" Tabstop 2.
 se ts=2
-se bs=2
-se sw=2
+" And use spaces not tabs.
 se expandtab
+" And << and >> indent by 2.
+se sw=2
+
+" Allow backspace to delete before start of line.
+se bs=2
+
+" Show the ruler.
 se ruler
+" Show partial commands in the ruler.
+se showcmd
+" And always show the status line.
+se laststatus=2
+
+" Use C indent style.
 se cindent
 se cinkeys=0{,0},0),:,!^F,o,O,e
-se showcmd
-se go=agilmrtT
+se cinoptions=b1,c2
+
+" GUI options.
+se go=aglmr
+
+" Don't be bugged by messages at the bottom of the screen.
+se shm=aot
+
+" Find as you type.
 se incsearch
+
+" Case-insensitive search.
 se ignorecase
+" But override by typing capitals.
 se smartcase
-se shm=aot
-se laststatus=2
-syn enable
-if has("gui_running")
-  se guifont=Bitstream\ Vera\ Sans\ Mono\ 12
-  colo darkblue
+
+" Look for ctags in home directory first.
+se tags=~/.tags,./tags,tags
+
+" Don't timeout waiting to interpet, eg, <ESC>OA as an escape code.
+se ttimeoutlen=100
+
+""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
+" Handle options only available in Vim 5 and above.
+""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
+if version >= "500"
+version 5.0
+
+" Tell Vim we use dark backgrounds in our terminals.
+if ! has("gui_running")
+  se bg=dark
 endif
-highlight StatusLine guifg=white guibg=blue ctermbg=white ctermfg=blue
-if has("win32")
-  se guifont=Bitstream_Vera_Sans_Mono:h10:cANSI
+
+" Enable tab-completion prompting for commands.
+se wildmenu
+" Don't list object files when globbing files to load.
+se wildignore+=*.o,*.obj
+" So there's no need to assign them low priority.
+se suffixes-=*.o,*.obj
+
+" Vim 5 hardcodes the size of numbers column to 8.
+let numberwidth=8
+
+" Save sessions in UNIX format with / as file separator.  This is
+" cross-platform.
+se ssop+=unix,slash
+
+" Nuke any pre-existing autocommands.
+autocmd!
+
+" Save the current window width so we can restore it when we quit.
+let oldcols=&columns
+
+" More GUI options.  Add icon, tearoffs and toolbar.
+se go+=itT
+
+" Allow dynamic window resize even if we aren't in an xterm.
+se t_WS=\e[8;%p1%d;%p2%dt
+
+" Highlight search results.
+se hlsearch
+
+" Syntax highlighting.  New versions will use syn enable instead.
+if version < 600
+  syn on
 endif
-:autocmd!
 
-" Initialise list format per buffer.
-au BufEnter * let b:iainlist = 0
+" Use a discernably different colour to highlight the cursor which shows 
+" matching brackets.  Our regular cursor is green so use blue instead of cyan.
+hi MatchParen ctermbg=blue
 
-fun Cycle_List()
-  let b:iainlist += 1
-  if b:iainlist > 2 | let b:iainlist = 0 | endif
-  if b:iainlist == 0
-    set nolist
-  elseif b:iainlist == 1
-    set lcs=tab:\\_,trail:_,extends:<,precedes:>
-    set list
-  else
-    set lcs=tab:\\_,trail:_,extends:<,precedes:>,eol:$
-    set list
+" Catch typos.
+command! W :w
+command! Wq :wq
+command! Wqa :wqa
+
+" Set up our variables.
+fun! Iain_Vars()
+  if ! exists("b:iainlist")
+    let b:iainlist = 0
+  endif
+  if ! exists("b:iainhex")
+    let b:iainhex = 0
+  endif
+  if ! exists("b:iainverbose")
+    let b:iainverbose = 0
+  endif
+  if ! exists("b:iainstatus")
+    " Window Flags: (F)ocused, (I)nsert mode, Cursor (H)old.
+    let b:iainstatus = "Fih"
   endif
 endfun
 
-fun Show_List()
+" Helper for status line.
+" Show space, underscore or dollar sign depending on list mode.
+fun! Show_List()
+  call Iain_Vars()
   if b:iainlist == 0
     " No list.
-    return "  "
+    return " "
   elseif b:iainlist == 1
     " Just tabs.
-    return "\\_"
+    return "_"
   else
     " Full list.
-    return "\.\$"
+    return "\$"
+  endif
+endfun
+
+" Helper for status line.
+" Show c or C to denote case-sensitivity.
+fun! Show_Case()
+  if &ic
+    return "c"
+  else
+    return "C"
   endif
 endfun
 
-" Save the current window width so if we change it we can restore it
-" when we quit.
-let andyoldcols=&columns
+" Helper for status line.
+" Show the size of the tabstop.
+fun! Show_Tabstop()
+  return &ts
+endfun
 
-" This expands the terminal to display two 80-column files side-by-side
-" when vim is opened in diff mode.
-if &diff | let &columns = 164 | endif
+" Helper for status line.
+" Show p when paste mode is on.
+fun! Show_Paste()
+  if &paste
+    return "p"
+  else
+    return ""
+  endif
+endfun
 
-map <C-"> viwvbi"<ESC>ea"<ESC>
-map - yyp:s/./-/g<RETURN>:let @/=''<RETURN>:<RETURN>
-map = yyp:s/./=/g<RETURN>:let @/=''<RETURN>:<RETURN>
-command W :w
-se tags=~/.ctags
+" Show the status line.
+fun! Show_StatusLine()
+  call Iain_Vars()
+  let sl1='%2n\:\ %<%f\ [%{Show_List()}%{Show_Case()}%{Show_Tabstop()}%{Show_Paste()}%Y%M%R]\ %='
+  let sl3='L:%4.6l/%-4.6L\ C:%3.6c\ \|\ %P'
+  let hexformat='%b'
+  if b:iainhex
+    let hexformat='0\x%02B'
+  endif
+  if b:iainverbose
+    let sl2=hexformat . '\ \|\ P:%4.6o\ '
+  else
+    let sl2=''
+  endif
+  exec "set statusline=" . sl1 . sl2 . sl3
+endfun
 
-fun Invert_Case()
+" Toggle case-sensitivity.
+fun! Invert_Case()
   let &ic = ! &ic
 endfun
 
-fun Show_Case()
-  if &ic | return "ca" | else | return "Ca" | endif
+" Restore window size.
+au VimLeave * if exists("oldcols") | let &columns=oldcols | endif
+
+" Map Makefile mode.
+au BufEnter * if &ft == "make" | call MakeMode_map() | endif
+au BufLeave * if &ft == "make" | call MakeMode_unmap() | endif
+
+" Entering Make mode.
+fun! MakeMode_map()
+  set list
+  set noexpandtab
 endfun
 
-set statusline=%2n\:\ %<%t\ [%{Show_List()}][%{Show_Case()}]%y%m%r%=%b\ 0\x%02B\ (%3.6c,%-4.6l)\ %4.6L\ %P
-" Swap case-sensitivity with ,c.
-map ,c :call Invert_Case()<CR>:<CR>
-" Cycle list styles with ,l.
-map ,l :call Cycle_List()<CR>:<CR>
+" Leaving Make mode.
+fun! MakeMode_unmap()
+  set nolist
+  set expandtab
+endfun
 
-au VimLeave * if exists("andyoldcols") | let &columns=andyoldcols | endif
+" Show the status line for the first time.
+call Show_StatusLine()
 
-" Autocommands to setup features we only want in certain modes...
+" Function to create mappings with either a hardcoded \ or <Leader>.
+fun! Mapping(keysequence,mapping)
+  if version >= "600"
+    exec "map \\" . a:keysequence . " " . a:mapping
+  else
+    exec "map <Leader>" . a:keysequence . " " . a:mapping
+  endif
+endfun
 
-" ... For C/C++ files:
+" Use - and = to create underlines.
+call Mapping("-", "yyp:s/./-/g<RETURN>:let @/=''<RETURN>:<RETURN>")
+call Mapping("=", "yyp:s/./=/g<RETURN>:let @/=''<RETURN>:<RETURN>")
 
-au BufEnter * if &ft == "c" || &ft == "cpp" | call CMode_map() | endif
-au BufLeave * if &ft == "c" || &ft == "cpp" | call CMode_unmap() | endif
+" Change to ts=2 with \2.
+call Mapping("2", ":se ts=2<CR>:<CR>")
+" Change to ts=4 with \4.
+call Mapping("4", ":se ts=4<CR>:<CR>")
+" Change to ts=8 with \8.
+call Mapping("8", ":se ts=8<CR>:<CR>")
+" Change to ts=16 with \6.
+call Mapping("6", ":se ts=16<CR>:<CR>")
+" Change to ts=32 with \3.
+call Mapping("3", ":se ts=32<CR>:<CR>")
+" Toggle paste mode with \p.
+call Mapping("p", ":se paste!<CR>:<CR>")
+" Swap case-sensitivity with \c.
+call Mapping("c", ":call Invert_Case()<CR>:<CR>")
+" Change number mode with \n.
+call Mapping("n", ":se number!<CR>:<CR>")
+" Expand or shrink window size with \> and \<.  For use after toggling number.
+call Mapping(">", ":exe 'se columns+=' . numberwidth<CR>:<CR>")
+call Mapping("<", ":exe 'se columns-=' . numberwidth<CR>:<CR>")
+" Clear search pattern with \/.
+call Mapping("/", ":let @/=\"\"<CR>:<CR>")
 
-" ... For Perl files:
+" Forget the Ex mode mapping.
+map Q <NOP>
 
-au BufEnter * if &ft == "perl" | call PerlMode_map() | endif
-au BufLeave * if &ft == "perl" | call PerlMode_unmap() | endif
+" Vim tip 99: What's the highlighting group under the cursor?
+call Mapping("h", ":echo \"hi<\" . synIDattr(synID(line(\".\"),col(\".\"),1),\"name\") . '> trans<' . synIDattr(synID(line(\".\"),col(\".\"),0),\"name\") . \"> lo<\" . synIDattr(synIDtrans(synID(line(\".\"),col(\".\"),1)),\"name\") . \">\"<CR>")
 
-" ... For PHP files:
+endif
 
-au BufEnter * if &ft == "php" | call PHPMode_map() | endif
-au BufLeave * if &ft == "php" | call PHPMode_unmap() | endif
+""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
+" Handle options only available in Vim 6 and above.
+""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
+if version >= "600"
+version 6.0
 
-" ... For makefiles:
+" Remember quickfix state.
+let g:quickfixing=0
 
-au BufEnter * if &ft == "make" | call MakeMode_map() | endif
-au BufLeave * if &ft == "make" | call MakeMode_unmap() | endif
+" Set indenting by filetype.
+filetype indent on
+
+" Less intrusive syntax highlighting.
+syn enable
+
+" Nice GUI colour.
+if has("gui_running")
+  se guifont=DejaVu\ Sans\ Mono\ 10
+  colo darkblue
+elseif &t_Co > 16
+  try
+    colo iain
+  catch
+  endtry
+endif
+if has("win32")
+  se guifont=DejaVu_Sans_Mono:h10:cANSI
+endif
 
-" Functions to call when we enter/leave a programming language buffer...
+" Ignore whitespace when diffing.
+se diffopt=filler,iwhite
 
-" ... For C-like languages:
+" Expand window when doing a vertical diff.
+if &diff
+  let &columns = 164
+endif
 
-fun CMode_map()
-  set cinkeys=0{,0},:,0#,!^F,o,O,e
-  set cinwords=if,else,while,do,for,switch
-endfun
+" Numbers in blue.
+highlight LineNr term=underline cterm=bold guifg=blue ctermfg=blue
 
-fun CMode_unmap()
-endfun
+" Remember that we are opening the quickfix window.
+au BufWinEnter quickfix let g:quickfixing=1
+au BufUnload * if &ft == "qf" | let g:quickfixing=0 | endif
 
-" .. For Perl files:
+" Make * and # work the way you expect in visual mode.
+vnoremap * y/\V<C-R>=substitute(escape(@@,"/\\"),"\n","\\\\n","ge")<CR><CR>
+vnoremap # y?\V<C-R>=substitute(escape(@@,"?\\"),"\n","\\\\n","ge")<CR><CR>
 
-fun PerlMode_map()
-  set cinkeys=0{,0},:,!^F,o,O,e
-  set cinwords=if,else,while,do,for,eval
-  set foldmethod=indent
+" Change list mode.
+fun! Cycle_List()
+  let basic='tab:\\_,trail:_,extends:<,precedes:>'
+  call Iain_Vars()
+  let b:iainlist = b:iainlist + 1
+  if b:iainlist > 2
+    let b:iainlist = 0
+  endif
+  if b:iainlist == 0
+    set nolist
+  elseif b:iainlist == 1
+    exec "set lcs=" . basic
+    set list
+  else
+    exec "set lcs=" . basic . ",eol:$"
+    set list
+  endif
 endfun
 
-fun PerlMode_unmap()
-  set foldmethod=manual
+" Cycle between hex and decimal display of toolbar stuff.
+fun! Cycle_HexStatusLine()
+  call Iain_Vars()
+  let b:iainhex = ! b:iainhex
+  call Show_StatusLine()
 endfun
 
-fun PHPMode_map()
-  set nocindent
-  set autoindent
-  set foldmethod=indent
+" Cycle verbose display of toolbar stuff.
+fun! Cycle_VerboseStatusLine()
+  call Iain_Vars()
+  let b:iainverbose = ! b:iainverbose
+  call Show_StatusLine()
 endfun
 
-fun PHPMode_unmap()
-  set noautoindent
-  set cindent
-  set foldmethod=manual
+" Toggle quickfix window.
+fun! Cycle_Quickfix()
+  if g:quickfixing == 1
+    cclose
+    let g:quickfixing=0
+  else
+    copen
+  endif
 endfun
 
-" ... For makefiles:
+" Swap hex/decimal statusline with \x.
+call Mapping("x", ":call Cycle_HexStatusLine()<CR>:<CR>")
+" Change statusline verbosity with \v.
+call Mapping("v", ":call Cycle_VerboseStatusLine()<CR>:<CR>")
+" Cycle list styles with \l.
+call Mapping("l", ":call Cycle_List()<CR>:<CR>")
+" Toggle tags with \t.
+call Mapping("t", ":Tlist<CR>")
+" Change foldmethod with \f.
+call Mapping("f", ":se foldenable!<CR>:<CR>")
+" Toggle quickfix window with \q.
+call Mapping("q", ":call Cycle_Quickfix()<CR>:<CR>")
+" Rerun filetype detection with \s.  The s is for syntax, as this will be
+" updated as a side-effect.
+call Mapping("s", ":filetype detect<CR>:<CR>")
 
-fun MakeMode_map()
-  set list
-  set noexpandtab
-endfun
+" Change status bar colour when various things happen.
+fun! Highlight_StatusLine(flag)
+  " Get current status.
+  call Iain_Vars()
 
-fun MakeMode_unmap()
-  set nolist
-  set expandtab
+  " Change the status based on the flag.  XXX: Does Vim let us to do flags?
+  let re = "[" . tolower(a:flag) . toupper(a:flag) . "]"
+  let b:iainstatus = substitute(b:iainstatus, re, a:flag, "")
+
+  " Default colour.
+  let s:colour = "darkblue"
+  let s:termcolour = ""
+  let s:term88colour = "17"
+  let s:term256colour = "17"
+  " Maybe override depending on status.
+  if b:iainstatus =~# "H"
+    if b:iainstatus =~# "I"
+      " Held in insert mode.  Add extra highlight if we don't have focus.
+      if b:iainstatus =~# "f"
+        let s:colour = "darkmagenta"
+      else
+        let s:colour = "darkred"
+      endif
+      let s:term88colour = "32"
+      let s:term256colour = "88"
+    endif
+  else
+    if b:iainstatus =~# "I"
+      " Regular insert mode.
+      let s:colour = "darkred"
+      let s:term88colour = "32"
+      let s:term256colour = "88"
+    endif
+  endif
+
+  if &t_Co == 88
+    let s:termcolour = s:term88colour
+  elseif &t_Co == 256
+    let s:termcolour = s:term256colour
+  else
+    let s:termcolour = s:colour
+  endif
+
+  exec "highlight StatusLine guifg=white guibg=" . s:colour . " ctermbg=white ctermfg=" . s:termcolour
 endfun
+
+call Highlight_StatusLine("")
+endif
+
+""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
+" Handle options only available in Vim 7 and above.
+""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
+if version >= "700"
+version 7.0
+
+au CursorHoldI * call Highlight_StatusLine("H")
+au CursorMovedI * call Highlight_StatusLine("h")
+au FocusGained * call Highlight_StatusLine("F")
+au FocusLost * call Highlight_StatusLine("f")
+au InsertEnter * call Highlight_StatusLine("I")
+au InsertLeave * call Highlight_StatusLine("i")
+
+" Make diffs vertical by default.
+se diffopt+=vertical
+
+" Set size of numbers column.
+se numberwidth=5
+
+" Add "previous tab" mapping as gb.
+map gb :tabPrev<CR>
+endif