emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: isearch-allow-move [Was: isearch-allow-prefix]


From: Juri Linkov
Subject: Re: isearch-allow-move [Was: isearch-allow-prefix]
Date: Sun, 09 Jun 2013 23:09:24 +0300
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (x86_64-pc-linux-gnu)

> However, the new feature `M-s C-f' helped to discover a problem
> in a word search where the same problem exists in `C-M-y'
> (isearch-yank-char).

Actually removing OMIT-NULLS from `split-string' produces
a wrong regexp "\\bw\\W+\\b" for the input string "w ".
Better is to handle leading/trailing whitespace explicitly to
produce more correct regexp "\\bw\\b\\W+" as the patch below does.

This feature also helped to discover more problems (in addition
to the above mentioned problem where `isearch-yank-char' failed
to yank trailing whitespace).

Another problem is that `word-search-regexp' fails to handle
multi-line matches.  This problem can be fixed by using "\\`"
instead of "\\^" in `word-search-regexp'.

The third problem is the need to apply the LAX arg to the beginning
of the regexp in a reverse search that can be fixed by adding a new
argument BACKWARD to `word-search-regexp'.

`isearch-symbol-regexp' requires the same changes that will make it
a copy of `word-search-regexp' that uses "\\_<" and "\\_>" instead of "\\b".

=== modified file 'lisp/isearch.el'
--- lisp/isearch.el     2013-06-06 06:23:19 +0000
+++ lisp/isearch.el     2013-06-09 20:08:29 +0000
@@ -1536,7 +1638,7 @@ (defun isearch-toggle-invisible ()
 
 ;; Word search
 
-(defun word-search-regexp (string &optional lax)
+(defun word-search-regexp (string &optional lax backward)
   "Return a regexp which matches words, ignoring punctuation.
 Given STRING, a string of words separated by word delimiters,
 compute a regexp that matches those exact words separated by
@@ -1545,12 +1647,14 @@ (defun word-search-regexp (string &optio
 
 Used in `word-search-forward', `word-search-backward',
 `word-search-forward-lax', `word-search-backward-lax'."
-  (if (string-match-p "^\\W*$" string)
-      ""
+  (if (string-match-p "\\`\\W*\\'" string)
+      "\\W*"
     (concat
-     "\\b"
-     (mapconcat 'identity (split-string string "\\W+" t) "\\W+")
-     (if (or (not lax) (string-match-p "\\W$" string)) "\\b"))))
+     (if (string-match-p "\\`\\W" string) "\\W+")
+     (if (or (not lax) (not backward)) "\\b")
+     (mapconcat 'regexp-quote (split-string string "\\W+" t) "\\W+")
+     (if (or (not lax) backward) "\\b")
+     (if (string-match-p "\\W\\'" string) "\\W+"))))
 
 (defun word-search-backward (string &optional bound noerror count)
   "Search backward from point for STRING, ignoring differences in punctuation.
@@ -1565,7 +1669,7 @@ (defun word-search-backward (string &opt
 of words in STRING to a regexp used to search words without regard
 to punctuation."
   (interactive "sWord search backward: ")
-  (re-search-backward (word-search-regexp string nil) bound noerror count))
+  (re-search-backward (word-search-regexp string nil t) bound noerror count))
 
 (defun word-search-forward (string &optional bound noerror count)
   "Search forward from point for STRING, ignoring differences in punctuation.
@@ -1580,7 +1684,7 @@ (defun word-search-forward (string &opti
 of words in STRING to a regexp used to search words without regard
 to punctuation."
   (interactive "sWord search: ")
-  (re-search-forward (word-search-regexp string nil) bound noerror count))
+  (re-search-forward (word-search-regexp string nil nil) bound noerror count))
 
 (defun word-search-backward-lax (string &optional bound noerror count)
   "Search backward from point for STRING, ignoring differences in punctuation.
@@ -1599,7 +1703,7 @@ (defun word-search-backward-lax (string
 of words in STRING to a regexp used to search words without regard
 to punctuation."
   (interactive "sWord search backward: ")
-  (re-search-backward (word-search-regexp string t) bound noerror count))
+  (re-search-backward (word-search-regexp string t t) bound noerror count))
 
 (defun word-search-forward-lax (string &optional bound noerror count)
   "Search forward from point for STRING, ignoring differences in punctuation.
@@ -1618,15 +1722,22 @@ (defun word-search-forward-lax (string &
 of words in STRING to a regexp used to search words without regard
 to punctuation."
   (interactive "sWord search: ")
-  (re-search-forward (word-search-regexp string t) bound noerror count))
+  (re-search-forward (word-search-regexp string t nil) bound noerror count))
 
 ;; Symbol search
 
-(defun isearch-symbol-regexp (string &optional lax)
+(defun isearch-symbol-regexp (string &optional lax backward)
   "Return a regexp which matches STRING as a symbol.
 Creates a regexp where STRING is surrounded by symbol delimiters \\_< and \\_>.
 If LAX is non-nil, the end of the string need not match a symbol boundary."
-  (concat "\\_<" (regexp-quote string) (unless lax "\\_>")))
+  (if (string-match-p "\\`\\W*\\'" string)
+      "\\W*"
+    (concat
+     (if (string-match-p "\\`\\W" string) "\\W+")
+     (if (or (not lax) (not backward)) "\\_<")
+     (mapconcat 'regexp-quote (split-string string "\\W+" t) "\\W+")
+     (if (or (not lax) backward) "\\_>")
+     (if (string-match-p "\\W\\'" string) "\\W+"))))
 

PS: a table that summarizes the returned values after the change:

(word-search-regexp ""     )          "\\W*"
(word-search-regexp " "    )          "\\W*"
(word-search-regexp "w"    )          "\\bw\\b"
(word-search-regexp " w"   )          "\\W+\\bw\\b"
(word-search-regexp "w "   )          "\\bw\\b\\W+"
(word-search-regexp " w "  )          "\\W+\\bw\\b\\W+"
(word-search-regexp "w w"  )          "\\bw\\W+w\\b"
(word-search-regexp " w w" )          "\\W+\\bw\\W+w\\b"
(word-search-regexp "w w " )          "\\bw\\W+w\\b\\W+"
(word-search-regexp " w w ")          "\\W+\\bw\\W+w\\b\\W+"

(word-search-regexp ""      nil t)    "\\W*"
(word-search-regexp " "     nil t)    "\\W*"
(word-search-regexp "w"     nil t)    "\\bw\\b"
(word-search-regexp " w"    nil t)    "\\W+\\bw\\b"
(word-search-regexp "w "    nil t)    "\\bw\\b\\W+"
(word-search-regexp " w "   nil t)    "\\W+\\bw\\b\\W+"
(word-search-regexp "w w"   nil t)    "\\bw\\W+w\\b"
(word-search-regexp " w w"  nil t)    "\\W+\\bw\\W+w\\b"
(word-search-regexp "w w "  nil t)    "\\bw\\W+w\\b\\W+"
(word-search-regexp " w w " nil t)    "\\W+\\bw\\W+w\\b\\W+"

(word-search-regexp ""      t)        "\\W*"
(word-search-regexp " "     t)        "\\W*"
(word-search-regexp "w"     t)        "\\bw"
(word-search-regexp " w"    t)        "\\W+\\bw"
(word-search-regexp "w "    t)        "\\bw\\W+"
(word-search-regexp " w "   t)        "\\W+\\bw\\W+"
(word-search-regexp "w w"   t)        "\\bw\\W+w"
(word-search-regexp " w w"  t)        "\\W+\\bw\\W+w"
(word-search-regexp "w w "  t)        "\\bw\\W+w\\W+"
(word-search-regexp " w w " t)        "\\W+\\bw\\W+w\\W+"

(word-search-regexp ""      t t)      "\\W*"
(word-search-regexp " "     t t)      "\\W*"
(word-search-regexp "w"     t t)      "w\\b"
(word-search-regexp " w"    t t)      "\\W+w\\b"
(word-search-regexp "w "    t t)      "w\\b\\W+"
(word-search-regexp " w "   t t)      "\\W+w\\b\\W+"
(word-search-regexp "w w"   t t)      "w\\W+w\\b"
(word-search-regexp " w w"  t t)      "\\W+w\\W+w\\b"
(word-search-regexp "w w "  t t)      "w\\W+w\\b\\W+"
(word-search-regexp " w w " t t)      "\\W+w\\W+w\\b\\W+"

(isearch-symbol-regexp ""     )       "\\W*"
(isearch-symbol-regexp " "    )       "\\W*"
(isearch-symbol-regexp "w"    )       "\\_<w\\_>"
(isearch-symbol-regexp " w"   )       "\\W+\\_<w\\_>"
(isearch-symbol-regexp "w "   )       "\\_<w\\_>\\W+"
(isearch-symbol-regexp " w "  )       "\\W+\\_<w\\_>\\W+"
(isearch-symbol-regexp "w w"  )       "\\_<w\\W+w\\_>"
(isearch-symbol-regexp " w w" )       "\\W+\\_<w\\W+w\\_>"
(isearch-symbol-regexp "w w " )       "\\_<w\\W+w\\_>\\W+"
(isearch-symbol-regexp " w w ")       "\\W+\\_<w\\W+w\\_>\\W+"

(isearch-symbol-regexp ""      nil t) "\\W*"
(isearch-symbol-regexp " "     nil t) "\\W*"
(isearch-symbol-regexp "w"     nil t) "\\_<w\\_>"
(isearch-symbol-regexp " w"    nil t) "\\W+\\_<w\\_>"
(isearch-symbol-regexp "w "    nil t) "\\_<w\\_>\\W+"
(isearch-symbol-regexp " w "   nil t) "\\W+\\_<w\\_>\\W+"
(isearch-symbol-regexp "w w"   nil t) "\\_<w\\W+w\\_>"
(isearch-symbol-regexp " w w"  nil t) "\\W+\\_<w\\W+w\\_>"
(isearch-symbol-regexp "w w "  nil t) "\\_<w\\W+w\\_>\\W+"
(isearch-symbol-regexp " w w " nil t) "\\W+\\_<w\\W+w\\_>\\W+"

(isearch-symbol-regexp ""      t)     "\\W*"
(isearch-symbol-regexp " "     t)     "\\W*"
(isearch-symbol-regexp "w"     t)     "\\_<w"
(isearch-symbol-regexp " w"    t)     "\\W+\\_<w"
(isearch-symbol-regexp "w "    t)     "\\_<w\\W+"
(isearch-symbol-regexp " w "   t)     "\\W+\\_<w\\W+"
(isearch-symbol-regexp "w w"   t)     "\\_<w\\W+w"
(isearch-symbol-regexp " w w"  t)     "\\W+\\_<w\\W+w"
(isearch-symbol-regexp "w w "  t)     "\\_<w\\W+w\\W+"
(isearch-symbol-regexp " w w " t)     "\\W+\\_<w\\W+w\\W+"

(isearch-symbol-regexp ""      t t)   "\\W*"
(isearch-symbol-regexp " "     t t)   "\\W*"
(isearch-symbol-regexp "w"     t t)   "w\\_>"
(isearch-symbol-regexp " w"    t t)   "\\W+w\\_>"
(isearch-symbol-regexp "w "    t t)   "w\\_>\\W+"
(isearch-symbol-regexp " w "   t t)   "\\W+w\\_>\\W+"
(isearch-symbol-regexp "w w"   t t)   "w\\W+w\\_>"
(isearch-symbol-regexp " w w"  t t)   "\\W+w\\W+w\\_>"
(isearch-symbol-regexp "w w "  t t)   "w\\W+w\\_>\\W+"
(isearch-symbol-regexp " w w " t t)   "\\W+w\\W+w\\_>\\W+"



reply via email to

[Prev in Thread] Current Thread [Next in Thread]