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

时间:2013-02-07 20:40:12

标签: emacs elisp

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

5 个答案:

答案 0 :(得分:10)

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

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

设置位置:

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

答案 1 :(得分: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))) ;;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手册的以下部分:

答案 2 :(得分: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)))

答案 3 :(得分: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(不要切换 主题每一分钟,即使它正在重新加载当前主题。)

答案 4 :(得分:0)

此实现会根据您提供的纬度和经度的日出和日落时间更改主题。唯一的依赖项是随 Emacs (IIRC) 发布的 solar.el

(我认为这里的代码可能会更短。)

;; theme changing at sunrises and sunsets according to lat and long
(require 'solar)

(defun today-date-integer (offset)
  "Returns today's date in a list of integers, i.e. month, date, and year, in system time."
  (let* ((date (mapcar
   (lambda (pattern)
     (string-to-number (format-time-string pattern)))
   '("%m" "%d" "%Y"))))
    (setcar
   (nthcdr
    1
    date)
   (+ offset (nth 1 date)))
    date))

(defun current-time-decimal ()
  (let* ((current-min-fraction (/ (string-to-number (format-time-string "%M")) 60.0))
    (current-hour (string-to-number (format-time-string "%H"))))
    (+ current-hour current-min-fraction)))

(defun next-alarm-time (sunrise-time sunset-time)
  (let* ((current-time (current-time-decimal)))
   (cond ((< current-time sunrise-time)
       (- sunrise-time current-time))
    ((and (>= current-time sunrise-time)
          (< current-time sunset-time))
      (- sunset-time current-time))
    ((>= current-time sunset-time)
     (let ((tomorrow-sunrise-time (car (car (solar-sunrise-sunset (today-date-integer 1))))))
       (- (+ 24 tomorrow-sunrise-time) current-time))))))

(defun to-seconds (hour) (* hour 60 60))

(defun change-theme (light-theme dark-theme coor)
  (let* ((_ (setq calendar-latitude (car coor)))
    ( _ (setq calendar-longitude (nth 1 coor)))
    (today-date (today-date-integer 0))
    (sunrise-sunset-list (solar-sunrise-sunset today-date))
    (sunrise-time (car (car sunrise-sunset-list)))
    (sunset-time (car (nth 1 sunrise-sunset-list)))
    (current-time (current-time-decimal))
    (current-theme (if (or (< current-time sunrise-time) (> current-time sunset-time))
             dark-theme
             light-theme))
    (next-alarm-t (next-alarm-time sunrise-time sunset-time)))
    (cancel-function-timers 'change-theme)
    (load-theme current-theme t)
    (run-at-time
     (to-seconds next-alarm-t) nil 'change-theme light-theme dark-theme coor)))

(change-theme 'solarized-gruvbox-light 'solarized-gruvbox-dark '(47.6062 -122.3321))