PageRenderTime 26ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/autoload/gundo.vim

https://github.com/isa/vim-gundo
Vim Script | 434 lines | 348 code | 57 blank | 29 comment | 49 complexity | fadc7be082993aabaafb0a3d62681a3d MD5 | raw file
  1. " ============================================================================
  2. " File: gundo.vim
  3. " Description: vim global plugin to visualize your undo tree
  4. " Maintainer: Steve Losh <steve@stevelosh.com>
  5. " License: GPLv2+ -- look it up.
  6. " Notes: Much of this code was thiefed from Mercurial, and the rest was
  7. " heavily inspired by scratch.vim and histwin.vim.
  8. "
  9. " ============================================================================
  10. "{{{ Init
  11. if v:version < '703'"{{{
  12. function! s:GundoDidNotLoad()
  13. echohl WarningMsg|echomsg "Gundo unavailable: requires Vim 7.3+"|echohl None
  14. endfunction
  15. command! -nargs=0 GundoToggle call s:GundoDidNotLoad()
  16. finish
  17. endif"}}}
  18. if has('python3')"{{{
  19. let s:has_supported_python = 2
  20. elseif has('python')
  21. let s:has_supported_python = 1
  22. else
  23. let s:has_supported_python = 0
  24. endif
  25. if !s:has_supported_python
  26. function! s:GundoDidNotLoad()
  27. echohl WarningMsg|echomsg "Gundo requires Vim to be compiled with Python 2.4+"|echohl None
  28. endfunction
  29. command! -nargs=0 GundoToggle call s:GundoDidNotLoad()
  30. finish
  31. endif"}}}
  32. let s:plugin_path = escape(expand('<sfile>:p:h'), '\')
  33. if !exists('g:gundo_width')"{{{
  34. let g:gundo_width = 45
  35. endif"}}}
  36. if !exists('g:gundo_preview_height')"{{{
  37. let g:gundo_preview_height = 15
  38. endif"}}}
  39. if !exists('g:gundo_preview_bottom')"{{{
  40. let g:gundo_preview_bottom = 0
  41. endif"}}}
  42. if !exists('g:gundo_right')"{{{
  43. let g:gundo_right = 0
  44. endif"}}}
  45. if !exists('g:gundo_help')"{{{
  46. let g:gundo_help = 1
  47. endif"}}}
  48. if !exists("g:gundo_map_move_older")"{{{
  49. let g:gundo_map_move_older = 'j'
  50. endif"}}}
  51. if !exists("g:gundo_map_move_newer")"{{{
  52. let g:gundo_map_move_newer = 'k'
  53. endif"}}}
  54. if !exists("g:gundo_close_on_revert")"{{{
  55. let g:gundo_close_on_revert = 0
  56. endif"}}}
  57. "}}}
  58. "{{{ Gundo utility functions
  59. function! s:GundoGetTargetState()"{{{
  60. let target_line = matchstr(getline("."), '\v\[[0-9]+\]')
  61. return matchstr(target_line, '\v[0-9]+')
  62. endfunction"}}}
  63. function! s:GundoGoToWindowForBufferName(name)"{{{
  64. if bufwinnr(bufnr(a:name)) != -1
  65. exe bufwinnr(bufnr(a:name)) . "wincmd w"
  66. return 1
  67. else
  68. return 0
  69. endif
  70. endfunction"}}}
  71. function! s:GundoIsVisible()"{{{
  72. if bufwinnr(bufnr("__Gundo__")) != -1 || bufwinnr(bufnr("__Gundo_Preview__")) != -1
  73. return 1
  74. else
  75. return 0
  76. endif
  77. endfunction"}}}
  78. function! s:GundoInlineHelpLength()"{{{
  79. if g:gundo_help
  80. return 6
  81. else
  82. return 0
  83. endif
  84. endfunction"}}}
  85. "}}}
  86. "{{{ Gundo buffer settings
  87. function! s:GundoMapGraph()"{{{
  88. exec 'nnoremap <script> <silent> <buffer> ' . g:gundo_map_move_older . " :call <sid>GundoMove(1)<CR>"
  89. exec 'nnoremap <script> <silent> <buffer> ' . g:gundo_map_move_newer . " :call <sid>GundoMove(-1)<CR>"
  90. nnoremap <script> <silent> <buffer> <CR> :call <sid>GundoRevert()<CR>
  91. nnoremap <script> <silent> <buffer> o :call <sid>GundoRevert()<CR>
  92. nnoremap <script> <silent> <buffer> <down> :call <sid>GundoMove(1)<CR>
  93. nnoremap <script> <silent> <buffer> <up> :call <sid>GundoMove(-1)<CR>
  94. nnoremap <script> <silent> <buffer> gg gg:call <sid>GundoMove(1)<CR>
  95. nnoremap <script> <silent> <buffer> P :call <sid>GundoPlayTo()<CR>
  96. nnoremap <script> <silent> <buffer> p :call <sid>GundoRenderChangePreview()<CR>
  97. nnoremap <script> <silent> <buffer> q :call <sid>GundoClose()<CR>
  98. cabbrev <script> <silent> <buffer> q call <sid>GundoClose()
  99. cabbrev <script> <silent> <buffer> quit call <sid>GundoClose()
  100. nnoremap <script> <silent> <buffer> <2-LeftMouse> :call <sid>GundoMouseDoubleClick()<CR>
  101. endfunction"}}}
  102. function! s:GundoMapPreview()"{{{
  103. nnoremap <script> <silent> <buffer> q :call <sid>GundoClose()<CR>
  104. cabbrev <script> <silent> <buffer> q call <sid>GundoClose()
  105. cabbrev <script> <silent> <buffer> quit call <sid>GundoClose()
  106. endfunction"}}}
  107. function! s:GundoSettingsGraph()"{{{
  108. setlocal buftype=nofile
  109. setlocal bufhidden=hide
  110. setlocal noswapfile
  111. setlocal nobuflisted
  112. setlocal nomodifiable
  113. setlocal filetype=gundo
  114. setlocal nolist
  115. setlocal nonumber
  116. setlocal norelativenumber
  117. setlocal nowrap
  118. call s:GundoSyntaxGraph()
  119. call s:GundoMapGraph()
  120. endfunction"}}}
  121. function! s:GundoSettingsPreview()"{{{
  122. setlocal buftype=nofile
  123. setlocal bufhidden=hide
  124. setlocal noswapfile
  125. setlocal nobuflisted
  126. setlocal nomodifiable
  127. setlocal filetype=diff
  128. setlocal nonumber
  129. setlocal norelativenumber
  130. setlocal nowrap
  131. setlocal foldlevel=20
  132. setlocal foldmethod=diff
  133. call s:GundoMapPreview()
  134. endfunction"}}}
  135. function! s:GundoSyntaxGraph()"{{{
  136. let b:current_syntax = 'gundo'
  137. syn match GundoCurrentLocation '@'
  138. syn match GundoHelp '\v^".*$'
  139. syn match GundoNumberField '\v\[[0-9]+\]'
  140. syn match GundoNumber '\v[0-9]+' contained containedin=GundoNumberField
  141. hi def link GundoCurrentLocation Keyword
  142. hi def link GundoHelp Comment
  143. hi def link GundoNumberField Comment
  144. hi def link GundoNumber Identifier
  145. endfunction"}}}
  146. "}}}
  147. "{{{ Gundo buffer/window management
  148. function! s:GundoResizeBuffers(backto)"{{{
  149. call s:GundoGoToWindowForBufferName('__Gundo__')
  150. exe "vertical resize " . g:gundo_width
  151. call s:GundoGoToWindowForBufferName('__Gundo_Preview__')
  152. exe "resize " . g:gundo_preview_height
  153. exe a:backto . "wincmd w"
  154. endfunction"}}}
  155. function! s:GundoOpenGraph()"{{{
  156. let existing_gundo_buffer = bufnr("__Gundo__")
  157. if existing_gundo_buffer == -1
  158. call s:GundoGoToWindowForBufferName('__Gundo_Preview__')
  159. exe "new __Gundo__"
  160. if g:gundo_preview_bottom
  161. if g:gundo_right
  162. wincmd L
  163. else
  164. wincmd H
  165. endif
  166. endif
  167. call s:GundoResizeBuffers(winnr())
  168. else
  169. let existing_gundo_window = bufwinnr(existing_gundo_buffer)
  170. if existing_gundo_window != -1
  171. if winnr() != existing_gundo_window
  172. exe existing_gundo_window . "wincmd w"
  173. endif
  174. else
  175. call s:GundoGoToWindowForBufferName('__Gundo_Preview__')
  176. if g:gundo_preview_bottom
  177. if g:gundo_right
  178. exe "botright vsplit +buffer" . existing_gundo_buffer
  179. else
  180. exe "topleft vsplit +buffer" . existing_gundo_buffer
  181. endif
  182. else
  183. exe "split +buffer" . existing_gundo_buffer
  184. endif
  185. call s:GundoResizeBuffers(winnr())
  186. endif
  187. endif
  188. endfunction"}}}
  189. function! s:GundoOpenPreview()"{{{
  190. let existing_preview_buffer = bufnr("__Gundo_Preview__")
  191. if existing_preview_buffer == -1
  192. if g:gundo_preview_bottom
  193. exe "botright new __Gundo_Preview__"
  194. else
  195. if g:gundo_right
  196. exe "botright vnew __Gundo_Preview__"
  197. else
  198. exe "topleft vnew __Gundo_Preview__"
  199. endif
  200. endif
  201. else
  202. let existing_preview_window = bufwinnr(existing_preview_buffer)
  203. if existing_preview_window != -1
  204. if winnr() != existing_preview_window
  205. exe existing_preview_window . "wincmd w"
  206. endif
  207. else
  208. if g:gundo_preview_bottom
  209. exe "botright split +buffer" . existing_preview_buffer
  210. else
  211. if g:gundo_right
  212. exe "botright vsplit +buffer" . existing_preview_buffer
  213. else
  214. exe "topleft vsplit +buffer" . existing_preview_buffer
  215. endif
  216. endif
  217. endif
  218. endif
  219. endfunction"}}}
  220. function! s:GundoClose()"{{{
  221. if s:GundoGoToWindowForBufferName('__Gundo__')
  222. quit
  223. endif
  224. if s:GundoGoToWindowForBufferName('__Gundo_Preview__')
  225. quit
  226. endif
  227. exe bufwinnr(g:gundo_target_n) . "wincmd w"
  228. endfunction"}}}
  229. function! s:GundoOpen()"{{{
  230. if !exists('g:gundo_py_loaded')
  231. if s:has_supported_python == 2
  232. exe 'py3file ' . s:plugin_path . '/gundo.py'
  233. python3 initPythonModule()
  234. else
  235. exe 'pyfile ' . s:plugin_path . '/gundo.py'
  236. python initPythonModule()
  237. endif
  238. if !s:has_supported_python
  239. function! s:GundoDidNotLoad()
  240. echohl WarningMsg|echomsg "Gundo unavailable: requires Vim 7.3+"|echohl None
  241. endfunction
  242. command! -nargs=0 GundoToggle call s:GundoDidNotLoad()
  243. call s:GundoDidNotLoad()
  244. return
  245. endif"
  246. let g:gundo_py_loaded = 1
  247. endif
  248. " Save `splitbelow` value and set it to default to avoid problems with
  249. " positioning new windows.
  250. let saved_splitbelow = &splitbelow
  251. let &splitbelow = 0
  252. call s:GundoOpenPreview()
  253. exe bufwinnr(g:gundo_target_n) . "wincmd w"
  254. call s:GundoRenderGraph()
  255. call s:GundoRenderPreview()
  256. " Restore `splitbelow` value.
  257. let &splitbelow = saved_splitbelow
  258. endfunction"}}}
  259. function! s:GundoToggle()"{{{
  260. if s:GundoIsVisible()
  261. call s:GundoClose()
  262. else
  263. let g:gundo_target_n = bufnr('')
  264. let g:gundo_target_f = @%
  265. call s:GundoOpen()
  266. endif
  267. endfunction"}}}
  268. "}}}
  269. "{{{ Gundo mouse handling
  270. function! s:GundoMouseDoubleClick()"{{{
  271. let start_line = getline('.')
  272. if stridx(start_line, '[') == -1
  273. return
  274. else
  275. call s:GundoRevert()
  276. endif
  277. endfunction"}}}
  278. "}}}
  279. "{{{ Gundo movement
  280. function! s:GundoMove(direction) range"{{{
  281. let start_line = getline('.')
  282. if v:count1 == 0
  283. let move_count = 1
  284. else
  285. let move_count = v:count1
  286. endif
  287. let distance = 2 * move_count
  288. " If we're in between two nodes we move by one less to get back on track.
  289. if stridx(start_line, '[') == -1
  290. let distance = distance - 1
  291. endif
  292. let target_n = line('.') + (distance * a:direction)
  293. " Bound the movement to the graph.
  294. if target_n <= s:GundoInlineHelpLength() - 1
  295. call cursor(s:GundoInlineHelpLength(), 0)
  296. else
  297. call cursor(target_n, 0)
  298. endif
  299. let line = getline('.')
  300. " Move to the node, whether it's an @ or an o
  301. let idx1 = stridx(line, '@')
  302. let idx2 = stridx(line, 'o')
  303. if idx1 != -1
  304. call cursor(0, idx1 + 1)
  305. else
  306. call cursor(0, idx2 + 1)
  307. endif
  308. call s:GundoRenderPreview()
  309. endfunction"}}}
  310. "}}}
  311. "{{{ Gundo rendering
  312. function! s:GundoRenderGraph()"{{{
  313. if s:has_supported_python == 2
  314. python3 GundoRenderGraph()
  315. else
  316. python GundoRenderGraph()
  317. endif
  318. endfunction"}}}
  319. function! s:GundoRenderPreview()"{{{
  320. if s:has_supported_python == 2
  321. python3 GundoRenderPreview()
  322. else
  323. python GundoRenderPreview()
  324. endif
  325. endfunction"}}}
  326. function! s:GundoRenderChangePreview()"{{{
  327. if s:has_supported_python == 2
  328. python3 GundoRenderChangePreview()
  329. else
  330. python GundoRenderChangePreview()
  331. endif
  332. endfunction"}}}
  333. "}}}
  334. "{{{ Gundo undo/redo
  335. function! s:GundoRevert()"{{{
  336. if s:has_supported_python == 2
  337. python3 GundoRevert()
  338. else
  339. python GundoRevert()
  340. endif
  341. endfunction"}}}
  342. function! s:GundoPlayTo()"{{{
  343. if s:has_supported_python == 2
  344. python3 GundoPlayTo()
  345. else
  346. python GundoPlayTo()
  347. endif
  348. endfunction"}}}
  349. "}}}
  350. "{{{ Misc
  351. function! gundo#GundoToggle()"{{{
  352. call s:GundoToggle()
  353. endfunction"}}}
  354. function! gundo#GundoRenderGraph()"{{{
  355. call s:GundoRenderGraph()
  356. endfunction"}}}
  357. augroup GundoAug
  358. autocmd!
  359. autocmd BufNewFile __Gundo__ call s:GundoSettingsGraph()
  360. autocmd BufNewFile __Gundo_Preview__ call s:GundoSettingsPreview()
  361. augroup END
  362. "}}}