Commits

steveb  committed d5834cd

Synch with font-latex.el-0.508, sync bib-cite.el

  • Participants
  • Parent commits e7a6f32

Comments (0)

Files changed (3)

 
 # remove the MULE_ELCS line for building without Mule.
 
-VERSION = 1.04
+VERSION = 1.05
 AUTHOR_VERSION = 9.7p
 MAINTAINER = XEmacs Development Team <xemacs-beta@xemacs.org>
 PACKAGE = auctex
 
 ;; Copyright (C) 1994, 1995, 1996, 1997 Peter S. Galbraith
  
-;; Author:    Peter S. Galbraith <rhogee@bathybius.meteo.mcgill.ca>
+;; Author:    Peter S. Galbraith <galbraith@mixing.qc.dfo.ca>
 ;; Created:   06 July 1994
-;; Version:   2.28 (22 January 97)
+;; Version:   3.07 (20 Nov 97)
 ;; Keywords:  bibtex, cite, auctex, emacs, xemacs
 
 ;; RCS $Id$
 ;; LCD Archive Entry:
 ;; bib-cite|Peter Galbraith|galbraith@mixing.qc.dfo.ca|
 ;; Display \cite, \ref or \label / Extract refs from BiBTeX file.|
-;; 22-Jan-1997|2.28|~/misc/bib-cite.el.gz|
+;; 21-May-1997|3.01|~/misc/bib-cite.el.gz|
 
 ;; ----------------------------------------------------------------------------
 ;;; Commentary:
+;; This minor-mode is used in various TeX modes to display or edit references
+;; associated with \cite commands, or matching \ref and \label commands.
 
 ;; New versions of this package (if they exist) may be found at:
 ;;   ftp://ftp.phys.ocean.dal.ca/users/rhogee/elisp/bib-cite.el
 ;;  is the auc-tex file!).  Make sure you replace that file, or rename it,
 ;;  or delete it!!!
 
+;; reftex users:
+;;  reftex is a package with similar functions to bib-cite.
+;;    ftp://strw.leidenuniv.nl/pub/dominik/reftex.el
+;;  I suggest that you use reftex to help you type-in text as it's functions
+;;  are better suited to this task than bib-cite, and use bib-cite's features
+;;  when you proof-read the text.
+;;  If you wish bib-cite to use reftex's reftex-view-crossref command to
+;;  display and find \label's and \cite bibliography entries, set the variable
+;;  bib-cite-use-reftex-view-crossref to t.
+
 ;; MS-DOS USERS:
 ;;  Multifile documents are supported by bib-cite by using etags (TAGS files)
 ;;  which contains a bug for MSDOS (at least for emacs 19.27 it does).
 ;;
 ;;    For multi-file documents, you must be using auctex (so that bib-cite can
 ;;    find the master file) and all \input and \include commands must be first
-;;    on a line (not preceded by any non-white text).
+;;    on a line (not preceeded by any non-white text).
 ;;
 ;;  imenu support  (Suggested key binding: Shift-Mouse-3)  
 ;;
  
 ;; Installation instructions:
 ;; ~~~~~~~~~~~~~~~~~~~~~~~~~
-;;  If you use a menued environment (e.g. X Window System), bib-cite must be 
-;;  loaded *after* your LaTeX-mode menus are created in order to bypass an
-;;  annoying bug in bib-cite.  This is done by loading bib-cite via a 
-;;  mode-hook:
-;;  - If you are using AUC-TeX (http://sunsite.auc.dk/auctex/), add the 
-;;    following lines to your ~/.emacs file:
+;;  bib-cite is a minor-mode, so you could invoke it in a LaTeX-mode hook.
+;;  e.g. If you are using AUC-TeX (http://sunsite.auc.dk/auctex/), you could
+;;  use:
 ;; 
-;;   (defun my-LaTeX-mode-hook ()
-;;     (require 'bib-cite))
-;;   (add-hook 'LaTeX-mode-hook 'my-LaTeX-mode-hook)
+;;   (autoload 'turn-on-bib-cite "bib-cite")
+;;   (add-hook 'LaTeX-mode-hook 'turn-on-bib-cite)
 ;; 
-;;  - If you are using Emacs' regulare LaTeX-mode, use instead:
+;;  If you are using Emacs' regular LaTeX-mode, use instead:
+;;
+;;   (autoload 'turn-on-bib-cite "bib-cite")
+;;   (add-hook 'latex-mode-hook 'turn-on-bib-cite)
 ;; 
-;;   (defun my-LaTeX-mode-hook ()
-;;     (require 'bib-cite))
-;;   (add-hook 'latex-mode-hook 'my-TeX-mode-hook)
-;; 
-;;  If you do not use a windowed environment, all you need to do is add this 
-;;  line to your .emacs file:
-;;
-;;    (require 'bib-cite)
-;;
 ;;  bib-cite can be used with auctex, or stand-alone.  If used with auctex on a
 ;;  multi-file document (and auctex's parsing is used), then all \bibliography
 ;;  commands in the document will be found and used.
 ;;
 ;;    (setq bib-novice nil)
 ;;  ---
-;;  By default, bib-cite adds a menu-bar pull-down menu under a separate name.
-;;  Under emacs' tex-mode and auctex's latex-mode, it can be placed within
-;;  the existing menu if you set the following:
+;;  If you wish bib-cite to use reftex's reftex-view-crossref command to
+;;  display and find \label's and \cite bibliography entries, set the variable
+;;  bib-cite-use-reftex-view-crossref to t:
 ;;
-;;    (setq bib-cite-put-menu-separately nil)
+;;    (setq bib-cite-use-reftex-view-crossref t)
+;;  ---
+;;  The following variable determines whether we will attempt to highlight
+;;  citation, ref and label commands in green when they are under the
+;;  mouse.  When highlighted, the mouse keys work to call bib-display 
+;;  (bound to [mouse-3]) and bib-find (bound to [mouse-2]).  If you use a 
+;;  mode other than LaTeX-mode, you'll want to call bib-highlight-mouse with 
+;;  a hook (See how we do this at the end of this file with the add-hook 
+;;  command).
 ;;
-;;  This variable has no effect under XEmacs.  Should I change this?
+;;    (setq bib-highlight-mouse-t nil)
 ;;  ---
 ;;  The imenu features will be disabled if you set this variable to nil
 ;;
 ;;  You may use `switch-to-buffer' `switch-to-buffer-other-window' or
 ;;  `switch-to-buffer-other-frame'.
 ;;  ---
-;;  The following variable determines whether we will attempt to highlight
-;;  citation, ref and label commands in green when they are under the
-;;  mouse.  When highlighted, the mouse keys work to call bib-display 
-;;  (bound to [mouse-3]) and bib-find (bound to [mouse-2]).  If you use a 
-;;  mode other than LaTeX-mode, you'll want to call bib-highlight-mouse with 
-;;  a hook (See how we do this at the end of this file with the add-hook 
-;;  command).
-;;
-;;    (setq bib-highlight-mouse-t nil)
-;;  ---
 ;;  If you use DOS or OS/2, you may have to set the following variable:
 ;;
 ;;    (setq bib-dos-or-os2-variable t)
 
 ;; ----------------------------------------------------------------------------
 ;;; Change log:
+;; V3.07 Nov 20 97 - Christoph Wedler <wedler@fmi.uni-passau.de>  (RCS V1.24)
+;;    bib-ext-list variable made permanent-local, otherwise VC registration 
+;;    would use two extents for each reference etc. This was not a visible bug.
+;; V3.06 Nov 12 97 - PSG (RCS V1.23)
+;;    Support use of reftex's reftex-view-crossref command.
+;; V3.05 Nov 12 97 - Vladimir Alexiev <vladimir@cs.ualberta.ca> (RCS V1.22)
+;;    regexp-quote the bibliography keys so a key like galbraith+kelley97 works
+;; V3.04 Aug 25 97 - Christoph Wedler  <wedler@fmi.uni-passau.de> (RCS V1.20)
+;;    (bib-highlight-mouse): Would bug out on detached extents, 
+;;    e.g. when killing a whole citation.
+;; V3.03 Jul 16 97 - Christoph Wedler  <wedler@fmi.uni-passau.de> (RCS V1.18)
+;;    turn-on-bib-cite back to non-interactive.
+;; V3.02 Jul 11 97 - Christoph Wedler  <wedler@fmi.uni-passau.de> (RCS V1.17)
+;;    * auctex/bib-cite.el (turn-on-bib-cite): Make interactive.
+;;    Argument to `bib-cite-minor-mode' is 1.
+;;    (bib-label-help-echo-format): New variable.
+;;    (bib-label-help-echo): New function.
+;;    (bib-label-help): Addition argument format.
+;;    (bib-highlight-mouse): Set extent property `help-echo' for XEmacs.
+;; V3.01 May 22 97 - Diego Calvanese <calvanes@dis.uniroma1.it> (RCS V1.16)
+;;    bib-make-bibliography handles commas separated citations in aux files.
+;; V3.00 May 16 97 - Peter Galbraith (RCS V1.15)
+;;    bib-cite is now a minor mode.
+;; V2.32 Apr 30 97 - Anders Stenman <stenman@isy.liu.se> (RCS V1.14)
+;;  - Support for balloon-help.
+;; V2.31 Mar 20 97 - Christoph Wedler <wedler@fmi.uni-passau.de> (RCS V1.12)
+;;  - Better fontification of help buffer as bibtex or latex for XEmacs.
+;; V2.30 Feb 10 97 - Peter Galbraith (RCS V1.11)
+;;  - Better fontification of help buffer as bibtex or latex.
+;; V2.29 Jan 29 97 - Peter Galbraith (RCS V1.10)
+;;  - imenu looks for `\label{stuff}' instead of `\label'
 ;; V2.28 Jan 22 97 - Peter Galbraith (RCS V1.9)
 ;;  - Bug in bib-create-auto-file.
 ;; V2.27 Dec 31 96 - Peter Galbraith (RCS V1.8)
 ;;   - Add key def for bibtex-mode to create auc-tex's parsing file.
 ;;   - Fix bugs found by <thompson@loon.econ.wisc.edu>
 ;;     - fix bib-get-citation for options 
-;;     - fix bib-get-citation for commas preceded citation command
+;;     - fix bib-get-citation for commas preceeded citation command
 ;;     - better regexp for citations and their keys.
 ;;     - Added @string support for any entry (not just journal entries).
 ;;       (I had to disallow numbers in @string keys because of years.  
 
 ;;>>>>>>User-Modifiable variables start here:
 
+(defvar bib-cite-use-reftex-view-crossref nil
+  "*Non-nil means, RefTeX will be used to find cross references.
+When this variable is non-nil, both `bib-find' and `bib-display' will
+call a function in RefTeX do find or display the cross reference of a
+\\ref or \\cite macro at point.")
+
 (defvar bib-novice t
   "*Give advice to novice users about what commands to use next.")
 
 (defvar bib-highlight-mouse-t t
   "*Call bib-highlight-mouse from LaTeX-mode-hook to add green highlight.")
 
+(defvar bib-label-help-echo-format "button2 finds %s, button3 displays %s"
+  "*Format string for info if the mouse is over LaTeX commands.
+If nil, do not display info.")
+
 (defvar bib-bibtex-env-variable "BIBINPUTS"
   "*Environment variable setting the path where BiBTeX input files are found.
 BiBTeX 0.99b manual says this should be TEXBIB.
 
 ;;<<<<<<User-Modifiable variables end here.
 
+
+(defvar bib-cite-is-XEmacs
+  (not (null (save-match-data (string-match "XEmacs\\|Lucid" emacs-version)))))
+
+(defvar bib-cite-minor-mode nil)
+
+;;;###autoload
+(defun bib-cite-minor-mode (arg)
+  "Toggle bib-cite mode.
+When bib-cite mode is enabled, citations, labels and refs are highlighted 
+when the mouse is over them.  Clicking on these highlights with [mouse-2] 
+runs bib-find, and [mouse-3] runs bib-display."
+  (interactive "P")
+  (set (make-local-variable 'bib-cite-minor-mode)
+       (if arg
+	   (> (prefix-numeric-value arg) 0)
+	 (not bib-cite-minor-mode)))
+  (cond 
+   (bib-cite-minor-mode                 ;Setup the minor-mode
+    ;; Christoph Wedler's <wedler@fmi.uni-passau.de> suggestion for xemacs
+    ;; Added for version 2.19
+    (if (boundp 'tags-always-exact)
+        (progn
+          (make-local-variable 'tags-always-exact)
+          (setq tags-always-exact nil)))
+    ;; imenu support
+    (if bib-use-imenu
+        (progn
+          ;; User who *never* uses multi-file documents could change this to:
+          ;;                              'imenu--create-LaTeX-index-for-buffer
+          (setq imenu-create-index-function 'imenu--create-LaTeX-index)
+          ;; Make sure that imenu-sort-function is nil
+          (and (boundp 'imenu-sort-function)
+               imenu-sort-function
+               (make-local-variable 'imenu-sort-function)
+               (setq imenu-sort-function nil))))
+    ;; mouse overlay        
+    (if bib-highlight-mouse-t
+        (progn
+          (bib-cite-setup-highlight-mouse-keymap)
+          (bib-highlight-mouse)
+          (make-local-hook 'after-change-functions)
+          (add-hook 'after-change-functions 
+                    'bib-cite-setup-mouse-function nil t)))
+    (if bib-cite-is-XEmacs
+	(progn
+	  (or (local-variable-p 'current-menubar (current-buffer))
+	      (set-buffer-menubar current-menubar))
+	  (add-submenu nil bib-cite-minor-mode-menu))))
+   (t
+   ;;;Undo the minor-mode
+    ;; mouse overlay        
+    (cond 
+     (bib-cite-is-XEmacs
+      (while bib-ext-list
+        (delete-extent (car bib-ext-list))
+        (setq bib-ext-list (cdr bib-ext-list))))
+     (t
+      (remove-hook 'after-change-functions 'bib-cite-setup-mouse-function t)
+      (let ((before-change-functions) (after-change-functions))
+        ;; FIXME This detroys all mouse-faces and local-maps!
+        ;; FIXME Hope no other package is using them in this buffer!
+        (remove-text-properties (point-min) (point-max)
+                                '(mouse-face t local-map t)))))
+    (if bib-cite-is-XEmacs
+        (delete-menu-item '("BCite"))))))
+
+;;This must be eval'ed when the LaTeX mode is in use.
+;; bib-highlight-mouse-keymap is a local variable so each buffer can have it's
+;; own.
+(defun bib-cite-setup-highlight-mouse-keymap ()
+  (make-local-variable 'bib-highlight-mouse-keymap)
+  (setq bib-highlight-mouse-keymap
+   ;;; First, copy the local keymap so we don't have `disappearing' menus
+   ;;; when the mouse is moved over a \ref, \label or \cite command.
+        
+   ;;; FIXME: Check out (mouse-major-mode-menu) to see how it grabs the local
+   ;;;        menus to display.  Maybe on `highlighted' commands we could only
+   ;;;        display the bib-cite stuff (or a subset of it).
+        (let ((m (copy-keymap (current-local-map))))
+          (cond
+           (bib-cite-is-XEmacs
+            (set-keymap-name m 'bib-highlight-mouse-keymap)
+            (cond
+             ;;action-key stuff from Vladimir Alexiev <vladimir@cs.ualberta.ca>
+             ((commandp 'action-key)
+              ;; for hyperbole. The Right Way is to define implicit buttons
+              ;; (defib) bib-cite and label-ref instead of overriding
+              ;; action-key and assist key, so that eg smart key help can
+              ;; be obtained, but I'm lazy.
+              (substitute-key-definition 'action-key 'bib-find m global-map)
+              (substitute-key-definition 'assist-key 'bib-display m global-map)
+              (substitute-key-definition 'action-key-depress 
+                                         'bib-find-mouse m global-map)
+              (substitute-key-definition 'assist-key-depress 
+                                         'bib-display-mouse m global-map)
+              (substitute-key-definition 'action-mouse-key nil m global-map)
+              (substitute-key-definition 'assist-mouse-key nil m global-map))
+             (t                               ; xemacs, not hyperbole
+              (define-key m "\e\r" 'bib-find-mouse) ;   bug Fixed in V2.17
+              (define-key m "\e\n" 'bib-display-mouse) ;bug Fixed in V2.17
+              ;;(define-key m [(shift button1)] 'bib-display-mouse)
+              (define-key m [button3] 'bib-display-mouse)
+              (define-key m [button2] 'bib-find-mouse))))
+            (t                                 ; emacs 19
+             (cond
+              ((commandp 'action-key)
+               (substitute-key-definition 'action-key 'bib-find m global-map)
+               (substitute-key-definition 'assist-key 'bib-display m global-map)
+               (substitute-key-definition 'action-mouse-key-emacs19 
+                                          'bib-find-mouse m global-map)
+               (substitute-key-definition 'assist-mouse-key-emacs19 
+                                          'bib-display-mouse m global-map)
+               (substitute-key-definition 'action-key-depress-emacs19 
+                                          nil m global-map)
+               (substitute-key-definition 'assist-key-depress-emacs19 
+                                          nil m global-map))
+              (t                               ; emacs 19, not hyperbole
+               (define-key m [down-mouse-3] 'bib-display-mouse)
+               (define-key m [mouse-2] 'bib-find-mouse)))))
+          m)))
+
+;;;###autoload
+(defun turn-on-bib-cite ()
+  "Unconditionally turn on Bib Cite mode."
+  (bib-cite-minor-mode 1))
+
+(defun bib-cite-setup-mouse-function (beg end old-len)
+  (save-excursion
+    (save-match-data
+      (save-restriction
+        (narrow-to-region
+         (progn (goto-char beg) (beginning-of-line) (point))
+         (progn (goto-char end) (forward-line 1) (point)))
+        (bib-highlight-mouse)))))
+
+(defvar bib-cite-minor-mode-map
+  (let ((map (make-sparse-keymap)))
+    (define-key map "\C-cba" 'bib-apropos)
+    (define-key map "\C-cbb" 'bib-make-bibliography)
+    (define-key map "\C-cbd" 'bib-display)
+    (define-key map "\C-cbe" 'bib-etags)
+    (define-key map "\C-cbf" 'bib-find)
+    (define-key map "\C-cbh" 'bib-highlight-mouse)
+    map)
+  "bib-cite minor-mode keymap")
+
+(easy-menu-define 
+ bib-cite-minor-mode-menu bib-cite-minor-mode-map "Menu keymap for bib-cite."
+ '("BCite"
+   ["Make BibTeX bibliography buffer" bib-make-bibliography t]
+   ["Display citation or matching \\ref or \\label" bib-display t]
+   ["Find BibTeX citation or matching \\ref or \\label" bib-find t]
+   ["Search apropos BibTeX files" bib-apropos t]
+   ["Build TAGS file for multi-file document" bib-etags (bib-master-file)]
+   ["Refresh \\cite, \\ref and \\label mouse highlight"
+    bib-highlight-mouse t]))
+
+;; Install ourselves:
+(or (assq 'bib-cite-minor-mode minor-mode-alist)
+    (setq minor-mode-alist 
+          (cons '(bib-cite-minor-mode " BCite") minor-mode-alist)))
+(or (assq 'bib-cite-minor-mode minor-mode-map-alist)
+    (setq minor-mode-map-alist 
+          (cons (cons 'bib-cite-minor-mode bib-cite-minor-mode-map) 
+                minor-mode-map-alist)))
+  
+
+;;; Add a menu entry to bibtex.el (Perhaps I should not do this).
+(cond
+ ((and (string-match "XEmacs\\|Lucid" emacs-version)
+       (or window-system
+           (fboundp 'smart-menu)))      ;text menus by Bob Weiner
+  ;;
+  ;; xemacs under X with auc-tex
+  ;;
+
+  ;; Add to bibtex.el's popup menu
+  (defvar bib-cite-xemacs-bibtex-mode-menu
+    '("---"
+      "Bib-Cite"
+      "---"
+      ["Search apropos BibTeX files" bib-apropos t]
+      ["Create auc-tex auto parsing file" bib-create-auto-file t])
+    "Submenu of bibtex-mode menu, used by bib-cite.")
+  
+  (if (boundp 'bibtex-menu)
+      ;; Add menu now
+      (setq bibtex-menu
+	    (append
+	     bibtex-menu
+	     bib-cite-xemacs-bibtex-mode-menu))
+    ;; Setup to add menu later
+    (defun bib-cite-bibtex-mode-hook ()
+      (if (boundp 'bibtex-menu)
+          (progn
+            (setq bibtex-menu
+                  (append
+                   bibtex-menu
+                   bib-cite-xemacs-bibtex-mode-menu))
+            (remove-hook 'bibtex-mode-hook 'bib-cite-bibtex-mode-hook))))
+    (add-hook 'bibtex-mode-hook 'bib-cite-bibtex-mode-hook))
+  )
+
+ ((and (not (string-match "XEmacs\\|Lucid" emacs-version))
+       (string-equal "19" (substring emacs-version 0 2))
+       (or window-system
+           (fboundp 'tmm-menubar)))     ; 19.30 - Will autoload if necessary 
+  ;;
+  ;; emacs-19 under X-windows (or non-X with tmm)
+  ;;
+  
+  ;; This *almost* makes me want to switch over to XEmacs...
+
+  ;; to auc-tex auto file for a bibtex buffer
+  (eval-after-load 
+   "bibtex"
+   '(progn
+      (cond
+       ((lookup-key bibtex-mode-map [menu-bar move/edit])
+        (define-key-after 
+          (lookup-key bibtex-mode-map [menu-bar move/edit]) 
+          [bib-nil] '("---" . nil) '"--")
+        (define-key-after 
+          (lookup-key bibtex-mode-map [menu-bar move/edit]) 
+          [bib-apropos] '("Search Apropos" . bib-apropos) 'bib-nil)
+        (define-key-after 
+          (lookup-key bibtex-mode-map [menu-bar move/edit]) 
+          [auc-tex-parse] 
+          '("Create auc-tex auto parsing file" . bib-create-auto-file)
+          'bib-apropos))
+       ((lookup-key bibtex-mode-map [menu-bar bibtex-edit])
+        (define-key-after 
+          (lookup-key bibtex-mode-map [menu-bar bibtex-edit]) 
+          [bib-nil] '("---" . nil) '"--")
+        (define-key-after 
+          (lookup-key bibtex-mode-map [menu-bar bibtex-edit]) 
+          [bib-apropos] '("Search Apropos" . bib-apropos) 'bib-nil)
+        (define-key-after 
+          (lookup-key bibtex-mode-map [menu-bar bibtex-edit]) 
+          [auc-tex-parse] 
+          '("Create auc-tex auto parsing file" . bib-create-auto-file)
+          'bib-apropos)))))))
+
 ;; Following from bibtex.el
 (defvar 
   bib-cite-bibtex-font-lock-keywords
 (defvar bib-ext-list nil 
   "xemacs buffer-local list of bib-cite extents.")
 (make-variable-buffer-local 'bib-ext-list)
+(put 'bib-ext-list 'permanent-local t)
 
 (defun bib-display ()
   "Display BibTeX citation or matching \\ref or \\label command under point.
 
 A TAGS file is created and used for multi-file documents under auctex."
   (interactive)
-  (let ((cite))
+  (let ((cite)(ref)(label))
     (save-excursion
       (if (not (looking-at "\\\\"))
-          (re-search-backward "[\\]" nil t))
-      (if (looking-at "\\\\[a-zA-Z]*cite")
-          (setq cite t)))
-    (if cite
-        (bib-display-citation)
-      (bib-display-label))))
+          (search-backward "\\" nil t))
+      (if (looking-at "\\\\ref{") 
+          (setq ref t)
+        (if (looking-at "\\\\label{")
+            (setq label t)              
+          (setq cite t))))
+    (cond 
+     ;; reftex doesn't handle label->ref
+     ((and bib-cite-use-reftex-view-crossref
+           (or ref cite))
+      ;;;FIXME: reftex doesn't want point on \ref or \cite part, but on keyword
+      (require 'reftex)
+      (reftex-view-crossref nil))
+     (cite
+      (bib-display-citation))
+     (t
+      (bib-display-label)))))
 
 (defun bib-find ()
   "Edit BibTeX citation or find matching \\ref or \\label command under point.
 
 A TAGS file is created and used for multi-file documents under auctex."
   (interactive)
-  (let ((cite))
+  (let ((cite)(ref)(label))
     (save-excursion
       (if (not (looking-at "\\\\"))
-          (re-search-backward "[ \\\n]" nil t))
-      (if (looking-at "\\\\[a-zA-Z]*cite")
-          (setq cite t)))
-    (if cite
-        (bib-edit-citation)
-      (bib-find-label))))
+          (search-backward "\\" nil t))
+      (if (looking-at "\\\\ref{") 
+          (setq ref t)
+        (if (looking-at "\\\\label{")
+            (setq label t)              
+          (setq cite t))))
+    (cond 
+     ;; reftex doesn't handle label->ref
+     ((and bib-cite-use-reftex-view-crossref
+           (or ref cite))
+      (require 'reftex)
+      (reftex-view-crossref t))
+     (cite
+      (bib-edit-citation))
+     (t
+      (bib-find-label)))))
 
 (defun bib-display-mouse (EVENT)
   "Display BibTeX citation or matching \\ref or \\label command under mouse.
           (message "Sorry, no matches found.")
         (with-output-to-temp-buffer "*Help*" 
           (mapcar 'princ (nreverse the-text)))
+        (bib-cite-fontify-help-as-bibtex)
         (if bib-novice
             (message
              (substitute-command-keys
               (concat "Use \\[bib-apropos] again in the *help* buffer"
-                      " to trim the search"))))
-        (cond
-         ((and bib-hilit-if-available
-               (fboundp 'hilit-highlight-region))
-          (set-buffer "*Help*")
-          (hilit-highlight-region (point-min) (point-max) 'bibtex-mode t))
-         ;; font-lock?
-         ((featurep 'font-lock)
-          (set-buffer "*Help*")
-          (set (make-local-variable 'font-lock-defaults)
-               '(bib-cite-bibtex-font-lock-keywords 
-                 nil t ((?$ . "\"")(?\" . "."))))
-          (font-lock-fontify-buffer))))
+                      " to trim the search")))))
       (if new-buffer-f
           (kill-buffer bib-buffer)))))
 
       (put-text-property (point-min)(or limit (point-max)) 
                          'face 'red-bold))))
 
+(defun bib-cite-fontify-help-xemacs (defaults)
+  (if (fboundp 'font-lock-set-defaults-1) ; >= XEmcas 19.14
+      (progn
+	(set-buffer "*Help*")
+	(setq font-lock-defaults-computed nil
+	      font-lock-keywords nil)
+	(font-lock-set-defaults-1
+	 (and defaults (font-lock-find-font-lock-defaults defaults)))
+	(font-lock-fontify-buffer)
+	(setq font-lock-defaults-computed nil
+	      font-lock-keywords nil)
+	(font-lock-set-defaults-1))))
+	
+(defun bib-cite-fontify-help-as-bibtex ()
+  (save-excursion
+    (cond 
+     ((and bib-hilit-if-available
+           (fboundp 'hilit-highlight-region))
+      (set-buffer "*Help*")
+      (hilit-highlight-region (point-min) (point-max) 'bibtex-mode t))
+     ((not (featurep 'font-lock))
+      nil)                              ;No font-lock! Stop here.
+     ;; font-lock under Emacs and XEmacs
+     ((string-match "XEmacs\\|Lucid" emacs-version)
+      ;; XEmacs
+      (bib-cite-fontify-help-xemacs 'bibtex-mode))
+     (t
+      ;; Emacs
+      (set-buffer "*Help*")
+      (let ((font-lock-defaults 
+             '(bib-cite-bibtex-font-lock-keywords
+               nil t ((?$ . "\"")(?\" . ".")))))
+        (if font-lock-mode
+            (font-lock-mode)
+          (font-lock-unset-defaults)
+          (font-lock-unfontify-buffer))
+        (font-lock-fontify-buffer))))))
+
 (defun bib-cite-fontify-help-as-latex ()
   (save-excursion
     (cond 
      ((and bib-hilit-if-available
            (fboundp 'hilit-highlight-region))
       (set-buffer "*Help*")
-      (hilit-highlight-region (point-min) (point-max) 'LaTeX-mode t))
-     ;; font-lock?
-     ((and (featurep 'font-lock)
-           (featurep 'font-latex))
+      (hilit-highlight-region (point-min) (point-max) 'latex-mode t))
+     ((not (featurep 'font-lock))
+      nil)                              ;No font-lock! Stop here.
+     ;; font-lock under Emacs and XEmacs
+     ((string-match "XEmacs\\|Lucid" emacs-version)
+      ;; XEmacs, not necessary to do s.th. special for font-latex, we do *not*
+      ;; want the buffer-local faces!
+      (bib-cite-fontify-help-xemacs 'latex-mode))
+     (t
+      ;; Emacs
       (set-buffer "*Help*")
-      (setq font-lock-defaults '(font-latex-keywords-2 nil nil ((?$ . "\""))))
-      ;; Add all syntax table for `proper' fontification?
-      (font-lock-fontify-buffer))
-     ((featurep 'font-lock)
-      (make-local-variable 'font-lock-defaults)
-      (setq font-lock-defaults '(tex-font-lock-keywords nil nil ((?$ . "\""))))
-      (set-buffer "*Help*")
-      (font-lock-fontify-buffer)))))
+      ;; Actually, don't want to `permanently' affect *Help* buffer...
+      ;;(if (featurep 'font-latex)
+      ;; (font-latex-setup)
+      ;; Rather I should deal with this in the `let' form:
+      ;; (make-local-variable 'font-lock-string-face)
+      ;; (setq font-lock-string-face font-latex-math-face
+      ;;       font-latex-string-face (default-value 'font-lock-string-face))
+      (let ((font-lock-defaults 
+             (if (featurep 'font-latex)
+                 '((font-latex-keywords font-latex-keywords-1 
+                                        font-latex-keywords-2)
+                   nil nil ((?\( . ".") (?\) . ".") (?$ . "\"")) nil
+                   (font-lock-comment-start-regexp . "%")
+                   (font-lock-mark-block-function . mark-paragraph))
+               '(tex-font-lock-keywords nil nil ((?$ . "\""))))))
+        (if font-lock-mode
+            (font-lock-mode)
+          (font-lock-unset-defaults)
+          (font-lock-unfontify-buffer))
+        (font-lock-fontify-buffer))))))
 
 (defvar bib-document-TeX-files-warnings nil 
   "bib-cite internal variable")
     ;;  tags-file-name set.
     ;;  To get around this.  I'm setting this variable in the TAGS buffer.
     ;; Skip this in XEmacs (Changed by Anders Stenman)
-    (if (not (string-match "XEmacs\\|Lucid" emacs-version))
+    (if (and (not (string-match "XEmacs\\|Lucid" emacs-version))
+             (get-file-buffer the-tags-file))
 	(save-excursion
 	  (set-buffer (get-file-buffer the-tags-file))
 	  (set (make-local-variable 'tags-file-name) the-tags-file))))
 
-
+  
   (if bib-document-TeX-files-warnings   ;free variable loose in emacs!
       (progn
         (with-output-to-temp-buffer "*Help*" 
           (princ bib-document-TeX-files-warnings))
         (setq bib-document-TeX-files-warnings nil) ;Reset
-        (bib-cite-fontify-red)
-     ;;;(if (and bib-hilit-if-available
-     ;;;         (fboundp 'hilit-region-set-face))
-     ;;;    (save-excursion
-     ;;;      (set-buffer "*Help*")
-     ;;;      (hilit-region-set-face 
-     ;;;       1 (point-max)
-     ;;;       (cdr (assq 'error hilit-face-translation-table)))))
-        )))
+        (bib-cite-fontify-red))))
 
 (defun bib-Is-hidden ()
   "Return true is current point is hidden"
 ;;; *all of code was here*
 ;;;      )
   (save-excursion
-    (let ((s)(e)(extent)
+    (let ((s)(e)(extent)(local-extent-list bib-ext-list)
           (inhibit-read-only t)
           (modified (buffer-modified-p))) ;put-text-property changing this?
-      (goto-char (point-min))
       ;; * peta Wed Nov  8 16:27:29 1995 -- better remove the mouse face
       ;;   properties first.
+      (setq bib-ext-list nil)		;Reconstructed below...
       (if (string-match "XEmacs\\|Lucid" emacs-version)
-          (while bib-ext-list
-            (delete-extent (car bib-ext-list))
-            (setq bib-ext-list (cdr bib-ext-list)))
+          (while local-extent-list
+	    (setq extent (car local-extent-list))
+ 	    (if (or (extent-detached-p extent)
+ 		    (and (<= (point-min)(extent-start-position extent))
+ 			 (>= (point-max)(extent-end-position extent))))
+		(delete-extent extent)
+	      (setq bib-ext-list (cons extent bib-ext-list)))
+            (setq local-extent-list (cdr local-extent-list)))
         ;; Remove properties for regular emacs
         ;; FIXME This detroys all mouse-faces and local-maps!
         ;; FIXME Hope no other package is using them in this buffer!
         (let ((before-change-functions) (after-change-functions))
           (remove-text-properties (point-min) (point-max)
                                   '(mouse-face t local-map t))))
+      (goto-char (point-min))
       (while 
           (re-search-forward 
            "\\\\\\(ref\\|label\\|[A-Za-z]*cite[A-Za-z]*\\(\\[.*\\]\\)?\\){[^}]*}"
           (setq bib-ext-list (cons extent bib-ext-list))
           (set-extent-property extent 'highlight t)
           (set-extent-property extent 'start-open t)
+	  (set-extent-property extent 'balloon-help 'bib-label-help)
+	  (set-extent-property extent 'help-echo 'bib-label-help-echo)
           (set-extent-property extent 'keymap bib-highlight-mouse-keymap))
          (t
           (let ((before-change-functions) (after-change-functions)
       (set-buffer-modified-p modified))))
 
 (defun bib-toggle-highlight () 
+;; FIXME: do something about after-change stuff?
   (interactive)
   (if (setq bib-highlight-mouse-t (not bib-highlight-mouse-t))
       (bib-highlight-mouse)
                                   '(mouse-face local-map)))))
       (set-buffer-modified-p modified))))
 
+(defun bib-label-help-echo (object)
+  (if bib-label-help-echo-format
+      (bib-label-help object bib-label-help-echo-format)))
+
+;;; Balloon-help callback. Anders Stenman <stenman@isy.liu.se>
+(defun bib-label-help (object &optional format)
+  (or format (setq format "Use mouse button 2 to find the %s.
+Use mouse button 3 to display the %s."))
+  (save-match-data
+    (let* ((string (extent-string object))
+	   (type (cond ((string-match "^\\\\cite" string) "citation")
+		       ((string-match "^\\\\ref" string) "\\label{}")
+		       ((string-match "^\\\\label" string) "\\ref{}")
+		       (t "bullshit"))))
+      (format format type type))))
+
 ;;----------------------------------------------------------------------------
 ;; Routines to display or edit a citation's bibliography
 
           "*Help*" 
         (set-buffer work-buffer)
         (princ (buffer-substring 1 (point-max))))
-      (cond
-       ((and bib-hilit-if-available
-             (fboundp 'hilit-highlight-region))
-        (set-buffer "*Help*")
-        (hilit-highlight-region (point-min) (point-max) 'bibtex-mode t)
-        (if the-warn-point
-            (hilit-region-set-face 
-             1 the-warn-point 
-             (cdr (assq 'error hilit-face-translation-table)))))
-       ((featurep 'font-lock)
-        (set-buffer "*Help*")
-        (set (make-local-variable 'font-lock-defaults)
-             '(bib-cite-bibtex-font-lock-keywords 
-               nil t ((?$ . "\"")(?\" . "."))))
-        (font-lock-fontify-buffer)
-        (if the-warn-point
-            (bib-cite-fontify-red the-warn-point))))
+      (bib-cite-fontify-help-as-bibtex)
+      (if the-warn-point
+          (bib-cite-fontify-red the-warn-point))
       (kill-buffer bib-buffer)
       (kill-buffer work-buffer))))
 
       (set-buffer bib-buffer)
       (goto-char (point-min))
       (if (not (re-search-forward 
-                (concat "@[^{(]+[{(][\t ]*" the-key "[ ,\n]") nil t))
+                (concat "@[^{(]+[{(][\t ]*" (regexp-quote the-key) "[ ,\n]")
+                nil t))
           (progn 
             (kill-buffer bib-buffer)
             (error "Sorry, could not find bib entry for %s" the-key))
 ;;; (find-file the-file)
     (funcall bib-switch-to-buffer-function (find-file-noselect the-file))
     (goto-char (point-min))             ;V2.19 fix
-    (re-search-forward (concat "@[^{(]+[{(][\t ]*" the-key "[ ,\n]") nil t)))
+    (re-search-forward (concat "@[^{(]+[{(][\t ]*" 
+                               (regexp-quote the-key) 
+                               "[ ,\n]") nil t)))
 
 ;;--------------------------------------------------------------------------
 ;; Function for bib-apropos
         (cond 
          (displayf                  ;Display only
           (set-buffer (bib-etags-find-noselect the-name masterdir))
-          (re-search-forward the-name nil t) ;'cos tags puts point line begin
+          (re-search-forward (regexp-quote the-name) nil t) 
+          ;; ...because tags puts point on beginning of line
           (if (string-match "^\\\\label" the-name)
               (bib-display-this-environment) ;display the label's environment
             (bib-display-this-ref)))    ;     display the ref's context
                       (the-string (cdr (car string-alist))))
                   (while (re-search-forward 
                           (concat "\\(^[, \t]*[a-zA-Z]+[ \t]*=[ \t]*\\)" 
-                                  the-key 
+                                  (regexp-quote the-key) 
                                   "\\([, \t\n]\\)")
                           nil t)
                     (replace-match (concat "\\1" the-string "\\2") t)))
                 (insert-file-contents (buffer-substring (match-beginning 1)
                                                         (match-end 1))))))
           (goto-char 1)
-          ;; look for \citation{gertsenshtein59}
+
+;;; Patched by calvanes@dis.uniroma1.it (Diego Calvanese)
+;;;      ;; look for \citation{gertsenshtein59}
+;;;       (while (re-search-forward "^\\\\citation{\\(.*\\)}$" nil t)
+;;;         (intern (buffer-substring (match-beginning 1)(match-end 1))
+;;;                 keys-obarray))
+          ;; look for \citation{gertsenshtein59,vardi88,...,ullmann90}
+          ;; comma-separation generated by certain LaTeX styles.
           (while (re-search-forward "^\\\\citation{\\(.*\\)}$" nil t)
-            (intern (buffer-substring (match-beginning 1)(match-end 1))
-                    keys-obarray)))
+	    (let ((string (buffer-substring (match-beginning 1)(match-end 1)))
+		  (start 0))
+	      (while (string-match "\\([^,\n]+\\)" string start)
+		(intern (substring string (match-beginning 1) (match-end 1))
+			keys-obarray)
+		(setq start (match-end 0))))))
         (kill-buffer work-buffer)
         keys-obarray))))
 
   (defvar bib-imenu-document-counter nil
     "bib-cite internal variable")
 
-;; FIXME: If bib-cite becomes a minor mode, then this hook will go away
-;;        and this will be done in the minor-mode function.
-  (defun LaTeX-hook-setq-imenu ()
-    ;; User who *never* uses multi-file documents could change this to:
-    ;;                                  'imenu--create-LaTeX-index-for-buffer
-    (setq imenu-create-index-function 'imenu--create-LaTeX-index))
-
   (defun imenu--create-LaTeX-index ()
     ;; dispatch to proper function, depending on whether a multi-file document.
     (let ((masterfile (bib-master-file)))
     ;; For a multi-file document in auctex only.
     ;; Create imenu--index-alist for master file buffer and use the same
     ;; for all input files?  This would be faster...  Maybe in next version?
+    ;;FIXME: How about parsing TAGS file instead of search through all files?
     (bib-etags)                         ;Create a new TAGS file, user needs it.
     (let ((tex-buffer (get-buffer-create "*imenu-tex*"))
           (index-alist '())
         (while 
             (re-search-backward  
 ;;;          "\\\\\\(\\(sub\\)*section\\|chapter\\|label\\){[^}]+}" 
-             "\\(\\\\label\\)\\|\\(^[ ]*\\\\\\(\\(sub\\)*section\\|chapter\\)\\){[^}]+}"
+             "\\(\\(\\\\label\\)\\|\\(^[ ]*\\\\\\(\\(sub\\)*section\\|chapter\\)\\)\\){[^}]+}"
              nil t)
           (imenu-progress-message prev-pos nil t)
           (save-match-data
 ;;;        "^[^;]*\\(\\\\\\)\\(\\(sub\\)*section\\|chapter\\|label\\){[^}]+}" 
 ;;; Original regexp that would catch commented-out stuff
 ;;;        "\\\\\\(\\(sub\\)*section\\|chapter\\|label\\){[^}]+}" 
-           "\\(\\\\label\\)\\|\\(^[ ]*\\\\\\(\\(sub\\)*section\\|chapter\\)\\){[^}]+}"
+           "\\(\\(\\\\label\\)\\|\\(^[ ]*\\\\\\(\\(sub\\)*section\\|chapter\\)\\)\\){[^}]+}"
            nil t)
         (imenu-progress-message prev-pos nil t)
         (save-match-data
   (defun imenu--remove-LaTeX-keyword-el (element keyword)
     "Remove the LaTeX KEYWORD from car of ELEMENT."
     (save-match-data
-      ;; Shouls I have extra option here: "[
-      (string-match (concat "\\\\" keyword "{\\(.*\\)}") (car element))
-      (cons 
-       (substring (car element) (match-beginning 1) (match-end 1)) 
-       (cdr element))))
+      ;; Should I have extra option here: "[
+      (if (string-match (concat "\\\\" keyword "{\\(.*\\)}") (car element))
+          (cons 
+           (substring (car element) (match-beginning 1) (match-end 1)) 
+           (cdr element)))))
   
   (defun imenu--label-cmp (el1 el2)
     "Predicate to compare labels in lists produced by 
 
     (TeX-auto-write)))
 
-;; ---------------------------------------------------------------------------
-;; Key definitions start here...
-
-;; Christoph Wedler <wedler@fmi.uni-passau.de>
-;;  Replace eval-after-load (which doesn't work with efs anyway)
-;;  with add-submenu in bib-cite-initialize
-
-;;(if ((and (string-match "XEmacs\\|Lucid" emacs-version)
-;;          (or window-system
-;;              (fboundp 'smart-menu))      ;text menus by Bob Weiner
-;;          (not (fboundp 'eval-after-load))))
-;;    ;; define eval-after-load for XEmacs
-;;    (defun eval-after-load (file form)
-;;      "Arrange that, if FILE is ever loaded, FORM will be run at that
-;;time.
-;;This makes or adds to an entry on `after-load-alist'.
-;;It does nothing if FORM is already on the list for FILE.
-;;FILE should be the name of a library, with no directory name."
-;;      (or (assoc file after-load-alist)
-;;          (setq after-load-alist (cons (list file) after-load-alist)))
-;;      (let ((elt (assoc file after-load-alist)))
-;;        (or (member form (cdr elt))
-;;            (nconc elt (list form))))
-;;      form))
-
-(defvar bib-cite-map
-  (let ((map (make-sparse-keymap)))
-    (define-key map "a" 'bib-apropos)
-    (define-key map "m" 'bib-make-bibliography)
-    (define-key map "d" 'bib-display)
-    (define-key map "e" 'bib-etags)
-    (define-key map "f" 'bib-find)
-    (define-key map "h" 'bib-highlight-mouse)
-  map)
-  "Keymap to bind to \\C-cb in latex keymap")
-
-(cond
- ((and (string-match "XEmacs\\|Lucid" emacs-version)
-       (or window-system
-           (fboundp 'smart-menu)))      ;text menus by Bob Weiner
-  ;;
-  ;; xemacs under X with auc-tex
-  ;;
-
-  ;; Christoph Wedler <wedler@fmi.uni-passau.de>
-  (defvar bib-cite-xemacs-menu
-    '("Bib-Cite"
-    ;;"---"
-      ["Make BibTeX bibliography buffer" bib-make-bibliography t]
-      ["Display citation or matching \\ref or \\label" bib-display t]
-      ["Find BibTeX citation or matching \\ref or \\label" bib-find t]
-      ["Search apropos BibTeX files" bib-apropos t]
-      ["Build TAGS file for multi-file document" bib-etags (bib-master-file)]
-      ["Refresh \\cite, \\ref and \\label mouse highlight"
-       bib-highlight-mouse t])
-    "Submenu of LaTeX menu, used by bib-cite.")
-
-;;; Add to bibtex.el's popup menu
-  (defvar bib-cite-xemacs-bibtex-mode-menu
-    '("---"
-      "Bib-Cite"
-      "---"
-      ["Search apropos BibTeX files" bib-apropos t]
-      ["Create auc-tex auto parsing file" bib-create-auto-file t])
-    "Submenu of bibtex-mode menu, used by bib-cite.")
-  
-  (if (boundp 'bibtex-menu)
-      ;; Add menu now
-      (setq bibtex-menu
-	    (append
-	     bibtex-menu
-	     bib-cite-xemacs-bibtex-mode-menu))
-    ;; Setup to add menu later
-    (defun bib-cite-bibtex-mode-hook ()
-      (if (boundp 'bibtex-menu)
-          (progn
-            (setq bibtex-menu
-                  (append
-                   bibtex-menu
-                   bib-cite-xemacs-bibtex-mode-menu))
-            (remove-hook 'bibtex-mode-hook 'bib-cite-bibtex-mode-hook))))
-    (add-hook 'bibtex-mode-hook 'bib-cite-bibtex-mode-hook))
-;;; Done - Add to bibtex.el's popup menu
-
-;;  (eval-after-load 
-;;   "latex"
-;;   '(progn
-;;      (add-menu-button '("LaTeX") ["----" nil t])
-;;      (add-menu-button 
-;;      '("LaTeX") ["Make BibTeX bibliography buffer" bib-make-bibliography t])
-;;      (add-menu-button 
-;;       '("LaTeX") ["Display citation or matching \\ref or \\label" 
-;;                   bib-display t])
-;;      (add-menu-button 
-;;       '("LaTeX") ["Find BibTeX citation or matching \\ref or \\label" 
-;;                   bib-find t])
-;;      (add-menu-button 
-;;       '("LaTeX") ["Search apropos BibTeX files" bib-apropos t])
-;;      (add-menu-button 
-;;       '("LaTeX") ["Build TAGS file for multi-file document" bib-etags t])
-;;      (add-menu-button 
-;;       '("LaTeX") 
-;;       ["Refresh \\cite, \\ref and \\label mouse highlight" 
-;;        bib-highlight-mouse t])
-
-      )
-
- ((and (not (string-match "XEmacs\\|Lucid" emacs-version))
-       (string-equal "19" (substring emacs-version 0 2))
-       (or window-system
-           (fboundp 'tmm-menubar)))     ; 19.30 - Will autoload if necessary 
-  ;;
-  ;; emacs-19 under X-windows (or non-X with tmm)
-  ;;
-  
-  ;; This *almost* makes me want to switch over to XEmacs...
-
-  ;; to auc-tex auto file for a bibtex buffer
-  (eval-after-load 
-   "bibtex"
-   '(progn
-      (cond
-       ((lookup-key bibtex-mode-map [menu-bar move/edit])
-        (define-key-after 
-          (lookup-key bibtex-mode-map [menu-bar move/edit]) 
-          [bib-nil] '("---" . nil) '"--")
-        (define-key-after 
-          (lookup-key bibtex-mode-map [menu-bar move/edit]) 
-          [bib-apropos] '("Search Apropos" . bib-apropos) 'bib-nil)
-        (define-key-after 
-          (lookup-key bibtex-mode-map [menu-bar move/edit]) 
-          [auc-tex-parse] 
-          '("Create auc-tex auto parsing file" . bib-create-auto-file)
-          'bib-apropos))
-       ((lookup-key bibtex-mode-map [menu-bar bibtex-edit])
-        (define-key-after 
-          (lookup-key bibtex-mode-map [menu-bar bibtex-edit]) 
-          [bib-nil] '("---" . nil) '"--")
-        (define-key-after 
-          (lookup-key bibtex-mode-map [menu-bar bibtex-edit]) 
-          [bib-apropos] '("Search Apropos" . bib-apropos) 'bib-nil)
-        (define-key-after 
-          (lookup-key bibtex-mode-map [menu-bar bibtex-edit]) 
-          [auc-tex-parse] 
-          '("Create auc-tex auto parsing file" . bib-create-auto-file)
-          'bib-apropos)))))
-
-  (defvar bib-cite-put-menu-separately t
-    "*Put bib-cite menubar menu separately, not within LaTeX pull-down menu")
-  (cond
-   ((not bib-cite-put-menu-separately)  ;Old method - Destroy code?
-     (defun bib-add-menu-keys (the-key)
-       (cond
-        (the-key                             ;make sure keymap exists
-         (define-key-after the-key [bib-nil] '("---" . nil) '"--")
-         (define-key-after the-key [bib-make-bibliography]
-           '("Make BiBTeX bibliography buffer" . bib-make-bibliography) 
-           'bib-nil)
-         (define-key-after the-key [bib-display]
-           '("Display citation or matching \\ref or \\label" . bib-display)
-           'bib-make-bibliography)
-         (define-key-after the-key [bib-find]
-           '("Find BiBTeX citation or matching \\ref or \\label" . bib-find)
-           'bib-display)
-         (define-key-after the-key [bib-apropos]
-           '("Search apropos BiBTeX files" . bib-apropos) 'bib-find)
-      ;;;(put 'ps-print-region-with-faces 'menu-enable 'mark-active)
-      ;;;(define-key menu-bar-tools-menu [ps-print-buffer]
-      ;;;  '("Postscript Print Buffer" . ps-print-buffer-with-faces))
-         (put 'bib-etags 'menu-enable '(bib-master-file))
-         (define-key-after the-key [bib-etags]
-           '("Build TAGS file for multi-file document" . bib-etags)
-           'bib-apropos)
-         (define-key-after the-key [bib-highlight-mouse]
-           '("Refresh \\cite, \\ref and \\label mouse highlight" . 
-             bib-highlight-mouse)
-           'bib-etags))))
-
-     ;;for auc-tex's LaTeX-mode
-     (eval-after-load 
-      "latex"
-      '(let ((the-key (or (lookup-key LaTeX-mode-map [menu-bar LaTeX])
-                          (lookup-key LaTeX-mode-map [menu-bar AUC\ TeX]))))
-      ;;;(define-key LaTeX-mode-map [S-mouse-1] 'bib-display-mouse)
-      ;;;(define-key LaTeX-mode-map [S-mouse-2] 'bib-find-mouse)
-         (define-key LaTeX-mode-map "\C-cb" bib-cite-map)
-         (bib-add-menu-keys the-key)))
-
-     ;;for auc-tex's TeX-mode
-     (eval-after-load 
-      "tex"
-      '(let ((the-key (lookup-key TeX-mode-map [menu-bar TeX])))
-      ;;;(define-key TeX-mode-map [S-mouse-1] 'bib-display-mouse)
-      ;;;(define-key TeX-mode-map [S-mouse-2] 'bib-find-mouse)
-         (define-key TeX-mode-map "\C-cb" bib-cite-map)
-         (bib-add-menu-keys the-key)))
-
-     ;;for plain tex-mode
-     (eval-after-load 
-      "tex-mode"
-      '(progn
-         (let ((the-key (lookup-key tex-mode-map [menu-bar tex])))
-        ;;;(define-key tex-mode-map [S-mouse-1] 'bib-display-mouse)
-        ;;;(define-key tex-mode-map [S-mouse-2] 'bib-find-mouse)
-           (define-key tex-mode-map "\C-cb" bib-cite-map)
-           (bib-add-menu-keys the-key)
-           (define-key tex-mode-map [menu-bar tex bib-etags] 
-             'undefined)))))
-   
-   (t                                   ;New method - separate menu
-    (setq bib-cite-menu-map (make-sparse-keymap "bib-cite"))
-    (define-key bib-cite-menu-map [bib-display]
-      '("Display citation or matching \\ref or \\label" . bib-display))
-    (define-key bib-cite-menu-map [bib-find]
-      '("Find BiBTeX citation or matching \\ref or \\label" . bib-find))
-    (define-key bib-cite-menu-map [bib-make-bibliography]
-      '("Make BiBTeX bibliography buffer" . bib-make-bibliography))
-    (put 'bib-etags 'menu-enable '(bib-master-file))
-    (define-key bib-cite-menu-map [bib-etags]
-      '("Build TAGS file for multi-file document" . bib-etags))
-    (define-key bib-cite-menu-map [bib-apropos]
-      '("Search apropos BiBTeX files" . bib-apropos))
-    (define-key bib-cite-menu-map [bib-highlight-mouse]
-      '("Refresh \\cite, \\ref and \\label mouse highlight"
-        . bib-highlight-mouse))
-
-    (eval-after-load 
-     "tex-mode"
-     '(progn
-     ;;;(define-key tex-mode-map [S-mouse-1] 'bib-display-mouse)
-     ;;;(define-key tex-mode-map [S-mouse-2] 'bib-find-mouse)
-        (define-key tex-mode-map "\C-cb" bib-cite-map)
-        (define-key tex-mode-map [menu-bar bib-cite-menu-map]
-          (cons "Bib-Cite" bib-cite-menu-map))
-        (define-key tex-mode-map [menu-bar bib-cite-menu-map bib-etags] 
-          'undefined)))
-
-    ;;for auc-tex's LaTeX-mode
-    (eval-after-load 
-     "latex"
-     '(progn
-     ;;;(define-key LaTeX-mode-map [S-mouse-1] 'bib-display-mouse)
-     ;;;(define-key LaTeX-mode-map [S-mouse-2] 'bib-find-mouse)
-        (define-key LaTeX-mode-map "\C-cb" bib-cite-map)
-        (define-key LaTeX-mode-map [menu-bar bib-cite-menu-map]
-          (cons "Bib-Cite" bib-cite-menu-map))))
-
-    ;;for auc-tex's TeX-mode
-    (eval-after-load 
-     "tex"
-     '(progn
-     ;;;(define-key TeX-mode-map [S-mouse-1] 'bib-display-mouse)
-     ;;;(define-key TeX-mode-map [S-mouse-2] 'bib-find-mouse)
-        (define-key TeX-mode-map "\C-cb" bib-cite-map)))
-    ))))
-    
-;; This must be after adding to LaTeX-mode-map because we copy it here.
-(defvar bib-highlight-mouse-keymap
-  ;; First, copy the local keymap so we don't have `disappearing' menus
-  ;; when the mouse is moved over a \ref, \label or \cite command.
-  ;; FIXME:  **This only works if bib-cite is loaded after TeX keymaps
-  ;;           are constructed.  Thus, if bib-cite is loaded in a hook.
-
-  ;; FIXME: Check out (mouse-major-mode-menu) to see how it grabs the local
-  ;;        menus to display.  Maybe on `highlighted' commands we could only
-  ;;        display the bib-cite stuff (or a subset of it).
-  (let ((m (cond
-            ((boundp 'LaTeX-mode-map)
-             (copy-keymap LaTeX-mode-map))
-            ((boundp 'tex-mode-map)
-             (copy-keymap tex-mode-map))
-            (t
-             (make-sparse-keymap)))))
-    (cond 
-     ((string-match "XEmacs\\|Lucid" emacs-version)
-      (set-keymap-name m 'bib-highlight-mouse-keymap)
-      (cond
-       ;; action-key stuff from Vladimir Alexiev <vladimir@cs.ualberta.ca>
-       ((commandp 'action-key)
-        ;; for hyperbole. The Right Way is to define implicit buttons
-        ;; (defib) bib-cite and label-ref instead of overriding action-key and
-        ;; assist key, so that eg smart key help can be obtained, but I'm
-        ;; lazy.
-        (substitute-key-definition 'action-key 'bib-find m global-map)
-        (substitute-key-definition 'assist-key 'bib-display m global-map)
-        (substitute-key-definition 'action-key-depress 
-                                   'bib-find-mouse m global-map)
-        (substitute-key-definition 'assist-key-depress 
-                                   'bib-display-mouse m global-map)
-        (substitute-key-definition 'action-mouse-key nil m global-map)
-        (substitute-key-definition 'assist-mouse-key nil m global-map))
-       (t                               ; xemacs, not hyperbole
-        (define-key m "\e\r" 'bib-find-mouse) ;   bug Fixed in V2.17
-        (define-key m "\e\n" 'bib-display-mouse) ;bug Fixed in V2.17
-      ;;(define-key m [(shift button1)] 'bib-display-mouse)
-        (define-key m [button3] 'bib-display-mouse)
-        (define-key m [button2] 'bib-find-mouse))))
-     (t                                 ; emacs 19
-      (cond
-       ((commandp 'action-key)
-        (substitute-key-definition 'action-key 'bib-find m global-map)
-        (substitute-key-definition 'assist-key 'bib-display m global-map)
-        (substitute-key-definition 'action-mouse-key-emacs19 
-                                   'bib-find-mouse m global-map)
-        (substitute-key-definition 'assist-mouse-key-emacs19 
-                                   'bib-display-mouse m global-map)
-        (substitute-key-definition 'action-key-depress-emacs19 
-                                   nil m global-map)
-        (substitute-key-definition 'assist-key-depress-emacs19 
-                                   nil m global-map))
-       (t                               ; emacs 19, not hyperbole
-        (define-key m [down-mouse-3] 'bib-display-mouse)
-        (define-key m [mouse-2] 'bib-find-mouse)))))
-    m))
 ;; --------------------------------------------------------------------------
 ;; The following routines are also defined in other packages...
 
             (setq answers (cons entry answers))))
       (nreverse answers))))
 
-;;
-;; Create the unified hook to call from LaTeX-mode-hook
-;;
-(defun bib-cite-initialize ()
-  ;; Christoph Wedler's <wedler@fmi.uni-passau.de> suggestion for xemacs
-  ;; Added for version 2.19
-  (if (boundp 'tags-always-exact)
-      (progn
-        (make-local-variable 'tags-always-exact)
-        (setq tags-always-exact nil)))
-
-  ;; Christoph Wedler <wedler@fmi.uni-passau.de>
-  (and (boundp 'bib-cite-xemacs-menu)
-       bib-cite-xemacs-menu
-       (fboundp 'add-submenu)     ;Insurance for emacs
-       (featurep 'menubar)
-     ;;;FIXME: I can do this to add a main menu.
-     ;;(add-submenu nil bib-cite-xemacs-menu)
-       ;; This makes it buffer-specific so I don't need to remove it.
-       (set-buffer-menubar (copy-sequence current-menubar))
-       (add-submenu '("LaTeX") bib-cite-xemacs-menu))
-
-  (cond 
-   ((string-match "XEmacs\\|Lucid" emacs-version)
-    ;; Define keys for XEmacs 
-    ;;  Could do all modes like this for emacs also...
-    (local-set-key "\C-cba" 'bib-apropos)
-    (local-set-key "\C-cbm" 'bib-make-bibliography)
-    (local-set-key "\C-cbd" 'bib-display)
-    (local-set-key "\C-cbe" 'bib-etags)
-    (local-set-key "\C-cbf" 'bib-find)
-    (local-set-key "\C-cbh" 'bib-highlight-mouse)))
-
-  (if bib-highlight-mouse-t
-      (bib-highlight-mouse))
-  (if bib-use-imenu
-      (LaTeX-hook-setq-imenu))
-
-  ;; Make sure that imenu-sort-function is nil
-  (and (boundp 'imenu-sort-function)
-       imenu-sort-function
-       (make-local-variable 'imenu-sort-function)
-       (setq imenu-sort-function nil))
-  )
-
-(add-hook 'LaTeX-mode-hook 'bib-cite-initialize t) ;auctex's latex-mode
-(add-hook 'latex-mode-hook 'bib-cite-initialize t) ;emacs' plain latex-mode
-
-;; If bib-cite.el is loaded in a mode hook, bib-highlight-mouse and
-;; LaTeX-hook-setq-imenu are not called on the buffer... 
-;; so invoke it now for .tex buffers.  Same for imenu.
-;(if (string-match ".tex$" (buffer-name))
-;  (bib-cite-initialize))
-
-(if (or (eq major-mode 'latex-mode) 
-        (eq major-mode 'plain-tex-mode))
-      (bib-cite-initialize))
 
 (provide 'bib-cite)
 ;;; bib-cite.el ends here

File font-latex.el

 ;;; font-latex.el --- LaTeX fontification for Font Lock mode.
 
-;; Copyright (C) 1996 Peter S. Galbraith
+;; Copyright (C) 1996, 1997, 1998 Peter S. Galbraith
  
 ;; Authors:    Peter S. Galbraith <galbraith@mixing.qc.dfo.ca>
 ;;             Simon Marshall <Simon.Marshall@esrin.esa.it>
 ;; Maintainer: Peter S. Galbraith <galbraith@mixing.qc.dfo.ca>
 ;; Created:    06 July 1996
-;; Version:    0.403 *Beta* (19 Nov 96)
+;; Version:    0.508 (06 Feb 98)
 ;; Keywords:   LaTeX faces
 
 ;; RCS $Id$
 ;; Boston, MA 02111-1307, USA.
 
 ;;; Commentary:
+;;  This package enhances font-lock fontification patterns for LaTeX.
 
 ;; New versions of this package (if they exist) may be found at:
 ;;  ftp://ftp.phys.ocean.dal.ca/users/rhogee/elisp/font-latex.el
 ;;    (if window-system
 ;;        (require 'font-latex))
 ;;
+;; Turning on font-latex:
+;;
+;;   After font-latex is loaded (or `required'), it will be automatically
+;;   used whenever you enter `font-lock-mode' on a LaTeX buffer. Therefore,
+;;   I direct you to the file font-lock.el that comes with Emacs for more
+;;   info.
+;;
+;; Turning on Font-lock automatically:
+;;
+;;  If you choose to turn-on font-lock by default using a mode-hook,
+;;  there is an order to respect with-respect-to loading font-latex.  
+;;  Do either:
+;;
+;;    (if window-system
+;;        (progn
+;;          (require 'font-latex)
+;;          (add-hook 'latex-mode-hook 'turn-on-font-lock 'append)
+;;          (add-hook 'LaTeX-mode-hook 'turn-on-font-lock 'append)))
+;;  or
+;;    (if window-system
+;;        (progn
+;;          (add-hook 'latex-mode-hook 'turn-on-font-lock)
+;;          (add-hook 'LaTeX-mode-hook 'turn-on-font-lock)
+;;          (require 'font-latex)))
+;;
+;;  It's probably not a bad idea to always append 'turn-on-font-lock
+;;  such that it is always sure to be last.
+;;
+;; Fontification Levels:
+;;
 ;;  There are two levels of fontification, selected by the value of the
-;;  font-lock variable font-lock-maximum-decoration.  font-latex uses two
-;;  levels.  There are ways documented in font-latex.el to set this
-;;  differently for each mode that uses font-lock, but if you are unsure and
-;;  are running on a fast enough machine, try putting this in your ~/.emacs
-;;  file:
-;;    (setq font-lock-maximum-decoration t)
+;;  font-lock variable font-lock-maximum-decoration.  There are ways
+;;  documented in font-latex.el to set this differently for each mode that
+;;  uses font-lock, but if you are unsure and are running on a fast enough
+;;  machine, try putting this in your ~/.emacs file: 
+;;    (setq font-lock-maximum-decoration t) 
 ;;  It probably best to put it before the (require 'font-latex) statement.
 ;;
+;; Changing colours
+;;
+;;  Okay, so you hate the colours I picked.  How do you change them you ask?
+;;  First, find the font name to change using the command:
+;;    M-x list-text-properties-at
+;;  Then, suppose you got `font-latex-math-face', edit ~/.Xdefaults and add:
+;;    Emacs.font-latex-math-face.attributeForeground: blue
+;;  without the semi-colon I'm using here ascomment delimiters, of course.
+;;
 ;; Lazy-lock users:
 ;;
-;;  lazy-lock and font-lock don't work too well together (up to Emacs 19.33
+;;  lazy-lock and font-latex don't work too well together (up to Emacs 19.33
 ;;  and XEmacs 19.14 anyway).  font-latex uses functions to find text to
 ;;  fontify that may span more than one line, and this doesn't suit
-;;  lazy-lock's search limits too well.
+;;  lazy-lock's search limits too well.  Recent versions of font-latex are
+;;  a bit better, and perhaps you can live with the occasional 
+;;  mis-fontification.
 ;;
 ;; Old hilit19 (and hilit-LaTeX) users:
 ;;
 ;;
 ;;    (setq hilit-mode-enable-list  '(not latex-mode))
 ;;
-;;  You can tell you using font-latex instead of hilit-LaTeX because:
+;;  You can tell you are using font-latex instead of hilit-LaTeX because:
 ;;
 ;;  - colours will be different 
 ;;  - You'll see a message like `Fontifying font-latex.tex...done' 
 ;;    instead of `highlighting 1: \(^\|[^\\]\)\(\\[a-zA-Z\\]+\)'
 ;; ----------------------------------------------------------------------------
 ;;; Change log:
+;; V0.508 06Feb98 PSG (RCS V1.51)
+;;    Created font-latex-match-textual; changed font-latex-math-face colour.
+;; V0.507 30Jan98 PSG (RCS V1.50)
+;;    Removed font-latex-find-matching-close because it broke the cache.
+;;    Rewrote the cache method.  Ouch!
+;; V0.506 08Jan98 PSG (RCS V1.48)
+;;    Added variables font-latex-match-variable, font-latex-match-function
+;;    font-latex-match-reference (built using reexp-opt).
+;; V0.505 07Jan98 PSG (RCS V1.47)
+;;    XEmacs20 has defface.
+;; V0.504 20Oct97 Kevin Ruland <kruland@seistl.com> (RCS V1.46)
+;;    Fixed the real bug in font-latex-match-command-outside-arguments
+;; V0.503 16Oct97 PSG (RCS V1.45)
+;;    Patched font-latex-match-command-outside-arguments for allow for
+;;    strange interaction with AUC-TeX's LaTeX-environment command.
+;; V0.502 07Oct97 (RCS V1.44)
+;;    Kevin Ruland <kevin@rodin.wustl.edu> edits font-latex-find-matching-close
+;;    PSG: Changed OliveGreen for OliveDrab, found in rgb.txt
+;; V0.501 24Sep97 (RCS V1.42)
+;;    Kevin Ruland <kevin@rodin.wustl.edu> added font-latex-find-matching-close
+;;    used instead of scan-sexp to find arguments containing extra brackets.
+;; V0.500 23Sep97 PSG (RCS V1.41)
+;;  - Support for Emacs-20 (No customize support yet)
 ;; V0.403 19Nov96 (RCS V1.37)
 ;;  - Christoph Wedler <wedler@fmi.uni-passau.de>
 ;;    XEmacs patch for local math-font 
 (defvar font-latex-math-face			'font-latex-math-face
   "Face to use for LaTeX math environments.")
 
+(defvar font-latex-match-variable
+  (concat 
+   "\\\\" "\\("
+;;;(regexp-opt
+;;; '("setlength" "settowidth" "setcounter" "addtolength" "addtocounter"))
+   "addto\\(counter\\|length\\)\\|set\\(counter\\|length\\|towidth\\)"
+   "\\)\\>")
+  "font-latex regexp to match LaTeX variable keywords.")
+
+(defvar font-latex-match-reference
+  (concat 
+   "\\\\" "\\("
+;;;(regexp-opt
+;;; '("nocite" "cite" "label" "pageref" "vref" "eqref" "ref"
+;;;   "include" "input" "bibliography"
+;;;   "index" "glossary" "footnote" "footnotemark" "footnotetext"))
+   "bibliography\\|cite[a-zA-Z]*\\|eqref\\|footnote\\(mark\\|text\\)?\\|"
+   "glossary\\|in\\(clude\\|dex\\|put\\)\\|label\\|nocite\\|pageref\\|ref\\|"
+   "vref"
+   "\\)\\>")
+  "font-latex regexp to match reference keywords.")
+  
+(defvar font-latex-match-function
+  (concat 
+   "\\\\" "\\("
+;;;(regexp-opt
+;;; '("begin" "end"
+;;;   "pagenumbering"
+;;;   "thispagestyle" "pagestyle"
+;;;   "nofiles" "includeonly"
+;;;   "bibliographystyle" "documentstyle" "documentclass"
+;;;   "newenvironment" "newcommand" "newlength" "newtheorem" "newcounter"
+;;;   "renewenvironment" "renewcommand" "renewlength" "renewtheorem" 
+;;;   "renewcounter"
+;;;   "usepackage" "fbox" "mbox" "sbox" "vspace" "hspace"))
+   "b\\(egin\\|ibliographystyle\\)\\|document\\(class\\|style\\)\\|"
+   "end\\|fbox\\|hspace\\|includeonly\\|mbox\\|"
+   "n\\(ew\\(co\\(mmand\\|unter\\)\\|environment\\|length\\|theorem\\)"
+       "\\|ofiles\\)\\|"
+    "page\\(numbering\\|style\\)\\|"
+    "renew\\(co\\(mmand\\|unter\\)\\|environment\\|length\\|theorem\\)\\|"
+    "sbox\\|thispagestyle\\|usepackage\\|vspace"
+   "\\)\\>")
+  "font-latex regexp to match LaTeX function keywords.")
+
+(defvar font-latex-match-textual
+  (concat 
+   "\\\\" "\\("
+;;;(regexp-opt
+;;; '("item" ;;;FIXME: does not have an {arg} so should treated elsewhere.
+;;;   "part" "chapter" "section" "subsection" "subsubsection" 
+;;;   "paragraph" "subparagraph" "subsubparagraph" 
+;;;   "title" "author" "date" "thanks" "address"
+;;;   "caption"))
+   "a\\(ddress\\|uthor\\)\\|c\\(aption\\|hapter\\)\\|date\\|item\\|"
+   "par\\(agraph\\|t\\)\\|"
+   "s\\(ection\\|"
+       "ub\\(paragraph\\|s\\(ection\\|ub\\(paragraph\\|section\\)\\)\\)\\)\\|"
+   "t\\(hanks\\|itle\\)"
+   "\\)\\>")
+  "font-latex regexp to match LaTeX function with text arguemnt.")
+
+
+(defvar font-latex-keywords-1
+  (list
+   ;; FIXME: Maybe I should put this in a function, use override but let
+   ;;        the function determine if commented-out.
+   (list (concat 
+          "\\\\" "\\("
+;;; (regexp-opt
+;;;  '("nopagebreak" "pagebreak" "newpage" "clearpage" "cleardoublepage"
+;;;    "enlargethispage" "nolinebreak" "linebreak" "newline"
+;;;    "-" "\\" "\\*" "displaybreak" "allowdisplaybreaks"))
+          "\\\\\\*\\|allowdisplaybreaks\\|clear\\(doublepage\\|page\\)\\|"
+          "displaybreak\\|enlargethispage\\|linebreak\\|"
+          "n\\(ew\\(line\\|page\\)\\|o\\(linebreak\\|pagebreak\\)\\)\\|"
+          "pagebreak\\|[\\-]"
+          
+          "\\)")
+	 '(0 font-latex-warning-face))
+   '("\\$\\$\\([^$]+\\)\\$\\$" 1 font-latex-math-face)        ;;; $$...$$
+   '(font-latex-match-quotation . font-latex-string-face)     ;;; ``...''
+   '(font-latex-match-font-outside-braces		      ;;;\textit{text}
+     (0 font-lock-keyword-face
+        append                         ;Override? [t 'keep 'prepend 'append]
+        ;; Can't use prepend because that overwrites syntax fontification
+        ;; e.g. comments.
+        t)                              ;Laxmatch? if t, do not signal error
+     (1 font-latex-italic-face append t)
+     (2 font-latex-bold-face append t)
+     (3 font-lock-type-face append t))
+   '(font-latex-match-font-inside-braces		      ;;;{\it text}
+     (0 font-lock-keyword-face append t)
+     (1 font-latex-italic-face append t)
+     (2 font-latex-bold-face append t)
+     (3 font-lock-type-face append t)))
+  "Subdued level highlighting for LaTeX modes.")
+
+(defvar font-latex-keywords-2
+  (append font-latex-keywords-1
+   '((font-latex-match-reference                              ;;;\cite
+      (0 font-lock-keyword-face append t)
+      (1 font-lock-variable-name-face append t)              ;;;    [opt]
+      (2 font-lock-reference-face append t))                 ;;;         {key}
+     (font-latex-match-function                               ;;;\documentclass
+      (0 font-lock-keyword-face append t)
+      (1 font-lock-variable-name-face append t)              ;;;   [opt]
+      (2 font-lock-function-name-face append t))             ;;;        {text}
+     (font-latex-match-textual                               ;;;\section
+      (0 font-lock-keyword-face append t)
+      (1 font-lock-variable-name-face append t)              ;;;   [opt]
+      (2 font-lock-type-face append t))                      ;;;        {text}
+     (font-latex-match-variable
+      (0 font-lock-keyword-face nil t)
+      (1 font-lock-variable-name-face append t)
+      (2 font-lock-variable-name-face append t))
+     (font-latex-match-math-env 
+      (0 font-latex-math-face append t))         	      ;;;\(...\)
+     (font-latex-match-math-envII                             ;;;Math environ.
+      (0 font-latex-math-face append t))
+     ("\\\\[@A-Za-z]+"                                        ;;;Other commands
+      (0 font-latex-sedate-face append))))
+  "High level highlighting for LaTeX modes.")
+
+(defvar font-latex-keywords font-latex-keywords-1
+  "Default expressions to highlight in TeX mode.")
+
 ;; End-User can stop reading here.
 
 ;; Make sure font-latex.el is supported.  I don't claim to have tested this...
       (and (= emacs-major-version 19) (< emacs-minor-version 29)))
     (error "`font-latex' was written for Emacs 19.29/XEmacs 19.14 or later"))
 
-(defvar font-latex-is-XEmacs
-  (not (null (save-match-data (string-match "XEmacs\\|Lucid" emacs-version)))))
+(defvar font-latex-is-XEmacs19
+  (and (not (null (save-match-data 
+                    (string-match "XEmacs\\|Lucid" emacs-version))))
+       (= 19 emacs-major-version)))
+(defvar font-latex-is-XEmacs20
+  (and (not (null (save-match-data 
+                    (string-match "XEmacs\\|Lucid" emacs-version))))
+       (= 20 emacs-major-version)))
+(defvar font-latex-is-Emacs19
+  (and (not font-latex-is-XEmacs19) 
+       (not font-latex-is-XEmacs20) 
+       (= 19 emacs-major-version)))
+(defvar font-latex-is-Emacs20
+  (and (not font-latex-is-XEmacs19) 
+       (not font-latex-is-XEmacs20) 
+       (= 20 emacs-major-version)))
 
 (defvar font-latex-string-face nil
   "Face to use for strings.  This is set by Font LaTeX.")
   (require 'cl))
 
 (cond
- ((not font-latex-is-XEmacs)
-  ;;; emacs:
-  ;; Otherwise I overwrite fock-lock-face-attributes.
-  ;; font-lock.el needs a better way to add these faces!        
+ ((or font-latex-is-Emacs20 
+      font-latex-is-XEmacs20)
+  (defface font-latex-bold-face
+    '((((class grayscale) (background light)) (:foreground "DimGray" :bold t))
+      (((class grayscale) (background dark)) (:foreground "LightGray" :bold t))
+      (((class color) (background light)) 
+       (:foreground "DarkOliveGreen" :bold t ))
+      (((class color) (background dark)) (:foreground "OliveDrab" :bold t ))
+      (t (:bold t)))
+    "Font Lock mode face used to bold LaTeX."
+    :group 'font-latex-highlighting-faces)
+  
+  (defface font-latex-italic-face
+    '((((class grayscale) (background light)) 
+       (:foreground "DimGray" :italic t))
+      (((class grayscale) (background dark)) 
+       (:foreground "LightGray" :italic t))
+      (((class color) (background light)) 
+       (:foreground "DarkOliveGreen" :italic t ))
+      (((class color) (background dark)) 
+       (:foreground "OliveDrab" :italic t ))
+      (t (:italic t)))
+    "Font Lock mode face used to highlight italic LaTeX."
+    :group 'font-latex-highlighting-faces)
+
+  (defface font-latex-math-face
+    '((((class grayscale) (background light)) 
+       (:foreground "DimGray" :underline t))
+      (((class grayscale) (background dark)) 
+       (:foreground "LightGray" :underline t))
+      (((class color) (background light)) (:foreground "SaddleBrown"))
+      (((class color) (background dark))  (:foreground "burlywood"))
+      (t (:underline t)))
+    "Font Lock mode face used to highlight math in LaTeX."
+    :group 'font-latex-highlighting-faces)
+
+  (defface font-latex-sedate-face
+    '((((class grayscale) (background light)) (:foreground "DimGray"))
+      (((class grayscale) (background dark))  (:foreground "LightGray"))
+      (((class color) (background light)) (:foreground "DimGray"))
+      (((class color) (background dark))  (:foreground "LightGray"))
+   ;;;(t (:underline t))
+      )
+    "Font Lock mode face used to highlight sedate stuff in LaTeX."
+    :group 'font-latex-highlighting-faces)
+
+  (copy-face 'font-lock-string-face 'font-latex-string-face)
+  (if font-latex-is-Emacs20
+      (copy-face 'font-lock-warning-face 'font-latex-warning-face)
+    (defface font-latex-warning-face
+      '((((class grayscale)(background light))(:foreground "DimGray" :bold t))
+        (((class grayscale)(background dark))(:foreground "LightGray" :bold t))
+        (((class color)(background light))(:foreground "red" :bold t ))
+        (((class color)(background dark))(:foreground "red" :bold t ))
+        (t (:bold t)))
+      "Font Lock for LaTeX major keywords."
+      :group 'font-latex-highlighting-faces)))
+ (font-latex-is-Emacs19
   (if (not font-lock-face-attributes)
+      ;; Otherwise I overwrite fock-lock-face-attributes.
       (font-lock-make-faces))
   (unless (assq 'font-latex-sedate-face font-lock-face-attributes)
     (cond 
                  ;;;       Should I change to a (copy-face) scheme?
             '((font-latex-bold-face "DarkOliveGreen" nil t nil nil)
               (font-latex-italic-face "DarkOliveGreen" nil nil t nil)
-              (font-latex-math-face "green4")
+              (font-latex-math-face "SaddleBrown")
               (font-latex-sedate-face "grey50")
               (font-latex-warning-face "red" nil t nil nil)))))
     (t			; dark colour background
      (setq font-lock-face-attributes
            (append 
             font-lock-face-attributes
-            '((font-latex-bold-face "OliveGreen" nil t nil nil)
-              (font-latex-italic-face "OliveGreen" nil nil t nil)
-              (font-latex-math-face "LightSeaGreen")
+            '((font-latex-bold-face "OliveDrab" nil t nil nil)
+              (font-latex-italic-face "OliveDrab" nil nil t nil)
+              (font-latex-math-face "burlywood")
 	      ;; good are > LightSeaGreen, LightCoral, coral, orchid, orange
               (font-latex-sedate-face "grey60")
               (font-latex-warning-face "red" nil t nil nil))))))))
  (t
-  ;;; XEmacs:
+  ;;; XEmacs19:
   (make-face 'font-latex-string-face "Face to use for LaTeX string.")
   (copy-face 'font-lock-string-face 'font-latex-string-face)
 
   "Setup this buffer for LaTeX font-lock.  Usually called from a hook."
   ;; Trickery to make $$ fontification be in `font-latex-math-face' while
   ;; strings get whatever `font-lock-string-face' has been set to.
-  (if font-latex-is-XEmacs
-      ;; Cool patch from Christoph Wedler...
-      (let (instance)
-	(mapcar (function
-		 (lambda (property)
-		   (setq instance
-			 (face-property-instance 'font-latex-math-face property
-						 nil 0 t))
-		   (if (numberp instance)
-		       (setq instance
-			     (face-property-instance 'default property nil 0)))
-		   (or (numberp instance)
-		       (set-face-property 'font-lock-string-face property
-                                          instance (current-buffer)))))
-		(built-in-face-specifiers)))
+  (cond
+   (font-latex-is-Emacs20
+    (make-local-variable 'font-lock-string-face)
+    (setq font-lock-string-face font-latex-math-face
+	  font-latex-string-face (default-value 'font-lock-string-face))
+    ;; Tell Font Lock about the support.
+    (make-local-variable 'font-lock-defaults)
+    ;; Parentheses () disabled because they should not delimit fontification
+    ;; in LaTeX text.
+    (setq font-lock-defaults
+	  '((font-latex-keywords font-latex-keywords-1 font-latex-keywords-2)
+	    nil nil ((?\( . ".") (?\) . ".") (?$ . "\"")) nil
+	    (font-lock-comment-start-regexp . "%")
+	    (font-lock-mark-block-function . mark-paragraph))))
+   ((or font-latex-is-XEmacs19 font-latex-is-XEmacs20)
+    ;; Cool patch from Christoph Wedler...
+    (let (instance)
+      (mapcar (function
+	       (lambda (property)
+		 (setq instance
+		       (face-property-instance 'font-latex-math-face property
+					       nil 0 t))
+		 (if (numberp instance)
+		     (setq instance
+			   (face-property-instance 'default property nil 0)))
+		 (or (numberp instance)
+		     (set-face-property 'font-lock-string-face property
+					instance (current-buffer)))))
+	      (built-in-face-specifiers))))
+   (t
     (font-lock-make-faces)
     (make-local-variable 'font-lock-string-face)
     (setq font-lock-string-face font-latex-math-face
 	  font-latex-string-face (default-value 'font-lock-string-face))
     ;; Tell Font Lock about the support.
     (make-local-variable 'font-lock-defaults)
-    ;; Parentheses () are disabled because they should not delimit fontification
+    ;; Parentheses () disabled because they should not delimit fontification
     ;; in LaTeX text.
     (setq font-lock-defaults
 	  '((font-latex-keywords font-latex-keywords-1 font-latex-keywords-2)
 	    nil nil ((?\( . ".") (?\) . ".") (?$ . "\"")) nil
 	    (font-lock-comment-start-regexp . "%")
-	    (font-lock-mark-block-function . mark-paragraph)))))
+	    (font-lock-mark-block-function . mark-paragraph))))))
 
-(when font-latex-is-XEmacs
+(when (or font-latex-is-XEmacs19 font-latex-is-XEmacs20)
     (put 'latex-mode 'font-lock-defaults
          '((font-latex-keywords font-latex-keywords-1 font-latex-keywords-2)
            nil nil ((?\( . ".") (?\) . ".") (?$ . "\"")) nil
     (put 'japanese-LaTeX-mode 'font-lock-defaults 'latex-mode)
     (put 'LATeX-MoDe	'font-lock-defaults 'latex-mode)
     (put 'lATEx-mODe	'font-lock-defaults 'latex-mode))
-
-(defconst font-latex-keywords-1
-  (list
-   ;; FIXME: Maybe I should put this in a function, use override but let
-   ;;        the function determine if commented-out.
-   (list (concat "\\\\\\(\\(no\\)?pagebreak\\|\\(new\\|clear\\(double\\)?\\)"
-		 "page\\|enlargethispage\\|\\(no\\)?linebreak\\|newline\\|"
-		 "-\\|\\\\\\(\*\\)?\\|displaybreak\\|allowdisplaybreaks\\)")
-	 '(0 font-latex-warning-face))
-   '("\\$\\$\\([^$]+\\)\\$\\$" 1 font-latex-math-face)        ;;; $$...$$
-   '(font-latex-match-quotation . font-latex-string-face)     ;;; ``...''
-   '(font-latex-match-font-outside-braces		      ;;;\textit{text}
-     (0 font-lock-keyword-face
-        append                         ;Override? [t 'keep 'prepend 'append]
-        ;; Can't use prepend because that overwrites syntax fontification
-        ;; e.g. comments.
-        t)                              ;Laxmatch? if t, do not signal error
-     (1 font-latex-italic-face append t)
-     (2 font-latex-bold-face append t)
-     (3 font-lock-type-face append t))
-   '(font-latex-match-font-inside-braces		      ;;;{\it text}
-     (0 font-lock-keyword-face append t)
-     (1 font-latex-italic-face append t)
-     (2 font-latex-bold-face append t)
-     (3 font-lock-type-face append t)))
-  "Subdued level highlighting for LaTeX modes.")
-
-(defconst font-latex-keywords-2
-  (append font-latex-keywords-1
-   '((font-latex-match-reference                              ;;;\cite
-      (0 font-lock-keyword-face append t)
-      (1 font-lock-variable-name-face append t)              ;;;    [opt]
-      (2 font-lock-reference-face append t))                 ;;;         {key}
-     (font-latex-match-function                               ;;;\section
-      (0 font-lock-keyword-face append t)
-      (1 font-lock-variable-name-face append t)              ;;;   [opt]
-      (2 font-lock-function-name-face append t))             ;;;        {text}
-     (font-latex-match-variable
-      (0 font-lock-keyword-face nil t)
-      (1 font-lock-variable-name-face nil t)
-      (2 font-lock-variable-name-face nil t))
-     (font-latex-match-math-env 
-      (0 font-latex-math-face append t))         	      ;;;\(...\)
-     (font-latex-match-math-envII                             ;;;Math environ.
-      (0 font-latex-math-face append t))
-     ("\\\\[@A-Za-z]+"                                        ;;;Other commands
-      (0 font-latex-sedate-face append))))
-  "High level highlighting for LaTeX modes.")
-
-(defvar font-latex-keywords font-latex-keywords-1
-  "Default expressions to highlight in TeX mode.")
 
 
 (defun font-latex-match-reference (limit)
-  (font-latex-match-command-outside-arguments
-   (eval-when-compile
-     (concat "\\\\" "\\("
-	     (mapconcat 'identity 
-	      '("[A-Za-z]*cite[A-Za-z]*" "label" "\\(page\\|v\\|eq\\)?ref"
-		"index" "glossary" "\\(footnote\\(mark\\|text\\)?\\)")
-	      "\\|")
-      "\\)\\>"))
-   limit nil nil))
+  (if font-latex-match-reference
+      (font-latex-match-command-outside-arguments font-latex-match-reference
+;;;   (eval-when-compile
+;;;     (concat "\\\\" "\\("
+;;;             (mapconcat 'identity 
+;;;              '("[A-Za-z]*cite[A-Za-z]*" "label" "\\(page\\|v\\|eq\\)?ref"
+;;;                "index" "glossary" "\\(footnote\\(mark\\|text\\)?\\)")
+;;;              "\\|")
+;;;      "\\)\\>"))
+                                                  limit nil nil)))
 
 (defun font-latex-match-function (limit)
+  "Fontify things like \\documentclass{article}"
+  (if font-latex-match-function
+      (font-latex-match-command-outside-arguments font-latex-match-function
+                                                  limit nil t)))
+(defun font-latex-match-textual (limit)
   "Fontify things like \\section{text}"
-  (font-latex-match-command-outside-arguments
-   (eval-when-compile
-     (concat "\\\\" "\\("
-      (mapconcat 'identity 
-       ;; \\*? doesn't work with \\> at the end of the regexp.
-       ;; Instead, allow `*' for all commands (!)
-       '("item" ;;;FIXME: does not have an {arg} so should treated elsewhere.
-         "include" "input" "bibliography"
-	 "part" "chapter" "\\(sub\\)*section" "\\(sub\\)*paragraph"
-	 "begin" "end"
-	 "title" "author" "date" "thanks" "address"
-	 "pagenumbering"
-	 "\\(this\\)?pagestyle"
-	 "nofiles" "includeonly"
-	 "bibliographystyle" "\\(document\\(style\\|class\\)\\)"
-     "\\(re\\)?new\\(environment\\|command\\|length\\|theorem\\|counter\\)"
-	 "usepackage" "caption" "\\(f\\|m\\|s\\)box" "\\(v\\|h\\)space")
-       "\\|")
-      "\\)\\>"))
-   limit nil t))
-
+  (if font-latex-match-textual
+      (font-latex-match-command-outside-arguments font-latex-match-textual
+                                                  limit nil t)))
 (defun font-latex-match-variable (limit)
   "Fontify things like \\newcommand{stuff}"
-  (font-latex-match-command-outside-arguments
-   (eval-when-compile
-     (concat "\\\\" "\\("
-	     "set\\(length\\|towidth\\|counter\\)\\|"
-	     "addto\\(length\\|counter\\)"
-             "\\)\\>"))
-   limit t nil))
+  (if font-latex-match-variable
+      (font-latex-match-command-outside-arguments font-latex-match-variable
+                                                  limit t nil)))
 
+;; font-latex-find-matching-close is a little helper function which
+;; is used like scan-sexp.  It skips over matching
+;; pairs of '{' and '}'.  As an added benefit, it ignores any characters
+;; which occur after the tex comment character %.
+(defun font-latex-find-matching-close (closechar)
+"*Skip over matching pairs of '{' and '}', ignoring
+any characters in comments, until closechar is found.  If the end of file
+is reached, return nil."
+  (save-excursion
+    (save-match-data
+      (let ((mycount 1))
+	(while (and (> mycount 0)
+		    (progn
+		      (backward-char 1)
+		      (re-search-forward
+		       (concat "[^\\\\]["
+			       ;; closechar might be ]
+			       ;; and therefor must be first in regexp
+			       (char-to-string closechar)
+			       "{}%]")
+		       nil t)))
+	  (if (= (preceding-char) ?%) ;; Found a comment
+	      (forward-line 1)
+	    (setq mycount (if (= (preceding-char) ?{)
+			      (+ mycount 1)
+			    (- mycount 1)))))
+	(if (= mycount 0)
+            (point)
+          (error ""))))))
 
 ;; FIXME: --About font-latex-commented-outp--
 ;; Fontification is *slower* for affected functions (in particular
             t
           nil)))))
 
-(defvar font-latex-match-command-cache-state nil
-  "Cache state of unterminated match to fontify")
 (defvar font-latex-match-command-cache-start nil
   "Cache start of unterminated match to fontify")
+(defvar font-latex-match-command-cache-end nil
+  "Cache end of unterminated match to fontify")
 (defvar font-latex-match-command-cache-limit nil
-  "Cache end of unterminated match to fontify")
+  "Cache limit of unterminated match to fontify")
 (defvar font-latex-match-command-cache-keywords nil
   "Cache keywords of unterminated match to fontify")
-(make-variable-buffer-local 'font-latex-match-command-cache-state)
+(defvar font-latex-match-command-cache-match nil
+  "Cache last match of unterminated match")
+(defvar font-latex-match-command-cache-stop nil
+  "Flag to not move point, because same pattern was returned twice to fontify")
+
 (make-variable-buffer-local 'font-latex-match-command-cache-start)
+(make-variable-buffer-local 'font-latex-match-command-cache-end)
 (make-variable-buffer-local 'font-latex-match-command-cache-limit)
 (make-variable-buffer-local 'font-latex-match-command-cache-keywords)
+(make-variable-buffer-local 'font-latex-match-command-cache-match)
+(make-variable-buffer-local 'font-latex-match-command-cache-stop)
 
 ;; FIXME - Note to myself 
 ;; In call to font-latex-match-command-outside-arguments, I could arrange
  subexpression 1 is the contents of any following [...] forms 
  subexpression 2 is the contents of any following {...} forms.  
 Returns nil if none of KEYWORDS is found."
-  ;; Prior incomplete match?
-  (if font-latex-match-command-cache-state
-      (setq font-latex-match-command-cache-state nil) ;Stop now!
-    (when (and font-latex-match-command-cache-keywords
-               (equal font-latex-match-command-cache-keywords keywords)
-               (>= font-latex-match-command-cache-limit (point))
-               (<  font-latex-match-command-cache-start (point)))
-      (goto-char font-latex-match-command-cache-start)
-      (setq font-latex-match-command-cache-state 'stop)) ;Can only do once
-    (when (re-search-forward keywords limit t)
-      (let ((this-start (match-beginning 0)))
-        (cond
-         ((font-latex-commented-outp)
-          ;; Return a nul match such that we skip over this pattern.
-          ;; (Would be better to skip over internally to this function)
-          (store-match-data (list nil nil))
-          t)
-         (t
-          (let ((kbeg (match-beginning 0))
-                (kend (+ (match-end 0) 
-                         (if (and asterix (eq (following-char) ?\*)) 1 0))) 
-                sbeg send cbeg cend)
-            (goto-char kend)            ;May be moved by asterix
-            (while (eq (following-char) ?\[)
-              (save-restriction
-                ;; Restrict to LIMIT.
-                (narrow-to-region (point-min) limit)
-                (setq sbeg (1+ kend))
-                (if (condition-case nil
-                        (goto-char (or (scan-sexps (point) 1) (point-max)))
-                      (error))
-                    (setq send (1- (point)))
-                  (setq send (point-max))
-                  (goto-char send)
-                  (setq font-latex-match-command-cache-state 'stop))))
+  (when (and font-latex-match-command-cache-keywords
+             font-latex-match-command-cache-limit
+             font-latex-match-command-cache-start
+             font-latex-match-command-cache-end
+             (equal font-latex-match-command-cache-keywords keywords)
+             (< font-latex-match-command-cache-start (point))
+             (not (= font-latex-match-command-cache-end (point)))
+             (> font-latex-match-command-cache-limit (point))
+             (not font-latex-match-command-cache-stop))
+    (goto-char font-latex-match-command-cache-start))
+  (setq font-latex-match-command-cache-stop nil)
+  (when (re-search-forward keywords limit t)
+    (cond
+     ((font-latex-commented-outp)
+      ;; Return a nul match such that we skip over this pattern.
+      ;; (Would be better to skip over internally to this function)
+      (store-match-data (list nil nil))
+      t)
+     (t
+      (let ((kbeg (match-beginning 0)) 
+            kend sbeg send cbeg cend
+            cache-reset
+            (parse-sexp-ignore-comments t)) ; scan-sexps ignores comments
+        (goto-char (match-end 0))
+        (if (and asterix (eq (following-char) ?\*))
+            (forward-char 1)) 
+        (skip-chars-forward " \n\t" limit)
+        (setq kend (point))
+        (while (eq (following-char) ?\[)
+          (setq sbeg kend)
+          (save-restriction
+            ;; Restrict to LIMIT.
+            (narrow-to-region (point-min) limit)
+            (if (condition-case nil
+                    (goto-char (or (scan-sexps (point) 1)
+                                ;;;(font-latex-find-matching-close ?\])
+                                   (point-max)))
+                  (error))
+                (setq send (point))
+              (setq cache-reset t)
+              (setq send (point-max))
+              (goto-char send))))
+        (skip-chars-forward " \n\t" limit)
+        (when (eq (following-char) ?\{)
+          (setq cbeg (point))
+          (save-restriction
+            ;; Restrict to LIMIT.
+            (narrow-to-region (point-min) limit)
+            (if (condition-case nil
+                    (goto-char (or (scan-sexps (point) 1) (point-max)))
+                  (error))
+                (setq cend (point))
+              (setq cache-reset t)
+              (setq cend (point-max))
+              (goto-char cend)))
+          (when twoargs
+            (skip-chars-forward " \n\t" limit)
             (when (eq (following-char) ?\{)
               (save-restriction
                 ;; Restrict to LIMIT.
                 (narrow-to-region (point-min) limit)
-                (setq cbeg (1+ (point)))
-                (if (condition-case nil
-                        (goto-char (or (scan-sexps (point) 1) (point-max)))
-                      (error))
-                    (setq cend (1- (point)))
-                  (setq cend (point-max))
-                  (goto-char cend)
-                  (setq font-latex-match-command-cache-state 'stop))))
-            (when (and twoargs (eq (following-char) ?\{))
-              (save-restriction
-                ;; Restrict to LIMIT.
-                (narrow-to-region (point-min) limit)
                 (if (condition-case nil
                         (goto-char (or (scan-sexps (point) 1) (point-max)))
                       (error))
-                    (setq cend (1- (point)))
+                    (setq cend (point))
+                  (setq cache-reset t)
                   (setq cend (point-max))
-                  (goto-char cend)
-                  (setq font-latex-match-command-cache-state 'stop))))
-            (store-match-data (list kbeg kend sbeg send cbeg cend))
-            (when font-latex-match-command-cache-state
-              (setq font-latex-match-command-cache-start this-start)
-              (setq font-latex-match-command-cache-limit (point))
-              (setq font-latex-match-command-cache-keywords keywords))
-            t)))))))
+                  (goto-char cend))))))
+        (store-match-data (list kbeg kend sbeg send cbeg cend))
 
-(defvar font-latex-match-font-cache-state nil
-  "Cache state of unterminated match to fontify")
+        (when (and font-latex-match-command-cache-keywords ;Sanity check!
+                   (equal font-latex-match-command-cache-keywords keywords)
+                   font-latex-match-command-cache-match
+                   (equal font-latex-match-command-cache-match
+                          (list kbeg kend sbeg send cbeg cend)))
+          (setq font-latex-match-command-cache-stop t))
+        (setq font-latex-match-command-cache-match
+              (list kbeg kend sbeg send cbeg cend))
+
+        (when cache-reset
+          (setq font-latex-match-command-cache-start kbeg
+                font-latex-match-command-cache-end   kend
+                font-latex-match-command-cache-limit (point)
+                font-latex-match-command-cache-keywords keywords))
+          t)))))
+
 (defvar font-latex-match-font-cache-start nil
   "Cache start of unterminated match to fontify")
+(defvar font-latex-match-font-cache-end nil
+  "Cache end of unterminated match to fontify")
 (defvar font-latex-match-font-cache-limit nil
-  "Cache end of unterminated match to fontify")
-(defvar font-latex-match-font-cache-keywords nil
-  "Cache keywords of unterminated match to fontify")
-(make-variable-buffer-local 'font-latex-match-font-cache-state)
+  "Cache limit of unterminated match to fontify")
 (make-variable-buffer-local 'font-latex-match-font-cache-start)
+(make-variable-buffer-local 'font-latex-match-font-cache-end)
 (make-variable-buffer-local 'font-latex-match-font-cache-limit)
-(make-variable-buffer-local 'font-latex-match-font-cache-keywords)
 
 (defun font-latex-match-font-outside-braces (limit)
   "Search for font-changing command like \textbf{fubar} before LIMIT.  
  subexpression 2 is the content to fontify in bold.
  subexpression 3 is the content to fontify in type-face.
 Returns nil if no font-changing command is found."
-  (if font-latex-match-font-cache-state
-      (setq font-latex-match-font-cache-state nil) ;Stop now!
-    (when (and font-latex-match-font-cache-keywords
-               (equal font-latex-match-font-cache-keywords keywords)
-               (>= font-latex-match-font-cache-limit (point))
-               (<  font-latex-match-font-cache-start (point)))
-      (goto-char font-latex-match-font-cache-start)
-      (setq font-latex-match-font-cache-state 'stop)) ;Can only do once
-    (when (re-search-forward
-           (eval-when-compile
-             (concat "\\\\" "\\("
-		     "\\(emph\\)\\|"			      ;;; 2 - italic
-		     "\\(text\\("
-		                "\\(it\\|sl\\)\\|"	      ;;; 5 - italic
-		                "\\(md\\|rm\\|sf\\|tt\\)\\|"  ;;; 6 - type
-		                "\\(bf\\|sc\\|up\\)"	      ;;; 7 - bold
-		     "\\)\\)\\|"
-		     "\\(boldsymbol\\|pmb\\)"		      ;;; 8 - bold
-		     "\\)" "{"))
-           limit t)
-      (cond
-       ((font-latex-commented-outp)
-        ;; Return a nul match such that we skip over this pattern.
-        ;; (Would be better to skip over internally to this function)
-        ;; Using `prepend' won't help here, because the problem is that
-        ;; scan-sexp *fails* to find a commented-out matching bracket!
-        (store-match-data (list nil nil))
-        t)
-       (t
-        (let ((kbeg (match-beginning 0)) (kend (match-end 1)) 
-              (beg  (match-end 0)) end itbeg itend bfbeg bfend ttbeg ttend)
-          (goto-char kend)
-          (save-restriction
-            ;; Restrict to LIMIT.
-            (narrow-to-region (point-min) limit)
-            (if (condition-case nil
-                    (goto-char (or (scan-sexps (point) 1) (point-max)))
-                  (error))
-                (setq end (1- (point)))
-              (setq end (point-max))
-              (goto-char end)
-              (setq font-latex-match-font-cache-state 'stop)))
-          (cond ((or (match-beginning 2) (match-beginning 5))
-                 (setq itbeg beg
-                       itend end))
-                ((match-beginning 6)
-                 (setq ttbeg beg
-                       ttend end))
-                (t
-                 (setq bfbeg beg
-                       bfend end)))
-          (store-match-data 
-           (list kbeg kend itbeg itend bfbeg bfend ttbeg ttend))
-          (when font-latex-match-font-cache-state
-            (setq font-latex-match-font-cache-start kbeg)
-            (setq font-latex-match-font-cache-limit (point))
-            (setq font-latex-match-font-cache-keywords keywords))
-          ;; Start the subsequent search immediately after this keyword.
-          (goto-char kend)
-          t))))))
+  (when (and font-latex-match-font-cache-limit
+             font-latex-match-font-cache-start
+             font-latex-match-font-cache-end
+             (not (= font-latex-match-font-cache-end (point)))
+             (> font-latex-match-font-cache-limit (point))
+             (< font-latex-match-font-cache-start (point)))
+    (goto-char font-latex-match-font-cache-start))
+  (when (re-search-forward
+         (eval-when-compile