Fix arg usage of format-decode-run-method (Bug#7488).
[bpt/emacs.git] / lisp / font-lock.el
index 2ddbe60..85a7ff1 100644 (file)
@@ -1,10 +1,12 @@
 ;;; font-lock.el --- Electric font lock mode
 
 ;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
-;;   2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
+;;   2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
 ;;   Free Software Foundation, Inc.
 
-;; Author: jwz, then rms, then sm
+;; Author: Jamie Zawinski
+;;     Richard Stallman
+;;     Stefan Monnier
 ;; Maintainer: FSF
 ;; Keywords: languages, faces
 
 ;; the pattern definitions.)  Font Lock mode chooses which variable to use for
 ;; fontification based on `font-lock-maximum-decoration'.
 ;;
-;; Font Lock mode fontification behaviour can be modified in a number of ways.
+;; Font Lock mode fontification behavior can be modified in a number of ways.
 ;; See the below comments and the comments distributed throughout this file.
 
 ;;; Constructing patterns:
 ;;   i.e., (b) above, all modes that have items that can be thought of as, say,
 ;;   keywords, should be highlighted with the same face, etc.
 ;; - Make the face attributes fit the concept as far as possible.
-;;   i.e., function names might be a bold colour such as blue, comments might
-;;   be a bright colour such as red, character strings might be brown, because,
+;;   i.e., function names might be a bold color such as blue, comments might
+;;   be a bright color such as red, character strings might be brown, because,
 ;;   err, strings are brown (that was not the reason, please believe me).
 ;; - Don't use a non-nil OVERRIDE unless you have a good reason.
 ;;   Only use OVERRIDE for special things that are easy to define, such as the
 ;; User variables.
 
 (defcustom font-lock-maximum-size 256000
-  "*Maximum size of a buffer for buffer fontification.
+  "Maximum size of a buffer for buffer fontification.
 Only buffers less than this can be fontified when Font Lock mode is turned on.
 If nil, means size is irrelevant.
 If a list, each element should be a cons pair of the form (MAJOR-MODE . SIZE),
@@ -249,7 +251,7 @@ for buffers in Rmail mode, and size is irrelevant otherwise."
   :group 'font-lock)
 
 (defcustom font-lock-maximum-decoration t
-  "*Maximum decoration level for fontification.
+  "Maximum decoration level for fontification.
 If nil, use the default decoration (typically the minimum available).
 If t, use the maximum decoration available.
 If a number, use that level of decoration (or if not available the maximum).
@@ -274,7 +276,7 @@ decoration for buffers in C++ mode, and level 1 decoration otherwise."
   :group 'font-lock)
 
 (defcustom font-lock-verbose 0
-  "*If non-nil, means show status messages for buffer fontification.
+  "If non-nil, means show status messages for buffer fontification.
 If a number, only buffers greater than this size have fontification messages."
   :type '(choice (const :tag "never" nil)
                 (other :tag "always" t)
@@ -288,6 +290,11 @@ If a number, only buffers greater than this size have fontification messages."
 ;; We now allow a FACENAME in `font-lock-keywords' to be any expression that
 ;; returns a face.  So the easiest thing is to continue using these variables,
 ;; rather than sometimes evaling FACENAME and sometimes not.  sm.
+
+;; Note that in new code, in the vast majority of cases there is no
+;; need to create variables that specify face names.  Simply using
+;; faces directly is enough.  Font-lock is not a template to be
+;; followed in this area.
 (defvar font-lock-comment-face         'font-lock-comment-face
   "Face name to use for comments.")
 
@@ -485,7 +492,8 @@ This is normally set via `font-lock-defaults'.")
 
 (defvar font-lock-keywords-case-fold-search nil
   "*Non-nil means the patterns in `font-lock-keywords' are case-insensitive.
-This is normally set via `font-lock-defaults'.")
+This is set via the function `font-lock-set-defaults', based on
+the CASE-FOLD argument of `font-lock-defaults'.")
 (make-variable-buffer-local 'font-lock-keywords-case-fold-search)
 
 (defvar font-lock-syntactically-fontified 0
@@ -552,6 +560,8 @@ outside of any comment, string, or sexp.  This variable is semi-obsolete;
 we recommend setting `syntax-begin-function' instead.
 
 This is normally set via `font-lock-defaults'.")
+(make-obsolete-variable 'font-lock-beginning-of-syntax-function
+                        'syntax-begin-function "23.3")
 
 (defvar font-lock-mark-block-function nil
   "*Non-nil means use this function to mark a block of text.
@@ -845,7 +855,7 @@ happens, so the major mode can be corrected."
 ;; themselves.
 
 (defcustom font-lock-support-mode 'jit-lock-mode
-  "*Support mode for Font Lock mode.
+  "Support mode for Font Lock mode.
 Support modes speed up Font Lock mode by being choosy about when fontification
 occurs.  The default support mode, Just-in-time Lock mode (symbol
 `jit-lock-mode'), is recommended.
@@ -916,26 +926,26 @@ The value of this variable is used when Font Lock mode is turned on."
                      nil t)))))
 
 (defun font-lock-turn-off-thing-lock ()
-  (cond ((and (boundp 'fast-lock-mode) fast-lock-mode)
+  (cond ((bound-and-true-p fast-lock-mode)
         (fast-lock-mode -1))
-       ((and (boundp 'jit-lock-mode) jit-lock-mode)
+       ((bound-and-true-p jit-lock-mode)
         (jit-lock-unregister 'font-lock-fontify-region)
         ;; Reset local vars to the non-jit-lock case.
         (kill-local-variable 'font-lock-fontify-buffer-function))
-       ((and (boundp 'lazy-lock-mode) lazy-lock-mode)
+       ((bound-and-true-p lazy-lock-mode)
         (lazy-lock-mode -1))))
 
 (defun font-lock-after-fontify-buffer ()
-  (cond ((and (boundp 'fast-lock-mode) fast-lock-mode)
+  (cond ((bound-and-true-p 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))
-       ((and (boundp 'lazy-lock-mode) lazy-lock-mode)
+       ((bound-and-true-p lazy-lock-mode)
         (lazy-lock-after-fontify-buffer))))
 
 (defun font-lock-after-unfontify-buffer ()
-  (cond ((and (boundp 'fast-lock-mode) fast-lock-mode)
+  (cond ((bound-and-true-p 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
@@ -945,7 +955,7 @@ The value of this variable is used when Font Lock mode is turned on."
        ;;
        ;; (jit-lock-mode
        ;;  (jit-lock-after-unfontify-buffer))
-       ((and (boundp 'lazy-lock-mode) lazy-lock-mode)
+       ((bound-and-true-p lazy-lock-mode)
         (lazy-lock-after-unfontify-buffer))))
 
 ;;; End of Font Lock Support mode.
@@ -1011,7 +1021,8 @@ The region it returns may start or end in the middle of a line.")
   "Fontify the current buffer the way the function `font-lock-mode' would."
   (interactive)
   (font-lock-set-defaults)
-  (let ((font-lock-verbose (or font-lock-verbose (interactive-p))))
+  (let ((font-lock-verbose (or font-lock-verbose
+                              (called-interactively-p 'interactive))))
     (funcall font-lock-fontify-buffer-function)))
 
 (defun font-lock-unfontify-buffer ()
@@ -1465,9 +1476,10 @@ 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)))
        ;; Apply each highlight to this instance of `matcher', which may be
        ;; specific highlights or more keywords anchored to `matcher'.
        (setq highlights (cdr keyword))
@@ -1757,15 +1769,35 @@ A LEVEL of nil is equal to a LEVEL of 0, a LEVEL of t is equal to
 
 (defvar font-lock-set-defaults nil)    ; Whether we have set up defaults.
 
-(defvar font-lock-mode-major-mode)
+(defun font-lock-refresh-defaults ()
+  "Restart fontification in current buffer after recomputing from defaults.
+Recompute fontification variables using `font-lock-defaults' (or,
+if nil, using `font-lock-defaults-alist') and
+`font-lock-maximum-decoration'.  Then restart fontification.
+
+Use this function when you have changed any of the above
+variables directly.
+
+Note: This function will erase modifications done by
+`font-lock-add-keywords' or `font-lock-remove-keywords', but will
+preserve `hi-lock-mode' highlighting patterns."
+  (let ((hi-lock--inhibit-font-lock-hook t))
+    (font-lock-mode -1))
+  (kill-local-variable 'font-lock-set-defaults)
+  (font-lock-mode 1))
+
+(defvar font-lock-major-mode nil
+  "Major mode for which the font-lock settings have been setup.")
+(make-variable-buffer-local 'font-lock-major-mode)
+
 (defun font-lock-set-defaults ()
   "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 if not previously set for correct major mode.
   (unless (and font-lock-set-defaults
-              (eq font-lock-mode-major-mode major-mode))
-    (setq font-lock-mode-major-mode major-mode)
+              (eq font-lock-major-mode major-mode))
+    (setq font-lock-major-mode major-mode)
     (set (make-local-variable 'font-lock-set-defaults) t)
     (make-local-variable 'font-lock-fontified)
     (make-local-variable 'font-lock-multiline)
@@ -1824,7 +1856,7 @@ Sets various variables using `font-lock-defaults' (or, if nil, using
        (setq font-lock-keywords
               (font-lock-compile-keywords font-lock-keywords))))))
 \f
-;;; Colour etc. support.
+;;; Color etc. support.
 
 ;; Note that `defface' will not overwrite any faces declared above via
 ;; `custom-declare-face'.
@@ -1863,7 +1895,7 @@ Sets various variables using `font-lock-defaults' (or, if nil, using
 (defface font-lock-string-face
   '((((class grayscale) (background light)) (:foreground "DimGray" :slant italic))
     (((class grayscale) (background dark)) (:foreground "LightGray" :slant italic))
-    (((class color) (min-colors 88) (background light)) (:foreground "RosyBrown"))
+    (((class color) (min-colors 88) (background light)) (:foreground "VioletRed4"))
     (((class color) (min-colors 88) (background dark)) (:foreground "LightSalmon"))
     (((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
     (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
@@ -1892,7 +1924,7 @@ Sets various variables using `font-lock-defaults' (or, if nil, using
 (defface font-lock-builtin-face
   '((((class grayscale) (background light)) (:foreground "LightGray" :weight bold))
     (((class grayscale) (background dark)) (:foreground "DimGray" :weight bold))
-    (((class color) (min-colors 88) (background light)) (:foreground "Orchid"))
+    (((class color) (min-colors 88) (background light)) (:foreground "MediumOrchid4"))
     (((class color) (min-colors 88) (background dark)) (:foreground "LightSteelBlue"))
     (((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
     (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
@@ -1916,7 +1948,7 @@ Sets various variables using `font-lock-defaults' (or, if nil, using
      (:foreground "Gray90" :weight bold :slant italic))
     (((class grayscale) (background dark))
      (:foreground "DimGray" :weight bold :slant italic))
-    (((class color) (min-colors 88) (background light)) (:foreground "DarkGoldenrod"))
+    (((class color) (min-colors 88) (background light)) (:foreground "sienna"))
     (((class color) (min-colors 88) (background dark)) (:foreground "LightGoldenrod"))
     (((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
     (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
@@ -1942,7 +1974,7 @@ Sets various variables using `font-lock-defaults' (or, if nil, using
      (:foreground "LightGray" :weight bold :underline t))
     (((class grayscale) (background dark))
      (:foreground "Gray50" :weight bold :underline t))
-    (((class color) (min-colors 88) (background light)) (:foreground "CadetBlue"))
+    (((class color) (min-colors 88) (background light)) (:foreground "dark cyan"))
     (((class color) (min-colors 88) (background dark)) (:foreground "Aquamarine"))
     (((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
     (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
@@ -1981,7 +2013,7 @@ Sets various variables using `font-lock-defaults' (or, if nil, using
   "Font Lock mode face used to highlight grouping constructs in Lisp regexps."
   :group 'font-lock-faces)
 
-;;; End of Colour etc. support.
+;;; End of Color etc. support.
 \f
 ;;; Menu support.
 
@@ -2154,7 +2186,7 @@ This function could be MATCHER in a MATCH-ANCHORED `font-lock-keywords' item."
 ;;
 (defconst cpp-font-lock-keywords-source-directives
   "define\\|e\\(?:l\\(?:if\\|se\\)\\|ndif\\|rror\\)\\|file\\|i\\(?:f\\(?:n?def\\)?\\|mport\\|nclude\\)\\|line\\|pragma\\|undef\\|warning"
-  "Regular expressoin used in `cpp-font-lock-keywords'.")
+  "Regular expression used in `cpp-font-lock-keywords'.")
 
 ;; `cpp-font-lock-keywords-source-depth' is calculated from:
 ;;
@@ -2203,10 +2235,10 @@ Used in `cpp-font-lock-keywords'.")
       '(1 font-lock-preprocessor-face prepend)
       (list (+ 2 directives-depth)
            'font-lock-variable-name-face nil t))))
-    "Font lock keyords for C preprocessor directives.
-`c-mode', `c++-mode' and `objc-mode' have their own
-font lock keyords for C preprocessor directives. This definition is for the
-other modes in which C preprocessor directives are used. e.g. `asm-mode' and
+  "Font lock keywords for C preprocessor directives.
+`c-mode', `c++-mode' and `objc-mode' have their own font lock keywords
+for C preprocessor directives.  This definition is for the other modes
+in which C preprocessor directives are used. e.g. `asm-mode' and
 `ld-script-mode'.")
 
 \f
@@ -2252,11 +2284,11 @@ other modes in which C preprocessor directives are used. e.g. `asm-mode' and
               '("cond" "if" "while" "while-no-input" "let" "let*"
                 "prog" "progn" "progv" "prog1" "prog2" "prog*"
                 "inline" "lambda" "save-restriction" "save-excursion"
-                "save-window-excursion" "save-selected-window"
-                "save-match-data" "save-current-buffer" "unwind-protect"
-                "condition-case" "track-mouse"
+                "save-selected-window" "save-window-excursion"
+                "save-match-data" "save-current-buffer"
+                "unwind-protect" "condition-case" "track-mouse"
                 "eval-after-load" "eval-and-compile" "eval-when-compile"
-                "eval-when" "eval-at-startup" "eval-next-after-load"
+                "eval-when" "eval-next-after-load"
                 "with-case-table" "with-category-table"
                 "with-current-buffer" "with-electric-help"
                 "with-local-quit" "with-no-warnings"
@@ -2274,7 +2306,7 @@ other modes in which C preprocessor directives are used. e.g. `asm-mode' and
                 "restart-bind" "restart-case" "in-package"
                 "break" "ignore-errors"
                 "loop" "do" "do*" "dotimes" "dolist" "the" "locally"
-                "proclaim" "declaim" "declare" "symbol-macrolet"
+                "proclaim" "declaim" "declare" "symbol-macrolet" "letf"
                 "lexical-let" "lexical-let*" "flet" "labels" "compiler-let"
                 "destructuring-bind" "macrolet" "tagbody" "block" "go"
                 "multiple-value-bind" "multiple-value-prog1"