org-icalendar.el 26 KB

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