emacs-devel
[Top][All Lists]
Advanced

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

RE: general perform-replace REPLACEMENTS arg for regexpquery-replacement


From: Drew Adams
Subject: RE: general perform-replace REPLACEMENTS arg for regexpquery-replacement?
Date: Sat, 15 Nov 2008 14:31:30 -0800

>  M-: (query-replace-regexp "\\$\\$" '("\\\\[" "\\\\]"))
> 
> But AFAICT, you cannot use `query-replace-regexp' 
> interactively to do this. You
> cannot, for instance, do this to get the same effect:
> 
>  C-M-% RET \$\$ RET \, '("\\\\[" "\\\\]")
> 
> That just replaces "$$" by the string "(\\[ \\])".

What's needed, I think, is some new char sequnce, e.g. `\@', that signifies that
what follows it should be taken as a list of replacement strings, not converted
to a single parenthesized replacement string as `\,' does.

Whether such a construct should be available at all levels recursively (as is
the case for `\,') or just at the top level, I don't know. But I suspect only
top level makes sense, given what `perform-replace' expects. 

Whether such a construct should be robust enough to treat not just a literal
list of regexp strings but an arbitrary sexp that evals to such a list, I don't
know.

Here is a naive definition that lets `C-M-%' work for a literal list of strings
at top level, such as in the example above (with `\,' replaced by `\@'):

 C-M-% RET \$\$ RET \@("\\\\[" "\\\\]")

The only change to the existing code is to add the outermost `if' and the first
`if' clause. The rest is unchanged.

(defun query-replace-compile-replacement (to regexp-flag)
  "Maybe convert a regexp replacement TO to Lisp.
Returns a list suitable for `perform-replace' if necessary,
the original string if not."
  (if (and regexp-flag
           (string-match
            "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\\\\
@\\((\"[^\"].*\"\\s-*\"[^\"].*\")\\)" to))
      (let ((lst (car (read-from-string
                       (substring
                        to (match-beginning 3) (match-end 3))))))
        (mapcar
         (lambda (x)
           (query-replace-compile-replacement x regexp-flag))
         lst))
    (if (and regexp-flag
             (string-match
              "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\\\[,#]" to))
        (let (pos list char)
          (while
              (progn
                (setq pos (match-end 0))
                (push (substring to 0 (- pos 2)) list)
                (setq char (aref to (1- pos))
                      to (substring to pos))
                (cond
                  ((eq char ?\#)
                   (push '(number-to-string replace-count) list))
                  ((eq char ?\,)
                   (setq pos (read-from-string to))
                   (push `(replace-quote ,(car pos)) list)
                   (let ((end
                          ;; Swallow a space after a symbol
                          ;; if there is a space.
                          (if (and
                               (or (symbolp (car pos))
                                   ;; Swallow a space after 'foo
                                   ;; but not after (quote foo).
                                   (and (eq (car-safe (car pos))
                                            'quote)
                                        (not (= ?\(
                                                (aref to 0)))))
                               (eq (string-match " " to (cdr pos))
                                   (cdr pos)))
                              (1+ (cdr pos))
                            (cdr pos))))
                     (setq to (substring to end)))))
                (string-match
                 "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\\\[,#]" to)))
          (setq to (nreverse (delete "" (cons to list))))
          (replace-match-string-symbols to)
          (cons 'replace-eval-replacement
                (if (cdr to)
                    (cons 'concat to)
                  (car to)))))
    to))

Perhaps this is good enough as is? Or perhaps someone more familiar with the
existing code can improve it. Some possible improvements were mentioned above.

If people try this and think it is OK as is, I can submit a patch.





reply via email to

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