/
agent.vim
287 lines (247 loc) · 7.76 KB
/
agent.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
" agent.vim
" @Author: Thomas Link (mailto:samul AT web de?subject=[vim])
" @Website: http://www.vim.org/account/profile.php?user_id=4037
" @License: GPL (see http://www.gnu.org/licenses/gpl.txt)
" @Created: 2007-06-24.
" @Last Change: 2007-07-09.
" @Revision: 0.1.41
if &cp || exists("loaded_tlib_agent_autoload")
finish
endif
let loaded_tlib_agent_autoload = 1
" General {{{1
function! tlib#agent#Exit(world, selected) "{{{3
call a:world.CloseScratch()
let a:world.state = 'exit escape'
let a:world.list = []
" let a:world.base = []
call a:world.ResetSelected()
return a:world
endf
function! tlib#agent#CopyItems(world, selected)
let @* = join(a:selected, "\n")
let a:world.state = 'redisplay'
return a:world
endf
" InputList related {{{1
function! tlib#agent#PageUp(world, selected) "{{{3
let a:world.offset -= (winheight(0) / 2)
let a:world.state = 'scroll'
return a:world
endf
function! tlib#agent#PageDown(world, selected) "{{{3
let a:world.offset += (winheight(0) / 2)
let a:world.state = 'scroll'
return a:world
endf
function! tlib#agent#Up(world, selected) "{{{3
let a:world.idx = ''
if a:world.prefidx > 1
let a:world.prefidx -= 1
else
let a:world.prefidx = len(a:world.list)
endif
let a:world.state = 'redisplay'
return a:world
endf
function! tlib#agent#Down(world, selected) "{{{3
let a:world.idx = ''
if a:world.prefidx < len(a:world.list)
let a:world.prefidx += 1
else
let a:world.prefidx = 1
endif
let a:world.state = 'redisplay'
return a:world
endf
function! tlib#agent#Reset(world, selected) "{{{3
let a:world.state = 'reset'
return a:world
endf
function! tlib#agent#Input(world, selected) "{{{3
let flt0 = a:world.filter[0][0]
let flt1 = input('Filter: ', flt0)
echo
if flt1 != flt0 && !empty(flt1)
let a:world.filter[0][0] = flt1
endif
let a:world.state = 'display'
return a:world
endf
function! tlib#agent#Suspend(world, selected) "{{{3
let bn = bufnr('.')
let wn = bufwinnr(bn)
exec 'noremap <buffer> <c-z> :call <SID>Resume("world", '. bn .', '. wn .')<cr>'
let b:tlib_world = a:world
let a:world.state = 'exit suspend'
return a:world
endf
function! s:Resume(name, bn, wn) "{{{3
echo
let b:tlib_{a:name}.state = 'display'
call tlib#input#List('resume '. a:name)
endf
function! tlib#agent#Help(world, selected) "{{{3
let a:world.state = 'help'
return a:world
endf
function! tlib#agent#OR(world, selected) "{{{3
if !empty(a:world.filter[0])
call insert(a:world.filter[0], '')
endif
let a:world.state = 'display'
return a:world
endf
function! tlib#agent#AND(world, selected) "{{{3
if !empty(a:world.filter[0])
call insert(a:world.filter, [''])
endif
let a:world.state = 'display'
return a:world
endf
function! tlib#agent#ReduceFilter(world, selected) "{{{3
call a:world.ReduceFilter()
let a:world.offset = 1
let a:world.state = 'display'
return a:world
endf
function! tlib#agent#PopFilter(world, selected) "{{{3
call a:world.PopFilter()
let a:world.offset = 1
let a:world.state = 'display'
return a:world
endf
function! tlib#agent#Debug(world, selected) "{{{3
" echo string(world.state)
echo string(a:world.filter)
echo string(a:world.idx)
echo string(a:world.prefidx)
echo string(a:world.sel_idx)
call getchar()
let a:world.state = 'display'
return a:world
endf
function! tlib#agent#Select(world, selected) "{{{3
call a:world.SelectItem('toggle', a:world.prefidx)
" let a:world.state = 'display keepcursor'
let a:world.state = 'redisplay'
return a:world
endf
function! tlib#agent#SelectUp(world, selected) "{{{3
call a:world.SelectItem('toggle', a:world.prefidx)
if a:world.prefidx > 1
let a:world.prefidx -= 1
endif
let a:world.state = 'redisplay'
return a:world
endf
function! tlib#agent#SelectDown(world, selected) "{{{3
call a:world.SelectItem('toggle', a:world.prefidx)
if a:world.prefidx < len(a:world.list)
let a:world.prefidx += 1
endif
let a:world.state = 'redisplay'
return a:world
endf
function! tlib#agent#SelectAll(world, selected) "{{{3
let listrange = range(1, len(a:world.list))
let mode = empty(filter(copy(listrange), 'index(a:world.sel_idx, a:world.GetBaseIdx(v:val)) == -1'))
\ ? 'toggle' : 'set'
for i in listrange
call a:world.SelectItem(mode, i)
endfor
let a:world.state = 'display keepcursor'
return a:world
endf
" EditList related {{{1
function! tlib#agent#EditItem(world, selected) "{{{3
let lidx = a:world.prefidx
" TLogVAR lidx
" TLogVAR a:world.table
let bidx = a:world.GetBaseIdx(lidx)
" TLogVAR bidx
let item = a:world.GetBaseItem(bidx)
let item = input(lidx .'@'. bidx .': ', item)
if item != ''
call a:world.SetBaseItem(bidx, item)
endif
let a:world.state = 'display'
return a:world
endf
function! tlib#agent#NewItem(world, selected) "{{{3
let basepi = a:world.GetBaseIdx(a:world.prefidx)
let item = input('New item: ')
call insert(a:world.base, item, basepi)
let a:world.state = 'reset'
return a:world
endf
function! tlib#agent#DeleteItems(world, selected) "{{{3
let remove = copy(a:world.sel_idx)
let basepi = a:world.GetBaseIdx(a:world.prefidx)
if index(remove, basepi) == -1
call add(remove, basepi)
endif
" call map(remove, 'a:world.GetBaseIdx(v:val)')
for idx in reverse(sort(remove))
call remove(a:world.base, idx - 1)
endfor
let a:world.state = 'display'
call a:world.ResetSelected()
" let a:world.state = 'reset'
return a:world
endf
function! tlib#agent#Cut(world, selected) "{{{3
let world = tlib#agent#Copy(a:world, a:selected)
return tlib#agent#DeleteItems(world, a:selected)
endf
function! tlib#agent#Copy(world, selected) "{{{3
let a:world.clipboard = []
let bidxs = copy(a:world.sel_idx)
call add(bidxs, a:world.GetBaseIdx(a:world.prefidx))
for bidx in sort(bidxs)
call add(a:world.clipboard, a:world.GetBaseItem(bidx))
endfor
let a:world.state = 'redisplay'
return a:world
endf
function! tlib#agent#Paste(world, selected) "{{{3
if has_key(a:world, 'clipboard')
for e in reverse(copy(a:world.clipboard))
call insert(a:world.base, e, a:world.prefidx)
endfor
endif
let a:world.state = 'display'
call a:world.ResetSelected()
return a:world
endf
function! tlib#agent#EditReturnValue(world, rv) "{{{3
return [a:world.state !~ '\<exit\>', a:world.base]
endf
" Files related {{{1
function! tlib#agent#EditFile(world, selected)
call a:world.CloseScratch()
call tlib#file#With('edit', 'buffer', a:selected, a:world)
return tlib#agent#Exit(a:world, a:selected)
endf
function! tlib#agent#EditFileInSplit(world, selected)
call a:world.CloseScratch()
call tlib#file#With('edit', 'buffer', a:selected[0:0], a:world)
call tlib#file#With('split', 'sbuffer', a:selected[1:-1], a:world)
return tlib#agent#Exit(a:world, a:selected)
endf
function! tlib#agent#EditFileInVSplit(world, selected)
call a:world.CloseScratch()
call tlib#file#With('edit', 'buffer', a:selected[0:0], a:world)
call tlib#file#With('vertical split', 'vertical sbuffer', a:selected[1:-1], a:world)
return tlib#agent#Exit(a:world, a:selected)
endf
function! tlib#agent#EditFileInTab(world, selected)
call a:world.CloseScratch()
call tlib#file#With('tabe', 'tab buffer', a:selected, a:world)
return tlib#agent#Exit(a:world, a:selected)
endf
function! tlib#agent#ToggleScrollbind(world, selected)
let a:world.scrollbind = get(a:world, 'scrollbind') ? 0 : 1
let a:world.state = 'redisplay'
return a:world
endf