Emacs Auto Load Color Theme by Time

To expand on @Anton Kovalenko's answer, you can get the current time using the current-time-string elisp function and extracting the current time of day in hours.

If you want to write a full implementation, you could do something like (Warning, not debugged):

;; <Color theme initialization code>
(setq current-theme '(color-theme-solarized-light))

(defun synchronize-theme ()
    (setq hour 
        (string-to-number 
            (substring (current-time-string) 11 13)))
    (if (member hour (number-sequence 6 17))
        (setq now '(color-theme-solarized-light))
        (setq now '(color-theme-solarized-dark))) 
    (if (equal now current-theme)
        nil
        (setq current-theme now)
        (eval now) ) ) ;; end of (defun ...

(run-with-timer 0 3600 synchronize-theme)

For more info on the functions used, see the following sections of the emacs manual:

  • Time of day
  • Strings
  • String Conversions
  • Idle Timers
  • Contains
  • Number Sequence

You can use this snippet of code to do what you want.

(defvar install-theme-loading-times nil
  "An association list of time strings and theme names.
The themes will be loaded at the specified time every day.")
(defvar install-theme-timers nil)
(defun install-theme-loading-at-times ()
  "Set up theme loading according to `install-theme-loading-at-times`"
  (interactive)
  (dolist (timer install-theme-timers)
(cancel-timer timer))
  (setq install-theme-timers nil)
  (dolist (time-theme install-theme-loading-times)
(add-to-list 'install-theme-timers
         (run-at-time (car time-theme) (* 60 60 24) 'load-theme (cdr time-theme)))))

Just customize the variable install-theme-loading-times as desired:

(setq install-theme-loading-times '(("9:00am" . solarized-light)
                ("8:00pm" . solarized-dark)))

Another (very elegant) solution is theme-changer.

Given a location and day/night color themes, this file provides a change-theme function that selects the appropriate theme based on whether it is day or night. It will continue to change themes at sunrise and sunset. To install:

Set the location:

(setq calendar-location-name "Dallas, TX") 
(setq calendar-latitude 32.85)
(setq calendar-longitude -96.85)

Specify the day and night themes:

(require 'theme-changer)
(change-theme 'tango 'tango-dark)

The project is hosted on Github, and can be installed through melpa.

Tags:

Emacs

Elisp