-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpartial-recall-hygiene.el
125 lines (87 loc) · 3.6 KB
/
partial-recall-hygiene.el
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
;;; partial-recall-hygiene.el --- Automatic flushing -*- lexical-binding: t; -*-
;; Author: Krister Schuchardt <[email protected]>
;; Homepage: https://github.com/Walheimat/partial-recall
;; Version: 0.13.1
;; Package-Requires: ((emacs "29.1"))
;; Keywords: frames files convenience
;;; Commentary:
;; Adds hygiene behavior to memories. This means that (1) memories are
;; regularly flushed and that (2) a warning is emitted for memories
;; that have reached maximum size.
;;; Code:
(require 'partial-recall)
;;;; Customization
(defcustom partial-recall-hygiene-idle-delay 5
"Idle delay before running hygiene routine."
:type 'integer
:group 'partial-recall)
(defcustom partial-recall-hygiene-flush t
"Whether to flush memories regularly."
:type 'boolean
:group 'partial-recall)
(defcustom partial-recall-hygiene-warn-on-full t
"Whether a memory reaching maximum size should emit a warning.
This is only done once."
:type 'boolean
:group 'partial-recall)
(defcustom partial-recall-hygiene-nag-buffer-action-params '((window-height . 1))
"The buffer display action alist for nag buffers."
:type 'alist
:group 'partial-recall)
(defcustom partial-recall-hygiene-messaging-method 'partial-recall-hygiene--message
"The method through which messaging is done.
The function will be called with a single argument: the message to be
displayed."
:type 'function
:group 'partial-recall)
;;;; Private variables
(defvar partial-recall-hygiene--timer nil
"Timer for for `partial-recall-hygiene-mode'.
Runs `partial-recall-hygiene--on-idle'.")
(defvar partial-recall-hygiene--warned nil
"List of memories that have already.")
(defvar partial-recall-hygiene--nag-buffer-name "*partial-recall-nag*"
"Name of the buffer displaying nags.")
;;;; Functionality
(defun partial-recall-hygiene--message (message)
"Display MESSAGE using function `partial-recall-log'."
(let ((partial-recall-log t))
(partial-recall-log message)))
(defun partial-recall-hygiene--on-idle ()
"Run a hygiene routine.
This flushes moments from all memories.
See `partial-recall--flush' and `partial-recall-memorable-traits' for
the heuristics on which moments get flushed."
(partial-recall-debug "Running hygiene routine")
(let ((sum 0)
(full nil))
(dolist (memory (partial-recall-memories))
;; Flush moments.
(when partial-recall-hygiene-flush
(setq sum (+ sum
(partial-recall--flush memory nil t))))
;; Keep track of full memories.
(when (and partial-recall-hygiene-warn-on-full
(partial-recall-memory--near-capacity-p memory)
(not (member (partial-recall-memory--unique memory) partial-recall-hygiene--warned)))
(setq full (append full (list (partial-recall-memory--unique memory))))))
(when (> sum 0)
(partial-recall-debug "Flushed %d moments in total" sum))
(when full
(setq partial-recall-hygiene--warned full)
(funcall partial-recall-hygiene-messaging-method
(format "Following memories are full: %s"
(mapconcat #'identity full ", "))))))
;;;; API
;;;###autoload
(define-minor-mode partial-recall-hygiene-mode
"Adds automatic flushing to `partial-recall' memories."
:group 'partial-recall
:global t
(if partial-recall-hygiene-mode
(setq partial-recall-hygiene--timer
(run-with-idle-timer partial-recall-hygiene-idle-delay t #'partial-recall-hygiene--on-idle))
(cancel-timer partial-recall-hygiene--timer)
(setq partial-recall-hygiene--timer nil)))
(provide 'partial-recall-hygiene)
;;; partial-recall-hygiene.el ends here