[ADD] add .vim conf, replace old _vim, some xinitrc change, will use xsession later.
[arrow.git] / conf_slk120 / vim / _vim / autoload / tlib / buffer.vim
blob24ceb3c6a3438c62dc4d47f0d3a5693f062ddaa1
1 " buffer.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-30.
6 " @Last Change: 2007-11-16.
7 " @Revision:    0.0.238
9 if &cp || exists("loaded_tlib_buffer_autoload")
10     finish
11 endif
12 let loaded_tlib_buffer_autoload = 1
15 " Set the buffer to buffer and return a command as string that can be 
16 " evaluated by |:execute| in order to restore the original view.
17 function! tlib#buffer#Set(buffer) "{{{3
18     let lazyredraw = &lazyredraw
19     set lazyredraw
20     try
21         let cb = bufnr('%')
22         let sn = bufnr(a:buffer)
23         if sn != cb
24             let ws = bufwinnr(sn)
25             if ws != -1
26                 let wb = bufwinnr('%')
27                 exec ws.'wincmd w'
28                 return wb.'wincmd w'
29             else
30                 silent exec 'sbuffer! '. sn
31                 return 'wincmd c'
32             endif
33         else
34             return ''
35         endif
36     finally
37         let &lazyredraw = lazyredraw
38     endtry
39 endf
42 " :def: function! tlib#buffer#Eval(buffer, code)
43 " Evaluate CODE in BUFFER.
45 " EXAMPLES: >
46 "   call tlib#buffer#Eval('foo.txt', 'echo b:bar')
47 function! tlib#buffer#Eval(buffer, code) "{{{3
48     " let cb = bufnr('%')
49     " let wb = bufwinnr('%')
50     " " TLogVAR cb
51     " let sn = bufnr(a:buffer)
52     " let sb = sn != cb
53     let lazyredraw = &lazyredraw
54     set lazyredraw
55     let restore = tlib#buffer#Set(a:buffer)
56     try
57         exec a:code
58         " if sb
59         "     let ws = bufwinnr(sn)
60         "     if ws != -1
61         "         try
62         "             exec ws.'wincmd w'
63         "             exec a:code
64         "         finally
65         "             exec wb.'wincmd w'
66         "         endtry
67         "     else
68         "         try
69         "             silent exec 'sbuffer! '. sn
70         "             exec a:code
71         "         finally
72         "             wincmd c
73         "         endtry
74         "     endif
75         " else
76         "     exec a:code
77         " endif
78     finally
79         exec restore
80         let &lazyredraw = lazyredraw
81     endtry
82 endf
85 " :def: function! tlib#buffer#GetList(?show_hidden=0, ?show_number=0)
86 function! tlib#buffer#GetList(...)
87     TVarArg ['show_hidden', 0], ['show_number', 0]
88     let ls_bang = show_hidden ? '!' : ''
89     redir => bfs
90     exec 'silent ls'. ls_bang
91     redir END
92     let buffer_list = split(bfs, '\n')
93     let buffer_nr = map(copy(buffer_list), 'matchstr(v:val, ''\s*\zs\d\+\ze'')')
94     " TLogVAR buffer_list
95     if show_number
96         call map(buffer_list, 'matchstr(v:val, ''\s*\d\+.\{-}\ze\s\+line \d\+\s*$'')')
97     else
98         call map(buffer_list, 'matchstr(v:val, ''\s*\d\+\zs.\{-}\ze\s\+line \d\+\s*$'')')
99     endif
100     " TLogVAR buffer_list
101     " call map(buffer_list, 'matchstr(v:val, ''^.\{-}\ze\s\+line \d\+\s*$'')')
102     " TLogVAR buffer_list
103     call map(buffer_list, 'matchstr(v:val, ''^[^"]\+''). printf("%-20s   %s", fnamemodify(matchstr(v:val, ''"\zs.\{-}\ze"$''), ":t"), fnamemodify(matchstr(v:val, ''"\zs.\{-}\ze"$''), ":h"))')
104     " TLogVAR buffer_list
105     return [buffer_nr, buffer_list]
106 endf
109 " :def: function! tlib#buffer#ViewLine(line, ?position='z')
110 " line is either a number or a string that begins with a number.
111 " For possible values for position see |scroll-cursor|.
112 " See also |g:tlib_viewline_position|.
113 function! tlib#buffer#ViewLine(line, ...) "{{{3
114     if a:line
115         TVarArg 'pos'
116         let ln = matchstr(a:line, '^\d\+')
117         let lt = matchstr(a:line, '^\d\+: \zs.*')
118         " TLogVAR pos, ln, lt
119         exec ln
120         if empty(pos)
121             let pos = tlib#var#Get('tlib_viewline_position', 'wbg')
122         endif
123         " TLogVAR pos
124         if !empty(pos)
125             exec 'norm! '. pos
126         endif
127         let @/ = '\%'. ln .'l.*'
128     endif
129 endf
132 function! tlib#buffer#HighlightLine(line) "{{{3
133     exec 'match MatchParen /\V\%'. a:line .'l.*/'
134 endf
137 " Delete the lines in the current buffer. Wrapper for |:delete|.
138 function! tlib#buffer#DeleteRange(line1, line2) "{{{3
139     exec a:line1.','.a:line2.'delete'
140 endf
143 " Replace a range of lines.
144 function! tlib#buffer#ReplaceRange(line1, line2, lines)
145     call tlib#buffer#DeleteRange(a:line1, a:line2)
146     call append(a:line1 - 1, a:lines)
147 endf
150 " Initialize some scratch area at the bottom of the current buffer.
151 function! tlib#buffer#ScratchStart() "{{{3
152     norm! Go
153     let b:tlib_inbuffer_scratch = line('$')
154     return b:tlib_inbuffer_scratch
155 endf
158 " Remove the in-buffer scratch area.
159 function! tlib#buffer#ScratchEnd() "{{{3
160     if !exists('b:tlib_inbuffer_scratch')
161         echoerr 'tlib: In-buffer scratch not initalized'
162     endif
163     call tlib#buffer#DeleteRange(b:tlib_inbuffer_scratch, line('$'))
164     unlet b:tlib_inbuffer_scratch
165 endf
168 " Run exec on all buffers via bufdo and return to the original buffer.
169 function! tlib#buffer#BufDo(exec) "{{{3
170     let bn = bufnr('%')
171     exec 'bufdo '. a:exec
172     exec 'buffer! '. bn
173 endf
176 " :def: function! tlib#buffer#InsertText(text, keyargs)
177 " Keyargs:
178 "   'shift': 0|N
179 "   'col': col('.')|N
180 "   'lineno': line('.')|N
181 "   'indent': 0|1
182 "   'pos': 'e'|'s' ... Where to locate the cursor (somewhat like s and e in {offset})
183 " Insert text (a string) in the buffer.
184 function! tlib#buffer#InsertText(text, ...) "{{{3
185     TVarArg ['keyargs', {}]
186     TKeyArg keyargs, ['shift', 0], ['col', col('.')], ['lineno', line('.')], ['pos', 'e'],
187                 \ ['indent', 0]
188     " TLogVAR shift, col, lineno, pos, indent
189     let line = getline(lineno)
190     if col + shift > 0
191         let pre  = line[0 : (col - 1 + shift)]
192         let post = line[(col + shift): -1]
193     else
194         let pre  = ''
195         let post = line
196     endif
197     " TLogVAR lineno, line, pre, post
198     let text0 = pre . a:text . post
199     let text  = split(text0, '\n', 1)
200     " TLogVAR text
201     let icol = len(pre)
202     " exec 'norm! '. lineno .'G'
203     call cursor(lineno, col)
204     if indent && col > 1
205                 if &fo =~# '[or]'
206                         " This doesn't work because it's not guaranteed that the 
207                         " cursor is set.
208                         let cline = getline('.')
209                         norm! a\r
210                         "norm! o
211                         " TAssertExec redraw | sleep 3
212                         let idt = strpart(getline('.'), 0, col('.') + shift)
213                         " TLogVAR idt
214                         let idtl = len(idt)
215                         -1,.delete
216                         " TAssertExec redraw | sleep 3
217                         call append(lineno - 1, cline)
218                         call cursor(lineno, col)
219                         " TAssertExec redraw | sleep 3
220                         if idtl == 0 && icol != 0
221                                 let idt = matchstr(pre, '^\s\+')
222                                 let idtl = len(idt)
223                         endif
224                 else
225                         let [m_0, idt, iline; rest] = matchlist(pre, '^\(\s*\)\(.*\)$')
226                         let idtl = len(idt)
227                 endif
228                 if idtl < icol
229                         let idt .= repeat(' ', icol - idtl)
230                 endif
231         " TLogVAR idt
232         for i in range(1, len(text) - 1)
233             let text[i] = idt . text[i]
234         endfor
235         " TLogVAR text
236     endif
237     " exec 'norm! '. lineno .'Gdd'
238     norm! dd
239     call append(lineno - 1, text)
240     let tlen = len(text)
241     let posshift = matchstr(pos, '\d\+')
242     if pos =~ '^e'
243         exec lineno + tlen - 1
244         exec 'norm! 0'. (len(text[-1]) - len(post) + posshift - 1) .'l'
245     elseif pos =~ '^s'
246         exec lineno
247         exec 'norm! '. len(pre) .'|'
248         if !empty(posshift)
249             exec 'norm! '. posshift .'h'
250         endif
251     endif
252     " TLogDBG string(getline(1, '$'))
253 endf
256 function! tlib#buffer#InsertText0(text, ...) "{{{3
257     TVarArg ['keyargs', {}]
258     let mode = get(keyargs, 'mode', 'i')
259     " TLogVAR mode
260     if !has_key(keyargs, 'shift')
261         let col = col('.')
262         " if mode =~ 'i'
263         "     let col += 1
264         " endif
265         let keyargs.shift = col >= col('$') ? 0 : -1
266         " let keyargs.shift = col('.') >= col('$') ? 0 : -1
267         " TLogVAR col
268         " TLogDBG col('.') .'-'. col('$') .': '. string(getline('.'))
269     endif
270     " TLogVAR keyargs.shift
271     return tlib#buffer#InsertText(a:text, keyargs)
272 endf
275 function! tlib#buffer#CurrentByte() "{{{3
276     return line2byte(line('.')) + col('.')
277 endf