Use ^w-^ to switch to the last used window.
[profile.git] / .vimrc
diff --git a/.vimrc b/.vimrc
index d28d4e5..28d060e 100644 (file)
--- a/.vimrc
+++ b/.vimrc
@@ -2,16 +2,16 @@
 " Multi-version vimrc compatible with version 4 and above.   vim:set fdm=marker:
 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
 
-" 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 
+" 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 
+" 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.
 " Some of these settings should strictly be wrapped inside "if has()" blocks
 " but that would cause them not to be ignored by Vim 4.
@@ -75,6 +75,10 @@ inoremap <C-l> <C-n><C-p>
 " Swap jump keys.
 noremap ' `
 noremap ` '
+
+" Select previous widnow.
+noremap <C-w>^ <C-w>p
+noremap <C-w><C-^> <C-w>p
 "}}}1
 
 " Find stuff.
@@ -320,7 +324,7 @@ endfun "}}}2
 " Set extra columns depending on window status.
 fun! Extra_Columns(extra, var, ...) "{{{2
   " Vim 6 doesn't have winnr("$").  Determine which windows are open
-  " ourselves by using :windo to incremement a counter.  As Vim 5 
+  " ourselves by using :windo to incremement a counter.  As Vim 5
   " doesn't have :windo we require Vim 6 for this.
   if v:version < "600"
     return ""
@@ -614,6 +618,30 @@ endif "}}}1
 if version >= "504" "{{{1
 version 5.4
 
+" Reuse windows when using sbuffer.
+se switchbuf=useopen
+
+" Do we have Unicode?
+fun! Has_Unicode() "{{{2
+  if ! has('multi_byte')
+    return 0
+  endif
+
+  if version < "602"
+    return 0
+  endif
+
+  if &tenc =~? '^u\(tf\|cs\)'
+    return 1
+  endif
+
+  if ! strlen(&tenc) && &enc =~? '^u\(tf\|cs\)'
+    return 1
+  endif
+
+  return 0
+endfun "}}}2
+
 " Helper for status line.
 " Show space, underscore or dollar sign depending on list mode.
 fun! Show_List() "{{{2
@@ -621,7 +649,7 @@ fun! Show_List() "{{{2
   if w:iainlist == 0
     " No list.
     return " "
-  elseif <SID>Has_Unicode()
+  elseif Has_Unicode()
     if w:iainlist == 1
       " Just tabs.
       return "»"
@@ -674,7 +702,7 @@ fun! Show_VirtualEdit() "{{{2
 
   if &ve == "all"
     return "V"
-  elseif &ve
+  elseif &ve != ''
     return "v"
   else
     return ""
@@ -709,14 +737,35 @@ fun! Show_Alt() "{{{2
   return " " . l:alt . ": " . expand("#:t")
 endfun "}}}2
 
+" Helper for status line.
+" Show scrollbind or cursorbind.
+fun! Show_Bind() "{{{2
+  if has("cursorbind")
+    if &cursorbind
+      if Has_Unicode()
+        return "⇄"
+      else
+        return ">"
+      endif
+    elseif &scrollbind
+      if Has_Unicode()
+        return "⇅"
+      else
+        return "^"
+      endif
+    endif
+  endif
+  return ""
+endfun "}}}2
+
 " Show the status line.
 fun! Show_StatusLine() "{{{2
   if ! has("statusline")
     return
   endif
   call Iain_Vars()
-  let l:sl1='%2n\:\ %<%1*%f%0*\ [%{Show_List()}%{Show_Case()}%{Show_Tabstop()}%{Show_Paste()}%{Show_VirtualEdit()}%{Show_Undo()}%Y%M%R]%{Show_Alt()}\ '
-  let l:sl3='L:%1*%4.6l%0*/%-4.6L\ C:%1*%3.6c%0*\ \|\ %P'
+  let l:sl1='%2n\:\ %<%1*%f%0*\ [%{Show_List()}%{Show_Bind()}%{Show_Case()}%{Show_Tabstop()}%{Show_Paste()}%{Show_VirtualEdit()}%{Show_Undo()}%Y%M%R]%{Show_Alt()}\ '
+  let l:sl3='L:%1*%4.6l%0*/%-4.6L\ C:%1*%3.6c%V%0*\ \|\ %P'
   let l:hexformat='%b'
   if b:iainhex
     let l:hexformat='0\x%02B'
@@ -864,7 +913,7 @@ fun! <SID>Cycle_Signs(resize) "{{{2
     " Signs with Type '-' will be highlighted with the MarkLine group.
     " Signs with Type '.' will be highlighted with the MarkDot group.
     " Define the Mark where Symbol is not also the mark name, eg "']".
-    if <SID>Has_Unicode()
+    if Has_Unicode()
       let g:iainsigns = "Dash:'=’ Dot:..• Quote:\"=” Caret:^.ʌ"
     else
       let g:iainsigns = "Dash=' Dot:..* Quote=\" Caret.^"
@@ -917,31 +966,10 @@ fun! <SID>Cycle_Signs(resize) "{{{2
   endif
 endfun "}}}2
 
-" Do we have Unicode?
-fun! <SID>Has_Unicode() "{{{2
-  if ! has('multi_byte')
-    return 0
-  endif
-
-  if version < "602"
-    return 0
-  endif
-
-  if &tenc =~? '^u\(tf\|cs\)'
-    return 1
-  endif
-
-  if ! strlen(&tenc) && &enc =~? '^u\(tf\|cs\)'
-    return 1
-  endif
-
-  return 0
-endfun "}}}2
-
 " Change list mode.
 fun! Cycle_List() "{{{2
   " Pretty UTF-8 listchars.
-  if <SID>Has_Unicode()
+  if Has_Unicode()
     let basic='tab:»·,trail:…,extends:«,precedes:»'
     let eol='eol:¶'
     if version >= "700"
@@ -970,6 +998,7 @@ fun! Cycle_List() "{{{2
   endif
 
   call Resize_Columns(Extra_Columns("list", "iainlist", " == 2"), 1)
+  call Extra_Whitespace_Match()
 endfun "}}}2
 
 " Cycle between hex and decimal display of toolbar stuff.
@@ -1006,6 +1035,11 @@ fun! Cycle_Alt() "{{{2
   call Show_StatusLine()
 endfun "{{{2
 
+" To be overridden later if applicable.
+fun! Extra_Whitespace_Match() "{{{2
+  " NOP.
+endfun "}}}2
+
 " Swap hex/decimal statusline with \x.
 call Mapping("x", ":call Cycle_HexStatusLine()<CR>:<CR>")
 " Change statusline verbosity with \v.
@@ -1108,12 +1142,15 @@ endfun "}}}2
 fun! <SID>ToggleCursorLine() "{{{2
   call Iain_Vars()
 
-  if b:iainstatus =~# "f" && b:iainstatus =~# "H" && b:iainstatus =~# "I"
-    " Focus lost while held in insert mode.
-    let b:iaincul = getbufvar("", "&cursorline")
-    setlocal cursorline
-  elseif ! b:iaincul
-    setlocal nocursorline
+  if b:iainstatus =~# "H" && b:iainstatus =~# "I"
+    " We are held in insert mode.
+    if b:iainstatus =~# "f"
+      " And focus was lost.
+      let b:iaincul = getbufvar("", "&cursorline")
+      setlocal cursorline
+    elseif ! b:iaincul
+      setlocal nocursorline
+    endif
   endif
 endfun "}}}2
 
@@ -1201,7 +1238,7 @@ map gb :tabprevious<CR>:<CR>
 " Transparency.
 if has("gui_macvim")
   se transparency=15
-endif 
+endif
 
 " Yet more GUI options.  Add tabs.
 if has("gui")
@@ -1221,6 +1258,55 @@ let g:NERDSpaceDelims=1
 endif "}}}1
 
 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
+" Handle options only available in Vim 7.2 and above.
+""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
+if version >= "702" "{{{1
+if has("autocmd")
+  " http://vim.wikia.com/wiki/Highlight_unwanted_spaces
+  augroup WhitespaceMatch
+  autocmd!
+  au Display BufWinEnter * call Extra_Whitespace_Match()
+  au Display Syntax * call Extra_Whitespace_Match()
+  au Display BufWinLeave * call clearmatches()
+  augroup END
+
+  fun! Extra_Whitespace_Match() "{{{2
+    " \s\+            <whitespace>
+    "            $    <before end of line>
+    "        \@<!     <unless preceded by>
+    "     \%#         <cursor position, ie when typing>
+    let l:pattern = '\s\+\%#\@<!$'
+    " Don't match single space in first column of diff.
+    if &ft =~# '^diff$\|git'
+      "         \@!  <unless also matching>
+      " \(^\s$\)     <a single whitespace>
+      let l:pattern = '\(^\s$\)\@!' . l:pattern
+    endif
+
+    let l:hl = 'ctermfg=red guifg=red'
+    if ! &list
+      " Underline if we aren't using listchars.
+      let l:hl = l:hl . ' cterm=underline gui=underline'
+    endif
+    highlight clear ExtraWhitespace
+    exe "highlight ExtraWhitespace " . l:hl
+    if exists('w:whitespace_match_number')
+      try
+        call matchdelete(w:whitespace_match_number)
+      catch
+      endtry
+      call matchadd('ExtraWhitespace', l:pattern, 10, w:whitespace_match_number)
+    else
+      let w:whitespace_match_number = matchadd('ExtraWhitespace', l:pattern)
+    endif
+  endfun "}}}2
+
+  call Extra_Whitespace_Match()
+endif
+
+endif "}}}1
+
+""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
 " Handle options only available in Vim 7.3 and above.
 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
 if version >= "703" "{{{1
@@ -1301,6 +1387,10 @@ if has("autocmd")
   if has("persistent_undo")
     au File BufReadPost * call <SID>Check_Undo()
   endif
+
+  if has("cursorbind")
+    au Display WinEnter * if &diff | se cursorbind | endif
+  endif
 endif
 endif "}}}1