Skip to content

Commit 986f7b5

Browse files
committed
Merge branch 'release-1.2.0'
2 parents 8bee5d0 + f192a43 commit 986f7b5

File tree

7 files changed

+283
-105
lines changed

7 files changed

+283
-105
lines changed

autoload/stay.vim

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,55 @@
11
" AUTOLOAD FUNCTION LIBRARY FOR VIM-STAY
22
" Core functions (will be loaded when first autocommand is triggered)
3-
let s:cpo = &cpo
4-
set cpo&vim
3+
let s:cpoptions = &cpoptions
4+
set cpoptions&vim
55

66
" Check if buffer {bufnr} is persistent:
77
" @signature: stay#ispersistent({bufnr:Number}, {volatile_ftypes:List<String>})
88
" @returns: Boolean
99
" @notes: the persistence heuristics are
1010
" - buffer must be listed
11+
" - buffer name must not be empty
1112
" - buffer must be of ordinary or "acwrite" 'buftype'
1213
" - not a preview window
1314
" - not a diff window
1415
" - buffer's 'bufhidden' must be empty or "hide"
15-
" - buffer must not be of a volatile file type
1616
" - buffer must map to a readable file
17+
" - buffer must not be of a volatile file type
18+
" - buffer file must not be located in a known temp dir
1719
function! stay#ispersistent(bufnr, volatile_ftypes) abort
20+
let l:bufpath = expand('#'.a:bufnr.':p')
1821
return bufexists(a:bufnr)
22+
\ && !empty(l:bufpath)
1923
\ && getbufvar(a:bufnr, 'stay_ignore', 0) isnot 1
2024
\ && getbufvar(a:bufnr, '&buflisted') is 1
2125
\ && index(['', 'acwrite'], getbufvar(a:bufnr, '&buftype')) isnot -1
2226
\ && getbufvar(a:bufnr, '&previewwindow') isnot 1
2327
\ && getbufvar(a:bufnr, '&diff') isnot 1
2428
\ && index(['', 'hide'], getbufvar(a:bufnr, '&bufhidden')) isnot -1
25-
\ && index(a:volatile_ftypes, getbufvar(a:bufnr, '&filetype')) is -1
26-
\ && filereadable(fnamemodify(bufname(a:bufnr), ':p'))
29+
\ && filereadable(l:bufpath)
30+
\ && stay#isftype(a:bufnr, a:volatile_ftypes) isnot 1
31+
\ && stay#istemp(l:bufpath) isnot 1
32+
endfunction
33+
34+
" Check if {fname} is in a 'backupskip' location:
35+
" @signature: stay#istemp({fname:String})
36+
" @returns: Boolean
37+
function! stay#istemp(path) abort
38+
let l:candidates = stay#shim#globpath(&backupskip, '**/'.fnamemodify(a:path, ':t'), 1, 1)
39+
return index(l:candidates, a:path) isnot -1
40+
endfunction
41+
42+
" Check if one of {bufnr}'s 'filetype' parts is on the {ftypes} List:
43+
" @signature: stay#isftype({bufnr:Number}, {ftypes:List<String>})
44+
" @returns: Boolean
45+
" @notes: - tests individual parts of composite (dotted) 'filetype's
46+
" - comparison is always case sensitive
47+
function! stay#isftype(bufnr, ftypes) abort
48+
let l:candidates = split(getbufvar(a:bufnr, '&filetype'), '\.')
49+
return !empty(filter(l:candidates, 'index(a:ftypes, v:val) isnot -1'))
2750
endfunction
2851

29-
let &cpo = s:cpo
30-
unlet! s:cpo
52+
let &cpoptions = s:cpoptions
53+
unlet! s:cpoptions
3154

3255
" vim:set sw=2 sts=2 ts=2 et fdm=marker fmr={{{,}}}:
Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,40 @@
11
" FASTFOLD INTEGRATION MODULE
22
" https://github.com/Konfekt/FastFold
3+
let s:cpoptions = &cpoptions
4+
set cpoptions&vim
35

4-
" - register integration autocommands
6+
" - cancel integration if FastFold is not found
7+
if empty(findfile('plugin/fastfold.vim', &rtp))
8+
let &cpoptions = s:cpoptions
9+
unlet! s:cpoptions
10+
finish
11+
endif
12+
13+
" - register integration autocommands if FastFold plug-in is found
514
function! stay#integrate#fastfold#setup() abort
615
autocmd User BufStaySavePre unsilent call stay#integrate#fastfold#save_pre()
716
autocmd User BufStaySavePost unsilent call stay#integrate#fastfold#save_post()
17+
autocmd User BufStayLoadPost,BufStaySavePost let b:isPersistent = 1
818
endfunction
919

1020
" - on User event 'BufStaySavePre': restore original 'foldmethod'
1121
function! stay#integrate#fastfold#save_pre() abort
12-
let [l:fdmlocal, l:fdmorig] = s:foldmethods()
13-
if l:fdmorig isnot l:fdmlocal
14-
\ && index(split(&viewoptions, ','), 'folds') isnot -1
15-
noautocmd silent let &l:foldmethod = l:fdmorig
22+
if index(split(&viewoptions, ','), 'folds') isnot -1
23+
let [l:fdmlocal, l:fdmorig] = [&l:foldmethod, get(w:, 'lastfdm', &l:foldmethod)]
24+
if l:fdmorig isnot l:fdmlocal
25+
noautocmd silent let &l:foldmethod = l:fdmorig
26+
endif
1627
endif
1728
endfunction
1829

1930
" - on User event 'BufStaySavePost': restore FastFold 'foldmethod'
2031
function! stay#integrate#fastfold#save_post() abort
21-
let [l:fdmlocal, l:fdmorig] = s:foldmethods()
22-
if l:fdmorig isnot l:fdmlocal
32+
if &foldmethod isnot# 'manual' && exists('w:lastfdm')
2333
noautocmd silent let &l:foldmethod = 'manual'
2434
endif
2535
endfunction
2636

27-
" - return tuple of current local and FastFold stored 'foldmethod'
28-
function! s:foldmethods() abort
29-
let l:fdmlocal = &l:foldmethod
30-
return [l:fdmlocal, get(w:, 'lastfdm', l:fdmlocal)]
31-
endfunction
37+
let &cpoptions = s:cpoptions
38+
unlet! s:cpoptions
3239

3340
" vim:set sw=2 sts=2 ts=2 et fdm=marker fmr={{{,}}}:

autoload/stay/shim.vim

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,74 @@
11
" STAY EVAL SHIM MODULE
22
" Are these Vim patch levels, my dear?
3+
if v:version < 700
4+
finish
5+
endif
36

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:
511
" - no {nosuf} argument before 7.2.051 - :h version7.txt
612
" - 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
1455
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')
1569
endfunction
1670

71+
let &cpoptions = s:cpoptions
72+
unlet! s:cpoptions
73+
1774
" vim:set sw=2 sts=2 ts=2 et fdm=marker fmr={{{,}}}:

autoload/stay/view.vim

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
" AUTOLOAD FUNCTION LIBRARY FOR VIM-STAY
22
" View session handling functions
3-
let s:cpo = &cpo
4-
set cpo&vim
3+
let s:cpoptions = &cpoptions
4+
set cpoptions&vim
55

66
" Make a persistent view for window {winnr}:
77
" @signature: stay#view#make({winnr:Number})
@@ -18,9 +18,9 @@ function! stay#view#make(winnr) abort
1818
return 0
1919
endif
2020
unlet! b:stay_atpos
21-
silent doautocmd <nomodeline> User BufStaySavePre
21+
call s:doautocmd('User', 'BufStaySavePre')
2222
mkview
23-
silent doautocmd <nomodeline> User BufStaySavePost
23+
call s:doautocmd('User', 'BufStaySavePost')
2424
call s:win.back()
2525
return 1
2626
finally
@@ -36,9 +36,15 @@ function! stay#view#load(winnr) abort
3636
return 0
3737
endif
3838

39-
silent doautocmd <nomodeline> User BufStayLoadPre
40-
noautocmd silent loadview
41-
silent doautocmd <nomodeline> User BufStayLoadPost
39+
call s:doautocmd('User', 'BufStayLoadPre')
40+
try " significantly slows down buffer loads without noautocmd
41+
noautocmd silent loadview
42+
catch " silently return on errors
43+
return 0
44+
endtry
45+
call s:doautocmd('User', 'BufStayLoadPost')
46+
call s:doautocmd('SessionLoadPost')
47+
4248
if exists('b:stay_atpos')
4349
call cursor(b:stay_atpos[0], b:stay_atpos[1])
4450
silent! normal! zOzz
@@ -47,7 +53,7 @@ function! stay#view#load(winnr) abort
4753
return 1
4854
endfunction
4955

50-
" Private helper functions:
56+
" Private helper functions: {{{
5157
" - window navigation stack
5258
let s:win = {'stack': []}
5359

@@ -72,7 +78,19 @@ function! s:win.back() abort
7278
return exists('l:towinnr') && winnr() is l:towinnr
7379
endfunction
7480

75-
let &cpo = s:cpo
76-
unlet! s:cpo
81+
" - apply {event} autocommands, optionally matching pattern {a:1},
82+
" but only if there are any
83+
" 1. avoids flooding message history with "No matching autocommands"
84+
" 2. avoids re-applying modelines in Vim < 7.3.442, which doesn't honor |<nomodeline>|
85+
" see https://groups.google.com/forum/#!topic/vim_dev/DidKMDAsppw
86+
function! s:doautocmd(event, ...) abort
87+
let l:event = a:0 ? [a:event, a:1] : [a:event]
88+
if exists('#'.join(l:event, '#'))
89+
execute 'doautocmd <nomodeline>' join(l:event, ' ')
90+
endif
91+
endfunction " }}}
92+
93+
let &cpoptions = s:cpoptions
94+
unlet! s:cpoptions
7795

7896
" vim:set sw=2 sts=2 ts=2 et fdm=marker fmr={{{,}}}:

autoload/stay/viewdir.vim

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
" AUTOLOAD FUNCTION LIBRARY FOR VIM-STAY
2+
" 'viewdir' handling functions
3+
let s:cpoptions = &cpoptions
4+
set cpoptions&vim
5+
6+
" Remove view session files from 'viewdir':
7+
" @signature: stay#viewdir#clean({bang:String}, [{keepdays:Number}])
8+
" @optargs: {keepdays} keep files not older than this in days (default: 0)
9+
" @returns: List<Number> tuple of deletion candidates count, deleted files count
10+
function! stay#viewdir#clean(bang, ...) abort
11+
let l:keepsecs = max([get(a:, 1, 0) * 86400, 0])
12+
let l:candidates = stay#shim#globpath(&viewdir, '*', 1, 1)
13+
call filter(l:candidates, 'localtime() - getftime(v:val) > l:keepsecs')
14+
let l:candcount = len(l:candidates)
15+
let l:delcount = 0
16+
if a:bang is 1
17+
\ || input("Type 'Y' to delete ".l:candcount." view session files: ") is# 'Y'
18+
for l:file in l:candidates
19+
let l:delcount += (delete(l:file) is 0)
20+
endfor
21+
endif
22+
return [l:candcount, l:delcount]
23+
endfunction
24+
25+
let &cpoptions = s:cpoptions
26+
unlet! s:cpoptions
27+
28+
" vim:set sw=2 sts=2 ts=2 et fdm=marker fmr={{{,}}}:

0 commit comments

Comments
 (0)