emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Ihor Radchenko <yantar92@posteo.net>
To: Antonio Romano <n58r@pm.me>
Cc: emacs-orgmode@gnu.org
Subject: Re: Should "org-element-parse-buffer" check whether the buffer's major mode is org mode?
Date: Wed, 04 Sep 2024 19:43:17 +0000	[thread overview]
Message-ID: <87r09zkxwq.fsf@localhost> (raw)
In-Reply-To: <1b2b8c2791e628f6a41ef533e49a5ac9e3dee8a4.camel@pm.me>

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>


  reply	other threads:[~2024-09-04 19:43 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2024-09-04 21:29   ` Antonio Romano
2024-09-22 16:37     ` 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=87r09zkxwq.fsf@localhost \
    --to=yantar92@posteo.net \
    --cc=emacs-orgmode@gnu.org \
    --cc=n58r@pm.me \
    /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).