gitgutter.vim 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. scriptencoding utf-8
  2. if exists('g:loaded_gitgutter') || !executable('git') || !has('signs') || &cp
  3. finish
  4. endif
  5. let g:loaded_gitgutter = 1
  6. " Initialisation {{{
  7. " Realtime sign updates require Vim 7.3.105+.
  8. if v:version < 703 || (v:version == 703 && !has("patch105"))
  9. let g:gitgutter_realtime = 0
  10. endif
  11. " Eager updates require gettabvar()/settabvar().
  12. if !exists("*gettabvar")
  13. let g:gitgutter_eager = 0
  14. endif
  15. function! s:set(var, default)
  16. if !exists(a:var)
  17. if type(a:default)
  18. execute 'let' a:var '=' string(a:default)
  19. else
  20. execute 'let' a:var '=' a:default
  21. endif
  22. endif
  23. endfunction
  24. call s:set('g:gitgutter_enabled', 1)
  25. call s:set('g:gitgutter_max_signs', 500)
  26. call s:set('g:gitgutter_signs', 1)
  27. call s:set('g:gitgutter_highlight_lines', 0)
  28. call s:set('g:gitgutter_sign_column_always', 0)
  29. call s:set('g:gitgutter_override_sign_column_highlight', 1)
  30. call s:set('g:gitgutter_realtime', 1)
  31. call s:set('g:gitgutter_eager', 1)
  32. call s:set('g:gitgutter_sign_added', '+')
  33. call s:set('g:gitgutter_sign_modified', '~')
  34. call s:set('g:gitgutter_sign_removed', '_')
  35. try
  36. call s:set('g:gitgutter_sign_removed_first_line', '‾')
  37. catch /E239/
  38. let g:gitgutter_sign_removed_first_line = '_^'
  39. endtry
  40. call s:set('g:gitgutter_sign_modified_removed', '~_')
  41. call s:set('g:gitgutter_diff_args', '')
  42. call s:set('g:gitgutter_escape_grep', 0)
  43. call s:set('g:gitgutter_map_keys', 1)
  44. call s:set('g:gitgutter_avoid_cmd_prompt_on_windows', 1)
  45. call gitgutter#highlight#define_sign_column_highlight()
  46. call gitgutter#highlight#define_highlights()
  47. call gitgutter#highlight#define_signs()
  48. " }}}
  49. " Primary functions {{{
  50. command -bar GitGutterAll call gitgutter#all()
  51. command -bar GitGutter call gitgutter#process_buffer(bufnr(''), 0)
  52. command -bar GitGutterDisable call gitgutter#disable()
  53. command -bar GitGutterEnable call gitgutter#enable()
  54. command -bar GitGutterToggle call gitgutter#toggle()
  55. " }}}
  56. " Line highlights {{{
  57. command -bar GitGutterLineHighlightsDisable call gitgutter#line_highlights_disable()
  58. command -bar GitGutterLineHighlightsEnable call gitgutter#line_highlights_enable()
  59. command -bar GitGutterLineHighlightsToggle call gitgutter#line_highlights_toggle()
  60. " }}}
  61. " Signs {{{
  62. command -bar GitGutterSignsEnable call gitgutter#signs_enable()
  63. command -bar GitGutterSignsDisable call gitgutter#signs_disable()
  64. command -bar GitGutterSignsToggle call gitgutter#signs_toggle()
  65. " }}}
  66. " Hunks {{{
  67. command -bar -count=1 GitGutterNextHunk call gitgutter#hunk#next_hunk(<count>)
  68. command -bar -count=1 GitGutterPrevHunk call gitgutter#hunk#prev_hunk(<count>)
  69. command -bar GitGutterStageHunk call gitgutter#stage_hunk()
  70. command -bar GitGutterRevertHunk call gitgutter#revert_hunk()
  71. command -bar GitGutterPreviewHunk call gitgutter#preview_hunk()
  72. " Returns the git-diff hunks for the file or an empty list if there
  73. " aren't any hunks.
  74. "
  75. " The return value is a list of lists. There is one inner list per hunk.
  76. "
  77. " [
  78. " [from_line, from_count, to_line, to_count],
  79. " [from_line, from_count, to_line, to_count],
  80. " ...
  81. " ]
  82. "
  83. " where:
  84. "
  85. " `from` - refers to the staged file
  86. " `to` - refers to the working tree's file
  87. " `line` - refers to the line number where the change starts
  88. " `count` - refers to the number of lines the change covers
  89. function! GitGutterGetHunks()
  90. return gitgutter#utility#is_active() ? gitgutter#hunk#hunks() : []
  91. endfunction
  92. " Returns an array that contains a summary of the current hunk status.
  93. " The format is [ added, modified, removed ], where each value represents
  94. " the number of lines added/modified/removed respectively.
  95. function! GitGutterGetHunkSummary()
  96. return gitgutter#hunk#summary()
  97. endfunction
  98. " }}}
  99. command -bar GitGutterDebug call gitgutter#debug#debug()
  100. " Maps {{{
  101. nnoremap <silent> <expr> <Plug>GitGutterNextHunk &diff ? ']c' : ":\<C-U>execute v:count1 . 'GitGutterNextHunk'\<CR>"
  102. nnoremap <silent> <expr> <Plug>GitGutterPrevHunk &diff ? '[c' : ":\<C-U>execute v:count1 . 'GitGutterPrevHunk'\<CR>"
  103. if g:gitgutter_map_keys
  104. if !hasmapto('<Plug>GitGutterPrevHunk') && maparg('[c', 'n') ==# ''
  105. nmap [c <Plug>GitGutterPrevHunk
  106. endif
  107. if !hasmapto('<Plug>GitGutterNextHunk') && maparg(']c', 'n') ==# ''
  108. nmap ]c <Plug>GitGutterNextHunk
  109. endif
  110. endif
  111. nnoremap <silent> <Plug>GitGutterStageHunk :GitGutterStageHunk<CR>
  112. nnoremap <silent> <Plug>GitGutterRevertHunk :GitGutterRevertHunk<CR>
  113. nnoremap <silent> <Plug>GitGutterPreviewHunk :GitGutterPreviewHunk<CR>
  114. if g:gitgutter_map_keys
  115. if !hasmapto('<Plug>GitGutterStageHunk') && maparg('<Leader>hs', 'n') ==# ''
  116. nmap <Leader>hs <Plug>GitGutterStageHunk
  117. endif
  118. if !hasmapto('<Plug>GitGutterRevertHunk') && maparg('<Leader>hr', 'n') ==# ''
  119. nmap <Leader>hr <Plug>GitGutterRevertHunk
  120. endif
  121. if !hasmapto('<Plug>GitGutterPreviewHunk') && maparg('<Leader>hp', 'n') ==# ''
  122. nmap <Leader>hp <Plug>GitGutterPreviewHunk
  123. endif
  124. endif
  125. " }}}
  126. " Autocommands {{{
  127. augroup gitgutter
  128. autocmd!
  129. if g:gitgutter_realtime
  130. autocmd CursorHold,CursorHoldI * call gitgutter#process_buffer(bufnr(''), 1)
  131. endif
  132. if g:gitgutter_eager
  133. autocmd BufEnter,BufWritePost,FileChangedShellPost *
  134. \ if gettabvar(tabpagenr(), 'gitgutter_didtabenter') |
  135. \ call settabvar(tabpagenr(), 'gitgutter_didtabenter', 0) |
  136. \ else |
  137. \ call gitgutter#process_buffer(bufnr(''), 0) |
  138. \ endif
  139. autocmd TabEnter *
  140. \ call settabvar(tabpagenr(), 'gitgutter_didtabenter', 1) |
  141. \ call gitgutter#all()
  142. if !has('gui_win32')
  143. autocmd FocusGained * call gitgutter#all()
  144. endif
  145. else
  146. autocmd BufRead,BufWritePost,FileChangedShellPost * call gitgutter#process_buffer(bufnr(''), 0)
  147. endif
  148. autocmd ColorScheme * call gitgutter#highlight#define_sign_column_highlight() | call gitgutter#highlight#define_highlights()
  149. " Disable during :vimgrep
  150. autocmd QuickFixCmdPre *vimgrep* let g:gitgutter_enabled = 0
  151. autocmd QuickFixCmdPost *vimgrep* let g:gitgutter_enabled = 1
  152. augroup END
  153. " }}}
  154. " vim:set et sw=2 fdm=marker: