For strings and comments it's better to use syntactic fontification. The syntax table for a given major mode has to be defined in variable <major-mode-name>-syntax-table
.
This entry from the Emacs manual helps clarifying how Font Lock works: Font Lock mode finds text to highlight in two ways: through syntactic parsing based on the syntax table, and through searching (usually for regular expressions). Syntactic fontification happens first; it finds comments and string constants and highlights them. Search-based fontification happens second.
Now, there are a couple of problems with your code:
Your regexp is badly written. To escape characters within a regexp string you have to use \\
, so "\".*?\""
should be "\\".*?\\""
.
Don't put the code on a hook, just add (font-lock-add-keywords 'org-mode ...)
to your init file. Otherwise, whenever you enter Org mode you'll be adding that entry to font-lock-keywords
.
The fontification definition is badly writen. The number is used for groups, which you have none and so you should be using ("\\".*?\\"" 0 'font-lock-constant-face t)
. Read the documentation of font-lock-keywords
for further details. It's pretty helpful.
If you need to use more complex regexps take a look at package rx
.