- (when jit-lock-mode
- (with-buffer-prepared-for-font-lock
- (let ((end (min (point-max) (+ start jit-lock-chunk-size)))
- (parse-sexp-lookup-properties font-lock-syntactic-keywords)
- (old-syntax-table (syntax-table))
- (font-lock-beginning-of-syntax-function nil)
- next)
- (when font-lock-syntax-table
- (set-syntax-table font-lock-syntax-table))
- (save-excursion
- (save-restriction
- (widen)
- (save-match-data
- (condition-case error
- ;; Fontify chunks beginning at START. The end of a
- ;; chunk is either `end', or the start of a region
- ;; before `end' that has already been fontified.
- (while start
- ;; Determine the end of this chunk.
- (setq next (or (text-property-any start end 'fontified t)
- end))
-
- ;; Goto to the start of the chunk. Make sure we
- ;; start fontifying at the beginning of the line
- ;; containing the chunk start because font-lock
- ;; functions seem to expects this, if I believe
- ;; lazy-lock.
- (goto-char start)
- (setq start (line-beginning-position))
-
- ;; Fontify the chunk, and mark it as fontified.
- (font-lock-fontify-region start end nil)
- (add-text-properties start next '(fontified t))
-
- ;; Find the start of the next chunk, if any.
- (setq start (text-property-any next end 'fontified nil)))
-
- ((error quit)
- (message "Fontifying region...%s" error))))))
-
- ;; Restore previous buffer settings.
- (set-syntax-table old-syntax-table)))))
-
-
-(defun jit-lock-after-fontify-buffer ()
- "Mark the current buffer as fontified.
-Called from `font-lock-after-fontify-buffer."
- (with-buffer-prepared-for-font-lock
- (add-text-properties (point-min) (point-max) '(fontified t))))
-
-
-(defun jit-lock-after-unfontify-buffer ()
- "Mark the current buffer as unfontified.
-Called from `font-lock-after-fontify-buffer."
- (with-buffer-prepared-for-font-lock
- (remove-text-properties (point-min) (point-max) '(fontified nil))))
-
+ (when (and jit-lock-mode (not (memory-full-p)))
+ (if (null jit-lock-defer-time)
+ ;; No deferral.
+ (jit-lock-fontify-now start (+ start jit-lock-chunk-size))
+ ;; Record the buffer for later fontification.
+ (unless (memq (current-buffer) jit-lock-defer-buffers)
+ (push (current-buffer) jit-lock-defer-buffers))
+ ;; Mark the area as defer-fontified so that the redisplay engine
+ ;; is happy and so that the idle timer can find the places to fontify.
+ (with-buffer-prepared-for-jit-lock
+ (put-text-property start
+ (next-single-property-change
+ start 'fontified nil
+ (min (point-max) (+ start jit-lock-chunk-size)))
+ 'fontified 'defer)))))
+
+(defun jit-lock-fontify-now (&optional start end)
+ "Fontify current buffer from START to END.
+Defaults to the whole buffer. END can be out of bounds."
+ (with-buffer-prepared-for-jit-lock
+ (save-excursion
+ (unless start (setq start (point-min)))
+ (setq end (if end (min end (point-max)) (point-max)))
+ ;; This did bind `font-lock-beginning-of-syntax-function' to
+ ;; nil at some point, for an unknown reason. Don't do this; it
+ ;; can make highlighting slow due to expensive calls to
+ ;; `parse-partial-sexp' in function
+ ;; `font-lock-fontify-syntactically-region'. Example: paging
+ ;; from the end of a buffer to its start, can do repeated
+ ;; `parse-partial-sexp' starting from `point-min', which can
+ ;; take a long time in a large buffer.
+ (let (next)
+ (save-match-data
+ ;; Fontify chunks beginning at START. The end of a
+ ;; chunk is either `end', or the start of a region
+ ;; before `end' that has already been fontified.
+ (while start
+ ;; Determine the end of this chunk.
+ (setq next (or (text-property-any start end 'fontified t)
+ end))
+
+ ;; Decide which range of text should be fontified.
+ ;; The problem is that START and NEXT may be in the
+ ;; middle of something matched by a font-lock regexp.
+ ;; Until someone has a better idea, let's start
+ ;; at the start of the line containing START and
+ ;; stop at the start of the line following NEXT.
+ (goto-char next) (setq next (line-beginning-position 2))
+ (goto-char start) (setq start (line-beginning-position))
+
+ ;; Fontify the chunk, and mark it as fontified.
+ ;; We mark it first, to make sure that we don't indefinitely
+ ;; re-execute this fontification if an error occurs.
+ (put-text-property start next 'fontified t)
+ (condition-case err
+ (run-hook-with-args 'jit-lock-functions start next)
+ ;; If the user quits (which shouldn't happen in normal on-the-fly
+ ;; jit-locking), make sure the fontification will be performed
+ ;; before displaying the block again.
+ (quit (put-text-property start next 'fontified nil)
+ (funcall 'signal (car err) (cdr err))))
+
+ ;; Find the start of the next chunk, if any.
+ (setq start (text-property-any next end 'fontified nil))))))))