Emacs根据时间更换Theme(Emacs27版本)
程序员文章站
2024-03-18 12:48:10
...
Emacs根据时间更换Theme(Emacs27版本)
环境
系统 版本 : 4.17.12-arch1-1-ARCH
Emacs版本:27.0.50
起因
看了陈斌的一年成为emacs高手后,对于Emacs配置决定不自己造*,于是clone了一份purcell的Emacs配置。发现他的默认Theme是sanityinc-tomorrow-bright这款暗色主题,白天的时候用的不大好。想起Emacs-China论坛曾经有一个根据时间切换Theme的配置,搜索了一番,没有找到,于是决定自己写(chao)一份配置。
过程
在Google上搜到了这样的问题Emacs Auto Load Color Theme by Time,copy了第一个回答的代码。
;; <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))) ;;closes (setq hour...
(if (member hour (number-sequence 6 17))
(setq now '(color-theme-solarized-light))
(setq now '(color-theme-solarized-dark))) ;; end of (if ...
(if (eq now current-theme)
nil
(setq current-theme now)
(eval now) ) ) ;; end of (defun ...
(run-with-timer 0 3600 synchronize-theme)
嗯,重启Emacs后发现报错,发现是Emacs版本导致原有的变量失效,一番debug后解决,代码如下:
(defun synchronize-theme ()
(setq hour
(string-to-number
(substring (current-time-string) 11 13))) ;;closes (setq hour...
(if (member hour (number-sequence 6 17))
(setq now '(sanityinc-tomorrow-day))
(setq now '(sanityinc-tomorrow-bright))) ;; end of (if ...
(if (eq now custom-enabled-themes)
nil
(setq custom-enabled-themes now)
(reapply-themes) ) ) ;; end of (defun ...
(run-with-timer 0 3600 'synchronize-theme)
这样设置的是6点到17点light系Zheme,其余时间dark系Theme。