1 ;;; ox-icalendar.el --- iCalendar Back-End for Org Export Engine -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2004-2018 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 <https://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.
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 specifying different exclude tags from other
80 back-ends. It can also be set with the ICALENDAR_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-date-time-format nil nil org-icalendar-date-time-format
)
265 (:icalendar-include-bbdb-anniversaries nil nil org-icalendar-include-bbdb-anniversaries
)
266 (:icalendar-include-body nil nil org-icalendar-include-body
)
267 (:icalendar-include-sexps nil nil org-icalendar-include-sexps
)
268 (:icalendar-include-todo nil nil org-icalendar-include-todo
)
269 (:icalendar-store-UID nil nil org-icalendar-store-UID
)
270 (:icalendar-timezone nil nil org-icalendar-timezone
)
271 (:icalendar-use-deadline nil nil org-icalendar-use-deadline
)
272 (:icalendar-use-scheduled nil nil org-icalendar-use-scheduled
))
274 '((:filter-headline . org-icalendar-clear-blank-lines
))
276 '(?c
"Export to iCalendar"
277 ((?f
"Current file" org-icalendar-export-to-ics
)
278 (?a
"All agenda files"
279 (lambda (a s v b
) (org-icalendar-export-agenda-files a
)))
280 (?c
"Combine all agenda files"
281 (lambda (a s v b
) (org-icalendar-combine-agenda-files a
))))))
285 ;;; Internal Functions
287 (defun org-icalendar-create-uid (file &optional bell
)
288 "Set ID property on headlines missing it in FILE.
289 When optional argument BELL is non-nil, inform the user with
290 a message if the file was modified."
294 (let ((entry (org-element-at-point)))
295 (unless (org-element-property :ID entry
)
297 (setq modified-flag t
)
300 (when (and bell modified-flag
)
301 (message "ID properties created in file \"%s\"" file
)
304 (defun org-icalendar-blocked-headline-p (headline info
)
305 "Non-nil when HEADLINE is considered to be blocked.
307 INFO is a plist used as a communication channel.
309 A headline is blocked when either
311 - it has children which are not all in a completed state;
313 - it has a parent with the property :ORDERED:, and there are
314 siblings prior to it with incomplete status;
316 - its parent is blocked because it has siblings that should be
317 done first or is a child of a blocked grandparent entry."
319 ;; Check if any child is not done.
320 (org-element-map headline
'headline
321 (lambda (hl) (eq (org-element-property :todo-type hl
) 'todo
))
323 ;; Check :ORDERED: node property.
325 (let ((current headline
))
326 (dolist (parent (org-element-lineage headline
))
328 ((not (org-element-property :todo-keyword parent
))
329 (throw 'blockedp nil
))
330 ((org-not-nil (org-element-property :ORDERED parent
))
331 (let ((sibling current
))
332 (while (setq sibling
(org-export-get-previous-element
334 (when (eq (org-element-property :todo-type sibling
) 'todo
)
335 (throw 'blockedp t
)))))
336 (t (setq current parent
))))))))
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 tz
)
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 TZ is non-nil, timezone data time will be
356 added to the timestamp. It can be the string \"UTC\", to use UTC
357 time, or a string in the IANA TZ database
358 format (e.g. \"Europe/London\"). In either case, the value of
359 `org-icalendar-date-time-format' will be ignored."
360 (let* ((year-start (org-element-property :year-start timestamp
))
361 (year-end (org-element-property :year-end timestamp
))
362 (month-start (org-element-property :month-start timestamp
))
363 (month-end (org-element-property :month-end timestamp
))
364 (day-start (org-element-property :day-start timestamp
))
365 (day-end (org-element-property :day-end timestamp
))
366 (hour-start (org-element-property :hour-start timestamp
))
367 (hour-end (org-element-property :hour-end timestamp
))
368 (minute-start (org-element-property :minute-start timestamp
))
369 (minute-end (org-element-property :minute-end timestamp
))
370 (with-time-p minute-start
)
372 (equal (list year-start month-start day-start hour-start minute-start
)
373 (list year-end month-end day-end hour-end minute-end
)))
374 (mi (cond ((not with-time-p
) 0)
375 ((not end
) minute-start
)
376 ((and org-agenda-default-appointment-duration equal-bounds-p
)
377 (+ minute-end org-agenda-default-appointment-duration
))
379 (h (cond ((not with-time-p
) 0)
380 ((not end
) hour-start
)
381 ((or (not equal-bounds-p
)
382 org-agenda-default-appointment-duration
)
385 (d (cond ((not end
) day-start
)
386 ((not with-time-p
) (1+ day-end
))
388 (m (if end month-end month-start
))
389 (y (if end year-end year-start
)))
393 (cond ((string-equal tz
"UTC") ":%Y%m%dT%H%M%SZ")
394 ((not with-time-p
) ";VALUE=DATE:%Y%m%d")
395 ((stringp tz
) (concat ";TZID=" tz
":%Y%m%dT%H%M%S"))
396 (t (replace-regexp-in-string "%Z"
397 org-icalendar-timezone
398 org-icalendar-date-time-format
400 ;; Convert timestamp into internal time in order to use
401 ;; `format-time-string' and fix any mistake (i.e. MI >= 60).
402 (encode-time 0 mi h d m y
)
403 (and (or (string-equal tz
"UTC")
406 (org-icalendar-use-UTC-date-time-p)))
409 (defun org-icalendar-dtstamp ()
410 "Return DTSTAMP property, as a string."
411 (format-time-string "DTSTAMP:%Y%m%dT%H%M%SZ" nil t
))
413 (defun org-icalendar-get-categories (entry info
)
414 "Return categories according to `org-icalendar-categories'.
415 ENTRY is a headline or an inlinetask element. INFO is a plist
416 used as a communication channel."
421 (dolist (type org-icalendar-categories
(nreverse categories
))
424 (push (org-export-get-category entry info
) categories
))
426 (let ((todo (org-element-property :todo-keyword entry
)))
427 (and todo
(push todo categories
))))
430 (append (nreverse (org-export-get-tags entry info
))
434 (append (nreverse (org-export-get-tags entry info nil t
))
438 (defun org-icalendar-transcode-diary-sexp (sexp uid summary
)
439 "Transcode a diary sexp into iCalendar format.
440 SEXP is the diary sexp being transcoded, as a string. UID is the
441 unique identifier for the entry. SUMMARY defines a short summary
442 or subject for the event."
443 (when (require 'icalendar nil t
)
444 (org-element-normalize-string
446 (let ((sexp (if (not (string-match "\\`<%%" sexp
)) sexp
447 (concat (substring sexp
1 -
1) " " summary
))))
448 (put-text-property 0 1 'uid uid sexp
)
450 (org-diary-to-ical-string (current-buffer))))))
452 (defun org-icalendar-cleanup-string (s)
453 "Cleanup string S according to RFC 5545."
455 ;; Protect "\", "," and ";" characters. and replace newline
456 ;; characters with literal \n.
457 (replace-regexp-in-string
459 (replace-regexp-in-string "[\\,;]" "\\\\\\&" s
)
462 (defun org-icalendar-fold-string (s)
463 "Fold string S according to RFC 5545."
464 (org-element-normalize-string
467 ;; Limit each line to a maximum of 75 characters. If it is
468 ;; longer, fold it by using "\r\n " as a continuation marker.
469 (let ((len (length line
)))
471 (let ((folded-line (substring line
0 75))
474 ;; Since continuation marker takes up one character on the
475 ;; line, real contents must be split at 74 chars.
476 (while (< (setq chunk-end
(+ chunk-start
74)) len
)
478 (concat folded-line
"\r\n "
479 (substring line chunk-start chunk-end
))
480 chunk-start chunk-end
))
481 (concat folded-line
"\r\n " (substring line chunk-start
))))))
482 (org-split-string s
"\n") "\r\n")))
488 (defun org-icalendar-clear-blank-lines (headline _back-end _info
)
489 "Remove blank lines in HEADLINE export.
490 HEADLINE is a string representing a transcoded headline.
491 BACK-END and INFO are ignored."
492 (replace-regexp-in-string "^\\(?:[ \t]*\n\\)+" "" headline
))
496 ;;; Transcode Functions
498 ;;;; Headline and Inlinetasks
500 ;; The main function is `org-icalendar-entry', which extracts
501 ;; information from a headline or an inlinetask (summary,
502 ;; description...) and then delegates code generation to
503 ;; `org-icalendar--vtodo' and `org-icalendar--vevent', depending
504 ;; on the component needed.
506 ;; Obviously, `org-icalendar--valarm' handles alarms, which can
507 ;; happen within a VTODO component.
509 (defun org-icalendar-entry (entry contents info
)
510 "Transcode ENTRY element into iCalendar format.
512 ENTRY is either a headline or an inlinetask. CONTENTS is
513 ignored. INFO is a plist used as a communication channel.
515 This function is called on every headline, the section below
516 it (minus inlinetasks) being its contents. It tries to create
517 VEVENT and VTODO components out of scheduled date, deadline date,
518 plain timestamps, diary sexps. It also calls itself on every
519 inlinetask within the section."
520 (unless (org-element-property :footnote-section-p entry
)
521 (let* ((type (org-element-type entry
))
522 ;; Determine contents really associated to the entry. For
523 ;; a headline, limit them to section, if any. For an
524 ;; inlinetask, this is every element within the task.
526 (if (eq type
'inlinetask
)
527 (cons 'org-data
(cons nil
(org-element-contents entry
)))
528 (let ((first (car (org-element-contents entry
))))
529 (and (eq (org-element-type first
) 'section
)
531 (cons nil
(org-element-contents first
))))))))
533 (let ((todo-type (org-element-property :todo-type entry
))
534 (uid (or (org-element-property :ID entry
) (org-id-new)))
535 (summary (org-icalendar-cleanup-string
536 (or (org-element-property :SUMMARY entry
)
538 (org-element-property :title entry
) info
))))
539 (loc (org-icalendar-cleanup-string
540 (org-export-get-node-property
542 (org-property-inherit-p "LOCATION"))))
543 ;; Build description of the entry from associated section
544 ;; (headline) or contents (inlinetask).
546 (org-icalendar-cleanup-string
547 (or (org-element-property :DESCRIPTION entry
)
548 (let ((contents (org-export-data inside info
)))
550 ((not (org-string-nw-p contents
)) nil
)
551 ((wholenump org-icalendar-include-body
)
552 (let ((contents (org-trim contents
)))
554 contents
0 (min (length contents
)
555 org-icalendar-include-body
))))
556 (org-icalendar-include-body (org-trim contents
)))))))
557 (cat (org-icalendar-get-categories entry info
))
558 (tz (org-export-get-node-property
560 (org-property-inherit-p "TIMEZONE"))))
562 ;; Events: Delegate to `org-icalendar--vevent' to generate
563 ;; "VEVENT" component from scheduled, deadline, or any
564 ;; timestamp in the entry.
565 (let ((deadline (org-element-property :deadline entry
)))
567 (memq (if todo-type
'event-if-todo
'event-if-not-todo
)
568 org-icalendar-use-deadline
)
569 (org-icalendar--vevent
570 entry deadline
(concat "DL-" uid
)
571 (concat "DL: " summary
) loc desc cat tz
)))
572 (let ((scheduled (org-element-property :scheduled entry
)))
574 (memq (if todo-type
'event-if-todo
'event-if-not-todo
)
575 org-icalendar-use-scheduled
)
576 (org-icalendar--vevent
577 entry scheduled
(concat "SC-" uid
)
578 (concat "S: " summary
) loc desc cat tz
)))
579 ;; When collecting plain timestamps from a headline and its
580 ;; title, skip inlinetasks since collection will happen once
581 ;; ENTRY is one of them.
585 (org-element-map (cons (org-element-property :title entry
)
586 (org-element-contents inside
))
589 (when (let ((type (org-element-property :type ts
)))
590 (cl-case (plist-get info
:with-timestamps
)
591 (active (memq type
'(active active-range
)))
592 (inactive (memq type
'(inactive inactive-range
)))
594 (let ((uid (format "TS%d-%s" (cl-incf counter
) uid
)))
595 (org-icalendar--vevent
596 entry ts uid summary loc desc cat tz
))))
597 info nil
(and (eq type
'headline
) 'inlinetask
))
599 ;; Task: First check if it is appropriate to export it. If
600 ;; so, call `org-icalendar--vtodo' to transcode it into
601 ;; a "VTODO" component.
603 (cl-case (plist-get info
:icalendar-include-todo
)
606 (and (eq type
'headline
)
607 (not (org-icalendar-blocked-headline-p
609 ((t) (eq todo-type
'todo
))))
610 (org-icalendar--vtodo entry uid summary loc desc cat tz
))
611 ;; Diary-sexp: Collect every diary-sexp element within ENTRY
612 ;; and its title, and transcode them. If ENTRY is
613 ;; a headline, skip inlinetasks: they will be handled
615 (when org-icalendar-include-sexps
617 (mapconcat #'identity
619 (cons (org-element-property :title entry
)
620 (org-element-contents inside
))
623 (org-icalendar-transcode-diary-sexp
624 (org-element-property :value sexp
)
625 (format "DS%d-%s" (cl-incf counter
) uid
)
627 info nil
(and (eq type
'headline
) 'inlinetask
))
629 ;; If ENTRY is a headline, call current function on every
630 ;; inlinetask within it. In agenda export, this is independent
631 ;; from the mark (or lack thereof) on the entry.
632 (when (eq type
'headline
)
633 (mapconcat #'identity
634 (org-element-map inside
'inlinetask
635 (lambda (task) (org-icalendar-entry task nil info
))
637 ;; Don't forget components from inner entries.
640 (defun org-icalendar--vevent
641 (entry timestamp uid summary location description categories timezone
)
642 "Create a VEVENT component.
644 ENTRY is either a headline or an inlinetask element. TIMESTAMP
645 is a timestamp object defining the date-time of the event. UID
646 is the unique identifier for the event. SUMMARY defines a short
647 summary or subject for the event. LOCATION defines the intended
648 venue for the event. DESCRIPTION provides the complete
649 description of the event. CATEGORIES defines the categories the
650 event belongs to. TIMEZONE specifies a time zone for this event
653 Return VEVENT component as a string."
654 (org-icalendar-fold-string
655 (if (eq (org-element-property :type timestamp
) 'diary
)
656 (org-icalendar-transcode-diary-sexp
657 (org-element-property :raw-value timestamp
) uid summary
)
658 (concat "BEGIN:VEVENT\n"
659 (org-icalendar-dtstamp) "\n"
661 (org-icalendar-convert-timestamp timestamp
"DTSTART" nil timezone
) "\n"
662 (org-icalendar-convert-timestamp timestamp
"DTEND" t timezone
) "\n"
664 (when (org-element-property :repeater-type timestamp
)
665 (format "RRULE:FREQ=%s;INTERVAL=%d\n"
666 (cl-case (org-element-property :repeater-unit timestamp
)
667 (hour "HOURLY") (day "DAILY") (week "WEEKLY")
668 (month "MONTHLY") (year "YEARLY"))
669 (org-element-property :repeater-value timestamp
)))
670 "SUMMARY:" summary
"\n"
671 (and (org-string-nw-p location
) (format "LOCATION:%s\n" location
))
672 (and (org-string-nw-p description
)
673 (format "DESCRIPTION:%s\n" description
))
674 "CATEGORIES:" categories
"\n"
676 (org-icalendar--valarm entry timestamp summary
)
679 (defun org-icalendar--vtodo
680 (entry uid summary location description categories timezone
)
681 "Create a VTODO component.
683 ENTRY is either a headline or an inlinetask element. UID is the
684 unique identifier for the task. SUMMARY defines a short summary
685 or subject for the task. LOCATION defines the intended venue for
686 the task. DESCRIPTION provides the complete description of the
687 task. CATEGORIES defines the categories the task belongs to.
688 TIMEZONE specifies a time zone for this TODO only.
690 Return VTODO component as a string."
691 (let ((start (or (and (memq 'todo-start org-icalendar-use-scheduled
)
692 (org-element-property :scheduled entry
))
693 ;; If we can't use a scheduled time for some
694 ;; reason, start task now.
695 (let ((now (decode-time)))
698 :minute-start
(nth 1 now
)
699 :hour-start
(nth 2 now
)
700 :day-start
(nth 3 now
)
701 :month-start
(nth 4 now
)
702 :year-start
(nth 5 now
)))))))
703 (org-icalendar-fold-string
704 (concat "BEGIN:VTODO\n"
706 (org-icalendar-dtstamp) "\n"
707 (org-icalendar-convert-timestamp start
"DTSTART" nil timezone
) "\n"
708 (and (memq 'todo-due org-icalendar-use-deadline
)
709 (org-element-property :deadline entry
)
710 (concat (org-icalendar-convert-timestamp
711 (org-element-property :deadline entry
) "DUE" nil timezone
)
713 "SUMMARY:" summary
"\n"
714 (and (org-string-nw-p location
) (format "LOCATION:%s\n" location
))
715 (and (org-string-nw-p description
)
716 (format "DESCRIPTION:%s\n" description
))
717 "CATEGORIES:" categories
"\n"
719 (format "PRIORITY:%d\n"
720 (let ((pri (or (org-element-property :priority entry
)
721 org-default-priority
)))
722 (floor (- 9 (* 8.
(/ (float (- org-lowest-priority pri
))
723 (- org-lowest-priority
724 org-highest-priority
)))))))
725 (format "STATUS:%s\n"
726 (if (eq (org-element-property :todo-type entry
) 'todo
)
731 (defun org-icalendar--valarm (entry timestamp summary
)
732 "Create a VALARM component.
734 ENTRY is the calendar entry triggering the alarm. TIMESTAMP is
735 the start date-time of the entry. SUMMARY defines a short
736 summary or subject for the task.
738 Return VALARM component as a string, or nil if it isn't allowed."
739 ;; Create a VALARM entry if the entry is timed. This is not very
741 ;; (a) only one alarm per entry is defined,
742 ;; (b) only minutes are allowed for the trigger period ahead of the
744 ;; (c) only a DISPLAY action is defined. [ESF]
747 (org-element-property :APPT_WARNTIME entry
)))
748 (if warntime
(string-to-number warntime
) 0))))
749 (and (or (> alarm-time
0) (> org-icalendar-alarm-time
0))
750 (org-element-property :hour-start timestamp
)
751 (format "BEGIN:VALARM
757 (if (zerop alarm-time
) org-icalendar-alarm-time alarm-time
)))))
762 (defun org-icalendar-template (contents info
)
763 "Return complete document string after iCalendar conversion.
764 CONTENTS is the transcoded contents string. INFO is a plist used
765 as a communication channel."
766 (org-icalendar--vcalendar
768 (if (not (plist-get info
:input-file
)) (buffer-name (buffer-base-buffer))
769 (file-name-nondirectory
770 (file-name-sans-extension (plist-get info
:input-file
))))
772 (if (not (plist-get info
:with-author
)) ""
773 (org-export-data (plist-get info
:author
) info
))
775 (if (org-string-nw-p org-icalendar-timezone
) org-icalendar-timezone
776 (cadr (current-time-zone)))
778 (org-export-data (plist-get info
:title
) info
)
781 (defun org-icalendar--vcalendar (name owner tz description contents
)
782 "Create a VCALENDAR component.
783 NAME, OWNER, TZ, DESCRIPTION and CONTENTS are all strings giving,
784 respectively, the name of the calendar, its owner, the timezone
785 used, a short description and the other components included."
786 (concat (format "BEGIN:VCALENDAR
789 PRODID:-//%s//Emacs with Org mode//EN
792 CALSCALE:GREGORIAN\n"
793 (org-icalendar-cleanup-string name
)
794 (org-icalendar-cleanup-string owner
)
795 (org-icalendar-cleanup-string tz
)
796 (org-icalendar-cleanup-string description
))
802 ;;; Interactive Functions
805 (defun org-icalendar-export-to-ics
806 (&optional async subtreep visible-only body-only
)
807 "Export current buffer to an iCalendar file.
809 If narrowing is active in the current buffer, only export its
812 If a region is active, export that region.
814 A non-nil optional argument ASYNC means the process should happen
815 asynchronously. The resulting file should be accessible through
816 the `org-export-stack' interface.
818 When optional argument SUBTREEP is non-nil, export the sub-tree
819 at point, extracting information from the headline properties
822 When optional argument VISIBLE-ONLY is non-nil, don't export
823 contents of hidden elements.
825 When optional argument BODY-ONLY is non-nil, only write code
826 between \"BEGIN:VCALENDAR\" and \"END:VCALENDAR\".
828 Return ICS file name."
830 (let ((file (buffer-file-name (buffer-base-buffer))))
831 (when (and file org-icalendar-store-UID
)
832 (org-icalendar-create-uid file
'warn-user
)))
833 ;; Export part. Since this back-end is backed up by `ascii', ensure
834 ;; links will not be collected at the end of sections.
835 (let ((outfile (org-export-output-file-name ".ics" subtreep
)))
836 (org-export-to-file 'icalendar outfile
837 async subtreep visible-only body-only
838 '(:ascii-charset utf-8
:ascii-links-to-notes nil
)
840 (run-hook-with-args 'org-icalendar-after-save-hook file
) nil
))))
843 (defun org-icalendar-export-agenda-files (&optional async
)
844 "Export all agenda files to iCalendar files.
845 When optional argument ASYNC is non-nil, export happens in an
849 ;; Asynchronous export is not interactive, so we will not call
850 ;; `org-check-agenda-file'. Instead we remove any non-existent
851 ;; agenda file from the list.
852 (let ((files (cl-remove-if-not #'file-exists-p
(org-agenda-files t
))))
853 (org-export-async-start
855 (dolist (f results
) (org-export-add-to-stack f
'icalendar
)))
857 (dolist (file ',files outputfiles
)
858 (with-current-buffer (org-get-agenda-file-buffer file
)
859 (push (expand-file-name (org-icalendar-export-to-ics))
861 (let ((files (org-agenda-files t
)))
862 (org-agenda-prepare-buffers files
)
866 (org-check-agenda-file file
)
867 (with-current-buffer (org-get-agenda-file-buffer file
)
868 (org-icalendar-export-to-ics))))
869 (org-release-buffers org-agenda-new-buffers
)))))
872 (defun org-icalendar-combine-agenda-files (&optional async
)
873 "Combine all agenda files into a single iCalendar file.
875 A non-nil optional argument ASYNC means the process should happen
876 asynchronously. The resulting file should be accessible through
877 the `org-export-stack' interface.
879 The file is stored under the name chosen in
880 `org-icalendar-combined-agenda-file'."
883 (let ((files (cl-remove-if-not #'file-exists-p
(org-agenda-files t
))))
884 (org-export-async-start
886 (org-export-add-to-stack
887 (expand-file-name org-icalendar-combined-agenda-file
)
889 `(apply #'org-icalendar--combine-files
',files
)))
890 (apply #'org-icalendar--combine-files
(org-agenda-files t
))))
892 (defun org-icalendar-export-current-agenda (file)
893 "Export current agenda view to an iCalendar FILE.
894 This function assumes major mode for current buffer is
896 (let* ((org-export-use-babel) ;don't evaluate Babel blocks
898 (org-export-string-as
899 (with-output-to-string
901 (let ((p (point-min))
902 (seen nil
)) ;prevent duplicates
903 (while (setq p
(next-single-property-change p
'org-hd-marker
))
904 (let ((m (get-text-property p
'org-hd-marker
)))
905 (when (and m
(not (member m seen
)))
907 (with-current-buffer (marker-buffer m
)
908 (org-with-wide-buffer
909 (goto-char (marker-position m
))
911 (org-element-normalize-string
912 (buffer-substring (point)
913 (org-entry-end-position))))))))
916 '(:ascii-charset utf-8
:ascii-links-to-notes nil
917 :icalendar-include-todo all
))))
920 (org-icalendar--vcalendar
921 org-icalendar-combined-name
923 (or (org-string-nw-p org-icalendar-timezone
) (cadr (current-time-zone)))
924 org-icalendar-combined-description
926 (run-hook-with-args 'org-icalendar-after-save-hook file
)))
928 (defun org-icalendar--combine-files (&rest files
)
929 "Combine entries from multiple files into an iCalendar file.
930 FILES is a list of files to build the calendar from."
931 ;; At the end of the process, all buffers related to FILES are going
932 ;; to be killed. Make sure to only kill the ones opened in the
934 (let ((org-agenda-new-buffers nil
))
937 (with-temp-file org-icalendar-combined-agenda-file
939 (org-icalendar--vcalendar
941 org-icalendar-combined-name
945 (or (org-string-nw-p org-icalendar-timezone
)
946 (cadr (current-time-zone)))
948 org-icalendar-combined-description
955 (org-check-agenda-file file
)
956 (with-current-buffer (org-get-agenda-file-buffer file
)
957 ;; Create ID if necessary.
958 (when org-icalendar-store-UID
959 (org-icalendar-create-uid file t
))
962 '(:ascii-charset utf-8
:ascii-links-to-notes nil
)))))
964 ;; BBDB anniversaries.
965 (when (and org-icalendar-include-bbdb-anniversaries
966 (require 'org-bbdb nil t
))
967 (with-output-to-string (org-bbdb-anniv-export-ical)))))))
968 (run-hook-with-args 'org-icalendar-after-save-hook
969 org-icalendar-combined-agenda-file
))
970 (org-release-buffers org-agenda-new-buffers
))))
973 (provide 'ox-icalendar
)
976 ;; generated-autoload-file: "org-loaddefs.el"
979 ;;; ox-icalendar.el ends here