Skip to content

Commit

Permalink
refac(concentration): factor into own module
Browse files Browse the repository at this point in the history
Users now need to enable this behavior by enabling
`partial-recall-concentration-mode`.
  • Loading branch information
Walheimat committed Feb 13, 2024
1 parent 1294475 commit 3182e22
Show file tree
Hide file tree
Showing 8 changed files with 415 additions and 296 deletions.
1 change: 1 addition & 0 deletions .releaserc
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"assets": [
"CHANGELOG.md",
"partial-recall.el",
"partial-recall-concentration.el",
"partial-recall-x.el",
"partial-recall-menu.el",
"Cask",
Expand Down
6 changes: 5 additions & 1 deletion Cask
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@

(package "partial-recall" "0.10.0" "Short-term buffer memory")

(files "partial-recall.el" "partial-recall-menu.el" "partial-recall-x.el")
(files
"partial-recall.el"
"partial-recall-concentration.el"
"partial-recall-menu.el"
"partial-recall-x.el")
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ PACKAGE_SUFFIX=tar
CURRENT_PACKAGE_VERSION=0.10.0
UPDATE_VERSION_FILES=Cask \
partial-recall.el \
partial-recall-concentration.el \
partial-recall-menu.el \
partial-recall-x.el \
Makefile

PACIFY_DEPS=partial-recall.el \
partial-recall-concentration.el \
partial-recall-menu.el \
partial-recall-x.el

Expand Down
6 changes: 5 additions & 1 deletion README.org
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ now this has no further consequences.
Switching tabs has concentration shift to the new tab's current buffer
and resets the cycle.

To enable this behavior, enable =partial-recall-concentration-mode=.

*** Meaningful buffers and buffer traits

=partial-recall= only "remembers" buffers that are "meaningful". This
Expand Down Expand Up @@ -179,7 +181,6 @@ taken into account.
(partial-recall-intermediate-term 1200) ;; The age (in seconds) that, when exceeded, will allow reclaiming.
(partial-recall-repress t) ;; Whether forgotten buffers should (eventually) be killed.
(partial-recall-auto-implant 20) ;; Amount of focus needed before auto-implanting.
(partial-recall-concentration-cycle 60) ;; Amount of seconds between checking whether a buffer remained visible.
(partial-recall-auto-switch t) ;; Whether to switch to a moment's tab automatically.
(partial-recall-lighter-prefix "pr") ;; What you will in the mode-line.
(partial-recall-record-triggers '(consult-buffer)) ;; Commands that might obscure buffers before they are handled.
Expand All @@ -193,8 +194,11 @@ taken into account.
(partial-recall-intensities '((swap . 1) (reinsert . 2) (concentrate . 2))) ;; Focus gained from actions.
(partial-recall-history-size 100) ;; Size of moment and memory history.

(partial-recall-concentration-cycle 60) ;; Amount of seconds between checking whether a buffer remained visible.

:config
(partial-recall-mode)
(partial-recall-concentration-mode)

;; If you use `consult', you can add the provided buffer source.
(with-eval-after-load 'consult
Expand Down
189 changes: 189 additions & 0 deletions partial-recall-concentration.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
;;; partial-recall-concentration.el --- Focus through concentration -*- lexical-binding: t; -*-

;; Author: Krister Schuchardt <krister.schuchardt@gmail.com>
;; Homepage: https://github.com/Walheimat/partial-recall
;; Version: 0.10.0
;; Package-Requires: ((emacs "29.1"))
;; Keywords: frames files convenience

;;; Commentary:

;; This is an optional behavior that increases a moment's focus when
;; it remains visible over long periods of time.

;;; Code:

(require 'partial-recall)

;;; Customization

(defcustom partial-recall-concentration-cycle 60
"Number of seconds a cycle of concentration takes.
If a buffer remains visible from when the cycle began until it
ends, its focus is increased. Can be set to nil to disable
concentration."
:type '(choice (integer :tag "Number of seconds")
(const :tag "Don't use" nil))
:group 'partial-recall)

;;; Variables

(defvar partial-recall-concentration--timer nil)
(defvar partial-recall-concentration--deferred nil)

;;; Errors

(define-error 'prcon-not-owned "Buffer is not owned" 'partial-recall-errors)
(define-error 'prcon-changed "Buffer has changed" 'partial-recall-errors)
(define-error 'prcon-not-ready "No reality" 'partial-recall-errors)

(defun partial-recall-concentration--concentrate ()
"Concentrate on the current moment.
If the moment has remained the same since the last cycle, its
focus is intensified, otherwise concentration breaks and the now
current moment is focused."
(condition-case err
(progn
(partial-recall-concentration--hold)

;; Move faint to last focus if it exists.
(when partial-recall--faint-focus
(setq partial-recall--last-focus partial-recall--faint-focus
partial-recall--faint-focus nil)
(partial-recall-concentration--renew))

(partial-recall-moment--intensify partial-recall--last-focus nil 'concentrate))

((prcon-not-ready prcon-not-owned)
(partial-recall-concentration--defer))
(prcon-changed
(let ((moment (cdr err)))

(when-let ((lost (or partial-recall--faint-focus partial-recall--last-focus)))
(partial-recall-debug "Concentration on `%s' broke" lost))

(partial-recall-concentration--renew)
(partial-recall-debug "Concentration on `%s' begins" moment)

(setq partial-recall--last-focus moment
partial-recall--faint-focus nil)))))

(defun partial-recall-concentration--hold ()
"Try to hold concentration."
(unless (partial-recall--reality)
(signal 'prcon-not-ready nil))

(let* ((buffer (current-buffer))
(moment (partial-recall--find-owning-moment buffer))
(focus (or partial-recall--last-focus partial-recall--faint-focus)))

(unless (or moment (partial-recall--buffer-in-memory-p buffer))
(signal 'prcon-not-owned buffer))

(unless (and focus
(or (eq moment focus)
(partial-recall--buffer-visible-p
(partial-recall-moment--buffer focus))))

(signal 'prcon-changed moment))

(partial-recall-debug "Concentration held on `%s'" focus)))

(defun partial-recall-concentration--defer ()
"Defer concentration.
This restarts the cycle with a much shorter repeat time until
concentration on a moment can begin. The last focus is retained
as a faint focus.
If deferring is already in place, the faint focus is lost as
well."
(if partial-recall-concentration--deferred
(setq partial-recall--faint-focus nil)

(partial-recall-debug "Deferring concentration")

(setq partial-recall-concentration--deferred t
partial-recall--faint-focus partial-recall--last-focus
partial-recall--last-focus nil)

(partial-recall-concentration--start nil (/ partial-recall-concentration-cycle 10))))

(defun partial-recall-concentration--renew ()
"Renew concentration cycle."
(when partial-recall-concentration--deferred
(partial-recall-debug "Aborting deferred concentration")

(setq partial-recall-concentration--deferred nil)

(partial-recall-concentration--start partial-recall-concentration-cycle)))

(defun partial-recall-concentration--shift (name)
"Re-concentrate after switching to NAME.
This cancels and re-runs the timer."
(partial-recall-debug "Shifting concentration towards `%s'" name)

(partial-recall-concentration--start))

(defun partial-recall-concentration--start (&optional secs repeat)
"Start concentrating.
This cancels a previously running timer.
Optionally SECS and REPEAT can be passed which are passed along
to `run-with-timer'. They default to
`partial-recall-handle-delay' and
`partial-recall-concentration-cycle'."
(when partial-recall-concentration--timer
(cancel-timer partial-recall-concentration--timer))

(when-let ((secs (or secs partial-recall-handle-delay))
(repeat (or repeat partial-recall-concentration-cycle)))

(setq partial-recall-concentration--timer (run-with-timer
(1+ secs)
(1+ repeat)
#'partial-recall-concentration--concentrate))))

;;;; Mode

(defun partial-recall-concentration--setup ()
"Setup `partial-recall-concentration-mode'."
(when (partial-recall--reality)
(partial-recall-concentration--concentrate))

(add-hook
'partial-recall-after-create-hook
#'partial-recall-concentration--shift)

(advice-add
'tab-bar-switch-to-tab :after
#'partial-recall-concentration--shift))

(defun partial-recall-concentration--teardown ()
"Tear down `partial-recall-concentration-mode'."
(cancel-timer partial-recall-concentration--timer)

(remove-hook
'partial-recall-after-create-hook
#'partial-recall-concentration--shift)

(advice-remove
'tab-bar-switch-to-tab
#'partial-recall-concentration--shift))

;;;###autoload
(define-minor-mode partial-recall-concentration-mode
"Mode that increases a moment's focus through concentration."
:group 'partial-recall
:global t
(if partial-recall-concentration-mode
(partial-recall-concentration--setup)
(partial-recall-concentration--teardown)))

(provide 'partial-recall-concentration)

;;; partial-recall-concentration.el ends here
Loading

0 comments on commit 3182e22

Please sign in to comment.