color-themeを設定する

.emacsで、かねてから少し気になっていた文字色などの設定をcolor-theme.elを用いてやってみた。

color-theme.elの概要

  • (require 'color-theme)する。
  • 「color-theme-なんとか」という名前の関数を定義してその中にスタイルの定義を書く。
  • テーマを適用する時は、そうして定義された関数を呼び出す。

スタイルの定義

  • 単にソースコードに色づけをするだけではなく、モードラインとかshow-paren-modeでハイライトされる括弧だとか選択したリージョン、インクリメンタルサーチしたときの検索結果のハイライト、ミニバッファに表示されるプロンプトなどなど、ほとんどあらゆる箇所のスタイルを設定できる。それを一覧表示するのが「M-x list-faces-display」。多すぎるのでデフォルトでいいところはデフォルトまたは他の人のをパクって、必要なところは自分で設定するのが常道だろう。
  • スタイルは前景色、背景色の他ボールドやイタリックやアンダーラインなどのフォントの装飾が可能。M-x list-faces-displayから、各フェイスをその場で設定する画面に入ることができて、そこで設定項目もわかる。
  • 新しいfaceを追加することもできる。例えばYaTeXは独自に定義したfaceをyatex-modeのsyntax highlightingに使っている(yatexlib.elを読むとわかる。また、YaTeXを起動した後にM-x list-faces-displayするとYaTeX用の設定が追加されているのを確認することができる)。ただし、新しく定義したfaceをどのような時に適用するのかを定義するやり方は調べていない。
  • 色の設定には、M-x list-colors-displayが便利。

font lock mode

しかし普通のsyntax highlightingはfont lock modeの設定を読みにいく(YaTeXも部分的にはfont lock modeの設定を読む)。Font lock modeとは:

Font Lock mode is a feature that automatically attaches face properties to certain parts of the buffer based on their syntactic role. How it parses the buffer depends on the major mode; most major modes define syntactic criteria for which faces to use in which contexts. This section explains how to customize Font Lock for a particular major mode.
GNU Emacs Lisp Reference Manual - 23.6 Font Lock Mode

で、最初から定義されているfont lock mode用のfaceは23.6.7 Faces for Font Lockに書いてあって、多くのメジャーモードもこの設定を参照するらしいので、syntax highlightingを自分の好きな色にするにはここを中心的に設定すればいいようである。

私の設定

http://www.geocities.co.jp/SiliconValley-SanJose/7474/EmacsCustomize.htmlさんを参考にして設定をしてみた。

(global-font-lock-mode t)
(setq default-frame-alist ; ウィンドウサイズなどの設定。
      (append
       '((width . 90)
	 (height . 45)
	 (right-fringe . 2)
	 (left-fringe . 4)
	 ) default-frame-alist))

;; ここからcolor-themeの設定。

(require 'color-theme)
(color-theme-initialize)
(defun color-theme-witchmakers ()
  (interactive)
  (color-theme-install
   '(color-theme-witchmakers
     ((foreground-color . "black")
      (background-color . "white")
      (cursor-color . "gray"))
     (modeline ((t (:foreground "dark cyan" :background "light cyan"))))
     (fringe ((t (:foreground "white" :background "white"))))
     (region ((t (:foreground "black" :background "beige"))))
     (font-lock-comment-face
      ((t (:foreground "red" :background "white"))))
     (font-lock-comment-delimiter-face
      ((t (:foreground "red" :background "white"))))
     (font-lock-doc-face
      ((t (:foreground "dark slate blue" :background "white" :italic t))))
     (font-lock-string-face
      ((t (:foreground "dark slate gray" :background "white"))))
     (font-lock-keyword-face
      ((t (:foreground "dark green" :background "white"))))
     (font-lock-builtin-face
      ((t (:foreground "blue violet" :background "white"))))
     (font-lock-function-name-face
      ((t (:foreground "orange" :background "white" :bold t))))
     (font-lock-variable-name-face
      ((t (:foreground "steel blue" :background "white"))))
     (font-lock-type-face
      ((t (:foreground "deep pink" :background "white"))))
     (font-lock-constant-face
      ((t (:foreground "light sea green" :background "white"))))
     (font-lock-preprocessor-face
      ((t (:foreground "dark red" :background "white"))))
     (font-lock-negation-char-face
      ((t (:foreground "dark blue" :background "white"))))
     (font-lock-warning-face
      ((t (:foreground "dark magenta" :background "white" :underline t))))
     (show-paren-match
      ((t (:foreground "dark cyan" :background "cyan"))))
     (show-paren-mismatch
      ((t (:foreground "dark violet" :background "violet"))))
     )))
(color-theme-witchmakers)