Skip to content

Commit 1c0d468

Browse files
ColinKennedychrisbra
authored andcommitted
patch 9.2.0061: Not possible to know when a session will be loaded
Problem: Not possible to know when a session will be loaded. Solution: Add the SessionLoadPre autocommand (Colin Kennedy). fixes: #19084 closes: #19306 Signed-off-by: Colin Kennedy <[email protected]> Signed-off-by: Christian Brabandt <[email protected]>
1 parent b7eb0c2 commit 1c0d468

File tree

10 files changed

+89
-5
lines changed

10 files changed

+89
-5
lines changed

runtime/doc/autocmd.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*autocmd.txt* For Vim version 9.2. Last change: 2026 Feb 14
1+
*autocmd.txt* For Vim version 9.2. Last change: 2026 Feb 25
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -435,6 +435,7 @@ Name triggered by ~
435435
|QuickFixCmdPre| before a quickfix command is run
436436
|QuickFixCmdPost| after a quickfix command is run
437437

438+
|SessionLoadPre| before loading a session file
438439
|SessionLoadPost| after loading a session file
439440

440441
|SessionWritePost| after writing the session file using
@@ -1182,6 +1183,9 @@ SafeStateAgain Like SafeState but after processing any
11821183
triggered often, don't do something that takes
11831184
time.
11841185

1186+
*SessionLoadPre*
1187+
SessionLoadPre Before loading the session file created using
1188+
the |:mksession| command.
11851189
*SessionLoadPost*
11861190
SessionLoadPost After loading the session file created using
11871191
the |:mksession| command.

runtime/doc/options.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*options.txt* For Vim version 9.2. Last change: 2026 Feb 24
1+
*options.txt* For Vim version 9.2. Last change: 2026 Feb 25
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -3595,6 +3595,7 @@ A jump table for the options with a short description can be found at |Q_op|.
35953595
|SafeState|,
35963596
|SafeStateAgain|,
35973597
|SessionLoadPost|,
3598+
|SessionLoadPre|,
35983599
|SessionWritePost|,
35993600
|ShellCmdPost|,
36003601
|SigUSR1|,

runtime/doc/starting.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*starting.txt* For Vim version 9.2. Last change: 2026 Feb 18
1+
*starting.txt* For Vim version 9.2. Last change: 2026 Feb 25
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -1468,8 +1468,8 @@ This saves the current Session, and starts off the command to load another.
14681468
A session includes all tab pages, unless "tabpages" was removed from
14691469
'sessionoptions'. |tab-page|
14701470

1471-
The |SessionLoadPost| autocmd event is triggered after a session file is
1472-
loaded/sourced.
1471+
The |SessionLoadPre| autocmd event is triggered before a session file is
1472+
loaded/sourced and |SessionLoadPost| autocmd event is triggered after.
14731473
*SessionLoad-variable*
14741474
While the session file is loading, the SessionLoad global variable is set to
14751475
1. Plugins can use this to postpone some work until the SessionLoadPost event

runtime/doc/tags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5896,6 +5896,7 @@ Select-mode-mapping visual.txt /*Select-mode-mapping*
58965896
Session starting.txt /*Session*
58975897
SessionLoad-variable starting.txt /*SessionLoad-variable*
58985898
SessionLoadPost autocmd.txt /*SessionLoadPost*
5899+
SessionLoadPre autocmd.txt /*SessionLoadPre*
58995900
SessionWritePost autocmd.txt /*SessionWritePost*
59005901
ShellCmdPost autocmd.txt /*ShellCmdPost*
59015902
ShellFilterPost autocmd.txt /*ShellFilterPost*

runtime/doc/version9.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52601,6 +52601,11 @@ Changed~
5260152601
*added-9.3*
5260252602
Added ~
5260352603
-----
52604+
Various syntax, indent and other plugins were added.
52605+
52606+
Autocommands: ~
52607+
52608+
|SessionLoadPre| before loading a |Session| file
5260452609

5260552610
==============================================================================
5260652611
PATCHES *patches-9.3* *bug-fixes-9.3*

src/autocmd.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ static keyvalue_T event_tab[NUM_EVENTS] = {
168168
KEYVALUE_ENTRY(EVENT_SAFESTATE, "SafeState"),
169169
KEYVALUE_ENTRY(EVENT_SAFESTATEAGAIN, "SafeStateAgain"),
170170
KEYVALUE_ENTRY(EVENT_SESSIONLOADPOST, "SessionLoadPost"),
171+
KEYVALUE_ENTRY(EVENT_SESSIONLOADPRE, "SessionLoadPre"),
171172
KEYVALUE_ENTRY(EVENT_SESSIONWRITEPOST, "SessionWritePost"),
172173
KEYVALUE_ENTRY(EVENT_SHELLCMDPOST, "ShellCmdPost"),
173174
KEYVALUE_ENTRY(-EVENT_SHELLFILTERPOST, "ShellFilterPost"),

src/session.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,10 @@ makeopens(
640640
# ifdef FEAT_EVAL
641641
if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
642642
goto fail;
643+
644+
if (put_line(fd, "doautoall SessionLoadPre") == FAIL)
645+
goto fail;
646+
643647
if (ssop_flags & SSOP_GLOBALS)
644648
if (store_session_globals(fd) == FAIL)
645649
goto fail;

src/testdir/test_autocmd.vim

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,71 @@ func Test_BufEnter()
11311131
only
11321132
endfunc
11331133

1134+
func Test_autocmd_SessLoadPre()
1135+
tabnew
1136+
set noswapfile
1137+
mksession! Session.vim
1138+
1139+
call assert_false(exists('g:session_loaded_var'))
1140+
1141+
let content =<< trim [CODE]
1142+
set nocp noswapfile
1143+
1144+
func! Assert(cond, msg)
1145+
if !a:cond
1146+
echomsg "ASSERT_FAIL: " .. a:msg
1147+
else
1148+
echomsg "ASSERT_OK: " .. a:msg
1149+
endif
1150+
endfunc
1151+
1152+
func! OnSessionLoadPre()
1153+
call Assert(!exists('g:session_loaded_var'),
1154+
\ 'SessionLoadPre: var NOT set')
1155+
endfunc
1156+
au SessionLoadPre * call OnSessionLoadPre()
1157+
1158+
func! OnSessionLoadPost()
1159+
call Assert(exists('g:session_loaded_var'),
1160+
\ 'SessionLoadPost: var IS set')
1161+
echomsg "SessionLoadPost DONE"
1162+
endfunc
1163+
au SessionLoadPost * call OnSessionLoadPost()
1164+
1165+
func! WriteErrors()
1166+
call writefile([execute("messages")], "XerrorsPost")
1167+
endfunc
1168+
au VimLeave * call WriteErrors()
1169+
[CODE]
1170+
1171+
call writefile(content, 'Xvimrc', 'D')
1172+
1173+
call writefile(
1174+
\ ['let g:session_loaded_var = 1'],
1175+
\ 'Sessionx.vim',
1176+
\ 'b'
1177+
\ )
1178+
1179+
" --- Run child Vim ---
1180+
call system(
1181+
\ GetVimCommand('Xvimrc')
1182+
\ .. ' --not-a-term --noplugins -S Session.vim -c cq'
1183+
\ )
1184+
1185+
call WaitForAssert({-> assert_true(filereadable('XerrorsPost'))})
1186+
1187+
let errors = join(readfile('XerrorsPost'), "\n")
1188+
call assert_notmatch('ASSERT_FAIL', errors)
1189+
call assert_match('ASSERT_OK: SessionLoadPre: var NOT set', errors)
1190+
call assert_match('ASSERT_OK: SessionLoadPost: var IS set', errors)
1191+
call assert_match('SessionLoadPost DONE', errors)
1192+
1193+
set swapfile
1194+
for file in ['Session.vim', 'Sessionx.vim', 'XerrorsPost']
1195+
call delete(file)
1196+
endfor
1197+
endfunc
1198+
11341199
" Closing a window might cause an endless loop
11351200
" E814 for older Vims
11361201
func Test_autocmd_bufwipe_in_SessLoadPost()

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,8 @@ static char *(features[]) =
734734

735735
static int included_patches[] =
736736
{ /* Add new patch number below this line */
737+
/**/
738+
61,
737739
/**/
738740
60,
739741
/**/

src/vim.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,6 +1470,7 @@ enum auto_event
14701470
EVENT_SAFESTATE, // going to wait for a character
14711471
EVENT_SAFESTATEAGAIN, // still waiting for a character
14721472
EVENT_SESSIONLOADPOST, // after loading a session file
1473+
EVENT_SESSIONLOADPRE, // before loading a session file
14731474
EVENT_SESSIONWRITEPOST, // after writing a session file
14741475
EVENT_SHELLCMDPOST, // after ":!cmd"
14751476
EVENT_SHELLFILTERPOST, // after ":1,2!cmd", ":w !cmd", ":r !cmd".

0 commit comments

Comments
 (0)