Subject: Re: bug#54804: 29.0.50; zap-to-char: case sensitive for upper-case letter

From: Tino Calancha <[email-masked]>
Date: Thu, 12 May 2022 17:55:38 +0200

Tino Calancha <[email-masked]> writes:

> I have removed completely that sentence and pushed
> to the master branch.

The Empire Strikes Back

--8<-----------------------------cut here---------------start------------->8---
commit 86750ab021ea889766a717458ed28a54e6a40ad3
Author: Tino Calancha <[email-masked]>
Date:   Thu May 12 17:48:09 2022 +0200

    zap-to-char: case sensitive for upper-case characters
    
    In interactive calls, behave case-sensitively if the given char
    is an upper-case character.  Same for zap-up-to-char.
    
    This is analog to what the user-level incremental search feature does.
    
    * lisp/misc.el (zap-up-to-char): Add an optional arg INTERACTIVE.
    Perform a case-sensitive search when INTERACTIVE is non-nil and
    CHAR is an upper-case character.
    * lisp/simple.el (zap-to-char): Same.
    
    * etc/NEWS (Editing Changes in Emacs 29.1): Announce this change.
    * test/lisp/misc-tests.el ((misc-test-zap-up-to-char): Add test cases.
    * test/lisp/simple-tests.el (with-simple-test): Add helper macro.
    (simple-tests-zap-to-char): Add a test.

diff --git a/etc/NEWS b/etc/NEWS
index 3bdc497f18..d6493e77e5 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -672,6 +672,12 @@ recreate it anew next time 'imenu' is invoked.
 
 * Editing Changes in Emacs 29.1
 
+---
+** 'zap-to-char' case sensitivity on interactive calls.
+The command now behaves as case-sensitive for interactive calls when is
+invoked with an uppercase argument, regardless of the
+`case-fold-search' value.  Likewise, for 'zap-up-to-char'.
+
 ---
 ** 'scroll-other-window' and 'scroll-other-window-down' now respects remapping.
 These commands (bound to 'C-M-v' and 'C-M-V') used to scroll the other
diff --git a/lisp/misc.el b/lisp/misc.el
index d85f889ffd..f92debf013 100644
--- a/lisp/misc.el
+++ b/lisp/misc.el
@@ -64,15 +64,21 @@ copy-from-above-command
 ;; Variation of `zap-to-char'.
 
 ;;;###autoload
-(defun zap-up-to-char (arg char)
+(defun zap-up-to-char (arg char &optional interactive)
   "Kill up to, but not including ARGth occurrence of CHAR.
 Case is ignored if `case-fold-search' is non-nil in the current buffer.
 Goes backward if ARG is negative; error if CHAR not found.
-Ignores CHAR at point."
+Ignores CHAR at point.
+Called interactively, do a case sensitive search if CHAR
+is an upper-case character."
   (interactive (list (prefix-numeric-value current-prefix-arg)
 		     (read-char-from-minibuffer "Zap up to char: "
-						nil 'read-char-history)))
-  (let ((direction (if (>= arg 0) 1 -1)))
+						nil 'read-char-history)
+                     t))
+  (let ((direction (if (>= arg 0) 1 -1))
+        (case-fold-search (if (and interactive (char-uppercase-p char))
+                              nil
+                            case-fold-search)))
     (kill-region (point)
 		 (progn
 		   (forward-char direction)
diff --git a/lisp/simple.el b/lisp/simple.el
index 3812f6d8c6..7d6c55c190 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -6062,21 +6062,25 @@ char-uppercase-p
          (characterp (get-char-code-property char 'lowercase)))
         ((and (>= char ?A) (<= char ?Z)))))
 
-(defun zap-to-char (arg char)
+(defun zap-to-char (arg char &optional interactive)
   "Kill up to and including ARGth occurrence of CHAR.
 Case is ignored if `case-fold-search' is non-nil in the current buffer.
 Goes backward if ARG is negative; error if CHAR not found.
-See also `zap-up-to-char'."
+See also `zap-up-to-char'.
+Called interactively, do a case sensitive search if CHAR
+is an upper-case character."
   (interactive (list (prefix-numeric-value current-prefix-arg)
 		     (read-char-from-minibuffer "Zap to char: "
-						nil 'read-char-history)))
+						nil 'read-char-history))
+               t)
   ;; Avoid "obsolete" warnings for translation-table-for-input.
   (with-no-warnings
     (if (char-table-p translation-table-for-input)
 	(setq char (or (aref translation-table-for-input char) char))))
-  (kill-region (point) (progn
-			 (search-forward (char-to-string char) nil nil arg)
-			 (point))))
+  (let ((case-fold-search (if (and interactive (char-uppercase-p char))
+                              nil
+                            case-fold-search)))
+    (kill-region (point) (search-forward (char-to-string char) nil nil arg))))
 
 ;; kill-line and its subroutines.
 
diff --git a/test/lisp/misc-tests.el b/test/lisp/misc-tests.el
index 36a8726b88..236223ef49 100644
--- a/test/lisp/misc-tests.el
+++ b/test/lisp/misc-tests.el
@@ -44,7 +44,14 @@ misc-test-zap-up-to-char
     (zap-up-to-char 1 ?c))
   (with-misc-test "abcde abc123" "c123"
     (goto-char (point-min))
-    (zap-up-to-char 2 ?c)))
+    (zap-up-to-char 2 ?c))
+  (let ((case-fold-search t))
+    (with-misc-test "abcdeCXYZ" "cdeCXYZ"
+      (goto-char (point-min))
+      (zap-up-to-char 1 ?C))
+    (with-misc-test "abcdeCXYZ" "CXYZ"
+      (goto-char (point-min))
+      (zap-up-to-char 1 ?C 'interactive))))
 
 (ert-deftest misc-test-upcase-char ()
   (with-misc-test "abcde" "aBCDe"
diff --git a/test/lisp/simple-tests.el b/test/lisp/simple-tests.el
index dcab811bb5..2baf7124d7 100644
--- a/test/lisp/simple-tests.el
+++ b/test/lisp/simple-tests.el
@@ -971,5 +971,27 @@ test-undo-region
     ;;(should (= (length (delq nil (undo-make-selective-list 5 9))) 0))
     (should (= (length (delq nil (undo-make-selective-list 6 9))) 0))))
 
+
+;;; Tests for `zap-to-char'
+
+(defmacro with-simple-test (original result &rest body)
+  (declare (indent 2) (debug (stringp stringp body)))
+  `(with-temp-buffer
+     (insert ,original)
+     (goto-char (point-min))
+     ,@body
+     (should (equal (buffer-string) ,result))))
+
+(ert-deftest simple-tests-zap-to-char ()
+  (with-simple-test "abcde" "de"
+    (zap-to-char 1 ?c))
+  (with-simple-test "abcde abc123" "123"
+    (zap-to-char 2 ?c))
+  (let ((case-fold-search t))
+    (with-simple-test "abcdeCXYZ" "deCXYZ"
+      (zap-to-char 1 ?C))
+    (with-simple-test "abcdeCXYZ" "XYZ"
+      (zap-to-char 1 ?C 'interactive))))
+
 (provide 'simple-test)
 ;;; simple-tests.el ends here

--8<-----------------------------cut here---------------end--------------->8---