From: Steve W. <sw...@pa...> - 2001-04-12 02:58:23
|
Here it is! :-) ~swain On Thu, 12 Apr 2001, Jon =C5slund wrote: > On Wed, Apr 11, 2001 at 05:35:52PM -0400, Steve Wainstead wrote: > > > > If there are no objections I'd like to reformat the code to comply with > > php-mode in Emacs. > > Where can I find this "php-mode"? :) Doesn't seem to be in FSF Emacs 20.7 > > -- > ___\ Jon =C5slund > > _______________________________________________ > Phpwiki-talk mailing list > Php...@li... > http://lists.sourceforge.net/lists/listinfo/phpwiki-talk > --- http://www.panix.com/~swain/ "Without music to decorate it, time is just a bunch of boring production deadlines or dates by which bills must be paid." -- Frank Zappa ;;; php-mode.el -- major mode for editing PHP source files ;; Author: Fred Yankowski <fc...@ac...> ;; Keywords: PHP, PHP3, languages ;; $Id: php-mode.el,v 1.24 2000/12/08 17:44:17 fred Exp $ ;; php-mode.el is Copyright (c) 1999,2000 by Fred Yankowski <fc...@ac...> ;; ;;=09This is free software; you can redistribute it and/or modify ;;=09it under the terms of the GNU General Public License as ;;=09published by the Free Software Foundation; either version 2, ;;=09or (at your option) any later version. ;; ;;=09This is distributed in the hope that it will be useful, but ;;=09WITHOUT ANY WARRANTY; without even the implied warranty of ;;=09MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;;=09GNU General Public License for more details. ;; ;;=09You should have received a copy of the GNU General Public ;;=09License as the file COPYING. If not, write to the Free ;;=09Software Foundation, Inc., 59 Temple Place - Suite 330, ;;=09Boston, MA 02111-1307, USA. ;;; Commentary; ;; ;; PHP mode is a major mode for editing the PHP programming language ;; <www.php.net>. It is mostly concerned with setting up syntax ;; coloring via the font-lock library. ;; ;; To use PHP mode, add this to your ~/.emacs file: ;; ;;=09(autoload 'php-mode "php-mode" "PHP editing mode" t) ;;=09(add-to-list 'auto-mode-alist '("\\.php3\\'" . php-mode)) ;; ;; Repeat the second line for any other filename suffixes that you ;; want to associate with PHP mode. Then, install this file in some ;; directory in your Emacs load-path and run byte-compile-file on it. ;; Voila'. ;; ;; If php-mode does not colorize the text of your PHP code, you may need ;; to tweak the supporting font-lock mode a bit. Here is more code for ;; .emacs that demonstrates one approach: ;; ;;=09(cond (window-system ;; (require 'font-lock) ;; (setq font-lock-support-mode 'lazy-lock-mode) ;; (setq font-lock-maximum-decoration t) ;; (global-font-lock-mode t) ;; )) ;; ;; The above configuration treats the entire file as being PHP code, ;; causing interspersed HTML code to be handled very poorly. An ;; option that provides very satisfying results is to use php-mode in ;; conjuction with the Multiple Major Modes package. Get that package ;; from mmm-mode.sourceforge.net, install it, and use something like ;; the following in your .emacs file to configure it: ;; ;;=09(require 'mmm-mode) ;;=09(setq mmm-global-mode 'maybe) ;;=09(mmm-add-mode-ext-class nil "\\.php3?\\'" 'html-php) ;;=09(mmm-add-classes ;;=09 '((html-php ;;=09 :submode php-mode ;;=09 :front "<\\?\\(php\\)?" ;;=09 :back "\\?>" ;;=09 ))) ;;=09(autoload 'php-mode "php-mode" "PHP editing mode" t) ;;=09(add-to-list 'auto-mode-alist '("\\.php3?\\'" . sgml-html-mode)) ;; ;; Note that .php files now have the PSGML/HTML mode as their major ;; mode and PHP mode as a submode applied by the MMM minor mode. You ;; can force a file to get PHP mode as a submode by starting the file ;; with a line like this: ;; ;;=09<?php // -*- mmm-classes: html-php -*- ;; ;; For files with HTML and PHP code that generates some of the ;; top-level elements of the HTML document, the following convinces ;; PSGML to treat the HTML content as if it were in the context of the ;; BODY element of an HTML document: ;; ;;=09<?php // -*- sgml-parent-document: ("dummy.html" "html" "body" ()) -*- ;; ;; This depends on having a dummy.html file that contains just the ;; DOCTYPE element for the desired HTML document type. See the PSGML ;; info file for more help. ;; ;; The font-coloring applied by the PSGML/HTML mode may collide with ;; the coloring applied by PHP mode. I got around this by removing ;; the list element for 'pi' in the sgml-markup-faces value. ;; Xemacs users report that regexp-opt is not defined. (eval-when-compile (unless (fboundp 'regexp-opt) (defun regexp-opt (strings paren) (let ((open-paren (if paren "\\(" "")) =09 (close-paren (if paren "\\)" ""))) =09(concat open-paren =09=09(mapconcat 'regexp-quote strings "\\|") =09=09close-paren))))) (defconst xemacsp (string-match "Lucid\\|XEmacs" emacs-version) "\ Non nil if using XEmacs.") (let* ((php-keywords =09(eval-when-compile =09 (regexp-opt =09 '("and" "break" "case" "continue" "default" "do" "echo" =09 "else" "elseif" "endfor" "endif" "endswitch" "endwhile" "exit" =09 "extends" "for" "global" "if" "include" =09 "or" "require" "return" "static" "switch" "then" =09 "var" "while" "xor") t))) ;; "class", "new" and "extends" get special treatment below (php-constants =09(eval-when-compile =09 (regexp-opt =09 '("false" "true" =09 "E_ERROR" "E_NOTICE" "E_PARSE" "E_WARNING" "E_ALL" =09 "PHP_OS" "PHP_VERSION" =09 "__LINE__" "__FILE__") t))) (php-types =09(eval-when-compile =09 (regexp-opt '("array" "bool" "char" "double" "float" "int" =09=09=09 "integer" "long" "mixed" "object" "real" =09=09=09 "string" "void") t))) ) (defconst php-font-lock-keywords-1 (list '("^[ \t]*\\(class\\)[ \t]*\\(\\sw+\\)?" (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t)) '("^[ \t]*\\(function\\)[ \t]*\\(\\sw+\\)?" (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t)) )) (defconst php-font-lock-keywords-2 (append php-font-lock-keywords-1 (list (concat "\\<\\(" php-keywords "\\)\\>") `(,(concat "\\<\\(" php-constants "\\)\\>") 1 font-lock-constant-face) ;; handle several words specially, to include following word, ;; thereby excluding it from unknown-symbol checks later '("\\<\\(new\\|extends\\)\\s-+\\$?\\(\\sw+\\)" (1 font-lock-keyword-face) (2 default)) ;; treat 'print' as keyword only when not used like a function name '("\\<print\\s-*(" . default) '("\\<print\\>" . font-lock-keyword-face) '("<\\?\\(php\\)?" . font-lock-constant-face) '("\\?>" . font-lock-constant-face) ))) (defconst php-font-lock-keywords-3 (append (list ;; warn about 'new FooBar()' -- empty parens are tempting but wrong '("\\<\\(new\\)\\s-+\\(\\sw+\\)\\((\\s-*)\\)" (1 font-lock-keyword-face) (2 default) (3 font-lock-warning-face)) ) php-font-lock-keywords-2 (list ;'("</?\\sw+[^>]*>" . font-lock-constant-face) ; <word> or </word> ;; warn about '$' immediately after -> '("\\$\\sw+->\\s-*\\(\\$\\)\\(\\sw+\\)" (1 font-lock-warning-face) (2 default)) ;; warn about $word.word -- it could be a valid concatenation, ;; but without any spaces we'll assume $word->word was meant. '("\\$\\sw+\\(\\.\\)\\sw" 1 font-lock-warning-face) ;; exclude casts from bare-word treatment `(,(concat "(\\(" php-types "\\))") 1 default) ;; Warn about bare symbols, those that don't follow '$' or precede ;; '('. But first explicitly mark some words that are OK. '("->\\s-*\\sw+" . default)=09; -->word '("\\$\\sw+" . default)=09=09; $word '("\\<\\sw+\\s-*[[(]" . default)=09; word( or word[ '("\\<[0-9]+" . default)=09=09; number (also matches word) '("\\<\\sw+\\>" . font-lock-warning-face) ;; Warn about =3D=3D> instead of =3D> (why do I *do* that?) '("=3D=3D+>" . font-lock-warning-face) )))) (defconst php-font-lock-syntactic-keywords (if xemacsp nil ;; Mark shell-style comments. font-lock handles this in a ;; separate pass from normal syntactic scanning (somehow), so we ;; get a chance to mark these in addition to C and C++ style ;; comments. This only works in GNU Emacs, not Xemacs 21 which ;; seems to ignore this same code if we try to use it. (list ;; Mark _all_ # chars as being comment-start. That will be ;; ignored when inside a quoted string. '("\\(\#\\)" (1 (11 . nil))) ;; Mark all newlines ending a line with # as being comment-end. ;; This causes a problem, premature end-of-comment, when '#' ;; appears inside a multiline C-style comment. Oh well. '("#.*\\([\n]\\)" (1 (12 . nil))) ))) (define-derived-mode php-mode c-mode "PHP" "A major mode for editing PHP source code. Key bindings: \\{php-mode-map}" (setq comment-start "// " =09comment-end "" =09comment-start-skip "// *") (defvar php-mode-syntax-table php-mode-syntax-table) (modify-syntax-entry ?_ "w" php-mode-syntax-table) ;; underscore considered part of word (modify-syntax-entry ?$ "." php-mode-syntax-table) ;; dollar-sign considered punctuation, not part of word (if xemacsp (progn =09=09(modify-syntax-entry ?# "< b" php-mode-syntax-table) =09=09(modify-syntax-entry ?\n "> b" php-mode-syntax-table))) ;; The above causes Xemacs to handle shell-style comments correctly, ;; but fails to work in GNU Emacs which fails to interpret \n as the ;; end of the comment. (make-local-variable 'font-lock-defaults) (setq font-lock-defaults =09'((php-font-lock-keywords-1 =09 php-font-lock-keywords-2 =09 ;; Comment-out the next line if the font-coloring is too =09 ;; extreme/ugly for you. =09 php-font-lock-keywords-3 =09 ) =09 nil=09=09=09=09; KEYWORDS-ONLY =09 T=09=09=09=09; CASE-FOLD =09 nil=09=09=09=09; SYNTAX-ALIST =09 nil=09=09=09=09; SYNTAX-BEGIN =09 (font-lock-syntactic-keywords . php-font-lock-syntactic-keywords))) (make-local-variable 'require-final-newline) (setq require-final-newline nil) (make-local-variable 'next-line-add-newlines) (setq next-line-add-newlines nil) ;; Will not force newline at end of file. Such newlines can cause ;; trouble if the PHP file is included in another file before calls ;; to header() or cookie(). ) (unless (boundp 'default) (defvar default 'default)) ;; Created "default" symbol for GNU Emacs so that both Xemacs and GNU ;; emacs can refer to the default face by a variable named "default". (unless (boundp 'font-lock-keyword-face) (copy-face 'bold 'font-lock-keyword-face)) ;; font-lock-keyword-face is sure to be valid now, assuming that the ;; bold face exists (unless (boundp 'font-lock-constant-face) (copy-face 'font-lock-keyword-face 'font-lock-constant-face)) ;; font-lock-constant-face now exists, which Xemacs doesn't seem to have ;; by default (provide 'php-mode) |