2 " @Author: Thomas Link (micathom AT gmail com?subject=[vim])
3 " @Website: http://www.vim.org/account/profile.php?user_id=4037
4 " @License: GPL (see http://www.gnu.org/licenses/gpl.txt)
5 " @Created: 2007-06-24.
6 " @Last Change: 2008-03-08.
9 if &cp || exists("loaded_tlib_agent_autoload") "{{{2
12 let loaded_tlib_agent_autoload = 1
15 " Various agents for use as key handlers in tlib#input#List()
19 function! tlib#agent#Exit(world, selected) "{{{3
20 call a:world.CloseScratch()
21 let a:world.state = 'exit escape'
23 " let a:world.base = []
24 call a:world.ResetSelected()
29 function! tlib#agent#CopyItems(world, selected) "{{{3
30 let @* = join(a:selected, "\n")
31 let a:world.state = 'redisplay'
37 " InputList related {{{1
39 function! tlib#agent#PageUp(world, selected) "{{{3
40 let a:world.offset -= (winheight(0) / 2)
41 let a:world.state = 'scroll'
46 function! tlib#agent#PageDown(world, selected) "{{{3
47 let a:world.offset += (winheight(0) / 2)
48 let a:world.state = 'scroll'
53 function! tlib#agent#Up(world, selected, ...) "{{{3
56 if a:world.prefidx > lines
57 let a:world.prefidx -= lines
59 let a:world.prefidx = len(a:world.list)
61 let a:world.state = 'redisplay'
66 function! tlib#agent#Down(world, selected, ...) "{{{3
69 if a:world.prefidx <= (len(a:world.list) - lines)
70 let a:world.prefidx += lines
72 let a:world.prefidx = 1
74 let a:world.state = 'redisplay'
79 function! tlib#agent#UpN(world, selected) "{{{3
80 return tlib#agent#Up(a:world, a:selected, g:tlib_scroll_lines)
84 function! tlib#agent#DownN(world, selected) "{{{3
85 return tlib#agent#Down(a:world, a:selected, g:tlib_scroll_lines)
89 function! tlib#agent#ShiftLeft(world, selected) "{{{3
90 let a:world.offset_horizontal -= (winwidth(0) / 2)
91 if a:world.offset_horizontal < 0
92 let a:world.offset_horizontal = 0
94 let a:world.state = 'display shift'
99 function! tlib#agent#ShiftRight(world, selected) "{{{3
100 let a:world.offset_horizontal += (winwidth(0) / 2)
101 let a:world.state = 'display shift'
106 function! tlib#agent#Reset(world, selected) "{{{3
107 let a:world.state = 'reset'
112 function! tlib#agent#Input(world, selected) "{{{3
113 let flt0 = a:world.filter[0][0]
114 let flt1 = input('Filter: ', flt0)
116 if flt1 != flt0 && !empty(flt1)
117 let a:world.filter[0][0] = flt1
119 let a:world.state = 'display'
124 " Suspend (see |tlib#agent#Suspend|) the input loop and jump back to the
125 " original position in the parent window.
126 function! tlib#agent#SuspendToParentWindow(world, selected) "{{{3
128 let winnr = world.win_wnr
131 let world = tlib#agent#Suspend(world, a:selected)
132 if world.state =~ '\<suspend\>'
133 call world.SwitchWindow('win')
134 " let pos = world.cursor
137 " call setpos('.', pos)
142 let world.state = 'redisplay'
147 " Suspend lets you temporarily leave the input loop of
148 " |tlib#input#List|. You can resume editing the list by pressing <c-z>,
149 " <m-z>. <cr>, <space>, or <LeftMouse> in the suspended window.
150 function! tlib#agent#Suspend(world, selected) "{{{3
151 if a:world.allow_suspend
152 " TAssert IsNotEmpty(a:world.scratch)
154 let br = tlib#buffer#Set(a:world.scratch)
155 " TLogVAR br, a:world.bufnr, a:world.scratch
157 let b:tlib_suspend = ['<m-z>', '<c-z>', '<cr>', '<space>', '<LeftMouse>']
158 for m in b:tlib_suspend
159 exec 'noremap <buffer> '. m .' :call tlib#input#Resume("world")<cr>'
161 let b:tlib_world = a:world
163 let a:world.state = 'exit suspend'
165 echom 'Suspend disabled'
166 let a:world.state = 'redisplay'
172 function! tlib#agent#Help(world, selected) "{{{3
173 let a:world.state = 'help'
178 function! tlib#agent#OR(world, selected) "{{{3
179 if !empty(a:world.filter[0])
180 call insert(a:world.filter[0], '')
182 let a:world.state = 'display'
187 function! tlib#agent#AND(world, selected) "{{{3
188 if !empty(a:world.filter[0])
189 call insert(a:world.filter, [''])
191 let a:world.state = 'display'
196 function! tlib#agent#ReduceFilter(world, selected) "{{{3
197 call a:world.ReduceFilter()
198 let a:world.offset = 1
199 let a:world.state = 'display'
204 function! tlib#agent#PopFilter(world, selected) "{{{3
205 call a:world.PopFilter()
206 let a:world.offset = 1
207 let a:world.state = 'display'
212 function! tlib#agent#Debug(world, selected) "{{{3
213 " echo string(world.state)
214 echo string(a:world.filter)
215 echo string(a:world.idx)
216 echo string(a:world.prefidx)
217 echo string(a:world.sel_idx)
219 let a:world.state = 'display'
224 function! tlib#agent#Select(world, selected) "{{{3
225 call a:world.SelectItem('toggle', a:world.prefidx)
226 " let a:world.state = 'display keepcursor'
227 let a:world.state = 'redisplay'
232 function! tlib#agent#SelectUp(world, selected) "{{{3
233 call a:world.SelectItem('toggle', a:world.prefidx)
234 if a:world.prefidx > 1
235 let a:world.prefidx -= 1
237 let a:world.state = 'redisplay'
242 function! tlib#agent#SelectDown(world, selected) "{{{3
243 call a:world.SelectItem('toggle', a:world.prefidx)
244 if a:world.prefidx < len(a:world.list)
245 let a:world.prefidx += 1
247 let a:world.state = 'redisplay'
252 function! tlib#agent#SelectAll(world, selected) "{{{3
253 let listrange = range(1, len(a:world.list))
254 let mode = empty(filter(copy(listrange), 'index(a:world.sel_idx, a:world.GetBaseIdx(v:val)) == -1'))
257 call a:world.SelectItem(mode, i)
259 let a:world.state = 'display keepcursor'
264 function! tlib#agent#ToggleStickyList(world, selected) "{{{3
265 let a:world.sticky = !a:world.sticky
266 let a:world.state = 'display keepcursor'
272 " EditList related {{{1
274 function! tlib#agent#EditItem(world, selected) "{{{3
275 let lidx = a:world.prefidx
277 " TLogVAR a:world.table
278 let bidx = a:world.GetBaseIdx(lidx)
280 let item = a:world.GetBaseItem(bidx)
281 let item = input(lidx .'@'. bidx .': ', item)
283 call a:world.SetBaseItem(bidx, item)
285 let a:world.state = 'display'
290 function! tlib#agent#NewItem(world, selected) "{{{3
291 let basepi = a:world.GetBaseIdx(a:world.prefidx)
292 let item = input('New item: ')
293 call insert(a:world.base, item, basepi)
294 let a:world.state = 'reset'
299 function! tlib#agent#DeleteItems(world, selected) "{{{3
300 let remove = copy(a:world.sel_idx)
301 let basepi = a:world.GetBaseIdx(a:world.prefidx)
302 if index(remove, basepi) == -1
303 call add(remove, basepi)
305 " call map(remove, 'a:world.GetBaseIdx(v:val)')
306 for idx in reverse(sort(remove))
307 call remove(a:world.base, idx - 1)
309 let a:world.state = 'display'
310 call a:world.ResetSelected()
311 " let a:world.state = 'reset'
316 function! tlib#agent#Cut(world, selected) "{{{3
317 let world = tlib#agent#Copy(a:world, a:selected)
318 return tlib#agent#DeleteItems(world, a:selected)
322 function! tlib#agent#Copy(world, selected) "{{{3
323 let a:world.clipboard = []
324 let bidxs = copy(a:world.sel_idx)
325 call add(bidxs, a:world.GetBaseIdx(a:world.prefidx))
326 for bidx in sort(bidxs)
327 call add(a:world.clipboard, a:world.GetBaseItem(bidx))
329 let a:world.state = 'redisplay'
334 function! tlib#agent#Paste(world, selected) "{{{3
335 if has_key(a:world, 'clipboard')
336 for e in reverse(copy(a:world.clipboard))
337 call insert(a:world.base, e, a:world.prefidx)
340 let a:world.state = 'display'
341 call a:world.ResetSelected()
346 function! tlib#agent#EditReturnValue(world, rv) "{{{3
347 return [a:world.state !~ '\<exit\>', a:world.base]
354 function! tlib#agent#ViewFile(world, selected) "{{{3
355 let back = a:world.SwitchWindow('win')
356 call tlib#file#With('edit', 'buffer', a:selected, a:world)
358 let a:world.state = 'display'
363 function! tlib#agent#EditFile(world, selected) "{{{3
364 return tlib#agent#Exit(tlib#agent#ViewFile(a:world, a:selected), a:selected)
368 function! tlib#agent#EditFileInSplit(world, selected) "{{{3
369 call a:world.CloseScratch()
370 " call tlib#file#With('edit', 'buffer', a:selected[0:0], a:world)
371 " call tlib#file#With('split', 'sbuffer', a:selected[1:-1], a:world)
372 call tlib#file#With('split', 'sbuffer', a:selected, a:world)
373 return tlib#agent#Exit(a:world, a:selected)
377 function! tlib#agent#EditFileInVSplit(world, selected) "{{{3
378 call a:world.CloseScratch()
379 " call tlib#file#With('edit', 'buffer', a:selected[0:0], a:world)
380 " call tlib#file#With('vertical split', 'vertical sbuffer', a:selected[1:-1], a:world)
381 call tlib#file#With('vertical split', 'vertical sbuffer', a:selected, a:world)
382 return tlib#agent#Exit(a:world, a:selected)
386 function! tlib#agent#EditFileInTab(world, selected) "{{{3
387 call a:world.CloseScratch()
388 call tlib#file#With('tabedit', 'tab sbuffer', a:selected, a:world)
389 return tlib#agent#Exit(a:world, a:selected)
393 function! tlib#agent#ToggleScrollbind(world, selected) "{{{3
394 let a:world.scrollbind = get(a:world, 'scrollbind') ? 0 : 1
395 let a:world.state = 'redisplay'
399 function! tlib#agent#ShowInfo(world, selected)
402 let desc = [getfperm(f), strftime('%c', getftime(f)), getfsize(f) .' bytes', getftype(f)]
403 echo fnamemodify(f, ':t') .':'
404 echo ' '. join(desc, '; ')
408 echo 'Press any key to continue'
411 let a:world.state = 'redisplay'
417 " Buffer related {{{1
419 function! tlib#agent#PreviewLine(world, selected) "{{{3
420 let l = a:selected[0]
422 exec a:world.win_wnr .'wincmd w'
423 call tlib#buffer#ViewLine(l)
425 let a:world.state = 'redisplay'
430 " If not called from the scratch, we assume/guess that we don't have to
431 " suspend the input-evaluation loop.
432 function! tlib#agent#GotoLine(world, selected) "{{{3
433 if !empty(a:selected)
434 let l = a:selected[0]
435 if a:world.win_wnr != winnr()
436 let world = tlib#agent#Suspend(a:world, a:selected)
437 exec a:world.win_wnr .'wincmd w'
439 call tlib#buffer#ViewLine(l)
445 function! tlib#agent#DoAtLine(world, selected) "{{{3
446 if !empty(a:selected)
447 let cmd = input('Command: ', '', 'command')
449 call a:world.SwitchWindow('win')
450 let pos = getpos('.')
452 call tlib#buffer#ViewLine(l, '')
455 call setpos('.', pos)
458 call a:world.ResetSelected()
459 let a:world.state = 'exit'