在 Emacs 随机切换主题

Emacs 里我目前最喜欢的主题是 modus-themesef-themes1,它们都是出自 protesilaos2

modus-themes 相对来说更成熟,覆盖了大部分的包和使用场景。

ef-themes 则提供了更多的色彩选择。

一个主题用久了就会觉得有些厌倦,想有一些新鲜感,就总是会找新主题,换新主题。

平时主要用 consult-theme3 去切换主题,前阵子看了 emacs-更改配色方案,受到文章的启发,我找 LLM 帮我写了一些用于定时切换主题的方法4

主题切换相关 Elisp
(defvar spike-leung/candidate-themes
  '(modus-operandi
    modus-operandi-tinted
    modus-vivendi
    modus-vivendi-tinted
    ef-arbutus
    ef-autumn
    ef-bio
    ef-cherie
    ef-cyprus
    ef-dark
    ef-deuteranopia-dark
    ef-deuteranopia-light
    ef-dream
    ef-duo-dark
    ef-duo-light
    ef-eagle
    ef-elea-dark
    ef-elea-light
    ef-frost
    ef-kassio
    ef-light
    ef-maris-dark
    ef-maris-light
    ;; ef-melissa-dark
    ef-melissa-light
    ef-night
    ef-owl
    ef-reverie
    ef-rosa
    ef-spring
    ef-summer
    ef-symbiosis
    ef-trio-dark
    ef-trio-light
    ef-tritanopia-dark
    ef-tritanopia-light
    ef-winter)
  "A list of themes to randomly cycle through.")

;;; theme related
;; @see: https://emacsredux.com/blog/2025/02/03/clean-unloading-of-emacs-themes/
(defun spike-leung/disable-all-active-themes ()
  "Disable all currently active themes."
  (interactive)
  (dolist (theme custom-enabled-themes)
    (disable-theme theme)))

(defun spike-leung/apply-random-theme ()
  "Disable current themes and apply a random theme from `spike-leung/candidate-themes`."
  (interactive)
  (when (boundp 'spike-leung/candidate-themes)
    (spike-leung/disable-all-active-themes)
    (let ((theme (nth (random (length spike-leung/candidate-themes)) spike-leung/candidate-themes)))
      (condition-case err
          (progn
            ;; Load theme to ensure its specific settings/customizations are applied
            (load-theme theme :no-confirm)
            ;; Enable the theme (this actually applies it and adds to custom-enabled-themes)
            (enable-theme theme)
            (message "Applied random theme: %s" theme))
        (error (message "Error applying theme %s: %s" theme err))))))

(run-with-timer (* 15 60) (* 15 60) 'spike-leung/apply-random-theme)

这段代码主要做了这几个事:

用了一阵子,它很好地满足了我的新鲜感5,如果你感兴趣,不妨试试~

へ(゜∇、°)へ へ(゜∇、°)へ

脚注:

1

后续可能还会加入 doric-themes,这个主题的颜色比较单一,写作的时候用感觉不错。

2

我很佩服 Prot,他写的文章、手册、教程都很不错,从他那学到了不少。他的 denote 也是我用的最多的一个笔记记录工具。

3

consult 提供的一个方法。

4

源码看这里: init-my-theme.el

5

说起来奇怪,如果是我自己主动切换的主题,可能切换后我马上就想换了。但是随机切换的主题,我反而有种顺其自然的心境,更愿意先用用看。

Author: Spike Leung

Date: 2025-05-21 Wed 00:00

Last Modified: 2025-05-21 Wed 22:42

License: CC BY-NC 4.0