org-icalendar.el 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. ;;; org-icalendar.el --- iCalendar export for Org-mode
  2. ;; Copyright (C) 2004-2013 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-agenda-prepare-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. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  290. (save-excursion
  291. (goto-char (point-min))
  292. (while (re-search-forward re1 nil t)
  293. (catch :skip
  294. (org-agenda-skip)
  295. (when org-icalendar-verify-function
  296. (unless (save-match-data (funcall org-icalendar-verify-function))
  297. (outline-next-heading)
  298. (backward-char 1)
  299. (throw :skip nil)))
  300. (setq pos (match-beginning 0)
  301. ts (match-string 0)
  302. tags (org-get-tags-at)
  303. inc t
  304. hd (condition-case nil
  305. (org-icalendar-cleanup-string
  306. (org-get-heading t))
  307. (error (throw :skip nil)))
  308. summary (org-icalendar-cleanup-string
  309. (org-entry-get nil "SUMMARY"))
  310. desc (org-icalendar-cleanup-string
  311. (or (org-entry-get nil "DESCRIPTION")
  312. (and org-icalendar-include-body (org-get-entry)))
  313. t org-icalendar-include-body)
  314. location (org-icalendar-cleanup-string
  315. (org-entry-get nil "LOCATION" 'selective))
  316. uid (if org-icalendar-store-UID
  317. (org-id-get-create)
  318. (or (org-id-get) (org-id-new)))
  319. categories (org-export-get-categories)
  320. alarm-time (get-text-property (point) 'org-appt-warntime)
  321. alarm-time (if alarm-time (string-to-number alarm-time) 0)
  322. alarm ""
  323. deadlinep nil scheduledp nil)
  324. (setq tmp (buffer-substring (max (point-min) (- pos org-ds-keyword-length)) pos)
  325. deadlinep (string-match org-deadline-regexp tmp)
  326. scheduledp (string-match org-scheduled-regexp tmp)
  327. todo (org-get-todo-state))
  328. ;; donep (org-entry-is-done-p)
  329. (if (looking-at re2)
  330. (progn
  331. (goto-char (match-end 0))
  332. (setq ts2 (match-string 1)
  333. inc (not (string-match "[0-9]\\{1,2\\}:[0-9][0-9]" ts2))))
  334. (setq ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
  335. (progn
  336. (setq inc nil)
  337. (replace-match "\\1" t nil ts))
  338. ts)))
  339. (when (and (not org-icalendar-use-plain-timestamp)
  340. (not deadlinep) (not scheduledp))
  341. (throw :skip t))
  342. ;; don't export entries with a :noexport: tag
  343. (when (and org-icalendar-honor-noexport-tag
  344. (delq nil (mapcar (lambda(x)
  345. (member x org-export-exclude-tags)) tags)))
  346. (throw :skip t))
  347. (when (and
  348. deadlinep
  349. (if todo
  350. (not (memq 'event-if-todo org-icalendar-use-deadline))
  351. (not (memq 'event-if-not-todo org-icalendar-use-deadline))))
  352. (throw :skip t))
  353. (when (and
  354. scheduledp
  355. (if todo
  356. (not (memq 'event-if-todo org-icalendar-use-scheduled))
  357. (not (memq 'event-if-not-todo org-icalendar-use-scheduled))))
  358. (throw :skip t))
  359. (setq prefix (if deadlinep "DL-" (if scheduledp "SC-" "TS-")))
  360. (if (or (string-match org-tr-regexp hd)
  361. (string-match org-ts-regexp hd))
  362. (setq hd (replace-match "" t t hd)))
  363. (if (string-match "\\+\\([0-9]+\\)\\([hdwmy]\\)>" ts)
  364. (setq rrule
  365. (concat "\nRRULE:FREQ="
  366. (cdr (assoc
  367. (match-string 2 ts)
  368. '(("h" . "HOURLY")("d" . "DAILY")("w" . "WEEKLY")
  369. ("m" . "MONTHLY")("y" . "YEARLY"))))
  370. ";INTERVAL=" (match-string 1 ts)))
  371. (setq rrule ""))
  372. (setq summary (or summary hd))
  373. ;; create an alarm entry if the entry is timed. this is not very general in that:
  374. ;; (a) only one alarm per entry is defined,
  375. ;; (b) only minutes are allowed for the trigger period ahead of the start time, and
  376. ;; (c) only a DISPLAY action is defined.
  377. ;; [ESF]
  378. (let ((t1 (ignore-errors (org-parse-time-string ts 'nodefault))))
  379. (if (and (or (> alarm-time 0) (> org-icalendar-alarm-time 0))
  380. (car t1) (nth 1 t1) (nth 2 t1))
  381. (setq alarm (format "\nBEGIN:VALARM\nACTION:DISPLAY\nDESCRIPTION:%s\nTRIGGER:-P0DT0H%dM0S\nEND:VALARM"
  382. summary (or alarm-time org-icalendar-alarm-time)))
  383. (setq alarm "")))
  384. (if (string-match org-bracket-link-regexp summary)
  385. (setq summary
  386. (replace-match (if (match-end 3)
  387. (match-string 3 summary)
  388. (match-string 1 summary))
  389. t t summary)))
  390. (if deadlinep (setq summary (concat "DL: " summary)))
  391. (if scheduledp (setq summary (concat "S: " summary)))
  392. (if (string-match "\\`<%%" ts)
  393. (with-current-buffer sexp-buffer
  394. (let ((entry (substring ts 1 -1)))
  395. (put-text-property 0 1 'uid
  396. (concat " " prefix uid) entry)
  397. (insert entry " " summary "\n")))
  398. (princ (format "BEGIN:VEVENT
  399. UID: %s
  400. %s
  401. %s%s
  402. SUMMARY:%s%s%s
  403. CATEGORIES:%s%s
  404. END:VEVENT\n"
  405. (concat prefix uid)
  406. (org-icalendar-ts-to-string ts "DTSTART")
  407. (org-icalendar-ts-to-string ts2 "DTEND" inc)
  408. rrule summary
  409. (if (and desc (string-match "\\S-" desc))
  410. (concat "\nDESCRIPTION: " desc) "")
  411. (if (and location (string-match "\\S-" location))
  412. (concat "\nLOCATION: " location) "")
  413. categories
  414. alarm)))))
  415. (when (and org-icalendar-include-sexps
  416. (condition-case nil (require 'icalendar) (error nil))
  417. (fboundp 'icalendar-export-region))
  418. ;; Get all the literal sexps
  419. (goto-char (point-min))
  420. (while (re-search-forward "^&?%%(" nil t)
  421. (catch :skip
  422. (org-agenda-skip)
  423. (when org-icalendar-verify-function
  424. (unless (save-match-data (funcall org-icalendar-verify-function))
  425. (outline-next-heading)
  426. (backward-char 1)
  427. (throw :skip nil)))
  428. (setq b (match-beginning 0))
  429. (goto-char (1- (match-end 0)))
  430. (forward-sexp 1)
  431. (end-of-line 1)
  432. (setq sexp (buffer-substring b (point)))
  433. (with-current-buffer sexp-buffer
  434. (insert sexp "\n"))))
  435. (princ (org-diary-to-ical-string sexp-buffer))
  436. (kill-buffer sexp-buffer))
  437. (when org-icalendar-include-todo
  438. (setq prefix "TODO-")
  439. (goto-char (point-min))
  440. (while (re-search-forward org-complex-heading-regexp nil t)
  441. (catch :skip
  442. (org-agenda-skip)
  443. (when org-icalendar-verify-function
  444. (unless (save-match-data
  445. (funcall org-icalendar-verify-function))
  446. (outline-next-heading)
  447. (backward-char 1)
  448. (throw :skip nil)))
  449. (setq state (match-string 2))
  450. (setq status (if (member state org-done-keywords)
  451. "COMPLETED" "NEEDS-ACTION"))
  452. (when (and state
  453. (cond
  454. ;; check if the state is one we should use
  455. ((eq org-icalendar-include-todo 'all)
  456. ;; all should be included
  457. t)
  458. ((eq org-icalendar-include-todo 'unblocked)
  459. ;; only undone entries that are not blocked
  460. (and (member state org-not-done-keywords)
  461. (or (not org-blocker-hook)
  462. (save-match-data
  463. (run-hook-with-args-until-failure
  464. 'org-blocker-hook
  465. (list :type 'todo-state-change
  466. :position (point-at-bol)
  467. :from 'todo
  468. :to 'done))))))
  469. ((eq org-icalendar-include-todo t)
  470. ;; include everything that is not done
  471. (member state org-not-done-keywords))))
  472. (setq hd (match-string 4)
  473. summary (org-icalendar-cleanup-string
  474. (org-entry-get nil "SUMMARY"))
  475. desc (org-icalendar-cleanup-string
  476. (or (org-entry-get nil "DESCRIPTION")
  477. (and org-icalendar-include-body (org-get-entry)))
  478. t org-icalendar-include-body)
  479. location (org-icalendar-cleanup-string
  480. (org-entry-get nil "LOCATION" 'selective))
  481. due (and (member 'todo-due org-icalendar-use-deadline)
  482. (org-entry-get nil "DEADLINE"))
  483. start (and (member 'todo-start org-icalendar-use-scheduled)
  484. (org-entry-get nil "SCHEDULED"))
  485. categories (org-export-get-categories)
  486. uid (if org-icalendar-store-UID
  487. (org-id-get-create)
  488. (or (org-id-get) (org-id-new))))
  489. (and due (setq due (org-icalendar-ts-to-string due "DUE")))
  490. (and start (setq start (org-icalendar-ts-to-string start "DTSTART")))
  491. (if (string-match org-bracket-link-regexp hd)
  492. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  493. (match-string 1 hd))
  494. t t hd)))
  495. (if (string-match org-priority-regexp hd)
  496. (setq pri (string-to-char (match-string 2 hd))
  497. hd (concat (substring hd 0 (match-beginning 1))
  498. (substring hd (match-end 1))))
  499. (setq pri org-default-priority))
  500. (setq pri (floor (- 9 (* 8. (/ (float (- org-lowest-priority pri))
  501. (- org-lowest-priority org-highest-priority))))))
  502. (princ (format "BEGIN:VTODO
  503. UID: %s
  504. %s
  505. SUMMARY:%s%s%s%s
  506. CATEGORIES:%s
  507. SEQUENCE:1
  508. PRIORITY:%d
  509. STATUS:%s
  510. END:VTODO\n"
  511. (concat prefix uid)
  512. (or start dts)
  513. (or summary hd)
  514. (if (and location (string-match "\\S-" location))
  515. (concat "\nLOCATION: " location) "")
  516. (if (and desc (string-match "\\S-" desc))
  517. (concat "\nDESCRIPTION: " desc) "")
  518. (if due (concat "\n" due) "")
  519. categories
  520. pri status)))))))))
  521. (defun org-export-get-categories ()
  522. "Get categories according to `org-icalendar-categories'."
  523. (let ((cs org-icalendar-categories) c rtn tmp)
  524. (while (setq c (pop cs))
  525. (cond
  526. ((eq c 'category) (push (org-get-category) rtn))
  527. ((eq c 'todo-state)
  528. (setq tmp (org-get-todo-state))
  529. (and tmp (push tmp rtn)))
  530. ((eq c 'local-tags)
  531. (setq rtn (append (nreverse (org-get-local-tags-at (point))) rtn)))
  532. ((eq c 'all-tags)
  533. (setq rtn (append (nreverse (org-get-tags-at (point))) rtn)))))
  534. (mapconcat 'identity (nreverse rtn) ",")))
  535. (defun org-icalendar-cleanup-string (s &optional is-body maxlength)
  536. "Take out stuff and quote what needs to be quoted.
  537. When IS-BODY is non-nil, assume that this is the body of an item, clean up
  538. whitespace, newlines, drawers, and timestamps, and cut it down to MAXLENGTH
  539. characters."
  540. (if (not s)
  541. nil
  542. (if is-body
  543. (let ((re (concat "\\(" org-drawer-regexp "\\)[^\000]*?:END:.*\n?"))
  544. (re2 (concat "^[ \t]*" org-keyword-time-regexp ".*\n?")))
  545. (while (string-match re s) (setq s (replace-match "" t t s)))
  546. (while (string-match re2 s) (setq s (replace-match "" t t s))))
  547. (setq s (replace-regexp-in-string "[[:space:]]+" " " s)))
  548. (let ((start 0))
  549. (while (string-match "\\([,;]\\)" s start)
  550. (setq start (+ (match-beginning 0) 2)
  551. s (replace-match "\\\\\\1" nil nil s))))
  552. (setq s (org-trim s))
  553. (when is-body
  554. (while (string-match "[ \t]*\n[ \t]*" s)
  555. (setq s (replace-match "\\n" t t s))))
  556. (if is-body
  557. (if maxlength
  558. (if (and (numberp maxlength)
  559. (> (length s) maxlength))
  560. (setq s (substring s 0 maxlength)))))
  561. s))
  562. (defun org-icalendar-cleanup-string-rfc2455 (s &optional is-body maxlength)
  563. "Take out stuff and quote what needs to be quoted.
  564. When IS-BODY is non-nil, assume that this is the body of an item, clean up
  565. whitespace, newlines, drawers, and timestamps, and cut it down to MAXLENGTH
  566. characters.
  567. This seems to be more like RFC 2455, but it causes problems, so it is
  568. not used right now."
  569. (if (not s)
  570. nil
  571. (if is-body
  572. (let ((re (concat "\\(" org-drawer-regexp "\\)[^\000]*?:END:.*\n?"))
  573. (re2 (concat "^[ \t]*" org-keyword-time-regexp ".*\n?")))
  574. (while (string-match re s) (setq s (replace-match "" t t s)))
  575. (while (string-match re2 s) (setq s (replace-match "" t t s)))
  576. (setq s (org-trim s))
  577. (while (string-match "[ \t]*\n[ \t]*" s)
  578. (setq s (replace-match "\\n" t t s)))
  579. (if maxlength
  580. (if (and (numberp maxlength)
  581. (> (length s) maxlength))
  582. (setq s (substring s 0 maxlength)))))
  583. (setq s (org-trim s)))
  584. (while (string-match "\"" s) (setq s (replace-match "''" t t s)))
  585. (when (string-match "[;,:]" s) (setq s (concat "\"" s "\"")))
  586. s))
  587. (defun org-icalendar-start-file (name)
  588. "Start an iCalendar file by inserting the header."
  589. (let ((user user-full-name)
  590. (name (or name "unknown"))
  591. (timezone (if (> (length org-icalendar-timezone) 0)
  592. org-icalendar-timezone
  593. (cadr (current-time-zone))))
  594. (description org-icalendar-combined-description))
  595. (princ
  596. (format "BEGIN:VCALENDAR
  597. VERSION:2.0
  598. X-WR-CALNAME:%s
  599. PRODID:-//%s//Emacs with Org-mode//EN
  600. X-WR-TIMEZONE:%s
  601. X-WR-CALDESC:%s
  602. CALSCALE:GREGORIAN\n" name user timezone description))))
  603. (defun org-icalendar-finish-file ()
  604. "Finish an iCalendar file by inserting the END statement."
  605. (princ "END:VCALENDAR\n"))
  606. (defun org-icalendar-ts-to-string (s keyword &optional inc)
  607. "Take a time string S and convert it to iCalendar format.
  608. KEYWORD is added in front, to make a complete line like DTSTART....
  609. When INC is non-nil, increase the hour by two (if time string contains
  610. a time), or the day by one (if it does not contain a time)."
  611. (let ((t1 (ignore-errors (org-parse-time-string s 'nodefault)))
  612. t2 fmt have-time time)
  613. (if (not t1)
  614. ""
  615. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  616. (setq t2 t1 have-time t)
  617. (setq t2 (org-parse-time-string s)))
  618. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  619. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  620. (when inc
  621. (if have-time
  622. (if org-agenda-default-appointment-duration
  623. (setq mi (+ org-agenda-default-appointment-duration mi))
  624. (setq h (+ 2 h)))
  625. (setq d (1+ d))))
  626. (setq time (encode-time s mi h d m y)))
  627. (setq fmt (if have-time
  628. (replace-regexp-in-string "%Z"
  629. org-icalendar-timezone
  630. org-icalendar-date-time-format t)
  631. ";VALUE=DATE:%Y%m%d"))
  632. (concat keyword (format-time-string fmt time
  633. (and (org-icalendar-use-UTC-date-timep)
  634. have-time))))))
  635. (provide 'org-icalendar)
  636. ;; Local variables:
  637. ;; generated-autoload-file: "org-loaddefs.el"
  638. ;; End:
  639. ;;; org-icalendar.el ends here