13

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

4

5 回答 5

12

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

于 2013-02-07T20:56:35.173 回答
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)

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

于 2014-03-22T19:44:41.163 回答
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)))
于 2013-02-07T21:07:24.247 回答
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在适当的时候调用(不要每分钟切换主题,即使它正在重新加载当前主题)。

于 2013-02-07T20:47:25.957 回答
0

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

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

;; 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))
于 2021-05-01T01:07:28.663 回答