org-icalendar.el 22 KB

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