Pages

Sunday, January 22, 2012

Emacs: Fonts, Colors and Themes

GNU Emacs is an extensible, customizable text editor--and more. It has various modes and features, making it a powerful editor for different purposes, some of which may be found in its project homepage. A thorough introduction to Emacs is provided by the book Learning GNU Emacs.

Figure 1. Emacs screenshot.

There are several features particularly popular among programmers: different modes for different languages, syntax highlighting, auto-completion, etc. Moreover, it is highly customizable, making it possible for us to extend it and make our own versions that meet our personal needs. This post will touch upon a few of these, focusing on how you can make your emacs look better.

Changing Fonts

Emacs has a command for specifying fonts:
M-x set-default-font
After hitting Enter, you'll be asked to provide a font name. You don't have to remember your favorite font, though, because you can see the full list of available fonts by taking advantage of Emacs' auto-completion feature - just try typing a Tab, then you'll simply choose whatever you like from the resulting list:


Figure 2. Fonts list.

You can make the change permanent by adding the following line to your .emacs file: (Make sure that you have substituted the correct font name.)
(set-default-font "-unknown-DejaVu Sans Mono-normal-normal-normal-*-*-*-*-*-m-0-iso10646-1")

You can also change the font size by adding another line to your .emacs file:
(set-face-attribute 'default nil :height 105)
.
where you specify the font size by its height.

Installing New Fonts

You're never confined to the default system fonts; you're free to add whatever fonts you like to your system and use them in Emacs as discussed above. Some of the fonts you may like can be found in the article Top 10 Programming Fonts.

The procedure of installing a new font is described in this tutorial. Here is a step-by-step example of installing a new font DejaVu Sans Mono:

    1. Download the font here;
    2. Unzip the tarball:
tar -jxf dejavu-fonts-ttf-2.33.tar.bz2
    3. Copy the *.ttf files to directory /usr/share/fonts:
sudo cp dejavu-fonts-ttf-2.33/ttf/ttf.* /usr/share/fonts
    4. Update the font cache:
sudo fc-cache -f -v

Then you're done! You should now be able to find and use your new font "-unknown-DejaVu Sans Mono-normal-normal-normal-*-*-*-*-*-m-0-iso10646-1" by following the procedure described in the previous section.

Changing Colors and Themes

To manage Emacs color themes, try color-theme, an emacs-lisp mode for skinning your emacs. You may download the tarball or use your package manager to install this package:
sudo apt-get install emacs-goodies-el
Then you can test the color themes in Emacs:
M-x color-theme-name
where name is the name of the theme you'd like to try. Note that, as is the case when you try the fonts, you may use TAB to list all available color themes and choose one you like. Once you've decided which theme to use, add the following lines of code to your .emacs file to make the modification permanent:
(require 'color-theme)
(color-theme-initialize)
(color-theme-name)
The next time you start up Emacs, you're ready to view your new color theme!

Designing Your Own Color Theme

It may seem not so desirable to spend too much time designing a brand-new color theme; yet more often is the case when you're comfortable with a particular theme, but feeling like to add some minor changes here or there to make it perfect. I started searching for such technique when I'd like keywords to be bold and comments to appear italic, while most of the themes I like don't achieve this.

To modify an existing color theme, locate it in the color-theme-library.el file (which should be found in the directory where you have installed the color-theme package). The color theme is essentially a lisp function, as shown in the following code snippet:
(defun color-theme-yang ()
  (interactive)
  (color-theme-install
   '(color-theme-yang
     ((background-color . "#ffffff")
     (background-mode . light)
     (border-color . "#969696")
     (cursor-color . "#000000")
     (foreground-color . "#000000")
     (mouse-color . "black"))
     (fringe ((t (:background "#ffffff"))))
     (mode-line ((t (:foreground "#000000" :background "#e5e5e5" :box (:line-width 1)))))
     (mode-line-inactive ((t (:foreground "#000000" :background "#e0e0e0 ":box (:line-width 1)))))
     (region ((t (:background "#dedede"))))
     (font-lock-builtin-face ((t (:foreground "#a73985" :bold t))))
     (font-lock-comment-face ((t (:foreground "#ff0000" :italic t))))
     (font-lock-function-name-face ((t (:foreground "#293cff"))))
     (font-lock-keyword-face ((t (:foreground "#ff9a00" :bold t))))
     (font-lock-string-face ((t (:foreground "#ff24f8"))))
     (font-lock-type-face ((t (:foreground"#293cff" :bold t))))
     (font-lock-constant-face ((t (:foreground "#ff00ff"))))
     (font-lock-variable-name-face ((t (:foreground "#12800f"))))
     (minibuffer-prompt ((t (:foreground "#7299ff" :bold t))))
     (font-lock-warning-face ((t (:foreground "red" :bold t))))
     )))
(provide 'color-theme-yang)
This means you have to modify the lisp code to get the desired effect. Fortunately, the code is quite straightforward. For example, if you like another keyword color, you just have to find the line
(font-lock-keyword-face ((t (:foreground "#ff9a00" :bold t))))
and change the color code #ff9a00 to whatever you like. Also, you may have noticed that if you want the keyword to be bold, you have to specify ":bold t" in the corresponding line; similarly, you can say ":italic t" if you'd like the relevant element to appear italic. Likewise, You may change properties of other programming elements such as class name, function name, string/number literal, etc. These elements together form a color theme.

After you're done, you have a new function. You're free to rename it, as I did here, which results in a new color theme. You can put the code in your color-theme-library.el file, .emacs file or wherever in your Emacs load path and use it the same way as discussed in the previous section.

You may also be interested in Emacs Color-theme Creator, a visual tool to design or modify an Emacs color theme. It provides two initial themes where you start, or you can provide an existing color theme (by copying the code in the color-theme-library.el file) to the text-box located at the bottom-right corner of the page. After you're satisfied with your new design, click the "Generate config file" above the text-box I've just mentioned, copy the resulting code to your color-theme-library.el file or .emacs file, follow the same procedure as described above, and you're done! Congratulations!

Finally, Figure 3 is what color-theme-yang looks like:

Figure 3. Demonstration of color-theme-yang.

Enjoy customizing your Emacs!

No comments:

Post a Comment