if has("gui_win32")
source $VIMRUNTIME/vimrc_example.vim
"source $VIMRUNTIME/mswin.vim
set nocompatible
"behave mswin
" Remove menu, scroll bar and tools bar
set guioptions-=m
set guioptions-=r
set guioptions-=T
" Command ToggleView
let s:view_status = 0
function ToggleView()
if s:view_status == 0
set guifont=
set lines=53
let s:view_status = 1
elseif s:view_status == 1
set guifont=Lucida_Console:h9:b:cANSI
set lines=73
let s:view_status = 2
elseif s:view_status == 2
set guifont=Consolas:h9:b:cANSI
let s:view_status = 3
else
set guifont=
set lines=46
let s:view_status = 0
endif
endfunction
set lsp=-1 lines=46 columns=99
set background=dark
set cursorline cursorcolumn
let gitgui = '!start C:\program files\TortoiseGit\bin\TortoiseGitProc.exe'
let svngui = '!start C:\program files\TortoiseGit\bin\TortoiseProc.exe'
nmap <C-j> :exe gitgui . ' /command:log /path:' . getcwd()<cr>
"nmap <C-z> :exe '!start C:\tools\console2\console.exe -d ' . getcwd()<cr>
nmap <C-z> :exe '!start C:\tools\pycmd\pycmd.exe'<cr>
nmap <C-e> :exe '!start explorer ' . getcwd()<cr>
" shift tab pages
nmap <S-Left> :tabp<cr>
nmap <S-Right> :tabn<cr>
endif
" General option
syntax enable
set ruler number
set hlsearch incsearch
set autowrite nobackup
set ignorecase
set linebreak
set mouse=a
set cindent shiftwidth=4 tabstop=4
set backspace=2
set formatoptions=tcqor
set scrolloff=6
set tags=tags,tagsx,..\tags;
set autochdir
set path=**
set laststatus=2
set grepprg=gfind\ .\ -name\ \"*.[chs]\"\ -exec\ grep\ -iHn\ <cword>\ {}\ ;
set shellpipe=2>&1\|tee
compiler! gcc
colorscheme torte
hi StatusLine gui=reverse " guifg=darkgrey guibg=cyan
hi cursorline guibg=grey30
hi cursorcolumn guibg=grey20
hi Search guifg=cyan
nmap ,d :bd<cr>
nmap ,4 :set ts=4<cr>
nmap tt "Ayy
nmap <space>f :find
nmap <space>g :tag
nmap <space>i [I
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Enhanced up-down-motions
nmap ff zz
nmap fj zb
nmap fk zt
nmap fm ztM
nmap f, zbM
nmap <space>j :call ToggleJK()<cr>
let s:jk_func = 0
function ToggleJK()
if s:jk_func == 0
nmap j <Down>zz
nmap k <Up>zz
let s:jk_func = 1
hi StatusLine guifg=darkcyan ctermbg=11
else
unmap j
unmap k
let s:jk_func = 0
hi StatusLine guifg=blue ctermbg=15
endif
endfunction
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Enhanced code navigation & source code management
nmap ,. :cs find s <cword><cr>
nmap ,, <C-]>
nmap ,m <C-T>
nmap ,j <C-^>
nmap ,a :lv <cword> **/*.[chsCHS]<cr>
" Update settings affected by the location of source code root directory
function UpdateSetting(path)
execute 'set path=**,' . a:path . '/**'
execute 'nmap ,a :lv <cword> ' . a:path . '/**/*.[chsCHS]<cr>'
endfunction
" To decide location of the source code root directory
let g:src_root = "."
call UpdateSetting(g:src_root)
" Rebuild environment according to source code root directory
command -nargs=1 SrcInit call SrcInit(<f-args>)
function SrcInit(str)
let g:src_root = a:str
" let g:src_root = resolve(getcwd() . "\\" . a:str)
TagUpdate
call Warning("Now src_root: " . g:src_root)
endfunction
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Work directory type: git working copy or svn working copy
function CheckWorkDirType()
let str = system("git status")
let ret = stridx(str, "branch")
if ret != -1 | return "git" | endif
let str = system("svn info")
let ret = stridx(str, "UUID")
if ret != -1 | return "svn" | endif
return "dir"
endfunction
let g:wd_type = CheckWorkDirType()
function GetRevision()
if g:wd_type == "git"
let str = system("git log --pretty=%h")
return str[0:6]
elseif g:wd_type == "svn"
let str = system("svn info")
let i1 = stridx(str, "Revision")
let i2 = match(str, "\n", i1)
return str[i1+10 : i2-1]
else
return "0"
endif
endfunction
let g:wd_rev = GetRevision()
set statusline=%{g:wd_type}:%n>\ %<%f\ %h%m%r%w%y%=%-5B%-15.(%l/%L,\ %c%V%)\ %P
set titlestring=%F\ %=%{g:wd_type}:%{g:wd_rev}
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Function keys
"nmap <special> <F1> :make!<cr>
nmap <special> <F1> :lne<cr>
nmap <special> <F2> :lp<cr>
nmap <special> <F3> :lnewer \| lli<cr>
nmap <special> <F4> :lolder \| lli<cr>
nmap <special> <F5> :cn<cr>
nmap <special> <F6> :cp<cr>
nmap <special> <F7> :call ToggleView()<cr>
nmap <special> <F8> :TagUpdate<cr>
if g:wd_type == "git"
nmap <special> <F9> :call GitStatus()<cr>
nmap <special> <F10> :call GitLog()<CR>
nmap <special> <F11> :call GitLog(bufname(""))<CR>
nmap <special> <F12> :call GitDiff()<CR>
else
nmap <special> <F9> :call SvnStatus()<cr>
nmap <special> <F10> :call SvnLog()<CR>
nmap <special> <F11> :call SvnLog(bufname(""))<CR>
nmap <special> <F12> :call SvnDiff()<CR>
endif
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" For cscope
if has("cscope")
set csprg=cscope csto=0 cst nocsverb
if filereadable("cscope.out") " add any database in current directory
cs add cscope.out
elseif $CSCOPE_DB != "" " else add database pointed to by environment
cs add $CSCOPE_DB
endif
set csverb
endif
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Save a string into a temp file and view in vim
let s:file_tmp = tempname() . "-zt"
function ViewInTmpFile(str)
let alist = split(a:str, "\n")
call writefile(alist, s:file_tmp, 'b')
let cmd = "view +setlocal\\ nomodifiable " . s:file_tmp
execute cmd
endfunction
" Save a command output into a temp file and view in vim
function CmdInTmpFile(cmd)
let ret = system(a:cmd)
call ViewInTmpFile(ret)
redraw
echo "Command: " . a:cmd
endfunction
function Warning(msg)
echohl LineNr | echo a:msg | echohl None
endfunction
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Enhanced search
set updatetime=800
hi MatchParen guifg=white guibg=grey40
hi xx1 guibg=blue guifg=lightyellow
hi xx2 guibg=darkgreen guifg=lightyellow
hi xx3 guibg=orange guifg=lightyellow
" Highlight the word under cursor
nmap ,8 :autocmd! CursorHold<cr>
nmap ,9 :autocmd! CursorHold * nested silent! call CursorWord()<cr>
nmap <space>7 :call matchadd("xx3", '\c\<' . expand('<cword>') . '\>', 11, 4)<cr>
nmap <space>8 :exe '2match xx1 /\c\<' . expand('<cword>') . '\>/'<cr>
nmap <space>9 :exe 'match xx2 /\c\<' . expand('<cword>') . '\>/'<cr>
nmap <space>0 :exe 'call clearmatches() \| nohlsearch'<cr>
let s:cursor_match_id = 0
function CursorWord()
call matchdelete(s:cursor_match_id)
let s:cursor_match_id = matchadd("MatchParen", '\c\<' . expand('<cword>') . '\>', -1)
endfunction
command -nargs=* -complete=tag_listfiles Vvim call Vvim(<f-args>)
function Vvim(...)
let searchpath = g:src_root . '/**/*.[chsCHS]'
if a:0 == 0
let pattern = expand("<cword>")
let cmd = "lv /" . pattern . "/ " . expand("%")
call matchadd("Pmenu", '\c' . pattern)
elseif a:0 == 1
let cmd = "lv /" . a:1 . "/ " . searchpath
call matchadd("Pmenu", '\c' . a:1)
elseif a:0 == 2
let ptn1 = a:1 . '.*' . a:2
let ptn2 = a:2 . '.*' . a:1
let pattern = '\(' . ptn1 . '\)\|\(' . ptn2 . '\)'
let cmd = "lv /" . pattern . "/ " . searchpath
call matchadd("Pmenu", '\c' . a:1)
call matchadd("Todo", '\c' . a:2)
else
let cmd = "lv /" . a:1 . "/ " . expand("%")
call matchadd("Pmenu", '\c' . a:1)
endif
try
echo cmd
execute cmd
catch /^Vim(lvimgrep)/
call Warning("vim: No match")
return
endtry
endfunction
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Git operations
"let s:gitlogcmd="git log --pretty=\"%h: %Cgreen%s %C(cyan)<%an>%Creset <%ai>\" "
"let s:gitlogcmd="git log --pretty=\"%h: %s <%an> <%ai>\" "
let s:gitlogcmd="git log --encoding=cp936 --decorate -- "
let s:diffmode = 0 " diffmode: 0 for whole source code tree, 1 for current file only.
command -nargs=1 Git call Git(<f-args>)
function Git(str)
let gitprg = "\"c:\\program files\\git\\bin\\git.exe\" " . a:str
echo system(gitprg)
endfunction
function GitDiff()
if s:diffmode == 0
call CmdInTmpFile("git diff -- " . g:src_root)
else
call CmdInTmpFile("git diff -- " . bufname(""))
call Warning("Diff mode: " . s:diffmode)
endif
set filetype=diff
endfunction
function GitStatus()
let g:wd_rev = GetRevision()
echo "Work dir:" getcwd()
echo "Src root:" g:src_root
echo "Git rev :" g:wd_rev
echo "Git branches:"
Git branch
echo "======================================="
Git status
endfunction
function GitLog(...)
if a:0 == 0
let s:diffmode = 0
call CmdInTmpFile(s:gitlogcmd . g:src_root)
else
let s:diffmode = 1
call CmdInTmpFile(s:gitlogcmd . a:1)
endif
endfunction
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Svn operations
function Svn(str)
let svnprg = "\"c:\\program files\\TortoiseSvn\\bin\\svn.exe\" " . a:str
let ret = system(svnprg)
let xx = split(ret, "\n")
let yy = sort(xx)
let zz = join(yy, "\n")
echo zz
endfunction
function SvnDiff()
if s:diffmode == 0
call CmdInTmpFile("svn diff " . g:src_root)
else
call CmdInTmpFile("svn diff " . bufname(""))
call Warning("Diff mode: " . s:diffmode)
endif
set filetype=diff
endfunction
function SvnStatus()
let g:wd_rev = GetRevision()
echo "Work dir:" getcwd()
echo "Src root:" g:src_root
echo "Svn rev :" g:wd_rev
call Svn("status " . g:src_root)
echo "======================================="
call Svn("info")
endfunction
function SvnLog(...)
if a:0 == 0
let s:diffmode = 0
call CmdInTmpFile("svn log " . g:src_root)
else
let s:diffmode = 1
call CmdInTmpFile("svn log " . a:1)
endif
endfunction
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" View patches according to svn revision number & git hash value
command -nargs=0 ViewRevision call ViewRevision(expand("<cword>"))
function ViewRevision(rev)
if g:wd_type == "git"
call CmdInTmpFile("git show " . a:rev)
elseif g:wd_type == "svn"
call CmdInTmpFile("svn diff -c " . a:rev)
else
call Warning("No source control")
endif
endfunction
" View blame result of current file
command -nargs=0 Blame call Blame(expand("%"))
function Blame(filename)
let pos = getpos(".")
if g:wd_type == "git"
call CmdInTmpFile("git blame " . a:filename)
elseif g:wd_type == "svn"
call CmdInTmpFile("svn blame " . a:filename)
else
call Warning("No source control")
return
endif
call setpos(".", pos)
endfunction
" Git commit in vim
command -nargs=0 Gitcommit call Gitcommit()
function Gitcommit()
echo system("git diff --stat")
let msg = input("Commit msg: ", "", "tag")
if strlen(msg) == 0
echo "Commit cannelled"
return
endif
let cmd = "git commit -a -m \"" . msg . "\""
echo cmd
echo system(cmd)
let g:wd_rev = GetRevision()
endfunction
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" tags and cscope database
let s:tagfile = tempname() . "-ztg"
let s:csfile = tempname() . "-zcs"
function RemoveFile(filename)
if filereadable(a:filename)
let ret = delete(a:filename)
if ret == 0
echo a:filename . " removed."
else
call Warning("Fail to remove " . a:filename)
endif
endif
endfunction
" Function BuildTag(): Update tags and cscope database
command -nargs=0 TagUpdate call BuildTag(g:src_root)
function BuildTag(path)
if cscope_connection()==1
cs kill -1
endif
call RemoveFile(s:tagfile)
call RemoveFile(s:csfile)
update
" Create tag file
let cmd = "ctags -R --tag-relative=yes --extra=+q --fields=+ias "
let cmd = cmd . "-f " . s:tagfile . " " . a:path
echo cmd
echo system(cmd)
if filereadable(s:tagfile)
let cmd = "set tags=" . s:tagfile
execute cmd
echo "** Ctags updated **"
else
call Warning("** Failed to update ctags **")
endif
" Create CS database
let cmd = "cscope -Rb -v -s " . a:path . " -f " . s:csfile
echo cmd
echo system(cmd)
if filereadable(s:csfile)
let cmd = "cs add " . s:csfile
execute cmd
echo "** CS updated **"
else
call Warning("** Failed to update CS **")
endif
call UpdateSetting(a:path)
endfunction
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Cleanup before vim exit
autocmd VimLeavePre * call CleanupStuff()
function CleanupStuff()
if cscope_connection() == 1
cs kill -1
endif
call RemoveFile(s:tagfile)
call RemoveFile(s:csfile)
call RemoveFile(s:file_tmp)
endfunction
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Coding style: Format C code with your favorite style, format a whole file.
command -nargs=0 Indentlzw call Indentlzw()
function Indentlzw()
let fcur = bufname("")
let cmd = "indent -gnu -npsl -ts4 -i4 -bli0 -npcs -nut -fc1 -ncs -st " . fcur
call CmdInTmpFile(cmd)
set filetype=c
endfunction
" Another style, change current file directly, no backup.
command -nargs=0 Indentlzwex call Indentlzwex()
function Indentlzwex()
let fcur = bufname("")
let cmd = "indent -gnu -npsl -ts4 -i4 -bli0 -npcs -nut -fc1 -ncs " . fcur
setlocal autoread
echo system(cmd)
execute "checktime " . fcur
redraw
set autoread<
endfunction
" Format selected code only.
command -range -nargs=0 Rind <line1>,<line2>call Indentrange()
function Indentrange() range
let src = getline(a:firstline, a:lastline)
let ftmp = tempname() . "-zi"
call writefile(src, ftmp, 'b')
let cmd = "indent -gnu -npsl -ts4 -i4 -bli0 -npcs -nut -fc1 -ncs " . ftmp
let result = system(cmd)
execute a:firstline . "," . a:lastline . "d"
execute a:firstline-1 . "read " . ftmp
redraw
call RemoveFile(ftmp)
endfunction
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Display time
command -nargs=0 Timex call Timex()
function Timex()
echo strftime("%Y-%m-%d %H:%M:%S (%W%w)")
endfunction
" Online translation
command -nargs=1 Dict call Dict(<f-args>)
function Dict(word)
let ftmp = tempname() . "-zd-" . a:word
let cmd = "wget -O " . ftmp . " http://dict.baidu.com/s?wd=" . a:word
echo system(cmd)
execute "vi " . ftmp
endfunction
" Online manpage
command -nargs=+ Manp call Manp(<f-args>)
function Manp(word, sec)
let cachedir = "e:\\mydoc\\manpage\\"
let fname = cachedir . a:word . "." . a:sec . ".man"
if filereadable(fname)
echo "file ready."
else
if a:sec == "" | let a:sec = "all" | endif
let website = " \"http://man.he.net/?topic="
let cmd = "wget -O " . fname . website . a:word . "§ion=" . a:sec . "\""
echo system(cmd)
endif
execute "vi " . fname
setlocal filetype=man nomodifiable readonly
endfunction
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Program build
let s:buildprg='!make '
if filereadable("build.bat") | let s:buildprg='!build.bat ' | endif
if filereadable("a.bat") | let s:buildprg='!a.bat ' | endif
command -nargs=? Build call Build(<q-args>)
function Build(arg)
exe s:buildprg . a:arg . ' 2>&1| tee ' . s:file_tmp
exe 'cf ' . s:file_tmp
endfunction
command -nargs=? -complete=file Cppcheck call Cppcheck(<f-args>)
function Cppcheck(arg)
exe '!cppcheck.exe --enable=all --template=gcc ' . a:arg . ' 2>&1| tee ' . s:file_tmp
exe 'cf ' . s:file_tmp
endfunction
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Temporary settings
filetype plugin on
set completeopt=menuone
nmap ,t :tag Main_Code
nmap ,r :tag ProcFunc
nmap ,n :exec "tag " . expand("<cword>") . "_Proxy"<cr>
command -nargs=0 Gsh !"C:\Program Files\Git\bin\sh.exe" --login -i
command -nargs=0 Cmd exe '!start C:\tools\console2\console.exe -d ' . expand('%:p:h')
command -nargs=? Bbb exe '!git commit -a -m "v: ' . <q-args> . '"'
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"当新建 .h .c .hpp .cpp 等文件时自动调用SetTitle 函数
autocmd BufNewFile *.[ch],*.hpp,*.cpp exec ":call SetTitle()"
"" 加入注释
func SetComment()
call setline(1,"/*==================================")
call append(line("."), "* Copyright (C) ".strftime("%Y")." All rights reserved.")
call append(line(".")+1, "* ")
call append(line(".")+2, "* 文件名称:".expand("%:t"))
call append(line(".")+3, "* 创 建 者:herb")
call append(line(".")+4, "* 创建日期:".strftime("%Y年%m月%d日"))
call append(line(".")+5, "* 描 述:")
call append(line(".")+6, "*")
call append(line(".")+7, "================================================================*/")
endfunc
"" 定义函数SetTitle,自动插入文件头
func SetTitle()
call SetComment()
if expand("%:e") == 'hpp'
call append(line(".")+8, "#ifndef _".toupper(expand("%:t:r"))."_H")
call append(line(".")+9, "#define _".toupper(expand("%:t:r"))."_H")
call append(line(".")+10, "#ifdef __cplusplus")
call append(line(".")+11, "extern \"C\"")
call append(line(".")+12, "{")
call append(line(".")+13, "#endif")
call append(line(".")+14, "")
call append(line(".")+15, "#ifdef __cplusplus")
call append(line(".")+16, "}")
call append(line(".")+17, "#endif")
call append(line(".")+18, "#endif //".toupper(expand("%:t:r"))."_H")
elseif expand("%:e") == 'h'
call append(line(".")+8, "#pragma once")
elseif &filetype == 'c'
call append(line(".")+8,"#include \"".expand("%:t:r").".h\"")
elseif &filetype == 'cpp'
call append(line(".")+8, "#include \"".expand("%:t:r").".h\"")
endif
endfunc