Difference between revisions of "Look and Feel"

From WikEmacs
Jump to: navigation, search
(Created page with "Color Themes")
 
(Added some basic info on color theming)
Line 1: Line 1:
Color Themes
+
== Color Themes ==
 +
 
 +
Prior to Emacs 24 the most popular way to incorporate custom color themes into Emacs was the [http://www.emacswiki.org/emacs/ColorTheme color-theme package]. While it usually got the job done it had some problems that we won’t be discussing here and more importantly - it’s a third-party package, that’s not part of Emacs proper.
 +
 
 +
[http://batsov.com/articles/2011/08/19/a-peek-at-emacs24/ Emacs 24] finally introduced a new standard way of dealing with color themes (based on Emacs’s built-in customize facility). While it doesn’t have a proper name (as far as I know) it’s commonly referred to as the <tt>deftheme</tt> facility, since <tt>deftheme</tt> is the name of the macro you’d use to create such a theme. ( <tt>deftheme</tt> has actually been around since Emacs 23, but it was heavily improved in Emacs 24 )
 +
 
 +
Emacs 24 comes with a selection of built-in themes that you can choose from, so you’re no longer bound to the default theme (which many find quite ugly). To choose a new theme just do a <tt>M-x load-theme</tt> (tab completion is available for the names of the available themes). At this point you can give the command a try with the <tt>tango</tt> theme. If you like a theme so much that you’d want to use it all the time you can put in your Emacs configuration (<tt>.emacs</tt> or <tt>init.el</tt> for instance) like this:
 +
 
 +
<pre class="cl">(load-theme 'theme-name t)</pre>
 +
If you’d like to return to the default-theme just do a <tt>M-x disable-theme</tt>.
 +
 
 +
How do you create a <tt>deftheme</tt> theme? Quite simply actually - just do a “M-x customize-create-theme”. You’ll be presented with an UI prompting you for a theme name, description and faces. After you save the theme a file called <tt>name-theme.el</tt> will be written on your filesystem. Here’s its skeleton:
 +
 
 +
<syntaxhighlight lang="lisp">
 +
(deftheme demo
 +
  &quot;Demo theme&quot;)
 +
 
 +
(custom-theme-set-faces
 +
'demo
 +
;;; list of custom faces
 +
)
 +
 
 +
(provide-theme 'demo)
 +
</syntaxhighlight>
 +
There was also an online theme generator [http://elpa.gnu.org/themes/ here], but it seems to be down at the moment.
 +
 
 +
If dislike customize, you can just have a look at the source code of the built-in tango (or any other) theme and use it as a reference.
 +
 
 +
Once you’ve created the new theme you’ll have to drop it in a folder that’s on the <tt>custom-theme-load-path</tt>. I’d suggest the following:
 +
 
 +
<pre class="lisp">(add-to-list 'custom-theme-load-path &quot;~/.emacs.d/themes&quot;)</pre>
 +
If you’re an [https://github.com/bbatsov/emacs-prelude Emacs Prelude] user you’re already covered. This folder exists and is automatically added to <tt>custom-theme-load-path</tt> by Prelude, so all you have to do is drop there the themes you’d want to try out.
 +
 
 +
You may find the [http://julien.danjou.info/software/rainbow-mode rainbow-mode] useful when developing color themes. If fontifies strings that represent color codes according to those colors. The mode is known to be a great addition to css-mode, but I find it very helpful with color theme development as well. It’s also included (and enabled) in Prelude by default.
 +
 
 +
The Emacs package manager <tt>package.el</tt> (formerly known as ELPA) is gaining a lot of popularity lately and the community [http://marmalade-repo.org/ Marmalade] repository already houses a few Emacs 24 themes that you can install from there. If you’re developing a theme that you’d like to submit to Marmalade it’s imperative that the theme modifies the <tt>custom-theme-load-path</tt> in an <tt>autoload</tt> - otherwise it won’t be of much use. Add the following snippet (or something similar) before the <tt>provide-theme</tt> line if your custom theme:
 +
 
 +
<syntaxhighlight lang="lisp">
 +
;;;###autoload
 +
(when load-file-name
 +
  (add-to-list 'custom-theme-load-path
 +
              (file-name-as-directory (file-name-directory load-file-name))))
 +
</syntaxhighlight>
 +
I’d also advise you follow the proper naming convention <tt>name-theme.el</tt> so that it’s apparent that your theme is <tt>deftheme</tt> compatible.
 +
 
 +
Oh, and one more thing - porting themes from color-theme to deftheme is really simple (just have a look at the old and the new version of Zenburn in its repo), so you should really consider porting all the themes you maintain to <tt>deftheme</tt>.

Revision as of 00:30, 25 March 2012

Color Themes

Prior to Emacs 24 the most popular way to incorporate custom color themes into Emacs was the color-theme package. While it usually got the job done it had some problems that we won’t be discussing here and more importantly - it’s a third-party package, that’s not part of Emacs proper.

Emacs 24 finally introduced a new standard way of dealing with color themes (based on Emacs’s built-in customize facility). While it doesn’t have a proper name (as far as I know) it’s commonly referred to as the deftheme facility, since deftheme is the name of the macro you’d use to create such a theme. ( deftheme has actually been around since Emacs 23, but it was heavily improved in Emacs 24 )

Emacs 24 comes with a selection of built-in themes that you can choose from, so you’re no longer bound to the default theme (which many find quite ugly). To choose a new theme just do a M-x load-theme (tab completion is available for the names of the available themes). At this point you can give the command a try with the tango theme. If you like a theme so much that you’d want to use it all the time you can put in your Emacs configuration (.emacs or init.el for instance) like this:

(load-theme 'theme-name t)

If you’d like to return to the default-theme just do a M-x disable-theme.

How do you create a deftheme theme? Quite simply actually - just do a “M-x customize-create-theme”. You’ll be presented with an UI prompting you for a theme name, description and faces. After you save the theme a file called name-theme.el will be written on your filesystem. Here’s its skeleton:

(deftheme demo
  &quot;Demo theme&quot;)
 
(custom-theme-set-faces
 'demo
 ;;; list of custom faces
 )
 
(provide-theme 'demo)

There was also an online theme generator here, but it seems to be down at the moment.

If dislike customize, you can just have a look at the source code of the built-in tango (or any other) theme and use it as a reference.

Once you’ve created the new theme you’ll have to drop it in a folder that’s on the custom-theme-load-path. I’d suggest the following:

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

If you’re an Emacs Prelude user you’re already covered. This folder exists and is automatically added to custom-theme-load-path by Prelude, so all you have to do is drop there the themes you’d want to try out.

You may find the rainbow-mode useful when developing color themes. If fontifies strings that represent color codes according to those colors. The mode is known to be a great addition to css-mode, but I find it very helpful with color theme development as well. It’s also included (and enabled) in Prelude by default.

The Emacs package manager package.el (formerly known as ELPA) is gaining a lot of popularity lately and the community Marmalade repository already houses a few Emacs 24 themes that you can install from there. If you’re developing a theme that you’d like to submit to Marmalade it’s imperative that the theme modifies the custom-theme-load-path in an autoload - otherwise it won’t be of much use. Add the following snippet (or something similar) before the provide-theme line if your custom theme:

;;;###autoload
(when load-file-name
  (add-to-list 'custom-theme-load-path
               (file-name-as-directory (file-name-directory load-file-name))))

I’d also advise you follow the proper naming convention name-theme.el so that it’s apparent that your theme is deftheme compatible.

Oh, and one more thing - porting themes from color-theme to deftheme is really simple (just have a look at the old and the new version of Zenburn in its repo), so you should really consider porting all the themes you maintain to deftheme.