I work with a Makefile-like infrastructure that allows having lists as variables and appending values to them. For this, the += operator is used.

However, I'm having incorrect highlighting using this operator.

+= highlighting

The + is being highlighted as part of the word. I tried doing (modify-syntax-entry ?+ "." my-syntax-table-based-on-makefile-mode-syntax-table) without getting the desired result (having the + in += highlighted the same as =).


The highlighting of assignments is not syntax controlled in makefile-mode. It is done via regular expressions inmakefile-font-lock-keywords.

The list makefile-font-lock-keywords is constructed by the function makefile-make-font-lock-keywords which uses the variable makefile-macroassign-regex for macro assignments.

If you change makefile-macroassign-regex you must also re-run makefile-make-font-lock-keywords to reconstruct the font-lock keyword lists you need.

Also makefile-imenu-generic-expression is constructed from makefile-macroassign-regex so you need to reconstruct the value of that variable as well.

I demonstrate that by a self-defined major mode derived from makefile-mode. The most important change in that major mode is the added +-sign in the character groups of makefile-macroassign-regex.

(define-derived-mode my-makefile-mode makefile-mode "myMake"
  "Makefiles with additional operator +=."
  (setq-local makefile-macroassign-regex
              "\\(?:^\\|^export\\|^override\\|:\\|:[ \t]*override\\)[ \t]*\\([^ \n\t+][^:#= \t\n+]*\\)[ \t]*\\(?:!=\\|[*:+]?[:?]?=\\)")
  (setq-local makefile-font-lock-keywords
  (setq-local makefile-imenu-generic-expression
              `(("Dependencies" makefile-previous-dependency 1)
                ("Macro Assignment" ,makefile-macroassign-regex 1))))
  • I cannot thank you enough for taking the time to explain such a complicated situation so clearly. Jun 6 '18 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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