方便的切换emacs主题


作为一个主题控,经常会切换主题,之前切换主题的方式是这样的,*M-x*,`load-theme`,选中,但是个人认为不够便捷,正好前几天发现了hydra这个插件,也想实践一下

获取主题列表

(setq maple-cycle-themes (mapcar 'symbol-name (custom-available-themes)))

获取当前主题索引

(cl-position (car (mapcar 'symbol-name custom-enabled-themes)) maple-cycle-themes :test 'equal)

获取下一个主题

(setq maple-current-theme-index
      (+ 1 maple-current-theme-index))
(setq maple-current-theme (nth maple-current-theme-index maple-cycle-themes))

加载主题

(load-theme (intern maple-current-theme) t)

最后得到这样的函数

(defun maple/cycle-theme (num)
  (interactive)
  (setq maple-current-theme-index
        (+ num
           (cl-position
            (car (mapcar 'symbol-name custom-enabled-themes)) maple-cycle-themes :test 'equal)))
  (when (>= maple-current-theme-index (length maple-cycle-themes))
    (setq maple-current-theme-index 0))
  (setq maple-current-theme (nth maple-current-theme-index maple-cycle-themes))
  (mapc 'disable-theme custom-enabled-themes)
  (let ((progress-reporter
         (make-progress-reporter
          (format "Loading theme %s..." maple-current-theme))))
    (load-theme (intern maple-current-theme) t)
    (progress-reporter-done progress-reporter)))
(defun maple/next-theme()
  (interactive)
  (maple/cycle-theme 1))
(defun maple/previous-theme()
  (interactive)
  (maple/cycle-theme -1))
(defhydra maple/cycle-themes ()
  ("n" maple/next-theme "next theme")
  ("p" maple/previous-theme "prev theme"))

ok,绑定一个快捷键吧