[Top][All Lists]

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

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

From: Juri Linkov
Subject: bug#25482: 26.0.50; Allow setting `query-replace-from-to-separator` to nil
Date: Thu, 16 Feb 2017 22:43:08 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.50 (x86_64-pc-linux-gnu)

>> 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

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 
       (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
+                " -> ")))
-           (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
-                             " -> ")
-                           'face 'minibuffer-prompt)
+           (when separator-string
+             (propertize separator-string
+                         'display separator-string
+                         'face 'minibuffer-prompt
                          'separator t)))
@@ -203,7 +210,8 @@ query-replace-read-from
                   (lambda ()
                     (setq-local text-property-default-nonsticky
-                                (cons '(separator . t) 
+                                (append '((separator . t) (face . t))
+                                        text-property-default-nonsticky)))
                 (if regexp-flag
                     (read-regexp prompt nil 'minibuffer-history)

reply via email to

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