1 ;;; ox-icalendar.el --- iCalendar Back-End for Org Export Engine -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2004-2019 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: https://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
"ol-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 (org-element-contents 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 (class (org-icalendar-cleanup-string
544 (org-export-get-node-property
546 (org-property-inherit-p "CLASS"))))
547 ;; Build description of the entry from associated section
548 ;; (headline) or contents (inlinetask).
550 (org-icalendar-cleanup-string
551 (or (org-element-property :DESCRIPTION entry
)
552 (let ((contents (org-export-data inside info
)))
554 ((not (org-string-nw-p contents
)) nil
)
555 ((wholenump org-icalendar-include-body
)
556 (let ((contents (org-trim contents
)))
558 contents
0 (min (length contents
)
559 org-icalendar-include-body
))))
560 (org-icalendar-include-body (org-trim contents
)))))))
561 (cat (org-icalendar-get-categories entry info
))
562 (tz (org-export-get-node-property
564 (org-property-inherit-p "TIMEZONE"))))
566 ;; Events: Delegate to `org-icalendar--vevent' to generate
567 ;; "VEVENT" component from scheduled, deadline, or any
568 ;; timestamp in the entry.
569 (let ((deadline (org-element-property :deadline entry
)))
571 (memq (if todo-type
'event-if-todo
'event-if-not-todo
)
572 org-icalendar-use-deadline
)
573 (org-icalendar--vevent
574 entry deadline
(concat "DL-" uid
)
575 (concat "DL: " summary
) loc desc cat tz class
)))
576 (let ((scheduled (org-element-property :scheduled entry
)))
578 (memq (if todo-type
'event-if-todo
'event-if-not-todo
)
579 org-icalendar-use-scheduled
)
580 (org-icalendar--vevent
581 entry scheduled
(concat "SC-" uid
)
582 (concat "S: " summary
) loc desc cat tz class
)))
583 ;; When collecting plain timestamps from a headline and its
584 ;; title, skip inlinetasks since collection will happen once
585 ;; ENTRY is one of them.
589 (org-element-map (cons (org-element-property :title entry
)
590 (org-element-contents inside
))
593 (when (let ((type (org-element-property :type ts
)))
594 (cl-case (plist-get info
:with-timestamps
)
595 (active (memq type
'(active active-range
)))
596 (inactive (memq type
'(inactive inactive-range
)))
598 (let ((uid (format "TS%d-%s" (cl-incf counter
) uid
)))
599 (org-icalendar--vevent
600 entry ts uid summary loc desc cat tz class
))))
601 info nil
(and (eq type
'headline
) 'inlinetask
))
603 ;; Task: First check if it is appropriate to export it. If
604 ;; so, call `org-icalendar--vtodo' to transcode it into
605 ;; a "VTODO" component.
607 (cl-case (plist-get info
:icalendar-include-todo
)
610 (and (eq type
'headline
)
611 (not (org-icalendar-blocked-headline-p
613 ((t) (eq todo-type
'todo
))))
614 (org-icalendar--vtodo entry uid summary loc desc cat tz class
))
615 ;; Diary-sexp: Collect every diary-sexp element within ENTRY
616 ;; and its title, and transcode them. If ENTRY is
617 ;; a headline, skip inlinetasks: they will be handled
619 (when org-icalendar-include-sexps
621 (mapconcat #'identity
623 (cons (org-element-property :title entry
)
624 (org-element-contents inside
))
627 (org-icalendar-transcode-diary-sexp
628 (org-element-property :value sexp
)
629 (format "DS%d-%s" (cl-incf counter
) uid
)
631 info nil
(and (eq type
'headline
) 'inlinetask
))
633 ;; If ENTRY is a headline, call current function on every
634 ;; inlinetask within it. In agenda export, this is independent
635 ;; from the mark (or lack thereof) on the entry.
636 (when (eq type
'headline
)
637 (mapconcat #'identity
638 (org-element-map inside
'inlinetask
639 (lambda (task) (org-icalendar-entry task nil info
))
641 ;; Don't forget components from inner entries.
644 (defun org-icalendar--vevent
645 (entry timestamp uid summary location description categories timezone class
)
646 "Create a VEVENT component.
648 ENTRY is either a headline or an inlinetask element. TIMESTAMP
649 is a timestamp object defining the date-time of the event. UID
650 is the unique identifier for the event. SUMMARY defines a short
651 summary or subject for the event. LOCATION defines the intended
652 venue for the event. DESCRIPTION provides the complete
653 description of the event. CATEGORIES defines the categories the
654 event belongs to. TIMEZONE specifies a time zone for this event
655 only. CLASS contains the visibility attribute. Three of them
656 (\"PUBLIC\", \"CONFIDENTIAL\", and \"PRIVATE\") are predefined, others
657 should be treated as \"PRIVATE\" if they are unknown to the iCalendar server.
659 Return VEVENT component as a string."
660 (org-icalendar-fold-string
661 (if (eq (org-element-property :type timestamp
) 'diary
)
662 (org-icalendar-transcode-diary-sexp
663 (org-element-property :raw-value timestamp
) uid summary
)
664 (concat "BEGIN:VEVENT\n"
665 (org-icalendar-dtstamp) "\n"
667 (org-icalendar-convert-timestamp timestamp
"DTSTART" nil timezone
) "\n"
668 (org-icalendar-convert-timestamp timestamp
"DTEND" t timezone
) "\n"
670 (when (org-element-property :repeater-type timestamp
)
671 (format "RRULE:FREQ=%s;INTERVAL=%d\n"
672 (cl-case (org-element-property :repeater-unit timestamp
)
673 (hour "HOURLY") (day "DAILY") (week "WEEKLY")
674 (month "MONTHLY") (year "YEARLY"))
675 (org-element-property :repeater-value timestamp
)))
676 "SUMMARY:" summary
"\n"
677 (and (org-string-nw-p location
) (format "LOCATION:%s\n" location
))
678 (and (org-string-nw-p class
) (format "CLASS:%s\n" class
))
679 (and (org-string-nw-p description
)
680 (format "DESCRIPTION:%s\n" description
))
681 "CATEGORIES:" categories
"\n"
683 (org-icalendar--valarm entry timestamp summary
)
686 (defun org-icalendar--vtodo
687 (entry uid summary location description categories timezone class
)
688 "Create a VTODO component.
690 ENTRY is either a headline or an inlinetask element. UID is the
691 unique identifier for the task. SUMMARY defines a short summary
692 or subject for the task. LOCATION defines the intended venue for
693 the task. DESCRIPTION provides the complete description of the
694 task. CATEGORIES defines the categories the task belongs to.
695 TIMEZONE specifies a time zone for this TODO only.
697 Return VTODO component as a string."
698 (let ((start (or (and (memq 'todo-start org-icalendar-use-scheduled
)
699 (org-element-property :scheduled entry
))
700 ;; If we can't use a scheduled time for some
701 ;; reason, start task now.
702 (let ((now (decode-time)))
705 :minute-start
(nth 1 now
)
706 :hour-start
(nth 2 now
)
707 :day-start
(nth 3 now
)
708 :month-start
(nth 4 now
)
709 :year-start
(nth 5 now
)))))))
710 (org-icalendar-fold-string
711 (concat "BEGIN:VTODO\n"
713 (org-icalendar-dtstamp) "\n"
714 (org-icalendar-convert-timestamp start
"DTSTART" nil timezone
) "\n"
715 (and (memq 'todo-due org-icalendar-use-deadline
)
716 (org-element-property :deadline entry
)
717 (concat (org-icalendar-convert-timestamp
718 (org-element-property :deadline entry
) "DUE" nil timezone
)
720 "SUMMARY:" summary
"\n"
721 (and (org-string-nw-p location
) (format "LOCATION:%s\n" location
))
722 (and (org-string-nw-p class
) (format "CLASS:%s\n" class
))
723 (and (org-string-nw-p description
)
724 (format "DESCRIPTION:%s\n" description
))
725 "CATEGORIES:" categories
"\n"
727 (format "PRIORITY:%d\n"
728 (let ((pri (or (org-element-property :priority entry
)
729 org-default-priority
)))
730 (floor (- 9 (* 8.
(/ (float (- org-lowest-priority pri
))
731 (- org-lowest-priority
732 org-highest-priority
)))))))
733 (format "STATUS:%s\n"
734 (if (eq (org-element-property :todo-type entry
) 'todo
)
739 (defun org-icalendar--valarm (entry timestamp summary
)
740 "Create a VALARM component.
742 ENTRY is the calendar entry triggering the alarm. TIMESTAMP is
743 the start date-time of the entry. SUMMARY defines a short
744 summary or subject for the task.
746 Return VALARM component as a string, or nil if it isn't allowed."
747 ;; Create a VALARM entry if the entry is timed. This is not very
749 ;; (a) only one alarm per entry is defined,
750 ;; (b) only minutes are allowed for the trigger period ahead of the
752 ;; (c) only a DISPLAY action is defined. [ESF]
755 (org-element-property :APPT_WARNTIME entry
)))
756 (if warntime
(string-to-number warntime
) 0))))
757 (and (or (> alarm-time
0) (> org-icalendar-alarm-time
0))
758 (org-element-property :hour-start timestamp
)
759 (format "BEGIN:VALARM
765 (if (zerop alarm-time
) org-icalendar-alarm-time alarm-time
)))))
770 (defun org-icalendar-template (contents info
)
771 "Return complete document string after iCalendar conversion.
772 CONTENTS is the transcoded contents string. INFO is a plist used
773 as a communication channel."
774 (org-icalendar--vcalendar
776 (if (not (plist-get info
:input-file
)) (buffer-name (buffer-base-buffer))
777 (file-name-nondirectory
778 (file-name-sans-extension (plist-get info
:input-file
))))
780 (if (not (plist-get info
:with-author
)) ""
781 (org-export-data (plist-get info
:author
) info
))
783 (if (org-string-nw-p org-icalendar-timezone
) org-icalendar-timezone
784 (cadr (current-time-zone)))
786 (org-export-data (plist-get info
:title
) info
)
789 (defun org-icalendar--vcalendar (name owner tz description contents
)
790 "Create a VCALENDAR component.
791 NAME, OWNER, TZ, DESCRIPTION and CONTENTS are all strings giving,
792 respectively, the name of the calendar, its owner, the timezone
793 used, a short description and the other components included."
794 (concat (format "BEGIN:VCALENDAR
797 PRODID:-//%s//Emacs with Org mode//EN
800 CALSCALE:GREGORIAN\n"
801 (org-icalendar-cleanup-string name
)
802 (org-icalendar-cleanup-string owner
)
803 (org-icalendar-cleanup-string tz
)
804 (org-icalendar-cleanup-string description
))
810 ;;; Interactive Functions
813 (defun org-icalendar-export-to-ics
814 (&optional async subtreep visible-only body-only
)
815 "Export current buffer to an iCalendar file.
817 If narrowing is active in the current buffer, only export its
820 If a region is active, export that region.
822 A non-nil optional argument ASYNC means the process should happen
823 asynchronously. The resulting file should be accessible through
824 the `org-export-stack' interface.
826 When optional argument SUBTREEP is non-nil, export the sub-tree
827 at point, extracting information from the headline properties
830 When optional argument VISIBLE-ONLY is non-nil, don't export
831 contents of hidden elements.
833 When optional argument BODY-ONLY is non-nil, only write code
834 between \"BEGIN:VCALENDAR\" and \"END:VCALENDAR\".
836 Return ICS file name."
838 (let ((file (buffer-file-name (buffer-base-buffer))))
839 (when (and file org-icalendar-store-UID
)
840 (org-icalendar-create-uid file
'warn-user
)))
841 ;; Export part. Since this back-end is backed up by `ascii', ensure
842 ;; links will not be collected at the end of sections.
843 (let ((outfile (org-export-output-file-name ".ics" subtreep
)))
844 (org-export-to-file 'icalendar outfile
845 async subtreep visible-only body-only
846 '(:ascii-charset utf-8
:ascii-links-to-notes nil
)
848 (run-hook-with-args 'org-icalendar-after-save-hook file
) nil
))))
851 (defun org-icalendar-export-agenda-files (&optional async
)
852 "Export all agenda files to iCalendar files.
853 When optional argument ASYNC is non-nil, export happens in an
857 ;; Asynchronous export is not interactive, so we will not call
858 ;; `org-check-agenda-file'. Instead we remove any non-existent
859 ;; agenda file from the list.
860 (let ((files (cl-remove-if-not #'file-exists-p
(org-agenda-files t
))))
861 (org-export-async-start
863 (dolist (f results
) (org-export-add-to-stack f
'icalendar
)))
865 (dolist (file ',files outputfiles
)
866 (with-current-buffer (org-get-agenda-file-buffer file
)
867 (push (expand-file-name (org-icalendar-export-to-ics))
869 (let ((files (org-agenda-files t
)))
870 (org-agenda-prepare-buffers files
)
874 (org-check-agenda-file file
)
875 (with-current-buffer (org-get-agenda-file-buffer file
)
876 (org-icalendar-export-to-ics))))
877 (org-release-buffers org-agenda-new-buffers
)))))
880 (defun org-icalendar-combine-agenda-files (&optional async
)
881 "Combine all agenda files into a single iCalendar file.
883 A non-nil optional argument ASYNC means the process should happen
884 asynchronously. The resulting file should be accessible through
885 the `org-export-stack' interface.
887 The file is stored under the name chosen in
888 `org-icalendar-combined-agenda-file'."
891 (let ((files (cl-remove-if-not #'file-exists-p
(org-agenda-files t
))))
892 (org-export-async-start
894 (org-export-add-to-stack
895 (expand-file-name org-icalendar-combined-agenda-file
)
897 `(apply #'org-icalendar--combine-files
',files
)))
898 (apply #'org-icalendar--combine-files
(org-agenda-files t
))))
900 (defun org-icalendar-export-current-agenda (file)
901 "Export current agenda view to an iCalendar FILE.
902 This function assumes major mode for current buffer is
904 (let* ((org-export-use-babel) ;don't evaluate Babel blocks
906 (org-export-string-as
907 (with-output-to-string
909 (let ((p (point-min))
910 (seen nil
)) ;prevent duplicates
911 (while (setq p
(next-single-property-change p
'org-hd-marker
))
912 (let ((m (get-text-property p
'org-hd-marker
)))
913 (when (and m
(not (member m seen
)))
915 (with-current-buffer (marker-buffer m
)
916 (org-with-wide-buffer
917 (goto-char (marker-position m
))
919 (org-element-normalize-string
920 (buffer-substring (point)
921 (org-entry-end-position))))))))
924 '(:ascii-charset utf-8
:ascii-links-to-notes nil
925 :icalendar-include-todo all
))))
928 (org-icalendar--vcalendar
929 org-icalendar-combined-name
931 (or (org-string-nw-p org-icalendar-timezone
) (cadr (current-time-zone)))
932 org-icalendar-combined-description
934 (run-hook-with-args 'org-icalendar-after-save-hook file
)))
936 (defun org-icalendar--combine-files (&rest files
)
937 "Combine entries from multiple files into an iCalendar file.
938 FILES is a list of files to build the calendar from."
939 ;; At the end of the process, all buffers related to FILES are going
940 ;; to be killed. Make sure to only kill the ones opened in the
942 (let ((org-agenda-new-buffers nil
))
945 (with-temp-file org-icalendar-combined-agenda-file
947 (org-icalendar--vcalendar
949 org-icalendar-combined-name
953 (or (org-string-nw-p org-icalendar-timezone
)
954 (cadr (current-time-zone)))
956 org-icalendar-combined-description
963 (org-check-agenda-file file
)
964 (with-current-buffer (org-get-agenda-file-buffer file
)
965 ;; Create ID if necessary.
966 (when org-icalendar-store-UID
967 (org-icalendar-create-uid file t
))
970 '(:ascii-charset utf-8
:ascii-links-to-notes nil
)))))
972 ;; BBDB anniversaries.
973 (when (and org-icalendar-include-bbdb-anniversaries
974 (require 'ol-bbdb nil t
))
975 (with-output-to-string (org-bbdb-anniv-export-ical)))))))
976 (run-hook-with-args 'org-icalendar-after-save-hook
977 org-icalendar-combined-agenda-file
))
978 (org-release-buffers org-agenda-new-buffers
))))
981 (provide 'ox-icalendar
)
984 ;; generated-autoload-file: "org-loaddefs.el"
987 ;;; ox-icalendar.el ends here