+""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
+" Handle options only available in Vim 7 and above.
+""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
+if version >= "700" "{{{1
+version 7.0
+
+" Helper to show tab name.
+fun! <SID>TabName(label, gui) "{{{2
+ let l:label = a:label
+ if l:label == ""
+ let l:label = "No Name"
+ if a:gui
+ let l:label = "[" . l:label . "]"
+ endif
+ else
+ let l:label = fnamemodify(l:label, ":t")
+ if strlen(l:label) >= 18
+ let l:label = l:label[0:17] . ".."
+ endif
+ endif
+ return l:label
+endfun "}}}2
+
+" Find out if any buffer was modified.
+fun! <SID>TabModified(buflist) "{{{2
+ let l:i = 0
+ while l:i < len(a:buflist)
+ if getbufvar(a:buflist[l:i], "&modified") == 1
+ return "+"
+ endif
+ let l:i = l:i + 1
+ endwhile
+ return ""
+endfun "}}}2
+
+" Tab line.
+fun! Show_TabLine() "{{{2
+ let l:s = "%#TabLineFill#Tabs:"
+
+ let l:i = 0
+ while l:i < tabpagenr("$")
+ let l:i = l:i + 1
+ " Get the label.
+ let l:buflist = tabpagebuflist(l:i)
+ let l:winnr = tabpagewinnr(l:i)
+ let l:n = tabpagewinnr(l:i, "$")
+ let l:label = <SID>TabName(bufname(l:buflist[l:winnr - 1]), 0)
+ let l:modified = <SID>TabModified(l:buflist)
+
+ " Choose highlighting.
+ if l:i == tabpagenr()
+ let l:s .= "%#TabLineSel#[" . l:n . l:modified . " " . l:label . "]"
+ else
+ let l:s .= "%#TabLine# " . l:n . l:modified . " " . l:label . " "
+ endif
+ endwhile
+
+ " Padding.
+ let l:s .= "%#TabLine#%T"
+ return l:s
+endfun "}}}2
+
+" Per tab label for the GUI.
+fun! Show_GUITabLine() "{{{2
+ let l:buflist = tabpagebuflist(v:lnum)
+ let l:winnr = tabpagewinnr(v:lnum)
+ let l:s = tabpagewinnr(v:lnum, "$")
+ let l:label = <SID>TabName(bufname(l:buflist[l:winnr - 1]), 1)
+ let l:modified = <SID>TabModified(l:buflist)
+
+ let l:s .= l:modified . " " . l:label
+ return l:s
+endfun "}}}2
+
+" Toggle highlighting cursor line when focus changes.
+fun! <SID>ToggleCursorLine() "{{{2
+ call Iain_Vars()
+
+ 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
+
+" 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()
+endif
+
+if has("autocmd")
+ au StatusLine CursorHoldI * call Highlight_StatusLine("H")
+ au StatusLine CursorMovedI * call Highlight_StatusLine("h")
+ au StatusLine FocusGained * call Highlight_StatusLine("F")
+ au StatusLine FocusLost * call Highlight_StatusLine("f")
+ au StatusLine InsertEnter * call Highlight_StatusLine("I")
+ au StatusLine InsertLeave * call Highlight_StatusLine("i")
+
+ if has("syntax")
+ au Display FocusGained,FocusLost * call <SID>ToggleCursorLine()
+ endif
+
+ if has("signs")
+ 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.
+if has("insert_expand")
+ se pumheight=20
+endif
+
+" Make diffs vertical by default.
+if has("diff")
+ se diffopt+=vertical
+endif
+
+" Set size of numbers column.
+if has("linebreak")
+ se numberwidth=5
+endif
+
+" Add "previous tab" mapping as gb.
+map gb :tabprevious<CR>:<CR>
+
+" Transparency.
+if has("gui_macvim")
+ se transparency=15
+endif
+
+" Yet more GUI options. Add tabs.
+if has("gui")
+ se go+=e
+endif
+
+" Perforce.
+let g:p4EnableMenu=1
+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.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
+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
+
+ if has("cursorbind")
+ au Display WinEnter * if &diff | se cursorbind | endif
+ endif
+endif
+endif "}}}1
+
+" Resize after startup.
+if version >= "500" "{{{1
+if has("autocmd")
+ au Display VimEnter * call Startup_Resize()
+endif
+endif "}}}1