1 ;;; ox-icalendar.el --- iCalendar Back-End for Org Export Engine
3 ;; Copyright (C) 2004-2014 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Nicolas Goaziou <n dot goaziou at gmail dot com>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; This library implements an iCalendar back-end for Org generic
28 ;; exporter. See Org manual for more information.
30 ;; It is expected to conform to RFC 5545.
34 (eval-when-compile (require 'cl
))
36 (declare-function org-bbdb-anniv-export-ical
"org-bbdb" nil
)
40 ;;; User-Configurable Variables
42 (defgroup org-export-icalendar nil
43 "Options specific for iCalendar export back-end."
44 :tag
"Org Export iCalendar"
47 (defcustom org-icalendar-combined-agenda-file
"~/org.ics"
48 "The file name for the iCalendar file covering all agenda files.
49 This file is created with the command \\[org-icalendar-combine-agenda-files].
50 The file name should be absolute. It will be overwritten without warning."
51 :group
'org-export-icalendar
54 (defcustom org-icalendar-alarm-time
0
55 "Number of minutes for triggering an alarm for exported timed events.
57 A zero value (the default) turns off the definition of an alarm trigger
58 for timed events. If non-zero, alarms are created.
60 - a single alarm per entry is defined
61 - The alarm will go off N minutes before the event
62 - only a DISPLAY action is defined."
63 :group
'org-export-icalendar
67 (defcustom org-icalendar-combined-name
"OrgMode"
68 "Calendar name for the combined iCalendar representing all agenda files."
69 :group
'org-export-icalendar
72 (defcustom org-icalendar-combined-description
""
73 "Calendar description for the combined iCalendar (all agenda files)."
74 :group
'org-export-icalendar
77 (defcustom org-icalendar-exclude-tags nil
78 "Tags that exclude a tree from export.
79 This variable allows to specify different exclude tags from other
80 back-ends. It can also be set with the ICAL_EXCLUDE_TAGS
82 :group
'org-export-icalendar
83 :type
'(repeat (string :tag
"Tag")))
85 (defcustom org-icalendar-use-deadline
'(event-if-not-todo todo-due
)
86 "Contexts where iCalendar export should use a deadline time stamp.
88 This is a list with possibly several symbols in it. Valid symbols are:
90 `event-if-todo' Deadlines in TODO entries become calendar events.
91 `event-if-not-todo' Deadlines in non-TODO entries become calendar events.
92 `todo-due' Use deadlines in TODO entries as due-dates."
93 :group
'org-export-icalendar
95 (const :tag
"Deadlines in non-TODO entries become events"
97 (const :tag
"Deadline in TODO entries become events"
99 (const :tag
"Deadlines in TODO entries become due-dates"
102 (defcustom org-icalendar-use-scheduled
'(todo-start)
103 "Contexts where iCalendar export should use a scheduling time stamp.
105 This is a list with possibly several symbols in it. Valid symbols are:
107 `event-if-todo' Scheduling time stamps in TODO entries become an event.
108 `event-if-not-todo' Scheduling time stamps in non-TODO entries become an event.
109 `todo-start' Scheduling time stamps in TODO entries become start date.
110 Some calendar applications show TODO entries only after
112 :group
'org-export-icalendar
113 :type
'(set :greedy t
115 "SCHEDULED timestamps in non-TODO entries become events"
117 (const :tag
"SCHEDULED timestamps in TODO entries become events"
119 (const :tag
"SCHEDULED in TODO entries become start date"
122 (defcustom org-icalendar-categories
'(local-tags category
)
123 "Items that should be entered into the \"categories\" field.
125 This is a list of symbols, the following are valid:
126 `category' The Org mode category of the current file or tree
127 `todo-state' The todo state, if any
128 `local-tags' The tags, defined in the current line
129 `all-tags' All tags, including inherited ones."
130 :group
'org-export-icalendar
133 (const :tag
"The file or tree category" category
)
134 (const :tag
"The TODO state" todo-state
)
135 (const :tag
"Tags defined in current line" local-tags
)
136 (const :tag
"All tags, including inherited ones" all-tags
))))
138 (defcustom org-icalendar-with-timestamps
'active
139 "Non-nil means make an event from plain time stamps.
141 It can be set to `active', `inactive', t or nil, in order to make
142 an event from, respectively, only active timestamps, only
143 inactive ones, all of them or none.
145 This variable has precedence over `org-export-with-timestamps'.
146 It can also be set with the #+OPTIONS line, e.g. \"<:t\"."
147 :group
'org-export-icalendar
149 (const :tag
"All timestamps" t
)
150 (const :tag
"Only active timestamps" active
)
151 (const :tag
"Only inactive timestamps" inactive
)
152 (const :tag
"No timestamp" nil
)))
154 (defcustom org-icalendar-include-todo nil
155 "Non-nil means create VTODO components from TODO items.
158 nil don't include any task.
159 t include tasks that are not in DONE state.
160 `unblocked' include all TODO items that are not blocked.
161 `all' include both done and not done items."
162 :group
'org-export-icalendar
164 (const :tag
"None" nil
)
165 (const :tag
"Unfinished" t
)
166 (const :tag
"Unblocked" unblocked
)
167 (const :tag
"All" all
)
168 (repeat :tag
"Specific TODO keywords"
169 (string :tag
"Keyword"))))
171 (defcustom org-icalendar-include-bbdb-anniversaries nil
172 "Non-nil means a combined iCalendar file should include anniversaries.
173 The anniversaries are defined in the BBDB database."
174 :group
'org-export-icalendar
177 (defcustom org-icalendar-include-sexps t
178 "Non-nil means export to iCalendar files should also cover sexp entries.
179 These are entries like in the diary, but directly in an Org file."
180 :group
'org-export-icalendar
183 (defcustom org-icalendar-include-body t
184 "Amount of text below headline to be included in iCalendar export.
185 This is a number of characters that should maximally be included.
186 Properties, scheduling and clocking lines will always be removed.
187 The text will be inserted into the DESCRIPTION field."
188 :group
'org-export-icalendar
190 (const :tag
"Nothing" nil
)
191 (const :tag
"Everything" t
)
192 (integer :tag
"Max characters")))
194 (defcustom org-icalendar-store-UID nil
195 "Non-nil means store any created UIDs in properties.
197 The iCalendar standard requires that all entries have a unique identifier.
198 Org will create these identifiers as needed. When this variable is non-nil,
199 the created UIDs will be stored in the ID property of the entry. Then the
200 next time this entry is exported, it will be exported with the same UID,
201 superseding the previous form of it. This is essential for
202 synchronization services.
204 This variable is not turned on by default because we want to avoid creating
205 a property drawer in every entry if people are only playing with this feature,
206 or if they are only using it locally."
207 :group
'org-export-icalendar
210 (defcustom org-icalendar-timezone
(getenv "TZ")
211 "The time zone string for iCalendar export.
212 When nil or the empty string, use output
213 from (current-time-zone)."
214 :group
'org-export-icalendar
216 (const :tag
"Unspecified" nil
)
217 (string :tag
"Time zone")))
219 (defcustom org-icalendar-date-time-format
":%Y%m%dT%H%M%S"
220 "Format-string for exporting icalendar DATE-TIME.
222 See `format-time-string' for a full documentation. The only
223 difference is that `org-icalendar-timezone' is used for %Z.
225 Interesting value are:
226 - \":%Y%m%dT%H%M%S\" for local time
227 - \";TZID=%Z:%Y%m%dT%H%M%S\" for local time with explicit timezone
228 - \":%Y%m%dT%H%M%SZ\" for time expressed in Universal Time"
229 :group
'org-export-icalendar
232 (const :tag
"Local time" ":%Y%m%dT%H%M%S")
233 (const :tag
"Explicit local time" ";TZID=%Z:%Y%m%dT%H%M%S")
234 (const :tag
"Universal time" ":%Y%m%dT%H%M%SZ")
235 (string :tag
"Explicit format")))
237 (defvar org-icalendar-after-save-hook nil
238 "Hook run after an iCalendar file has been saved.
239 This hook is run with the name of the file as argument. A good
240 way to use this is to tell a desktop calendar application to
241 re-read the iCalendar file.")
247 (org-export-define-derived-backend 'icalendar
'ascii
248 :translate-alist
'((clock . ignore
)
249 (footnote-definition . ignore
)
250 (footnote-reference . ignore
)
251 (headline . org-icalendar-entry
)
252 (inlinetask . ignore
)
255 (inner-template .
(lambda (c i
) c
))
256 (template . org-icalendar-template
))
259 "ICALENDAR_EXCLUDE_TAGS" nil org-icalendar-exclude-tags split
)
260 (:with-timestamps nil
"<" org-icalendar-with-timestamps
)
262 (:icalendar-alarm-time nil nil org-icalendar-alarm-time
)
263 (:icalendar-categories nil nil org-icalendar-categories
)
264 (:icalendar-combined-agenda-file nil nil org-icalendar-combined-agenda-file
)
265 (:icalendar-combined-description nil nil org-icalendar-combined-description
)
266 (:icalendar-combined-name nil nil org-icalendar-combined-name
)
267 (:icalendar-date-time-format nil nil org-icalendar-date-time-format
)
268 (:icalendar-include-bbdb-anniversaries nil nil org-icalendar-include-bbdb-anniversaries
)
269 (:icalendar-include-body nil nil org-icalendar-include-body
)
270 (:icalendar-include-sexps nil nil org-icalendar-include-sexps
)
271 (:icalendar-include-todo nil nil org-icalendar-include-todo
)
272 (:icalendar-store-UID nil nil org-icalendar-store-UID
)
273 (:icalendar-timezone nil nil org-icalendar-timezone
)
274 (:icalendar-use-deadline nil nil org-icalendar-use-deadline
)
275 (:icalendar-use-scheduled nil nil org-icalendar-use-scheduled
)
276 ;; The following property will be non-nil when export has been
277 ;; started from org-agenda-mode. In this case, any entry without
278 ;; a non-nil "ICALENDAR_MARK" property will be ignored.
279 (:icalendar-agenda-view nil nil nil
))
281 '((:filter-headline . org-icalendar-clear-blank-lines
))
283 '(?c
"Export to iCalendar"
284 ((?f
"Current file" org-icalendar-export-to-ics
)
285 (?a
"All agenda files"
286 (lambda (a s v b
) (org-icalendar-export-agenda-files a
)))
287 (?c
"Combine all agenda files"
288 (lambda (a s v b
) (org-icalendar-combine-agenda-files a
))))))
292 ;;; Internal Functions
294 (defun org-icalendar-create-uid (file &optional bell h-markers
)
295 "Set ID property on headlines missing it in FILE.
296 When optional argument BELL is non-nil, inform the user with
297 a message if the file was modified. With optional argument
298 H-MARKERS non-nil, it is a list of markers for the headlines
299 which will be updated."
300 (let ((pt (if h-markers
(goto-char (car h-markers
)) (point-min)))
304 (let ((entry (org-element-at-point)))
305 (unless (or (< (point) pt
) (org-element-property :ID entry
))
307 (setq modified-flag t
)
309 (when h-markers
(setq org-map-continue-from
(pop h-markers
)))))
311 (when (and bell modified-flag
)
312 (message "ID properties created in file \"%s\"" file
)
315 (defun org-icalendar-blocked-headline-p (headline info
)
316 "Non-nil when HEADLINE is considered to be blocked.
318 INFO is a plist used as a communication channel.
320 A headline is blocked when either
322 - it has children which are not all in a completed state;
324 - it has a parent with the property :ORDERED:, and there are
325 siblings prior to it with incomplete status;
327 - its parent is blocked because it has siblings that should be
328 done first or is a child of a blocked grandparent entry."
330 ;; Check if any child is not done.
331 (org-element-map headline
'headline
332 (lambda (hl) (eq (org-element-property :todo-type hl
) 'todo
))
334 ;; Check :ORDERED: node property.
336 (let ((current headline
))
337 (mapc (lambda (parent)
339 ((not (org-element-property :todo-keyword parent
))
340 (throw 'blockedp nil
))
341 ((org-not-nil (org-element-property :ORDERED parent
))
342 (let ((sibling current
))
343 (while (setq sibling
(org-export-get-previous-element
345 (when (eq (org-element-property :todo-type sibling
) 'todo
)
346 (throw 'blockedp t
)))))
347 (t (setq current parent
))))
348 (org-export-get-genealogy headline
))
351 (defun org-icalendar-use-UTC-date-time-p ()
352 "Non-nil when `org-icalendar-date-time-format' requires UTC time."
353 (char-equal (elt org-icalendar-date-time-format
354 (1- (length org-icalendar-date-time-format
))) ?Z
))
356 (defvar org-agenda-default-appointment-duration
) ; From org-agenda.el.
357 (defun org-icalendar-convert-timestamp (timestamp keyword
&optional end utc
)
358 "Convert TIMESTAMP to iCalendar format.
360 TIMESTAMP is a timestamp object. KEYWORD is added in front of
361 it, in order to make a complete line (e.g. \"DTSTART\").
363 When optional argument END is non-nil, use end of time range.
364 Also increase the hour by two (if time string contains a time),
365 or the day by one (if it does not contain a time) when no
366 explicit ending time is specified.
368 When optional argument UTC is non-nil, time will be expressed in
369 Universal Time, ignoring `org-icalendar-date-time-format'."
370 (let* ((year-start (org-element-property :year-start timestamp
))
371 (year-end (org-element-property :year-end timestamp
))
372 (month-start (org-element-property :month-start timestamp
))
373 (month-end (org-element-property :month-end timestamp
))
374 (day-start (org-element-property :day-start timestamp
))
375 (day-end (org-element-property :day-end timestamp
))
376 (hour-start (org-element-property :hour-start timestamp
))
377 (hour-end (org-element-property :hour-end timestamp
))
378 (minute-start (org-element-property :minute-start timestamp
))
379 (minute-end (org-element-property :minute-end timestamp
))
380 (with-time-p minute-start
)
382 (equal (list year-start month-start day-start hour-start minute-start
)
383 (list year-end month-end day-end hour-end minute-end
)))
384 (mi (cond ((not with-time-p
) 0)
385 ((not end
) minute-start
)
386 ((and org-agenda-default-appointment-duration equal-bounds-p
)
387 (+ minute-end org-agenda-default-appointment-duration
))
389 (h (cond ((not with-time-p
) 0)
390 ((not end
) hour-start
)
391 ((or (not equal-bounds-p
)
392 org-agenda-default-appointment-duration
)
395 (d (cond ((not end
) day-start
)
396 ((not with-time-p
) (1+ day-end
))
398 (m (if end month-end month-start
))
399 (y (if end year-end year-start
)))
403 (cond (utc ":%Y%m%dT%H%M%SZ")
404 ((not with-time-p
) ";VALUE=DATE:%Y%m%d")
405 (t (replace-regexp-in-string "%Z"
406 org-icalendar-timezone
407 org-icalendar-date-time-format
409 ;; Convert timestamp into internal time in order to use
410 ;; `format-time-string' and fix any mistake (i.e. MI >= 60).
411 (encode-time 0 mi h d m y
)
412 (or utc
(and with-time-p
(org-icalendar-use-UTC-date-time-p)))))))
414 (defun org-icalendar-dtstamp ()
415 "Return DTSTAMP property, as a string."
416 (format-time-string "DTSTAMP:%Y%m%dT%H%M%SZ" nil t
))
418 (defun org-icalendar-get-categories (entry info
)
419 "Return categories according to `org-icalendar-categories'.
420 ENTRY is a headline or an inlinetask element. INFO is a plist
421 used as a communication channel."
429 (push (org-export-get-category entry info
) categories
))
431 (let ((todo (org-element-property :todo-keyword entry
)))
432 (and todo
(push todo categories
))))
435 (append (nreverse (org-export-get-tags entry info
))
439 (append (nreverse (org-export-get-tags entry info nil t
))
441 org-icalendar-categories
)
442 ;; Return list of categories, following specified order.
443 (nreverse categories
))) ","))
445 (defun org-icalendar-transcode-diary-sexp (sexp uid summary
)
446 "Transcode a diary sexp into iCalendar format.
447 SEXP is the diary sexp being transcoded, as a string. UID is the
448 unique identifier for the entry. SUMMARY defines a short summary
449 or subject for the event."
450 (when (require 'icalendar nil t
)
451 (org-element-normalize-string
453 (let ((sexp (if (not (string-match "\\`<%%" sexp
)) sexp
454 (concat (substring sexp
1 -
1) " " summary
))))
455 (put-text-property 0 1 'uid uid sexp
)
457 (org-diary-to-ical-string (current-buffer))))))
459 (defun org-icalendar-cleanup-string (s)
460 "Cleanup string S according to RFC 5545."
462 ;; Protect "\", "," and ";" characters. and replace newline
463 ;; characters with literal \n.
464 (replace-regexp-in-string
466 (replace-regexp-in-string "[\\,;]" "\\\&" s
)
469 (defun org-icalendar-fold-string (s)
470 "Fold string S according to RFC 5545."
471 (org-element-normalize-string
474 ;; Limit each line to a maximum of 75 characters. If it is
475 ;; longer, fold it by using "\n " as a continuation marker.
476 (let ((len (length line
)))
478 (let ((folded-line (substring line
0 75))
481 ;; Since continuation marker takes up one character on the
482 ;; line, real contents must be split at 74 chars.
483 (while (< (setq chunk-end
(+ chunk-start
74)) len
)
485 (concat folded-line
"\n "
486 (substring line chunk-start chunk-end
))
487 chunk-start chunk-end
))
488 (concat folded-line
"\n " (substring line chunk-start
))))))
489 (org-split-string s
"\n") "\n")))
495 (defun org-icalendar-clear-blank-lines (headline back-end info
)
496 "Remove blank lines in HEADLINE export.
497 HEADLINE is a string representing a transcoded headline.
498 BACK-END and INFO are ignored."
499 (replace-regexp-in-string "^\\(?:[ \t]*\n\\)+" "" headline
))
503 ;;; Transcode Functions
505 ;;;; Headline and Inlinetasks
507 ;; The main function is `org-icalendar-entry', which extracts
508 ;; information from a headline or an inlinetask (summary,
509 ;; description...) and then delegates code generation to
510 ;; `org-icalendar--vtodo' and `org-icalendar--vevent', depending
511 ;; on the component needed.
513 ;; Obviously, `org-icalendar--valarm' handles alarms, which can
514 ;; happen within a VTODO component.
516 (defun org-icalendar-entry (entry contents info
)
517 "Transcode ENTRY element into iCalendar format.
519 ENTRY is either a headline or an inlinetask. CONTENTS is
520 ignored. INFO is a plist used as a communication channel.
522 This function is called on every headline, the section below
523 it (minus inlinetasks) being its contents. It tries to create
524 VEVENT and VTODO components out of scheduled date, deadline date,
525 plain timestamps, diary sexps. It also calls itself on every
526 inlinetask within the section."
527 (unless (org-element-property :footnote-section-p entry
)
528 (let* ((type (org-element-type entry
))
529 ;; Determine contents really associated to the entry. For
530 ;; a headline, limit them to section, if any. For an
531 ;; inlinetask, this is every element within the task.
533 (if (eq type
'inlinetask
)
534 (cons 'org-data
(cons nil
(org-element-contents entry
)))
535 (let ((first (car (org-element-contents entry
))))
536 (and (eq (org-element-type first
) 'section
)
538 (cons nil
(org-element-contents first
))))))))
540 (unless (and (plist-get info
:icalendar-agenda-view
)
541 (not (org-element-property :ICALENDAR-MARK entry
)))
542 (let ((todo-type (org-element-property :todo-type entry
))
543 (uid (or (org-element-property :ID entry
) (org-id-new)))
544 (summary (org-icalendar-cleanup-string
545 (or (org-element-property :SUMMARY entry
)
547 (org-element-property :title entry
) info
))))
548 (loc (org-icalendar-cleanup-string
549 (org-element-property :LOCATION entry
)))
550 ;; Build description of the entry from associated
551 ;; section (headline) or contents (inlinetask).
553 (org-icalendar-cleanup-string
554 (or (org-element-property :DESCRIPTION entry
)
555 (let ((contents (org-export-data inside info
)))
557 ((not (org-string-nw-p contents
)) nil
)
558 ((wholenump org-icalendar-include-body
)
559 (let ((contents (org-trim contents
)))
561 contents
0 (min (length contents
)
562 org-icalendar-include-body
))))
563 (org-icalendar-include-body (org-trim contents
)))))))
564 (cat (org-icalendar-get-categories entry info
)))
566 ;; Events: Delegate to `org-icalendar--vevent' to
567 ;; generate "VEVENT" component from scheduled, deadline,
568 ;; or any timestamp in the entry.
569 (let ((deadline (org-element-property :deadline entry
)))
571 (memq (if todo-type
'event-if-todo
'event-if-not-todo
)
572 org-icalendar-use-deadline
)
573 (org-icalendar--vevent
574 entry deadline
(concat "DL-" uid
)
575 (concat "DL: " summary
) loc desc cat
)))
576 (let ((scheduled (org-element-property :scheduled entry
)))
578 (memq (if todo-type
'event-if-todo
'event-if-not-todo
)
579 org-icalendar-use-scheduled
)
580 (org-icalendar--vevent
581 entry scheduled
(concat "SC-" uid
)
582 (concat "S: " summary
) loc desc cat
)))
583 ;; When collecting plain timestamps from a headline and
584 ;; its title, skip inlinetasks since collection will
585 ;; happen once ENTRY is one of them.
589 (org-element-map (cons (org-element-property :title entry
)
590 (org-element-contents inside
))
593 (when (let ((type (org-element-property :type ts
)))
594 (case (plist-get info
:with-timestamps
)
595 (active (memq type
'(active active-range
)))
596 (inactive (memq type
'(inactive inactive-range
)))
598 (let ((uid (format "TS%d-%s" (incf counter
) uid
)))
599 (org-icalendar--vevent
600 entry ts uid summary loc desc cat
))))
601 info nil
(and (eq type
'headline
) 'inlinetask
))
603 ;; Task: First check if it is appropriate to export it.
604 ;; If so, call `org-icalendar--vtodo' to transcode it
605 ;; into a "VTODO" component.
607 (case (plist-get info
:icalendar-include-todo
)
610 (and (eq type
'headline
)
611 (not (org-icalendar-blocked-headline-p
613 ((t) (eq todo-type
'todo
))))
614 (org-icalendar--vtodo entry uid summary loc desc cat
))
615 ;; Diary-sexp: Collect every diary-sexp element within
616 ;; ENTRY and its title, and transcode them. If ENTRY is
617 ;; a headline, skip inlinetasks: they will be handled
619 (when org-icalendar-include-sexps
621 (mapconcat #'identity
623 (cons (org-element-property :title entry
)
624 (org-element-contents inside
))
627 (org-icalendar-transcode-diary-sexp
628 (org-element-property :value sexp
)
629 (format "DS%d-%s" (incf counter
) uid
)
631 info nil
(and (eq type
'headline
) 'inlinetask
))
633 ;; If ENTRY is a headline, call current function on every
634 ;; inlinetask within it. In agenda export, this is independent
635 ;; from the mark (or lack thereof) on the entry.
636 (when (eq type
'headline
)
637 (mapconcat #'identity
638 (org-element-map inside
'inlinetask
639 (lambda (task) (org-icalendar-entry task nil info
))
641 ;; Don't forget components from inner entries.
644 (defun org-icalendar--vevent
645 (entry timestamp uid summary location description categories
)
646 "Create a VEVENT component.
648 ENTRY is either a headline or an inlinetask element. TIMESTAMP
649 is a timestamp object defining the date-time of the event. UID
650 is the unique identifier for the event. SUMMARY defines a short
651 summary or subject for the event. LOCATION defines the intended
652 venue for the event. DESCRIPTION provides the complete
653 description of the event. CATEGORIES defines the categories the
656 Return VEVENT component as a string."
657 (org-icalendar-fold-string
658 (if (eq (org-element-property :type timestamp
) 'diary
)
659 (org-icalendar-transcode-diary-sexp
660 (org-element-property :raw-value timestamp
) uid summary
)
661 (concat "BEGIN:VEVENT\n"
662 (org-icalendar-dtstamp) "\n"
664 (org-icalendar-convert-timestamp timestamp
"DTSTART") "\n"
665 (org-icalendar-convert-timestamp timestamp
"DTEND" t
) "\n"
667 (when (org-element-property :repeater-type timestamp
)
668 (format "RRULE:FREQ=%s;INTERVAL=%d\n"
669 (case (org-element-property :repeater-unit timestamp
)
670 (hour "HOURLY") (day "DAILY") (week "WEEKLY")
671 (month "MONTHLY") (year "YEARLY"))
672 (org-element-property :repeater-value timestamp
)))
673 "SUMMARY:" summary
"\n"
674 (and (org-string-nw-p location
) (format "LOCATION:%s\n" location
))
675 (and (org-string-nw-p description
)
676 (format "DESCRIPTION:%s\n" description
))
677 "CATEGORIES:" categories
"\n"
679 (org-icalendar--valarm entry timestamp summary
)
682 (defun org-icalendar--vtodo
683 (entry uid summary location description categories
)
684 "Create a VTODO component.
686 ENTRY is either a headline or an inlinetask element. UID is the
687 unique identifier for the task. SUMMARY defines a short summary
688 or subject for the task. LOCATION defines the intended venue for
689 the task. DESCRIPTION provides the complete description of the
690 task. CATEGORIES defines the categories the task belongs to.
692 Return VTODO component as a string."
693 (let ((start (or (and (memq 'todo-start org-icalendar-use-scheduled
)
694 (org-element-property :scheduled entry
))
695 ;; If we can't use a scheduled time for some
696 ;; reason, start task now.
697 (let ((now (decode-time (current-time))))
700 :minute-start
(nth 1 now
)
701 :hour-start
(nth 2 now
)
702 :day-start
(nth 3 now
)
703 :month-start
(nth 4 now
)
704 :year-start
(nth 5 now
)))))))
705 (org-icalendar-fold-string
706 (concat "BEGIN:VTODO\n"
708 (org-icalendar-dtstamp) "\n"
709 (org-icalendar-convert-timestamp start
"DTSTART") "\n"
710 (and (memq 'todo-due org-icalendar-use-deadline
)
711 (org-element-property :deadline entry
)
712 (concat (org-icalendar-convert-timestamp
713 (org-element-property :deadline entry
) "DUE")
715 "SUMMARY:" summary
"\n"
716 (and (org-string-nw-p location
) (format "LOCATION:%s\n" location
))
717 (and (org-string-nw-p description
)
718 (format "DESCRIPTION:%s\n" description
))
719 "CATEGORIES:" categories
"\n"
721 (format "PRIORITY:%d\n"
722 (let ((pri (or (org-element-property :priority entry
)
723 org-default-priority
)))
724 (floor (- 9 (* 8.
(/ (float (- org-lowest-priority pri
))
725 (- org-lowest-priority
726 org-highest-priority
)))))))
727 (format "STATUS:%s\n"
728 (if (eq (org-element-property :todo-type entry
) 'todo
)
733 (defun org-icalendar--valarm (entry timestamp summary
)
734 "Create a VALARM component.
736 ENTRY is the calendar entry triggering the alarm. TIMESTAMP is
737 the start date-time of the entry. SUMMARY defines a short
738 summary or subject for the task.
740 Return VALARM component as a string, or nil if it isn't allowed."
741 ;; Create a VALARM entry if the entry is timed. This is not very
743 ;; (a) only one alarm per entry is defined,
744 ;; (b) only minutes are allowed for the trigger period ahead of the
746 ;; (c) only a DISPLAY action is defined. [ESF]
749 (org-element-property :APPT_WARNTIME entry
)))
750 (if warntime
(string-to-number warntime
) 0))))
751 (and (or (> alarm-time
0) (> org-icalendar-alarm-time
0))
752 (org-element-property :hour-start timestamp
)
753 (format "BEGIN:VALARM
759 (if (zerop alarm-time
) org-icalendar-alarm-time alarm-time
)))))
764 (defun org-icalendar-template (contents info
)
765 "Return complete document string after iCalendar conversion.
766 CONTENTS is the transcoded contents string. INFO is a plist used
767 as a communication channel."
768 (org-icalendar--vcalendar
770 (if (not (plist-get info
:input-file
)) (buffer-name (buffer-base-buffer))
771 (file-name-nondirectory
772 (file-name-sans-extension (plist-get info
:input-file
))))
774 (if (not (plist-get info
:with-author
)) ""
775 (org-export-data (plist-get info
:author
) info
))
777 (if (org-string-nw-p org-icalendar-timezone
) org-icalendar-timezone
778 (cadr (current-time-zone)))
780 (org-export-data (plist-get info
:title
) info
)
783 (defun org-icalendar--vcalendar (name owner tz description contents
)
784 "Create a VCALENDAR component.
785 NAME, OWNER, TZ, DESCRIPTION and CONTENTS are all strings giving,
786 respectively, the name of the calendar, its owner, the timezone
787 used, a short description and the other components included."
788 (concat (format "BEGIN:VCALENDAR
791 PRODID:-//%s//Emacs with Org mode//EN
794 CALSCALE:GREGORIAN\n"
795 (org-icalendar-cleanup-string name
)
796 (org-icalendar-cleanup-string owner
)
797 (org-icalendar-cleanup-string tz
)
798 (org-icalendar-cleanup-string description
))
804 ;;; Interactive Functions
807 (defun org-icalendar-export-to-ics
808 (&optional async subtreep visible-only body-only
)
809 "Export current buffer to an iCalendar file.
811 If narrowing is active in the current buffer, only export its
814 If a region is active, export that region.
816 A non-nil optional argument ASYNC means the process should happen
817 asynchronously. The resulting file should be accessible through
818 the `org-export-stack' interface.
820 When optional argument SUBTREEP is non-nil, export the sub-tree
821 at point, extracting information from the headline properties
824 When optional argument VISIBLE-ONLY is non-nil, don't export
825 contents of hidden elements.
827 When optional argument BODY-ONLY is non-nil, only write code
828 between \"BEGIN:VCALENDAR\" and \"END:VCALENDAR\".
830 Return ICS file name."
832 (let ((file (buffer-file-name (buffer-base-buffer))))
833 (when (and file org-icalendar-store-UID
)
834 (org-icalendar-create-uid file
'warn-user
)))
835 ;; Export part. Since this back-end is backed up by `ascii', ensure
836 ;; links will not be collected at the end of sections.
837 (let ((outfile (org-export-output-file-name ".ics" subtreep
)))
838 (org-export-to-file 'icalendar outfile
839 async subtreep visible-only body-only
'(:ascii-charset utf-8
)
841 (run-hook-with-args 'org-icalendar-after-save-hook file
) nil
))))
844 (defun org-icalendar-export-agenda-files (&optional async
)
845 "Export all agenda files to iCalendar files.
846 When optional argument ASYNC is non-nil, export happens in an
850 ;; Asynchronous export is not interactive, so we will not call
851 ;; `org-check-agenda-file'. Instead we remove any non-existent
852 ;; agenda file from the list.
853 (let ((files (org-remove-if-not 'file-exists-p
(org-agenda-files t
))))
854 (org-export-async-start
856 (mapc (lambda (f) (org-export-add-to-stack f
'icalendar
))
860 (with-current-buffer (org-get-agenda-file-buffer file
)
861 (push (expand-file-name (org-icalendar-export-to-ics))
865 (let ((files (org-agenda-files t
)))
866 (org-agenda-prepare-buffers files
)
870 (org-check-agenda-file file
)
871 (with-current-buffer (org-get-agenda-file-buffer file
)
872 (org-icalendar-export-to-ics))))
874 (org-release-buffers org-agenda-new-buffers
)))))
877 (defun org-icalendar-combine-agenda-files (&optional async
)
878 "Combine all agenda files into a single iCalendar file.
880 A non-nil optional argument ASYNC means the process should happen
881 asynchronously. The resulting file should be accessible through
882 the `org-export-stack' interface.
884 The file is stored under the name chosen in
885 `org-icalendar-combined-agenda-file'."
888 (let ((files (org-remove-if-not 'file-exists-p
(org-agenda-files t
))))
889 (org-export-async-start
891 (org-export-add-to-stack
892 (expand-file-name org-icalendar-combined-agenda-file
)
894 `(apply 'org-icalendar--combine-files nil
',files
)))
895 (apply 'org-icalendar--combine-files nil
(org-agenda-files t
))))
897 (defun org-icalendar-export-current-agenda (file)
898 "Export current agenda view to an iCalendar FILE.
899 This function assumes major mode for current buffer is
901 (let (org-export-babel-evaluate ; Don't evaluate Babel block
902 (org-icalendar-combined-agenda-file file
)
904 ;; Collect the markers pointing to entries in the current
908 (goto-char (point-min))
910 (let ((m (or (org-get-at-bol 'org-hd-marker
)
911 (org-get-at-bol 'org-marker
))))
912 (and m
(push m markers
)))
913 (beginning-of-line 2)))
914 (nreverse markers
))))
915 (apply 'org-icalendar--combine-files
916 ;; Build restriction alist.
918 ;; Sort markers in each association within RESTRICTION.
919 (mapcar (lambda (x) (setcdr x
(sort (copy-sequence (cdr x
)) '<)) x
)
920 (dolist (m marker-list restriction
)
921 (let* ((pos (marker-position m
))
922 (file (buffer-file-name
923 (org-base-buffer (marker-buffer m
))))
924 (file-markers (assoc file restriction
)))
925 ;; Add POS in FILE association if one exists
926 ;; or create a new association for FILE.
927 (if file-markers
(push pos
(cdr file-markers
))
928 (push (list file pos
) restriction
))))))
929 (org-agenda-files nil
'ifmode
))))
931 (defun org-icalendar--combine-files (restriction &rest files
)
932 "Combine entries from multiple files into an iCalendar file.
933 RESTRICTION, when non-nil, is an alist where key is a file name
934 and value a list of buffer positions pointing to entries that
935 should appear in the calendar. It only makes sense if the
936 function was called from an agenda buffer. FILES is a list of
937 files to build the calendar from."
938 (org-agenda-prepare-buffers files
)
941 (with-temp-file org-icalendar-combined-agenda-file
943 (org-icalendar--vcalendar
945 org-icalendar-combined-name
949 (or (org-string-nw-p org-icalendar-timezone
)
950 (cadr (current-time-zone)))
952 org-icalendar-combined-description
959 (org-check-agenda-file file
)
960 (with-current-buffer (org-get-agenda-file-buffer file
)
961 (let ((marks (cdr (assoc (expand-file-name file
)
963 ;; Create ID if necessary.
964 (when org-icalendar-store-UID
965 (org-icalendar-create-uid file t marks
))
966 (unless (and restriction
(not marks
))
967 ;; Add a hook adding :ICALENDAR_MARK: property
968 ;; to each entry appearing in agenda view.
969 ;; Use `apply-partially' because the function
970 ;; still has to accept one argument.
971 (let ((org-export-before-processing-hook
972 (cons (apply-partially
973 (lambda (m-list dummy
)
976 m
"ICALENDAR-MARK" "t"))
979 org-export-before-processing-hook
)))
982 (list :ascii-charset
'utf-8
983 :icalendar-agenda-view restriction
))))))))
985 ;; BBDB anniversaries.
986 (when (and org-icalendar-include-bbdb-anniversaries
987 (require 'org-bbdb nil t
))
988 (with-output-to-string (org-bbdb-anniv-export-ical)))))))
989 (run-hook-with-args 'org-icalendar-after-save-hook
990 org-icalendar-combined-agenda-file
))
991 (org-release-buffers org-agenda-new-buffers
)))
994 (provide 'ox-icalendar
)
997 ;; generated-autoload-file: "org-loaddefs.el"
1000 ;;; ox-icalendar.el ends here