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)
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
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
45 ;; (goto-char (point-min))
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))
53 ;; (re-search-backward "^[0-9]+:[0-9]+-[0-9]+:[0-9]+ " nil t))
54 ;; (insert (match-string 0)))))
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
66 (defun org-mac-iCal ()
67 "Selects checked calendars in iCal.app and imports them into
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
)
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
)
85 (setq caldav-calendars
(nconc caldav-calendars
(directory-files x
1 ".*calendar$"))))
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
99 ;; for each calendar, concatenate individual events into a single ics file
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
110 (rename-file (concat x
"/" y
);
111 (concat "~/Library/Calendars/" y
)))
112 (directory-files x nil
".*ics$")))
115 ;; check calendar has contents and import
116 (setq import-calendars
(directory-files "~/Library/Calendars" 1 ".*ics$"))
119 (when (/= (nth 7 (file-attributes x
'string
)) 0)
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"
138 (setq omi-leopard-events
(directory-files (concat x
"/Events") 1 ".*ics$"))
142 (insert-file-contents (expand-file-name y
)))
144 (write-region (point-min) (point-max) (concat (expand-file-name x
) ".ics"))))
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"
152 (insert-file-contents string
)
153 (goto-char (point-min))
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))
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
))
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")))
174 (or (< monthEntry
(- month
(/ org-mac-iCal-range
2))) (> monthEntry
(+ month
(/ org-mac-iCal-range
2)))))
175 (< yearEntry
(- year
1))
176 (> yearEntry
(+ year
1))
178 (= yearEntry
(- year
1)) (/= monthEntry
12))
180 (= yearEntry
(+ year
1)) (/= monthEntry
1)))
181 (delete-region startEntry endEntry
))))
183 (re-search-forward "^END:VEVENT$" nil t
)
184 (delete-blank-lines))
186 (insert "BEGIN:VCALENDAR\n\n")
189 (re-search-forward "^BEGIN:VCALENDAR$" nil t
)
190 (replace-match "\n"))
193 (re-search-forward "^END:VCALENDAR$" nil t
)
194 (replace-match "\n"))
195 (insert "END:VCALENDAR")
199 (re-search-forward "^END:VEVENT$" nil t
)
200 (delete-blank-lines))
203 (re-search-forward "^ORG.*" nil t
)
204 (replace-match "\n"))
206 (write-region (point-min) (point-max) string
))
208 (icalendar-import-file string diary-file
))
210 (defun omi-kill-diary-buffer (list)
213 (if (string-match "^diary" x
)
217 (defun omi-kill-ics-buffer (list)
220 (if (string-match "ics$" x
)
224 (defun omi-delete-ics-file (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"))))
236 (dict (car (xml-get-children plist
'dict
)))
237 (keys (cdr (xml-node-children dict
)))
243 (keys (delq 'nil keys
)))
244 (when (equal "1" (car (cddr (lax-plist-get keys
'(key nil
"Checked")))))
247 (provide 'org-mac-iCal
)
249 ;;; org-mac-iCal.el ends here