我可以让Emacs自动加载主题吗?或者在自定义时间执行某些命令?我想说的是,当我早上9点在办公室时对M-x load-theme RET solarized-light
说,当我回到家并在晚上8点继续服用emacs时对M-x laod-theme RET solarized-dark
说。
要扩展@Anton Kovalenko的答案,您可以使用当前时间字符串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手册的以下部分:
- 一天中的时间
- 字符串
- 字符串转换
- 空闲计时器
- 包含
- 编号顺序
另一个(非常优雅的)解决方案是主题转换器。
给定位置和昼夜颜色主题,此文件提供了一个更改主题功能,该功能可根据白天还是晚上选择适当的主题。它将在日出和日落时继续改变主题。安装:
设置位置:
(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进行安装。
您可以使用这段代码来执行您想要的操作。
(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)))
您可以从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
(不要切换主题,即使它正在重新加载当前主题)。
找到了适用于doom emacs的简单代码。将其放入配置文件:
(load-theme 'solarized-light t t) ;;load light theme
(run-at-time "09:00" (* 60 60 24) (lambda () (enable-theme 'solarized-light)))
(load-theme 'solarized-dark t t) ;;load dark theme
(run-at-time "20:00" (* 60 60 24) (lambda () (enable-theme 'solarized-dark)))
用你的选择替换明暗主题。时间也可以从上午9点/晚上8点以24小时格式更改。
来源和信用:https://parasurv.neocities.org/emacs/change-emacs-theme-depending-on-time.html
此实现根据您提供的纬度和经度的日出和日落时间来更改主题。唯一的依赖项是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))