2

I am improving the C++ highlighting in Emacs with the minor mode: modern-cpp-font-lock.

Consider this following C++14 code:

int main() { int i = 1'2'3l; }

From cppreference.com

Optional single quotes(') may be inserted between the digits as a separator. They are ignored by the compiler.

In modern-cpp-font-lock, I set 1'2'3 to font-lock-constant-face and I set the integer suffix l to font-lock-keyword-face.

In fundamental-mode and modern-c++-font-lock-mode activated, I see the expected result. However, with c++-mode and modern-c++-font-lock-mode activated, '2' is highlighted as a string (l is correctly font locked).

I know the highlighting rely on the principle "first come first served" - meaning rules will not apply on words already highlighted.

Do you know if it is possible to override font-lock of a major mode? Or is it possible to change the order of rules to apply the font-lock (for example, longer rules first)?

1

Font-lock use two phases:

  • The syntactic phase. In this phase, Emacs determines things like strings and comments.
  • The keyword phase, i.e. the normal font-lock rules.

If you want to make Emacs ignore single quotes in digits, you have to do this in the first phase. Effectively, you will have to supply a custom syntax-propertize-function, which should set a suitable syntax-table text property on the quote character.

See this article on how to do this. Link broken, archived version.

Just remember that if you install your own syntax-propertize-function in a minor mode, you still have to ensure that the original function is executed.

Also, I've noticed that there is some work being done on modern C++ on the Emacs master branch, maybe this has already been handled there.

  • Yes there is some work done on the Emacs master branch. I send emails to the maintainer of cc-mode for bugs (single quotes in integers for example). However the release cycle on Emacs is too long (the fix in cc-mode won't be included in Emacs 25). That's why I provide this extended font-lock. – user12181 Jun 22 '16 at 9:20
  • Please include answer instead of links... which are now broken. – ideasman42 Aug 19 '17 at 2:54
  • @ideasman42, I agree that a full answer would be better. However, it would require quite some time to write such an answer -- time I currently don't have. Anyway, someone seems to have fixed the link, so I just leave it as it is for now. – Lindydancer Aug 19 '17 at 8:18

Your Answer

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