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 mode
181 :group
'org-export-icalendar
184 (defcustom org-icalendar-include-body t
185 "Amount of text below headline to be included in iCalendar export.
186 This is a number of characters that should maximally be included.
187 Properties, scheduling and clocking lines will always be removed.
188 The text will be inserted into the DESCRIPTION field."
189 :group
'org-export-icalendar
191 (const :tag
"Nothing" nil
)
192 (const :tag
"Everything" t
)
193 (integer :tag
"Max characters")))
195 (defcustom org-icalendar-store-UID nil
196 "Non-nil means store any created UIDs in properties.
198 The iCalendar standard requires that all entries have a unique identifier.
199 Org will create these identifiers as needed. When this variable is non-nil,
200 the created UIDs will be stored in the ID property of the entry. Then the
201 next time this entry is exported, it will be exported with the same UID,
202 superseding the previous form of it. This is essential for
203 synchronization services.
205 This variable is not turned on by default because we want to avoid creating
206 a property drawer in every entry if people are only playing with this feature,
207 or if they are only using it locally."
208 :group
'org-export-icalendar
211 (defcustom org-icalendar-timezone
(getenv "TZ")
212 "The time zone string for iCalendar export.
213 When nil or the empty string, use output
214 from (current-time-zone)."
215 :group
'org-export-icalendar
217 (const :tag
"Unspecified" nil
)
218 (string :tag
"Time zone")))
220 (defcustom org-icalendar-date-time-format
":%Y%m%dT%H%M%S"
221 "Format-string for exporting icalendar DATE-TIME.
223 See `format-time-string' for a full documentation. The only
224 difference is that `org-icalendar-timezone' is used for %Z.
226 Interesting value are:
227 - \":%Y%m%dT%H%M%S\" for local time
228 - \";TZID=%Z:%Y%m%dT%H%M%S\" for local time with explicit timezone
229 - \":%Y%m%dT%H%M%SZ\" for time expressed in Universal Time"
230 :group
'org-export-icalendar
233 (const :tag
"Local time" ":%Y%m%dT%H%M%S")
234 (const :tag
"Explicit local time" ";TZID=%Z:%Y%m%dT%H%M%S")
235 (const :tag
"Universal time" ":%Y%m%dT%H%M%SZ")
236 (string :tag
"Explicit format")))
238 (defvar org-icalendar-after-save-hook nil
239 "Hook run after an iCalendar file has been saved.
240 This hook is run with the name of the file as argument. A good
241 way to use this is to tell a desktop calendar application to
242 re-read the iCalendar file.")
248 (org-export-define-derived-backend 'icalendar
'ascii
249 :translate-alist
'((clock . ignore
)
250 (footnote-definition . ignore
)
251 (footnote-reference . ignore
)
252 (headline . org-icalendar-entry
)
253 (inlinetask . ignore
)
256 (inner-template .
(lambda (c i
) c
))
257 (template . org-icalendar-template
))
260 "ICALENDAR_EXCLUDE_TAGS" nil org-icalendar-exclude-tags split
)
261 (:with-timestamps nil
"<" org-icalendar-with-timestamps
)
262 (:with-vtodo nil nil org-icalendar-include-todo
)
263 ;; The following property will be non-nil when export has been
264 ;; started from org-agenda-mode. In this case, any entry without
265 ;; a non-nil "ICALENDAR_MARK" property will be ignored.
266 (:icalendar-agenda-view nil nil nil
))
268 '((:filter-headline . org-icalendar-clear-blank-lines
))
270 '(?c
"Export to iCalendar"
271 ((?f
"Current file" org-icalendar-export-to-ics
)
272 (?a
"All agenda files"
273 (lambda (a s v b
) (org-icalendar-export-agenda-files a
)))
274 (?c
"Combine all agenda files"
275 (lambda (a s v b
) (org-icalendar-combine-agenda-files a
))))))
279 ;;; Internal Functions
281 (defun org-icalendar-create-uid (file &optional bell h-markers
)
282 "Set ID property on headlines missing it in FILE.
283 When optional argument BELL is non-nil, inform the user with
284 a message if the file was modified. With optional argument
285 H-MARKERS non-nil, it is a list of markers for the headlines
286 which will be updated."
287 (let ((pt (if h-markers
(goto-char (car h-markers
)) (point-min)))
291 (let ((entry (org-element-at-point)))
292 (unless (or (< (point) pt
) (org-element-property :ID entry
))
294 (setq modified-flag t
)
296 (when h-markers
(setq org-map-continue-from
(pop h-markers
)))))
298 (when (and bell modified-flag
)
299 (message "ID properties created in file \"%s\"" file
)
302 (defun org-icalendar-blocked-headline-p (headline info
)
303 "Non-nil when HEADLINE is considered to be blocked.
305 INFO is a plist used as a communication channel.
307 a headline is blocked when either:
309 - It has children which are not all in a completed state.
311 - It has a parent with the property :ORDERED:, and there are
312 siblings prior to it with incomplete status.
314 - Its parent is blocked because it has siblings that should be
315 done first or is a child of a blocked grandparent entry."
317 ;; Check if any child is not done.
318 (org-element-map headline
'headline
319 (lambda (hl) (eq (org-element-property :todo-type hl
) 'todo
))
321 ;; Check :ORDERED: node property.
323 (let ((current headline
))
324 (mapc (lambda (parent)
326 ((not (org-element-property :todo-keyword parent
))
327 (throw 'blockedp nil
))
328 ((org-not-nil (org-element-property :ORDERED parent
))
329 (let ((sibling current
))
330 (while (setq sibling
(org-export-get-previous-element
332 (when (eq (org-element-property :todo-type sibling
) 'todo
)
333 (throw 'blockedp t
)))))
334 (t (setq current parent
))))
335 (org-export-get-genealogy headline
))
338 (defun org-icalendar-use-UTC-date-time-p ()
339 "Non-nil when `org-icalendar-date-time-format' requires UTC time."
340 (char-equal (elt org-icalendar-date-time-format
341 (1- (length org-icalendar-date-time-format
))) ?Z
))
343 (defvar org-agenda-default-appointment-duration
) ; From org-agenda.el.
344 (defun org-icalendar-convert-timestamp (timestamp keyword
&optional end utc
)
345 "Convert TIMESTAMP to iCalendar format.
347 TIMESTAMP is a timestamp object. KEYWORD is added in front of
348 it, in order to make a complete line (e.g. \"DTSTART\").
350 When optional argument END is non-nil, use end of time range.
351 Also increase the hour by two (if time string contains a time),
352 or the day by one (if it does not contain a time) when no
353 explicit ending time is specified.
355 When optional argument UTC is non-nil, time will be expressed in
356 Universal Time, ignoring `org-icalendar-date-time-format'."
357 (let* ((year-start (org-element-property :year-start timestamp
))
358 (year-end (org-element-property :year-end timestamp
))
359 (month-start (org-element-property :month-start timestamp
))
360 (month-end (org-element-property :month-end timestamp
))
361 (day-start (org-element-property :day-start timestamp
))
362 (day-end (org-element-property :day-end timestamp
))
363 (hour-start (org-element-property :hour-start timestamp
))
364 (hour-end (org-element-property :hour-end timestamp
))
365 (minute-start (org-element-property :minute-start timestamp
))
366 (minute-end (org-element-property :minute-end timestamp
))
367 (with-time-p minute-start
)
369 (equal (list year-start month-start day-start hour-start minute-start
)
370 (list year-end month-end day-end hour-end minute-end
)))
371 (mi (cond ((not with-time-p
) 0)
372 ((not end
) minute-start
)
373 ((and org-agenda-default-appointment-duration equal-bounds-p
)
374 (+ minute-end org-agenda-default-appointment-duration
))
376 (h (cond ((not with-time-p
) 0)
377 ((not end
) hour-start
)
378 ((or (not equal-bounds-p
)
379 org-agenda-default-appointment-duration
)
382 (d (cond ((not end
) day-start
)
383 ((not with-time-p
) (1+ day-end
))
385 (m (if end month-end month-start
))
386 (y (if end year-end year-start
)))
390 (cond (utc ":%Y%m%dT%H%M%SZ")
391 ((not with-time-p
) ";VALUE=DATE:%Y%m%d")
392 (t (replace-regexp-in-string "%Z"
393 org-icalendar-timezone
394 org-icalendar-date-time-format
396 ;; Convert timestamp into internal time in order to use
397 ;; `format-time-string' and fix any mistake (i.e. MI >= 60).
398 (encode-time 0 mi h d m y
)
399 (or utc
(and with-time-p
(org-icalendar-use-UTC-date-time-p)))))))
401 (defun org-icalendar-dtstamp ()
402 "Return DTSTAMP property, as a string."
403 (format-time-string "DTSTAMP:%Y%m%dT%H%M%SZ" nil t
))
405 (defun org-icalendar-get-categories (entry info
)
406 "Return categories according to `org-icalendar-categories'.
407 ENTRY is a headline or an inlinetask element. INFO is a plist
408 used as a communication channel."
416 (push (org-export-get-category entry info
) categories
))
418 (let ((todo (org-element-property :todo-keyword entry
)))
419 (and todo
(push todo categories
))))
422 (append (nreverse (org-export-get-tags entry info
))
426 (append (nreverse (org-export-get-tags entry info nil t
))
428 org-icalendar-categories
)
429 ;; Return list of categories, following specified order.
430 (nreverse categories
))) ","))
432 (defun org-icalendar-transcode-diary-sexp (sexp uid summary
)
433 "Transcode a diary sexp into iCalendar format.
434 SEXP is the diary sexp being transcoded, as a string. UID is the
435 unique identifier for the entry. SUMMARY defines a short summary
436 or subject for the event."
437 (when (require 'icalendar nil t
)
438 (org-element-normalize-string
440 (let ((sexp (if (not (string-match "\\`<%%" sexp
)) sexp
441 (concat (substring sexp
1 -
1) " " summary
))))
442 (put-text-property 0 1 'uid uid sexp
)
444 (org-diary-to-ical-string (current-buffer))))))
446 (defun org-icalendar-cleanup-string (s)
447 "Cleanup string S according to RFC 5545."
449 ;; Protect "\", "," and ";" characters. and replace newline
450 ;; characters with literal \n.
451 (replace-regexp-in-string
453 (replace-regexp-in-string "[\\,;]" "\\\&" s
)
456 (defun org-icalendar-fold-string (s)
457 "Fold string S according to RFC 5545."
458 (org-element-normalize-string
461 ;; Limit each line to a maximum of 75 characters. If it is
462 ;; longer, fold it by using "\n " as a continuation marker.
463 (let ((len (length line
)))
465 (let ((folded-line (substring line
0 75))
468 ;; Since continuation marker takes up one character on the
469 ;; line, real contents must be split at 74 chars.
470 (while (< (setq chunk-end
(+ chunk-start
74)) len
)
472 (concat folded-line
"\n "
473 (substring line chunk-start chunk-end
))
474 chunk-start chunk-end
))
475 (concat folded-line
"\n " (substring line chunk-start
))))))
476 (org-split-string s
"\n") "\n")))
482 (defun org-icalendar-clear-blank-lines (headline back-end info
)
483 "Remove trailing blank lines in HEADLINE export.
484 HEADLINE is a string representing a transcoded headline.
485 BACK-END and INFO are ignored."
486 (replace-regexp-in-string "^\\(?:[ \t]*\n\\)*" "" headline
))
490 ;;; Transcode Functions
492 ;;;; Headline and Inlinetasks
494 ;; The main function is `org-icalendar-entry', which extracts
495 ;; information from a headline or an inlinetask (summary,
496 ;; description...) and then delegates code generation to
497 ;; `org-icalendar--vtodo' and `org-icalendar--vevent', depending
498 ;; on the component needed.
500 ;; Obviously, `org-icalendar--valarm' handles alarms, which can
501 ;; happen within a VTODO component.
503 (defun org-icalendar-entry (entry contents info
)
504 "Transcode ENTRY element into iCalendar format.
506 ENTRY is either a headline or an inlinetask. CONTENTS is
507 ignored. INFO is a plist used as a communication channel.
509 This function is called on every headline, the section below
510 it (minus inlinetasks) being its contents. It tries to create
511 VEVENT and VTODO components out of scheduled date, deadline date,
512 plain timestamps, diary sexps. It also calls itself on every
513 inlinetask within the section."
514 (unless (org-element-property :footnote-section-p entry
)
515 (let* ((type (org-element-type entry
))
516 ;; Determine contents really associated to the entry. For
517 ;; a headline, limit them to section, if any. For an
518 ;; inlinetask, this is every element within the task.
520 (if (eq type
'inlinetask
)
521 (cons 'org-data
(cons nil
(org-element-contents entry
)))
522 (let ((first (car (org-element-contents entry
))))
523 (and (eq (org-element-type first
) 'section
)
525 (cons nil
(org-element-contents first
))))))))
527 (unless (and (plist-get info
:icalendar-agenda-view
)
528 (not (org-element-property :ICALENDAR-MARK entry
)))
529 (let ((todo-type (org-element-property :todo-type entry
))
530 (uid (or (org-element-property :ID entry
) (org-id-new)))
531 (summary (org-icalendar-cleanup-string
532 (or (org-element-property :SUMMARY entry
)
534 (org-element-property :title entry
) info
))))
535 (loc (org-icalendar-cleanup-string
536 (org-element-property :LOCATION entry
)))
537 ;; Build description of the entry from associated
538 ;; section (headline) or contents (inlinetask).
540 (org-icalendar-cleanup-string
541 (or (org-element-property :DESCRIPTION entry
)
542 (let ((contents (org-export-data inside info
)))
544 ((not (org-string-nw-p contents
)) nil
)
545 ((wholenump org-icalendar-include-body
)
546 (let ((contents (org-trim contents
)))
548 contents
0 (min (length contents
)
549 org-icalendar-include-body
))))
550 (org-icalendar-include-body (org-trim contents
)))))))
551 (cat (org-icalendar-get-categories entry info
)))
553 ;; Events: Delegate to `org-icalendar--vevent' to
554 ;; generate "VEVENT" component from scheduled, deadline,
555 ;; or any timestamp in the entry.
556 (let ((deadline (org-element-property :deadline entry
)))
558 (memq (if todo-type
'event-if-todo
'event-if-not-todo
)
559 org-icalendar-use-deadline
)
560 (org-icalendar--vevent
561 entry deadline
(concat "DL-" uid
)
562 (concat "DL: " summary
) loc desc cat
)))
563 (let ((scheduled (org-element-property :scheduled entry
)))
565 (memq (if todo-type
'event-if-todo
'event-if-not-todo
)
566 org-icalendar-use-scheduled
)
567 (org-icalendar--vevent
568 entry scheduled
(concat "SC-" uid
)
569 (concat "S: " summary
) loc desc cat
)))
570 ;; When collecting plain timestamps from a headline and
571 ;; its title, skip inlinetasks since collection will
572 ;; happen once ENTRY is one of them.
576 (org-element-map (cons (org-element-property :title entry
)
577 (org-element-contents inside
))
580 (let ((uid (format "TS%d-%s" (incf counter
) uid
)))
581 (org-icalendar--vevent entry ts uid summary loc desc cat
)))
582 info nil
(and (eq type
'headline
) 'inlinetask
))
584 ;; Task: First check if it is appropriate to export it.
585 ;; If so, call `org-icalendar--vtodo' to transcode it
586 ;; into a "VTODO" component.
588 (case (plist-get info
:with-vtodo
)
591 (and (eq type
'headline
)
592 (not (org-icalendar-blocked-headline-p
594 ('t
(eq todo-type
'todo
))))
595 (org-icalendar--vtodo entry uid summary loc desc cat
))
596 ;; Diary-sexp: Collect every diary-sexp element within
597 ;; ENTRY and its title, and transcode them. If ENTRY is
598 ;; a headline, skip inlinetasks: they will be handled
600 (when org-icalendar-include-sexps
604 (cons (org-element-property :title entry
)
605 (org-element-contents inside
))
608 (org-icalendar-transcode-diary-sexp
609 (org-element-property :value sexp
)
610 (format "DS%d-%s" (incf counter
) uid
)
612 info nil
(and (eq type
'headline
) 'inlinetask
))
614 ;; If ENTRY is a headline, call current function on every
615 ;; inlinetask within it. In agenda export, this is independent
616 ;; from the mark (or lack thereof) on the entry.
617 (when (eq type
'headline
)
619 (org-element-map inside
'inlinetask
620 (lambda (task) (org-icalendar-entry task nil info
))
622 ;; Don't forget components from inner entries.
625 (defun org-icalendar--vevent
626 (entry timestamp uid summary location description categories
)
627 "Create a VEVENT component.
629 ENTRY is either a headline or an inlinetask element. TIMESTAMP
630 is a timestamp object defining the date-time of the event. UID
631 is the unique identifier for the event. SUMMARY defines a short
632 summary or subject for the event. LOCATION defines the intended
633 venue for the event. DESCRIPTION provides the complete
634 description of the event. CATEGORIES defines the categories the
637 Return VEVENT component as a string."
638 (org-icalendar-fold-string
639 (if (eq (org-element-property :type timestamp
) 'diary
)
640 (org-icalendar-transcode-diary-sexp
641 (org-element-property :raw-value timestamp
) uid summary
)
642 (concat "BEGIN:VEVENT\n"
643 (org-icalendar-dtstamp) "\n"
645 (org-icalendar-convert-timestamp timestamp
"DTSTART") "\n"
646 (org-icalendar-convert-timestamp timestamp
"DTEND" t
) "\n"
648 (when (org-element-property :repeater-type timestamp
)
649 (format "RRULE:FREQ=%s;INTERVAL=%d\n"
650 (case (org-element-property :repeater-unit timestamp
)
651 (hour "HOURLY") (day "DAILY") (week "WEEKLY")
652 (month "MONTHLY") (year "YEARLY"))
653 (org-element-property :repeater-value timestamp
)))
654 "SUMMARY:" summary
"\n"
655 (and (org-string-nw-p location
) (format "LOCATION:%s\n" location
))
656 (and (org-string-nw-p description
)
657 (format "DESCRIPTION:%s\n" description
))
658 "CATEGORIES:" categories
"\n"
660 (org-icalendar--valarm entry timestamp summary
)
663 (defun org-icalendar--vtodo
664 (entry uid summary location description categories
)
665 "Create a VTODO component.
667 ENTRY is either a headline or an inlinetask element. UID is the
668 unique identifier for the task. SUMMARY defines a short summary
669 or subject for the task. LOCATION defines the intended venue for
670 the task. DESCRIPTION provides the complete description of the
671 task. CATEGORIES defines the categories the task belongs to.
673 Return VTODO component as a string."
674 (let ((start (or (and (memq 'todo-start org-icalendar-use-scheduled
)
675 (org-element-property :scheduled entry
))
676 ;; If we can't use a scheduled time for some
677 ;; reason, start task now.
678 (let ((now (decode-time (current-time))))
681 :minute-start
(nth 1 now
)
682 :hour-start
(nth 2 now
)
683 :day-start
(nth 3 now
)
684 :month-start
(nth 4 now
)
685 :year-start
(nth 5 now
)))))))
686 (org-icalendar-fold-string
687 (concat "BEGIN:VTODO\n"
689 (org-icalendar-dtstamp) "\n"
690 (org-icalendar-convert-timestamp start
"DTSTART") "\n"
691 (and (memq 'todo-due org-icalendar-use-deadline
)
692 (org-element-property :deadline entry
)
693 (concat (org-icalendar-convert-timestamp
694 (org-element-property :deadline entry
) "DUE")
696 "SUMMARY:" summary
"\n"
697 (and (org-string-nw-p location
) (format "LOCATION:%s\n" location
))
698 (and (org-string-nw-p description
)
699 (format "DESCRIPTION:%s\n" description
))
700 "CATEGORIES:" categories
"\n"
702 (format "PRIORITY:%d\n"
703 (let ((pri (or (org-element-property :priority entry
)
704 org-default-priority
)))
705 (floor (- 9 (* 8.
(/ (float (- org-lowest-priority pri
))
706 (- org-lowest-priority
707 org-highest-priority
)))))))
708 (format "STATUS:%s\n"
709 (if (eq (org-element-property :todo-type entry
) 'todo
)
714 (defun org-icalendar--valarm (entry timestamp summary
)
715 "Create a VALARM component.
717 ENTRY is the calendar entry triggering the alarm. TIMESTAMP is
718 the start date-time of the entry. SUMMARY defines a short
719 summary or subject for the task.
721 Return VALARM component as a string, or nil if it isn't allowed."
722 ;; Create a VALARM entry if the entry is timed. This is not very
724 ;; (a) only one alarm per entry is defined,
725 ;; (b) only minutes are allowed for the trigger period ahead of the
727 ;; (c) only a DISPLAY action is defined. [ESF]
730 (org-element-property :APPT_WARNTIME entry
)))
731 (if warntime
(string-to-number warntime
) 0))))
732 (and (or (> alarm-time
0) (> org-icalendar-alarm-time
0))
733 (org-element-property :hour-start timestamp
)
734 (format "BEGIN:VALARM
740 (if (zerop alarm-time
) org-icalendar-alarm-time alarm-time
)))))
745 (defun org-icalendar-template (contents info
)
746 "Return complete document string after iCalendar conversion.
747 CONTENTS is the transcoded contents string. INFO is a plist used
748 as a communication channel."
749 (org-icalendar--vcalendar
751 (if (not (plist-get info
:input-file
)) (buffer-name (buffer-base-buffer))
752 (file-name-nondirectory
753 (file-name-sans-extension (plist-get info
:input-file
))))
755 (if (not (plist-get info
:with-author
)) ""
756 (org-export-data (plist-get info
:author
) info
))
758 (if (org-string-nw-p org-icalendar-timezone
) org-icalendar-timezone
759 (cadr (current-time-zone)))
761 (org-export-data (plist-get info
:title
) info
)
764 (defun org-icalendar--vcalendar (name owner tz description contents
)
765 "Create a VCALENDAR component.
766 NAME, OWNER, TZ, DESCRIPTION and CONTENTS are all strings giving,
767 respectively, the name of the calendar, its owner, the timezone
768 used, a short description and the other components included."
769 (concat (format "BEGIN:VCALENDAR
772 PRODID:-//%s//Emacs with Org mode//EN
775 CALSCALE:GREGORIAN\n"
776 (org-icalendar-cleanup-string name
)
777 (org-icalendar-cleanup-string owner
)
778 (org-icalendar-cleanup-string tz
)
779 (org-icalendar-cleanup-string description
))
785 ;;; Interactive Functions
788 (defun org-icalendar-export-to-ics
789 (&optional async subtreep visible-only body-only
)
790 "Export current buffer to an iCalendar file.
792 If narrowing is active in the current buffer, only export its
795 If a region is active, export that region.
797 A non-nil optional argument ASYNC means the process should happen
798 asynchronously. The resulting file should be accessible through
799 the `org-export-stack' interface.
801 When optional argument SUBTREEP is non-nil, export the sub-tree
802 at point, extracting information from the headline properties
805 When optional argument VISIBLE-ONLY is non-nil, don't export
806 contents of hidden elements.
808 When optional argument BODY-ONLY is non-nil, only write code
809 between \"BEGIN:VCALENDAR\" and \"END:VCALENDAR\".
811 Return ICS file name."
813 (let ((file (buffer-file-name (buffer-base-buffer))))
814 (when (and file org-icalendar-store-UID
)
815 (org-icalendar-create-uid file
'warn-user
)))
816 ;; Export part. Since this back-end is backed up by `ascii', ensure
817 ;; links will not be collected at the end of sections.
818 (let ((outfile (org-export-output-file-name ".ics" subtreep
)))
819 (org-export-to-file 'icalendar outfile
820 async subtreep visible-only body-only
'(:ascii-charset utf-8
)
822 (run-hook-with-args 'org-icalendar-after-save-hook file
) nil
))))
825 (defun org-icalendar-export-agenda-files (&optional async
)
826 "Export all agenda files to iCalendar files.
827 When optional argument ASYNC is non-nil, export happens in an
831 ;; Asynchronous export is not interactive, so we will not call
832 ;; `org-check-agenda-file'. Instead we remove any non-existent
833 ;; agenda file from the list.
834 (let ((files (org-remove-if-not 'file-exists-p
(org-agenda-files t
))))
835 (org-export-async-start
837 (mapc (lambda (f) (org-export-add-to-stack f
'icalendar
))
841 (with-current-buffer (org-get-agenda-file-buffer file
)
842 (push (expand-file-name (org-icalendar-export-to-ics))
846 (let ((files (org-agenda-files t
)))
847 (org-agenda-prepare-buffers files
)
851 (org-check-agenda-file file
)
852 (with-current-buffer (org-get-agenda-file-buffer file
)
853 (org-icalendar-export-to-ics))))
855 (org-release-buffers org-agenda-new-buffers
)))))
858 (defun org-icalendar-combine-agenda-files (&optional async
)
859 "Combine all agenda files into a single iCalendar file.
861 A non-nil optional argument ASYNC means the process should happen
862 asynchronously. The resulting file should be accessible through
863 the `org-export-stack' interface.
865 The file is stored under the name chosen in
866 `org-icalendar-combined-agenda-file'."
869 (let ((files (org-remove-if-not 'file-exists-p
(org-agenda-files t
))))
870 (org-export-async-start
872 (org-export-add-to-stack
873 (expand-file-name org-icalendar-combined-agenda-file
)
875 `(apply 'org-icalendar--combine-files nil
',files
)))
876 (apply 'org-icalendar--combine-files nil
(org-agenda-files t
))))
878 (defun org-icalendar-export-current-agenda (file)
879 "Export current agenda view to an iCalendar FILE.
880 This function assumes major mode for current buffer is
882 (let (org-export-babel-evaluate ; Don't evaluate Babel block
883 (org-icalendar-combined-agenda-file file
)
885 ;; Collect the markers pointing to entries in the current
889 (goto-char (point-min))
891 (let ((m (or (org-get-at-bol 'org-hd-marker
)
892 (org-get-at-bol 'org-marker
))))
893 (and m
(push m markers
)))
894 (beginning-of-line 2)))
895 (nreverse markers
))))
896 (apply 'org-icalendar--combine-files
897 ;; Build restriction alist.
899 ;; Sort markers in each association within RESTRICTION.
900 (mapcar (lambda (x) (setcdr x
(sort (copy-sequence (cdr x
)) '<)) x
)
901 (dolist (m marker-list restriction
)
902 (let* ((pos (marker-position m
))
903 (file (buffer-file-name
904 (org-base-buffer (marker-buffer m
))))
905 (file-markers (assoc file restriction
)))
906 ;; Add POS in FILE association if one exists
907 ;; or create a new association for FILE.
908 (if file-markers
(push pos
(cdr file-markers
))
909 (push (list file pos
) restriction
))))))
910 (org-agenda-files nil
'ifmode
))))
912 (defun org-icalendar--combine-files (restriction &rest files
)
913 "Combine entries from multiple files into an iCalendar file.
914 RESTRICTION, when non-nil, is an alist where key is a file name
915 and value a list of buffer positions pointing to entries that
916 should appear in the calendar. It only makes sense if the
917 function was called from an agenda buffer. FILES is a list of
918 files to build the calendar from."
919 (org-agenda-prepare-buffers files
)
922 (with-temp-file org-icalendar-combined-agenda-file
924 (org-icalendar--vcalendar
926 org-icalendar-combined-name
930 (or (org-string-nw-p org-icalendar-timezone
)
931 (cadr (current-time-zone)))
933 org-icalendar-combined-description
940 (org-check-agenda-file file
)
941 (with-current-buffer (org-get-agenda-file-buffer file
)
942 (let ((marks (cdr (assoc (expand-file-name file
)
944 ;; Create ID if necessary.
945 (when org-icalendar-store-UID
946 (org-icalendar-create-uid file t marks
))
947 (unless (and restriction
(not marks
))
948 ;; Add a hook adding :ICALENDAR_MARK: property
949 ;; to each entry appearing in agenda view.
950 ;; Use `apply-partially' because the function
951 ;; still has to accept one argument.
952 (let ((org-export-before-processing-hook
953 (cons (apply-partially
954 (lambda (m-list dummy
)
957 m
"ICALENDAR-MARK" "t"))
960 org-export-before-processing-hook
)))
963 (list :ascii-charset
'utf-8
964 :icalendar-agenda-view restriction
))))))))
966 ;; BBDB anniversaries.
967 (when (and org-icalendar-include-bbdb-anniversaries
968 (require 'org-bbdb nil t
))
969 (with-output-to-string (org-bbdb-anniv-export-ical)))))))
970 (run-hook-with-args 'org-icalendar-after-save-hook
971 org-icalendar-combined-agenda-file
))
972 (org-release-buffers org-agenda-new-buffers
)))
975 (provide 'ox-icalendar
)
978 ;; generated-autoload-file: "org-loaddefs.el"
981 ;;; ox-icalendar.el ends here