2013-02-07 104 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