help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: I can't yank regexp for/into isearch-forward-regexp's pmpt


From: Kevin Rodgers
Subject: Re: I can't yank regexp for/into isearch-forward-regexp's pmpt
Date: Thu, 03 Aug 2006 08:18:07 -0600
User-agent: Thunderbird 1.5.0.5 (Windows/20060719)

David Combs wrote:
Doing isearch-forward-regexp against the line (note the "x"):

     "aaa  aaaa   aaaaaaaa   aaaaa   aaaaax    aa"

  , via the regexp "x", works fine -- and does so REGARDLESS
  of whether the regexp is:

    (a) typed-in the "x" by hand or
    (b) *META*-YANKED-in (ie via "M-y" -- not a plain "y").

However, when yanked-in, none of *these* (for trying to match
FIVE "a's") will match that *same* "aaaaaa aaa..." string (at least not for me!):

---- candidate regexps to try with C-y and/or M-y:

1: "a{5}"
2: "a\{5\}"      <<--- works fine IF prompt answered TYPED-IN BY HAND.
3: "a\\{5\\}"
4: "a\\\{5\\\}"

Remember, the regexp itself only uses a single backslash to introduce
special characters like the left and right braces.  It is only when
the regexp needs to be represented as a Lisp string literal (delimited
by double quotes) that the backslash needs to be doubled.  That's why
regexp #2 works (without the double quotes, of course) and the others
don't when entered directly in minibuffer.

But *none* of the four work (for me) via yank, ie regionize
the regexp, grab it via M-w, put POINT somewhere before that
"aaaaa"-string, and then, as above, do the M-C-s (ie isearch-forward-regexp), and respond to its prompt
with M-y.

(Well, it does match, but not that "aaaaaa..."-line,
but rather the very one of the four trial-regexps
I had just grabbed -- as if it were a straight
*non*-regexp search!)


[ But note: the grab-regexp then yank back as prompt-response works
JUST FINE, not with issearch-whatever, but with M-x OCCUR, and for it's prompt, all it needs is just a PLAIN "C-y"! ]

Please, guys, what's up?


All I want to do is to be able to save into a file some regexps I find useful,
and then be able to use them merely by grabbing one via M-w, and then
answer the prompt by merely yanking or even meta-yanking it into or as
the prompt-response.


Please, what am I doing wrong?

You're not doing anything wrong.  But Emacs assumes that the text you
are yanking is literal text, not a regexp, even when you are doing a
regexp search.  In isearch-mode-map, M-y is bound to isearch-yank-kill,
which calls isearch-yank-string, which explicitly turns the yanked text
into a regexp (when doing a regexp search) so that you end up searching
for the regexp itself in the buffer (vs. the text that matches the
regexp).

You might be able to do what you want with a little hacking:

(defun isearch-yank-kill-as-regexp ()
  "Like `isearch-yank-kill', except during regular expression search:
Search for the text that matches the string (as a regexp) instead of
searching for the literal string itself."
  (let ((isearch-regexp nil))
    (isearch-yank-kill)))

(define-key isearch-mode-map "\M-z" 'isearch-yank-kill-as-regexp)

Is there any good way to *explain* it in some tutorial-way that I (or any
user) a rule or technique for doing this with *any* regexp-prompting-for
command, eg that will work equally for *both*:

    M-x occur

    isearch-forward-regexp

    isearch-forward

isearch-forward does not prompt for a regexp, unless you toggle it
with M-r.

    M-x query-replace-regexp

    etc, etc

M-x occur and M-x query-replace-regexp prompt for and read the regexp in
the minibuffer, like any other normal command argument.  But isearch
does not read an argument: you don't have to terminate the regexp with
RET.  Instead, isearch is implemented as a special minor mode, with its
own keymap, to implement the search incrementally -- have you noticed
that typing a single SPC in regexp isearch matches any number of
consecutive whitespace characters?

 AND also, what rules for back-slashing a regexp as needed
     for *any* of the above.

I think I answered that above.

--
Kevin





reply via email to

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