org-icalendar.el 25 KB

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