March 29, 2019
emacs

Load light or dark theme based on time of the day

I have to work in a dark room sometimes during the night, so I have the screen brightness set to nearly zero. And it’s a PITA to see what on the screen with a dark color scheme such as tomorrow-night. Time like this, the light scheme is way better, I find that solarized-light is one of the best.

So I wrote a script to automatically select the right color scheme when I open Emacs. If it’s between 20:00 in the evening and 6:00 in the morning, it is the light theme, otherwise, use the dark one:

(defun set-light-theme ()
  "Set the light theme with some customization if needed."
  (interactive)
  (load-theme 'doom-solarized-light t))

(defun set-dark-theme ()
  "Set the dark theme with some customization if needed."
  (interactive)
  (load-theme 'doom-tomorrow-night t))

(let ((current-hour (string-to-number (format-time-string "%H"))))
  (if (or (< current-hour 6) (> current-hour 20)) (set-light-theme) (set-dark-theme)))

If you want your Emacs automatically switch between the color schemes when the time come, you can use run-with-timer function to do the check every hour:

(defun theme-switcher ()
  (let ((current-hour (string-to-number (format-time-string "%H"))))
    (if (or (< current-hour 6) (> current-hour 20)) (set-light-theme) (set-dark-theme))))

;; Run at every 3600 seconds, after 0s delay
(run-with-timer 0 3600 'theme-switcher)
@this-is-a-tag
Waiting
* Work in progress
Done
Flagged
Hey! Have you tried Pomoday?
It's a keyboard-oriented, very easy to use and very powerful web-based task management software.
Powered by a lot of ☕