org-icalendar.el 26 KB

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