From 985e942d1ccb71c9b29b35080098b8fb851eecde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rudolf=20Adamkovi=C4=8D?= Date: Tue, 9 Dec 2025 13:35:42 +0100 Subject: [PATCH] hi-lock: Use active region for default values in more places * lisp/hi-lock.el (hi-lock-line-face-buffer, hi-lock-face-buffer) (hi-lock-face-phrase-buffer): Use the new function `hi-lock-read-regexp' to read font-lock patterns, mirroring `hi-lock-read-face-name' used to read face names. For end users, all three functions now get the default value from the active region, rather than just `hi-lock-face-buffer'. (hi-lock-read-regexp): Extract font-lock pattern reading functionality from `hi-lock-face-buffer' into this function, to mirror how faces are read with `hi-lock-read-face-name' and to promote reuse. (Bug#79976) --- etc/NEWS | 8 ++++++++ lisp/hi-lock.el | 36 +++++++++++++++++++++--------------- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index 6292cf981da..e3ea4557355 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -3231,6 +3231,14 @@ Command 'antlr-run-tool' now usually runs on the file for the current buffer. Customize this user option to have value ' nil' to get the previous behavior back. +** Hi Lock + +--- +*** Use active region for default values in more functions. +If an active region exists, the commands 'hi-lock-line-face-buffer' and +'hi-lock-face-phrase-buffer' now use its contents as their default +value. Previously, only 'hi-lock-face-buffer' supported this. + * New Modes and Packages in Emacs 31.1 diff --git a/lisp/hi-lock.el b/lisp/hi-lock.el index 35ee01c2f3b..71f302f3ebc 100644 --- a/lisp/hi-lock.el +++ b/lisp/hi-lock.el @@ -393,7 +393,7 @@ The lines that match REGEXP will be displayed by merging the attributes of FACE with any other face attributes of text in those lines. -Interactively, prompt for REGEXP using `read-regexp', then FACE. +Interactively, prompt for REGEXP using `hi-lock-read-regexp', then FACE. Use the global history list for FACE. If REGEXP contains upper case characters (excluding those preceded by `\\') @@ -404,8 +404,7 @@ use overlays for highlighting. If overlays are used, the highlighting will not update as you type." (interactive (list - (hi-lock-regexp-okay - (read-regexp "Regexp to highlight line" 'regexp-history-last)) + (hi-lock-read-regexp "Regexp to highlight line") (hi-lock-read-face-name))) (or (facep face) (setq face 'hi-yellow)) (unless hi-lock-mode (hi-lock-mode 1)) @@ -423,7 +422,7 @@ highlighting will not update as you type." ;;;###autoload (defun hi-lock-face-buffer (regexp &optional face subexp lighter) "Set face of each match of REGEXP to FACE. -Interactively, prompt for REGEXP using `read-regexp', then FACE. +Interactively, prompt for REGEXP using `hi-lock-read-regexp', then FACE. Use the global history list for FACE. Limit face setting to the corresponding SUBEXP (interactively, the prefix argument) of REGEXP. If SUBEXP is omitted or nil, the entire REGEXP is highlighted. @@ -443,14 +442,7 @@ causes `font-lock-specified-p' to return non-nil, which means the major mode specifies support for Font Lock." (interactive (list - (hi-lock-regexp-okay - (read-regexp "Regexp to highlight" - (if (use-region-p) - (prog1 - (buffer-substring (region-beginning) - (region-end)) - (deactivate-mark)) - 'regexp-history-last))) + (hi-lock-read-regexp "Regexp to highlight") (hi-lock-read-face-name) current-prefix-arg)) (when (stringp face) @@ -469,7 +461,7 @@ the major mode specifies support for Font Lock." ;;;###autoload (defun hi-lock-face-phrase-buffer (regexp &optional face) "Set face of each match of phrase REGEXP to FACE. -Interactively, prompt for REGEXP using `read-regexp', then FACE. +Interactively, prompt for REGEXP using `hi-lock-read-regexp', then FACE. Use the global history list for FACE. If REGEXP contains upper case characters (excluding those preceded by `\\') @@ -484,8 +476,7 @@ causes `font-lock-specified-p' to return non-nil, which means the major mode specifies support for Font Lock." (interactive (list - (hi-lock-regexp-okay - (read-regexp "Phrase to highlight" 'regexp-history-last)) + (hi-lock-read-regexp "Phrase to highlight") (hi-lock-read-face-name))) (or (facep face) (setq face 'hi-yellow)) (unless hi-lock-mode (hi-lock-mode 1)) @@ -726,6 +717,21 @@ with completion and history." (add-to-list 'hi-lock-face-defaults face t)) (intern face))) +(defun hi-lock-read-regexp (prompt) + "Read font-lock pattern from the minibuffer and return it. + +The pattern is read using `read-regexp' with PROMPT and validated using +`hi-lock-regexp-okay'. If the region is active, use its content as the +default value." + (hi-lock-regexp-okay + (read-regexp prompt + (if (use-region-p) + (prog1 + (buffer-substring (region-beginning) + (region-end)) + (deactivate-mark)) + 'regexp-history-last)))) + (defvar hi-lock-use-overlays nil "Whether to always use overlays instead of font-lock rules. When `font-lock-mode' is enabled and the buffer specifies font-lock rules,