org-mac-iCal.el 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. ;;; org-mac-iCal.el --- Imports events from iCal.app to the Emacs diary
  2. ;; Copyright (C) 2009-2012 Christopher Suckling
  3. ;; Author: Christopher Suckling <suckling at gmail dot com>
  4. ;; This file is Free Software; you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation; either version 3, or (at your option)
  7. ;; any later version.
  8. ;; It is distributed in the hope that it will be useful, but WITHOUT
  9. ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. ;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  11. ;; License for more details.
  12. ;; You should have received a copy of the GNU General Public License
  13. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  14. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. ;; Boston, MA 02110-1301, USA.
  16. ;; Version: 0.1057.104
  17. ;; Keywords: outlines, calendar
  18. ;;; Commentary:
  19. ;;
  20. ;; This file provides the import of events from Mac OS X 10.5 iCal.app
  21. ;; into the Emacs diary (it is not compatible with OS X < 10.5). The
  22. ;; function org-mac-iCal will import events in all checked iCal.app
  23. ;; calendars for the date range org-mac-iCal-range months, centered
  24. ;; around the current date.
  25. ;;
  26. ;; CAVEAT: This function is destructive; it will overwrite the current
  27. ;; contents of the Emacs diary.
  28. ;;
  29. ;; Installation: add (require 'org-mac-iCal) to your .emacs.
  30. ;;
  31. ;; If you view Emacs diary entries in org-agenda, the following hook
  32. ;; will ensure that all-day events are not orphaned below TODO items
  33. ;; and that any supplementary fields to events (e.g. Location) are
  34. ;; grouped with their parent event
  35. ;;
  36. ;; (add-hook 'org-agenda-cleanup-fancy-diary-hook
  37. ;; (lambda ()
  38. ;; (goto-char (point-min))
  39. ;; (save-excursion
  40. ;; (while (re-search-forward "^[a-z]" nil t)
  41. ;; (goto-char (match-beginning 0))
  42. ;; (insert "0:00-24:00 ")))
  43. ;; (while (re-search-forward "^ [a-z]" nil t)
  44. ;; (goto-char (match-beginning 0))
  45. ;; (save-excursion
  46. ;; (re-search-backward "^[0-9]+:[0-9]+-[0-9]+:[0-9]+ " nil t))
  47. ;; (insert (match-string 0)))))
  48. ;;; Code:
  49. (defcustom org-mac-iCal-range 2
  50. "The range in months to import iCal.app entries into the Emacs
  51. diary. The import is centered around today's date; thus a value
  52. of 2 imports entries for one month before and one month after
  53. today's date"
  54. :group 'org-time
  55. :type 'integer)
  56. (defun org-mac-iCal ()
  57. "Selects checked calendars in iCal.app and imports them into
  58. the the Emacs diary"
  59. (interactive)
  60. ;; kill diary buffers then empty diary files to avoid duplicates
  61. (setq currentBuffer (buffer-name))
  62. (setq openBuffers (mapcar (function buffer-name) (buffer-list)))
  63. (omi-kill-diary-buffer openBuffers)
  64. (with-temp-buffer
  65. (insert-file-contents diary-file)
  66. (delete-region (point-min) (point-max))
  67. (write-region (point-min) (point-max) diary-file))
  68. ;; determine available calendars
  69. (setq caldav-folders (directory-files "~/Library/Calendars" 1 ".*caldav$"))
  70. (setq caldav-calendars nil)
  71. (mapc
  72. (lambda (x)
  73. (setq caldav-calendars (nconc caldav-calendars (directory-files x 1 ".*calendar$"))))
  74. caldav-folders)
  75. (setq local-calendars nil)
  76. (setq local-calendars (directory-files "~/Library/Calendars" 1 ".*calendar$"))
  77. (setq all-calendars (append caldav-calendars local-calendars))
  78. ;; parse each calendar's Info.plist to see if calendar is checked in iCal
  79. (setq all-calendars (delq 'nil (mapcar
  80. (lambda (x)
  81. (omi-checked x))
  82. all-calendars)))
  83. ;; for each calendar, concatenate individual events into a single ics file
  84. (with-temp-buffer
  85. (shell-command "sw_vers" (current-buffer))
  86. (when (re-search-backward "10\\.[567]" nil t)
  87. (omi-concat-leopard-ics all-calendars)))
  88. ;; move all caldav ics files to the same place as local ics files
  89. (mapc
  90. (lambda (x)
  91. (mapc
  92. (lambda (y)
  93. (rename-file (concat x "/" y);
  94. (concat "~/Library/Calendars/" y)))
  95. (directory-files x nil ".*ics$")))
  96. caldav-folders)
  97. ;; check calendar has contents and import
  98. (setq import-calendars (directory-files "~/Library/Calendars" 1 ".*ics$"))
  99. (mapc
  100. (lambda (x)
  101. (when (/= (nth 7 (file-attributes x 'string)) 0)
  102. (omi-import-ics x)))
  103. import-calendars)
  104. ;; tidy up intermediate files and buffers
  105. (setq usedCalendarsBuffers (mapcar (function buffer-name) (buffer-list)))
  106. (omi-kill-ics-buffer usedCalendarsBuffers)
  107. (setq usedCalendarsFiles (directory-files "~/Library/Calendars" 1 ".*ics$"))
  108. (omi-delete-ics-file usedCalendarsFiles)
  109. (org-pop-to-buffer-same-window currentBuffer))
  110. (defun omi-concat-leopard-ics (list)
  111. "Leopard stores each iCal.app event in a separate ics file.
  112. Whilst useful for Spotlight indexing, this is less helpful for
  113. icalendar-import-file. omi-concat-leopard-ics concatenates these
  114. individual event files into a single ics file"
  115. (mapc
  116. (lambda (x)
  117. (setq omi-leopard-events (directory-files (concat x "/Events") 1 ".*ics$"))
  118. (with-temp-buffer
  119. (mapc
  120. (lambda (y)
  121. (insert-file-contents (expand-file-name y)))
  122. omi-leopard-events)
  123. (write-region (point-min) (point-max) (concat (expand-file-name x) ".ics"))))
  124. list))
  125. (defun omi-import-ics (string)
  126. "Imports an ics file into the Emacs diary. First tidies up the
  127. ics file so that it is suitable for import and selects a sensible
  128. date range so that Emacs calendar view doesn't grind to a halt"
  129. (with-temp-buffer
  130. (insert-file-contents string)
  131. (goto-char (point-min))
  132. (while
  133. (re-search-forward "^BEGIN:VCALENDAR$" nil t)
  134. (setq startEntry (match-beginning 0))
  135. (re-search-forward "^END:VCALENDAR$" nil t)
  136. (setq endEntry (match-end 0))
  137. (save-restriction
  138. (narrow-to-region startEntry endEntry)
  139. (goto-char (point-min))
  140. (re-search-forward "\\(^DTSTART;.*:\\)\\([0-9][0-9][0-9][0-9]\\)\\([0-9][0-9]\\)" nil t)
  141. (if (or (eq (match-string 2) nil) (eq (match-string 3) nil))
  142. (progn
  143. (setq yearEntry 1)
  144. (setq monthEntry 1))
  145. (setq yearEntry (string-to-number (match-string 2)))
  146. (setq monthEntry (string-to-number (match-string 3))))
  147. (setq year (string-to-number (format-time-string "%Y")))
  148. (setq month (string-to-number (format-time-string "%m")))
  149. (setq now (list month 1 year))
  150. (setq entryDate (list monthEntry 1 yearEntry))
  151. ;; Check to see if this is a repeating event
  152. (goto-char (point-min))
  153. (setq isRepeating (re-search-forward "^RRULE:" nil t))
  154. ;; Delete if outside range and not repeating
  155. (when (and
  156. (not isRepeating)
  157. (> (abs (- (calendar-absolute-from-gregorian now)
  158. (calendar-absolute-from-gregorian entryDate)))
  159. (* (/ org-mac-iCal-range 2) 30))
  160. (delete-region startEntry endEntry)))
  161. (goto-char (point-max))))
  162. (while
  163. (re-search-forward "^END:VEVENT$" nil t)
  164. (delete-blank-lines))
  165. (goto-line 1)
  166. (insert "BEGIN:VCALENDAR\n\n")
  167. (goto-line 2)
  168. (while
  169. (re-search-forward "^BEGIN:VCALENDAR$" nil t)
  170. (replace-match "\n"))
  171. (goto-line 2)
  172. (while
  173. (re-search-forward "^END:VCALENDAR$" nil t)
  174. (replace-match "\n"))
  175. (insert "END:VCALENDAR")
  176. (goto-line 1)
  177. (delete-blank-lines)
  178. (while
  179. (re-search-forward "^END:VEVENT$" nil t)
  180. (delete-blank-lines))
  181. (goto-line 1)
  182. (while
  183. (re-search-forward "^ORG.*" nil t)
  184. (replace-match "\n"))
  185. (goto-line 1)
  186. (write-region (point-min) (point-max) string))
  187. (icalendar-import-file string diary-file))
  188. (defun omi-kill-diary-buffer (list)
  189. (mapc
  190. (lambda (x)
  191. (if (string-match "^diary" x)
  192. (kill-buffer x)))
  193. list))
  194. (defun omi-kill-ics-buffer (list)
  195. (mapc
  196. (lambda (x)
  197. (if (string-match "ics$" x)
  198. (kill-buffer x)))
  199. list))
  200. (defun omi-delete-ics-file (list)
  201. (mapc
  202. (lambda (x)
  203. (delete-file x))
  204. list))
  205. (defun omi-checked (directory)
  206. "Parse Info.plist in iCal.app calendar folder and determine
  207. whether Checked key is 1. If Checked key is not 1, remove
  208. calendar from list of calendars for import"
  209. (let* ((root (xml-parse-file (car (directory-files directory 1 "Info.plist"))))
  210. (plist (car root))
  211. (dict (car (xml-get-children plist 'dict)))
  212. (keys (cdr (xml-node-children dict)))
  213. (keys (mapcar
  214. (lambda (x)
  215. (cond ((listp x)
  216. x)))
  217. keys))
  218. (keys (delq 'nil keys)))
  219. (when (equal "1" (car (cddr (lax-plist-get keys '(key nil "Checked")))))
  220. directory)))
  221. (provide 'org-mac-iCal)
  222. ;;; org-mac-iCal.el ends here