org-icalendar.el 22 KB

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