org-icalendar.el 26 KB

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