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.
9 if &cp || exists("loaded_tlib_buffer_autoload")
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
22 let sn = bufnr(a:buffer)
26 let wb = bufwinnr('%')
30 silent exec 'sbuffer! '. sn
37 let &lazyredraw = lazyredraw
42 " :def: function! tlib#buffer#Eval(buffer, code)
43 " Evaluate CODE in BUFFER.
46 " call tlib#buffer#Eval('foo.txt', 'echo b:bar')
47 function! tlib#buffer#Eval(buffer, code) "{{{3
49 " let wb = bufwinnr('%')
51 " let sn = bufnr(a:buffer)
53 let lazyredraw = &lazyredraw
55 let restore = tlib#buffer#Set(a:buffer)
59 " let ws = bufwinnr(sn)
69 " silent exec 'sbuffer! '. sn
80 let &lazyredraw = lazyredraw
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 ? '!' : ''
90 exec 'silent ls'. ls_bang
92 let buffer_list = split(bfs, '\n')
93 let buffer_nr = map(copy(buffer_list), 'matchstr(v:val, ''\s*\zs\d\+\ze'')')
96 call map(buffer_list, 'matchstr(v:val, ''\s*\d\+.\{-}\ze\s\+line \d\+\s*$'')')
98 call map(buffer_list, 'matchstr(v:val, ''\s*\d\+\zs.\{-}\ze\s\+line \d\+\s*$'')')
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]
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
116 let ln = matchstr(a:line, '^\d\+')
117 let lt = matchstr(a:line, '^\d\+: \zs.*')
118 " TLogVAR pos, ln, lt
121 let pos = tlib#var#Get('tlib_viewline_position', 'wbg')
127 let @/ = '\%'. ln .'l.*'
132 function! tlib#buffer#HighlightLine(line) "{{{3
133 exec 'match MatchParen /\V\%'. a:line .'l.*/'
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'
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)
150 " Initialize some scratch area at the bottom of the current buffer.
151 function! tlib#buffer#ScratchStart() "{{{3
153 let b:tlib_inbuffer_scratch = line('$')
154 return b:tlib_inbuffer_scratch
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'
163 call tlib#buffer#DeleteRange(b:tlib_inbuffer_scratch, line('$'))
164 unlet b:tlib_inbuffer_scratch
168 " Run exec on all buffers via bufdo and return to the original buffer.
169 function! tlib#buffer#BufDo(exec) "{{{3
171 exec 'bufdo '. a:exec
176 " :def: function! tlib#buffer#InsertText(text, keyargs)
180 " 'lineno': line('.')|N
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'],
188 " TLogVAR shift, col, lineno, pos, indent
189 let line = getline(lineno)
191 let pre = line[0 : (col - 1 + shift)]
192 let post = line[(col + shift): -1]
197 " TLogVAR lineno, line, pre, post
198 let text0 = pre . a:text . post
199 let text = split(text0, '\n', 1)
202 " exec 'norm! '. lineno .'G'
203 call cursor(lineno, col)
206 " This doesn't work because it's not guaranteed that the
208 let cline = getline('.')
211 " TAssertExec redraw | sleep 3
212 let idt = strpart(getline('.'), 0, col('.') + shift)
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\+')
225 let [m_0, idt, iline; rest] = matchlist(pre, '^\(\s*\)\(.*\)$')
229 let idt .= repeat(' ', icol - idtl)
232 for i in range(1, len(text) - 1)
233 let text[i] = idt . text[i]
237 " exec 'norm! '. lineno .'Gdd'
239 call append(lineno - 1, text)
241 let posshift = matchstr(pos, '\d\+')
243 exec lineno + tlen - 1
244 exec 'norm! 0'. (len(text[-1]) - len(post) + posshift - 1) .'l'
247 exec 'norm! '. len(pre) .'|'
249 exec 'norm! '. posshift .'h'
252 " TLogDBG string(getline(1, '$'))
256 function! tlib#buffer#InsertText0(text, ...) "{{{3
257 TVarArg ['keyargs', {}]
258 let mode = get(keyargs, 'mode', 'i')
260 if !has_key(keyargs, 'shift')
265 let keyargs.shift = col >= col('$') ? 0 : -1
266 " let keyargs.shift = col('.') >= col('$') ? 0 : -1
268 " TLogDBG col('.') .'-'. col('$') .': '. string(getline('.'))
270 " TLogVAR keyargs.shift
271 return tlib#buffer#InsertText(a:text, keyargs)
275 function! tlib#buffer#CurrentByte() "{{{3
276 return line2byte(line('.')) + col('.')