Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[tlug] Emacs lisp: setting font faces



Josh Glover writes:

 > But I hate this, because if hell freezes over and I have to change my
 > colour scheme, I have to change crap in multiple places.
 > 
 > I'd rather define some variables and use them.

Well, for starters, it should be possible to inherit faces (but it
currently doesn't work right in XEmacs).

Even though you've got it all in one place in the source (init.el),
it's bara-bara in the running Emacs.  This stuff is hardly
performance-critical.  So, I would suggest a hash table:

(setq colour-role-table (make-hash-table :test 'eq))
(mapc (lambda (x) (puthash (nth 0 x) (nth 1 x) colour-role-table))
      '((setq bg-colour      "Black")
        (setq comment-colour "White")
        (setq code-colour    "SpringGreen")
        (setq string-colour  "Orange")
        (setq preproc-colour "Green")
        (setq keyword-colour "Yellow")
        (setq ref-colour     "red3")
        (setq type-colour    "LightBlue")))

;; Face to use for comments
;; Generalization of the function below to handle additional styles
;; (such as bold or italic) is left to the reader.
(set-face-foreground 'font-lock-comment-face comment-colour)

;; This lambda, or even the mapc, could be encapsulated in a named
;; function.
(mapc (lambda (x)
        (set-face-foreground (nth 0 x) (gethash (nth 1) colour-role-table))
        (make-face-bold (nth 0 x)))
      '(
        ; Face to use for function and variable names
        (font-lock-function-name-face code-colour)
        (font-lock-variable-name-face code-colour)
        ; Face to use for strings and documentation strings
        (font-lock-string-face        string-colour)
        (font-lock-doc-string-face    string-colour)
        ; Face to use for preprocessor commands
        (font-lock-preprocessor-face  preproc-colour)
        ; Face to use for keywords
        (font-lock-keyword-face       keyword-colour)
        ; Face to use for reference names
        (font-lock-reference-face     ref-colour)
        ; Face to use for type names
        (font-lock-type-face          type-colour)))

(when you-want-to-see-whats-in-the-table
  (with-displaying-temp-buffer
    ;; Sorting is left as an exercise for the reader.
    ;; If you want to be sure to have the same order as in the data list
    ;; above, use external plists rather than a hash table (homework).
    ;; Hint: C-x f plist-put RET.
    (maphash (lambda (key value)
               (princ (format "%32s%s\n" key value))))))

N.B. The above isn't quite right, I've messed up which faces should be
bolded.



Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links