[Top][All Lists]

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

Re: regexp repacement, how to present replacement to user?

From: Juri Linkov
Subject: Re: regexp repacement, how to present replacement to user?
Date: Wed, 17 Oct 2007 02:54:14 +0300
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/23.0.50 (gnu/linux)

>>      (while (re-search-forward from nil t)
>>         (if y-or-n-p (format "Replace %s with %s "
>>                        (match-string 0)
>>                           (save-match-data
>>                             (replace-match to nil nil (match-string 0)
>>                              (prog1 nil
>>                                (set-match-data
>>                                  (mapcar
>>                                    (lambda(x)(if (numberp x)
>>                                                    (- x (match-beginning 0))
>>                                                    x)))
>>                                  (match-data t)))
> Thanks, but shouldn't this be a function somewhere?  I mean, it's
> not like random peope are going to easily write such code.

`query-replace-regexp' already highlights the text matched by
`from-regexp' in the source buffer, so the user can see the text to
replace.  And the result of the replacement is also displayed in the
prompt in the expanded form, so the user sees the resulting string
before replacing it in the buffer.

The only thing I don't understand why the result of evaluating \,
expressions is displayed in the prompt, but the result of back references
is not.  I mean why, for instance,

C-M-% \(a\|b\) RET \,\1  \1 RET


Query replacing \(a\|b\) with a \1:

but not

Query replacing \(a\|b\) with a a:


I think replacing \1 with the value of the back reference in the prompt
will be more useful for the user to see the result of the future replacement.

> Besides, what if Emacs regexps are slightly improved with more features,
> like named groups or whatnot?

Recently Stefan improved Emacs regexps with numbered groups where a number
can be explicitly assigned to a regexp group.

Juri Linkov

reply via email to

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