Subject: bug#25482: 26.0.50; Allow setting `query-replace-from-to-separator` to nil

From: Juri Linkov <[email-masked]>
Date: Thu, 16 Feb 2017 22:43:08 +0200

>> As for your idea of using plain ASCII separators without text properties,
>> the problem is that it will not work if the user wants to replace
>> the same string as is used for the separator, e.g. to replace
>> → with  -> in the current buffer.
>
> So yes, this is a good reason.

Wait, maybe you are right in the idea to use the separator string as is,
instead of the null character.  We still need to use text properties
to unambiguously mark separator boundaries, so we can't rely on
regexp matching to find separator boundaries.  But at least “ → ”
is not more frequent string to replace than the null character.

Putting special text properties on the separator itself instead of
the null character might be more beneficial for such situations
where the separator is exposed without text properties, e.g. when
copying the contents of the minibuffer.  However, one drawback is
that searching in the replacement minibuffer history for the same
string as the separator (e.g. “→”) will find matches in the separator,
not only in replacement strings.

This patch demonstrates what we could do.  It still uses the ‘display’
property because I see no other simple way to make the separator string
intangible.

diff --git a/lisp/replace.el b/lisp/replace.el
index b96c883..6600ff6 100644
--- a/lisp/replace.el
+++ b/lisp/replace.el
@@ -149,14 +149,17 @@ query-replace-descr
   (mapconcat 'isearch-text-char-description string ""))
 
 (defun query-replace--split-string (string)
-  "Split string STRING at a character with property `separator'"
+  "Split string STRING at a substring with property `separator'"
   (let* ((length (length string))
          (split-pos (text-property-any 0 length 'separator t string)))
     (if (not split-pos)
         (substring-no-properties string)
-      (cl-assert (not (text-property-any (1+ split-pos) length 'separator t string)))
       (cons (substring-no-properties string 0 split-pos)
-            (substring-no-properties string (1+ split-pos) length)))))
+            (substring-no-properties
+             string (or (text-property-not-all
+                         (1+ split-pos) length 'separator t string)
+                        length)
+             length)))))
 
 (defun query-replace-read-from (prompt regexp-flag)
   "Query and return the `from' argument of a query-replace operation.
@@ -165,17 +168,19 @@ query-replace-read-from
   (if query-replace-interactive
       (car (if regexp-flag regexp-search-ring search-ring))
     (let* ((history-add-new-input nil)
+           (separator-string
+            (when query-replace-from-to-separator
+              ;; Check if the first non-whitespace char is displayable
+	      (if (char-displayable-p
+                   (string-to-char (replace-regexp-in-string
+                                    " " "" query-replace-from-to-separator)))
+                  query-replace-from-to-separator
+                " -> ")))
 	   (separator
-	    (when query-replace-from-to-separator
-	      (propertize "\0"
-			  'display
-                          (propertize
-                           (if (char-displayable-p
-                                (string-to-char (replace-regexp-in-string
-                                                 " " "" query-replace-from-to-separator)))
-                               query-replace-from-to-separator
-                             " -> ")
-                           'face 'minibuffer-prompt)
+	    (when separator-string
+	      (propertize separator-string
+                         'display separator-string
+                         'face 'minibuffer-prompt
 			  'separator t)))
 	   (minibuffer-history
 	    (append
@@ -203,7 +210,8 @@ query-replace-read-from
               (minibuffer-with-setup-hook
                   (lambda ()
                     (setq-local text-property-default-nonsticky
-                                (cons '(separator . t) text-property-default-nonsticky)))
+                                (append '((separator . t) (face . t))
+                                        text-property-default-nonsticky)))
                 (if regexp-flag
                     (read-regexp prompt nil 'minibuffer-history)
                   (read-from-minibuffer