Use -bg for background colour.
[profile.git] / .vimrc
1 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
2 " Multi-version vimrc compatible with version 4 and above.
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 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
17 " No compatibility mode.
18 se nocp
19
20 " Tabstop 2.
21 se ts=2
22 " And use spaces not tabs.
23 se expandtab
24 " And << and >> indent by 2.
25 se sw=2
26
27 " Allow backspace to delete before start of line.
28 se bs=2
29
30 " Show the ruler.
31 se ruler
32 " Show partial commands in the ruler.
33 se showcmd
34 " And always show the status line.
35 se laststatus=2
36
37 " Use C indent style.
38 se cindent
39 se cinkeys=0{,0},0),:,!^F,o,O,e
40 se cinoptions=b1,c2
41
42 " GUI options.
43 se go=aglmr
44
45 " Don't be bugged by messages at the bottom of the screen.
46 se shm=aot
47
48 " Find as you type.
49 se incsearch
50
51 " Case-insensitive search.
52 se ignorecase
53 " But override by typing capitals.
54 se smartcase
55
56 " Look for ctags in home directory first.
57 se tags=~/.tags,./tags,tags
58
59 " Don't timeout waiting to interpet, eg, <ESC>OA as an escape code.
60 se ttimeoutlen=100
61
62 " Use ^B to search backward when completing.
63 inoremap <C-b> <C-p>
64 " Use ^L to show matching completions but don't select one.
65 inoremap <C-l> <C-n><C-p>
66
67 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
68 " Handle options only available in Vim 5 and above.
69 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
70 if version >= "500"
71 version 5.0
72
73 " Tell Vim we use dark backgrounds in our terminals.
74 if ! has("gui_running")
75   se bg=dark
76 endif
77
78 " Allow mouse use in a terminal.
79 se mouse=nvir
80
81 " Update more quickly.  For use with sign highlighting as polling for
82 " CursorMove makes redrawing slow.
83 if has("signs")
84   se updatetime=500
85 endif
86
87 " Enable tab-completion prompting for commands.
88 se wildmenu
89 " Don't list object files when globbing files to load.
90 se wildignore+=*.o,*.obj
91 " So there's no need to assign them low priority.
92 se suffixes-=*.o,*.obj
93
94 " Save sessions in UNIX format with / as file separator.  This is
95 " cross-platform.
96 se ssop+=unix,slash
97
98 " Nuke any pre-existing autocommands.
99 augroup Display
100 autocmd!
101 augroup Mode
102 autocmd!
103 if has("signs")
104   augroup Signs
105   autocmd!
106 endif
107 augroup StatusLine
108 autocmd!
109 augroup END
110
111 " Save the current window width so we can restore it when we quit.
112 if ! exists("g:oldcols")
113   let g:oldcols=&columns
114 endif
115
116 " More GUI options.  Add icon, tearoffs and toolbar.
117 se go+=itT
118
119 " Allow dynamic window resize even if we aren't in an xterm.
120 se t_WS=\e[8;%p1%d;%p2%dt
121
122 " Highlight search results.
123 se hlsearch
124
125 " Set graphical window title.
126 se titlestring=%{Show_TitleString()}
127
128 " Syntax highlighting.  New versions will use syn enable instead.
129 if version < "600"
130   syn on
131 endif
132
133 " Use a discernably different colour to highlight the cursor which shows 
134 " matching brackets.  Our regular cursor is green so use blue instead of cyan.
135 hi MatchParen ctermbg=blue
136
137 " Catch typos.
138 command! W :w
139 command! Wq :wq
140 command! Wqa :wqa
141
142 " Set up our variables.
143 fun! Iain_Vars()
144   if ! exists("b:iainlist")
145     let b:iainlist = 0
146   endif
147   if ! exists("b:iainhex")
148     let b:iainhex = 0
149   endif
150   if ! exists("b:iainverbose")
151     let b:iainverbose = 0
152   endif
153   if ! exists("b:iainstatus")
154     " Window Flags: (F)ocused, (I)nsert mode, Cursor (H)old.
155     let b:iainstatus = "Fih"
156   endif
157   if ! exists("g:iainextracolumns")
158     let g:iainextracolumns = 0
159   endif
160   if has("signs")
161     if ! exists("g:marksigns")
162       let g:marksigns = 0
163     endif
164     if ! exists("g:firstsign")
165       let g:firstsign = 100
166     endif
167   endif
168 endfun
169
170 " Helper for status line.
171 " Show space, underscore or dollar sign depending on list mode.
172 fun! Show_List()
173   call Iain_Vars()
174   if b:iainlist == 0
175     " No list.
176     return " "
177   elseif b:iainlist == 1
178     " Just tabs.
179     return "_"
180   else
181     " Full list.
182     return "\$"
183   endif
184 endfun
185
186 " Helper for status line.
187 " Show c or C to denote case-sensitivity.
188 fun! Show_Case()
189   if &ic
190     return "c"
191   else
192     return "C"
193   endif
194 endfun
195
196 " Helper for status line.
197 " Show the size of the tabstop.
198 fun! Show_Tabstop()
199   return &ts
200 endfun
201
202 " Helper for status line.
203 " Show p when paste mode is on.
204 fun! Show_Paste()
205   if &paste
206     return "p"
207   else
208     return ""
209   endif
210 endfun
211
212 " Show the window title.
213 fun! Show_TitleString()
214   if bufname("") == ""
215     let l:ts1='Vim'
216   else
217     let l:ts1=printf("%2d: %s", bufnr(""), expand('%t'))
218   endif
219   return printf("%s (%s) %s", l:ts1, getcwd(), v:servername)
220 endfun
221
222 " Show the status line.
223 fun! Show_StatusLine()
224   call Iain_Vars()
225   let l:sl1='%2n\:\ %<%f\ [%{Show_List()}%{Show_Case()}%{Show_Tabstop()}%{Show_Paste()}%Y%M%R]\ %='
226   let l:sl3='L:%4.6l/%-4.6L\ C:%3.6c\ \|\ %P'
227   let l:hexformat='%b'
228   if b:iainhex
229     let l:hexformat='0\x%02B'
230   endif
231   if b:iainverbose
232     let l:sl2=l:hexformat . '\ \|\ P:%4.6o\ '
233   else
234     let l:sl2=''
235   endif
236   exec "set statusline=" . l:sl1 . l:sl2 . l:sl3
237 endfun
238
239 " Toggle case-sensitivity.
240 fun! Invert_Case()
241   let &ic = ! &ic
242 endfun
243
244 " Grow or shrink the window size.
245 fun! Resize_Columns(op, ...)
246   if a:0 == 0
247     " Vim 5 hardcodes the size of numbers column to 8.
248     if version >= "700"
249       let l:columns = &numberwidth
250     else
251       let l:columns = 8
252     endif
253   else
254     let l:columns = a:1
255   endif
256
257   exe "let l:resize=" . &columns . a:op . l:columns
258   let l:resize = "se columns=" . l:resize
259
260   " HACK: Inside screen there is an extra line for the status bar.  Vim
261   " manages the resize by sending an escape sequence to set the number of
262   " lines and number of columns in one action.  To do this it will first query
263   " the number of lines and then set <same number of lines> by <new number of
264   " columns>.  Because of the extra line for the status bar this results in
265   " the real terminal being shrunk by a line.  We ask for the terminal to grow
266   " by a line so it ends up actually being the same.
267   if &term =~ '^screen'
268     let l:resize = l:resize . " lines=" . (&lines + 1)
269   endif
270
271   exe l:resize
272 endfun
273
274 " Toggle number display.
275 fun! Number(resize)
276   call Iain_Vars()
277   let &number = ! &number
278
279   if version >= "700"
280     let l:i = 0
281     let l:num_numbers = 0
282     while l:i <= winnr("$")
283       if getwinvar(l:i, "&number") == 1
284         let l:num_numbers = l:num_numbers + 1
285       endif
286       let l:i = l:i + 1
287     endwhile
288
289     if l:num_numbers == 0
290       let g:iainextracolumns = 0
291       if a:resize
292         call Resize_Columns("-")
293       endif
294     elseif g:iainextracolumns == 0
295       let g:iainextracolumns = 1
296       if a:resize
297         call Resize_Columns("+")
298       endif
299     endif
300   endif
301 endfun
302
303 " Restore window size.
304 au Display VimLeave * if exists("g:oldcols") | call Resize_Columns("-", (&columns - g:oldcols)) | endif
305
306 " Map Makefile mode.
307 au Mode BufEnter * if &ft == "make" | call MakeMode_map() | endif
308 au Mode BufLeave * if &ft == "make" | call MakeMode_unmap() | endif
309
310 " Entering Make mode.
311 fun! MakeMode_map()
312         call Iain_Vars()
313   let b:iainlist=1
314   call Cycle_List()
315   set ts=8
316   set noexpandtab
317 endfun
318
319 " Leaving Make mode.
320 fun! MakeMode_unmap()
321   call Cycle_List()
322   set ts=2
323   set expandtab
324 endfun
325
326 " Show the status line for the first time.
327 call Show_StatusLine()
328
329 " Function to create mappings with either a hardcoded \ or <Leader>.
330 fun! Mapping(keysequence,mapping)
331   if version < "600"
332     exec "map \\" . a:keysequence . " " . a:mapping
333   else
334     exec "map <Leader>" . a:keysequence . " " . a:mapping
335   endif
336 endfun
337
338 " Use - and = to create underlines.
339 call Mapping("-", "yyp:s/./-/g<RETURN>:let @/=''<RETURN>:<RETURN>")
340 call Mapping("=", "yyp:s/./=/g<RETURN>:let @/=''<RETURN>:<RETURN>")
341
342 " Change to ts=2 with \2.
343 call Mapping("2", ":se ts=2<CR>:<CR>")
344 " Change to ts=4 with \4.
345 call Mapping("4", ":se ts=4<CR>:<CR>")
346 " Change to ts=8 with \8.
347 call Mapping("8", ":se ts=8<CR>:<CR>")
348 " Change to ts=16 with \6.
349 call Mapping("6", ":se ts=16<CR>:<CR>")
350 " Change to ts=32 with \3.
351 call Mapping("3", ":se ts=32<CR>:<CR>")
352 " Toggle paste mode with \p.
353 call Mapping("p", ":se paste!<CR>:<CR>")
354 " Swap case-sensitivity with \c.
355 call Mapping("c", ":call Invert_Case()<CR>:<CR>")
356 " Change number mode with \n.
357 call Mapping("n", ":call Number(1)<CR>:<CR>")
358 " Expand or shrink window size with \> and \<.
359 call Mapping(">", ":call Resize_Columns('+')<CR>:<CR>")
360 call Mapping("<", ":call Resize_Columns('-')<CR>:<CR>")
361 " Clear search pattern with \/.
362 call Mapping("/", ":let @/=\"\"<CR>:<CR>")
363
364 " Forget the Ex mode mapping.
365 map Q <NOP>
366
367 " Vim tip 99: What's the highlighting group under the cursor?
368 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>")
369
370 fun! Startup_Resize()
371   let l:columns = 0
372
373   " Resize for numbers.
374   if &number
375     if version >= "700"
376       let l:columns = &numberwidth
377     else
378       let l:columns = 8
379     endif
380   endif
381
382   " Resize for signs.
383   if has("signs")
384     if g:marksigns
385       if version >= "600"
386         let l:columns = l:columns + 2
387       endif
388     endif
389   endif
390
391   if g:oldcols < (80 + l:columns)
392     call Resize_Columns("+", l:columns)
393   endif
394 endfun
395
396 " Show numbers by default.
397 au Display VimEnter * call Number(0)
398
399 " Resize after startup.
400 au Display VimEnter * call Startup_Resize()
401 endif
402
403 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
404 " Handle options only available in Vim 6 and above.
405 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
406 if version >= "600"
407 version 6.0
408
409 " Remember quickfix state.
410 let g:quickfixing=0
411
412 " Set indenting by filetype.
413 filetype indent on
414
415 " Less intrusive syntax highlighting.
416 syn enable
417
418 " Set colours.
419 if has("gui_running")
420   try
421     if has("win32")
422       se guifont=DejaVu_Sans_Mono:h10:cANSI
423     else
424       se guifont=DejaVu\ Sans\ Mono\ 10
425     endif
426   catch
427   endtry
428 endif
429 if has("gui_running") || &t_Co > 16
430   try
431     colo iain
432   catch
433   endtry
434 endif
435
436 " Ignore whitespace when diffing.
437 se diffopt=filler,iwhite
438
439 " Expand window when doing a vertical diff.
440 if &diff
441   let &columns = 164
442 endif
443
444 " Remember that we are opening the quickfix window.
445 au Mode BufWinEnter quickfix let g:quickfixing=1
446 au Mode BufUnload * if &ft == "qf" | let g:quickfixing=0 | endif
447
448 " Allow in-place editing of crontabs.
449 au Mode FileType crontab set backupcopy=yes
450
451 " Make * and # work the way you expect in visual mode.
452 vnoremap * y/\V<C-R>=substitute(escape(@@,"/\\"),"\n","\\\\n","ge")<CR><CR>
453 vnoremap # y?\V<C-R>=substitute(escape(@@,"?\\"),"\n","\\\\n","ge")<CR><CR>
454
455 " Set mark and update highlighting.
456 if has("signs")
457   au Signs BufEnter * call <SID>Highlight_Signs()
458   au Signs CursorHold * call <SID>Highlight_Signs()
459 endif
460
461 fun! <SID>Prep_Signs()
462   if ! exists("b:signdot") || ! g:marksigns
463     let b:signdot=0
464   endif
465   if ! exists("b:signdash") || ! g:marksigns
466     let b:signdash=0
467   endif
468   if ! exists("b:signquote") || ! g:marksigns
469     let b:signquote=0
470   endif
471   if ! exists("b:signcaret") || ! g:marksigns
472     let b:signcaret=0
473   endif
474   if ! exists("b:signless") || ! g:marksigns
475     let b:signless=0
476   endif
477   if ! exists("b:signgreater") || ! g:marksigns
478     let b:signgreater=0
479   endif
480   if ! exists("b:signleft") || ! g:marksigns
481     let b:signleft=0
482   endif
483   if ! exists("b:signright") || ! g:marksigns
484     let b:signright=0
485   endif
486   if ! exists("b:signa") || ! g:marksigns
487     let b:signa=0
488   endif
489   if ! exists("b:signb") || ! g:marksigns
490     let b:signb=0
491   endif
492   if ! exists("b:signc") || ! g:marksigns
493     let b:signc=0
494   endif
495   if ! exists("b:signd") || ! g:marksigns
496     let b:signd=0
497   endif
498   if ! exists("b:signe") || ! g:marksigns
499     let b:signe=0
500   endif
501   if ! exists("b:signf") || ! g:marksigns
502     let b:signf=0
503   endif
504   if ! exists("b:signA") || ! g:marksigns
505     let b:signA=0
506   endif
507   if ! exists("b:signB") || ! g:marksigns
508     let b:signB=0
509   endif
510   if ! exists("b:signC") || ! g:marksigns
511     let b:signC=0
512   endif
513   if ! exists("b:signD") || ! g:marksigns
514     let b:signD=0
515   endif
516   if ! exists("b:signE") || ! g:marksigns
517     let b:signE=0
518   endif
519   if ! exists("b:signF") || ! g:marksigns
520     let b:signF=0
521   endif
522 endfun!
523
524 fun! <SID>Place_Sign(number, line, old, name)
525   if a:line == a:old
526     return a:old
527   endif
528
529   exe "sign unplace " . (g:firstsign + a:number) . " buffer=" . bufnr("")
530   " Don't place the sign if it would conflict with the last change sign.
531   exe "sign place " . (g:firstsign + a:number) . " line=" . a:line . " name=" . a:name . " buffer=" . bufnr("")
532   return a:line
533 endfun
534
535 fun! <SID>Highlight_Signs(...)
536   if ! has("signs") || ! g:marksigns
537     return
538   endif
539
540   call <SID>Prep_Signs()
541
542   let b:signdot = <SID>Place_Sign(0, line("'."), b:signdot, "MarkDot")
543   let b:signdash = <SID>Place_Sign(1, line("''"), b:signdash, "MarkDash")
544   let b:signquote = <SID>Place_Sign(2, line("'\""), b:signquote, "MarkQuote")
545   let b:signcaret = <SID>Place_Sign(3, line("'^"), b:signcaret, "MarkCaret")
546   let b:signless = <SID>Place_Sign(4, line("'<"), b:signcaret, "MarkLess")
547   let b:signgreater = <SID>Place_Sign(5, line("'>"), b:signcaret, "MarkGreater")
548   let b:signleft = <SID>Place_Sign(6, line("'["), b:signcaret, "MarkLeft")
549   let b:signright = <SID>Place_Sign(7, line("']"), b:signcaret, "MarkRight")
550
551   let b:signa = <SID>Place_Sign(8, line("'a"), b:signa, "Marka")
552   let b:signb = <SID>Place_Sign(9, line("'b"), b:signb, "Markb")
553   let b:signc = <SID>Place_Sign(10, line("'c"), b:signc, "Markc")
554   let b:signd = <SID>Place_Sign(11, line("'d"), b:signd, "Markd")
555   let b:signe = <SID>Place_Sign(12, line("'e"), b:signe, "Marke")
556   let b:signf = <SID>Place_Sign(13, line("'f"), b:signf, "Markf")
557   let b:signA = <SID>Place_Sign(14, line("'A"), b:signA, "MarkA")
558   let b:signB = <SID>Place_Sign(15, line("'B"), b:signB, "MarkB")
559   let b:signC = <SID>Place_Sign(16, line("'C"), b:signC, "MarkC")
560   let b:signD = <SID>Place_Sign(17, line("'D"), b:signD, "MarkD")
561   let b:signE = <SID>Place_Sign(18, line("'E"), b:signE, "MarkE")
562   let b:signF = <SID>Place_Sign(19, line("'F"), b:signF, "MarkF")
563 endfun
564
565 " Toggle signs.
566 fun! <SID>Cycle_Signs(resize)
567   if ! has("signs")
568     return
569   endif
570   call Iain_Vars()
571   let g:marksigns = ! g:marksigns
572
573   if g:marksigns
574     " Signs to highlight marks.
575     " Syntax won't work properly in Vim 6.
576     sign define MarkDash text=' texthl=MarkSign
577     sign define MarkDot text=* texthl=MarkDot
578     sign define MarkQuote text=" texthl=MarkSign
579     sign define MarkCaret text=^ texthl=MarkSign
580     sign define MarkLess text=< texthl=MarkSign
581     sign define MarkGreater text=> texthl=MarkSign
582     sign define MarkLeft text=[ texthl=MarkSign
583     sign define MarkRight text=] texthl=MarkSign
584     sign define Marka text=a texthl=MarkSign linehl=MarkLine
585     sign define Markb text=b texthl=MarkSign linehl=MarkLine
586     sign define Markc text=c texthl=MarkSign linehl=MarkLine
587     sign define Markd text=d texthl=MarkSign linehl=MarkLine
588     sign define Marke text=e texthl=MarkSign linehl=MarkLine
589     sign define Markf text=f texthl=MarkSign linehl=MarkLine
590     sign define MarkA text=A texthl=MarkSign linehl=MarkLine
591     sign define MarkB text=B texthl=MarkSign linehl=MarkLine
592     sign define MarkC text=C texthl=MarkSign linehl=MarkLine
593     sign define MarkD text=D texthl=MarkSign linehl=MarkLine
594     sign define MarkE text=E texthl=MarkSign linehl=MarkLine
595     sign define MarkF text=F texthl=MarkSign linehl=MarkLine
596
597     if a:resize
598       call Resize_Columns("+", 2)
599     endif
600     call <SID>Highlight_Signs()
601   else
602     exe "sign unplace " . (g:firstsign + 0)
603     exe "sign unplace " . (g:firstsign + 1)
604     exe "sign unplace " . (g:firstsign + 2)
605     exe "sign unplace " . (g:firstsign + 3)
606     exe "sign unplace " . (g:firstsign + 4)
607     exe "sign unplace " . (g:firstsign + 5)
608     exe "sign unplace " . (g:firstsign + 6)
609     exe "sign unplace " . (g:firstsign + 7)
610     exe "sign unplace " . (g:firstsign + 8)
611     exe "sign unplace " . (g:firstsign + 9)
612     exe "sign unplace " . (g:firstsign + 10)
613     exe "sign unplace " . (g:firstsign + 11)
614     exe "sign unplace " . (g:firstsign + 12)
615     exe "sign unplace " . (g:firstsign + 13)
616     exe "sign unplace " . (g:firstsign + 14)
617     exe "sign unplace " . (g:firstsign + 15)
618     exe "sign unplace " . (g:firstsign + 16)
619     exe "sign unplace " . (g:firstsign + 17)
620     exe "sign unplace " . (g:firstsign + 18)
621     exe "sign unplace " . (g:firstsign + 19)
622
623     sign undefine MarkDash
624     sign undefine MarkDot
625     sign undefine MarkQuote
626     sign undefine MarkCaret
627     sign undefine MarkLess
628     sign undefine MarkGreater
629     sign undefine MarkLeft
630     sign undefine MarkRight
631     sign undefine Marka
632     sign undefine Markb
633     sign undefine Markc
634     sign undefine Markd
635     sign undefine Marke
636     sign undefine Markf
637     sign undefine MarkA
638     sign undefine MarkB
639     sign undefine MarkC
640     sign undefine MarkD
641     sign undefine MarkE
642     sign undefine MarkF
643
644     call <SID>Prep_Signs()
645     if a:resize
646       call Resize_Columns("-", 2)
647     endif
648   endif
649 endfun
650
651 " Change list mode.
652 fun! Cycle_List()
653   let basic='tab:\\_,trail:_,extends:<,precedes:>'
654   call Iain_Vars()
655   let b:iainlist = b:iainlist + 1
656   if b:iainlist > 2
657     let b:iainlist = 0
658   endif
659   if b:iainlist == 0
660     set nolist
661   elseif b:iainlist == 1
662     exec "set lcs=" . basic
663     set list
664   else
665     exec "set lcs=" . basic . ",eol:$"
666     set list
667   endif
668 endfun
669
670 " Cycle between hex and decimal display of toolbar stuff.
671 fun! Cycle_HexStatusLine()
672   call Iain_Vars()
673   let b:iainhex = ! b:iainhex
674   call Show_StatusLine()
675 endfun
676
677 " Cycle verbose display of toolbar stuff.
678 fun! Cycle_VerboseStatusLine()
679   call Iain_Vars()
680   let b:iainverbose = ! b:iainverbose
681   call Show_StatusLine()
682 endfun
683
684 " Toggle quickfix window.
685 fun! Cycle_Quickfix()
686   if g:quickfixing == 1
687     cclose
688     let g:quickfixing=0
689   else
690     copen
691   endif
692 endfun
693
694 " Swap hex/decimal statusline with \x.
695 call Mapping("x", ":call Cycle_HexStatusLine()<CR>:<CR>")
696 " Change statusline verbosity with \v.
697 call Mapping("v", ":call Cycle_VerboseStatusLine()<CR>:<CR>")
698 " Cycle list styles with \l.
699 call Mapping("l", ":call Cycle_List()<CR>:<CR>")
700 " Toggle tags with \t.
701 call Mapping("t", ":Tlist<CR>")
702 " Change foldmethod with \f.
703 call Mapping("f", ":se foldenable!<CR>:<CR>")
704 " Toggle quickfix window with \q.
705 call Mapping("q", ":call Cycle_Quickfix()<CR>:<CR>")
706 " Rerun filetype detection with \s.  The s is for syntax, as this will be
707 " updated as a side-effect.
708 call Mapping("s", ":filetype detect<CR>:<CR>")
709 " Toggle marks with \m.
710 call Mapping("m", ":call <SID>Cycle_Signs(1)<CR>:<CR>")
711
712 fun! <SID>Iain_Colour(colour)
713   if &t_Co == 88
714     if a:colour == "darkblue"
715       return 17
716     elseif a:colour == "darkmagenta"
717       return 33
718     elseif a:colour == "darkred"
719       return 32
720     elseif a:colour == "red"
721       return 64
722     endif
723   elseif &t_Co == 256
724     if a:colour == "darkblue"
725       return 17
726     elseif a:colour == "darkmagenta"
727       return 90
728     elseif a:colour == "darkred"
729       return 88
730     elseif a:colour == "red"
731       return 196
732     endif
733   else
734     return a:colour
735   endif
736 endfun
737
738 " Change status bar colour when various things happen.
739 " Flags: H/h: Cursor held/moved.
740 "        F/f: Focus gained/lost.
741 "        I/i: Insert mode entered/left.
742 fun! Highlight_StatusLine(flag)
743   " Get current status.
744   call Iain_Vars()
745
746   " Change the status based on the flag.  XXX: Does Vim let us to do flags?
747   let re = "[" . tolower(a:flag) . toupper(a:flag) . "]"
748   let b:iainstatus = substitute(b:iainstatus, re, a:flag, "")
749
750   let l:normalcolour = "darkblue"
751   let l:editingcolour = "darkmagenta"
752   let l:warningcolour = "darkred"
753   let l:readonlycolour = "red"
754
755   " Default colour.
756   let l:colour = l:normalcolour
757   " Maybe override depending on status.
758   if b:iainstatus =~# "H"
759     if b:iainstatus =~# "I"
760       " Held in insert mode.  Add extra highlight if we don't have focus.
761       if b:iainstatus =~# "f"
762         let l:colour = l:warningcolour
763       else
764         let l:colour = l:editingcolour
765       endif
766     endif
767   else
768     if b:iainstatus =~# "I"
769       " Regular insert mode.
770       let l:colour = l:editingcolour
771     endif
772   endif
773
774   " Override again if readonly.
775   if l:colour != l:normalcolour
776     if getbufvar("", "&ro")
777       let l:colour = l:readonlycolour
778     endif
779   endif
780
781   let l:termcolour = <SID>Iain_Colour(l:colour)
782
783   exec "highlight StatusLine gui=bold term=bold cterm=bold guifg=white guibg=" . l:colour . " ctermfg=white ctermbg=" . l:termcolour
784 endfun
785
786 au Display VimEnter * call Highlight_StatusLine("")
787
788 " Show signs by default.
789 au Display VimEnter * call <SID>Cycle_Signs(0)
790 endif
791
792 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
793 " Handle options only available in Vim 7 and above.
794 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
795 if version >= "700"
796 version 7.0
797
798 " Helper to show tab name.
799 fun! <SID>TabName(label, gui)
800   let l:label = a:label
801   if l:label == ""
802     let l:label = "No Name"
803     if a:gui
804       let l:label = "[" . l:label . "]"
805     endif
806   else
807     let l:label = fnamemodify(l:label, ":t")
808     if strlen(l:label) >= 18
809       let l:label = l:label[0:17] . ".."
810     endif
811   endif
812   return l:label
813 endfun
814
815 " Find out if any buffer was modified.
816 fun! <SID>TabModified(buflist)
817   let l:i = 0
818   while i < len(a:buflist)
819     if getbufvar(a:buflist[l:i], "&modified") == 1
820       return "+"
821     endif
822     let l:i = l:i + 1
823   endwhile
824   return ""
825 endfun
826
827 " Tab line.
828 fun! Show_TabLine()
829   let l:s = "%#TabLineFill#Tabs:"
830
831   let l:i = 0
832   while l:i < tabpagenr("$")
833     let l:i = l:i + 1
834     " Get the label.
835     let l:buflist = tabpagebuflist(l:i)
836     let l:winnr = tabpagewinnr(l:i)
837     let l:n = tabpagewinnr(l:i, "$")
838     let l:label = <SID>TabName(bufname(l:buflist[l:winnr - 1]), 0)
839     let l:modified = <SID>TabModified(l:buflist)
840
841     " Choose highlighting.
842     if l:i == tabpagenr()
843       let l:s .= "%#TabLineSel#[" . l:n . l:modified . " " . l:label . "]"
844     else
845       let l:s .= "%#TabLine# " . l:n . l:modified . " " . l:label . " "
846     endif
847   endwhile
848
849   " Padding.
850   let l:s .= "%#TabLine#%T"
851   return l:s
852 endfun
853
854 " Per tab label for the GUI.
855 fun! Show_GUITabLine()
856   let l:buflist = tabpagebuflist(v:lnum)
857   let l:winnr = tabpagewinnr(v:lnum)
858   let l:s = tabpagewinnr(v:lnum, "$")
859   let l:label = <SID>TabName(bufname(l:buflist[l:winnr - 1]), 1)
860   let l:modified = <SID>TabModified(l:buflist)
861
862   let l:s .= l:modified . " " . l:label
863   return l:s
864 endfun
865
866 se tabline=%!Show_TabLine()
867 se guitablabel=%!Show_GUITabLine()
868
869 au StatusLine CursorHoldI * call Highlight_StatusLine("H")
870 au StatusLine CursorMovedI * call Highlight_StatusLine("h")
871 au StatusLine FocusGained * call Highlight_StatusLine("F")
872 au StatusLine FocusLost * call Highlight_StatusLine("f")
873 au StatusLine InsertEnter * call Highlight_StatusLine("I")
874 au StatusLine InsertLeave * call Highlight_StatusLine("i")
875
876 if has("signs")
877   au Signs InsertEnter * call <SID>Highlight_Signs()
878   au Signs InsertLeave * call <SID>Highlight_Signs()
879 endif
880
881 " Limit the size of the popup menu when completing.
882 se pumheight=20
883
884 " Make diffs vertical by default.
885 se diffopt+=vertical
886
887 " Set size of numbers column.
888 se numberwidth=5
889
890 " Add "previous tab" mapping as gb.
891 map gb :tabprevious<CR>:<CR>
892
893 " Transparency.
894 if has("gui_macvim")
895   se transparency=15
896 endif 
897
898 " Yet more GUI options.  Add tabs.
899 se go+=e
900
901 " Perforce.
902 let g:p4EnableMenu=1
903 let g:p4Presets='P4CONFIG'
904 endif