HTTPS SSH

theme-switcher

Switch Emacs theme depending on the file being edited.

Introduction

For years I used to set terminal colors depending on the machine I am logged on. I am less likely to stop production database instead of development one when development terminal is white-on-black while production one black-on-pink.

This module tries to implement the same idea to Emacs: editor theme changes depending on the file being edited at the moment. In simplest configuration one theme is used for local, and another one for remote edits, but it is also possible to bind themes to various local source trees and pick different theme depending on the machine.

In my setup I use twilight for normal edits, solarized for remote files open via ssh on development machine, and anti-zenburn for fragile remote edits on important hosts. This gives immediate visual feedback on what I am editing.

WARNING: I don't really speak Emacs Lisp. Any suggetions how to improve this code are welcome.

Installation

Drop theme-switcher.el anywhere in your load-path. Install some (noticeably different) themes. Then configure as described below.

Configuration

In your ~/.emacs:

;; Let Emacs know about your theme paths (it may be unnecessary in
;; some setups, but for package-installed themes I was getting
;; errors without this). Must be called before
;; (theme-switcher-activate)

(require 'twilight-theme-autoloads)
(require 'solarized-theme-autoloads)
(require 'molokai-theme-autoloads)
(require 'zenburn-theme-autoloads)
(require 'twilight-anti-bright-theme-autoloads)
(require 'anti-zenburn-theme-autoloads)
(require 'sublime-themes-autoloads)

;; Load theme-switcher
(require 'theme-switcher)

;; Configure themes according to your preferences
(custom-set-variables
    ;; Theme used by default, if nothing else applies
    '(theme-switcher-default "twilight")
    ;; Theme used by default for remote (tramp) edits
    '(theme-switcher-default-remote "solarized-dark")
    ;; Overrides for specific remote hosts. Pairs 
    ;; hostname . theme name
    '(theme-switcher-remote '(
         ("venus" . "zenburn")
         ("mars"  . "molokai")
     ))
    ;; Overrides for specific local directory trees
    '(theme-switcher-local '(
         ("~/private" . "twilight-anti-bright")
         ("~/work" . "twilight")
     ))
 )

 ;; Enable theme swiching once initialization is finished
 (theme-switcher-activate-lazily)

Usage

Just edit files and directories as you like. Emacs should change it's colors depending on the current active buffer.

There are also two additional commands:

M-x theme-switcher-load-theme

Similar to M-x load theme, but:

  • unloads previous theme before applying new one (no more mixed colors from many themes, this may be handy even without using remaining functionality of this module),

  • disables theme switching (as you loaded the theme manually, you likely wish to stay with it).

M-x theme-switcher-dynamic

Enable dynamic switching again if it was disabled by M-x theme-switcher-load-theme. Also, sets theme according to the current buffer (this should work even if switching hook is not active, so you can use this to test your setup).

Source

Module code is hosted on BitBucket:

 hg clone https://bitbucket.org/Mekk/emacs_libs-theme-switcher

(or download repository if you don’t speak Mercurial).

Ideas, patches, suggestions

Are most welcome.

BitBucket has issue tracker which may be used for bug reports and impovement suggestions, contact me via email (listed in sources) if BitBucket is unacceptable for some reason.

For code changes, pull requests are most welcome, patches attached to issues or sent by email are also OK.

TODO

Things I would like to do (once I find out how):

  • fine-tune logic of picking for which windows we apply the rules (maybe skip changes on some trees, maybe handle more dired-like windows)

  • limit theme to single frame (or maybe even buffer?),

  • avoid "white flash" effect on dark-to-dark transition (calculate what to unload and what to load? function „transition between faces” which unloads only things which will not be overriden? Or maybe just different ordering of load/unload?)

  • make it possible to define more general theme matching rules (reference remote user, group a few remote machines together, reference both remote machine and actual directory...),

  • better customize support (define type for structural variables, maybe dropdown for themes),

  • publish to https://github.com/milkypostman/melpa