Subject: 29.0.50; zap-to-char: case sensitive for upper-case letter

From: Tino Calancha <[email-masked]>
Date: Sat, 09 Apr 2022 00:03:45 +0200

X-Debbugs-Cc: [email-masked]
Severity: wishlist
Tags: patch

A wishlist for the `zap-to-char' and `zap-up-to-char':
In interactive calls, when users input an upper-case letter then perform
a case-sensitive search.

This behavior is analog to what `isearch' does.

--8<-----------------------------cut here---------------start------------->8---
commit c55557d6eb83ac1fe3aa678cae080df979922f7c
Author: Tino Calancha <[email-masked]>
Date:   Fri Apr 8 23:00:18 2022 +0200

    zap-to-char: case sensitive for upper-case letter
    
    In interactive calls, perform a case sensitive search when the given
    char is an upper-case letter.  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): Delete it.
    * lisp/simple.el (zap--to-char-region): Add helper function.
    (zap-to-char, zap-up-to-char): Add the optional argument INTERACTIVE.
    Do a case-sensitive search when CHAR is an upper-case letter and
    INTERACTIVE is non-nil.
    
    * etc/NEWS (Editing Changes in Emacs 29.1): Announce this change.
    * test/lisp/misc-tests.el (misc-test-zap-up-to-char): Delete it.
    * test/lisp/simple-tests.el (with-simple-test): Add helper macro.
    (simple-tests-zap-to-char, simple-tests-zap-up-to-char): Add tests.

diff --git a/etc/NEWS b/etc/NEWS
index 2fac893cc5..5fb9f14366 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -501,6 +501,11 @@ are met.  The conditions are given by the argument, which can be
 
 * Editing Changes in Emacs 29.1
 
+---
+** 'zap-to-char' performs a case-sensitive search in interactive calls
+when the input character is an upper-case letter.  Likewise, for
+'zap-up-to-char'.
+
 ---
 ** Indentation of 'cl-flet' and 'cl-labels' has changed.
 These forms now indent like this:
diff --git a/lisp/misc.el b/lisp/misc.el
index d85f889ffd..389b1a6ef5 100644
--- a/lisp/misc.el
+++ b/lisp/misc.el
@@ -61,25 +61,6 @@ copy-from-above-command
 				 (+ n (point)))))))
     (insert string)))
 
-;; Variation of `zap-to-char'.
-
-;;;###autoload
-(defun zap-up-to-char (arg char)
-  "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."
-  (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)))
-    (kill-region (point)
-		 (progn
-		   (forward-char direction)
-		   (unwind-protect
-		       (search-forward (char-to-string char) nil nil arg)
-		     (backward-char direction))
-		   (point)))))
 
 ;; These were added with an eye to making possible a more CCA-compatible
 ;; command set; but that turned out not to be interesting.
diff --git a/lisp/simple.el b/lisp/simple.el
index eb65701803..50aed97680 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -6031,21 +6031,61 @@ backward-delete-char-untabify
     ;; Avoid warning about delete-backward-char
     (with-no-warnings (delete-backward-char n killp))))
 
-(defun zap-to-char (arg char)
+(defun zap--to-char-region (arg char up-to interactive)
+  "Return the region to kill with `zap-to-char'.
+
+Helper function for `zap-to-char' and `zap-up-to-char'.
+If UP-TO is non-nil then implement the later, otherwise the former.
+The rest of the arguments are described in the commands."
+  (with-no-warnings
+    (when (char-table-p translation-table-for-input)
+      (setq char (or (aref translation-table-for-input char) char))))
+  (let* ((str (char-to-string char))
+         (upper-case (let (case-fold-search)
+                       (string-match-p "[[:upper:]]" str)))
+         (case-fold-search (if (and interactive upper-case)
+                               nil
+                             case-fold-search)))
+    (list (point) (if up-to (let ((direction (if (>= arg 0) 1 -1)))
+		              (forward-char direction)
+		              (unwind-protect
+		                  (search-forward str nil nil arg)
+		                (backward-char direction))
+		              (point))
+		    (search-forward str nil nil arg)
+		    (point)))))
+
+(defun zap-to-char (arg char &optional interactive)
   "Kill up to and including ARGth occurrence of CHAR.
+Optional argument INTERACTIVE tells the function to behave as when
+it's called interactively.
 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.
+Called interactively, do a case sensitive search if the char is an
+upper case letter.
 See also `zap-up-to-char'."
   (interactive (list (prefix-numeric-value current-prefix-arg)
 		     (read-char-from-minibuffer "Zap to char: "
-						nil 'read-char-history)))
-  ;; 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))))
+						nil 'read-char-history)
+                     t))
+  (pcase-let ((`(,start ,end) (zap--to-char-region arg char nil interactive)))
+    (kill-region start end)))
+
+(defun zap-up-to-char (arg char &optional interactive)
+  "Kill up to, but not including ARGth occurrence of CHAR.
+Optional argument INTERACTIVE tells the function to behave as when
+it's called interactively.
+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.
+Called interactively, do a case sensitive search if the char is an
+upper case letter.
+Ignores CHAR at point."
+  (interactive (list (prefix-numeric-value current-prefix-arg)
+		     (read-char-from-minibuffer "Zap up to char: "
+						nil 'read-char-history)
+                     t))
+  (pcase-let ((`(,start ,end) (zap--to-char-region arg char 'up-to interactive)))
+    (kill-region start end)))
 
 ;; kill-line and its subroutines.
 
diff --git a/test/lisp/misc-tests.el b/test/lisp/misc-tests.el
index 36a8726b88..bec786c8c7 100644
--- a/test/lisp/misc-tests.el
+++ b/test/lisp/misc-tests.el
@@ -38,14 +38,6 @@ misc-test-copy-from-above-command
   (with-misc-test "abc\n" "abc\nab"
     (copy-from-above-command 2)))
 
-(ert-deftest misc-test-zap-up-to-char ()
-  (with-misc-test "abcde" "cde"
-    (goto-char (point-min))
-    (zap-up-to-char 1 ?c))
-  (with-misc-test "abcde abc123" "c123"
-    (goto-char (point-min))
-    (zap-up-to-char 2 ?c)))
-
 (ert-deftest misc-test-upcase-char ()
   (with-misc-test "abcde" "aBCDe"
     (goto-char (1+ (point-min)))
diff --git a/test/lisp/simple-tests.el b/test/lisp/simple-tests.el
index 6350bebeee..b26a4e4b83 100644
--- a/test/lisp/simple-tests.el
+++ b/test/lisp/simple-tests.el
@@ -971,5 +971,43 @@ 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))))
 
+(defmacro with-simple-test (original result &rest body)
+  (declare (indent 2) (debug (stringp stringp body)))
+  `(with-temp-buffer
+     (insert ,original)
+     ,@body
+     (should (equal (buffer-string) ,result))))
+
+(ert-deftest simple-tests-zap-to-char ()
+  (with-simple-test "abcde" "de"
+    (goto-char (point-min))
+    (zap-to-char 1 ?c))
+  (with-simple-test "abcde abc123" "123"
+    (goto-char (point-min))
+    (zap-to-char 2 ?c))
+  (let ((case-fold-search t))
+    (with-simple-test "abcdeCXYZ" "deCXYZ"
+      (goto-char (point-min))
+      (zap-to-char 1 ?C))
+    (with-simple-test "abcdeCXYZ" "XYZ"
+      (goto-char (point-min))
+      (zap-to-char 1 ?C 'interactive))))
+
+(ert-deftest simple-tests-zap-up-to-char ()
+  (with-simple-test "abcde" "cde"
+    (goto-char (point-min))
+    (zap-up-to-char 1 ?c))
+  (with-simple-test "abcde abc123" "c123"
+    (goto-char (point-min))
+    (zap-up-to-char 2 ?c))
+  (let ((case-fold-search t))
+    (with-simple-test "abcdeCXYZ" "cdeCXYZ"
+      (goto-char (point-min))
+      (zap-up-to-char 1 ?C))
+    (with-simple-test "abcdeCXYZ" "CXYZ"
+      (goto-char (point-min))
+      (zap-up-to-char 1 ?C 'interactive))))
+
+
 (provide 'simple-test)
 ;;; simple-tests.el ends here
--8<-----------------------------cut here---------------end--------------->8---

In GNU Emacs 29.0.50 (build 1, x86_64-pc-linux-gnu, X toolkit, cairo version 1.16.0, Xaw scroll bars)
 of 2022-04-08 built
Repository revision: 022a1f48a4e2005be7aa66b67eb2f17f8386c853
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12011000
System Description: Debian GNU/Linux 11 (bullseye)