根据时间切换 emacs theme

赞!多谢,我再学习一下手册。

整理了一下,加上了随机选择主题

;; 亮色主题和暗色主题
(setq day-theme-list '( modus-operandi ef-day ef-spring ef-summer))
(setq dark-theme-list '( modus-vivendi ef-night ef-autumn ef-winter))

;; 随机选取主题
(defun my-random-element (my-list)
  "Return a random element from MY-LIST."
  (let ((my-length (length my-list))
        (my-random-index (random (length my-list))))
    (nth my-random-index my-list)))

;; 根据时间选择亮/暗主题
(defun synchronize-theme ()
  (setq hour
	(string-to-number
	 (substring (current-time-string) 11 13)))
  (if (member hour (number-sequence 6 18))
      (progn
	(setq now (my-random-element day-theme-list))
	(setq lst (my-random-element dark-theme-list))
	)
    (setq now (my-random-element dark-theme-list))
    (setq lst (my-random-element day-theme-list))
    )
  (mapc 'disable-theme custom-enabled-themes)
  (load-theme now t))
(synchronize-theme) ;; 启动时立即执行一次
;; 每小时执行一次
(let* ((current-minutes
	(string-to-number (substring (current-time-string) 14 16)))
       (current-seconds
	(string-to-number (substring (current-time-string) 17 20)))
       (remain-seconds
	;; remaining seconds = 3600 - 60 * min - sec
	(- 3600 (* 60 current-minutes) current-seconds))
       )
  (run-with-timer remain-seconds 3600 'synchronize-theme))

2 个赞

这相当于用手电筒可以控制emacs的颜色主题。

我是想在白天启用浅色主题,晚上启用深色主题。在网上看到有简单的设置方法,与君共享。

按照上述链接,我的代码如下:

(load-theme 'tango t t)     ;;加载浅色主题,但不启用。
(load-theme 'zerodark t t)	;;加载深色主题,但不启用。
;;早上5点启用浅色主题, 每24小时重复一次。
(run-at-time "05:00" (* 60 60 24) (lambda () (enable-theme 'tango)))
;;晚上7点启用深色主题, 每24小时重复一次。
(run-at-time "19:00" (* 60 60 24) (lambda () (enable-theme 'zerodark)))

若需进一步了解函数参数含义,可以使用C-h f查看。

2 个赞

为了避免 enable-theme 出现混乱,考虑在enable-theme之前,先做一下disable-theme为好。

;;; Theme
(use-package modus-themes)
(use-package standard-themes)
(use-package ef-themes)

;; Switch Themes automatically according to sunrise
(use-package circadian
  :ensure nil
  :demand t
  :load-path "~/.emacs.d/site-lisp/circadian"
  :config
  (setq calendar-latitude 40.0)
  (setq calendar-longitude 116.4)
  (setq circadian-themes `((:sunrise . ,(nth (random 7) '(modus-operandi
                                                          modus-operandi-tinted
                                                          standard-light
                                                          ef-elea-light
                                                          ef-maris-light
                                                          ef-deuteranopia-light
                                                          ef-cyprus)))
                           (:sunset  . ,(nth (random 7) '(modus-vivendi
                                                          modus-vivendi-tinted
                                                          standard-dark
                                                          ef-elea-dark
                                                          ef-maris-dark
                                                          ef-deuteranopia-dark
                                                          ef-bio)))))
  (circadian-setup))

也可以用 darkman