一个根据当天时间自动切换主题的代码段

珍爱你的眼睛,白天light主题,晚上dark主题…

它每一个小时检查一次当前时间,然和根据time-themes-table中的配置,自动切换主题,不会与spacemacs中的themes机制冲突

;; Usage
;; item of time-themes-table: ( hours-in-string . theme-name)
;; 6:00 - 17::00 use spacemacs-light, 17:00 - 24:00 use monokai, 24:00 - 6:00 use spacemacs-light
;; you could add more items.
(mp-ui/config-time-themes-table '(("6" . spacemacs-light) ("17" . monokai)))
(mp-ui/open-themes-auto-change)

;; ====================================Themes automatically change =====================================
;;timer for automatically changing themes
(setq mp-ui--interval-timer nil)

;;table is used to save (time themes) pair for automatically changing themes
;;time should be a string. themes should be a variant , not symbos.
(setq mp-ui--time-themes-table nil)

(defun mp-ui/config-time-themes-table (tt)
  "Set time . themes table for time-themes-table."
  (setq mp-ui--time-themes-table
      ;; sort firstly, get-themes-according require a sorted table.
      (sort tt (lambda (x y) (< (string-to-int (car x)) (string-to-int (car y)))))
        )
  )

(defun mp-ui/get-themes-according (hour-string)
  "This function return the theme according to hour-string.
Value of hour-string should be between 1 and 24(including)."
  (catch 'break
    (let (
          (now-time (string-to-int hour-string))
          ;; init current-themes to the themes of final item
          (correct-themes (cdr (car (last mp-ui--time-themes-table))))
          (loop-list mp-ui--time-themes-table)
          )

        ;; loop to set correct themes to correct-themes
        (while loop-list
          (let ((v (car loop-list)))
            (let ((v-time (string-to-int (car v))) (v-themes (cdr v)))
              (if (< now-time v-time)
                (throw 'break correct-themes)  ; t
                (setq correct-themes v-themes) ; nil
                )))
          (setq loop-list (cdr loop-list))
        )
        ;; This is returned for value of hour-string is bigger than or equal to car of final item
        (throw 'break correct-themes) ; t
    ))
)

(defun mp-ui/check-time-and-modify-theme ()
  "This function will get the theme of now according to time-table-themes,
then check whether emacs should to modify theme, if so, modify it."
  (let ((new-theme (mp-ui/get-themes-according (format-time-string "%H"))))
    (unless (eq new-theme spacemacs--cur-theme)
      (spacemacs/load-theme new-theme)
    ))
  )

(defun mp-ui/open-themes-auto-change ()
  "Start to automatically change themes."
  (interactive)
  (mp-ui/check-time-and-modify-theme)
  (setq
   mp-ui--interval-timer (run-at-time 3600 3600 'mp-ui/check-time-and-modify-theme))
  (message "themes auto change open.")
  )

(defun mp-ui/close-themes-auto-change ()
  "Close automatically change themes."
  (interactive)
  (cancel-timer mp-ui--interval-timer)
  (message "themes auto change close.")
  )

:grinning:

6 个赞

这个可以有:grinning:

1 个赞

Y(^_^)Y,感谢提醒timer基础函数

好棒,写成package放melpa上吧

我试了两次,同样的问题:提示 mp-ui/config-time-themes-table 这一函数, symbol's function definition is void,遂不能生效。 各位遇到过吗?

你忘记autoload了吧

楼主原来的代码是先调用函数

(mp-ui/config-time-themes-table '(("6" . spacemacs-light) ("17" . monokai)))

然后下面才是函数的定义

(defun mp-ui/config-time-themes-table (tt)
  "Set time . themes table for time-themes-table."
  (setq mp-ui--time-themes-table
      ;; sort firstly, get-themes-according require a sorted table.
      (sort tt (lambda (x y) (< (string-to-int (car x)) (string-to-int (car y)))))
        )
  )

这样是会出错的。 只需要将定义函数和调用函数的位置调换一下就应该可以解决了。

1 个赞

Error in dotspacemacs/user-config: Symbol’s function definition is void: string-to-int 我的spacemacs里只有stirng-to-number,但用这个替换后还是有错


(string-to-int STRING &optional BASE)

This function is obsolete since 22.1;
use ‘string-to-number’ instead.

Parse STRING as a decimal number and return the number.
Ignore leading spaces and tabs, and all trailing chars.  Return 0 if
STRING cannot be parsed as an integer or floating point number.

If BASE, interpret STRING as a number in that base.  If BASE isn’t
present, base 10 is used.  BASE must be between 2 and 16 (inclusive).
If the base used is not 10, STRING is always parsed as an integer.

文档建议使用string-to-number, string-to-int 也是 string-to-number的别名函数,应该没问题的.报什么错误呢?你的Emacs 版本?