]> git.eshelyaron.com Git - emacs.git/commitdiff
Fix 'backward-delete-char-untabify'
authorAsher Copeland <ashercopeland@gmail.com>
Wed, 2 Apr 2025 17:55:06 +0000 (10:55 -0700)
committerEshel Yaron <me@eshelyaron.com>
Sun, 13 Apr 2025 20:55:59 +0000 (22:55 +0200)
* lisp/simple.el (backward-delete-char-untabify): Fix
behavior when there's an active region.  (Bug#75042)

Copyright-paperwork-exempt: yes
(cherry picked from commit 5665b446b7a5b2f6ff4d4e7ea9b2c91e0e733c92)

lisp/simple.el

index c49cf3ea2dadf0d243399112f8d559a4a73bc6e4..a406faf414361601d05f8904628761327b7ca6af 100644 (file)
@@ -6414,30 +6414,36 @@ To disable this, set option `delete-active-region' to nil.
 Interactively, ARG is the prefix arg (default 1)
 and KILLP is t if a prefix arg was specified."
   (interactive "*p\nP")
-  (when (eq backward-delete-char-untabify-method 'untabify)
-    (let ((count arg))
-      (save-excursion
-       (while (and (> count 0) (not (bobp)))
-         (if (= (preceding-char) ?\t)
-             (let ((col (current-column)))
-               (forward-char -1)
-               (setq col (- col (current-column)))
-               (insert-char ?\s col)
-               (delete-char 1)))
-         (forward-char -1)
-         (setq count (1- count))))))
-  (let* ((skip (cond ((eq backward-delete-char-untabify-method 'hungry) " \t")
-                     ((eq backward-delete-char-untabify-method 'all)
-                      " \t\n\r")))
-         (n (if skip
-                (let* ((oldpt (point))
-                       (wh (- oldpt (save-excursion
-                                      (skip-chars-backward skip)
-                                      (constrain-to-field nil oldpt)))))
-                  (+ arg (if (zerop wh) 0 (1- wh))))
-              arg)))
-    ;; Avoid warning about delete-backward-char
-    (with-no-warnings (delete-backward-char n killp))))
+  (if (and (use-region-p)
+           delete-active-region
+           (= arg 1))
+      ;; Delete the text of the region and deactivate the mark, in the
+      ;; same way that `delete-backward-char' does.
+      (with-no-warnings (delete-backward-char 1 killp))
+    (when (eq backward-delete-char-untabify-method 'untabify)
+      (let ((count arg))
+        (save-excursion
+         (while (and (> count 0) (not (bobp)))
+           (if (= (preceding-char) ?\t)
+               (let ((col (current-column)))
+                 (forward-char -1)
+                 (setq col (- col (current-column)))
+                 (insert-char ?\s col)
+                 (delete-char 1)))
+           (forward-char -1)
+           (setq count (1- count))))))
+    (let* ((skip (cond ((eq backward-delete-char-untabify-method 'hungry) " \t")
+                       ((eq backward-delete-char-untabify-method 'all)
+                        " \t\n\r")))
+           (n (if skip
+                  (let* ((oldpt (point))
+                         (wh (- oldpt (save-excursion
+                                        (skip-chars-backward skip)
+                                        (constrain-to-field nil oldpt)))))
+                    (+ arg (if (zerop wh) 0 (1- wh))))
+                arg)))
+      ;; Avoid warning about delete-backward-char
+      (with-no-warnings (delete-backward-char n killp)))))
 
 (defun char-uppercase-p (char)
   "Return non-nil if CHAR is an upper-case character.