org-icalendar.el 23 KB

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