Function: icalendar-maybe-add-value-param

icalendar-maybe-add-value-param is a byte-compiled function defined in icalendar-parser.el.gz.

Signature

(icalendar-maybe-add-value-param PROPERTY-NODE)

Documentation

Add a VALUE parameter to PROPERTY-NODE if necessary.

If the type of PROPERTY-NODE's value is not the same as its default-type, check that its parameter list contains an icalendar-valuetypeparam specifying that type as the type for the value. If not, add such a parameter to PROPERTY-NODE's list of parameters. Returns the possibly-modified PROPERTY-NODE.

If the parameter list already contains a value type parameter for a type other than the property value's type, an icalendar-validation-error is signaled.

If PROPERTY's value is a list, the type of the first element will be assumed to be the type for all the values in the list. If the list is empty, no change will be made to PROPERTY's parameters.

Source Code

;; Defined in /usr/src/emacs/lisp/calendar/icalendar-parser.el.gz
(defun ical:maybe-add-value-param (property-node)
  "Add a VALUE parameter to PROPERTY-NODE if necessary.

If the type of PROPERTY-NODE's value is not the same as its
default-type, check that its parameter list contains an
`icalendar-valuetypeparam' specifying that type as the type for
the value.  If not, add such a parameter to PROPERTY-NODE's list
of parameters.  Returns the possibly-modified PROPERTY-NODE.

If the parameter list already contains a value type parameter for
a type other than the property value's type, an
`icalendar-validation-error' is signaled.

If PROPERTY's value is a list, the type of the first element will
be assumed to be the type for all the values in the list.  If the
list is empty, no change will be made to PROPERTY's parameters."
  (catch 'no-value-type
    (let* ((property-type (ical:ast-node-type property-node))
           (value/s (ical:ast-node-value property-node))
           (value (if (and (ical:expects-list-of-values-p property-type)
                           (listp value/s))
                      (car value/s)
                    value/s))
           (value-type (cond ((stringp value) 'ical:text)
                             ((ical:ast-node-p value)
                              (ical:ast-node-type value))
                             ;; if we can't determine a type from the value, bail:
                             (t (throw 'no-value-type property-node))))
           (params (ical:ast-node-children property-node))
           (expected-type (ical:value-type-from-params params)))

      (when (not (eq value-type (get property-type 'ical:default-type)))
        (if expected-type
            (when (not (eq value-type expected-type))
              (ical:signal-validation-error
                (format (concat "Mismatching VALUE parameter.  VALUE specifies %s "
                                "but property value has type %s")
                        expected-type value-type)
                :node property-node))
          ;; the value isn't of the default type, but we didn't find a
          ;; VALUE parameter, so add one now:
          (let* ((valuetype-param
                  (ical:make-ast-node 'ical:valuetypeparam
                                      (list :value (ical:make-ast-node
                                                    'ical:printed-value-type
                                                    (list :value value-type)))))
                 (new-params (cons valuetype-param
                                   (ical:ast-node-children property-node))))
            (apply #'ical:ast-node-set-children property-node new-params))))

      ;; Return the modified property node:
      property-node)))