gitcommit.vim 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. " Vim filetype plugin
  2. " Language: git commit file
  3. " Maintainer: Tim Pope <vimNOSPAM@tpope.org>
  4. " Last Change: 2016 Aug 29
  5. " Only do this when not done yet for this buffer
  6. if (exists("b:did_ftplugin"))
  7. finish
  8. endif
  9. runtime! ftplugin/git.vim
  10. let b:did_ftplugin = 1
  11. setlocal comments=:# commentstring=#\ %s
  12. setlocal nomodeline tabstop=8 formatoptions+=tl textwidth=72
  13. setlocal formatoptions-=c formatoptions-=r formatoptions-=o formatoptions-=q
  14. let b:undo_ftplugin = 'setl modeline< tabstop< formatoptions< tw< com< cms<'
  15. if exists("g:no_gitcommit_commands") || v:version < 700
  16. finish
  17. endif
  18. if !exists("b:git_dir")
  19. let b:git_dir = expand("%:p:h")
  20. endif
  21. command! -bang -bar -buffer -complete=custom,s:diffcomplete -nargs=* DiffGitCached :call s:gitdiffcached(<bang>0,b:git_dir,<f-args>)
  22. let b:undo_ftplugin = b:undo_ftplugin . "|delc DiffGitCached"
  23. function! s:diffcomplete(A,L,P)
  24. let args = ""
  25. if a:P <= match(a:L." -- "," -- ")+3
  26. let args = args . "-p\n--stat\n--shortstat\n--summary\n--patch-with-stat\n--no-renames\n-B\n-M\n-C\n"
  27. end
  28. if exists("b:git_dir") && a:A !~ '^-'
  29. let tree = fnamemodify(b:git_dir,':h')
  30. if strpart(getcwd(),0,strlen(tree)) == tree
  31. let args = args."\n".system("git diff --cached --name-only")
  32. endif
  33. endif
  34. return args
  35. endfunction
  36. function! s:gitdiffcached(bang,gitdir,...)
  37. let tree = fnamemodify(a:gitdir,':h')
  38. let name = tempname()
  39. let git = "git"
  40. if strpart(getcwd(),0,strlen(tree)) != tree
  41. let git .= " --git-dir=".(exists("*shellescape") ? shellescape(a:gitdir) : '"'.a:gitdir.'"')
  42. endif
  43. if a:0
  44. let extra = join(map(copy(a:000),exists("*shellescape") ? 'shellescape(v:val)' : "'\"'.v:val.'\"'"))
  45. else
  46. let extra = "-p --stat=".&columns
  47. endif
  48. call system(git." diff --cached --no-color --no-ext-diff ".extra." > ".(exists("*shellescape") ? shellescape(name) : name))
  49. exe "pedit ".(exists("*fnameescape") ? fnameescape(name) : name)
  50. wincmd P
  51. let b:git_dir = a:gitdir
  52. command! -bang -bar -buffer -complete=custom,s:diffcomplete -nargs=* DiffGitCached :call s:gitdiffcached(<bang>0,b:git_dir,<f-args>)
  53. nnoremap <buffer> <silent> q :q<CR>
  54. setlocal buftype=nowrite nobuflisted noswapfile nomodifiable filetype=git
  55. endfunction