all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Drew Adams" <drew.adams@oracle.com>
To: "'Drew Adams'" <drew.adams@oracle.com>, <emacs-devel@gnu.org>
Subject: RE: general perform-replace REPLACEMENTS arg for regexpquery-replacement?
Date: Sat, 15 Nov 2008 14:31:30 -0800	[thread overview]
Message-ID: <005b01c94771$e8129be0$0200a8c0@us.oracle.com> (raw)
In-Reply-To: <005901c94752$06ca6e70$0200a8c0@us.oracle.com>

>  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	other threads:[~2008-11-15 22:31 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-11-15 18:43 general perform-replace REPLACEMENTS arg for regexp query-replacement? Drew Adams
2008-11-15 22:31 ` Drew Adams [this message]
2008-11-16 22:34   ` general perform-replace REPLACEMENTS arg for regexpquery-replacement? Juri Linkov
2008-11-17  1:25     ` Drew Adams
2008-11-17 22:51       ` Juri Linkov
2008-11-18  1:01         ` Drew Adams
2008-11-18 22:18           ` Juri Linkov
2008-11-18 23:20             ` Drew Adams

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='005b01c94771$e8129be0$0200a8c0@us.oracle.com' \
    --to=drew.adams@oracle.com \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.