Skip to content

Commit

Permalink
Add predicate that determines if the session is saved automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescherti committed Aug 17, 2024
1 parent ca0dd83 commit ed07034
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,17 @@ To set up a minimal environment when easysession creates a new session, you can
(add-hook 'easysession-new-session-hook #'my-empty-easysession)
```

### How can I configure easysession-save-mode to automatically save only the "main" session and let me manually save others?

To set up `easysession-save-mode` to automatically save only the "main" session and allow you to manually save other sessions, add the following code to your configuration:
```emacs-lisp
(defun my-easysession-only-main-saved ()
"Only save the main session."
(when (string= "main" (easysession-get-current-session-name))
t))
(setq easysession-save-mode-predicate 'my-easysession-only-main-saved)
```

### How does the author use easysession?

The author uses `easysession.el` by setting up each session to represent a distinct project or a specific "view" on a particular project, including various tabs (built-in tab-bar), window splits, dired buffers, and file editing buffers. This organization allows for the creation of dedicated environments for different tasks or aspects of a project, such as development, debugging, specific issue, and documentation. The author switches between projects and views of the same projects multiple times a day, and `easysession.el` helps significantly by allowing quick transitions between them.
Expand Down
35 changes: 33 additions & 2 deletions easysession.el
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,23 @@ criteria."
:type 'function
:group 'easysession)

(defun easysession--default-auto-save-predicate ()
"Default predicate function for `easysession-save-predicate`.
This function always returns non-nil, ensuring the session is saved."
t)

(defcustom
easysession-save-mode-predicate #'easysession--default-auto-save-predicate
"Predicate that determines if the session is saved automatically.
This function is called with no arguments and should return non-nil if
`easysession-save-mode' should save the session automatically. The default
predicate always returns non-nil, ensuring all sessions are saved
automatically."
:type 'function
:group 'easysession)

(defvar easysession--debug nil)

(defvar easysession--timer nil)

(defvar easysession--overwrite-frameset-filter-alist
Expand Down Expand Up @@ -856,6 +873,20 @@ initialized."
(t (easysession--message "Switched to %ssession: %s"
(if new-session "new " "") session-name)))))

(defun easysession--auto-save ()
"Save the session automatically based on the auto-save predicate.
This function is usually called by `easysession-save-mode'. It evaluates the
`easysession-save-mode-predicate' function, and if the predicate returns
non-nil, the current session is saved."
(interactive)
(if (funcall easysession-save-mode-predicate)
(easysession-save)
(when easysession--debug
(easysession--message
(concat "[DEBUG] Auto-save ignored: `easysession-save-mode-predicate' "
"returned nil."))
nil)))

;;;###autoload
(define-minor-mode easysession-save-mode
"Toggle `easysession-save-mode'."
Expand All @@ -869,8 +900,8 @@ initialized."
(setq easysession--timer
(run-with-timer easysession-save-interval
easysession-save-interval
#'easysession-save)))
(add-hook 'kill-emacs-hook #'easysession-save))
#'easysession--auto-save)))
(add-hook 'kill-emacs-hook #'easysession--auto-save))
(when easysession--timer
(cancel-timer easysession--timer)
(setq easysession--timer nil))
Expand Down
17 changes: 16 additions & 1 deletion tests/test-easysession.el
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,22 @@ storing them in respective variables for later use."
(unless (string= (expand-file-name test-easysession--dired-buffer-path)
(expand-file-name default-directory))
(error
"The Dired buffer points to the wrong path"))))
"The Dired buffer points to the wrong path")))

;; Test save-mode predicate
(defun my-easysession-nothing-saved ()
"Nothing is saved."
nil)
(setq easysession-save-mode-predicate 'my-easysession-nothing-saved)
(kill-buffer test-easysession--dired-buffer)
(easysession--auto-save)
(easysession-load)
(setq test-easysession--dired-buffer
(dired-noselect test-easysession--dired-buffer-path))
(when test-easysession--dired-buffer
(error (concat "easysession--auto-save or the "
"easysession-save-mode-predicate does not seem to "
"be working"))))

(defun test-easysession--get-all-names ()
"Test: `easysession--get-all-names'."
Expand Down

0 comments on commit ed07034

Please sign in to comment.