Subject: Re: bug#52558: Option for easier typing of regexps

From: ndame <[email-masked]>
Date: Sat, 18 Dec 2021 16:47:47 +0000

> You can probably implement what you want by overriding
> `read-regexp'. In principle I'd expect anything prompting
> for a regexp to be using this.

That's what I thought too at first, but turns out it's not
the right way, because then history contains the normalized
regexp, so if the user goes back in history then he doesn't
see the easy version, though the goal is that the user works
with the easy variant.

So read-regexp is not the right place, because the result of
that goes into history. The translation has to be done only
before the regexp is used.

In case of query-replace-regexp it can be done before
perform-replace. Here's a solution with advice:

(defun my-perform-replace (origfun &rest args)
  (apply
   origfun

   (if (fourth args) ;; do conversion only for regexp replace
       (cons (let ((s (car args))
                   (chars '("|" "(" ")"))
                   (placeholder (format "@placeholder%s@"
                                        (int-to-string
                                         (buffer-modified-tick)))))
               (dolist (char chars)
                 (setq s (replace-regexp-in-string
                          placeholder char
                          (replace-regexp-in-string
                           char (concat "\\\\" char)
                           (replace-regexp-in-string
                            (concat "\\\\" char) placeholder
                            s)))))
               s)

             (cdr args))

     args)))

(advice-add 'perform-replace :around 'my-perform-replace)