aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGrégory Mounié2018-08-03 23:08:10 +0200
committerNoam Postavsky2018-08-14 19:38:21 -0400
commitcc5a23d40bfa7a832f7a6fb7a016557ac1416559 (patch)
tree258ec3f55c167d7d8c6da9635c67ed81d7677777
parent1164d49ba6a3ce59a2bd404219851d8e27b54611 (diff)
downloademacs-cc5a23d40bfa7a832f7a6fb7a016557ac1416559.tar.gz
emacs-cc5a23d40bfa7a832f7a6fb7a016557ac1416559.zip
Interactive Highlighting: prefix argument to select subexp
* doc/emacs/display.texi (Highlight Interactively): * etc/NEWS: Document the change. * lisp/hi-lock.el (hi-lock-face-buffer, hi-lock-set-pattern): Use the prefix argument to highlight only the corresponding sub-expression of the regexp (Bug#32365). Copyright-paperwork-exempt: yes
-rw-r--r--doc/emacs/display.texi3
-rw-r--r--etc/NEWS6
-rw-r--r--lisp/hi-lock.el20
3 files changed, 20 insertions, 9 deletions
diff --git a/doc/emacs/display.texi b/doc/emacs/display.texi
index 2f5ce80d607..fe4936d85a1 100644
--- a/doc/emacs/display.texi
+++ b/doc/emacs/display.texi
@@ -975,7 +975,8 @@ the word ``whim'' using the default face (a yellow background), type
975@kbd{M-s h r whim @key{RET} @key{RET}}. Any face can be used for 975@kbd{M-s h r whim @key{RET} @key{RET}}. Any face can be used for
976highlighting, Hi Lock provides several of its own and these are 976highlighting, Hi Lock provides several of its own and these are
977pre-loaded into a list of default values. While being prompted 977pre-loaded into a list of default values. While being prompted
978for a face use @kbd{M-n} and @kbd{M-p} to cycle through them. 978for a face use @kbd{M-n} and @kbd{M-p} to cycle through them. A prefix
979argument limits the highlighting to the corresponding subexpression.
979 980
980@vindex hi-lock-auto-select-face 981@vindex hi-lock-auto-select-face
981Setting the option @code{hi-lock-auto-select-face} to a non-@code{nil} 982Setting the option @code{hi-lock-auto-select-face} to a non-@code{nil}
diff --git a/etc/NEWS b/etc/NEWS
index 3ae956c7880..5146e756d00 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -716,6 +716,12 @@ A symbol 'uuid' can be passed to thing-at-point and it returns the
716UUID at point. 716UUID at point.
717 717
718 718
719** Interactive automatic highlighting
720
721+++
722*** 'highlight-regexp' can now highlight subexpressions.
723The command accepts a prefix argument to choose the subexpression.
724
719 725
720* New Modes and Packages in Emacs 27.1 726* New Modes and Packages in Emacs 27.1
721 727
diff --git a/lisp/hi-lock.el b/lisp/hi-lock.el
index 13ebffb1afc..23820cda587 100644
--- a/lisp/hi-lock.el
+++ b/lisp/hi-lock.el
@@ -429,10 +429,11 @@ highlighting will not update as you type."
429;;;###autoload 429;;;###autoload
430(defalias 'highlight-regexp 'hi-lock-face-buffer) 430(defalias 'highlight-regexp 'hi-lock-face-buffer)
431;;;###autoload 431;;;###autoload
432(defun hi-lock-face-buffer (regexp &optional face) 432(defun hi-lock-face-buffer (regexp &optional face subexp)
433 "Set face of each match of REGEXP to FACE. 433 "Set face of each match of REGEXP to FACE.
434Interactively, prompt for REGEXP using `read-regexp', then FACE. 434Interactively, prompt for REGEXP using `read-regexp', then FACE.
435Use the global history list for FACE. 435Use the global history list for FACE. Limit face setting to the
436corresponding SUBEXP of REGEXP.
436 437
437Use Font lock mode, if enabled, to highlight REGEXP. Otherwise, 438Use Font lock mode, if enabled, to highlight REGEXP. Otherwise,
438use overlays for highlighting. If overlays are used, the 439use overlays for highlighting. If overlays are used, the
@@ -441,10 +442,11 @@ highlighting will not update as you type."
441 (list 442 (list
442 (hi-lock-regexp-okay 443 (hi-lock-regexp-okay
443 (read-regexp "Regexp to highlight" 'regexp-history-last)) 444 (read-regexp "Regexp to highlight" 'regexp-history-last))
444 (hi-lock-read-face-name))) 445 (hi-lock-read-face-name)
446 current-prefix-arg))
445 (or (facep face) (setq face 'hi-yellow)) 447 (or (facep face) (setq face 'hi-yellow))
446 (unless hi-lock-mode (hi-lock-mode 1)) 448 (unless hi-lock-mode (hi-lock-mode 1))
447 (hi-lock-set-pattern regexp face)) 449 (hi-lock-set-pattern regexp face subexp))
448 450
449;;;###autoload 451;;;###autoload
450(defalias 'highlight-phrase 'hi-lock-face-phrase-buffer) 452(defalias 'highlight-phrase 'hi-lock-face-phrase-buffer)
@@ -686,11 +688,12 @@ with completion and history."
686 (add-to-list 'hi-lock-face-defaults face t)) 688 (add-to-list 'hi-lock-face-defaults face t))
687 (intern face))) 689 (intern face)))
688 690
689(defun hi-lock-set-pattern (regexp face) 691(defun hi-lock-set-pattern (regexp face &optional subexp)
690 "Highlight REGEXP with face FACE." 692 "Highlight SUBEXP of REGEXP with face FACE."
691 ;; Hashcons the regexp, so it can be passed to remove-overlays later. 693 ;; Hashcons the regexp, so it can be passed to remove-overlays later.
692 (setq regexp (hi-lock--hashcons regexp)) 694 (setq regexp (hi-lock--hashcons regexp))
693 (let ((pattern (list regexp (list 0 (list 'quote face) 'prepend))) 695 (setq subexp (or subexp 0))
696 (let ((pattern (list regexp (list subexp (list 'quote face) 'prepend)))
694 (no-matches t)) 697 (no-matches t))
695 ;; Refuse to highlight a text that is already highlighted. 698 ;; Refuse to highlight a text that is already highlighted.
696 (if (assoc regexp hi-lock-interactive-patterns) 699 (if (assoc regexp hi-lock-interactive-patterns)
@@ -712,7 +715,8 @@ with completion and history."
712 (goto-char search-start) 715 (goto-char search-start)
713 (while (re-search-forward regexp search-end t) 716 (while (re-search-forward regexp search-end t)
714 (when no-matches (setq no-matches nil)) 717 (when no-matches (setq no-matches nil))
715 (let ((overlay (make-overlay (match-beginning 0) (match-end 0)))) 718 (let ((overlay (make-overlay (match-beginning subexp)
719 (match-end subexp))))
716 (overlay-put overlay 'hi-lock-overlay t) 720 (overlay-put overlay 'hi-lock-overlay t)
717 (overlay-put overlay 'hi-lock-overlay-regexp regexp) 721 (overlay-put overlay 'hi-lock-overlay-regexp regexp)
718 (overlay-put overlay 'face face)) 722 (overlay-put overlay 'face face))