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