Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I get emacs to recognize single quotes as not being string begin/end tokens in font-lock mode

Tags:

emacs

elisp

I've a preprocessor (xhp) that allows me to write unquoted text in php code e.g.:

<foo>
    my enemies' base
</foo>

might appear in a .php file, but as soon as emacs sees that single quote it sees the entire rest of the file as being in a string.

  • I can't figure out where 'font-lock-syntactic-keywords' is getting set in (c-mode), but it has a syntax table associated with it that seems to cause this
  • (c-in-literal) returns 'string as well, so maybe I need to solve this deeper in the code than at the font-lock level, if anyone has any tips on this it would be appreciated

The simplest solution that I'd be happy with would be just assuming the string is one-line only.

like image 227
aaron Avatar asked May 05 '12 20:05

aaron


1 Answers

I don't know what major-mode you're using, but in general the trick is to change the syntax of the ' character with something like (modify-syntax-entry ?\' "." <syntaxtable>). Of course, if the ' character can sometimes delimit strings and sometimes not, then it's more tricky and you'll need to come up with a font-lock-syntactic-keywords (or syntax-propertize-function) rule which can tell which is used at any given point.

E.g. assuming PHP never treats ' as a string delimiter, something like the following might solve your problem:

(add-hook 'php-mode-hook
          (lambda () (modify-syntax-table ?\' ".")))
like image 195
Stefan Avatar answered Nov 09 '22 05:11

Stefan