emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [BUG] org-capture-kill: doesn't remove the right text region
@ 2022-07-14 21:24 Caleb Chase
  2022-07-17 10:00 ` Ihor Radchenko
  2022-07-24  6:21 ` Ihor Radchenko
  0 siblings, 2 replies; 4+ messages in thread
From: Caleb Chase @ 2022-07-14 21:24 UTC (permalink / raw)
  To: emacs-orgmode

I discovered a bug with org-capture and org-capture kill when
canceling an item or plain type template on a heading that has only an
empty line for content. Minimal reproduction is as follows:

1. Add the following to =/tmp/capture-bug-init.el=:
   #+begin_src emacs-lisp :noeval t
     (require 'org)
     (setq org-capture-templates
           '(("i" "Repro: item type" item (file+headline
"/tmp/test-capture.org" "Foo") nil :unnarrowed t)
             ("p" "Repro: plain type" plain (file+headline
"/tmp/test-capture.org" "Foo") "Plain capture: %?" :unnarrowed t)))

     (with-temp-buffer
       ;; FIXME BUG: occurs with empty line between headings.
       (insert "* Foo\n\n* Bar\n")
       ;; FIXME WORKING: no empty line between headings.
       ;; (insert "* Foo\n* Bar\n")
       ;; FIXME WORKING: existing item between headings.
       ;; (insert "* Foo\n- Some note\n* Bar\n")
       (write-file "/tmp/test-capture.org"))
     (find-file "/tmp/test-capture.org")
     (org-capture nil "i")
   #+end_src
2. Run Emacs: =emacs -Q --load /tmp/capture-bug-init.el=
3. Cancel the capture with =C-c C-k=
4. Bug occurs: the dash is left behind, so the last line is now =-*
Bar= instead of =* Bar=.

You can also reproduce this bug with the plain type template I have
above. Notice the different commented-out variants illustrating when
it behaves properly. This bug occurs when there is a single blank
empty line between the two headings, but does not occur if there is
not empty line or if there is an existing item.

---
*POSSIBLE ROOT CAUSE*
I suspect the =:begin-marker= capture marker is determined incorrectly
for plain and item capture types. I haven't dug into the relevant
functions, =org-capture-place-item= and =org-capture-place-plain-text=
closely enough to know exactly what is going on, but here's a
workaround I came up with. It's obviously not the Right Solution (TM),
but at least it illustrates the problem:

#+begin_src emacs-lisp :noeval t
  (defun capture-cancel-bug-fix ()
    (let ((begin (org-capture-get :begin-marker 'local)))
      (unless (equal (char-before begin) (string-to-char "\n"))
        (setq org-capture-current-plist
              (plist-put org-capture-current-plist
                         :begin-marker (1- begin))))))
  (add-hook 'org-capture-mode-hook #'capture-cancel-bug-fix)
#+end_src

---
*CONTEXT*
- Emacs version: 28.1
- Org version: 9.5.4. More specifically, tested on current bugfix
branch revision 6dc785288d3514af4071f210dac0a18c14a6c45b and current
main branch revision d9479887226ad79a1a8de739e7be0fc1fffec536.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [BUG] org-capture-kill: doesn't remove the right text region
  2022-07-14 21:24 [BUG] org-capture-kill: doesn't remove the right text region Caleb Chase
@ 2022-07-17 10:00 ` Ihor Radchenko
  2022-07-24  6:21 ` Ihor Radchenko
  1 sibling, 0 replies; 4+ messages in thread
From: Ihor Radchenko @ 2022-07-17 10:00 UTC (permalink / raw)
  To: Caleb Chase; +Cc: emacs-orgmode

Caleb Chase <caleb@chasecaleb.com> writes:

> I discovered a bug with org-capture and org-capture kill when
> canceling an item or plain type template on a heading that has only an
> empty line for content. Minimal reproduction is as follows:

Thanks for reporting!
Confirmed.

Best,
Ihor


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [BUG] org-capture-kill: doesn't remove the right text region
  2022-07-14 21:24 [BUG] org-capture-kill: doesn't remove the right text region Caleb Chase
  2022-07-17 10:00 ` Ihor Radchenko
@ 2022-07-24  6:21 ` Ihor Radchenko
  2022-07-31  5:06   ` Ihor Radchenko
  1 sibling, 1 reply; 4+ messages in thread
From: Ihor Radchenko @ 2022-07-24  6:21 UTC (permalink / raw)
  To: Caleb Chase; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 764 bytes --]

Caleb Chase <caleb@chasecaleb.com> writes:

> I discovered a bug with org-capture and org-capture kill when
> canceling an item or plain type template on a heading that has only an
> empty line for content. Minimal reproduction is as follows:

The fix is attached.
The reason why this bug is happening is `org-capture-empty-lines-before`
adding/removing the lines before the capture region. This situation was
not considered by org-capture.

Note that `org-capture-empty-lines-before` changes are not going to be
restored upon capture abortion. Fixing this will not be trivial (AFAIU,
suggestions are welcome) and the gains are marginal - I doubt that
anyone is going to care much about this side effect. Of course, feel
free to disagree by replying.

Best,
Ihor


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org-capture-kill-Fix-capture-boundaries-when-deletin.patch --]
[-- Type: text/x-patch, Size: 2744 bytes --]

From 66c3f8c6f54ec4a4aa4a3ca567e9c2ff5370246e Mon Sep 17 00:00:00 2001
Message-Id: <66c3f8c6f54ec4a4aa4a3ca567e9c2ff5370246e.1658643473.git.yantar92@gmail.com>
From: Ihor Radchenko <yantar92@gmail.com>
Date: Sun, 24 Jul 2022 14:14:58 +0800
Subject: [PATCH] org-capture-kill: Fix capture boundaries when deleting empty
 lines above

* lisp/org-capture.el (org-capture-place-entry):
(org-capture-place-item):
(org-capture-place-table-line):
(org-capture-place-plain-text): Store beginning of the capture region
as marker.  This will make the ORIGIN move if
`org-capture-empty-lines-before` has to add/remove lines.

Fixes https://orgmode.org/list/CAGyCDkPos+W_MEJffSZga83NsOLZx2XGTYEmSGQuHQvS-sNa8A@mail.gmail.com
---
 lisp/org-capture.el | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/lisp/org-capture.el b/lisp/org-capture.el
index 8748b7f84..6ca66835a 100644
--- a/lisp/org-capture.el
+++ b/lisp/org-capture.el
@@ -1178,7 +1178,7 @@ (defun org-capture-place-entry ()
         ;; Make sure that last point is not folded.
         (org-fold-core-cycle-over-indirect-buffers
           (org-fold-region (max 1 (1- (point-max))) (point-max) nil))))
-    (let ((origin (point)))
+    (let ((origin (point-marker)))
       (unless (bolp) (insert "\n"))
       (org-capture-empty-lines-before)
       (let ((beg (point)))
@@ -1243,7 +1243,7 @@ (defun org-capture-place-item ()
 				     (point))
 				   beg)))))))
     ;; Insert template.
-    (let ((origin (point)))
+    (let ((origin (point-marker)))
       (unless (bolp) (insert "\n"))
       ;; When a new list is created, always obey to `:empty-lines' and
       ;; friends.
@@ -1344,7 +1344,7 @@ (defun org-capture-place-table-line ()
       ;; No table found.  Create it with an empty header.
       (goto-char end)
       (unless (bolp) (insert "\n"))
-      (let ((origin (point)))
+      (let ((origin (point-marker)))
 	(insert "|   |\n|---|\n")
 	(narrow-to-region origin (point))))
     ;; In the current table, find the appropriate location for TEXT.
@@ -1373,7 +1373,7 @@ (defun org-capture-place-table-line ()
      (t
       (goto-char (org-table-end))))
     ;; Insert text and position point according to template.
-    (let ((origin (point)))
+    (let ((origin (point-marker)))
       (unless (bolp) (insert "\n"))
       (let ((beg (point))
 	    (end (save-excursion
@@ -1405,7 +1405,7 @@ (defun org-capture-place-plain-text ()
    (t
     ;; Beginning or end of file.
     (goto-char (if (org-capture-get :prepend) (point-min) (point-max)))))
-  (let ((origin (point)))
+  (let ((origin (point-marker)))
     (unless (bolp) (insert "\n"))
     (org-capture-empty-lines-before)
     (org-capture-position-for-last-stored (point))
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [BUG] org-capture-kill: doesn't remove the right text region
  2022-07-24  6:21 ` Ihor Radchenko
@ 2022-07-31  5:06   ` Ihor Radchenko
  0 siblings, 0 replies; 4+ messages in thread
From: Ihor Radchenko @ 2022-07-31  5:06 UTC (permalink / raw)
  To: Caleb Chase; +Cc: emacs-orgmode

Ihor Radchenko <yantar92@gmail.com> writes:

> Caleb Chase <caleb@chasecaleb.com> writes:
>
>> I discovered a bug with org-capture and org-capture kill when
>> canceling an item or plain type template on a heading that has only an
>> empty line for content. Minimal reproduction is as follows:
>
> The fix is attached.
> The reason why this bug is happening is `org-capture-empty-lines-before`
> adding/removing the lines before the capture region. This situation was
> not considered by org-capture.
>
> Note that `org-capture-empty-lines-before` changes are not going to be
> restored upon capture abortion. Fixing this will not be trivial (AFAIU,
> suggestions are welcome) and the gains are marginal - I doubt that
> anyone is going to care much about this side effect. Of course, feel
> free to disagree by replying.

Applied onto main via 2ea0830f0.
https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=2ea0830f07fa81b6e22ccdd43ad351ad76fcc097

Best,
Ihor


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-07-31  5:06 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-07-14 21:24 [BUG] org-capture-kill: doesn't remove the right text region Caleb Chase
2022-07-17 10:00 ` Ihor Radchenko
2022-07-24  6:21 ` Ihor Radchenko
2022-07-31  5:06   ` Ihor Radchenko

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).