Function: diary-icalendar-export-region

diary-icalendar-export-region is an autoloaded, interactive and byte-compiled function defined in diary-icalendar.el.gz.

Signature

(diary-icalendar-export-region BEGIN END FILENAME &optional ERASE)

Documentation

Export diary entries between BEGIN and END to iCalendar format in FILENAME.

If FILENAME exists and is not empty, this function asks whether to erase its contents first. If ERASE is non-nil, the contents of FILENAME will always be erased without asking. Otherwise the exported data will be appended to the end of FILENAME.

The export depends on a number of user-customizable variables. Before running this command for the first time, you may especially wish to check the values of: diary-file diary-date-forms calendar-date-style as well as variables in the customization group diary-icalendar-export.

View in manual

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/calendar/diary-icalendar.el.gz
;;;###autoload
(defun di:export-region (begin end filename &optional erase)
  "Export diary entries between BEGIN and END to iCalendar format in FILENAME.

If FILENAME exists and is not empty, this function asks whether to erase
its contents first.  If ERASE is non-nil, the contents of FILENAME will
always be erased without asking.  Otherwise the exported data will be
appended to the end of FILENAME.

The export depends on a number of user-customizable variables.  Before
running this command for the first time, you may especially wish to
check the values of:
`diary-file'
`diary-date-forms'
`calendar-date-style'
as well as variables in the customization group `diary-icalendar-export'."
  (interactive (list (region-beginning)
                     (region-end)
                     (expand-file-name
                      (read-file-name "iCalendar file: "))))

  (ical:init-error-buffer)
  (let (output-buffer local-tz components vcalendar)
    (when (and (null erase)
               (file-exists-p filename)
               (< 0 (file-attribute-size (file-attributes filename)))
               (y-or-n-p (format "Delete existing contents of %s?" filename)))
      (setq erase t))
    (ical:condition-case err
      (setq output-buffer (find-file-noselect filename)))
    (when output-buffer
      (save-excursion
        (save-restriction
          (narrow-to-region begin end)
          (goto-char (point-min))
          (cond ((listp di:time-zone-export-strategy)
                 (setq local-tz (di:current-tz-to-vtimezone
                                 di:time-zone-export-strategy)))
                ((and (eq 'local di:time-zone-export-strategy)
                      (not (di:-tz-is-utc-p))) ; don't generate a VTIMEZONE in UTC
                 (setq local-tz (di:current-tz-to-vtimezone))))
          (while (re-search-forward di:entry-regexp nil t)
            (let ((entry-start (match-beginning 0))
                  (entry-end (match-end 0))
                  (first-line (match-string 1)))
              (ical:condition-case err-data
                 (setq components
                       (append (di:parse-entry entry-start entry-end local-tz)
                               components))
                (ical:export-error
                 (ical:warn
                  (concat
                   (format "Unable to export entry \"%s...\"; skipping" first-line)
                   "\nError was:\n"
                   (plist-get err-data :message))
                  :position entry-start
                  :buffer (current-buffer))))
              (goto-char (1+ entry-end))))
          (setq components (nreverse components))
          (when (ical:vtimezone-component-p local-tz)
            (push local-tz components))
          (ical:condition-case err-data
             (setq vcalendar (ical:make-vcalendar (@ components))))

          (when vcalendar
            (with-current-buffer output-buffer
              (when erase (erase-buffer))
              (goto-char (point-max)) ; append, if user chose not to erase
              (unless (bolp) (insert "\n"))
              (ical:condition-case err-data
                 (insert (ical:print-calendar-node vcalendar)))
              (let ((coding-system-for-write 'utf-8-dos))
                (save-buffer))))))))

  (message
   (if (ical:errors-p)
       (format "iCalendar export completed with errors; see buffer %s"
               (buffer-name (ical:error-buffer)))
     "iCalendar export completed successfully.")))