More generic code.
[~madcoder/dotfiles.git] / vimrc
1 " {{{ settings
2
3 set nocompatible                " Use Vim defaults instead of 100% vi compatibility
4 set backspace=indent,eol,start  " more powerful backspacing
5
6 set autoindent                  " always set auto-indenting on
7 set autoread                    " reload unchanged files silentely
8 set clipboard=unnamed           " copy/paste to global clipboard
9 set hidden                      " allow to cycle and hide modified buffers
10 set nobackup                    " Don't keep a backup file
11 set backupcopy=auto,breakhardlink
12 set swapsync=
13 set esckeys                     " allow usage of curs keys within insert mode
14 set timeout
15 set ttimeoutlen=10
16 set timeoutlen=500              " set timout for esc to 50ms
17
18 set listchars=tab:\ \ ,trail:-,extends:>,precedes:<
19 set list
20 set joinspaces                  " insert two spaces after a period with every joining of lines.
21                                 " This is very nice!
22 set lazyredraw                  " [VIM5];  do not update screen while executing macros
23 set ttyfast
24 set magic                       " Use some magic in search patterns?  Certainly!
25 set modeline                    " Allow the last line to be a modeline - useful when
26                                 " the last line in sig gives the preferred text-width
27                                 " for replies.
28 set modelines=5
29 set pastetoggle=<F4>
30
31 set nonumber
32 set report=0                    " show a report when N lines were changed.
33                                 " report=0 thus means "show all changes"!
34
35 set laststatus=2                " show status line?  Yes, always!
36 set noruler                       " show cursor position?  Yep!
37 set statusline=[\ %f%m\ %r%y\ %=\ hex:%B\ \ %l,%c%V\ \ %P\ ]
38
39 set shiftwidth=4                " Number of spaces to use for each insertion of
40                                 " (auto)indent.
41 set shortmess=at                " Kind of messages to show.   Abbreviate them all!
42                                 " New since vim-5.0v: flag 'I' to suppress "intro message".
43 set scrolloff=2                 " context
44 set showcmd                     " Show current uncompleted command?  Absolutely!
45 set showmatch                   " Show the matching bracket for the last ')'?
46 set showmode                    " Show the current mode?  YEEEEEEEEESSSSSSSSSSS!
47
48 set suffixes=.bak,~,.swp,.o,.info,.aux,.log,.dvi,.bbl,.blg,.brf,.cb,.ind,.idx,.ilg,.inx,.out,.toc,.cmi,.cmo
49                                 " Suffixes to ignore in file completion
50
51 set tabstop=8                   " tabstop
52 set softtabstop=4               " sts
53 set expandtab                   " expand tabs
54 set notextmode                  " no - I am using Vim on UNIX!
55 set textwidth=0                 " Don't wrap words by default
56 set title                       " Permet de voir le tit. du doc. crt. ds les XTERM
57 set viminfo='1000,/1000,:1000,<1000,@1000,n~/.viminfo
58 set history=1000
59                                 " What info to store from an editing session
60                                 " in the viminfo file;  can be used at next session.
61 set sessionoptions=buffers,folds,localoptions,options,tabpages,help
62
63 set ignorecase                  " Do case insensitive matching
64 set incsearch                   " Incremental search
65 set hlsearch                    " hilight search
66
67
68 set whichwrap=<,>,[,]           "
69 set wildchar=<TAB>              " the char used for "expansion" on the command line
70                                 " default value is "<C-E>" but I prefer the tab key:
71 set wildmenu                    " Completion on the command line shows a menu
72 set wildmode=longest,full
73 set winminheight=0              " Minimum height of VIM's windows opened
74 set mouse=a
75 set mousefocus
76 set wrapmargin=1
77 set nowritebackup
78 set foldmethod=marker
79
80 set noequalalways
81 set eadirection=
82
83 set cpoptions-=C                " enable commands that continue on the next line
84
85 " use ctrl-n ctrl-n instead of ctrl-x ctrl-k
86 set complete-=k complete+=k
87
88 set tags=tags;/,.tags;/,TAGS;/
89
90 set cinoptions=
91 set cinoptions+=t0             " type on the line before the functions is not idented
92 set cinoptions+=:2,=2          " indent case ...: of 2 from the switch {
93 set cinoptions+=(0,Ws          " indent in functions ( ... ) when it breaks
94 set cinoptions+=g2,h2          " indent C++ scope of 2, and the members from 2 from it
95 set cinoptions+=m1             " aligh the closing ) properly
96 "set cinoptions+=l1            " align closing brace with the case
97 "set cinoptions+=b1            " align break; with case ...:
98
99 set diffopt=filler,context:5,iwhite
100 set fillchars+=diff:\ ,vert:│
101
102 set makeprg=LC_ALL=C\ make\ MONOCHROME=1
103
104 exe "set path=." . system("echo | cpp -v 2>&1 | grep '^ .*/include' | tr -d \"\n\" | tr \" \" \",\"")
105 set path+=.;/
106
107 " }}}
108 " {{{ vim 7 settings
109
110 if version >= 700
111     set autochdir              " autochdir...
112     set virtualedit=onemore    " Yes I want to be able to be "on \n"
113     set spelllang=en_us,fr
114     set pumheight=16
115
116     " make <enter> work in popup
117     inoremap <cr> <C-R>=pumvisible() ? "\<lt>C-Y>" : "\<lt>cr>"<cr>
118 else
119     au BufEnter * :lcd %:p:h
120 endif
121
122 " }}}
123 " {{{ Window magic
124
125 function! DeleteBuffer()
126    let bid = bufnr("%")
127    bnext
128    exe "bdel " . bid
129    redraw
130 endfunction
131
132 map <C-W>d :call DeleteBuffer()<cr>
133 "map <C-W>k :bd<cr>
134 map <C-W>g :bo cw 8<cr>
135
136 " }}}
137 " {{{ Mappings
138
139 " {{{ Tab Key magic ...
140 vmap <tab> >gv
141 vmap <bs> <gv
142
143 function! TabAlign()
144     let col  = col('.')
145     let lnum = line('.')
146
147     while lnum > 1
148         let lnum = lnum - 1
149         let ln = strpart(getline(lnum), col-1)
150         let ms = matchstr(ln, '[^ ]*  *[^ ]')
151         if ms != ""
152             break
153         endif
154     endwhile
155
156     if lnum == 1
157         return "\<Tab>"
158     else
159         let @z = substitute(strpart(ms, 0, strlen(ms)-1), '.', ' ', 'g')
160         if col > strlen(getline('.'))
161             return "\<C-O>\"zp"
162         else
163             return "\<C-O>\"zP"
164         endif
165     endif
166
167 endfunction
168
169 function! CleverTab()
170     let c = strpart(getline('.'), col('.')-2, 1)
171     if c == ' ' || c == '\t' || c == ''
172         return TabAlign()
173     else
174         return "\<C-P>"
175     endif
176 endfunction
177 inoremap <Tab> <C-R>=CleverTab()<CR>
178 inoremap <S-Tab> <C-R>=TabAlign()<CR>
179
180 " }}}
181
182 map + :cn<cr>
183 map - :cp<cr>
184 map <kPlus> :cn<cr>
185 map <kMinus> :cp<cr>
186
187 map Q gq
188
189 noremap  <F2>   :set mouse=a<cr>
190 noremap  <F3>   :set mouse=h<cr>
191 noremap  <F5>   :source ~/.vimrc<cr>
192
193 inoremap <F6>   <c-o>mzviwU`z
194 noremap  <F7>   :set ai!<CR>:set ai?<cr>
195 inoremap <F7>   <space><esc><F7>a<bs>
196
197 noremap  <F9>   :bp<cr>
198 inoremap <F9>   <esc>:bp<cr>
199 noremap  <F10>  :bn<cr>
200 inoremap <F10>  <esc>:bn<cr>
201 noremap  <F11>  :make!<cr>:bo cw 8<cr><cr>
202 inoremap <F11>  <esc>:make!<cr>:bo cw 8<cr><cr>
203 noremap  <S-F11> :make clean<cr><cr>
204 inoremap <S-F11> <esc>:make clean<cr><cr>
205
206 " }}}
207 "{{{ FTYPES
208
209 if has("autocmd")
210     filetype plugin indent on
211     syntax on
212
213     au BufReadPost *
214                 \ if line("'\"") > 0 && line("'\"") <= line("$") |
215                 \   exe "normal g`\"" |
216                 \ endif
217
218     au VimLeave * mksession! ~/.cache/session.vim
219
220     au FileType debchangelog normal zO
221
222     au FileType javascript setlocal cindent tw=78 iskeyword+=$
223     au FileType actionscript setlocal cindent tw=78 iskeyword+=$ noignorecase ff=dos
224     au FileType mail setlocal noet iskeyword+=- tw=72
225
226     au FileType haxe setlocal tw=78 noignorecase cin ai
227     au FileType c,cpp setlocal tw=78 noignorecase "fo-=ro
228     au FileType ocaml,acute,omlet setlocal sw=2 sts=2 tw=78
229     au FileType php  setlocal et fo+=ro tw=78 indentexpr= cin
230     au FileType python setlocal foldmethod=indent foldnestmax=1
231
232     au FileType diff   setlocal nofoldenable
233
234     au FileType html,xhtml,xml setlocal sw=2
235     au FileType mail setlocal spell
236
237     " make error list special
238     au BufRead quickfix setlocal nobuflisted nowrap number
239
240     au FileType svn setlocal spell tw=76
241     au FileType git setlocal spell tw=76
242
243     au BufRead,BufNewFile */dev/scm/git/* setlocal noet sw=8
244
245     let ocaml_noindent_let=1
246     let git_diff_spawn_mode=2
247     let git_diff_opts = "--patience -C -C -M"
248     let c_gnu=1
249     let c_space_errors=1
250     let c_no_curly_error=1
251
252     let g:bufExplorerFindActive=0
253     let g:bufExplorerSplitOutPathName=1
254     let g:bufExplorerShowRelativePath=0
255     let g:bufExplorerSortBy='fullpath'
256
257     let g:is_bash=1
258     let g:sh_fold_enabled=1
259
260     let g:debchangelog_fold_enable = 1
261     let g:debcontrol_fold_enable = 1
262 endif " has ("autocmd")
263
264 " }}}
265 " {{{ PLUGINS
266
267 runtime macros/matchit.vim
268 "runtime macros/justify.vim
269
270 runtime ftplugin/man.vim
271 nmap ! :exe "Man" expand("<cword>")<cr>
272
273 " }}}
274 " {{{ COLORS, GUI and FOLDING
275
276 set background=light
277 hi clear
278 if exists("syntax_on")
279    syntax reset
280 endif
281
282 if has("gui_running")
283     set guioptions=eit
284     set guifont=terminus
285     set guicursor=a:blinkon0
286     set background=light
287
288     fun! GuiTabLabel()
289         let label = ''
290         let bufnrlist = tabpagebuflist(v:lnum)
291
292         " Append the number of windows in the tab page if more than one
293         let wincount = tabpagewinnr(v:lnum, '$')
294         let label .= wincount
295
296         " Add '[*]' if one of the buffers in the tab page is modified
297         for bufnr in bufnrlist
298             if getbufvar(bufnr, "&modified")
299                 let label .= '[*]'
300                 break
301             endif
302         endfor
303
304
305         if exists("t:tabname")
306             return t:tabname . ': ' . label
307         else
308             return label . '[' . bufname(bufnrlist[tabpagewinnr(v:lnum) - 1]) .']'
309         endif
310     endf
311     set guitablabel=%{GuiTabLabel()}
312 endif
313
314 fun! <SID>Y(a)
315     if strlen(a:a) != 6 || a:a == "yellow"
316         return a:a
317     endif
318     if has("gui_running")
319         return "#".a:a
320     endif
321     let l:r = ("0x" . strpart(a:a, 0, 2)) + 0
322     let l:g = ("0x" . strpart(a:a, 2, 2)) + 0
323     let l:b = ("0x" . strpart(a:a, 4, 2)) + 0
324     if &t_Co == 88
325         let l:gray  = 10
326         let l:cube  = 4
327         let l:shift = 79
328     else
329         let l:gray  = 26
330         let l:cube  = 6
331         let l:shift = 232
332     endif
333
334     if l:r * l:gray / 256 == l:g * l:gray / 256 && l:r * l:gray / 256 == l:b * l:gray / 256
335         let l:s = l:r * l:gray / 256
336         if l:s == 0
337             return "black"
338         elseif l:s == l:gray - 1
339             return "white"
340         else
341             return l:shift + l:s
342         endif
343     endif
344     let l:x = ((l:r * l:cube) / 256)
345     let l:y = ((l:g * l:cube) / 256)
346     let l:z = ((l:b * l:cube) / 256)
347     return 16 + ((l:x * l:cube + l:y) * l:cube) + l:z
348 endfun
349
350 fun! <SID>myhi(cls, m, fg, bg)
351     if has("gui_running")
352         exec "hi ".a:cls." gui=".a:m." guifg=".<SID>Y(a:fg)." guibg=".<SID>Y(a:bg)
353     else
354         exec "hi ".a:cls." cterm=".a:m." ctermfg=".<SID>Y(a:fg)." ctermbg=".<SID>Y(a:bg)
355     endif
356 endfun
357
358 if has("gui_running") || &t_Co >= 88
359     if has("gui_running")
360         exec <SID>myhi("Normal",       "none",       "dfdfdf",    "00000f")
361         exec <SID>myhi("MoreMsg",      "none",       "dfdfdf",    "00000f")
362     else
363         exec <SID>myhi("Normal",       "none",       "dfdfdf",    "NONE")
364         exec <SID>myhi("MoreMsg",      "none",       "dfdfdf",    "NONE")
365     endif
366     exec <SID>myhi("Comment",      "none",       "afafff",    "0f0f2f")
367     exec <SID>myhi("Folded",       "none",       "afafff",    "0f0f2f")
368
369     exec <SID>myhi("SpecialKey",   "none",       "dfdfdf",    "0f0f2f")
370     exec <SID>myhi("Todo",         "underline",  "yellow",    "333333")
371     exec <SID>myhi("Error",        "bold",       "white",     "red")
372
373     exec <SID>myhi("Function",     "none",       "4fcfcf",    "NONE")
374     exec <SID>myhi("Identifier",   "none",       "4fcfcf",    "NONE")
375
376     exec <SID>myhi("Cursor",       "reverse",    "dfdfdf",    "black")
377     exec <SID>myhi("Visual",       "none",       "NONE",      "333333")
378     exec <SID>myhi("IncSearch",    "none",       "black",     "yellow")
379     exec <SID>myhi("Search",       "none",       "black",     "yellow")
380
381     exec <SID>myhi("StatusLine",   "none",       "yellow",    "333333")
382     exec <SID>myhi("StatusLineNc", "none",       "dfdfdf",    "1c1c1c")
383     exec <SID>myhi("WildMenu",     "none",       "white",     "0f0f2f")
384     exec <SID>myhi("VertSplit",    "none",       "darkgray",  "0f0f2f")
385     exec <SID>myhi("NonText",      "none",       "darkgray",  "NONE")
386
387     exec <SID>myhi("MatchParen",   "none",       "white",     "0f0f2f")
388     exec <SID>myhi("Pmenu",        "none",       "dfdfdf",    "0f0f2f")
389     exec <SID>myhi("PmenuSel",     "none",       "white",     "3f3f7f")
390     exec <SID>myhi("PmenuSbar",    "none",       "white",     "0f0f2f")
391     exec <SID>myhi("PmenuThumb",   "none",       "3f3f7f",    "3f3f7f")
392
393     exec <SID>myhi("SpellBad",     "none",       "NONE",      "800000")
394     exec <SID>myhi("SpellCap",     "none",       "NONE",      "004000")
395     exec <SID>myhi("SpellLocal",   "none",       "NONE",      "004000")
396     exec <SID>myhi("SpellRare",    "none",       "NONE",      "NONE")
397
398     exec <SID>myhi("Label",        "none",       "bf7f00",    "NONE")
399     exec <SID>myhi("Conditional",  "none",       "bf7f00",    "NONE")
400     exec <SID>myhi("Repeat",       "none",       "bf7f00",    "NONE")
401     exec <SID>myhi("Statement",    "none",       "bf7f00",    "NONE")
402
403     exec <SID>myhi("StorageClass", "none",       "3fbf3f",    "NONE")
404     exec <SID>myhi("Type",         "none",       "3fbf3f",    "NONE")
405     exec <SID>myhi("Structure",    "none",       "3fbf3f",    "NONE")
406     exec <SID>myhi("Directory",    "none",       "3fbf3f",    "NONE")
407
408     exec <SID>myhi("Include",      "none",       "bf0fbf",    "NONE")
409     exec <SID>myhi("PreProc",      "none",       "bf0fbf",    "NONE")
410     exec <SID>myhi("Macro",        "none",       "bf0fbf",    "NONE")
411     exec <SID>myhi("SpecialChar",  "none",       "bf0fbf",    "NONE")
412
413     exec <SID>myhi("Character",    "none",       "bf0f0f",    "NONE")
414     exec <SID>myhi("String",       "none",       "bf0f0f",    "NONE")
415     exec <SID>myhi("Constant",     "none",       "bf0f0f",    "NONE")
416
417     " diff
418     exec <SID>myhi("DiffAdd",      "none",       "NONE",      "002f0f")
419     exec <SID>myhi("DiffDelete",   "none",       "NONE",      "2f000f")
420     exec <SID>myhi("DiffChange",   "none",       "NONE",      "00003f")
421     exec <SID>myhi("DiffText",     "underline",  "NONE",      "00003f")
422     exec <SID>myhi("diffRemoved",  "none",       "bf0fbf",    "NONE")
423
424     " C
425     exec <SID>myhi("cFunction",    "none",       "b0b0b0",    "NONE")
426 else
427     hi Comment      cterm=none       ctermfg=blue       ctermbg=none
428     hi Folded       cterm=none       ctermfg=brown      ctermbg=none
429
430     hi Visual       cterm=reverse    ctermfg=none       ctermbg=none
431     hi IncSearch    cterm=underline  ctermfg=white      ctermbg=none
432     hi Search       cterm=underline  ctermfg=white      ctermbg=none
433
434     hi StatusLine   cterm=none       ctermfg=white      ctermbg=blue
435     hi StatusLineNc cterm=none       ctermfg=black      ctermbg=white
436     hi WildMenu     cterm=none       ctermfg=white      ctermbg=none
437     hi VertSplit    cterm=none       ctermfg=darkgray   ctermbg=none
438     hi NonText      cterm=none       ctermfg=darkgray   ctermbg=none
439
440     hi MatchParen   cterm=underline  ctermfg=none       ctermbg=none
441     hi Pmenu        cterm=none       ctermfg=gray       ctermbg=black
442     hi PmenuSel     cterm=none       ctermfg=black      ctermbg=gray
443     hi PmenuSbar    cterm=none       ctermfg=blue       ctermbg=blue
444     hi PmenuThumb   cterm=none       ctermfg=gray       ctermbg=gray
445
446     hi SpellBad     cterm=underline  ctermfg=lightred   ctermbg=none
447     hi SpellCap     cterm=none       ctermfg=lightred   ctermbg=none
448     hi SpellLocal   cterm=underline  ctermfg=darkgreen  ctermbg=none
449     hi SpellRare    cterm=none       ctermfg=none       ctermbg=none
450 endif
451
452 " Custom
453 hi def link htmlTag htmlStatement
454 hi def link htmlEndTag htmlStatement
455
456 " }}}