all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* C-q SPC in isearch
@ 2004-12-06  1:37 Stefan Monnier
  2004-12-08  0:59 ` Juri Linkov
  0 siblings, 1 reply; 3+ messages in thread
From: Stefan Monnier @ 2004-12-06  1:37 UTC (permalink / raw)



The recent change to C-q SPC in isearch fixes the problem of searching for
a b [ C-q SPC ] but not the case of a b \ C-q SPC which will end up
searching for "ab\\[ ]".

I suggest the patch below which additionally introduces the function
subregexp-context-p which can be used at other places that need the same
kind of information (e.g. regexp-opt).

Any objection?


        Stefan


Index: lisp/subr.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/subr.el,v
retrieving revision 1.428
diff -u -r1.428 subr.el
--- lisp/subr.el	23 Nov 2004 15:23:57 -0000	1.428
+++ lisp/subr.el	6 Dec 2004 01:32:31 -0000
@@ -2188,6 +2188,46 @@
       ;; Reconstruct a string from the pieces.
       (setq matches (cons (substring string start l) matches)) ; leftover
       (apply #'concat (nreverse matches)))))
+
+(defun subregexp-context-p (regexp pos &optional start)
+  "Return non-nil if POS is in a normal subregexp context in REGEXP.
+A subregexp context is one where a sub-regexp can appear.
+A non-subregexp context is for example within brackets, or within a repetition
+bounds operator \\{..\\}, or right after a \\.
+If START is non-nil, it should be a position in REGEXP, smaller than POS,
+and known to be in a subregexp context."
+  ;; Here's one possible implementation, with the great benefit that it
+  ;; reuses the regexp-matcher's own parser, so it understands all the
+  ;; details of the syntax.  A disadvantage is that it needs to match the
+  ;; error string.
+  (condition-case err
+      (progn
+        (string-match (substring regexp (or start 0) pos) "")
+        t)
+    (invalid-regexp
+     (not (member (cadr err) '("Unmatched [ or [^"
+                               "Unmatched \\{"
+                               "Trailing backslash")))))
+  ;; An alternative implementation:
+  ;; (defconst re-context-re
+  ;;   (let* ((harmless-ch "[^\\[]")
+  ;;          (harmless-esc "\\\\[^{]")
+  ;;          (class-harmless-ch "[^][]")
+  ;;          (class-lb-harmless "[^]:]")
+  ;;          (class-lb-colon-maybe-charclass ":\\([a-z]+:]\\)?")
+  ;;          (class-lb (concat "\\[\\(" class-lb-harmless
+  ;;                            "\\|" class-lb-colon-maybe-charclass "\\)"))
+  ;;          (class
+  ;;           (concat "\\[^?]?"
+  ;;                   "\\(" class-harmless-ch
+  ;;                   "\\|" class-lb "\\)*"
+  ;;                   "\\[?]"))     ; special handling for bare [ at end of re
+  ;;          (braces "\\\\{[0-9,]+\\\\}"))
+  ;;     (concat "\\`\\(" harmless-ch "\\|" harmless-esc
+  ;;             "\\|" class "\\|" braces "\\)*\\'"))
+  ;;   "Matches any prefix that corresponds to a normal subregexp context.")
+  ;; (string-match re-context-re (substring regexp (or start 0) pos))
+  )
 \f
 (defun shell-quote-argument (argument)
   "Quote an argument for passing as argument to an inferior shell."
Index: lisp/isearch.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/isearch.el,v
retrieving revision 1.243
diff -u -r1.243 isearch.el
--- lisp/isearch.el	3 Dec 2004 00:27:11 -0000	1.243
+++ lisp/isearch.el	6 Dec 2004 01:32:32 -0000
@@ -1682,13 +1682,9 @@
     ;; single-byte character set, and convert them to Emacs
     ;; characters.
     (if (and isearch-regexp (= char ?\ ))
-	(if (condition-case err
-		(progn
-		  (string-match isearch-string "")
-		  nil)
-	      (error (equal (cadr err) "Unmatched [ or [^")))
-	    (isearch-process-search-char char)
-	  (isearch-process-search-string "[ ]" " "))
+	(if (subregexp-context-p isearch-string pos)
+	    (isearch-process-search-string "[ ]" " ")
+	  (isearch-process-search-char char))
       (and enable-multibyte-characters
 	   (>= char ?\200)
 	   (<= char ?\377)
Index: regexp-opt.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/emacs-lisp/regexp-opt.el,v
retrieving revision 1.26
diff -u -u -b -r1.26 regexp-opt.el
--- regexp-opt.el	1 Sep 2003 15:45:22 -0000	1.26
+++ regexp-opt.el	6 Dec 2004 01:38:26 -0000
@@ -110,24 +112,6 @@
 	   (re (regexp-opt-group sorted-strings open)))
       (if words (concat "\\<" re "\\>") re))))
 
-(defconst regexp-opt-not-groupie*-re
-  (let* ((harmless-ch "[^\\\\[]")
-         (esc-pair-not-lp "\\\\[^(]")
-         (class-harmless-ch "[^][]")
-         (class-lb-harmless "[^]:]")
-         (class-lb-colon-maybe-charclass ":\\([a-z]+:]\\)?")
-         (class-lb (concat "\\[\\(" class-lb-harmless
-                           "\\|" class-lb-colon-maybe-charclass "\\)"))
-         (class
-          (concat "\\[^?]?"
-                  "\\(" class-harmless-ch
-                  "\\|" class-lb "\\)*"
-                  "\\[?]"))         ; special handling for bare [ at end of re
-         (shy-lp "\\\\(\\?:"))
-    (concat "\\(" harmless-ch "\\|" esc-pair-not-lp
-            "\\|" class "\\|" shy-lp "\\)*"))
-  "Matches any part of a regular expression EXCEPT for non-shy \"\\\\(\"s")
-
 ;;;###autoload
 (defun regexp-opt-depth (regexp)
   "Return the depth of REGEXP.
@@ -137,13 +121,15 @@
     ;; Hack to signal an error if REGEXP does not have balanced parentheses.
     (string-match regexp "")
     ;; Count the number of open parentheses in REGEXP.
-    (let ((count 0) start)
-      (while
-          (progn
-            (string-match regexp-opt-not-groupie*-re regexp start)
-            (setq start ( + (match-end 0) 2))  ; +2 for "\\(" after match-end.
-            (<= start (length regexp)))
-        (setq count (1+ count)))
+    (let ((count 0) start last)
+      (while (string-match "\\\\(\\(\\?:\\)?" regexp start)
+	(setq start (match-end 0))	      ; Start of next search.
+	(when (and (not (match-beginning 1))
+		   (subregexp-context-p regexp (match-beginning 0) last))
+	  ;; It's not a shy group and it's not inside brackets or after
+	  ;; a backslash: it's really a group-open marker.
+	  (setq last start)	    ; Speed up next regexp-opt-re-context-p.
+	  (setq count (1+ count))))
       count)))
 \f
 ;;; Workhorse functions.

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

* Re: C-q SPC in isearch
  2004-12-06  1:37 C-q SPC in isearch Stefan Monnier
@ 2004-12-08  0:59 ` Juri Linkov
  2004-12-08  1:38   ` Stefan Monnier
  0 siblings, 1 reply; 3+ messages in thread
From: Juri Linkov @ 2004-12-08  0:59 UTC (permalink / raw)
  Cc: emacs-devel

Stefan Monnier <monnier@iro.umontreal.ca> writes:
> I suggest the patch below which additionally introduces the function
> subregexp-context-p which can be used at other places that need the same
> kind of information (e.g. regexp-opt).
>
> Any objection?

There is a small problem:

> RCS file: /cvsroot/emacs/emacs/lisp/isearch.el,v
> [...]
> +	(if (subregexp-context-p isearch-string pos)
                                                ===
`pos' is not defined.

I can't deduce a useful value for it from the context.  So it seems
it should be removed from isearch.el, and the argument `pos' of
subregexp-context-p be changed to optional.  Is it what you intended?

PS: I noticed this problem only during testing.  Even if the byte
compiler generated a warning message, it remains unnoticed among other
numerous similar warnings.  There is an urgent need to get rid of all
warnings that refer to a valid use of free variables, and to find and
fix true bugs reported by remaining warnings before the next release.

-- 
Juri Linkov
http://www.jurta.org/emacs/

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

* Re: C-q SPC in isearch
  2004-12-08  0:59 ` Juri Linkov
@ 2004-12-08  1:38   ` Stefan Monnier
  0 siblings, 0 replies; 3+ messages in thread
From: Stefan Monnier @ 2004-12-08  1:38 UTC (permalink / raw)
  Cc: emacs-devel

>> +	(if (subregexp-context-p isearch-string pos)
>                                                 ===
> `pos' is not defined.

Thanks, fixed,


        Stefan

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

end of thread, other threads:[~2004-12-08  1:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-12-06  1:37 C-q SPC in isearch Stefan Monnier
2004-12-08  0:59 ` Juri Linkov
2004-12-08  1:38   ` Stefan Monnier

Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.