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-keywords '("SEQ_SKEL" "NEMG" "NSDN" "SSTEP" "STEP" "START" "SCU" "LAMP" "HORN" "PRINTER" "MODULE" "END MODULE" "CALL TEMPLATE" "IF" "FOR") )
(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
))
;;;###autoload
(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
- :ALM_SIP_PROB_123
- (:ALM_SIP_PROB_123)
- (&:ALM_SIP_PROB_123)
- 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
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)
font-lock-operator
face? I don't have that one here...(setq foo bar) ... (setq foo nil)
above could be advantageously replaced with(let ((foo bar)) ...)
.setq
is meant to modify a variable that is already defined elsewhere. The definition of a variable (likeMOD5-font-lock-keywords
above) should be done wither withdefvar
or withdefconst
.