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

From: Juri Linkov <[email-masked]>
Date: Sun, 19 Dec 2021 20:38:10 +0200

>> Such option already exists, and it allows using custom regexp syntax
>> both for isearch and query-replace:
>
> Interesting, though its help mentions only isearch, so if I start
> M-x query-replace-regexp directly, it doesn't seem affected.

If you want to use M-x query-replace directly,
then simpler would be add replace-regexp-function
that you can use with:

(setq replace-regexp-function
      (lambda (s &optional _lax)
	(let ((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)))

diff --git a/lisp/replace.el b/lisp/replace.el
index 0e81b15a09..84c2d523d7 100644
--- a/lisp/replace.el
+++ b/lisp/replace.el
@@ -2621,6 +2621,8 @@ replace-re-search-function
 It is called with three arguments, as if it were
 `re-search-forward'.")
 
+(defvar replace-regexp-function nil)
+
 (defun replace-search (search-string limit regexp-flag delimited-flag
 		       case-fold &optional backward)
   "Search for the next occurrence of SEARCH-STRING to replace."
@@ -2633,7 +2635,8 @@ replace-search
   ;; outside of this function because then another I-search
   ;; used after `recursive-edit' might override them.
   (let* ((isearch-regexp regexp-flag)
-	 (isearch-regexp-function (or delimited-flag
+	 (isearch-regexp-function (or replace-regexp-function
+                                      delimited-flag
 				      (and replace-char-fold
 					   (not regexp-flag)
 					   #'char-fold-to-regexp)))
--