summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRudolf Adamkovič <rudolf@adamkovic.org>2025-12-09 13:35:42 +0100
committerEli Zaretskii <eliz@gnu.org>2025-12-20 14:21:42 +0200
commit985e942d1ccb71c9b29b35080098b8fb851eecde (patch)
treea4459bb7d50ad3969421e8a63f14f1394c5af859
parent9e16010686a99a8b9ee1c01dff248a680605a6ca (diff)
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)
-rw-r--r--etc/NEWS8
-rw-r--r--lisp/hi-lock.el36
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,