+
+ if ! Can_Resize()
+ return
+ endif
+
+ if a:0 == 0
+ " Vim 5 hardcodes the size of numbers column to 8.
+ if version >= "700" && has("linebreak")
+ let l:columns = &numberwidth
+ else
+ let l:columns = 8
+ endif
+ else
+ let l:columns = a:1
+ endif
+
+ exe "let l:resize=" . &columns . a:op . l:columns
+ let l:resize = "se columns=" . l:resize
+
+ " HACK: Inside screen there is an extra line for the status bar. Vim
+ " manages the resize by sending an escape sequence to set the number of
+ " lines and number of columns in one action. To do this it will first query
+ " the number of lines and then set <same number of lines> by <new number of
+ " columns>. Because of the extra line for the status bar this results in
+ " the real terminal being shrunk by a line. We ask for the terminal to grow
+ " by a line so it ends up actually being the same.
+ if &term =~ '^screen'
+ let l:resize = l:resize . " lines=" . (&lines + 1)
+ endif
+
+ exe l:resize
+endfun "}}}2
+
+" Grow or shrink the window height.
+fun! Resize_Lines(op, lines) "{{{2
+ if a:op == ""
+ return
+ endif
+
+ if ! Can_Resize()
+ return
+ endif
+
+ exe "let l:resize=" . &lines . a:op . a:lines
+ if &term =~ '^screen'
+ let l:resize = l:resize + 1
+ endif
+ let l:resize = "se lines=" . l:resize
+
+ exe l:resize
+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
+ " doesn't have :windo we require Vim 6 for this.
+ if v:version < "600"
+ return ""
+ endif
+ if ! has("windows")
+ return ""
+ endif
+
+ " Remember which window we're in.
+ let l:winnr = winnr()
+ let l:num_windows = 0
+ windo let l:num_windows = l:num_windows + 1
+ " Switch back to the window we were in.
+ exe l:winnr . "wincmd w"
+
+ call Iain_Vars()
+
+ if a:0 == 0
+ let l:condition = ""
+ else
+ let l:condition = a:1
+ endif
+
+ let l:n = 0
+ let l:i = 1
+ let l:windows = ""
+ while l:n < l:num_windows
+ " If window w exists then getwinvar(w, "&modified") will be 0 or 1.
+ if getwinvar(l:i, "&modified") =~ '^\d'
+ let l:n = l:n + 1
+
+ let l:val = 0
+ exe "if getwinvar(" . l:i . ", '" . a:var . "') " . l:condition . " | let l:val = 1 | endif"
+ if l:val
+ exe "let l:windows = '" . l:windows . ":" . l:i . "'"
+ endif
+ endif
+ let l:i = l:i + 1
+ endwhile
+
+ let l:extra = "g:iainextracolumns" . a:extra
+ exe "let l:val = " . l:extra
+ exe "let " . l:extra . " = '" . l:windows . "'"
+
+ if l:windows == l:val
+ return ""
+ endif
+
+ if l:windows == ""
+ return "-"
+ elseif l:val == ""
+ return "+"
+ endif
+endfun "}}}2
+
+" Toggle number display.
+fun! Number(resize) "{{{2
+ call Iain_Vars()
+ let &number = ! &number
+
+ " Ensure we keep track of any extra columns even if we aren't resizing.
+ " This prevents confusion when number is set at startup.
+ let l:extra = Extra_Columns("number", "&number")
+
+ if a:resize
+ call Resize_Columns(l:extra)
+ endif
+endfun "}}}2
+
+" Restore window size.
+if has("autocmd") && ! has("gui_running")
+ au Display VimLeave * if exists("g:oldcols") | call Resize_Columns("-", (&columns - g:oldcols)) | endif
+ au Display VimLeave * if exists("g:oldlines") | call Resize_Lines("-", (&lines - g:oldlines)) | endif
+endif
+
+" Map Makefile mode.
+if has("autocmd")
+ au Mode BufEnter * if &ft == "make" | call MakeMode_map() | endif
+ au Mode BufLeave * if &ft == "make" | call MakeMode_unmap() | endif
+endif
+
+" Entering Make mode.
+fun! MakeMode_map() "{{{2
+ call Iain_Vars()
+ let w:iainlist=1
+ call Cycle_List()
+ set ts=8
+ set noexpandtab
+endfun "}}}2
+
+" Leaving Make mode.
+fun! MakeMode_unmap() "{{{2
+ call Cycle_List()
+ set ts=2
+ set expandtab
+endfun "}}}2
+
+" Function to create mappings with either a hardcoded \ or <Leader>.
+fun! Mapping(keysequence,mapping) "{{{2
+ if version < "600"
+ exec "map \\" . a:keysequence . " " . a:mapping
+ else
+ exec "map <Leader>" . a:keysequence . " " . a:mapping
+ endif
+endfun "}}}2
+
+" Use - and = to create underlines.
+call Mapping("-", "yyp:s/./-/g<RETURN>:let @/=''<RETURN>:<RETURN>")
+call Mapping("=", "yyp:s/./=/g<RETURN>:let @/=''<RETURN>:<RETURN>")
+
+" 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", ":call Number(1)<CR>:<CR>")
+" Expand or shrink window size with \> and \<.
+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")
+ " Windows taskbar entries are probably too small to show full titles.
+ se titlestring=%t
+else
+ se titlestring=%{Show_TitleString()}
+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>")
+
+fun! Uncluttered_Buffer() "{{{2
+ if exists("uncluttered_buffer")
+ if uncluttered_buffer == 1
+ return 1
+ endif
+ endif
+
+ if version >= "600"
+ if &buftype != ''
+ return 1
+ endif
+ endif
+
+ if &ft == 'perforce'
+ return 1
+ endif
+
+ if &ft == 'svn'
+ return 1
+ endif
+
+ if &ft == 'gitcommit'
+ return 1
+ endif
+
+ return 0
+endfun "}}}2
+
+fun! Startup_Resize() "{{{2
+ let l:columns = 0
+
+ " Resize for numbers.
+ if &number
+ if version >= "700" && has("linebreak")
+ let l:columns = &numberwidth
+ else
+ let l:columns = 8
+ endif
+ endif
+
+ " Resize for signs.
+ if has("signs")
+ if g:marksigns
+ if version >= "600"
+ let l:columns = l:columns + 2
+ endif
+ endif
+ endif
+
+ if g:oldcols < (80 + l:columns)
+ call Resize_Columns("+", l:columns)
+ endif
+endfun "}}}2
+
+" Change status bar colour when various things happen.
+" Flags: H/h: Cursor held/moved.
+" F/f: Focus gained/lost.
+" I/i: Insert mode entered/left.
+fun! Highlight_StatusLine(flag) "{{{2
+ if ! has("statusline")
+ return
+ endif
+ " Get current status.
+ call Iain_Vars()
+
+ " Change the status based on the flag. XXX: Does Vim let us to do flags?
+ let l:ic = &ic
+ set ic
+ let b:iainstatus = substitute(b:iainstatus, a:flag, a:flag, "")
+ let &ic = l:ic
+
+ let l:normalcolour = "darkblue"
+ let l:editingcolour = "darkmagenta"
+ let l:warningcolour = "darkred"
+ let l:readonlycolour = "red"
+
+ " Default colour.
+ let l:colour = l:normalcolour
+ " 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 l:colour = l:warningcolour
+ else
+ let l:colour = l:editingcolour
+ endif
+ endif
+ else
+ if b:iainstatus =~# "I"
+ " Regular insert mode.
+ let l:colour = l:editingcolour
+ endif
+ endif
+
+ " Override again if readonly.
+ if l:colour != l:normalcolour
+ if getbufvar("", "&ro")
+ let l:colour = l:readonlycolour
+ endif
+ endif
+
+ let l:termcolour = Iain_Colour(l:colour)
+
+ exec "highlight StatusLine gui=none term=none cterm=none guifg=white guibg=" . l:colour . " ctermfg=white ctermbg=" . l:termcolour
+ exec "highlight User1 gui=bold term=bold cterm=bold guifg=white guibg=" . l:colour . " ctermfg=white ctermbg=" . l:termcolour
+endfun "}}}2
+
+fun! Iain_Colour(colour) "{{{2
+ if &t_Co == 88
+ if a:colour == "darkblue"
+ return 17
+ elseif a:colour == "darkmagenta"
+ return 33
+ elseif a:colour == "darkred"
+ return 32
+ elseif a:colour == "red"
+ return 64
+ endif
+ elseif &t_Co == 256
+ if a:colour == "darkblue"
+ return 17
+ elseif a:colour == "darkmagenta"
+ return 90
+ elseif a:colour == "darkred"
+ return 88
+ elseif a:colour == "red"
+ return 196
+ endif
+ else
+ return a:colour
+ endif
+endfun "}}}2
+
+if has("autocmd")
+ au StatusLine VimEnter * call Highlight_StatusLine("")
+
+ " Show numbers by default.
+ au Display VimEnter * if ! Uncluttered_Buffer() | call Number(0) | endif
+endif
+endif "}}}1
+
+""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
+" Handle options only available in Vim 5.4 and above.
+""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
+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