emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] [BUG] org.el: Fix first call of `org-paste-subtree'
@ 2021-11-29 12:02 Max Nikulin
  2022-09-17 11:07 ` Max Nikulin
  2022-09-20 13:16 ` Ihor Radchenko
  0 siblings, 2 replies; 7+ messages in thread
From: Max Nikulin @ 2021-11-29 12:02 UTC (permalink / raw)
  To: emacs-orgmode

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

An old bug is living in Org.

=C-c C-x C-y= ~org-paste-subtree~ fails just after Emacs start.
Maybe there are more similar issues.

- Start *new* instance of emacs.
- Copy some text that is Org subtree from some
   *external* application (not Emacs).
   I usually come across this issue when I copy capture
   from my Firefox extension. Alternative:
   #+begin_src elisp :results silent
     (require 'ob-shell)
   #+end_src

   #+begin_src bash :results silent
     printf '%b' '* Heading\n\nbody\n' |
       xclip -in -selection clipborad >/dev/null
       # xsel --input --clipboard
   #+end_src

- Try =C-c C-x C-y= or [[elisp:(org-paste-subtree)]]

   + Actual result is the following message:
     : user-error: The kill is not a (set of) tree(s).  Use ‘C-y’ to 
yank anyway
   + Expected result is a heading pasted at the end of the buffer.
   + The result may be achieved by calling [[elisp:(org-paste-subtree)]] 
once more.

- The problem is ~(and kill-ring (current-kill 0))~ expression
   in the definition of ~org-paste-subtree~.
   Restart emacs and repeat steps above skipping ~org-paste-subtree~.
   Notice that ~(current-kill 0)~ call changes value of ~kill-ring~.
   #+begin_src elisp :results pp
     (list (and kill-ring t) (current-kill 0) (and kill-ring t))
   #+end_src

   #+RESULTS:
   : (nil "* Heading\n\nbody\n" t)

I suppose, it is better to let the error from ~current-kill~ to 
propagate (in the case of empty `kill-ring' and clipboard). I do not 
have Windows machine available to test the change.

There is another occurence of ~(and (current-kill 0))~
in ~org-kill-is-subtree-p~. I would rather transform it
to ~org~subtree-p~ to avoid call of ~current-kill~ inside, but this 
patch does not include such change.

[-- Attachment #2: 0001-org.el-Fix-first-call-of-org-paste-subtree.patch --]
[-- Type: text/x-patch, Size: 1214 bytes --]

From 04bbf6359f370bddb6ca5fff1d8c7737e7ac5ee7 Mon Sep 17 00:00:00 2001
From: Max Nikulin <manikulin@gmail.com>
Date: Mon, 29 Nov 2021 18:54:43 +0700
Subject: [PATCH] org.el: Fix first call of `org-paste-subtree'

* lisp/org.el (org-paste-subtree): Do not check `kill-ring' before
calling `current-kill' since the latter can pull content of clipboard.

First call of `org-paste-subtree' failed if nothing had been yanked
before since Emacs start but system clipboard had text with valid
subtree originating from other application.  The bug was where since
the commit adding `org-paste-subtree'.

If both `kill-ring' and system clipboard are empty then `current-kill'
generates meaningful error.
---
 lisp/org.el | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lisp/org.el b/lisp/org.el
index 025513e7a..55953e97b 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -7774,7 +7774,7 @@ the inserted text when done.
 
 When REMOVE is non-nil, remove the subtree from the clipboard."
   (interactive "P")
-  (setq tree (or tree (and kill-ring (current-kill 0))))
+  (setq tree (or tree (current-kill 0)))
   (unless (org-kill-is-subtree-p tree)
     (user-error
      (substitute-command-keys
-- 
2.25.1


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

* Re: [PATCH] [BUG] org.el: Fix first call of `org-paste-subtree'
  2021-11-29 12:02 [PATCH] [BUG] org.el: Fix first call of `org-paste-subtree' Max Nikulin
@ 2022-09-17 11:07 ` Max Nikulin
  2022-09-20 13:16 ` Ihor Radchenko
  1 sibling, 0 replies; 7+ messages in thread
From: Max Nikulin @ 2022-09-17 11:07 UTC (permalink / raw)
  To: emacs-orgmode

On 29/11/2021 19:02, Max Nikulin wrote:
> =C-c C-x C-y= ~org-paste-subtree~ fails just after Emacs start.
> Maybe there are more similar issues.
> 
> - Start *new* instance of emacs.
> 
>      printf '%b' '* Heading\n\nbody\n' |
>        xclip -in -selection clipborad >/dev/null
> - Try =C-c C-x C-y= or [[elisp:(org-paste-subtree)]]
> 
>    + Actual result is the following message:
>      : user-error: The kill is not a (set of) tree(s).  Use ‘C-y’ to 
> yank anyway

This is a reminder about the pending patch. The bug is still present in 
the main branch.

> diff --git a/lisp/org.el b/lisp/org.el
> index 025513e7a..55953e97b 100644
> --- a/lisp/org.el
> +++ b/lisp/org.el
> @@ -7774,7 +7774,7 @@ the inserted text when done.
>  
>  When REMOVE is non-nil, remove the subtree from the clipboard."
>    (interactive "P")
> -  (setq tree (or tree (and kill-ring (current-kill 0))))

"and kill-ring" was added in the commit

63f95c5c4 Carsten Dominik, "Release 4.34" 2008-01-31 11:31:55 +0100

and might be related to

Philip Rooke. remember problem in 4.33? Wed, 24 May 2006 08:49:28 +0000
https://list.orgmode.org/loom.20060524T103500-160@post.gmane.org/T/#u

but I may trigger

   let*: Wrong type argument: char-or-string-p, nil

on the main branch using `org-capture' with the "* A\nkill: %c" template 
and empty kill ring and X primary selection and clipboard.  Either I can 
not figure out what was the actual problem or protection was added in 
unrelated code.

So I still believe that my patch fixes the issue, but there may be more 
bugs around.



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

* Re: [PATCH] [BUG] org.el: Fix first call of `org-paste-subtree'
  2021-11-29 12:02 [PATCH] [BUG] org.el: Fix first call of `org-paste-subtree' Max Nikulin
  2022-09-17 11:07 ` Max Nikulin
@ 2022-09-20 13:16 ` Ihor Radchenko
  2022-09-20 17:11   ` Max Nikulin
  1 sibling, 1 reply; 7+ messages in thread
From: Ihor Radchenko @ 2022-09-20 13:16 UTC (permalink / raw)
  To: Max Nikulin; +Cc: emacs-orgmode

Max Nikulin <manikulin@gmail.com> writes:

>  When REMOVE is non-nil, remove the subtree from the clipboard."
>    (interactive "P")
> -  (setq tree (or tree (and kill-ring (current-kill 0))))
> +  (setq tree (or tree (current-kill 0)))
>    (unless (org-kill-is-subtree-p tree)

The main problem the old code solves is working around user error when
kill-ring is empty. We do not really want to err in such cases; just
handle empty kill ring specially.

I agree that (and kill-ring ...) condition misses the system clipboard.
The proper way to handle this issue is explicitly catching "Kill ring is
empty" error thrown by `current-kill' (i.e. `condition-case').

We have 3 occurrences of (and kill-ring (current-kill 0)) constructs in
the code and may fix the problem either by replacing each instance with
`condition-case' or we may create a separate macro/function in org-macs
and use it.

-- 
Ihor Radchenko,
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


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

* Re: [PATCH] [BUG] org.el: Fix first call of `org-paste-subtree'
  2022-09-20 13:16 ` Ihor Radchenko
@ 2022-09-20 17:11   ` Max Nikulin
  2022-09-21  8:27     ` Ihor Radchenko
  0 siblings, 1 reply; 7+ messages in thread
From: Max Nikulin @ 2022-09-20 17:11 UTC (permalink / raw)
  To: emacs-orgmode

On 20/09/2022 20:16, Ihor Radchenko wrote:
> Max Nikulin writes:
> 
>>   When REMOVE is non-nil, remove the subtree from the clipboard."
>>     (interactive "P")
>> -  (setq tree (or tree (and kill-ring (current-kill 0))))
>> +  (setq tree (or tree (current-kill 0)))
>>     (unless (org-kill-is-subtree-p tree)
> 
> The main problem the old code solves is working around user error when
> kill-ring is empty. We do not really want to err in such cases; just
> handle empty kill ring specially.

 From my point of view "kill ring is empty" user error clearly describes 
what happens in such case, so I do not see any point to spit suggestion 
to try simple yank instead.

> I agree that (and kill-ring ...) condition misses the system clipboard.
> The proper way to handle this issue is explicitly catching "Kill ring is
> empty" error thrown by `current-kill' (i.e. `condition-case').

Why do you believe that just allowing to propagate this error is worse?

> We have 3 occurrences of (and kill-ring (current-kill 0)) constructs in
> the code and may fix the problem either by replacing each instance with
> `condition-case' or we may create a separate macro/function in org-macs
> and use it.

Other cases (such as the one at the end of `org-paste-subtree' to 
determine if yanked text should be folded) require more care.




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

* Re: [PATCH] [BUG] org.el: Fix first call of `org-paste-subtree'
  2022-09-20 17:11   ` Max Nikulin
@ 2022-09-21  8:27     ` Ihor Radchenko
  2022-11-01  1:45       ` Ihor Radchenko
  0 siblings, 1 reply; 7+ messages in thread
From: Ihor Radchenko @ 2022-09-21  8:27 UTC (permalink / raw)
  To: Max Nikulin; +Cc: emacs-orgmode

Max Nikulin <manikulin@gmail.com> writes:

>> The main problem the old code solves is working around user error when
>> kill-ring is empty. We do not really want to err in such cases; just
>> handle empty kill ring specially.
>
>  From my point of view "kill ring is empty" user error clearly describes 
> what happens in such case, so I do not see any point to spit suggestion 
> to try simple yank instead.

>> I agree that (and kill-ring ...) condition misses the system clipboard.
>> The proper way to handle this issue is explicitly catching "Kill ring is
>> empty" error thrown by `current-kill' (i.e. `condition-case').
>
> Why do you believe that just allowing to propagate this error is worse?

I agree with you for `org-paste-subtree', but not for
`org-kill-is-subtree-p' and `org-capture-fill-template'. The two latter
ones also use (and kill-ring ...).

>> We have 3 occurrences of (and kill-ring (current-kill 0)) constructs in
>> the code and may fix the problem either by replacing each instance with
>> `condition-case' or we may create a separate macro/function in org-macs
>> and use it.
>
> Other cases (such as the one at the end of `org-paste-subtree' to 
> determine if yanked text should be folded) require more care.

This particular case, kill-ring test appears to be unnecessary (see
snippet below). current-kill should return non-nil because otherwise
"The kill is not a (set of) tree(s)" error would be thrown earlier.

(when (and (not for-yank) ; in this case, org-yank will decide about folding
		  kill-ring
		  (equal org-subtree-clip (current-kill 0))
		  org-subtree-clip-folded)
         ;; The tree was folded before it was killed/copied
         (org-fold-subtree t))

The other piece (when remove (pop kill-ring)) is indeed trickier and we
may need to test (equal org-subtree-clip (current-kill 0)) like the
above.

-- 
Ihor Radchenko,
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


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

* Re: [PATCH] [BUG] org.el: Fix first call of `org-paste-subtree'
  2022-09-21  8:27     ` Ihor Radchenko
@ 2022-11-01  1:45       ` Ihor Radchenko
  2022-11-01 16:13         ` Max Nikulin
  0 siblings, 1 reply; 7+ messages in thread
From: Ihor Radchenko @ 2022-11-01  1:45 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: Max Nikulin, emacs-orgmode

Ihor Radchenko <yantar92@gmail.com> writes:

> Max Nikulin <manikulin@gmail.com> writes:
>
>>> The main problem the old code solves is working around user error when
>>> kill-ring is empty. We do not really want to err in such cases; just
>>> handle empty kill ring specially.
>>
>>  From my point of view "kill ring is empty" user error clearly describes 
>> what happens in such case, so I do not see any point to spit suggestion 
>> to try simple yank instead.
>
>>> I agree that (and kill-ring ...) condition misses the system clipboard.
>>> The proper way to handle this issue is explicitly catching "Kill ring is
>>> empty" error thrown by `current-kill' (i.e. `condition-case').

Since I have no objections to the original patch, applied now.
https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=57abbd4b928752f2770898b09bbac8126b9d0eb3

I also reviewed the usage of (current-kill 0) in the code and applied
two additional improvements:
https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=33cbb11d5a522c82dfd9730a0e028a9c3df11518
https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=5dc8ea0abee10a787160456f74672da6ff3c3b40

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


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

* Re: [PATCH] [BUG] org.el: Fix first call of `org-paste-subtree'
  2022-11-01  1:45       ` Ihor Radchenko
@ 2022-11-01 16:13         ` Max Nikulin
  0 siblings, 0 replies; 7+ messages in thread
From: Max Nikulin @ 2022-11-01 16:13 UTC (permalink / raw)
  To: emacs-orgmode

On 01/11/2022 08:45, Ihor Radchenko wrote:
> Since I have no objections to the original patch, applied now.
> https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=57abbd4b928752f2770898b09bbac8126b9d0eb3
> 
> I also reviewed the usage of (current-kill 0) in the code and applied
> two additional improvements:
> https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=33cbb11d5a522c82dfd9730a0e028a9c3df11518
> https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=5dc8ea0abee10a787160456f74672da6ff3c3b40

I hope, it does not break anything. I tried to trace possible call 
paths, but I have not managed to figure out was the original problem 
caused adding "(and kill-ring" to "(current-kill 0)".





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

end of thread, other threads:[~2022-11-01 17:10 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-11-29 12:02 [PATCH] [BUG] org.el: Fix first call of `org-paste-subtree' Max Nikulin
2022-09-17 11:07 ` Max Nikulin
2022-09-20 13:16 ` Ihor Radchenko
2022-09-20 17:11   ` Max Nikulin
2022-09-21  8:27     ` Ihor Radchenko
2022-11-01  1:45       ` Ihor Radchenko
2022-11-01 16:13         ` Max Nikulin

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