From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Sebastian Miele Newsgroups: gmane.emacs.bugs Subject: bug#65734: [BUG] kill-whole-line on folded subtrees [9.6.8 (release_9.6.8-3-g21171d @ /home/w/usr/emacs/0/29/0/lisp/org/)] Date: Mon, 25 Dec 2023 19:53:36 +0100 Message-ID: <87o7eew0is.fsf@whxvd.name> References: <87il8pao4l.fsf@whxvd.name> <87tts8vrpb.fsf@localhost> <83h6o84yz1.fsf@gnu.org> <875y4oaban.fsf@whxvd.name> <83bkeg4o1u.fsf@gnu.org> <87o7ifu2v8.fsf@localhost> <838r9j339x.fsf@gnu.org> <87tts78lve.fsf@whxvd.name> <87zg1u0yrp.fsf@whxvd.name> <8334zmrmnu.fsf@gnu.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="2754"; mail-complaints-to="usenet@ciao.gmane.io" Cc: yantar92@posteo.net, 65734@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Mon Dec 25 20:23:15 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1rHqXi-0000SB-Ja for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 25 Dec 2023 20:23:14 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rHqXR-0001rg-DF; Mon, 25 Dec 2023 14:22:57 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rHqXP-0001rW-R1 for bug-gnu-emacs@gnu.org; Mon, 25 Dec 2023 14:22:55 -0500 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rHqXP-00028V-IF for bug-gnu-emacs@gnu.org; Mon, 25 Dec 2023 14:22:55 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rHqXW-0001PT-1K for bug-gnu-emacs@gnu.org; Mon, 25 Dec 2023 14:23:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Sebastian Miele Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 25 Dec 2023 19:23:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 65734 X-GNU-PR-Package: emacs Original-Received: via spool by 65734-submit@debbugs.gnu.org id=B65734.17035321485376 (code B ref 65734); Mon, 25 Dec 2023 19:23:01 +0000 Original-Received: (at 65734) by debbugs.gnu.org; 25 Dec 2023 19:22:28 +0000 Original-Received: from localhost ([127.0.0.1]:55400 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rHqWw-0001Od-Un for submit@debbugs.gnu.org; Mon, 25 Dec 2023 14:22:27 -0500 Original-Received: from mout-p-202.mailbox.org ([80.241.56.172]:47628) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rHqWt-0001ON-Du for 65734@debbugs.gnu.org; Mon, 25 Dec 2023 14:22:25 -0500 Original-Received: from smtp102.mailbox.org (smtp102.mailbox.org [10.196.197.102]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-202.mailbox.org (Postfix) with ESMTPS id 4SzST95Mdvz9sSS; Mon, 25 Dec 2023 20:22:05 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=whxvd.name; s=MBO0001; t=1703532125; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=8L4a+Z9DwOxbiBMrD4nxZvRQ6A8Hgz/5wzHH8PYaOeI=; b=cP3ERtE2pJj7jdzPvOBBu94GKFMSJHZ2znrd1+oIkcshVVi4La4p+NOvhyUNjE+ZNZitES tEfftx6RPT0LjiluojM2vLbjVELav8kRhuI43qnp0Ne2wfC733Zh3Kk5zaquDW77zy/+dY ZL4Ki4V3ac9duM3i4OADpw7ocz8ZeS6NHPsTfOwdUtFQ74vnODeRcNNeyWraEMfb1wEM62 oz+q/kuMqW4OltKvUSRDaHHkyvS6GMeFzTJ8VrEVJ4/KiAhfHgziGUoZMcvEiE8p+XHXQN ygkHJZEawhLhF8VRNFHBuZI5he5q3ZYf3I/5twMdsEwqdbTQmdblLS0IRiht/w== In-reply-to: <8334zmrmnu.fsf@gnu.org> X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:276886 Archived-At: --=-=-= Content-Type: text/plain Attached are two patches. The first patch introduces the tests, including two tests that are expected to be failing for the current `kill-whole-line'. The test `kill-whole-line-read-only' does not fail because of the bug reported in this bug report, but because of another bug that I stumbled upon while investigating and testing: `kill-whole-line' always kills by two calls to `kill-region'. When the buffer is readonly, the first of the two calls to `kill-region' errors out / exits non-locally. That causes `kill-region' to omit to put the remaining stuff (from the second `kill-region') into the kill ring. The second patch fixes both bugs, and removes the corresponding `:expected-result :failed' from the tests. > From: Eli Zaretskii > Date: Sun, 2023-09-10 19:57 +0300 > >> From: Sebastian Miele >> Cc: Eli Zaretskii , Ihor Radchenko >> Date: Sun, 10 Sep 2023 18:31:20 +0200 >> >> I removed emacs-orgmode@gnu.org from CC. >> >> > From: Sebastian Miele >> > Date: Wed, 2023-09-06 15:30 +0200 >> > >> > I will write the tests. And I will probably come up with an updated >> > version of the original patch. There is at least one cosmetic change. >> > And something else that I want to have tried. May take some time. >> >> Please have a look at the following patch. For now it contains three >> tests, two of them with :expected-result :failed. (They do not fail on >> the bug-fixed version of `kill-whole-line'.) > > Yes, there should be more tests, ideally: there are situations where > kill-whole-line signals an error, and I don't think I see tests where > some of the text is invisible (as the function uses > forward-visible-line and end-of-visual-line). I added tests for cases when `kill-whole-line' signals errors. The tests for the invisible stuff were already there, in the test `kill-whole-line-invisible'. The calls that introduce invisibility and `after-change-functions' in the test are: (org-mode) (org-fold-hide-sublevels 1) Even though its only five tests, I think they are rather very thorough, now. Except one test, all tests tests have several subtests. All branches of `kill-whole-line' are covered. >> There probably will be more tests and further questions. But for >> now, I would like to basically have a statement of whether the style >> of writing the tests goes in an acceptable direction. > > Looks reasonable, but I'm not sure I understand what will the test > show if one of the tests fails: will the information shown then tell > enough to understand which of the sub-tests failed and why? I found the `ert-info' macro which allows to add arbitrary annotiations to `should's in its body, used it for all subtests. They now are clearly distinguishable in the output of `ert', even when run from a terminal. --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=tests.patch diff --git a/test/lisp/simple-tests.el b/test/lisp/simple-tests.el index 28d8120f143..e6d3ffe8d34 100644 --- a/test/lisp/simple-tests.el +++ b/test/lisp/simple-tests.el @@ -22,6 +22,7 @@ ;;; Code: (require 'ert) +(require 'ert-x) (eval-when-compile (require 'cl-lib)) (defun simple-test--buffer-substrings () @@ -40,6 +41,49 @@ simple-test--dummy-buffer ,@body (with-no-warnings (simple-test--buffer-substrings)))) +(defconst simple-test-point-tag "") +(defconst simple-test-mark-tag "") + +(defun simple-test--set-buffer-text-point-mark (description) + "Set the current buffer's text, point and mark according to +DESCRIPTION. + +Erase current buffer and insert DESCRIPTION. Set point to the +first occurrence of `simple-test-point-tag' (\"\") in the +buffer, removing it. If there is no `simple-test-point-tag', set +point to the beginning of the buffer. If there is a +`simple-test-mark-tag' (\"\"), remove it, and set an active +mark there." + (erase-buffer) + (insert description) + (goto-char (point-min)) + (when (search-forward simple-test-mark-tag nil t) + (delete-char (- (length simple-test-mark-tag))) + (push-mark (point) nil 'activate)) + (goto-char (point-min)) + (when (search-forward simple-test-point-tag nil t) + (delete-char (- (length simple-test-point-tag))))) + +(defun simple-test--get-buffer-text-point-mark () + "Inverse of `simple-test--set-buffer-text-point-mark'." + (cond + ((not mark-active) + (concat (buffer-substring-no-properties (point-min) (point)) + simple-test-point-tag + (buffer-substring-no-properties (point) (point-max)))) + ((< (mark) (point)) + (concat (buffer-substring-no-properties (point-min) (mark)) + simple-test-mark-tag + (buffer-substring-no-properties (mark) (point)) + simple-test-point-tag + (buffer-substring-no-properties (point) (point-max)))) + (t + (concat (buffer-substring-no-properties (point-min) (point)) + simple-test-point-tag + (buffer-substring-no-properties (point) (mark)) + simple-test-mark-tag + (buffer-substring-no-properties (mark) (point-max)))))) + ;;; `count-words' (ert-deftest simple-test-count-words-bug-41761 () @@ -1046,5 +1090,190 @@ simple-tests-zap-to-char (with-zap-to-char-test "abcdeCXYZ" "XYZ" (zap-to-char 1 ?C 'interactive)))) + +;;; Tests for `kill-whole-line' + +(ert-deftest kill-whole-line-invisible () + :expected-result :failed + (cl-flet ((test (kill-whole-line-arg &rest expected-lines) + (ert-info ((format "%s" kill-whole-line-arg) :prefix "Subtest: ") + (ert-with-test-buffer-selected nil + (simple-test--set-buffer-text-point-mark + (string-join + '("* -2" "hidden" + "* -1" "hidden" + "* AB" "hidden" + "* 1" "hidden" + "* 2" "hidden" + "") + "\n")) + (org-mode) + (org-fold-hide-sublevels 1) + (kill-whole-line kill-whole-line-arg) + (should + (equal (string-join expected-lines "\n") + (simple-test--get-buffer-text-point-mark))))))) + (test 0 + "* -2" "hidden" + "* -1" "hidden" + "" + "* 1" "hidden" + "* 2" "hidden" + "") + (test 1 + "* -2" "hidden" + "* -1" "hidden" + "* 1" "hidden" + "* 2" "hidden" + "") + (test 2 + "* -2" "hidden" + "* -1" "hidden" + "* 2" "hidden" + "") + (test 3 + "* -2" "hidden" + "* -1" "hidden" + "") + (test 9 + "* -2" "hidden" + "* -1" "hidden" + "") + (test -1 + "* -2" "hidden" + "* -1" "hidden" + "* 1" "hidden" + "* 2" "hidden" + "") + (test -2 + "* -2" "hidden" + "* 1" "hidden" + "* 2" "hidden" + "") + (test -3 + "" + "* 1" "hidden" + "* 2" "hidden" + "") + (test -9 + "" + "* 1" "hidden" + "* 2" "hidden" + ""))) + +(ert-deftest kill-whole-line-read-only () + :expected-result :failed + (cl-flet + ((test (kill-whole-line-arg expected-kill-lines expected-buffer-lines) + (ert-info ((format "%s" kill-whole-line-arg) :prefix "Subtest: ") + (ert-with-test-buffer-selected nil + (simple-test--set-buffer-text-point-mark + (string-join '("-2" "-1" "AB" "1" "2" "") "\n")) + (read-only-mode 1) + (setq last-command #'ignore) + (should-error (kill-whole-line kill-whole-line-arg) + :type 'buffer-read-only) + (should (equal (string-join expected-kill-lines "\n") + (car kill-ring))) + (should (equal (string-join expected-buffer-lines "\n") + (simple-test--get-buffer-text-point-mark))))))) + (test 0 '("AB") '("-2" "-1" "AB" "1" "2" "")) + (test 1 '("AB" "") '("-2" "-1" "AB" "1" "2" "")) + (test 2 '("AB" "1" "") '("-2" "-1" "AB" "1" "2" "")) + (test 3 '("AB" "1" "2" "") '("-2" "-1" "AB" "1" "2" "")) + (test 9 '("AB" "1" "2" "") '("-2" "-1" "AB" "1" "2" "")) + (test -1 '("" "AB") '("-2" "-1" "AB" "1" "2" "")) + (test -2 '("" "-1" "AB") '("-2" "-1" "AB" "1" "2" "")) + (test -3 '("-2" "-1" "AB") '("-2" "-1" "AB" "1" "2" "")) + (test -9 '("-2" "-1" "AB") '("-2" "-1" "AB" "1" "2" "")))) + +(ert-deftest kill-whole-line-after-other-kill () + (ert-with-test-buffer-selected nil + (simple-test--set-buffer-text-point-mark "AXB") + (setq last-command #'ignore) + (kill-region (point) (mark)) + (deactivate-mark 'force) + (setq last-command #'kill-region) + (kill-whole-line) + (should (equal "AXB" (car kill-ring))) + (should (equal "" + (simple-test--get-buffer-text-point-mark))))) + +(ert-deftest kill-whole-line-buffer-boundaries () + (ert-with-test-buffer-selected nil + (ert-info ("0" :prefix "Subtest: ") + (simple-test--set-buffer-text-point-mark "") + (should-error (kill-whole-line -1) + :type 'beginning-of-buffer) + (should-error (kill-whole-line 1) + :type 'end-of-buffer)) + (ert-info ("1a" :prefix "Subtest: ") + (simple-test--set-buffer-text-point-mark "-1\n") + (should-error (kill-whole-line 1) + :type 'end-of-buffer)) + (ert-info ("1b" :prefix "Subtest: ") + (simple-test--set-buffer-text-point-mark "-1\nA") + (setq last-command #'ignore) + (kill-whole-line 1) + (should (equal "-1\n" + (simple-test--get-buffer-text-point-mark))) + (should (equal "A" (car kill-ring)))) + (ert-info ("2" :prefix "Subtest: ") + (simple-test--set-buffer-text-point-mark "\n1") + (should-error (kill-whole-line -1) + :type 'beginning-of-buffer)) + (ert-info ("2b" :prefix "Subtest: ") + (simple-test--set-buffer-text-point-mark "A\n1") + (setq last-command #'ignore) + (kill-whole-line 1) + (should (equal "1" + (simple-test--get-buffer-text-point-mark))) + (should (equal "A\n" (car kill-ring)))))) + +(ert-deftest kill-whole-line-line-boundaries () + (ert-with-test-buffer-selected nil + (ert-info ("1a" :prefix "Subtest: ") + (simple-test--set-buffer-text-point-mark "-1\n\n1\n") + (setq last-command #'ignore) + (kill-whole-line 1) + (should (equal "-1\n1\n" + (simple-test--get-buffer-text-point-mark))) + (should (equal "\n" (car kill-ring)))) + (ert-info ("1b" :prefix "Subtest: ") + (simple-test--set-buffer-text-point-mark "-1\n\n1\n") + (setq last-command #'ignore) + (kill-whole-line -1) + (should (equal "-1\n1\n" + (simple-test--get-buffer-text-point-mark))) + (should (equal "\n" (car kill-ring)))) + (ert-info ("2a" :prefix "Subtest: ") + (simple-test--set-buffer-text-point-mark "-1\nA\n1\n") + (setq last-command #'ignore) + (kill-whole-line 1) + (should (equal "-1\n1\n" + (simple-test--get-buffer-text-point-mark))) + (should (equal "A\n" (car kill-ring)))) + (ert-info ("2b" :prefix "Subtest: ") + (simple-test--set-buffer-text-point-mark "-1\nA\n1\n") + (setq last-command #'ignore) + (kill-whole-line -1) + (should (equal "-1\n1\n" + (simple-test--get-buffer-text-point-mark))) + (should (equal "\nA" (car kill-ring)))) + (ert-info ("3a" :prefix "Subtest: ") + (simple-test--set-buffer-text-point-mark "-1\nA\n1\n") + (setq last-command #'ignore) + (kill-whole-line 1) + (should (equal "-1\n1\n" + (simple-test--get-buffer-text-point-mark))) + (should (equal "A\n" (car kill-ring)))) + (ert-info ("3b" :prefix "Subtest: ") + (simple-test--set-buffer-text-point-mark "-1\nA\n1\n") + (setq last-command #'ignore) + (kill-whole-line -1) + (should (equal "-1\n1\n" + (simple-test--get-buffer-text-point-mark))) + (should (equal "\nA" (car kill-ring)))))) + (provide 'simple-test) ;;; simple-tests.el ends here --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=fix.patch diff --git a/lisp/simple.el b/lisp/simple.el index 6453dfbcd2b..1fd087538b7 100644 --- a/lisp/simple.el +++ b/lisp/simple.el @@ -6640,28 +6640,53 @@ kill-whole-line (unless (eq last-command 'kill-region) (kill-new "") (setq last-command 'kill-region)) - (cond ((zerop arg) - ;; We need to kill in two steps, because the previous command - ;; could have been a kill command, in which case the text - ;; before point needs to be prepended to the current kill - ;; ring entry and the text after point appended. Also, we - ;; need to use save-excursion to avoid copying the same text - ;; twice to the kill ring in read-only buffers. - (save-excursion - (kill-region (point) (progn (forward-visible-line 0) (point)))) - (kill-region (point) (progn (end-of-visible-line) (point)))) - ((< arg 0) - (save-excursion - (kill-region (point) (progn (end-of-visible-line) (point)))) - (kill-region (point) - (progn (forward-visible-line (1+ arg)) - (unless (bobp) (backward-char)) - (point)))) - (t - (save-excursion - (kill-region (point) (progn (forward-visible-line 0) (point)))) - (kill-region (point) - (progn (forward-visible-line arg) (point)))))) + ;; - We need to kill in two steps, because the previous command + ;; could have been a kill command, in which case the text before + ;; point needs to be prepended to the current kill ring entry and + ;; the text after point appended. + ;; - We need to be careful to avoid copying text twice to the kill + ;; ring in read-only buffers. + ;; - We need to determine the boundaries of visible lines before we + ;; do the first kill. Otherwise `after-change-functions' may + ;; change visibility (bug#65734). + (let (;; The beginning of both regions to kill + (regions-begin (point-marker)) + ;; The end of the first region to kill. Moreover, after + ;; evaluation of the value form, (point) will be the end of + ;; the second region to kill. + (region1-end (cond ((zerop arg) + (prog1 (save-excursion + (forward-visible-line 0) + (point-marker)) + (end-of-visible-line))) + ((< arg 0) + (prog1 (save-excursion + (end-of-visible-line) + (point-marker)) + (forward-visible-line (1+ arg)) + (unless (bobp) (backward-char)))) + (t + (prog1 (save-excursion + (forward-visible-line 0) + (point-marker)) + (forward-visible-line arg)))))) + ;; - Pass the marker positions and not the markers themselves. + ;; kill-region determines whether to prepend or append to a + ;; previous kill by checking the direction of the region. But + ;; it deletes the content and hence moves the markers before + ;; that. That effectively makes every region delimited by + ;; markers an (empty) forward region. + ;; - Make the first kill-region emit a non-local exit only if the + ;; second kill-region below would not operate on a non-empty + ;; region. + (let ((kill-read-only-ok (or kill-read-only-ok + (/= regions-begin (point))))) + (kill-region (marker-position regions-begin) + (marker-position region1-end))) + (kill-region (marker-position regions-begin) + (point)) + (set-marker regions-begin nil) + (set-marker region1-end nil))) (defun forward-visible-line (arg) "Move forward by ARG lines, ignoring currently invisible newlines only. diff --git a/test/lisp/simple-tests.el b/test/lisp/simple-tests.el index e6d3ffe8d34..15f2db7e610 100644 --- a/test/lisp/simple-tests.el +++ b/test/lisp/simple-tests.el @@ -1094,7 +1094,6 @@ simple-tests-zap-to-char ;;; Tests for `kill-whole-line' (ert-deftest kill-whole-line-invisible () - :expected-result :failed (cl-flet ((test (kill-whole-line-arg &rest expected-lines) (ert-info ((format "%s" kill-whole-line-arg) :prefix "Subtest: ") (ert-with-test-buffer-selected nil @@ -1162,7 +1161,6 @@ kill-whole-line-invisible ""))) (ert-deftest kill-whole-line-read-only () - :expected-result :failed (cl-flet ((test (kill-whole-line-arg expected-kill-lines expected-buffer-lines) (ert-info ((format "%s" kill-whole-line-arg) :prefix "Subtest: ") --=-=-=--