help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Changing font-lock for combined HTML and PHP code??


From: Karl Hegbloom
Subject: Re: Changing font-lock for combined HTML and PHP code??
Date: Tue, 17 Apr 2007 12:49:15 -0600

On Mon, 2007-04-16 at 14:22 -0400, William Case wrote:
> I am working on web site made up of files that combine HTML and PHP.  My
> main interest is in the PHP coding.  Is there a relatively easy way to
> set the font-lock colors for the tags (constants) that distinguishes
> between a HTML tag and a PHP tag when I am in php-mode. 
> 
> In case I am misusing terminology, by tag I mean expressions like
> <p> ... </p> in HTML; <?php ... ?> in PHP.

The attached elisp file uses mmm-mode, psgml html mode, and php-mode
(which I obtained from Debian or via a google search) to make it do what
you want.  I have not used the php stuff in there very much, so it will
probably need a small amount of work.

;;; mmm-html-ext.el
;;;

(require 'css-mode)
(mmm-add-classes
 '((embedded-css
    :submode css-mode
    :face mmm-declaration-submode-face
    :front "<style[^>]*>"
    :back "</style>")))

(require 'generic-x)
(mmm-add-classes
 '((embedded-javascript
    :submode javascript-generic-mode
    :face mmm-code-submode-face
    :front 
"<script.*?\\(?:type\\|language\\)=\"\\(?:text/\\)?[Jj]ava[Ss]cript.*>"
    :back "</script>")))


;;; At the top of a mason component written in the style that I like
;;; to use is:
;;;
;;; <?xml version="1.0"?>
;;; <!DOCTYPE div PUBLIC "-//W3C//DTD XHTML 1.1//EN" "xhtml11.dtd">
;;;
;;; The thing is that those should not appear mid-document in the
;;; generated output.  It's computationaly cheaper to comment them off
;;; than to filter them out with a <%filter> section.  This next block
;;; of code sets things up so that the XML and DOCTYPE declarations
;;; are automatically commented off in the file saved to disk, but not
;;; commented off in the editor buffer, so that the normal mmm-mode
;;; and PSGML mode DTD parsing can take place.
;;;
;;; Copyright (c) 2005
;;; Karl Hegbloom <hegbloom@pdx.edu>
;;; GPL
;;;
(defun mason-comment-decls (arg)
  (interactive "p")
  (save-excursion
    (save-restriction
      (widen)
      (goto-char (point-min))
      (cond
       ((and (< arg 0) (looking-at "%#"))
        (progn
          (delete-char 2)
          (forward-line 1)
          (beginning-of-line)
          (delete-char 2)))
       ((and (>= arg 0) (not (looking-at "%#")))
        (progn
          (insert "%#")
          (forward-line 1)
          (beginning-of-line)
          (insert "%#")))))))

(defvar mason-comment-decls nil)
(make-variable-buffer-local 'mason-comment-decls)

(defun %mason-file-hook (arg)
  (when (and mmm-mode
             mason-comment-decls)
    (mason-comment-decls arg)))

(defun mason-find-file-hook ()
  (%mason-file-hook -1)
  (set-buffer-modified-p nil))

(defun mason-write-file-hook ()
  (%mason-file-hook 1))

(defun mason-after-save-hook ()
  (mason-find-file-hook))

(add-hook 'find-file-hooks #'mason-find-file-hook)
(add-hook 'write-file-hooks #'mason-write-file-hook)
(add-hook 'after-save-hook #'mason-after-save-hook)


(require 'php-mode)
(setq auto-mode-alist
      (remove-if #'(lambda (elt)
                     (eq (cdr elt) 'php-mode))
                 auto-mode-alist))
(let ((ext '("\\.php[345]?\\'" "\\.phtml\\'")))
  (while ext
    (add-to-list 'auto-mode-alist (cons (car ext) 'html-mode))
    (add-to-list 'mmm-mode-ext-classes-alist (list 'html-mode (car ext) 'php))
    (setq ext (cdr ext))))
(mmm-add-group
 'php
 '((php-xml-processing-directive
    :submode php-mode
    :face mmm-code-submode-face
    :front "<[?]php"
    :back "[?]>"
    :insert ((?p php nil @ "<?php " @ _ @ " ?>" @))
    )
   (php-inline=
    :submode php-mode
    :face mmm-output-submode-face
    :front "<[?]="
    :back "[?]>"
    :insert ((?= php nil @ "<?= " @ _ @ " ?>" @))
    )
   (php-inline
    :submode php-mode
    :face mmm-output-submode-face
    :front "<[?]"
    :back "[?]>"
    :insert ((?\? php nil @ "<? " @ _ @ " ?>" @))
    )
   (php-inline-asp=
    :submode php-mode
    :face mmm-output-submode-face
    :front "<%="
    :back "%>"
    )
   (php-inline-asp
    :submode php-mode
    :face mmm-output-submode-face
    :front "<%"
    :back "%>"
    )
   (php-script
    :submode php-mode
    :face mmm-code-submode-face
    :front "<script language=\"?php.*?>"
    :back "</script>"
    )
   ))

(mmm-add-group
 'HTML::Template
 '((html-template-tag
    :submode text-mode
    :face mmm-special-submode-face
    :front "<TMPL_"
    :include-front t
    :back ">"
    :include-back t
    :insert ((?t HTML::Template nil @ @ "<TMPL_" _ ">" @ @))
    )))

(provide 'mmm-html-ext)

reply via email to

[Prev in Thread] Current Thread [Next in Thread]