Skip to content

Commit 3349b7b

Browse files
committed
feat(events): fire User autocmds around session switches
Fire RepossessionSwitchPre before a switch, while the old session is still active, and RepossessionSwitchPost after the new session has loaded and the cwd has changed. Both carry a data table identifying the sessions involved. Pre handlers run synchronously and the outgoing session is saved one final time after they complete, so windows and buffers closed in a Pre handler are guaranteed to be absent from the saved session file. Pre is skipped on initial startup load; Post fires alone.
1 parent 3791f4d commit 3349b7b

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,33 @@ loaded session.
5151
- When creating or renaming a session, leaving the name blank creates or renames to the default session.
5252

5353

54+
## Events
55+
56+
repossession fires `User` autocmd events around every session switch, so other
57+
plugins and config can react.
58+
59+
| Event | When it fires |
60+
|---|---|
61+
| `RepossessionSwitchPre` | Before a switch, while the old session is still active |
62+
| `RepossessionSwitchPost` | After the new session has loaded and the cwd has changed |
63+
64+
Both events carry a `data` table:
65+
66+
| Event | Field | Value |
67+
|---|---|---|
68+
| `RepossessionSwitchPre` | `old_session` | Path of the outgoing session file |
69+
| | `new_session` | Path of the incoming session file |
70+
| `RepossessionSwitchPost` | `session_file` | Path of the loaded session file |
71+
| | `session_name` | Display name of the loaded session |
72+
| | `git_root` | Git root of the session, or `nil` for local sessions |
73+
74+
Pre handlers run synchronously, and the outgoing session is saved one final
75+
time after they complete — making Pre the right place to close windows and
76+
delete buffers that shouldn't be recorded in the session file. Pre only fires
77+
when there is an active session to switch away from; the initial load at
78+
startup fires Post alone.
79+
80+
5481
## Installation
5582

5683
Using [lazy.nvim](https://github.com/folke/lazy.nvim):

lua/repossession/init.lua

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,21 @@ end
7777

7878

7979
local function activate_session(session_file, git_root, track_history)
80+
if active_session_file then
81+
vim.api.nvim_exec_autocmds("User", {
82+
pattern = "RepossessionSwitchPre",
83+
data = {
84+
old_session = active_session_file,
85+
new_session = session_file,
86+
},
87+
})
88+
89+
-- Deterministic final save of the old session, now that Pre
90+
-- handlers have cleaned up. Overwrites any dirty autosave that
91+
-- fired earlier (e.g. from the picker window closing).
92+
safe_mksession(active_session_file)
93+
end
94+
8095
if track_history ~= false then
8196
last_session_file = active_session_file
8297
last_git_root = active_git_root
@@ -120,6 +135,15 @@ local function activate_session(session_file, git_root, track_history)
120135
else
121136
vim.fn.chdir(cwd)
122137
end
138+
139+
vim.api.nvim_exec_autocmds("User", {
140+
pattern = "RepossessionSwitchPost",
141+
data = {
142+
session_file = session_file,
143+
session_name = get_session_name(session_file),
144+
git_root = git_root,
145+
},
146+
})
123147
end
124148

125149

0 commit comments

Comments
 (0)