vim-patch:9.2.0061: Not possible to know when a session will be loaded (#38071)

Problem:  Not possible to know when a session will be loaded.
Solution: Add the SessionLoadPre autocommand (Colin Kennedy).

fixes:  vim/vim#19084
closes: vim/vim#19306

1c0d468d72

Co-authored-by: Colin Kennedy <colinvfx@gmail.com>
This commit is contained in:
zeertzjq
2026-02-26 21:30:44 +08:00
committed by GitHub
parent 34b3bd1ac5
commit 7852993f49
9 changed files with 78 additions and 2 deletions

View File

@@ -961,6 +961,9 @@ SafeState When nothing is pending, going to wait for the
check more with `state()`, e.g. whether the
screen was scrolled for messages.
*SessionLoadPre*
SessionLoadPre Before loading the session file created using
the |:mksession| command.
*SessionLoadPost*
SessionLoadPost After loading the session file created using
the |:mksession| command.

View File

@@ -240,6 +240,7 @@ EVENTS
• Creating or updating a progress message with |nvim_echo()| triggers a |Progress| event.
• |MarkSet| is triggered after a |mark| is set by the user (currently doesn't
support implicit marks like |'[| or |'<|, …).
• |SessionLoadPre| is triggered before loading a |Session| file.
• |TabClosedPre| is triggered before closing a |tabpage|.
• New `terminator` parameter for |TermRequest| event.

View File

@@ -2553,6 +2553,7 @@ A jump table for the options with a short description can be found at |Q_op|.
|RemoteReply|,
|SafeState|,
|SessionLoadPost|,
|SessionLoadPre|,
|SessionWritePost|,
|ShellCmdPost|,
|Signal|,

View File

@@ -852,8 +852,8 @@ This saves the current Session, and starts off the command to load another.
A session includes all tab pages, unless "tabpages" was removed from
'sessionoptions'. |tab-page|
The |SessionLoadPost| autocmd event is triggered after a session file is
loaded/sourced.
The |SessionLoadPre| autocmd event is triggered before a session file is
loaded/sourced and |SessionLoadPost| autocmd event is triggered after.
*SessionLoad-variable*
While the session file is loading, the SessionLoad global variable is set to
1. Plugins can use this to postpone some work until the SessionLoadPost event

View File

@@ -176,6 +176,7 @@ error('Cannot require a meta file')
--- |'SafeState'
--- |'SearchWrapped'
--- |'SessionLoadPost'
--- |'SessionLoadPre'
--- |'SessionWritePost'
--- |'ShellCmdPost'
--- |'ShellFilterPost'

View File

@@ -2231,6 +2231,7 @@ vim.go.ei = vim.go.eventignore
--- `RemoteReply`,
--- `SafeState`,
--- `SessionLoadPost`,
--- `SessionLoadPre`,
--- `SessionWritePost`,
--- `ShellCmdPost`,
--- `Signal`,

View File

@@ -97,6 +97,7 @@ return {
SafeState = false, -- going to wait for a character
SearchWrapped = true, -- after the search wrapped around
SessionLoadPost = false, -- after loading a session file
SessionLoadPre = false, -- before loading a session file
SessionWritePost = false, -- after writing a session file
ShellCmdPost = false, -- after ":!cmd"
ShellFilterPost = true, -- after ":1,2!cmd", ":w !cmd", ":r !cmd".

View File

@@ -600,6 +600,9 @@ static int makeopens(FILE *fd, char *dirnow)
// Begin by setting v:this_session, and then other sessionable variables.
PUTLINE_FAIL("let v:this_session=expand(\"<sfile>:p\")");
PUTLINE_FAIL("doautoall SessionLoadPre");
if (ssop_flags & kOptSsopFlagGlobals) {
if (store_session_globals(fd) == FAIL) {
return FAIL;

View File

@@ -1135,6 +1135,71 @@ func Test_BufEnter()
only
endfunc
func Test_autocmd_SessLoadPre()
tabnew
set noswapfile
mksession! Session.vim
call assert_false(exists('g:session_loaded_var'))
let content =<< trim [CODE]
set nocp noswapfile
func! Assert(cond, msg)
if !a:cond
echomsg "ASSERT_FAIL: " .. a:msg
else
echomsg "ASSERT_OK: " .. a:msg
endif
endfunc
func! OnSessionLoadPre()
call Assert(!exists('g:session_loaded_var'),
\ 'SessionLoadPre: var NOT set')
endfunc
au SessionLoadPre * call OnSessionLoadPre()
func! OnSessionLoadPost()
call Assert(exists('g:session_loaded_var'),
\ 'SessionLoadPost: var IS set')
echomsg "SessionLoadPost DONE"
endfunc
au SessionLoadPost * call OnSessionLoadPost()
func! WriteErrors()
call writefile([execute("messages")], "XerrorsPost")
endfunc
au VimLeave * call WriteErrors()
[CODE]
call writefile(content, 'Xvimrc', 'D')
call writefile(
\ ['let g:session_loaded_var = 1'],
\ 'Sessionx.vim',
\ 'b'
\ )
" --- Run child Vim ---
call system(
\ GetVimCommand('Xvimrc')
\ .. ' --headless --noplugins -S Session.vim -c cq'
\ )
call WaitForAssert({-> assert_true(filereadable('XerrorsPost'))})
let errors = join(readfile('XerrorsPost'), "\n")
call assert_notmatch('ASSERT_FAIL', errors)
call assert_match('ASSERT_OK: SessionLoadPre: var NOT set', errors)
call assert_match('ASSERT_OK: SessionLoadPost: var IS set', errors)
call assert_match('SessionLoadPost DONE', errors)
set swapfile
for file in ['Session.vim', 'Sessionx.vim', 'XerrorsPost']
call delete(file)
endfor
endfunc
" Closing a window might cause an endless loop
" E814 for older Vims
func Test_autocmd_bufwipe_in_SessLoadPost()