improve comment in local.mk template
[org-mode.git] / lisp / org-icalendar.el
blobd322786d52d782b095dd5c1027837360f8ac5d8b
1 ;;; org-icalendar.el --- iCalendar export for Org-mode
3 ;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;;; Code:
29 (require 'org-exp)
31 (eval-when-compile
32 (require 'cl))
34 (declare-function org-bbdb-anniv-export-ical "org-bbdb" nil)
36 (defgroup org-export-icalendar nil
37 "Options specific for iCalendar export of Org-mode files."
38 :tag "Org Export iCalendar"
39 :group 'org-export)
41 (defcustom org-combined-agenda-icalendar-file "~/org.ics"
42 "The file name for the iCalendar file covering all agenda files.
43 This file is created with the command \\[org-export-icalendar-all-agenda-files].
44 The file name should be absolute, the file will be overwritten without warning."
45 :group 'org-export-icalendar
46 :type 'file)
48 (defcustom org-icalendar-alarm-time 0
49 "Number of minutes for triggering an alarm for exported timed events.
50 A zero value (the default) turns off the definition of an alarm trigger
51 for timed events. If non-zero, alarms are created.
53 - a single alarm per entry is defined
54 - The alarm will go off N minutes before the event
55 - only a DISPLAY action is defined."
56 :group 'org-export-icalendar
57 :version "24.1"
58 :type 'integer)
60 (defcustom org-icalendar-combined-name "OrgMode"
61 "Calendar name for the combined iCalendar representing all agenda files."
62 :group 'org-export-icalendar
63 :type 'string)
65 (defcustom org-icalendar-combined-description nil
66 "Calendar description for the combined iCalendar (all agenda files)."
67 :group 'org-export-icalendar
68 :version "24.1"
69 :type 'string)
71 (defcustom org-icalendar-use-plain-timestamp t
72 "Non-nil means make an event from every plain time stamp."
73 :group 'org-export-icalendar
74 :type 'boolean)
76 (defcustom org-icalendar-honor-noexport-tag nil
77 "Non-nil means don't export entries with a tag in `org-export-exclude-tags'."
78 :group 'org-export-icalendar
79 :version "24.1"
80 :type 'boolean)
82 (defcustom org-icalendar-use-deadline '(event-if-not-todo todo-due)
83 "Contexts where iCalendar export should use a deadline time stamp.
84 This is a list with several symbols in it. Valid symbol are:
86 event-if-todo Deadlines in TODO entries become calendar events.
87 event-if-not-todo Deadlines in non-TODO entries become calendar events.
88 todo-due Use deadlines in TODO entries as due-dates"
89 :group 'org-export-icalendar
90 :type '(set :greedy t
91 (const :tag "Deadlines in non-TODO entries become events"
92 event-if-not-todo)
93 (const :tag "Deadline in TODO entries become events"
94 event-if-todo)
95 (const :tag "Deadlines in TODO entries become due-dates"
96 todo-due)))
98 (defcustom org-icalendar-use-scheduled '(todo-start)
99 "Contexts where iCalendar export should use a scheduling time stamp.
100 This is a list with several symbols in it. Valid symbol are:
102 event-if-todo Scheduling time stamps in TODO entries become an event.
103 event-if-not-todo Scheduling time stamps in non-TODO entries become an event.
104 todo-start Scheduling time stamps in TODO entries become start date.
105 Some calendar applications show TODO entries only after
106 that date."
107 :group 'org-export-icalendar
108 :type '(set :greedy t
109 (const :tag
110 "SCHEDULED timestamps in non-TODO entries become events"
111 event-if-not-todo)
112 (const :tag "SCHEDULED timestamps in TODO entries become events"
113 event-if-todo)
114 (const :tag "SCHEDULED in TODO entries become start date"
115 todo-start)))
117 (defcustom org-icalendar-categories '(local-tags category)
118 "Items that should be entered into the categories field.
119 This is a list of symbols, the following are valid:
121 category The Org-mode category of the current file or tree
122 todo-state The todo state, if any
123 local-tags The tags, defined in the current line
124 all-tags All tags, including inherited ones."
125 :group 'org-export-icalendar
126 :type '(repeat
127 (choice
128 (const :tag "The file or tree category" category)
129 (const :tag "The TODO state" todo-state)
130 (const :tag "Tags defined in current line" local-tags)
131 (const :tag "All tags, including inherited ones" all-tags))))
133 (defcustom org-icalendar-include-todo nil
134 "Non-nil means export to iCalendar files should also cover TODO items.
135 Valid values are:
136 nil don't include any TODO items
137 t include all TODO items that are not in a DONE state
138 unblocked include all TODO items that are not blocked
139 all include both done and not done items."
140 :group 'org-export-icalendar
141 :type '(choice
142 (const :tag "None" nil)
143 (const :tag "Unfinished" t)
144 (const :tag "Unblocked" unblocked)
145 (const :tag "All" all)))
147 (defvar org-icalendar-verify-function nil
148 "Function to verify entries for iCalendar export.
149 This can be set to a function that will be called at each entry that
150 is considered for export to iCalendar. When the function returns nil,
151 the entry will be skipped. When it returns a non-nil value, the entry
152 will be considered for export.
153 This is used internally when an agenda buffer is exported to an ics file,
154 to make sure that only entries currently listed in the agenda will end
155 up in the ics file. But for normal iCalendar export, you can use this
156 for whatever you need.")
158 (defcustom org-icalendar-include-bbdb-anniversaries nil
159 "Non-nil means a combined iCalendar files should include anniversaries.
160 The anniversaries are define in the BBDB database."
161 :group 'org-export-icalendar
162 :type 'boolean)
164 (defcustom org-icalendar-include-sexps t
165 "Non-nil means export to iCalendar files should also cover sexp entries.
166 These are entries like in the diary, but directly in an Org-mode file."
167 :group 'org-export-icalendar
168 :type 'boolean)
170 (defcustom org-icalendar-include-body 100
171 "Amount of text below headline to be included in iCalendar export.
172 This is a number of characters that should maximally be included.
173 Properties, scheduling and clocking lines will always be removed.
174 The text will be inserted into the DESCRIPTION field."
175 :group 'org-export-icalendar
176 :type '(choice
177 (const :tag "Nothing" nil)
178 (const :tag "Everything" t)
179 (integer :tag "Max characters")))
181 (defcustom org-icalendar-store-UID nil
182 "Non-nil means store any created UIDs in properties.
183 The iCalendar standard requires that all entries have a unique identifier.
184 Org will create these identifiers as needed. When this variable is non-nil,
185 the created UIDs will be stored in the ID property of the entry. Then the
186 next time this entry is exported, it will be exported with the same UID,
187 superseding the previous form of it. This is essential for
188 synchronization services.
189 This variable is not turned on by default because we want to avoid creating
190 a property drawer in every entry if people are only playing with this feature,
191 or if they are only using it locally."
192 :group 'org-export-icalendar
193 :type 'boolean)
195 (defcustom org-icalendar-timezone (getenv "TZ")
196 "The time zone string for iCalendar export.
197 When nil or the empty string, use output from \(current-time-zone\)."
198 :group 'org-export-icalendar
199 :type '(choice
200 (const :tag "Unspecified" nil)
201 (string :tag "Time zone")))
203 ;; Backward compatibility with previous variable
204 (defvar org-icalendar-use-UTC-date-time nil)
205 (defcustom org-icalendar-date-time-format
206 (if org-icalendar-use-UTC-date-time
207 ":%Y%m%dT%H%M%SZ"
208 ":%Y%m%dT%H%M%S")
209 "Format-string for exporting icalendar DATE-TIME.
210 See `format-time-string' for a full documentation. The only
211 difference is that `org-icalendar-timezone' is used for %Z.
213 Interesting value are:
214 - \":%Y%m%dT%H%M%S\" for local time
215 - \";TZID=%Z:%Y%m%dT%H%M%S\" for local time with explicit timezone
216 - \":%Y%m%dT%H%M%SZ\" for time expressed in Universal Time"
218 :group 'org-export-icalendar
219 :version "24.1"
220 :type '(choice
221 (const :tag "Local time" ":%Y%m%dT%H%M%S")
222 (const :tag "Explicit local time" ";TZID=%Z:%Y%m%dT%H%M%S")
223 (const :tag "Universal time" ":%Y%m%dT%H%M%SZ")
224 (string :tag "Explicit format")))
226 (defun org-icalendar-use-UTC-date-timep ()
227 (char-equal (elt org-icalendar-date-time-format
228 (1- (length org-icalendar-date-time-format))) ?Z))
230 ;;; iCalendar export
232 ;;;###autoload
233 (defun org-export-icalendar-this-file ()
234 "Export current file as an iCalendar file.
235 The iCalendar file will be located in the same directory as the Org-mode
236 file, but with extension `.ics'."
237 (interactive)
238 (org-export-icalendar nil buffer-file-name))
240 ;;;###autoload
241 (defun org-export-icalendar-all-agenda-files ()
242 "Export all files in the variable `org-agenda-files' to iCalendar .ics files.
243 Each iCalendar file will be located in the same directory as the Org-mode
244 file, but with extension `.ics'."
245 (interactive)
246 (apply 'org-export-icalendar nil (org-agenda-files t)))
248 ;;;###autoload
249 (defun org-export-icalendar-combine-agenda-files ()
250 "Export all files in `org-agenda-files' to a single combined iCalendar file.
251 The file is stored under the name `org-combined-agenda-icalendar-file'."
252 (interactive)
253 (apply 'org-export-icalendar t (org-agenda-files t)))
255 (defun org-export-icalendar (combine &rest files)
256 "Create iCalendar files for all elements of FILES.
257 If COMBINE is non-nil, combine all calendar entries into a single large
258 file and store it under the name `org-combined-agenda-icalendar-file'."
259 (save-excursion
260 (org-prepare-agenda-buffers files)
261 (let* ((dir (org-export-directory
262 :ical (list :publishing-directory
263 org-export-publishing-directory)))
264 file ical-file ical-buffer category started org-agenda-new-buffers)
265 (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
266 (when combine
267 (setq ical-file
268 (if (file-name-absolute-p org-combined-agenda-icalendar-file)
269 org-combined-agenda-icalendar-file
270 (expand-file-name org-combined-agenda-icalendar-file dir))
271 ical-buffer (org-get-agenda-file-buffer ical-file))
272 (set-buffer ical-buffer) (erase-buffer))
273 (while (setq file (pop files))
274 (catch 'nextfile
275 (org-check-agenda-file file)
276 (set-buffer (org-get-agenda-file-buffer file))
277 (unless combine
278 (setq ical-file (concat (file-name-as-directory dir)
279 (file-name-sans-extension
280 (file-name-nondirectory buffer-file-name))
281 ".ics"))
282 (setq ical-buffer (org-get-agenda-file-buffer ical-file))
283 (with-current-buffer ical-buffer (erase-buffer)))
284 (setq category (or org-category
285 (file-name-sans-extension
286 (file-name-nondirectory buffer-file-name))))
287 (if (symbolp category) (setq category (symbol-name category)))
288 (let ((standard-output ical-buffer))
289 (if combine
290 (and (not started) (setq started t)
291 (org-start-icalendar-file org-icalendar-combined-name))
292 (org-start-icalendar-file category))
293 (org-print-icalendar-entries combine)
294 (when (or (and combine (not files)) (not combine))
295 (when (and combine org-icalendar-include-bbdb-anniversaries)
296 (require 'org-bbdb)
297 (org-bbdb-anniv-export-ical))
298 (org-finish-icalendar-file)
299 (set-buffer ical-buffer)
300 (run-hooks 'org-before-save-iCalendar-file-hook)
301 (save-buffer)
302 (run-hooks 'org-after-save-iCalendar-file-hook)
303 (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
304 ))))
305 (org-release-buffers org-agenda-new-buffers))))
307 (defvar org-before-save-iCalendar-file-hook nil
308 "Hook run before an iCalendar file has been saved.
309 This can be used to modify the result of the export.")
311 (defvar org-after-save-iCalendar-file-hook nil
312 "Hook run after an iCalendar file has been saved.
313 The iCalendar buffer is still current when this hook is run.
314 A good way to use this is to tell a desktop calendar application to re-read
315 the iCalendar file.")
317 (defvar org-agenda-default-appointment-duration) ; defined in org-agenda.el
318 (defun org-print-icalendar-entries (&optional combine)
319 "Print iCalendar entries for the current Org-mode file to `standard-output'.
320 When COMBINE is non nil, add the category to each line."
321 (require 'org-agenda)
322 (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
323 (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
324 (dts (org-ical-ts-to-string
325 (format-time-string (cdr org-time-stamp-formats) (current-time))
326 "DTSTART"))
327 hd ts ts2 state status (inc t) pos b sexp rrule
328 scheduledp deadlinep todo prefix due start tags
329 tmp pri categories location summary desc uid alarm
330 (sexp-buffer (get-buffer-create "*ical-tmp*")))
331 (org-refresh-category-properties)
332 (save-excursion
333 (goto-char (point-min))
334 (while (re-search-forward re1 nil t)
335 (catch :skip
336 (org-agenda-skip)
337 (when org-icalendar-verify-function
338 (unless (save-match-data (funcall org-icalendar-verify-function))
339 (outline-next-heading)
340 (backward-char 1)
341 (throw :skip nil)))
342 (setq pos (match-beginning 0)
343 ts (match-string 0)
344 tags (org-get-tags-at)
345 inc t
346 hd (condition-case nil
347 (org-icalendar-cleanup-string
348 (org-get-heading t))
349 (error (throw :skip nil)))
350 summary (org-icalendar-cleanup-string
351 (org-entry-get nil "SUMMARY"))
352 desc (org-icalendar-cleanup-string
353 (or (org-entry-get nil "DESCRIPTION")
354 (and org-icalendar-include-body (org-get-entry)))
355 t org-icalendar-include-body)
356 location (org-icalendar-cleanup-string
357 (org-entry-get nil "LOCATION" 'selective))
358 uid (if org-icalendar-store-UID
359 (org-id-get-create)
360 (or (org-id-get) (org-id-new)))
361 categories (org-export-get-categories)
362 alarm ""
363 deadlinep nil scheduledp nil)
364 (if (looking-at re2)
365 (progn
366 (goto-char (match-end 0))
367 (setq ts2 (match-string 1)
368 inc (not (string-match "[0-9]\\{1,2\\}:[0-9][0-9]" ts2))))
369 (setq tmp (buffer-substring (max (point-min)
370 (- pos org-ds-keyword-length))
371 pos)
372 ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
373 (progn
374 (setq inc nil)
375 (replace-match "\\1" t nil ts))
377 deadlinep (string-match org-deadline-regexp tmp)
378 scheduledp (string-match org-scheduled-regexp tmp)
379 todo (org-get-todo-state)
380 ;; donep (org-entry-is-done-p)
382 (when (and (not org-icalendar-use-plain-timestamp)
383 (not deadlinep) (not scheduledp))
384 (throw :skip t))
385 ;; don't export entries with a :noexport: tag
386 (when (and org-icalendar-honor-noexport-tag
387 (delq nil (mapcar (lambda(x)
388 (member x org-export-exclude-tags)) tags)))
389 (throw :skip t))
390 (when (and
391 deadlinep
392 (if todo
393 (not (memq 'event-if-todo org-icalendar-use-deadline))
394 (not (memq 'event-if-not-todo org-icalendar-use-deadline))))
395 (throw :skip t))
396 (when (and
397 scheduledp
398 (if todo
399 (not (memq 'event-if-todo org-icalendar-use-scheduled))
400 (not (memq 'event-if-not-todo org-icalendar-use-scheduled))))
401 (throw :skip t))
402 (setq prefix (if deadlinep "DL-" (if scheduledp "SC-" "TS-")))
403 (if (or (string-match org-tr-regexp hd)
404 (string-match org-ts-regexp hd))
405 (setq hd (replace-match "" t t hd)))
406 (if (string-match "\\+\\([0-9]+\\)\\([hdwmy]\\)>" ts)
407 (setq rrule
408 (concat "\nRRULE:FREQ="
409 (cdr (assoc
410 (match-string 2 ts)
411 '(("h" . "HOURLY")("d" . "DAILY")("w" . "WEEKLY")
412 ("m" . "MONTHLY")("y" . "YEARLY"))))
413 ";INTERVAL=" (match-string 1 ts)))
414 (setq rrule ""))
415 (setq summary (or summary hd))
416 ;; create an alarm entry if the entry is timed. this is not very general in that:
417 ;; (a) only one alarm per entry is defined,
418 ;; (b) only minutes are allowed for the trigger period ahead of the start time, and
419 ;; (c) only a DISPLAY action is defined.
420 ;; [ESF]
421 (let ((t1 (ignore-errors (org-parse-time-string ts 'nodefault))))
422 (if (and (> org-icalendar-alarm-time 0)
423 (car t1) (nth 1 t1) (nth 2 t1))
424 (setq alarm (format "\nBEGIN:VALARM\nACTION:DISPLAY\nDESCRIPTION:%s\nTRIGGER:-P0DT0H%dM0S\nEND:VALARM" summary org-icalendar-alarm-time))
425 (setq alarm ""))
427 (if (string-match org-bracket-link-regexp summary)
428 (setq summary
429 (replace-match (if (match-end 3)
430 (match-string 3 summary)
431 (match-string 1 summary))
432 t t summary)))
433 (if deadlinep (setq summary (concat "DL: " summary)))
434 (if scheduledp (setq summary (concat "S: " summary)))
435 (if (string-match "\\`<%%" ts)
436 (with-current-buffer sexp-buffer
437 (let ((entry (substring ts 1 -1)))
438 (put-text-property 0 1 'uid
439 (concat " " prefix uid) entry)
440 (insert entry " " summary "\n")))
441 (princ (format "BEGIN:VEVENT
442 UID: %s
444 %s%s
445 SUMMARY:%s%s%s
446 CATEGORIES:%s%s
447 END:VEVENT\n"
448 (concat prefix uid)
449 (org-ical-ts-to-string ts "DTSTART")
450 (org-ical-ts-to-string ts2 "DTEND" inc)
451 rrule summary
452 (if (and desc (string-match "\\S-" desc))
453 (concat "\nDESCRIPTION: " desc) "")
454 (if (and location (string-match "\\S-" location))
455 (concat "\nLOCATION: " location) "")
456 categories
457 alarm)))))
458 (when (and org-icalendar-include-sexps
459 (condition-case nil (require 'icalendar) (error nil))
460 (fboundp 'icalendar-export-region))
461 ;; Get all the literal sexps
462 (goto-char (point-min))
463 (while (re-search-forward "^&?%%(" nil t)
464 (catch :skip
465 (org-agenda-skip)
466 (when org-icalendar-verify-function
467 (unless (save-match-data (funcall org-icalendar-verify-function))
468 (outline-next-heading)
469 (backward-char 1)
470 (throw :skip nil)))
471 (setq b (match-beginning 0))
472 (goto-char (1- (match-end 0)))
473 (forward-sexp 1)
474 (end-of-line 1)
475 (setq sexp (buffer-substring b (point)))
476 (with-current-buffer sexp-buffer
477 (insert sexp "\n"))))
478 (princ (org-diary-to-ical-string sexp-buffer))
479 (kill-buffer sexp-buffer))
481 (when org-icalendar-include-todo
482 (setq prefix "TODO-")
483 (goto-char (point-min))
484 (while (re-search-forward org-complex-heading-regexp nil t)
485 (catch :skip
486 (org-agenda-skip)
487 (when org-icalendar-verify-function
488 (unless (save-match-data
489 (funcall org-icalendar-verify-function))
490 (outline-next-heading)
491 (backward-char 1)
492 (throw :skip nil)))
493 (setq state (match-string 2))
494 (setq status (if (member state org-done-keywords)
495 "COMPLETED" "NEEDS-ACTION"))
496 (when (and state
497 (cond
498 ;; check if the state is one we should use
499 ((eq org-icalendar-include-todo 'all)
500 ;; all should be included
502 ((eq org-icalendar-include-todo 'unblocked)
503 ;; only undone entries that are not blocked
504 (and (member state org-not-done-keywords)
505 (or (not org-blocker-hook)
506 (save-match-data
507 (run-hook-with-args-until-failure
508 'org-blocker-hook
509 (list :type 'todo-state-change
510 :position (point-at-bol)
511 :from 'todo
512 :to 'done))))))
513 ((eq org-icalendar-include-todo t)
514 ;; include everything that is not done
515 (member state org-not-done-keywords))))
516 (setq hd (match-string 4)
517 summary (org-icalendar-cleanup-string
518 (org-entry-get nil "SUMMARY"))
519 desc (org-icalendar-cleanup-string
520 (or (org-entry-get nil "DESCRIPTION")
521 (and org-icalendar-include-body (org-get-entry)))
522 t org-icalendar-include-body)
523 location (org-icalendar-cleanup-string
524 (org-entry-get nil "LOCATION" 'selective))
525 due (and (member 'todo-due org-icalendar-use-deadline)
526 (org-entry-get nil "DEADLINE"))
527 start (and (member 'todo-start org-icalendar-use-scheduled)
528 (org-entry-get nil "SCHEDULED"))
529 categories (org-export-get-categories)
530 uid (if org-icalendar-store-UID
531 (org-id-get-create)
532 (or (org-id-get) (org-id-new))))
533 (and due (setq due (org-ical-ts-to-string due "DUE")))
534 (and start (setq start (org-ical-ts-to-string start "DTSTART")))
536 (if (string-match org-bracket-link-regexp hd)
537 (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
538 (match-string 1 hd))
539 t t hd)))
540 (if (string-match org-priority-regexp hd)
541 (setq pri (string-to-char (match-string 2 hd))
542 hd (concat (substring hd 0 (match-beginning 1))
543 (substring hd (match-end 1))))
544 (setq pri org-default-priority))
545 (setq pri (floor (- 9 (* 8. (/ (float (- org-lowest-priority pri))
546 (- org-lowest-priority org-highest-priority))))))
548 (princ (format "BEGIN:VTODO
549 UID: %s
551 SUMMARY:%s%s%s%s
552 CATEGORIES:%s
553 SEQUENCE:1
554 PRIORITY:%d
555 STATUS:%s
556 END:VTODO\n"
557 (concat prefix uid)
558 (or start dts)
559 (or summary hd)
560 (if (and location (string-match "\\S-" location))
561 (concat "\nLOCATION: " location) "")
562 (if (and desc (string-match "\\S-" desc))
563 (concat "\nDESCRIPTION: " desc) "")
564 (if due (concat "\n" due) "")
565 categories
566 pri status)))))))))
568 (defun org-export-get-categories ()
569 "Get categories according to `org-icalendar-categories'."
570 (let ((cs org-icalendar-categories) c rtn tmp)
571 (while (setq c (pop cs))
572 (cond
573 ((eq c 'category) (push (org-get-category) rtn))
574 ((eq c 'todo-state)
575 (setq tmp (org-get-todo-state))
576 (and tmp (push tmp rtn)))
577 ((eq c 'local-tags)
578 (setq rtn (append (nreverse (org-get-local-tags-at (point))) rtn)))
579 ((eq c 'all-tags)
580 (setq rtn (append (nreverse (org-get-tags-at (point))) rtn)))))
581 (mapconcat 'identity (nreverse rtn) ",")))
583 (defun org-icalendar-cleanup-string (s &optional is-body maxlength)
584 "Take out stuff and quote what needs to be quoted.
585 When IS-BODY is non-nil, assume that this is the body of an item, clean up
586 whitespace, newlines, drawers, and timestamps, and cut it down to MAXLENGTH
587 characters."
588 (if (not s)
590 (if is-body
591 (let ((re (concat "\\(" org-drawer-regexp "\\)[^\000]*?:END:.*\n?"))
592 (re2 (concat "^[ \t]*" org-keyword-time-regexp ".*\n?")))
593 (while (string-match re s) (setq s (replace-match "" t t s)))
594 (while (string-match re2 s) (setq s (replace-match "" t t s))))
595 (setq s (replace-regexp-in-string "[[:space:]]+" " " s)))
596 (let ((start 0))
597 (while (string-match "\\([,;]\\)" s start)
598 (setq start (+ (match-beginning 0) 2)
599 s (replace-match "\\\\\\1" nil nil s))))
600 (setq s (org-trim s))
601 (when is-body
602 (while (string-match "[ \t]*\n[ \t]*" s)
603 (setq s (replace-match "\\n" t t s))))
604 (if is-body
605 (if maxlength
606 (if (and (numberp maxlength)
607 (> (length s) maxlength))
608 (setq s (substring s 0 maxlength)))))
611 (defun org-icalendar-cleanup-string-rfc2455 (s &optional is-body maxlength)
612 "Take out stuff and quote what needs to be quoted.
613 When IS-BODY is non-nil, assume that this is the body of an item, clean up
614 whitespace, newlines, drawers, and timestamps, and cut it down to MAXLENGTH
615 characters.
616 This seems to be more like RFC 2455, but it causes problems, so it is
617 not used right now."
618 (if (not s)
620 (if is-body
621 (let ((re (concat "\\(" org-drawer-regexp "\\)[^\000]*?:END:.*\n?"))
622 (re2 (concat "^[ \t]*" org-keyword-time-regexp ".*\n?")))
623 (while (string-match re s) (setq s (replace-match "" t t s)))
624 (while (string-match re2 s) (setq s (replace-match "" t t s)))
625 (setq s (org-trim s))
626 (while (string-match "[ \t]*\n[ \t]*" s)
627 (setq s (replace-match "\\n" t t s)))
628 (if maxlength
629 (if (and (numberp maxlength)
630 (> (length s) maxlength))
631 (setq s (substring s 0 maxlength)))))
632 (setq s (org-trim s)))
633 (while (string-match "\"" s) (setq s (replace-match "''" t t s)))
634 (when (string-match "[;,:]" s) (setq s (concat "\"" s "\"")))
637 (defun org-start-icalendar-file (name)
638 "Start an iCalendar file by inserting the header."
639 (let ((user user-full-name)
640 (name (or name "unknown"))
641 (timezone (if (> (length org-icalendar-timezone) 0)
642 org-icalendar-timezone
643 (cadr (current-time-zone))))
644 (description org-icalendar-combined-description))
645 (princ
646 (format "BEGIN:VCALENDAR
647 VERSION:2.0
648 X-WR-CALNAME:%s
649 PRODID:-//%s//Emacs with Org-mode//EN
650 X-WR-TIMEZONE:%s
651 X-WR-CALDESC:%s
652 CALSCALE:GREGORIAN\n" name user timezone description))))
654 (defun org-finish-icalendar-file ()
655 "Finish an iCalendar file by inserting the END statement."
656 (princ "END:VCALENDAR\n"))
658 (defun org-ical-ts-to-string (s keyword &optional inc)
659 "Take a time string S and convert it to iCalendar format.
660 KEYWORD is added in front, to make a complete line like DTSTART....
661 When INC is non-nil, increase the hour by two (if time string contains
662 a time), or the day by one (if it does not contain a time)."
663 (let ((t1 (ignore-errors (org-parse-time-string s 'nodefault)))
664 t2 fmt have-time time)
665 (if (not t1)
667 (if (and (car t1) (nth 1 t1) (nth 2 t1))
668 (setq t2 t1 have-time t)
669 (setq t2 (org-parse-time-string s)))
670 (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
671 (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
672 (when inc
673 (if have-time
674 (if org-agenda-default-appointment-duration
675 (setq mi (+ org-agenda-default-appointment-duration mi))
676 (setq h (+ 2 h)))
677 (setq d (1+ d))))
678 (setq time (encode-time s mi h d m y)))
679 (setq fmt (if have-time
680 (replace-regexp-in-string "%Z"
681 org-icalendar-timezone
682 org-icalendar-date-time-format)
683 ";VALUE=DATE:%Y%m%d"))
684 (concat keyword (format-time-string fmt time
685 (and (org-icalendar-use-UTC-date-timep)
686 have-time))))))
688 (provide 'org-icalendar)
690 ;;; org-icalendar.el ends here