Show virtualedit status in statusline.
[profile.git] / .vimrc
diff --git a/.vimrc b/.vimrc
index a589ea0..d28d4e5 100644 (file)
--- a/.vimrc
+++ b/.vimrc
@@ -134,6 +134,8 @@ if has("autocmd")
   endif
   augroup StatusLine
   autocmd!
+  augroup File
+  autocmd!
   augroup END
 endif
 
@@ -204,6 +206,7 @@ fun! Iain_Vars() "{{{2
   call Prep_Var("g:iainextracolumnsnumber", "''")
   call Prep_Var("g:iainextracolumnslist", "''")
   call Prep_Var("b:iaincul", 0)
+  call Prep_Var("b:iainalt", 0)
   if has("signs")
     call Prep_Var("g:marksigns", 0)
     call Prep_Var("g:firstsign", 100)
@@ -449,6 +452,8 @@ call Mapping(">", ":call Resize_Columns('+')<CR>:<CR>")
 call Mapping("<", ":call Resize_Columns('-')<CR>:<CR>")
 " Clear search pattern with \/.
 call Mapping("/", ":let @/=\"\"<CR>:<CR>")
+" Toggle alternate buffer name with \#.
+call Mapping("#", ":call Cycle_Alt()<CR>:<CR>")
 
 " Set graphical window title.
 if has("win32") || has("win64")
@@ -659,13 +664,58 @@ fun! Show_Paste() "{{{2
   endif
 endfun "}}}2
 
+" Helper for status line.
+" Show v when virtualedit mode is block, insert or onemore.
+" Show V when virtualedit mode is all.
+fun! Show_VirtualEdit() "{{{2
+  if ! has("virtualedit")
+    return ""
+  endif
+
+  if &ve == "all"
+    return "V"
+  elseif &ve
+    return "v"
+  else
+    return ""
+  endif
+endfun "}}}2
+
+" Helper for status line.
+" Show U when persistent undo is on.
+" Show u when persistent undo is off but an undofile exists.
+fun! Show_Undo() "{{{2
+  if ! exists("&undofile")
+    return ""
+  endif
+
+  if &undofile
+    return "U"
+  elseif filereadable(undofile(expand("%")))
+    return "u"
+  else
+    return ""
+  endif
+endfun "}}}2
+
+" Helper for status line.
+" Show alternate buffer number and name.
+fun! Show_Alt() "{{{2
+  let l:alt = bufnr("#")
+  if l:alt < 0 || l:alt == bufnr("") || ! b:iainalt
+    return ""
+  endif
+
+  return " " . l:alt . ": " . expand("#:t")
+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()}%Y%M%R]\ '
+  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:hexformat='%b'
   if b:iainhex
@@ -949,6 +999,13 @@ fun! Cycle_Quickfix() "{{{2
   endif
 endfun "}}}2
 
+" Toggle showing alternate buffer information.
+fun! Cycle_Alt() "{{{2
+  call Iain_Vars()
+  let b:iainalt = ! b:iainalt
+  call Show_StatusLine()
+endfun "{{{2
+
 " Swap hex/decimal statusline with \x.
 call Mapping("x", ":call Cycle_HexStatusLine()<CR>:<CR>")
 " Change statusline verbosity with \v.
@@ -1060,6 +1117,44 @@ fun! <SID>ToggleCursorLine() "{{{2
   endif
 endfun "}}}2
 
+" Handle searching in a BufExplorer window.
+fun! <SID>BufExplorer_Search(n) "{{{2
+  if a:n == 0
+    let l:re = '^  *\d %'
+  else
+    let l:re = "^ *" . a:n
+  endif
+
+  " Find matching line.
+  let l:line = search(l:re, 'w')
+  if ! l:line
+    return
+  endif
+
+  if a:n == 0
+    return
+  endif
+
+  " Peek ahead to the next matching line.
+  let l:next = search(l:re, 'w')
+
+  " Select the buffer if the match is unambiguous.
+  if l:next == l:line
+    exe "normal \<CR>"
+    return
+  endif
+
+  " Go back.
+  call cursor(l:line, 0)
+endfun! "}}}2
+
+" Entering a BufExplorer window.
+fun! <SID>BufExplorer_Map() "{{{2
+  for l:n in [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" ]
+    exec "nnoremap <buffer> <silent>" . l:n . " :call <SID>BufExplorer_Search(" . l:n . ")<CR>"
+  endfor
+endfun "}}}2
+
 if has("windows")
   se tabline=%!Show_TabLine()
   se guitablabel=%!Show_GUITabLine()
@@ -1081,6 +1176,8 @@ if has("autocmd")
     au Signs InsertEnter * call <SID>Highlight_Signs()
     au Signs InsertLeave * call <SID>Highlight_Signs()
   endif
+
+  au Mode BufEnter \[BufExplorer\] call <SID>BufExplorer_Map()
 endif
 
 " Limit the size of the popup menu when completing.
@@ -1118,6 +1215,93 @@ let g:p4Presets='P4CONFIG'
 " BufExplorer.
 let g:bufExplorerShowRelativePath=1
 let g:bufExplorerSplitOutPathName=0
+
+" NERDcommenter.
+let g:NERDSpaceDelims=1
+endif "}}}1
+
+""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
+" Handle options only available in Vim 7.3 and above.
+""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
+if version >= "703" "{{{1
+version 7.3
+
+" Toggle persistent undo with \u.
+call Mapping("u", ":call <SID>Cycle_Undo()<CR>:<CR>")
+" Remove persistent undo file with \U.
+call Mapping("U", ":call <SID>Clear_Undo()<CR>:<CR>")
+
+" Use a persistent undo file if it exists.
+fun! <SID>Check_Undo() "{{{2
+  if filereadable(undofile(expand("%")))
+    setlocal undofile
+  endif
+endfun "}}}2
+
+" Toggle persistent undo for this buffer.
+fun! <SID>Cycle_Undo() "{{{2
+  if has("persistent_undo")
+    let &undofile = ! &undofile
+  endif
+endfun "}}}2
+
+" Remove the persistent undo file for this buffer.
+fun! <SID>Clear_Undo() "{{{2
+  if ! has("persistent_undo")
+    return
+  endif
+
+  setlocal noundofile
+
+  let l:f = expand("%")
+  if l:f == ""
+    return
+  endif
+
+  let l:u = undofile(l:f)
+  if l:u == ""
+    return
+  endif
+
+  if ! filereadable(l:u) || ! filewritable(l:u)
+    return
+  endif
+
+  call delete(l:u)
+endfun "}}}2
+
+" Toggle ColorColumn at cursor position.
+fun! <SID>Cycle_ColorColumn() "{{{2
+  if ! has("syntax")
+    return
+  endif
+
+  let l:cc = &colorcolumn
+  let l:column = col(".")
+  let l:re = ",*\\<" . l:column . "\\>"
+  if l:cc =~# l:re
+    let l:cc = substitute(l:cc, l:re, "", "g")
+  else
+    let l:cc = l:cc . "," . l:column
+  endif
+  let &colorcolumn = substitute(l:cc, "^,*", "", "")
+endfun "}}}2
+
+if has("syntax")
+  " Enable showing ColorColumn at cursor position with \CC.
+  call Mapping("CC", ":call <SID>Cycle_ColorColumn()<CR>:<CR>")
+  " Remove last shown ColorColumn with \Cc.
+  call Mapping("Cc", ":let &colorcolumn=substitute(&colorcolumn, \",*[0-9]*$\", \"\", \"\")<CR>:<CR>")
+  " Remove all ColorColumns with \Cx.
+  call Mapping("Cx", ":se colorcolumn=<CR>:<CR>")
+endif
+
+" Use persistent undo if available.
+if has("autocmd")
+  if has("persistent_undo")
+    au File BufReadPost * call <SID>Check_Undo()
+  endif
+endif
 endif "}}}1
 
 " Resize after startup.