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
  25. (require 'cl))
  26. (declare-function org-bbdb-anniv-export-ical "org-bbdb" nil)
  27. (defgroup org-export-icalendar nil
  28. "Options specific for iCalendar export of Org-mode files."
  29. :tag "Org Export iCalendar"
  30. :group 'org-export)
  31. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  32. "The file name for the iCalendar file covering all agenda files.
  33. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  34. The file name should be absolute, the file will be overwritten without warning."
  35. :group 'org-export-icalendar
  36. :type 'file)
  37. (defcustom org-icalendar-alarm-time 0
  38. "Number of minutes for triggering an alarm for exported timed events.
  39. A zero value (the default) turns off the definition of an alarm trigger
  40. for timed events. If non-zero, alarms are created.
  41. - a single alarm per entry is defined
  42. - The alarm will go off N minutes before the event
  43. - only a DISPLAY action is defined."
  44. :group 'org-export-icalendar
  45. :version "24.1"
  46. :type 'integer)
  47. (defcustom org-icalendar-combined-name "OrgMode"
  48. "Calendar name for the combined iCalendar representing all agenda files."
  49. :group 'org-export-icalendar
  50. :type 'string)
  51. (defcustom org-icalendar-combined-description nil
  52. "Calendar description for the combined iCalendar (all agenda files)."
  53. :group 'org-export-icalendar
  54. :version "24.1"
  55. :type 'string)
  56. (defcustom org-icalendar-use-plain-timestamp t
  57. "Non-nil means make an event from every plain time stamp."
  58. :group 'org-export-icalendar
  59. :type 'boolean)
  60. (defcustom org-icalendar-honor-noexport-tag nil
  61. "Non-nil means don't export entries with a tag in `org-export-exclude-tags'."
  62. :group 'org-export-icalendar
  63. :version "24.1"
  64. :type 'boolean)
  65. (defcustom org-icalendar-use-deadline '(event-if-not-todo todo-due)
  66. "Contexts where iCalendar export should use a deadline time stamp.
  67. This is a list with several symbols in it. Valid symbol are:
  68. event-if-todo Deadlines in TODO entries become calendar events.
  69. event-if-not-todo Deadlines in non-TODO entries become calendar events.
  70. todo-due Use deadlines in TODO entries as due-dates"
  71. :group 'org-export-icalendar
  72. :type '(set :greedy t
  73. (const :tag "Deadlines in non-TODO entries become events"
  74. event-if-not-todo)
  75. (const :tag "Deadline in TODO entries become events"
  76. event-if-todo)
  77. (const :tag "Deadlines in TODO entries become due-dates"
  78. todo-due)))
  79. (defcustom org-icalendar-use-scheduled '(todo-start)
  80. "Contexts where iCalendar export should use a scheduling time stamp.
  81. This is a list with several symbols in it. Valid symbol are:
  82. event-if-todo Scheduling time stamps in TODO entries become an event.
  83. event-if-not-todo Scheduling time stamps in non-TODO entries become an event.
  84. todo-start Scheduling time stamps in TODO entries become start date.
  85. Some calendar applications show TODO entries only after
  86. that date."
  87. :group 'org-export-icalendar
  88. :type '(set :greedy t
  89. (const :tag
  90. "SCHEDULED timestamps in non-TODO entries become events"
  91. event-if-not-todo)
  92. (const :tag "SCHEDULED timestamps in TODO entries become events"
  93. event-if-todo)
  94. (const :tag "SCHEDULED in TODO entries become start date"
  95. todo-start)))
  96. (defcustom org-icalendar-categories '(local-tags category)
  97. "Items that should be entered into the categories field.
  98. This is a list of symbols, the following are valid:
  99. category The Org-mode category of the current file or tree
  100. todo-state The todo state, if any
  101. local-tags The tags, defined in the current line
  102. all-tags All tags, including inherited ones."
  103. :group 'org-export-icalendar
  104. :type '(repeat
  105. (choice
  106. (const :tag "The file or tree category" category)
  107. (const :tag "The TODO state" todo-state)
  108. (const :tag "Tags defined in current line" local-tags)
  109. (const :tag "All tags, including inherited ones" all-tags))))
  110. (defcustom org-icalendar-include-todo nil
  111. "Non-nil means export to iCalendar files should also cover TODO items.
  112. Valid values are:
  113. nil don't include any TODO items
  114. t include all TODO items that are not in a DONE state
  115. unblocked include all TODO items that are not blocked
  116. all include both done and not done items."
  117. :group 'org-export-icalendar
  118. :type '(choice
  119. (const :tag "None" nil)
  120. (const :tag "Unfinished" t)
  121. (const :tag "Unblocked" unblocked)
  122. (const :tag "All" all)))
  123. (defvar org-icalendar-verify-function nil
  124. "Function to verify entries for iCalendar export.
  125. This can be set to a function that will be called at each entry that
  126. is considered for export to iCalendar. When the function returns nil,
  127. the entry will be skipped. When it returns a non-nil value, the entry
  128. will be considered for export.
  129. This is used internally when an agenda buffer is exported to an ics file,
  130. to make sure that only entries currently listed in the agenda will end
  131. up in the ics file. But for normal iCalendar export, you can use this
  132. for whatever you need.")
  133. (defcustom org-icalendar-include-bbdb-anniversaries nil
  134. "Non-nil means a combined iCalendar files should include anniversaries.
  135. The anniversaries are define in the BBDB database."
  136. :group 'org-export-icalendar
  137. :type 'boolean)
  138. (defcustom org-icalendar-include-sexps t
  139. "Non-nil means export to iCalendar files should also cover sexp entries.
  140. These are entries like in the diary, but directly in an Org-mode file."
  141. :group 'org-export-icalendar
  142. :type 'boolean)
  143. (defcustom org-icalendar-include-body 100
  144. "Amount of text below headline to be included in iCalendar export.
  145. This is a number of characters that should maximally be included.
  146. Properties, scheduling and clocking lines will always be removed.
  147. The text will be inserted into the DESCRIPTION field."
  148. :group 'org-export-icalendar
  149. :type '(choice
  150. (const :tag "Nothing" nil)
  151. (const :tag "Everything" t)
  152. (integer :tag "Max characters")))
  153. (defcustom org-icalendar-store-UID nil
  154. "Non-nil means store any created UIDs in properties.
  155. The iCalendar standard requires that all entries have a unique identifier.
  156. Org will create these identifiers as needed. When this variable is non-nil,
  157. the created UIDs will be stored in the ID property of the entry. Then the
  158. next time this entry is exported, it will be exported with the same UID,
  159. superseding the previous form of it. This is essential for
  160. synchronization services.
  161. This variable is not turned on by default because we want to avoid creating
  162. a property drawer in every entry if people are only playing with this feature,
  163. or if they are only using it locally."
  164. :group 'org-export-icalendar
  165. :type 'boolean)
  166. (defcustom org-icalendar-timezone (getenv "TZ")
  167. "The time zone string for iCalendar export.
  168. When nil of the empty string, use the abbreviation retrieved from Emacs."
  169. :group 'org-export-icalendar
  170. :type '(choice
  171. (const :tag "Unspecified" nil)
  172. (string :tag "Time zone")))
  173. ;; Backward compatibility with previous variable
  174. (defvar org-icalendar-use-UTC-date-time nil)
  175. (defcustom org-icalendar-date-time-format
  176. (if org-icalendar-use-UTC-date-time
  177. ":%Y%m%dT%H%M%SZ"
  178. ":%Y%m%dT%H%M%S")
  179. "Format-string for exporting icalendar DATE-TIME.
  180. See `format-time-string' for a full documentation. The only
  181. difference is that `org-icalendar-timezone' is used for %Z.
  182. Interesting value are:
  183. - \":%Y%m%dT%H%M%S\" for local time
  184. - \";TZID=%Z:%Y%m%dT%H%M%S\" for local time with explicit timezone
  185. - \":%Y%m%dT%H%M%SZ\" for time expressed in Universal Time"
  186. :group 'org-export-icalendar
  187. :version "24.1"
  188. :type '(choice
  189. (const :tag "Local time" ":%Y%m%dT%H%M%S")
  190. (const :tag "Explicit local time" ";TZID=%Z:%Y%m%dT%H%M%S")
  191. (const :tag "Universal time" ":%Y%m%dT%H%M%SZ")
  192. (string :tag "Explicit format")))
  193. (defun org-icalendar-use-UTC-date-timep ()
  194. (char-equal (elt org-icalendar-date-time-format
  195. (1- (length org-icalendar-date-time-format))) ?Z))
  196. ;;; iCalendar export
  197. ;;;###autoload
  198. (defun org-export-icalendar-this-file ()
  199. "Export current file as an iCalendar file.
  200. The iCalendar file will be located in the same directory as the Org-mode
  201. file, but with extension `.ics'."
  202. (interactive)
  203. (org-export-icalendar nil buffer-file-name))
  204. ;;;###autoload
  205. (defun org-export-icalendar-all-agenda-files ()
  206. "Export all files in the variable `org-agenda-files' to iCalendar .ics files.
  207. Each iCalendar file will be located in the same directory as the Org-mode
  208. file, but with extension `.ics'."
  209. (interactive)
  210. (apply 'org-export-icalendar nil (org-agenda-files t)))
  211. ;;;###autoload
  212. (defun org-export-icalendar-combine-agenda-files ()
  213. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  214. The file is stored under the name `org-combined-agenda-icalendar-file'."
  215. (interactive)
  216. (apply 'org-export-icalendar t (org-agenda-files t)))
  217. (defun org-export-icalendar (combine &rest files)
  218. "Create iCalendar files for all elements of FILES.
  219. If COMBINE is non-nil, combine all calendar entries into a single large
  220. file and store it under the name `org-combined-agenda-icalendar-file'."
  221. (save-excursion
  222. (org-prepare-agenda-buffers files)
  223. (let* ((dir (org-export-directory
  224. :ical (list :publishing-directory
  225. org-export-publishing-directory)))
  226. file ical-file ical-buffer category started org-agenda-new-buffers)
  227. (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
  228. (when combine
  229. (setq ical-file
  230. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  231. org-combined-agenda-icalendar-file
  232. (expand-file-name org-combined-agenda-icalendar-file dir))
  233. ical-buffer (org-get-agenda-file-buffer ical-file))
  234. (set-buffer ical-buffer) (erase-buffer))
  235. (while (setq file (pop files))
  236. (catch 'nextfile
  237. (org-check-agenda-file file)
  238. (set-buffer (org-get-agenda-file-buffer file))
  239. (unless combine
  240. (setq ical-file (concat (file-name-as-directory dir)
  241. (file-name-sans-extension
  242. (file-name-nondirectory buffer-file-name))
  243. ".ics"))
  244. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  245. (with-current-buffer ical-buffer (erase-buffer)))
  246. (setq category (or org-category
  247. (file-name-sans-extension
  248. (file-name-nondirectory buffer-file-name))))
  249. (if (symbolp category) (setq category (symbol-name category)))
  250. (let ((standard-output ical-buffer))
  251. (if combine
  252. (and (not started) (setq started t)
  253. (org-start-icalendar-file org-icalendar-combined-name))
  254. (org-start-icalendar-file category))
  255. (org-print-icalendar-entries combine)
  256. (when (or (and combine (not files)) (not combine))
  257. (when (and combine org-icalendar-include-bbdb-anniversaries)
  258. (require 'org-bbdb)
  259. (org-bbdb-anniv-export-ical))
  260. (org-finish-icalendar-file)
  261. (set-buffer ical-buffer)
  262. (run-hooks 'org-before-save-iCalendar-file-hook)
  263. (save-buffer)
  264. (run-hooks 'org-after-save-iCalendar-file-hook)
  265. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  266. ))))
  267. (org-release-buffers org-agenda-new-buffers))))
  268. (defvar org-before-save-iCalendar-file-hook nil
  269. "Hook run before an iCalendar file has been saved.
  270. This can be used to modify the result of the export.")
  271. (defvar org-after-save-iCalendar-file-hook nil
  272. "Hook run after an iCalendar file has been saved.
  273. The iCalendar buffer is still current when this hook is run.
  274. A good way to use this is to tell a desktop calendar application to re-read
  275. the iCalendar file.")
  276. (defvar org-agenda-default-appointment-duration) ; defined in org-agenda.el
  277. (defun org-print-icalendar-entries (&optional combine)
  278. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  279. When COMBINE is non nil, add the category to each line."
  280. (require 'org-agenda)
  281. (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
  282. (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  283. (dts (org-ical-ts-to-string
  284. (format-time-string (cdr org-time-stamp-formats) (current-time))
  285. "DTSTART"))
  286. hd ts ts2 state status (inc t) pos b sexp rrule
  287. scheduledp deadlinep todo prefix due start tags
  288. tmp pri categories location summary desc uid alarm
  289. (sexp-buffer (get-buffer-create "*ical-tmp*")))
  290. (org-refresh-category-properties)
  291. (save-excursion
  292. (goto-char (point-min))
  293. (while (re-search-forward re1 nil t)
  294. (catch :skip
  295. (org-agenda-skip)
  296. (when org-icalendar-verify-function
  297. (unless (save-match-data (funcall org-icalendar-verify-function))
  298. (outline-next-heading)
  299. (backward-char 1)
  300. (throw :skip nil)))
  301. (setq pos (match-beginning 0)
  302. ts (match-string 0)
  303. tags (org-get-tags-at)
  304. inc t
  305. hd (condition-case nil
  306. (org-icalendar-cleanup-string
  307. (org-get-heading t))
  308. (error (throw :skip nil)))
  309. summary (org-icalendar-cleanup-string
  310. (org-entry-get nil "SUMMARY"))
  311. desc (org-icalendar-cleanup-string
  312. (or (org-entry-get nil "DESCRIPTION")
  313. (and org-icalendar-include-body (org-get-entry)))
  314. t org-icalendar-include-body)
  315. location (org-icalendar-cleanup-string
  316. (org-entry-get nil "LOCATION" 'selective))
  317. uid (if org-icalendar-store-UID
  318. (org-id-get-create)
  319. (or (org-id-get) (org-id-new)))
  320. categories (org-export-get-categories)
  321. alarm ""
  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]+\\)\\([dwmy]\\)>" ts)
  366. (setq rrule
  367. (concat "\nRRULE:FREQ="
  368. (cdr (assoc
  369. (match-string 2 ts)
  370. '(("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 (> 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" summary org-icalendar-alarm-time))
  384. (setq alarm ""))
  385. )
  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-ical-ts-to-string ts "DTSTART")
  409. (org-ical-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-ical-ts-to-string due "DUE")))
  492. (and start (setq start (org-ical-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-start-icalendar-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-finish-icalendar-file ()
  606. "Finish an iCalendar file by inserting the END statement."
  607. (princ "END:VCALENDAR\n"))
  608. (defun org-ical-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