Unarchiving sessions
Would it make sense to have a command for unarchiving sessions? I just found myself restarting Emacs then realized I had forgotten to save a session.
I really like the idea of this package, by the way!
Have you tried with mindstream-persist set to true? That might be the UX you're looking for.
I did consider setting mindstream-persist to true, but most of the time, when I close Emacs, I do want my notes to be archived. I think my ideal UX would be for sessions to be automatically archived on startup, but for there to be a way to undo that. (But it's possible I misunderstood the purpose of the archive.)
I guess you're looking for something like Emacs's session recovery feature, where if Emacs crashes it allows you to select a crashed session by date, and then attempt to recover all unsaved buffers in that session?
At the moment, a problem is that once archived, the sessions join dozens or hundreds of other sessions that you might already have there, and it's not so easy to guess which sessions you might be interested in un-archiving.
We could potentially introduce a lightweight indexing mechanism that captures the set of sessions that were archived in bulk each time that's done (e.g. on startup), and then provide an unarchive feature that allows you to select from these by timestamp.
One thing to know about Mindstream's design is that it strives to defer as much functionality as possible to standard system tools like the filesystem, and tries to avoid "state" or any Emacs-specific magic. For this reason I think it would be preferable for the state here (i.e. the set of archived sessions) to be stored on disk in a file somewhere in the archive folder, so that there's no Emacs-specific magic, and so that it's robust to multiple crashes. Btw this design also means that to fix your issue you could just move the archived folder back into your anonymous path manually, or to any other location on disk (where it would be considered "saved") using a standard file manager like dired or the shell, and Mindstream would handle it appropriately without you needing to do anything additional within Emacs besides the usual commands.
Another, orthogonal, idea to consider is to file all sessions under the current date (currently it's just by template name -- so the proposal here is something like /path/to/mindstream/archive/elisp/2024-08-19/ab123df6...), so that they are easier to find within the archive folder, whether we move it manually or programmatically.
In general, I agree there do seem to be cases (though "uncommon") when we'd like to search the archive for things (usually recent things, as you've pointed out) that we were working on at some point. It could be useful to be able to quickly browse the archive in order of recency, and then have a way to unarchive (or save, for that matter) once we've found the session we care about.
Sorry, that's a lot of words and not very much direction. I'm just thinking out loud (disjointedly! It's late here).
I was more just imagining that you could find the session you want to unarchive yourself in the filesystem, and then there could be a command to unarchive it for you. Here's a quick draft that seems to work for me (though I'd want to clean it up a little bit more before making a PR):
(defun mindstream--archived-sessions ()
"Return a list of all directories of archived sessions."
(let* ((archived-templates (mindstream--directory-dirs mindstream-archive-path))
(archived-sessions (mapcar #'mindstream--directory-dirs archived-templates)))
(seq-reduce #'append archived-sessions nil)))
(defun mindstream--archived-session-p (session-dir)
"Is SESSION-DIR an archived session?"
(and session-dir
(file-in-directory-p session-dir mindstream-archive-path)))
(defun mindstream-unarchive (archived-session)
"Move ARCHIVED-SESSION back to `mindstream-path'.
Interactively, if the current buffer is part of an archived
session, unarchive that session. Otherwise, prompt the user for
the name of an archived session."
(interactive
(list
(let ((current-session-dir (mindstream--session-dir (current-buffer))))
(if (mindstream--archived-session-p current-session-dir)
current-session-dir
(completing-read "Choose an archived session: " (mindstream--archived-sessions))))))
(let* ((template (file-name-nondirectory (directory-file-name (file-name-directory (directory-file-name archived-session)))))
(new-path (mindstream--build-path mindstream-path template (file-name-nondirectory archived-session))))
(mindstream--move-dir archived-session new-path)
(unless (and buffer-file-name (file-in-directory-p buffer-file-name new-path))
(find-file new-path))))
I agree it would be nice to have a way to search the archive for things, but a command like that is all I was suggesting personally.
Once you've tracked the session down, you should be able to save it using mindstream-save-session. Do you have a specific reason for preferring to unarchive rather than save here? I want to make sure I understand your perspective.
It's mostly just that I hadn't thought of trying mindstream-save-session for some reason! But I just tried it, and I got the error Not a mindstream buffer!
I just pushed a fix for that. Let me know if that addresses it for you. I'm also happy to continue the conversation on unarchiving if you feel it's a separately warranted feature.
While this PR doesn't introduce unarchiving, it files all sessions under their creation date, which should make the archive easier to navigate and much more useful. These sessions can still be saved (as discussed above).
Let me know if you would still like to have the ability to unarchive. One reason I can think of for when we might want to is if we set persist to true, and we would like to restore an archived session as an "active" one that we'd want to have open each time Emacs restarts. Any further thoughts welcome @AjaiKN .
I've been a little busy lately, and I haven't been using mindstream the last couple weeks, so I probably won't get around to thinking about this for a little while. But thank you for putting so much thought into this! I think my original problem has been solved, so feel free to close the issue if you'd like.
Sounds good. I'm happy to continue the conversation (and would accept a PR), but I'll close this issue for now. Thanks!