Package: emacs;
Reported by: Sebastian Miele <iota <at> whxvd.name>
Date: Mon, 4 Sep 2023 14:49:02 UTC
Severity: normal
Found in version 29.1.50
Done: Eli Zaretskii <eliz <at> gnu.org>
Bug is archived. No further changes may be made.
Message #188 received at 65734 <at> debbugs.gnu.org (full text, mbox):
From: Ihor Radchenko <yantar92 <at> posteo.net> To: Eli Zaretskii <eliz <at> gnu.org> Cc: 65734 <at> debbugs.gnu.org, Sebastian Miele <iota <at> whxvd.name>, Stefan Monnier <monnier <at> iro.umontreal.ca>, Stefan Kangas <stefankangas <at> gmail.com> Subject: Re: 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: Wed, 19 Jun 2024 14:01:12 +0000
[Message part 1 (text/plain, inline)]
Eli Zaretskii <eliz <at> gnu.org> writes: > The patches lack suitable ChangeLog-style commit log messages (see > CONTRIBUTE for details and you can use "git log" to show examples of > how we do this). > > I'd also ask Stefan Monnier and Stefan Kangas to review the patch, > since this is an important command and I would like to avoid breaking > it. Please see the attached edited patches with proper commit messages.
[0001-kill-whole-line-Honor-visibility-fix-kill-ring-when-.patch (text/x-patch, inline)]
From 340e1a6a9d394c89c23ef34cdb37fa9124b4bd84 Mon Sep 17 00:00:00 2001 Message-ID: <340e1a6a9d394c89c23ef34cdb37fa9124b4bd84.1718805590.git.yantar92 <at> posteo.net> From: Sebastian Miele <iota <at> whxvd.name> Date: Wed, 19 Jun 2024 15:48:59 +0200 Subject: [PATCH 1/2] kill-whole-line: Honor visibility; fix kill-ring when read-only (bug#65734) * lisp/simple.el (kill-whole-line): Use visibility state before performing any edits as reference instead of expecting that visibility cannot change. First of the two calls to `kill-region' may trigger `after-change-functions' that might alter the visibility state. Make sure that we populate the `kill-ring' with the contents of the whole line when buffer is in `read-only-mode'. --- lisp/simple.el | 69 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 22 deletions(-) diff --git a/lisp/simple.el b/lisp/simple.el index b48f46fc711..76dffcdd327 100644 --- a/lisp/simple.el +++ b/lisp/simple.el @@ -6703,28 +6703,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. -- 2.45.1
[0002-Add-tests-for-kill-whole-line-bug-65734.patch (text/x-patch, inline)]
From 40a14348da6680b2213133aa3909e47223459e14 Mon Sep 17 00:00:00 2001 Message-ID: <40a14348da6680b2213133aa3909e47223459e14.1718805590.git.yantar92 <at> posteo.net> In-Reply-To: <340e1a6a9d394c89c23ef34cdb37fa9124b4bd84.1718805590.git.yantar92 <at> posteo.net> References: <340e1a6a9d394c89c23ef34cdb37fa9124b4bd84.1718805590.git.yantar92 <at> posteo.net> From: Sebastian Miele <iota <at> whxvd.name> Date: Wed, 19 Jun 2024 15:58:24 +0200 Subject: [PATCH 2/2] Add tests for `kill-whole-line' (bug#65734) * test/lisp/simple-tests.el (simple-test-point-tag): (simple-test-mark-tag): (simple-test--set-buffer-text-point-mark): (simple-test--get-buffer-text-point-mark): Add helper functions used by the tests. (kill-whole-line-invisible): (kill-whole-line-read-only): (kill-whole-line-after-other-kill): (kill-whole-line-buffer-boundaries): (kill-whole-line-line-boundaries): Add tests for `kill-whole-line'. --- test/lisp/simple-tests.el | 227 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 227 insertions(+) diff --git a/test/lisp/simple-tests.el b/test/lisp/simple-tests.el index afd75786804..9e3e71bd69b 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,188 @@ 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 () + (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 () + (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 -- 2.45.1
[Message part 4 (text/plain, inline)]
-- Ihor Radchenko // yantar92, Org mode contributor, Learn more about Org mode at <https://orgmode.org/>. Support Org development at <https://liberapay.com/org-mode>, or support my work at <https://liberapay.com/yantar92>
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.