jao-doc-session.el 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. ;;; jao-doc-session.el --- persistent document sessions -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2022, 2024 jao
  3. ;; Author: jao <mail@jao.io>
  4. ;; Keywords: docs
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. ;;; Code:
  16. (persist-defvar jao-doc-session nil "Documents session")
  17. (defvar-local jao-doc-session--is-doc nil)
  18. (defun jao-doc-session-is-doc (&optional buffer)
  19. "Check whether the given or current buffer belong to the doc session."
  20. (buffer-local-value 'jao-doc-session--is-doc (or buffer (current-buffer))))
  21. (defun jao-doc-session (&optional file) jao-doc-session)
  22. (defun jao-doc-session-save (&optional skip-current force)
  23. "Traverse all current buffers and update the value of `jao-doc-session'."
  24. (interactive)
  25. (let ((docs '())
  26. (cb (and skip-current (current-buffer))))
  27. (dolist (b (buffer-list))
  28. (when-let (fs (and (not (eq cb b)) (jao-doc-session-is-doc b)))
  29. (dolist (f fs) (add-to-list 'docs f))))
  30. (when (or force (> (length docs) 0))
  31. (setq jao-doc-session docs))))
  32. (defun jao-doc-session-mark (&optional path)
  33. "Mark the current buffer's file, or PATH, as persistent across sessions."
  34. (unless (listp jao-doc-session--is-doc)
  35. (setq jao-doc-session--is-doc (ensure-list jao-doc-session--is-doc)))
  36. (cl-pushnew (or path (buffer-file-name)) jao-doc-session--is-doc)
  37. (jao-doc-session-save))
  38. (defun jao-doc-session--maybe-save ()
  39. (when (jao-doc-session-is-doc) (jao-doc-session-save t)))
  40. (defvar jao-doc-session-inhibit-save nil)
  41. (add-hook 'kill-buffer-hook #'jao-doc-session--maybe-save)
  42. (provide 'jao-doc-session)
  43. ;;; jao-doc-session.el ends here