1 " vim:shiftwidth=2:tabstop=8:expandtab

   2 if has('autocmd')
   3   " Remove ALL autocommands for the current group
   4   au!

   5   " Mark .asm files MASM-type assembly
   6   au BufNewFile,BufReadPre *.asm let b:asmsyntax='masm'
   7 endif

   8 if has('gui_running')
   9   let do_syntax_sel_menu=1
  10 endif

  11 if has('gui_running') && $LANG !~ '\.'
  12   set encoding=utf-8
  13 endif

  14 set nocompatible
  15 source $VIMRUNTIME/vimrc_example.vim

  16 set autoindent
  17 set nobackup
  18 set showmatch
  19 set formatoptions+=mM
  20 set fileencodings=ucs-bom,utf-8,gbk
  21 set statusline=%<%f\ %h%m%r%=%k[%{(&fenc==\"\")?&enc:&fenc}%{(&bomb?\",BOM\":\"\")}]\ %-14.(%l,%c%V%)\ %P
  22 if has('mouse')
  23   set mouse=a
  24 endif
  25 if has('multi_byte') && v:version > 601
  26   if v:lang =~? '^\(zh\)\|\(ja\)\|\(ko\)'
  27     set ambiwidth=double
  28   endif
  29 endif

  30 " Key mappings to ease browsing long lines
  31 noremap  <C-J>       gj
  32 noremap  <C-K>       gk
  33 noremap  <Down>      gj
  34 noremap  <Up>        gk
  35 inoremap <Down> <C-O>gj
  36 inoremap <Up>   <C-O>gk

  37 " Key mappings for quick arithmetic inside Vim
  38 nnoremap <silent> <Leader>ma yypV:!calcu '<C-R>"'<CR>k$
  39 vnoremap <silent> <Leader>ma yo<ESC>pV:!calcu '<C-R>"'<CR>k$
  40 nnoremap <silent> <Leader>mr yyV:!calcu '<C-R>"'<CR>$
  41 vnoremap <silent> <Leader>mr ygvmaomb:r !calcu '<C-R>"'<CR>"ay$dd`bv`a"ap

  42 " Key mapping to stop the search highlight
  43 nmap <silent> <F2>      :nohlsearch<CR>
  44 imap <silent> <F2> <C-O>:nohlsearch<CR>

  45 " Key mapping for the taglist.vim plugin
  46 nmap <F9>      :Tlist<CR>
  47 imap <F9> <C-O>:Tlist<CR>

  48 " Key mappings for the quickfix commands
  49 nmap <F11> :cn<CR>
  50 nmap <F12> :cp<CR>

  51 " Non-GUI setting
  52 if !has('gui_running')
  53   " Do not increase the windows width in the taglist.vim plugin
  54   if has('eval')
  55     let Tlist_Inc_Winwidth=0
  56   endif

  57   " Set text-mode menu
  58   if has('wildmenu')
  59     set wildmenu
  60     set cpoptions-=<
  61     set wildcharm=<C-Z>
  62     nmap <F10>      :emenu <C-Z>
  63     imap <F10> <C-O>:emenu <C-Z>
  64   endif
  65 endif

  66 if has('autocmd')
  67   function! SetFileEncodings(encodings)
  68     let b:my_fileencodings_bak=&fileencodings
  69     let &fileencodings=a:encodings
  70   endfunction

  71   function! RestoreFileEncodings()
  72     let &fileencodings=b:my_fileencodings_bak
  73     unlet b:my_fileencodings_bak
  74   endfunction

  75   function! CheckFileEncoding()
  76     if &modified && &fileencoding != ''
  77       exec 'e! ++enc=' . &fileencoding
  78     endif
  79   endfunction

  80   function! ConvertHtmlEncoding(encoding)
  81     if a:encoding ==? 'gb2312'
  82       return 'gbk'              " GB2312 imprecisely means GBK in HTML
  83     elseif a:encoding ==? 'iso-8859-1'
  84       return 'latin1'           " The canonical encoding name in Vim
  85     elseif a:encoding ==? 'utf8'
  86       return 'utf-8'            " Other encoding aliases should follow here
  87     else
  88       return a:encoding
  89     endif
  90   endfunction

  91   function! DetectHtmlEncoding()
  92     if &filetype != 'html'
  93       return
  94     endif
  95     normal m`
  96     normal gg
  97     if search('\c<meta http-equiv=\("\?\)Content-Type\1 content="text/html; charset=[-A-Za-z0-9_]\+">') != 0
  98       let reg_bak=@"
  99       normal y$
 100       let charset=matchstr(@", 'text/html; charset=\zs[-A-Za-z0-9_]\+')
 101       let charset=ConvertHtmlEncoding(charset)
 102       normal ``
 103       let @"=reg_bak
 104       if &fileencodings == ''
 105         let auto_encodings=',' . &encoding . ','
 106       else
 107         let auto_encodings=',' . &fileencodings . ','
 108       endif
 109       if charset !=? &fileencoding &&
 110          \(auto_encodings =~ ',' . &fileencoding . ',' || &fileencoding == '')
 111         silent! exec 'e ++enc=' . charset
 112       endif
 113     else
 114       normal ``
 115     endif
 116   endfunction

 117   function! GnuIndent()
 118     setlocal cinoptions=>4,n-2,{2,^-2,:2,=2,g0,h2,p5,t0,+2,(0,u0,w1,m1
 119     setlocal shiftwidth=2
 120     setlocal tabstop=8
 121   endfunction

 122   function! RemoveTrailingSpace()
 123     if $VIM_HATE_SPACE_ERRORS != '0' &&
 124           \(&filetype == 'c' || &filetype == 'cpp' || &filetype == 'vim')
 125       normal m`
 126       silent! :%s/\s\+$//e
 127       normal ``
 128     endif
 129   endfunction

 130   " Highlight space errors in C/C++ source files (Vim tip #935)
 131   if $VIM_HATE_SPACE_ERRORS != '0'
 132     let c_space_errors=1
 133   endif

 134   " Use Canadian spelling convention in engspchk (Vim script #195)
 135   let spchkdialect='can'

 136   " Show syntax highlighting attributes of character under cursor (Vim
 137   " script #383)
 138   map <Leader>a :call SyntaxAttr()<CR>

 139   " Automatically find scripts in the autoload directory
 140   au FuncUndefined * exec 'runtime autoload/' . expand('<afile>') . '.vim'

 141   " File type related autosetting
 142   au FileType c,cpp setlocal cinoptions=:0,g0,(0,w1 shiftwidth=4 tabstop=4
 143   au FileType diff  setlocal shiftwidth=4 tabstop=4
 144   au FileType html  setlocal autoindent indentexpr=
 145   au FileType changelog setlocal textwidth=76

 146   " Text file encoding autodetection
 147   au BufReadPre  *.gb               call SetFileEncodings('gbk')
 148   au BufReadPre  *.big5             call SetFileEncodings('big5')
 149   au BufReadPre  *.nfo              call SetFileEncodings('cp437')
 150   au BufReadPost *.gb,*.big5,*.nfo  call RestoreFileEncodings()
 151   au BufWinEnter *.txt              call CheckFileEncoding()

 152   " Detect charset encoding in an HTML file
 153   au BufReadPost *.htm* nested      call DetectHtmlEncoding()

 154   " Recognize standard C++ headers
 155   au BufEnter /usr/include/c++/*    setf cpp
 156   au BufEnter /usr/include/g++-3/*  setf cpp

 157   " Setting for files following the GNU coding standard
 158   au BufEnter /usr/*                call GnuIndent()

 159   " Remove trailing spaces for C/C++ and Vim files
 160   au BufWritePre *                  call RemoveTrailingSpace()
 161 endif