98c3c52c9faf8aae6d78a9be880090b2138ffe27
[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 " Enable tab-completion prompting for commands.
79 se wildmenu
80 " Don't list object files when globbing files to load.
81 se wildignore+=*.o,*.obj
82 " So there's no need to assign them low priority.
83 se suffixes-=*.o,*.obj
84
85 " Save sessions in UNIX format with / as file separator.  This is
86 " cross-platform.
87 se ssop+=unix,slash
88
89 " Nuke any pre-existing autocommands.
90 autocmd!
91
92 " Save the current window width so we can restore it when we quit.
93 let oldcols=&columns
94
95 " More GUI options.  Add icon, tearoffs and toolbar.
96 se go+=itT
97
98 " Allow dynamic window resize even if we aren't in an xterm.
99 se t_WS=\e[8;%p1%d;%p2%dt
100
101 " Highlight search results.
102 se hlsearch
103
104 " Set graphical window title.
105 se titlestring=%{Show_TitleString()}
106
107 " Syntax highlighting.  New versions will use syn enable instead.
108 if version < "600"
109   syn on
110 endif
111
112 " Use a discernably different colour to highlight the cursor which shows 
113 " matching brackets.  Our regular cursor is green so use blue instead of cyan.
114 hi MatchParen ctermbg=blue
115
116 " Catch typos.
117 command! W :w
118 command! Wq :wq
119 command! Wqa :wqa
120
121 " Set up our variables.
122 fun! Iain_Vars()
123   if ! exists("b:iainlist")
124     let b:iainlist = 0
125   endif
126   if ! exists("b:iainhex")
127     let b:iainhex = 0
128   endif
129   if ! exists("b:iainverbose")
130     let b:iainverbose = 0
131   endif
132   if ! exists("b:iainstatus")
133     " Window Flags: (F)ocused, (I)nsert mode, Cursor (H)old.
134     let b:iainstatus = "Fih"
135   endif
136   if ! exists("g:iainextracolumns")
137     let g:iainextracolumns = 0
138   endif
139 endfun
140
141 " Helper for status line.
142 " Show space, underscore or dollar sign depending on list mode.
143 fun! Show_List()
144   call Iain_Vars()
145   if b:iainlist == 0
146     " No list.
147     return " "
148   elseif b:iainlist == 1
149     " Just tabs.
150     return "_"
151   else
152     " Full list.
153     return "\$"
154   endif
155 endfun
156
157 " Helper for status line.
158 " Show c or C to denote case-sensitivity.
159 fun! Show_Case()
160   if &ic
161     return "c"
162   else
163     return "C"
164   endif
165 endfun
166
167 " Helper for status line.
168 " Show the size of the tabstop.
169 fun! Show_Tabstop()
170   return &ts
171 endfun
172
173 " Helper for status line.
174 " Show p when paste mode is on.
175 fun! Show_Paste()
176   if &paste
177     return "p"
178   else
179     return ""
180   endif
181 endfun
182
183 " Show the window title.
184 fun! Show_TitleString()
185   if bufname("") == ""
186     let l:ts1='Vim'
187   else
188     let l:ts1=printf("%2d: %s", bufnr(""), expand('%t'))
189   endif
190   return printf("%s (%s) %s", l:ts1, getcwd(), v:servername)
191 endfun
192
193 " Show the status line.
194 fun! Show_StatusLine()
195   call Iain_Vars()
196   let l:sl1='%2n\:\ %<%f\ [%{Show_List()}%{Show_Case()}%{Show_Tabstop()}%{Show_Paste()}%Y%M%R]\ %='
197   let l:sl3='L:%4.6l/%-4.6L\ C:%3.6c\ \|\ %P'
198   let l:hexformat='%b'
199   if b:iainhex
200     let l:hexformat='0\x%02B'
201   endif
202   if b:iainverbose
203     let l:sl2=l:hexformat . '\ \|\ P:%4.6o\ '
204   else
205     let l:sl2=''
206   endif
207   exec "set statusline=" . l:sl1 . l:sl2 . l:sl3
208 endfun
209
210 " Toggle case-sensitivity.
211 fun! Invert_Case()
212   let &ic = ! &ic
213 endfun
214
215 " Grow or shrink the window size.
216 fun! Resize_Columns(op)
217   " Vim 5 hardcodes the size of numbers column to 8.
218   if version >= "700"
219     let l:numberwidth = &numberwidth
220   else
221     let l:numberwidth = 8
222   endif
223
224   let l:resize = "se columns" . a:op . "=" . l:numberwidth
225
226   " HACK: Inside screen there is an extra line for the status bar.  Vim
227   " manages the resize by sending an escape sequence to set the number of
228   " lines and number of columns in one action.  To do this it will first query
229   " the number of lines and then set <same number of lines> by <new number of
230   " columns>.  Because of the extra line for the status bar this results in
231   " the real terminal being shrunk by a line.  We ask for the terminal to grow
232   " by a line so it ends up actually being the same.
233   if &term =~ '^screen'
234     let l:resize .= " lines+=1"
235   endif
236
237   exec l:resize
238 endfun
239
240 " Toggle number display.
241 fun! Number()
242   call Iain_Vars()
243   let &number = ! &number
244
245   if version >= "700"
246     let l:i = 0
247     let l:num_numbers = 0
248     while l:i <= winnr("$")
249       if getwinvar(l:i, "&number") == 1
250         let l:num_numbers = l:num_numbers + 1
251       endif
252       let l:i = l:i + 1
253     endwhile
254
255     if l:num_numbers == 0
256       let g:iainextracolumns = 0
257       call Resize_Columns("-")
258     elseif g:iainextracolumns == 0
259       let g:iainextracolumns = 1
260       call Resize_Columns("+")
261     endif
262   endif
263 endfun
264
265 " Restore window size.
266 au VimLeave * if exists("oldcols") | let &columns=oldcols | endif
267
268 " Map Makefile mode.
269 au BufEnter * if &ft == "make" | call MakeMode_map() | endif
270 au BufLeave * if &ft == "make" | call MakeMode_unmap() | endif
271
272 " Entering Make mode.
273 fun! MakeMode_map()
274         call Iain_Vars()
275   let b:iainlist=1
276   call Cycle_List()
277   set ts=8
278   set noexpandtab
279 endfun
280
281 " Leaving Make mode.
282 fun! MakeMode_unmap()
283   call Cycle_List()
284   set ts=2
285   set expandtab
286 endfun
287
288 " Show the status line for the first time.
289 call Show_StatusLine()
290
291 " Function to create mappings with either a hardcoded \ or <Leader>.
292 fun! Mapping(keysequence,mapping)
293   if version < "600"
294     exec "map \\" . a:keysequence . " " . a:mapping
295   else
296     exec "map <Leader>" . a:keysequence . " " . a:mapping
297   endif
298 endfun
299
300 " Use - and = to create underlines.
301 call Mapping("-", "yyp:s/./-/g<RETURN>:let @/=''<RETURN>:<RETURN>")
302 call Mapping("=", "yyp:s/./=/g<RETURN>:let @/=''<RETURN>:<RETURN>")
303
304 " Change to ts=2 with \2.
305 call Mapping("2", ":se ts=2<CR>:<CR>")
306 " Change to ts=4 with \4.
307 call Mapping("4", ":se ts=4<CR>:<CR>")
308 " Change to ts=8 with \8.
309 call Mapping("8", ":se ts=8<CR>:<CR>")
310 " Change to ts=16 with \6.
311 call Mapping("6", ":se ts=16<CR>:<CR>")
312 " Change to ts=32 with \3.
313 call Mapping("3", ":se ts=32<CR>:<CR>")
314 " Toggle paste mode with \p.
315 call Mapping("p", ":se paste!<CR>:<CR>")
316 " Swap case-sensitivity with \c.
317 call Mapping("c", ":call Invert_Case()<CR>:<CR>")
318 " Change number mode with \n.
319 call Mapping("n", ":call Number()<CR>:<CR>")
320 " Expand or shrink window size with \> and \<.
321 call Mapping(">", ":call Resize_Columns('+')<CR>:<CR>")
322 call Mapping("<", ":call Resize_Columns('-')<CR>:<CR>")
323 " Clear search pattern with \/.
324 call Mapping("/", ":let @/=\"\"<CR>:<CR>")
325
326 " Forget the Ex mode mapping.
327 map Q <NOP>
328
329 " Vim tip 99: What's the highlighting group under the cursor?
330 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>")
331
332 endif
333
334 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
335 " Handle options only available in Vim 6 and above.
336 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
337 if version >= "600"
338 version 6.0
339
340 " Remember quickfix state.
341 let g:quickfixing=0
342
343 " Set indenting by filetype.
344 filetype indent on
345
346 " Less intrusive syntax highlighting.
347 syn enable
348
349 " Nice GUI colour.
350 if has("gui_running")
351   se guifont=DejaVu\ Sans\ Mono\ 10
352   colo darkblue
353   hi LineNr guibg=#303030
354 elseif &t_Co > 16
355   try
356     colo iain
357   catch
358   endtry
359 endif
360 if has("win32")
361   se guifont=DejaVu_Sans_Mono:h10:cANSI
362 endif
363 hi! link TabLineSel StatusLine
364 hi! link TabLine StatusLineNC
365
366 " Ignore whitespace when diffing.
367 se diffopt=filler,iwhite
368
369 " Expand window when doing a vertical diff.
370 if &diff
371   let &columns = 164
372 endif
373
374 " Remember that we are opening the quickfix window.
375 au BufWinEnter quickfix let g:quickfixing=1
376 au BufUnload * if &ft == "qf" | let g:quickfixing=0 | endif
377
378 " Allow in-place editing of crontabs.
379 au FileType crontab set backupcopy=yes
380
381 " Make * and # work the way you expect in visual mode.
382 vnoremap * y/\V<C-R>=substitute(escape(@@,"/\\"),"\n","\\\\n","ge")<CR><CR>
383 vnoremap # y?\V<C-R>=substitute(escape(@@,"?\\"),"\n","\\\\n","ge")<CR><CR>
384
385 " Change list mode.
386 fun! Cycle_List()
387   let basic='tab:\\_,trail:_,extends:<,precedes:>'
388   call Iain_Vars()
389   let b:iainlist = b:iainlist + 1
390   if b:iainlist > 2
391     let b:iainlist = 0
392   endif
393   if b:iainlist == 0
394     set nolist
395   elseif b:iainlist == 1
396     exec "set lcs=" . basic
397     set list
398   else
399     exec "set lcs=" . basic . ",eol:$"
400     set list
401   endif
402 endfun
403
404 " Cycle between hex and decimal display of toolbar stuff.
405 fun! Cycle_HexStatusLine()
406   call Iain_Vars()
407   let b:iainhex = ! b:iainhex
408   call Show_StatusLine()
409 endfun
410
411 " Cycle verbose display of toolbar stuff.
412 fun! Cycle_VerboseStatusLine()
413   call Iain_Vars()
414   let b:iainverbose = ! b:iainverbose
415   call Show_StatusLine()
416 endfun
417
418 " Toggle quickfix window.
419 fun! Cycle_Quickfix()
420   if g:quickfixing == 1
421     cclose
422     let g:quickfixing=0
423   else
424     copen
425   endif
426 endfun
427
428 " Swap hex/decimal statusline with \x.
429 call Mapping("x", ":call Cycle_HexStatusLine()<CR>:<CR>")
430 " Change statusline verbosity with \v.
431 call Mapping("v", ":call Cycle_VerboseStatusLine()<CR>:<CR>")
432 " Cycle list styles with \l.
433 call Mapping("l", ":call Cycle_List()<CR>:<CR>")
434 " Toggle tags with \t.
435 call Mapping("t", ":Tlist<CR>")
436 " Change foldmethod with \f.
437 call Mapping("f", ":se foldenable!<CR>:<CR>")
438 " Toggle quickfix window with \q.
439 call Mapping("q", ":call Cycle_Quickfix()<CR>:<CR>")
440 " Rerun filetype detection with \s.  The s is for syntax, as this will be
441 " updated as a side-effect.
442 call Mapping("s", ":filetype detect<CR>:<CR>")
443
444 fun! <SID>Iain_Colour(colour)
445   if &t_Co == 88
446     if a:colour == "darkblue"
447       return 17
448     elseif a:colour == "darkmagenta"
449       return 33
450     elseif a:colour == "darkred"
451       return 32
452     elseif a:colour == "red"
453       return 64
454     endif
455   elseif &t_Co == 256
456     if a:colour == "darkblue"
457       return 17
458     elseif a:colour == "darkmagenta"
459       return 90
460     elseif a:colour == "darkred"
461       return 88
462     elseif a:colour == "red"
463       return 196
464     endif
465   else
466     return a:colour
467   endif
468 endfun
469
470 " Change status bar colour when various things happen.
471 " Flags: H/h: Cursor held/moved.
472 "        F/f: Focus gained/lost.
473 "        I/i: Insert mode entered/left.
474 fun! Highlight_StatusLine(flag)
475   " Get current status.
476   call Iain_Vars()
477
478   " Change the status based on the flag.  XXX: Does Vim let us to do flags?
479   let re = "[" . tolower(a:flag) . toupper(a:flag) . "]"
480   let b:iainstatus = substitute(b:iainstatus, re, a:flag, "")
481
482   let l:normalcolour = "darkblue"
483   let l:editingcolour = "darkmagenta"
484   let l:warningcolour = "darkred"
485   let l:readonlycolour = "red"
486
487   " Default colour.
488   let l:colour = l:normalcolour
489   " Maybe override depending on status.
490   if b:iainstatus =~# "H"
491     if b:iainstatus =~# "I"
492       " Held in insert mode.  Add extra highlight if we don't have focus.
493       if b:iainstatus =~# "f"
494         let l:colour = l:warningcolour
495       else
496         let l:colour = l:editingcolour
497       endif
498     endif
499   else
500     if b:iainstatus =~# "I"
501       " Regular insert mode.
502       let l:colour = l:editingcolour
503     endif
504   endif
505
506   " Override again if readonly.
507   if l:colour != l:normalcolour
508     if getbufvar("", "&ro")
509       let l:colour = l:readonlycolour
510     endif
511   endif
512
513   let l:termcolour = <SID>Iain_Colour(l:colour)
514
515   exec "highlight StatusLine guifg=white guibg=" . l:colour . " ctermbg=white ctermfg=" . l:termcolour
516 endfun
517
518 call Highlight_StatusLine("")
519 endif
520
521 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
522 " Handle options only available in Vim 7 and above.
523 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
524 if version >= "700"
525 version 7.0
526
527 " Helper to show tab name.
528 fun! <SID>TabName(label, gui)
529   let l:label = a:label
530   if l:label == ""
531     let l:label = "No Name"
532     if a:gui
533       let l:label = "[" . l:label . "]"
534     endif
535   else
536     let l:label = fnamemodify(l:label, ":t")
537     if strlen(l:label) >= 18
538       let l:label = l:label[0:17] . ".."
539     endif
540   endif
541   return l:label
542 endfun
543
544 " Find out if any buffer was modified.
545 fun! <SID>TabModified(buflist)
546   let l:i = 0
547   while i < len(a:buflist)
548     if getbufvar(a:buflist[l:i], "&modified") == 1
549       return "+"
550     endif
551     let l:i = l:i + 1
552   endwhile
553   return ""
554 endfun
555
556 " Tab line.
557 fun! Show_TabLine()
558   let l:s = "%#TabLineFill#Tabs:"
559
560   let l:i = 0
561   while l:i < tabpagenr("$")
562     let l:i = l:i + 1
563     " Get the label.
564     let l:buflist = tabpagebuflist(l:i)
565     let l:winnr = tabpagewinnr(l:i)
566     let l:n = tabpagewinnr(l:i, "$")
567     let l:label = <SID>TabName(bufname(l:buflist[l:winnr - 1]), 0)
568     let l:modified = <SID>TabModified(l:buflist)
569
570     " Choose highlighting.
571     if l:i == tabpagenr()
572       let l:s .= "%#TabLineSel#[" . l:n . l:modified . " " . l:label . "]"
573     else
574       let l:s .= "%#TabLine# " . l:n . l:modified . " " . l:label . " "
575     endif
576   endwhile
577
578   " Padding.
579   let l:s .= "%#TabLine#%T"
580   return l:s
581 endfun
582
583 " Per tab label for the GUI.
584 fun! Show_GUITabLine()
585   let l:buflist = tabpagebuflist(v:lnum)
586   let l:winnr = tabpagewinnr(v:lnum)
587   let l:s = tabpagewinnr(v:lnum, "$")
588   let l:label = <SID>TabName(bufname(l:buflist[l:winnr - 1]), 1)
589   let l:modified = <SID>TabModified(l:buflist)
590
591   let l:s .= l:modified . " " . l:label
592   return l:s
593 endfun
594
595 se tabline=%!Show_TabLine()
596 se guitablabel=%!Show_GUITabLine()
597
598 au CursorHoldI * call Highlight_StatusLine("H")
599 au CursorMovedI * call Highlight_StatusLine("h")
600 au FocusGained * call Highlight_StatusLine("F")
601 au FocusLost * call Highlight_StatusLine("f")
602 au InsertEnter * call Highlight_StatusLine("I")
603 au InsertLeave * call Highlight_StatusLine("i")
604
605 " Limit the size of the popup menu when completing.
606 se pumheight=20
607
608 " Make diffs vertical by default.
609 se diffopt+=vertical
610
611 " Set size of numbers column.
612 se numberwidth=5
613
614 " Add "previous tab" mapping as gb.
615 map gb :tabprevious<CR>:<CR>
616
617 " Transparency.
618 if has("gui_macvim")
619   se transparency=15
620 endif 
621
622 " Yet more GUI options.  Add tabs.
623 se go+=e
624
625 " Perforce.
626 let g:p4EnableMenu=1
627 let g:p4Presets='P4CONFIG'
628 endif