I have a package which checks the face of a character before operating on it.

A problem with this is, the face might not yet have been calculated because of:
jit-lock-context-time, which delays contextual fontification.

How can I ensure a region of text has all it's font-faces calculated, including contextual fonts?

  • Why do you check the face of characters the buffer? Historically, I've done it as well, but got bitten so many times that I now avoid it at all costs. In most cases, you can use syntax-ppss instead. – Lindydancer Apr 17 at 9:43
  • I'd like to run a spell checker over comments, ignoring code or other symbols in the comments. – ideasman42 Apr 17 at 10:19
  • 1
    syntax-ppss returns a list that describes the context. Element 4 in this is non-nil when in a comment. This is a much more robust way than to check font-lock faces. (However, this does not solve the problem of ignoring other things within comments, like code.) – Lindydancer Apr 17 at 14:20

font-lock-ensure does exactly that. There's also font-lock-fontify-buffer for interactive use and older Emacsen that do not have font-lock-ensure yet, hence you'll often see the following construction:

(if (fboundp 'font-lock-ensure)

Note that it may be necessary to call font-lock-flush before font-lock-ensure to tell font-lock that a certain font-lock region is outdated.

| improve this answer | |
  • 1
    Maybe, it is worth noting that if one switches on some minor mode that adds/changes/removes some font-lock keywords one should run font-lock-flush before font-lock-ensure. – Tobias Apr 17 at 8:18
  • @Tobias this should be made part of the answer - since font-lock-ensure on it's own wasn't working. The issue with flushing is it causes re-fontification, I'd like just to do remaining fontification, instead of adding extra work. – ideasman42 Apr 17 at 8:21
  • font-lock-ensure does the remaining fontification and font-lock-flush is the big hammer when that doesn't work. I've edited the answer appropriately. – wasamasa Apr 17 at 9:28

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.