Emacs按时间自动加载颜色主题

liu*_*hao 9 emacs elisp

我可以让Emacs自动加载主题吗?或者在定制时间做某些命令?说我想要的是M-x load-theme RET solarized-light当我在上午9点在办公室时以及M-x laod-theme RET solarized-dark当我回到家中并在晚上8点继续使用emacs时.

Ale*_*sky 11

另一个(非常优雅)的解决方案是主题转换器.

给定位置和日/夜颜色主题,此文件提供更改主题功能,根据是白天还是晚上选择适当的主题.它将继续改变日出和日落时的主题.安装:

设置位置:

(setq calendar-location-name "Dallas, TX") 
(setq calendar-latitude 32.85)
(setq calendar-longitude -96.85)
Run Code Online (Sandbox Code Playgroud)

指定日夜主题:

(require 'theme-changer)
(change-theme 'tango 'tango-dark)
Run Code Online (Sandbox Code Playgroud)

该项目托管在Github上,可以通过melpa安装.


Dan*_*Dan 8

要扩展@Anton Kovalenko的答案,您可以使用当前时间字符串 elisp函数获取当前时间并提取当前时间(以小时为单位).

如果要编写完整的实现,可以执行类似(警告,未调试)的操作:

;; <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)
Run Code Online (Sandbox Code Playgroud)

有关所用函数的更多信息,请参阅emacs手册的以下部分:


Tre*_*son 5

您可以使用此代码段来执行您想要的操作.

(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)))))
Run Code Online (Sandbox Code Playgroud)

只需根据install-theme-loading-times需要自定义变量:

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