Auto change emacs theme

[2020-09-22 Tue] on Yann Esposito's blog
A small snippet to automatically change theme in emacs.

One thing I kind of like is the ability to change emacs theme depending of the hour. There are two possibilities. One would be to sync with the sun using the current location. There is an emacs package for that. It's called theme-changer which at the time of writing those lines is asking for a new maintainer. This theme changer is very elegant because like macOS use the location to determine if it is day or night. But I wanted to have more themes from morning to night:

  1. early morning: deep yellow (gruvbox-light),
  2. morning: light yellow (solarized-light),
  3. day: grey/blueish during the day (nord-light),
  4. evening: deep yellow again (gruvbox-light)
  5. night: dark theme (oceanic-next)
  6. sleep time: neon-like (laserwave)

And also, I wanted that to follow my working hours and not really the sun. I might change my mind and use the code of theme-changer to follow the curve of the sun. But for now, just using straight hours should be good enough. So here is my piece of code I added to my doom-emacs config.el:

(defun y/auto-update-theme ()
  "depending on time use different theme"
  ;; very early => gruvbox-light, solarized-light, nord-light
  (let* ((hour (nth 2 (decode-time (current-time))))
         (theme (cond ((<= 7 hour 8)   'doom-gruvbox-light)
                      ((= 9 hour)      'doom-solarized-light)
                      ((<= 10 hour 16) 'doom-nord-light)
                      ((<= 17 hour 18) 'doom-gruvbox-light)
                      ((<= 19 hour 22) 'doom-oceanic-next)
                      (t               'doom-laserwave))))
    (when (not (equal doom-theme theme))
      (setq doom-theme theme)
      (load-theme doom-theme t))
    ;; run that function again next hour
    (run-at-time (format "%02d:%02d" (+ hour 1) 0) nil 'y/auto-update-theme)))


I'm still playing with it. So there still might be a bug. Use at your own risk. Happy hacking to all of you.