emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Should "org-element-parse-buffer" check whether the buffer's major mode is org mode?
@ 2024-09-02  9:51 Antonio Romano
  2024-09-04 19:43 ` Ihor Radchenko
  0 siblings, 1 reply; 5+ messages in thread
From: Antonio Romano @ 2024-09-02  9:51 UTC (permalink / raw)
  To: emacs-orgmode

Hi all,

yesterday I've sent a question on Emacs StackExchange 
(https://emacs.stackexchange.com/questions/82063) where I described a
small issue I had encountered while using Org Element API. 

What I did was simply parse out a buffer, change all headlines's TODO
keyword in its AST to "DONE", interpret the resulting AST and finally
print out the resulting document to a new buffer. The issue I had
encountered was that an headline like "* TODO Hello" would wrongly
become "* DONE TODO Hello" instead of "* DONE Hello".

A kind user pointed out that the issue was that I called "org-element-
parse-buffer" in a fundamental-mode buffer and, in fact, turning on
org-mode beforehand solved the issue. As the user suggested, I'm here
to ask you if this is the intended behavior or if it would be better
limiting the usage of the function to org-mode (and derived) buffers
only. Thank you in advance.



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

* Re: Should "org-element-parse-buffer" check whether the buffer's major mode is org mode?
  2024-09-02  9:51 Should "org-element-parse-buffer" check whether the buffer's major mode is org mode? Antonio Romano
@ 2024-09-04 19:43 ` Ihor Radchenko
  2024-09-04 21:29   ` Antonio Romano
  0 siblings, 1 reply; 5+ messages in thread
From: Ihor Radchenko @ 2024-09-04 19:43 UTC (permalink / raw)
  To: Antonio Romano; +Cc: emacs-orgmode

Antonio Romano <n58r@pm.me> writes:

> What I did was simply parse out a buffer, change all headlines's TODO
> keyword in its AST to "DONE", interpret the resulting AST and finally
> print out the resulting document to a new buffer. The issue I had
> encountered was that an headline like "* TODO Hello" would wrongly
> become "* DONE TODO Hello" instead of "* DONE Hello".

That might happen, yes. The behavior in non-Org buffers is currently
undefined. Sometimes, you may also get errors.

> A kind user pointed out that the issue was that I called "org-element-
> parse-buffer" in a fundamental-mode buffer and, in fact, turning on
> org-mode beforehand solved the issue. As the user suggested, I'm here
> to ask you if this is the intended behavior or if it would be better
> limiting the usage of the function to org-mode (and derived) buffers
> only. Thank you in advance.

If you look into the docstring, it says

    ...
    This function assumes that current major mode is `org-mode'.

Should we complain when current buffer is not in Org mode? It is not
easy to answer. In Org 9.7 (before the release), we modified
`org-element-at-point' to do exactly this - complain when current major
mode is not Org mode. Soon, a number of people reported that
`org-element-at-point' is, in fact, misused by a number of packages and
does get called in non-Org buffers. At the end, I had to change
`org-element-at-point' to throw a warning rather than an error as some
packages simply _rely_ upon such misuse.

The current tentative plan for this is the following:

1. Slowly update the parser so that using it in non-Org buffers becomes
   less unreliable. (It mostly involves computing some variables that
   Org mode normally initializes dynamically)

2. Allow the parser to be used on arbitrary text.


So, you should be able to use `org-element-parse-buffer' in non-Org
buffers in the long run. For now, a note in the docstring is what we
have. I may also add a warning, but I am not sure - a number of people
are confused enough by the warning from `org-element-at-point' (caused
by packages abusing it in non-Org buffers); to the point that I am
thinking to remove it as well.

-- 
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] 5+ messages in thread

* Re: Should "org-element-parse-buffer" check whether the buffer's major mode is org mode?
  2024-09-04 19:43 ` Ihor Radchenko
@ 2024-09-04 21:29   ` Antonio Romano
  2024-09-22 16:37     ` Ihor Radchenko
  0 siblings, 1 reply; 5+ messages in thread
From: Antonio Romano @ 2024-09-04 21:29 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: emacs-orgmode

Ouch, that part of the docstring totally slipped my eye. 

The situation is a bit more complex than I imagined and am not
experienced enough to have a strong opinion on the matter. If things
are to stay as they are right now, I guess emphasizing even more in the
docstring that this function may have undefined behavior in different
modes can only do good. 

Thank you for clarifying.



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

* Re: Should "org-element-parse-buffer" check whether the buffer's major mode is org mode?
  2024-09-04 21:29   ` Antonio Romano
@ 2024-09-22 16:37     ` Ihor Radchenko
  2024-10-26 17:34       ` Ihor Radchenko
  0 siblings, 1 reply; 5+ messages in thread
From: Ihor Radchenko @ 2024-09-22 16:37 UTC (permalink / raw)
  To: Antonio Romano; +Cc: emacs-orgmode

Antonio Romano <n58r@pm.me> writes:

> The situation is a bit more complex than I imagined and am not
> experienced enough to have a strong opinion on the matter. If things
> are to stay as they are right now, I guess emphasizing even more in the
> docstring that this function may have undefined behavior in different
> modes can only do good. 

Would you be interested to submit a patch to that effect?
See https://orgmode.org/worg/org-contribute.html#first-patch

-- 
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] 5+ messages in thread

* Re: Should "org-element-parse-buffer" check whether the buffer's major mode is org mode?
  2024-09-22 16:37     ` Ihor Radchenko
@ 2024-10-26 17:34       ` Ihor Radchenko
  0 siblings, 0 replies; 5+ messages in thread
From: Ihor Radchenko @ 2024-10-26 17:34 UTC (permalink / raw)
  To: Antonio Romano; +Cc: emacs-orgmode

Ihor Radchenko <yantar92@posteo.net> writes:

> Antonio Romano <n58r@pm.me> writes:
>
>> The situation is a bit more complex than I imagined and am not
>> experienced enough to have a strong opinion on the matter. If things
>> are to stay as they are right now, I guess emphasizing even more in the
>> docstring that this function may have undefined behavior in different
>> modes can only do good. 

Done, on bugfix.
https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=377741dc5e

-- 
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] 5+ messages in thread

end of thread, other threads:[~2024-10-26 17:33 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-09-02  9:51 Should "org-element-parse-buffer" check whether the buffer's major mode is org mode? Antonio Romano
2024-09-04 19:43 ` Ihor Radchenko
2024-09-04 21:29   ` Antonio Romano
2024-09-22 16:37     ` Ihor Radchenko
2024-10-26 17:34       ` 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).