org-icalendar.el 23 KB

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