org-icalendar.el 26 KB

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