emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Sebastian Miele <iota@whxvd.name>
To: Eli Zaretskii <eliz@gnu.org>
Cc: Ihor Radchenko <yantar92@posteo.net>,
	65734@debbugs.gnu.org, emacs-orgmode@gnu.org
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: Tue, 05 Sep 2023 17:25:38 +0200	[thread overview]
Message-ID: <875y4oaban.fsf@whxvd.name> (raw)
In-Reply-To: <83h6o84yz1.fsf@gnu.org>

> From: Eli Zaretskii <eliz@gnu.org>
> Date: Tue, 2023-09-05 14:54 +0300
>
> […]
>
> So we could decide that this command needs to become smarter when the
> visual line includes invisible text.  That is, improve the command
> without making any Org-specific changes anywhere.  Patches to that
> effect are welcome.

The following would do it.  I think I tested it rather thoroughly.
During testing I found another bug that is addressed by the let-binding
of kill-read-only-ok during the first kill-region below.

diff --git a/lisp/simple.el b/lisp/simple.el
index abd587245fe..d983cb85ab3 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -6631,28 +6631,50 @@ 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, because that may change visibility
+  ;;   (bug#65734).
+  (let ((regions-begin (point-marker))
+        region1-end)
+    (cond ((zerop arg)
+           (setq region1-end (save-excursion
+                               (forward-visible-line 0)
+                               (point-marker)))
+           (end-of-visible-line))
+	  ((< arg 0)
+	   (setq region1-end (save-excursion
+                               (end-of-visible-line)
+                               (point-marker)))
+           (forward-visible-line (1+ arg))
+	   (unless (bobp) (backward-char)))
+	  (t
+	   (setq region1-end (save-excursion
+                               (forward-visible-line 0)
+                               (point-marker)))
+	   (progn (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.


  reply	other threads:[~2023-09-05 15:30 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-04 16:30 [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/)] Sebastian Miele
2023-09-05 10:29 ` Ihor Radchenko
2023-09-05 11:54   ` bug#65734: " Eli Zaretskii
2023-09-05 15:25     ` Sebastian Miele [this message]
2023-09-05 15:50       ` Eli Zaretskii
2023-09-06  8:23         ` Ihor Radchenko
2023-09-06 12:16           ` Eli Zaretskii
2023-09-06 13:30             ` Sebastian Miele
2023-09-07 13:49               ` Ihor Radchenko
2023-09-06  8:30     ` Ihor Radchenko
2023-09-06 12:20       ` Eli Zaretskii
2023-09-07 10:00         ` Ihor Radchenko
2023-09-07 10:19           ` Eli Zaretskii
2023-09-07 10:27             ` Sebastian Miele
2023-09-07 13:43             ` Ihor Radchenko
2023-09-06 15:04       ` Sebastian Miele
2023-09-07 10:03         ` Ihor Radchenko
2023-09-05 14:30   ` Max Nikulin
2023-09-05 15:42     ` Eli Zaretskii
2023-09-05 15:50       ` Ihor Radchenko
2023-09-05 16:02         ` Max Nikulin
2023-09-05 16:12           ` Ihor Radchenko
2023-09-05 16:14         ` Eli Zaretskii
2024-01-07 16:27         ` Stefan Monnier
2024-01-08 12:15           ` Ihor Radchenko
2024-01-08 15:33             ` Stefan Monnier
2024-01-09 14:52               ` Ihor Radchenko
2024-01-09 16:48                 ` Stefan Monnier
2024-01-09 22:21                   ` Ihor Radchenko
2024-01-09 15:47             ` Ihor Radchenko
2024-01-09 16:01               ` Stefan Monnier
2024-01-09 22:33                 ` Ihor Radchenko
2024-01-10  3:08                   ` Stefan Monnier
2024-01-10 12:52                   ` Eli Zaretskii
2024-01-10 13:05                     ` Ihor Radchenko
2024-01-10 13:55                       ` Eli Zaretskii
2024-01-11 15:50                         ` Ihor Radchenko
2024-01-11 16:05                           ` Eli Zaretskii
2024-01-11 16:15                             ` Ihor Radchenko
2024-01-11 16:44                               ` Eli Zaretskii
2024-01-11 18:08                                 ` Ihor Radchenko
2024-01-11 19:19                                   ` Eli Zaretskii
2024-01-12 12:24                                     ` Ihor Radchenko
2024-01-12 12:32                                       ` Eli Zaretskii
2024-01-12 12:39                                         ` Ihor Radchenko
2024-01-12 14:03                                           ` Eli Zaretskii
2024-01-12 14:15                                             ` Ihor Radchenko
2024-01-12 21:09                                   ` Stefan Monnier
2024-01-12 21:16                                     ` Ihor Radchenko
2024-01-10 16:26                       ` Stefan Monnier
2024-01-10 16:39                         ` Eli Zaretskii
2024-01-11 15:44                         ` Ihor Radchenko
2024-01-09 15:50 ` Ihor Radchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=875y4oaban.fsf@whxvd.name \
    --to=iota@whxvd.name \
    --cc=65734@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=emacs-orgmode@gnu.org \
    --cc=yantar92@posteo.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).