1
1
local Lib = require " auto-session-library"
2
+ local autocmds = require " auto-session-autocmds"
2
3
3
4
-- Run comand hooks
4
5
local function run_hook_cmds (cmds , hook_name )
@@ -40,6 +41,11 @@ local defaultConf = {
40
41
41
42
local luaOnlyConf = {
42
43
bypass_session_save_file_types = nil , -- Bypass auto save when only buffer open is one of these file types
44
+ cwd_change_handling = { -- Config for handling the DirChangePre and DirChanged autocmds, can be set to nil to disable altogether
45
+ restore_upcoming_session = true ,
46
+ pre_cwd_changed_hook = nil , -- lua function hook. This is called after auto_session code runs for the `DirChangedPre` autocmd
47
+ post_cwd_changed_hook = nil , -- lua function hook. This is called after auto_session code runs for the `DirChanged` autocmd
48
+ },
43
49
}
44
50
45
51
-- Set default config on plugin load
@@ -57,6 +63,8 @@ function AutoSession.setup(config)
57
63
Lib .setup {
58
64
log_level = AutoSession .conf .log_level ,
59
65
}
66
+
67
+ autocmds .setup_autocmds (AutoSession .conf , AutoSession )
60
68
end
61
69
62
70
local function is_enabled ()
@@ -216,7 +224,7 @@ local function get_session_file_name(sessions_dir)
216
224
-- When we get here session and sessions_dir either both point to a file or do not exist
217
225
return session
218
226
else
219
- local session_name = Lib .conf .last_loaded_session
227
+ local session_name = AutoSession . conf . auto_session_enable_last_session and Lib .conf .last_loaded_session
220
228
if not session_name then
221
229
session_name = Lib .escaped_session_name_from_cwd ()
222
230
local branch_name = get_branch_name ()
@@ -301,22 +309,25 @@ end
301
309
--- Formats an autosession file name to be more presentable to a user
302
310
--- @param path string
303
311
--- @return string
304
- local function format_file_name (path )
312
+ function AutoSession . format_file_name (path )
305
313
return Lib .unescape_dir (path ):match " (.+)%.vim"
306
314
end
307
315
308
316
--- @return PickerItem[]
309
317
function AutoSession .get_session_files ()
310
318
local files = {}
311
319
local sessions_dir = AutoSession .get_root_dir ()
320
+
312
321
if not vim .fn .isdirectory (sessions_dir ) then
313
322
return files
314
323
end
315
- local entries = vim .fn .readdir (sessions_dir , function (item )
324
+
325
+ local entries = vim .fn .readdir (sessions_dir , function (item )
316
326
return vim .fn .isdirectory (item ) == 0
317
327
end )
318
- return vim .tbl_map (function (entry )
319
- return { display_name = format_file_name (entry ), path = entry }
328
+
329
+ return vim .tbl_map (function (entry )
330
+ return { display_name = AutoSession .format_file_name (entry ), path = entry }
320
331
end , entries )
321
332
end
322
333
@@ -342,9 +353,8 @@ local function handle_autosession_command(data)
342
353
local files = AutoSession .get_session_files ()
343
354
if data .args :match " search" then
344
355
open_picker (files , " Select a session:" , function (choice )
345
- AutoSession .AutoSaveSession ()
346
- vim .cmd " %bd!"
347
- AutoSession .RestoreSessionFromFile (choice .display_name )
356
+ -- Change dir to selected session path, the DirChangePre and DirChange events will take care of the rest
357
+ vim .fn .chdir (choice .display_name )
348
358
end )
349
359
elseif data .args :match " delete" then
350
360
open_picker (files , " Delete a session:" , function (choice )
374
384
-- This function avoids calling RestoreSession automatically when argv is not nil.
375
385
function AutoSession .AutoRestoreSession (sessions_dir )
376
386
if is_enabled () and auto_restore () and not suppress_session () then
377
- AutoSession .RestoreSession (sessions_dir )
387
+ return AutoSession .RestoreSession (sessions_dir )
378
388
end
379
389
end
380
390
404
414
-- TODO: make this more readable!
405
415
-- Restores the session by sourcing the session file if it exists/is readable.
406
416
function AutoSession .RestoreSession (sessions_dir_or_file )
407
- Lib .logger .debug (" sessions dir or file" , sessions_dir_or_file )
408
417
local sessions_dir , session_file = extract_dir_or_file (sessions_dir_or_file )
418
+ Lib .logger .debug (" sessions_dir, session_file" , sessions_dir , session_file )
409
419
410
420
local restore = function (file_path , session_name )
411
421
local pre_cmds = AutoSession .get_cmds " pre_restore"
@@ -424,7 +434,10 @@ function AutoSession.RestoreSession(sessions_dir_or_file)
424
434
end
425
435
426
436
Lib .logger .info (" Session restored from " .. file_path )
427
- Lib .conf .last_loaded_session = session_name
437
+
438
+ if AutoSession .conf .auto_session_enable_last_session then
439
+ Lib .conf .last_loaded_session = session_name
440
+ end
428
441
429
442
local post_cmds = AutoSession .get_cmds " post_restore"
430
443
run_hook_cmds (post_cmds , " post-restore" )
@@ -433,7 +446,8 @@ function AutoSession.RestoreSession(sessions_dir_or_file)
433
446
-- I still don't like reading this chunk, please cleanup
434
447
if sessions_dir then
435
448
Lib .logger .debug " ==== Using session DIR"
436
- local session_name = Lib .conf .last_loaded_session
449
+
450
+ local session_name = AutoSession .conf .auto_session_enable_last_session and Lib .conf .last_loaded_session
437
451
local session_file_path
438
452
if not session_name then
439
453
session_file_path = get_session_file_name (sessions_dir )
@@ -459,6 +473,7 @@ function AutoSession.RestoreSession(sessions_dir_or_file)
459
473
end
460
474
else
461
475
Lib .logger .debug " File not readable, not restoring session"
476
+ return false
462
477
end
463
478
end
464
479
elseif session_file then
@@ -473,6 +488,8 @@ function AutoSession.RestoreSession(sessions_dir_or_file)
473
488
else
474
489
Lib .logger .error " Error while trying to parse session dir or file"
475
490
end
491
+
492
+ return true
476
493
end
477
494
478
495
local maybe_disable_autosave = function (session_name )
0 commit comments