Maurex
Maurex

Reputation: 339

Derived mode sh-mode like macro variables

I am creating a major mode for some files I constantly use at work and am having some issues displaying macros in a different colour. The issue is that the macros occur in strings and they are highlighted as strings even though I set the $ character as the expression character. i.e. "$(macro1)" is highlighted like a string, not like a macro

In here I try and set the $ character as syntax for an expression, and also sort out the comment syntax as well:

(defvar test-syntax-table nil)
(defvar my-highlights nil)
(setq test-syntax-table
  (let ((synTable (make-syntax-table)))
    ;; bash style comment: “# …”
    (modify-syntax-entry ?# "< b" synTable)
    (modify-syntax-entry ?\n "> b" synTable)
    (modify-syntax-entry ?$ "'" synTable)
    synTable))
(setq my-highlights
  '(("record" . font-lock-function-name-face)
    ("field" . font-lock-keyword-face)
    ("$" . font-lock-variable-name-face)))

In here I set the syntax table and the highlights:

;;;###autoload
(define-derived-mode test-mode fundamental-mode
  (setq font-lock-defaults '(my-highlights))
  (set-syntax-table test-syntax-table)
  (setq comment-start "#")
  (visual-line-mode 0)
  (setq truncate-lines t)
  (setq mode-name "test"))

changes:

(defvar test-mode-syntax-table nil)
 (defvar my-highlights nil)
 (setq test-mode-syntax-table
       (let ((synTable (make-syntax-table)))
         ;; bash style comment: “# …”                                                                                                                                                                             
        (modify-syntax-entry ?# "< b" synTable)
        (modify-syntax-entry ?\n "> b" synTable)
        (modify-syntax-entry ?$ "'" synTable)
        synTable))
(setq my-highlights
  '(
    ("record" . font-lock-function-name-face)
    ("field" . font-lock-keyword-face)
    ("\\$" . (0 font-lock-variable-name-face override))
    ))



;;;###autoload                                                                                                                                                                                                   
(define-derived-mode test-mode fundamental-mode "test"
  (setq font-lock-defaults '(my-highlights))                                                                                                                                                                       
  (setq comment-start "#")
  (visual-line-mode 0)
  (setq truncate-lines t))

Upvotes: 0

Views: 65

Answers (1)

Stefan
Stefan

Reputation: 28571

You named your derived mode (setq font-lock-defaults '(my-highlights)). That's a rather quaint choice, I must say.

So, try to rename test-syntax-table to test-mode-syntax-table as god intended, then remove the set-syntax-table call since the renaming made it redundant, then remove the (setq mode-name "test") and add "test" between the parent mode argument and the setting of font-lock-defaults.

Then you can try and fix your $ highlighting by using something like ("\\$" (0 font-lock-variable-name-face override)) since $ matches the end of line rather than the $ character, and since by default faces are only applied where no other face was applied before (and strings/comments's faces are applied first).

Upvotes: 0

Related Questions