From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Ihor Radchenko 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: Wed, 19 Jun 2024 14:01:12 +0000 Message-ID: <87wmmlowuv.fsf@localhost> 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> <87o7eew0is.fsf@whxvd.name> <83mstiu9c3.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="32403"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 65734@debbugs.gnu.org, Sebastian Miele , Stefan Monnier , Stefan Kangas To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Wed Jun 19 16:00:14 2024 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 1sJvrB-0008Db-FI for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 19 Jun 2024 16:00:13 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sJvqz-00037S-RL; Wed, 19 Jun 2024 10:00:01 -0400 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 1sJvqx-000372-Ts for bug-gnu-emacs@gnu.org; Wed, 19 Jun 2024 09:59:59 -0400 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 1sJvqx-0004XA-M5 for bug-gnu-emacs@gnu.org; Wed, 19 Jun 2024 09:59:59 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1sJvr0-0001W4-If for bug-gnu-emacs@gnu.org; Wed, 19 Jun 2024 10:00:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Ihor Radchenko Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 19 Jun 2024 14:00:02 +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.17188055845749 (code B ref 65734); Wed, 19 Jun 2024 14:00:02 +0000 Original-Received: (at 65734) by debbugs.gnu.org; 19 Jun 2024 13:59:44 +0000 Original-Received: from localhost ([127.0.0.1]:45589 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sJvqg-0001Ue-PU for submit@debbugs.gnu.org; Wed, 19 Jun 2024 09:59:43 -0400 Original-Received: from mout02.posteo.de ([185.67.36.66]:37991) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sJvqd-0001UF-Bn for 65734@debbugs.gnu.org; Wed, 19 Jun 2024 09:59:41 -0400 Original-Received: from submission (posteo.de [185.67.36.169]) by mout02.posteo.de (Postfix) with ESMTPS id A1B31240103 for <65734@debbugs.gnu.org>; Wed, 19 Jun 2024 15:59:29 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1718805569; bh=nKh+54oW3CZEmNrJYYNM/ZLI1uZFQqvEXsjuheS9sIs=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type: From; b=XxYOyeT9fgpLhQjLGOjqAeLbsTjzSi9VEpSks91v2+DCxBVn11MdyfDXxQw+47dg1 cQ0NhHP37ZsPS1ZUa01pKwEX/uJCuXzjeHP1e0oX+BCAhf433X0jiprGO6PBH1BZkb FnvpUlXSn4F2kCsrL+kDD/nYgXHSitSiarXYCP8FmPr92QuA94wj/g/xYEi4j3Ooyf AYGye6t/7s6Jq7UBM1qd8eY+/GVZdzbxpgqxYXadFxaKJMCpA2+0HUZysDSpyYhOUP NOdCS+BZ/tPKt9U5HPjIkgZ78SVaSupk5fNwDiM8KitZd8EL9xdO5UU15lcSOM0Su8 guoOpfBnkLZQw== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4W44xD2kYwz6txh; Wed, 19 Jun 2024 15:59:28 +0200 (CEST) In-Reply-To: <83mstiu9c3.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:287483 Archived-At: --=-=-= Content-Type: text/plain Eli Zaretskii 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. --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-kill-whole-line-Honor-visibility-fix-kill-ring-when-.patch >From 340e1a6a9d394c89c23ef34cdb37fa9124b4bd84 Mon Sep 17 00:00:00 2001 Message-ID: <340e1a6a9d394c89c23ef34cdb37fa9124b4bd84.1718805590.git.yantar92@posteo.net> From: Sebastian Miele 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 --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0002-Add-tests-for-kill-whole-line-bug-65734.patch >From 40a14348da6680b2213133aa3909e47223459e14 Mon Sep 17 00:00:00 2001 Message-ID: <40a14348da6680b2213133aa3909e47223459e14.1718805590.git.yantar92@posteo.net> In-Reply-To: <340e1a6a9d394c89c23ef34cdb37fa9124b4bd84.1718805590.git.yantar92@posteo.net> References: <340e1a6a9d394c89c23ef34cdb37fa9124b4bd84.1718805590.git.yantar92@posteo.net> From: Sebastian Miele 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 "") +(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,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" + "* 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 () + (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 -- 2.45.1 --=-=-= Content-Type: text/plain -- Ihor Radchenko // yantar92, Org mode contributor, Learn more about Org mode at . Support Org development at , or support my work at --=-=-=--