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