Use new Highlight_StatusLine() function to handle statusline highlighting.
[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 " Use - and = to create underlines.
62 map - yyp:s/./-/g<RETURN>:let @/=''<RETURN>:<RETURN>
63 map = yyp:s/./=/g<RETURN>:let @/=''<RETURN>:<RETURN>
64
65 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
66 " Handle options only available in Vim 5 and above.
67 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
68 if version >= "500"
69 version 5.0
70
71 " Vim 5 hardcodes the size of numbers column to 8.
72 let numberwidth=8
73
74 " Save sessions in UNIX format with / as file separator.  This is
75 " cross-platoform.
76 se ssop+=unix,slash
77
78 " Nuke any pre-existing autocommands.
79 autocmd!
80
81 " Save the current window width so we can restore it when we quit.
82 let oldcols=&columns
83
84 " More GUI options.  Add icon, tearoffs and toolbar.
85 se go+=itT
86
87 " Allow dynamic window resize even if we aren't in an xterm.
88 se t_WS=\e[8;%p1%d;%p2%dt
89
90 " Highlight search results.
91 se hlsearch
92
93 " Syntax highlighting.
94 syn on
95
96 " Use a discernably different colour to highlight the cursor which shows 
97 " matching brackets.  Our regular cursor is green so use blue instead of cyan.
98 hi MatchParen ctermbg=blue
99
100 " Catch typos.
101 command! W :w
102 command! Wq :wq
103 command! Wqa :wqa
104
105 " Set up our variables.
106 fun! Iain_Vars()
107   if ! exists("b:iainlist")
108     let b:iainlist = 0
109   endif
110   if ! exists("b:iainhex")
111     let b:iainhex = 0
112   endif
113   if ! exists("b:iainverbose")
114     let b:iainverbose = 0
115   endif
116   if ! exists("b:iainstatus")
117     " Window Flags: (F)ocused, (I)nsert mode, Cursor (H)old.
118     let b:iainstatus = "Fih"
119   endif
120 endfun
121
122 " Helper for status line.
123 " Show space, underscore or dollar sign depending on list mode.
124 fun! Show_List()
125   call Iain_Vars()
126   if b:iainlist == 0
127     " No list.
128     return " "
129   elseif b:iainlist == 1
130     " Just tabs.
131     return "_"
132   else
133     " Full list.
134     return "\$"
135   endif
136 endfun
137
138 " Helper for status line.
139 " Show c or C to denote case-sensitivity.
140 fun! Show_Case()
141   if &ic
142     return "c"
143   else
144     return "C"
145   endif
146 endfun
147
148 " Helper for status line.
149 " Show the size of the tabstop.
150 fun! Show_Tabstop()
151   return &ts
152 endfun
153
154 " Helper for status line.
155 " Show p when paste mode is on.
156 fun! Show_Paste()
157   if &paste
158     return "p"
159   else
160     return ""
161   endif
162 endfun
163
164 " Show the status line.
165 fun! Show_StatusLine()
166   call Iain_Vars()
167   let sl1='%2n\:\ %<%f\ [%{Show_List()}%{Show_Case()}%{Show_Tabstop()}%{Show_Paste()}%Y%M%R]\ %='
168   let sl3='L:%4.6l/%-4.6L\ C:%3.6c\ \|\ %P'
169   let hexformat='%b'
170   if b:iainhex
171     let hexformat='0\x%02B'
172   endif
173   if b:iainverbose
174     let sl2=hexformat . '\ \|\ P:%4.6o\ '
175   else
176     let sl2=''
177   endif
178   exec "set statusline=" . sl1 . sl2 . sl3
179 endfun
180
181 " Toggle case-sensitivity.
182 fun! Invert_Case()
183   let &ic = ! &ic
184 endfun
185
186 " Restore window size.
187 au VimLeave * if exists("oldcols") | let &columns=oldcols | endif
188
189 " Map Makefile mode.
190 au BufEnter * if &ft == "make" | call MakeMode_map() | endif
191 au BufLeave * if &ft == "make" | call MakeMode_unmap() | endif
192
193 " Entering Make mode.
194 fun! MakeMode_map()
195   set list
196   set noexpandtab
197 endfun
198
199 " Leaving Make mode.
200 fun! MakeMode_unmap()
201   set nolist
202   set expandtab
203 endfun
204
205 " Show the status line for the first time.
206 call Show_StatusLine()
207
208 " Change to ts=2 with Q2.
209 map Q2 :se ts=2<CR>:<CR>
210 " Change to ts=4 with Q4.
211 map Q4 :se ts=4<CR>:<CR>
212 " Change to ts=8 with Q8.
213 map Q8 :se ts=8<CR>:<CR>
214 " Change to ts=16 with Q6.
215 map Q6 :se ts=16<CR>:<CR>
216 " Change to ts=32 with Q3.
217 map Q3 :se ts=32<CR>:<CR>
218 " Toggle paste mode with Qp.
219 map Qp :se paste!<CR>:<CR>
220 " Swap case-sensitivity with Qc.
221 map Qc :call Invert_Case()<CR>:<CR>
222 " Change number mode with Qn.
223 map Qn :se number!<CR>:<CR>
224 " Expand or shrink window size with Q> and Q<.  For use after toggling number.
225 map Q> :exe 'se columns+=' . numberwidth<CR>:<CR>
226 map Q< :exe 'se columns-=' . numberwidth<CR>:<CR>
227 " Clear search pattern with Q/.
228 map Q/ :let @/=""<CR>:<CR>
229
230 endif
231
232 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
233 " Handle options only available in Vim 6 and above.
234 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
235 if version >= "600"
236 version 6.0
237
238 " Remember quickfix state.
239 let g:quickfixing=0
240
241 " Set indenting by filetype.
242 filetype indent on
243
244 " Less intrusive syntax highlighting.
245 syn enable
246
247 " Nice GUI colour.
248 if has("gui_running")
249   se guifont=DejaVu\ Sans\ Mono\ 10
250   colo darkblue
251 endif
252 if has("win32")
253   se guifont=DejaVu_Sans_Mono:h10:cANSI
254 endif
255
256 " Ignore whitespace when diffing.
257 se diffopt=filler,iwhite
258
259 " Expand window when doing a vertical diff.
260 if &diff
261   let &columns = 164
262 endif
263
264 " Status bar matches the colour.
265 highlight StatusLine guifg=white guibg=darkblue ctermbg=white ctermfg=darkblue
266
267 " Numbers in blue.
268 highlight LineNr term=underline cterm=bold guifg=blue ctermfg=blue
269
270 " Remember that we are opening the quickfix window.
271 au BufWinEnter quickfix let g:quickfixing=1
272 au BufUnload * if &ft == "qf" | let g:quickfixing=0 | endif
273
274 " Make * and # work the way you expect in visual mode.
275 vnoremap * y/\V<C-R>=substitute(escape(@@,"/\\"),"\n","\\\\n","ge")<CR><CR>
276 vnoremap # y?\V<C-R>=substitute(escape(@@,"?\\"),"\n","\\\\n","ge")<CR><CR>
277
278 " Change list mode.
279 fun! Cycle_List()
280   let basic='tab:\\_,trail:_,extends:<,precedes:>'
281   call Iain_Vars()
282   let b:iainlist = b:iainlist + 1
283   if b:iainlist > 2
284     let b:iainlist = 0
285   endif
286   if b:iainlist == 0
287     set nolist
288   elseif b:iainlist == 1
289     exec "set lcs=" . basic
290     set list
291   else
292     exec "set lcs=" . basic . ",eol:$"
293     set list
294   endif
295 endfun
296
297 " Cycle between hex and decimal display of toolbar stuff.
298 fun! Cycle_HexStatusLine()
299   call Iain_Vars()
300   let b:iainhex = ! b:iainhex
301   call Show_StatusLine()
302 endfun
303
304 " Cycle verbose display of toolbar stuff.
305 fun! Cycle_VerboseStatusLine()
306   call Iain_Vars()
307   let b:iainverbose = ! b:iainverbose
308   call Show_StatusLine()
309 endfun
310
311 " Toggle quickfix window.
312 fun! Cycle_Quickfix()
313   if g:quickfixing == 1
314     cclose
315     let g:quickfixing=0
316   else
317     copen
318   endif
319 endfun
320
321 " We use Q for various commands.  Unmap it.
322 " Vim 5 won't let us unmap this as it treats Q as an ambiguous mapping (because 
323 " Qx also exists.  With Vim 5 you are rewarded with Ex mode if you don't type 
324 " the Qx sequence quickly enough.  Vim 6 allows us to forget the Ex mapping.
325 map Q <Nop>
326
327 " Swap hex/decimal statusline with Qx.
328 map Qx :call Cycle_HexStatusLine()<CR>:<CR>
329 " Change statusline verbosity with Qv.
330 map Qv :call Cycle_VerboseStatusLine()<CR>:<CR>
331 " Cycle list styles with Ql.
332 map Ql :call Cycle_List()<CR>:<CR>
333 " Toggle tags with Qt.
334 map Qt :Tlist<CR>
335 " Change foldmethod with Qf.
336 map Qf :se foldenable!<CR>:<CR>
337 " Toggle quickfix window with Qq.
338 map Qq :call Cycle_Quickfix()<CR>:<CR>
339 " Rerun filetype detection with Qs.  The s is for syntax, as this will be
340 " updated as a side-effect.
341 map Qs :filetype detect<CR>:<CR>
342
343 endif
344
345 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
346 " Handle options only available in Vim 7 and above.
347 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
348 if version >= "700"
349 version 7.0
350
351 " Change status bar colour when entering insert mode.
352 fun! Highlight_StatusLine(flag)
353   " Get current status.
354   call Iain_Vars()
355
356   " Change the status based on the flag.  XXX: Does Vim let us to do flags?
357   let re = "[" . tolower(a:flag) . toupper(a:flag) . "]"
358   let b:iainstatus = substitute(b:iainstatus, re, a:flag, "")
359
360   " Default colour.
361   let colour = "darkblue"
362   " Maybe override depending on status.
363   if b:iainstatus =~# "H"
364     if b:iainstatus =~# "I"
365       " Held in insert mode.  Add extra highlight if we don't have focus.
366       if b:iainstatus =~# "f"
367         let colour = "darkmagenta"
368       else
369         let colour = "darkred"
370       endif
371     endif
372   else
373     if b:iainstatus =~# "I"
374       " Regular insert mode.
375       let colour = "darkred"
376     endif
377   endif
378
379   exec "highlight StatusLine guifg=white guibg=" . colour . " ctermbg=white ctermfg=" . colour
380 endfun
381
382 au CursorHoldI * call Highlight_StatusLine("H")
383 au CursorMovedI * call Highlight_StatusLine("h")
384 au FocusGained * call Highlight_StatusLine("F")
385 au FocusLost * call Highlight_StatusLine("f")
386 au InsertEnter * call Highlight_StatusLine("I")
387 au InsertLeave * call Highlight_StatusLine("i")
388
389 " Make diffs vertical by default.
390 se diffopt+=vertical
391
392 " Set size of numbers column.
393 se numberwidth=5
394
395 " Add "previous tab" mapping as gb.
396 map gb :tabPrev<CR>
397 endif