Skip to content

Commit

Permalink
FIX: solve recursive timer calls with timer variable + cancel-timer
Browse files Browse the repository at this point in the history
  • Loading branch information
guidoschmidt committed Apr 25, 2024
1 parent 33e621a commit 9927f4e
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions circadian.el
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
(require 'cl-lib)
(require 'solar)

(defcustom circadian-next-timer nil
"Timer to execute on next theme switch."
:type 'timer
:group 'circadian)

(defcustom circadian-before-load-theme-hook nil
"Functions to run before the theme is changed."
:type 'hook
Expand All @@ -65,16 +70,24 @@
;; Only load the argument theme, when `custom-enabled-themes'
;; does not contain it.
(mapc #'disable-theme custom-enabled-themes)

(if (not (equal nil circadian-next-timer))
(progn
(cancel-timer circadian-next-timer)
(setq circadian-next-timer nil)))

(condition-case nil
(progn
(run-hook-with-args 'circadian-before-load-theme-hook theme)

(if (not (equal (list theme) custom-enabled-themes))
(progn
(load-theme theme t)
(let ((time (circadian-now-time)))
(message "[circadian.el] → Enabled %s theme @ %s"
theme
(format-time-string "%H:%M:%S" time)))))

(let* ((themes (circadian-themes-parse))
(now (circadian-now-time))
(past-themes (circadian-filter-inactivate-themes themes now))
Expand All @@ -86,8 +99,11 @@
(next-time (circadian--encode-time
(cl-first (cl-first next-entry))
(cl-second (cl-first next-entry)))))
(run-at-time next-time 1 #'circadian-activate-and-schedule)
(message (concat "[circadian.el] → Next run @ " (format-time-string "%H:%M:%S" next-time))))
(if (equal nil circadian-next-timer)
(progn
(setq circadian-next-timer (run-at-time next-time nil #'circadian-activate-current))
(message (concat "[circadian.el] → Next run @ " (format-time-string "%H:%M:%S" next-time))))))

(run-hook-with-args 'circadian-after-load-theme-hook theme))
(error "[circadian.el/ERROR] → Problem loading theme %s" theme)))

Expand Down Expand Up @@ -146,7 +162,8 @@ set and and sort the final list by time."
(theme (if (listp theme-or-theme-list)
(nth (random (length (cl-second theme-or-theme-list))) (cl-second theme-or-theme-list))
theme-or-theme-list)))
(circadian-enable-theme theme)))
(circadian-enable-theme theme)
))


(defun circadian-activate-and-schedule ()
Expand Down

0 comments on commit 9927f4e

Please sign in to comment.