]> code.delx.au - gnu-emacs/blobdiff - lisp/font-lock.el
(ps-print-preprint): Special handling if
[gnu-emacs] / lisp / font-lock.el
index b7df4df0599c3e4e10d11cd49ae622c746f84b93..33ba04dc41ae5cc2d5f89c9071326780d6b02066 100644 (file)
@@ -97,7 +97,7 @@
 
 ;;;###autoload
 (defvar font-lock-maximum-decoration nil
-  "*If non-nil, the maximum decoration level for fontifying.
+  "*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).
@@ -109,7 +109,7 @@ available for buffers in `c-mode', and level 1 decoration otherwise.")
 
 ;;;###autoload
 (defvar font-lock-maximum-size (* 250 1024)
-  "*If non-nil, the maximum size for buffers for fontifying.
+  "*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),
@@ -402,10 +402,6 @@ the major mode's hook.  For example, put in your ~/.emacs:
 
  (add-hook 'c-mode-hook 'turn-on-font-lock)
 
-Or for any visited file with the following in your ~/.emacs:
-
- (add-hook 'find-file-hooks 'turn-on-font-lock)
-
 Alternatively, you can use Global Font Lock mode to automagically turn on Font
 Lock mode in buffers whose major mode supports it, or in buffers whose major
 mode is one of `font-lock-global-modes'.  For example, put in your ~/.emacs:
@@ -432,15 +428,12 @@ syntactic change on other lines, you can use \\[font-lock-fontify-block]."
   (interactive "P")
   ;; Don't turn on Font Lock mode if we don't have a display (we're running a
   ;; batch job) or if the buffer is invisible (the name starts with a space).
-  (let ((on-p (and (not noninteractive)
+  (let ((maximum-size (font-lock-value-in-major-mode font-lock-maximum-size))
+       (on-p (and (not noninteractive)
                   (not (eq (aref (buffer-name) 0) ?\ ))
                   (if arg
                       (> (prefix-numeric-value arg) 0)
-                    (not font-lock-mode))))
-       (maximum-size (if (not (consp font-lock-maximum-size))
-                         font-lock-maximum-size
-                       (cdr (or (assq major-mode font-lock-maximum-size)
-                                (assq t font-lock-maximum-size))))))
+                    (not font-lock-mode)))))
     (if (not on-p)
        (remove-hook 'after-change-functions 'font-lock-after-change-function
                     t)
@@ -480,8 +473,12 @@ syntactic change on other lines, you can use \\[font-lock-fontify-block]."
 
 ;;;###autoload
 (defun turn-on-font-lock ()
-  "Turn on Font Lock mode, if the terminal can display it."
-  (if window-system (font-lock-mode t)))
+  "Turn on Font Lock mode conditionally.
+Turn on only if the buffer mode supports it and the terminal can display it."
+  (if (and window-system
+          (not font-lock-mode)
+          (or font-lock-defaults (assq major-mode font-lock-defaults-alist)))
+      (font-lock-mode t)))
 \f
 ;; Code for Global Font Lock mode.
 
@@ -500,7 +497,7 @@ syntactic change on other lines, you can use \\[font-lock-fontify-block]."
 ;; hook is run, the major mode is in the process of being changed and we do not
 ;; know what the final major mode will be.  So, `font-lock-change-major-mode'
 ;; only (a) notes the name of the current buffer, and (b) adds our function
-;; `turn-on-font-lock-if-supported' to the hook variable `post-command-hook'.
+;; `turn-on-font-lock-if-enabled' to the hook variable `post-command-hook'.
 ;; By the time the functions on `post-command-hook' are run, the new major mode
 ;; is assumed to be in place.
 
@@ -519,12 +516,12 @@ syntactic change on other lines, you can use \\[font-lock-fontify-block]."
 ;; `major-mode-hook' is simpler), but maybe someone can come up with another
 ;; solution? --sm.
 
-(defvar font-lock-cache-buffers nil)           ; For remembering buffers.
-(defvar change-major-mode-hook nil)            ; Make sure it's not void.
+(defvar font-lock-buffers nil)         ; For remembering buffers.
+(defvar change-major-mode-hook nil)    ; Make sure it's not void.
 
 ;;;###autoload
 (defvar font-lock-global-modes t
-  "*List of modes for which Font Lock mode is automatically turned on.
+  "*Modes for which Font Lock mode is automatically turned on.
 Global Font Lock mode is controlled by the `global-font-lock-mode' command.
 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.
@@ -533,47 +530,50 @@ Font Lock is automatically turned on if the buffer major mode supports it and
 is in this list.  The sense of the list is negated if it begins with `not'.")
 
 ;;;###autoload
-(defun global-font-lock-mode (&optional arg)
+(defun global-font-lock-mode (&optional arg message)
   "Toggle Global Font Lock mode.
-With arg, turn Global Font Lock mode on if and only if arg is positive.
+With prefix ARG, turn Global Font Lock mode on if and only if ARG is positive.
+Displays a message saying whether the mode is on or off if MESSAGE is non-nil.
+Returns the new status of Global Font Lock mode (non-nil means on).
 
 When Global Font Lock mode is enabled, Font Lock mode is automagically
 turned on in a buffer if its major mode is one of `font-lock-global-modes'."
-  (interactive "P")
-  (if (if arg
-         (<= (prefix-numeric-value arg) 0)
-       (memq 'font-lock-change-major-mode change-major-mode-hook))
-      (remove-hook 'change-major-mode-hook 'font-lock-change-major-mode)
-    (add-hook 'change-major-mode-hook 'font-lock-change-major-mode)
-    (add-hook 'post-command-hook 'turn-on-font-lock-if-supported)
-    (setq font-lock-cache-buffers (buffer-list))))
+  (interactive "P\np")
+  (let ((off-p (if arg
+                  (<= (prefix-numeric-value arg) 0)
+                (memq 'font-lock-change-major-mode change-major-mode-hook))))
+    (if off-p
+       (remove-hook 'change-major-mode-hook 'font-lock-change-major-mode)
+      (add-hook 'change-major-mode-hook 'font-lock-change-major-mode)
+      (add-hook 'post-command-hook 'turn-on-font-lock-if-enabled)
+      (setq font-lock-buffers (buffer-list)))
+    (if message
+       (message "Global Font Lock mode is now %s." (if off-p "OFF" "ON")))
+    (not off-p)))
 
 (defun font-lock-change-major-mode ()
   ;; Gross hack warning: Delicate readers should avert eyes now.
-  ;; Something is running `kill-all-local-variables', which generally means
-  ;; the major mode is being changed.  Run `turn-on-font-lock-if-supported'
-  ;; after the current command has finished.
-  (add-hook 'post-command-hook 'turn-on-font-lock-if-supported)
-  (add-to-list 'font-lock-cache-buffers (current-buffer)))
+  ;; Something is running `kill-all-local-variables', which generally means the
+  ;; major mode is being changed.  Run `turn-on-font-lock-if-enabled' after the
+  ;; current command has finished.
+  (add-hook 'post-command-hook 'turn-on-font-lock-if-enabled)
+  (add-to-list 'font-lock-buffers (current-buffer)))
 
-(defun turn-on-font-lock-if-supported ()
+(defun turn-on-font-lock-if-enabled ()
   ;; Gross hack warning: Delicate readers should avert eyes now.
-  ;; Turn on Font Lock mode if (a) it's not already on, (b) the major mode
-  ;; supports Font Lock mode, and (c) it's one of `font-lock-global-modes'.
-  (remove-hook 'post-command-hook 'turn-on-font-lock-if-supported)
-  (while font-lock-cache-buffers
-    (if (buffer-name (car font-lock-cache-buffers))
+  ;; Turn on Font Lock mode if it's one of `font-lock-global-modes'.
+  (remove-hook 'post-command-hook 'turn-on-font-lock-if-enabled)
+  (while font-lock-buffers
+    (if (buffer-live-p (car font-lock-buffers))
        (save-excursion
-         (set-buffer (car font-lock-cache-buffers))
-         (if (and (not font-lock-mode)
-                  (or font-lock-defaults
-                      (assq major-mode font-lock-defaults-alist))
-                  (or (eq font-lock-global-modes t)
-                      (if (eq (car-safe font-lock-global-modes) 'not)
-                          (not (memq major-mode (cdr font-lock-global-modes)))
-                        (memq major-mode font-lock-global-modes))))
-             (turn-on-font-lock))))
-    (setq font-lock-cache-buffers (cdr font-lock-cache-buffers))))
+         (set-buffer (car font-lock-buffers))
+         (if (or (eq font-lock-global-modes t)
+                 (if (eq (car-safe font-lock-global-modes) 'not)
+                     (not (memq major-mode (cdr font-lock-global-modes)))
+                   (memq major-mode font-lock-global-modes)))
+             (let (inhibit-quit)
+               (turn-on-font-lock)))))
+    (setq font-lock-buffers (cdr font-lock-buffers))))
 
 ;; End of Global Font Lock mode.
 \f
@@ -672,12 +672,12 @@ turned on in a buffer if its major mode is one of `font-lock-global-modes'."
 (defun font-lock-fontify-block (&optional arg)
   "Fontify some lines the way `font-lock-fontify-buffer' would.
 The lines could be a function or paragraph, or a specified number of lines.
-If `font-lock-mark-block-function' non-nil and no ARG is given, it is used to
-delimit the region to fontify.
 If ARG is given, fontify that many lines before and after point, or 16 lines if
-no ARG is given and `font-lock-mark-block-function' is nil."
+no ARG is given and `font-lock-mark-block-function' is nil.
+If `font-lock-mark-block-function' non-nil and no ARG is given, it is used to
+delimit the region to fontify."
   (interactive "P")
-  (let ((font-lock-beginning-of-syntax-function nil))
+  (let (font-lock-beginning-of-syntax-function deactivate-mark)
     ;; Make sure we have the right `font-lock-keywords' etc.
     (if (not font-lock-mode) (font-lock-set-defaults))
     (save-excursion
@@ -692,7 +692,7 @@ no ARG is given and `font-lock-mark-block-function' is nil."
              (font-lock-fontify-region (point) (mark)))
          ((error quit) (message "Fontifying block... %s" error-data)))))))
 
-(define-key esc-map "\C-g" 'font-lock-fontify-block)
+(define-key facemenu-keymap "\M-g" 'font-lock-fontify-block)
 \f
 ;; Syntactic fontification functions.
 
@@ -1024,20 +1024,24 @@ START should be at the beginning of a line."
        (t                              ; Hopefully (MATCHER HIGHLIGHT ...)
         keyword)))
 
+(defun font-lock-value-in-major-mode (alist)
+  ;; Return value in ALIST for `major-mode', or ALIST if it is not an alist.
+  ;; 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)).
-  (let ((level (if (not (consp level))
-                  level
-                (cdr (or (assq major-mode level) (assq t level))))))
-    (cond ((symbolp keywords)
-          keywords)
-         ((numberp level)
-          (or (nth level keywords) (car (reverse keywords))))
-         ((eq level t)
-          (car (reverse keywords)))
-         (t
-          (car keywords)))))
+  (cond ((symbolp keywords)
+        keywords)
+       ((numberp level)
+        (or (nth level keywords) (car (reverse keywords))))
+       ((eq level t)
+        (car (reverse keywords)))
+       (t
+        (car keywords))))
 
 (defun font-lock-set-defaults ()
   "Set fontification defaults appropriately for this mode.
@@ -1047,40 +1051,40 @@ Sets various variables using `font-lock-defaults' (or, if nil, using
   (font-lock-make-faces)
   ;; Set fontification defaults.
   (make-local-variable 'font-lock-fontified)
-  (if font-lock-keywords
-      nil
-    (let* ((defaults (or font-lock-defaults
-                        (cdr (assq major-mode font-lock-defaults-alist))))
-          (keywords (font-lock-choose-keywords
-                     (nth 0 defaults) font-lock-maximum-decoration)))
-      ;; Regexp fontification?
-      (setq font-lock-keywords (if (fboundp keywords)
-                                  (funcall keywords)
-                                (eval keywords)))
-      ;; Syntactic fontification?
-      (if (nth 1 defaults)
-         (set (make-local-variable 'font-lock-keywords-only) t))
-      ;; Case fold during regexp fontification?
-      (if (nth 2 defaults)
-         (set (make-local-variable 'font-lock-keywords-case-fold-search) t))
-      ;; Syntax table for regexp and syntactic fontification?
-      (if (nth 3 defaults)
-         (let ((slist (nth 3 defaults)))
-           (set (make-local-variable 'font-lock-syntax-table)
-                (copy-syntax-table (syntax-table)))
-           (while slist
-             (modify-syntax-entry (car (car slist)) (cdr (car slist))
-                                  font-lock-syntax-table)
-             (setq slist (cdr slist)))))
-      ;; Syntax function for syntactic fontification?
-      (if (nth 4 defaults)
-         (set (make-local-variable 'font-lock-beginning-of-syntax-function)
-              (nth 4 defaults)))
-      ;; Variable alist?
-      (let ((alist (nthcdr 5 defaults)))
-       (while alist
-         (set (make-local-variable (car (car alist))) (cdr (car alist)))
-         (setq alist (cdr alist)))))))
+  (if (member font-lock-keywords '(nil (t)))
+      (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))))
+       ;; Regexp fontification?
+       (setq font-lock-keywords (if (fboundp keywords)
+                                    (funcall keywords)
+                                  (eval keywords)))
+       ;; Syntactic fontification?
+       (if (nth 1 defaults)
+           (set (make-local-variable 'font-lock-keywords-only) t))
+       ;; Case fold during regexp fontification?
+       (if (nth 2 defaults)
+           (set (make-local-variable 'font-lock-keywords-case-fold-search) t))
+       ;; Syntax table for regexp and syntactic fontification?
+       (if (nth 3 defaults)
+           (let ((slist (nth 3 defaults)))
+             (set (make-local-variable 'font-lock-syntax-table)
+                  (copy-syntax-table (syntax-table)))
+             (while slist
+               (modify-syntax-entry (car (car slist)) (cdr (car slist))
+                                    font-lock-syntax-table)
+               (setq slist (cdr slist)))))
+       ;; Syntax function for syntactic fontification?
+       (if (nth 4 defaults)
+           (set (make-local-variable 'font-lock-beginning-of-syntax-function)
+                (nth 4 defaults)))
+       ;; Variable alist?
+       (let ((alist (nthcdr 5 defaults)))
+         (while alist
+           (set (make-local-variable (car (car alist))) (cdr (car alist)))
+           (setq alist (cdr alist)))))))
 
 (defun font-lock-unset-defaults ()
   "Unset fontification defaults.  See `font-lock-set-defaults'."
@@ -1351,7 +1355,7 @@ the face is also set; its value is the face name."
     '("\\<:\\sw+\\>" 0 font-lock-reference-face prepend)
     ;;
     ;; ELisp and CLisp `&' keywords as types.
-    '("\\<\\&\\(optional\\|rest\\|whole\\)\\>" . font-lock-type-face)
+    '("\\<\\&\\sw+\\>" . font-lock-type-face)
     ))
   "Gaudy level highlighting for Lisp modes.")