emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Modifying the parse tree
@ 2023-05-25 10:22 bvchgvbt
  2023-05-25 10:55 ` Ihor Radchenko
  0 siblings, 1 reply; 6+ messages in thread
From: bvchgvbt @ 2023-05-25 10:22 UTC (permalink / raw)
  To: orgmode ml

After calling, say, org-element-adopt-elements, do I (or rather my code)
have to do anything to make the modified tree apply to the Org document/
buffer that the tree came from?

I can see that the tree is modified but it isn't reflected in the buffer
it came from, and furthermore calling org-element-interpret-data on the
modified tree at that stage results in an error:

"org-element-headline-interpreter: Wrong type argument: wholenump, nil"

so I'm guessing that, actually, something is wrong with the tree at that
point, which may explain why no changes are available in the buffer if
they should just show up automagically.


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

* Re: Modifying the parse tree
  2023-05-25 10:22 Modifying the parse tree bvchgvbt
@ 2023-05-25 10:55 ` Ihor Radchenko
  2023-05-25 11:15   ` bvchgvbt
  0 siblings, 1 reply; 6+ messages in thread
From: Ihor Radchenko @ 2023-05-25 10:55 UTC (permalink / raw)
  To: bvchgvbt; +Cc: orgmode ml

bvchgvbt@mail.com writes:

> After calling, say, org-element-adopt-elements, do I (or rather my code)
> have to do anything to make the modified tree apply to the Org document/
> buffer that the tree came from?

Yes. Parse tree is not kept in sync with the original buffer when you
retrieve it by `org-element-parse-buffer'. We generally do not currently
have a way to modify Org buffer text from AST.

The only way to write the parse tree back is
replacing it with the output `org-element-interpret-data'. But be aware
that interpreted tree and the original text do not have exact 1-to-1
equivalence. Some whitespace might be lost.

> I can see that the tree is modified but it isn't reflected in the buffer
> it came from, and furthermore calling org-element-interpret-data on the
> modified tree at that stage results in an error:
>
> "org-element-headline-interpreter: Wrong type argument: wholenump, nil"

Which implies that you added invalid headline element to the tree.

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

* Re: Modifying the parse tree
  2023-05-25 10:55 ` Ihor Radchenko
@ 2023-05-25 11:15   ` bvchgvbt
  2023-05-25 11:29     ` Ihor Radchenko
  0 siblings, 1 reply; 6+ messages in thread
From: bvchgvbt @ 2023-05-25 11:15 UTC (permalink / raw)
  To: orgmode ml

"Ihor Radchenko" wrote:
> bvchgvbt@mail.com writes:

> Parse tree is not kept in sync with the original buffer when you
> retrieve it by `org-element-parse-buffer'.

Okay, thanks. I thought that might be the case, but couldn't be sure.

> We generally do not currently
> have a way to modify Org buffer text from AST.
>
> The only way to write the parse tree back is
> replacing it with the output `org-element-interpret-data'.

Okay, fair enough. Thanks for the info.

> But be aware
> that interpreted tree and the original text do not have exact 1-to-1
> equivalence. Some whitespace might be lost.

Only whitespace? I can live with that.

> > "org-element-headline-interpreter: Wrong type argument: wholenump, nil"
>
> Which implies that you added invalid headline element to the tree.

Hmm. Does
headline (:title hello :todo-keyword TODO :todo-type todo)
look valid to you as a minimal TODO-type headline? Created via:

  (let ((todo (org-element-create 'headline)))
    (org-element-put-property todo :title todo-title)
    (org-element-put-property todo :todo-keyword "TODO")
    (org-element-put-property todo :todo-type "todo"))



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

* Re: Modifying the parse tree
  2023-05-25 11:15   ` bvchgvbt
@ 2023-05-25 11:29     ` Ihor Radchenko
  2023-05-26 11:27       ` bvchgvbt
  0 siblings, 1 reply; 6+ messages in thread
From: Ihor Radchenko @ 2023-05-25 11:29 UTC (permalink / raw)
  To: bvchgvbt; +Cc: orgmode ml

bvchgvbt@mail.com writes:

>> But be aware
>> that interpreted tree and the original text do not have exact 1-to-1
>> equivalence. Some whitespace might be lost.
>
> Only whitespace? I can live with that.

I know for sure about whitespace. We have some tests for interpreter,
but this has never been tested thoughtfully for the use cases like yours.
Should be mostly fine though, except some edge cases that you can report
as bugs. The original aim of `org-element-interpret-data' is creating
equivalence between the parse tree and the buffer text.

>> > "org-element-headline-interpreter: Wrong type argument: wholenump, nil"
>>
>> Which implies that you added invalid headline element to the tree.
>
> Hmm. Does
> headline (:title hello :todo-keyword TODO :todo-type todo)
> look valid to you as a minimal TODO-type headline? Created via:
>
>   (let ((todo (org-element-create 'headline)))
>     (org-element-put-property todo :title todo-title)
>     (org-element-put-property todo :todo-keyword "TODO")
>     (org-element-put-property todo :todo-type "todo"))

Yes, it is invalid.  You must always have :level.
You might try https://github.com/ndwarshuis/org-ml, though I plan some
breaking changes affecting org-ml soon.

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

* Re: Modifying the parse tree
  2023-05-25 11:29     ` Ihor Radchenko
@ 2023-05-26 11:27       ` bvchgvbt
  2023-05-26 14:35         ` Ihor Radchenko
  0 siblings, 1 reply; 6+ messages in thread
From: bvchgvbt @ 2023-05-26 11:27 UTC (permalink / raw)
  To: orgmode ml

> "Ihor Radchenko" wrote:

> >> Which implies that you added invalid headline element to the tree.
> >
> > Hmm. Does
> > headline (:title hello :todo-keyword TODO :todo-type todo)
> > look valid to you as a minimal TODO-type headline? Created via:
> >
> >   (let ((todo (org-element-create 'headline)))
> >     (org-element-put-property todo :title todo-title)
> >     (org-element-put-property todo :todo-keyword "TODO")
> >     (org-element-put-property todo :todo-type "todo"))
>
> Yes, it is invalid.  You must always have :level.

Ah! Thank you! Is there some documentation that explains which properties
each element must have in order to be valid, please?

For example I'm now trying to create a deadline for the heading, and it's
also not working.

> You might try https://github.com/ndwarshuis/org-ml, though I plan some
> breaking changes affecting org-ml soon.

Thanks, I'll look into that. From first glance it looks useful. I'll
probaby continue as I am until I get something that works, and then maybe
refactor to use org-ml.


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

* Re: Modifying the parse tree
  2023-05-26 11:27       ` bvchgvbt
@ 2023-05-26 14:35         ` Ihor Radchenko
  0 siblings, 0 replies; 6+ messages in thread
From: Ihor Radchenko @ 2023-05-26 14:35 UTC (permalink / raw)
  To: bvchgvbt; +Cc: orgmode ml

bvchgvbt@mail.com writes:

>> Yes, it is invalid.  You must always have :level.
>
> Ah! Thank you! Is there some documentation that explains which properties
> each element must have in order to be valid, please?

Just https://orgmode.org/worg/dev/org-element-api.html
And source code, of course.

> For example I'm now trying to create a deadline for the heading, and it's
> also not working.

That's because deadline is not a part of the heading object. It is just
kept there as property for convenience.

You need to explicitly create
 (heading (...) (section (...) (planning ...) ...))
AST to add a deadline.

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

end of thread, other threads:[~2023-05-26 14:32 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-25 10:22 Modifying the parse tree bvchgvbt
2023-05-25 10:55 ` Ihor Radchenko
2023-05-25 11:15   ` bvchgvbt
2023-05-25 11:29     ` Ihor Radchenko
2023-05-26 11:27       ` bvchgvbt
2023-05-26 14:35         ` 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).