Changing Color Themes Emacs 24 -- order matters

14,510

Solution 1

It seem to me that even on Emacs 24 you're still using the old (and unmaintained) color-theme package. Emacs 24 has a built-in color theming infrastructure (and themes like zenburn and solarized have been ported to it) that I'd suggest you use instead. Have a look here for details on deftheme and friends.

То answer your particular question about color-theme - themes usually do not define every face that a previous theme might have tweaked and that causes your problems. Moving to the default theme between themes might probably be considered similar to a css reset.

Solution 2

To automatically disable current theme before load the new one, you can also use advice:

(defadvice load-theme 
  (before theme-dont-propagate activate)
  (mapcar #'disable-theme custom-enabled-themes))

Solution 3

Inserting the code below in your .emacs/init.el, I bound C-t to cycle through a fixed list of themes in the specified order. This is compatible with Emacs 24.

;;;;; Theme ;;;;;
;; Cycle through this set of themes
(setq my-themes '(solarized-light solarized-dark zenburn railscast))

(setq my-cur-theme nil)
(defun cycle-my-theme ()
  "Cycle through a list of themes, my-themes"
  (interactive)
  (when my-cur-theme
    (disable-theme my-cur-theme)
    (setq my-themes (append my-themes (list my-cur-theme))))
  (setq my-cur-theme (pop my-themes))
  (load-theme my-cur-theme t))

;; Switch to the first theme in the list above
(cycle-my-theme)

;; Bind this to C-t
(global-set-key (kbd "C-t") 'cycle-my-theme)

Solution 4

I wrote a function that disables current theme before emacs switches to new one.

You can paste following snippet into you'r init.el and use M-x l0ad-theme.

https://github.com/maruks/.emacs.d

    ;; color themes
(add-to-list 'custom-theme-load-path "~/.emacs.d/themes")

(setq current-t43m3 nil)

(defun enab-theme (theme) 
  (if current-t43m3 (disable-theme current-t43m3))
  (setq current-t43m3 theme) 
  (load-theme theme t)) 

(defun disab-current-theme () 
  (if current-t43m3 (disable-theme current-t43m3))
  (setq current-t43m3 nil))

(global-set-key (kbd "C-c ltwo") '(lambda () (interactive) (enab-theme 'wombat)))

(global-set-key (kbd "C-c ltze") '(lambda () (interactive) (enab-theme 'zenburn)))

(global-set-key (kbd "C-c ltsd") '(lambda () (interactive) (enab-theme 'solarized-dark)))

(global-set-key (kbd "C-c ltsl") '(lambda () (interactive) (enab-theme 'solarized-light)))

(global-set-key (kbd "C-c ltne") '(lambda () (interactive) (enab-theme 'tomorrow-night-eighties)))

(global-set-key (kbd "C-c ltni") '(lambda () (interactive) (enab-theme 'tomorrow-night)))

(global-set-key (kbd "C-c ltnb") '(lambda () (interactive) (enab-theme 'tomorrow-night-bright)))

(global-set-key (kbd "C-c ltto") '(lambda () (interactive) (enab-theme 'tomorrow)))

(global-set-key (kbd "C-c ltta") '(lambda () (interactive) (enab-theme 'tango)))

(global-set-key (kbd "C-c ltdb") '(lambda () (interactive) (enab-theme 'deeper-blue)))

(global-set-key (kbd "C-c ltdi") '(lambda () (interactive) (enab-theme 'dichromacy)))

(global-set-key (kbd "C-c dct") '(lambda () (interactive) (disab-current-theme)))

(defun l0ad-theme (name) 
  (interactive
   (list
    (intern (completing-read "Load custom theme: "
                 (mapcar 'symbol-name (custom-available-themes))))))
  (enab-theme name))

(setq d3fault-theme (getenv "EMACS_DEFAULT_THEME"))

(when d3fault-theme
  (enab-theme (intern d3fault-theme)))

Solution 5

As others said, switch to the Emacs 24 version of themes. Once you're using that, you can "undo" a theme with disable-theme. Just give it the same argument that you passed to load-theme and you should get back to a blank slate. Then just load the new theme.

Share:
14,510

Related videos on Youtube

justingordon
Author by

justingordon

Updated on June 12, 2022

Comments

  • justingordon
    justingordon about 2 years

    In emacs 24, the order that color themes are applied seems to matter. This is obvious if you do M-x color-theme-select. The order that you ic

    Does anybody have any insight into this issue?

    I'd like to be able to switch between the following color themes without restarting:

    1. solarized-light
    2. solarized-dark
    3. zenburn
    4. railscasts

    I guess I need the equivalent of a css-reset for emacs. One other tip that is invaluable is that if you use evil, then you need this line or else your cursor stays black, which is horrible for the dark themes:

    (setq evil-default-cursor t) 
    

    This is a related issue: Switching between color themes in Emacs ( < v.24). I am using Emacs 24.0. I'm posting this question because I'm looking for workaround for pre 24.1, or maybe advice if 24.1 is stable enough.

    • kindahero
      kindahero over 12 years
      emacs 24.0??. M-x display-about-screen or M-x emacs-version
  • justingordon
    justingordon over 12 years
    How do I move to the default theme between switches? Thanks so much! If I'm using elpa, should I make sure that I don't install color-theme? BTW, I love zenburn, but I'd like to also have railscasts colors for when i want a little more contrast during the day. Any, any opinions on customized .Xresources that comes with solarized?
  • Bozhidar Batsov
    Bozhidar Batsov over 12 years
    I guess you've installed a theme that depended on color-theme - there are many of those on Marmalade. In Emacs 24 - M-x disable-theme (though I'm not sure the problem you've describe will be present there). I haven't used color-theme in a while so I cannot help you about it. I personally do not customize .Xresources (for color themes) at all. Why do you want to do so?
  • justingordon
    justingordon over 12 years
    I put in the recommendations here for .Xresources: ethanschoonover.com/solarized. I think that might conflict a little with using emacs color theming.
  • Drew
    Drew over 10 years
    Color theme has a few advantages compared with the custom-them feature of Emacs: (1) Switching color themes is very fast. Switching custom themes is very slow, and is accompanied by flashing as the preceding theme(s) are disabled and the next theme is enabled. The difference is greatly exacerbated if you have several frame. (2) You cannot undo a custom theme, to restore a previous non-theme (but perhaps customized) appearance. You can only "disable" a custom theme relative to another theme.
  • Drew
    Drew over 10 years
    No, you cannot undo a custom theme. AND you can, for the most part, undo a color theme, restoring a previous, non-themed appearance. "Disabling" a custom theme is only relative to another theme.
  • Mike Vella
    Mike Vella over 10 years
    This answer is wrong,, or not completely right. I'm using load-theme and when I use zenburn and solarized (which need to be installed from MELPA/Marmalade) I get the exact problems described by the OP. disable-theme seems to be a good solution, but It would be nice to automate it.
  • ericx
    ericx over 9 years
    What's (kb "C-t")? Why that instead of (kbd "C-t")? Sorry, just a typo?
  • sshine
    sshine over 9 years
    It was not a typo, but in this case, you could just as well have used (kbd "C-t"). The reason I have a 'kb' macro is because of another piece of code for doing multiple keybindings at once: (dolist (pair '(("C-k" kill-whole-line) ("C-z" undo) ...)) ((global-set-key (kb (car pair)) (cadr pair))). If I used 'kbd' rather than my own 'kb' macro: (defun kb (k) (read-kbd-macro k)), it'd complain about 'kbd' being a macro and not a function. Or something like that. Stupid little work-around. I've fixed 'kb' into 'kbd' in the code above, so thanks!
  • sshine
    sshine over 8 years
    If you follow my advice below, I did automate it.
  • Talespin_Kit
    Talespin_Kit almost 4 years
    To try all themes "(setq my-themes (custom-available-themes))"