1 ;;; icalendar.el --- iCalendar implementation
3 ;; Copyright (C) 2002-2016 Free Software Foundation, Inc.
5 ;; Author: Ulf Jasper <ulf.jasper@web.de>
6 ;; Created: August 2002
8 ;; Human-Keywords: calendar, diary, iCalendar, vCalendar
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; This package is documented in the Emacs Manual.
31 ;; - Diary entries which have a start time but no end time are assumed to
32 ;; last for one hour when they are exported.
33 ;; - Weekly diary entries are assumed to occur the first time in the first
34 ;; week of the year 2000 when they are exported.
35 ;; - Yearly diary entries are assumed to occur the first time in the year
36 ;; 1900 when they are exported.
37 ;; - Float diary entries are assumed to occur the first time on the
38 ;; day when they are exported.
42 ;; 0.07 onwards: see commit logs and ../ChangeLog*.
45 ;; - Bugfixes regarding icalendar-import-format-*.
46 ;; - Fix in icalendar-convert-diary-to-ical -- thanks to Philipp Grau.
49 ;; - New import format scheme: Replaced icalendar-import-prefix-*,
50 ;; icalendar-import-ignored-properties, and
51 ;; icalendar-import-separator with icalendar-import-format(-*).
52 ;; - icalendar-import-file and icalendar-convert-diary-to-ical
53 ;; have an extra parameter which should prevent them from
54 ;; erasing their target files (untested!).
55 ;; - Tested with Emacs 21.3.2
58 ;; - Bugfix: import: double quoted param values did not work
59 ;; - Read DURATION property when importing.
60 ;; - Added parameter icalendar-duration-correction.
63 ;; - Export takes care of european-calendar-style.
64 ;; - Tested with Emacs 21.3.2 and XEmacs 21.4.12
67 ;; - Should work in XEmacs now. Thanks to Len Trigg for the XEmacs patches!
68 ;; - Added exporting from Emacs diary to ical.
69 ;; - Some bugfixes, after testing with calendars from http://icalshare.com.
70 ;; - Tested with Emacs 21.3.2 and XEmacs 21.4.12
73 ;; - First published version. Trial version. Alpha version.
75 ;; ======================================================================
78 ;; * Import from ical to diary:
79 ;; + Need more properties for icalendar-import-format
80 ;; (added all that Mozilla Calendar uses)
81 ;; From iCal specifications (RFC2445: 4.8.1), icalendar.el lacks
82 ;; ATTACH, CATEGORIES, COMMENT, GEO, PERCENT-COMPLETE (VTODO),
83 ;; PRIORITY, RESOURCES) not considering date/time and time-zone
84 ;; + check vcalendar version
85 ;; + check (unknown) elements
86 ;; + recurring events!
87 ;; + works for european style calendars only! Does it?
89 ;; + exceptions in recurring events
90 ;; + the parser is too soft
91 ;; + error log is incomplete
92 ;; + nice to have: #include "webcal://foo.com/some-calendar.ics"
93 ;; + timezones probably still need some improvements.
95 ;; * Export from diary to ical
96 ;; + diary-date, diary-float, and self-made sexp entries are not
100 ;; + clean up all those date/time parsing functions
101 ;; + Handle todo items?
102 ;; + Check iso 8601 for datetime and period
103 ;; + Which chars to (un)escape?
108 (defconst icalendar-version
"0.19"
109 "Version number of icalendar.el.")
111 ;; ======================================================================
113 ;; ======================================================================
114 (defgroup icalendar nil
119 (defcustom icalendar-import-format
121 "Format for importing events from iCalendar into Emacs diary.
122 It defines how iCalendar events are inserted into diary file.
123 This may either be a string or a function.
125 In case of a formatting STRING the following specifiers can be used:
126 %c Class, see `icalendar-import-format-class'
127 %d Description, see `icalendar-import-format-description'
128 %l Location, see `icalendar-import-format-location'
129 %o Organizer, see `icalendar-import-format-organizer'
130 %s Summary, see `icalendar-import-format-summary'
131 %t Status, see `icalendar-import-format-status'
132 %u URL, see `icalendar-import-format-url'
133 %U UID, see `icalendar-import-format-uid'
135 A formatting FUNCTION will be called with a VEVENT as its only
136 argument. It must return a string. See
137 `icalendar-import-format-sample' for an example."
139 (string :tag
"String")
140 (function :tag
"Function"))
143 (defcustom icalendar-import-format-summary
145 "Format string defining how the summary element is formatted.
146 This applies only if the summary is not empty! `%s' is replaced
151 (defcustom icalendar-import-format-description
153 "Format string defining how the description element is formatted.
154 This applies only if the description is not empty! `%s' is
155 replaced by the description."
159 (defcustom icalendar-import-format-location
161 "Format string defining how the location element is formatted.
162 This applies only if the location is not empty! `%s' is replaced
167 (defcustom icalendar-import-format-organizer
169 "Format string defining how the organizer element is formatted.
170 This applies only if the organizer is not empty! `%s' is
171 replaced by the organizer."
175 (defcustom icalendar-import-format-url
177 "Format string defining how the URL element is formatted.
178 This applies only if the URL is not empty! `%s' is replaced by
183 (defcustom icalendar-import-format-uid
185 "Format string defining how the UID element is formatted.
186 This applies only if the UID is not empty! `%s' is replaced by
192 (defcustom icalendar-import-format-status
194 "Format string defining how the status element is formatted.
195 This applies only if the status is not empty! `%s' is replaced by
200 (defcustom icalendar-import-format-class
202 "Format string defining how the class element is formatted.
203 This applies only if the class is not empty! `%s' is replaced by
208 (defcustom icalendar-recurring-start-year
210 "Start year for recurring events.
211 Some calendar browsers only propagate recurring events for
212 several years beyond the start time. Set this string to a year
213 just before the start of your personal calendar."
217 (defcustom icalendar-export-hidden-diary-entries
219 "Determines whether hidden diary entries are exported.
220 If non-nil hidden diary entries (starting with `&') get exported,
221 if nil they are ignored."
225 (defcustom icalendar-uid-format
227 "Format of unique ID code (UID) for each iCalendar object.
228 The following specifiers are available:
229 %c COUNTER, an integer value that is increased each time a uid is
230 generated. This may be necessary for systems which do not
231 provide time-resolution finer than a second.
232 %h HASH, a hash value of the diary entry,
233 %s DTSTART, the start date (excluding time) of the diary entry,
234 %t TIMESTAMP, a unique creation timestamp,
235 %u USERNAME, the variable `user-login-name'.
237 For example, a value of \"%s_%h@mydomain.com\" will generate a
238 UID code for each entry composed of the time of the event, a hash
239 code for the event, and your personal domain name."
243 (defcustom icalendar-export-sexp-enumeration-days
245 "Number of days over which a sexp diary entry is enumerated.
246 In general sexp entries cannot be translated to icalendar format.
247 They are therefore enumerated, i.e. explicitly evaluated for a
248 certain number of days, and then exported. The enumeration starts
249 on the current day and continues for the number of days given here.
251 See `icalendar-export-sexp-enumerate-all' for a list of sexp
252 entries which by default are NOT enumerated."
257 (defcustom icalendar-export-sexp-enumerate-all
259 "Determines whether ALL sexp diary entries are enumerated.
260 If non-nil all sexp diary entries are enumerated for
261 `icalendar-export-sexp-enumeration-days' days instead of
262 translating into an icalendar equivalent. This affects the
263 following sexp diary entries: `diary-anniversary',
264 `diary-cyclic', `diary-date', `diary-float', `diary-block'. All
265 other sexp entries are enumerated in any case."
271 (defcustom icalendar-export-alarms
273 "Determine if and how alarms are included in exported diary events."
275 :type
'(choice (const :tag
"Do not include alarms in export"
277 (list :tag
"Create alarms in exported diary entries"
278 (integer :tag
"Advance time (minutes)"
280 (set :tag
"Alarm type"
282 (const audio
:tag
"Audio"))
284 (const display
:tag
"Display"))
287 (repeat :tag
"Attendees"
288 (string :tag
"Email"))))))
292 (defvar icalendar-debug nil
293 "Enable icalendar debug messages.")
295 ;; ======================================================================
296 ;; NO USER SERVICEABLE PARTS BELOW THIS LINE
297 ;; ======================================================================
299 (defconst icalendar--weekday-array
["SU" "MO" "TU" "WE" "TH" "FR" "SA"])
301 ;; ======================================================================
302 ;; all the other libs we need
303 ;; ======================================================================
307 ;; ======================================================================
309 ;; ======================================================================
310 (defun icalendar--dmsg (&rest args
)
311 "Print message ARGS if `icalendar-debug' is non-nil."
313 (apply 'message args
)))
315 ;; ======================================================================
316 ;; Core functionality
317 ;; Functions for parsing icalendars, importing and so on
318 ;; ======================================================================
320 (defun icalendar--get-unfolded-buffer (folded-ical-buffer)
321 "Return a new buffer containing the unfolded contents of a buffer.
322 Folding is the iCalendar way of wrapping long lines. In the
323 created buffer all occurrences of CR LF BLANK are replaced by the
324 empty string. Argument FOLDED-ICAL-BUFFER is the folded input
326 (let ((unfolded-buffer (get-buffer-create " *icalendar-work*")))
328 (set-buffer unfolded-buffer
)
330 (insert-buffer-substring folded-ical-buffer
)
331 (icalendar--clean-up-line-endings)
332 (goto-char (point-min))
333 (while (re-search-forward "\r?\n[ \t]" nil t
)
334 (replace-match "" nil nil
)))
337 (defun icalendar--clean-up-line-endings ()
338 "Replace DOS- and MAC-like line endings with unix line endings.
339 All occurrences of (CR LF) and (LF CF) are replaced with LF in
340 the current buffer. This is necessary in buffers which contain a
341 mix of different line endings."
343 (goto-char (point-min))
344 (while (re-search-forward "\r\n\\|\n\r" nil t
)
345 (replace-match "\n" nil nil
))))
347 (defsubst icalendar--rris
(regexp rep string
&optional fixedcase literal
)
348 "Replace regular expression in string.
349 Pass arguments REGEXP REP STRING FIXEDCASE LITERAL to
350 `replace-regexp-in-string' (Emacs) or to `replace-in-string' (XEmacs)."
351 (cond ((fboundp 'replace-regexp-in-string
)
353 (replace-regexp-in-string regexp rep string fixedcase literal
))
354 ((fboundp 'replace-in-string
)
356 (save-match-data ;; apparently XEmacs needs save-match-data
357 (replace-in-string string regexp rep literal
)))))
359 (defun icalendar--read-element (invalue inparams
)
360 "Recursively read the next iCalendar element in the current buffer.
361 INVALUE gives the current iCalendar element we are reading.
362 INPARAMS gives the current parameters.....
363 This function calls itself recursively for each nested calendar element
365 (let (element children line name params param param-name param-value
370 (re-search-forward "^\\([A-Za-z0-9-]+\\)[;:]" nil t
))
371 (setq name
(intern (match-string 1)))
375 (while (looking-at ";")
376 (re-search-forward ";\\([A-Za-z0-9-]+\\)=" nil nil
)
377 (setq param-name
(intern (match-string 1)))
378 (re-search-forward "\\(\\([^;,:\"]+\\)\\|\"\\([^\"]+\\)\"\\)[;:]"
381 (setq param-value
(or (match-string 2) (match-string 3)))
382 (setq param
(list param-name param-value
))
383 (while (looking-at ",")
384 (re-search-forward "\\(\\([^;,:]+\\)\\|\"\\([^\"]+\\)\"\\)"
387 (setq param-value
(match-string 2))
388 (setq param-value
(match-string 3)))
389 (setq param
(append param param-value
)))
390 (setq params
(append params param
)))
391 (unless (looking-at ":")
394 (re-search-forward "\\(.*\\)\\(\r?\n[ \t].*\\)*" nil t
)
395 (setq value
(icalendar--rris "\r?\n[ \t]" "" (match-string 0)))
396 (setq line
(list name params value
))
397 (cond ((eq name
'BEGIN
)
400 (list (icalendar--read-element (intern value
)
405 (setq element
(append element
(list line
))))))
407 (list invalue inparams element children
)
410 ;; ======================================================================
411 ;; helper functions for examining events
412 ;; ======================================================================
414 ;;(defsubst icalendar--get-all-event-properties (event)
415 ;; "Return the list of properties in this EVENT."
416 ;; (car (cddr event)))
418 (defun icalendar--get-event-property (event prop
)
419 "For the given EVENT return the value of the first occurrence of PROP."
421 (let ((props (car (cddr event
))) pp
)
423 (setq pp
(car props
))
424 (if (eq (car pp
) prop
)
425 (throw 'found
(car (cddr pp
))))
426 (setq props
(cdr props
))))
429 (defun icalendar--get-event-property-attributes (event prop
)
430 "For the given EVENT return attributes of the first occurrence of PROP."
432 (let ((props (car (cddr event
))) pp
)
434 (setq pp
(car props
))
435 (if (eq (car pp
) prop
)
436 (throw 'found
(cadr pp
)))
437 (setq props
(cdr props
))))
440 (defun icalendar--get-event-properties (event prop
)
441 "For the given EVENT return a list of all values of the property PROP."
442 (let ((props (car (cddr event
))) pp result
)
444 (setq pp
(car props
))
445 (if (eq (car pp
) prop
)
446 (setq result
(append (split-string (car (cddr pp
)) ",") result
)))
447 (setq props
(cdr props
)))
450 ;; (defun icalendar--set-event-property (event prop new-value)
451 ;; "For the given EVENT set the property PROP to the value NEW-VALUE."
453 ;; (let ((props (car (cddr event))) pp)
455 ;; (setq pp (car props))
456 ;; (when (eq (car pp) prop)
457 ;; (setcdr (cdr pp) new-value)
458 ;; (throw 'found (car (cddr pp))))
459 ;; (setq props (cdr props)))
460 ;; (setq props (car (cddr event)))
461 ;; (setcar (cddr event)
462 ;; (append props (list (list prop nil new-value)))))))
464 (defun icalendar--get-children (node name
)
465 "Return all children of the given NODE which have a name NAME.
466 For instance the VCALENDAR node can have VEVENT children as well as VTODO
469 (children (cadr (cddr node
))))
470 (when (eq (car node
) name
)
472 ;;(message "%s" node)
477 (icalendar--get-children n name
))
481 (setq result
(append result subresult
))
482 (setq result subresult
)))))
486 (defun icalendar--all-events (icalendar)
487 "Return the list of all existing events in the given ICALENDAR."
490 (setq result
(append (icalendar--get-children elt
'VEVENT
)
492 (nreverse icalendar
))
495 (defun icalendar--split-value (value-string)
496 "Split VALUE-STRING at `;='."
498 param-name param-value
)
501 (set-buffer (get-buffer-create " *icalendar-work*"))
502 (set-buffer-modified-p nil
)
504 (insert value-string
)
505 (goto-char (point-min))
508 "\\([A-Za-z0-9-]+\\)=\\(\\([^;:]+\\)\\|\"\\([^\"]+\\)\"\\);?"
510 (setq param-name
(intern (match-string 1)))
511 (setq param-value
(match-string 2))
513 (append result
(list (list param-name param-value
)))))))
516 (defun icalendar--convert-tz-offset (alist dst-p
)
517 "Return a cons of two strings representing a timezone start.
518 ALIST is an alist entry from a VTIMEZONE, like STANDARD.
519 DST-P is non-nil if this is for daylight savings time.
520 The strings are suitable for assembling into a TZ variable."
521 (let* ((offsetto (car (cddr (assq 'TZOFFSETTO alist
))))
522 (offsetfrom (car (cddr (assq 'TZOFFSETFROM alist
))))
523 (rrule-value (car (cddr (assq 'RRULE alist
))))
524 (dtstart (car (cddr (assq 'DTSTART alist
))))
525 (no-dst (equal offsetto offsetfrom
)))
526 ;; FIXME: for now we only handle RRULE and not RDATE here.
527 (when (and offsetto dtstart
(or rrule-value no-dst
))
528 (let* ((rrule (icalendar--split-value rrule-value
))
529 (freq (cadr (assq 'FREQ rrule
)))
530 (bymonth (cadr (assq 'BYMONTH rrule
)))
531 (byday (cadr (assq 'BYDAY rrule
))))
532 ;; FIXME: we don't correctly handle WKST here.
533 (if (or no-dst
(and (string= freq
"YEARLY") bymonth
))
537 (if dst-p
"DST" "STD")
538 ;; For TZ, OFFSET is added to the local time. So,
539 ;; invert the values.
540 (if (eq (aref offsetto
0) ?-
) "+" "-")
541 (substring offsetto
1 3)
543 (substring offsetto
3 5))
545 (let* ((day (if no-dst
547 (icalendar--get-weekday-number (substring byday -
2))))
552 (substring byday
0 -
2)))))
553 ;; "Translate" the iCalendar way to specify the last
554 ;; (sun|mon|...)day in month to the tzset way.
555 (if (string= week
"-1") ; last day as iCalendar calls it
556 (setq week
"5")) ; last day as tzset calls it
557 (when no-dst
(setq bymonth
"1"))
558 (concat "M" bymonth
"." week
"." (if (eq day -
1) "0"
562 (substring dtstart -
6 -
4)
564 (substring dtstart -
4 -
2)
566 (substring dtstart -
2)))))))))
568 (defun icalendar--parse-vtimezone (alist)
569 "Turn a VTIMEZONE ALIST into a cons (ID . TZ-STRING).
570 Return nil if timezone cannot be parsed."
571 (let* ((tz-id (icalendar--convert-string-for-import
572 (icalendar--get-event-property alist
'TZID
)))
573 (daylight (cadr (cdar (icalendar--get-children alist
'DAYLIGHT
))))
574 (day (and daylight
(icalendar--convert-tz-offset daylight t
)))
575 (standard (cadr (cdar (icalendar--get-children alist
'STANDARD
))))
576 (std (and standard
(icalendar--convert-tz-offset standard nil
))))
580 (concat (car std
) (car day
)
581 "," (cdr day
) "," (cdr std
))
584 (defun icalendar--convert-all-timezones (icalendar)
585 "Convert all timezones in the ICALENDAR into an alist.
586 Each element of the alist is a cons (ID . TZ-STRING),
587 like `icalendar--parse-vtimezone'."
589 (dolist (zone (icalendar--get-children (car icalendar
) 'VTIMEZONE
))
590 (setq zone
(icalendar--parse-vtimezone zone
))
592 (setq result
(cons zone result
))))
595 (defun icalendar--find-time-zone (prop-list zone-map
)
596 "Return a timezone string for the time zone in PROP-LIST, or nil if none.
597 ZONE-MAP is a timezone alist as returned by `icalendar--convert-all-timezones'."
598 (let ((id (plist-get prop-list
'TZID
)))
600 (cdr (assoc id zone-map
)))))
602 (defun icalendar--decode-isodatetime (isodatetimestring &optional day-shift
604 "Return ISODATETIMESTRING in format like `decode-time'.
605 Converts from ISO-8601 to Emacs representation. If
606 ISODATETIMESTRING specifies UTC time (trailing letter Z) the
607 decoded time is given in the local time zone! If optional
608 parameter DAY-SHIFT is non-nil the result is shifted by DAY-SHIFT
610 ZONE, if provided, is the timezone, in any format understood by `encode-time'.
612 FIXME: multiple comma-separated values should be allowed!"
613 (icalendar--dmsg isodatetimestring
)
614 (if isodatetimestring
615 ;; day/month/year must be present
616 (let ((year (read (substring isodatetimestring
0 4)))
617 (month (read (substring isodatetimestring
4 6)))
618 (day (read (substring isodatetimestring
6 8)))
622 (when (> (length isodatetimestring
) 12)
623 ;; hour/minute present
624 (setq hour
(read (substring isodatetimestring
9 11)))
625 (setq minute
(read (substring isodatetimestring
11 13))))
626 (when (> (length isodatetimestring
) 14)
628 (setq second
(read (substring isodatetimestring
13 15))))
629 (when (and (> (length isodatetimestring
) 15)
630 ;; UTC specifier present
631 (char-equal ?Z
(aref isodatetimestring
15)))
633 ;; shift if necessary
635 (let ((mdy (calendar-gregorian-from-absolute
636 (+ (calendar-absolute-from-gregorian
637 (list month day year
))
639 (setq month
(nth 0 mdy
))
640 (setq day
(nth 1 mdy
))
641 (setq year
(nth 2 mdy
))))
642 ;; create the decoded date-time
645 (decode-time (encode-time second minute hour day month year zone
))
647 (message "Cannot decode \"%s\"" isodatetimestring
)
648 ;; hope for the best...
649 (list second minute hour day month year
0 nil
0))))
650 ;; isodatetimestring == nil
653 (defun icalendar--decode-isoduration (isodurationstring
654 &optional duration-correction
)
655 "Convert ISODURATIONSTRING into format provided by `decode-time'.
656 Converts from ISO-8601 to Emacs representation. If ISODURATIONSTRING
657 specifies UTC time (trailing letter Z) the decoded time is given in
660 Optional argument DURATION-CORRECTION shortens result by one day.
662 FIXME: TZID-attributes are ignored....!
663 FIXME: multiple comma-separated values should be allowed!"
664 (if isodurationstring
669 "\\(\\([0-9]+\\)D\\)" ; days only
671 "\\(\\(\\([0-9]+\\)D\\)?T\\(\\([0-9]+\\)H\\)?" ; opt days
672 "\\(\\([0-9]+\\)M\\)?\\(\\([0-9]+\\)S\\)?\\)" ; mand. time
674 "\\(\\([0-9]+\\)W\\)" ; weeks only
675 "\\)$") isodurationstring
)
683 ((match-beginning 2) ;days only
684 (setq days
(read (substring isodurationstring
687 (when duration-correction
688 (setq days
(1- days
))))
689 ((match-beginning 4) ;days and time
690 (if (match-beginning 5)
691 (setq days
(* 7 (read (substring isodurationstring
694 (if (match-beginning 7)
695 (setq hours
(read (substring isodurationstring
698 (if (match-beginning 9)
699 (setq minutes
(read (substring isodurationstring
702 (if (match-beginning 11)
703 (setq seconds
(read (substring isodurationstring
706 ((match-beginning 13) ;weeks only
707 (setq days
(* 7 (read (substring isodurationstring
710 (list seconds minutes hours days months years
)))
711 ;; isodatetimestring == nil
714 (defun icalendar--add-decoded-times (time1 time2
)
716 Both times must be given in decoded form. One of these times must be
717 valid (year > 1900 or something)."
718 ;; FIXME: does this function exist already?
719 (decode-time (encode-time
720 (+ (nth 0 time1
) (nth 0 time2
))
721 (+ (nth 1 time1
) (nth 1 time2
))
722 (+ (nth 2 time1
) (nth 2 time2
))
723 (+ (nth 3 time1
) (nth 3 time2
))
724 (+ (nth 4 time1
) (nth 4 time2
))
725 (+ (nth 5 time1
) (nth 5 time2
))
728 ;;(or (nth 6 time1) (nth 6 time2)) ;; FIXME?
731 (defun icalendar--datetime-to-american-date (datetime &optional separator
)
732 "Convert the decoded DATETIME to American-style format.
733 Optional argument SEPARATOR gives the separator between month,
734 day, and year. If nil a blank character is used as separator.
735 American format: \"month day year\"."
737 (format "%d%s%d%s%d" (nth 4 datetime
) ;month
739 (nth 3 datetime
) ;day
741 (nth 5 datetime
)) ;year
745 (define-obsolete-function-alias 'icalendar--datetime-to-noneuropean-date
746 'icalendar--datetime-to-american-date
"icalendar 0.19")
748 (defun icalendar--datetime-to-european-date (datetime &optional separator
)
749 "Convert the decoded DATETIME to European format.
750 Optional argument SEPARATOR gives the separator between month,
751 day, and year. If nil a blank character is used as separator.
752 European format: (day month year).
755 (format "%d%s%d%s%d" (nth 3 datetime
) ;day
757 (nth 4 datetime
) ;month
759 (nth 5 datetime
)) ;year
763 (defun icalendar--datetime-to-iso-date (datetime &optional separator
)
764 "Convert the decoded DATETIME to ISO format.
765 Optional argument SEPARATOR gives the separator between month,
766 day, and year. If nil a blank character is used as separator.
767 ISO format: (year month day)."
769 (format "%d%s%d%s%d" (nth 5 datetime
) ;year
771 (nth 4 datetime
) ;month
773 (nth 3 datetime
)) ;day
777 (defun icalendar--datetime-to-diary-date (datetime &optional separator
)
778 "Convert the decoded DATETIME to diary format.
779 Optional argument SEPARATOR gives the separator between month,
780 day, and year. If nil a blank character is used as separator.
781 Call icalendar--datetime-to-*-date according to the current
782 calendar date style."
783 (funcall (intern-soft (format "icalendar--datetime-to-%s-date"
784 calendar-date-style
))
787 (defun icalendar--datetime-to-colontime (datetime)
788 "Extract the time part of a decoded DATETIME into 24-hour format.
789 Note that this silently ignores seconds."
790 (format "%02d:%02d" (nth 2 datetime
) (nth 1 datetime
)))
792 (defun icalendar--get-month-number (monthname)
793 "Return the month number for the given MONTHNAME."
796 (m (downcase monthname
)))
797 (mapc (lambda (month)
798 (let ((mm (downcase month
)))
799 (if (or (string-equal mm m
)
800 (string-equal (substring mm
0 3) m
))
802 (setq num
(1+ num
))))
803 calendar-month-name-array
))
807 (defun icalendar--get-weekday-number (abbrevweekday)
808 "Return the number for the ABBREVWEEKDAY."
812 (aw (downcase abbrevweekday
)))
814 (let ((d (downcase day
)))
815 (if (string-equal d aw
)
817 (setq num
(1+ num
))))
818 icalendar--weekday-array
)))
822 (defun icalendar--get-weekday-numbers (abbrevweekdays)
823 "Return the list of numbers for the comma-separated ABBREVWEEKDAYS."
826 (weekday-alist (mapcar (lambda (day)
829 (cons (downcase day
) num
)))
830 icalendar--weekday-array
)))
832 (mapcar (lambda (abbrevday)
833 (cdr (assoc abbrevday weekday-alist
)))
834 (split-string (downcase abbrevweekdays
) ","))))))
836 (defun icalendar--get-weekday-abbrev (weekday)
837 "Return the abbreviated WEEKDAY."
840 (w (downcase weekday
)))
842 (let ((d (downcase day
)))
843 (if (or (string-equal d w
)
844 (string-equal (substring d
0 3) w
))
845 (throw 'found
(aref icalendar--weekday-array num
)))
846 (setq num
(1+ num
))))
847 calendar-day-name-array
))
851 (defun icalendar--date-to-isodate (date &optional day-shift
)
852 "Convert DATE to iso-style date.
853 DATE must be a list of the form (month day year).
854 If DAY-SHIFT is non-nil, the result is shifted by DAY-SHIFT days."
855 (let ((mdy (calendar-gregorian-from-absolute
856 (+ (calendar-absolute-from-gregorian date
)
858 (format "%04d%02d%02d" (nth 2 mdy
) (nth 0 mdy
) (nth 1 mdy
))))
861 (defun icalendar--datestring-to-isodate (datestring &optional day-shift
)
862 "Convert diary-style DATESTRING to iso-style date.
863 If DAY-SHIFT is non-nil, the result is shifted by DAY-SHIFT days
864 -- DAY-SHIFT must be either nil or an integer. This function
865 tries to figure the date style from DATESTRING itself. If that
866 is not possible it uses the current calendar date style."
867 (let ((day -
1) month year
)
869 (cond ( ;; iso-style numeric date
870 (string-match (concat "\\s-*"
871 "\\([0-9]\\{4\\}\\)[ \t/]\\s-*"
872 "0?\\([1-9][0-9]?\\)[ \t/]\\s-*"
873 "0?\\([1-9][0-9]?\\)")
875 (setq year
(read (substring datestring
(match-beginning 1)
877 (setq month
(read (substring datestring
(match-beginning 2)
879 (setq day
(read (substring datestring
(match-beginning 3)
881 ( ;; non-iso numeric date -- must rely on configured
883 (string-match (concat "\\s-*"
884 "0?\\([1-9][0-9]?\\)[ \t/]\\s-*"
885 "0?\\([1-9][0-9]?\\),?[ \t/]\\s-*"
886 "\\([0-9]\\{4\\}\\)")
888 (setq day
(read (substring datestring
(match-beginning 1)
890 (setq month
(read (substring datestring
(match-beginning 2)
892 (setq year
(read (substring datestring
(match-beginning 3)
894 (if (eq calendar-date-style
'american
)
898 ( ;; date contains month names -- iso style
899 (string-match (concat "\\s-*"
900 "\\([0-9]\\{4\\}\\)[ \t/]\\s-*"
901 "\\([A-Za-z][^ ]+\\)[ \t/]\\s-*"
902 "0?\\([123]?[0-9]\\)")
904 (setq year
(read (substring datestring
(match-beginning 1)
906 (setq month
(icalendar--get-month-number
907 (substring datestring
(match-beginning 2)
909 (setq day
(read (substring datestring
(match-beginning 3)
911 ( ;; date contains month names -- european style
912 (string-match (concat "\\s-*"
913 "0?\\([123]?[0-9]\\)[ \t/]\\s-*"
914 "\\([A-Za-z][^ ]+\\)[ \t/]\\s-*"
915 "\\([0-9]\\{4\\}\\)")
917 (setq day
(read (substring datestring
(match-beginning 1)
919 (setq month
(icalendar--get-month-number
920 (substring datestring
(match-beginning 2)
922 (setq year
(read (substring datestring
(match-beginning 3)
924 ( ;; date contains month names -- american style
925 (string-match (concat "\\s-*"
926 "\\([A-Za-z][^ ]+\\)[ \t/]\\s-*"
927 "0?\\([123]?[0-9]\\),?[ \t/]\\s-*"
928 "\\([0-9]\\{4\\}\\)")
930 (setq day
(read (substring datestring
(match-beginning 2)
932 (setq month
(icalendar--get-month-number
933 (substring datestring
(match-beginning 1)
935 (setq year
(read (substring datestring
(match-beginning 3)
940 (let ((mdy (calendar-gregorian-from-absolute
941 (+ (calendar-absolute-from-gregorian (list month day
944 (icalendar--dmsg (format "%04d%02d%02d" (nth 2 mdy
) (nth 0 mdy
) (nth 1 mdy
)))
945 (format "%04d%02d%02d" (nth 2 mdy
) (nth 0 mdy
) (nth 1 mdy
)))
948 (defun icalendar--diarytime-to-isotime (timestring ampmstring
)
949 "Convert a time like 9:30pm to an iso-conform string like T213000.
950 In this example the TIMESTRING would be \"9:30\" and the
951 AMPMSTRING would be \"pm\". The minutes may be missing as long
952 as the colon is missing as well, i.e. \"9\" is allowed as
953 TIMESTRING and has the same result as \"9:00\"."
955 (let* ((parts (save-match-data (split-string timestring
":")))
957 (m (if (cdr parts
) (cadr parts
)
958 (if (> (length h
) 2) "" "00")))
959 (starttimenum (read (concat h m
))))
960 ;; take care of am/pm style
961 ;; Be sure *not* to convert 12:00pm - 12:59pm to 2400-2459
962 (if (and ampmstring
(string= "pm" ampmstring
) (< starttimenum
1200))
963 (setq starttimenum
(+ starttimenum
1200)))
964 ;; Similar effect with 12:00am - 12:59am (need to convert to 0000-0059)
965 (if (and ampmstring
(string= "am" ampmstring
) (>= starttimenum
1200))
966 (setq starttimenum
(- starttimenum
1200)))
967 (format "T%04d00" starttimenum
))
970 (defun icalendar--convert-string-for-export (string)
971 "Escape comma and other critical characters in STRING."
972 (icalendar--rris "," "\\\\," string
))
974 (defun icalendar--convert-string-for-import (string)
975 "Remove escape chars for comma, semicolon etc. from STRING."
977 "\\\\n" "\n " (icalendar--rris
978 "\\\\\"" "\"" (icalendar--rris
979 "\\\\;" ";" (icalendar--rris
980 "\\\\," "," string
)))))
982 ;; ======================================================================
983 ;; Export -- convert emacs-diary to iCalendar
984 ;; ======================================================================
987 (defun icalendar-export-file (diary-filename ical-filename
)
988 "Export diary file to iCalendar format.
989 All diary entries in the file DIARY-FILENAME are converted to iCalendar
990 format. The result is appended to the file ICAL-FILENAME."
991 (interactive "FExport diary data from file: \n\
992 Finto iCalendar file: ")
994 (set-buffer (find-file diary-filename
))
995 (icalendar-export-region (point-min) (point-max) ical-filename
)))
997 (define-obsolete-function-alias 'icalendar-convert-diary-to-ical
998 'icalendar-export-file
"22.1")
1000 (defvar icalendar--uid-count
0
1001 "Auxiliary counter for creating unique ids.")
1003 (defun icalendar--create-uid (entry-full contents
)
1004 "Construct a unique iCalendar UID for a diary entry.
1005 ENTRY-FULL is the full diary entry string. CONTENTS is the
1006 current iCalendar object, as a string. Increase
1007 `icalendar--uid-count'. Returns the UID string."
1008 (let ((uid icalendar-uid-format
))
1010 ;; Allow other apps (such as org-mode) to create its own uid
1011 (get-text-property 0 'uid entry-full
)
1012 (setq uid
(get-text-property 0 'uid entry-full
))
1013 (setq uid
(replace-regexp-in-string
1015 (format "%d" icalendar--uid-count
)
1017 (setq icalendar--uid-count
(1+ icalendar--uid-count
))
1018 (setq uid
(replace-regexp-in-string
1020 (format "%d%d%d" (car (current-time))
1021 (cadr (current-time))
1022 (car (cddr (current-time))))
1024 (setq uid
(replace-regexp-in-string
1026 (format "%d" (abs (sxhash entry-full
))) uid t t
))
1027 (setq uid
(replace-regexp-in-string
1028 "%u" (or user-login-name
"UNKNOWN_USER") uid t t
))
1029 (let ((dtstart (if (string-match "^DTSTART[^:]*:\\([0-9]*\\)" contents
)
1030 (substring contents
(match-beginning 1) (match-end 1))
1032 (setq uid
(replace-regexp-in-string "%s" dtstart uid t t
))))
1034 ;; Return the UID string
1038 (defun icalendar-export-region (min max ical-filename
)
1039 "Export region in diary file to iCalendar format.
1040 All diary entries in the region from MIN to MAX in the current buffer are
1041 converted to iCalendar format. The result is appended to the file
1043 This function attempts to return t if something goes wrong. In this
1044 case an error string which describes all the errors and problems is
1045 written into the buffer `*icalendar-errors*'."
1047 FExport diary data into iCalendar file: ")
1054 (contents-n-summary)
1058 (nonmarker (concat "^" (regexp-quote diary-nonmarking-symbol
)
1060 (other-elements nil
)
1061 (cns-cons-or-list nil
))
1062 ;; prepare buffer with error messages
1063 (save-current-buffer
1064 (set-buffer (get-buffer-create "*icalendar-errors*"))
1070 (while (re-search-forward
1071 ;; possibly ignore hidden entries beginning with "&"
1072 (if icalendar-export-hidden-diary-entries
1073 "^\\([^ \t\n#].+\\)\\(\\(\n[ \t].*\\)*\\)"
1074 "^\\([^ \t\n&#].+\\)\\(\\(\n[ \t].*\\)*\\)") max t
)
1075 (setq entry-main
(match-string 1))
1076 (if (match-beginning 2)
1077 (setq entry-rest
(match-string 2))
1078 (setq entry-rest
""))
1079 (setq entry-full
(concat entry-main entry-rest
))
1081 (condition-case error-val
1083 (setq cns-cons-or-list
1084 (icalendar--convert-to-ical nonmarker entry-main
))
1085 (setq other-elements
(icalendar--parse-summary-and-rest
1087 (mapc (lambda (contents-n-summary)
1088 (setq contents
(concat (car contents-n-summary
)
1090 (cdr contents-n-summary
)))
1091 (let ((cla (cdr (assoc 'cla other-elements
)))
1092 (des (cdr (assoc 'des other-elements
)))
1093 (loc (cdr (assoc 'loc other-elements
)))
1094 (org (cdr (assoc 'org other-elements
)))
1095 (sta (cdr (assoc 'sta other-elements
)))
1096 (sum (cdr (assoc 'sum other-elements
)))
1097 (url (cdr (assoc 'url other-elements
)))
1098 (uid (cdr (assoc 'uid other-elements
))))
1100 (setq contents
(concat contents
"\nCLASS:" cla
)))
1102 (setq contents
(concat contents
"\nDESCRIPTION:"
1105 (setq contents
(concat contents
"\nLOCATION:" loc
)))
1107 (setq contents
(concat contents
"\nORGANIZER:"
1110 (setq contents
(concat contents
"\nSTATUS:" sta
)))
1112 ;; (setq contents (concat contents "\nSUMMARY:" sum)))
1114 (setq contents
(concat contents
"\nURL:" url
)))
1116 (setq header
(concat "\nBEGIN:VEVENT\nUID:"
1118 (icalendar--create-uid
1119 entry-full contents
))))
1120 (setq alarm
(icalendar--create-ical-alarm
1121 (cdr contents-n-summary
))))
1122 (setq result
(concat result header contents alarm
1124 (if (consp cns-cons-or-list
)
1125 (list cns-cons-or-list
)
1129 (setq found-error t
)
1130 (save-current-buffer
1131 (set-buffer (get-buffer-create "*icalendar-errors*"))
1132 (insert (format-message "Error in line %d -- %s: `%s'\n"
1133 (count-lines (point-min) (point))
1137 ;; we're done, insert everything into the file
1138 (save-current-buffer
1139 (let ((coding-system-for-write 'utf-8
))
1140 (set-buffer (find-file ical-filename
))
1141 (goto-char (point-max))
1142 (insert "BEGIN:VCALENDAR")
1143 (insert "\nPRODID:-//Emacs//NONSGML icalendar.el//EN")
1144 (insert "\nVERSION:2.0")
1146 (insert "\nEND:VCALENDAR\n")
1147 ;; save the diary file
1153 (defun icalendar--convert-to-ical (nonmarker entry-main
)
1154 "Convert a diary entry to iCalendar format.
1155 NONMARKER is a regular expression matching the start of non-marking
1156 entries. ENTRY-MAIN is the first line of the diary entry."
1158 (unless icalendar-export-sexp-enumerate-all
1160 ;; anniversaries -- %%(diary-anniversary ...)
1161 (icalendar--convert-anniversary-to-ical nonmarker entry-main
)
1162 ;; cyclic events -- %%(diary-cyclic ...)
1163 (icalendar--convert-cyclic-to-ical nonmarker entry-main
)
1164 ;; diary-date -- %%(diary-date ...)
1165 (icalendar--convert-date-to-ical nonmarker entry-main
)
1166 ;; float events -- %%(diary-float ...)
1167 (icalendar--convert-float-to-ical nonmarker entry-main
)
1168 ;; block events -- %%(diary-block ...)
1169 (icalendar--convert-block-to-ical nonmarker entry-main
)))
1170 ;; other sexp diary entries
1171 (icalendar--convert-sexp-to-ical nonmarker entry-main
)
1172 ;; weekly by day -- Monday 8:30 Team meeting
1173 (icalendar--convert-weekly-to-ical nonmarker entry-main
)
1174 ;; yearly by day -- 1 May Tag der Arbeit
1175 (icalendar--convert-yearly-to-ical nonmarker entry-main
)
1176 ;; "ordinary" events, start and end time given
1178 (icalendar--convert-ordinary-to-ical nonmarker entry-main
)
1180 ;; Oops! what's that?
1181 (error "Could not parse entry")))
1183 (defun icalendar--parse-summary-and-rest (summary-and-rest)
1184 "Parse SUMMARY-AND-REST from a diary to fill iCalendar properties.
1187 (if (functionp icalendar-import-format
)
1188 ;; can't do anything
1190 ;; split summary-and-rest
1191 (let* ((case-fold-search nil
)
1192 (s icalendar-import-format
)
1193 (p-cla (or (string-match "%c" icalendar-import-format
) -
1))
1194 (p-des (or (string-match "%d" icalendar-import-format
) -
1))
1195 (p-loc (or (string-match "%l" icalendar-import-format
) -
1))
1196 (p-org (or (string-match "%o" icalendar-import-format
) -
1))
1197 (p-sum (or (string-match "%s" icalendar-import-format
) -
1))
1198 (p-sta (or (string-match "%t" icalendar-import-format
) -
1))
1199 (p-url (or (string-match "%u" icalendar-import-format
) -
1))
1200 (p-uid (or (string-match "%U" icalendar-import-format
) -
1))
1201 (p-list (sort (list p-cla p-des p-loc p-org p-sta p-sum p-url p-uid
) '<))
1203 pos-cla pos-des pos-loc pos-org pos-sta pos-sum pos-url pos-uid
)
1204 (dotimes (i (length p-list
))
1205 ;; Use 'ct' to keep track of current position in list
1206 (cond ((and (>= p-cla
0) (= (nth i p-list
) p-cla
))
1208 (setq pos-cla
(* 2 ct
)))
1209 ((and (>= p-des
0) (= (nth i p-list
) p-des
))
1211 (setq pos-des
(* 2 ct
)))
1212 ((and (>= p-loc
0) (= (nth i p-list
) p-loc
))
1214 (setq pos-loc
(* 2 ct
)))
1215 ((and (>= p-org
0) (= (nth i p-list
) p-org
))
1217 (setq pos-org
(* 2 ct
)))
1218 ((and (>= p-sta
0) (= (nth i p-list
) p-sta
))
1220 (setq pos-sta
(* 2 ct
)))
1221 ((and (>= p-sum
0) (= (nth i p-list
) p-sum
))
1223 (setq pos-sum
(* 2 ct
)))
1224 ((and (>= p-url
0) (= (nth i p-list
) p-url
))
1226 (setq pos-url
(* 2 ct
)))
1227 ((and (>= p-uid
0) (= (nth i p-list
) p-uid
))
1229 (setq pos-uid
(* 2 ct
)))) )
1231 (setq s
(icalendar--rris (car ij
) (cadr ij
) s t t
)))
1233 ;; summary must be first! because of %s
1235 (concat "\\(" icalendar-import-format-summary
"\\)??"))
1237 (concat "\\(" icalendar-import-format-class
"\\)??"))
1239 (concat "\\(" icalendar-import-format-description
"\\)??"))
1241 (concat "\\(" icalendar-import-format-location
"\\)??"))
1243 (concat "\\(" icalendar-import-format-organizer
"\\)??"))
1245 (concat "\\(" icalendar-import-format-status
"\\)??"))
1247 (concat "\\(" icalendar-import-format-url
"\\)??"))
1249 (concat "\\(" icalendar-import-format-uid
"\\)??"))))
1250 ;; Need the \' regexp in order to detect multi-line items
1251 (setq s
(concat "\\`"
1252 (icalendar--rris "%s" "\\(.*?\\)" s nil t
)
1254 (if (string-match s summary-and-rest
)
1255 (let (cla des loc org sta sum url uid
)
1256 (if (and pos-sum
(match-beginning pos-sum
))
1257 (setq sum
(substring summary-and-rest
1258 (match-beginning pos-sum
)
1259 (match-end pos-sum
))))
1260 (if (and pos-cla
(match-beginning pos-cla
))
1261 (setq cla
(substring summary-and-rest
1262 (match-beginning pos-cla
)
1263 (match-end pos-cla
))))
1264 (if (and pos-des
(match-beginning pos-des
))
1265 (setq des
(substring summary-and-rest
1266 (match-beginning pos-des
)
1267 (match-end pos-des
))))
1268 (if (and pos-loc
(match-beginning pos-loc
))
1269 (setq loc
(substring summary-and-rest
1270 (match-beginning pos-loc
)
1271 (match-end pos-loc
))))
1272 (if (and pos-org
(match-beginning pos-org
))
1273 (setq org
(substring summary-and-rest
1274 (match-beginning pos-org
)
1275 (match-end pos-org
))))
1276 (if (and pos-sta
(match-beginning pos-sta
))
1277 (setq sta
(substring summary-and-rest
1278 (match-beginning pos-sta
)
1279 (match-end pos-sta
))))
1280 (if (and pos-url
(match-beginning pos-url
))
1281 (setq url
(substring summary-and-rest
1282 (match-beginning pos-url
)
1283 (match-end pos-url
))))
1284 (if (and pos-uid
(match-beginning pos-uid
))
1285 (setq uid
(substring summary-and-rest
1286 (match-beginning pos-uid
)
1287 (match-end pos-uid
))))
1288 (list (if cla
(cons 'cla cla
) nil
)
1289 (if des
(cons 'des des
) nil
)
1290 (if loc
(cons 'loc loc
) nil
)
1291 (if org
(cons 'org org
) nil
)
1292 (if sta
(cons 'sta sta
) nil
)
1293 ;;(if sum (cons 'sum sum) nil)
1294 (if url
(cons 'url url
) nil
)
1295 (if uid
(cons 'uid uid
) nil
))))))))
1297 (defun icalendar--create-ical-alarm (summary)
1298 "Return VALARM blocks for the given SUMMARY."
1299 (when icalendar-export-alarms
1300 (let* ((advance-time (car icalendar-export-alarms
))
1301 (alarm-specs (cadr icalendar-export-alarms
))
1303 (icalendar--do-create-ical-alarm advance-time spec summary
))))
1304 (mapconcat fun alarm-specs
""))))
1306 (defun icalendar--do-create-ical-alarm (advance-time alarm-spec summary
)
1307 "Return a VALARM block.
1308 Argument ADVANCE-TIME is a number giving the time when the alarm
1309 fires (minutes before the respective event). Argument ALARM-SPEC
1310 is a list which must be one of (audio), (display) or
1311 (email (ADDRESS1 ...)), see `icalendar-export-alarms'. Argument
1312 SUMMARY is a string which contains a short description for the
1314 (let* ((action (car alarm-spec
))
1315 (act (format "\nACTION:%s"
1316 (cdr (assoc action
'((audio .
"AUDIO")
1317 (display .
"DISPLAY")
1318 (email .
"EMAIL"))))))
1319 (tri (format "\nTRIGGER:-PT%dM" advance-time
))
1320 (des (if (memq action
'(display email
))
1321 (format "\nDESCRIPTION:%s" summary
)
1323 (sum (if (eq action
'email
)
1324 (format "\nSUMMARY:%s" summary
)
1326 (att (if (eq action
'email
)
1327 (mapconcat (lambda (i)
1328 (format "\nATTENDEE:MAILTO:%s" i
))
1329 (cadr alarm-spec
) "")
1332 (concat "\nBEGIN:VALARM" act tri des sum att
"\nEND:VALARM")))
1334 ;; subroutines for icalendar-export-region
1335 (defun icalendar--convert-ordinary-to-ical (nonmarker entry-main
)
1336 "Convert \"ordinary\" diary entry to iCalendar format.
1337 NONMARKER is a regular expression matching the start of non-marking
1338 entries. ENTRY-MAIN is the first line of the diary entry."
1341 "\\([^ /]+[ /]+[^ /]+[ /]+[^ ]+\\)\\s-*" ; date
1342 "\\(\\([0-9][0-9]?\\(:[0-9][0-9]\\)?\\)\\([ap]m\\)?" ; start time
1344 "-\\([0-9][0-9]?\\(:[0-9][0-9]\\)?\\)\\([ap]m\\)?\\)?" ; end time
1346 "\\s-*\\(.*?\\) ?$")
1348 (let* ((datetime (substring entry-main
(match-beginning 1)
1350 (startisostring (icalendar--datestring-to-isodate
1352 (endisostring (icalendar--datestring-to-isodate
1355 (starttimestring (icalendar--diarytime-to-isotime
1356 (if (match-beginning 3)
1357 (substring entry-main
1361 (if (match-beginning 5)
1362 (substring entry-main
1366 (endtimestring (icalendar--diarytime-to-isotime
1367 (if (match-beginning 7)
1368 (substring entry-main
1372 (if (match-beginning 9)
1373 (substring entry-main
1377 (summary (icalendar--convert-string-for-export
1378 (substring entry-main
(match-beginning 10)
1380 (icalendar--dmsg "ordinary %s" entry-main
)
1382 (unless startisostring
1383 (error "Could not parse date"))
1385 ;; If only start-date is specified, then end-date is next day,
1386 ;; otherwise it is same day.
1387 (setq endisostring1
(if starttimestring
1391 (when starttimestring
1392 (unless endtimestring
1394 (read (icalendar--rris "^T0?" ""
1397 ;; Case: ends on same day
1398 (setq endtimestring
(format "T%06d"
1400 ;; Case: ends on next day
1401 (setq endtimestring
(format "T%06d"
1403 (setq endisostring1 endisostring
)) )))
1405 (cons (concat "\nDTSTART;"
1406 (if starttimestring
"VALUE=DATE-TIME:"
1409 (or starttimestring
"")
1411 (if endtimestring
"VALUE=DATE-TIME:"
1414 (or endtimestring
""))
1419 (defun icalendar-first-weekday-of-year (abbrevweekday year
)
1420 "Find the first ABBREVWEEKDAY in a given YEAR.
1421 Returns day number."
1422 (let* ((day-of-week-jan01 (calendar-day-of-week (list 1 1 year
)))
1424 (- (icalendar--get-weekday-number abbrevweekday
)
1425 day-of-week-jan01
))))
1426 (cond ((<= result
0)
1427 (setq result
(+ result
7)))
1429 (setq result
(- result
7))))
1432 (defun icalendar--convert-weekly-to-ical (nonmarker entry-main
)
1433 "Convert weekly diary entry to iCalendar format.
1434 NONMARKER is a regular expression matching the start of non-marking
1435 entries. ENTRY-MAIN is the first line of the diary entry."
1436 (if (and (string-match (concat nonmarker
1438 "\\(\\([0-9][0-9]?:[0-9][0-9]\\)"
1441 "\\([0-9][0-9]?:[0-9][0-9]\\)"
1444 "\\s-*\\(.*?\\) ?$")
1446 (icalendar--get-weekday-abbrev
1447 (substring entry-main
(match-beginning 1)
1449 (let* ((day (icalendar--get-weekday-abbrev
1450 (substring entry-main
(match-beginning 1)
1452 (starttimestring (icalendar--diarytime-to-isotime
1453 (if (match-beginning 3)
1454 (substring entry-main
1458 (if (match-beginning 4)
1459 (substring entry-main
1463 (endtimestring (icalendar--diarytime-to-isotime
1464 (if (match-beginning 6)
1465 (substring entry-main
1469 (if (match-beginning 7)
1470 (substring entry-main
1474 (summary (icalendar--convert-string-for-export
1475 (substring entry-main
(match-beginning 8)
1477 (icalendar--dmsg "weekly %s" entry-main
)
1479 (when starttimestring
1480 (unless endtimestring
1482 (icalendar--rris "^T0?" ""
1484 (setq endtimestring
(format "T%06d"
1486 (cons (concat "\nDTSTART;"
1490 ;; Find the first requested weekday of the
1492 (funcall 'format
"%04d%02d%02d"
1493 icalendar-recurring-start-year
1
1494 (icalendar-first-weekday-of-year
1495 day icalendar-recurring-start-year
))
1496 (or starttimestring
"")
1501 (funcall 'format
"%04d%02d%02d"
1502 ;; end is non-inclusive!
1503 icalendar-recurring-start-year
1
1504 (+ (icalendar-first-weekday-of-year
1505 day icalendar-recurring-start-year
)
1506 (if endtimestring
0 1)))
1507 (or endtimestring
"")
1508 "\nRRULE:FREQ=WEEKLY;INTERVAL=1;BYDAY="
1514 (defun icalendar--convert-yearly-to-ical (nonmarker entry-main
)
1515 "Convert yearly diary entry to iCalendar format.
1516 NONMARKER is a regular expression matching the start of non-marking
1517 entries. ENTRY-MAIN is the first line of the diary entry."
1518 (if (string-match (concat nonmarker
1519 (if (eq calendar-date-style
'european
)
1520 "\\([0-9]+[0-9]?\\)\\s-+\\([a-z]+\\)\\s-+"
1521 "\\([a-z]+\\)\\s-+\\([0-9]+[0-9]?\\)\\s-+")
1523 "\\(\\([0-9][0-9]?:[0-9][0-9]\\)\\([ap]m\\)?"
1525 "-\\([0-9][0-9]?:[0-9][0-9]\\)\\([ap]m\\)?\\)?"
1527 "\\s-*\\([^0-9]+.*?\\) ?$" ; must not match years
1530 (let* ((daypos (if (eq calendar-date-style
'european
) 1 2))
1531 (monpos (if (eq calendar-date-style
'european
) 2 1))
1532 (day (read (substring entry-main
1533 (match-beginning daypos
)
1534 (match-end daypos
))))
1535 (month (icalendar--get-month-number
1536 (substring entry-main
1537 (match-beginning monpos
)
1538 (match-end monpos
))))
1539 (starttimestring (icalendar--diarytime-to-isotime
1540 (if (match-beginning 4)
1541 (substring entry-main
1545 (if (match-beginning 5)
1546 (substring entry-main
1550 (endtimestring (icalendar--diarytime-to-isotime
1551 (if (match-beginning 7)
1552 (substring entry-main
1556 (if (match-beginning 8)
1557 (substring entry-main
1561 (summary (icalendar--convert-string-for-export
1562 (substring entry-main
(match-beginning 9)
1564 (icalendar--dmsg "yearly %s" entry-main
)
1566 (when starttimestring
1567 (unless endtimestring
1569 (icalendar--rris "^T0?" ""
1571 (setq endtimestring
(format "T%06d"
1573 (cons (concat "\nDTSTART;"
1574 (if starttimestring
"VALUE=DATE-TIME:"
1576 (format "1900%02d%02d" month day
)
1577 (or starttimestring
"")
1579 (if endtimestring
"VALUE=DATE-TIME:"
1581 ;; end is not included! shift by one day
1582 (icalendar--date-to-isodate
1583 (list month day
1900)
1584 (if endtimestring
0 1))
1585 (or endtimestring
"")
1586 "\nRRULE:FREQ=YEARLY;INTERVAL=1;BYMONTH="
1594 (defun icalendar--convert-sexp-to-ical (nonmarker entry-main
&optional start
)
1595 "Convert sexp diary entry to iCalendar format.
1596 Enumerate the evaluated sexp entry for the next
1597 `icalendar-export-sexp-enumeration-days' days. NONMARKER is a
1598 regular expression matching the start of non-marking entries.
1599 ENTRY-MAIN is the first line of the diary entry.
1601 Optional argument START determines the first day of the
1602 enumeration, given as a time value, in same format as returned by
1603 `current-time' -- used for test purposes."
1604 (cond ((string-match (concat nonmarker
1605 "%%(and \\(([^)]+)\\))\\(\\s-*.*?\\) ?$")
1607 ;; simple sexp entry as generated by icalendar.el: strip off the
1608 ;; unnecessary (and)
1609 (icalendar--dmsg "diary-sexp from icalendar.el %s" entry-main
)
1610 (icalendar--convert-to-ical
1613 (substring entry-main
(match-beginning 1) (match-end 1))
1614 (substring entry-main
(match-beginning 2) (match-end 2)))))
1615 ((string-match (concat nonmarker
1616 "%%\\(([^)]+)\\)\\s-*\\(.*\\)")
1618 ;; regular sexp entry
1619 (icalendar--dmsg "diary-sexp %s" entry-main
)
1620 (let ((p1 (substring entry-main
(match-beginning 1) (match-end 1)))
1621 (p2 (substring entry-main
(match-beginning 2) (match-end 2)))
1622 (now (or start
(current-time))))
1626 (let* ((day (decode-time (time-add now
1628 (* offset
60 60 24)))))
1632 (se (diary-sexp-entry p1 p2
(list m d y
)))
1633 (see (cond ((stringp se
) se
)
1634 ((consp se
) (cdr se
))
1639 (let ((calendar-date-style 'iso
))
1640 (icalendar--convert-ordinary-to-ical
1641 nonmarker
(format "%4d/%02d/%02d %s" y m d see
))))
1643 (error "Unsupported Sexp-entry: %s"
1646 0 (- icalendar-export-sexp-enumeration-days
1))))))
1651 (defun icalendar--convert-block-to-ical (nonmarker entry-main
)
1652 "Convert block diary entry to iCalendar format.
1653 NONMARKER is a regular expression matching the start of non-marking
1654 entries. ENTRY-MAIN is the first line of the diary entry."
1655 (if (string-match (concat nonmarker
1656 "%%(diary-block \\([^ /]+[ /]+[^ /]+[ /]+[^ ]+\\)"
1657 " +\\([^ /]+[ /]+[^ /]+[ /]+[^ ]+\\))\\s-*"
1658 "\\(\\([0-9][0-9]?:[0-9][0-9]\\)\\([ap]m\\)?"
1660 "-\\([0-9][0-9]?:[0-9][0-9]\\)\\([ap]m\\)?\\)?"
1662 "\\s-*\\(.*?\\) ?$")
1664 (let* ((startstring (substring entry-main
1667 (endstring (substring entry-main
1670 (startisostring (icalendar--datestring-to-isodate
1672 (endisostring (icalendar--datestring-to-isodate
1674 (endisostring+1 (icalendar--datestring-to-isodate
1676 (starttimestring (icalendar--diarytime-to-isotime
1677 (if (match-beginning 4)
1678 (substring entry-main
1682 (if (match-beginning 5)
1683 (substring entry-main
1687 (endtimestring (icalendar--diarytime-to-isotime
1688 (if (match-beginning 7)
1689 (substring entry-main
1693 (if (match-beginning 8)
1694 (substring entry-main
1698 (summary (icalendar--convert-string-for-export
1699 (substring entry-main
(match-beginning 9)
1701 (icalendar--dmsg "diary-block %s" entry-main
)
1702 (when starttimestring
1703 (unless endtimestring
1705 (read (icalendar--rris "^T0?" ""
1707 (setq endtimestring
(format "T%06d"
1710 ;; with time -> write rrule
1711 (cons (concat "\nDTSTART;VALUE=DATE-TIME:"
1714 "\nDTEND;VALUE=DATE-TIME:"
1717 "\nRRULE:FREQ=DAILY;INTERVAL=1;UNTIL="
1720 ;; no time -> write long event
1721 (cons (concat "\nDTSTART;VALUE=DATE:" startisostring
1722 "\nDTEND;VALUE=DATE:" endisostring
+1)
1727 (defun icalendar--convert-float-to-ical (nonmarker entry-main
)
1728 "Convert float diary entry to iCalendar format -- partially unsupported!
1730 FIXME! DAY from diary-float yet unimplemented.
1732 NONMARKER is a regular expression matching the start of non-marking
1733 entries. ENTRY-MAIN is the first line of the diary entry."
1734 (if (string-match (concat nonmarker
"%%\\((diary-float .+\\) ?$") entry-main
)
1736 (insert (match-string 1 entry-main
))
1737 (goto-char (point-min))
1738 (let* ((sexp (read (current-buffer))) ;using `read' here
1740 ;matching, esp. with
1743 (month (nth 1 sexp
))
1744 (dayname (nth 2 sexp
))
1748 (replace-regexp-in-string
1749 "\\(^\s+\\|\s+$\\)" ""
1750 (buffer-substring (point) (point-max)))))
1754 (icalendar--dmsg "diary-float %s" entry-main
)
1755 (error "Don't know if or how to implement day in `diary-float'")))
1758 ;;Start today (yes this is an arbitrary choice):
1759 "\nDTSTART;VALUE=DATE:"
1760 (format-time-string "%Y%m%d")
1761 ;;BUT remove today if `diary-float'
1762 ;;expression does not hold true for today:
1764 (null (let ((date (calendar-current-date))
1766 (diary-float month dayname n
)))
1768 "\nEXDATE;VALUE=DATE:"
1769 (format-time-string "%Y%m%d")))
1771 (if (or (numberp month
) (listp month
))
1772 "FREQ=YEARLY;BYMONTH="
1778 (number-to-string m
))
1782 (number-to-string month
))
1784 (number-to-string n
)
1785 (aref icalendar--weekday-array dayname
))
1790 (defun icalendar--convert-date-to-ical (nonmarker entry-main
)
1791 "Convert `diary-date' diary entry to iCalendar format -- unsupported!
1795 NONMARKER is a regular expression matching the start of non-marking
1796 entries. ENTRY-MAIN is the first line of the diary entry."
1797 (if (string-match (concat nonmarker
1798 "%%(diary-date \\([^)]+\\))\\s-*\\(.*?\\) ?$")
1801 (icalendar--dmsg "diary-date %s" entry-main
)
1802 (error "`diary-date' is not supported yet"))
1806 (defun icalendar--convert-cyclic-to-ical (nonmarker entry-main
)
1807 "Convert `diary-cyclic' diary entry to iCalendar format.
1808 NONMARKER is a regular expression matching the start of non-marking
1809 entries. ENTRY-MAIN is the first line of the diary entry."
1810 (if (string-match (concat nonmarker
1811 "%%(diary-cyclic \\([^ ]+\\) +"
1812 "\\([^ /]+[ /]+[^ /]+[ /]+[^ ]+\\))\\s-*"
1813 "\\(\\([0-9][0-9]?:[0-9][0-9]\\)\\([ap]m\\)?"
1815 "-\\([0-9][0-9]?:[0-9][0-9]\\)\\([ap]m\\)?\\)?"
1817 "\\s-*\\(.*?\\) ?$")
1819 (let* ((frequency (substring entry-main
(match-beginning 1)
1821 (datetime (substring entry-main
(match-beginning 2)
1823 (startisostring (icalendar--datestring-to-isodate
1825 (endisostring (icalendar--datestring-to-isodate
1827 (endisostring+1 (icalendar--datestring-to-isodate
1829 (starttimestring (icalendar--diarytime-to-isotime
1830 (if (match-beginning 4)
1831 (substring entry-main
1835 (if (match-beginning 5)
1836 (substring entry-main
1840 (endtimestring (icalendar--diarytime-to-isotime
1841 (if (match-beginning 7)
1842 (substring entry-main
1846 (if (match-beginning 8)
1847 (substring entry-main
1851 (summary (icalendar--convert-string-for-export
1852 (substring entry-main
(match-beginning 9)
1854 (icalendar--dmsg "diary-cyclic %s" entry-main
)
1855 (when starttimestring
1856 (unless endtimestring
1858 (read (icalendar--rris "^T0?" ""
1860 (setq endtimestring
(format "T%06d"
1862 (cons (concat "\nDTSTART;"
1863 (if starttimestring
"VALUE=DATE-TIME:"
1866 (or starttimestring
"")
1868 (if endtimestring
"VALUE=DATE-TIME:"
1870 (if endtimestring endisostring endisostring
+1)
1871 (or endtimestring
"")
1872 "\nRRULE:FREQ=DAILY;INTERVAL=" frequency
1873 ;; strange: korganizer does not expect
1874 ;; BYSOMETHING here...
1880 (defun icalendar--convert-anniversary-to-ical (nonmarker entry-main
)
1881 "Convert `diary-anniversary' diary entry to iCalendar format.
1882 NONMARKER is a regular expression matching the start of non-marking
1883 entries. ENTRY-MAIN is the first line of the diary entry."
1884 (if (string-match (concat nonmarker
1885 "%%(diary-anniversary \\([^)]+\\))\\s-*"
1886 "\\(\\([0-9][0-9]?:[0-9][0-9]\\)\\([ap]m\\)?"
1888 "-\\([0-9][0-9]?:[0-9][0-9]\\)\\([ap]m\\)?\\)?"
1890 "\\s-*\\(.*?\\) ?$")
1892 (let* ((datetime (substring entry-main
(match-beginning 1)
1894 (startisostring (icalendar--datestring-to-isodate
1896 (endisostring (icalendar--datestring-to-isodate
1898 (starttimestring (icalendar--diarytime-to-isotime
1899 (if (match-beginning 3)
1900 (substring entry-main
1904 (if (match-beginning 4)
1905 (substring entry-main
1909 (endtimestring (icalendar--diarytime-to-isotime
1910 (if (match-beginning 6)
1911 (substring entry-main
1915 (if (match-beginning 7)
1916 (substring entry-main
1920 (summary (icalendar--convert-string-for-export
1921 (substring entry-main
(match-beginning 8)
1923 (icalendar--dmsg "diary-anniversary %s" entry-main
)
1924 (when starttimestring
1925 (unless endtimestring
1927 (read (icalendar--rris "^T0?" ""
1929 (setq endtimestring
(format "T%06d"
1931 (cons (concat "\nDTSTART;"
1932 (if starttimestring
"VALUE=DATE-TIME:"
1935 (or starttimestring
"")
1937 (if endtimestring
"VALUE=DATE-TIME:"
1940 (or endtimestring
"")
1941 "\nRRULE:FREQ=YEARLY;INTERVAL=1"
1942 ;; the following is redundant,
1943 ;; but korganizer seems to expect this... ;(
1944 ;; and evolution doesn't understand it... :(
1945 ;; so... who is wrong?!
1947 (substring startisostring
4 6)
1949 (substring startisostring
6 8))
1954 ;; ======================================================================
1955 ;; Import -- convert iCalendar to emacs-diary
1956 ;; ======================================================================
1959 (defun icalendar-import-file (ical-filename diary-filename
1960 &optional non-marking
)
1961 "Import an iCalendar file and append to a diary file.
1962 Argument ICAL-FILENAME output iCalendar file.
1963 Argument DIARY-FILENAME input `diary-file'.
1964 Optional argument NON-MARKING determines whether events are created as
1965 non-marking or not."
1966 (interactive "fImport iCalendar data from file: \n\
1969 ;; clean up the diary file
1970 (save-current-buffer
1971 ;; now load and convert from the ical file
1972 (set-buffer (find-file ical-filename
))
1973 (icalendar-import-buffer diary-filename t non-marking
)))
1976 (defun icalendar-import-buffer (&optional diary-file do-not-ask
1978 "Extract iCalendar events from current buffer.
1980 This function searches the current buffer for the first iCalendar
1981 object, reads it and adds all VEVENT elements to the diary
1984 It will ask for each appointment whether to add it to the diary
1985 unless DO-NOT-ASK is non-nil. When called interactively,
1986 DO-NOT-ASK is nil, so that you are asked for each event.
1988 NON-MARKING determines whether diary events are created as
1991 Return code t means that importing worked well, return code nil
1992 means that an error has occurred. Error messages will be in the
1993 buffer `*icalendar-errors*'."
1995 (save-current-buffer
1997 (message "Preparing iCalendar...")
1998 (set-buffer (icalendar--get-unfolded-buffer (current-buffer)))
1999 (goto-char (point-min))
2000 (message "Preparing iCalendar...done")
2001 (if (re-search-forward "^BEGIN:VCALENDAR\\s-*$" nil t
)
2002 (let (ical-contents ical-errors
)
2004 (message "Reading iCalendar...")
2006 (setq ical-contents
(icalendar--read-element nil nil
))
2007 (message "Reading iCalendar...done")
2009 (message "Converting iCalendar...")
2010 (setq ical-errors
(icalendar--convert-ical-to-diary
2012 diary-file do-not-ask non-marking
))
2014 ;; save the diary file if it is visited already
2015 (let ((b (find-buffer-visiting diary-file
)))
2017 (save-current-buffer
2020 (message "Converting iCalendar...done")
2021 ;; return t if no error occurred
2024 "Current buffer does not contain iCalendar contents!")
2025 ;; return nil, i.e. import did not work
2028 (define-obsolete-function-alias 'icalendar-extract-ical-from-buffer
2029 'icalendar-import-buffer
"22.1")
2031 (defun icalendar--format-ical-event (event)
2032 "Create a string representation of an iCalendar EVENT."
2033 (if (functionp icalendar-import-format
)
2034 (funcall icalendar-import-format event
)
2035 (let ((string icalendar-import-format
)
2036 (case-fold-search nil
)
2038 '(("%c" CLASS icalendar-import-format-class
)
2039 ("%d" DESCRIPTION icalendar-import-format-description
)
2040 ("%l" LOCATION icalendar-import-format-location
)
2041 ("%o" ORGANIZER icalendar-import-format-organizer
)
2042 ("%s" SUMMARY icalendar-import-format-summary
)
2043 ("%t" STATUS icalendar-import-format-status
)
2044 ("%u" URL icalendar-import-format-url
)
2045 ("%U" UID icalendar-import-format-uid
))))
2046 ;; convert the specifiers in the format string
2048 (let* ((spec (car i
))
2050 (format (car (cddr i
)))
2051 (contents (icalendar--get-event-property event prop
))
2052 (formatted-contents ""))
2053 (when (and contents
(> (length contents
) 0))
2054 (setq formatted-contents
2055 (icalendar--rris "%s"
2056 (icalendar--convert-string-for-import
2058 (symbol-value format
)
2060 (setq string
(icalendar--rris spec
2067 (defun icalendar--convert-ical-to-diary (ical-list diary-file
2068 &optional do-not-ask
2070 "Convert iCalendar data to an Emacs diary file.
2071 Import VEVENTS from the iCalendar object ICAL-LIST and saves them to a
2072 DIARY-FILE. If DO-NOT-ASK is nil the user is asked for each event
2073 whether to actually import it. NON-MARKING determines whether diary
2074 events are created as non-marking.
2075 This function attempts to return t if something goes wrong. In this
2076 case an error string which describes all the errors and problems is
2077 written into the buffer `*icalendar-errors*'."
2078 (let* ((ev (icalendar--all-events ical-list
))
2082 (zone-map (icalendar--convert-all-timezones ical-list
))
2084 ;; step through all events/appointments
2089 (condition-case error-val
2090 (let* ((dtstart (icalendar--get-event-property e
'DTSTART
))
2091 (dtstart-zone (icalendar--find-time-zone
2092 (icalendar--get-event-property-attributes
2095 (dtstart-dec (icalendar--decode-isodatetime dtstart nil
2097 (start-d (icalendar--datetime-to-diary-date
2099 (start-t (icalendar--datetime-to-colontime dtstart-dec
))
2100 (dtend (icalendar--get-event-property e
'DTEND
))
2101 (dtend-zone (icalendar--find-time-zone
2102 (icalendar--get-event-property-attributes
2105 (dtend-dec (icalendar--decode-isodatetime dtend
2107 (dtend-1-dec (icalendar--decode-isodatetime dtend -
1
2112 (summary (icalendar--convert-string-for-import
2113 (or (icalendar--get-event-property e
'SUMMARY
)
2115 (rrule (icalendar--get-event-property e
'RRULE
))
2116 (rdate (icalendar--get-event-property e
'RDATE
))
2117 (duration (icalendar--get-event-property e
'DURATION
)))
2118 (icalendar--dmsg "%s: `%s'" start-d summary
)
2119 ;; check whether start-time is missing
2122 (cadr (icalendar--get-event-property-attributes
2127 (let ((dtend-dec-d (icalendar--add-decoded-times
2129 (icalendar--decode-isoduration duration
)))
2130 (dtend-1-dec-d (icalendar--add-decoded-times
2132 (icalendar--decode-isoduration duration
2134 (if (and dtend-dec
(not (eq dtend-dec dtend-dec-d
)))
2135 (message "Inconsistent endtime and duration for %s"
2137 (setq dtend-dec dtend-dec-d
)
2138 (setq dtend-1-dec dtend-1-dec-d
)))
2139 (setq end-d
(if dtend-dec
2140 (icalendar--datetime-to-diary-date dtend-dec
)
2142 (setq end-1-d
(if dtend-1-dec
2143 (icalendar--datetime-to-diary-date dtend-1-dec
)
2145 (setq end-t
(if (and
2149 (icalendar--get-event-property-attributes
2152 (icalendar--datetime-to-colontime dtend-dec
)
2154 (icalendar--dmsg "start-d: %s, end-d: %s" start-d end-d
)
2159 (icalendar--convert-recurring-to-diary e dtstart-dec start-t
2163 (icalendar--dmsg "rdate event")
2164 (setq diary-string
"")
2165 (mapc (lambda (datestring)
2167 (concat diary-string
2168 (format "......"))))
2169 (icalendar--split-value rdate
)))
2170 ;; non-recurring event
2172 ((not (string= start-d end-d
))
2174 (icalendar--convert-non-recurring-all-day-to-diary
2178 ((and start-t
(or (not end-t
)
2179 (not (string= start-t end-t
))))
2181 (icalendar--convert-non-recurring-not-all-day-to-diary
2182 e dtstart-dec dtend-dec start-t end-t
))
2186 (icalendar--dmsg "all day event")
2187 (setq diary-string
(icalendar--datetime-to-diary-date
2190 ;; add all other elements unless the user doesn't want to have
2195 (concat diary-string
" "
2196 (icalendar--format-ical-event e
)))
2197 (if do-not-ask
(setq summary nil
))
2198 ;; add entry to diary and store actual name of diary
2199 ;; file (in case it was nil)
2201 (icalendar--add-diary-entry diary-string diary-file
2202 non-marking summary
)))
2204 (setq found-error t
)
2206 (format "%s\nCannot handle this event:%s"
2208 ;; FIXME: inform user about ignored event properties
2211 (message "Ignoring event \"%s\"" e
)
2212 (setq found-error t
)
2213 (setq error-string
(format "%s\n%s\nCannot handle this event: %s"
2214 error-val error-string e
))
2215 (message "%s" error-string
))))
2217 ;; insert final newline
2219 (let ((b (find-buffer-visiting diary-file
)))
2221 (save-current-buffer
2223 (goto-char (point-max))
2226 (save-current-buffer
2227 (set-buffer (get-buffer-create "*icalendar-errors*"))
2229 (insert error-string
)))
2230 (message "Converting iCalendar...done")
2233 ;; subroutines for importing
2234 (defun icalendar--convert-recurring-to-diary (e dtstart-dec start-t end-t
)
2235 "Convert recurring iCalendar event E to diary format.
2237 DTSTART-DEC is the DTSTART property of E.
2238 START-T is the event's start time in diary format.
2239 END-T is the event's end time in diary format."
2240 (icalendar--dmsg "recurring event")
2241 (let* ((rrule (icalendar--get-event-property e
'RRULE
))
2242 (rrule-props (icalendar--split-value rrule
))
2243 (frequency (cadr (assoc 'FREQ rrule-props
)))
2244 (until (cadr (assoc 'UNTIL rrule-props
)))
2245 (count (cadr (assoc 'COUNT rrule-props
)))
2246 (interval (read (or (cadr (assoc 'INTERVAL rrule-props
)) "1")))
2247 (dtstart-conv (icalendar--datetime-to-diary-date dtstart-dec
))
2248 (until-conv (icalendar--datetime-to-diary-date
2249 (icalendar--decode-isodatetime until
)))
2250 (until-1-conv (icalendar--datetime-to-diary-date
2251 (icalendar--decode-isodatetime until -
1)))
2254 ;; FIXME FIXME interval!!!!!!!!!!!!!
2258 (message "Must not have UNTIL and COUNT -- ignoring COUNT element!")
2260 (cond ((string-equal frequency
"DAILY")
2261 (setq until
(icalendar--add-decoded-times
2263 (list 0 0 0 (* (read count
) interval
) 0 0)))
2264 (setq until-1
(icalendar--add-decoded-times
2266 (list 0 0 0 (* (- (read count
) 1) interval
)
2269 ((string-equal frequency
"WEEKLY")
2270 (setq until
(icalendar--add-decoded-times
2272 (list 0 0 0 (* (read count
) 7 interval
) 0 0)))
2273 (setq until-1
(icalendar--add-decoded-times
2275 (list 0 0 0 (* (- (read count
) 1) 7
2278 ((string-equal frequency
"MONTHLY")
2279 (setq until
(icalendar--add-decoded-times
2280 dtstart-dec
(list 0 0 0 0 (* (- (read count
) 1)
2282 (setq until-1
(icalendar--add-decoded-times
2283 dtstart-dec
(list 0 0 0 0 (* (- (read count
) 1)
2286 ((string-equal frequency
"YEARLY")
2287 (setq until
(icalendar--add-decoded-times
2288 dtstart-dec
(list 0 0 0 0 0 (* (- (read count
) 1)
2290 (setq until-1
(icalendar--add-decoded-times
2292 (list 0 0 0 0 0 (* (- (read count
) 1)
2296 (message "Cannot handle COUNT attribute for `%s' events."
2298 (setq until-conv
(icalendar--datetime-to-diary-date until
))
2299 (setq until-1-conv
(icalendar--datetime-to-diary-date until-1
))
2302 (cond ((string-equal frequency
"WEEKLY")
2303 (let* ((byday (cadr (assoc 'BYDAY rrule-props
)))
2305 (icalendar--get-weekday-numbers byday
))
2307 (when (> (length weekdays
) 1)
2308 (format "(memq (calendar-day-of-week date) '%s) "
2312 ;; weekly and all-day
2313 (icalendar--dmsg "weekly all-day")
2319 "(diary-block %s %s))")
2321 (format "(diary-cyclic %d %s) "
2325 (if count until-1-conv until-conv
)
2328 (format "%%%%(and %s(diary-cyclic %d %s))"
2329 (or weekday-clause
"")
2330 (if weekday-clause
1 (* interval
7))
2332 ;; weekly and not all-day
2333 (icalendar--dmsg "weekly not-all-day")
2339 "(diary-block %s %s)) "
2342 (format "(diary-cyclic %d %s) "
2348 (if end-t
"-" "") (or end-t
"")))
2351 ;; DTSTART;VALUE=DATE-TIME:20030919T090000
2352 ;; DTEND;VALUE=DATE-TIME:20030919T113000
2355 "%%%%(and %s(diary-cyclic %d %s)) %s%s%s"
2356 (or weekday-clause
"")
2357 (if weekday-clause
1 (* interval
7))
2360 (if end-t
"-" "") (or end-t
"")))))))
2362 ((string-equal frequency
"YEARLY")
2363 (icalendar--dmsg "yearly")
2365 (let ((day (nth 3 dtstart-dec
))
2366 (month (nth 4 dtstart-dec
)))
2367 (setq result
(concat "%%(and (diary-date "
2368 (cond ((eq calendar-date-style
'iso
)
2369 (format "t %d %d" month day
))
2370 ((eq calendar-date-style
'european
)
2371 (format "%d %d t" day month
))
2372 ((eq calendar-date-style
'american
)
2373 (format "%d %d t" month day
)))
2382 (setq result
(format
2383 "%%%%(and (diary-anniversary %s)) %s%s%s"
2386 (if end-t
"-" "") (or end-t
"")))))
2388 ((string-equal frequency
"MONTHLY")
2389 (icalendar--dmsg "monthly")
2392 "%%%%(and (diary-date %s) (diary-block %s %s)) %s%s%s"
2393 (let ((day (nth 3 dtstart-dec
)))
2394 (cond ((eq calendar-date-style
'iso
)
2395 (format "t t %d" day
))
2396 ((eq calendar-date-style
'european
)
2397 (format "%d t t" day
))
2398 ((eq calendar-date-style
'american
)
2399 (format "t %d t" day
))))
2403 (if (eq calendar-date-style
'iso
) "9999 1 1" "1 1 9999")) ;; FIXME: should be unlimited
2405 (if end-t
"-" "") (or end-t
""))))
2407 ((and (string-equal frequency
"DAILY"))
2411 (concat "%%%%(and (diary-cyclic %s %s) "
2412 "(diary-block %s %s)) %s%s%s")
2413 interval dtstart-conv dtstart-conv
2414 (if count until-1-conv until-conv
)
2416 (if end-t
"-" "") (or end-t
"")))
2419 "%%%%(and (diary-cyclic %s %s)) %s%s%s"
2423 (if end-t
"-" "") (or end-t
""))))))
2424 ;; Handle exceptions from recurrence rules
2425 (let ((ex-dates (icalendar--get-event-properties e
'EXDATE
)))
2427 (let* ((ex-start (icalendar--decode-isodatetime
2429 (ex-d (icalendar--datetime-to-diary-date
2432 (icalendar--rris "^%%(\\(and \\)?"
2434 "%%%%(and (not (diary-date %s)) "
2437 (setq ex-dates
(cdr ex-dates
))))
2438 ;; FIXME: exception rules are not recognized
2439 (if (icalendar--get-event-property e
'EXRULE
)
2442 "\n Exception rules: "
2443 (icalendar--get-event-properties
2447 (defun icalendar--convert-non-recurring-all-day-to-diary (event start-d end-d
)
2448 "Convert non-recurring iCalendar EVENT to diary format.
2450 DTSTART is the decoded DTSTART property of E.
2451 Argument START-D gives the first day.
2452 Argument END-D gives the last day."
2453 (icalendar--dmsg "non-recurring all-day event")
2454 (format "%%%%(and (diary-block %s %s))" start-d end-d
))
2456 (defun icalendar--convert-non-recurring-not-all-day-to-diary (event dtstart-dec
2460 "Convert recurring icalendar EVENT to diary format.
2462 DTSTART-DEC is the decoded DTSTART property of E.
2463 DTEND-DEC is the decoded DTEND property of E.
2464 START-T is the event's start time in diary format.
2465 END-T is the event's end time in diary format."
2466 (icalendar--dmsg "not all day event")
2469 (icalendar--datetime-to-diary-date
2474 (icalendar--datetime-to-diary-date
2478 (defun icalendar--add-diary-entry (string diary-file non-marking
2480 "Add STRING to the diary file DIARY-FILE.
2481 STRING must be a properly formatted valid diary entry. NON-MARKING
2482 determines whether diary events are created as non-marking. If
2483 SUMMARY is not nil it must be a string that gives the summary of the
2484 entry. In this case the user will be asked whether he wants to insert
2486 (when (or (not summary
)
2487 (y-or-n-p (format-message "Add appointment for `%s' to diary? "
2491 (y-or-n-p (format "Make appointment non-marking? "))))
2492 (save-window-excursion
2495 (read-file-name "Add appointment to this diary file: ")))
2496 ;; Note: diary-make-entry will add a trailing blank char.... :(
2497 (funcall (if (fboundp 'diary-make-entry
)
2500 string non-marking diary-file
)))
2501 ;; Würgaround to remove the trailing blank char
2502 (with-current-buffer (find-file diary-file
)
2503 (goto-char (point-max))
2504 (if (= (char-before) ?
)
2506 ;; return diary-file in case it has been changed interactively
2509 ;; ======================================================================
2511 ;; ======================================================================
2512 (defun icalendar-import-format-sample (event)
2513 "Example function for formatting an iCalendar EVENT."
2514 (format (concat "SUMMARY='%s' DESCRIPTION='%s' LOCATION='%s' ORGANIZER='%s' "
2515 "STATUS='%s' URL='%s' CLASS='%s'")
2516 (or (icalendar--get-event-property event
'SUMMARY
) "")
2517 (or (icalendar--get-event-property event
'DESCRIPTION
) "")
2518 (or (icalendar--get-event-property event
'LOCATION
) "")
2519 (or (icalendar--get-event-property event
'ORGANIZER
) "")
2520 (or (icalendar--get-event-property event
'STATUS
) "")
2521 (or (icalendar--get-event-property event
'URL
) "")
2522 (or (icalendar--get-event-property event
'CLASS
) "")))
2524 (provide 'icalendar
)
2526 ;;; icalendar.el ends here