From c77e6e67da31b918d92aacce1c86ac819cfd5fcc Mon Sep 17 00:00:00 2001 From: Gregory Heytings Date: Thu, 25 Mar 2021 20:08:53 +0000 Subject: [PATCH] Improve font-lock-update and rename it to font-lock-dwim * lisp/font-lock.el (font-lock-dwim): Renamed from 'font-lock-update'. Only refontify the region when it is active. Do not refontify too large buffers at once. * lisp/bindings.el (ctl-x-x-map): Adjust the command name. * etc/NEWS: Adjust the command name. --- etc/NEWS | 4 ++-- lisp/bindings.el | 2 +- lisp/font-lock.el | 14 +++++++++----- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index 68812c64cc..58cc4a2b82 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -94,7 +94,7 @@ useful on systems such as FreeBSD which ships only with "etc/termcap". * Changes in Emacs 28.1 +++ -** New command 'font-lock-update', bound to 'C-x x f'. +** New command 'font-lock-dwim', bound to 'C-x x f'. This command updates the syntax highlighting in this buffer. ** The new NonGNU ELPA archive is enabled by default alongside GNU ELPA. @@ -255,7 +255,7 @@ The 'C-x x' keymap now holds keystrokes for various buffer-oriented commands. The new keystrokes are 'C-x x g' ('revert-buffer'), 'C-x x r' ('rename-buffer'), 'C-x x u' ('rename-uniquely'), 'C-x x n' ('clone-buffer'), 'C-x x i' ('insert-buffer'), 'C-x x t' -('toggle-truncate-lines') and 'C-x x f' ('font-lock-update'). +('toggle-truncate-lines') and 'C-x x f' ('font-lock-dwim'). --- ** Commands 'set-frame-width' and 'set-frame-height' can now get their diff --git a/lisp/bindings.el b/lisp/bindings.el index 6eac528eb6..6f25e9738a 100644 --- a/lisp/bindings.el +++ b/lisp/bindings.el @@ -1432,7 +1432,7 @@ ctl-x-map (defvar ctl-x-x-map (let ((map (make-sparse-keymap))) - (define-key map "f" #'font-lock-update) + (define-key map "f" #'font-lock-dwim) (define-key map "g" #'revert-buffer) (define-key map "r" #'rename-buffer) (define-key map "u" #'rename-uniquely) diff --git a/lisp/font-lock.el b/lisp/font-lock.el index 82915d8c8b..1800f0b56d 100644 --- a/lisp/font-lock.el +++ b/lisp/font-lock.el @@ -1120,15 +1120,19 @@ font-lock-ensure (funcall font-lock-ensure-function (or beg (point-min)) (or end (point-max))))) -(defun font-lock-update (&optional arg) +(defun font-lock-dwim (&optional arg) "Updates the syntax highlighting in this buffer. -Refontify the accessible portion of this buffer, or enable Font Lock mode -in this buffer if it is currently disabled. With prefix ARG, toggle Font -Lock mode." +Enable Font Lock mode if it is disabled. Otherwise, refontify the region +if it is active, or a large part of the accessible portion of the buffer. +Otherwise, with prefix ARG, toggle Font Lock mode." (interactive "P") (save-excursion (if (and (not arg) font-lock-mode) - (font-lock-fontify-region (point-min) (point-max)) + (if (use-region-p) + (font-lock-fontify-region (region-beginning) (region-end)) + (font-lock-flush (point-min) (point-max)) + (font-lock-fontify-region (max (point-min) (min (- (point) 50000) (window-start))) + (min (point-max) (max (+ (point) 50000) (window-end))))) (font-lock-unfontify-region (point-min) (point-max)) (font-lock-mode 'toggle)))) -- 2.30.2