Re: [GNU ELPA] New package: caffeinate-mode
Philip Kaludercic <[email protected]>
| Newsgroups | gmane.emacs.devel |
|---|---|
| Message-ID | <[email protected]> |
Stéphane Marks <[email protected]> writes: > On Tue, Jul 21, 2026 at 10:27 PM Lucas Christian <[email protected]> wrote: > >> Stéphane Marks <[email protected]> writes: >> >> > Does it even need to be a mode or can it just be a package with a few >> > commands, e.g., caffeinate, caffeinate-display, decaffeinate, >> > defcustom for the default display sleep state and interval, and global >> > private variables for the active sleep token and timer? >> >> I think the main advantage of it being a mode is that it leaves a clear >> indicator in the mode line when it is active. >> > > That's pretty easy to do without being a mode. Here is a skeetch of what I had in mind when it came to merging the two minor modes: It also simplifies the code, since you don't need to maintain the invariant that the major modes are mutually exclusive. It is a sub-minor-mode so to speak.
(unnamed)
(text/x-patch, 4.4 KB)
diff --git a/caffeinate.el b/caffeinate.el
index d644bbe..fd74588 100644
--- a/caffeinate.el
+++ b/caffeinate.el
@@ -35,9 +35,6 @@
;; * `caffeinate-mode' prevents system idle sleep but allows the
;; display to sleep.
;;
-;; * `display-caffeinate-mode' prevents system idle sleep and also
-;; keeps the display active.
-;;
;; The modes are mutually exclusive: enabling one automatically
;; disables the other. Disabling either mode releases the active
;; power assertion, allowing the system to resume its normal sleep
@@ -98,9 +95,7 @@ non-nil, only prevent system idle sleep."
"Disable caffeinate after timeout."
(setq caffeinate--timer nil
caffeinate--timeout-seconds nil)
- (cond
- ((bound-and-true-p caffeinate-mode) (caffeinate-mode -1))
- ((bound-and-true-p display-caffeinate-mode) (display-caffeinate-mode -1))))
+ (caffeinate-mode -1))
(defun caffeinate-set-timeout (duration)
"Schedule caffeinate to turn itself off after DURATION.
@@ -113,9 +108,8 @@ response cancels the pending timeout."
(interactive
(list (read-string
"Caffeinate timeout (e.g. 30 min, 2 hours, blank to cancel): ")))
- (unless (or (bound-and-true-p caffeinate-mode)
- (bound-and-true-p display-caffeinate-mode))
- (user-error "Either caffeinate-mode or display-caffeinate-mode must be active to set a timeout"))
+ (unless (bound-and-true-p caffeinate-mode)
+ (user-error "caffeinate-mode must be active to set a timeout"))
(let ((secs (cond
((null duration) nil)
((numberp duration) duration)
@@ -141,16 +135,14 @@ response cancels the pending timeout."
(defun caffeinate-toggle-display ()
"Toggle between `caffeinate-mode' and `display-caffeinate-mode'."
(interactive)
- (if (bound-and-true-p display-caffeinate-mode)
- (caffeinate-mode 1)
- (display-caffeinate-mode 1)))
+ (unless (bound-and-true-p caffeinate-mode)
+ (user-error "Caffeinated-mode not enabled"))
+ (setq caffeinate-mode (if (eq caffeinate-mode 'display) t 'display)))
(defun caffeinate-turn-off ()
"Disable the active caffeinate mode."
(interactive)
- (cond
- ((bound-and-true-p display-caffeinate-mode) (display-caffeinate-mode -1))
- ((bound-and-true-p caffeinate-mode) (caffeinate-mode -1))))
+ (caffeinate-mode -1))
(defvar caffeinate-mode-map
(let ((map (make-sparse-keymap)))
@@ -159,7 +151,7 @@ response cancels the pending timeout."
'("Caffeinate"
["Keep display awake" caffeinate-toggle-display
:style toggle
- :selected (bound-and-true-p display-caffeinate-mode)
+ :selected (eq (bound-and-true-p caffeinate-mode) 'display)
:help "Prevent the display from going to sleep"]
"--"
("Timeout"
@@ -190,19 +182,19 @@ response cancels the pending timeout."
"Keymap for caffeinate modes.")
(defvar display-caffeinate-mode-map caffeinate-mode-map
- "Keymap for `display-caffeinate-mode'; shared with `caffeinate-mode'.")
+ "Keymap for `caffeinate-mode'.")
;;;###autoload
(define-minor-mode caffeinate-mode
- "Prevent the system from going to sleep."
+ "Prevent the system from going to sleep.
+By setting this user option to `display' yo"
:global t
- :group 'caffeinate
- :keymap caffeinate-mode-map
- :lighter " Caffeinate"
+ :lighter (:eval (pcase caffeinate-mode
+ ('nil nil)
+ ('display " Caffeinate[Display]")
+ (_ " Caffeinate")))
(cond
(caffeinate-mode
- (when (bound-and-true-p display-caffeinate-mode)
- (display-caffeinate-mode -1))
(condition-case err
(caffeinate--acquire t)
(error
@@ -210,29 +202,7 @@ response cancels the pending timeout."
(signal (car err) (cdr err)))))
(t
(caffeinate--release)
- (unless (bound-and-true-p display-caffeinate-mode)
- (caffeinate--cancel-timer)))))
-
-;;;###autoload
-(define-minor-mode display-caffeinate-mode
- "Prevent the display from going to sleep."
- :global t
- :group 'caffeinate
- :keymap display-caffeinate-mode-map
- :lighter " Caffeinate[Display]"
- (cond
- (display-caffeinate-mode
- (when (bound-and-true-p caffeinate-mode)
- (caffeinate-mode -1))
- (condition-case err
- (caffeinate--acquire nil)
- (error
- (setq display-caffeinate-mode nil)
- (signal (car err) (cdr err)))))
- (t
- (caffeinate--release)
- (unless caffeinate-mode
- (caffeinate--cancel-timer)))))
+ (caffeinate--cancel-timer))))
(provide 'caffeinate)