utility.vim 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. function! gitgutter#utility#supports_overscore_sign()
  2. if gitgutter#utility#windows()
  3. return &encoding ==? 'utf-8'
  4. else
  5. return &termencoding ==? &encoding || &termencoding == ''
  6. endif
  7. endfunction
  8. function! gitgutter#utility#setbufvar(buffer, varname, val)
  9. let buffer = +a:buffer
  10. " Default value for getbufvar() was introduced in Vim 7.3.831.
  11. let ggvars = getbufvar(buffer, 'gitgutter')
  12. if type(ggvars) == type('')
  13. unlet ggvars
  14. let ggvars = {}
  15. call setbufvar(buffer, 'gitgutter', ggvars)
  16. endif
  17. let ggvars[a:varname] = a:val
  18. endfunction
  19. function! gitgutter#utility#getbufvar(buffer, varname, ...)
  20. let ggvars = getbufvar(a:buffer, 'gitgutter')
  21. if type(ggvars) == type({}) && has_key(ggvars, a:varname)
  22. return ggvars[a:varname]
  23. endif
  24. if a:0
  25. return a:1
  26. endif
  27. endfunction
  28. function! gitgutter#utility#warn(message) abort
  29. echohl WarningMsg
  30. echo a:message
  31. echohl None
  32. let v:warningmsg = a:message
  33. endfunction
  34. function! gitgutter#utility#warn_once(bufnr, message, key) abort
  35. if empty(gitgutter#utility#getbufvar(a:bufnr, a:key))
  36. call gitgutter#utility#setbufvar(a:bufnr, a:key, '1')
  37. echohl WarningMsg
  38. redraw | echom a:message
  39. echohl None
  40. let v:warningmsg = a:message
  41. endif
  42. endfunction
  43. " Returns truthy when the buffer's file should be processed; and falsey when it shouldn't.
  44. " This function does not and should not make any system calls.
  45. function! gitgutter#utility#is_active(bufnr) abort
  46. return gitgutter#utility#getbufvar(a:bufnr, 'enabled') &&
  47. \ !pumvisible() &&
  48. \ s:is_file_buffer(a:bufnr) &&
  49. \ s:exists_file(a:bufnr) &&
  50. \ s:not_git_dir(a:bufnr)
  51. endfunction
  52. function! s:not_git_dir(bufnr) abort
  53. return s:dir(a:bufnr) !~ '[/\\]\.git\($\|[/\\]\)'
  54. endfunction
  55. function! s:is_file_buffer(bufnr) abort
  56. return empty(getbufvar(a:bufnr, '&buftype'))
  57. endfunction
  58. " From tpope/vim-fugitive
  59. function! s:winshell()
  60. return &shell =~? 'cmd' || exists('+shellslash') && !&shellslash
  61. endfunction
  62. " From tpope/vim-fugitive
  63. function! gitgutter#utility#shellescape(arg) abort
  64. if a:arg =~ '^[A-Za-z0-9_/.-]\+$'
  65. return a:arg
  66. elseif s:winshell()
  67. return '"' . substitute(substitute(a:arg, '"', '""', 'g'), '%', '"%"', 'g') . '"'
  68. else
  69. return shellescape(a:arg)
  70. endif
  71. endfunction
  72. function! gitgutter#utility#file(bufnr)
  73. return s:abs_path(a:bufnr, 1)
  74. endfunction
  75. " Not shellescaped
  76. function! gitgutter#utility#extension(bufnr) abort
  77. return fnamemodify(s:abs_path(a:bufnr, 0), ':e')
  78. endfunction
  79. function! gitgutter#utility#system(cmd, ...) abort
  80. call gitgutter#debug#log(a:cmd, a:000)
  81. call s:use_known_shell()
  82. silent let output = (a:0 == 0) ? system(a:cmd) : system(a:cmd, a:1)
  83. call s:restore_shell()
  84. return output
  85. endfunction
  86. function! gitgutter#utility#has_repo_path(bufnr)
  87. return index(['', -1, -2], gitgutter#utility#repo_path(a:bufnr, 0)) == -1
  88. endfunction
  89. " Path of file relative to repo root.
  90. "
  91. " * empty string - not set
  92. " * non-empty string - path
  93. " * -1 - pending
  94. " * -2 - not tracked by git
  95. " * -3 - assume unchanged
  96. function! gitgutter#utility#repo_path(bufnr, shellesc) abort
  97. let p = gitgutter#utility#getbufvar(a:bufnr, 'path', '')
  98. return a:shellesc ? gitgutter#utility#shellescape(p) : p
  99. endfunction
  100. let s:set_path_handler = {}
  101. function! s:set_path_handler.out(buffer, listing) abort
  102. let listing = s:strip_trailing_new_line(a:listing)
  103. let [status, path] = [listing[0], listing[2:]]
  104. if status =~# '[a-z]'
  105. call gitgutter#utility#setbufvar(a:buffer, 'path', -3)
  106. else
  107. call gitgutter#utility#setbufvar(a:buffer, 'path', path)
  108. endif
  109. if type(self.continuation) == type(function('tr'))
  110. call self.continuation()
  111. else
  112. call call(self.continuation.function, self.continuation.arguments)
  113. endif
  114. endfunction
  115. function! s:set_path_handler.err(buffer) abort
  116. call gitgutter#utility#setbufvar(a:buffer, 'path', -2)
  117. endfunction
  118. " continuation - a funcref or hash to call after setting the repo path asynchronously.
  119. "
  120. " Returns 'async' if the the path is set asynchronously, 0 otherwise.
  121. function! gitgutter#utility#set_repo_path(bufnr, continuation) abort
  122. " Values of path:
  123. " * non-empty string - path
  124. " * -1 - pending
  125. " * -2 - not tracked by git
  126. " * -3 - assume unchanged
  127. call gitgutter#utility#setbufvar(a:bufnr, 'path', -1)
  128. let cmd = gitgutter#utility#cd_cmd(a:bufnr,
  129. \ g:gitgutter_git_executable.' '.g:gitgutter_git_args.
  130. \ ' ls-files -v --error-unmatch --full-name -z -- '.
  131. \ gitgutter#utility#shellescape(s:filename(a:bufnr)))
  132. if g:gitgutter_async && gitgutter#async#available() && !has('vim_starting')
  133. let handler = copy(s:set_path_handler)
  134. let handler.continuation = a:continuation
  135. call gitgutter#async#execute(cmd, a:bufnr, handler)
  136. return 'async'
  137. endif
  138. let listing = gitgutter#utility#system(cmd)
  139. if v:shell_error
  140. call gitgutter#utility#setbufvar(a:bufnr, 'path', -2)
  141. return
  142. endif
  143. let listing = s:strip_trailing_new_line(listing)
  144. let [status, path] = [listing[0], listing[2:]]
  145. if status =~# '[a-z]'
  146. call gitgutter#utility#setbufvar(a:bufnr, 'path', -3)
  147. else
  148. call gitgutter#utility#setbufvar(a:bufnr, 'path', path)
  149. endif
  150. endfunction
  151. function! gitgutter#utility#cd_cmd(bufnr, cmd) abort
  152. let cd = s:unc_path(a:bufnr) ? 'pushd' : (gitgutter#utility#windows() && s:dos_shell() ? 'cd /d' : 'cd')
  153. return cd.' '.s:dir(a:bufnr).' && '.a:cmd
  154. endfunction
  155. function! s:unc_path(bufnr)
  156. return s:abs_path(a:bufnr, 0) =~ '^\\\\'
  157. endfunction
  158. function! s:dos_shell()
  159. return &shell == 'cmd.exe' || &shell == 'command.com'
  160. endfunction
  161. function! s:use_known_shell() abort
  162. if has('unix') && &shell !=# 'sh'
  163. let [s:shell, s:shellcmdflag, s:shellredir, s:shellpipe, s:shellquote, s:shellxquote] = [&shell, &shellcmdflag, &shellredir, &shellpipe, &shellquote, &shellxquote]
  164. let &shell = 'sh'
  165. set shellcmdflag=-c shellredir=>%s\ 2>&1
  166. endif
  167. if has('win32') && (&shell =~# 'pwsh' || &shell =~# 'powershell')
  168. let [s:shell, s:shellcmdflag, s:shellredir, s:shellpipe, s:shellquote, s:shellxquote] = [&shell, &shellcmdflag, &shellredir, &shellpipe, &shellquote, &shellxquote]
  169. let &shell = 'cmd.exe'
  170. set shellcmdflag=/s\ /c shellredir=>%s\ 2>&1 shellpipe=>%s\ 2>&1 shellquote= shellxquote="
  171. endif
  172. endfunction
  173. function! s:restore_shell() abort
  174. if (has('unix') || has('win32')) && exists('s:shell')
  175. let [&shell, &shellcmdflag, &shellredir, &shellpipe, &shellquote, &shellxquote] = [s:shell, s:shellcmdflag, s:shellredir, s:shellpipe, s:shellquote, s:shellxquote]
  176. endif
  177. endfunction
  178. function! gitgutter#utility#get_diff_base(bufnr)
  179. let p = resolve(expand('#'.a:bufnr.':p'))
  180. let ml = matchlist(p, '\v^fugitive:/.*/(\x{40,})/')
  181. if !empty(ml) && !empty(ml[1])
  182. return ml[1].'^'
  183. endif
  184. return g:gitgutter_diff_base
  185. endfunction
  186. function! s:abs_path(bufnr, shellesc)
  187. let p = resolve(expand('#'.a:bufnr.':p'))
  188. " Remove extra parts from fugitive's filepaths
  189. let p = substitute(substitute(p, '^fugitive:', '', ''), '\v\.git/\x{40,}/', '', '')
  190. return a:shellesc ? gitgutter#utility#shellescape(p) : p
  191. endfunction
  192. function! s:dir(bufnr) abort
  193. return gitgutter#utility#shellescape(fnamemodify(s:abs_path(a:bufnr, 0), ':h'))
  194. endfunction
  195. " Not shellescaped.
  196. function! s:filename(bufnr) abort
  197. return fnamemodify(s:abs_path(a:bufnr, 0), ':t')
  198. endfunction
  199. function! s:exists_file(bufnr) abort
  200. return filereadable(s:abs_path(a:bufnr, 0))
  201. endfunction
  202. " Get rid of any trailing new line or SOH character.
  203. "
  204. " git ls-files -z produces output with null line termination.
  205. " Vim's system() replaces any null characters in the output
  206. " with SOH (start of header), i.e. ^A.
  207. function! s:strip_trailing_new_line(line) abort
  208. return substitute(a:line, '[[:cntrl:]]$', '', '')
  209. endfunction
  210. function! gitgutter#utility#windows()
  211. return has('win64') || has('win32') || has('win16')
  212. endfunction