I'm creating an elisp file based on http://ergoemacs.org/emacs/elisp_syntax_coloring.html from this site.

My updated code is shown below

;;; MOD5-mode.el --- sample major mode for editing MOD5 code.

;; Copyleft © 2016, by 

;; Author:  (@gmail.com)
;; Version: 0.0.1
;; Created: 07-Sep-2016
;; Keywords: languages
;; Homepage: n/a

;; This file is not part of GNU Emacs.

;;; License:

;; You can redistribute this program and/or modify it under the terms of the GNU General Public License version 2.

;;; Commentary:

;; This elisp file will enable the highlight of the MOD5 code when viewed in Emacs.

;; ********* probably a link will be helpful here - under construction ***********

;;; Code:

;; define several category of keywords
(setq MOD5-types '("AI" "AO" "DI" "DO" "AISIM" "DISIM" "ACO" "AIM" "AOT" "DOT" "AP" "AC" "DC" "DT" "DM" "AK" "AG" "AR" "TA"))
(setq MOD5-constants '("PFS" "NFS" "IONE" "ZERO"))
(setq MOD5-events '("WARN" "REQ" "ALT"))
(setq MOD5-functions '("ALM_DI_DO05B" "IFD03B" "ALM_4S_AX04B" "ALM_DX05B" "ALM_RERING03B" "ALM_DI_DO05B" "IS04B"))
(setq MOD5-operators '("=" "AND" "OR" "XOR"))
(setq MOD5-acmdb '("\[(&A-Z0-9_)\]+" "\[(:A-Z0-9_)\]"))

;; generate regex string for each category of keywords
(setq MOD5-keywords-regexp (regexp-opt MOD5-keywords 'words))
(setq MOD5-type-regexp (regexp-opt MOD5-types 'words))
(setq MOD5-constant-regexp (regexp-opt MOD5-constants 'words))
(setq MOD5-event-regexp (regexp-opt MOD5-events 'words))
(setq MOD5-functions-regexp (regexp-opt MOD5-functions 'words))
(setq MOD5-operators-regexp (regexp-opt MOD5-operators 'words))
(setq MOD5-acmdb-regexp (regexp-opt MOD5-acmdb 'words))

;; create the list for font-lock.
;; each category of keyword is given a particular face
(setq MOD5-font-lock-keywords
        (,MOD5-type-regexp . font-lock-type-face)
        (,MOD5-constant-regexp . font-lock-constant-face)
        (,MOD5-event-regexp . font-lock-builtin-face)
        (,MOD5-functions-regexp . font-lock-function-name-face)
        (,MOD5-keywords-regexp . font-lock-keyword-face)
    (,MOD5-acmdb-regexp . font-lock-variable-name-face)
    (,MOD5-operators-regexp . font-lock-negation-char-face)
        ;; note: order above matters, because once colored, that part won't change.
        ;; in general, longer words first

(define-derived-mode MOD5-mode fundamental-mode
  "MOD5 mode"
  "Major mode for editing MOD5 Dowtran Code…"

  ;; code for syntax highlighting
  (setq font-lock-defaults '((MOD5-font-lock-keywords))))

;; clear memory. no longer needed
(setq MOD5-keywords nil)
(setq MOD5-types nil)
(setq MOD5-constants nil)
(setq MOD5-events nil)
(setq MOD5-functions nil)
(setq MOD5-acmdb nil)
(setq MOD5-operators nil)

;; clear memory. no longer needed
(setq MOD5-keywords-regexp nil)
(setq MOD5-types-regexp nil)
(setq MOD5-constants-regexp nil)
(setq MOD5-events-regexp nil)
(setq MOD5-functions-regexp nil)
(setq MOD5-acmdb-regexp nil)
(setq MOD5-operators-regexp nil)

;; add the mode to the `features' list
(provide 'MOD5-mode)

;; Local Variables:
;; coding: utf-8
;; End:

;;; MOD5-mode.el ends here

In the above code, I'm unable to get the following to work:

(setq MOD5-operators '("=" "AND" "OR" "XOR"))
(setq MOD5-acmdb '("\[(&A-Z0-9_)\]+" "\[(:A-Z0-9_)\]"))

What is wrong here? I have copy-pasted the existing code and created the above two lines and for some reason they do not highlight what is within quotes.

And also the regex "\[(&A-Z0-9_)\]+" "\[(:A-Z0-9_)\]" is intended to match text like

  1. :ALM_SIP_PROB_123
  2. (:ALM_SIP_PROB_123)
  3. (&:ALM_SIP_PROB_123)
  4. PIT14501

I'm using the following to generate the regex string:

(setq MOD5-operators-regexp (regexp-opt MOD5-operators 'words))
(setq MOD5-acmdb-regexp (regexp-opt MOD5-acmdb 'words))

I'm wondering if something could be wrong with this?

Update - Adding a sample program

enter image description here

From the above image you can see that the OR, AND have not been highlighted.
Also the numbers within parenthesis have not been highlighted

Update - Changed the elisp code - face name updated from font-lock-operator to font-lock-negation-char-face

(,MOD5-operators-regexp . font-lock-negation-char-face)
  • What's the font-lock-operator face? I don't have that one here...
    – wasamasa
    Commented Sep 8, 2016 at 6:16
  • Agree with @wasamasa.
    – JeanPierre
    Commented Sep 8, 2016 at 6:56
  • Can you provide an example MOD5 file where syntax highlighting doesn't work, and describe what thought should happen. Commented Sep 8, 2016 at 7:02
  • 2
    All the (setq foo bar) ... (setq foo nil) above could be advantageously replaced with (let ((foo bar)) ...).
    – Stefan
    Commented Sep 9, 2016 at 12:31
  • 2
    setq is meant to modify a variable that is already defined elsewhere. The definition of a variable (like MOD5-font-lock-keywords above) should be done wither with defvar or with defconst.
    – Stefan
    Commented Sep 9, 2016 at 12:33

1 Answer 1


When I try your code, it highlights more than it does in your example, including the AND:s. Try to restart Emacs and see if anything you've done earlier caused this.

However, there are some remaining problems: You can't apply regexp-opt to regexp:s, it only work on plain strings, so the setup of MOD5-acmdb doesn't work. Unfortunately, it's not 100% clear what you are trying to match, but try write a plain regexp without using regexp-opt.

You can't highlight = if you pass words to regexp-opt, so I suggest that you match it separately from AND, OR and XOR. (The reason is that the resulting regexp contains \< and \> to match the start and end of a word, but = isn't a word character.)

Minor details:

  • Your regexp will match words inside symbols, as the AND in THIS_AND_THAT. This is easily fixed by passing symbols instead of words to regexp-opt.
  • You can use let to create temporary variables rather that setting them using setq and later setting them to nil.
  • In elisp strings, you will need two backslashes to yeild one. In other words, you need "\\[" to create the regexp snippet \[. ("\[" is the same as "[".)
  • If you would like to interactively debug your font-lock rules, you can use https://github.com/Lindydancer/font-lock-studio

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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