[ADD] conf for archlinux, <init>
[arrow.git] / archlinux_conf / home / .vim / autoload / tlib / agent.vim
blob11c17b9eb86a1db0fa0ef62a32138445450ad35e
1 " agent.vim
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.
7 " @Revision:    0.1.140
9 if &cp || exists("loaded_tlib_agent_autoload") "{{{2
10     finish
11 endif
12 let loaded_tlib_agent_autoload = 1
14 " :filedoc:
15 " Various agents for use as key handlers in tlib#input#List()
17 " General {{{1
19 function! tlib#agent#Exit(world, selected) "{{{3
20     call a:world.CloseScratch()
21     let a:world.state = 'exit escape'
22     let a:world.list = []
23     " let a:world.base = []
24     call a:world.ResetSelected()
25     return a:world
26 endf
29 function! tlib#agent#CopyItems(world, selected) "{{{3
30     let @* = join(a:selected, "\n")
31     let a:world.state = 'redisplay'
32     return a:world
33 endf
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'
42     return a:world
43 endf
46 function! tlib#agent#PageDown(world, selected) "{{{3
47     let a:world.offset += (winheight(0) / 2)
48     let a:world.state = 'scroll'
49     return a:world
50 endf
53 function! tlib#agent#Up(world, selected, ...) "{{{3
54     TVarArg ['lines', 1]
55     let a:world.idx = ''
56     if a:world.prefidx > lines
57         let a:world.prefidx -= lines
58     else
59         let a:world.prefidx = len(a:world.list)
60     endif
61     let a:world.state = 'redisplay'
62     return a:world
63 endf
66 function! tlib#agent#Down(world, selected, ...) "{{{3
67     TVarArg ['lines', 1]
68     let a:world.idx = ''
69     if a:world.prefidx <= (len(a:world.list) - lines)
70         let a:world.prefidx += lines
71     else
72         let a:world.prefidx = 1
73     endif
74     let a:world.state = 'redisplay'
75     return a:world
76 endf
79 function! tlib#agent#UpN(world, selected) "{{{3
80     return tlib#agent#Up(a:world, a:selected, g:tlib_scroll_lines)
81 endf
84 function! tlib#agent#DownN(world, selected) "{{{3
85     return tlib#agent#Down(a:world, a:selected, g:tlib_scroll_lines)
86 endf
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
93     endif
94     let a:world.state = 'display shift'
95     return a:world
96 endf
99 function! tlib#agent#ShiftRight(world, selected) "{{{3
100     let a:world.offset_horizontal += (winwidth(0) / 2)
101     let a:world.state = 'display shift'
102     return a:world
103 endf
106 function! tlib#agent#Reset(world, selected) "{{{3
107     let a:world.state = 'reset'
108     return a:world
109 endf
112 function! tlib#agent#Input(world, selected) "{{{3
113     let flt0 = a:world.filter[0][0]
114     let flt1 = input('Filter: ', flt0)
115     echo
116     if flt1 != flt0 && !empty(flt1)
117         let a:world.filter[0][0] = flt1
118     endif
119     let a:world.state = 'display'
120     return a:world
121 endf
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
127     let world = a:world
128     let winnr = world.win_wnr
129     " TLogVAR winnr
130     if winnr != -1
131         let world = tlib#agent#Suspend(world, a:selected)
132         if world.state =~ '\<suspend\>'
133             call world.SwitchWindow('win')
134             " let pos = world.cursor
135             " " TLogVAR pos
136             " if !empty(pos)
137             "     call setpos('.', pos)
138             " endif
139             return world
140         endif
141     endif
142     let world.state = 'redisplay'
143     return world
144 endf
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)
153         " TLogDBG bufnr('%')
154         let br = tlib#buffer#Set(a:world.scratch)
155         " TLogVAR br, a:world.bufnr, a:world.scratch
156         " TLogDBG bufnr('%')
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>'
160         endfor
161         let b:tlib_world = a:world
162         exec br
163         let a:world.state = 'exit suspend'
164     else
165         echom 'Suspend disabled'
166         let a:world.state = 'redisplay'
167     endif
168     return a:world
169 endf
172 function! tlib#agent#Help(world, selected) "{{{3
173     let a:world.state = 'help'
174     return a:world
175 endf
178 function! tlib#agent#OR(world, selected) "{{{3
179     if !empty(a:world.filter[0])
180         call insert(a:world.filter[0], '')
181     endif
182     let a:world.state = 'display'
183     return a:world
184 endf
187 function! tlib#agent#AND(world, selected) "{{{3
188     if !empty(a:world.filter[0])
189         call insert(a:world.filter, [''])
190     endif
191     let a:world.state = 'display'
192     return a:world
193 endf
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'
200     return a:world
201 endf
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'
208     return a:world
209 endf
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)
218     call getchar()
219     let a:world.state = 'display'
220     return a:world
221 endf
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'
228     return a:world
229 endf
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
236     endif
237     let a:world.state = 'redisplay'
238     return a:world
239 endf
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
246     endif
247     let a:world.state = 'redisplay'
248     return a:world
249 endf
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'))
255                 \ ? 'toggle' : 'set'
256     for i in listrange
257         call a:world.SelectItem(mode, i)
258     endfor
259     let a:world.state = 'display keepcursor'
260     return a:world
261 endf
264 function! tlib#agent#ToggleStickyList(world, selected) "{{{3
265     let a:world.sticky = !a:world.sticky
266     let a:world.state = 'display keepcursor'
267     return a:world
268 endf
272 " EditList related {{{1
274 function! tlib#agent#EditItem(world, selected) "{{{3
275     let lidx = a:world.prefidx
276     " TLogVAR lidx
277     " TLogVAR a:world.table
278     let bidx = a:world.GetBaseIdx(lidx)
279     " TLogVAR bidx
280     let item = a:world.GetBaseItem(bidx)
281     let item = input(lidx .'@'. bidx .': ', item)
282     if item != ''
283         call a:world.SetBaseItem(bidx, item)
284     endif
285     let a:world.state = 'display'
286     return a:world
287 endf
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'
295     return a:world
296 endf
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)
304     endif
305     " call map(remove, 'a:world.GetBaseIdx(v:val)')
306     for idx in reverse(sort(remove))
307         call remove(a:world.base, idx - 1)
308     endfor
309     let a:world.state = 'display'
310     call a:world.ResetSelected()
311     " let a:world.state = 'reset'
312     return a:world
313 endf
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)
319 endf
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))
328     endfor
329     let a:world.state = 'redisplay'
330     return a:world
331 endf
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)
338         endfor
339     endif
340     let a:world.state = 'display'
341     call a:world.ResetSelected()
342     return a:world
343 endf
346 function! tlib#agent#EditReturnValue(world, rv) "{{{3
347     return [a:world.state !~ '\<exit\>', a:world.base]
348 endf
352 " Files related {{{1
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)
357     exec back
358     let a:world.state = 'display'
359     return a:world
360 endf
363 function! tlib#agent#EditFile(world, selected) "{{{3
364     return tlib#agent#Exit(tlib#agent#ViewFile(a:world, a:selected), a:selected)
365 endf
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)
374 endf
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)
383 endf
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)
390 endf
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'
396     return a:world
397 endf
399 function! tlib#agent#ShowInfo(world, selected)
400     for f in a:selected
401         if filereadable(f)
402             let desc = [getfperm(f), strftime('%c', getftime(f)),  getfsize(f) .' bytes', getftype(f)]
403             echo fnamemodify(f, ':t') .':'
404             echo '  '. join(desc, '; ')
405         endif
406     endfor
407     echohl MoreMsg
408     echo 'Press any key to continue'
409     echohl NONE
410     call getchar()
411     let a:world.state = 'redisplay'
412     return a:world
413 endf
417 " Buffer related {{{1
419 function! tlib#agent#PreviewLine(world, selected) "{{{3
420     let l = a:selected[0]
421     let ww = winnr()
422     exec a:world.win_wnr .'wincmd w'
423     call tlib#buffer#ViewLine(l)
424     exec ww .'wincmd w'
425     let a:world.state = 'redisplay'
426     return a:world
427 endf
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'
438         endif
439         call tlib#buffer#ViewLine(l)
440     endif
441     return a:world
442 endf
445 function! tlib#agent#DoAtLine(world, selected) "{{{3
446     if !empty(a:selected)
447         let cmd = input('Command: ', '', 'command')
448         if !empty(cmd)
449             call a:world.SwitchWindow('win')
450             let pos = getpos('.')
451             for l in a:selected
452                 call tlib#buffer#ViewLine(l, '')
453                 exec cmd
454             endfor
455             call setpos('.', pos)
456         endif
457     endif
458     call a:world.ResetSelected()
459     let a:world.state = 'exit'
460     return a:world
461 endf