org-babel: use %s to format inline result when it is a string.
[rgr-org-mode.git] / contrib / lisp / org-mac-iCal.el
blob056d73dd2112c98fb143bf1dde2f8a2723d92470
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 caledar, 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 any caldav ics files to the same place as local ics files
106 (mapc
107 (lambda (x)
108 (when (directory-files x 1 ".*ics$")
109 (rename-file (car (directory-files x 1 ".*ics$")) (concat "~/Library/Calendars/" (car (directory-files x nil ".*ics$"))))))
110 caldav-folders)
112 ;; check calendar has contents and import
113 (setq import-calendars (directory-files "~/Library/Calendars" 1 ".*ics$"))
114 (mapc
115 (lambda (x)
116 (when (/= (nth 7 (file-attributes x 'string)) 0)
117 (omi-import-ics x)))
118 import-calendars)
120 ;; tidy up intermediate files and buffers
121 (setq usedCalendarsBuffers (mapcar (function buffer-name) (buffer-list)))
122 (omi-kill-ics-buffer usedCalendarsBuffers)
123 (setq usedCalendarsFiles (directory-files "~/Library/Calendars" 1 ".*ics$"))
124 (omi-delete-ics-file usedCalendarsFiles)
126 (switch-to-buffer currentBuffer))
128 (defun omi-concat-leopard-ics (list)
129 "Leopard stores each iCal.app event in a separate ics file.
130 Whilst useful for Spotlight indexing, this is less helpful for
131 icalendar-import-file. omi-concat-leopard-ics concatenates these
132 individual event files into a single ics file"
133 (mapc
134 (lambda (x)
135 (setq omi-leopard-events (directory-files (concat x "/Events") 1 ".*ics$"))
136 (with-temp-buffer
137 (mapc
138 (lambda (y)
139 (insert-file-contents (expand-file-name y)))
140 omi-leopard-events)
141 (write-region (point-min) (point-max) (concat (expand-file-name x) ".ics"))))
142 list))
144 (defun omi-import-ics (string)
145 "Imports an ics file into the Emacs diary. First tidies up the
146 ics file so that it is suitable for import and selects a sensible
147 date range so that Emacs calendar view doesn't grind to a halt"
148 (with-temp-buffer
149 (insert-file-contents string)
150 (goto-char (point-min))
151 (while
152 (re-search-forward "^BEGIN:VCALENDAR$" nil t)
153 (setq startEntry (match-beginning 0))
154 (re-search-forward "^END:VCALENDAR$" nil t)
155 (setq endEntry (match-end 0))
156 (save-restriction
157 (narrow-to-region startEntry endEntry)
158 (goto-char (point-min))
159 (re-search-forward "\\(^DTSTART;.*:\\)\\([0-9][0-9][0-9][0-9]\\)\\([0-9][0-9]\\)" nil t)
160 (if (or (eq (match-string 2) nil) (eq (match-string 3) nil))
161 (progn
162 (setq yearEntry 0)
163 (setq monthEntry 0))
164 (setq yearEntry (string-to-number (match-string 2)))
165 (setq monthEntry (string-to-number (match-string 3))))
166 (setq year (string-to-number (format-time-string "%Y")))
167 (setq month (string-to-number (format-time-string "%m")))
168 (when (or
169 (and
170 (= yearEntry year)
171 (or (< monthEntry (- month (/ org-mac-iCal-range 2))) (> monthEntry (+ month (/ org-mac-iCal-range 2)))))
172 (< yearEntry (- year 1))
173 (> yearEntry (+ year 1))
174 (and
175 (= yearEntry (- year 1)) (/= monthEntry 12))
176 (and
177 (= yearEntry (+ year 1)) (/= monthEntry 1)))
178 (delete-region startEntry endEntry))))
179 (while
180 (re-search-forward "^END:VEVENT$" nil t)
181 (delete-blank-lines))
182 (goto-line 1)
183 (insert "BEGIN:VCALENDAR\n\n")
184 (goto-line 2)
185 (while
186 (re-search-forward "^BEGIN:VCALENDAR$" nil t)
187 (replace-match "\n"))
188 (goto-line 2)
189 (while
190 (re-search-forward "^END:VCALENDAR$" nil t)
191 (replace-match "\n"))
192 (insert "END:VCALENDAR")
193 (goto-line 1)
194 (delete-blank-lines)
195 (while
196 (re-search-forward "^END:VEVENT$" nil t)
197 (delete-blank-lines))
198 (goto-line 1)
199 (while
200 (re-search-forward "^ORG.*" nil t)
201 (replace-match "\n"))
202 (goto-line 1)
203 (write-region (point-min) (point-max) string))
205 (icalendar-import-file string (expand-file-name "~/.emacs.d/diary")))
207 (defun omi-kill-diary-buffer (list)
208 (mapc
209 (lambda (x)
210 (if (string-match "^diary" x)
211 (kill-buffer x)))
212 list))
214 (defun omi-kill-ics-buffer (list)
215 (mapc
216 (lambda (x)
217 (if (string-match "ics$" x)
218 (kill-buffer x)))
219 list))
221 (defun omi-delete-ics-file (list)
222 (mapc
223 (lambda (x)
224 (delete-file x))
225 list))
227 (defun omi-checked (directory)
228 "Parse Info.plist in iCal.app calendar folder and determine
229 whether Checked key is 1. If Checked key is not 1, remove
230 calendar from list of calendars for import"
231 (let* ((root (xml-parse-file (car (directory-files directory 1 "Info.plist"))))
232 (plist (car root))
233 (dict (car (xml-get-children plist 'dict)))
234 (keys (cdr (xml-node-children dict)))
235 (keys (mapcar
236 (lambda (x)
237 (cond ((listp x)
238 x)))
239 keys))
240 (keys (delq 'nil keys)))
241 (when (equal "1" (car (cddr (lax-plist-get keys '(key nil "Checked")))))
242 directory)))
244 (provide 'org-mac-iCal)
246 ;;; org-mac-iCal.el ends here