Update runtime files
[MacVim.git] / runtime / ftplugin / gitcommit.vim
blob9edb055650cf51fa694fbebc241fd397e205cc36
1 " Vim filetype plugin
2 " Language:     git config file
3 " Maintainer:   Tim Pope <vimNOSPAM@tpope.info>
4 " Last Change:  2007 Dec 16
6 " Only do this when not done yet for this buffer
7 if (exists("b:did_ftplugin"))
8   finish
9 endif
10 let b:did_ftplugin = 1
12 " allow gf to work even if in a subdirectory
13 let b:git_dir = expand("%:p:h")
14 let &l:path = fnamemodify(b:git_dir,':h').",".&l:path
15 let b:undo_ftplugin = "setl path<"
17 if &textwidth == 0
18   " make sure that log messages play nice with git-log on standard terminals
19   setlocal textwidth=72
20   let b:undo_ftplugin = b:undo_ftplugin . " tw<"
21 endif
23 if exists("g:no_gitcommit_commands")
24   finish
25 endif
27 " Automatically diffing can be done with:
28 "   autocmd FileType gitcommit DiffGitCached | wincmd p
29 command! -bang -bar -buffer -complete=custom,s:diffcomplete -nargs=* DiffGitCached :call s:gitdiffcached(<bang>0,b:git_dir,<f-args>)
31 function! s:diffcomplete(A,L,P)
32     let args = ""
33     let g:L = a:L
34     let g:P = a:P
35     if a:P <= match(a:L." -- "," -- ")+3
36         let args = args . "-p\n--stat\n--shortstat\n--summary\n--patch-with-stat\n--no-renames\n-B\n-M\n-C\n"
37     end
38     if exists("b:git_dir") && a:A !~ '^-'
39         let tree = fnamemodify(b:git_dir,':h')
40         if strpart(getcwd(),0,strlen(tree)) == tree
41             let args = args."\n".system("git diff --cached --name-only")
42         endif
43     endif
44     return args
45 endfunction
47 function! s:gitdiffcached(bang,gitdir,...)
48     let tree = fnamemodify(a:gitdir,':h')
49     let name = tempname()
50     let prefix = ""
51     if strpart(getcwd(),0,strlen(tree)) != tree
52         if has("win32")
53             let oldgit = $GIT_DIR
54             let $GIT_DIR = a:gitdir
55         else
56             " Can't unset an env var, so use shell syntax instead
57             let prefix = 'GIT_DIR='.shellescape(a:gitdir).' '
58         endif
59     endif
60     if a:0
61         let extra = join(map(copy(a:000),has("*shellescape") ? 'shellescape(v:val)' : "'\"'.v:val.'\"'"))
62     else
63         let extra = "-p --stat=".&columns
64     endif
65     call system(prefix."git diff --cached --no-color ".extra." > ".name)
66     if exists("l:oldgit")
67         let $GIT_DIR = oldgit
68     endif
69     exe "pedit ".name
70     wincmd P
71     let b:git_dir = a:gitdir
72     command! -bang -bar -buffer -complete=custom,s:diffcomplete -nargs=* DiffGitCached :call s:gitdiffcached(<bang>0,b:git_dir,<f-args>)
73     nnoremap <silent> q :q<CR>
74     set buftype=nowrite nobuflisted noswapfile nomodifiable
75     set filetype=diff includeexpr=substitute(v:fname,'^[ab]/','','')
76     if strpart(&l:path,0,strlen(tree)) != tree
77         let &l:path = tree.','.&l:path
78     endif
79 endfunction