org-icalendar.el 22 KB

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