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/)]

From: Sebastian Miele <[email-masked]>
Date: Mon, 25 Dec 2023 19:53:36 +0100

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 <[email-masked]>
> Date: Sun, 2023-09-10 19:57 +0300
>
>> From: Sebastian Miele <[email-masked]>
>> Cc: Eli Zaretskii <[email-masked]>, Ihor Radchenko <[email-masked]>
>> Date: Sun, 10 Sep 2023 18:31:20 +0200
>>
>> I removed [email-masked] from CC.
>>
>> > From: Sebastian Miele <[email-masked]>
>> > 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.

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 "<POINT>")
+(defconst simple-test-mark-tag "<MARK>")
+
+(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' (\"<POINT>\") 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' (\"<MARK>\"), 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"
+                      "* A<POINT>B" "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"
+          "<POINT>"
+          "* 1" "hidden"
+          "* 2" "hidden"
+          "")
+    (test 1
+          "* -2" "hidden"
+          "* -1" "hidden"
+          "<POINT>* 1" "hidden"
+          "* 2" "hidden"
+          "")
+    (test 2
+          "* -2" "hidden"
+          "* -1" "hidden"
+          "<POINT>* 2" "hidden"
+          "")
+    (test 3
+          "* -2" "hidden"
+          "* -1" "hidden"
+          "<POINT>")
+    (test 9
+          "* -2" "hidden"
+          "* -1" "hidden"
+          "<POINT>")
+    (test -1
+          "* -2" "hidden"
+          "* -1" "hidden<POINT>"
+          "* 1" "hidden"
+          "* 2" "hidden"
+          "")
+    (test -2
+          "* -2" "hidden<POINT>"
+          "* 1" "hidden"
+          "* 2" "hidden"
+          "")
+    (test -3
+          "<POINT>"
+          "* 1" "hidden"
+          "* 2" "hidden"
+          "")
+    (test -9
+          "<POINT>"
+          "* 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" "A<POINT>B" "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<POINT>" "1" "2" ""))
+    (test 1 '("AB" "") '("-2" "-1" "AB" "<POINT>1" "2" ""))
+    (test 2 '("AB" "1" "") '("-2" "-1" "AB" "1" "<POINT>2" ""))
+    (test 3 '("AB" "1" "2" "") '("-2" "-1" "AB" "1" "2" "<POINT>"))
+    (test 9 '("AB" "1" "2" "") '("-2" "-1" "AB" "1" "2" "<POINT>"))
+    (test -1 '("" "AB") '("-2" "-1<POINT>" "AB" "1" "2" ""))
+    (test -2 '("" "-1" "AB") '("-2<POINT>" "-1" "AB" "1" "2" ""))
+    (test -3 '("-2" "-1" "AB") '("<POINT>-2" "-1" "AB" "1" "2" ""))
+    (test -9 '("-2" "-1" "AB") '("<POINT>-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 "A<POINT>X<MARK>B")
+    (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 "<POINT>"
+                   (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 "<POINT>")
+      (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<POINT>")
+      (should-error (kill-whole-line 1)
+                    :type 'end-of-buffer))
+    (ert-info ("1b" :prefix "Subtest: ")
+      (simple-test--set-buffer-text-point-mark "-1\nA<POINT>")
+      (setq last-command #'ignore)
+      (kill-whole-line 1)
+      (should (equal "-1\n<POINT>"
+                     (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 "<POINT>\n1")
+      (should-error (kill-whole-line -1)
+                    :type 'beginning-of-buffer))
+    (ert-info ("2b" :prefix "Subtest: ")
+      (simple-test--set-buffer-text-point-mark "<POINT>A\n1")
+      (setq last-command #'ignore)
+      (kill-whole-line 1)
+      (should (equal "<POINT>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<POINT>\n1\n")
+      (setq last-command #'ignore)
+      (kill-whole-line 1)
+      (should (equal "-1\n<POINT>1\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<POINT>\n1\n")
+      (setq last-command #'ignore)
+      (kill-whole-line -1)
+      (should (equal "-1<POINT>\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<POINT>\n1\n")
+      (setq last-command #'ignore)
+      (kill-whole-line 1)
+      (should (equal "-1\n<POINT>1\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<POINT>\n1\n")
+      (setq last-command #'ignore)
+      (kill-whole-line -1)
+      (should (equal "-1<POINT>\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\n<POINT>A\n1\n")
+      (setq last-command #'ignore)
+      (kill-whole-line 1)
+      (should (equal "-1\n<POINT>1\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\n<POINT>A\n1\n")
+      (setq last-command #'ignore)
+      (kill-whole-line -1)
+      (should (equal "-1<POINT>\n1\n"
+                     (simple-test--get-buffer-text-point-mark)))
+      (should (equal "\nA" (car kill-ring))))))
+
 (provide 'simple-test)
 ;;; simple-tests.el ends here
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: ")