Alex Hall
Alex Hall

Reputation: 171

Cycle Custom Themes w/ emacs 24

I am currently using the new customtheme tool for emacs 24, and I'd like to be able to cycle through custom themes by hitting a key (such as f12).

The code below sounds like it does exactly what I want it to do, but it uses color-theme, which I don't want to do. I'm not familiar enough with Lisp yet to write this on my own or to convert from the old version to the new one. Is there anyone that can help me get the same functionality into emacs 24 using the new built in load-theme command?

Also, based on this discussion, I may need to call disable-theme with each call of the function.

Any help is greatly appreciated.

Here's the old solution using color-theme

(defun my-theme-set-default () ; Set the first row
  (interactive)
  (setq theme-current my-color-themes)
  (funcall (car theme-current)))

(defun my-describe-theme () ; Show the current theme
  (interactive)
  (message "%s" (car theme-current)))

; Set the next theme (fixed by Chris Webber - tanks)
(defun my-theme-cycle ()        
  (interactive)
  (setq theme-current (cdr theme-current))
  (if (null theme-current)
  (setq theme-current my-color-themes))
  (funcall (car theme-current))
  (message "%S" (car theme-current)))

(setq theme-current my-color-themes)
(setq color-theme-is-global nil) ; Initialization
(my-theme-set-default)
(global-set-key [f12] 'my-theme-cycle)

Upvotes: 2

Views: 891

Answers (2)

user1245262
user1245262

Reputation: 7505

Here is some code that's analogous to JakubHozak's code, but the switching isn't as fast as it is for color-themes. I don't understand why, entirely, but it seems to be caused by the difference between custom themes and color themes. Although if you restrict your cycling to themes that come with emacs, it seems to be much more bearable.

(setq my-themes (list 'tango 'solarized-dark 'solarized-light 'alect-dark)) ;;the themes I cycle among
(setq curr-theme my-themes)

(defun my-theme-cycle ()
    (interactive)
    (disable-theme (car curr-theme)) ;;Nee flickeringded to stop even worse
    (setq curr-theme (cdr curr-theme))
    (if (null curr-theme) (setq curr-theme my-themes))
    (load-theme (car curr-theme) t)
    (message "%s" (car curr-theme))
)

(global-set-key [f12] 'my-theme-cycle)
(setq curr-theme my-themes)
(load-theme (car curr-theme) t) 

Upvotes: 3

Drew
Drew

Reputation: 30701

FWIW, here are two ready-made ways (using Icicles or Do Re Mi) to cycle Emacs custom themes (what you call color themes for Emacs 24, but they are custom themes, not color themes). The same page explains differences between color themes and custom themes.

Upvotes: 3

Related Questions