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

问题描述 投票:9回答:4

我可以让Emacs自动加载主题吗?还是在自定义时间执行某些命令?假设我想在上午9:00到办公室时选择M-x load-theme RET solarized-light,在回到家并在8:00 pm继续使用emacs时选择M-x laod-theme RET solarized-dark

emacs elisp
4个回答
8
投票

要扩展@Anton Kovalenko的答案,您可以使用current-time-string 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)))
    (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)

有关使用的功能的更多信息,请参阅emacs手册的以下部分:


11
投票

另一个(非常优雅的)解决方案是theme-changer

考虑到位置和白天/夜晚的颜色主题,此文件提供了一个更改主题功能,该功能可以根据白天还是晚上选择合适的主题。它将在日出和日落时继续改变主题。要安装:

设置位置:

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

指定昼夜主题:

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

该项目托管在on Github,可以通过melpa安装。


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)))))

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

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

2
投票

您可以使用run-with-timer功能开始:

(run-with-timer SECS REPEAT FUNCTION &rest ARGS)

Perform an action after a delay of SECS seconds.
Repeat the action every REPEAT seconds, if REPEAT is non-nil.
SECS and REPEAT may be integers or floating point numbers.
The action is to call FUNCTION with arguments ARGS.

This function returns a timer object which you can use in `cancel-timer'.

安排要每分钟运行一次的功能,这将检查当前时间,并在适当时致电load-theme(请勿切换主题,即使它正在重新加载当前主题也是如此)。

© www.soinside.com 2019 - 2024. All rights reserved.