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