*** empty log message ***
[bpt/emacs.git] / lisp / font-lock.el
index 9ce0ade..9bf9f8e 100644 (file)
@@ -1,8 +1,9 @@
 ;;; font-lock.el --- Electric font lock mode
 
-;; Copyright (C) 1992-1999 Free Software Foundation, Inc.
+;; Copyright (C) 1992, 93, 94, 95, 96, 97, 98, 1999, 2000
+;;  Free Software Foundation, Inc.
 
-;; Author: jwz, then rms, then sm <simon@gnu.org>
+;; Author: jwz, then rms, then sm
 ;; Maintainer: FSF
 ;; Keywords: languages, faces
 
 ;;
 ;; Efficient regexps for use as MATCHERs for `font-lock-keywords' and
 ;; `font-lock-syntactic-keywords' can be generated via the function
-;; `regexp-opt', and their depth counted via the function `regexp-opt-depth'.
+;; `regexp-opt'.
 
 ;;; Adding patterns for modes that already support Font Lock:
 
 (defgroup font-lock nil
   "Font Lock mode text highlighting package."
   :link '(custom-manual "(emacs)Font Lock")
+  :link '(custom-manual "(elisp)Font Lock Mode")
   :group 'faces)
 
 (defgroup font-lock-highlighting-faces nil
   :link '(custom-manual "(emacs)Support Modes")
   :load 'lazy-lock
   :group 'font-lock)
+
+(defgroup jit-lock nil
+  "Font Lock support mode to fontify just-in-time."
+  :link '(custom-manual "(emacs)Support Modes")
+  :version "21.1"
+  :load 'jit-lock
+  :group 'font-lock)
 \f
 ;; User variables.
 
@@ -279,7 +288,7 @@ decoration for buffers in C++ mode, and level 1 decoration otherwise."
                                      (symbol :tag "name"))
                               (radio :tag "Decoration"
                                      (const :tag "default" nil)
-                                     (const :tag "maximum" t) 
+                                     (const :tag "maximum" t)
                                      (integer :tag "level" 1)))))
   :group 'font-lock)
 
@@ -323,10 +332,10 @@ MATCH-HIGHLIGHT should be of the form:
 where MATCHER can be either the regexp to search for, or the function name to
 call to make the search (called with one argument, the limit of the search) and
 return non-nil if it succeeds (and set `match-data' appropriately).
-MATCHER regexps can be generated via the function `regexp-opt'.  MATCH is the
-subexpression of MATCHER to be highlighted.  MATCH can be calculated via the
-function `regexp-opt-depth'.  FACENAME is an expression whose value is the face
-name to use.  Face default attributes can be modified via \\[customize].
+MATCHER regexps can be generated via the function `regexp-opt'.  MATCH is
+the subexpression of MATCHER to be highlighted.  FACENAME is an expression
+whose value is the face name to use.  Face default attributes can be
+modified via \\[customize].
 
 OVERRIDE and LAXMATCH are flags.  If OVERRIDE is t, existing fontification can
 be overwritten.  If `keep', only parts not already fontified are highlighted.
@@ -380,9 +389,10 @@ PRE-MATCH-FORM is evaluated, that position is used as the limit of the search.
 It is generally a bad idea to return a position greater than the end of the
 line, i.e., cause the MATCHER search to span lines.
 
-These regular expressions should not match text which spans lines.  While
-\\[font-lock-fontify-buffer] handles multi-line patterns correctly, updating
-when you edit the buffer does not, since it considers text one line at a time.
+These regular expressions can match text which spans lines, although
+it is better to avoid it if possible since updating them while editing
+text is slower, and it is not guaranteed to be always correct when using
+support modes like jit-lock or lazy-lock.
 
 This variable is set by major modes via the variable `font-lock-defaults'.
 Be careful when composing regexps for this list; a poorly written pattern can
@@ -446,7 +456,7 @@ and those for buffer-specialised fontification functions,
           nil nil ((?_ . "w")) beginning-of-defun
           (font-lock-mark-block-function . mark-defun)))
        (c++-mode-defaults
-        '((c++-font-lock-keywords c++-font-lock-keywords-1 
+        '((c++-font-lock-keywords c++-font-lock-keywords-1
            c++-font-lock-keywords-2 c++-font-lock-keywords-3)
           nil nil ((?_ . "w")) beginning-of-defun
           (font-lock-mark-block-function . mark-defun)))
@@ -498,7 +508,13 @@ settings.  See the variable `font-lock-defaults', which takes precedence.")
 
 (defvar font-lock-keywords-alist nil
   "*Alist of `font-lock-keywords' local to a `major-mode'.
-This is normally set via `font-lock-add-keywords'.")
+This is normally set via `font-lock-add-keywords' and
+`font-lock-remove-keywords'.")
+
+(defvar font-lock-removed-keywords-alist nil
+  "*Alist of `font-lock-keywords' removed from `major-mode'.
+This is normally set via `font-lock-add-keywords' and
+`font-lock-remove-keywords'.")
 
 (defvar font-lock-keywords-only nil
   "*Non-nil means Font Lock should not fontify comments or strings.
@@ -516,9 +532,10 @@ the differences are listed below.  MATCH-HIGHLIGHT should be of the form:
 
  (MATCH SYNTAX OVERRIDE LAXMATCH)
 
-where SYNTAX can be of the form (SYNTAX-CODE . MATCHING-CHAR), the name of a
-syntax table, or an expression whose value is such a form or a syntax table.
-OVERRIDE cannot be `prepend' or `append'.
+where SYNTAX can be of the form (SYNTAX-CODE . MATCHING-CHAR) (see
+also `string-to-syntax'), the name of a syntax table, or an expression
+whose value is such a form or a syntax table.  OVERRIDE cannot be
+`prepend' or `append'.
 
 For example, an element of the form highlights syntactically:
 
@@ -585,8 +602,15 @@ This is normally set via `font-lock-defaults'.")
 
 (defvar font-lock-inhibit-thing-lock nil
   "List of Font Lock mode related modes that should not be turned on.
-Currently, valid mode names as `fast-lock-mode' and `lazy-lock-mode'.
-This is normally set via `font-lock-defaults'.")
+Currently, valid mode names are `fast-lock-mode', `jit-lock-mode' and
+`lazy-lock-mode'.  This is normally set via `font-lock-defaults'.")
+
+(defvar font-lock-multiline 'undecided
+  "Whether font-lock should cater to multiline keywords.
+If nil, don't try to handle multiline patterns.
+If t, always handle multiline patterns.
+If `undecided', don't try to handle multiline patterns until you see one.
+Major/minor modes can set this variable if they know which option applies.")
 
 (defvar font-lock-mode nil)            ; Whether we are turned on/modeline.
 (defvar font-lock-fontified nil)       ; Whether we have fontified the buffer.
@@ -615,6 +639,7 @@ This is normally set via `font-lock-defaults'.")
         (when (and (not modified) (buffer-modified-p))
           (set-buffer-modified-p nil)))))
   (put 'save-buffer-state 'lisp-indent-function 1)
+  (def-edebug-spec save-buffer-state let)
   ;;
   ;; Shut up the byte compiler.
   (defvar global-font-lock-mode)       ; Now a defcustom.
@@ -708,13 +733,13 @@ buffer local value for `font-lock-defaults', via its mode hook."
 (defun turn-on-font-lock ()
   "Turn on Font Lock mode conditionally.
 Turn on only if the terminal can display it."
-  (when (and (not font-lock-mode) window-system)
+  (unless font-lock-mode
     (font-lock-mode)))
 
 ;;;###autoload
-(defun font-lock-add-keywords (major-mode keywords &optional append)
-  "Add highlighting KEYWORDS for MAJOR-MODE.
-MAJOR-MODE should be a symbol, the major mode command name, such as `c-mode'
+(defun font-lock-add-keywords (mode keywords &optional append)
+  "Add highlighting KEYWORDS for MODE.
+MODE should be a symbol, the major mode command name, such as `c-mode'
 or nil.  If nil, highlighting keywords are added for the current buffer.
 KEYWORDS should be a list; see the variable `font-lock-keywords'.
 By default they are added at the beginning of the current highlighting list.
@@ -734,23 +759,126 @@ comments, and to fontify `and', `or' and `not' words as keywords.
 Note that some modes have specialised support for additional patterns, e.g.,
 see the variables `c-font-lock-extra-types', `c++-font-lock-extra-types',
 `objc-font-lock-extra-types' and `java-font-lock-extra-types'."
-  (cond (major-mode
-        ;; If MAJOR-MODE is non-nil, add the KEYWORDS and APPEND spec to
+  (cond (mode
+        ;; If MODE is non-nil, add the KEYWORDS and APPEND spec to
         ;; `font-lock-keywords-alist' so `font-lock-set-defaults' uses them.
         (let ((spec (cons keywords append)) cell)
-          (if (setq cell (assq major-mode font-lock-keywords-alist))
-              (setcdr cell (append (cdr cell) (list spec)))
-            (push (list major-mode spec) font-lock-keywords-alist))))
-       (font-lock-mode
-        ;; Otherwise if Font Lock mode is on, set or add the keywords now.
-        (if (eq append 'set)
-            (setq font-lock-keywords keywords)
+          (if (setq cell (assq mode font-lock-keywords-alist))
+              (if (eq append 'set)
+                  (setcdr cell (list spec))
+                (setcdr cell (append (cdr cell) (list spec))))
+            (push (list mode spec) font-lock-keywords-alist)))
+        ;; Make sure that `font-lock-removed-keywords-alist' does not
+        ;; contain the new keywords.
+        (font-lock-update-removed-keyword-alist mode keywords append))
+       (t
+        ;; Otherwise set or add the keywords now.
+        (font-lock-set-defaults)
+        (if (eq append 'set)
+            (setq font-lock-keywords keywords)
+          (font-lock-remove-keywords nil keywords) ;to avoid duplicates
           (let ((old (if (eq (car-safe font-lock-keywords) t)
                          (cdr font-lock-keywords)
                        font-lock-keywords)))
             (setq font-lock-keywords (if append
                                          (append old keywords)
                                        (append keywords old))))))))
+
+(defun font-lock-update-removed-keyword-alist (mode keywords append)
+  ;; Update `font-lock-removed-keywords-alist' when adding new
+  ;; KEYWORDS to MODE.
+  ;;
+  ;; When font-lock is enabled first all keywords in the list
+  ;; `font-lock-keywords-alist' are added, then all keywords in the
+  ;; list `font-lock-removed-keywords-alist' are removed.  If a
+  ;; keyword was once added, removed, and then added again it must be
+  ;; removed from the removed-keywords list.  Otherwise the second add
+  ;; will not take effect.
+  (let ((cell (assq mode font-lock-removed-keywords-alist)))
+    (if cell
+       (if (eq append 'set)
+           ;; A new set of keywords is defined.  Forget all about
+           ;; our old keywords that should be removed.
+           (setq font-lock-removed-keywords-alist
+                 (delq cell font-lock-removed-keywords-alist))
+         ;; Delete all previously removed keywords.
+         (dolist (kword keywords)
+           (setcdr cell (delete kword (cdr cell))))
+         ;; Delete the mode cell if empty.
+         (if (null (cdr cell))
+             (setq font-lock-removed-keywords-alist
+                   (delq cell font-lock-removed-keywords-alist)))))))
+
+;; Written by Anders Lindgren <andersl@andersl.com>.
+;;
+;; Case study:
+;; (I)  The keywords are removed from a major mode.
+;;      In this case the keyword could be local (i.e. added earlier by
+;;      `font-lock-add-keywords'), global, or both.
+;;
+;;      (a) In the local case we remove the keywords from the variable
+;;          `font-lock-keywords-alist'.
+;;
+;;      (b) The actual global keywords are not known at this time.
+;;          All keywords are added to `font-lock-removed-keywords-alist',
+;;          when font-lock is enabled those keywords are removed.
+;;
+;;      Note that added keywords are taken out of the list of removed
+;;      keywords.  This ensure correct operation when the same keyword
+;;      is added and removed several times.
+;;
+;; (II) The keywords are removed from the current buffer.
+;;;###autoload
+(defun font-lock-remove-keywords (mode keywords)
+  "Remove highlighting KEYWORDS for MODE.
+
+MODE should be a symbol, the major mode command name, such as `c-mode'
+or nil.  If nil, highlighting keywords are removed for the current buffer."
+  (cond (mode
+        ;; Remove one keyword at the time.
+        (dolist (keyword keywords)
+          (let ((top-cell (assq mode font-lock-keywords-alist)))
+            ;; If MODE is non-nil, remove the KEYWORD from
+            ;; `font-lock-keywords-alist'.
+            (when top-cell
+              (dolist (keyword-list-append-pair (cdr top-cell))
+                ;; `keywords-list-append-pair' is a cons with a list of
+                ;; keywords in the car top-cell and the original append
+                ;; argument in the cdr top-cell.
+                (setcar keyword-list-append-pair
+                        (delete keyword (car keyword-list-append-pair))))
+              ;; Remove keyword list/append pair when the keyword list
+              ;; is empty and append doesn't specify `set'.  (If it
+              ;; should be deleted then previously deleted keywords
+              ;; would appear again.)
+              (let ((cell top-cell))
+                (while (cdr cell)
+                  (if (and (null (car (car (cdr cell))))
+                           (not (eq (cdr (car (cdr cell))) 'set)))
+                      (setcdr cell (cdr (cdr cell)))
+                    (setq cell (cdr cell)))))
+              ;; Final cleanup, remove major mode cell if last keyword
+              ;; was deleted.
+              (if (null (cdr top-cell))
+                  (setq font-lock-keywords-alist
+                        (delq top-cell font-lock-keywords-alist))))
+            ;; Remember the keyword in case it is not local.
+            (let ((cell (assq mode font-lock-removed-keywords-alist)))
+              (if cell
+                  (unless (member keyword (cdr cell))
+                    (nconc cell (list keyword)))
+                (push (cons mode (list keyword))
+                      font-lock-removed-keywords-alist))))))
+       (t
+        ;; Otherwise remove it immediately.
+        (font-lock-set-defaults)
+        (setq font-lock-keywords (copy-sequence font-lock-keywords))
+        (dolist (keyword keywords)
+          (setq font-lock-keywords
+                (delete keyword
+                        ;; The keywords might be compiled.
+                        (delete (font-lock-compile-keyword keyword)
+                                font-lock-keywords)))))))
 \f
 ;;; Global Font Lock mode.
 
@@ -838,6 +966,7 @@ turned on in a buffer if its major mode is one of `font-lock-global-modes'."
 ;; This variable was originally a `defvar' to keep track of
 ;; whether Global Font Lock mode was turned on or not.  As a `defcustom' with
 ;; special `:set' and `:require' forms, we can provide custom mode control.
+;;;###autoload
 (defcustom global-font-lock-mode nil
   "Toggle Global Font Lock mode.
 When Global Font Lock mode is enabled, Font Lock mode is automagically
@@ -846,13 +975,14 @@ Setting this variable directly does not take effect;
 use either \\[customize] or the function `global-font-lock-mode'."
   :set (lambda (symbol value)
         (global-font-lock-mode (or value 0)))
+  :initialize 'custom-initialize-default
   :type 'boolean
   :group 'font-lock
   :require 'font-lock)
 
 (defcustom font-lock-global-modes t
   "*Modes for which Font Lock mode is automagically turned on.
-Global Font Lock mode is controlled by the `global-font-lock-mode' command.
+Global Font Lock mode is controlled by the command `global-font-lock-mode'.
 If nil, means no modes have Font Lock mode automatically turned on.
 If t, all modes that support Font Lock mode have it automatically turned on.
 If a list, it should be a list of `major-mode' symbol names for which Font Lock
@@ -911,11 +1041,12 @@ means that Font Lock mode is turned on for buffers in C and C++ modes only."
 ;; `font-lock-after-fontify-buffer' and/or `font-lock-after-unfontify-buffer'
 ;; themselves.
 
-(defcustom font-lock-support-mode nil
+(defcustom font-lock-support-mode 'jit-lock-mode
   "*Support mode for Font Lock mode.
 Support modes speed up Font Lock mode by being choosy about when fontification
-occurs.  Known support modes are Fast Lock mode (symbol `fast-lock-mode') and
-Lazy Lock mode (symbol `lazy-lock-mode').  See those modes for more info.
+occurs.  Known support modes are Fast Lock mode (symbol `fast-lock-mode'),
+Lazy Lock mode (symbol `lazy-lock-mode'), and Just-in-time Lock mode (symbol
+`jit-lock-mode'.  See those modes for more info.
 If nil, means support for Font Lock mode is never performed.
 If a symbol, use that support mode.
 If a list, each element should be of the form (MAJOR-MODE . SUPPORT-MODE),
@@ -928,6 +1059,7 @@ The value of this variable is used when Font Lock mode is turned on."
   :type '(choice (const :tag "none" nil)
                 (const :tag "fast lock" fast-lock-mode)
                 (const :tag "lazy lock" lazy-lock-mode)
+                (const :tag "jit lock" jit-lock-mode)
                 (repeat :menu-tag "mode specific" :tag "mode specific"
                         :value ((t . lazy-lock-mode))
                         (cons :tag "Instance"
@@ -937,35 +1069,52 @@ The value of this variable is used when Font Lock mode is turned on."
                               (radio :tag "Support"
                                      (const :tag "none" nil)
                                      (const :tag "fast lock" fast-lock-mode)
-                                     (const :tag "lazy lock" lazy-lock-mode)))
+                                     (const :tag "lazy lock" lazy-lock-mode)
+                                     (const :tag "JIT lock" jit-lock-mode)))
                         ))
   :group 'font-lock)
 
 (defvar fast-lock-mode nil)
 (defvar lazy-lock-mode nil)
+(defvar jit-lock-mode nil)
 
 (defun font-lock-turn-on-thing-lock ()
   (let ((thing-mode (font-lock-value-in-major-mode font-lock-support-mode)))
     (cond ((eq thing-mode 'fast-lock-mode)
           (fast-lock-mode t))
          ((eq thing-mode 'lazy-lock-mode)
-          (lazy-lock-mode t)))))
+          (lazy-lock-mode t))
+         ((eq thing-mode 'jit-lock-mode)
+          (jit-lock-mode t)))))
 
 (defun font-lock-turn-off-thing-lock ()
   (cond (fast-lock-mode
         (fast-lock-mode nil))
+       (jit-lock-mode
+        (jit-lock-mode nil))
        (lazy-lock-mode
         (lazy-lock-mode nil))))
 
 (defun font-lock-after-fontify-buffer ()
   (cond (fast-lock-mode
         (fast-lock-after-fontify-buffer))
+       ;; Useless now that jit-lock intercepts font-lock-fontify-buffer.  -sm
+       ;; (jit-lock-mode
+       ;;  (jit-lock-after-fontify-buffer))
        (lazy-lock-mode
         (lazy-lock-after-fontify-buffer))))
 
 (defun font-lock-after-unfontify-buffer ()
   (cond (fast-lock-mode
         (fast-lock-after-unfontify-buffer))
+       ;; Useless as well.  It's only called when:
+       ;; - turning off font-lock: it does not matter if we leave spurious
+       ;;   `fontified' text props around since jit-lock-mode is also off.
+       ;; - font-lock-default-fontify-buffer fails: this is not run
+       ;;   any more anyway.   -sm
+       ;; 
+       ;; (jit-lock-mode
+       ;;  (jit-lock-after-unfontify-buffer))
        (lazy-lock-mode
         (lazy-lock-after-unfontify-buffer))))
 
@@ -1014,7 +1163,7 @@ The value of this variable is used when Font Lock mode is turned on."
 
 ;;;###autoload
 (defun font-lock-fontify-buffer ()
-  "Fontify the current buffer the way `font-lock-mode' would."
+  "Fontify the current buffer the way the function `font-lock-mode' would."
   (interactive)
   (let ((font-lock-verbose (or font-lock-verbose (interactive-p))))
     (funcall font-lock-fontify-buffer-function)))
@@ -1071,6 +1220,23 @@ The value of this variable is used when Font Lock mode is turned on."
          ;; Use the fontification syntax table, if any.
          (when font-lock-syntax-table
            (set-syntax-table font-lock-syntax-table))
+         ;; check to see if we should expand the beg/end area for
+         ;; proper multiline matches
+         (setq beg (if (get-text-property beg 'font-lock-multiline)
+                       ;; if the text-property is non-nil, (1+ beg)
+                       ;; is valid.  We need to use (1+ beg) for the
+                       ;; case where (get-text-property (1- beg)) is nil
+                       ;; in which case we want to keep BEG but
+                       ;; previous-single-property-change will return
+                       ;; the previous change (if any) rather than
+                       ;; the one at BEG.
+                       (or (previous-single-property-change
+                            (1+ beg) 'font-lock-multiline)
+                           (point-min))
+                     beg))
+         (setq end (or (text-property-any end (point-max)
+                                          'font-lock-multiline nil)
+                       (point-max)))
          ;; Now do the fontification.
          (font-lock-unfontify-region beg end)
          (when font-lock-syntactic-keywords
@@ -1084,16 +1250,17 @@ The value of this variable is used when Font Lock mode is turned on."
 ;; The following must be rethought, since keywords can override fontification.
 ;      ;; Now scan for keywords, but not if we are inside a comment now.
 ;      (or (and (not font-lock-keywords-only)
-;             (let ((state (parse-partial-sexp beg end nil nil 
+;             (let ((state (parse-partial-sexp beg end nil nil
 ;                                              font-lock-cache-state)))
 ;               (or (nth 4 state) (nth 7 state))))
 ;        (font-lock-fontify-keywords-region beg end))
 
 (defun font-lock-default-unfontify-region (beg end)
   (save-buffer-state nil
-    (if font-lock-syntactic-keywords
-       (remove-text-properties beg end '(face nil syntax-table nil))
-      (remove-text-properties beg end '(face nil)))))
+    (remove-text-properties beg end
+                           (if font-lock-syntactic-keywords
+                               '(face nil syntax-table nil font-lock-multiline nil)
+                             '(face nil font-lock-multiline nil)))))
 
 ;; Called when any modification is made to buffer text.
 (defun font-lock-after-change-function (beg end old-len)
@@ -1232,8 +1399,10 @@ see `font-lock-syntactic-keywords'."
         (start (match-beginning match)) (end (match-end match))
         (value (nth 1 highlight))
         (override (nth 2 highlight)))
-    (unless (numberp (car-safe value))
-      (setq value (eval value)))
+    (cond ((stringp value)
+          (setq value (string-to-syntax value)))
+         ((not (numberp (car-safe value)))
+          (setq value (eval value))))
     (cond ((not start)
           ;; No match but we might not signal an error.
           (or (nth 3 highlight)
@@ -1259,7 +1428,7 @@ LIMIT can be modified by the value of its PRE-MATCH-FORM."
     ;; Set LIMIT to value of PRE-MATCH-FORM or the end of line.
     (if (and (numberp pre-match-value) (> pre-match-value (point)))
        (setq limit pre-match-value)
-      (save-excursion (end-of-line) (setq limit (point))))
+      (setq limit (line-end-position)))
     (save-match-data
       ;; Find an occurrence of `matcher' before `limit'.
       (while (if (stringp matcher)
@@ -1349,8 +1518,8 @@ START should be at the beginning of a line."
     (when (or (nth 3 state) (nth 4 state))
       (setq string (nth 3 state) beg (point))
       (setq state (parse-partial-sexp (point) end nil nil state 'syntax-table))
-      (put-text-property beg (point) 'face 
-                        (if string 
+      (put-text-property beg (point) 'face
+                        (if string
                             font-lock-string-face
                           font-lock-comment-face)))
     ;;
@@ -1362,8 +1531,8 @@ START should be at the beginning of a line."
                  (or (nth 3 state) (nth 4 state))))
       (setq string (nth 3 state) beg (nth 8 state))
       (setq state (parse-partial-sexp (point) end nil nil state 'syntax-table))
-      (put-text-property beg (point) 'face 
-                        (if string 
+      (put-text-property beg (point) 'face
+                        (if string
                             font-lock-string-face
                           font-lock-comment-face)))))
 
@@ -1403,17 +1572,26 @@ HIGHLIGHT should be of the form MATCH-HIGHLIGHT, see `font-lock-keywords'."
 KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords',
 LIMIT can be modified by the value of its PRE-MATCH-FORM."
   (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights
+       (lead-start (match-beginning 0))
        ;; Evaluate PRE-MATCH-FORM.
        (pre-match-value (eval (nth 1 keywords))))
     ;; Set LIMIT to value of PRE-MATCH-FORM or the end of line.
-    (if (and (numberp pre-match-value) (> pre-match-value (point)))
-       (setq limit pre-match-value)
-      (save-excursion (end-of-line) (setq limit (point))))
+    (if (not (and (numberp pre-match-value) (> pre-match-value (point))))
+       (setq limit (line-end-position))
+      (setq limit pre-match-value)
+      (when (and font-lock-multiline
+                (funcall (if (eq font-lock-multiline t) '>= '>)
+                         pre-match-value
+                         (save-excursion (forward-line 1) (point))))
+       ;; this is a multiline anchored match
+       (setq font-lock-multiline t)
+       (put-text-property (point) limit 'font-lock-multiline t)))
     (save-match-data
       ;; Find an occurrence of `matcher' before `limit'.
-      (while (if (stringp matcher)
-                (re-search-forward matcher limit t)
-              (funcall matcher limit))
+      (while (and (< (point) limit)
+                 (if (stringp matcher)
+                     (re-search-forward matcher limit t)
+                   (funcall matcher limit)))
        ;; Apply each highlight to this instance of `matcher'.
        (setq highlights lowdarks)
        (while highlights
@@ -1440,9 +1618,20 @@ START should be at the beginning of a line."
       ;; Find an occurrence of `matcher' from `start' to `end'.
       (setq keyword (car keywords) matcher (car keyword))
       (goto-char start)
-      (while (if (stringp matcher)
-                (re-search-forward matcher end t)
-              (funcall matcher end))
+      (while (and (< (point) end)
+                 (if (stringp matcher)
+                     (re-search-forward matcher end t)
+                   (funcall matcher end)))
+       (when (and font-lock-multiline
+                  (match-beginning 0)
+                  (funcall (if (eq font-lock-multiline t) '>= '>)
+                           (point)
+                           (save-excursion (goto-char (match-beginning 0))
+                                           (forward-line 1) (point))))
+         ;; this is a multiline regexp match
+         (setq font-lock-multiline t)
+         (put-text-property (match-beginning 0) (point)
+                            'font-lock-multiline t))
        ;; Apply each highlight to this instance of `matcher', which may be
        ;; specific highlights or more keywords anchored to `matcher'.
        (setq highlights (cdr keyword))
@@ -1458,8 +1647,9 @@ START should be at the beginning of a line."
 ;; Various functions.
 
 (defun font-lock-compile-keywords (keywords)
-  ;; Compile KEYWORDS into the form (t KEYWORD ...) where KEYWORD is of the
-  ;; form (MATCHER HIGHLIGHT ...) as shown in `font-lock-keywords' doc string.
+  "Compile KEYWORDS into the form (t KEYWORD ...).
+Here KEYWORD is of the form (MATCHER HIGHLIGHT ...) as shown in the
+`font-lock-keywords' doc string."
   (if (eq (car-safe keywords) t)
       keywords
     (cons t (mapcar 'font-lock-compile-keyword keywords))))
@@ -1484,7 +1674,7 @@ START should be at the beginning of a line."
         keyword)))
 
 (defun font-lock-eval-keywords (keywords)
-  ;; Evalulate KEYWORDS if a function (funcall) or variable (eval) name.
+  "Evalulate KEYWORDS if a function (funcall) or variable (eval) name."
   (if (listp keywords)
       keywords
     (font-lock-eval-keywords (if (fboundp keywords)
@@ -1492,15 +1682,16 @@ START should be at the beginning of a line."
                               (eval keywords)))))
 
 (defun font-lock-value-in-major-mode (alist)
-  ;; Return value in ALIST for `major-mode', or ALIST if it is not an alist.
-  ;; Structure is ((MAJOR-MODE . VALUE) ...) where MAJOR-MODE may be t.
+  "Return value in ALIST for `major-mode', or ALIST if it is not an alist.
+Structure is ((MAJOR-MODE . VALUE) ...) where MAJOR-MODE may be t."
   (if (consp alist)
       (cdr (or (assq major-mode alist) (assq t alist)))
     alist))
 
 (defun font-lock-choose-keywords (keywords level)
-  ;; Return LEVELth element of KEYWORDS.  A LEVEL of nil is equal to a
-  ;; LEVEL of 0, a LEVEL of t is equal to (1- (length KEYWORDS)).
+  "Return LEVELth element of KEYWORDS.
+A LEVEL of nil is equal to a LEVEL of 0, a LEVEL of t is equal to
+\(1- (length KEYWORDS))."
   (cond ((symbolp keywords)
         keywords)
        ((numberp level)
@@ -1516,19 +1707,21 @@ START should be at the beginning of a line."
   "Set fontification defaults appropriately for this mode.
 Sets various variables using `font-lock-defaults' (or, if nil, using
 `font-lock-defaults-alist') and `font-lock-maximum-decoration'."
-  ;; Set fontification defaults.
-  (make-local-variable 'font-lock-fontified)
-  ;; Set iff not previously set.
+  ;; Set fontification defaults iff not previously set.
   (unless font-lock-set-defaults
     (set (make-local-variable 'font-lock-set-defaults)         t)
     (set (make-local-variable 'font-lock-cache-state)          nil)
     (set (make-local-variable 'font-lock-cache-position)       (make-marker))
+    (make-local-variable 'font-lock-fontified)
+    (make-local-variable 'font-lock-multiline)
     (let* ((defaults (or font-lock-defaults
                         (cdr (assq major-mode font-lock-defaults-alist))))
           (keywords
            (font-lock-choose-keywords (nth 0 defaults)
             (font-lock-value-in-major-mode font-lock-maximum-decoration)))
-          (local (cdr (assq major-mode font-lock-keywords-alist))))
+          (local (cdr (assq major-mode font-lock-keywords-alist)))
+          (removed-keywords
+           (cdr-safe (assq major-mode font-lock-removed-keywords-alist))))
       ;; Regexp fontification?
       (set (make-local-variable 'font-lock-keywords)
           (font-lock-compile-keywords (font-lock-eval-keywords keywords)))
@@ -1536,6 +1729,8 @@ Sets various variables using `font-lock-defaults' (or, if nil, using
       (while local
        (font-lock-add-keywords nil (car (car local)) (cdr (car local)))
        (setq local (cdr local)))
+      (when removed-keywords
+       (font-lock-remove-keywords nil removed-keywords))
       ;; Syntactic fontification?
       (when (nth 1 defaults)
        (set (make-local-variable 'font-lock-keywords-only) t))
@@ -1571,7 +1766,7 @@ Sets various variables using `font-lock-defaults' (or, if nil, using
            (setq alist (cdr alist))))))))
 
 (defun font-lock-unset-defaults ()
-  "Unset fontification defaults.  See `font-lock-set-defaults'."
+  "Unset fontification defaults.  See function `font-lock-set-defaults'."
   (setq font-lock-set-defaults                 nil
        font-lock-keywords                      nil
        font-lock-keywords-only                 nil
@@ -1621,7 +1816,7 @@ Sets various variables using `font-lock-defaults' (or, if nil, using
   "Face name to use for things that should stand out.")
 
 (defvar font-lock-reference-face       'font-lock-constant-face
-  "This variable is obsolete.  Use font-lock-constant-face.")
+  "This variable is obsolete.  Use `font-lock-constant-face'.")
 
 ;; Originally face attributes were specified via `font-lock-face-attributes'.
 ;; Users then changed the default face attributes by setting that variable.
@@ -1655,7 +1850,8 @@ Sets various variables using `font-lock-defaults' (or, if nil, using
 ;; But now we do it the custom way.  Note that `defface' will not overwrite any
 ;; faces declared above via `custom-declare-face'.
 (defface font-lock-comment-face
-  '((((class grayscale) (background light))
+  '((((type tty) (class color)) (:foreground "red"))
+    (((class grayscale) (background light))
      (:foreground "DimGray" :bold t :italic t))
     (((class grayscale) (background dark))
      (:foreground "LightGray" :bold t :italic t))
@@ -1666,7 +1862,8 @@ Sets various variables using `font-lock-defaults' (or, if nil, using
   :group 'font-lock-highlighting-faces)
 
 (defface font-lock-string-face
-  '((((class grayscale) (background light)) (:foreground "DimGray" :italic t))
+  '((((type tty) (class color)) (:foreground "green"))
+    (((class grayscale) (background light)) (:foreground "DimGray" :italic t))
     (((class grayscale) (background dark)) (:foreground "LightGray" :italic t))
     (((class color) (background light)) (:foreground "RosyBrown"))
     (((class color) (background dark)) (:foreground "LightSalmon"))
@@ -1675,7 +1872,8 @@ Sets various variables using `font-lock-defaults' (or, if nil, using
   :group 'font-lock-highlighting-faces)
 
 (defface font-lock-keyword-face
-  '((((class grayscale) (background light)) (:foreground "LightGray" :bold t))
+  '((((type tty) (class color)) (:foreground "cyan" :weight bold))
+    (((class grayscale) (background light)) (:foreground "LightGray" :bold t))
     (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
     (((class color) (background light)) (:foreground "Purple"))
     (((class color) (background dark)) (:foreground "Cyan"))
@@ -1684,7 +1882,8 @@ Sets various variables using `font-lock-defaults' (or, if nil, using
   :group 'font-lock-highlighting-faces)
 
 (defface font-lock-builtin-face
-  '((((class grayscale) (background light)) (:foreground "LightGray" :bold t))
+  '((((type tty) (class color)) (:foreground "blue" :weight light))
+    (((class grayscale) (background light)) (:foreground "LightGray" :bold t))
     (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
     (((class color) (background light)) (:foreground "Orchid"))
     (((class color) (background dark)) (:foreground "LightSteelBlue"))
@@ -1693,14 +1892,16 @@ Sets various variables using `font-lock-defaults' (or, if nil, using
   :group 'font-lock-highlighting-faces)
 
 (defface font-lock-function-name-face
-  '((((class color) (background light)) (:foreground "Blue"))
+  '((((type tty) (class color)) (:foreground "blue" :weight bold))
+    (((class color) (background light)) (:foreground "Blue"))
     (((class color) (background dark)) (:foreground "LightSkyBlue"))
     (t (:inverse-video t :bold t)))
   "Font Lock mode face used to highlight function names."
   :group 'font-lock-highlighting-faces)
 
 (defface font-lock-variable-name-face
-  '((((class grayscale) (background light))
+  '((((type tty) (class color)) (:foreground "yellow" :weight light))
+    (((class grayscale) (background light))
      (:foreground "Gray90" :bold t :italic t))
     (((class grayscale) (background dark))
      (:foreground "DimGray" :bold t :italic t))
@@ -1711,7 +1912,8 @@ Sets various variables using `font-lock-defaults' (or, if nil, using
   :group 'font-lock-highlighting-faces)
 
 (defface font-lock-type-face
-  '((((class grayscale) (background light)) (:foreground "Gray90" :bold t))
+  '((((type tty) (class color)) (:foreground "green"))
+    (((class grayscale) (background light)) (:foreground "Gray90" :bold t))
     (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
     (((class color) (background light)) (:foreground "ForestGreen"))
     (((class color) (background dark)) (:foreground "PaleGreen"))
@@ -1720,7 +1922,8 @@ Sets various variables using `font-lock-defaults' (or, if nil, using
   :group 'font-lock-highlighting-faces)
 
 (defface font-lock-constant-face
-  '((((class grayscale) (background light))
+  '((((type tty) (class color)) (:foreground "magenta"))
+    (((class grayscale) (background light))
      (:foreground "LightGray" :bold t :underline t))
     (((class grayscale) (background dark))
      (:foreground "Gray50" :bold t :underline t))
@@ -1731,7 +1934,8 @@ Sets various variables using `font-lock-defaults' (or, if nil, using
   :group 'font-lock-highlighting-faces)
 
 (defface font-lock-warning-face
-  '((((class color) (background light)) (:foreground "Red" :bold t))
+  '((((type tty) (class color)) (:foreground "red"))
+    (((class color) (background light)) (:foreground "Red" :bold t))
     (((class color) (background dark)) (:foreground "Pink" :bold t))
     (t (:inverse-video t :bold t)))
   "Font Lock mode face used to highlight warnings."
@@ -1892,9 +2096,9 @@ This function could be MATCHER in a MATCH-ANCHORED `font-lock-keywords' item."
                   ;; Function declarations.
                   "\\(advice\\|alias\\|generic\\|macro\\*?\\|method\\|"
                    "setf\\|subst\\*?\\|un\\*?\\|"
-                   "ine-\\(condition\\|derived-mode\\|function\\|"
+                   "ine-\\(condition\\|\\(?:derived\\|minor\\)-mode\\|"
                    "method-combination\\|setf-expander\\|skeleton\\|widget\\|"
-                   "\\(compiler\\|modify\\|symbol\\)-macro\\)\\)\\|"
+                   "function\\|\\(compiler\\|modify\\|symbol\\)-macro\\)\\)\\|"
                   ;; Variable declarations.
                   "\\(const\\(ant\\)?\\|custom\\|face\\|parameter\\|var\\)\\|"
                   ;; Structure declarations.
@@ -1902,7 +2106,7 @@ This function could be MATCHER in a MATCH-ANCHORED `font-lock-keywords' item."
                   "\\)\\)\\>"
                   ;; Any whitespace and defined object.
                   "[ \t'\(]*"
-                  "\\(\\sw+\\)?")
+                  "\\(setf[ \t]+\\sw+)\\|\\sw+\\)?")
           '(1 font-lock-keyword-face)
           '(9 (cond ((match-beginning 3) font-lock-function-name-face)
                     ((match-beginning 6) font-lock-variable-name-face)
@@ -1910,7 +2114,7 @@ This function could be MATCHER in a MATCH-ANCHORED `font-lock-keywords' item."
               nil t))
      ;;
      ;; Emacs Lisp autoload cookies.
-     '("^;;;###\\(autoload\\)\\>" 1 font-lock-warning-face prepend)
+     '("^;;;###\\(autoload\\)" 1 font-lock-warning-face prepend)
      ))
   "Subdued level highlighting for Lisp modes.")
 
@@ -1971,7 +2175,7 @@ This function could be MATCHER in a MATCH-ANCHORED `font-lock-keywords' item."
       '("\\<:\\sw\\sw+\\>" 0 font-lock-builtin-face)
       ;;
       ;; ELisp and CLisp `&' keywords as types.
-      '("\\<\\&\\sw+\\>" . font-lock-type-face)
+      '("\\&\\sw+\\>" . font-lock-type-face)
       )))
   "Gaudy level highlighting for Lisp modes.")
 
@@ -2179,6 +2383,7 @@ The value of this variable is used when Font Lock mode is turned on."
     "map" "multimap"
     "hash\\(_\\(m\\(ap\\|ulti\\(map\\|set\\)\\)\\|set\\)\\)?"
     "stack" "queue" "priority_queue"
+    "type_info"
     "iterator" "const_iterator" "reverse_iterator" "const_reverse_iterator"
     "reference" "const_reference")
   "*List of extra types to fontify in C++ mode.
@@ -2254,7 +2459,7 @@ See also `c-font-lock-extra-types'.")
        (regexp-opt-depth c-type-specs))
        (c-type-names
        `(mapconcat 'identity
-         (cons 
+         (cons
           (,@ (eval-when-compile
                 (regexp-opt
                  '("char" "short" "int" "long" "signed" "unsigned"
@@ -2263,6 +2468,13 @@ See also `c-font-lock-extra-types'.")
          "\\|"))
        (c-type-names-depth
        `(regexp-opt-depth (,@ c-type-names)))
+       (c-preprocessor-directives
+       (eval-when-compile
+         (regexp-opt
+          '("define"  "elif" "else" "endif" "error" "file" "if" "ifdef"
+            "ifndef" "include" "line" "pragma" "undef"))))
+       (c-preprocessor-directives-depth
+       (regexp-opt-depth c-preprocessor-directives))
        )
  (setq c-font-lock-keywords-1
   (list
@@ -2289,8 +2501,12 @@ See also `c-font-lock-extra-types'.")
       (1 font-lock-builtin-face) (2 font-lock-variable-name-face nil t)))
    ;;
    ;; Fontify otherwise as symbol names, and the preprocessor directive names.
-   '("^#[ \t]*\\(\\sw+\\)\\>[ \t!]*\\(\\sw+\\)?"
-     (1 font-lock-builtin-face) (2 font-lock-variable-name-face nil t))
+   (list
+    (concat "^#[ \t]*\\(" c-preprocessor-directives
+           "\\)\\>[ \t!]*\\(\\sw+\\)?")
+    '(1 font-lock-builtin-face)
+    (list (+ 2 c-preprocessor-directives-depth)
+         'font-lock-variable-name-face nil t))
    ))
 
  (setq c-font-lock-keywords-2
@@ -2307,8 +2523,13 @@ See also `c-font-lock-extra-types'.")
     (concat "\\<\\(" c-keywords "\\|" c-type-specs "\\)\\>")
     ;;
     ;; Fontify case/goto keywords and targets, and case default/goto tags.
-    '("\\<\\(case\\|goto\\)\\>[ \t]*\\(-?\\sw+\\)?"
-      (1 font-lock-keyword-face) (2 font-lock-constant-face nil t))
+    '("\\<\\(case\\|goto\\)\\>"
+      (1 font-lock-keyword-face)
+      ("\\(-[0-9]+\\|\\sw+\\)"
+       ;; Return limit of search.
+       (save-excursion (skip-chars-forward "^:\n") (point))
+       nil
+       (1 font-lock-constant-face nil t)))
     ;; Anders Lindgren <andersl@andersl.com> points out that it is quicker to
     ;; use MATCH-ANCHORED to effectively anchor the regexp on the left.
     ;; This must come after the one for keywords and targets.
@@ -2420,6 +2641,24 @@ See also `c++-font-lock-extra-types'.")
            (goto-char (match-end 2)))
        (error t)))))
 
+(defun font-lock-match-c++-structor-declaration (limit)
+  ;; Match C++ constructors and destructors inside class declarations.
+  (let ((res nil)
+       (regexp (concat "^\\s-+\\(\\(virtual\\|explicit\\)\\s-+\\)*~?\\(\\<"
+                       (mapconcat 'identity
+                                  c++-font-lock-extra-types "\\|")
+                       "\\>\\)\\s-*("
+                       ;; Don't match function pointer declarations, e.g.:
+                       ;;    Foo (*fptr)();
+                       "\\s-*[^*( \t]")))
+    (while (progn (setq res (re-search-forward regexp limit t))
+                 (and res
+                      (save-excursion
+                        (beginning-of-line)
+                        (save-match-data
+                          (not (vectorp (c-at-toplevel-p))))))))
+    res))
+
 (let* ((c++-keywords
        (eval-when-compile
          (regexp-opt
@@ -2453,7 +2692,7 @@ See also `c++-font-lock-extra-types'.")
        (regexp-opt-depth c++-type-specs))
        (c++-type-names
        `(mapconcat 'identity
-         (cons 
+         (cons
           (,@ (eval-when-compile
                 (regexp-opt
                  '("signed" "unsigned" "short" "long"
@@ -2508,8 +2747,20 @@ See also `c++-font-lock-extra-types'.")
          '(2 font-lock-builtin-face nil t))
     ;;
     ;; Fontify case/goto keywords and targets, and case default/goto tags.
-    '("\\<\\(case\\|goto\\)\\>[ \t]*\\(-?\\sw+\\)?"
-      (1 font-lock-keyword-face) (2 font-lock-constant-face nil t))
+    '("\\<\\(case\\|goto\\)\\>"
+      (1 font-lock-keyword-face)
+      ("\\(-[0-9]+\\|\\sw+\\)[ \t]*\\(::\\)?"
+       ;; Return limit of search.
+       (save-excursion
+        (while (progn
+                 (skip-chars-forward "^:\n")
+                 (looking-at "::"))
+          (forward-char 2))
+        (point))
+       nil
+       (1 (if (match-beginning 2)
+             font-lock-type-face
+           font-lock-constant-face) nil t)))
     ;; This must come after the one for keywords and targets.
     '(":" ("^[ \t]*\\(\\sw+\\)[ \t]*:\\($\\|[^:]\\)"
           (beginning-of-line) (end-of-line)
@@ -2601,6 +2852,10 @@ See also `c++-font-lock-extra-types'.")
            (5 (if (match-beginning 6)
                   font-lock-function-name-face
                 font-lock-variable-name-face) nil t)))
+    ;;
+    ;; Fontify constructors and destructors inside class declarations.
+    '(font-lock-match-c++-structor-declaration
+      (3 font-lock-function-name-face t))
     )))
  )
 
@@ -2794,7 +3049,7 @@ See also `java-font-lock-extra-types'.")
        ;; Classes immediately followed by an object name.
        (java-type-names
        `(mapconcat 'identity
-         (cons 
+         (cons
           (,@ (eval-when-compile
                 (regexp-opt '("boolean" "char" "byte" "short" "int" "long"
                               "float" "double" "void"))))
@@ -2852,11 +3107,19 @@ See also `java-font-lock-extra-types'.")
     '("\\<\\(false\\|null\\|true\\)\\>" . font-lock-constant-face)
     ;;
     ;; Javadoc tags within comments.
-    '("@\\(author\\|exception\\|return\\|see\\|version\\)\\>"
-      (1 font-lock-constant-face prepend))
+    (list
+     (concat "@\\("
+            "author\\|deprecated\\|exception"
+            "\\|link\\|return\\|see\\|serial\\|serialData\\|serialField"
+            "\\|since\\|throws"
+            "\\|version"
+            "\\)\\>"))
     '("@\\(param\\)\\>[ \t]*\\(\\sw+\\)?"
       (1 font-lock-constant-face prepend)
       (2 font-lock-variable-name-face prepend t))
+    '("@\\(exception\\|throws\\)\\>[ \t]*\\(\\S-+\\)?"
+      (1 font-lock-constant-face prepend)
+      (2 font-lock-type-face prepend t))
     )))
 
  (setq java-font-lock-keywords-3
@@ -2907,6 +3170,7 @@ See also `java-font-lock-extra-types'.")
 \f
 ;; Install ourselves:
 
+;; Useful for the popup-menu for mouse-3 on the modeline.
 (unless (assq 'font-lock-mode minor-mode-alist)
   (push '(font-lock-mode nil) minor-mode-alist))