emacs-syntax-hilight


Syntax highlighting under GNU Emacs

Emacs is capable of coloring symbols and syntactical elements in Scheme code; in Emacs jargon this is called font locking. To understand it read the elisp guide, node Font Lock Mode.

To turn on font lock mode in all the files, we can add the following to the .emacs configuration file:

(global-font-lock-mode t)
(setq font-lock-maximum-decoration t)

To customize the Scheme mode of Emacs we add a function to the scheme-mode-hook in the .emacs configuration file:

(add-hook 'scheme-mode-hook 'my-scheme-mode-hook)

(defun my-scheme-mode-hook ()
  "Custom Scheme mode hook."
  (interactive)
  ...)

Font Lock mode configuration

Font locking is configured with the variable font-lock-defaults; the setting below is copied from the original function scheme-mode-variables in the file scheme.el. The value of this variable must be a list.

There are 3 levels of font locking, the first element of font-lock-defaults can be a list of symbols, each symbol being the name of a variable holding the font locking specification for a level. The default for Scheme mode is:

(scheme-font-lock-keywords scheme-font-lock-keywords-1
 scheme-font-lock-keywords-2)

we change it to:

(scheme-font-lock-keywords scheme-font-lock-keywords-1
 my-scheme-font-lock-keywords)

and embed scheme-font-lock-keywords-2 inside the custom specification. Notice that scheme-font-lock-keywords-1 embeds scheme-font-lock-keywords, so each level is a superset of the lower one.

In the Scheme mode hook we can add:

 (defun my-scheme-mode-hook () 
   ...  
   (setq font-lock-defaults 
         '((scheme-font-lock-keywords 
            scheme-font-lock-keywords-1 
            my-scheme-font-lock-keywords) 
            nil t (("+-*/.<>=!?$%_&~^:" . "w")) beginning-of-defun 
            (font-lock-mark-block-function . mark-defun))) 
   ...) 

To define my-scheme-font-lock-keywords we need the value of scheme-font-lock-keywords-2 which is available only when scheme.el is loaded; so we use defconst to declare the my-scheme-font-lock-keywords and set it to a nil value:

 (defconst my-scheme-font-lock-keywords 
   '() 
   "Custom highlighting in Scheme modes.") 

then in the Scheme hook we set the real value:

 (defun my-scheme-mode-hook () 
   ... 
   (setq my-scheme-font-lock-keywords 
         (append scheme-font-lock-keywords-2 
                 (eval-when-compile 
                   (list 
                    (regexp-opt '("compensate" "when") t) 
                    ;;This must come before the errors specification, or 
                    ;;"misc-error" will not be colored correctly. 
                    (cons (regexp-opt '("wrong-type-arg" "misc-error" 
                                        "out-of-range" "system-error") t) 
                          'font-lock-constant-face) 
                    (cons (regexp-opt '("scm-error" "error" 
                                        "false-if-exception") t) 
                          'font-lock-warning-face))))) 
   ...) 

this value:

  1. adds compensate and with as keywords, font-lock-keyword-face is automatically selected for them;
  2. highlights the common exception keywords of Guile with the constant face;
  3. highlights the common exception handling function symbols with the warning face, making them stick out in the code.

The function regexp-opt accepts a list of strings as first argument and returns a regular expression that matches them. When we need to associate a regexp to a face, we build a pair for them:

(regular-expression . font-lock-face)

we can add any number of pairs, we only have to notice that many highlight specification will make colorization slow. The elisp guide documents the available font lock faces in the node Faces for Font Lock.


category-emacs