A small fix to syntax highlighting of PHP comments in emacs

If you're like me, you use emacs to edit a lot of PHP, and if you're like me, you sometimes run across code like this:

/* This is a comment
$foo = 'this is some commented out code'; # Bla bla bla

That's not so bad, really, but the php-mode that I had been using got confused and ended the comment at the newline after the hash mark. Argh. It took a little searching and a bit of trial and error, but eventually, I found some documentation of syntax tables within emacs. From what I can tell, emacs has a little lexer-like routine that goes character by character during syntax highlighting. The built-in functionality seems like it might have some shortcuts over what might be hidden inside your favorite compiler suite, but whatever.

Let's start with what I added inside of php-mode.el. I added this after the code that this comment ;; Specify that cc-mode recognize Javadoc comment style describes, but I have a feeling it could live just about anywhere within that code block.

;; This works for me in GNU Emacs 21.4.1 and 22.2.1 on CentOS and Ubuntu, respectively.

(modify-syntax-entry ?/ ". 124b" php-mode-syntax-table)
(modify-syntax-entry ?* ". 23" php-mode-syntax-table)
(modify-syntax-entry ?# "< b" php-mode-syntax-table)
(modify-syntax-entry ?\n "> b" php-mode-syntax-table)

Okay, what does that mean?

(modify-syntax-entry ?/ ". 124b" php-mode-syntax-table)

modify-syntax-entry is a command that takes two or three arguments, as follows:

  1. ?/: This is the character whose lexing you are modifying

  2. ". 124b": This is how you are defining that character.

    • . The dot means that the character, a slash, can be used as punctuation

    • " " The single space means that the character can be used as whitespace (I think). Things did not work unless I had the space in there. Your mileage may vary, and I would love an explanation.

    • 1 The slash character can be the first character in a pair of opening comment characters: // or /*

    • 2 It can also be the second character in a pair of opening comment characters: //

    • 4 It can be the second character in a pair of closing comment characters: */

    • b This signifies the when the slash is used as the second character in a comment character pair, it is part of the "b" class of comments, so it must be closed by a "b" closer. We have defined \n as a "b" closer (the only one?), so there you go. // gets closed by \n. You can write this stuff as regular expressions and functions, but it won't be fun. There might be a time when you want another set of comment openers and closers, or any arbitrary number, really, but as far as I can tell, you only get "a" and "b".

  3. php-mode-syntax-table : This argument is optional, but presumably you are only doing this for the table with which you are currently working.

?* ". 23" The star gets used as the second character in a comment opening pair /*, and the first character in a comment closing pair */. ?# "< b" This sets the hash mark as a comment beginner, whitespace, and part of the b-team. Roughly the same thing happens to the newline.

That's about it.

0 Responses to A small fix to syntax highlighting of PHP comments in emacs

  1. There are currently no comments.

Leave a Reply

About You