|
1 | 1 | " STAY EVAL SHIM MODULE
|
2 | 2 | " Are these Vim patch levels, my dear?
|
| 3 | +if v:version < 700 |
| 4 | + finish |
| 5 | +endif |
3 | 6 |
|
4 |
| -" globpath: |
| 7 | +let s:cpoptions = &cpoptions |
| 8 | +set cpoptions&vim |
| 9 | + |
| 10 | +" Full forward and backward `globpath()` compatibility between Vim 7.0 and Vim 7.4: |
5 | 11 | " - no {nosuf} argument before 7.2.051 - :h version7.txt
|
6 | 12 | " - no {list} argument before 7.4.279 - http://ftp.vim.org/pub/vim/patches/7.4/README
|
7 |
| -function! stay#shim#globpath(path, glob, nosuf, list) abort |
8 |
| - if v:version < 702 || (v:version is 702 && !has('patch-051')) |
9 |
| - return split(globpath(a:path, a:glob), '\n') |
10 |
| - elseif v:version < 704 || (v:version is 704 && !has('patch-279')) |
11 |
| - return split(globpath(a:path, a:glob, a:nosuf), '\n') |
12 |
| - else |
13 |
| - return globpath(a:path, a:glob, a:nosuf, a:list) |
| 13 | +if v:version < 702 || (v:version is 702 && !has('patch051')) |
| 14 | + function! stay#shim#globpath(path, glob, ...) abort |
| 15 | + let l:nosuf = get(a:, 1, 0) |
| 16 | + let l:list = get(a:, 2, 0) |
| 17 | + let l:suffixes = &suffixes |
| 18 | + let l:wildignore = &wildignore |
| 19 | + try |
| 20 | + if l:nosuf isnot 0 |
| 21 | + set suffixes= |
| 22 | + set wildignore= |
| 23 | + endif |
| 24 | + let l:result = globpath(a:path, a:glob) |
| 25 | + return l:list isnot 0 ? s:fnames2list(l:result, 0) : l:result |
| 26 | + finally |
| 27 | + let &suffixes = l:suffixes |
| 28 | + let &wildignore = l:wildignore |
| 29 | + endtry |
| 30 | + endfunction |
| 31 | + |
| 32 | +elseif v:version < 704 || (v:version is 704 && !has('patch279')) |
| 33 | + function! stay#shim#globpath(path, glob, ...) abort |
| 34 | + let l:nosuf = get(a:, 1, 0) |
| 35 | + let l:list = get(a:, 2, 0) |
| 36 | + let l:result = globpath(a:path, a:glob, l:nosuf) |
| 37 | + return l:list isnot 0 ? s:fnames2list(l:result, l:nosuf) : l:result |
| 38 | + endfunction |
| 39 | + |
| 40 | +else |
| 41 | + function! stay#shim#globpath(path, glob, ...) abort |
| 42 | + return globpath(a:path, a:glob, get(a:, 1, 0), get(a:, 2, 0)) |
| 43 | + endfunction |
| 44 | +endif |
| 45 | + |
| 46 | +" Get a List out of {fnames} without mangling file names with NL in them: |
| 47 | +" @signature: s:fnames2list({fnames:String[NL-separated]}, {setnosuf:Boolean}) |
| 48 | +" @returns: List<String> of file system object paths in {fnames} |
| 49 | +function! s:fnames2list(fnames, setnosuf) abort |
| 50 | + let l:globcmd = a:setnosuf is 1 ? 'glob(%s, 1)' : 'glob(%s)' |
| 51 | + let l:fnames = split(a:fnames, '\n') |
| 52 | + let l:fragments = filter(copy(l:fnames), 'empty('.printf(l:globcmd, 'v:val').')') |
| 53 | + if empty(l:fragments) |
| 54 | + return l:fnames |
14 | 55 | endif
|
| 56 | + |
| 57 | + let l:fnames = filter(l:fnames, '!empty('.printf(l:globcmd, 'v:val').')') |
| 58 | + let l:index = 0 |
| 59 | + while l:index+1 < len(l:fragments) |
| 60 | + let l:composite = get(l:, 'composite', l:fragments[l:index])."\n".l:fragments[l:index+1] |
| 61 | + if !empty(eval(printf(l:globcmd, string(l:composite)))) |
| 62 | + call add(l:fnames, l:composite) |
| 63 | + unlet l:composite |
| 64 | + let l:index += 1 |
| 65 | + endif |
| 66 | + let l:index += 1 |
| 67 | + endwhile |
| 68 | + return sort(l:fnames, 'i') |
15 | 69 | endfunction
|
16 | 70 |
|
| 71 | +let &cpoptions = s:cpoptions |
| 72 | +unlet! s:cpoptions |
| 73 | + |
17 | 74 | " vim:set sw=2 sts=2 ts=2 et fdm=marker fmr={{{,}}}:
|
0 commit comments