I am writing a major mode derived from f90-mode in order to fontify the syntax of a commonly used Fortran preprocessor, fypp (similar to jinja2 for HTML templating). Here's a contrived example of what that looks like, with plain f90-mode highlighting on the left and my fypp-mode on the right.
The #: directive and the ${...}$ evaluator are new syntax atop Fortran, which is why f90-mode does not color them properly. I want my major mode to inherit the fontifications defined in f90-mode.el and add to them. Here's a minimal but representative implementation
;;; fypp-mode.el
(setq fypp-highlights
'(("#:set" . font-lock-preprocessor-face)
("\\${\\|}\\$" . font-lock-preprocessor-face)))
(define-derived-mode fypp-mode f90-mode "fypp"
"Major mode for editing Fortran code with fypp extensions"
(font-lock-add-keywords nil fypp-highlights))
I have two complaints with this, seen in the right-side image above:
The
${...}$syntax in the subroutine name declaration breaks the fontification of the subroutine name.The
${...}$inside a string literal does not get fontified, but I would like it to.
Issue #1 I am able to solve by modifying the syntax table entries for { and } to make them word class instead of parentheses. I do wonder if this is likely to bite me later, though.
Issue #2 is the trickier matter for me. Normally, you want strings fontified as strings, even if they happen to contain keywords. Here, though, the string "Hello, ${whom}$!" will get expanded by the preprocessor to "Hello, world!", so I want the ${...}$ to be fontified. I still want string literals to be fontified, I just want my preprocessing tokens to "take precedence". But I don't know how to do this cleanly, if at all.
