Use :undolist for older Vim versions

This commit is contained in:
Kien N 2012-03-24 11:58:01 +07:00
parent 3ee411cdea
commit c4fe304565
3 changed files with 55 additions and 34 deletions

View File

@ -735,7 +735,7 @@ fu! s:SetWD(...)
cal extend(markers, s:rmarkers, 0) cal extend(markers, s:rmarkers, 0)
en en
for marker in markers for marker in markers
cal s:findroot(getcwd(), marker, 0, 0) cal s:findroot(s:dyncwd, marker, 0, 0)
if exists('s:foundroot') | brea | en if exists('s:foundroot') | brea | en
endfo endfo
unl! s:foundroot unl! s:foundroot
@ -1413,9 +1413,8 @@ fu! s:getenv()
let s:wpmode = exists('b:ctrlp_working_path_mode') let s:wpmode = exists('b:ctrlp_working_path_mode')
\ ? b:ctrlp_working_path_mode : s:pathmode \ ? b:ctrlp_working_path_mode : s:pathmode
if exists('g:ctrlp_extensions') if exists('g:ctrlp_extensions')
if index(g:ctrlp_extensions, 'undo') >= 0 && exists('*undotree') if index(g:ctrlp_extensions, 'undo') >= 0
\ && ( v:version > 703 || ( v:version == 703 && has('patch005') ) ) let s:undos = s:getundo()
let s:undotree = undotree()
en en
if index(g:ctrlp_extensions, 'tag') >= 0 if index(g:ctrlp_extensions, 'tag') >= 0
let s:tagfiles = s:tagfiles() let s:tagfiles = s:tagfiles()
@ -1541,6 +1540,18 @@ fu! s:extvar(key)
en en
endf endf
fu! s:getundo()
if exists('*undotree')
\ && ( v:version > 703 || ( v:version == 703 && has('patch005') ) )
retu [1, undotree()]
el
redi => result
sil! undol
redi END
retu [0, split(result, "\n")[1:]]
en
endf
fu! ctrlp#exit() fu! ctrlp#exit()
cal s:PrtExit() cal s:PrtExit()
endf endf

View File

@ -6,16 +6,16 @@
" Init {{{1 " Init {{{1
if ( exists('g:loaded_ctrlp_undo') && g:loaded_ctrlp_undo ) if ( exists('g:loaded_ctrlp_undo') && g:loaded_ctrlp_undo )
\ || !( v:version > 703 || ( v:version == 703 && has('patch005') ) )
fini fini
en en
let g:loaded_ctrlp_undo = 1 let g:loaded_ctrlp_undo = 1
let s:undo_var = { let s:undo_var = {
\ 'init': 'ctrlp#undo#init(s:undotree)', \ 'init': 'ctrlp#undo#init(s:undos)',
\ 'accept': 'ctrlp#undo#accept', \ 'accept': 'ctrlp#undo#accept',
\ 'lname': 'undo', \ 'lname': 'undo',
\ 'sname': 'udo', \ 'sname': 'udo',
\ 'exit': 'ctrlp#undo#exit()',
\ 'type': 'line', \ 'type': 'line',
\ } \ }
@ -23,6 +23,9 @@ let g:ctrlp_ext_vars = exists('g:ctrlp_ext_vars') && !empty(g:ctrlp_ext_vars)
\ ? add(g:ctrlp_ext_vars, s:undo_var) : [s:undo_var] \ ? add(g:ctrlp_ext_vars, s:undo_var) : [s:undo_var]
let s:id = g:ctrlp_builtins + len(g:ctrlp_ext_vars) let s:id = g:ctrlp_builtins + len(g:ctrlp_ext_vars)
let s:text = map(['second', 'seconds', 'minutes', 'hours', 'days', 'weeks',
\ 'months', 'years'], '" ".v:val." ago"')
" Utilities {{{1 " Utilities {{{1
fu! s:flatten(tree, cur) fu! s:flatten(tree, cur)
let flatdict = {} let flatdict = {}
@ -37,24 +40,14 @@ fu! s:flatten(tree, cur)
retu flatdict retu flatdict
endf endf
fu! s:humantime(nr) fu! s:elapsed(nr)
let elapsed = localtime() - a:nr let [text, time] = [s:text, localtime() - a:nr]
let mins = elapsed / 60 let mins = time / 60
let hrs = elapsed / 3600 let hrs = time / 3600
let days = elapsed / 86400 let days = time / 86400
let wks = elapsed / 604800 let wks = time / 604800
let mons = elapsed / 2592000 let mons = time / 2592000
let yrs = elapsed / 31536000 let yrs = time / 31536000
let text = [
\ ' second ago',
\ ' seconds ago',
\ ' minutes ago',
\ ' hours ago',
\ ' days ago',
\ ' weeks ago',
\ ' months ago',
\ ' years ago',
\ ]
if yrs > 1 if yrs > 1
retu yrs.text[7] retu yrs.text[7]
elsei mons > 1 elsei mons > 1
@ -67,10 +60,10 @@ fu! s:humantime(nr)
retu hrs.text[3] retu hrs.text[3]
elsei mins > 1 elsei mins > 1
retu mins.text[2] retu mins.text[2]
elsei elapsed == 1 elsei time == 1
retu elapsed.text[0] retu time.text[0]
elsei elapsed < 120 elsei time < 120
retu elapsed.text[1] retu time.text[1]
en en
endf endf
@ -81,7 +74,7 @@ fu! s:syntax()
exe 'hi link CtrlPUndo'.ke va exe 'hi link CtrlPUndo'.ke va
en en
endfo endfo
sy match CtrlPUndoT '\d\+ \zs[^ ]\+\ze' sy match CtrlPUndoT '\v\d+ \zs[^ ]+\ze|\d+:\d+:\d+'
sy match CtrlPUndoBr '\[\|\]' sy match CtrlPUndoBr '\[\|\]'
sy match CtrlPUndoNr '\[\d\+\]' contains=CtrlPUndoBr sy match CtrlPUndoNr '\[\d\+\]' contains=CtrlPUndoBr
sy match CtrlPUndoSv 'saved' sy match CtrlPUndoSv 'saved'
@ -90,7 +83,7 @@ endf
fu! s:dict2list(dict) fu! s:dict2list(dict)
for ke in keys(a:dict) for ke in keys(a:dict)
let a:dict[ke][0] = s:humantime(a:dict[ke][0]) let a:dict[ke][0] = s:elapsed(a:dict[ke][0])
endfo endfo
retu map(keys(a:dict), 'eval(''[v:val, a:dict[v:val]]'')') retu map(keys(a:dict), 'eval(''[v:val, a:dict[v:val]]'')')
endf endf
@ -104,16 +97,29 @@ fu! s:format(...)
let current = !empty(a:1[1][2]) ? ' '.a:1[1][2] : '' let current = !empty(a:1[1][2]) ? ' '.a:1[1][2] : ''
retu a:1[1][0].' ['.a:1[0].']'.saved.current retu a:1[1][0].' ['.a:1[0].']'.saved.current
endf endf
fu! s:formatul(...)
let parts = matchlist(a:1,
\ '\v^\s+(\d+)\s+\d+\s+([^ ]+\s?[^ ]+|\d+\s\w+\s\w+)(\s*\d*)$')
retu parts[2].' ['.parts[1].']'.( parts[3] != '' ? ' saved' : '' )
endf
" Public {{{1 " Public {{{1
fu! ctrlp#undo#init(undo) fu! ctrlp#undo#init(undo)
let entries = a:undo['entries'] let entries = a:undo[0] ? a:undo[1]['entries'] : a:undo[1]
if empty(entries) | retu [] | en if empty(entries) | retu [] | en
if has('syntax') && exists('g:syntax_on') if has('syntax') && exists('g:syntax_on')
cal s:syntax() cal s:syntax()
en en
let g:ctrlp_nolimit = 1 let g:ctrlp_nolimit = 1
let entries = s:dict2list(s:flatten(entries, a:undo['seq_cur'])) if !exists('s:lines')
retu map(sort(entries, 's:compval'), 's:format(v:val)') if a:undo[0]
let entries = s:dict2list(s:flatten(entries, a:undo[1]['seq_cur']))
let s:lines = map(sort(entries, 's:compval'), 's:format(v:val)')
el
let s:lines = map(reverse(entries), 's:formatul(v:val)')
en
en
retu s:lines
endf endf
fu! ctrlp#undo#accept(mode, str) fu! ctrlp#undo#accept(mode, str)
@ -126,6 +132,10 @@ endf
fu! ctrlp#undo#id() fu! ctrlp#undo#id()
retu s:id retu s:id
endf endf
fu! ctrlp#undo#exit()
unl! s:lines
endf
"}}} "}}}
" vim:fen:fdm=marker:fmr={{{,}}}:fdl=0:fdc=1:ts=2:sw=2:sts=2 " vim:fen:fdm=marker:fmr={{{,}}}:fdl=0:fdc=1:ts=2:sw=2:sts=2

View File

@ -729,7 +729,7 @@ Available extensions:~
* Undo mode:~ * Undo mode:~
- Name: 'undo' - Name: 'undo'
- Command: ':CtrlPUndo' - Command: ':CtrlPUndo'
- Browse undo history (requires Vim 7.3.005+). - Browse undo history.
*:CtrlPLine* *:CtrlPLine*
* Line mode:~ * Line mode:~