Haga una función para alternar temas


8

¿Cómo podría hacer una función interactiva para alternar entre dos temas de color?

Por lo que he podido encontrar, no hay una variable establecida para qué tema de color se está utilizando actualmente, de hecho, ¿se pueden cargar varias simultáneamente?

Además, para alternar, primero debe hacer disable-themepara el tema cargado actualmente, para no hacer que los temas choquen.

¿Cómo hacer eso sin saber qué tema está cargado actualmente?


2
(car custom-enabled-themes)devuelve el tema habilitado actualmente.
mutbuerger


Solo use el tema de la carga del abogado, si usa un abogado.
InHarmsWay

Respuestas:


4

Lo hice pero cambio 3 temas (Mi propio kosmos, leuven y predeterminado)

Puede consultar https://github.com/habamax/.emacs.d/blob/master/lisp/haba-appearance.el

extracto de ella:

(defvar *haba-theme-dark* 'kosmos)
(defvar *haba-theme-light* 'leuven)
(defvar *haba-current-theme* *haba-theme-dark*)

;; disable other themes before loading new one
(defadvice load-theme (before theme-dont-propagate activate)
  "Disable theme before loading new one."
  (mapcar #'disable-theme custom-enabled-themes))


(defun haba/next-theme (theme)
  (if (eq theme 'default)
      (disable-theme *haba-current-theme*)
    (progn
      (load-theme theme t)))
  (setq *haba-current-theme* theme))

(defun haba/toggle-theme ()
  (interactive)
  (cond ((eq *haba-current-theme* *haba-theme-dark*) (haba/next-theme *haba-theme-light*))
        ((eq *haba-current-theme* *haba-theme-light*) (haba/next-theme 'default))
        ((eq *haba-current-theme* 'default) (haba/next-theme *haba-theme-dark*))))

Luego, une alguna tecla para haba / toggle-theme.

Utilizo emacs en 2 máquinas y entornos diferentes (día, tarde), por lo que existe la posibilidad de guardar / restaurar el tema actual en emacs para salir / cargar. Lo cual es útil :)


5

Escribí algunas funciones para recorrer un grupo de temas.

(setq ivan/themes '(elixir elixir-dark))
(setq ivan/themes-index 0)

(defun ivan/cycle-theme ()
  (interactive)
  (setq ivan/themes-index (% (1+ ivan/themes-index) (length ivan/themes)))
  (ivan/load-indexed-theme))

(defun ivan/load-indexed-theme ()
  (ivan/try-load-theme (nth ivan/themes-index ivan/themes)))

(defun ivan/try-load-theme (theme)
  (if (ignore-errors (load-theme theme :no-confirm))
      (mapcar #'disable-theme (remove theme custom-enabled-themes))
    (message "Unable to find theme file for ‘%s’" theme)))

Llamo ivan/load-indexed-themea mi archivo init para inicializar mi tema.

Ato ivan/cycle-themea Space\en el modo de mal. ( Spacees mi clave de líder)


2

Aunque las respuestas existentes funcionan bien, me gustaría compartir una más simple:

(defun toggle-theme ()
  (interactive)
  (if (eq (car custom-enabled-themes) 'leuven)
      (disable-theme 'leuven)
    (enable-theme 'leuven)))
(global-set-key [f5] 'toggle-theme)

Esto no deshabilita el tema personalizado predeterminado primero, pero me gusta.


1

Este es el módulo que escribí para mis propios .emacs para resolver este problema. Mi enfoque básico parece ser similar en la intención a la solución de Maxim Kim (rotando a través de una lista de temas), pero creo que el mío es más modular y, por lo tanto, puede ser más accesible para un extraño. Por otro lado, no tengo ninguna de las características de persistencia de Kim.

Aquí está el código relevante, dejando de lado las declaraciones variables y los comentarios del paquete:

(require 'dash)

(defun multitheme--enable (theme)
  "As `enable-theme', but load the theme if necessary.
Respect `custom-safe-themes'."
  (if (custom-theme-p theme)
      (enable-theme theme)
    (load-theme theme)))

(defun multitheme-cycle ()
  "Cycle between the themes in `multitheme-base-theme-list'.
If none of these themes is currently active, instead enable the
first element of `multitheme-base-theme-list'.

Also re-enable `multitheme-overtheme' so it remains \"on top\" of
the base theme.

If a theme to be enabled is not yet defined, attempt to load it
first (using `load-theme').  Respect `custom-safe-themes'.

After all theme changes have been made, run
`multitheme-base-change-hook'."
  (interactive)
  (when (require 'validate nil :noerror)
    (validate-variable 'multitheme-base-theme-list)
    (validate-variable 'multitheme-overtheme)
    (validate-variable 'multitheme-base-theme-change-hook))
  (let ((themes (-drop-while
                 (lambda (thm) (not (custom-theme-enabled-p thm)))
                 multitheme-base-theme-list)))
    ;; Cycle base theme
    (if (null themes)
        (multitheme--enable (car multitheme-base-theme-list))
      (disable-theme (car themes))
      (multitheme--enable (or (cadr themes)
                              (car multitheme-base-theme-list))))
    ;; Reassert overtheme
    (when multitheme-overtheme
      (multitheme--enable multitheme-overtheme))
    ;; Run hooks
    (run-hooks 'multitheme-base-theme-change-hook)))

0

Tengo esta configuración para temas de ciclismo:

(defvar quick-switch-themes
  (let ((themes-list (list 'jazz
                           ;; 'vicarie-and-blackboard
                           ;; 'tangotango
                           'poet)))
    (nconc themes-list themes-list))
  "A circular list of themes to keep switching between.
Make sure that the currently enabled theme is at the head of this
list always.

A nil value implies no custom theme should be enabled.")

(defun quick-switch-themes* ()
  "Switch between to commonly used faces in Emacs.
One for writing code and the other for reading articles."
  (interactive)
  (if-let* ((next-theme (cadr quick-switch-themes)))
      (progn (when-let* ((current-theme (car quick-switch-themes)))
               (disable-theme (car quick-switch-themes)))
             (load-theme next-theme t)
             (message "Loaded theme: %s" next-theme))
    ;; Always have the dark mode-line theme
    (mapc #'disable-theme (delq 'smart-mode-line-dark custom-enabled-themes)))
  (setq quick-switch-themes (cdr quick-switch-themes)))

0

Sé que llego un poco tarde a la fiesta, pero creé un paquete para hacer exactamente eso y mucho más.

Básicamente, le permite definir una lista de sus temas de color favoritos (que es opcional) y moverse convenientemente por la lista.

Al usar nuestro sitio, usted reconoce que ha leído y comprende nuestra Política de Cookies y Política de Privacidad.
Licensed under cc by-sa 3.0 with attribution required.