unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH] isearch: lazy-highlighting of sub-exps of regexps
@ 2010-11-22 10:38 Andrew Helsley
  2010-11-23  0:59 ` Juri Linkov
  0 siblings, 1 reply; 8+ messages in thread
From: Andrew Helsley @ 2010-11-22 10:38 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: TEXT/PLAIN, Size: 382 bytes --]

Hi,

Attached is a patch for `isearch-mode'.  With this enhancement, the user 
can easily see examples of what the groups in their regular expressions 
capture as they type them.  Using this feedback should make it easier for 
the user to accurately build a replacement string when they invoke 
`query-replace-regexp' directly from `isearch-mode'.

--------
regards,
Andrew Helsley

[-- Attachment #2: Type: TEXT/PLAIN, Size: 4993 bytes --]

diff -r -c emacs-1/lisp/ChangeLog emacs-2/lisp/ChangeLog
*** emacs-1/lisp/ChangeLog	2010-11-22 00:28:12.000000000 -0800
--- emacs-2/lisp/ChangeLog	2010-11-22 00:42:23.000000000 -0800
***************
*** 1,3 ****
--- 1,9 ----
+ 2010-11-22  Andrew Helsley <helsleya@cs.ucr.edu>
+ 
+ 	* isearch.el: Added lazy-highlighting of up to 9 sub-expressions of
+ 	isearch regexps using different faces for each
+ 	(isearch-lazy-highlight-propertize-one-match).
+ 
  2010-11-22  Tassilo Horn  <tassilo@member.fsf.org>
  
  	* textmodes/reftex-ref.el (reftex-goto-label): Use the current
diff -r -c emacs-1/lisp/isearch.el emacs-2/lisp/isearch.el
*** emacs-1/lisp/isearch.el	2010-11-21 22:57:58.000000000 -0800
--- emacs-2/lisp/isearch.el	2010-11-22 00:37:55.000000000 -0800
***************
*** 2581,2586 ****
--- 2581,2590 ----
  (defvar isearch-lazy-highlight-regexp nil)
  (defvar isearch-lazy-highlight-space-regexp nil)
  (defvar isearch-lazy-highlight-forward nil)
+ (defvar isearch-lazy-highlight-last-capture-face 9)
+ (defvar isearch-lazy-highlight-minimum-overlay-priority 1000
+   "Minimum priority to assign to `isearch' mode overlays.
+ 1000 is higher than `ediff's 100+.")
  
  (defun lazy-highlight-cleanup (&optional force)
    "Stop lazy highlighting and remove extra highlighting from current buffer.
***************
*** 2717,2729 ****
  			      (forward-char -1)))
  
  			;; non-zero-length match
! 			(let ((ov (make-overlay mb me)))
! 			  (push ov isearch-lazy-highlight-overlays)
! 			  ;; 1000 is higher than ediff's 100+,
! 			  ;; but lower than isearch main overlay's 1001
! 			  (overlay-put ov 'priority 1000)
! 			  (overlay-put ov 'face lazy-highlight-face)
! 			  (overlay-put ov 'window (selected-window))))
  		      (if isearch-lazy-highlight-forward
  			  (setq isearch-lazy-highlight-end (point))
  			(setq isearch-lazy-highlight-start (point)))))
--- 2721,2727 ----
  			      (forward-char -1)))
  
  			;; non-zero-length match
! 			(isearch-lazy-highlight-propertize-one-match))
  		      (if isearch-lazy-highlight-forward
  			  (setq isearch-lazy-highlight-end (point))
  			(setq isearch-lazy-highlight-start (point)))))
***************
*** 2747,2752 ****
--- 2745,2801 ----
  		    (run-at-time lazy-highlight-interval nil
  				 'isearch-lazy-highlight-update)))))))))
  
+ (defun isearch-lazy-highlight-propertize-one-match ()
+   "Propertize the groups of the most recent regexp match."
+   (let* ((matches   (match-data))
+ 	 (n	    0))
+     (while (>= (length matches) 2)
+       (let* ((m   (min n isearch-lazy-highlight-last-capture-face))
+ 	     (beg (car matches))
+ 	     (end (cadr matches))
+ 	     (ov  (make-overlay beg end)))
+ 	(push ov isearch-lazy-highlight-overlays)
+ 	(overlay-put ov 'priority (+ isearch-lazy-highlight-minimum-overlay-priority n))
+ 	(overlay-put ov 'face (intern (format "isearch-lazy-highlight-capture-%02d" m)))
+ 	(overlay-put ov 'window (selected-window)))
+       (setq n (1+ n))
+       (setq matches (cddr matches)))))
+ 
+ (eval-when-compile (require 'cl))
+ (flet ((isearch-lazy-highlight-define-capture-face
+ 	(num foreground-color background-color &rest other-face-attributes)
+ 	(custom-declare-face (intern (format "isearch-lazy-highlight-capture-%02d" num))
+ 			     `((((background dark))
+ 				(:background	,foreground-color
+ 				 :foreground	,background-color
+ 				 :underline	,(> num 0)
+ 				 :overline	,(> num 1)
+ 				 ,@other-face-attributes
+ 				 :inherit	lazy-highlight))
+ 			       (((background light))
+ 				(:background	,background-color
+ 				 :foreground	,foreground-color
+ 				 :underline	,(> num 0)
+ 				 :overline	,(> num 1)
+ 				 ,@other-face-attributes
+ 				 :inherit	lazy-highlight))
+ 			       (t
+ 				(:underline	t)))
+ 			       (concat "Face for lazy highlighting group "
+ 				       (number-to-string num)
+ 				       " of a regexp search.")
+ 			       :group 'lazy-highlight :group 'basic-faces)))
+   (isearch-lazy-highlight-define-capture-face 0 nil "cyan")
+   (isearch-lazy-highlight-define-capture-face 1 "blue" "yellow" :weight 'bold)
+   (isearch-lazy-highlight-define-capture-face 2 "blue" "green" :weight 'bold)
+   (isearch-lazy-highlight-define-capture-face 3 "white" "magenta" :weight 'bold)
+   (isearch-lazy-highlight-define-capture-face 4 "yellow" "red" :weight 'bold)
+   (isearch-lazy-highlight-define-capture-face 5 "yellow" "blue" :weight 'bold)
+   (isearch-lazy-highlight-define-capture-face 6 "white" "blue" :weight 'bold)
+   (isearch-lazy-highlight-define-capture-face 7 "cyan" "blue" :weight 'bold)
+   (isearch-lazy-highlight-define-capture-face 8 "magenta" "blue" :weight 'bold)
+   (isearch-lazy-highlight-define-capture-face 9 "green" "blue" :weight 'bold))
+ 
  (defun isearch-resume (string regexp word forward message case-fold)
    "Resume an incremental search.
  STRING is the string or regexp searched for.

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2010-11-25  3:20 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-11-22 10:38 [PATCH] isearch: lazy-highlighting of sub-exps of regexps Andrew Helsley
2010-11-23  0:59 ` Juri Linkov
2010-11-23 14:48   ` Stefan Monnier
2010-11-24  1:55     ` Juri Linkov
2010-11-24  2:43       ` Stefan Monnier
2010-11-24  5:03         ` Drew Adams
2010-11-25  1:39           ` Juri Linkov
2010-11-25  3:20             ` Drew Adams

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).