]> git.eshelyaron.com Git - emacs.git/commitdiff
New option: font-lock-large-files
authorDmitry Gutov <dgutov@yandex.ru>
Fri, 12 Aug 2022 10:46:04 +0000 (13:46 +0300)
committerDmitry Gutov <dgutov@yandex.ru>
Fri, 12 Aug 2022 10:46:04 +0000 (13:46 +0300)
* lisp/font-lock.el (font-lock-fontify-region): Use it.
(font-lock-large-files): New option (bug#56682).

* src/xdisp.c (handle_fontified_prop): Don't apply narrowing here.

lisp/font-lock.el
src/xdisp.c

index 7eb5a414fe3dc9d7db55e3cff4b4deff671d8368..88bd6cbc5b64badaa1a7fb716c78bf0ec8adc703 100644 (file)
@@ -254,6 +254,23 @@ decoration for buffers in C++ mode, and level 1 decoration otherwise."
                                      (integer :tag "level" 1)))))
   :group 'font-lock)
 
+(defcustom font-lock-large-files t
+  "How to fontify large files.
+When t, apply highlighting without restriction.
+
+When its a cons with car equal to `head', fontify the first (cdr
+value) number of characters only.
+
+When its a cons with car equal to `narrow', narrow the buffer
+to (cdr value) characters around point. That speeds up
+fontification at the expense of possible misdetection of syntax
+context."
+  :type '(choice (const :tag "full" t)
+                 (cons :tag "head"
+                       (const head) (integer :tag "length" 1000000))
+                 (cons :tag "narrow"
+                       (const narrow) (integer :tag "width" 15000))))
+
 (defcustom font-lock-ignore nil
   "Rules to selectively disable fontifications due to `font-lock-keywords'.
 If non-nil, the value should be a list of condition sets of the form
@@ -996,7 +1013,18 @@ If LOUDLY is non-nil, print status messages while fontifying.
 This works by calling `font-lock-fontify-region-function'."
   (font-lock-set-defaults)
   (save-restriction
-    (unless font-lock-dont-widen (widen))
+    (pcase-exhaustive font-lock-large-files
+      (`t nil)
+      (`(head . ,length)
+       (setq beg (min length beg)
+             end (min length end)))
+      (`(narrow . ,width)
+       (narrow-to-region (max (point-min) (* (1- (/ beg width)) width))
+                         (min (point-max) (* (1+ (/ beg width)) width)))
+       (setq end (min end (point-max)))))
+    (unless (or font-lock-dont-widen
+                (eq (car-safe font-lock-large-files) 'narrow))
+      (widen))
     (funcall font-lock-fontify-region-function beg end loudly)))
 
 (defun font-lock-unfontify-region (beg end)
index 855f48f2bdee07a9b31dcd5c4c68751bd6ee88a5..1fcff2c2e3bc23f49d48edea0c6b434e41a2df1b 100644 (file)
@@ -4392,20 +4392,6 @@ handle_fontified_prop (struct it *it)
 
       eassert (it->end_charpos == ZV);
 
-      if (current_buffer->long_line_optimizations_p)
-       {
-         ptrdiff_t begv = it->narrowed_begv;
-         ptrdiff_t zv = it->narrowed_zv;
-         ptrdiff_t charpos = IT_CHARPOS (*it);
-         if (charpos < begv || charpos > zv)
-           {
-             begv = get_narrowed_begv (it->w, charpos);
-             zv = get_narrowed_zv (it->w, charpos);
-           }
-         narrow_to_region_internal (make_fixnum (begv), make_fixnum (zv), true);
-         specbind (Qrestrictions_locked, Qt);
-       }
-
       /* Don't allow Lisp that runs from 'fontification-functions'
         clear our face and image caches behind our back.  */
       it->f->inhibit_clear_image_cache = true;