在 Emacs 随机切换主题
文章介绍了 Emacs 中 modus-themes 和 ef-themes 主题的特点,并分享了通过 Elisp 代码实现随机定时切换主题的方法。 2025-5-21 00:0:0 Author: taxodium.ink(查看原文) 阅读量:11 收藏

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)

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

  • 定义一个变量,存储所有我需要随机的主题
  • 定义一个函数,随机从变量中获取一个主题然后应用
    • 重置当前的主题设置
    • 随机获取一个主题
    • 加载获取的主题
  • 设置一个定时器,每隔一定时间调用函数切换主题

用了一阵子,它很好地满足了我的新鲜感,如果你感兴趣,不妨试试 へ(゜∇、°)へ

Date: 2025-05-21 Wed 00:00

Last Modified: 2025-05-21 Wed 18:25

License: CC BY-NC 4.0


文章来源: https://taxodium.ink//emacs-random-theme.html
如有侵权请联系:admin#unsafe.sh