[Top][All Lists]

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

Re: query-replace-interactive not documented

From: Juri Linkov
Subject: Re: query-replace-interactive not documented
Date: Tue, 15 Jun 2004 17:29:07 +0300
User-agent: Gnus/5.110002 (No Gnus v0.2) Emacs/21.3.50 (gnu/linux)

David Kastrup <address@hidden> writes:
> address@hidden (Kim F. Storm) writes:
>> Richard Stallman <address@hidden> writes:
>> > That seems like an interesting idea.
>> I would prefer a syntax which can be parsed with `read', which
>> includes the second example above, but not the first one.
> Well, this was of course just a wild proposal without any serious
> thought behind the involved details.  One should probably take a look
> at eshell syntax maybe for getting some suggestions for a somewhat
> consistent syntax, and one would need to think about a way that makes
> it possible to end the expression in question without adding
> unnecessary spaces or so to the match.

I like this idea.  To avoid problems with `read' and to end the expression
explicitly I suggest to always place the expression in parenthesis.
To use a single variable inside a replacement string, one can use
a no-op function like `+':

    \\footnote{\\label{fn:\!(+ replace-count)}

> And of course, this sort of stuff should not be done by replace-match
> or similar functions but just by the interactive function, to avoid
> opening security concerns when Emacs is used as a match-and-replace
> engine (where people don't expect it to execute arbitrary code).

Doing it only in the interactive function instead of `replace-match'
seems right.  This is needed only to save keystrokes for users.
Since `replace-match' is not a command, when the programmer writes
a loop with `re-search-forward' and `replace-match', he can construct
a replacement string explicitly with appropriate functions.

Anyway, below is a quick implementation of such a feature.  I tried it
a bit and I like it.  What I don't like yet is the choice of a special
meta symbol `\!'.  Usually this symbol is associated with the error code.
Perhaps, a better symbol would be \( itself like in:

    \\footnote{\\label{fn:\(+ replace-count)}

but this has another problem: the open parenthesis is escaped
and Emacs commands don't match it with the close parenthesis.
So the choice of a good symbol is still an open issue.

Index: replace.el
RCS file: /cvsroot/emacs/emacs/lisp/replace.el,v
retrieving revision 1.172
diff -c -r1.172 replace.el
*** replace.el  10 Jun 2004 04:21:14 -0000      1.172
--- replace.el  15 Jun 2004 14:56:15 -0000
*** 174,180 ****
           (if (and transient-mark-mode mark-active)
!   (perform-replace regexp to-string t t delimited nil nil start end))
  (define-key esc-map [?\C-%] 'query-replace-regexp)
  (defun query-replace-regexp-eval (regexp to-expr &optional delimited start 
--- 183,200 ----
           (if (and transient-mark-mode mark-active)
!   (if (string-match "!\(" to-string)
!       (let (res)
!         (while (string-match "!\(" to-string)
!           (setq res (cons (substring to-string 0 (1- (match-beginning 0))) 
!                 to-string (substring to-string (1- (match-end 0))))
!           (let ((r (read-from-string to-string)))
!             (setq res (cons `(format "%s" ,(car r)) res)
!                   to-string (substring to-string (cdr r)))))
!         (perform-replace regexp `(replace-eval-replacement
!                                   concat ,@(nreverse (cons to-string res)))
!                          t 'literal delimited nil nil start end))
!     (perform-replace regexp to-string t t delimited nil nil start end)))
  (define-key esc-map [?\C-%] 'query-replace-regexp)
  (defun query-replace-regexp-eval (regexp to-expr &optional delimited start 

Juri Linkov

reply via email to

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