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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
|
"=============================================================================
" Copyright (c) 2009-2010 Takeshi NISHIDA
"
"=============================================================================
" LOAD GUARD {{{1
if exists('g:loaded_autoload_l9')
finish
endif
let g:loaded_autoload_l9 = 1
" }}}1
"=============================================================================
" COMPATIBILITY TEST {{{1
"
let s:L9_VERSION_CURRENT = 101
let s:L9_VERSION_PASSABLE = 101
" returns true if given version is compatible.
function l9#isCompatible(ver)
return
endfunction
let s:VERSION_FACTOR = str2float('0.01')
" returns false if the caller script should finish.
" a:vimVersion: if 0, don't check vim version
" a:l9Version: same rule as v:version
function l9#guardScriptLoading(path, vimVersion, l9Version, exprs)
let loadedVarName = 'g:loaded_' . substitute(a:path, '\W', '_', 'g')
if exists(loadedVarName)
return 0
elseif a:vimVersion > 0 && a:vimVersion > v:version
echoerr a:path . ' requires Vim version ' . string(a:vimVersion * s:VERSION_FACTOR)
return 0
elseif a:l9Version > 0 && (a:l9Version > s:L9_VERSION_CURRENT ||
\ a:l9Version < s:L9_VERSION_PASSABLE)
echoerr a:path . ' requires L9 library version ' . string(a:l9Version * s:VERSION_FACTOR)
return 0
endif
for expr in a:exprs
if !eval(expr)
echoerr a:path . ' requires: ' . expr
return 0
endif
endfor
let {loadedVarName} = 1
return 1
endfunction
"
function l9#getVersion()
return s:L9_VERSION_CURRENT
endfunction
" }}}1
"=============================================================================
" LIST {{{1
" Removes duplicates (unstable)
" This function doesn't change the list of argument.
function l9#unique(items)
let sorted = sort(a:items)
if len(sorted) < 2
return sorted
endif
let last = remove(sorted, 0)
let result = [last]
for item in sorted
if item != last
call add(result, item)
let last = item
endif
endfor
return result
endfunction
" Removes duplicates (stable)
" This function doesn't change the list of argument.
function l9#uniqueStably(items)
let result = []
for item in a:items
if count(result, item, &ignorecase) == 0
call add(result, item)
endif
endfor
return result
endfunction
" [ [0], [1,2], [3] ] -> [ 0, 1, 2, 3 ]
" This function doesn't change the list of argument.
function l9#concat(items)
let result = []
for l in a:items
let result += l
endfor
return result
endfunction
" [ [0,1,2], [3,4], [5,6,7,8] ] -> [ [0,3,5],[1,4,6] ]
" This function doesn't change the list of argument.
function l9#zip(items)
let result = []
for i in range(min(map(copy(a:items), 'len(v:val)')))
call add(result, map(copy(a:items), 'v:val[i]'))
endfor
return result
endfunction
" filter() with the maximum number of items
" This function doesn't change the list of argument.
function l9#filterWithLimit(items, expr, limit)
if a:limit <= 0
return filter(copy(a:items), a:expr)
endif
let result = []
let stride = a:limit * 3 / 2 " x1.5
for i in range(0, len(a:items) - 1, stride)
let result += filter(a:items[i : i + stride - 1], a:expr)
if len(result) >= a:limit
return remove(result, 0, a:limit - 1)
endif
endfor
return result
endfunction
" Removes if a:expr is evaluated as non-zero and returns removed items.
" This function change the list of argument.
function l9#removeIf(items, expr)
let removed = filter(copy(a:items), a:expr)
call filter(a:items, '!( ' . a:expr . ')')
return removed
endfunction
" }}}1
"=============================================================================
" NUMERIC {{{1
" }}}1
"=============================================================================
" STRING {{{1
" Snips a:str and add a:mask if the length of a:str is more than a:len
function l9#snipHead(str, len, mask)
if a:len >= len(a:str)
return a:str
elseif a:len <= len(a:mask)
return a:mask
endif
return a:mask . a:str[-a:len + len(a:mask):]
endfunction
" Snips a:str and add a:mask if the length of a:str is more than a:len
function l9#snipTail(str, len, mask)
if a:len >= len(a:str)
return a:str
elseif a:len <= len(a:mask)
return a:mask
endif
return a:str[:a:len - 1 - len(a:mask)] . a:mask
endfunction
" Snips a:str and add a:mask if the length of a:str is more than a:len
function l9#snipMid(str, len, mask)
if a:len >= len(a:str)
return a:str
elseif a:len <= len(a:mask)
return a:mask
endif
let len_head = (a:len - len(a:mask)) / 2
let len_tail = a:len - len(a:mask) - len_head
return (len_head > 0 ? a:str[: len_head - 1] : '') . a:mask .
\ (len_tail > 0 ? a:str[-len_tail :] : '')
endfunction
"
function l9#hash224(str)
let a = 0x00000800 " shift 11 bit (if unsigned)
let b = 0x001fffff " extract 11 bit (if unsigned)
let nHash = 7
let hashes = repeat([0], nHash)
for i in range(len(a:str))
let iHash = i % nHash
let hashes[iHash] = hashes[iHash] * a + hashes[iHash] / b
let hashes[iHash] += char2nr(a:str[i])
endfor
return join(map(hashes, 'printf("%08x", v:val)'), '')
endfunction
" wildcard -> regexp
function l9#convertWildcardToRegexp(expr)
let re = escape(a:expr, '\')
for [pat, sub] in [ [ '*', '\\.\\*' ], [ '?', '\\.' ], [ '[', '\\[' ], ]
let re = substitute(re, pat, sub, 'g')
endfor
return '\V' . re
endfunction
" }}}1
"=============================================================================
" LINES {{{1
" Removes from the line matching with a:begin first to the line matching with
" a:end next and returns removed lines.
" If matching range is not found, returns []
function l9#removeLinesBetween(lines, begin, end)
for i in range(len(a:lines) - 1)
if a:lines[i] =~ a:begin
break
endif
endfor
for j in range(i + 1, len(a:lines) - 1)
if a:lines[j] =~ a:end
let g:l0 += [a:lines[i : j]]
return remove(a:lines, i, j)
endif
endfor
return []
endfunction
" }}}1
"=============================================================================
" PATH {{{1
" returns the path separator charactor.
function l9#getPathSeparator()
return (!&shellslash && (has('win32') || has('win64')) ? '\' : '/')
endfunction
" [ 'a', 'b/', '/c' ] -> 'a/b/c'
function l9#concatPaths(paths)
let result = ''
for p in a:paths
if empty(p)
continue
elseif empty(result)
let result = p
else
let result = substitute(result, '[/\\]$', '', '') . l9#getPathSeparator()
\ . substitute(p, '^[/\\]', '', '')
endif
endfor
return result
endfunction
" path: '/a/b/c/d', dir: '/a/b' => 'c/d'
function l9#modifyPathRelativeToDir(path, dir)
let pathFull = fnamemodify(a:path, ':p')
let dirFull = fnamemodify(a:dir, ':p')
if len(pathFull) < len(dirFull) || pathFull[:len(dirFull) - 1] !=# dirFull
return pathFull
endif
return pathFull[len(dirFull):]
endfunction
" }}}1
"=============================================================================
" FILE {{{1
" Almost same as readfile().
function l9#readFile(...)
let args = copy(a:000)
let args[0] = expand(args[0])
try
return call('readfile', args)
catch
endtry
return []
endfunction
" Almost same as writefile().
function l9#writeFile(...)
let args = copy(a:000)
let args[1] = expand(args[1])
let dir = fnamemodify(args[1], ':h')
try
if !isdirectory(dir)
call mkdir(dir, 'p')
endif
return call('writefile', args)
catch
endtry
return -1 " -1 is error code.
endfunction
" }}}1
"=============================================================================
" BUFFER {{{1
" :wall/:wall! wrapper. Useful for writing readonly buffers.
function l9#writeAll()
try
silent update " NOTE: avoiding a problem with a buftype=acwrite buffer.
silent wall
catch /^Vim/ " E45, E505
if l9#inputHl('Question', v:exception . "\nWrite readonly files? (Y/N) : ", 'Y') ==? 'y'
redraw
:wall!
endif
endtry
endfunction
" Loads given files with :edit command
function l9#loadFilesToBuffers(files)
for file in filter(copy(a:files), '!bufloaded(v:val)')
execute 'edit ' . fnameescape(file)
if !exists('bufNrFirst')
let bufNrFirst = bufnr('%')
endif
endfor
if exists('bufNrFirst')
execute bufNrFirst . 'buffer'
endif
endfunction
" Deletes all buffers except given files with :bdelete command
function l9#deleteAllBuffersExcept(files)
let bufNrExcepts = map(copy(a:files), 'bufnr("^" . v:val . "$")')
for bufNr in filter(range(1, bufnr('$')), 'bufloaded(v:val)')
if count(bufNrExcepts, bufNr) == 0
execute bufNr . 'bdelete'
endif
endfor
endfunction
" }}}1
"=============================================================================
" WINDOW {{{1
" move current window to next tabpage.
function l9#shiftWinNextTabpage()
if tabpagenr('$') < 2
return
endif
let bufnr = bufnr('%')
tabnext
execute bufnr . 'sbuffer'
tabprevious
if winnr('$') > 1
close
tabnext
else
close " if tabpage is closed, next tabpage will become current
endif
endfunction
" move current window to previous tabpage.
function l9#shiftWinPrevTabpage()
if tabpagenr('$') < 2
return
endif
let bufnr = bufnr('%')
tabprevious
execute bufnr . 'sbuffer'
tabnext
close
tabprevious
endfunction
" move to a window containing specified buffer.
" returns 0 if the buffer is not found.
function l9#moveToBufferWindowInCurrentTabpage(bufNr)
if bufnr('%') == a:bufNr
return 1
elseif count(tabpagebuflist(), a:bufNr) == 0
return 0
endif
execute bufwinnr(a:bufNr) . 'wincmd w'
return 1
endfunction
" returns 0 if the buffer is not found.
function s:moveToOtherTabpageOpeningBuffer(bufNr)
for tabNr in range(1, tabpagenr('$'))
if tabNr != tabpagenr() && count(tabpagebuflist(tabNr), a:bufNr) > 0
execute 'tabnext ' . tabNr
return 1
endif
endfor
return 0
endfunction
" move to a window containing specified buffer.
" returns 0 if the buffer is not found.
function l9#moveToBufferWindowInOtherTabpage(bufNr)
if !s:moveToOtherTabpageOpeningBuffer(a:bufNr)
return 0
endif
return l9#moveToBufferWindowInCurrentTabpage(a:bufNr)
endfunction
" }}}1
"=============================================================================
" COMMAND LINE {{{1
" echo/echomsg with highlighting.
function l9#echoHl(hl, msg, prefix, addingHistory)
let echoCmd = (a:addingHistory ? 'echomsg' : 'echo')
execute "echohl " . a:hl
try
for l in (type(a:msg) == type([]) ? a:msg : split(a:msg, "\n"))
execute echoCmd . ' a:prefix . l'
endfor
finally
echohl None
endtry
endfunction
" input() with highlighting.
" This function can take list as {completion} argument.
function l9#inputHl(hl, ...)
execute "echohl " . a:hl
try
let args = copy(a:000)
if len(args) > 2 && type(args[2]) == type([])
let s:candidatesForInputHl = args[2]
let args[2] = 'custom,l9#completeForInputHl'
endif
let s = call('input', args)
unlet! s:candidatesForInputHl
finally
echohl None
endtry
redraw " needed to show following echo to next line.
return s
endfunction
" only called by l9#inputHl() for completion.
function l9#completeForInputHl(lead, line, pos)
return join(s:candidatesForInputHl, "\n")
endfunction
" }}}1
"=============================================================================
" VISUAL MODE {{{1
" returns last selected text in Visual mode.
function l9#getSelectedText()
let reg_ = [@", getregtype('"')]
let regA = [@a, getregtype('a')]
if mode() =~# "[vV\<C-v>]"
silent normal! "aygv
else
let pos = getpos('.')
silent normal! gv"ay
call setpos('.', pos)
endif
let text = @a
call setreg('"', reg_[0], reg_[1])
call setreg('a', regA[0], regA[1])
return text
endfunction
" }}}1
"=============================================================================
" EVAL {{{1
" loads given text as Vim script with :source command
function l9#loadScript(text)
let lines = (type(a:text) == type([]) ? a:text : split(a:text, "\n"))
let fname = tempname()
call writefile(lines, fname)
source `=fname`
call delete(fname)
endfunction
" }}}1
"=============================================================================
" VARIABLES {{{1
"
function l9#defineVariableDefault(name, default)
if !exists(a:name)
let {a:name} = a:default
endif
endfunction
" }}}1
"=============================================================================
" GREP {{{1
" Execute :vimgrep and opens the quickfix window if matches are found.
"
" a:pattern: search pattern. If ommitted, last search pattern (@/) is used.
" a:files: List of files
function l9#grepFiles(pattern, files)
let target = join(map(a:files, 'escape(v:val, " ")'), ' ')
let pattern = (a:pattern[0] ==# '/' ? a:pattern[1:] : a:pattern)
let pattern = (empty(pattern) ? @/ : pattern)
try
execute printf('vimgrep/%s/j %s', pattern, target)
catch /^Vim/
call setqflist([])
endtry
call l9#quickfix#sort()
call l9#quickfix#openIfNotEmpty(1, 0)
endfunction
" Execute :vimgrep for buffers using l9#grepFiles()
" See also: :L9GrepBuffer :L9GrepBufferAll
function l9#grepBuffers(pattern, bufNrs)
let files = map(filter(a:bufNrs, 'bufloaded(v:val)'), 'bufname(v:val)')
call l9#grepFiles(a:pattern, files)
endfunction
" }}}1
"=============================================================================
" SIGN {{{1
" Highlights lines using :sign define and :sign place.
"
" a:linehl, a:text, a:texthl: See |signs|. Ignored if empty string.
" a:locations: List of [{buffer number}, {line number}] for highlighting
function l9#placeSign(linehl, text, texthl, locations)
let argLinehl = (empty(a:linehl) ? '' : 'linehl=' . a:linehl)
let argText = (empty(a:text) ? '' : 'text=' . a:text)
let argTexthl = (empty(a:texthl) ? '' : 'texthl=' . a:texthl)
let name = 'l9--' . a:linehl . '--' . a:text . '--' . a:texthl
execute printf('sign define %s linehl=%s text=%s texthl=%s',
\ name, a:linehl, a:text, a:texthl)
for [bufNr, lnum] in a:locations
execute printf('sign place 1 line=%d name=%s buffer=%d', lnum, name, bufNr)
endfor
endfunction
" }}}1
"=============================================================================
" NOTIFY EXTERNALLY {{{1
" Notify a message using an external program.
" Currently supports Balloonly, Screen, and Tmux.
function l9#notifyExternally(msg)
return l9#notifyBalloonly(a:msg)
\ || l9#notifyScreen(a:msg)
\ || l9#notifyTmux(a:msg)
endfunction
"
function l9#notifyBalloonly(msg)
if !(has('win32') || has('win64')) || !executable(g:l9_balloonly)
return 0
endif
execute 'silent !start ' . shellescape(g:l9_balloonly) . ' 4000 "l9" ' . shellescape(a:msg)
return 1
endfunction
"
function l9#notifyScreen(msg)
if !has('unix') || has('gui_running') || $WINDOW !~ '\d' || !executable('screen')
return 0
endif
call system('screen -X wall ' . shellescape('l9: ' . a:msg))
return 1
endfunction
"
function l9#notifyTmux(msg)
if !has('unix') || has('gui_running') || empty($TMUX) || !executable('tmux')
return 0
endif
call system('tmux display-message ' . shellescape('l9: ' . a:msg))
return 1
endfunction
" }}}1
"=============================================================================
" vim: set fdm=marker:
|