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