2012-01-06 01:22:55 -05:00
|
|
|
" =============================================================================
|
|
|
|
" File: autoload/ctrlp/undo.vim
|
2012-03-12 04:34:40 -04:00
|
|
|
" Description: Undo extension
|
2012-01-06 01:22:55 -05:00
|
|
|
" Author: Kien Nguyen <github.com/kien>
|
|
|
|
" =============================================================================
|
|
|
|
|
|
|
|
" Init {{{1
|
|
|
|
if ( exists('g:loaded_ctrlp_undo') && g:loaded_ctrlp_undo )
|
|
|
|
fini
|
|
|
|
en
|
|
|
|
let g:loaded_ctrlp_undo = 1
|
|
|
|
|
2012-01-15 12:42:14 -05:00
|
|
|
let s:undo_var = {
|
2012-03-28 21:53:00 -04:00
|
|
|
\ 'init': 'ctrlp#undo#init()',
|
2012-01-15 12:42:14 -05:00
|
|
|
\ 'accept': 'ctrlp#undo#accept',
|
|
|
|
\ 'lname': 'undo',
|
|
|
|
\ 'sname': 'udo',
|
2012-03-28 21:53:00 -04:00
|
|
|
\ 'enter': 'ctrlp#undo#enter()',
|
2012-03-24 00:58:01 -04:00
|
|
|
\ 'exit': 'ctrlp#undo#exit()',
|
2012-01-15 12:42:14 -05:00
|
|
|
\ 'type': 'line',
|
2012-03-30 14:58:51 -04:00
|
|
|
\ 'sort': 0,
|
2012-01-15 12:42:14 -05:00
|
|
|
\ }
|
2012-01-06 01:22:55 -05:00
|
|
|
|
|
|
|
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]
|
|
|
|
|
|
|
|
let s:id = g:ctrlp_builtins + len(g:ctrlp_ext_vars)
|
2012-03-24 00:58:01 -04:00
|
|
|
|
|
|
|
let s:text = map(['second', 'seconds', 'minutes', 'hours', 'days', 'weeks',
|
|
|
|
\ 'months', 'years'], '" ".v:val." ago"')
|
2012-01-06 01:22:55 -05:00
|
|
|
" Utilities {{{1
|
2012-03-28 21:53:00 -04:00
|
|
|
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
|
|
|
|
|
2012-03-12 04:34:40 -04:00
|
|
|
fu! s:flatten(tree, cur)
|
2012-01-06 01:22:55 -05:00
|
|
|
let flatdict = {}
|
|
|
|
for each in a:tree
|
2012-03-12 04:34:40 -04:00
|
|
|
let saved = has_key(each, 'save') ? 'saved' : ''
|
|
|
|
let current = each['seq'] == a:cur ? 'current' : ''
|
|
|
|
cal extend(flatdict, { each['seq'] : [each['time'], saved, current] })
|
2012-01-06 01:22:55 -05:00
|
|
|
if has_key(each, 'alt')
|
2012-03-12 04:34:40 -04:00
|
|
|
cal extend(flatdict, s:flatten(each['alt'], a:cur))
|
2012-01-06 01:22:55 -05:00
|
|
|
en
|
|
|
|
endfo
|
|
|
|
retu flatdict
|
|
|
|
endf
|
|
|
|
|
2012-03-24 00:58:01 -04:00
|
|
|
fu! s:elapsed(nr)
|
|
|
|
let [text, time] = [s:text, localtime() - a:nr]
|
|
|
|
let mins = time / 60
|
|
|
|
let hrs = time / 3600
|
|
|
|
let days = time / 86400
|
|
|
|
let wks = time / 604800
|
|
|
|
let mons = time / 2592000
|
|
|
|
let yrs = time / 31536000
|
2012-01-06 01:22:55 -05:00
|
|
|
if yrs > 1
|
|
|
|
retu yrs.text[7]
|
|
|
|
elsei mons > 1
|
|
|
|
retu mons.text[6]
|
|
|
|
elsei wks > 1
|
|
|
|
retu wks.text[5]
|
|
|
|
elsei days > 1
|
|
|
|
retu days.text[4]
|
|
|
|
elsei hrs > 1
|
|
|
|
retu hrs.text[3]
|
|
|
|
elsei mins > 1
|
|
|
|
retu mins.text[2]
|
2012-03-24 00:58:01 -04:00
|
|
|
elsei time == 1
|
|
|
|
retu time.text[0]
|
|
|
|
elsei time < 120
|
|
|
|
retu time.text[1]
|
2012-01-06 01:22:55 -05:00
|
|
|
en
|
|
|
|
endf
|
|
|
|
|
|
|
|
fu! s:syntax()
|
2012-03-12 04:34:40 -04:00
|
|
|
for [ke, va] in items({'T': 'Directory', 'Br': 'Comment', 'Nr': 'String',
|
|
|
|
\ 'Sv': 'Comment', 'Po': 'Title'})
|
2012-02-05 04:13:52 -05:00
|
|
|
if !hlexists('CtrlPUndo'.ke)
|
|
|
|
exe 'hi link CtrlPUndo'.ke va
|
|
|
|
en
|
|
|
|
endfo
|
2012-03-24 00:58:01 -04:00
|
|
|
sy match CtrlPUndoT '\v\d+ \zs[^ ]+\ze|\d+:\d+:\d+'
|
2012-01-06 01:22:55 -05:00
|
|
|
sy match CtrlPUndoBr '\[\|\]'
|
2012-03-12 04:34:40 -04:00
|
|
|
sy match CtrlPUndoNr '\[\d\+\]' contains=CtrlPUndoBr
|
|
|
|
sy match CtrlPUndoSv 'saved'
|
|
|
|
sy match CtrlPUndoPo 'current'
|
2012-01-06 01:22:55 -05:00
|
|
|
endf
|
|
|
|
|
|
|
|
fu! s:dict2list(dict)
|
2012-03-12 04:34:40 -04:00
|
|
|
for ke in keys(a:dict)
|
2012-03-24 00:58:01 -04:00
|
|
|
let a:dict[ke][0] = s:elapsed(a:dict[ke][0])
|
2012-03-12 04:34:40 -04:00
|
|
|
endfo
|
|
|
|
retu map(keys(a:dict), 'eval(''[v:val, a:dict[v:val]]'')')
|
2012-01-06 01:22:55 -05:00
|
|
|
endf
|
|
|
|
|
|
|
|
fu! s:compval(...)
|
|
|
|
retu a:2[0] - a:1[0]
|
|
|
|
endf
|
2012-03-12 04:34:40 -04:00
|
|
|
|
|
|
|
fu! s:format(...)
|
|
|
|
let saved = !empty(a:1[1][1]) ? ' '.a:1[1][1] : ''
|
|
|
|
let current = !empty(a:1[1][2]) ? ' '.a:1[1][2] : ''
|
|
|
|
retu a:1[1][0].' ['.a:1[0].']'.saved.current
|
|
|
|
endf
|
2012-03-24 00:58:01 -04:00
|
|
|
|
|
|
|
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
|
2012-01-06 01:22:55 -05:00
|
|
|
" Public {{{1
|
2012-03-28 21:53:00 -04:00
|
|
|
fu! ctrlp#undo#init()
|
|
|
|
let entries = s:undos[0] ? s:undos[1]['entries'] : s:undos[1]
|
2012-01-06 01:22:55 -05:00
|
|
|
if empty(entries) | retu [] | en
|
2012-02-20 02:58:03 -05:00
|
|
|
if has('syntax') && exists('g:syntax_on')
|
|
|
|
cal s:syntax()
|
|
|
|
en
|
2012-01-06 01:22:55 -05:00
|
|
|
let g:ctrlp_nolimit = 1
|
2012-03-24 00:58:01 -04:00
|
|
|
if !exists('s:lines')
|
2012-03-28 21:53:00 -04:00
|
|
|
if s:undos[0]
|
|
|
|
let entries = s:dict2list(s:flatten(entries, s:undos[1]['seq_cur']))
|
2012-03-24 00:58:01 -04:00
|
|
|
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
|
2012-01-06 01:22:55 -05:00
|
|
|
endf
|
|
|
|
|
|
|
|
fu! ctrlp#undo#accept(mode, str)
|
|
|
|
let undon = matchstr(a:str, '\[\zs\d\+\ze\]')
|
|
|
|
if empty(undon) | retu | en
|
|
|
|
cal ctrlp#exit()
|
|
|
|
exe 'u' undon
|
|
|
|
endf
|
|
|
|
|
|
|
|
fu! ctrlp#undo#id()
|
|
|
|
retu s:id
|
|
|
|
endf
|
2012-03-24 00:58:01 -04:00
|
|
|
|
2012-03-28 21:53:00 -04:00
|
|
|
fu! ctrlp#undo#enter()
|
|
|
|
let s:undos = s:getundo()
|
|
|
|
endf
|
|
|
|
|
2012-03-24 00:58:01 -04:00
|
|
|
fu! ctrlp#undo#exit()
|
|
|
|
unl! s:lines
|
|
|
|
endf
|
2012-01-06 01:22:55 -05:00
|
|
|
"}}}
|
|
|
|
|
|
|
|
" vim:fen:fdm=marker:fmr={{{,}}}:fdl=0:fdc=1:ts=2:sw=2:sts=2
|