Separate aliases and variables.
[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 " Vim 5 hardcodes the size of numbers column to 8.
81 let numberwidth=8
82
83 " Save sessions in UNIX format with / as file separator.  This is
84 " cross-platform.
85 se ssop+=unix,slash
86
87 " Nuke any pre-existing autocommands.
88 autocmd!
89
90 " Save the current window width so we can restore it when we quit.
91 let oldcols=&columns
92
93 " More GUI options.  Add icon, tearoffs and toolbar.
94 se go+=itT
95
96 " Allow dynamic window resize even if we aren't in an xterm.
97 se t_WS=\e[8;%p1%d;%p2%dt
98
99 " Highlight search results.
100 se hlsearch
101
102 " Set graphical window title.
103 se titlestring=%{Show_TitleString()}
104
105 " Syntax highlighting.  New versions will use syn enable instead.
106 if version < 600
107   syn on
108 endif
109
110 " Use a discernably different colour to highlight the cursor which shows 
111 " matching brackets.  Our regular cursor is green so use blue instead of cyan.
112 hi MatchParen ctermbg=blue
113
114 " Catch typos.
115 command! W :w
116 command! Wq :wq
117 command! Wqa :wqa
118
119 " Set up our variables.
120 fun! Iain_Vars()
121   if ! exists("b:iainlist")
122     let b:iainlist = 0
123   endif
124   if ! exists("b:iainhex")
125     let b:iainhex = 0
126   endif
127   if ! exists("b:iainverbose")
128     let b:iainverbose = 0
129   endif
130   if ! exists("b:iainstatus")
131     " Window Flags: (F)ocused, (I)nsert mode, Cursor (H)old.
132     let b:iainstatus = "Fih"
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 ts1='Vim'
182   else
183     let ts1=printf("%2d: %s", bufnr(""), expand('%t'))
184   endif
185   return printf("%s (%s) %s", ts1, getcwd(), v:servername)
186 endfun
187
188 " Show the status line.
189 fun! Show_StatusLine()
190   call Iain_Vars()
191   let sl1='%2n\:\ %<%f\ [%{Show_List()}%{Show_Case()}%{Show_Tabstop()}%{Show_Paste()}%Y%M%R]\ %='
192   let sl3='L:%4.6l/%-4.6L\ C:%3.6c\ \|\ %P'
193   let hexformat='%b'
194   if b:iainhex
195     let hexformat='0\x%02B'
196   endif
197   if b:iainverbose
198     let sl2=hexformat . '\ \|\ P:%4.6o\ '
199   else
200     let sl2=''
201   endif
202   exec "set statusline=" . sl1 . sl2 . sl3
203 endfun
204
205 " Toggle case-sensitivity.
206 fun! Invert_Case()
207   let &ic = ! &ic
208 endfun
209
210 " Restore window size.
211 au VimLeave * if exists("oldcols") | let &columns=oldcols | endif
212
213 " Map Makefile mode.
214 au BufEnter * if &ft == "make" | call MakeMode_map() | endif
215 au BufLeave * if &ft == "make" | call MakeMode_unmap() | endif
216
217 " Entering Make mode.
218 fun! MakeMode_map()
219         call Iain_Vars()
220   let b:iainlist=1
221   call Cycle_List()
222   set ts=8
223   set noexpandtab
224 endfun
225
226 " Leaving Make mode.
227 fun! MakeMode_unmap()
228   call Cycle_List()
229   set ts=2
230   set expandtab
231 endfun
232
233 " Show the status line for the first time.
234 call Show_StatusLine()
235
236 " Function to create mappings with either a hardcoded \ or <Leader>.
237 fun! Mapping(keysequence,mapping)
238   if version < "600"
239     exec "map \\" . a:keysequence . " " . a:mapping
240   else
241     exec "map <Leader>" . a:keysequence . " " . a:mapping
242   endif
243 endfun
244
245 " Use - and = to create underlines.
246 call Mapping("-", "yyp:s/./-/g<RETURN>:let @/=''<RETURN>:<RETURN>")
247 call Mapping("=", "yyp:s/./=/g<RETURN>:let @/=''<RETURN>:<RETURN>")
248
249 " Change to ts=2 with \2.
250 call Mapping("2", ":se ts=2<CR>:<CR>")
251 " Change to ts=4 with \4.
252 call Mapping("4", ":se ts=4<CR>:<CR>")
253 " Change to ts=8 with \8.
254 call Mapping("8", ":se ts=8<CR>:<CR>")
255 " Change to ts=16 with \6.
256 call Mapping("6", ":se ts=16<CR>:<CR>")
257 " Change to ts=32 with \3.
258 call Mapping("3", ":se ts=32<CR>:<CR>")
259 " Toggle paste mode with \p.
260 call Mapping("p", ":se paste!<CR>:<CR>")
261 " Swap case-sensitivity with \c.
262 call Mapping("c", ":call Invert_Case()<CR>:<CR>")
263 " Change number mode with \n.
264 call Mapping("n", ":se number!<CR>:<CR>")
265 " Expand or shrink window size with \> and \<.  For use after toggling number.
266 call Mapping(">", ":exe 'se columns+=' . numberwidth<CR>:<CR>")
267 call Mapping("<", ":exe 'se columns-=' . numberwidth<CR>:<CR>")
268 " Clear search pattern with \/.
269 call Mapping("/", ":let @/=\"\"<CR>:<CR>")
270
271 " Forget the Ex mode mapping.
272 map Q <NOP>
273
274 " Vim tip 99: What's the highlighting group under the cursor?
275 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>")
276
277 endif
278
279 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
280 " Handle options only available in Vim 6 and above.
281 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
282 if version >= "600"
283 version 6.0
284
285 " Remember quickfix state.
286 let g:quickfixing=0
287
288 " Set indenting by filetype.
289 filetype indent on
290
291 " Less intrusive syntax highlighting.
292 syn enable
293
294 " Nice GUI colour.
295 if has("gui_running")
296   se guifont=DejaVu\ Sans\ Mono\ 10
297   colo darkblue
298 elseif &t_Co > 16
299   try
300     colo iain
301   catch
302   endtry
303 endif
304 if has("win32")
305   se guifont=DejaVu_Sans_Mono:h10:cANSI
306 endif
307
308 " Ignore whitespace when diffing.
309 se diffopt=filler,iwhite
310
311 " Expand window when doing a vertical diff.
312 if &diff
313   let &columns = 164
314 endif
315
316 " Remember that we are opening the quickfix window.
317 au BufWinEnter quickfix let g:quickfixing=1
318 au BufUnload * if &ft == "qf" | let g:quickfixing=0 | endif
319
320 " Allow in-place editing of crontabs.
321 au FileType crontab set backupcopy=yes
322
323 " Make * and # work the way you expect in visual mode.
324 vnoremap * y/\V<C-R>=substitute(escape(@@,"/\\"),"\n","\\\\n","ge")<CR><CR>
325 vnoremap # y?\V<C-R>=substitute(escape(@@,"?\\"),"\n","\\\\n","ge")<CR><CR>
326
327 " Change list mode.
328 fun! Cycle_List()
329   let basic='tab:\\_,trail:_,extends:<,precedes:>'
330   call Iain_Vars()
331   let b:iainlist = b:iainlist + 1
332   if b:iainlist > 2
333     let b:iainlist = 0
334   endif
335   if b:iainlist == 0
336     set nolist
337   elseif b:iainlist == 1
338     exec "set lcs=" . basic
339     set list
340   else
341     exec "set lcs=" . basic . ",eol:$"
342     set list
343   endif
344 endfun
345
346 " Cycle between hex and decimal display of toolbar stuff.
347 fun! Cycle_HexStatusLine()
348   call Iain_Vars()
349   let b:iainhex = ! b:iainhex
350   call Show_StatusLine()
351 endfun
352
353 " Cycle verbose display of toolbar stuff.
354 fun! Cycle_VerboseStatusLine()
355   call Iain_Vars()
356   let b:iainverbose = ! b:iainverbose
357   call Show_StatusLine()
358 endfun
359
360 " Toggle quickfix window.
361 fun! Cycle_Quickfix()
362   if g:quickfixing == 1
363     cclose
364     let g:quickfixing=0
365   else
366     copen
367   endif
368 endfun
369
370 " Swap hex/decimal statusline with \x.
371 call Mapping("x", ":call Cycle_HexStatusLine()<CR>:<CR>")
372 " Change statusline verbosity with \v.
373 call Mapping("v", ":call Cycle_VerboseStatusLine()<CR>:<CR>")
374 " Cycle list styles with \l.
375 call Mapping("l", ":call Cycle_List()<CR>:<CR>")
376 " Toggle tags with \t.
377 call Mapping("t", ":Tlist<CR>")
378 " Change foldmethod with \f.
379 call Mapping("f", ":se foldenable!<CR>:<CR>")
380 " Toggle quickfix window with \q.
381 call Mapping("q", ":call Cycle_Quickfix()<CR>:<CR>")
382 " Rerun filetype detection with \s.  The s is for syntax, as this will be
383 " updated as a side-effect.
384 call Mapping("s", ":filetype detect<CR>:<CR>")
385
386 " Change status bar colour when various things happen.
387 fun! Highlight_StatusLine(flag)
388   " Get current status.
389   call Iain_Vars()
390
391   " Change the status based on the flag.  XXX: Does Vim let us to do flags?
392   let re = "[" . tolower(a:flag) . toupper(a:flag) . "]"
393   let b:iainstatus = substitute(b:iainstatus, re, a:flag, "")
394
395   " Default colour.
396   let s:colour = "darkblue"
397   let s:termcolour = ""
398   let s:term88colour = "17"
399   let s:term256colour = "17"
400   " Maybe override depending on status.
401   if b:iainstatus =~# "H"
402     if b:iainstatus =~# "I"
403       " Held in insert mode.  Add extra highlight if we don't have focus.
404       if b:iainstatus =~# "f"
405         let s:colour = "darkred"
406         let s:term88colour = "32"
407         let s:term256colour = "88"
408       else
409         let s:colour = "darkmagenta"
410         let s:term88colour = "33"
411         let s:term256colour = "90"
412       endif
413     endif
414   else
415     if b:iainstatus =~# "I"
416       " Regular insert mode.
417       let s:colour = "darkmagenta"
418       let s:term88colour = "33"
419       let s:term256colour = "90"
420     endif
421   endif
422
423   if &t_Co == 88
424     let s:termcolour = s:term88colour
425   elseif &t_Co == 256
426     let s:termcolour = s:term256colour
427   else
428     let s:termcolour = s:colour
429   endif
430
431   exec "highlight StatusLine guifg=white guibg=" . s:colour . " ctermbg=white ctermfg=" . s:termcolour
432 endfun
433
434 call Highlight_StatusLine("")
435 endif
436
437 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
438 " Handle options only available in Vim 7 and above.
439 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
440 if version >= "700"
441 version 7.0
442
443 au CursorHoldI * call Highlight_StatusLine("H")
444 au CursorMovedI * call Highlight_StatusLine("h")
445 au FocusGained * call Highlight_StatusLine("F")
446 au FocusLost * call Highlight_StatusLine("f")
447 au InsertEnter * call Highlight_StatusLine("I")
448 au InsertLeave * call Highlight_StatusLine("i")
449
450 " Make diffs vertical by default.
451 se diffopt+=vertical
452
453 " Set size of numbers column.
454 se numberwidth=5
455
456 " Add "previous tab" mapping as gb.
457 map gb :tabPrev<CR>
458
459 " Transparency.
460 if has("gui_macvim")
461   se transparency=15
462 endif 
463
464 " Perforce.
465 let g:p4EnableMenu=1
466 let g:p4Presets='P4CONFIG'
467 endif