I played around with the emacs php mode, and made a few fixes to what
on my system is  /usr/share/emacs/site-lisp/php-elisp/php-mode.el

I made the fixes against the latest version 1.4 from sourceforge, but
I think they would be easy to apply to the version 1.1 currently in
Debian unstable.

The fixes are:
1) Add support for the "clone" keyword, by copying the existing code
for the "print" keyword. Both these keywords get special treatment as
they can be used boat as "print 'lala'" and "print('lala')" (Debian
bug #342632)

2) Add support for highlighting the object name to the right of
"instanceof" keyword, as in "$student instanceof Person", by using the
existing logic for for example the "new" keyword. (Debian bug #342634)

3) Make sure that keywords, for example "if", don't become highlighed
in "$lala_if". This is done by inserting a few "[^_]"'s in the source
code. For start of line I had to insert "\\(?:^\\|[^_]\\)"
(non-underscore or start of line), otherwise emacs would not always
mark up keywords at the start of the line (specifically emacs would
mark up correctly if the keyword was there when the file was loaded,
but not when the keyword was freshly written). (Debian bug #212422)

Bug 3) looks somewhat like a bug which could be more elegantly fixed
at a higher level in emacs, but my band-aid fix could serve in the
mean-time. I tested using GNU Emacs 21.4.1.


Here is some PHP code which illustrates the highlighting bugs:

<?php
$aa_if;
$aa_if_aa;
$aa_break;
$aa_break_aa;
$aa_print_aa;
$aa_class_aa;
$aa_class;
$aa_self;
$a_private;
$a_continue; //this one is not fixed

function if_a() {}
function a_if() {}
function private_aa() {}
function continue_aa() {}

class Person {}
$aa instanceof Person;
clone $aa;
?>

Regards, Thue
--- lala/php-mode-1.4.0/php-mode.el	2008-01-04 01:49:18.000000000 +0100
+++ php-mode2.el	2008-01-26 23:02:38.000000000 +0100
@@ -913,7 +913,7 @@
        "extends" "for" "foreach" "global" "if" "include" "include_once"
        "next" "or" "require" "require_once" "return" "static" "switch"
        "then" "var" "while" "xor" "private" "throw" "catch" "try"
-       "instanceof" "catch all" "finally")))
+       "catch all" "finally")))
   "PHP keywords.")
 
 (defconst php-identifier
@@ -944,11 +944,11 @@
 
    ;; Fontify keywords
    (cons
-    (concat "\\<\\(" php-keywords "\\)\\>")
-    'font-lock-keyword-face)
+    (concat "\\(?:^\\|[^_]\\)\\<\\(" php-keywords "\\)\\>[^_]")
+    '(1 font-lock-keyword-face))
 
    ;; Fontify keywords and targets, and case default tags.
-   (list "\\<\\(break\\|case\\|continue\\)\\>[ \t]*\\(-?\\(?:\\sw\\|\\s_\\)+\\)?"
+   (list "\\(?:^\\|[^_]\\)\\<\\(break\\|case\\|continue\\)\\>[ \t]*\\(-?\\(?:\\sw\\|\\s_\\)+\\)?"
 	 '(1 font-lock-keyword-face) '(2 font-lock-constant-face t t))
    ;; This must come after the one for keywords and targets.
    '(":" ("^[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)[ \t]*:[ \t]*$"
@@ -957,7 +957,11 @@
 
    ;; treat 'print' as keyword only when not used like a function name
    '("\\<print\\s-*(" . php-default-face)
-   '("\\<print\\>" . font-lock-keyword-face)
+   '("\\(?:^\\|[^_]\\)\\<\\(print\\)\\>[^_]" (1 font-lock-keyword-face))
+
+   ;; treat 'clone' as keyword only when not used like a function name
+   '("\\<clone\\s-*(" . php-default-face)
+   '("\\(?:^\\|[^_]\\)\\<\\(clone\\)\\>[^_]" (1 font-lock-keyword-face))
 
    ;; Fontify PHP tag
    '("<\\?\\(php\\)?" . font-lock-constant-face)
@@ -976,13 +980,13 @@
    (list
 
     ;; class declaration
-    '("[^_]\\<\\(class\\|interface\\)[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)?"
+    '("\\(?:^\\|[^_]\\)\\<\\(class\\|interface\\)[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)?"
       (1 font-lock-keyword-face) (2 font-lock-type-face nil t))
     ;; handle several words specially, to include following word,
     ;; thereby excluding it from unknown-symbol checks later
     ;; FIX to handle implementing multiple
     ;; currently breaks on "class Foo implements Bar, Baz"
-    '("\\<\\(new\\|extends\\|implements\\)\\s-+\\$?\\(\\(?:\\sw\\|\\s_\\)+\\)"
+    '("\\(?:^\\|[^_]\\)\\<\\(new\\|extends\\|implements\\|instanceof\\)\\s-+\\$?\\(\\(?:\\sw\\|\\s_\\)+\\)"
       (1 font-lock-keyword-face) (2 font-lock-type-face))
 
     ;; function declaration
@@ -991,7 +995,7 @@
       (2 font-lock-function-name-face nil t))
 
     ;; class hierarchy
-    '("\\(self\\|parent\\)\\W" (1 font-lock-constant-face nil nil))
+    '("\\(?:^\\|[^_]\\)\\(self\\|parent\\)[^_\\w]" (1 font-lock-constant-face nil nil))
 
     ;; method and variable features
     '("\\<\\(private\\|protected\\|public\\)\\s-+\\$?\\(?:\\sw\\|\\s_\\)+"

Reply via email to