org-icalendar.el 23 KB

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