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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
" buffer.vim
" @Author: Tom Link (micathom AT gmail com?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-30.
" @Last Change: 2015-11-06.
" @Revision: 7.1.352
" Where to display the line when using |tlib#buffer#ViewLine|.
" For possible values for position see |scroll-cursor|.
TLet g:tlib_viewline_position = 'zz'
let s:bmru = []
function! tlib#buffer#EnableMRU() "{{{3
call tlib#autocmdgroup#Init()
autocmd TLib BufEnter * call s:BMRU_Push(bufnr('%'))
endf
function! tlib#buffer#DisableMRU() "{{{3
call tlib#autocmdgroup#Init()
autocmd! TLib BufEnter
endf
function! s:BMRU_Push(bnr) "{{{3
let i = index(s:bmru, a:bnr)
if i >= 0
call remove(s:bmru, i)
endif
call insert(s:bmru, a:bnr)
endf
function! s:CompareBuffernameByBasename(a, b) "{{{3
let rx = '"\zs.\{-}\ze" \+\S\+ \+\d\+$'
let an = matchstr(a:a, rx)
let an = fnamemodify(an, ':t')
let bn = matchstr(a:b, rx)
let bn = fnamemodify(bn, ':t')
let rv = an == bn ? 0 : an > bn ? 1 : -1
return rv
endf
function! s:CompareBufferNrByMRU(a, b) "{{{3
let an = matchstr(a:a, '\s*\zs\d\+\ze')
let bn = matchstr(a:b, '\s*\zs\d\+\ze')
let ai = index(s:bmru, 0 + an)
if ai == -1
return 1
else
let bi = index(s:bmru, 0 + bn)
if bi == -1
return -1
else
return ai == bi ? 0 : ai > bi ? 1 : -1
endif
endif
endf
" Set the buffer to buffer and return a command as string that can be
" evaluated by |:execute| in order to restore the original view.
function! tlib#buffer#Set(buffer) "{{{3
let lazyredraw = &lazyredraw
set lazyredraw
try
let cb = bufnr('%')
let sn = bufnr(a:buffer)
if sn != cb
let ws = bufwinnr(sn)
if ws != -1
let wb = bufwinnr('%')
exec ws.'wincmd w'
return wb.'wincmd w'
else
silent exec 'sbuffer! '. sn
return 'wincmd c'
endif
else
return ''
endif
finally
let &lazyredraw = lazyredraw
endtry
endf
" :def: function! tlib#buffer#Eval(buffer, code)
" Evaluate CODE in BUFFER.
"
" EXAMPLES: >
" call tlib#buffer#Eval('foo.txt', 'echo b:bar')
function! tlib#buffer#Eval(buffer, code) "{{{3
" let cb = bufnr('%')
" let wb = bufwinnr('%')
" " TLogVAR cb
" let sn = bufnr(a:buffer)
" let sb = sn != cb
let lazyredraw = &lazyredraw
set lazyredraw
let restore = tlib#buffer#Set(a:buffer)
try
exec a:code
" if sb
" let ws = bufwinnr(sn)
" if ws != -1
" try
" exec ws.'wincmd w'
" exec a:code
" finally
" exec wb.'wincmd w'
" endtry
" else
" try
" silent exec 'sbuffer! '. sn
" exec a:code
" finally
" wincmd c
" endtry
" endif
" else
" exec a:code
" endif
finally
exec restore
let &lazyredraw = lazyredraw
endtry
endf
" :def: function! tlib#buffer#GetList(?show_hidden=0, ?show_number=0, " ?order='bufnr')
" Possible values for the "order" argument:
" bufnr :: Default behaviour
" mru :: Sort buffers according to most recent use
" basename :: Sort by the file's basename (last component)
"
" NOTE: MRU order works on second invocation only. If you want to always
" use MRU order, call tlib#buffer#EnableMRU() in your ~/.vimrc file.
function! tlib#buffer#GetList(...)
TVarArg ['show_hidden', 0], ['show_number', 0], ['order', '']
" TLogVAR show_hidden, show_number, order
let ls_bang = show_hidden ? '!' : ''
redir => bfs
exec 'silent ls'. ls_bang
redir END
let buffer_list = split(bfs, '\n')
if order == 'mru'
if empty(s:bmru)
call tlib#buffer#EnableMRU()
echom 'tlib: Installed Buffer MRU logger; disable with: call tlib#buffer#DisableMRU()'
else
call sort(buffer_list, function('s:CompareBufferNrByMRU'))
endif
elseif order == 'basename'
call sort(buffer_list, function('s:CompareBuffernameByBasename'))
endif
let buffer_nr = map(copy(buffer_list), 'str2nr(matchstr(v:val, ''\s*\zs\d\+\ze''))')
" TLogVAR buffer_list, buffer_nr
if show_number
call map(buffer_list, 'matchstr(v:val, ''^\s*\d\+.\{-}\ze\s\+\S\+ \d\+\s*$'')')
else
call map(buffer_list, 'matchstr(v:val, ''^\s*\d\+\zs.\{-}\ze\s\+\S\+ \d\+\s*$'')')
endif
" TLogVAR buffer_list
" call map(buffer_list, 'matchstr(v:val, ''^.\{-}\ze\s\+line \d\+\s*$'')')
" TLogVAR buffer_list
call map(buffer_list, 'matchstr(v:val, ''^[^"]\+''). printf("%-20s %s", fnamemodify(matchstr(v:val, ''"\zs.\{-}\ze"$''), ":t"), fnamemodify(matchstr(v:val, ''"\zs.\{-}\ze"$''), ":h"))')
" TLogVAR buffer_list
return [buffer_nr, buffer_list]
endf
" :def: function! tlib#buffer#ViewLine(line, ?position='z')
" line is either a number or a string that begins with a number.
" For possible values for position see |scroll-cursor|.
" See also |g:tlib_viewline_position|.
function! tlib#buffer#ViewLine(line, ...) "{{{3
if a:line
TVarArg 'pos'
let ln = matchstr(a:line, '^\d\+')
let lt = matchstr(a:line, '^\d\+: \zs.*')
" TLogVAR pos, ln, lt
exec ln
if empty(pos)
let pos = tlib#var#Get('tlib_viewline_position', 'wbg')
endif
" TLogVAR pos
if !empty(pos)
exec 'norm! '. pos
endif
call tlib#buffer#HighlightLine(ln)
" let @/ = '\%'. ln .'l.*'
endif
endf
function! s:UndoHighlightLine() "{{{3
2match none
autocmd! TLib CursorMoved,CursorMovedI <buffer>
autocmd! TLib CursorHold,CursorHoldI <buffer>
autocmd! TLib InsertEnter,InsertChange,InsertLeave <buffer>
autocmd! TLib BufLeave,BufWinLeave,WinLeave,BufHidden <buffer>
endf
function! tlib#buffer#HighlightLine(...) "{{{3
TVarArg ['line', line('.')]
" exec '2match MatchParen /^\%'. a:line .'l.*/'
exec '2match Search /^\%'. line .'l.*/'
call tlib#autocmdgroup#Init()
exec 'autocmd TLib CursorMoved,CursorMovedI <buffer> if line(".") != '. line .' | call s:UndoHighlightLine() | endif'
autocmd TLib CursorHold,CursorHoldI <buffer> call s:UndoHighlightLine()
autocmd TLib InsertEnter <buffer> call s:UndoHighlightLine()
" autocmd TLib BufLeave,BufWinLeave,WinLeave,BufHidden <buffer> call s:UndoHighlightLine()
endf
" Delete the lines in the current buffer. Wrapper for |:delete|.
function! tlib#buffer#DeleteRange(line1, line2) "{{{3
let r = @t
try
exec a:line1.','.a:line2.'delete t'
finally
let @t = r
endtry
endf
" Replace a range of lines.
function! tlib#buffer#ReplaceRange(line1, line2, lines)
call tlib#buffer#DeleteRange(a:line1, a:line2)
call append(a:line1 - 1, a:lines)
endf
" Initialize some scratch area at the bottom of the current buffer.
function! tlib#buffer#ScratchStart() "{{{3
norm! Go
let b:tlib_inbuffer_scratch = line('$')
return b:tlib_inbuffer_scratch
endf
" Remove the in-buffer scratch area.
function! tlib#buffer#ScratchEnd() "{{{3
if !exists('b:tlib_inbuffer_scratch')
echoerr 'tlib: In-buffer scratch not initalized'
endif
call tlib#buffer#DeleteRange(b:tlib_inbuffer_scratch, line('$'))
unlet b:tlib_inbuffer_scratch
endf
" Run exec on all buffers via bufdo and return to the original buffer.
function! tlib#buffer#BufDo(exec) "{{{3
let bn = bufnr('%')
exec 'bufdo '. a:exec
exec 'buffer! '. bn
endf
" :def: function! tlib#buffer#InsertText(text, keyargs)
" Keyargs:
" 'shift': 0|N
" 'col': col('.')|N
" 'lineno': line('.')|N
" 'indent': 0|1
" 'pos': 'e'|'s' ... Where to locate the cursor (somewhat like s and e in {offset})
" Insert text (a string) in the buffer.
function! tlib#buffer#InsertText(text, ...) "{{{3
TVarArg ['keyargs', {}]
" TLogVAR a:text, keyargs
let keyargs = extend({
\ 'shift': 0, 'col': col('.'), 'lineno': line('.'), 'pos': 'e', 'indent': 0
\ }, keyargs)
" TLogVAR keyargs
let grow = 0
let post_del_last_line = line('$') == 1
let line = getline(keyargs.lineno)
if keyargs.col + keyargs.shift > 0
let pre = line[0 : (keyargs.col - 1 + keyargs.shift)]
let post = line[(keyargs.col + keyargs.shift): -1]
else
let pre = ''
let post = line
endif
" TLogVAR keyargs.lineno, line, pre, post
let text0 = pre . a:text . post
let text = split(text0, '\n', 1)
" TLogVAR text
let icol = len(pre)
" exec 'norm! '. keyargs.lineno .'G'
call cursor(keyargs.lineno, keyargs.col)
if keyargs.indent && keyargs.col > 1
if &fo =~# '[or]'
" FIXME: Is the simple version sufficient?
" VERSION 1
" " This doesn't work because it's not guaranteed that the
" " cursor is set.
" let cline = getline('.')
" norm! a
" "norm! o
" " TAssertExec redraw | sleep 3
" let idt = strpart(getline('.'), 0, keyargs.col('.') + keyargs.shift)
" " TLogVAR idt
" let idtl = len(idt)
" -1,.delete
" " TAssertExec redraw | sleep 3
" call append(keyargs.lineno - 1, cline)
" call cursor(keyargs.lineno, keyargs.col)
" " TAssertExec redraw | sleep 3
" if idtl == 0 && icol != 0
" let idt = matchstr(pre, '^\s\+')
" let idtl = len(idt)
" endif
" VERSION 2
let idt = matchstr(pre, '^\s\+')
let idtl = len(idt)
else
let [m_0, idt, iline; rest] = matchlist(pre, '^\(\s*\)\(.*\)$')
let idtl = len(idt)
endif
if idtl < icol
let idt .= repeat(' ', icol - idtl)
endif
" TLogVAR idt
let idtl1 = len(idt)
for i in range(1, len(text) - 1)
let text[i] = idt . text[i]
let grow += idtl1
endfor
endif
" TLogVAR text
" exec 'norm! '. keyargs.lineno .'Gdd'
call tlib#normal#WithRegister('"tdd', 't')
call append(keyargs.lineno - 1, text)
if post_del_last_line
call tlib#buffer#KeepCursorPosition('$delete')
endif
let tlen = len(text)
let posshift = matchstr(keyargs.pos, '\d\+')
" TLogVAR keyargs.pos
if keyargs.pos =~ '^e'
exec keyargs.lineno + tlen - 1
exec 'norm! 0'. (len(text[-1]) - len(post) + posshift - 1) .'l'
elseif keyargs.pos =~ '^s'
" TLogVAR keyargs.lineno, pre, posshift
exec keyargs.lineno
exec 'norm! '. len(pre) .'|'
if !empty(posshift)
exec 'norm! '. posshift .'h'
endif
endif
" TLogDBG getline(keyargs.lineno)
" TLogDBG string(getline(1, '$'))
return grow
endf
function! tlib#buffer#InsertText0(text, ...) "{{{3
TVarArg ['keyargs', {}]
let mode = get(keyargs, 'mode', 'i')
" TLogVAR mode
if !has_key(keyargs, 'shift')
let col = col('.')
" if mode =~ 'i'
" let col += 1
" endif
let keyargs.shift = col >= col('$') ? 0 : -1
" let keyargs.shift = col('.') >= col('$') ? 0 : -1
" TLogVAR col
" TLogDBG col('.') .'-'. col('$') .': '. string(getline('.'))
endif
" TLogVAR keyargs.shift
return tlib#buffer#InsertText(a:text, keyargs)
endf
function! tlib#buffer#CurrentByte() "{{{3
return line2byte(line('.')) + col('.')
endf
" Evaluate cmd while maintaining the cursor position and jump registers.
function! tlib#buffer#KeepCursorPosition(cmd) "{{{3
" let pos = getpos('.')
let view = winsaveview()
try
keepjumps exec a:cmd
finally
" call setpos('.', pos)
call winrestview(view)
endtry
endf
|