[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: toggle-light-dark-mode

From: Juri Linkov
Subject: Re: toggle-light-dark-mode
Date: Tue, 15 Sep 2020 22:03:20 +0300
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (x86_64-pc-linux-gnu)

>> [Tip, it is a good idea to change the subject!]
>>    I like using a light theme during the day and a dark theme during the
>>    night when I'm in a dark room. I use a function to toggle between those
>>    two different themes. Here is some code based on my personal
>>    configuration:
>>    _____
>>   [ ... ]
>>    _____
>>    Maybe we could use something like this and then add buttons to the menu
>>    and the tool bar? The tool bar could use an icon like the image
>>    attached (the image is just illustrative, it probably has copyright).
>> This looks like a good idea.
>> I think something in the tool-bar is a bit much, I don't think it is
>> the most used option that should that have such a high visibility --
>> rather I think users would like to toggle it once, so the menu-bar
>> would be a better place where the user would also be prompt to save
>> their settings if they quit emacs.
>> Would you like to purpose a patch for this?
> Ok, I tried to improve that code, a patch is attached.

But the theme can be toggled from light to dark using the command
line switch --reverse-video, -r, -rv, or with the frame parameter
'background-mode' that can be either 'dark' or 'light', and the
user selected theme should support both modes.  Then toggling can be

(defvar my-sunset-timer nil)
(defvar my-sunrise-timer nil)

(defun toggle-light-dark-mode (&optional frame)
  "Automatically switch to dark background after sunset
and to light background after sunrise.
(Note that `calendar-latitude' and `calendar-longitude'
should be set before calling the `solar-sunrise-sunset')."
  (require 'solar)
  (if (and (bound-and-true-p calendar-latitude)
           (bound-and-true-p calendar-longitude)
           (bound-and-true-p calendar-time-zone))
      (let* ((l (solar-sunrise-sunset (calendar-current-date)))
             (sunrise-string (apply 'solar-time-string (car l)))
             (sunset-string (apply 'solar-time-string (car (cdr l))))
             (current-time-string (format-time-string "%H:%M")))
        (if (or (string-lessp current-time-string sunrise-string)
                (string-lessp sunset-string current-time-string))
            (toggle-dark-mode frame)
          (toggle-light-mode frame))
        (if (and (boundp 'my-sunset-timer)  (timerp my-sunset-timer))
            (cancel-timer my-sunset-timer))
        (if (and (boundp 'my-sunrise-timer) (timerp my-sunrise-timer))
            (cancel-timer my-sunrise-timer))
        (setq my-sunset-timer  (run-at-time sunset-string  (* 60 60 24)
        (setq my-sunrise-timer (run-at-time sunrise-string (* 60 60 24)

(add-hook 'after-make-frame-functions 'toggle-light-dark-mode)

(defun my-colors-light (&optional frame)
  "Set colors suitable for working in light environments,
i.e. in daylight or under bright electric lamps."
  (setq frame-background-mode 'light)
  (modify-frame-parameters nil (list (cons 'background-mode 'light)))
  ;; The color with minimal eye fatigue in light environments
  ;; is "AntiqueWhite3" (RGB: 205 192 176),
  ;; (set-background-color "AntiqueWhite3")

(defun my-colors-dark (&optional frame)
  "Set colors suitable for working in the darkness without electricity."
  (setq frame-background-mode 'dark)
  (modify-frame-parameters nil (list (cons 'background-mode 'dark))))

reply via email to

[Prev in Thread] Current Thread [Next in Thread]