summaryrefslogtreecommitdiffhomepage
path: root/lib/doc/jao-doc-session.el
blob: 669b19145a0175fac82e9aedaeb47fe629bb1238 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
;;; jao-doc-session.el --- persistent document sessions  -*- lexical-binding: t; -*-

;; Copyright (C) 2022  jao

;; Author: jao <mail@jao.io>
;; Keywords: docs

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Code:

(defvar jao-doc-session-file "~/.emacs.d/cache/doc-view-session.eld")
(defvar-local jao-doc-session--is-doc nil)

(defun jao-doc-session-is-doc (&optional buffer)
  (buffer-local-value 'jao-doc-session--is-doc (or buffer (current-buffer))))

(defun jao-doc-session--read-file (file)
  (let ((buff (find-file-noselect file)))
    (ignore-errors
      (with-current-buffer buff
        (goto-char (point-min)))
      (read buff))))

(defun jao-doc-session--save-to-file (file value)
  (with-current-buffer (find-file-noselect file)
    (erase-buffer)
    (insert (format "%S" value))
    (save-buffer)))

(defun jao-doc-session (&optional file)
  (let ((file (or file jao-doc-session-file)))
    (jao-doc-session--read-file file)))

(defun jao-doc-session-save (&optional skip-current force)
  (interactive)
  (let ((docs '())
        (cb (and skip-current (current-buffer))))
    (dolist (b (buffer-list))
      (when-let (fs (and (not (eq cb b)) (jao-doc-session-is-doc b)))
        (dolist (f fs) (add-to-list 'docs f))))
    (when (or force (> (length docs) 0))
      (jao-doc-session--save-to-file jao-doc-session-file docs))))

(defun jao-doc-session--save-session ()
  (let ((inhibit-message t)
        (message-log-max nil))
    (when (not jao-doc-session-inhibit-save) (jao-doc-session-save))
    t))

(defun jao-doc-session-mark (&optional path)
  (unless (listp jao-doc-session--is-doc)
    (setq jao-doc-session--is-doc (ensure-list jao-doc-session--is-doc)))
  (cl-pushnew (or path (buffer-file-name)) jao-doc-session--is-doc)
  (jao-doc-session--save-session))

(defun jao-doc-session--save-1 ()
  (when (jao-doc-session-is-doc) (jao-doc-session-save t)))

(defvar jao-doc-session-inhibit-save nil)

(add-hook 'kill-emacs-query-functions #'jao-doc-session--save-session)
(add-hook 'kill-buffer-hook #'jao-doc-session--save-1)

(provide 'jao-doc-session)
;;; jao-doc-session.el ends here