]> code.delx.au - gnu-emacs/blobdiff - lisp/font-lock.el
; Merge from origin/emacs-25
[gnu-emacs] / lisp / font-lock.el
index 8e8c6eb3845379211a86a2f04e44db32c030bfcf..471db6b148fedb8cee6fc597447f359d0280d258 100644 (file)
@@ -1,6 +1,6 @@
 ;;; font-lock.el --- Electric font lock mode
 
-;; Copyright (C) 1992-2015 Free Software Foundation, Inc.
+;; Copyright (C) 1992-2016 Free Software Foundation, Inc.
 
 ;; Author: Jamie Zawinski
 ;;     Richard Stallman
@@ -39,7 +39,7 @@
 ;;
 ;; To turn Font Lock mode on automatically, add this to your init file:
 ;;
-;;  (add-hook 'emacs-lisp-mode-hook 'turn-on-font-lock)
+;;  (add-hook 'emacs-lisp-mode-hook #'turn-on-font-lock)
 ;;
 ;; Or if you want to turn Font Lock mode on in many modes:
 ;;
@@ -556,32 +556,17 @@ This is normally set via `font-lock-defaults'.")
 If this is nil, the major mode's syntax table is used.
 This is normally set via `font-lock-defaults'.")
 
-(defvar font-lock-beginning-of-syntax-function nil
-  "Non-nil means use this function to move back outside all constructs.
-When called with no args it should move point backward to a place which
-is not in a string or comment and not within any bracket-pairs (or else,
-a place such that any bracket-pairs outside it can be ignored for Emacs
-syntax analysis and fontification).
-
-If this is nil, Font Lock uses `syntax-begin-function' to move back
-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" 'set)
-
 (defvar font-lock-mark-block-function nil
   "Non-nil means use this function to mark a block of text.
 When called with no args it should leave point at the beginning of any
 enclosing textual block and mark at the end.
 This is normally set via `font-lock-defaults'.")
 
-(defvar font-lock-fontify-buffer-function 'font-lock-default-fontify-buffer
+(defvar font-lock-fontify-buffer-function #'font-lock-default-fontify-buffer
   "Function to use for fontifying the buffer.
 This is normally set via `font-lock-defaults'.")
 
-(defvar font-lock-unfontify-buffer-function 'font-lock-default-unfontify-buffer
+(defvar font-lock-unfontify-buffer-function #'font-lock-default-unfontify-buffer
   "Function to use for unfontifying the buffer.
 This is used when turning off Font Lock mode.
 This is normally set via `font-lock-defaults'.")
@@ -595,7 +580,7 @@ If it fontifies a larger region, it should ideally return a list of the form
 \(jit-lock-bounds BEG . END) indicating the bounds of the region actually
 fontified.")
 
-(defvar font-lock-unfontify-region-function 'font-lock-default-unfontify-region
+(defvar font-lock-unfontify-region-function #'font-lock-default-unfontify-region
   "Function to use for unfontifying a region.
 It should take two args, the beginning and end of the region.
 This is normally set via `font-lock-defaults'.")
@@ -662,12 +647,12 @@ be enabled."
 (defun font-lock-mode-internal (arg)
   ;; Turn on Font Lock mode.
   (when arg
-    (add-hook 'after-change-functions 'font-lock-after-change-function t t)
+    (add-hook 'after-change-functions #'font-lock-after-change-function t t)
     (font-lock-set-defaults)
     (font-lock-turn-on-thing-lock))
   ;; Turn off Font Lock mode.
   (unless font-lock-mode
-    (remove-hook 'after-change-functions 'font-lock-after-change-function t)
+    (remove-hook 'after-change-functions #'font-lock-after-change-function t)
     (font-lock-unfontify-buffer)
     (font-lock-turn-off-thing-lock)))
 
@@ -778,7 +763,7 @@ see the variables `c-font-lock-extra-types', `c++-font-lock-extra-types',
              (setq font-lock-removed-keywords-alist
                    (delq cell font-lock-removed-keywords-alist)))))))
 
-;; Written by Anders Lindgren <andersl@andersl.com>.
+;; Written by Anders Lindgren
 ;;
 ;; Case study:
 ;; (I)  The keywords are removed from a major mode.
@@ -926,17 +911,17 @@ The value of this variable is used when Font Lock mode is turned on."
     (`jit-lock-mode
      ;; Prepare for jit-lock
      (remove-hook 'after-change-functions
-                  'font-lock-after-change-function t)
+                  #'font-lock-after-change-function t)
      (set (make-local-variable 'font-lock-flush-function)
-          'jit-lock-refontify)
+          #'jit-lock-refontify)
      (set (make-local-variable 'font-lock-ensure-function)
-          'jit-lock-fontify-now)
+          #'jit-lock-fontify-now)
      ;; Prevent font-lock-fontify-buffer from fontifying eagerly the whole
      ;; buffer.  This is important for things like CWarn mode which
      ;; adds/removes a few keywords and does a refontify (which takes ages on
      ;; large files).
      (set (make-local-variable 'font-lock-fontify-buffer-function)
-          'jit-lock-refontify)
+          #'jit-lock-refontify)
      ;; Don't fontify eagerly (and don't abort if the buffer is large).
      (set (make-local-variable 'font-lock-fontified) t)
      ;; Use jit-lock.
@@ -944,7 +929,7 @@ The value of this variable is used when Font Lock mode is turned on."
                         (not font-lock-keywords-only))
      ;; Tell jit-lock how we extend the region to refontify.
      (add-hook 'jit-lock-after-change-extend-region-functions
-               'font-lock-extend-jit-lock-region-after-change
+               #'font-lock-extend-jit-lock-region-after-change
                nil t))))
 
 (defun font-lock-turn-off-thing-lock ()
@@ -1052,7 +1037,7 @@ The region it returns may start or end in the middle of a line.")
    ;; Of course, this function doesn't do all of the above in all situations
    ;; (e.g. depending on whether jit-lock is in use) and it can't guess what
    ;; the caller wants.
-   (interactive-only "use ‘font-lock-ensure’ or ‘font-lock-flush’ instead."))
+   (interactive-only "use `font-lock-ensure' or `font-lock-flush' instead."))
   (interactive "p")
   (font-lock-set-defaults)
   (let ((font-lock-verbose (or font-lock-verbose interactively)))
@@ -1080,7 +1065,8 @@ Called with two arguments BEG and END.")
 
 (defun font-lock-flush (&optional beg end)
   "Declare the region BEG...END's fontification as out-of-date.
-If the region is not specified, it defaults to the whole buffer."
+If the region is not specified, it defaults to the entire
+accessible portion of the current buffer."
   (and font-lock-mode
        font-lock-fontified
        (funcall font-lock-flush-function
@@ -1088,13 +1074,20 @@ If the region is not specified, it defaults to the whole buffer."
 
 (defvar font-lock-ensure-function
   (lambda (_beg _end)
-    (unless font-lock-fontified (font-lock-default-fontify-buffer)))
+    (unless font-lock-fontified
+      (font-lock-default-fontify-buffer)
+      (unless font-lock-mode
+        ;; If font-lock is not enabled, we don't have the hooks in place to
+        ;; track modifications, so a subsequent call to font-lock-ensure can't
+        ;; assume that the fontification is still valid.
+        (setq font-lock-fontified nil))))
   "Function to make sure a region has been fontified.
 Called with two arguments BEG and END.")
 
 (defun font-lock-ensure (&optional beg end)
   "Make sure the region BEG...END has been fontified.
-If the region is not specified, it defaults to the whole buffer."
+If the region is not specified, it defaults to the entire accessible
+portion of the buffer."
   (font-lock-set-defaults)
   (funcall font-lock-ensure-function
            (or beg (point-min)) (or end (point-max))))
@@ -1181,7 +1174,9 @@ Put first the functions more likely to cause a change and cheaper to compute.")
   (let ((changed nil))
     (goto-char font-lock-beg)
     (unless (bolp)
-      (setq changed t font-lock-beg (line-beginning-position)))
+      (setq changed t font-lock-beg
+            (let ((inhibit-field-text-motion t))
+              (line-beginning-position))))
     (goto-char font-lock-end)
     (unless (bolp)
       (unless (eq font-lock-end
@@ -1313,15 +1308,18 @@ This function does 2 things:
                       (point-min))))
       (when (< end (point-max))
         (setq end
-              (if (get-text-property end 'font-lock-multiline)
-                  (or (text-property-any end (point-max)
-                                         'font-lock-multiline nil)
-                      (point-max))
+              (cond
+               ((get-text-property end 'font-lock-multiline)
+                (or (text-property-any end (point-max)
+                                       'font-lock-multiline nil)
+                    (point-max)))
+               ;; If `end' has been set by the function above, don't corrupt it.
+               (font-lock-extend-after-change-region-function end)
                 ;; Rounding up to a whole number of lines should include the
                 ;; line right after `end'.  Typical case: the first char of
                 ;; the line was deleted.  Or a \n was inserted in the middle
                 ;; of a line.
-                (1+ end))))
+               (t (1+ end)))))
       ;; Finally, pre-enlarge the region to a whole number of lines, to try
       ;; and anticipate what font-lock-default-fontify-region will do, so as to
       ;; avoid double-redisplay.
@@ -1347,7 +1345,7 @@ 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 ((inhibit-point-motion-hooks t) font-lock-beginning-of-syntax-function
+  (let ((inhibit-point-motion-hooks t)
        deactivate-mark)
     ;; Make sure we have the right `font-lock-keywords' etc.
     (if (not font-lock-mode) (font-lock-set-defaults))
@@ -1572,6 +1570,7 @@ START should be at the beginning of a line."
   "Put proper face on each string and comment between START and END.
 START should be at the beginning of a line."
   (syntax-propertize end)  ; Apply any needed syntax-table properties.
+  (with-syntax-table (or syntax-ppss-table (syntax-table))
   (let ((comment-end-regexp
         (or font-lock-comment-end-skip
             (regexp-quote
@@ -1606,7 +1605,7 @@ START should be at the beginning of a line."
                                     font-lock-comment-delimiter-face))))
          (< (point) end))
       (setq state (parse-partial-sexp (point) end nil nil state
-                                     'syntax-table)))))
+                                     'syntax-table))))))
 
 ;;; End of Syntactic fontification functions.
 \f
@@ -1765,11 +1764,10 @@ If SYNTACTIC-KEYWORDS is non-nil, it means these keywords are used for
          (cons t (cons keywords
                        (mapcar #'font-lock-compile-keyword keywords))))
     (if (and (not syntactic-keywords)
-            (let ((beg-function
-                   (or font-lock-beginning-of-syntax-function
-                       syntax-begin-function)))
-              (or (eq beg-function 'beginning-of-defun)
-                  (get beg-function 'font-lock-syntax-paren-check)))
+            (let ((beg-function syntax-begin-function))
+              (or (eq beg-function #'beginning-of-defun)
+                   (if (symbolp beg-function)
+                       (get beg-function 'font-lock-syntax-paren-check))))
             (not beginning-of-defun-function))
        ;; Try to detect when a string or comment contains something that
        ;; looks like a defun and would thus confuse font-lock.
@@ -1888,19 +1886,16 @@ Sets various variables using `font-lock-defaults' and
          (let ((syntax (cdr selem)))
            (dolist (char (if (numberp (car selem))
                              (list (car selem))
-                           (mapcar 'identity (car selem))))
+                           (mapcar #'identity (car selem))))
              (modify-syntax-entry char syntax font-lock-syntax-table)))))
-      ;; Syntax function for syntactic fontification?
-      (if (nth 4 defaults)
-       (set (make-local-variable 'font-lock-beginning-of-syntax-function)
-               (nth 4 defaults))
-        (kill-local-variable 'font-lock-beginning-of-syntax-function))
+      ;; (nth 4 defaults) used to hold `font-lock-beginning-of-syntax-function',
+      ;; but that was removed in 25.1, so if it's a cons cell, we assume that
+      ;; it's part of the variable alist.
       ;; Variable alist?
-      (dolist (x (nthcdr 5 defaults))
+      (dolist (x (nthcdr (if (consp (nth 4 defaults)) 4 5) defaults))
        (set (make-local-variable (car x)) (cdr x)))
       ;; Set up `font-lock-keywords' last because its value might depend
-      ;; on other settings (e.g. font-lock-compile-keywords uses
-      ;; font-lock-beginning-of-syntax-function).
+      ;; on other settings.
       (set (make-local-variable 'font-lock-keywords)
           (font-lock-eval-keywords keywords))
       ;; Local fontification?
@@ -2154,7 +2149,7 @@ Sets various variables using `font-lock-defaults' and
 ;;          ;; The default level is usually, but not necessarily, level 1.
 ;;          (setq level (- (length keywords)
 ;;                         (length (member (eval (car keywords))
-;;                                         (mapcar 'eval (cdr keywords))))))))
+;;                                         (mapcar #'eval (cdr keywords))))))))
 ;;      (setq font-lock-fontify-level (list level (> level 1)
 ;;                                       (< level (1- (length keywords))))))))
 ;;