help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: (where can I find a) query-replace-list?


From: Andreas Röhler
Subject: Re: (where can I find a) query-replace-list?
Date: Fri, 22 Dec 2023 16:26:52 +0100
User-agent: Mozilla Thunderbird


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.




reply via email to

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