-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpartial-recall.el
2078 lines (1533 loc) · 69.4 KB
/
partial-recall.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; partial-recall.el --- STM for `tab-bar-mode' -*- 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:
;; Short-term (buffer) memory for `tab-bar-mode' tabs.
;;
;;`partial-recall' will keep track of meaningful buffers opened in a
;; tab in a ring. A buffer is meaningful if it satisfies user-defined
;; traits. The aforementioned rings are called memories in the context
;; of this package; the current memory is called reality, all other
;; memories are dreams. Buffers are time-stamped to (1) allow for the
;; memory to grow if the oldest one is still relatively young and (2)
;; to allow reclaiming buffers from other memories if they're
;; relatively old. Buffers are called moments in the context of this
;; package.
;;
;; Moments can be reclaimed from other memories, they can be
;; forgotten, they can be made permanent and they can be reinforced.
;; All of these things happen automatically but can be performed
;; explicitly by the user as well.
;;
;; When moments are forgotten, their buffers are marked as belonging
;; to the subconscious (from where they can be lifted).
;;; Code:
(require 'cl-lib)
(require 'ring)
(require 'subr-x)
(require 'eieio)
;;;; Customization
(defgroup partial-recall nil
"Short-term (buffer) memory."
:group 'partial-recall)
(defcustom partial-recall-handle-delay 3
"The delay in seconds after which a buffer will be handled.
It should give you ample time to switch to another buffer before
handling. The underlying time is reset on every switch.
Buffers visited with a no-record flag aren't handled."
:type 'integer
:group 'partial-recall)
(defcustom partial-recall-memory-size 10
"The amount of buffers to recall per tab.
This limit may temporarily increase if buffers are remembered in
quick succession. See `partial-recall-intermediate-term'."
:type 'integer
:group 'partial-recall)
(defcustom partial-recall-intermediate-term (* 20 60)
"Threshold after which moments are handled differently.
This threshold is used in three places: (1) Moments at or above
this threshold can be reclaimed automatically. (2) Moments below
this value will not be flushed. (3) Moments below this threshold
will entail switching to their tab if
`partial-recall-auto-switch' is t.
See `partial-recall--reclaim', `partial-recall--gracedp' and
`partial-recall--maybe-switch-memory'.
Can be set to nil to disable this behavior."
:type '(choice (integer :tag "Number of seconds")
(const :tag "Don't use" nil))
:group 'partial-recall)
(defcustom partial-recall-auto-switch t
"Whether to automatically switch to a buffer's memory."
:type '(choice (const :tag "Don't switch" nil)
(const :tag "Switch" t)
(const :tag "Prompt first" prompt))
:group 'partial-recall)
(defcustom partial-recall-lighter-prefix "pr"
"The prefix used in `partial-recall-lighter'."
:type 'string
:group 'partial-recall)
(defcustom partial-recall-lighter-show-info t
"Whether to show information about memory and moment in lighter."
:type 'boolean
:group 'partial-recall)
(defcustom partial-recall-record-triggers '(consult-buffer)
"Commands that should trigger recording the buffer.
It will allow the buffer visited before these commands to be
considered visible. This is relevant for
`partial-recall-concentration--concentrate' which increases a
moment's focus."
:type '(repeat symbol)
:group 'partial-recall)
(defcustom partial-recall-log t
"Whether to log.
This is either nil meaning no logging, or 1 for info logging and
0 for debug logging. Any other symbol also means info logging."
:type '(choice (const :tag "No logging" nil)
(symbol :tag "Default")
(const :tag "Info" 1)
(const :tag "Debug" 0))
:group 'partial-recall)
(defcustom partial-recall-log-echo nil
"Whether to also log messages in the echo area."
:type 'boolean
:group 'partial-recall)
(defcustom partial-recall-log-prefix "PR"
"The prefix used for log messages."
:type '(choice (const :tag "Text" "PR")
(const :tag "None" nil))
:group 'partial-recall)
(defcustom partial-recall-filter '("COMMIT_EDITMSG" "git-rebase-todo")
"Names of buffers that should be ignored.
See trait `partial-recall--not-filtered-p'."
:type '(repeat regexp)
:group 'partial-recall)
(defcustom partial-recall-meaningful-traits '(buffer-file-name
partial-recall--not-filtered-p
partial-recall--not-in-view-mode-p
partial-recall--not-to-be-viewed-p
partial-recall--not-banished-p)
"List of functions that describe traits of a meaningful buffer.
These functions are inspected using `func-arity'. If they have a
minimum arity of at least 1 OR the symbol `many' for their
maximum arity, they will be called with `current-buffer' as the
first argument, otherwise they are called with no argument.
If any such function does not return a truthy value, the buffer
is not considered meaningful, meaning a buffer needs to satisfy
all predicates."
:type '(repeat function)
:group 'partial-recall)
(defcustom partial-recall-memorable-traits '(partial-recall--gracedp)
"List of functions that determine a memorable moment.
These functions are called with moments up for suppression and
the current prefix argument.
If any such function does return a truthy value, the moment is
considered memorable. See `partial-recall--flush'."
:type '(repeat function)
:group 'partial-recall)
(defcustom partial-recall-intensities '((swap . -10) (reinsert . 20) (concentrate . 4))
"The amount of focus gained (or lost) from actions.
See `partial-recall-moment--adjust' and its callers."
:type '(alist :key-type symbol :value-type integer)
:group 'partial-recall)
;;;; Variables
;;;;; Private variables
(defvar partial-recall--table (make-hash-table :test #'equal)
"Hash table mapping tabs to memories using common key.")
(defvar partial-recall--schedule-timer nil
"Timer that runs until a buffer will be handled.
Switching to a buffer just schedules its handling. Only the
buffer the user lingers on will actually be handled.")
(defvar partial-recall--last-focus nil
"The moment that was last focused.")
(defvar partial-recall--last-handled nil
"The buffer that was last handled.")
(defvar partial-recall--neglect nil
"If t, the next visited buffer will not be handled.")
(defvar partial-recall--switch-to-buffer-function #'switch-to-buffer
"Function to call when switching to a buffer.")
(defvar partial-recall--pop-to-buffer-function #'pop-to-buffer
"Function to call when popping to a buffer.")
(defvar partial-recall--before-minibuffer nil
"Buffer visited before minibuffer was set up.")
(defvar-local partial-recall--permanent nil
"Whether the buffer's moment is permanent.
This is only set for extensions and doesn't affect internal
logic.")
(defvar partial-recall--restored-tab nil
"The tab that was just restored.")
(defvar partial-recall--to-be-viewed nil
"The buffer about to be viewed by `view-buffer'.")
(defvar-local partial-recall--banished nil
"Whether the buffer should never be scheduled.
This has no effect if `partial-recall--not-banished-p' is not
part of `partial-recall-meaningful-traits'.")
;;;;; Maps
;;;###autoload
(defvar partial-recall-command-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "4") 'partial-recall-switch-to-buffer-other-window)
(define-key map (kbd "b") 'partial-recall-switch-to-buffer)
(define-key map (kbd "c") 'partial-recall-reclaim)
(define-key map (kbd "f") 'partial-recall-forget)
(define-key map (kbd "g") 'partial-recall-pop-to-logs)
(define-key map (kbd "i") 'partial-recall-make-permanent)
(define-key map (kbd "j") 'partial-recall-reject)
(define-key map (kbd "k") 'partial-recall-forget-some)
(define-key map (kbd "l") 'partial-recall-lift)
(define-key map (kbd "m") 'partial-recall-menu)
(define-key map (kbd "n") 'partial-recall-next)
(define-key map (kbd "o") 'partial-recall-explain-omission)
(define-key map (kbd "p") 'partial-recall-previous)
(define-key map (kbd "r") 'partial-recall-remember)
(define-key map (kbd "s") 'partial-recall-remember-some)
(define-key map (kbd "t") 'partial-recall-retrieve)
(define-key map (kbd "u") 'partial-recall-meld)
(define-key map (kbd "x") 'partial-recall-flush)
(define-key map (kbd "z") 'partial-recall-banish)
map)
"Map for `partial-recall-mode' commands.")
(defvar-keymap partial-recall-navigation-map
:doc "Keymap to repeat navigation commands."
:repeat t
"n" 'partial-recall-next
"p" 'partial-recall-previous)
;;;;; Faces
(defface partial-recall-emphasis
'((t (:inherit (font-lock-type-face))))
"Face used for emphasis."
:group 'partial-recall)
(defface partial-recall-soothe
'((t (:inherit (success))))
"Face used to soothe."
:group 'partial-recall)
(defface partial-recall-deemphasized
'((t (:inherit (shadow))))
"Face used to de-emphasize."
:group 'partial-recall)
(defface partial-recall-contrast
'((t (:inherit (warning))))
"Face used for contrast."
:group 'partial-recall)
(defface partial-recall-alert
'((t (:inherit (error))))
"Face used to alert."
:group 'partial-recall)
;;;;; Hooks
(defvar partial-recall-before-probe-hook nil
"Functions called before a memory was probed.
Each function will be called with the probed memory as its only
argument.")
(defvar partial-recall-after-probe-hook nil
"Functions called after a memory was probed.
Each function will be called with the probed memory as its only
argument.")
(defvar partial-recall-permanence-change-hook nil
"Functions called after a moment's permanence has changed.
Each function will be called with the moment and its new
permanence value as arguments.")
(defvar partial-recall-after-insert-hook nil
"Functions called after a moment was inserted.
Each function will be called with the inserted moment as its only
argument.")
(defvar partial-recall-after-create-hook nil
"Functions run after a memory is created.
Each function will be called with the name of the memory (that is
the name of the tab).")
(defvar partial-recall-after-reality-change-hook nil
"Function to run after reality changes.
Each function is called with the name of the memory (that is the
name of the tab).")
(defvar partial-recall-after-focus-change-hook nil
"Function to run after focus changes.
Each function will be called with the moment that gained focus.")
;;;; Structures
(cl-defstruct (partial-recall-moment
(:conc-name partial-recall-moment--)
(:constructor partial-recall-moment--create
(buffer
&aux
(timestamp (floor (time-to-seconds)))
(focus 0)
(permanence nil))))
"A moment of partial recall.
A moment is defined by a buffer, a timestamp when that buffer was
first remembered, a count of how many times it was updated and a
permanence marker that can prevent it from being forgotten if
`partial-recall-plasticity-of-moment' is on.
The timestamp is distinct from `buffer-display-time' and the
focus is distinct from `buffer-display-count'."
buffer timestamp focus permanence)
(cl-defstruct (partial-recall-memory
(:conc-name partial-recall-memory--)
(:constructor partial-recall-memory--create
(key
&key
(orig-size partial-recall-memory-size)
&aux
(moments (make-ring orig-size)))))
"A memory of partial recall.
A memory is a key that connects it to the hash table, a ring of
moments and the size it had upon construction."
key orig-size moments)
;;;; Utility
;;;;; Key creation and look-up
(defun partial-recall--key (&optional tab)
"Get the hash key of TAB."
(when-let* ((tab (or tab (tab-bar--current-tab))))
(alist-get 'pr tab)))
(defun partial-recall--create-key (tab)
"Create the key for TAB.
This uses a message digest of the tab, a random number, the Emacs
PID and `recent-keys' vector."
(let ((object (format "%s%s%s%s" tab (random) (emacs-pid) (recent-keys))))
(md5 object)))
(defun partial-recall--memory-by-key (key &optional size)
"Get or create memory identified by KEY.
If not found, the memory is created, optionally using size SIZE."
(when key
(if-let* ((table partial-recall--table)
(memory (gethash key table)))
memory
(let* ((size (or size partial-recall-memory-size))
(new-memory (partial-recall-memory--create key :orig-size size)))
(puthash key new-memory table)
new-memory))))
;;;;; Rings
(defun partial-recall--ring-oldest (ring)
"Get the oldest element in RING."
(unless (ring-empty-p ring)
(ring-ref ring (1- (ring-length ring)))))
(defun partial-recall--ring-youngest (ring)
"Get the oldest element in RING."
(unless (ring-empty-p ring)
(ring-ref ring 0)))
(defun partial-recall--ring-insert (ring item &optional extend)
"Insert ITEM in RING.
If EXTEND is t, also extend."
(ring-insert+extend ring item extend)
(run-hook-with-args 'partial-recall-after-insert-hook item)
item)
;;;;; Windows
(defun partial-recall--window-list ()
"Get all windows."
(cl-loop for frame in (visible-frame-list)
append (window-list frame)))
(defun partial-recall--maybe-call-with-buffer (buffer fun)
"Call FUN maybe with BUFFER."
(let* ((arity (func-arity fun))
(min (car arity))
(max (cdr arity)))
(if (or (> min 0)
(eq 'many max)
(and (numberp max)
(> max 0)))
(funcall fun buffer)
(partial-recall-warn "Function `%s' has the wrong arity" fun)
t)))
;;;;; Frames
(defmacro partial-recall--in-other-frame (memory &rest forms)
"Evaluate FORMS in other frame.
If MEMORY is not in another form, this is a no-op."
(declare (indent defun))
`(and-let* ((partial-recall--foreignp ,memory)
(info (partial-recall-memory--find-in-other-frame ,memory))
(frame (plist-get info :frame)))
(partial-recall-debug "Evaluating on behalf of `%s' in other frame" ,memory)
(with-selected-frame frame
,@forms)))
(defun partial-recall--is-other-frame-p (frame)
"Check that FRAME is not the selected frame."
(not (eq frame (selected-frame))))
(defun partial-recall--foreignp (memory)
"Check if MEMORY belongs to foreign frame."
(null (partial-recall--find-tab-from-memory memory)))
;;;; Accessors
;;;;; Memories
(defun partial-recall-memories ()
"Get all memories."
(hash-table-values partial-recall--table))
(defun partial-recall-memory--owns-buffer-p (memory buffer)
"Check if MEMORY owns BUFFER."
(let ((moments (partial-recall-memory--moments memory)))
(catch 'found
(dotimes (ind (ring-length moments))
(when (equal buffer (partial-recall-moment--buffer (ring-ref moments ind)))
(throw 'found ind))))))
(defun partial-recall-memory--remove-buffer (buffer memory)
"Remove BUFFER from MEMORY and return it."
(when-let* ((moments (partial-recall-memory--moments memory))
(index (partial-recall-memory--owns-buffer-p memory buffer))
(removed (ring-remove moments index)))
removed))
(defun partial-recall-memory--find-in-other-frame (memory)
"Get tab and frame of MEMORY."
(when-let* ((other-frames (filtered-frame-list #'partial-recall--is-other-frame-p))
(found (catch 'found
(dotimes (ind (length other-frames))
(when-let* ((frame (nth ind other-frames))
(tab (partial-recall--find-tab-from-memory memory frame)))
(throw 'found (list :tab tab :frame frame)))))))
found))
(defun partial-recall-memory--has-buffers-p (&optional memory)
"Check if the MEMORY has buffers."
(when-let* ((memory (or memory (partial-recall--reality)))
(moments (partial-recall-memory--moments memory)))
(not (ring-empty-p moments))))
(defun partial-recall-memory--name (memory)
"Get the name of MEMORY."
(partial-recall--find-any-tab-from-memory memory))
(defun partial-recall-memory--unique (memory)
"Get the unique name of MEMORY.
This is just indirection to access the key slot."
(partial-recall-memory--key memory))
(defun partial-recall-memory--removed-buffers (&optional memory)
"Get a list of buffers removed from MEMORY."
(let* ((memory (or memory (partial-recall--reality)))
(buffers (partial-recall--suppressed (partial-recall-memory--unique memory))))
buffers))
(defun partial-recall--reality ()
"Get the current memory."
(partial-recall--memory-by-key (partial-recall--key)))
(defun partial-recall--realityp (memory)
"Check if MEMORY is the reality."
(eq (partial-recall--reality) memory))
(defun partial-recall-memory--near-capacity-p (memory &optional threshold)
"Check if MEMORY is near capacity.
Optional THRESHOLD determines what this means. If it is not
passed, this checks if the memory is exactly at capacity. Passing
a positive integer determines if the capacity is near that
threshold."
(when-let ((threshold (or threshold 0))
(ring (partial-recall-memory--moments memory)))
(>= (ring-length ring)
(- (ring-size ring) threshold))))
;;;;; Moments
(defun partial-recall-moments ()
"Get all moments."
(cl-loop for _k being the hash-keys of partial-recall--table
using (hash-values memory)
append (ring-elements (partial-recall-memory--moments memory))))
(defun partial-recall-moment--set-permanence (moment permanence)
"Set MOMENT PERMANENCE."
(setf (partial-recall-moment--permanence moment) permanence)
(with-current-buffer (partial-recall-moment--buffer moment)
(setq-local partial-recall--permanent permanence))
(run-hook-with-args 'partial-recall-permanence-change-hook moment permanence)
moment)
(defun partial-recall-moment--update-timestamp (moment)
"Update the timestamp for MOMENT."
(setf (partial-recall-moment--timestamp moment) (floor (time-to-seconds)))
moment)
(defun partial-recall-moment--adjust-focus (moment context)
"Adjust the focus for MOMENT using CONTEXT.
Permanent moments do not gain additional focus."
(and-let* ((amount (or (alist-get context partial-recall-intensities) 0))
((or (not (partial-recall-moment--permanence moment))
(< amount 0)))
(count (partial-recall-moment--focus moment))
(updated-count (max 0 (+ count amount))))
(setf (partial-recall-moment--focus moment) updated-count)
(run-hook-with-args 'partial-recall-after-focus-change-hook moment updated-count)
moment))
(defun partial-recall-moment--adjust (moment &optional context)
"Intensify MOMENT.
This will update its timestamp and increment its focus.
If CONTEXT has a value in `partial-recall-intensities', increase
by that amount."
(partial-recall-moment--adjust-focus moment context)
(partial-recall-moment--update-timestamp moment))
(defun partial-recall-moment--reset-count (moment)
"Reset the focus for MOMENT."
(setf (partial-recall-moment--focus moment) 0)
moment)
;;;;; Buffers
(defun partial-recall-buffers ()
"Get all mapped buffers."
(mapcar #'partial-recall-moment--buffer (partial-recall-moments)))
(defun partial-recall--buffer-in-memory-p (buffer &optional memory)
"Check if BUFFER is a member of MEMORY.
If MEMORY is not passed, use the current reality."
(partial-recall-memory--owns-buffer-p (or memory (partial-recall--reality)) buffer))
(defun partial-recall--buffer-mapped-p (buffer)
"Check if BUFFER is mapped."
(let ((buffers (partial-recall-buffers)))
(memq buffer buffers)))
(defun partial-recall--buffer-visible-p (buffer)
"Check that BUFFER remains visible.
This also checks for buffers that might have been obscured."
(let* ((windows (partial-recall--window-list))
(visible (and buffer
(buffer-live-p buffer)
(or (eq buffer partial-recall--before-minibuffer)
(memq buffer (mapcar #'window-buffer windows))))))
visible))
(defun partial-recall--buffer-new-p (buffer)
"Check if BUFFER is actually new."
(cond
((eq partial-recall--last-handled buffer)
nil)
((eq partial-recall--neglect buffer)
(setq partial-recall--neglect nil))
(t t)))
(defun partial-recall--buffer-equals-moment-p (buffer moment)
"Check if BUFFER is encapsulated by MOMENT."
(eq (partial-recall-moment--buffer moment) buffer))
(defun partial-recall--buffer-focus (&optional buffer)
"Get the focus of BUFFER."
(when-let* ((buffer (or buffer (current-buffer)))
(moment (partial-recall--find-owning-moment buffer)))
(partial-recall-moment--focus moment)))
(defun partial-recall--find-owning-memory (&optional buffer)
"Return the memory that owns BUFFER.
Defaults to the current buffer."
(let* ((buffer (or buffer (current-buffer)))
(memories (partial-recall-memories))
(find (apply-partially #'partial-recall--buffer-in-memory-p buffer)))
(seq-find find memories)))
(defun partial-recall--find-owning-moment (buffer &optional memory)
"Get the moment that encapsulates BUFFER.
Searches all memories unless MEMORY is provided."
(when-let* ((memory (or memory
(let ((memories (partial-recall-memories))
(find-memory (apply-partially #'partial-recall--buffer-in-memory-p buffer)))
(seq-find find-memory memories))))
(ring (partial-recall-memory--moments memory))
(index (partial-recall-memory--owns-buffer-p memory buffer)))
(ring-ref ring index)))
(defun partial-recall-current-moment ()
"Get the current buffer's moment."
(partial-recall--find-owning-moment (current-buffer)))
;;;;; Tabs
(defun partial-recall--find-tab-from-memory (memory &optional frame)
"Get the tab for MEMORY.
Optionally search in FRAME."
(when-let ((key (partial-recall-memory--unique memory))
(tabs (if frame
(with-selected-frame frame
(funcall tab-bar-tabs-function frame))
(funcall tab-bar-tabs-function))))
(seq-find (lambda (it) (string= key (alist-get 'pr it))) tabs)))
(defun partial-recall--find-any-tab-from-memory (memory)
"Get the tab name for MEMORY searching all frames."
(let ((frames (frame-list)))
(catch 'found
(dotimes (ind (length frames))
(when-let* ((frame (nth ind frames))
(name (partial-recall--find-tab-name-from-memory memory frame)))
(throw 'found name))))))
(defun partial-recall--find-tab-name-from-memory (&optional memory frame)
"Get the tab name for MEMORY.
Optionally search in FRAME."
(when-let ((memory (or memory (partial-recall--reality)))
(tab (partial-recall--find-tab-from-memory memory frame)))
(alist-get 'name tab)))
(defun partial-recall--fix-up-primary-tab ()
"Fix up the primary tab."
(if-let* ((mode tab-bar-mode)
(tabs (funcall tab-bar-tabs-function))
(original (nth 0 tabs)))
(unless (partial-recall--key original)
(partial-recall--on-create original))
(partial-recall-warn "Might have failed to set up original tab")))
(defun partial-recall--queue-tab-fix-up (&rest _r)
"Queue a fix-up of the original tab."
(run-at-time 1.0 nil #'partial-recall--fix-up-primary-tab))
;;;; Handlers
(defun partial-recall--schedule-buffer (buffer)
"Schedule handling BUFFER."
(and-let* ((buffer (get-buffer buffer))
((partial-recall--buffer-new-p buffer))
((partial-recall--meaningful-buffer-p buffer)))
(partial-recall--void-schedule-timer)
(partial-recall-debug "Scheduling buffer `%s'" buffer)
(setq partial-recall--schedule-timer
(run-at-time
partial-recall-handle-delay
nil
#'partial-recall--handle-buffer buffer))))
(defun partial-recall--void-schedule-timer ()
"Void the current timer."
(when partial-recall--schedule-timer
(unless (timer--triggered partial-recall--schedule-timer)
(partial-recall-debug "Canceling previous timer `%s'" partial-recall--schedule-timer)
(cancel-timer partial-recall--schedule-timer))
(setq partial-recall--schedule-timer nil)))
(defun partial-recall--handle-buffer (buffer)
"Handle BUFFER.
This will remember new buffers and maybe reclaim mapped buffers.
If in between scheduling and handling the buffer it can no longer
be found, it will be ignored."
(partial-recall--void-schedule-timer)
(if (partial-recall--buffer-visible-p buffer)
(progn
(partial-recall-log "Handling buffer `%s' as it remains visible" buffer)
(if (partial-recall--buffer-mapped-p buffer)
(partial-recall--recollect buffer)
(partial-recall--remember buffer))
(setq partial-recall--last-handled buffer))
(partial-recall-log "Not handling `%s' as it is no longer visible" buffer)))
;;;;; Reactions
(defun partial-recall--before-switch-to-buffer (buffer &optional norecord &rest _)
"Maybe switch memories before scheduling BUFFER.
Don't do anything if NORECORD is t."
(unless norecord
(partial-recall--maybe-switch-memory (get-buffer buffer))))
(defun partial-recall--after-switch-to-buffer (buffer &optional norecord &rest _)
"Schedule the BUFFER that was switched to.
Don't do anything if NORECORD is t."
(unless norecord
(partial-recall--schedule-buffer buffer)))
(defun partial-recall--after-pop-to-buffer (buffer &optional _action norecord)
"Schedule the BUFFER that was popped to.
Don't do anything if NORECORD is t."
(unless norecord
(partial-recall--schedule-buffer buffer)))
(defun partial-recall--on-create (tab)
"Equip TAB with a unique hash key."
(let ((key (partial-recall--create-key tab))
(state (cdr tab)))
(setcdr tab (push (cons 'pr key) state))
(run-hook-with-args 'partial-recall-after-create-hook (alist-get 'name tab))))
(defun partial-recall--on-close (tab only)
"Remove TAB from table if it is not the ONLY one."
(and-let* (((not only))
(tab-key (partial-recall--key tab))
(table partial-recall--table)
(memory (gethash tab-key table))
(moments (partial-recall-memory--moments memory)))
(dolist (it (ring-elements moments))
(partial-recall--clean-up-buffer (partial-recall-moment--buffer it))
(partial-recall--suppress it memory))
(remhash tab-key table)))
(defun partial-recall--on-frame-delete (frame)
"Clear hashes associated with FRAME."
(let ((tabs (funcall tab-bar-tabs-function frame)))
(dolist (tab tabs)
(partial-recall--on-close tab nil))))
(defun partial-recall--on-minibuffer-setup ()
"Maybe record the buffer before entry."
(and-let* (((memq this-command partial-recall-record-triggers))
(buffer (window-buffer (minibuffer-selected-window))))
(setq partial-recall--before-minibuffer buffer)))
(defun partial-recall--on-minibuffer-exit ()
"Delete the recorded buffer."
(setq partial-recall--before-minibuffer nil))
(defun partial-recall--after-register-val-jump-to (value &rest _args)
"Maybe switch memory after jumping to VALUE."
(cond
((window-configuration-p (car-safe value))
(when-let* ((marker (cadr value))
(buffer (marker-buffer marker)))
(partial-recall--maybe-switch-memory buffer t)))))
(defun partial-recall--after-winner (&rest _)
"Maybe switch memory after `winner-undo' or `winner-redo'."
(when-let* ((foreign (seq-find
(lambda (it)
(not (partial-recall--buffer-in-memory-p (window-buffer it))))
(window-list))))
(partial-recall--maybe-switch-memory (window-buffer foreign) t)))
(defun partial-recall--after-tab-bar-switch (name)
"Call hook with NAME."
(run-hook-with-args 'partial-recall-after-reality-change-hook name))
(defun partial-recall--before-undo-close-tab ()
"Record the tab that will be undone and record its key."
(and-let* ((predicate (lambda (it) (frame-live-p (alist-get 'frame it))))
(closed (seq-find predicate tab-bar-closed-tabs))
(tab (alist-get 'tab closed)))
(setq partial-recall--restored-tab (alist-get 'pr tab))))
(defun partial-recall--after-undo-close-tab ()
"Lift subconscious buffers."
(and-let* ((key partial-recall--restored-tab)
(buffers (partial-recall--suppressed key))
(predicate (lambda (a b)
(not (time-less-p (buffer-local-value 'buffer-display-time a)
(buffer-local-value 'buffer-display-time b)))))
(sorted (sort buffers predicate))
(truncated (seq-subseq sorted 0
(min (length sorted)
partial-recall-memory-size)))
(memory (partial-recall--memory-by-key key))
(moments (partial-recall-memory--moments memory)))
(dolist (buffer truncated)
(partial-recall--clear-remnant buffer)
(partial-recall--clear-disturbed buffer)
(ring-insert moments (partial-recall-moment--create buffer)))))
(defun partial-recall--before-view-buffer (buffer &rest _)
"Record BUFFER as about to be viewed."
(setq partial-recall--to-be-viewed buffer))
;;;; Verbs
;;
;; This section holds the core of the package, namely the verbs that
;; change state.
(defun partial-recall--remember (buffer)
"Remember BUFFER as belonging to the current reality.
This will either create a new moment for the buffer or reclaim
one from the subconscious.
If BUFFER is already owned by the current reality, this is a
no-op."
(and-let* ((memory (partial-recall--reality))
(ring (partial-recall-memory--moments memory))
((not (partial-recall-memory--owns-buffer-p memory buffer))))
(partial-recall--probe-memory memory)
(partial-recall--clear-remnant buffer)
(partial-recall--clear-disturbed buffer)
(let ((moment (partial-recall-moment--create buffer)))
(partial-recall--ring-insert ring moment))))
(defvar-local partial-recall--disturbed nil
"If non-nil don't include during `partial-recall--remember-some'.")
(defun partial-recall--clear-disturbed (&optional buffer)
"Clear disturbed marker from BUFFER."
(with-current-buffer buffer
(setq partial-recall--disturbed nil)))
(defun partial-recall--remember-some (&optional include-all)
"Remember some buffers that were lost.
This will prompt the user for each lost buffer exactly once unless
optional INCLUDE-ALL is t."
(let ((buffers (partial-recall-memory--removed-buffers)))
(dolist (buffer buffers)
(if (and (or include-all
(not (buffer-local-value 'partial-recall--disturbed buffer)))
(yes-or-no-p (format "Remember lost buffer `%s'?" buffer)))
(partial-recall--remember buffer)
(with-current-buffer buffer
(setq partial-recall--disturbed t))))))
(defun partial-recall--reinforce (buffer)
"Reinforce BUFFER in reality.
This will re-insert the buffer's moment in the current reality.
If this buffer isn't already part of the current reality, this is
a no-op."
(and-let* ((reality (partial-recall--reality))
(moment (partial-recall--find-owning-moment buffer reality)))
(partial-recall--reinsert moment reality "reinforcement")))
(defun partial-recall--reinsert (moment memory &optional reason)
"Reinsert MOMENT into MEMORY.
This removes, inserts and extends the memory. The moment is
intensified.
If the moment doesn't belong to memory, this is a no-op.
Optionally, give a REASON why moment was re-inserted."
(and-let* ((ring (partial-recall-memory--moments memory))
((ring-member ring moment))
(buffer (partial-recall-moment--buffer moment))
(reason (or reason "unknown reason")))
(partial-recall-debug "Re-inserting `%s' in `%s' (%s)" moment memory reason)
(partial-recall-moment--adjust moment 'reinsert)
(ring-remove+insert+extend ring moment t)))
(defun partial-recall--reclaim (buffer &optional force)
"Reclaim BUFFER for the current reality.
This will move the buffer's moment from another memory provided
that it is neither permanent nor short-term. This works across
frames.
If FORCE is t it will be reclaimed either way.
If the buffer already belongs to the current reality, this is a
no-op."
(if-let* ((reality (partial-recall--reality))
(owner (partial-recall--find-owning-memory buffer))
((not (eq reality owner)))
(moment (partial-recall--find-owning-moment buffer owner))
((or force
(and
(not (partial-recall-moment--permanence moment))
(partial-recall--intermediate-term-p moment)))))
(progn
(partial-recall--in-other-frame owner
(partial-recall--clean-up-window buffer))
(partial-recall--swap owner reality moment))