1 ;;; icalendar.el --- iCalendar implementation -*-coding: utf-8 -*-
3 ;; Copyright (C) 2002-2012 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 lisp/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 (defvar icalendar-debug nil
244 "Enable icalendar debug messages.")
246 ;; ======================================================================
247 ;; NO USER SERVICEABLE PARTS BELOW THIS LINE
248 ;; ======================================================================
250 (defconst icalendar--weekday-array
["SU" "MO" "TU" "WE" "TH" "FR" "SA"])
252 ;; ======================================================================
253 ;; all the other libs we need
254 ;; ======================================================================
258 ;; ======================================================================
260 ;; ======================================================================
261 (defun icalendar--dmsg (&rest args
)
262 "Print message ARGS if `icalendar-debug' is non-nil."
264 (apply 'message args
)))
266 ;; ======================================================================
267 ;; Core functionality
268 ;; Functions for parsing icalendars, importing and so on
269 ;; ======================================================================
271 (defun icalendar--get-unfolded-buffer (folded-ical-buffer)
272 "Return a new buffer containing the unfolded contents of a buffer.
273 Folding is the iCalendar way of wrapping long lines. In the
274 created buffer all occurrences of CR LF BLANK are replaced by the
275 empty string. Argument FOLDED-ICAL-BUFFER is the unfolded input
277 (let ((unfolded-buffer (get-buffer-create " *icalendar-work*")))
279 (set-buffer unfolded-buffer
)
281 (insert-buffer-substring folded-ical-buffer
)
282 (goto-char (point-min))
283 (while (re-search-forward "\r?\n[ \t]" nil t
)
284 (replace-match "" nil nil
)))
287 (defsubst icalendar--rris
(regexp rep string
&optional fixedcase literal
)
288 "Replace regular expression in string.
289 Pass arguments REGEXP REP STRING FIXEDCASE LITERAL to
290 `replace-regexp-in-string' (Emacs) or to `replace-in-string' (XEmacs)."
291 (cond ((fboundp 'replace-regexp-in-string
)
293 (replace-regexp-in-string regexp rep string fixedcase literal
))
294 ((fboundp 'replace-in-string
)
296 (save-match-data ;; apparently XEmacs needs save-match-data
297 (replace-in-string string regexp rep literal
)))))
299 (defun icalendar--read-element (invalue inparams
)
300 "Recursively read the next iCalendar element in the current buffer.
301 INVALUE gives the current iCalendar element we are reading.
302 INPARAMS gives the current parameters.....
303 This function calls itself recursively for each nested calendar element
305 (let (element children line name params param param-name param-value
310 (re-search-forward "^\\([A-Za-z0-9-]+\\)[;:]" nil t
))
311 (setq name
(intern (match-string 1)))
315 (while (looking-at ";")
316 (re-search-forward ";\\([A-Za-z0-9-]+\\)=" nil nil
)
317 (setq param-name
(intern (match-string 1)))
318 (re-search-forward "\\(\\([^;,:\"]+\\)\\|\"\\([^\"]+\\)\"\\)[;:]"
321 (setq param-value
(or (match-string 2) (match-string 3)))
322 (setq param
(list param-name param-value
))
323 (while (looking-at ",")
324 (re-search-forward "\\(\\([^;,:]+\\)\\|\"\\([^\"]+\\)\"\\)"
327 (setq param-value
(match-string 2))
328 (setq param-value
(match-string 3)))
329 (setq param
(append param param-value
)))
330 (setq params
(append params param
)))
331 (unless (looking-at ":")
334 (re-search-forward "\\(.*\\)\\(\r?\n[ \t].*\\)*" nil t
)
335 (setq value
(icalendar--rris "\r?\n[ \t]" "" (match-string 0)))
336 (setq line
(list name params value
))
337 (cond ((eq name
'BEGIN
)
340 (list (icalendar--read-element (intern value
)
345 (setq element
(append element
(list line
))))))
347 (list invalue inparams element children
)
350 ;; ======================================================================
351 ;; helper functions for examining events
352 ;; ======================================================================
354 ;;(defsubst icalendar--get-all-event-properties (event)
355 ;; "Return the list of properties in this EVENT."
356 ;; (car (cddr event)))
358 (defun icalendar--get-event-property (event prop
)
359 "For the given EVENT return the value of the first occurrence of PROP."
361 (let ((props (car (cddr event
))) pp
)
363 (setq pp
(car props
))
364 (if (eq (car pp
) prop
)
365 (throw 'found
(car (cddr pp
))))
366 (setq props
(cdr props
))))
369 (defun icalendar--get-event-property-attributes (event prop
)
370 "For the given EVENT return attributes of the first occurrence of PROP."
372 (let ((props (car (cddr event
))) pp
)
374 (setq pp
(car props
))
375 (if (eq (car pp
) prop
)
376 (throw 'found
(cadr pp
)))
377 (setq props
(cdr props
))))
380 (defun icalendar--get-event-properties (event prop
)
381 "For the given EVENT return a list of all values of the property PROP."
382 (let ((props (car (cddr event
))) pp result
)
384 (setq pp
(car props
))
385 (if (eq (car pp
) prop
)
386 (setq result
(append (split-string (car (cddr pp
)) ",") result
)))
387 (setq props
(cdr props
)))
390 ;; (defun icalendar--set-event-property (event prop new-value)
391 ;; "For the given EVENT set the property PROP to the value NEW-VALUE."
393 ;; (let ((props (car (cddr event))) pp)
395 ;; (setq pp (car props))
396 ;; (when (eq (car pp) prop)
397 ;; (setcdr (cdr pp) new-value)
398 ;; (throw 'found (car (cddr pp))))
399 ;; (setq props (cdr props)))
400 ;; (setq props (car (cddr event)))
401 ;; (setcar (cddr event)
402 ;; (append props (list (list prop nil new-value)))))))
404 (defun icalendar--get-children (node name
)
405 "Return all children of the given NODE which have a name NAME.
406 For instance the VCALENDAR node can have VEVENT children as well as VTODO
409 (children (cadr (cddr node
))))
410 (when (eq (car node
) name
)
412 ;;(message "%s" node)
417 (icalendar--get-children n name
))
421 (setq result
(append result subresult
))
422 (setq result subresult
)))))
426 (defun icalendar--all-events (icalendar)
427 "Return the list of all existing events in the given ICALENDAR."
430 (setq result
(append (icalendar--get-children elt
'VEVENT
)
432 (nreverse icalendar
))
435 (defun icalendar--split-value (value-string)
436 "Split VALUE-STRING at ';='."
438 param-name param-value
)
441 (set-buffer (get-buffer-create " *icalendar-work*"))
442 (set-buffer-modified-p nil
)
444 (insert value-string
)
445 (goto-char (point-min))
448 "\\([A-Za-z0-9-]+\\)=\\(\\([^;:]+\\)\\|\"\\([^\"]+\\)\"\\);?"
450 (setq param-name
(intern (match-string 1)))
451 (setq param-value
(match-string 2))
453 (append result
(list (list param-name param-value
)))))))
456 (defun icalendar--convert-tz-offset (alist dst-p
)
457 "Return a cons of two strings representing a timezone start.
458 ALIST is an alist entry from a VTIMEZONE, like STANDARD.
459 DST-P is non-nil if this is for daylight savings time.
460 The strings are suitable for assembling into a TZ variable."
461 (let ((offset (car (cddr (assq 'TZOFFSETTO alist
))))
462 (rrule-value (car (cddr (assq 'RRULE alist
))))
463 (dtstart (car (cddr (assq 'DTSTART alist
)))))
464 ;; FIXME: for now we only handle RRULE and not RDATE here.
465 (when (and offset rrule-value dtstart
)
466 (let* ((rrule (icalendar--split-value rrule-value
))
467 (freq (cadr (assq 'FREQ rrule
)))
468 (bymonth (cadr (assq 'BYMONTH rrule
)))
469 (byday (cadr (assq 'BYDAY rrule
))))
470 ;; FIXME: we don't correctly handle WKST here.
471 (if (and (string= freq
"YEARLY") bymonth
)
475 (if dst-p
"DST" "STD")
476 ;; For TZ, OFFSET is added to the local time. So,
477 ;; invert the values.
478 (if (eq (aref offset
0) ?-
) "+" "-")
479 (substring offset
1 3)
481 (substring offset
3 5))
483 (let* ((day (icalendar--get-weekday-number (substring byday -
2)))
484 (week (if (eq day -
1)
486 (substring byday
0 -
2))))
487 ;; "Translate" the iCalendar way to specify the last
488 ;; (sun|mon|...)day in month to the tzset way.
489 (if (string= week
"-1") ; last day as iCalendar calls it
490 (setq week
"5")) ; last day as tzset calls it
491 (concat "M" bymonth
"." week
"." (if (eq day -
1) "0"
495 (substring dtstart -
6 -
4)
497 (substring dtstart -
4 -
2)
499 (substring dtstart -
2)))))))))
501 (defun icalendar--parse-vtimezone (alist)
502 "Turn a VTIMEZONE ALIST into a cons (ID . TZ-STRING).
503 Return nil if timezone cannot be parsed."
504 (let* ((tz-id (icalendar--convert-string-for-import
505 (icalendar--get-event-property alist
'TZID
)))
506 (daylight (cadr (cdar (icalendar--get-children alist
'DAYLIGHT
))))
507 (day (and daylight
(icalendar--convert-tz-offset daylight t
)))
508 (standard (cadr (cdar (icalendar--get-children alist
'STANDARD
))))
509 (std (and standard
(icalendar--convert-tz-offset standard nil
))))
513 (concat (car std
) (car day
)
514 "," (cdr day
) "," (cdr std
))
517 (defun icalendar--convert-all-timezones (icalendar)
518 "Convert all timezones in the ICALENDAR into an alist.
519 Each element of the alist is a cons (ID . TZ-STRING),
520 like `icalendar--parse-vtimezone'."
522 (dolist (zone (icalendar--get-children (car icalendar
) 'VTIMEZONE
))
523 (setq zone
(icalendar--parse-vtimezone zone
))
525 (setq result
(cons zone result
))))
528 (defun icalendar--find-time-zone (prop-list zone-map
)
529 "Return a timezone string for the time zone in PROP-LIST, or nil if none.
530 ZONE-MAP is a timezone alist as returned by `icalendar--convert-all-timezones'."
531 (let ((id (plist-get prop-list
'TZID
)))
533 (cdr (assoc id zone-map
)))))
535 (defun icalendar--decode-isodatetime (isodatetimestring &optional day-shift
537 "Return ISODATETIMESTRING in format like `decode-time'.
538 Converts from ISO-8601 to Emacs representation. If
539 ISODATETIMESTRING specifies UTC time (trailing letter Z) the
540 decoded time is given in the local time zone! If optional
541 parameter DAY-SHIFT is non-nil the result is shifted by DAY-SHIFT
543 ZONE, if provided, is the timezone, in any format understood by `encode-time'.
545 FIXME: multiple comma-separated values should be allowed!"
546 (icalendar--dmsg isodatetimestring
)
547 (if isodatetimestring
548 ;; day/month/year must be present
549 (let ((year (read (substring isodatetimestring
0 4)))
550 (month (read (substring isodatetimestring
4 6)))
551 (day (read (substring isodatetimestring
6 8)))
555 (when (> (length isodatetimestring
) 12)
556 ;; hour/minute present
557 (setq hour
(read (substring isodatetimestring
9 11)))
558 (setq minute
(read (substring isodatetimestring
11 13))))
559 (when (> (length isodatetimestring
) 14)
561 (setq second
(read (substring isodatetimestring
13 15))))
562 (when (and (> (length isodatetimestring
) 15)
563 ;; UTC specifier present
564 (char-equal ?Z
(aref isodatetimestring
15)))
565 ;; if not UTC add current-time-zone offset
566 (setq second
(+ (car (current-time-zone)) second
)))
567 ;; shift if necessary
569 (let ((mdy (calendar-gregorian-from-absolute
570 (+ (calendar-absolute-from-gregorian
571 (list month day year
))
573 (setq month
(nth 0 mdy
))
574 (setq day
(nth 1 mdy
))
575 (setq year
(nth 2 mdy
))))
576 ;; create the decoded date-time
579 (decode-time (encode-time second minute hour day month year zone
))
581 (message "Cannot decode \"%s\"" isodatetimestring
)
582 ;; hope for the best...
583 (list second minute hour day month year
0 nil
0))))
584 ;; isodatetimestring == nil
587 (defun icalendar--decode-isoduration (isodurationstring
588 &optional duration-correction
)
589 "Convert ISODURATIONSTRING into format provided by `decode-time'.
590 Converts from ISO-8601 to Emacs representation. If ISODURATIONSTRING
591 specifies UTC time (trailing letter Z) the decoded time is given in
594 Optional argument DURATION-CORRECTION shortens result by one day.
596 FIXME: TZID-attributes are ignored....!
597 FIXME: multiple comma-separated values should be allowed!"
598 (if isodurationstring
603 "\\(\\([0-9]+\\)D\\)" ; days only
605 "\\(\\(\\([0-9]+\\)D\\)?T\\(\\([0-9]+\\)H\\)?" ; opt days
606 "\\(\\([0-9]+\\)M\\)?\\(\\([0-9]+\\)S\\)?\\)" ; mand. time
608 "\\(\\([0-9]+\\)W\\)" ; weeks only
609 "\\)$") isodurationstring
)
617 ((match-beginning 2) ;days only
618 (setq days
(read (substring isodurationstring
621 (when duration-correction
622 (setq days
(1- days
))))
623 ((match-beginning 4) ;days and time
624 (if (match-beginning 5)
625 (setq days
(* 7 (read (substring isodurationstring
628 (if (match-beginning 7)
629 (setq hours
(read (substring isodurationstring
632 (if (match-beginning 9)
633 (setq minutes
(read (substring isodurationstring
636 (if (match-beginning 11)
637 (setq seconds
(read (substring isodurationstring
640 ((match-beginning 13) ;weeks only
641 (setq days
(* 7 (read (substring isodurationstring
644 (list seconds minutes hours days months years
)))
645 ;; isodatetimestring == nil
648 (defun icalendar--add-decoded-times (time1 time2
)
650 Both times must be given in decoded form. One of these times must be
651 valid (year > 1900 or something)."
652 ;; FIXME: does this function exist already?
653 (decode-time (encode-time
654 (+ (nth 0 time1
) (nth 0 time2
))
655 (+ (nth 1 time1
) (nth 1 time2
))
656 (+ (nth 2 time1
) (nth 2 time2
))
657 (+ (nth 3 time1
) (nth 3 time2
))
658 (+ (nth 4 time1
) (nth 4 time2
))
659 (+ (nth 5 time1
) (nth 5 time2
))
662 ;;(or (nth 6 time1) (nth 6 time2)) ;; FIXME?
665 (defun icalendar--datetime-to-american-date (datetime &optional separator
)
666 "Convert the decoded DATETIME to American-style format.
667 Optional argument SEPARATOR gives the separator between month,
668 day, and year. If nil a blank character is used as separator.
669 American format: \"month day year\"."
671 (format "%d%s%d%s%d" (nth 4 datetime
) ;month
673 (nth 3 datetime
) ;day
675 (nth 5 datetime
)) ;year
679 (define-obsolete-function-alias 'icalendar--datetime-to-noneuropean-date
680 'icalendar--datetime-to-american-date
"icalendar 0.19")
682 (defun icalendar--datetime-to-european-date (datetime &optional separator
)
683 "Convert the decoded DATETIME to European format.
684 Optional argument SEPARATOR gives the separator between month,
685 day, and year. If nil a blank character is used as separator.
686 European format: (day month year).
689 (format "%d%s%d%s%d" (nth 3 datetime
) ;day
691 (nth 4 datetime
) ;month
693 (nth 5 datetime
)) ;year
697 (defun icalendar--datetime-to-iso-date (datetime &optional separator
)
698 "Convert the decoded DATETIME to ISO format.
699 Optional argument SEPARATOR gives the separator between month,
700 day, and year. If nil a blank character is used as separator.
701 ISO format: (year month day)."
703 (format "%d%s%d%s%d" (nth 5 datetime
) ;year
705 (nth 4 datetime
) ;month
707 (nth 3 datetime
)) ;day
711 (defun icalendar--date-style ()
712 "Return current calendar date style.
713 Convenience function to handle transition from old
714 `european-calendar-style' to new `calendar-date-style'."
715 (if (boundp 'calendar-date-style
)
717 (if (with-no-warnings european-calendar-style
)
721 (defun icalendar--datetime-to-diary-date (datetime &optional separator
)
722 "Convert the decoded DATETIME to diary format.
723 Optional argument SEPARATOR gives the separator between month,
724 day, and year. If nil a blank character is used as separator.
725 Call icalendar--datetime-to-*-date according to the current
726 calendar date style."
727 (funcall (intern-soft (format "icalendar--datetime-to-%s-date"
728 (icalendar--date-style)))
731 (defun icalendar--datetime-to-colontime (datetime)
732 "Extract the time part of a decoded DATETIME into 24-hour format.
733 Note that this silently ignores seconds."
734 (format "%02d:%02d" (nth 2 datetime
) (nth 1 datetime
)))
736 (defun icalendar--get-month-number (monthname)
737 "Return the month number for the given MONTHNAME."
740 (m (downcase monthname
)))
741 (mapc (lambda (month)
742 (let ((mm (downcase month
)))
743 (if (or (string-equal mm m
)
744 (string-equal (substring mm
0 3) m
))
746 (setq num
(1+ num
))))
747 calendar-month-name-array
))
751 (defun icalendar--get-weekday-number (abbrevweekday)
752 "Return the number for the ABBREVWEEKDAY."
756 (aw (downcase abbrevweekday
)))
758 (let ((d (downcase day
)))
759 (if (string-equal d aw
)
761 (setq num
(1+ num
))))
762 icalendar--weekday-array
)))
766 (defun icalendar--get-weekday-numbers (abbrevweekdays)
767 "Return the list of numbers for the comma-separated ABBREVWEEKDAYS."
770 (weekday-alist (mapcar (lambda (day)
773 (cons (downcase day
) num
)))
774 icalendar--weekday-array
)))
776 (mapcar (lambda (abbrevday)
777 (cdr (assoc abbrevday weekday-alist
)))
778 (split-string (downcase abbrevweekdays
) ","))))))
780 (defun icalendar--get-weekday-abbrev (weekday)
781 "Return the abbreviated WEEKDAY."
784 (w (downcase weekday
)))
786 (let ((d (downcase day
)))
787 (if (or (string-equal d w
)
788 (string-equal (substring d
0 3) w
))
789 (throw 'found
(aref icalendar--weekday-array num
)))
790 (setq num
(1+ num
))))
791 calendar-day-name-array
))
795 (defun icalendar--date-to-isodate (date &optional day-shift
)
796 "Convert DATE to iso-style date.
797 DATE must be a list of the form (month day year).
798 If DAY-SHIFT is non-nil, the result is shifted by DAY-SHIFT days."
799 (let ((mdy (calendar-gregorian-from-absolute
800 (+ (calendar-absolute-from-gregorian date
)
802 (format "%04d%02d%02d" (nth 2 mdy
) (nth 0 mdy
) (nth 1 mdy
))))
805 (defun icalendar--datestring-to-isodate (datestring &optional day-shift
)
806 "Convert diary-style DATESTRING to iso-style date.
807 If DAY-SHIFT is non-nil, the result is shifted by DAY-SHIFT days
808 -- DAY-SHIFT must be either nil or an integer. This function
809 tries to figure the date style from DATESTRING itself. If that
810 is not possible it uses the current calendar date style."
811 (let ((day -
1) month year
)
813 (cond ( ;; iso-style numeric date
814 (string-match (concat "\\s-*"
815 "\\([0-9]\\{4\\}\\)[ \t/]\\s-*"
816 "0?\\([1-9][0-9]?\\)[ \t/]\\s-*"
817 "0?\\([1-9][0-9]?\\)")
819 (setq year
(read (substring datestring
(match-beginning 1)
821 (setq month
(read (substring datestring
(match-beginning 2)
823 (setq day
(read (substring datestring
(match-beginning 3)
825 ( ;; non-iso numeric date -- must rely on configured
827 (string-match (concat "\\s-*"
828 "0?\\([1-9][0-9]?\\)[ \t/]\\s-*"
829 "0?\\([1-9][0-9]?\\),?[ \t/]\\s-*"
830 "\\([0-9]\\{4\\}\\)")
832 (setq day
(read (substring datestring
(match-beginning 1)
834 (setq month
(read (substring datestring
(match-beginning 2)
836 (setq year
(read (substring datestring
(match-beginning 3)
838 (if (eq (icalendar--date-style) 'american
)
842 ( ;; date contains month names -- iso style
843 (string-match (concat "\\s-*"
844 "\\([0-9]\\{4\\}\\)[ \t/]\\s-*"
845 "\\([A-Za-z][^ ]+\\)[ \t/]\\s-*"
846 "0?\\([123]?[0-9]\\)")
848 (setq year
(read (substring datestring
(match-beginning 1)
850 (setq month
(icalendar--get-month-number
851 (substring datestring
(match-beginning 2)
853 (setq day
(read (substring datestring
(match-beginning 3)
855 ( ;; date contains month names -- european style
856 (string-match (concat "\\s-*"
857 "0?\\([123]?[0-9]\\)[ \t/]\\s-*"
858 "\\([A-Za-z][^ ]+\\)[ \t/]\\s-*"
859 "\\([0-9]\\{4\\}\\)")
861 (setq day
(read (substring datestring
(match-beginning 1)
863 (setq month
(icalendar--get-month-number
864 (substring datestring
(match-beginning 2)
866 (setq year
(read (substring datestring
(match-beginning 3)
868 ( ;; date contains month names -- american style
869 (string-match (concat "\\s-*"
870 "\\([A-Za-z][^ ]+\\)[ \t/]\\s-*"
871 "0?\\([123]?[0-9]\\),?[ \t/]\\s-*"
872 "\\([0-9]\\{4\\}\\)")
874 (setq day
(read (substring datestring
(match-beginning 2)
876 (setq month
(icalendar--get-month-number
877 (substring datestring
(match-beginning 1)
879 (setq year
(read (substring datestring
(match-beginning 3)
884 (let ((mdy (calendar-gregorian-from-absolute
885 (+ (calendar-absolute-from-gregorian (list month day
888 (icalendar--dmsg (format "%04d%02d%02d" (nth 2 mdy
) (nth 0 mdy
) (nth 1 mdy
)))
889 (format "%04d%02d%02d" (nth 2 mdy
) (nth 0 mdy
) (nth 1 mdy
)))
892 (defun icalendar--diarytime-to-isotime (timestring ampmstring
)
893 "Convert a time like 9:30pm to an iso-conform string like T213000.
894 In this example the TIMESTRING would be \"9:30\" and the AMPMSTRING
897 (let ((starttimenum (read (icalendar--rris ":" "" timestring
))))
898 ;; take care of am/pm style
899 ;; Be sure *not* to convert 12:00pm - 12:59pm to 2400-2459
900 (if (and ampmstring
(string= "pm" ampmstring
) (< starttimenum
1200))
901 (setq starttimenum
(+ starttimenum
1200)))
902 ;; Similar effect with 12:00am - 12:59am (need to convert to 0000-0059)
903 (if (and ampmstring
(string= "am" ampmstring
) (>= starttimenum
1200))
904 (setq starttimenum
(- starttimenum
1200)))
905 (format "T%04d00" starttimenum
))
908 (defun icalendar--convert-string-for-export (string)
909 "Escape comma and other critical characters in STRING."
910 (icalendar--rris "," "\\\\," string
))
912 (defun icalendar--convert-string-for-import (string)
913 "Remove escape chars for comma, semicolon etc. from STRING."
915 "\\\\n" "\n " (icalendar--rris
916 "\\\\\"" "\"" (icalendar--rris
917 "\\\\;" ";" (icalendar--rris
918 "\\\\," "," string
)))))
920 ;; ======================================================================
921 ;; Export -- convert emacs-diary to iCalendar
922 ;; ======================================================================
925 (defun icalendar-export-file (diary-filename ical-filename
)
926 "Export diary file to iCalendar format.
927 All diary entries in the file DIARY-FILENAME are converted to iCalendar
928 format. The result is appended to the file ICAL-FILENAME."
929 (interactive "FExport diary data from file: \n\
930 Finto iCalendar file: ")
932 (set-buffer (find-file diary-filename
))
933 (icalendar-export-region (point-min) (point-max) ical-filename
)))
935 (define-obsolete-function-alias 'icalendar-convert-diary-to-ical
936 'icalendar-export-file
"22.1")
938 (defvar icalendar--uid-count
0
939 "Auxiliary counter for creating unique ids.")
941 (defun icalendar--create-uid (entry-full contents
)
942 "Construct a unique iCalendar UID for a diary entry.
943 ENTRY-FULL is the full diary entry string. CONTENTS is the
944 current iCalendar object, as a string. Increase
945 `icalendar--uid-count'. Returns the UID string."
946 (let ((uid icalendar-uid-format
))
948 ;; Allow other apps (such as org-mode) to create its own uid
949 (get-text-property 0 'uid entry-full
)
950 (setq uid
(get-text-property 0 'uid entry-full
))
951 (setq uid
(replace-regexp-in-string
953 (format "%d" icalendar--uid-count
)
955 (setq icalendar--uid-count
(1+ icalendar--uid-count
))
956 (setq uid
(replace-regexp-in-string
958 (format "%d%d%d" (car (current-time))
959 (cadr (current-time))
960 (car (cddr (current-time))))
962 (setq uid
(replace-regexp-in-string
964 (format "%d" (abs (sxhash entry-full
))) uid t t
))
965 (setq uid
(replace-regexp-in-string
966 "%u" (or user-login-name
"UNKNOWN_USER") uid t t
))
967 (let ((dtstart (if (string-match "^DTSTART[^:]*:\\([0-9]*\\)" contents
)
968 (substring contents
(match-beginning 1) (match-end 1))
970 (setq uid
(replace-regexp-in-string "%s" dtstart uid t t
))))
972 ;; Return the UID string
976 (defun icalendar-export-region (min max ical-filename
)
977 "Export region in diary file to iCalendar format.
978 All diary entries in the region from MIN to MAX in the current buffer are
979 converted to iCalendar format. The result is appended to the file
981 This function attempts to return t if something goes wrong. In this
982 case an error string which describes all the errors and problems is
983 written into the buffer `*icalendar-errors*'."
985 FExport diary data into iCalendar file: ")
995 (nonmarker (concat "^" (regexp-quote diary-nonmarking-symbol
)
997 (other-elements nil
))
998 ;; prepare buffer with error messages
1000 (set-buffer (get-buffer-create "*icalendar-errors*"))
1006 (while (re-search-forward
1007 ;; possibly ignore hidden entries beginning with "&"
1008 (if icalendar-export-hidden-diary-entries
1009 "^\\([^ \t\n#].+\\)\\(\\(\n[ \t].*\\)*\\)"
1010 "^\\([^ \t\n&#].+\\)\\(\\(\n[ \t].*\\)*\\)") max t
)
1011 (setq entry-main
(match-string 1))
1012 (if (match-beginning 2)
1013 (setq entry-rest
(match-string 2))
1014 (setq entry-rest
""))
1015 (setq entry-full
(concat entry-main entry-rest
))
1017 (condition-case error-val
1019 (setq contents-n-summary
1020 (icalendar--convert-to-ical nonmarker entry-main
))
1021 (setq other-elements
(icalendar--parse-summary-and-rest
1023 (setq contents
(concat (car contents-n-summary
)
1024 "\nSUMMARY:" (cadr contents-n-summary
)))
1025 (let ((cla (cdr (assoc 'cla other-elements
)))
1026 (des (cdr (assoc 'des other-elements
)))
1027 (loc (cdr (assoc 'loc other-elements
)))
1028 (org (cdr (assoc 'org other-elements
)))
1029 (sta (cdr (assoc 'sta other-elements
)))
1030 (sum (cdr (assoc 'sum other-elements
)))
1031 (url (cdr (assoc 'url other-elements
)))
1032 (uid (cdr (assoc 'uid other-elements
))))
1034 (setq contents
(concat contents
"\nCLASS:" cla
)))
1036 (setq contents
(concat contents
"\nDESCRIPTION:" des
)))
1038 (setq contents
(concat contents
"\nLOCATION:" loc
)))
1040 (setq contents
(concat contents
"\nORGANIZER:" org
)))
1042 (setq contents
(concat contents
"\nSTATUS:" sta
)))
1044 ;; (setq contents (concat contents "\nSUMMARY:" sum)))
1046 (setq contents
(concat contents
"\nURL:" url
)))
1048 (setq header
(concat "\nBEGIN:VEVENT\nUID:"
1050 (icalendar--create-uid entry-full
1052 (setq result
(concat result header contents
"\nEND:VEVENT")))
1055 (setq found-error t
)
1056 (save-current-buffer
1057 (set-buffer (get-buffer-create "*icalendar-errors*"))
1058 (insert (format "Error in line %d -- %s: `%s'\n"
1059 (count-lines (point-min) (point))
1063 ;; we're done, insert everything into the file
1064 (save-current-buffer
1065 (let ((coding-system-for-write 'utf-8
))
1066 (set-buffer (find-file ical-filename
))
1067 (goto-char (point-max))
1068 (insert "BEGIN:VCALENDAR")
1069 (insert "\nPRODID:-//Emacs//NONSGML icalendar.el//EN")
1070 (insert "\nVERSION:2.0")
1072 (insert "\nEND:VCALENDAR\n")
1073 ;; save the diary file
1079 (defun icalendar--convert-to-ical (nonmarker entry-main
)
1080 "Convert a diary entry to iCalendar format.
1081 NONMARKER is a regular expression matching the start of non-marking
1082 entries. ENTRY-MAIN is the first line of the diary entry."
1084 ;; anniversaries -- %%(diary-anniversary ...)
1085 (icalendar--convert-anniversary-to-ical nonmarker entry-main
)
1086 ;; cyclic events -- %%(diary-cyclic ...)
1087 (icalendar--convert-cyclic-to-ical nonmarker entry-main
)
1088 ;; diary-date -- %%(diary-date ...)
1089 (icalendar--convert-date-to-ical nonmarker entry-main
)
1090 ;; float events -- %%(diary-float ...)
1091 (icalendar--convert-float-to-ical nonmarker entry-main
)
1092 ;; block events -- %%(diary-block ...)
1093 (icalendar--convert-block-to-ical nonmarker entry-main
)
1094 ;; other sexp diary entries
1095 (icalendar--convert-sexp-to-ical nonmarker entry-main
)
1096 ;; weekly by day -- Monday 8:30 Team meeting
1097 (icalendar--convert-weekly-to-ical nonmarker entry-main
)
1098 ;; yearly by day -- 1 May Tag der Arbeit
1099 (icalendar--convert-yearly-to-ical nonmarker entry-main
)
1100 ;; "ordinary" events, start and end time given
1102 (icalendar--convert-ordinary-to-ical nonmarker entry-main
)
1104 ;; Oops! what's that?
1105 (error "Could not parse entry")))
1107 (defun icalendar--parse-summary-and-rest (summary-and-rest)
1108 "Parse SUMMARY-AND-REST from a diary to fill iCalendar properties.
1111 (if (functionp icalendar-import-format
)
1112 ;; can't do anything
1114 ;; split summary-and-rest
1115 (let* ((case-fold-search nil
)
1116 (s icalendar-import-format
)
1117 (p-cla (or (string-match "%c" icalendar-import-format
) -
1))
1118 (p-des (or (string-match "%d" icalendar-import-format
) -
1))
1119 (p-loc (or (string-match "%l" icalendar-import-format
) -
1))
1120 (p-org (or (string-match "%o" icalendar-import-format
) -
1))
1121 (p-sum (or (string-match "%s" icalendar-import-format
) -
1))
1122 (p-sta (or (string-match "%t" icalendar-import-format
) -
1))
1123 (p-url (or (string-match "%u" icalendar-import-format
) -
1))
1124 (p-uid (or (string-match "%U" icalendar-import-format
) -
1))
1125 (p-list (sort (list p-cla p-des p-loc p-org p-sta p-sum p-url p-uid
) '<))
1127 pos-cla pos-des pos-loc pos-org pos-sta pos-sum pos-url pos-uid
)
1128 (dotimes (i (length p-list
))
1129 ;; Use 'ct' to keep track of current position in list
1130 (cond ((and (>= p-cla
0) (= (nth i p-list
) p-cla
))
1132 (setq pos-cla
(* 2 ct
)))
1133 ((and (>= p-des
0) (= (nth i p-list
) p-des
))
1135 (setq pos-des
(* 2 ct
)))
1136 ((and (>= p-loc
0) (= (nth i p-list
) p-loc
))
1138 (setq pos-loc
(* 2 ct
)))
1139 ((and (>= p-org
0) (= (nth i p-list
) p-org
))
1141 (setq pos-org
(* 2 ct
)))
1142 ((and (>= p-sta
0) (= (nth i p-list
) p-sta
))
1144 (setq pos-sta
(* 2 ct
)))
1145 ((and (>= p-sum
0) (= (nth i p-list
) p-sum
))
1147 (setq pos-sum
(* 2 ct
)))
1148 ((and (>= p-url
0) (= (nth i p-list
) p-url
))
1150 (setq pos-url
(* 2 ct
)))
1151 ((and (>= p-uid
0) (= (nth i p-list
) p-uid
))
1153 (setq pos-uid
(* 2 ct
)))) )
1155 (setq s
(icalendar--rris (car ij
) (cadr ij
) s t t
)))
1157 ;; summary must be first! because of %s
1159 (concat "\\(" icalendar-import-format-summary
"\\)??"))
1161 (concat "\\(" icalendar-import-format-class
"\\)??"))
1163 (concat "\\(" icalendar-import-format-description
"\\)??"))
1165 (concat "\\(" icalendar-import-format-location
"\\)??"))
1167 (concat "\\(" icalendar-import-format-organizer
"\\)??"))
1169 (concat "\\(" icalendar-import-format-status
"\\)??"))
1171 (concat "\\(" icalendar-import-format-url
"\\)??"))
1173 (concat "\\(" icalendar-import-format-uid
"\\)??"))))
1174 ;; Need the \' regexp in order to detect multi-line items
1175 (setq s
(concat "\\`"
1176 (icalendar--rris "%s" "\\(.*?\\)" s nil t
)
1178 (if (string-match s summary-and-rest
)
1179 (let (cla des loc org sta sum url uid
)
1180 (if (and pos-sum
(match-beginning pos-sum
))
1181 (setq sum
(substring summary-and-rest
1182 (match-beginning pos-sum
)
1183 (match-end pos-sum
))))
1184 (if (and pos-cla
(match-beginning pos-cla
))
1185 (setq cla
(substring summary-and-rest
1186 (match-beginning pos-cla
)
1187 (match-end pos-cla
))))
1188 (if (and pos-des
(match-beginning pos-des
))
1189 (setq des
(substring summary-and-rest
1190 (match-beginning pos-des
)
1191 (match-end pos-des
))))
1192 (if (and pos-loc
(match-beginning pos-loc
))
1193 (setq loc
(substring summary-and-rest
1194 (match-beginning pos-loc
)
1195 (match-end pos-loc
))))
1196 (if (and pos-org
(match-beginning pos-org
))
1197 (setq org
(substring summary-and-rest
1198 (match-beginning pos-org
)
1199 (match-end pos-org
))))
1200 (if (and pos-sta
(match-beginning pos-sta
))
1201 (setq sta
(substring summary-and-rest
1202 (match-beginning pos-sta
)
1203 (match-end pos-sta
))))
1204 (if (and pos-url
(match-beginning pos-url
))
1205 (setq url
(substring summary-and-rest
1206 (match-beginning pos-url
)
1207 (match-end pos-url
))))
1208 (if (and pos-uid
(match-beginning pos-uid
))
1209 (setq uid
(substring summary-and-rest
1210 (match-beginning pos-uid
)
1211 (match-end pos-uid
))))
1212 (list (if cla
(cons 'cla cla
) nil
)
1213 (if des
(cons 'des des
) nil
)
1214 (if loc
(cons 'loc loc
) nil
)
1215 (if org
(cons 'org org
) nil
)
1216 (if sta
(cons 'sta sta
) nil
)
1217 ;;(if sum (cons 'sum sum) nil)
1218 (if url
(cons 'url url
) nil
)
1219 (if uid
(cons 'uid uid
) nil
))))))))
1221 ;; subroutines for icalendar-export-region
1222 (defun icalendar--convert-ordinary-to-ical (nonmarker entry-main
)
1223 "Convert \"ordinary\" diary entry to iCalendar format.
1224 NONMARKER is a regular expression matching the start of non-marking
1225 entries. ENTRY-MAIN is the first line of the diary entry."
1228 "\\([^ /]+[ /]+[^ /]+[ /]+[^ ]+\\)\\s-*" ; date
1229 "\\(\\([0-9][0-9]?:[0-9][0-9]\\)\\([ap]m\\)?" ; start time
1231 "-\\([0-9][0-9]?:[0-9][0-9]\\)\\([ap]m\\)?\\)?" ; end time
1233 "\\s-*\\(.*?\\) ?$")
1235 (let* ((datetime (substring entry-main
(match-beginning 1)
1237 (startisostring (icalendar--datestring-to-isodate
1239 (endisostring (icalendar--datestring-to-isodate
1242 (starttimestring (icalendar--diarytime-to-isotime
1243 (if (match-beginning 3)
1244 (substring entry-main
1248 (if (match-beginning 4)
1249 (substring entry-main
1253 (endtimestring (icalendar--diarytime-to-isotime
1254 (if (match-beginning 6)
1255 (substring entry-main
1259 (if (match-beginning 7)
1260 (substring entry-main
1264 (summary (icalendar--convert-string-for-export
1265 (substring entry-main
(match-beginning 8)
1267 (icalendar--dmsg "ordinary %s" entry-main
)
1269 (unless startisostring
1270 (error "Could not parse date"))
1272 ;; If only start-date is specified, then end-date is next day,
1273 ;; otherwise it is same day.
1274 (setq endisostring1
(if starttimestring
1278 (when starttimestring
1279 (unless endtimestring
1281 (read (icalendar--rris "^T0?" ""
1284 ;; Case: ends on same day
1285 (setq endtimestring
(format "T%06d"
1287 ;; Case: ends on next day
1288 (setq endtimestring
(format "T%06d"
1290 (setq endisostring1 endisostring
)) )))
1292 (list (concat "\nDTSTART;"
1293 (if starttimestring
"VALUE=DATE-TIME:"
1296 (or starttimestring
"")
1298 (if endtimestring
"VALUE=DATE-TIME:"
1301 (or endtimestring
""))
1306 (defun icalendar-first-weekday-of-year (abbrevweekday year
)
1307 "Find the first ABBREVWEEKDAY in a given YEAR.
1308 Returns day number."
1309 (let* ((day-of-week-jan01 (calendar-day-of-week (list 1 1 year
)))
1311 (- (icalendar--get-weekday-number abbrevweekday
)
1312 day-of-week-jan01
))))
1313 (cond ((<= result
0)
1314 (setq result
(+ result
7)))
1316 (setq result
(- result
7))))
1319 (defun icalendar--convert-weekly-to-ical (nonmarker entry-main
)
1320 "Convert weekly diary entry to iCalendar format.
1321 NONMARKER is a regular expression matching the start of non-marking
1322 entries. ENTRY-MAIN is the first line of the diary entry."
1323 (if (and (string-match (concat nonmarker
1325 "\\(\\([0-9][0-9]?:[0-9][0-9]\\)"
1328 "\\([0-9][0-9]?:[0-9][0-9]\\)"
1331 "\\s-*\\(.*?\\) ?$")
1333 (icalendar--get-weekday-abbrev
1334 (substring entry-main
(match-beginning 1)
1336 (let* ((day (icalendar--get-weekday-abbrev
1337 (substring entry-main
(match-beginning 1)
1339 (starttimestring (icalendar--diarytime-to-isotime
1340 (if (match-beginning 3)
1341 (substring entry-main
1345 (if (match-beginning 4)
1346 (substring entry-main
1350 (endtimestring (icalendar--diarytime-to-isotime
1351 (if (match-beginning 6)
1352 (substring entry-main
1356 (if (match-beginning 7)
1357 (substring entry-main
1361 (summary (icalendar--convert-string-for-export
1362 (substring entry-main
(match-beginning 8)
1364 (icalendar--dmsg "weekly %s" entry-main
)
1366 (when starttimestring
1367 (unless endtimestring
1369 (icalendar--rris "^T0?" ""
1371 (setq endtimestring
(format "T%06d"
1373 (list (concat "\nDTSTART;"
1377 ;; Find the first requested weekday of the
1379 (funcall 'format
"%04d%02d%02d"
1380 icalendar-recurring-start-year
1
1381 (icalendar-first-weekday-of-year
1382 day icalendar-recurring-start-year
))
1383 (or starttimestring
"")
1388 (funcall 'format
"%04d%02d%02d"
1389 ;; end is non-inclusive!
1390 icalendar-recurring-start-year
1
1391 (+ (icalendar-first-weekday-of-year
1392 day icalendar-recurring-start-year
)
1393 (if endtimestring
0 1)))
1394 (or endtimestring
"")
1395 "\nRRULE:FREQ=WEEKLY;INTERVAL=1;BYDAY="
1401 (defun icalendar--convert-yearly-to-ical (nonmarker entry-main
)
1402 "Convert yearly diary entry to iCalendar format.
1403 NONMARKER is a regular expression matching the start of non-marking
1404 entries. ENTRY-MAIN is the first line of the diary entry."
1405 (if (string-match (concat nonmarker
1406 (if (eq (icalendar--date-style) 'european
)
1407 "\\([0-9]+[0-9]?\\)\\s-+\\([a-z]+\\)\\s-+"
1408 "\\([a-z]+\\)\\s-+\\([0-9]+[0-9]?\\)\\s-+")
1410 "\\(\\([0-9][0-9]?:[0-9][0-9]\\)\\([ap]m\\)?"
1412 "-\\([0-9][0-9]?:[0-9][0-9]\\)\\([ap]m\\)?\\)?"
1414 "\\s-*\\([^0-9]+.*?\\) ?$" ; must not match years
1417 (let* ((daypos (if (eq (icalendar--date-style) 'european
) 1 2))
1418 (monpos (if (eq (icalendar--date-style) 'european
) 2 1))
1419 (day (read (substring entry-main
1420 (match-beginning daypos
)
1421 (match-end daypos
))))
1422 (month (icalendar--get-month-number
1423 (substring entry-main
1424 (match-beginning monpos
)
1425 (match-end monpos
))))
1426 (starttimestring (icalendar--diarytime-to-isotime
1427 (if (match-beginning 4)
1428 (substring entry-main
1432 (if (match-beginning 5)
1433 (substring entry-main
1437 (endtimestring (icalendar--diarytime-to-isotime
1438 (if (match-beginning 7)
1439 (substring entry-main
1443 (if (match-beginning 8)
1444 (substring entry-main
1448 (summary (icalendar--convert-string-for-export
1449 (substring entry-main
(match-beginning 9)
1451 (icalendar--dmsg "yearly %s" entry-main
)
1453 (when starttimestring
1454 (unless endtimestring
1456 (icalendar--rris "^T0?" ""
1458 (setq endtimestring
(format "T%06d"
1460 (list (concat "\nDTSTART;"
1461 (if starttimestring
"VALUE=DATE-TIME:"
1463 (format "1900%02d%02d" month day
)
1464 (or starttimestring
"")
1466 (if endtimestring
"VALUE=DATE-TIME:"
1468 ;; end is not included! shift by one day
1469 (icalendar--date-to-isodate
1470 (list month day
1900)
1471 (if endtimestring
0 1))
1472 (or endtimestring
"")
1473 "\nRRULE:FREQ=YEARLY;INTERVAL=1;BYMONTH="
1481 (defun icalendar--convert-sexp-to-ical (nonmarker entry-main
)
1482 "Convert complex sexp diary entry to iCalendar format -- unsupported!
1486 NONMARKER is a regular expression matching the start of non-marking
1487 entries. ENTRY-MAIN is the first line of the diary entry."
1488 (cond ((string-match (concat nonmarker
1489 "%%(and \\(([^)]+)\\))\\(\\s-*.*?\\) ?$")
1491 ;; simple sexp entry as generated by icalendar.el: strip off the
1492 ;; unnecessary (and)
1493 (icalendar--dmsg "diary-sexp from icalendar.el %s" entry-main
)
1494 (icalendar--convert-to-ical
1497 (substring entry-main
(match-beginning 1) (match-end 1))
1498 (substring entry-main
(match-beginning 2) (match-end 2)))))
1499 ((string-match (concat nonmarker
1502 (icalendar--dmsg "diary-sexp %s" entry-main
)
1503 (error "Sexp-entries are not supported yet"))
1508 (defun icalendar--convert-block-to-ical (nonmarker entry-main
)
1509 "Convert block diary entry to iCalendar format.
1510 NONMARKER is a regular expression matching the start of non-marking
1511 entries. ENTRY-MAIN is the first line of the diary entry."
1512 (if (string-match (concat nonmarker
1513 "%%(diary-block \\([^ /]+[ /]+[^ /]+[ /]+[^ ]+\\)"
1514 " +\\([^ /]+[ /]+[^ /]+[ /]+[^ ]+\\))\\s-*"
1515 "\\(\\([0-9][0-9]?:[0-9][0-9]\\)\\([ap]m\\)?"
1517 "-\\([0-9][0-9]?:[0-9][0-9]\\)\\([ap]m\\)?\\)?"
1519 "\\s-*\\(.*?\\) ?$")
1521 (let* ((startstring (substring entry-main
1524 (endstring (substring entry-main
1527 (startisostring (icalendar--datestring-to-isodate
1529 (endisostring (icalendar--datestring-to-isodate
1531 (endisostring+1 (icalendar--datestring-to-isodate
1533 (starttimestring (icalendar--diarytime-to-isotime
1534 (if (match-beginning 4)
1535 (substring entry-main
1539 (if (match-beginning 5)
1540 (substring entry-main
1544 (endtimestring (icalendar--diarytime-to-isotime
1545 (if (match-beginning 7)
1546 (substring entry-main
1550 (if (match-beginning 8)
1551 (substring entry-main
1555 (summary (icalendar--convert-string-for-export
1556 (substring entry-main
(match-beginning 9)
1558 (icalendar--dmsg "diary-block %s" entry-main
)
1559 (when starttimestring
1560 (unless endtimestring
1562 (read (icalendar--rris "^T0?" ""
1564 (setq endtimestring
(format "T%06d"
1567 ;; with time -> write rrule
1568 (list (concat "\nDTSTART;VALUE=DATE-TIME:"
1571 "\nDTEND;VALUE=DATE-TIME:"
1574 "\nRRULE:FREQ=DAILY;INTERVAL=1;UNTIL="
1577 ;; no time -> write long event
1578 (list (concat "\nDTSTART;VALUE=DATE:" startisostring
1579 "\nDTEND;VALUE=DATE:" endisostring
+1)
1584 (defun icalendar--convert-float-to-ical (nonmarker entry-main
)
1585 "Convert float diary entry to iCalendar format -- partially unsupported!
1587 FIXME! DAY from diary-float yet unimplemented.
1589 NONMARKER is a regular expression matching the start of non-marking
1590 entries. ENTRY-MAIN is the first line of the diary entry."
1591 (if (string-match (concat nonmarker
"%%\\((diary-float .+\\) ?$") entry-main
)
1593 (insert (match-string 1 entry-main
))
1594 (goto-char (point-min))
1595 (let* ((sexp (read (current-buffer))) ;using `read' here
1597 ;matching, esp. with
1600 (month (nth 1 sexp
))
1601 (dayname (nth 2 sexp
))
1605 (replace-regexp-in-string
1606 "\\(^\s+\\|\s+$\\)" ""
1607 (buffer-substring (point) (point-max)))))
1611 (icalendar--dmsg "diary-float %s" entry-main
)
1612 (error "Don't know if or how to implement day in `diary-float'")))
1615 ;;Start today (yes this is an arbitrary choice):
1616 "\nDTSTART;VALUE=DATE:"
1617 (format-time-string "%Y%m%d" (current-time))
1618 ;;BUT remove today if `diary-float'
1619 ;;expression does not hold true for today:
1621 (null (let ((date (calendar-current-date))
1623 (diary-float month dayname n
)))
1625 "\nEXDATE;VALUE=DATE:"
1626 (format-time-string "%Y%m%d" (current-time))))
1628 (if (or (numberp month
) (listp month
))
1629 "FREQ=YEARLY;BYMONTH="
1635 (number-to-string m
))
1639 (number-to-string month
))
1641 (number-to-string n
)
1642 (aref icalendar--weekday-array dayname
))
1647 (defun icalendar--convert-date-to-ical (nonmarker entry-main
)
1648 "Convert `diary-date' diary entry to iCalendar format -- unsupported!
1652 NONMARKER is a regular expression matching the start of non-marking
1653 entries. ENTRY-MAIN is the first line of the diary entry."
1654 (if (string-match (concat nonmarker
1655 "%%(diary-date \\([^)]+\\))\\s-*\\(.*?\\) ?$")
1658 (icalendar--dmsg "diary-date %s" entry-main
)
1659 (error "`diary-date' is not supported yet"))
1663 (defun icalendar--convert-cyclic-to-ical (nonmarker entry-main
)
1664 "Convert `diary-cyclic' diary entry to iCalendar format.
1665 NONMARKER is a regular expression matching the start of non-marking
1666 entries. ENTRY-MAIN is the first line of the diary entry."
1667 (if (string-match (concat nonmarker
1668 "%%(diary-cyclic \\([^ ]+\\) +"
1669 "\\([^ /]+[ /]+[^ /]+[ /]+[^ ]+\\))\\s-*"
1670 "\\(\\([0-9][0-9]?:[0-9][0-9]\\)\\([ap]m\\)?"
1672 "-\\([0-9][0-9]?:[0-9][0-9]\\)\\([ap]m\\)?\\)?"
1674 "\\s-*\\(.*?\\) ?$")
1676 (let* ((frequency (substring entry-main
(match-beginning 1)
1678 (datetime (substring entry-main
(match-beginning 2)
1680 (startisostring (icalendar--datestring-to-isodate
1682 (endisostring (icalendar--datestring-to-isodate
1684 (endisostring+1 (icalendar--datestring-to-isodate
1686 (starttimestring (icalendar--diarytime-to-isotime
1687 (if (match-beginning 4)
1688 (substring entry-main
1692 (if (match-beginning 5)
1693 (substring entry-main
1697 (endtimestring (icalendar--diarytime-to-isotime
1698 (if (match-beginning 7)
1699 (substring entry-main
1703 (if (match-beginning 8)
1704 (substring entry-main
1708 (summary (icalendar--convert-string-for-export
1709 (substring entry-main
(match-beginning 9)
1711 (icalendar--dmsg "diary-cyclic %s" entry-main
)
1712 (when starttimestring
1713 (unless endtimestring
1715 (read (icalendar--rris "^T0?" ""
1717 (setq endtimestring
(format "T%06d"
1719 (list (concat "\nDTSTART;"
1720 (if starttimestring
"VALUE=DATE-TIME:"
1723 (or starttimestring
"")
1725 (if endtimestring
"VALUE=DATE-TIME:"
1727 (if endtimestring endisostring endisostring
+1)
1728 (or endtimestring
"")
1729 "\nRRULE:FREQ=DAILY;INTERVAL=" frequency
1730 ;; strange: korganizer does not expect
1731 ;; BYSOMETHING here...
1737 (defun icalendar--convert-anniversary-to-ical (nonmarker entry-main
)
1738 "Convert `diary-anniversary' diary entry to iCalendar format.
1739 NONMARKER is a regular expression matching the start of non-marking
1740 entries. ENTRY-MAIN is the first line of the diary entry."
1741 (if (string-match (concat nonmarker
1742 "%%(diary-anniversary \\([^)]+\\))\\s-*"
1743 "\\(\\([0-9][0-9]?:[0-9][0-9]\\)\\([ap]m\\)?"
1745 "-\\([0-9][0-9]?:[0-9][0-9]\\)\\([ap]m\\)?\\)?"
1747 "\\s-*\\(.*?\\) ?$")
1749 (let* ((datetime (substring entry-main
(match-beginning 1)
1751 (startisostring (icalendar--datestring-to-isodate
1753 (endisostring (icalendar--datestring-to-isodate
1755 (starttimestring (icalendar--diarytime-to-isotime
1756 (if (match-beginning 3)
1757 (substring entry-main
1761 (if (match-beginning 4)
1762 (substring entry-main
1766 (endtimestring (icalendar--diarytime-to-isotime
1767 (if (match-beginning 6)
1768 (substring entry-main
1772 (if (match-beginning 7)
1773 (substring entry-main
1777 (summary (icalendar--convert-string-for-export
1778 (substring entry-main
(match-beginning 8)
1780 (icalendar--dmsg "diary-anniversary %s" entry-main
)
1781 (when starttimestring
1782 (unless endtimestring
1784 (read (icalendar--rris "^T0?" ""
1786 (setq endtimestring
(format "T%06d"
1788 (list (concat "\nDTSTART;"
1789 (if starttimestring
"VALUE=DATE-TIME:"
1792 (or starttimestring
"")
1794 (if endtimestring
"VALUE=DATE-TIME:"
1797 (or endtimestring
"")
1798 "\nRRULE:FREQ=YEARLY;INTERVAL=1"
1799 ;; the following is redundant,
1800 ;; but korganizer seems to expect this... ;(
1801 ;; and evolution doesn't understand it... :(
1802 ;; so... who is wrong?!
1804 (substring startisostring
4 6)
1806 (substring startisostring
6 8))
1811 ;; ======================================================================
1812 ;; Import -- convert iCalendar to emacs-diary
1813 ;; ======================================================================
1816 (defun icalendar-import-file (ical-filename diary-filename
1817 &optional non-marking
)
1818 "Import an iCalendar file and append to a diary file.
1819 Argument ICAL-FILENAME output iCalendar file.
1820 Argument DIARY-FILENAME input `diary-file'.
1821 Optional argument NON-MARKING determines whether events are created as
1822 non-marking or not."
1823 (interactive "fImport iCalendar data from file: \n\
1826 ;; clean up the diary file
1827 (save-current-buffer
1828 ;; now load and convert from the ical file
1829 (set-buffer (find-file ical-filename
))
1830 (icalendar-import-buffer diary-filename t non-marking
)))
1833 (defun icalendar-import-buffer (&optional diary-file do-not-ask
1835 "Extract iCalendar events from current buffer.
1837 This function searches the current buffer for the first iCalendar
1838 object, reads it and adds all VEVENT elements to the diary
1841 It will ask for each appointment whether to add it to the diary
1842 unless DO-NOT-ASK is non-nil. When called interactively,
1843 DO-NOT-ASK is nil, so that you are asked for each event.
1845 NON-MARKING determines whether diary events are created as
1848 Return code t means that importing worked well, return code nil
1849 means that an error has occurred. Error messages will be in the
1850 buffer `*icalendar-errors*'."
1852 (save-current-buffer
1854 (message "Preparing iCalendar...")
1855 (set-buffer (icalendar--get-unfolded-buffer (current-buffer)))
1856 (goto-char (point-min))
1857 (message "Preparing iCalendar...done")
1858 (if (re-search-forward "^BEGIN:VCALENDAR\\s-*$" nil t
)
1859 (let (ical-contents ical-errors
)
1861 (message "Reading iCalendar...")
1863 (setq ical-contents
(icalendar--read-element nil nil
))
1864 (message "Reading iCalendar...done")
1866 (message "Converting iCalendar...")
1867 (setq ical-errors
(icalendar--convert-ical-to-diary
1869 diary-file do-not-ask non-marking
))
1871 ;; save the diary file if it is visited already
1872 (let ((b (find-buffer-visiting diary-file
)))
1874 (save-current-buffer
1877 (message "Converting iCalendar...done")
1878 ;; return t if no error occurred
1881 "Current buffer does not contain iCalendar contents!")
1882 ;; return nil, i.e. import did not work
1885 (define-obsolete-function-alias 'icalendar-extract-ical-from-buffer
1886 'icalendar-import-buffer
"22.1")
1888 (defun icalendar--format-ical-event (event)
1889 "Create a string representation of an iCalendar EVENT."
1890 (if (functionp icalendar-import-format
)
1891 (funcall icalendar-import-format event
)
1892 (let ((string icalendar-import-format
)
1893 (case-fold-search nil
)
1895 '(("%c" CLASS icalendar-import-format-class
)
1896 ("%d" DESCRIPTION icalendar-import-format-description
)
1897 ("%l" LOCATION icalendar-import-format-location
)
1898 ("%o" ORGANIZER icalendar-import-format-organizer
)
1899 ("%s" SUMMARY icalendar-import-format-summary
)
1900 ("%t" STATUS icalendar-import-format-status
)
1901 ("%u" URL icalendar-import-format-url
)
1902 ("%U" UID icalendar-import-format-uid
))))
1903 ;; convert the specifiers in the format string
1905 (let* ((spec (car i
))
1907 (format (car (cddr i
)))
1908 (contents (icalendar--get-event-property event prop
))
1909 (formatted-contents ""))
1910 (when (and contents
(> (length contents
) 0))
1911 (setq formatted-contents
1912 (icalendar--rris "%s"
1913 (icalendar--convert-string-for-import
1915 (symbol-value format
)
1917 (setq string
(icalendar--rris spec
1924 (defun icalendar--convert-ical-to-diary (ical-list diary-file
1925 &optional do-not-ask
1927 "Convert iCalendar data to an Emacs diary file.
1928 Import VEVENTS from the iCalendar object ICAL-LIST and saves them to a
1929 DIARY-FILE. If DO-NOT-ASK is nil the user is asked for each event
1930 whether to actually import it. NON-MARKING determines whether diary
1931 events are created as non-marking.
1932 This function attempts to return t if something goes wrong. In this
1933 case an error string which describes all the errors and problems is
1934 written into the buffer `*icalendar-errors*'."
1935 (let* ((ev (icalendar--all-events ical-list
))
1939 (zone-map (icalendar--convert-all-timezones ical-list
))
1941 ;; step through all events/appointments
1946 (condition-case error-val
1947 (let* ((dtstart (icalendar--get-event-property e
'DTSTART
))
1948 (dtstart-zone (icalendar--find-time-zone
1949 (icalendar--get-event-property-attributes
1952 (dtstart-dec (icalendar--decode-isodatetime dtstart nil
1954 (start-d (icalendar--datetime-to-diary-date
1956 (start-t (icalendar--datetime-to-colontime dtstart-dec
))
1957 (dtend (icalendar--get-event-property e
'DTEND
))
1958 (dtend-zone (icalendar--find-time-zone
1959 (icalendar--get-event-property-attributes
1962 (dtend-dec (icalendar--decode-isodatetime dtend
1964 (dtend-1-dec (icalendar--decode-isodatetime dtend -
1
1969 (summary (icalendar--convert-string-for-import
1970 (or (icalendar--get-event-property e
'SUMMARY
)
1972 (rrule (icalendar--get-event-property e
'RRULE
))
1973 (rdate (icalendar--get-event-property e
'RDATE
))
1974 (duration (icalendar--get-event-property e
'DURATION
)))
1975 (icalendar--dmsg "%s: `%s'" start-d summary
)
1976 ;; check whether start-time is missing
1979 (cadr (icalendar--get-event-property-attributes
1984 (let ((dtend-dec-d (icalendar--add-decoded-times
1986 (icalendar--decode-isoduration duration
)))
1987 (dtend-1-dec-d (icalendar--add-decoded-times
1989 (icalendar--decode-isoduration duration
1991 (if (and dtend-dec
(not (eq dtend-dec dtend-dec-d
)))
1992 (message "Inconsistent endtime and duration for %s"
1994 (setq dtend-dec dtend-dec-d
)
1995 (setq dtend-1-dec dtend-1-dec-d
)))
1996 (setq end-d
(if dtend-dec
1997 (icalendar--datetime-to-diary-date dtend-dec
)
1999 (setq end-1-d
(if dtend-1-dec
2000 (icalendar--datetime-to-diary-date dtend-1-dec
)
2002 (setq end-t
(if (and
2006 (icalendar--get-event-property-attributes
2009 (icalendar--datetime-to-colontime dtend-dec
)
2011 (icalendar--dmsg "start-d: %s, end-d: %s" start-d end-d
)
2016 (icalendar--convert-recurring-to-diary e dtstart-dec start-t
2020 (icalendar--dmsg "rdate event")
2021 (setq diary-string
"")
2022 (mapc (lambda (datestring)
2024 (concat diary-string
2025 (format "......"))))
2026 (icalendar--split-value rdate
)))
2027 ;; non-recurring event
2029 ((not (string= start-d end-d
))
2031 (icalendar--convert-non-recurring-all-day-to-diary
2035 ((and start-t
(or (not end-t
)
2036 (not (string= start-t end-t
))))
2038 (icalendar--convert-non-recurring-not-all-day-to-diary
2039 e dtstart-dec dtend-dec start-t end-t
))
2043 (icalendar--dmsg "all day event")
2044 (setq diary-string
(icalendar--datetime-to-diary-date
2047 ;; add all other elements unless the user doesn't want to have
2052 (concat diary-string
" "
2053 (icalendar--format-ical-event e
)))
2054 (if do-not-ask
(setq summary nil
))
2055 ;; add entry to diary and store actual name of diary
2056 ;; file (in case it was nil)
2058 (icalendar--add-diary-entry diary-string diary-file
2059 non-marking summary
)))
2061 (setq found-error t
)
2063 (format "%s\nCannot handle this event:%s"
2065 ;; FIXME: inform user about ignored event properties
2068 (message "Ignoring event \"%s\"" e
)
2069 (setq found-error t
)
2070 (setq error-string
(format "%s\n%s\nCannot handle this event: %s"
2071 error-val error-string e
))
2072 (message "%s" error-string
))))
2074 ;; insert final newline
2076 (let ((b (find-buffer-visiting diary-file
)))
2078 (save-current-buffer
2080 (goto-char (point-max))
2083 (save-current-buffer
2084 (set-buffer (get-buffer-create "*icalendar-errors*"))
2086 (insert error-string
)))
2087 (message "Converting iCalendar...done")
2090 ;; subroutines for importing
2091 (defun icalendar--convert-recurring-to-diary (e dtstart-dec start-t end-t
)
2092 "Convert recurring iCalendar event E to diary format.
2094 DTSTART-DEC is the DTSTART property of E.
2095 START-T is the event's start time in diary format.
2096 END-T is the event's end time in diary format."
2097 (icalendar--dmsg "recurring event")
2098 (let* ((rrule (icalendar--get-event-property e
'RRULE
))
2099 (rrule-props (icalendar--split-value rrule
))
2100 (frequency (cadr (assoc 'FREQ rrule-props
)))
2101 (until (cadr (assoc 'UNTIL rrule-props
)))
2102 (count (cadr (assoc 'COUNT rrule-props
)))
2103 (interval (read (or (cadr (assoc 'INTERVAL rrule-props
)) "1")))
2104 (dtstart-conv (icalendar--datetime-to-diary-date dtstart-dec
))
2105 (until-conv (icalendar--datetime-to-diary-date
2106 (icalendar--decode-isodatetime until
)))
2107 (until-1-conv (icalendar--datetime-to-diary-date
2108 (icalendar--decode-isodatetime until -
1)))
2111 ;; FIXME FIXME interval!!!!!!!!!!!!!
2115 (message "Must not have UNTIL and COUNT -- ignoring COUNT element!")
2117 (cond ((string-equal frequency
"DAILY")
2118 (setq until
(icalendar--add-decoded-times
2120 (list 0 0 0 (* (read count
) interval
) 0 0)))
2121 (setq until-1
(icalendar--add-decoded-times
2123 (list 0 0 0 (* (- (read count
) 1) interval
)
2126 ((string-equal frequency
"WEEKLY")
2127 (setq until
(icalendar--add-decoded-times
2129 (list 0 0 0 (* (read count
) 7 interval
) 0 0)))
2130 (setq until-1
(icalendar--add-decoded-times
2132 (list 0 0 0 (* (- (read count
) 1) 7
2135 ((string-equal frequency
"MONTHLY")
2136 (setq until
(icalendar--add-decoded-times
2137 dtstart-dec
(list 0 0 0 0 (* (- (read count
) 1)
2139 (setq until-1
(icalendar--add-decoded-times
2140 dtstart-dec
(list 0 0 0 0 (* (- (read count
) 1)
2143 ((string-equal frequency
"YEARLY")
2144 (setq until
(icalendar--add-decoded-times
2145 dtstart-dec
(list 0 0 0 0 0 (* (- (read count
) 1)
2147 (setq until-1
(icalendar--add-decoded-times
2149 (list 0 0 0 0 0 (* (- (read count
) 1)
2153 (message "Cannot handle COUNT attribute for `%s' events."
2155 (setq until-conv
(icalendar--datetime-to-diary-date until
))
2156 (setq until-1-conv
(icalendar--datetime-to-diary-date until-1
))
2159 (cond ((string-equal frequency
"WEEKLY")
2160 (let* ((byday (cadr (assoc 'BYDAY rrule-props
)))
2162 (icalendar--get-weekday-numbers byday
))
2164 (when (> (length weekdays
) 1)
2165 (format "(memq (calendar-day-of-week date) '%s) "
2169 ;; weekly and all-day
2170 (icalendar--dmsg "weekly all-day")
2176 "(diary-block %s %s))")
2178 (format "(diary-cyclic %d %s) "
2182 (if count until-1-conv until-conv
)
2185 (format "%%%%(and %s(diary-cyclic %d %s))"
2186 (or weekday-clause
"")
2187 (if weekday-clause
1 (* interval
7))
2189 ;; weekly and not all-day
2190 (icalendar--dmsg "weekly not-all-day")
2196 "(diary-block %s %s)) "
2199 (format "(diary-cyclic %d %s) "
2205 (if end-t
"-" "") (or end-t
"")))
2208 ;; DTSTART;VALUE=DATE-TIME:20030919T090000
2209 ;; DTEND;VALUE=DATE-TIME:20030919T113000
2212 "%%%%(and %s(diary-cyclic %d %s)) %s%s%s"
2213 (or weekday-clause
"")
2214 (if weekday-clause
1 (* interval
7))
2217 (if end-t
"-" "") (or end-t
"")))))))
2219 ((string-equal frequency
"YEARLY")
2220 (icalendar--dmsg "yearly")
2222 (let ((day (nth 3 dtstart-dec
))
2223 (month (nth 4 dtstart-dec
)))
2224 (setq result
(concat "%%(and (diary-date "
2225 (cond ((eq (icalendar--date-style) 'iso
)
2226 (format "t %d %d" month day
))
2227 ((eq (icalendar--date-style) 'european
)
2228 (format "%d %d t" day month
))
2229 ((eq (icalendar--date-style) 'american
)
2230 (format "%d %d t" month day
)))
2239 (setq result
(format
2240 "%%%%(and (diary-anniversary %s)) %s%s%s"
2243 (if end-t
"-" "") (or end-t
"")))))
2245 ((string-equal frequency
"MONTHLY")
2246 (icalendar--dmsg "monthly")
2249 "%%%%(and (diary-date %s) (diary-block %s %s)) %s%s%s"
2250 (let ((day (nth 3 dtstart-dec
)))
2251 (cond ((eq (icalendar--date-style) 'iso
)
2252 (format "t t %d" day
))
2253 ((eq (icalendar--date-style) 'european
)
2254 (format "%d t t" day
))
2255 ((eq (icalendar--date-style) 'american
)
2256 (format "t %d t" day
))))
2260 (if (eq (icalendar--date-style) 'iso
) "9999 1 1" "1 1 9999")) ;; FIXME: should be unlimited
2262 (if end-t
"-" "") (or end-t
""))))
2264 ((and (string-equal frequency
"DAILY"))
2268 (concat "%%%%(and (diary-cyclic %s %s) "
2269 "(diary-block %s %s)) %s%s%s")
2270 interval dtstart-conv dtstart-conv
2271 (if count until-1-conv until-conv
)
2273 (if end-t
"-" "") (or end-t
"")))
2276 "%%%%(and (diary-cyclic %s %s)) %s%s%s"
2280 (if end-t
"-" "") (or end-t
""))))))
2281 ;; Handle exceptions from recurrence rules
2282 (let ((ex-dates (icalendar--get-event-properties e
'EXDATE
)))
2284 (let* ((ex-start (icalendar--decode-isodatetime
2286 (ex-d (icalendar--datetime-to-diary-date
2289 (icalendar--rris "^%%(\\(and \\)?"
2291 "%%%%(and (not (diary-date %s)) "
2294 (setq ex-dates
(cdr ex-dates
))))
2295 ;; FIXME: exception rules are not recognized
2296 (if (icalendar--get-event-property e
'EXRULE
)
2299 "\n Exception rules: "
2300 (icalendar--get-event-properties
2304 (defun icalendar--convert-non-recurring-all-day-to-diary (event start-d end-d
)
2305 "Convert non-recurring iCalendar EVENT to diary format.
2307 DTSTART is the decoded DTSTART property of E.
2308 Argument START-D gives the first day.
2309 Argument END-D gives the last day."
2310 (icalendar--dmsg "non-recurring all-day event")
2311 (format "%%%%(and (diary-block %s %s))" start-d end-d
))
2313 (defun icalendar--convert-non-recurring-not-all-day-to-diary (event dtstart-dec
2317 "Convert recurring icalendar EVENT to diary format.
2319 DTSTART-DEC is the decoded DTSTART property of E.
2320 DTEND-DEC is the decoded DTEND property of E.
2321 START-T is the event's start time in diary format.
2322 END-T is the event's end time in diary format."
2323 (icalendar--dmsg "not all day event")
2326 (icalendar--datetime-to-diary-date
2331 (icalendar--datetime-to-diary-date
2335 (defun icalendar--add-diary-entry (string diary-file non-marking
2337 "Add STRING to the diary file DIARY-FILE.
2338 STRING must be a properly formatted valid diary entry. NON-MARKING
2339 determines whether diary events are created as non-marking. If
2340 SUMMARY is not nil it must be a string that gives the summary of the
2341 entry. In this case the user will be asked whether he wants to insert
2343 (when (or (not summary
)
2344 (y-or-n-p (format "Add appointment for `%s' to diary? "
2348 (y-or-n-p (format "Make appointment non-marking? "))))
2349 (save-window-excursion
2352 (read-file-name "Add appointment to this diary file: ")))
2353 ;; Note: diary-make-entry will add a trailing blank char.... :(
2354 (funcall (if (fboundp 'diary-make-entry
)
2357 string non-marking diary-file
)))
2358 ;; Würgaround to remove the trailing blank char
2359 (with-current-buffer (find-file diary-file
)
2360 (goto-char (point-max))
2361 (if (= (char-before) ?
)
2363 ;; return diary-file in case it has been changed interactively
2366 ;; ======================================================================
2368 ;; ======================================================================
2369 (defun icalendar-import-format-sample (event)
2370 "Example function for formatting an iCalendar EVENT."
2371 (format (concat "SUMMARY=`%s' DESCRIPTION=`%s' LOCATION=`%s' ORGANIZER=`%s' "
2372 "STATUS=`%s' URL=`%s' CLASS=`%s'")
2373 (or (icalendar--get-event-property event
'SUMMARY
) "")
2374 (or (icalendar--get-event-property event
'DESCRIPTION
) "")
2375 (or (icalendar--get-event-property event
'LOCATION
) "")
2376 (or (icalendar--get-event-property event
'ORGANIZER
) "")
2377 (or (icalendar--get-event-property event
'STATUS
) "")
2378 (or (icalendar--get-event-property event
'URL
) "")
2379 (or (icalendar--get-event-property event
'CLASS
) "")))
2381 (provide 'icalendar
)
2383 ;;; icalendar.el ends here