Following my previous question on making custom highlight rules here: Color-code a new generic character combination , I have stumbled on a new issue, that possibly merits another question.

I have a group of three rules that I would like to apply (code below). The first one works, the other two (also when alone) seem to break down something in coloring. Under each Org-mode heading, where these particular instances are found, the headline and its subheadings lose coloring. The instances themselves are not highlighted in any way.

The regular expressions are tested with the inbuilt regexp-builder. Example instances would be:

_Theorem 1:

(Author & Author, 2012)


While for the first rule, the system seems to work, for the other two rules, it instead breaks down the heading coloring.

(add-hook 'org-mode-hook
          (lambda ()
                (font-lock-add-keywords nil
                                    "\\(^\\|\\s-+\\)\\(_\\([a-zA-Z0-9]+\\s-?\\)\\{0,4\\}:\\)" 2
                                    font-lock-type-face t)))
                (font-lock-add-keywords nil
                                    "(\\w+\\(\\s-&\\s-\\w+\\)?,\\s-[0-9]\\{4\\})" 2
                                    font-lock-string-face t)))
                (font-lock-add-keywords nil
                                    "^\\w+:" 2
                                       font-lock-constant-face t)))

Unfortunately I am new enough to Emacs that I don't know where exactly the problem should lie. Many thanks if you can point it out, or provide some advice.

  • 3
    Your first regex would not match "Theorem 1:" because there's an underscore in it. Your second expression seems fine, but you're telling the font-lock engine to highlight sub-expression 2, but there is only one capture group in the regex. Your third expression has the same problem-- there are no capture parentheses in the regex, but you're telling it to highlight sub-expression 2. Does that help? Feb 13, 2015 at 18:50
  • 2
    @SueD.Nymme is absolutely correct. Font-lock stops when if finds an error. Unfortunately, this is hard to track down. One way to do this is to single step your rules in github.com/Lindydancer/font-lock-studio which will tell you about such problems. Feb 13, 2015 at 20:56
  • That looks like quite an impressive package, @Lindydancer. I must check it out. Thanks! Feb 13, 2015 at 21:36
  • 3
    take out the t from your keywords, what that t specifies is that font-lock should override already colored things. Feb 13, 2015 at 22:10


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.