Hello All,
Attached is another new version of jde-java-font-lock.el. It
works well with GNU Emacs 20.7, 21.0 and XEmacs 21.1.
Summary of changes since my previous post:
- Added comments to describe face usage :-)
- Added missing "strictfp" modifier.
- XEmacs only: `font-lock-builtin-face' and `font-lock-constant-face'
are now based on respectively `font-lock-preprocessor-face' and
`font-lock-reference-face' if available.
- Non official javadoc tag are fontified too.
- @see tag references (package, class, method, field) are fontified
with `jde-java-font-lock-code-face'.
Please try it :-)
Any feedback will be appreciated.
Sincerely,
David
;;; jde-java-font-lock.el -- Extra level font locking for java
;; Copyright (C) 1998, 1999, 2000, 2001 by David Ponce
;; Author: David Ponce <[EMAIL PROTECTED]>
;; Maintainer: David Ponce <[EMAIL PROTECTED]>
;; Paul Kinnucan <[EMAIL PROTECTED]>
;; Created: September 28 1998
;; Keywords: java, tools
;; VC: $Id: jde-java-font-lock.el,v 1.3 2000/12/18 05:22:45 paulk Exp $
;; This file is not part of Emacs
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;;
;; Adds some extra level font locking for java in `jde-mode'.
;;
;; * Numbers are fontified with `jde-java-font-lock-number-face'.
;;
;; * Modifiers are fontified with `font-lock-builtin-face'. This face
;; is based on XEmacs `font-lock-preprocessor-face' if available.
;;
;; * Capitalized identifiers, text between `' in comments and javadoc
;; tags (including non official javadoc tags) are fontified with
;; `font-lock-constant-face'. This face is based on XEmacs
;; `font-lock-reference-face' if available.
;;
;; * Javadoc links (following @link tags or enclosed in HTML <a> tags)
;; are fontified with `jde-java-font-lock-link-face'
;;
;; * Javadoc code samples (enclosed in HTML <code> tags or following
;; @see tags) are fontified with `jde-java-font-lock-code-face'. By
;; default, this face is based on `font-lock-builtin-face'.
;;
;; * Javadoc HTML bold style is fontified with
;; `jde-java-font-lock-bold-face'. By default, this face is based
;; on `bold'.
;;
;; * Javadoc HTML italic style is fontified with
;; `jde-java-font-lock-italic-face'. By default, this face is based
;; on `italic'.
;;
;; * Javadoc HTML underlined style is fontified with
;; `jde-java-font-lock-underline-face'. By default, this face is
;; based on `underline'.
;;
;; * Javadoc HTML preformatted style is fontified with
;; `jde-java-font-lock-pre-face'. By default, this face is based on
;; `default'.
;;
;; All font-lock and jde-java-font-lock faces are individually
;; customizable.
;; This code has been tested with GNU Emacs 20.7, 21.0 and XEmacs
;; 21.1. Any comments, suggestions, bug reports or upgrade requests
;; are welcome. Please send them to the maintainers.
;;; History:
;;
;; See at end of this file.
;;; Code:
(defcustom jde-use-font-lock t
"*Turn on font-locking if non-nil.
Set to nil to disable the use of font-locking."
:group 'jde-project
:type 'boolean)
;; Create a specific face for numbers
(defface jde-java-font-lock-number-face
'((((class grayscale) (background light)) (:foreground "DimGray" :italic t))
(((class grayscale) (background dark)) (:foreground "LightGray" :italic t))
(((class color) (background light)) (:foreground "RosyBrown"))
(((class color) (background dark)) (:foreground "LightSalmon"))
(t (:italic t)))
"Font Lock mode face used to highlight numbers."
:group 'font-lock-highlighting-faces)
;; Create a specific face for links
(defface jde-java-font-lock-link-face
'((t (:foreground "blue" :italic nil :underline t)))
"Font Lock mode face used to highlight links."
:group 'font-lock-highlighting-faces)
;;; Compatibility
(if jde-xemacsp
(progn
(defvar font-lock-builtin-face 'font-lock-builtin-face
"Face name to use for builtins.")
;; For consistency try to define the builtin face as the XEmacs
;; preprocessor face
(condition-case nil
(copy-face 'font-lock-preprocessor-face 'font-lock-builtin-face)
(error
(defface font-lock-builtin-face
'((t (:foreground "blue" :italic nil :underline t)))
"Font Lock mode face used to highlight builtins."
:group 'font-lock-highlighting-faces)))
(defvar font-lock-constant-face 'font-lock-constant-face
"Face name to use for constant and label names.")
;; For consistency try to define the constant face as the XEmacs
;; reference face
(condition-case nil
(copy-face 'font-lock-reference-face 'font-lock-constant-face)
(error
(defface font-lock-constant-face
'((((class grayscale) (background light))
(:foreground "LightGray" :bold t :underline t))
(((class grayscale) (background dark))
(:foreground "Gray50" :bold t :underline t))
(((class color) (background light)) (:foreground "CadetBlue"))
(((class color) (background dark)) (:foreground "Aquamarine"))
(t (:bold t :underline t)))
"Font Lock mode face used to highlight constants and labels."
:group 'font-lock-highlighting-faces)))
))
;; Make new faces based on existing ones
(copy-face 'bold 'jde-java-font-lock-bold-face)
(copy-face 'italic 'jde-java-font-lock-italic-face)
(copy-face 'underline 'jde-java-font-lock-underline-face)
(copy-face 'default 'jde-java-font-lock-pre-face)
(copy-face 'font-lock-builtin-face 'jde-java-font-lock-code-face)
;; Define the extra font lock faces
(defvar jde-java-font-lock-number-face 'jde-java-font-lock-number-face
"Face name to use for numbers.")
(defvar jde-java-font-lock-link-face 'jde-java-font-lock-link-face
"Face name to use for links.")
(defvar jde-java-font-lock-bold-face 'jde-java-font-lock-bold-face
"Face name to use for HTML bold text style.")
(defvar jde-java-font-lock-italic-face 'jde-java-font-lock-italic-face
"Face name to use for HTML italic text style.")
(defvar jde-java-font-lock-underline-face 'jde-java-font-lock-underline-face
"Face name to use for HTML underlined text style.")
(defvar jde-java-font-lock-pre-face 'jde-java-font-lock-pre-face
"Face name to use for HTML preformatted text style.")
(defvar jde-java-font-lock-code-face 'jde-java-font-lock-code-face
"Face name to use for HTML program code style.")
;; Setup extra java font lock keywords
(defconst java-font-lock-keywords-4 nil
"Extra level highlighting for JDE mode.")
(setq java-font-lock-keywords-4
(append
;; XEmacs feature scoping: These must come first or the
;; Modifiers and Packages from keywords-1 will catch them.
(list
;; Fontify modifiers.
(cons
(concat "\\<\\("
(eval-when-compile
(regexp-opt '("abstract" "const" "final" "synchronized"
"transient" "static" "volatile" "public"
"private" "protected" "native" "strictfp")))
"\\)\\>")
'font-lock-builtin-face)
;; Fontify capitalised identifiers as constant
'("\\b[A-Z_]+[A-Z0-9_]*\\b" . font-lock-constant-face)
;; Fontify package names in import and package directives.
'("\\<\\(import\\|package\\)\\>[ \t]*\\(\\sw+\\)?"
(1 font-lock-keyword-face)
(2 font-lock-constant-face nil t)
("\\=\\.\\(\\*\\|\\sw+\\)" nil nil
(1 font-lock-constant-face nil t)))
)
java-font-lock-keywords-3
(list
;; Fontify numbers
(cons
(concat "\\b\\(0[xX][0-9a-fA-F]+[lL]?\\|[0-9]+\\.?[0-9]*"
"\\([eE][-+]?[0-9]+\\)?\\([lL]\\|[fF]\\|[dD]\\)?\\)\\b")
'jde-java-font-lock-number-face)
(cons
(concat "\\b\\(\\.[0-9]+"
"\\([eE][-+]?[0-9]+\\)?\\([lL]\\|[fF]\\|[dD]\\)?\\)\\b")
'jde-java-font-lock-number-face)
;; Fontify text between `' in comments
'("`\\(.*\\)'"
1 font-lock-constant-face prepend)
;; Fontify javadoc tags within comments (including non
;; official ones)
'("^[ \t]*\\(/\\*\\*\\|\\*?\\)[ \t]*\\(@[^ \t]+\\)"
(2 font-lock-constant-face t))
;;;; The following fontify only official javadoc tags
;;;; (list
;;;; (concat
;;;; "@\\("
;;;; "author\\|deprecated\\|exception\\|param"
;;;; "\\|link\\|return\\|see\\|serial\\|serialData\\|serialField"
;;;; "\\|since\\|throws"
;;;; "\\|version"
;;;; "\\)\\>")
;;;; '(1 font-lock-constant-face t))
'("{\\(@docRoot\\)}"
1 font-lock-constant-face t)
'("{\\(@link\\)\\>[ \t]+\\([^}]*\\)}"
(1 font-lock-constant-face t)
(2 jde-java-font-lock-link-face t))
(list
(concat "^[ \t]*\\(/\\*\\*\\|\\*?\\)[ \t]*@see[ \t]+"
"\\([.#a-zA-Z0-9_$\300-\326\330-\366\370-\377]+\\)")
'(2 jde-java-font-lock-code-face t))
;; Basic HTML highlighting in javadoc comments
;; Fontify the text of a HREF anchor
'("<[Aa]\\s-+[Hh][Rr][Ee][Ff][^>]*>\\([^>]+\\)</[Aa]>"
1 jde-java-font-lock-link-face t)
;; Fontify <b>, <strong>, <i>, <u>, <code> and <pre> tags when
;; no tags inside
'("<[Ss][Tt][Rr][Oo][Nn][Gg]>\\([^<]*\\)</[Ss][Tt][Rr][Oo][Nn][Gg]>"
1 jde-java-font-lock-bold-face t)
'("<[Bb]>\\([^<]*\\)</[Bb]>"
1 jde-java-font-lock-bold-face t)
'("<[Ii]>\\([^<]*\\)</[Ii]>"
1 jde-java-font-lock-italic-face t)
'("<[Uu]>\\([^<]*\\)</[Uu]>"
1 jde-java-font-lock-underline-face t)
'("<[Cc][Oo][Dd][Ee]>\\([^<]*\\)</[Cc][Oo][Dd][Ee]>"
1 jde-java-font-lock-code-face t)
'("<[Pp][Rr][Ee]>\\([^<]*\\)</[Pp][Rr][Ee]>"
1 jde-java-font-lock-pre-face t))
))
;;; Compatibility
;; Setup JDE mode for font locking
(if jde-xemacsp
(progn
(put 'jde-mode 'font-lock-defaults
'((java-font-lock-keywords
java-font-lock-keywords-1
java-font-lock-keywords-2
java-font-lock-keywords-3
java-font-lock-keywords-4)
nil nil ((?_ . "w")) beginning-of-defun
(font-lock-mark-block-function . mark-defun))))
(progn
(font-lock-add-keywords 'jde-mode java-font-lock-keywords)
(font-lock-add-keywords 'jde-mode java-font-lock-keywords-1)
(font-lock-add-keywords 'jde-mode java-font-lock-keywords-2)
(font-lock-add-keywords 'jde-mode java-font-lock-keywords-3)
(font-lock-add-keywords 'jde-mode java-font-lock-keywords-4)))
(defun jde-setup-syntax-coloring()
"Set up JDE mode syntax coloring."
(cond (window-system
;; If not XEmacs 20.1 turn on font lock.
;; (XEmacs 21 has font-lock on by default.)
(if (or
(not jde-xemacsp)
(not
(and
(eq emacs-major-version 21)
(eq emacs-minor-version 0))))
(turn-on-font-lock))
(setq font-lock-maximum-decoration t)
(if (not jde-xemacsp)
(global-font-lock-mode 1 t))
)))
(provide 'jde-java-font-lock)
;;; Change History:
;;
;; $Log: jde-java-font-lock.el,v $
;; Revision 1.3 2000/12/18 05:22:45 paulk
;; *** empty log message ***
;;
;; Revision 1.2 2000/10/10 06:41:47 paulk
;; Fixed some XEmacs compatibility problems.
;;
;; Revision 1.1 2000/10/08 12:53:22 paulk
;; Initial revision.
;;
;;; jde-java-font-lock.el ends here