Use a different escape sequence for magenta.
[profile.git] / .vimrc
1 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
2 " $Id$
3 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
4 " Multi-version vimrc compatible with version 4 and above.
5 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
6
7 " Note that "if <condition> | call Something() | endif" syntax is unsupported 
8 " in Vim 4 so we write all our functions out the long way.  It does work in 
9 " autocommand definitions, however.
10
11 " Vim 4 complains if version isn't set in the configuration file.
12 version 4.0
13
14 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
15 " Handle options safe to use in version 4.  Vim 4 parses but ignores the 
16 " "if version" syntax used later in this file so we don't use it.  No attempt 
17 " is made to make this configuration compatible with Vim 3.
18 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
19 " No compatibility mode.
20 se nocp
21
22 " Tabstop 2.
23 se ts=2
24 " And use spaces not tabs.
25 se expandtab
26 " And << and >> indent by 2.
27 se sw=2
28
29 " Allow backspace to delete before start of line.
30 se bs=2
31
32 " Show the ruler.
33 se ruler
34 " Show partial commands in the ruler.
35 se showcmd
36 " And always show the status line.
37 se laststatus=2
38
39 " Use C indent style.
40 se cindent
41 se cinkeys=0{,0},0),:,!^F,o,O,e
42 se cinoptions=b1,c2
43
44 " GUI options.
45 se go=aglmr
46
47 " Don't be bugged by messages at the bottom of the screen.
48 se shm=aot
49
50 " Find as you type.
51 se incsearch
52
53 " Case-insensitive search.
54 se ignorecase
55 " But override by typing capitals.
56 se smartcase
57
58 " Look for ctags in home directory first.
59 se tags=~/.tags,./tags,tags
60
61 " Don't timeout waiting to interpet, eg, <ESC>OA as an escape code.
62 se ttimeoutlen=100
63
64 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
65 " Handle options only available in Vim 5 and above.
66 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
67 if version >= "500"
68 version 5.0
69
70 " Tell Vim we use dark backgrounds in our terminals.
71 if ! has("gui_running")
72   se bg=dark
73 endif
74
75 " Enable tab-completion prompting for commands.
76 se wildmenu
77 " Don't list object files when globbing files to load.
78 se wildignore+=*.o,*.obj
79 " So there's no need to assign them low priority.
80 se suffixes-=*.o,*.obj
81
82 " Vim 5 hardcodes the size of numbers column to 8.
83 let numberwidth=8
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 endfun
137
138 " Helper for status line.
139 " Show space, underscore or dollar sign depending on list mode.
140 fun! Show_List()
141   call Iain_Vars()
142   if b:iainlist == 0
143     " No list.
144     return " "
145   elseif b:iainlist == 1
146     " Just tabs.
147     return "_"
148   else
149     " Full list.
150     return "\$"
151   endif
152 endfun
153
154 " Helper for status line.
155 " Show c or C to denote case-sensitivity.
156 fun! Show_Case()
157   if &ic
158     return "c"
159   else
160     return "C"
161   endif
162 endfun
163
164 " Helper for status line.
165 " Show the size of the tabstop.
166 fun! Show_Tabstop()
167   return &ts
168 endfun
169
170 " Helper for status line.
171 " Show p when paste mode is on.
172 fun! Show_Paste()
173   if &paste
174     return "p"
175   else
176     return ""
177   endif
178 endfun
179
180 " Show the window title.
181 fun! Show_TitleString()
182   if bufname("") == ""
183     let ts1='Vim'
184   else
185     let ts1=printf("%2d: %s", bufnr(""), expand('%t'))
186   endif
187   return printf("%s (%s) %s", ts1, getcwd(), v:servername)
188 endfun
189
190 " Show the status line.
191 fun! Show_StatusLine()
192   call Iain_Vars()
193   let sl1='%2n\:\ %<%f\ [%{Show_List()}%{Show_Case()}%{Show_Tabstop()}%{Show_Paste()}%Y%M%R]\ %='
194   let sl3='L:%4.6l/%-4.6L\ C:%3.6c\ \|\ %P'
195   let hexformat='%b'
196   if b:iainhex
197     let hexformat='0\x%02B'
198   endif
199   if b:iainverbose
200     let sl2=hexformat . '\ \|\ P:%4.6o\ '
201   else
202     let sl2=''
203   endif
204   exec "set statusline=" . sl1 . sl2 . sl3
205 endfun
206
207 " Toggle case-sensitivity.
208 fun! Invert_Case()
209   let &ic = ! &ic
210 endfun
211
212 " Restore window size.
213 au VimLeave * if exists("oldcols") | let &columns=oldcols | endif
214
215 " Map Makefile mode.
216 au BufEnter * if &ft == "make" | call MakeMode_map() | endif
217 au BufLeave * if &ft == "make" | call MakeMode_unmap() | endif
218
219 " Entering Make mode.
220 fun! MakeMode_map()
221         call Iain_Vars()
222   let b:iainlist=1
223   call Cycle_List()
224   set ts=8
225   set noexpandtab
226 endfun
227
228 " Leaving Make mode.
229 fun! MakeMode_unmap()
230   call Cycle_List()
231   set ts=2
232   set expandtab
233 endfun
234
235 " Show the status line for the first time.
236 call Show_StatusLine()
237
238 " Function to create mappings with either a hardcoded \ or <Leader>.
239 fun! Mapping(keysequence,mapping)
240   if version < "600"
241     exec "map \\" . a:keysequence . " " . a:mapping
242   else
243     exec "map <Leader>" . a:keysequence . " " . a:mapping
244   endif
245 endfun
246
247 " Use - and = to create underlines.
248 call Mapping("-", "yyp:s/./-/g<RETURN>:let @/=''<RETURN>:<RETURN>")
249 call Mapping("=", "yyp:s/./=/g<RETURN>:let @/=''<RETURN>:<RETURN>")
250
251 " Change to ts=2 with \2.
252 call Mapping("2", ":se ts=2<CR>:<CR>")
253 " Change to ts=4 with \4.
254 call Mapping("4", ":se ts=4<CR>:<CR>")
255 " Change to ts=8 with \8.
256 call Mapping("8", ":se ts=8<CR>:<CR>")
257 " Change to ts=16 with \6.
258 call Mapping("6", ":se ts=16<CR>:<CR>")
259 " Change to ts=32 with \3.
260 call Mapping("3", ":se ts=32<CR>:<CR>")
261 " Toggle paste mode with \p.
262 call Mapping("p", ":se paste!<CR>:<CR>")
263 " Swap case-sensitivity with \c.
264 call Mapping("c", ":call Invert_Case()<CR>:<CR>")
265 " Change number mode with \n.
266 call Mapping("n", ":se number!<CR>:<CR>")
267 " Expand or shrink window size with \> and \<.  For use after toggling number.
268 call Mapping(">", ":exe 'se columns+=' . numberwidth<CR>:<CR>")
269 call Mapping("<", ":exe 'se columns-=' . numberwidth<CR>:<CR>")
270 " Clear search pattern with \/.
271 call Mapping("/", ":let @/=\"\"<CR>:<CR>")
272
273 " Forget the Ex mode mapping.
274 map Q <NOP>
275
276 " Vim tip 99: What's the highlighting group under the cursor?
277 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>")
278
279 endif
280
281 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
282 " Handle options only available in Vim 6 and above.
283 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
284 if version >= "600"
285 version 6.0
286
287 " Remember quickfix state.
288 let g:quickfixing=0
289
290 " Set indenting by filetype.
291 filetype indent on
292
293 " Less intrusive syntax highlighting.
294 syn enable
295
296 " Nice GUI colour.
297 if has("gui_running")
298   se guifont=DejaVu\ Sans\ Mono\ 10
299   colo darkblue
300 elseif &t_Co > 16
301   try
302     colo iain
303   catch
304   endtry
305 endif
306 if has("win32")
307   se guifont=DejaVu_Sans_Mono:h10:cANSI
308 endif
309
310 " Ignore whitespace when diffing.
311 se diffopt=filler,iwhite
312
313 " Expand window when doing a vertical diff.
314 if &diff
315   let &columns = 164
316 endif
317
318 " Remember that we are opening the quickfix window.
319 au BufWinEnter quickfix let g:quickfixing=1
320 au BufUnload * if &ft == "qf" | let g:quickfixing=0 | endif
321
322 " Allow in-place editing of crontabs.
323 au FileType crontab set backupcopy=yes
324
325 " Make * and # work the way you expect in visual mode.
326 vnoremap * y/\V<C-R>=substitute(escape(@@,"/\\"),"\n","\\\\n","ge")<CR><CR>
327 vnoremap # y?\V<C-R>=substitute(escape(@@,"?\\"),"\n","\\\\n","ge")<CR><CR>
328
329 " Change list mode.
330 fun! Cycle_List()
331   let basic='tab:\\_,trail:_,extends:<,precedes:>'
332   call Iain_Vars()
333   let b:iainlist = b:iainlist + 1
334   if b:iainlist > 2
335     let b:iainlist = 0
336   endif
337   if b:iainlist == 0
338     set nolist
339   elseif b:iainlist == 1
340     exec "set lcs=" . basic
341     set list
342   else
343     exec "set lcs=" . basic . ",eol:$"
344     set list
345   endif
346 endfun
347
348 " Cycle between hex and decimal display of toolbar stuff.
349 fun! Cycle_HexStatusLine()
350   call Iain_Vars()
351   let b:iainhex = ! b:iainhex
352   call Show_StatusLine()
353 endfun
354
355 " Cycle verbose display of toolbar stuff.
356 fun! Cycle_VerboseStatusLine()
357   call Iain_Vars()
358   let b:iainverbose = ! b:iainverbose
359   call Show_StatusLine()
360 endfun
361
362 " Toggle quickfix window.
363 fun! Cycle_Quickfix()
364   if g:quickfixing == 1
365     cclose
366     let g:quickfixing=0
367   else
368     copen
369   endif
370 endfun
371
372 " Swap hex/decimal statusline with \x.
373 call Mapping("x", ":call Cycle_HexStatusLine()<CR>:<CR>")
374 " Change statusline verbosity with \v.
375 call Mapping("v", ":call Cycle_VerboseStatusLine()<CR>:<CR>")
376 " Cycle list styles with \l.
377 call Mapping("l", ":call Cycle_List()<CR>:<CR>")
378 " Toggle tags with \t.
379 call Mapping("t", ":Tlist<CR>")
380 " Change foldmethod with \f.
381 call Mapping("f", ":se foldenable!<CR>:<CR>")
382 " Toggle quickfix window with \q.
383 call Mapping("q", ":call Cycle_Quickfix()<CR>:<CR>")
384 " Rerun filetype detection with \s.  The s is for syntax, as this will be
385 " updated as a side-effect.
386 call Mapping("s", ":filetype detect<CR>:<CR>")
387
388 " Change status bar colour when various things happen.
389 fun! Highlight_StatusLine(flag)
390   " Get current status.
391   call Iain_Vars()
392
393   " Change the status based on the flag.  XXX: Does Vim let us to do flags?
394   let re = "[" . tolower(a:flag) . toupper(a:flag) . "]"
395   let b:iainstatus = substitute(b:iainstatus, re, a:flag, "")
396
397   " Default colour.
398   let s:colour = "darkblue"
399   let s:termcolour = ""
400   let s:term88colour = "17"
401   let s:term256colour = "17"
402   " Maybe override depending on status.
403   if b:iainstatus =~# "H"
404     if b:iainstatus =~# "I"
405       " Held in insert mode.  Add extra highlight if we don't have focus.
406       if b:iainstatus =~# "f"
407         let s:colour = "darkred"
408         let s:term88colour = "32"
409         let s:term256colour = "88"
410       else
411         let s:colour = "darkmagenta"
412         let s:term88colour = "33"
413         let s:term256colour = "90"
414       endif
415     endif
416   else
417     if b:iainstatus =~# "I"
418       " Regular insert mode.
419       let s:colour = "darkmagenta"
420       let s:term88colour = "33"
421       let s:term256colour = "90"
422     endif
423   endif
424
425   if &t_Co == 88
426     let s:termcolour = s:term88colour
427   elseif &t_Co == 256
428     let s:termcolour = s:term256colour
429   else
430     let s:termcolour = s:colour
431   endif
432
433   exec "highlight StatusLine guifg=white guibg=" . s:colour . " ctermbg=white ctermfg=" . s:termcolour
434 endfun
435
436 call Highlight_StatusLine("")
437 endif
438
439 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
440 " Handle options only available in Vim 7 and above.
441 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
442 if version >= "700"
443 version 7.0
444
445 au CursorHoldI * call Highlight_StatusLine("H")
446 au CursorMovedI * call Highlight_StatusLine("h")
447 au FocusGained * call Highlight_StatusLine("F")
448 au FocusLost * call Highlight_StatusLine("f")
449 au InsertEnter * call Highlight_StatusLine("I")
450 au InsertLeave * call Highlight_StatusLine("i")
451
452 " Make diffs vertical by default.
453 se diffopt+=vertical
454
455 " Set size of numbers column.
456 se numberwidth=5
457
458 " Add "previous tab" mapping as gb.
459 map gb :tabPrev<CR>
460
461 " Transparency.
462 if has("gui_macvim")
463   se transparency=15
464 endif 
465
466 " Perforce.
467 let g:p4EnableMenu=1
468 let g:p4Presets='P4CONFIG'
469 endif