I would like to start writing a few simple Domain Specific Languages in Common Lisp and would like the names of macros I define to be highlighted as if they were Common Lisp functions or macros in Emacs.

Below is a simple example which is not precisely the code I am hoping to write but it shows the kind of functionality I desire.

(defmacro class* (class-name superclasses slots)
  (let ((slot-declarations
     (loop for slot in slots collecting
          (list slot :accessor slot :initform nil))))
    `(defclass ,class-name ,superclasses ,slot-declarations)))

(class* graph () (nodes edges))

What I would like is for class* in the second form to show up in magenta (given the default colours in my Emacs installation) and graph should be highlighted in blue.

I have seen some examples of how to adjust syntax highlighting for LaTeX, but I could not extend those to work for me in Common Lisp.


I have been able to solve my issue above by looking through the Emacs source code, specifically font-lock.el. It is recommended that one use regex-opt to generate a suitable regular expression to be added to the font lock keywords for the mode.

In my case (regex-opt '("class*")) generates "\\(?:class\\*\\)".

This is then added (in my case) to lisp-mode as below.

(font-lock-add-keywords 'lisp-mode '("\\(?:class\\*\\)"))

However, this has revealed to me a deeper issue - I would actually like to have the class* in the macro definition highlit in blue since it is the name being declared/defined. Also, it would be nice if the word graph was in blue as well, but later on if used to create graph objects it would be in maroon.

I will work on this add anything I find later to this post in case it is of interest. Otherwise if it is felt this issue should be closed, please do so.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.