+fun! Iain_Vars() "{{{2
+ call Prep_Var("w:iainlist", 0)
+ call Prep_Var("b:iainhex", 0)
+ call Prep_Var("b:iainverbose", 0)
+ " Window Flags: (F)ocused, (I)nsert mode, Cursor (H)old.
+ call Prep_Var("b:iainstatus", "'Fih'")
+ call Prep_Var("g:iainextracolumnsnumber", "''")
+ call Prep_Var("g:iainextracolumnslist", "''")
+ call Prep_Var("b:iainalt", 0)
+ if has("signs")
+ call Prep_Var("g:marksigns", 0)
+ call Prep_Var("g:firstsign", 100)
+ endif
+ call Prep_Var("g:resizable", "''")
+endfun "}}}2
+
+" Show the window title.
+fun! Show_TitleString() "{{{2
+ if bufname("") == ""
+ let l:ts1='Vim'
+ else
+ " Vim 5 doesn't have printf.
+ let l:ts1=bufnr("")
+ if l:ts1 < 10
+ let l:ts1=" " . l:ts1
+ endif
+ let l:ts1=l:ts1 . ": " . expand('%t')
+ endif
+ let l:ts1=l:ts1 . " (" . getcwd() . ")"
+ if has("clientserver")
+ let l:ts1=l:ts1 . " " . v:servername
+ endif
+ return l:ts1
+endfun "}}}2
+
+" Toggle case-sensitivity.
+fun! Invert_Case() "{{{2
+ let &ic = ! &ic
+endfun "}}}2
+
+" Can we resize this window?
+fun! Can_Resize() "{{{2
+ call Iain_Vars()
+
+ if g:resizable == "0" || g:resizable == "1"
+ return g:resizable
+ endif
+
+ " Do we KNOW we can(not) resize?
+ if has("gui_running")
+ let g:resizable = 1
+ elseif $RESIZABLE == &term
+ let g:resizable = 1
+ elseif $RESIZABLE == "0"
+ let g:resizable = 0
+ else
+ " Assume we can. Allow overriding.
+ let g:resizable = 1
+ endif
+ return g:resizable
+endfun "}}}2
+
+" Grow or shrink the window width.
+fun! Resize_Columns(op, ...) "{{{2
+ if a:op == ""
+ return
+ endif
+
+ 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"
+ let l:args = "\\" . a:keysequence . " " . a:mapping . ":<CR>"
+ else
+ let l:args = "<silent> <Leader>" . a:keysequence . " " . a:mapping
+ endif
+ if a:0 == 0
+ " General mapping.
+ exec "map " . l:args
+ else
+ " Specific mapping(s).
+ let l:i = 1
+ while l:i <= a:0
+ exec "let l:map = a:" . l:i . " . \"map\""
+ exec l:map . " " . l:args
+ let l:i = l:i + 1
+ endwhile
+ endif
+endfun "}}}2
+
+" Use - and = to create underlines.
+call Mapping("-", "yyp:s/./-/g<CR>:let @/=''<CR>")
+call Mapping("=", "yyp:s/./=/g<CR>:let @/=''<CR>")
+
+fun! Tab_Width(width) "{{{2
+ exec "set sw=" . a:width
+ exec "set ts=" . a:width
+endfun "}}}2
+
+" Set 2-column tabs with \2.
+call Mapping("2", ":call Tab_Width(2)<CR>")
+" Set 4-column tabs with \4.
+call Mapping("4", ":call Tab_Width(4)<CR>")
+" Set 8-column tabs with \8.
+call Mapping("8", ":call Tab_Width(8)<CR>")
+" Set 16-column tabs with \6.
+call Mapping("6", ":call Tab_Width(16)<CR>")
+" Set 32-column tabs with \3.
+call Mapping("3", ":call Tab_Width(32)<CR>")
+" Toggle paste mode with \p.
+call Mapping("p", ":se paste!<CR>")
+" Swap case-sensitivity with \c.
+call Mapping("C", ":call Invert_Case()<CR>")
+" Change number mode with \n.
+call Mapping("n", ":call Number(1)<CR>")
+" Expand or shrink window size with \> and \<.
+call Mapping(">", ":call Resize_Columns('+')<CR>")
+call Mapping("<", ":call Resize_Columns('-')<CR>")
+" Clear search pattern with \/.
+call Mapping("/", ":let @/=\"\"<CR>")
+" Toggle alternate buffer name with \#.
+call Mapping("#", ":call Cycle_Alt()<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