2013-02-07 41 views
8

我可以让Emacs自动加载主题吗?或在定制时间执行某些命令?说我想要的是M-x load-theme RET solarized-light,当我在上午9:00在办公室,M-x laod-theme RET solarized-dark当我回家,并在晚上8:00继续emacs。Emacs自动加载时间颜色主题

回答

6

要扩展@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))) ;;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手册的以下部分:

+0

很好的例子。我每天使用emacs,但从未尝试学习elisp。刚开始学习并遵循你的例子。有用。谢谢。小提醒:应该是'substring(当前时间字符串)11 13)'?没有括号?也可以在'run-with-timer'中的'synchronize-theme'之前添加'''。 – liuminzhao

+0

@ liuminzhao:你能否澄清需要解决的问题(或者直接修复)。 – Dan

+0

它修复了一些错误后可以使用:(if(eq now current-theme)to(if(now now current-theme) – tangxinfa

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在适当的时候(不转每分钟 主题,甚至如果它重新加载当前主题)。

+0

感谢您的指导。继@Dan代码之后,我想我已经明白了。谢谢。 – liuminzhao

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

跟着@Dan的代码我会通过你的代码学习elisp。谢谢。 – liuminzhao

7

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

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

设置的位置:

(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安装。

+0

非常好的解决方案。谢谢你的提示。 – liuminzhao