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)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website with WordPress.com
Get started
%d bloggers like this: