@@ -5,6 +5,16 @@ local pending_config
55local current_session
66local tab_sessions = {}
77local session_configs = {}
8+ local hooks = setmetatable ({
9+ pre_load = {},
10+ post_load = {},
11+ pre_save = {},
12+ post_save = {},
13+ }, {
14+ __index = function (t , key )
15+ error (string.format (" Unrecognized hook '%s'" , key ))
16+ end ,
17+ })
818
919local function do_setup ()
1020 if pending_config then
@@ -14,6 +24,12 @@ local function do_setup()
1424 end
1525end
1626
27+ local function dispatch (name , ...)
28+ for _ , cb in ipairs (hooks [name ]) do
29+ cb (... )
30+ end
31+ end
32+
1733--- Initialize resession with configuration options
1834--- @param config table
1935M .setup = function (config )
@@ -139,6 +155,7 @@ local function save(name, opts, target_tabpage)
139155 local layout = require (" resession.layout" )
140156 local util = require (" resession.util" )
141157 local filename = util .get_session_file (name , opts .dir )
158+ dispatch (" pre_save" , name , opts , target_tabpage )
142159 local data = {
143160 buffers = {},
144161 tabs = {},
@@ -205,6 +222,7 @@ local function save(name, opts, target_tabpage)
205222 dir = opts .dir ,
206223 }
207224 end
225+ dispatch (" post_save" , name , opts , target_tabpage )
208226end
209227
210228--- Save a session to disk
@@ -326,6 +344,7 @@ local function close_everything()
326344 vim .cmd (" silent! only" )
327345end
328346
347+ local _is_loading = false
329348--- Load a session
330349--- @param name nil | string
331350--- @param opts nil | resession.LoadOpts
@@ -388,6 +407,8 @@ M.load = function(name, opts)
388407 end
389408 return
390409 end
410+ dispatch (" pre_load" , name , opts )
411+ _is_loading = true
391412 if opts .reset == " auto" then
392413 opts .reset = not data .tab_scoped
393414 end
@@ -476,6 +497,28 @@ M.load = function(name, opts)
476497 dir = opts .dir ,
477498 }
478499 end
500+ _is_loading = false
501+ dispatch (" post_load" , name , opts )
502+ end
503+
504+ --- Add a callback that runs at a specific time
505+ --- @param name " pre_save" | " post_save" | " pre_load" | " post_load"
506+ --- @param callback fun ()
507+ M .add_hook = function (name , callback )
508+ table.insert (hooks [name ], callback )
509+ end
510+
511+ --- Remove a hook callback
512+ --- @param name " pre_save" | " post_save" | " pre_load" | " post_load"
513+ --- @param callback fun ()
514+ M .remove_hook = function (name , callback )
515+ local cbs = hooks [name ]
516+ for i , cb in ipairs (cbs ) do
517+ if cb == callback then
518+ table.remove (cbs , i )
519+ break
520+ end
521+ end
479522end
480523
481524--- The default config.buf_filter (takes all buflisted files with "", "acwrite", or "help" buftype)
@@ -492,6 +535,12 @@ M.default_buf_filter = function(bufnr)
492535 return vim .bo [bufnr ].buflisted
493536end
494537
538+ --- Returns true if a session is currently being loaded
539+ --- @return boolean
540+ M .is_loading = function ()
541+ return _is_loading
542+ end
543+
495544-- Make sure all the API functions trigger the lazy load
496545for k , v in pairs (M ) do
497546 if type (v ) == " function" and k ~= " setup" then
0 commit comments