From 5665b446b7a5b2f6ff4d4e7ea9b2c91e0e733c92 Mon Sep 17 00:00:00 2001 From: Asher Copeland Date: Wed, 2 Apr 2025 10:55:06 -0700 Subject: [PATCH] Fix 'backward-delete-char-untabify' * lisp/simple.el (backward-delete-char-untabify): Fix behavior when there's an active region. (Bug#75042) Copyright-paperwork-exempt: yes --- lisp/simple.el | 54 ++++++++++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/lisp/simple.el b/lisp/simple.el index 7037158df8d..ee09a6f1b19 100644 --- a/lisp/simple.el +++ b/lisp/simple.el @@ -6601,30 +6601,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.