bgneal@7
|
1 " =============================================================================
|
bgneal@7
|
2 " File: autoload/ctrlp/undo.vim
|
bgneal@7
|
3 " Description: Undo extension
|
bgneal@7
|
4 " Author: Kien Nguyen <github.com/kien>
|
bgneal@7
|
5 " =============================================================================
|
bgneal@7
|
6
|
bgneal@7
|
7 " Init {{{1
|
bgneal@7
|
8 if ( exists('g:loaded_ctrlp_undo') && g:loaded_ctrlp_undo )
|
bgneal@7
|
9 fini
|
bgneal@7
|
10 en
|
bgneal@7
|
11 let g:loaded_ctrlp_undo = 1
|
bgneal@7
|
12
|
bgneal@7
|
13 cal add(g:ctrlp_ext_vars, {
|
bgneal@7
|
14 \ 'init': 'ctrlp#undo#init()',
|
bgneal@7
|
15 \ 'accept': 'ctrlp#undo#accept',
|
bgneal@7
|
16 \ 'lname': 'undo',
|
bgneal@7
|
17 \ 'sname': 'udo',
|
bgneal@7
|
18 \ 'enter': 'ctrlp#undo#enter()',
|
bgneal@7
|
19 \ 'exit': 'ctrlp#undo#exit()',
|
bgneal@7
|
20 \ 'type': 'line',
|
bgneal@7
|
21 \ 'sort': 0,
|
bgneal@7
|
22 \ 'nolim': 1,
|
bgneal@7
|
23 \ })
|
bgneal@7
|
24
|
bgneal@7
|
25 let s:id = g:ctrlp_builtins + len(g:ctrlp_ext_vars)
|
bgneal@7
|
26
|
bgneal@7
|
27 let s:text = map(['second', 'seconds', 'minutes', 'hours', 'days', 'weeks',
|
bgneal@7
|
28 \ 'months', 'years'], '" ".v:val." ago"')
|
bgneal@7
|
29 " Utilities {{{1
|
bgneal@7
|
30 fu! s:getundo()
|
bgneal@7
|
31 if exists('*undotree')
|
bgneal@7
|
32 \ && ( v:version > 703 || ( v:version == 703 && has('patch005') ) )
|
bgneal@7
|
33 retu [1, undotree()]
|
bgneal@7
|
34 el
|
bgneal@7
|
35 redi => result
|
bgneal@7
|
36 sil! undol
|
bgneal@7
|
37 redi END
|
bgneal@7
|
38 retu [0, split(result, "\n")[1:]]
|
bgneal@7
|
39 en
|
bgneal@7
|
40 endf
|
bgneal@7
|
41
|
bgneal@7
|
42 fu! s:flatten(tree, cur)
|
bgneal@7
|
43 let flatdict = {}
|
bgneal@7
|
44 for each in a:tree
|
bgneal@7
|
45 let saved = has_key(each, 'save') ? 'saved' : ''
|
bgneal@7
|
46 let current = each['seq'] == a:cur ? 'current' : ''
|
bgneal@7
|
47 cal extend(flatdict, { each['seq'] : [each['time'], saved, current] })
|
bgneal@7
|
48 if has_key(each, 'alt')
|
bgneal@7
|
49 cal extend(flatdict, s:flatten(each['alt'], a:cur))
|
bgneal@7
|
50 en
|
bgneal@7
|
51 endfo
|
bgneal@7
|
52 retu flatdict
|
bgneal@7
|
53 endf
|
bgneal@7
|
54
|
bgneal@7
|
55 fu! s:elapsed(nr)
|
bgneal@7
|
56 let [text, time] = [s:text, localtime() - a:nr]
|
bgneal@7
|
57 let mins = time / 60
|
bgneal@7
|
58 let hrs = time / 3600
|
bgneal@7
|
59 let days = time / 86400
|
bgneal@7
|
60 let wks = time / 604800
|
bgneal@7
|
61 let mons = time / 2592000
|
bgneal@7
|
62 let yrs = time / 31536000
|
bgneal@7
|
63 if yrs > 1
|
bgneal@7
|
64 retu yrs.text[7]
|
bgneal@7
|
65 elsei mons > 1
|
bgneal@7
|
66 retu mons.text[6]
|
bgneal@7
|
67 elsei wks > 1
|
bgneal@7
|
68 retu wks.text[5]
|
bgneal@7
|
69 elsei days > 1
|
bgneal@7
|
70 retu days.text[4]
|
bgneal@7
|
71 elsei hrs > 1
|
bgneal@7
|
72 retu hrs.text[3]
|
bgneal@7
|
73 elsei mins > 1
|
bgneal@7
|
74 retu mins.text[2]
|
bgneal@7
|
75 elsei time == 1
|
bgneal@7
|
76 retu time.text[0]
|
bgneal@7
|
77 elsei time < 120
|
bgneal@7
|
78 retu time.text[1]
|
bgneal@7
|
79 en
|
bgneal@7
|
80 endf
|
bgneal@7
|
81
|
bgneal@7
|
82 fu! s:syntax()
|
bgneal@7
|
83 if ctrlp#nosy() | retu | en
|
bgneal@7
|
84 for [ke, va] in items({'T': 'Directory', 'Br': 'Comment', 'Nr': 'String',
|
bgneal@7
|
85 \ 'Sv': 'Comment', 'Po': 'Title'})
|
bgneal@7
|
86 cal ctrlp#hicheck('CtrlPUndo'.ke, va)
|
bgneal@7
|
87 endfo
|
bgneal@7
|
88 sy match CtrlPUndoT '\v\d+ \zs[^ ]+\ze|\d+:\d+:\d+'
|
bgneal@7
|
89 sy match CtrlPUndoBr '\[\|\]'
|
bgneal@7
|
90 sy match CtrlPUndoNr '\[\d\+\]' contains=CtrlPUndoBr
|
bgneal@7
|
91 sy match CtrlPUndoSv 'saved'
|
bgneal@7
|
92 sy match CtrlPUndoPo 'current'
|
bgneal@7
|
93 endf
|
bgneal@7
|
94
|
bgneal@7
|
95 fu! s:dict2list(dict)
|
bgneal@7
|
96 for ke in keys(a:dict)
|
bgneal@7
|
97 let a:dict[ke][0] = s:elapsed(a:dict[ke][0])
|
bgneal@7
|
98 endfo
|
bgneal@7
|
99 retu map(keys(a:dict), 'eval(''[v:val, a:dict[v:val]]'')')
|
bgneal@7
|
100 endf
|
bgneal@7
|
101
|
bgneal@7
|
102 fu! s:compval(...)
|
bgneal@7
|
103 retu a:2[0] - a:1[0]
|
bgneal@7
|
104 endf
|
bgneal@7
|
105
|
bgneal@7
|
106 fu! s:format(...)
|
bgneal@7
|
107 let saved = !empty(a:1[1][1]) ? ' '.a:1[1][1] : ''
|
bgneal@7
|
108 let current = !empty(a:1[1][2]) ? ' '.a:1[1][2] : ''
|
bgneal@7
|
109 retu a:1[1][0].' ['.a:1[0].']'.saved.current
|
bgneal@7
|
110 endf
|
bgneal@7
|
111
|
bgneal@7
|
112 fu! s:formatul(...)
|
bgneal@7
|
113 let parts = matchlist(a:1,
|
bgneal@7
|
114 \ '\v^\s+(\d+)\s+\d+\s+([^ ]+\s?[^ ]+|\d+\s\w+\s\w+)(\s*\d*)$')
|
bgneal@7
|
115 retu parts == [] ? '----'
|
bgneal@7
|
116 \ : parts[2].' ['.parts[1].']'.( parts[3] != '' ? ' saved' : '' )
|
bgneal@7
|
117 endf
|
bgneal@7
|
118 " Public {{{1
|
bgneal@7
|
119 fu! ctrlp#undo#init()
|
bgneal@7
|
120 let entries = s:undos[0] ? s:undos[1]['entries'] : s:undos[1]
|
bgneal@7
|
121 if empty(entries) | retu [] | en
|
bgneal@7
|
122 if !exists('s:lines')
|
bgneal@7
|
123 if s:undos[0]
|
bgneal@7
|
124 let entries = s:dict2list(s:flatten(entries, s:undos[1]['seq_cur']))
|
bgneal@7
|
125 let s:lines = map(sort(entries, 's:compval'), 's:format(v:val)')
|
bgneal@7
|
126 el
|
bgneal@7
|
127 let s:lines = map(reverse(entries), 's:formatul(v:val)')
|
bgneal@7
|
128 en
|
bgneal@7
|
129 en
|
bgneal@7
|
130 cal s:syntax()
|
bgneal@7
|
131 retu s:lines
|
bgneal@7
|
132 endf
|
bgneal@7
|
133
|
bgneal@7
|
134 fu! ctrlp#undo#accept(mode, str)
|
bgneal@7
|
135 let undon = matchstr(a:str, '\[\zs\d\+\ze\]')
|
bgneal@7
|
136 if empty(undon) | retu | en
|
bgneal@7
|
137 cal ctrlp#exit()
|
bgneal@7
|
138 exe 'u' undon
|
bgneal@7
|
139 endf
|
bgneal@7
|
140
|
bgneal@7
|
141 fu! ctrlp#undo#id()
|
bgneal@7
|
142 retu s:id
|
bgneal@7
|
143 endf
|
bgneal@7
|
144
|
bgneal@7
|
145 fu! ctrlp#undo#enter()
|
bgneal@7
|
146 let s:undos = s:getundo()
|
bgneal@7
|
147 endf
|
bgneal@7
|
148
|
bgneal@7
|
149 fu! ctrlp#undo#exit()
|
bgneal@7
|
150 unl! s:lines
|
bgneal@7
|
151 endf
|
bgneal@7
|
152 "}}}
|
bgneal@7
|
153
|
bgneal@7
|
154 " vim:fen:fdm=marker:fmr={{{,}}}:fdl=0:fdc=1:ts=2:sw=2:sts=2
|