From: "Andreas Röhler" <andreas.roehler@easy-emacs.de>
To: help-gnu-emacs@gnu.org
Subject: Re: (where can I find a) query-replace-list?
Date: Fri, 22 Dec 2023 16:26:52 +0100 [thread overview]
Message-ID: <a3a632b0-ffe4-4421-bedf-f70a868ce59a@easy-emacs.de> (raw)
In-Reply-To: <CADs++6jEmmLf2RQDffeLeoCFi-cECP6Cp9mY+Pd_1AU-BnbiFQ@mail.gmail.com>
Am 22.12.23 um 15:12 schrieb Eduardo Ochs:
> Hi list,
>
> sometimes I need to quote special characters in a string in a special
> way, that includes that each "{" should become a "{<}" and each "}"
> should become a "{>}", and I prefer to do that interactively using
> some variant of query-replace or query-replace-regexp... I had a hack
> to do that but it was ugly and fragile - it query-replaced all
> "{"->"{<}"s before "}"->"{>}"s, so I had to be very careful with my
> "yes"es and "no"s - so I decided to rewrite it...
>
> I tried to implement something that could be called like this,
>
> (query-replace-list
> "\"" "\\\""
> "\\" "\\\\"
> "{" "{<}"
> "}" "{>}")
>
> and I sort of got it - but I ended up using mapconcat, regexp-quote, a
> function to delete text-properties, a plist-get, and then a call like
> this,
>
> (query-replace-regexp
> "\"\\|\\\\\\|{\\|}"
> (list 'my-replacer-function nil))
>
> in which my-replacer-function uses (match-string 0) to obtain its
> input string.
>
> That's so clumsy that I _guess_ that there should exist a standard
> function, or at least a package in one of the *ELPAs, that implements
> something like this query-replace-list - but I couldn't find it...
>
> Any hints? Any pointers?
>
> Here is my prototype, for the sake of completeness:
>
> --snip--snip--
> (defun ee-no-properties (str)
> (setq str (copy-sequence str))
> (set-text-properties 0 (length str) nil str)
> str)
>
> (defvar ee-qrl-plist
> '("\"" "\\\"" "\\" "\\\\"
> "{" "{<}" "}" "{>}"))
>
> (defun ee-qrl-as () (cl-loop for (a b) on ee-qrl-plist by 'cddr collect a))
> (defun ee-qrl-regexp () (mapconcat 'regexp-quote (ee-qrl-as) "\\|"))
>
> (defun ee-qrl-r0 (s) (plist-get ee-qrl-plist (ee-no-properties s) 'equal))
> (defun ee-qrl-r1 (s) (replace-regexp-in-string "\\\\" "\\\\\\\\" s))
> (defun ee-qrl-r2 (s) (ee-qrl-r1 (ee-qrl-r0 s)))
> (defun ee-qrl-r3 (a b) (ee-qrl-r2 (match-string 0)))
> (defun ee-qrl ()
> (interactive)
> (query-replace-regexp (ee-qrl-regexp) (list 'ee-qrl-r3 nil)))
> --snip--snip--
>
> Thanks in advance,
> Eduardo Ochs
> http://anggtwu.net/eepitch.html
>
Would consider a pcase, each match followed by a cond, listing all the
clauses. Then a simple replace-match.
prev parent reply other threads:[~2023-12-22 15:26 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-12-22 14:12 (where can I find a) query-replace-list? Eduardo Ochs
2023-12-22 15:26 ` Andreas Röhler [this message]
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=a3a632b0-ffe4-4421-bedf-f70a868ce59a@easy-emacs.de \
--to=andreas.roehler@easy-emacs.de \
--cc=help-gnu-emacs@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.