feat: directory-aware session resume

persist/recall now key on default-directory so each working directory
tracks its own last session independently.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Levi Neely 2026-04-15 06:59:30 +02:00
parent fa13794468
commit 830c037f9b
1 changed files with 35 additions and 9 deletions

View File

@ -134,18 +134,44 @@ Signals `user-error' if no session is active."
;;;; ──────────────── Last-session persistence ──────────────── ;;;; ──────────────── Last-session persistence ────────────────
(defun ollie--persist-session (id) (defun ollie--persist-session (id)
"Save ID to the last-session file." "Upsert the {cwd}TAB{id} entry for `default-directory' in the last-session file."
(let ((path (ollie--last-session-path))) (let* ((path (ollie--last-session-path))
(cwd (expand-file-name default-directory))
(raw (ollie--fread path))
(lines (when raw
(seq-filter (lambda (l) (not (string-empty-p (string-trim l))))
(split-string raw "\n"))))
(entry (concat cwd "\t" id))
(found nil)
(new-lines
(mapcar (lambda (l)
(let ((parts (split-string l "\t" nil nil)))
(if (and (= (length parts) 2) (string= (car parts) cwd))
(progn (setq found t) entry)
l)))
lines)))
(make-directory (file-name-directory path) t) (make-directory (file-name-directory path) t)
(ollie--fwrite path (concat id "\n")))) (ollie--fwrite path
(concat (mapconcat #'identity
(if found new-lines (append new-lines (list entry)))
"\n")
"\n"))))
(defun ollie--recall-session () (defun ollie--recall-session ()
"Return the last-used session ID if it still exists, else nil." "Return the last-used session ID for `default-directory' if it still exists, else nil."
(when-let* ((raw (ollie--fread (ollie--last-session-path))) (let* ((path (ollie--last-session-path))
(id (string-trim raw))) (cwd (expand-file-name default-directory))
(when (and (not (string-empty-p id)) (raw (ollie--fread path)))
(file-directory-p (expand-file-name id (ollie--sessions-dir)))) (when raw
id))) (catch 'found
(dolist (line (split-string raw "\n"))
(let ((parts (split-string line "\t" nil nil)))
(when (and (= (length parts) 2)
(string= (car parts) cwd))
(let ((id (string-trim (cadr parts))))
(when (and (not (string-empty-p id))
(file-directory-p (expand-file-name id (ollie--sessions-dir))))
(throw 'found id))))))))))
;;;; ──────────────── Session lifecycle ──────────────── ;;;; ──────────────── Session lifecycle ────────────────