pumheight was introduced in Vim 7.
[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   " XXX: This won't work inside screen.
218   " We should really detect whether it would work rather than assume it won't.
219   if &term =~ '^screen'
220     return
221   endif
222
223   " Vim 5 hardcodes the size of numbers column to 8.
224   if version >= "700"
225     let l:numberwidth = &numberwidth
226   else
227     let l:numberwidth = 8
228   endif
229
230   exec "se columns" . a:op . "=" . l:numberwidth
231 endfun
232
233 " Toggle number display.
234 fun! Number()
235   call Iain_Vars()
236   let &number = ! &number
237
238   if version >= "700"
239     let l:i = 0
240     let l:num_numbers = 0
241     while l:i <= winnr("$")
242       if getwinvar(l:i, "&number") == 1
243         let l:num_numbers = l:num_numbers + 1
244       endif
245       let l:i = l:i + 1
246     endwhile
247
248     if l:num_numbers == 0
249       let g:iainextracolumns = 0
250       call Resize_Columns("-")
251     elseif g:iainextracolumns == 0
252       let g:iainextracolumns = 1
253       call Resize_Columns("+")
254     endif
255   endif
256 endfun
257
258 " Restore window size.
259 au VimLeave * if exists("oldcols") | let &columns=oldcols | endif
260
261 " Map Makefile mode.
262 au BufEnter * if &ft == "make" | call MakeMode_map() | endif
263 au BufLeave * if &ft == "make" | call MakeMode_unmap() | endif
264
265 " Entering Make mode.
266 fun! MakeMode_map()
267         call Iain_Vars()
268   let b:iainlist=1
269   call Cycle_List()
270   set ts=8
271   set noexpandtab
272 endfun
273
274 " Leaving Make mode.
275 fun! MakeMode_unmap()
276   call Cycle_List()
277   set ts=2
278   set expandtab
279 endfun
280
281 " Show the status line for the first time.
282 call Show_StatusLine()
283
284 " Function to create mappings with either a hardcoded \ or <Leader>.
285 fun! Mapping(keysequence,mapping)
286   if version < "600"
287     exec "map \\" . a:keysequence . " " . a:mapping
288   else
289     exec "map <Leader>" . a:keysequence . " " . a:mapping
290   endif
291 endfun
292
293 " Use - and = to create underlines.
294 call Mapping("-", "yyp:s/./-/g<RETURN>:let @/=''<RETURN>:<RETURN>")
295 call Mapping("=", "yyp:s/./=/g<RETURN>:let @/=''<RETURN>:<RETURN>")
296
297 " Change to ts=2 with \2.
298 call Mapping("2", ":se ts=2<CR>:<CR>")
299 " Change to ts=4 with \4.
300 call Mapping("4", ":se ts=4<CR>:<CR>")
301 " Change to ts=8 with \8.
302 call Mapping("8", ":se ts=8<CR>:<CR>")
303 " Change to ts=16 with \6.
304 call Mapping("6", ":se ts=16<CR>:<CR>")
305 " Change to ts=32 with \3.
306 call Mapping("3", ":se ts=32<CR>:<CR>")
307 " Toggle paste mode with \p.
308 call Mapping("p", ":se paste!<CR>:<CR>")
309 " Swap case-sensitivity with \c.
310 call Mapping("c", ":call Invert_Case()<CR>:<CR>")
311 " Change number mode with \n.
312 call Mapping("n", ":call Number()<CR>:<CR>")
313 " Expand or shrink window size with \> and \<.
314 call Mapping(">", ":call Resize_Columns('+')<CR>:<CR>")
315 call Mapping("<", ":call Resize_Columns('-')<CR>:<CR>")
316 " Clear search pattern with \/.
317 call Mapping("/", ":let @/=\"\"<CR>:<CR>")
318
319 " Forget the Ex mode mapping.
320 map Q <NOP>
321
322 " Vim tip 99: What's the highlighting group under the cursor?
323 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>")
324
325 endif
326
327 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
328 " Handle options only available in Vim 6 and above.
329 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
330 if version >= "600"
331 version 6.0
332
333 " Remember quickfix state.
334 let g:quickfixing=0
335
336 " Set indenting by filetype.
337 filetype indent on
338
339 " Less intrusive syntax highlighting.
340 syn enable
341
342 " Nice GUI colour.
343 if has("gui_running")
344   se guifont=DejaVu\ Sans\ Mono\ 10
345   colo darkblue
346   hi LineNr guibg=#303030
347 elseif &t_Co > 16
348   try
349     colo iain
350   catch
351   endtry
352 endif
353 if has("win32")
354   se guifont=DejaVu_Sans_Mono:h10:cANSI
355 endif
356 hi! link TabLineSel StatusLine
357 hi! link TabLine StatusLineNC
358
359 " Ignore whitespace when diffing.
360 se diffopt=filler,iwhite
361
362 " Expand window when doing a vertical diff.
363 if &diff
364   let &columns = 164
365 endif
366
367 " Remember that we are opening the quickfix window.
368 au BufWinEnter quickfix let g:quickfixing=1
369 au BufUnload * if &ft == "qf" | let g:quickfixing=0 | endif
370
371 " Allow in-place editing of crontabs.
372 au FileType crontab set backupcopy=yes
373
374 " Make * and # work the way you expect in visual mode.
375 vnoremap * y/\V<C-R>=substitute(escape(@@,"/\\"),"\n","\\\\n","ge")<CR><CR>
376 vnoremap # y?\V<C-R>=substitute(escape(@@,"?\\"),"\n","\\\\n","ge")<CR><CR>
377
378 " Change list mode.
379 fun! Cycle_List()
380   let basic='tab:\\_,trail:_,extends:<,precedes:>'
381   call Iain_Vars()
382   let b:iainlist = b:iainlist + 1
383   if b:iainlist > 2
384     let b:iainlist = 0
385   endif
386   if b:iainlist == 0
387     set nolist
388   elseif b:iainlist == 1
389     exec "set lcs=" . basic
390     set list
391   else
392     exec "set lcs=" . basic . ",eol:$"
393     set list
394   endif
395 endfun
396
397 " Cycle between hex and decimal display of toolbar stuff.
398 fun! Cycle_HexStatusLine()
399   call Iain_Vars()
400   let b:iainhex = ! b:iainhex
401   call Show_StatusLine()
402 endfun
403
404 " Cycle verbose display of toolbar stuff.
405 fun! Cycle_VerboseStatusLine()
406   call Iain_Vars()
407   let b:iainverbose = ! b:iainverbose
408   call Show_StatusLine()
409 endfun
410
411 " Toggle quickfix window.
412 fun! Cycle_Quickfix()
413   if g:quickfixing == 1
414     cclose
415     let g:quickfixing=0
416   else
417     copen
418   endif
419 endfun
420
421 " Swap hex/decimal statusline with \x.
422 call Mapping("x", ":call Cycle_HexStatusLine()<CR>:<CR>")
423 " Change statusline verbosity with \v.
424 call Mapping("v", ":call Cycle_VerboseStatusLine()<CR>:<CR>")
425 " Cycle list styles with \l.
426 call Mapping("l", ":call Cycle_List()<CR>:<CR>")
427 " Toggle tags with \t.
428 call Mapping("t", ":Tlist<CR>")
429 " Change foldmethod with \f.
430 call Mapping("f", ":se foldenable!<CR>:<CR>")
431 " Toggle quickfix window with \q.
432 call Mapping("q", ":call Cycle_Quickfix()<CR>:<CR>")
433 " Rerun filetype detection with \s.  The s is for syntax, as this will be
434 " updated as a side-effect.
435 call Mapping("s", ":filetype detect<CR>:<CR>")
436
437 " Change status bar colour when various things happen.
438 fun! Highlight_StatusLine(flag)
439   " Get current status.
440   call Iain_Vars()
441
442   " Change the status based on the flag.  XXX: Does Vim let us to do flags?
443   let re = "[" . tolower(a:flag) . toupper(a:flag) . "]"
444   let b:iainstatus = substitute(b:iainstatus, re, a:flag, "")
445
446   " Default colour.
447   let s:colour = "darkblue"
448   let s:termcolour = ""
449   let s:term88colour = "17"
450   let s:term256colour = "17"
451   " Maybe override depending on status.
452   if b:iainstatus =~# "H"
453     if b:iainstatus =~# "I"
454       " Held in insert mode.  Add extra highlight if we don't have focus.
455       if b:iainstatus =~# "f"
456         let s:colour = "darkred"
457         let s:term88colour = "32"
458         let s:term256colour = "88"
459       else
460         let s:colour = "darkmagenta"
461         let s:term88colour = "33"
462         let s:term256colour = "90"
463       endif
464     endif
465   else
466     if b:iainstatus =~# "I"
467       " Regular insert mode.
468       let s:colour = "darkmagenta"
469       let s:term88colour = "33"
470       let s:term256colour = "90"
471     endif
472   endif
473
474   if &t_Co == 88
475     let s:termcolour = s:term88colour
476   elseif &t_Co == 256
477     let s:termcolour = s:term256colour
478   else
479     let s:termcolour = s:colour
480   endif
481
482   exec "highlight StatusLine guifg=white guibg=" . s:colour . " ctermbg=white ctermfg=" . s:termcolour
483 endfun
484
485 call Highlight_StatusLine("")
486 endif
487
488 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
489 " Handle options only available in Vim 7 and above.
490 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
491 if version >= "700"
492 version 7.0
493
494 " Helper to show tab name.
495 fun! <SID>TabName(label, gui)
496   let l:label = a:label
497   if l:label == ""
498     let l:label = "No Name"
499     if a:gui
500       let l:label = "[" . l:label . "]"
501     endif
502   else
503     let l:label = fnamemodify(l:label, ":t")
504     if strlen(l:label) >= 18
505       let l:label = l:label[0:17] . ".."
506     endif
507   endif
508   return l:label
509 endfun
510
511 " Find out if any buffer was modified.
512 fun! <SID>TabModified(buflist)
513   let l:i = 0
514   while i < len(a:buflist)
515     if getbufvar(a:buflist[l:i], "&modified") == 1
516       return "+"
517     endif
518     let l:i = l:i + 1
519   endwhile
520   return ""
521 endfun
522
523 " Tab line.
524 fun! Show_TabLine()
525   let l:s = "%#TabLineFill#Tabs:"
526
527   let l:i = 0
528   while l:i < tabpagenr("$")
529     let l:i = l:i + 1
530     " Get the label.
531     let l:buflist = tabpagebuflist(l:i)
532     let l:winnr = tabpagewinnr(l:i)
533     let l:n = tabpagewinnr(l:i, "$")
534     let l:label = <SID>TabName(bufname(l:buflist[l:winnr - 1]), 0)
535     let l:modified = <SID>TabModified(l:buflist)
536
537     " Choose highlighting.
538     if l:i == tabpagenr()
539       let l:s .= "%#TabLineSel#[" . l:n . l:modified . " " . l:label . "]"
540     else
541       let l:s .= "%#TabLine# " . l:n . l:modified . " " . l:label . " "
542     endif
543   endwhile
544
545   " Padding.
546   let l:s .= "%#TabLine#%T"
547   return l:s
548 endfun
549
550 " Per tab label for the GUI.
551 fun! Show_GUITabLine()
552   let l:buflist = tabpagebuflist(v:lnum)
553   let l:winnr = tabpagewinnr(v:lnum)
554   let l:s = tabpagewinnr(v:lnum, "$")
555   let l:label = <SID>TabName(bufname(l:buflist[l:winnr - 1]), 1)
556   let l:modified = <SID>TabModified(l:buflist)
557
558   let l:s .= l:modified . " " . l:label
559   return l:s
560 endfun
561
562 se tabline=%!Show_TabLine()
563 se guitablabel=%!Show_GUITabLine()
564
565 au CursorHoldI * call Highlight_StatusLine("H")
566 au CursorMovedI * call Highlight_StatusLine("h")
567 au FocusGained * call Highlight_StatusLine("F")
568 au FocusLost * call Highlight_StatusLine("f")
569 au InsertEnter * call Highlight_StatusLine("I")
570 au InsertLeave * call Highlight_StatusLine("i")
571
572 " Limit the size of the popup menu when completing.
573 se pumheight=20
574
575 " Make diffs vertical by default.
576 se diffopt+=vertical
577
578 " Set size of numbers column.
579 se numberwidth=5
580
581 " Add "previous tab" mapping as gb.
582 map gb :tabprevious<CR>:<CR>
583
584 " Transparency.
585 if has("gui_macvim")
586   se transparency=15
587 endif 
588
589 " Yet more GUI options.  Add tabs.
590 se go+=e
591
592 " Perforce.
593 let g:p4EnableMenu=1
594 let g:p4Presets='P4CONFIG'
595 endif