all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Richard Lawrence <rwl@recursewithless.net>
To: Ihor Radchenko <yantar92@posteo.net>
Cc: emacs-devel@gnu.org
Subject: Re: Upstreaming org-element-ast (was: Improving Emacs' iCalendar support)
Date: Wed, 01 Jan 2025 13:38:05 +0100	[thread overview]
Message-ID: <87seq290b6.fsf@recursewithless.net> (raw)
In-Reply-To: <87r05mg9ve.fsf@localhost>

Ihor Radchenko <yantar92@posteo.net> writes:
         
>> 2) I'm finding org-element-create rather unintuitive to work with, I
>>    think because of its &rest argument for children and the handling of
>>    "anonymous" nodes.
>> ...
>>    As it is, I can't figure out how to call org-element-create and
>>    org-element-contents in the right way to set the contents of a node
>>    to a list of other nodes, and return that list from
>>    org-element-contents.
>
> Should be just
> (org-element-create type props contents)
> and then
> (org-element-contents new-node)

That's what I would have thought. In my experiment, I have done this:

(defun ical:make-ast-node (type &optional props &rest children)
  ;; automatically mark :value as a seconary property for org-element-ast:
  (let ((full-props (plist-put props :secondary (list :value))))
        (org-element-create type full-props children)))

(so basically just aliasing org-element-create) and

(defalias 'ical:ast-node-children #'org-element-contents)

I also changed all my calls to ical:make-ast-node to reflect the calling
convention of org-element-create, e.g. in ical:read-property-value I now
have

(ical:make-ast-node type
                    (list :value value :original-value unrecognized-val)
                    params)

The calls to ical:ast-node-children shouldn't need adjusting, since both
the old version and org-element-set-contents just take a single
argument, the syntax node.

But when I run my parser tests with the above changes, they (almost) all
fail with errors like this:

Test string:
"ATTENDEE;RSVP=TRUE;ROLE=REQ-PARTICIPANT:mailto:jsmith@example.com\n"

Error:
    (icalendar-validation-error
     "`icalendar-attendee' may not contain `nil'"
     (icalendar-attendee
      (:standard-properties
       [1 nil nil nil 66 nil (:value) nil nil nil ...] :value
       (icalendar-cal-address
        (:standard-properties
         [nil nil nil nil nil nil ... nil nil nil ...] :value
         "mailto:jsmith@example.com")
        nil)
       :original-value nil :value-begin 41 :value-end 66)
      ((icalendar-rsvpparam
        (:standard-properties [10 nil nil nil 19 nil ... nil nil nil ...]
                              :value (icalendar-boolean ... nil)
                              :original-value nil :value-begin 15
                              :value-end 19)
        nil)
       (icalendar-roleparam
        (:standard-properties [20 nil nil nil 40 nil ... nil nil nil ...]
                              :value "REQ-PARTICIPANT" :original-value nil
                              :value-begin 25 :value-end 40)
        nil))))

And probing with the debugger reveals e.g. that (icalendar-node-children node)
returns in this example:

(((icalendar-rsvpparam
   (:standard-properties
    [10 nil nil nil 19 nil (:value) nil nil nil nil nil nil nil
        #<buffer *iCalendar Parse Test*> nil nil nil]
    :value
    (icalendar-boolean
     (:standard-properties
      [nil nil nil nil nil nil (:value) nil nil nil nil nil nil nil
           nil nil nil nil]
      :value t)
     nil)
    :original-value nil :value-begin 15 :value-end 19)
   nil)
  (icalendar-roleparam
   (:standard-properties
    [20 nil nil nil 40 nil (:value) nil nil nil nil nil nil nil
        #<buffer *iCalendar Parse Test*> nil nil nil]
    :value "REQ-PARTICIPANT" :original-value nil :value-begin 25
    :value-end 40)
   nil)))

That is, the list of parameter nodes is wrapped in an extra list, which
causes my function which counts children by type to fail and signal the
error.

Initially I thought that this is because org-element-contents is
implemented in the relevant case as (nthcdr 2 node) rather than e.g.
(nth 2 node). Changing the definition of ical:ast-node-children from the
above alias to

(defun ical:ast-node-children (node)
  (nth 2 node))

helps -- more tests pass -- but I'm not sure why this change should be
necessary. (I feel like I'm missing something really obvious here but so
far it eludes me!)

Even with this change, most tests still fail with an error like the
above, because an *empty* list of children is still returned as (nil)
rather than nil. The problem here is apparently that I am passing nil as
an explicit &rest argument to org-element-create; if I then change the
definition of ical:make-ast-node to work around this, e.g.

(defun ical:make-ast-node (type &optional props &rest children)
  ;; automatically mark :value as a seconary property for org-element-ast:
  (let ((full-props (plist-put props :secondary (list :value))))
    (if (equal children '(nil))
        (org-element-create type full-props)
      (org-element-create type full-props children))))

then all the tests pass again. But this is where I find the calling
convention of org-element-create unintuitive and would prefer an
interface where I can just always pass a list of child nodes, including
an empty list, and org-element-contents will always give me exactly that
list back.

This got a bit long, but I hope it's detailed enough to make things clear!

Best,
Richard




  reply	other threads:[~2025-01-01 12:38 UTC|newest]

Thread overview: 58+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-18  9:01 Improving Emacs' iCalendar support Richard Lawrence
2024-10-18  9:24 ` Eli Zaretskii
2024-10-19  8:22   ` Richard Lawrence
2024-10-19 10:11     ` Eli Zaretskii
2024-10-20  5:08       ` Richard Lawrence
2024-10-20  5:21         ` Eli Zaretskii
2024-10-18 12:58 ` Sebastián Monía
2024-10-19  8:28   ` Richard Lawrence
2024-10-19  5:44 ` Adam Porter
2024-10-19  8:39   ` Richard Lawrence
2024-10-20  3:09     ` Adam Porter
2024-10-22 18:40     ` Ihor Radchenko
2024-10-23  7:29       ` Björn Bidar
     [not found]       ` <87plnr6zvk.fsf@>
2024-10-23  8:09         ` Ihor Radchenko
2024-10-23  9:05         ` Richard Lawrence
2024-10-23 10:03           ` Björn Bidar
2024-10-23  9:01       ` Richard Lawrence
2024-10-23 20:24         ` Ferdinand Pieper
2024-10-24 14:52           ` Richard Lawrence
2024-10-24 17:45             ` Ihor Radchenko
2024-10-25 12:53               ` Richard Lawrence
2024-10-25 18:59                 ` Ihor Radchenko
2024-10-21  6:10 ` Björn Bidar
2024-10-21  6:22 ` Visuwesh
2024-10-23  9:15   ` Richard Lawrence
2024-10-23  9:45     ` Visuwesh
     [not found] ` <87wmi29eaf.fsf@>
2024-10-26 17:49   ` Ihor Radchenko
2024-10-28 10:44     ` Björn Bidar
     [not found]     ` <87r080tslf.fsf@>
2024-10-28 19:09       ` Ihor Radchenko
2024-11-23  8:44 ` Richard Lawrence
2024-11-24  9:50   ` Eli Zaretskii
2024-11-24 10:21     ` Ihor Radchenko
2024-11-24 10:41       ` Eli Zaretskii
2024-11-24 10:58         ` Ihor Radchenko
2024-11-24 11:08           ` Eli Zaretskii
2024-11-24 11:21             ` Upstreaming org-element-ast (was: Improving Emacs' iCalendar support) Ihor Radchenko
2024-11-24 12:55               ` Eli Zaretskii
2024-12-25 11:31                 ` Ihor Radchenko
2024-12-29 20:19                   ` Richard Lawrence
2024-12-29 20:53                     ` Richard Lawrence
2024-12-30 17:18                       ` bug#74994: " Ihor Radchenko
2024-12-30 17:18                       ` Ihor Radchenko
2024-12-29 20:53                     ` bug#74994: " Richard Lawrence
2024-12-30 17:16                     ` Ihor Radchenko
2024-12-31  7:55                       ` Richard Lawrence
2025-01-01  9:29                         ` Ihor Radchenko
2025-01-01 12:38                           ` Richard Lawrence [this message]
2025-01-02 18:03                             ` Ihor Radchenko
2025-01-02 20:01                               ` Richard Lawrence
2025-01-02 20:09                                 ` Ihor Radchenko
2025-01-01 13:01                           ` Richard Lawrence
2024-12-29 20:19                   ` bug#74994: " Richard Lawrence
2024-11-25  9:09     ` Improving Emacs' iCalendar support Richard Lawrence
2024-11-25 12:42       ` Eli Zaretskii
2024-11-25 13:03         ` Rudolf Schlatte
2024-11-25 13:36           ` Eli Zaretskii
2024-11-25 17:14           ` Richard Lawrence
2024-12-20 13:21 ` Richard Lawrence

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87seq290b6.fsf@recursewithless.net \
    --to=rwl@recursewithless.net \
    --cc=emacs-devel@gnu.org \
    --cc=yantar92@posteo.net \
    /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 external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.