Emacs major mode for emerald. Correct highlighting


Long story short:

I am a student and in one of my courses we have to use/learn the emerald programming language. After a quick google search I found that somebody already wrote a major-mode for it. Link to the script

The problem: After the first comment line “%…”, syntax highlighting stops. So all subsequent lines are without any syntax highlighting. Which is extremely anoying.

Emerald doesn’t have multi line comments. Only one line comments are part of the language syntax. A one line comment starts with “%” and ends with the end of line.

I am a complete newb to the whole major-mode, writing lisp scripts for emacs…etc.

Question: Can anyone help me figure out what exacly goes wrong with the syntax highlighting ? Why does it stop after the first one line comment? And how to fix it ?

Other quick question:
How do I make emacs highlight the other language keywords ? For example object, export, operation, function…etc ?

Thanks in advance 🙂

Edit: Here is a simple hello world test program to demonstrate syntax highlighting :

%this object pronts a hello message on screen
const hello <- object hello
  var n : integer <-  0

  operation gs[] -> [ret: integer]
    n <- n + 1
    ret <- n
  end gs

  % a comment
  % second comment
  % third comment
    stdout.putstring["Hello World!\n"]
  end initially
end hello


The first question: You can single-step the font-lock keywords using the https://github.com/Lindydancer/font-lock-studio package. The problem could either be in one of the font-lock keywords, or in the syntax table (which is used to highlight comments and strings). If, after you start font-lock-studio (i.e. before any font-lock keywords are applied), the highlighting is incorrect, then the problem is with the syntax table.

The second question: You can call font-lock-add-keywords in a major mode hook to add new font-lock keywords. See the variable font-lock-keywords for a description on the format. However, the description is somewhat cryptic, so I would also recommend you to search the net for good examples.

Update: After installing emerald-mode I could replicate the problems that you saw. The root cause is that the mode implements core Emacs functions in a non-compatible way (the one provided with Emacs taked up to three arguments). If you delete the following function and restarts Emacs, font-locking appears to be working.

(defun looking-back (regexp)
  "Simulates a looking-at, but works backwards"
  (and (save-excursion
     (re-search-backward regexp (min (point-min) (- (point) 100)) t))
       (= (point) (match-end 0))))

Note that I could not have found this without font-lock-studio, as normal font-lock silently ignores errors whereas font-lock-studio enters the lisp debugger (if debug-on-error is non-nil).

Update 2: It appears that emerald-mode defines a set of font-lock keywords, but never adds them. If you add the following lines to your init file, it seems to work:

(defun my-emeral-mode-hook ()
  (setq font-lock-defaults '(emerald-font-lock-keywords nil t))
  (font-lock-mode 1))
(add-hook 'emerald-mode-hook 'my-emeral-mode-hook)

I noticed that emerald-mode is in a dire need of removation. For example, the major mode does not call kill-all-local-variables, which mean that it keeps all local variables from the previous major mode. (An even better solution would be to use define-derived-mode rather than defining the mode using a plain defun.) It sets font-lock-keywords-case-fold-search as a global variable and not via font-lock-defaults etc.

Answered By – Lindydancer

Answer Checked By – Marilyn (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.