org-icalendar.el 23 KB

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