diff options
| author | Grégory Mounié | 2018-08-03 23:08:10 +0200 |
|---|---|---|
| committer | Noam Postavsky | 2018-08-14 19:38:21 -0400 |
| commit | cc5a23d40bfa7a832f7a6fb7a016557ac1416559 (patch) | |
| tree | 258ec3f55c167d7d8c6da9635c67ed81d7677777 | |
| parent | 1164d49ba6a3ce59a2bd404219851d8e27b54611 (diff) | |
| download | emacs-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.texi | 3 | ||||
| -rw-r--r-- | etc/NEWS | 6 | ||||
| -rw-r--r-- | lisp/hi-lock.el | 20 |
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 |
| 976 | highlighting, Hi Lock provides several of its own and these are | 976 | highlighting, Hi Lock provides several of its own and these are |
| 977 | pre-loaded into a list of default values. While being prompted | 977 | pre-loaded into a list of default values. While being prompted |
| 978 | for a face use @kbd{M-n} and @kbd{M-p} to cycle through them. | 978 | for a face use @kbd{M-n} and @kbd{M-p} to cycle through them. A prefix |
| 979 | argument limits the highlighting to the corresponding subexpression. | ||
| 979 | 980 | ||
| 980 | @vindex hi-lock-auto-select-face | 981 | @vindex hi-lock-auto-select-face |
| 981 | Setting the option @code{hi-lock-auto-select-face} to a non-@code{nil} | 982 | Setting the option @code{hi-lock-auto-select-face} to a non-@code{nil} |
| @@ -716,6 +716,12 @@ A symbol 'uuid' can be passed to thing-at-point and it returns the | |||
| 716 | UUID at point. | 716 | UUID at point. |
| 717 | 717 | ||
| 718 | 718 | ||
| 719 | ** Interactive automatic highlighting | ||
| 720 | |||
| 721 | +++ | ||
| 722 | *** 'highlight-regexp' can now highlight subexpressions. | ||
| 723 | The 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. |
| 434 | Interactively, prompt for REGEXP using `read-regexp', then FACE. | 434 | Interactively, prompt for REGEXP using `read-regexp', then FACE. |
| 435 | Use the global history list for FACE. | 435 | Use the global history list for FACE. Limit face setting to the |
| 436 | corresponding SUBEXP of REGEXP. | ||
| 436 | 437 | ||
| 437 | Use Font lock mode, if enabled, to highlight REGEXP. Otherwise, | 438 | Use Font lock mode, if enabled, to highlight REGEXP. Otherwise, |
| 438 | use overlays for highlighting. If overlays are used, the | 439 | use 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)) |