org-icalendar.el 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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.4
  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-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. :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 of the empty string, use the abbreviation retrieved from Emacs."
  168. :group 'org-export-icalendar
  169. :type '(choice
  170. (const :tag "Unspecified" nil)
  171. (string :tag "Time zone")))
  172. (defcustom org-icalendar-use-UTC-date-time ()
  173. "Non-nil force the use of the universal time for iCalendar DATE-TIME.
  174. The iCalendar DATE-TIME can be expressed with local time or universal Time,
  175. universal time could be more compatible with some external tools."
  176. :group 'org-export-icalendar
  177. :type 'boolean)
  178. ;;; iCalendar export
  179. ;;;###autoload
  180. (defun org-export-icalendar-this-file ()
  181. "Export current file as an iCalendar file.
  182. The iCalendar file will be located in the same directory as the Org-mode
  183. file, but with extension `.ics'."
  184. (interactive)
  185. (org-export-icalendar nil buffer-file-name))
  186. ;;;###autoload
  187. (defun org-export-icalendar-all-agenda-files ()
  188. "Export all files in the variable `org-agenda-files' to iCalendar .ics files.
  189. Each iCalendar file will be located in the same directory as the Org-mode
  190. file, but with extension `.ics'."
  191. (interactive)
  192. (apply 'org-export-icalendar nil (org-agenda-files t)))
  193. ;;;###autoload
  194. (defun org-export-icalendar-combine-agenda-files ()
  195. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  196. The file is stored under the name `org-combined-agenda-icalendar-file'."
  197. (interactive)
  198. (apply 'org-export-icalendar t (org-agenda-files t)))
  199. (defun org-export-icalendar (combine &rest files)
  200. "Create iCalendar files for all elements of FILES.
  201. If COMBINE is non-nil, combine all calendar entries into a single large
  202. file and store it under the name `org-combined-agenda-icalendar-file'."
  203. (save-excursion
  204. (org-prepare-agenda-buffers files)
  205. (let* ((dir (org-export-directory
  206. :ical (list :publishing-directory
  207. org-export-publishing-directory)))
  208. file ical-file ical-buffer category started org-agenda-new-buffers)
  209. (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
  210. (when combine
  211. (setq ical-file
  212. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  213. org-combined-agenda-icalendar-file
  214. (expand-file-name org-combined-agenda-icalendar-file dir))
  215. ical-buffer (org-get-agenda-file-buffer ical-file))
  216. (set-buffer ical-buffer) (erase-buffer))
  217. (while (setq file (pop files))
  218. (catch 'nextfile
  219. (org-check-agenda-file file)
  220. (set-buffer (org-get-agenda-file-buffer file))
  221. (unless combine
  222. (setq ical-file (concat (file-name-as-directory dir)
  223. (file-name-sans-extension
  224. (file-name-nondirectory buffer-file-name))
  225. ".ics"))
  226. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  227. (with-current-buffer ical-buffer (erase-buffer)))
  228. (setq category (or org-category
  229. (file-name-sans-extension
  230. (file-name-nondirectory buffer-file-name))))
  231. (if (symbolp category) (setq category (symbol-name category)))
  232. (let ((standard-output ical-buffer))
  233. (if combine
  234. (and (not started) (setq started t)
  235. (org-start-icalendar-file org-icalendar-combined-name))
  236. (org-start-icalendar-file category))
  237. (org-print-icalendar-entries combine)
  238. (when (or (and combine (not files)) (not combine))
  239. (when (and combine org-icalendar-include-bbdb-anniversaries)
  240. (require 'org-bbdb)
  241. (org-bbdb-anniv-export-ical))
  242. (org-finish-icalendar-file)
  243. (set-buffer ical-buffer)
  244. (run-hooks 'org-before-save-iCalendar-file-hook)
  245. (save-buffer)
  246. (run-hooks 'org-after-save-iCalendar-file-hook)
  247. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  248. ))))
  249. (org-release-buffers org-agenda-new-buffers))))
  250. (defvar org-before-save-iCalendar-file-hook nil
  251. "Hook run before an iCalendar file has been saved.
  252. This can be used to modify the result of the export.")
  253. (defvar org-after-save-iCalendar-file-hook nil
  254. "Hook run after an iCalendar file has been saved.
  255. The iCalendar buffer is still current when this hook is run.
  256. A good way to use this is to tell a desktop calendar application to re-read
  257. the iCalendar file.")
  258. (defvar org-agenda-default-appointment-duration) ; defined in org-agenda.el
  259. (defun org-print-icalendar-entries (&optional combine)
  260. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  261. When COMBINE is non nil, add the category to each line."
  262. (require 'org-agenda)
  263. (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
  264. (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  265. (dts (org-ical-ts-to-string
  266. (format-time-string (cdr org-time-stamp-formats) (current-time))
  267. "DTSTART"))
  268. hd ts ts2 state status (inc t) pos b sexp rrule
  269. scheduledp deadlinep todo prefix due start tags
  270. tmp pri categories location summary desc uid alarm
  271. (sexp-buffer (get-buffer-create "*ical-tmp*")))
  272. (org-refresh-category-properties)
  273. (save-excursion
  274. (goto-char (point-min))
  275. (while (re-search-forward re1 nil t)
  276. (catch :skip
  277. (org-agenda-skip)
  278. (when org-icalendar-verify-function
  279. (unless (save-match-data (funcall org-icalendar-verify-function))
  280. (outline-next-heading)
  281. (backward-char 1)
  282. (throw :skip nil)))
  283. (setq pos (match-beginning 0)
  284. ts (match-string 0)
  285. tags (org-get-tags-at)
  286. inc t
  287. hd (condition-case nil
  288. (org-icalendar-cleanup-string
  289. (org-get-heading t))
  290. (error (throw :skip nil)))
  291. summary (org-icalendar-cleanup-string
  292. (org-entry-get nil "SUMMARY"))
  293. desc (org-icalendar-cleanup-string
  294. (or (org-entry-get nil "DESCRIPTION")
  295. (and org-icalendar-include-body (org-get-entry)))
  296. t org-icalendar-include-body)
  297. location (org-icalendar-cleanup-string
  298. (org-entry-get nil "LOCATION" 'selective))
  299. uid (if org-icalendar-store-UID
  300. (org-id-get-create)
  301. (or (org-id-get) (org-id-new)))
  302. categories (org-export-get-categories)
  303. alarm ""
  304. deadlinep nil scheduledp nil)
  305. (if (looking-at re2)
  306. (progn
  307. (goto-char (match-end 0))
  308. (setq ts2 (match-string 1)
  309. inc (not (string-match "[0-9]\\{1,2\\}:[0-9][0-9]" ts2))))
  310. (setq tmp (buffer-substring (max (point-min)
  311. (- pos org-ds-keyword-length))
  312. pos)
  313. ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
  314. (progn
  315. (setq inc nil)
  316. (replace-match "\\1" t nil ts))
  317. ts)
  318. deadlinep (string-match org-deadline-regexp tmp)
  319. scheduledp (string-match org-scheduled-regexp tmp)
  320. todo (org-get-todo-state)
  321. ;; donep (org-entry-is-done-p)
  322. ))
  323. (when (and (not org-icalendar-use-plain-timestamp)
  324. (not deadlinep) (not scheduledp))
  325. (throw :skip t))
  326. ;; don't export entries with a :noexport: tag
  327. (when (and org-icalendar-honor-noexport-tag
  328. (delq nil (mapcar (lambda(x)
  329. (member x org-export-exclude-tags)) tags)))
  330. (throw :skip t))
  331. (when (and
  332. deadlinep
  333. (if todo
  334. (not (memq 'event-if-todo org-icalendar-use-deadline))
  335. (not (memq 'event-if-not-todo org-icalendar-use-deadline))))
  336. (throw :skip t))
  337. (when (and
  338. scheduledp
  339. (if todo
  340. (not (memq 'event-if-todo org-icalendar-use-scheduled))
  341. (not (memq 'event-if-not-todo org-icalendar-use-scheduled))))
  342. (throw :skip t))
  343. (setq prefix (if deadlinep "DL-" (if scheduledp "SC-" "TS-")))
  344. (if (or (string-match org-tr-regexp hd)
  345. (string-match org-ts-regexp hd))
  346. (setq hd (replace-match "" t t hd)))
  347. (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts)
  348. (setq rrule
  349. (concat "\nRRULE:FREQ="
  350. (cdr (assoc
  351. (match-string 2 ts)
  352. '(("d" . "DAILY")("w" . "WEEKLY")
  353. ("m" . "MONTHLY")("y" . "YEARLY"))))
  354. ";INTERVAL=" (match-string 1 ts)))
  355. (setq rrule ""))
  356. (setq summary (or summary hd))
  357. ;; create an alarm entry if the entry is timed. this is not very general in that:
  358. ;; (a) only one alarm per entry is defined,
  359. ;; (b) only minutes are allowed for the trigger period ahead of the start time, and
  360. ;; (c) only a DISPLAY action is defined.
  361. ;; [ESF]
  362. (let ((t1 (ignore-errors (org-parse-time-string ts 'nodefault))))
  363. (if (and (> org-icalendar-alarm-time 0)
  364. (car t1) (nth 1 t1) (nth 2 t1))
  365. (setq alarm (format "\nBEGIN:VALARM\nACTION:DISPLAY\nDESCRIPTION:%s\nTRIGGER:-P0D0H%dM0S\nEND:VALARM" summary org-icalendar-alarm-time))
  366. (setq alarm ""))
  367. )
  368. (if (string-match org-bracket-link-regexp summary)
  369. (setq summary
  370. (replace-match (if (match-end 3)
  371. (match-string 3 summary)
  372. (match-string 1 summary))
  373. t t summary)))
  374. (if deadlinep (setq summary (concat "DL: " summary)))
  375. (if scheduledp (setq summary (concat "S: " summary)))
  376. (if (string-match "\\`<%%" ts)
  377. (with-current-buffer sexp-buffer
  378. (insert (substring ts 1 -1) " " summary "\n"))
  379. (princ (format "BEGIN:VEVENT
  380. UID: %s
  381. %s
  382. %s%s
  383. SUMMARY:%s%s%s
  384. CATEGORIES:%s%s
  385. END:VEVENT\n"
  386. (concat prefix uid)
  387. (org-ical-ts-to-string ts "DTSTART")
  388. (org-ical-ts-to-string ts2 "DTEND" inc)
  389. rrule summary
  390. (if (and desc (string-match "\\S-" desc))
  391. (concat "\nDESCRIPTION: " desc) "")
  392. (if (and location (string-match "\\S-" location))
  393. (concat "\nLOCATION: " location) "")
  394. categories
  395. alarm)))))
  396. (when (and org-icalendar-include-sexps
  397. (condition-case nil (require 'icalendar) (error nil))
  398. (fboundp 'icalendar-export-region))
  399. ;; Get all the literal sexps
  400. (goto-char (point-min))
  401. (while (re-search-forward "^&?%%(" nil t)
  402. (catch :skip
  403. (org-agenda-skip)
  404. (when org-icalendar-verify-function
  405. (unless (save-match-data (funcall org-icalendar-verify-function))
  406. (outline-next-heading)
  407. (backward-char 1)
  408. (throw :skip nil)))
  409. (setq b (match-beginning 0))
  410. (goto-char (1- (match-end 0)))
  411. (forward-sexp 1)
  412. (end-of-line 1)
  413. (setq sexp (buffer-substring b (point)))
  414. (with-current-buffer sexp-buffer
  415. (insert sexp "\n"))))
  416. (princ (org-diary-to-ical-string sexp-buffer))
  417. (kill-buffer sexp-buffer))
  418. (when org-icalendar-include-todo
  419. (setq prefix "TODO-")
  420. (goto-char (point-min))
  421. (while (re-search-forward org-complex-heading-regexp nil t)
  422. (catch :skip
  423. (org-agenda-skip)
  424. (when org-icalendar-verify-function
  425. (unless (save-match-data
  426. (funcall org-icalendar-verify-function))
  427. (outline-next-heading)
  428. (backward-char 1)
  429. (throw :skip nil)))
  430. (setq state (match-string 2))
  431. (setq status (if (member state org-done-keywords)
  432. "COMPLETED" "NEEDS-ACTION"))
  433. (when (and state
  434. (cond
  435. ;; check if the state is one we should use
  436. ((eq org-icalendar-include-todo 'all)
  437. ;; all should be included
  438. t)
  439. ((eq org-icalendar-include-todo 'unblocked)
  440. ;; only undone entries that are not blocked
  441. (and (member state org-not-done-keywords)
  442. (or (not org-blocker-hook)
  443. (save-match-data
  444. (run-hook-with-args-until-failure
  445. 'org-blocker-hook
  446. (list :type 'todo-state-change
  447. :position (point-at-bol)
  448. :from 'todo
  449. :to 'done))))))
  450. ((eq org-icalendar-include-todo t)
  451. ;; include everything that is not done
  452. (member state org-not-done-keywords))))
  453. (setq hd (match-string 4)
  454. summary (org-icalendar-cleanup-string
  455. (org-entry-get nil "SUMMARY"))
  456. desc (org-icalendar-cleanup-string
  457. (or (org-entry-get nil "DESCRIPTION")
  458. (and org-icalendar-include-body (org-get-entry)))
  459. t org-icalendar-include-body)
  460. location (org-icalendar-cleanup-string
  461. (org-entry-get nil "LOCATION" 'selective))
  462. due (and (member 'todo-due org-icalendar-use-deadline)
  463. (org-entry-get nil "DEADLINE"))
  464. start (and (member 'todo-start org-icalendar-use-scheduled)
  465. (org-entry-get nil "SCHEDULED"))
  466. categories (org-export-get-categories)
  467. uid (if org-icalendar-store-UID
  468. (org-id-get-create)
  469. (or (org-id-get) (org-id-new))))
  470. (and due (setq due (org-ical-ts-to-string due "DUE")))
  471. (and start (setq start (org-ical-ts-to-string start "DTSTART")))
  472. (if (string-match org-bracket-link-regexp hd)
  473. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  474. (match-string 1 hd))
  475. t t hd)))
  476. (if (string-match org-priority-regexp hd)
  477. (setq pri (string-to-char (match-string 2 hd))
  478. hd (concat (substring hd 0 (match-beginning 1))
  479. (substring hd (match-end 1))))
  480. (setq pri org-default-priority))
  481. (setq pri (floor (- 9 (* 8. (/ (float (- org-lowest-priority pri))
  482. (- org-lowest-priority org-highest-priority))))))
  483. (princ (format "BEGIN:VTODO
  484. UID: %s
  485. %s
  486. SUMMARY:%s%s%s%s
  487. CATEGORIES:%s
  488. SEQUENCE:1
  489. PRIORITY:%d
  490. STATUS:%s
  491. END:VTODO\n"
  492. (concat prefix uid)
  493. (or start dts)
  494. (or summary hd)
  495. (if (and location (string-match "\\S-" location))
  496. (concat "\nLOCATION: " location) "")
  497. (if (and desc (string-match "\\S-" desc))
  498. (concat "\nDESCRIPTION: " desc) "")
  499. (if due (concat "\n" due) "")
  500. categories
  501. pri status)))))))))
  502. (defun org-export-get-categories ()
  503. "Get categories according to `org-icalendar-categories'."
  504. (let ((cs org-icalendar-categories) c rtn tmp)
  505. (while (setq c (pop cs))
  506. (cond
  507. ((eq c 'category) (push (org-get-category) rtn))
  508. ((eq c 'todo-state)
  509. (setq tmp (org-get-todo-state))
  510. (and tmp (push tmp rtn)))
  511. ((eq c 'local-tags)
  512. (setq rtn (append (nreverse (org-get-local-tags-at (point))) rtn)))
  513. ((eq c 'all-tags)
  514. (setq rtn (append (nreverse (org-get-tags-at (point))) rtn)))))
  515. (mapconcat 'identity (nreverse rtn) ",")))
  516. (defun org-icalendar-cleanup-string (s &optional is-body maxlength)
  517. "Take out stuff and quote what needs to be quoted.
  518. When IS-BODY is non-nil, assume that this is the body of an item, clean up
  519. whitespace, newlines, drawers, and timestamps, and cut it down to MAXLENGTH
  520. characters."
  521. (if (not s)
  522. nil
  523. (if is-body
  524. (let ((re (concat "\\(" org-drawer-regexp "\\)[^\000]*?:END:.*\n?"))
  525. (re2 (concat "^[ \t]*" org-keyword-time-regexp ".*\n?")))
  526. (while (string-match re s) (setq s (replace-match "" t t s)))
  527. (while (string-match re2 s) (setq s (replace-match "" t t s))))
  528. (setq s (replace-regexp-in-string "[[:space:]]+" " " s)))
  529. (let ((start 0))
  530. (while (string-match "\\([,;]\\)" s start)
  531. (setq start (+ (match-beginning 0) 2)
  532. s (replace-match "\\\\\\1" nil nil s))))
  533. (setq s (org-trim s))
  534. (when is-body
  535. (while (string-match "[ \t]*\n[ \t]*" s)
  536. (setq s (replace-match "\\n" t t s))))
  537. (if is-body
  538. (if maxlength
  539. (if (and (numberp maxlength)
  540. (> (length s) maxlength))
  541. (setq s (substring s 0 maxlength)))))
  542. s))
  543. (defun org-icalendar-cleanup-string-rfc2455 (s &optional is-body maxlength)
  544. "Take out stuff and quote what needs to be quoted.
  545. When IS-BODY is non-nil, assume that this is the body of an item, clean up
  546. whitespace, newlines, drawers, and timestamps, and cut it down to MAXLENGTH
  547. characters.
  548. This seems to be more like RFC 2455, but it causes problems, so it is
  549. not used right now."
  550. (if (not s)
  551. nil
  552. (if is-body
  553. (let ((re (concat "\\(" org-drawer-regexp "\\)[^\000]*?:END:.*\n?"))
  554. (re2 (concat "^[ \t]*" org-keyword-time-regexp ".*\n?")))
  555. (while (string-match re s) (setq s (replace-match "" t t s)))
  556. (while (string-match re2 s) (setq s (replace-match "" t t s)))
  557. (setq s (org-trim s))
  558. (while (string-match "[ \t]*\n[ \t]*" s)
  559. (setq s (replace-match "\\n" t t s)))
  560. (if maxlength
  561. (if (and (numberp maxlength)
  562. (> (length s) maxlength))
  563. (setq s (substring s 0 maxlength)))))
  564. (setq s (org-trim s)))
  565. (while (string-match "\"" s) (setq s (replace-match "''" t t s)))
  566. (when (string-match "[;,:]" s) (setq s (concat "\"" s "\"")))
  567. s))
  568. (defun org-start-icalendar-file (name)
  569. "Start an iCalendar file by inserting the header."
  570. (let ((user user-full-name)
  571. (name (or name "unknown"))
  572. (timezone (if (> (length org-icalendar-timezone) 0)
  573. org-icalendar-timezone
  574. (cadr (current-time-zone))))
  575. (description org-icalendar-combined-description))
  576. (princ
  577. (format "BEGIN:VCALENDAR
  578. VERSION:2.0
  579. X-WR-CALNAME:%s
  580. PRODID:-//%s//Emacs with Org-mode//EN
  581. X-WR-TIMEZONE:%s
  582. X-WR-CALDESC:%s
  583. CALSCALE:GREGORIAN\n" name user timezone description))))
  584. (defun org-finish-icalendar-file ()
  585. "Finish an iCalendar file by inserting the END statement."
  586. (princ "END:VCALENDAR\n"))
  587. (defun org-ical-ts-to-string (s keyword &optional inc)
  588. "Take a time string S and convert it to iCalendar format.
  589. KEYWORD is added in front, to make a complete line like DTSTART....
  590. When INC is non-nil, increase the hour by two (if time string contains
  591. a time), or the day by one (if it does not contain a time)."
  592. (let ((t1 (ignore-errors (org-parse-time-string s 'nodefault)))
  593. t2 fmt have-time time)
  594. (if (not t1)
  595. ""
  596. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  597. (setq t2 t1 have-time t)
  598. (setq t2 (org-parse-time-string s)))
  599. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  600. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  601. (when inc
  602. (if have-time
  603. (if org-agenda-default-appointment-duration
  604. (setq mi (+ org-agenda-default-appointment-duration mi))
  605. (setq h (+ 2 h)))
  606. (setq d (1+ d))))
  607. (setq time (encode-time s mi h d m y)))
  608. (setq fmt (if have-time (if org-icalendar-use-UTC-date-time
  609. ":%Y%m%dT%H%M%SZ"
  610. ":%Y%m%dT%H%M%S")
  611. ";VALUE=DATE:%Y%m%d"))
  612. (concat keyword (format-time-string fmt time
  613. (and org-icalendar-use-UTC-date-time
  614. have-time))))))
  615. (provide 'org-icalendar)
  616. ;; arch-tag: 2dee2b6e-9211-4aee-8a47-a3c7e5bc30cf
  617. ;;; org-icalendar.el ends here