GNU bug report logs - #13178
Query-replace fails soon after narrow-to-region

Previous Next

Package: emacs;

Reported by: Teika Kazura <teika <at> lavabit.com>

Date: Fri, 14 Dec 2012 04:06:02 UTC

Severity: normal

Done: Juri Linkov <juri <at> linkov.net>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 13178 in the body.
You can then email your comments to 13178 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#13178; Package emacs. (Fri, 14 Dec 2012 04:06:03 GMT) Full text and rfc822 format available.

Acknowledgement sent to Teika Kazura <teika <at> lavabit.com>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Fri, 14 Dec 2012 04:06:03 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Teika Kazura <teika <at> lavabit.com>
To: bug-gnu-emacs <at> gnu.org
Subject: Query-replace fails soon after narrow-to-region
Date: Fri, 14 Dec 2012 13:03:20 +0900 (JST)
Hi, Emacs developers.

In Emacs 24.1, query-replace soon after narrow-to-region fails. More
precisely, minibuffer says "Replaced 0 occurrences", and the
query-replace finishes even if there're matches. I don't think it's a
recent regresion.

Steps to reproduce:
0. emacs -Q
1. Open a buffer.
2. Set mark, and do narrow-to-region
3. Move to the top of the narrowed buffer.
4. Do query-replace.
5. minibuffer says "Replaced 0 occurrences", and the query-replace
   finishes. If you do query-replace again, then it starts correctly.

It seems that some extra commands between each step hinder to reproduce.
Probably you have to enable narrow-to-region beforehand, and
query-replace history is necessary, so that "M-% <enter>" can invoke
a query-replace.

In fact, it was already reported in bug #8952:
  http://debbugs.gnu.org/cgi/bugreport.cgi?bug=8952
but as a "wishlist", mixed with several other things. So I dare to
submit a separate entry.

I've been bothered by this bug for years, but I didn't know how to
reproduce it. Thank you in advance.

Best regards,
Teika (Teika kazura)





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#13178; Package emacs. (Fri, 27 Feb 2015 08:14:01 GMT) Full text and rfc822 format available.

Message #8 received at 13178 <at> debbugs.gnu.org (full text, mbox):

From: Teika Kazura <teika <at> gmx.com>
To: 13178 <at> debbugs.gnu.org
Subject: Re: Query-replace fails soon after narrow-to-region
Date: Fri, 27 Feb 2015 17:12:30 +0900 (JST)
unarchive 13178

Sorry, it's not a bug, but a specification.

If a region is active, `query-replace' (together with `replace-string') gets narrowed inside there. I didn't know that, and activated a zero-size region in the procedure described above.

If you want to discuss it further, please go to bug #8952:
 http://debbugs.gnu.org/cgi/bugreport.cgi?bug=8952

Thanks, 
Teika (Teika kazura)




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#13178; Package emacs. (Fri, 27 Feb 2015 15:03:01 GMT) Full text and rfc822 format available.

Message #11 received at 13178 <at> debbugs.gnu.org (full text, mbox):

From: Drew Adams <drew.adams <at> oracle.com>
To: Teika Kazura <teika <at> gmx.com>, 13178 <at> debbugs.gnu.org
Subject: RE: bug#13178: Query-replace fails soon after narrow-to-region
Date: Fri, 27 Feb 2015 07:01:52 -0800 (PST)
> Sorry, it's not a bug, but a specification.
> 
> If a region is active, `query-replace' (together with `replace-string') gets
> narrowed inside there.  I didn't know that, and activated a zero-size region
> in the procedure described above.

Just FYI - It might help you recognize this situation if you use library
`mode-line-posn.el'.  It has the following behavior, which addresses the
gotcha you mention:

1. The size of the region is shown in the mode line when the region is active.  
   Option `modelinepos-empty-region-flag' determines whether to also indicate
   an empty active region.  By default it is t, meaning indicate an empty
   active region.

2. Option `modelinepos-style' lets you choose the region info shown when the
   region is active: (a) number of chars, (b) number of chars and lines, or
   (c) anything else you might want.  Choice (b) is the default.

3. The region highlighting in the mode-line is changed to a different face
   when you invoke a command that behaves specially for an active region,
   such as `query-replace'.

The fact that `modelinepos-empty-region-flag' is non-nil by default means
that whenever the region is active but empty you can easily notice this.

#3 means that you can immediately tell when the region is active and you
are currently using a command whose behavior changes when the region is
active.

More info and screenshots here:
http://www.emacswiki.org/emacs/ModeLinePosition

I proposed such behavior for Emacs, but there was no interest in it.
It helps me.  Maybe it will help you too.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#13178; Package emacs. (Fri, 27 Feb 2015 19:52:02 GMT) Full text and rfc822 format available.

Message #14 received at 13178 <at> debbugs.gnu.org (full text, mbox):

From: Artur Malabarba <bruce.connor.am <at> gmail.com>
Cc: Teika Kazura <teika <at> gmx.com>, 13178 <at> debbugs.gnu.org
Subject: Re: bug#13178: Query-replace fails soon after narrow-to-region
Date: Fri, 27 Feb 2015 16:50:57 -0300
[Message part 1 (text/plain, inline)]
Perhaps the "replace" family of commands ought to ignore the region if it's
empty? Or at least notify the user?
[Message part 2 (text/html, inline)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#13178; Package emacs. (Mon, 02 Mar 2015 17:24:02 GMT) Full text and rfc822 format available.

Message #17 received at 13178 <at> debbugs.gnu.org (full text, mbox):

From: Stefan Monnier <monnier <at> IRO.UMontreal.CA>
To: Artur Malabarba <bruce.connor.am <at> gmail.com>
Cc: Teika Kazura <teika <at> gmx.com>, 13178 <at> debbugs.gnu.org
Subject: Re: bug#13178: Query-replace fails soon after narrow-to-region
Date: Mon, 02 Mar 2015 12:23:40 -0500
> Perhaps the "replace" family of commands ought to ignore the region if it's
> empty?

It should obey use-empty-active-region.

> Or at least notify the user?

It definitely shouldn't fail with an "unrelated" error.


        Stefan




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#13178; Package emacs. (Mon, 02 Mar 2015 20:17:01 GMT) Full text and rfc822 format available.

Message #20 received at 13178 <at> debbugs.gnu.org (full text, mbox):

From: Juri Linkov <juri <at> linkov.net>
To: Stefan Monnier <monnier <at> IRO.UMontreal.CA>
Cc: Teika Kazura <teika <at> gmx.com>, 13178 <at> debbugs.gnu.org,
 Artur Malabarba <bruce.connor.am <at> gmail.com>
Subject: Re: bug#13178: Query-replace fails soon after narrow-to-region
Date: Mon, 02 Mar 2015 22:14:17 +0200
>> Perhaps the "replace" family of commands ought to ignore the region if it's
>> empty?
>
> It should obey use-empty-active-region.

This patch allows query-replace commands to obey use-empty-active-region:

diff --git a/lisp/replace.el b/lisp/replace.el
index e0636e0..8b62b54 100644
--- a/lisp/replace.el
+++ b/lisp/replace.el
@@ -312,15 +312,15 @@ (defun query-replace (from-string to-string &optional delimited start end backwa
 		   (if current-prefix-arg
 		       (if (eq current-prefix-arg '-) " backward" " word")
 		     "")
-		   (if (and transient-mark-mode mark-active) " in region" ""))
+		   (if (use-region-p) " in region" ""))
 	   nil)))
      (list (nth 0 common) (nth 1 common) (nth 2 common)
 	   ;; These are done separately here
 	   ;; so that command-history will record these expressions
 	   ;; rather than the values they had this time.
-	   (if (and transient-mark-mode mark-active)
+	   (if (use-region-p)
 	       (region-beginning))
-	   (if (and transient-mark-mode mark-active)
+	   (if (use-region-p)
 	       (region-end))
 	   (nth 3 common))))
   (perform-replace from-string to-string t nil delimited nil nil start end backward))
@@ -392,15 +392,15 @@ (defun query-replace-regexp (regexp to-string &optional delimited start end back
 		       (if (eq current-prefix-arg '-) " backward" " word")
 		     "")
 		   " regexp"
-		   (if (and transient-mark-mode mark-active) " in region" ""))
+		   (if (use-region-p) " in region" ""))
 	   t)))
      (list (nth 0 common) (nth 1 common) (nth 2 common)
 	   ;; These are done separately here
 	   ;; so that command-history will record these expressions
 	   ;; rather than the values they had this time.
-	   (if (and transient-mark-mode mark-active)
+	   (if (use-region-p)
 	       (region-beginning))
-	   (if (and transient-mark-mode mark-active)
+	   (if (use-region-p)
 	       (region-end))
 	   (nth 3 common))))
   (perform-replace regexp to-string t t delimited nil nil start end backward))
@@ -469,9 +469,9 @@ (defun query-replace-regexp-eval (regexp to-expr &optional delimited start end)
        ;; and the user might enter a single token.
        (replace-match-string-symbols to)
        (list from (car to) current-prefix-arg
-	     (if (and transient-mark-mode mark-active)
+	     (if (use-region-p)
 		 (region-beginning))
-	     (if (and transient-mark-mode mark-active)
+	     (if (use-region-p)
 		 (region-end))))))
   (perform-replace regexp (cons 'replace-eval-replacement to-expr)
 		   t 'literal delimited nil nil start end))
@@ -507,9 +507,9 @@ (defun map-query-replace-regexp (regexp to-strings &optional n start end)
      (list from to
 	   (and current-prefix-arg
 		(prefix-numeric-value current-prefix-arg))
-	   (if (and transient-mark-mode mark-active)
+	   (if (use-region-p)
 	       (region-beginning))
-	   (if (and transient-mark-mode mark-active)
+	   (if (use-region-p)
 	       (region-end)))))
   (let (replacements)
     (if (listp to-strings)
@@ -571,12 +571,12 @@ (defun replace-string (from-string to-string &optional delimited start end backw
 		       (if (eq current-prefix-arg '-) " backward" " word")
 		     "")
 		   " string"
-		   (if (and transient-mark-mode mark-active) " in region" ""))
+		   (if (use-region-p) " in region" ""))
 	   nil)))
      (list (nth 0 common) (nth 1 common) (nth 2 common)
-	   (if (and transient-mark-mode mark-active)
+	   (if (use-region-p)
 	       (region-beginning))
-	   (if (and transient-mark-mode mark-active)
+	   (if (use-region-p)
 	       (region-end))
 	   (nth 3 common))))
   (perform-replace from-string to-string nil nil delimited nil nil start end backward))
@@ -645,12 +645,12 @@ (defun replace-regexp (regexp to-string &optional delimited start end backward)
 		       (if (eq current-prefix-arg '-) " backward" " word")
 		     "")
 		   " regexp"
-		   (if (and transient-mark-mode mark-active) " in region" ""))
+		   (if (use-region-p) " in region" ""))
 	   t)))
      (list (nth 0 common) (nth 1 common) (nth 2 common)
-	   (if (and transient-mark-mode mark-active)
+	   (if (use-region-p)
 	       (region-beginning))
-	   (if (and transient-mark-mode mark-active)
+	   (if (use-region-p)
 	       (region-end))
 	   (nth 3 common))))
   (perform-replace regexp to-string nil t delimited nil nil start end backward))
@@ -816,7 +816,7 @@ (defun keep-lines (regexp &optional rstart rend interactive)
 		  (unless (or (bolp) (eobp))
 		    (forward-line 0))
 		  (point-marker)))))
-    (if (and interactive transient-mark-mode mark-active)
+    (if (and interactive (use-region-p))
 	(setq rstart (region-beginning)
 	      rend (progn
 		     (goto-char (region-end))
@@ -885,7 +885,7 @@ (defun flush-lines (regexp &optional rstart rend interactive)
       (progn
 	(goto-char (min rstart rend))
 	(setq rend (copy-marker (max rstart rend))))
-    (if (and interactive transient-mark-mode mark-active)
+    (if (and interactive (use-region-p))
 	(setq rstart (region-beginning)
 	      rend (copy-marker (region-end)))
       (setq rstart (point)
@@ -935,7 +935,7 @@ (defun how-many (regexp &optional rstart rend interactive)
               (setq rend (max rstart rend)))
           (goto-char rstart)
           (setq rend (point-max)))
-      (if (and interactive transient-mark-mode mark-active)
+      (if (and interactive (use-region-p))
 	  (setq rstart (region-beginning)
 		rend (region-end))
 	(setq rstart (point)




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#13178; Package emacs. (Mon, 02 Mar 2015 21:52:01 GMT) Full text and rfc822 format available.

Message #23 received at 13178 <at> debbugs.gnu.org (full text, mbox):

From: Artur Malabarba <bruce.connor.am <at> gmail.com>
To: Juri Linkov <juri <at> linkov.net>
Cc: Teika Kazura <teika <at> gmx.com>, 13178 <at> debbugs.gnu.org,
 Stefan Monnier <monnier <at> iro.umontreal.ca>
Subject: Re: bug#13178: Query-replace fails soon after narrow-to-region
Date: Mon, 2 Mar 2015 18:51:00 -0300
>> It should obey use-empty-active-region.
>
> This patch allows query-replace commands to obey use-empty-active-region:

Looks good to me.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#13178; Package emacs. (Tue, 03 Mar 2015 23:32:01 GMT) Full text and rfc822 format available.

Message #26 received at 13178 <at> debbugs.gnu.org (full text, mbox):

From: Teika Kazura <teika <at> gmx.com>
To: 13178 <at> debbugs.gnu.org
Cc: monnier <at> iro.umontreal.ca, bruce.connor.am <at> gmail.com, juri <at> linkov.net
Subject: Re: bug#13178: Query-replace fails soon after narrow-to-region
Date: Wed, 04 Mar 2015 08:31:03 +0900 (JST)
Docs of `use-empty-active-region' and `use-region-p' use the word "empty", but in fact the predicate is (> (region-end) (region-beginning)). It's confusing. (And also the Emacs Info, "Operating on the Region" section.)

For replace.el, the condition is correct (= what users expect.) Although I don't know other cases, maybe the explanation "empty" is too simplistic? (Sorry that I don't come up with a concise replacement description for that case.)

Thank you very much Emacs developers. And of course to Drew Adams for your kind reply and wide range, long time contributions.





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#13178; Package emacs. (Wed, 04 Mar 2015 01:18:02 GMT) Full text and rfc822 format available.

Message #29 received at 13178 <at> debbugs.gnu.org (full text, mbox):

From: Artur Malabarba <bruce.connor.am <at> gmail.com>
To: Teika Kazura <teika <at> gmx.com>
Cc: 13178 <at> debbugs.gnu.org, Stefan Monnier <monnier <at> iro.umontreal.ca>,
 juri <at> linkov.net
Subject: Re: bug#13178: Query-replace fails soon after narrow-to-region
Date: Wed, 4 Mar 2015 01:16:54 +0000
[Message part 1 (text/plain, inline)]
On Mar 3, 2015 11:31 PM, "Teika Kazura" <teika <at> gmx.com> wrote:
>
> Docs of `use-empty-active-region' and `use-region-p' use the word
"empty", but in fact the predicate is (> (region-end) (region-beginning)).
It's confusing.

What's confusing about that? Isn't that a non empty region?
[Message part 2 (text/html, inline)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#13178; Package emacs. (Thu, 05 Mar 2015 00:29:02 GMT) Full text and rfc822 format available.

Message #32 received at 13178 <at> debbugs.gnu.org (full text, mbox):

From: Teika Kazura <teika <at> gmx.com>
To: 13178 <at> debbugs.gnu.org
Cc: monnier <at> iro.umontreal.ca, bruce.connor.am <at> gmail.com, juri <at> linkov.net
Subject: Re: bug#13178: Query-replace fails soon after narrow-to-region
Date: Thu, 05 Mar 2015 09:16:10 +0900 (JST)
I was wrong. I'm sorry.

> What's confusing about that? Isn't that a non empty region?





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#13178; Package emacs. (Thu, 12 Mar 2015 19:55:02 GMT) Full text and rfc822 format available.

Message #35 received at 13178 <at> debbugs.gnu.org (full text, mbox):

From: Juri Linkov <juri <at> linkov.net>
To: Artur Malabarba <bruce.connor.am <at> gmail.com>
Cc: Teika Kazura <teika <at> gmx.com>, 13178 <at> debbugs.gnu.org,
 Stefan Monnier <monnier <at> iro.umontreal.ca>
Subject: Re: bug#13178: Query-replace fails soon after narrow-to-region
Date: Thu, 12 Mar 2015 21:53:56 +0200
>>> It should obey use-empty-active-region.
>>
>> This patch allows query-replace commands to obey use-empty-active-region:
>
> Looks good to me.

bug#13178 could be handled together with bug#19829 and bug#20070.




Reply sent to Juri Linkov <juri <at> linkov.net>:
You have taken responsibility. (Fri, 13 Nov 2015 23:42:02 GMT) Full text and rfc822 format available.

Notification sent to Teika Kazura <teika <at> lavabit.com>:
bug acknowledged by developer. (Fri, 13 Nov 2015 23:42:02 GMT) Full text and rfc822 format available.

Message #40 received at 13178-done <at> debbugs.gnu.org (full text, mbox):

From: Juri Linkov <juri <at> linkov.net>
To: 13178-done <at> debbugs.gnu.org
Subject: Re: bug#13178: Query-replace fails soon after narrow-to-region
Date: Sat, 14 Nov 2015 01:39:54 +0200
>>>> It should obey use-empty-active-region.
>>>
>>> This patch allows query-replace commands to obey use-empty-active-region:
>>
>> Looks good to me.
>
> bug#13178 could be handled together with bug#19829 and bug#20070.

This is fixed by bug#19829.




bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Sat, 12 Dec 2015 12:24:03 GMT) Full text and rfc822 format available.

This bug report was last modified 9 years and 194 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.