emacs-diffs
[Top][All Lists]
Advanced

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

master 212aea97f9: zap-to-char: case sensitive for upper-case characters


From: Tino Calancha
Subject: master 212aea97f9: zap-to-char: case sensitive for upper-case characters
Date: Sat, 21 May 2022 05:25:05 -0400 (EDT)

branch: master
commit 212aea97f9c9fdabdf7e8a47e64c8243953a7690
Author: Tino Calancha <tino.calancha@gmail.com>
Commit: Tino Calancha <tino.calancha@gmail.com>

    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 (Bug#54804).
    
    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-zap-to-char-test): Add helper macro.
    (simple-tests-zap-to-char): Add a test.
---
 etc/NEWS                  |  6 ++++++
 lisp/misc.el              | 15 +++++++++++----
 lisp/simple.el            | 17 +++++++++++------
 test/lisp/misc-tests.el   |  9 ++++++++-
 test/lisp/simple-tests.el | 21 +++++++++++++++++++++
 5 files changed, 57 insertions(+), 11 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 5eab8e23bb..d238fd74fe 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -698,6 +698,12 @@ Formerly it invoked 'just-one-space'.  The actions 
performed by
 'cycle-spacing' and their order can now be customized via the user
 option 'cycle-spacing-actions'.
 
+---
+** 'zap-to-char' and 'zap-up-to-char' are case-sensitive for upper-case chars.
+These commands now behave as case-sensitive for interactive calls when
+are invoked with an uppercase argument, regardless of the
+`case-fold-search' value.
+
 ---
 ** 'scroll-other-window' and 'scroll-other-window-down' now respect 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..0bb8ee6c7b 100644
--- a/lisp/misc.el
+++ b/lisp/misc.el
@@ -64,15 +64,22 @@ The characters copied are inserted in the buffer before 
point."
 ;; 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.
+When run interactively, the argument INTERACTIVE is non-nil.
 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.
+If 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 6aa079464a..6906675f68 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -6342,21 +6342,26 @@ then gives correct answers only for ASCII characters."
          (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.
+When run interactively, the argument INTERACTIVE is non-nil.
 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'.
+If 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 @@
     (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 437c62f61d..b4576889dc 100644
--- a/test/lisp/simple-tests.el
+++ b/test/lisp/simple-tests.el
@@ -1004,6 +1004,27 @@ See Bug#21722."
       (yank-in-context)
       (buffer-string))
     "echo 'f'\\''bar'\\''oo'")))
+
+;;; Tests for `zap-to-char'
+
+(defmacro with-zap-to-char-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-zap-to-char-test "abcde" "de"
+    (zap-to-char 1 ?c))
+  (with-zap-to-char-test "abcde abc123" "123"
+    (zap-to-char 2 ?c))
+  (let ((case-fold-search t))
+    (with-zap-to-char-test "abcdeCXYZ" "deCXYZ"
+      (zap-to-char 1 ?C))
+    (with-zap-to-char-test "abcdeCXYZ" "XYZ"
+      (zap-to-char 1 ?C 'interactive))))
 
 (provide 'simple-test)
 ;;; simple-tests.el ends here



reply via email to

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