1 " ============================================================================
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.
9 " ============================================================================
14 if v:version < '703'"{{{
15 function! s:GundoDidNotLoad()
16 echohl WarningMsg|echomsg "Gundo unavailable: requires Vim 7.3+"|echohl None
18 command! -nargs=0 GundoToggle call s:GundoDidNotLoad()
22 if !exists('g:gundo_width')"{{{
23 let g:gundo_width = 45
25 if !exists('g:gundo_preview_height')"{{{
26 let g:gundo_preview_height = 15
28 if !exists('g:gundo_preview_bottom')"{{{
29 let g:gundo_preview_bottom = 0
31 if !exists('g:gundo_right')"{{{
34 if !exists('g:gundo_help')"{{{
37 if !exists("g:gundo_map_move_older")"{{{
38 let g:gundo_map_move_older = 'j'
40 if !exists("g:gundo_map_move_newer")"{{{
41 let g:gundo_map_move_newer = 'k'
43 if !exists("g:gundo_close_on_revert")"{{{
44 let g:gundo_close_on_revert = 0
46 if !exists("g:gundo_prefer_python3")"{{{
47 let g:gundo_prefer_python3 = 0
50 let s:has_supported_python = 0
51 if g:gundo_prefer_python3 && has('python3')"{{{
52 let s:has_supported_python = 2
54 let s:has_supported_python = 1
57 if !s:has_supported_python
58 function! s:GundoDidNotLoad()
59 echohl WarningMsg|echomsg "Gundo requires Vim to be compiled with Python 2.4+"|echohl None
61 command! -nargs=0 GundoToggle call s:GundoDidNotLoad()
65 let s:plugin_path = escape(expand('<sfile>:p:h'), '\')
68 "{{{ Gundo utility functions
70 function! s:GundoGetTargetState()"{{{
71 let target_line = matchstr(getline("."), '\v\[[0-9]+\]')
72 return matchstr(target_line, '\v[0-9]+')
75 function! s:GundoGoToWindowForBufferName(name)"{{{
76 if bufwinnr(bufnr(a:name)) != -1
77 exe bufwinnr(bufnr(a:name)) . "wincmd w"
84 function! s:GundoIsVisible()"{{{
85 if bufwinnr(bufnr("__Gundo__")) != -1 || bufwinnr(bufnr("__Gundo_Preview__")) != -1
92 function! s:GundoInlineHelpLength()"{{{
102 "{{{ Gundo buffer settings
104 function! s:GundoMapGraph()"{{{
105 exec 'nnoremap <script> <silent> <buffer> ' . g:gundo_map_move_older . " :call <sid>GundoMove(1)<CR>"
106 exec 'nnoremap <script> <silent> <buffer> ' . g:gundo_map_move_newer . " :call <sid>GundoMove(-1)<CR>"
107 nnoremap <script> <silent> <buffer> <CR> :call <sid>GundoRevert()<CR>
108 nnoremap <script> <silent> <buffer> o :call <sid>GundoRevert()<CR>
109 nnoremap <script> <silent> <buffer> <down> :call <sid>GundoMove(1)<CR>
110 nnoremap <script> <silent> <buffer> <up> :call <sid>GundoMove(-1)<CR>
111 nnoremap <script> <silent> <buffer> gg gg:call <sid>GundoMove(1)<CR>
112 nnoremap <script> <silent> <buffer> P :call <sid>GundoPlayTo()<CR>
113 nnoremap <script> <silent> <buffer> p :call <sid>GundoRenderChangePreview()<CR>
114 nnoremap <script> <silent> <buffer> q :call <sid>GundoClose()<CR>
115 cabbrev <script> <silent> <buffer> q call <sid>GundoClose()
116 cabbrev <script> <silent> <buffer> quit call <sid>GundoClose()
117 nnoremap <script> <silent> <buffer> <2-LeftMouse> :call <sid>GundoMouseDoubleClick()<CR>
120 function! s:GundoMapPreview()"{{{
121 nnoremap <script> <silent> <buffer> q :call <sid>GundoClose()<CR>
122 cabbrev <script> <silent> <buffer> q call <sid>GundoClose()
123 cabbrev <script> <silent> <buffer> quit call <sid>GundoClose()
126 function! s:GundoSettingsGraph()"{{{
127 setlocal buftype=nofile
128 setlocal bufhidden=hide
131 setlocal nomodifiable
132 setlocal filetype=gundo
135 setlocal norelativenumber
137 call s:GundoSyntaxGraph()
138 call s:GundoMapGraph()
141 function! s:GundoSettingsPreview()"{{{
142 setlocal buftype=nofile
143 setlocal bufhidden=hide
146 setlocal nomodifiable
147 setlocal filetype=diff
149 setlocal norelativenumber
151 setlocal foldlevel=20
152 setlocal foldmethod=diff
153 call s:GundoMapPreview()
156 function! s:GundoSyntaxGraph()"{{{
157 let b:current_syntax = 'gundo'
159 syn match GundoCurrentLocation '@'
160 syn match GundoHelp '\v^".*$'
161 syn match GundoNumberField '\v\[[0-9]+\]'
162 syn match GundoNumber '\v[0-9]+' contained containedin=GundoNumberField
164 hi def link GundoCurrentLocation Keyword
165 hi def link GundoHelp Comment
166 hi def link GundoNumberField Comment
167 hi def link GundoNumber Identifier
172 "{{{ Gundo buffer/window management
174 function! s:GundoResizeBuffers(backto)"{{{
175 call s:GundoGoToWindowForBufferName('__Gundo__')
176 exe "vertical resize " . g:gundo_width
178 call s:GundoGoToWindowForBufferName('__Gundo_Preview__')
179 exe "resize " . g:gundo_preview_height
181 exe a:backto . "wincmd w"
184 function! s:GundoOpenGraph()"{{{
185 let existing_gundo_buffer = bufnr("__Gundo__")
187 if existing_gundo_buffer == -1
188 call s:GundoGoToWindowForBufferName('__Gundo_Preview__')
190 if g:gundo_preview_bottom
197 call s:GundoResizeBuffers(winnr())
199 let existing_gundo_window = bufwinnr(existing_gundo_buffer)
201 if existing_gundo_window != -1
202 if winnr() != existing_gundo_window
203 exe existing_gundo_window . "wincmd w"
206 call s:GundoGoToWindowForBufferName('__Gundo_Preview__')
207 if g:gundo_preview_bottom
209 exe "botright vsplit +buffer" . existing_gundo_buffer
211 exe "topleft vsplit +buffer" . existing_gundo_buffer
214 exe "split +buffer" . existing_gundo_buffer
216 call s:GundoResizeBuffers(winnr())
219 if exists("g:gundo_tree_statusline")
220 let &l:statusline = g:gundo_tree_statusline
224 function! s:GundoOpenPreview()"{{{
225 let existing_preview_buffer = bufnr("__Gundo_Preview__")
227 if existing_preview_buffer == -1
228 if g:gundo_preview_bottom
229 exe "botright new __Gundo_Preview__"
232 exe "botright vnew __Gundo_Preview__"
234 exe "topleft vnew __Gundo_Preview__"
238 let existing_preview_window = bufwinnr(existing_preview_buffer)
240 if existing_preview_window != -1
241 if winnr() != existing_preview_window
242 exe existing_preview_window . "wincmd w"
245 if g:gundo_preview_bottom
246 exe "botright split +buffer" . existing_preview_buffer
249 exe "botright vsplit +buffer" . existing_preview_buffer
251 exe "topleft vsplit +buffer" . existing_preview_buffer
256 if exists("g:gundo_preview_statusline")
257 let &l:statusline = g:gundo_preview_statusline
261 function! s:GundoClose()"{{{
262 if s:GundoGoToWindowForBufferName('__Gundo__')
266 if s:GundoGoToWindowForBufferName('__Gundo_Preview__')
270 exe bufwinnr(g:gundo_target_n) . "wincmd w"
273 function! s:GundoOpen()"{{{
274 if !exists('g:gundo_py_loaded')
275 if s:has_supported_python == 2 && g:gundo_prefer_python3
276 exe 'py3file ' . s:plugin_path . '/gundo.py'
277 python3 initPythonModule()
279 exe 'pyfile ' . s:plugin_path . '/gundo.py'
280 python initPythonModule()
283 if !s:has_supported_python
284 function! s:GundoDidNotLoad()
285 echohl WarningMsg|echomsg "Gundo unavailable: requires Vim 7.3+"|echohl None
287 command! -nargs=0 GundoToggle call s:GundoDidNotLoad()
288 call s:GundoDidNotLoad()
292 let g:gundo_py_loaded = 1
295 " Save `splitbelow` value and set it to default to avoid problems with
296 " positioning new windows.
297 let saved_splitbelow = &splitbelow
300 call s:GundoOpenPreview()
301 exe bufwinnr(g:gundo_target_n) . "wincmd w"
303 call s:GundoRenderGraph()
304 call s:GundoRenderPreview()
306 " Restore `splitbelow` value.
307 let &splitbelow = saved_splitbelow
310 function! s:GundoToggle()"{{{
311 if s:GundoIsVisible()
314 let g:gundo_target_n = bufnr('')
315 let g:gundo_target_f = @%
320 function! s:GundoShow()"{{{
324 function! s:GundoHide()"{{{
330 "{{{ Gundo mouse handling
332 function! s:GundoMouseDoubleClick()"{{{
333 let start_line = getline('.')
335 if stridx(start_line, '[') == -1
346 function! s:GundoMove(direction) range"{{{
347 let start_line = getline('.')
351 let move_count = v:count1
353 let distance = 2 * move_count
355 " If we're in between two nodes we move by one less to get back on track.
356 if stridx(start_line, '[') == -1
357 let distance = distance - 1
360 let target_n = line('.') + (distance * a:direction)
362 " Bound the movement to the graph.
363 if target_n <= s:GundoInlineHelpLength() - 1
364 call cursor(s:GundoInlineHelpLength(), 0)
366 call cursor(target_n, 0)
369 let line = getline('.')
371 " Move to the node, whether it's an @ or an o
372 let idx1 = stridx(line, '@')
373 let idx2 = stridx(line, 'o')
375 call cursor(0, idx1 + 1)
377 call cursor(0, idx2 + 1)
380 call s:GundoRenderPreview()
387 function! s:GundoRenderGraph()"{{{
388 if s:has_supported_python == 2 && g:gundo_prefer_python3
389 python3 GundoRenderGraph()
391 python GundoRenderGraph()
395 function! s:GundoRenderPreview()"{{{
396 if s:has_supported_python == 2 && g:gundo_prefer_python3
397 python3 GundoRenderPreview()
399 python GundoRenderPreview()
403 function! s:GundoRenderChangePreview()"{{{
404 if s:has_supported_python == 2 && g:gundo_prefer_python3
405 python3 GundoRenderChangePreview()
407 python GundoRenderChangePreview()
415 function! s:GundoRevert()"{{{
416 if s:has_supported_python == 2 && g:gundo_prefer_python3
417 python3 GundoRevert()
423 function! s:GundoPlayTo()"{{{
424 if s:has_supported_python == 2 && g:gundo_prefer_python3
425 python3 GundoPlayTo()
435 function! gundo#GundoToggle()"{{{
439 function! gundo#GundoRenderGraph()"{{{
440 call s:GundoRenderGraph()
445 autocmd BufNewFile __Gundo__ call s:GundoSettingsGraph()
446 autocmd BufNewFile __Gundo_Preview__ call s:GundoSettingsPreview()