unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* (where can I find a) query-replace-list?
@ 2023-12-22 14:12 Eduardo Ochs
  2023-12-22 15:26 ` Andreas Röhler
  0 siblings, 1 reply; 2+ messages in thread
From: Eduardo Ochs @ 2023-12-22 14:12 UTC (permalink / raw)
  To: help-gnu-emacs

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



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: (where can I find a) query-replace-list?
  2023-12-22 14:12 (where can I find a) query-replace-list? Eduardo Ochs
@ 2023-12-22 15:26 ` Andreas Röhler
  0 siblings, 0 replies; 2+ messages in thread
From: Andreas Röhler @ 2023-12-22 15:26 UTC (permalink / raw)
  To: help-gnu-emacs


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.




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-12-22 15:26 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-12-22 14:12 (where can I find a) query-replace-list? Eduardo Ochs
2023-12-22 15:26 ` Andreas Röhler

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).