Set PROMPT_DIRTRIM dynamically.
[profile.git] / .vimrc
1 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
2 " Multi-version vimrc compatible with version 4 and above.   vim6:set fdm=marker:
3 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
4
5 " Note that "if <condition> | call Something() | endif" syntax is unsupported
6 " in Vim 4 so we write all our functions out the long way.  It does work in
7 " autocommand definitions, however.
8
9 " Vim 4 complains if version isn't set in the configuration file.
10 version 4.0
11
12 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
13 " Handle options safe to use in version 4.  Vim 4 parses but ignores the
14 " "if version" syntax used later in this file so we don't use it.  No attempt
15 " is made to make this configuration compatible with Vim 3.
16 " Some of these settings should strictly be wrapped inside "if has()" blocks
17 " but that would cause them not to be ignored by Vim 4.
18 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
19 "{{{1
20 " No compatibility mode.
21 se nocp
22
23 " Tabstop 2.
24 se ts=2
25 " And use spaces not tabs.
26 se expandtab
27 " And << and >> indent by 2.
28 se sw=2
29 " Backspace deletes full tab width at the start of a line.
30 se smarttab
31
32 " Allow backspace to delete before start of line.
33 se bs=2
34
35 " Don't jump to the start of the line when using H, L etc.
36 se nosol
37
38 " Show the ruler.
39 se ruler
40 " Show partial commands in the ruler.
41 se showcmd
42 " And always show the status line.
43 se laststatus=2
44
45 " Use C indent style.
46 se cindent
47 se cinkeys=0{,0},0),:,!^F,o,O,e
48 se cinoptions=b1,c2
49
50 " GUI options.
51 se go=aglmr
52
53 " Don't be bugged by messages at the bottom of the screen.
54 se shm=aot
55
56 " Find as you type.
57 se incsearch
58
59 " Case-insensitive search.
60 se ignorecase
61 " But override by typing capitals.
62 se smartcase
63
64 " Look for ctags in home directory first.
65 se tags=~/.tags,./tags,tags
66
67 " Don't timeout waiting to interpet, eg, <ESC>OA as an escape code.
68 se ttimeoutlen=100
69
70 " Remember undo list for closed (but not wiped) buffers.
71 se hidden
72
73 " Use ^B to search backward when completing.
74 inoremap <C-b> <C-p>
75 " Use ^L to show matching completions but don't select one.
76 inoremap <C-l> <C-n><C-p>
77
78 " Swap jump keys.
79 nnoremap ' `
80 nnoremap ` '
81
82 " Select previous widnow.
83 nnoremap <C-w>^ <C-w>p
84 nnoremap <C-w><C-^> <C-w>p
85 "}}}1
86
87 " Find stuff.
88 if (has("win32") || has("win64")) && version >= "504"
89   se rtp=~/.vim,$VIMRUNTIME
90 endif
91
92 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
93 " Handle options only available in Vim 5 and above.
94 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
95 if version >= "500" "{{{1
96 version 5.0
97
98 " Tell Vim we use dark backgrounds in our terminals.
99 if ! has("gui_running")
100   se bg=dark
101 endif
102
103 " Allow mouse use in a terminal but only if it can work.
104 if has("xterm_clipboard")
105   se mouse=nvir
106 endif
107
108 " Update more quickly.  For use with sign highlighting as polling for
109 " CursorMove makes redrawing slow.
110 if has("signs")
111   se updatetime=500
112 endif
113
114 " Enable tab-completion prompting for commands.
115 if has("wildmenu")
116   se wildmenu
117   " Don't list object files when globbing files to load.
118   se wildignore+=*.o,*.obj
119   " So there's no need to assign them low priority.
120   se suffixes-=*.o,*.obj
121 endif
122
123 " Save sessions in UNIX format with / as file separator.  This is
124 " cross-platform.
125 if has("mksession")
126   se ssop+=unix,slash
127 endif
128
129 " How often do we need to use ^A/^X on octals?
130 se nf=hex
131
132 " Nuke any pre-existing autocommands.
133 if has("autocmd")
134   augroup Display
135   autocmd!
136   augroup Mode
137   autocmd!
138   if has("signs")
139     augroup Signs
140     autocmd!
141   endif
142   augroup StatusLine
143   autocmd!
144   augroup File
145   autocmd!
146   augroup END
147 endif
148
149 " Save the current window dimensions so we can restore them when we quit.
150 if ! exists("g:oldcols")
151   let g:oldcols=&columns
152 endif
153 if ! exists("g:oldlines")
154   let g:oldlines=&lines
155 endif
156
157 " More GUI options.  Add icon and tearoffs.
158 if has("gui")
159   se go+=i
160   se go+=t
161 endif
162
163 " Allow dynamic window resize even if we aren't in an xterm.
164 se t_WS=\e[8;%p1%d;%p2%dt
165
166 " Highlight search results.
167 if has("extra_search")
168   se hlsearch
169 endif
170
171 " Syntax highlighting.  New versions will use syn enable instead.
172 if version < "600"
173   syn on
174 endif
175
176 if has("user_commands")
177   " Catch typos.
178   command! W :w
179   command! Wq :wq
180   command! Wqa :wqa
181 endif
182
183 " Forget the Ex mode mapping.
184 map Q <NOP>
185
186 if has("autocmd")
187   " Position the compview plugin window.
188   au Display BufEnter -SearchResults- set buftype=nowrite | set nonumber | wincmd J
189 endif
190 endif "}}}1
191
192 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
193 " Handle options only available in Vim 5.2 and above.
194 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
195 if version >= "502" "{{{1
196 version 5.2
197
198 " Helper to initialise a variable.
199 fun! Prep_Var(var, value) "{{{2
200   if exists(a:var)
201     return
202   endif
203   exe "let " . a:var . "=" . a:value
204 endfun "}}}2
205
206 " Set up our variables.
207 fun! Iain_Vars() "{{{2
208   call Prep_Var("w:iainlist", 0)
209   call Prep_Var("b:iainhex", 0)
210   call Prep_Var("b:iainverbose", 0)
211   " Window Flags: (F)ocused, (I)nsert mode, Cursor (H)old.
212   call Prep_Var("b:iainstatus", "'Fih'")
213   call Prep_Var("g:iainextracolumnsnumber", "''")
214   call Prep_Var("g:iainextracolumnslist", "''")
215   call Prep_Var("b:iainalt", 0)
216   if has("signs")
217     call Prep_Var("g:marksigns", 0)
218     call Prep_Var("g:firstsign", 100)
219   endif
220   call Prep_Var("g:resizable", "''")
221 endfun "}}}2
222
223 " Show the window title.
224 fun! Show_TitleString() "{{{2
225   if bufname("") == ""
226     let l:ts1='Vim'
227   else
228     " Vim 5 doesn't have printf.
229     let l:ts1=bufnr("")
230     if l:ts1 < 10
231       let l:ts1=" " . l:ts1
232     endif
233     let l:ts1=l:ts1 . ": " . expand('%t')
234   endif
235   let l:ts1=l:ts1 . " (" .  getcwd() . ")"
236   if has("clientserver")
237     let l:ts1=l:ts1 . " " . v:servername
238   endif
239   return l:ts1
240 endfun "}}}2
241
242 " Toggle case-sensitivity.
243 fun! Invert_Case() "{{{2
244   let &ic = ! &ic
245 endfun "}}}2
246
247 " Can we resize this window?
248 fun! Can_Resize() "{{{2
249   call Iain_Vars()
250
251   if g:resizable == "0" || g:resizable == "1"
252     return g:resizable
253   endif
254
255   " Do we KNOW we can(not) resize?
256   if has("gui_running")
257     let g:resizable = 1
258   elseif $RESIZABLE == &term
259     let g:resizable = 1
260   elseif $RESIZABLE == "0"
261     let g:resizable = 0
262   else
263     " Assume we can.  Allow overriding.
264     let g:resizable = 1
265   endif
266   return g:resizable
267 endfun "}}}2
268
269 " Grow or shrink the window width.
270 fun! Resize_Columns(op, ...) "{{{2
271   if a:op == ""
272     return
273   endif
274
275   if ! Can_Resize()
276     return
277   endif
278
279   if a:0 == 0
280     " Vim 5 hardcodes the size of numbers column to 8.
281     if version >= "700" && has("linebreak")
282       let l:columns = &numberwidth
283     else
284       let l:columns = 8
285     endif
286   else
287     let l:columns = a:1
288   endif
289
290   exe "let l:resize=" . &columns . a:op . l:columns
291   let l:resize = "se columns=" . l:resize
292
293   " HACK: Inside screen there is an extra line for the status bar.  Vim
294   " manages the resize by sending an escape sequence to set the number of
295   " lines and number of columns in one action.  To do this it will first query
296   " the number of lines and then set <same number of lines> by <new number of
297   " columns>.  Because of the extra line for the status bar this results in
298   " the real terminal being shrunk by a line.  We ask for the terminal to grow
299   " by a line so it ends up actually being the same.
300   if &term =~ '^screen'
301     let l:resize = l:resize . " lines=" . (&lines + 1)
302   endif
303
304   exe l:resize
305 endfun "}}}2
306
307 " Grow or shrink the window height.
308 fun! Resize_Lines(op, lines) "{{{2
309   if a:op == ""
310     return
311   endif
312
313   if ! Can_Resize()
314     return
315   endif
316
317   exe "let l:resize=" . &lines . a:op . a:lines
318   if &term =~ '^screen'
319     let l:resize = l:resize + 1
320   endif
321   let l:resize = "se lines=" . l:resize
322
323   exe l:resize
324 endfun "}}}2
325
326 " Set extra columns depending on window status.
327 fun! Extra_Columns(extra, var, ...) "{{{2
328   " Vim 6 doesn't have winnr("$").  Determine which windows are open
329   " ourselves by using :windo to incremement a counter.  As Vim 5
330   " doesn't have :windo we require Vim 6 for this.
331   if v:version < "600"
332     return ""
333   endif
334   if ! has("windows")
335     return ""
336   endif
337
338   " Remember which window we're in.
339   let l:winnr = winnr()
340   let l:num_windows = 0
341   windo let l:num_windows = l:num_windows + 1
342   " Switch back to the window we were in.
343   exe l:winnr . "wincmd w"
344
345   call Iain_Vars()
346
347   if a:0 == 0
348     let l:condition = ""
349   else
350     let l:condition = a:1
351   endif
352
353   let l:n = 0
354   let l:i = 1
355   let l:windows = ""
356   while l:n < l:num_windows
357     " If window w exists then getwinvar(w, "&modified") will be 0 or 1.
358     if getwinvar(l:i, "&modified") =~ '^\d'
359       let l:n = l:n + 1
360
361       let l:val = 0
362       exe "if getwinvar(" . l:i . ", '" . a:var . "') " . l:condition . " | let l:val = 1 | endif"
363       if l:val
364         exe "let l:windows = '" . l:windows . ":" . l:i . "'"
365       endif
366     endif
367     let l:i = l:i + 1
368   endwhile
369
370   let l:extra = "g:iainextracolumns" . a:extra
371   exe "let l:val = " . l:extra
372   exe "let " . l:extra . " = '" . l:windows . "'"
373
374   if l:windows == l:val
375     return ""
376   endif
377
378   if l:windows == ""
379     return "-"
380   elseif l:val == ""
381     return "+"
382   endif
383 endfun "}}}2
384
385 " Toggle number display.
386 fun! Number(resize) "{{{2
387   call Iain_Vars()
388   let &number = ! &number
389
390   " Ensure we keep track of any extra columns even if we aren't resizing.
391   " This prevents confusion when number is set at startup.
392   let l:extra = Extra_Columns("number", "&number")
393
394   if a:resize
395     call Resize_Columns(l:extra)
396   endif
397 endfun "}}}2
398
399 " Restore window size.
400 if has("autocmd") && ! has("gui_running")
401   au Display VimLeave * if exists("g:oldcols") | call Resize_Columns("-", (&columns - g:oldcols)) | endif
402   au Display VimLeave * if exists("g:oldlines") | call Resize_Lines("-", (&lines - g:oldlines)) | endif
403 endif
404
405 " Map Makefile mode.
406 if has("autocmd")
407   au Mode BufEnter * if &ft == "make" | call MakeMode_map() | endif
408   au Mode BufLeave * if &ft == "make" | call MakeMode_unmap() | endif
409 endif
410
411 " Entering Make mode.
412 fun! MakeMode_map() "{{{2
413   call Iain_Vars()
414   let w:iainlist=1
415   call Cycle_List()
416   set ts=8
417   set noexpandtab
418 endfun "}}}2
419
420 " Leaving Make mode.
421 fun! MakeMode_unmap() "{{{2
422   call Cycle_List()
423   set ts=2
424   set expandtab
425 endfun "}}}2
426
427 " Function to create mappings with either a hardcoded \ or <Leader>.
428 fun! Mapping(keysequence, mapping, ...) "{{{2
429   if version < "600"
430     let l:args = "\\" . a:keysequence . " " . a:mapping . ":<CR>"
431   else
432     let l:args = "<silent> <Leader>" . a:keysequence . " " . a:mapping
433   endif
434   if a:0 == 0
435     " General mapping.
436     exec "map " . l:args
437   else
438     " Specific mapping(s).
439     let l:i = 1
440     while l:i <= a:0
441       exec "let l:map = a:" . l:i . " . \"map\""
442       exec l:map . " " . l:args
443       let l:i = l:i + 1
444     endwhile
445   endif
446 endfun "}}}2
447
448 " Use - and = to create underlines.
449 call Mapping("-", "yyp:s/./-/g<CR>:let @/=''<CR>")
450 call Mapping("=", "yyp:s/./=/g<CR>:let @/=''<CR>")
451
452 fun! Tab_Width(width) "{{{2
453   exec "set sw=" . a:width
454   exec "set ts=" . a:width
455 endfun "}}}2
456
457 " Set 2-column tabs with \2.
458 call Mapping("2", ":call Tab_Width(2)<CR>")
459 " Set 4-column tabs with \4.
460 call Mapping("4", ":call Tab_Width(4)<CR>")
461 " Set 8-column tabs with \8.
462 call Mapping("8", ":call Tab_Width(8)<CR>")
463 " Set 16-column tabs with \6.
464 call Mapping("6", ":call Tab_Width(16)<CR>")
465 " Set 32-column tabs with \3.
466 call Mapping("3", ":call Tab_Width(32)<CR>")
467 " Toggle paste mode with \p.
468 call Mapping("p", ":se paste!<CR>")
469 " Swap case-sensitivity with \c.
470 call Mapping("C", ":call Invert_Case()<CR>")
471 " Change number mode with \n.
472 call Mapping("n", ":call Number(1)<CR>")
473 " Expand or shrink window size with \> and \<.
474 call Mapping(">", ":call Resize_Columns('+')<CR>")
475 call Mapping("<", ":call Resize_Columns('-')<CR>")
476 " Clear search pattern with \/.
477 call Mapping("/", ":let @/=\"\"<CR>")
478 " Toggle alternate buffer name with \#.
479 call Mapping("#", ":call Cycle_Alt()<CR>")
480
481 " Set graphical window title.
482 if has("win32") || has("win64")
483   " Windows taskbar entries are probably too small to show full titles.
484   se titlestring=%t
485 else
486   se titlestring=%{Show_TitleString()}
487 endif
488
489 " Vim tip 99: What's the highlighting group under the cursor?
490 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>")
491
492 fun! Uncluttered_Buffer() "{{{2
493   if exists("uncluttered_buffer")
494     if uncluttered_buffer == 1
495       return 1
496     endif
497   endif
498
499   if version >= "600"
500     if &buftype != ''
501       return 1
502     endif
503   endif
504
505   if &ft == 'perforce'
506     return 1
507   endif
508
509   if &ft == 'svn'
510     return 1
511   endif
512
513   if &ft == 'gitcommit'
514     return 1
515   endif
516
517   return 0
518 endfun "}}}2
519
520 fun! Startup_Resize() "{{{2
521   let l:columns = 0
522
523   " Resize for numbers.
524   if &number
525     if version >= "700" && has("linebreak")
526       let l:columns = &numberwidth
527     else
528       let l:columns = 8
529     endif
530   endif
531
532   " Resize for signs.
533   if has("signs")
534     if g:marksigns
535       if version >= "600"
536         let l:columns = l:columns + 2
537       endif
538     endif
539   endif
540
541   if g:oldcols < (80 + l:columns)
542     call Resize_Columns("+", l:columns)
543   endif
544 endfun "}}}2
545
546 " Change status bar colour when various things happen.
547 " Flags: H/h: Cursor held/moved.
548 "        F/f: Focus gained/lost.
549 "        I/i: Insert mode entered/left.
550 fun! Highlight_StatusLine(flag) "{{{2
551   if ! has("statusline")
552     return
553   endif
554   " Get current status.
555   call Iain_Vars()
556
557   " Change the status based on the flag.  XXX: Does Vim let us to do flags?
558   let l:ic = &ic
559   set ic
560   let b:iainstatus = substitute(b:iainstatus, a:flag, a:flag, "")
561   let &ic = l:ic
562
563   let l:normalcolour = "darkblue"
564   let l:editingcolour = "darkmagenta"
565   let l:replacecolour = "purple"
566   let l:warningcolour = "darkred"
567   let l:readonlycolour = "red"
568
569   if b:iainstatus =~# "I"
570     if v:insertmode == "r"
571       let l:editingcolour = l:replacecolour
572     endif
573   endif
574
575   " Default colour.
576   let l:colour = l:normalcolour
577   " Maybe override depending on status.
578   if b:iainstatus =~# "H"
579     if b:iainstatus =~# "I"
580       " Held in insert mode.  Add extra highlight if we don't have focus.
581       if b:iainstatus =~# "f"
582         let l:colour = l:warningcolour
583       else
584         let l:colour = l:editingcolour
585       endif
586     endif
587   else
588     if b:iainstatus =~# "I"
589       " Regular insert mode.
590       let l:colour = l:editingcolour
591     endif
592   endif
593
594   " Override again if readonly.
595   if l:colour != l:normalcolour
596     if getbufvar("", "&ro")
597       let l:colour = l:readonlycolour
598     endif
599   endif
600
601   let l:termcolour = Iain_Colour(l:colour)
602
603   exec "highlight StatusLine gui=none term=none cterm=none guifg=white guibg=" . l:colour . " ctermfg=white ctermbg=" . l:termcolour
604   exec "highlight User1 gui=bold term=bold cterm=bold guifg=white guibg=" . l:colour . " ctermfg=white ctermbg=" . l:termcolour
605 endfun "}}}2
606
607 fun! Iain_Colour(colour) "{{{2
608   if &t_Co == 88
609     if a:colour == "darkblue"
610       return 17
611     elseif a:colour == "darkmagenta"
612       return 33
613     elseif a:colour == "purple"
614       return 35
615     elseif a:colour == "darkred"
616       return 32
617     elseif a:colour == "red"
618       return 64
619     endif
620   elseif &t_Co == 256
621     if a:colour == "darkblue"
622       return 17
623     elseif a:colour == "darkmagenta"
624       return 90
625     elseif a:colour == "purple"
626       return 57
627     elseif a:colour == "darkred"
628       return 88
629     elseif a:colour == "red"
630       return 196
631     endif
632   else
633     " Colours which cterm*g doesn't recognise.
634     if a:colour == "purple"
635       return "magenta"
636     endif
637     return a:colour
638   endif
639 endfun "}}}2
640
641 if has("autocmd")
642   au StatusLine VimEnter * call Highlight_StatusLine("")
643
644   " Show numbers by default.
645   au Display VimEnter * if ! Uncluttered_Buffer() | call Number(0) | endif
646 endif
647 endif "}}}1
648
649 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
650 " Handle options only available in Vim 5.4 and above.
651 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
652 if version >= "504" "{{{1
653 version 5.4
654
655 " Reuse windows when using sbuffer.
656 se switchbuf=useopen
657
658 " Vim 5 requires a 'n setting for viminfo.
659 if ! &viminfo
660   se viminfo='100
661 endif
662
663 " Allow persistent variable saving for localvimrc.
664 se viminfo+=!
665
666 " Do we have Unicode?
667 fun! Has_Unicode() "{{{2
668   if ! has('multi_byte')
669     return 0
670   endif
671
672   if version < "602"
673     return 0
674   endif
675
676   if &tenc =~? '^u\(tf\|cs\)'
677     return 1
678   endif
679
680   if ! strlen(&tenc) && &enc =~? '^u\(tf\|cs\)'
681     return 1
682   endif
683
684   return 0
685 endfun "}}}2
686
687 " Helper for status line.
688 " Show file encoding
689 fun! Show_Encoding() "{{{2
690   if version < "600"
691     return ""
692   endif
693
694   let l:enc = &fenc
695   let l:symbol = ""
696   if l:enc == ""
697     let l:enc = &enc
698     if l:enc == ""
699       return ""
700     endif
701     if bufname("%") == ""
702       if Has_Unicode()
703         let l:symbol = '•'
704       else
705         let l:symbol = '*'
706       endif
707     endif
708   endif
709
710   if has("multi_byte")
711     if &bomb
712       if Has_Unicode()
713         let l:symbol = "☻"
714       else
715         let l:symbol = "@"
716       endif
717     endif
718   endif
719
720   " Don't return anything if the encoding is utf-8.
721   if l:enc == "utf-8"
722     if l:symbol == ""
723       return ""
724     endif
725   endif
726
727   return l:symbol . l:enc . ","
728 endfun "}}}2
729
730 " Helper for status line.
731 " Show space, underscore or dollar sign depending on list mode.
732 fun! Show_List() "{{{2
733   call Iain_Vars()
734   if w:iainlist == 0
735     " No list.
736     return ""
737   elseif Has_Unicode()
738     if w:iainlist == 1
739       " Just tabs.
740       return "⇥"
741     else
742       " Full list.
743       return "¶"
744     endif
745   else
746     if w:iainlist == 1
747       return "_"
748     else
749       return "\$"
750     endif
751   endif
752 endfun "}}}2
753
754 " Helper for status line.
755 " Show c or C to denote case-sensitivity.
756 fun! Show_Case() "{{{2
757   if &ic
758     return "c"
759   else
760     return "C"
761   endif
762 endfun "}}}2
763
764 " Helper for status line.
765 " Mark DOS buffers.
766 fun! Show_DOS() "{{{2
767   if &ff == "unix"
768     return ""
769   endif
770   return &ff . ","
771 endfun "}}}2
772
773 " Helper for status line.
774 " Show the size of the tabstop.
775 fun! Show_Tabstop() "{{{2
776   if &et
777     return &ts
778   else
779     if Has_Unicode()
780       return &ts . "↹"
781     else
782       return &ts . "\\"
783     fi
784   endif
785 endfun "}}}2
786
787 " Helper for status line.
788 " Show p when paste mode is on.
789 fun! Show_Paste() "{{{2
790   if &paste
791     return "p"
792   else
793     return ""
794   endif
795 endfun "}}}2
796
797 " Helper for status line.
798 " Show v when virtualedit mode is block, insert or onemore.
799 " Show V when virtualedit mode is all.
800 fun! Show_VirtualEdit() "{{{2
801   if ! has("virtualedit")
802     return ""
803   endif
804
805   if &ve == "all"
806     return "V"
807   elseif &ve != ''
808     return "v"
809   else
810     return ""
811   endif
812 endfun "}}}2
813
814 " Helper for status line.
815 " Show U when persistent undo is on.
816 " Show u when persistent undo is off but an undofile exists.
817 fun! Show_Undo() "{{{2
818   if ! exists("&undofile")
819     return ""
820   endif
821
822   if &undofile
823     return "U"
824   elseif filereadable(undofile(expand("%")))
825     return "u"
826   else
827     return ""
828   endif
829 endfun "}}}2
830
831 " Helper for status line.
832 " Show alternate buffer number and name.
833 fun! Show_Alt() "{{{2
834   let l:alt = bufnr("#")
835   if l:alt < 0 || l:alt == bufnr("") || ! b:iainalt
836     return ""
837   endif
838
839   return " " . l:alt . ": " . expand("#:t")
840 endfun "}}}2
841
842 " Helper for status line.
843 " Show scrollbind or cursorbind.
844 fun! Show_Bind() "{{{2
845   if has("cursorbind")
846     if &cursorbind
847       if Has_Unicode()
848         return "⇄"
849       else
850         return ">"
851       endif
852     elseif &scrollbind
853       if Has_Unicode()
854         return "⇅"
855       else
856         return "^"
857       endif
858     endif
859   endif
860   return ""
861 endfun "}}}2
862
863 " Helper for status line.
864 " Show marker if searchforward is unset.
865 fun! Show_SearchForward() "{{{2
866   if version >= "702"
867     if ! v:searchforward
868       if Has_Unicode()
869         return "∆"
870       else
871         return "^"
872       endif
873     endif
874   endif
875   return ""
876 endfun "}}}2
877
878 " Helper for status line.
879 " Show marks set in cursor line.
880 fun! Show_Marks() "{{{2
881   if ! exists("g:iainsigns")
882     return ""
883   endif
884
885   let l:marks = ""
886
887   let l:signs = g:iainsigns
888   let l:sign = ""
889   let l:cursorline = line(".")
890   while strlen(l:signs)
891     let l:sign = matchstr(l:signs, '^[A-Za-z]\+\(:.\)*[.=>-][^ ]\+')
892     let l:sign = substitute(l:sign, '^[A-Za-z]\+', "", "")
893     let l:ascii = matchstr(l:sign, '^:.')
894     let l:mark = substitute(l:sign, '^\(:.\)*[.=>-]', "", "")
895     if strlen(l:ascii)
896       let l:ascii = substitute(l:ascii, '^:', "", "")
897     else
898       let l:ascii = l:mark
899     endif
900     let l:ascii = substitute(l:ascii, '"', '\\"', "")
901
902     if l:ascii == "o"
903       let l:line = "."
904     else
905       let l:line = "'" . l:ascii
906     endif
907
908     " Ignore cursor line which will always match.
909     if l:line != "."
910       if l:cursorline == line(l:line)
911         let l:marks = l:marks . l:mark
912       endif
913     endif
914
915     let l:signs = substitute(l:signs, '^[^ ]\+ *', "", "")
916   endwhile
917
918   if l:marks == ""
919     return ""
920   else
921     return "M:" . l:marks . " "
922   endif
923 endfun "}}}2
924
925 " Helper for status lines.
926 " Show Space plugin command.
927 fun! Show_Space() "{{{2
928   if exists("*GetSpaceMovement")
929     let l:ret = GetSpaceMovement()
930     if l:ret != ""
931       if Has_Unicode()
932         let l:ret = "▭" . l:ret . Show_SearchForward()
933       else
934         let l:ret = "[" . l:ret . Show_SearchForward() . "]"
935       endif
936     endif
937     return l:ret
938   else
939     return Show_SearchForward()
940   endif
941 endfun "}}}2
942
943 " Show the status line.
944 fun! Show_StatusLine() "{{{2
945   if ! has("statusline")
946     return
947   endif
948   call Iain_Vars()
949
950   " User{N} highlights %{N}*.
951   " Named highlight groups %#group% aren't available until Vim 7.
952   let l:normal = '%0*'
953   let l:bold = '%1*'
954
955   " sl1 contains left-aligned stuff.
956   " sl2 contains stuff shown only when verbose mode is enabled.
957   " sl3 contains right-aligned stuff.
958   let l:sl1='%2n\:\ %<' . l:bold . '%f' . l:normal . '\ [%{Show_Encoding()}%{Show_DOS()}%{Show_List()}%{Show_Bind()}%{Show_Case()}%{Show_Tabstop()}%{Show_Paste()}%{Show_VirtualEdit()}%{Show_Undo()}%Y%M%R%{Show_Space()}]%{Show_Alt()}\ '
959   let l:sl3='%{Show_Marks()}L:' . l:bold . '%4.6l' . l:normal . '/%-4.6L\ C:' . l:bold . '%3.6c%V' . l:normal . '\ \|\ %P'
960   let l:hexformat='%b'
961   if b:iainhex
962     let l:hexformat='0\x%02B'
963   endif
964   if b:iainverbose
965     let l:sl1=l:sl1 . v:version . '\ %='
966     let l:sl2=l:hexformat . '\ \|\ P:%4.6o\ '
967   else
968     let l:sl1=l:sl1 . '%='
969     let l:sl2=''
970   endif
971   exec "set statusline=" . l:sl1 . l:sl2 . l:sl3
972 endfun "}}}2
973
974 " Show the status line for the first time.
975 call Show_StatusLine()
976 endif "}}}1
977
978 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
979 " Handle options only available in Vim 6 and above.
980 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
981 if version >= "600" "{{{1
982 version 6.0
983
984 if has("win32") || has("win64")
985   se encoding=utf-8
986 endif
987
988 " Remember quickfix state.
989 if has("quickfix")
990   let g:quickfixing=0
991 endif
992
993 " Set indenting by filetype.
994 filetype indent on
995
996 " Less intrusive syntax highlighting.
997 if has("syntax")
998   " The :syntax enable command tries to source the syntax.vim runtime script.
999   " Parsing this .vimrc will fail if for some reason the runtime doesn't
1000   " exist, as could be the case if the binary was installed with no support
1001   " files.  GNU On Windows is one example of an incomplete installation.
1002   try
1003     syn enable
1004   catch
1005   endtry
1006 endif
1007
1008 " Set colours.
1009 if has("gui_running")
1010   if has("win32") || has("win64")
1011     exe "silent se guifont=DejaVu_Sans_Mono:h10:cANSI"
1012   else
1013     exe "silent se guifont=DejaVu\\ Sans\\ Mono\\ 10"
1014   endif
1015 endif
1016 if has("gui_running") || &t_Co > 16
1017   exe "silent colo iain"
1018 endif
1019
1020 " Ignore whitespace when diffing.
1021 if has("diff")
1022   se diffopt=filler,iwhite
1023 endif
1024
1025 if has("autocmd")
1026   if has("quickfix")
1027     " Remember that we are opening the quickfix window.
1028     au Mode BufWinEnter quickfix let g:quickfixing=1
1029     au Mode BufUnload * if &ft == "qf" | let g:quickfixing=0 | endif
1030   endif
1031
1032   " Allow in-place editing of crontabs.
1033   au Mode FileType crontab set backupcopy=yes
1034 endif
1035
1036 " Make * and # work the way you expect in visual mode.
1037 vnoremap * y/\V<C-R>=substitute(escape(@@,"/\\"),"\n","\\\\n","ge")<CR><CR>
1038 vnoremap # y?\V<C-R>=substitute(escape(@@,"?\\"),"\n","\\\\n","ge")<CR><CR>
1039
1040 " Set mark and update highlighting.
1041 if has("signs")
1042   au Signs BufReadPost * call <SID>Highlight_Signs()
1043   au Signs CursorHold * call <SID>Highlight_Signs()
1044 endif
1045
1046 " Helper to set buffer variable for a given sign.
1047 fun! <SID>Prep_Sign(sign) "{{{2
1048   if ! exists("b:sign" . a:sign) || ! g:marksigns
1049     exe "let b:sign" . a:sign . "=0"
1050    endif
1051 endfun "}}}2
1052
1053 fun! <SID>Place_Sign(number, line, old, name) "{{{2
1054   if a:line == a:old
1055     return a:old
1056   endif
1057
1058   exe "sign unplace " . (g:firstsign + a:number) . " buffer=" . bufnr("")
1059   " Don't place the sign if it would conflict with the last change sign.
1060   exe "sign place " . (g:firstsign + a:number) . " line=" . a:line . " name=" . a:name . " buffer=" . bufnr("")
1061   return a:line
1062 endfun "}}}2
1063
1064 fun! <SID>Highlight_Signs(...) "{{{2
1065   if ! has("signs") || ! g:marksigns || Uncluttered_Buffer()
1066     return
1067   endif
1068
1069   let l:signs = g:iainsigns
1070   let l:sign = ""
1071   let l:i = 0
1072   while strlen(l:signs)
1073     let l:sign = matchstr(l:signs, '^[A-Za-z]\+\(:.\)*[.=>-][^ ]\+')
1074
1075     let l:name = substitute(l:sign, '[:.=>-].*', "", "")
1076     let l:var = tolower(l:name)
1077     let l:sign = substitute(l:sign, '^[A-Za-z]\+', "", "")
1078     let l:ascii = matchstr(l:sign, '^:.')
1079     let l:mark = substitute(l:sign, '^\(:.\)*[.=>-]', "", "")
1080     if strlen(l:ascii)
1081       let l:ascii = substitute(l:ascii, '^:', "", "")
1082     else
1083       let l:ascii = l:mark
1084     endif
1085     let l:ascii = substitute(l:ascii, '"', '\\"', "")
1086
1087     if l:ascii == "o"
1088       let l:line = "."
1089     else
1090       let l:line = "'" . l:ascii
1091     endif
1092
1093     call <SID>Prep_Sign(l:var)
1094     exe "let " . l:var . " = <SID>Place_Sign(" . l:i . ", line(\"" . l:line . "\"), b:sign" . l:var . ", \"Mark" . l:name . "\")"
1095     let l:i = l:i + 1
1096
1097     let l:signs = substitute(l:signs, '^[^ ]\+ *', "", "")
1098   endwhile
1099 endfun "}}}2
1100
1101 " Toggle signs.
1102 fun! <SID>Cycle_Signs(resize) "{{{2
1103   if ! has("signs")
1104     return
1105   endif
1106   call Iain_Vars()
1107   let g:marksigns = ! g:marksigns
1108
1109   " Retrofit arrays on to Vim 6.
1110   if ! exists("g:iainsigns")
1111     " Signs are defined in g:iainsigns.  The syntax is as follows:
1112     "
1113     " Sign ::= Name (':' Mark)* Type Symbol
1114     " Type ::= '=' | '-' | '.'
1115     "
1116     " Signs with Type '=' will be highlighted with the MarkSign group.
1117     " Signs with Type '-' will be highlighted with the MarkLine group.
1118     " Signs with Type '.' will be highlighted with the MarkDot group.
1119     " Signs with Type '>' will be highlighted with the MarkArrow group.
1120     " Define the Mark where Symbol is not also the mark name, eg "']".
1121     let g:iainsigns = "Less=< Greater=> Left=( Right=) SquareLeft=[ SquareRight=] BraceLeft={ BraceRight=} a-a b-b c-c d-d e-e f-f A-A B-B C-C D-D E-E F-F"
1122     if Has_Unicode()
1123       let g:iainsigns = g:iainsigns . " Quote:\"=” Dash:'=’ Caret:^.ʌ Dot:..•"
1124       if version < "704"
1125         let g:iainsigns = g:iainsigns ." Cursor:o>▶"
1126       endif
1127     else
1128       let g:iainsigns = g:iainsigns . " Quote=\" Dash=' Caret.^ Dot:..*"
1129       if version < "704"
1130         let g:iainsigns = g:iainsigns ." Cursor>o"
1131       endif
1132     endif
1133   endif
1134
1135   if g:marksigns
1136     " Signs to highlight marks.
1137     " Syntax won't work properly in Vim 6.
1138     let l:signs = g:iainsigns
1139     let l:sign = ""
1140     while strlen(l:signs)
1141       let l:sign = matchstr(l:signs, '^[A-Za-z]\+\(:.\)*[.=>-][^ ]\+')
1142
1143       let l:name = substitute(l:sign, '[:.=>-].*', "", "")
1144       let l:sign = substitute(l:sign, '^[A-Za-z]\+', "", "")
1145       let l:ascii = matchstr(l:sign, '^:.')
1146       let l:mark = substitute(l:sign, '^\(:.\)*[.=>-]', "", "")
1147       if strlen(l:ascii)
1148         let l:ascii = substitute(l:ascii, '^:', "", "")
1149         let l:ascii = matchstr(l:ascii, '^.')
1150       else
1151         let l:ascii = l:mark
1152       endif
1153       let l:ascii = substitute(l:ascii, '"', '\\"', "")
1154       let l:type = substitute(l:sign, '^:.', "", "")
1155       let l:type = matchstr(l:type, '^.')
1156
1157       let l:hl = ""
1158       if l:type == "="
1159         let l:hl = "texthl=MarkSign text="
1160       elseif l:type == "."
1161         let l:hl = "texthl=MarkDot text="
1162       elseif l:type == "-"
1163         let l:hl = "texthl=MarkLine text="
1164       elseif l:type == ">"
1165         let l:hl = "texthl=MarkArrow text="
1166       endif
1167
1168       exe "sign define Mark" . l:name . " " . l:hl . l:mark
1169
1170       let l:signs = substitute(l:signs, '^[^ ]\+ *', "", "")
1171     endwhile
1172
1173     if a:resize
1174       call Resize_Columns("+", 2)
1175     endif
1176     call <SID>Highlight_Signs()
1177   else
1178     let l:i = 0
1179     while l:i < 25
1180       exe "sign unplace " . (g:firstsign + l:i)
1181       let l:i = l:i + 1
1182     endwhile
1183
1184     let l:signs = g:iainsigns
1185     let l:sign = ""
1186     while strlen(l:signs)
1187       let l:sign = matchstr(l:signs, '^[A-Za-z]\+')
1188
1189       exe "sign undefine Mark" . l:sign
1190       call <SID>Prep_Sign(tolower(l:sign))
1191       let l:signs = substitute(l:signs, '^[^ ]\+ *', "", "")
1192     endwhile
1193
1194     if a:resize
1195       call Resize_Columns("-", 2)
1196     endif
1197   endif
1198 endfun "}}}2
1199
1200 " Change list mode.
1201 fun! Cycle_List() "{{{2
1202   " Pretty UTF-8 listchars.
1203   if Has_Unicode()
1204     let basic='tab:⇥·,trail:…,extends:«,precedes:»'
1205     let eol='eol:¶'
1206     if version >= "700"
1207       let basic=basic . ',nbsp:•'
1208     endif
1209   else
1210     let basic='tab:\\_,trail:_,extends:<,precedes:>'
1211     let eol='eol:$'
1212     if version >= "700"
1213       let basic=basic . ',nbsp:+'
1214     endif
1215   endif
1216   call Iain_Vars()
1217   let w:iainlist = w:iainlist + 1
1218   if w:iainlist > 2
1219     let w:iainlist = 0
1220   endif
1221   if w:iainlist == 0
1222     setlocal nolist
1223   elseif w:iainlist == 1
1224     exec "setlocal lcs=" . basic
1225     setlocal list
1226   else
1227     exec "setlocal lcs=" . basic . "," . eol
1228     setlocal list
1229   endif
1230
1231   call Resize_Columns(Extra_Columns("list", "iainlist", " == 2"), 1)
1232   call Extra_Whitespace_Match()
1233 endfun "}}}2
1234
1235 " Cycle between hex and decimal display of toolbar stuff.
1236 fun! Cycle_HexStatusLine() "{{{2
1237   call Iain_Vars()
1238   let b:iainhex = ! b:iainhex
1239   call Show_StatusLine()
1240 endfun "}}}2
1241
1242 " Cycle verbose display of toolbar stuff.
1243 fun! Cycle_VerboseStatusLine() "{{{2
1244   call Iain_Vars()
1245   let b:iainverbose = ! b:iainverbose
1246   call Show_StatusLine()
1247 endfun "}}}2
1248
1249 " Toggle quickfix window.
1250 fun! Cycle_Quickfix() "{{{2
1251   if ! has("quickfix")
1252     return
1253   endif
1254   if g:quickfixing == 1
1255     cclose
1256     let g:quickfixing=0
1257   else
1258     copen
1259   endif
1260 endfun "}}}2
1261
1262 " Toggle showing alternate buffer information.
1263 fun! Cycle_Alt() "{{{2
1264   call Iain_Vars()
1265   let b:iainalt = ! b:iainalt
1266   call Show_StatusLine()
1267 endfun "}}}2
1268
1269 " To be overridden later if applicable.
1270 fun! Extra_Whitespace_Match() "{{{2
1271   " NOP.
1272 endfun "}}}2
1273
1274 " Swap hex/decimal statusline with \x.
1275 call Mapping("x", ":call Cycle_HexStatusLine()<CR>")
1276 " Change statusline verbosity with \v.
1277 call Mapping("V", ":call Cycle_VerboseStatusLine()<CR>")
1278 " Cycle list styles with \l.
1279 call Mapping("l", ":call Cycle_List()<CR>")
1280 " Toggle tags with \t.
1281 call Mapping("t", ":Tlist<CR>")
1282 " Change foldmethod with \f.
1283 call Mapping("f", ":se foldenable!<CR>")
1284 " Toggle quickfix window with \q.
1285 call Mapping("q", ":call Cycle_Quickfix()<CR>")
1286 " Rerun filetype detection with \s.  The s is for syntax, as this will be
1287 " updated as a side-effect.
1288 call Mapping("S", ":filetype detect<CR>")
1289 " Toggle marks with \m.
1290 call Mapping("m", ":call <SID>Cycle_Signs(1)<CR>")
1291
1292 if has("autocmd")
1293   " Show signs by default.
1294   au Display VimEnter * call <SID>Cycle_Signs(0)
1295 endif
1296
1297 " move.
1298 nmap <A-u> <Plug>MoveLineHalfPageUp
1299 nmap <A-d> <Plug>MoveLineHalfPageDown
1300 vmap <A-u> <Plug>MoveBlockHalfPageUp
1301 vmap <A-d> <Plug>MoveBlockHalfPageDown
1302 endif "}}}1
1303
1304 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1305 " Handle options only available in Vim 7 and above.
1306 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1307 if version >= "700" "{{{1
1308 version 7.0
1309
1310 " Narrow buffer with \w.
1311 call Mapping("w", ":NarrowRegion<CR>", "n", "v")
1312 " Narrow window with \W.
1313 call Mapping("W", ":NarrowWindow<CR>", "n", "v")
1314
1315 " Helper to show tab name.
1316 fun! <SID>TabName(label, gui) "{{{2
1317   let l:label = a:label
1318   if l:label == ""
1319     let l:label = "No Name"
1320     if a:gui
1321       let l:label = "[" . l:label . "]"
1322     endif
1323   else
1324     let l:label = fnamemodify(l:label, ":t")
1325     if strlen(l:label) >= 18
1326       let l:label = l:label[0:17] . ".."
1327     endif
1328   endif
1329   return l:label
1330 endfun "}}}2
1331
1332 " Find out if any buffer was modified.
1333 fun! <SID>TabModified(buflist) "{{{2
1334   let l:i = 0
1335   while l:i < len(a:buflist)
1336     if getbufvar(a:buflist[l:i], "&modified") == 1
1337       return "+"
1338     endif
1339     let l:i = l:i + 1
1340   endwhile
1341   return ""
1342 endfun "}}}2
1343
1344 " Tab line.
1345 fun! Show_TabLine() "{{{2
1346   let l:colwidth = &numberwidth
1347   if g:marksigns
1348     let l:colwidth += 2
1349   endif
1350   if &diff == 1
1351     let l:colwidth += 2
1352   endif
1353   let l:fmt = printf("%%s%% %ds", l:colwidth)
1354   let l:s = printf(l:fmt, "%#TabLineFill#", "Tabs ")
1355
1356   let l:i = 0
1357   while l:i < tabpagenr("$")
1358     let l:i = l:i + 1
1359     " Get the label.
1360     let l:buflist = tabpagebuflist(l:i)
1361     let l:winnr = tabpagewinnr(l:i)
1362     let l:n = tabpagewinnr(l:i, "$")
1363     let l:label = <SID>TabName(bufname(l:buflist[l:winnr - 1]), 0)
1364     let l:modified = <SID>TabModified(l:buflist)
1365
1366     " Choose highlighting.
1367     if l:i == tabpagenr()
1368       let l:s .= "%#TabLineSel#[" . l:n . l:modified . " " . l:label . "]"
1369     else
1370       let l:s .= "%#TabLine# " . l:n . l:modified . " " . l:label . " "
1371     endif
1372   endwhile
1373
1374   " Padding.
1375   let l:s .= "%#TabLine#%T"
1376   return l:s
1377 endfun "}}}2
1378
1379 " Per tab label for the GUI.
1380 fun! Show_GUITabLine() "{{{2
1381   let l:buflist = tabpagebuflist(v:lnum)
1382   let l:winnr = tabpagewinnr(v:lnum)
1383   let l:s = tabpagewinnr(v:lnum, "$")
1384   let l:label = <SID>TabName(bufname(l:buflist[l:winnr - 1]), 1)
1385   let l:modified = <SID>TabModified(l:buflist)
1386
1387   let l:s .= l:modified . " " . l:label
1388   return l:s
1389 endfun "}}}2
1390
1391 " Handle searching in a BufExplorer window.
1392 fun! <SID>BufExplorer_Search(n) "{{{2
1393   if a:n == 0
1394     let l:re = '^  *\d %'
1395   else
1396     let l:re = "^ *" . a:n
1397   endif
1398
1399   " Find matching line.
1400   let l:line = search(l:re, 'w')
1401   if ! l:line
1402     return
1403   endif
1404
1405   if a:n == 0
1406     return
1407   endif
1408
1409   " Peek ahead to the next matching line.
1410   let l:next = search(l:re, 'w')
1411
1412   " Select the buffer if the match is unambiguous.
1413   if l:next == l:line
1414     exe "normal \<CR>"
1415     return
1416   endif
1417
1418   " Go back.
1419   call cursor(l:line, 0)
1420 endfun! "}}}2
1421
1422 " Entering a BufExplorer window.
1423 fun! <SID>BufExplorer_Map() "{{{2
1424   for l:n in [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" ]
1425     exec "nnoremap <buffer> <silent>" . l:n . " :call <SID>BufExplorer_Search(" . l:n . ")<CR>"
1426   endfor
1427 endfun "}}}2
1428
1429 if has("windows")
1430   se tabline=%!Show_TabLine()
1431   se guitablabel=%!Show_GUITabLine()
1432 endif
1433
1434 if has("autocmd")
1435   au StatusLine CursorHoldI * call Highlight_StatusLine("H")
1436   au StatusLine CursorMovedI * call Highlight_StatusLine("h")
1437   au StatusLine FocusGained * call Highlight_StatusLine("F")
1438   au StatusLine FocusLost * call Highlight_StatusLine("f")
1439   au StatusLine InsertEnter * call Highlight_StatusLine("I")
1440   au StatusLine InsertChange * call Highlight_StatusLine("I")
1441   au StatusLine InsertLeave * call Highlight_StatusLine("i")
1442
1443   if has("signs")
1444     au Signs CursorHoldI * call <SID>Highlight_Signs()
1445     au Signs InsertEnter * call <SID>Highlight_Signs()
1446     au Signs InsertLeave * call <SID>Highlight_Signs()
1447   endif
1448
1449   au Mode BufEnter \[BufExplorer\] call <SID>BufExplorer_Map()
1450 endif
1451
1452 " Limit the size of the popup menu when completing.
1453 if has("insert_expand")
1454   se pumheight=20
1455 endif
1456
1457 " Make diffs vertical by default.
1458 if has("diff")
1459   se diffopt+=vertical
1460 endif
1461
1462 " Set size of numbers column.
1463 if has("linebreak")
1464   se numberwidth=5
1465 endif
1466
1467 " Add "previous tab" mapping as gb.
1468 map gb :tabprevious<CR>:<CR>
1469
1470 " Transparency.
1471 if has("gui_macvim")
1472   se transparency=15
1473 endif
1474
1475 " Yet more GUI options.  Add tabs.
1476 if has("gui")
1477   se go+=e
1478 endif
1479
1480 " Perforce.
1481 let g:p4EnableMenu=1
1482 let g:p4Presets='P4CONFIG'
1483
1484 " BufExplorer.
1485 let g:bufExplorerShowRelativePath=1
1486 let g:bufExplorerSplitOutPathName=0
1487
1488 " NERDcommenter.
1489 let g:NERDSpaceDelims=1
1490
1491 " localvimrc.
1492 let g:localvimrc_persistent=1
1493
1494 " Gitv.
1495 let g:Gitv_OpenHorizontal='auto'
1496 let g:Gitv_WipeAllOnClose=1
1497 endif "}}}1
1498
1499 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1500 " Handle options only available in Vim 7.2 and above.
1501 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1502 if version >= "702" "{{{1
1503 if has("autocmd")
1504   " http://vim.wikia.com/wiki/Highlight_unwanted_spaces
1505   augroup WhitespaceMatch
1506   autocmd!
1507   au Display BufWinEnter * call Extra_Whitespace_Match()
1508   au Display Syntax * call Extra_Whitespace_Match()
1509   au Display BufWinLeave * call clearmatches()
1510   augroup END
1511
1512   fun! Extra_Whitespace_Match() "{{{2
1513     " \s\+            <whitespace>
1514     "            $    <before end of line>
1515     "        \@<!     <unless preceded by>
1516     "     \%#         <cursor position, ie when typing>
1517     let l:pattern = '\s\+\%#\@<!$'
1518     " Don't match single space in first column of diff.
1519     if &ft =~# '^diff$\|git'
1520       "         \@!  <unless also matching>
1521       " \(^\s$\)     <a single whitespace>
1522       let l:pattern = '\(^\s$\)\@!' . l:pattern
1523     endif
1524
1525     let l:hl = 'ctermfg=red guifg=red'
1526     if ! &list
1527       " Underline if we aren't using listchars.
1528       let l:hl = l:hl . ' cterm=underline gui=underline'
1529     endif
1530     highlight clear ExtraWhitespace
1531     exe "highlight ExtraWhitespace " . l:hl
1532     if exists('w:whitespace_match_number')
1533       try
1534         call matchdelete(w:whitespace_match_number)
1535       catch
1536       endtry
1537       call matchadd('ExtraWhitespace', l:pattern, 10, w:whitespace_match_number)
1538     else
1539       let w:whitespace_match_number = matchadd('ExtraWhitespace', l:pattern)
1540     endif
1541   endfun "}}}2
1542
1543   call Extra_Whitespace_Match()
1544 endif
1545
1546 endif "}}}1
1547
1548 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1549 " Handle options only available in Vim 7.3 and above.
1550 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1551 if version >= "703" "{{{1
1552 version 7.3
1553
1554 " Toggle persistent undo with \u.
1555 call Mapping("u", ":call <SID>Cycle_Undo()<CR>")
1556 " Remove persistent undo file with \U.
1557 call Mapping("U", ":call <SID>Clear_Undo()<CR>")
1558
1559 " Toggle gundo window with \g.
1560 call Mapping("g", ":call gundo#GundoToggle()<CR>")
1561
1562 " Use a persistent undo file if it exists.
1563 fun! <SID>Check_Undo() "{{{2
1564   if filereadable(undofile(expand("%")))
1565     setlocal undofile
1566   endif
1567 endfun "}}}2
1568
1569 " Toggle persistent undo for this buffer.
1570 fun! <SID>Cycle_Undo() "{{{2
1571   if has("persistent_undo")
1572     let &undofile = ! &undofile
1573   endif
1574 endfun "}}}2
1575
1576 " Remove the persistent undo file for this buffer.
1577 fun! <SID>Clear_Undo() "{{{2
1578   if ! has("persistent_undo")
1579     return
1580   endif
1581
1582   setlocal noundofile
1583
1584   let l:f = expand("%")
1585   if l:f == ""
1586     return
1587   endif
1588
1589   let l:u = undofile(l:f)
1590   if l:u == ""
1591     return
1592   endif
1593
1594   if ! filereadable(l:u) || ! filewritable(l:u)
1595     return
1596   endif
1597
1598   call delete(l:u)
1599 endfun "}}}2
1600
1601 " Toggle ColorColumn at cursor position.
1602 fun! <SID>Cycle_ColorColumn() "{{{2
1603   if ! has("syntax")
1604     return
1605   endif
1606
1607   let l:cc = &colorcolumn
1608   let l:column = col(".")
1609   let l:re = ",*\\<" . l:column . "\\>"
1610   if l:cc =~# l:re
1611     let l:cc = substitute(l:cc, l:re, "", "g")
1612   else
1613     let l:cc = l:cc . "," . l:column
1614   endif
1615   let &colorcolumn = substitute(l:cc, "^,*", "", "")
1616 endfun "}}}2
1617
1618 if has("syntax")
1619   " Enable showing ColorColumn at cursor position with \CC.
1620   call Mapping("CC", ":call <SID>Cycle_ColorColumn()<CR>")
1621   " Remove last shown ColorColumn with \Cc.
1622   call Mapping("Cc", ":let &colorcolumn=substitute(&colorcolumn, \",*[0-9]*$\", \"\", \"\")<CR>")
1623   " Remove all ColorColumns with \Cx.
1624   call Mapping("Cx", ":se colorcolumn=<CR>")
1625 endif
1626
1627 " Use persistent undo if available.
1628 if has("autocmd")
1629   if has("persistent_undo")
1630     au File BufReadPost * call <SID>Check_Undo()
1631   endif
1632
1633   if has("cursorbind")
1634     au Display WinEnter * if &diff | se cursorbind | endif
1635   endif
1636 endif
1637
1638 " Mapping to reload the gundo window.
1639 if has("autocmd")
1640   au Mode BufEnter * if &ft == "gundo" | try | nnoremap <silent> <buffer> <unique> r :call gundo#GundoToggle()<CR>:call gundo#GundoToggle()<CR> | catch | endtry | endif
1641 endif
1642
1643 endif "}}}1
1644 "
1645 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1646 " Handle options only available in Vim 7.4 and above.
1647 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1648 if version >= "704" "{{{1
1649 version 7.4
1650
1651 if has("syntax")
1652   se cursorline
1653 endif
1654
1655 endif "}}}1
1656
1657 " Resize after startup.
1658 if version >= "500" "{{{1
1659 if has("autocmd")
1660   au Display VimEnter * call Startup_Resize()
1661 endif
1662 endif "}}}1