1 ;;; ox-icalendar.el --- iCalendar Back-End for Org Export Engine
3 ;; Copyright (C) 2004-2012 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 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; This library implements an iCalendar back-end for Org generic
28 ;; It provides three commands for export, depending on the chosen
29 ;; source and desired output: `org-icalendar-export-to-ics' (current
30 ;; file), `org-icalendar-export-agenda-files' (agenda files into
31 ;; separate calendars) and `org-icalendar-combined-agenda-file'
32 ;; (agenda files into one combined calendar).
34 ;; It also provides `org-icalendar-export-current-agenda' function,
35 ;; which will create a calendar file from current agenda view. It is
36 ;; meant to be called through `org-agenda-write'.
38 ;; This back-end introduces a new keyword, ICALENDAR_EXCLUDE_TAGS,
39 ;; which allows to specify a different set of exclude tags from other
42 ;; It should follow RFC 5545 specifications.
46 (eval-when-compile (require 'cl
))
48 (declare-function org-bbdb-anniv-export-ical
"org-bbdb" nil
)
52 ;;; User-Configurable Variables
54 (defgroup org-export-icalendar nil
55 "Options specific for iCalendar export back-end."
56 :tag
"Org Export iCalendar"
59 (defcustom org-icalendar-combined-agenda-file
"~/org.ics"
60 "The file name for the iCalendar file covering all agenda files.
61 This file is created with the command \\[org-icalendar-combine-agenda-files].
62 The file name should be absolute. It will be overwritten without warning."
63 :group
'org-export-icalendar
66 (defcustom org-icalendar-alarm-time
0
67 "Number of minutes for triggering an alarm for exported timed events.
69 A zero value (the default) turns off the definition of an alarm trigger
70 for timed events. If non-zero, alarms are created.
72 - a single alarm per entry is defined
73 - The alarm will go off N minutes before the event
74 - only a DISPLAY action is defined."
75 :group
'org-export-icalendar
79 (defcustom org-icalendar-combined-name
"OrgMode"
80 "Calendar name for the combined iCalendar representing all agenda files."
81 :group
'org-export-icalendar
84 (defcustom org-icalendar-combined-description
""
85 "Calendar description for the combined iCalendar (all agenda files)."
86 :group
'org-export-icalendar
89 (defcustom org-icalendar-exclude-tags nil
90 "Tags that exclude a tree from export.
91 This variable allows to specify different exclude tags from other
92 back-ends. It can also be set with the ICAL_EXCLUDE_TAGS
94 :group
'org-export-icalendar
95 :type
'(repeat (string :tag
"Tag")))
97 (defcustom org-icalendar-use-deadline
'(event-if-not-todo todo-due
)
98 "Contexts where iCalendar export should use a deadline time stamp.
100 This is a list with several symbols in it. Valid symbol are:
101 `event-if-todo' Deadlines in TODO entries become calendar events.
102 `event-if-not-todo' Deadlines in non-TODO entries become calendar events.
103 `todo-due' Use deadlines in TODO entries as due-dates"
104 :group
'org-export-icalendar
105 :type
'(set :greedy t
106 (const :tag
"Deadlines in non-TODO entries become events"
108 (const :tag
"Deadline in TODO entries become events"
110 (const :tag
"Deadlines in TODO entries become due-dates"
113 (defcustom org-icalendar-use-scheduled
'(todo-start)
114 "Contexts where iCalendar export should use a scheduling time stamp.
116 This is a list with several symbols in it. Valid symbol are:
117 `event-if-todo' Scheduling time stamps in TODO entries become an event.
118 `event-if-not-todo' Scheduling time stamps in non-TODO entries become an event.
119 `todo-start' Scheduling time stamps in TODO entries become start date.
120 Some calendar applications show TODO entries only after
122 :group
'org-export-icalendar
123 :type
'(set :greedy t
125 "SCHEDULED timestamps in non-TODO entries become events"
127 (const :tag
"SCHEDULED timestamps in TODO entries become events"
129 (const :tag
"SCHEDULED in TODO entries become start date"
132 (defcustom org-icalendar-categories
'(local-tags category
)
133 "Items that should be entered into the \"categories\" field.
135 This is a list of symbols, the following are valid:
136 `category' The Org mode category of the current file or tree
137 `todo-state' The todo state, if any
138 `local-tags' The tags, defined in the current line
139 `all-tags' All tags, including inherited ones."
140 :group
'org-export-icalendar
143 (const :tag
"The file or tree category" category
)
144 (const :tag
"The TODO state" todo-state
)
145 (const :tag
"Tags defined in current line" local-tags
)
146 (const :tag
"All tags, including inherited ones" all-tags
))))
148 (defcustom org-icalendar-with-timestamps
'active
149 "Non-nil means make an event from plain time stamps.
151 It can be set to `active', `inactive', t or nil, in order to make
152 an event from, respectively, only active timestamps, only
153 inactive ones, all of them or none.
155 This variable has precedence over `org-export-with-timestamps'.
156 It can also be set with the #+OPTIONS line, e.g. \"<:t\"."
157 :group
'org-export-icalendar
159 (const :tag
"All timestamps" t
)
160 (const :tag
"Only active timestamps" active
)
161 (const :tag
"Only inactive timestamps" inactive
)
162 (const :tag
"No timestamp" nil
)))
164 (defcustom org-icalendar-include-todo nil
165 "Non-nil means create VTODO components from TODO items.
168 nil don't include any task.
169 t include tasks that are not in DONE state.
170 `unblocked' include all TODO items that are not blocked.
171 `all' include both done and not done items."
172 :group
'org-export-icalendar
174 (const :tag
"None" nil
)
175 (const :tag
"Unfinished" t
)
176 (const :tag
"Unblocked" unblocked
)
177 (const :tag
"All" all
)
178 (repeat :tag
"Specific TODO keywords"
179 (string :tag
"Keyword"))))
181 (defcustom org-icalendar-include-bbdb-anniversaries nil
182 "Non-nil means a combined iCalendar file should include anniversaries.
183 The anniversaries are defined in the BBDB database."
184 :group
'org-export-icalendar
187 (defcustom org-icalendar-include-sexps t
188 "Non-nil means export to iCalendar files should also cover sexp entries.
189 These are entries like in the diary, but directly in an Org mode
191 :group
'org-export-icalendar
194 (defcustom org-icalendar-include-body t
195 "Amount of text below headline to be included in iCalendar export.
196 This is a number of characters that should maximally be included.
197 Properties, scheduling and clocking lines will always be removed.
198 The text will be inserted into the DESCRIPTION field."
199 :group
'org-export-icalendar
201 (const :tag
"Nothing" nil
)
202 (const :tag
"Everything" t
)
203 (integer :tag
"Max characters")))
205 (defcustom org-icalendar-store-UID nil
206 "Non-nil means store any created UIDs in properties.
208 The iCalendar standard requires that all entries have a unique identifier.
209 Org will create these identifiers as needed. When this variable is non-nil,
210 the created UIDs will be stored in the ID property of the entry. Then the
211 next time this entry is exported, it will be exported with the same UID,
212 superseding the previous form of it. This is essential for
213 synchronization services.
215 This variable is not turned on by default because we want to avoid creating
216 a property drawer in every entry if people are only playing with this feature,
217 or if they are only using it locally."
218 :group
'org-export-icalendar
221 (defcustom org-icalendar-timezone
(getenv "TZ")
222 "The time zone string for iCalendar export.
223 When nil or the empty string, use output
224 from (current-time-zone)."
225 :group
'org-export-icalendar
227 (const :tag
"Unspecified" nil
)
228 (string :tag
"Time zone")))
230 (defcustom org-icalendar-date-time-format
":%Y%m%dT%H%M%S"
231 "Format-string for exporting icalendar DATE-TIME.
233 See `format-time-string' for a full documentation. The only
234 difference is that `org-icalendar-timezone' is used for %Z.
236 Interesting value are:
237 - \":%Y%m%dT%H%M%S\" for local time
238 - \";TZID=%Z:%Y%m%dT%H%M%S\" for local time with explicit timezone
239 - \":%Y%m%dT%H%M%SZ\" for time expressed in Universal Time"
240 :group
'org-export-icalendar
243 (const :tag
"Local time" ":%Y%m%dT%H%M%S")
244 (const :tag
"Explicit local time" ";TZID=%Z:%Y%m%dT%H%M%S")
245 (const :tag
"Universal time" ":%Y%m%dT%H%M%SZ")
246 (string :tag
"Explicit format")))
248 (defvar org-icalendar-after-save-hook nil
249 "Hook run after an iCalendar file has been saved.
250 This hook is run with the name of the file as argument. A good
251 way to use this is to tell a desktop calendar application to
252 re-read the iCalendar file.")
258 (org-export-define-derived-backend icalendar ascii
259 :translate-alist
((clock . ignore
)
260 (headline . org-icalendar-entry
)
261 (inlinetask . ignore
)
264 (template . org-icalendar-template
))
267 "ICALENDAR_EXCLUDE_TAGS" nil org-icalendar-exclude-tags split
)
268 (:with-timestamps nil
"<" org-icalendar-with-timestamps
)
269 (:with-vtodo nil nil org-icalendar-include-todo
)
270 ;; The following property will be non-nil when export has been
271 ;; started from org-agenda-mode. In this case, any entry without
272 ;; a non-nil "ICALENDAR_MARK" property will be ignored.
273 (:icalendar-agenda-view nil nil nil
))
275 ((:filter-headline . org-icalendar-clear-blank-lines
))
277 (?c
"Export to iCalendar"
278 ((?f
"Current file" org-icalendar-export-to-ics
)
279 (?a
"All agenda files"
280 (lambda (a s v b
) (org-icalendar-export-agenda-files a
)))
281 (?c
"Combine all agenda files"
282 (lambda (a s v b
) (org-icalendar-combine-agenda-files a
))))))
286 ;;; Internal Functions
288 (defun org-icalendar-create-uid (file &optional bell h-markers
)
289 "Set ID property on headlines missing it in FILE.
290 When optional argument BELL is non-nil, inform the user with
291 a message if the file was modified. With optional argument
292 H-MARKERS non-nil, it is a list of markers for the headlines
293 which will be updated."
294 (let (modified-flag pt
)
295 (when h-markers
(setq pt
(goto-char (car h-markers
))))
298 (let ((entry (org-element-at-point)))
299 (unless (or (< (point) pt
) (org-element-property :ID entry
))
301 (setq modified-flag t
)
303 (setq org-map-continue-from
304 (if h-markers
(pop h-markers
) (point-max)))))
306 (when (and bell modified-flag
)
307 (message "ID properties created in file \"%s\"" file
)
310 (defun org-icalendar-blocked-headline-p (headline info
)
311 "Non-nil when HEADLINE is considered to be blocked.
313 INFO is a plist used as a communication channel.
315 a headline is blocked when either:
317 - It has children which are not all in a completed state.
319 - It has a parent with the property :ORDERED:, and there are
320 siblings prior to it with incomplete status.
322 - Its parent is blocked because it has siblings that should be
323 done first or is a child of a blocked grandparent entry."
325 ;; Check if any child is not done.
326 (org-element-map headline
'headline
327 (lambda (hl) (eq (org-element-property :todo-type hl
) 'todo
))
329 ;; Check :ORDERED: node property.
331 (let ((current headline
))
332 (mapc (lambda (parent)
334 ((not (org-element-property :todo-keyword parent
))
335 (throw 'blockedp nil
))
336 ((org-not-nil (org-element-property :ORDERED parent
))
337 (let ((sibling current
))
338 (while (setq sibling
(org-export-get-previous-element
340 (when (eq (org-element-property :todo-type sibling
) 'todo
)
341 (throw 'blockedp t
)))))
342 (t (setq current parent
))))
343 (org-export-get-genealogy headline
))
346 (defun org-icalendar-use-UTC-date-time-p ()
347 "Non-nil when `org-icalendar-date-time-format' requires UTC time."
348 (char-equal (elt org-icalendar-date-time-format
349 (1- (length org-icalendar-date-time-format
))) ?Z
))
351 (defvar org-agenda-default-appointment-duration
) ; From org-agenda.el.
352 (defun org-icalendar-convert-timestamp (timestamp keyword
&optional end utc
)
353 "Convert TIMESTAMP to iCalendar format.
355 TIMESTAMP is a timestamp object. KEYWORD is added in front of
356 it, in order to make a complete line (e.g. \"DTSTART\").
358 When optional argument END is non-nil, use end of time range.
359 Also increase the hour by two (if time string contains a time),
360 or the day by one (if it does not contain a time) when no
361 explicit ending time is specified.
363 When optional argument UTC is non-nil, time will be expressed in
364 Universal Time, ignoring `org-icalendar-date-time-format'.
365 This is mandatory for \"DTSTAMP\" property."
366 (let* ((year-start (org-element-property :year-start timestamp
))
367 (year-end (org-element-property :year-end timestamp
))
368 (month-start (org-element-property :month-start timestamp
))
369 (month-end (org-element-property :month-end timestamp
))
370 (day-start (org-element-property :day-start timestamp
))
371 (day-end (org-element-property :day-end timestamp
))
372 (hour-start (org-element-property :hour-start timestamp
))
373 (hour-end (org-element-property :hour-end timestamp
))
374 (minute-start (org-element-property :minute-start timestamp
))
375 (minute-end (org-element-property :minute-end timestamp
))
376 (with-time-p minute-start
)
378 (equal (list year-start month-start day-start hour-start minute-start
)
379 (list year-end month-end day-end hour-end minute-end
)))
380 (mi (cond ((not with-time-p
) 0)
381 ((not end
) minute-start
)
382 ((and org-agenda-default-appointment-duration equal-bounds-p
)
383 (+ minute-end org-agenda-default-appointment-duration
))
385 (h (cond ((not with-time-p
) 0)
386 ((not end
) hour-start
)
387 ((or (not equal-bounds-p
)
388 org-agenda-default-appointment-duration
)
391 (d (cond ((not end
) day-start
)
392 ((not with-time-p
) (1+ day-end
))
394 (m (if end month-end month-start
))
395 (y (if end year-end year-start
)))
399 (cond (utc ":%Y%m%dT%H%M%SZ")
400 ((not with-time-p
) ";VALUE=DATE:%Y%m%d")
401 (t (replace-regexp-in-string "%Z"
402 org-icalendar-timezone
403 org-icalendar-date-time-format
405 ;; Convert timestamp into internal time in order to use
406 ;; `format-time-string' and fix any mistake (i.e. MI >= 60).
407 (encode-time 0 mi h d m y
)
408 (or utc
(and with-time-p
(org-icalendar-use-UTC-date-time-p)))))))
410 (defun org-icalendar-get-categories (entry info
)
411 "Return categories according to `org-icalendar-categories'.
412 ENTRY is a headline or an inlinetask element. INFO is a plist
413 used as a communication channel."
421 (push (org-export-get-category entry info
) categories
))
423 (let ((todo (org-element-property :todo-keyword entry
)))
424 (and todo
(push todo categories
))))
427 (append (nreverse (org-export-get-tags entry info
))
431 (append (nreverse (org-export-get-tags entry info nil t
))
433 org-icalendar-categories
)
434 ;; Return list of categories, following specified order.
435 (nreverse categories
))) ","))
437 (defun org-icalendar-transcode-diary-sexp (sexp uid summary
)
438 "Transcode a diary sexp into iCalendar format.
439 SEXP is the diary sexp being transcoded, as a string. UID is the
440 unique identifier for the entry. SUMMARY defines a short summary
441 or subject for the event."
442 (when (require 'icalendar nil t
)
443 (org-element-normalize-string
445 (let ((sexp (if (not (string-match "\\`<%%" sexp
)) sexp
446 (concat (substring sexp
1 -
1) " " summary
))))
447 (put-text-property 0 1 'uid uid sexp
)
449 (org-diary-to-ical-string (current-buffer))))))
451 (defun org-icalendar-cleanup-string (s)
452 "Cleanup string S according to RFC 5545."
454 ;; Protect "\", "," and ";" characters. and replace newline
455 ;; characters with literal \n.
456 (replace-regexp-in-string
458 (replace-regexp-in-string "[\\,;]" "\\\&" s
)
461 (defun org-icalendar-fold-string (s)
462 "Fold string S according to RFC 5545."
463 (org-element-normalize-string
466 ;; Limit each line to a maximum of 75 characters. If it is
467 ;; longer, fold it by using "\n " as a continuation marker.
468 (let ((len (length line
)))
470 (let ((folded-line (substring line
0 75))
473 ;; Since continuation marker takes up one character on the
474 ;; line, real contents must be split at 74 chars.
475 (while (< (setq chunk-end
(+ chunk-start
74)) len
)
477 (concat folded-line
"\n "
478 (substring line chunk-start chunk-end
))
479 chunk-start chunk-end
))
480 (concat folded-line
"\n " (substring line chunk-start
))))))
481 (org-split-string s
"\n") "\n")))
487 (defun org-icalendar-clear-blank-lines (headline back-end info
)
488 "Remove trailing blank lines in HEADLINE export.
489 HEADLINE is a string representing a transcoded headline.
490 BACK-END and INFO are ignored."
491 (replace-regexp-in-string "^\\(?:[ \t]*\n\\)*" "" headline
))
495 ;;; Transcode Functions
497 ;;;; Headline and Inlinetasks
499 ;; The main function is `org-icalendar-entry', which extracts
500 ;; information from a headline or an inlinetask (summary,
501 ;; description...) and then delegates code generation to
502 ;; `org-icalendar--vtodo' and `org-icalendar--vevent', depending
503 ;; on the component needed.
505 ;; Obviously, `org-icalendar--valarm' handles alarms, which can
506 ;; happen within a VTODO component.
508 (defun org-icalendar-entry (entry contents info
)
509 "Transcode ENTRY element into iCalendar format.
511 ENTRY is either a headline or an inlinetask. CONTENTS is
512 ignored. INFO is a plist used as a communication channel.
514 This function is called on every headline, the section below
515 it (minus inlinetasks) being its contents. It tries to create
516 VEVENT and VTODO components out of scheduled date, deadline date,
517 plain timestamps, diary sexps. It also calls itself on every
518 inlinetask within the section."
519 (unless (org-element-property :footnote-section-p entry
)
520 (let* ((type (org-element-type entry
))
521 ;; Determine contents really associated to the entry. For
522 ;; a headline, limit them to section, if any. For an
523 ;; inlinetask, this is every element within the task.
525 (if (eq type
'inlinetask
)
526 (cons 'org-data
(cons nil
(org-element-contents entry
)))
527 (let ((first (car (org-element-contents entry
))))
528 (and (eq (org-element-type first
) 'section
)
530 (cons nil
(org-element-contents first
))))))))
532 (unless (and (plist-get info
:icalendar-agenda-view
)
533 (not (org-element-property :ICALENDAR-MARK entry
)))
534 (let ((todo-type (org-element-property :todo-type entry
))
535 (uid (or (org-element-property :ID entry
) (org-id-new)))
536 (summary (org-icalendar-cleanup-string
537 (or (org-element-property :SUMMARY entry
)
539 (org-element-property :title entry
) info
))))
540 (loc (org-icalendar-cleanup-string
541 (org-element-property :LOCATION entry
)))
542 ;; Build description of the entry from associated
543 ;; section (headline) or contents (inlinetask).
545 (org-icalendar-cleanup-string
546 (or (org-element-property :DESCRIPTION entry
)
547 (let ((contents (org-export-data inside info
)))
549 ((not (org-string-nw-p contents
)) nil
)
550 ((wholenump org-icalendar-include-body
)
551 (let ((contents (org-trim contents
)))
553 contents
0 (min (length contents
)
554 org-icalendar-include-body
))))
555 (org-icalendar-include-body (org-trim contents
)))))))
556 (cat (org-icalendar-get-categories entry info
)))
558 ;; Events: Delegate to `org-icalendar--vevent' to
559 ;; generate "VEVENT" component from scheduled, deadline,
560 ;; or any timestamp in the entry.
561 (let ((deadline (org-element-property :deadline entry
)))
563 (memq (if todo-type
'event-if-todo
'event-if-not-todo
)
564 org-icalendar-use-deadline
)
565 (org-icalendar--vevent
566 entry deadline
(concat "DL-" uid
)
567 (concat "DL: " summary
) loc desc cat
)))
568 (let ((scheduled (org-element-property :scheduled entry
)))
570 (memq (if todo-type
'event-if-todo
'event-if-not-todo
)
571 org-icalendar-use-scheduled
)
572 (org-icalendar--vevent
573 entry scheduled
(concat "SC-" uid
)
574 (concat "S: " summary
) loc desc cat
)))
575 ;; When collecting plain timestamps from a headline and
576 ;; its title, skip inlinetasks since collection will
577 ;; happen once ENTRY is one of them.
581 (org-element-map (cons (org-element-property :title entry
)
582 (org-element-contents inside
))
585 (let ((uid (format "TS%d-%s" (incf counter
) uid
)))
586 (org-icalendar--vevent entry ts uid summary loc desc cat
)))
587 info nil
(and (eq type
'headline
) 'inlinetask
))
589 ;; Task: First check if it is appropriate to export it.
590 ;; If so, call `org-icalendar--vtodo' to transcode it
591 ;; into a "VTODO" component.
593 (case (plist-get info
:with-vtodo
)
596 (and (eq type
'headline
)
597 (not (org-icalendar-blocked-headline-p
599 ('t
(eq todo-type
'todo
))))
600 (org-icalendar--vtodo entry uid summary loc desc cat
))
601 ;; Diary-sexp: Collect every diary-sexp element within
602 ;; ENTRY and its title, and transcode them. If ENTRY is
603 ;; a headline, skip inlinetasks: they will be handled
605 (when org-icalendar-include-sexps
609 (cons (org-element-property :title entry
)
610 (org-element-contents inside
))
613 (org-icalendar-transcode-diary-sexp
614 (org-element-property :value sexp
)
615 (format "DS%d-%s" (incf counter
) uid
)
617 info nil
(and (eq type
'headline
) 'inlinetask
))
619 ;; If ENTRY is a headline, call current function on every
620 ;; inlinetask within it. In agenda export, this is independent
621 ;; from the mark (or lack thereof) on the entry.
622 (when (eq type
'headline
)
624 (org-element-map inside
'inlinetask
625 (lambda (task) (org-icalendar-entry task nil info
))
627 ;; Don't forget components from inner entries.
630 (defun org-icalendar--vevent
631 (entry timestamp uid summary location description categories
)
632 "Create a VEVENT component.
634 ENTRY is either a headline or an inlinetask element. TIMESTAMP
635 is a timestamp object defining the date-time of the event. UID
636 is the unique identifier for the event. SUMMARY defines a short
637 summary or subject for the event. LOCATION defines the intended
638 venue for the event. DESCRIPTION provides the complete
639 description of the event. CATEGORIES defines the categories the
642 Return VEVENT component as a string."
643 (org-icalendar-fold-string
644 (if (eq (org-element-property :type timestamp
) 'diary
)
645 (org-icalendar-transcode-diary-sexp
646 (org-element-property :raw-value timestamp
) uid summary
)
647 (concat "BEGIN:VEVENT\n"
648 (org-icalendar-convert-timestamp timestamp
"DTSTAMP" nil t
) "\n"
650 (org-icalendar-convert-timestamp timestamp
"DTSTART") "\n"
651 (org-icalendar-convert-timestamp timestamp
"DTEND" t
) "\n"
653 (when (org-element-property :repeater-type timestamp
)
654 (format "RRULE:FREQ=%s;INTERVAL=%d\n"
655 (case (org-element-property :repeater-unit timestamp
)
656 (hour "HOURLY") (day "DAILY") (week "WEEKLY")
657 (month "MONTHLY") (year "YEARLY"))
658 (org-element-property :repeater-value timestamp
)))
659 "SUMMARY:" summary
"\n"
660 (and (org-string-nw-p location
) (format "LOCATION:%s\n" location
))
661 (and (org-string-nw-p description
)
662 (format "DESCRIPTION:%s\n" description
))
663 "CATEGORIES:" categories
"\n"
665 (org-icalendar--valarm entry timestamp summary
)
668 (defun org-icalendar--vtodo
669 (entry uid summary location description categories
)
670 "Create a VTODO component.
672 ENTRY is either a headline or an inlinetask element. UID is the
673 unique identifier for the task. SUMMARY defines a short summary
674 or subject for the task. LOCATION defines the intended venue for
675 the task. DESCRIPTION provides the complete description of the
676 task. CATEGORIES defines the categories the task belongs to.
678 Return VTODO component as a string."
679 (let ((start (or (and (memq 'todo-start org-icalendar-use-scheduled
)
680 (org-element-property :scheduled entry
))
681 ;; If we can't use a scheduled time for some
682 ;; reason, start task now.
683 (let ((now (decode-time (current-time))))
686 :minute-start
(nth 1 now
)
687 :hour-start
(nth 2 now
)
688 :day-start
(nth 3 now
)
689 :month-start
(nth 4 now
)
690 :year-start
(nth 5 now
)))))))
691 (org-icalendar-fold-string
692 (concat "BEGIN:VTODO\n"
694 (org-icalendar-convert-timestamp start
"DTSTAMP" nil t
) "\n"
695 (org-icalendar-convert-timestamp start
"DTSTART") "\n"
696 (and (memq 'todo-due org-icalendar-use-deadline
)
697 (org-element-property :deadline entry
)
698 (concat (org-icalendar-convert-timestamp
699 (org-element-property :deadline entry
) "DUE")
701 "SUMMARY:" summary
"\n"
702 (and (org-string-nw-p location
) (format "LOCATION:%s\n" location
))
703 (and (org-string-nw-p description
)
704 (format "DESCRIPTION:%s\n" description
))
705 "CATEGORIES:" categories
"\n"
707 (format "PRIORITY:%d\n"
708 (let ((pri (or (org-element-property :priority entry
)
709 org-default-priority
)))
710 (floor (- 9 (* 8.
(/ (float (- org-lowest-priority pri
))
711 (- org-lowest-priority
712 org-highest-priority
)))))))
713 (format "STATUS:%s\n"
714 (if (eq (org-element-property :todo-type entry
) 'todo
)
719 (defun org-icalendar--valarm (entry timestamp summary
)
720 "Create a VALARM component.
722 ENTRY is the calendar entry triggering the alarm. TIMESTAMP is
723 the start date-time of the entry. SUMMARY defines a short
724 summary or subject for the task.
726 Return VALARM component as a string, or nil if it isn't allowed."
727 ;; Create a VALARM entry if the entry is timed. This is not very
729 ;; (a) only one alarm per entry is defined,
730 ;; (b) only minutes are allowed for the trigger period ahead of the
732 ;; (c) only a DISPLAY action is defined. [ESF]
735 (org-element-property :APPT_WARNTIME entry
)))
736 (if warntime
(string-to-number warntime
) 0))))
737 (and (or (> alarm-time
0) (> org-icalendar-alarm-time
0))
738 (org-element-property :hour-start timestamp
)
739 (format "BEGIN:VALARM
745 (if (zerop alarm-time
) org-icalendar-alarm-time alarm-time
)))))
750 (defun org-icalendar-template (contents info
)
751 "Return complete document string after iCalendar conversion.
752 CONTENTS is the transcoded contents string. INFO is a plist used
753 as a communication channel."
754 (org-icalendar--vcalendar
756 (if (not (plist-get info
:input-file
)) (buffer-name (buffer-base-buffer))
757 (file-name-nondirectory
758 (file-name-sans-extension (plist-get info
:input-file
))))
760 (if (not (plist-get info
:with-author
)) ""
761 (org-export-data (plist-get info
:author
) info
))
763 (if (org-string-nw-p org-icalendar-timezone
) org-icalendar-timezone
764 (cadr (current-time-zone)))
766 (org-export-data (plist-get info
:title
) info
)
769 (defun org-icalendar--vcalendar (name owner tz description contents
)
770 "Create a VCALENDAR component.
771 NAME, OWNER, TZ, DESCRIPTION and CONTENTS are all strings giving,
772 respectively, the name of the calendar, its owner, the timezone
773 used, a short description and the other components included."
774 (concat (format "BEGIN:VCALENDAR
777 PRODID:-//%s//Emacs with Org mode//EN
780 CALSCALE:GREGORIAN\n"
781 (org-icalendar-cleanup-string name
)
782 (org-icalendar-cleanup-string owner
)
783 (org-icalendar-cleanup-string tz
)
784 (org-icalendar-cleanup-string description
))
790 ;;; Interactive Functions
793 (defun org-icalendar-export-to-ics
794 (&optional async subtreep visible-only body-only
)
795 "Export current buffer to an iCalendar file.
797 If narrowing is active in the current buffer, only export its
800 If a region is active, export that region.
802 A non-nil optional argument ASYNC means the process should happen
803 asynchronously. The resulting file should be accessible through
804 the `org-export-stack' interface.
806 When optional argument SUBTREEP is non-nil, export the sub-tree
807 at point, extracting information from the headline properties
810 When optional argument VISIBLE-ONLY is non-nil, don't export
811 contents of hidden elements.
813 When optional argument BODY-ONLY is non-nil, only write code
814 between \"BEGIN:VCALENDAR\" and \"END:VCALENDAR\".
816 Return ICS file name."
818 (let ((file (buffer-file-name (buffer-base-buffer))))
819 (when (and file org-icalendar-store-UID
)
820 (org-icalendar-create-uid file
'warn-user
)))
821 ;; Export part. Since this back-end is backed up by `e-ascii',
822 ;; ensure links will not be collected at the end of sections.
823 (let ((outfile (org-export-output-file-name ".ics" subtreep
)))
825 (org-export-async-start
827 (org-export-add-to-stack f
'icalendar
)
828 (run-hook-with-args 'org-icalendar-after-save-hook f
))
829 `(let ((org-ascii-links-to-notes nil
))
832 'icalendar
,outfile
,subtreep
,visible-only
,body-only
833 '(:ascii-charset utf-8
)))))
834 (let ((org-ascii-links-to-notes nil
))
835 (org-export-to-file 'icalendar outfile subtreep visible-only body-only
836 '(:ascii-charset utf-8
)))
837 (run-hook-with-args 'org-icalendar-after-save-hook outfile
)
841 (defun org-icalendar-export-agenda-files (&optional async
)
842 "Export all agenda files to iCalendar files.
843 When optional argument ASYNC is non-nil, export happens in an
847 ;; Asynchronous export is not interactive, so we will not call
848 ;; `org-check-agenda-file'. Instead we remove any non-existent
849 ;; agenda file from the list.
850 (let ((files (org-remove-if-not 'file-exists-p
(org-agenda-files t
))))
851 (org-export-async-start
853 (mapc (lambda (f) (org-export-add-to-stack f
'icalendar
))
857 (with-current-buffer (org-get-agenda-file-buffer file
)
858 (push (expand-file-name (org-icalendar-export-to-ics))
862 (let ((files (org-agenda-files t
)))
863 (org-agenda-prepare-buffers files
)
867 (org-check-agenda-file file
)
868 (with-current-buffer (org-get-agenda-file-buffer file
)
869 (org-icalendar-export-to-ics))))
871 (org-release-buffers org-agenda-new-buffers
)))))
874 (defun org-icalendar-combine-agenda-files (&optional async
)
875 "Combine all agenda files into a single iCalendar file.
877 A non-nil optional argument ASYNC means the process should happen
878 asynchronously. The resulting file should be accessible through
879 the `org-export-stack' interface.
881 The file is stored under the name chosen in
882 `org-icalendar-combined-agenda-file'."
885 (let ((files (org-remove-if-not 'file-exists-p
(org-agenda-files t
))))
886 (org-export-async-start
888 (org-export-add-to-stack
889 (expand-file-name org-icalendar-combined-agenda-file
)
891 `(apply 'org-icalendar--combine-files nil
',files
)))
892 (apply 'org-icalendar--combine-files nil
(org-agenda-files t
))))
894 (defun org-icalendar-export-current-agenda (file)
895 "Export current agenda view to an iCalendar FILE.
896 This function assumes major mode for current buffer is
898 (let ((org-icalendar-combined-agenda-file file
)
900 ;; Collect the markers pointing to entries in the current
904 (goto-char (point-min))
906 (let ((m (or (org-get-at-bol 'org-hd-marker
)
907 (org-get-at-bol 'org-marker
))))
908 (and m
(push m markers
)))
909 (beginning-of-line 2)))
910 (nreverse markers
))))
911 (apply 'org-icalendar--combine-files
912 ;; Build restriction alist.
914 ;; Sort markers in each association within RESTRICTION.
915 (mapcar (lambda (x) (setcdr x
(sort (copy-sequence (cdr x
)) '<)) x
)
916 (dolist (m marker-list restriction
)
917 (let* ((pos (marker-position m
))
918 (file (buffer-file-name
919 (org-base-buffer (marker-buffer m
))))
920 (file-markers (assoc file restriction
)))
921 ;; Add POS in FILE association if one exists
922 ;; or create a new association for FILE.
923 (if file-markers
(push pos
(cdr file-markers
))
924 (push (list file pos
) restriction
))))))
925 (org-agenda-files nil
'ifmode
))))
927 (defun org-icalendar--combine-files (restriction &rest files
)
928 "Combine entries from multiple files into an iCalendar file.
929 RESTRICTION, when non-nil, is an alist where key is a file name
930 and value a list of buffer positions pointing to entries that
931 should appear in the calendar. It only makes sense if the
932 function was called from an agenda buffer. FILES is a list of
933 files to build the calendar from."
934 (org-agenda-prepare-buffers files
)
937 (with-temp-file org-icalendar-combined-agenda-file
939 (org-icalendar--vcalendar
941 org-icalendar-combined-name
945 (if (org-string-nw-p org-icalendar-timezone
)
946 org-icalendar-timezone
947 (cadr (current-time-zone)))
949 org-icalendar-combined-description
956 (org-check-agenda-file file
)
957 (with-current-buffer (org-get-agenda-file-buffer file
)
958 (let ((marks (cdr (assoc (expand-file-name file
) restriction
))))
959 ;; Create ID if necessary.
960 (when org-icalendar-store-UID
961 (org-icalendar-create-uid file t marks
))
962 (unless (and restriction
(not marks
))
963 ;; Add a hook adding :ICALENDAR_MARK: property
964 ;; to each entry appearing in agenda view.
965 ;; Use `apply-partially' because the function
966 ;; still has to accept one argument.
967 (let ((org-export-before-processing-hook
968 (cons (apply-partially
969 (lambda (m-list dummy
)
972 m
"ICALENDAR_MARK" "t"))
975 org-export-before-processing-hook
)))
978 (list :ascii-charset
'utf-8
979 :icalendar-agenda-view restriction
))))))))
981 ;; BBDB anniversaries.
982 (when (and org-icalendar-include-bbdb-anniversaries
983 (require 'org-bbdb nil t
))
985 (org-bbdb-anniv-export-ical)
986 (buffer-string)))))))
987 (run-hook-with-args 'org-icalendar-after-save-hook
988 org-icalendar-combined-agenda-file
))
989 (org-release-buffers org-agenda-new-buffers
)))
992 (provide 'ox-icalendar
)
995 ;; generated-autoload-file: "org-loaddefs.el"
998 ;;; ox-icalendar.el ends here