Commit | Line | Data |
---|---|---|
8d642074 | 1 | ;;; org-mobile.el --- Code for asymmetric sync with a mobile device |
5df4f04c | 2 | ;; Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc. |
8d642074 CD |
3 | ;; |
4 | ;; Author: Carsten Dominik <carsten at orgmode dot org> | |
5 | ;; Keywords: outlines, hypermedia, calendar, wp | |
6 | ;; Homepage: http://orgmode.org | |
5dec9555 | 7 | ;; Version: 6.33x |
8d642074 CD |
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 | ;; | |
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ;; | |
21 | ;; You should have received a copy of the GNU General Public License | |
22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. | |
23 | ;; | |
24 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
25 | ;; | |
26 | ;;; Commentary: | |
27 | ;; | |
28 | ;; This file contains the code to interact with Richard Moreland's iPhone | |
29 | ;; application MobileOrg. This code is documented in Appendix B of the | |
30 | ;; Org-mode manual. The code is not specific for the iPhone, however. | |
8bfe682a | 31 | ;; Any external viewer/flagging/editing application that uses the same |
8d642074 CD |
32 | ;; conventions could be used. |
33 | ||
34 | (require 'org) | |
35 | (require 'org-agenda) | |
8bfe682a | 36 | (eval-when-compile (require 'cl)) |
8d642074 CD |
37 | |
38 | (defgroup org-mobile nil | |
8bfe682a | 39 | "Options concerning support for a viewer/editor on a mobile device." |
8d642074 CD |
40 | :tag "Org Mobile" |
41 | :group 'org) | |
42 | ||
43 | (defcustom org-mobile-files '(org-agenda-files) | |
44 | "Files to be staged for MobileOrg. | |
8bfe682a | 45 | This is basically a list of files and directories. Files will be staged |
8d642074 CD |
46 | directly. Directories will be search for files with the extension `.org'. |
47 | In addition to this, the list may also contain the following symbols: | |
48 | ||
49 | org-agenda-files | |
50 | This means, include the complete, unrestricted list of files given in | |
51 | the variable `org-agenda-files'. | |
52 | org-agenda-text-search-extra-files | |
53 | Include the files given in the variable | |
54 | `org-agenda-text-search-extra-files'" | |
55 | :group 'org-mobile | |
56 | :type '(list :greedy t | |
57 | (option (const :tag "org-agenda-files" org-agenda-files)) | |
58 | (option (const :tag "org-agenda-text-search-extra-files" | |
59 | org-agenda-text-search-extra-files)) | |
60 | (repeat :inline t :tag "Additional files" | |
61 | (file)))) | |
62 | ||
63 | (defcustom org-mobile-directory "" | |
64 | "The WebDAV directory where the interaction with the mobile takes place." | |
65 | :group 'org-mobile | |
66 | :type 'directory) | |
67 | ||
68 | (defcustom org-mobile-inbox-for-pull "~/org/from-mobile.org" | |
69 | "The file where captured notes and flags will be appended to. | |
70 | During the execution of `org-mobile-pull', the file | |
71 | `org-mobile-capture-file' will be emptied it's contents have | |
8bfe682a CD |
72 | been appended to the file given here. This file should be in |
73 | `org-directory', and not in the staging area or on the web server." | |
8d642074 CD |
74 | :group 'org-mobile |
75 | :type 'file) | |
76 | ||
77 | (defconst org-mobile-capture-file "mobileorg.org" | |
78 | "The capture file where the mobile stores captured notes and flags. | |
79 | This should not be changed, because MobileOrg assumes this name.") | |
80 | ||
81 | (defcustom org-mobile-index-file "index.org" | |
82 | "The index file with inks to all Org files that should be loaded by MobileOrg. | |
83 | Relative to `org-mobile-directory'. The Address field in the MobileOrg setup | |
84 | should point to this file." | |
85 | :group 'org-mobile | |
86 | :type 'file) | |
87 | ||
88 | (defcustom org-mobile-force-id-on-agenda-items t | |
89 | "Non-nil means make all agenda items carry and ID." | |
90 | :group 'org-mobile | |
91 | :type 'boolean) | |
92 | ||
8bfe682a CD |
93 | (defcustom org-mobile-force-mobile-change nil |
94 | "Non-nil means, force the change made on the mobile device. | |
95 | So even if there have been changes to the computer version of the entry, | |
96 | force the new value set on the mobile. | |
97 | When nil, mark the entry from the mobile with an error message. | |
98 | Instead of nil or t, this variable can also be a list of symbols, indicating | |
99 | the editing types for which the mobile version should always dominate." | |
100 | :group 'org-mobile | |
101 | :type '(choice | |
102 | (const :tag "Always" t) | |
103 | (const :tag "Never" nil) | |
104 | (set :greedy t :tag "Specify" | |
105 | (const todo) | |
106 | (const tags) | |
107 | (const priority) | |
108 | (const heading) | |
109 | (const body)))) | |
110 | ||
8d642074 | 111 | (defcustom org-mobile-action-alist |
8bfe682a | 112 | '(("edit" . (org-mobile-edit data old new))) |
8d642074 CD |
113 | "Alist with flags and actions for mobile sync. |
114 | When flagging an entry, MobileOrg will create entries that look like | |
115 | ||
116 | * F(action:data) [[id:entry-id][entry title]] | |
117 | ||
118 | This alist defines that the ACTION in the parentheses of F() should mean, | |
119 | i.e. what action should be taken. The :data part in the parenthesis is | |
120 | optional. If present, the string after the colon will be passed to the | |
121 | action form as the `data' variable. | |
122 | The car of each elements of the alist is an actions string. The cdr is | |
123 | an Emacs Lisp form that will be evaluated with the cursor on the headline | |
8bfe682a CD |
124 | of that entry. |
125 | ||
126 | For now, it is not recommended to change this variable." | |
8d642074 CD |
127 | :group 'org-mobile |
128 | :type '(repeat | |
129 | (cons (string :tag "Action flag") | |
130 | (sexp :tag "Action form")))) | |
131 | ||
8bfe682a CD |
132 | (defcustom org-mobile-checksum-binary (or (executable-find "shasum") |
133 | (executable-find "sha1sum") | |
134 | (executable-find "md5sum") | |
135 | (executable-find "md5")) | |
136 | "Executable used for computing checksums of agenda files." | |
137 | :group 'org-mobile | |
138 | :type 'string) | |
139 | ||
8d642074 CD |
140 | (defvar org-mobile-pre-push-hook nil |
141 | "Hook run before running `org-mobile-push'. | |
142 | This could be used to clean up `org-mobile-directory', for example to | |
143 | remove files that used to be included in the agenda but no longer are. | |
144 | The presence of such files would not really be a problem, but after time | |
145 | they may accumulate.") | |
146 | ||
147 | (defvar org-mobile-post-push-hook nil | |
148 | "Hook run after running `org-mobile-push'. | |
149 | If Emacs does not have direct write access to the WebDAV directory used | |
150 | by the mobile device, this hook should be used to copy all files from the | |
151 | local staging directory `org-mobile-directory' to the WebDAV directory, | |
152 | for example using `rsync' or `scp'.") | |
153 | ||
154 | (defvar org-mobile-pre-pull-hook nil | |
155 | "Hook run before executing `org-mobile-pull'. | |
156 | If Emacs does not have direct write access to the WebDAV directory used | |
157 | by the mobile device, this hook should be used to copy the capture file | |
158 | `mobileorg.org' from the WebDAV location to the local staging | |
159 | directory `org-mobile-directory'.") | |
160 | ||
161 | (defvar org-mobile-post-pull-hook nil | |
162 | "Hook run after running `org-mobile-pull'. | |
163 | If Emacs does not have direct write access to the WebDAV directory used | |
164 | by the mobile device, this hook should be used to copy the emptied | |
165 | capture file `mobileorg.org' back to the WebDAV directory, for example | |
166 | using `rsync' or `scp'.") | |
167 | ||
168 | (defvar org-mobile-last-flagged-files nil | |
8bfe682a | 169 | "List of files containing entries flagged in the latest pull.") |
8d642074 CD |
170 | |
171 | (defvar org-mobile-files-alist nil) | |
172 | (defvar org-mobile-checksum-files nil) | |
173 | ||
174 | (defun org-mobile-prepare-file-lists () | |
175 | (setq org-mobile-files-alist (org-mobile-files-alist)) | |
8bfe682a | 176 | (setq org-mobile-checksum-files nil)) |
8d642074 CD |
177 | |
178 | (defun org-mobile-files-alist () | |
179 | "Expand the list in `org-mobile-files' to a list of existing files." | |
8bfe682a CD |
180 | (let* ((include-archives |
181 | (and (member 'org-agenda-text-search-extra-files org-mobile-files) | |
182 | (member 'agenda-archives org-agenda-text-search-extra-files) | |
183 | t)) | |
184 | (files | |
185 | (apply 'append | |
186 | (mapcar | |
187 | (lambda (f) | |
188 | (cond | |
189 | ((eq f 'org-agenda-files) | |
190 | (org-agenda-files t include-archives)) | |
191 | ((eq f 'org-agenda-text-search-extra-files) | |
192 | (delq 'agenda-archives | |
193 | (copy-sequence | |
194 | org-agenda-text-search-extra-files))) | |
195 | ((and (stringp f) (file-directory-p f)) | |
196 | (directory-files f 'full "\\.org\\'")) | |
197 | ((and (stringp f) (file-exists-p f)) | |
198 | (list f)) | |
199 | (t nil))) | |
200 | org-mobile-files))) | |
8d642074 CD |
201 | (orgdir-uname (file-name-as-directory (file-truename org-directory))) |
202 | (orgdir-re (concat "\\`" (regexp-quote orgdir-uname))) | |
203 | uname seen rtn file link-name) | |
204 | ;; Make the files unique, and determine the name under which they will | |
205 | ;; be listed. | |
206 | (while (setq file (pop files)) | |
8bfe682a CD |
207 | (if (not (file-name-absolute-p file)) |
208 | (setq file (expand-file-name file org-directory))) | |
8d642074 CD |
209 | (setq uname (file-truename file)) |
210 | (unless (member uname seen) | |
211 | (push uname seen) | |
212 | (if (string-match orgdir-re uname) | |
213 | (setq link-name (substring uname (match-end 0))) | |
214 | (setq link-name (file-name-nondirectory uname))) | |
215 | (push (cons file link-name) rtn))) | |
216 | (nreverse rtn))) | |
217 | ||
218 | ;;;###autoload | |
219 | (defun org-mobile-push () | |
220 | "Push the current state of Org affairs to the WebDAV directory. | |
221 | This will create the index file, copy all agenda files there, and also | |
222 | create all custom agenda views, for upload to the mobile phone." | |
223 | (interactive) | |
8bfe682a CD |
224 | (let ((a-buffer (get-buffer org-agenda-buffer-name))) |
225 | (let ((org-agenda-buffer-name "*SUMO*") | |
226 | (org-agenda-filter org-agenda-filter) | |
227 | (org-agenda-redo-command org-agenda-redo-command)) | |
228 | (save-excursion | |
229 | (save-window-excursion | |
230 | (org-mobile-check-setup) | |
231 | (org-mobile-prepare-file-lists) | |
232 | (run-hooks 'org-mobile-pre-push-hook) | |
233 | (message "Creating agendas...") | |
234 | (let ((inhibit-redisplay t)) (org-mobile-create-sumo-agenda)) | |
235 | (message "Creating agendas...done") | |
236 | (org-save-all-org-buffers) ; to save any IDs created by this process | |
237 | (message "Copying files...") | |
238 | (org-mobile-copy-agenda-files) | |
239 | (message "Writing index file...") | |
240 | (org-mobile-create-index-file) | |
241 | (message "Writing checksums...") | |
242 | (org-mobile-write-checksums) | |
243 | (run-hooks 'org-mobile-post-push-hook)))) | |
244 | (redraw-display) | |
245 | (when (and a-buffer (buffer-live-p a-buffer)) | |
246 | (if (not (get-buffer-window a-buffer)) | |
247 | (kill-buffer a-buffer) | |
248 | (let ((cw (selected-window))) | |
249 | (select-window (get-buffer-window a-buffer)) | |
250 | ||
251 | (org-agenda-redo) | |
252 | (select-window cw))))) | |
8d642074 | 253 | (message "Files for mobile viewer staged")) |
8bfe682a CD |
254 | |
255 | (defvar org-mobile-before-process-capture-hook nil | |
256 | "Hook that is run after content was moved to `org-mobile-inbox-for-pull'. | |
257 | The inbox file is in the current buffer, and the buffer is arrowed to the | |
258 | new captured data.") | |
8d642074 CD |
259 | |
260 | ;;;###autoload | |
261 | (defun org-mobile-pull () | |
262 | "Pull the contents of `org-mobile-capture-file' and integrate them. | |
263 | Apply all flagged actions, flag entries to be flagged and then call an | |
264 | agenda view showing the flagged items." | |
265 | (interactive) | |
266 | (org-mobile-check-setup) | |
267 | (run-hooks 'org-mobile-pre-pull-hook) | |
268 | (let ((insertion-marker (org-mobile-move-capture))) | |
269 | (if (not (markerp insertion-marker)) | |
270 | (message "No new items") | |
271 | (org-with-point-at insertion-marker | |
8bfe682a CD |
272 | (save-restriction |
273 | (narrow-to-region (point) (point-max)) | |
274 | (run-hooks 'org-mobile-before-process-capture-hook))) | |
275 | (org-with-point-at insertion-marker | |
276 | (org-mobile-apply (point) (point-max))) | |
8d642074 CD |
277 | (move-marker insertion-marker nil) |
278 | (run-hooks 'org-mobile-post-pull-hook) | |
279 | (when org-mobile-last-flagged-files | |
280 | ;; Make an agenda view of flagged entries, but only in the files | |
281 | ;; where stuff has been added. | |
282 | (put 'org-agenda-files 'org-restrict org-mobile-last-flagged-files) | |
8bfe682a | 283 | (let ((org-agenda-keep-restricted-file-list t)) |
8d642074 CD |
284 | (org-agenda nil "?")))))) |
285 | ||
286 | (defun org-mobile-check-setup () | |
287 | "Check if org-mobile-directory has been set up." | |
8bfe682a CD |
288 | (unless (and org-directory |
289 | (stringp org-directory) | |
290 | (string-match "\\S-" org-directory) | |
291 | (file-exists-p org-directory) | |
292 | (file-directory-p org-directory)) | |
293 | (error | |
294 | "Please set `org-directory' to the directory where your org files live")) | |
295 | (unless (and org-mobile-directory | |
296 | (stringp org-mobile-directory) | |
297 | (string-match "\\S-" org-mobile-directory) | |
298 | (file-exists-p org-mobile-directory) | |
299 | (file-directory-p org-mobile-directory)) | |
8d642074 CD |
300 | (error |
301 | "Variable `org-mobile-directory' must point to an existing directory")) | |
8bfe682a CD |
302 | (unless (and org-mobile-inbox-for-pull |
303 | (stringp org-mobile-inbox-for-pull) | |
304 | (string-match "\\S-" org-mobile-inbox-for-pull) | |
305 | (file-exists-p | |
306 | (file-name-directory org-mobile-inbox-for-pull))) | |
8d642074 CD |
307 | (error |
308 | "Variable `org-mobile-inbox-for-pull' must point to a file in an existing directory"))) | |
309 | ||
310 | (defun org-mobile-create-index-file () | |
311 | "Write the index file in the WebDAV directory." | |
8bfe682a CD |
312 | (let ((files-alist (sort (copy-sequence org-mobile-files-alist) |
313 | (lambda (a b) (string< (cdr a) (cdr b))))) | |
314 | (def-todo (default-value 'org-todo-keywords)) | |
315 | (def-tags (default-value 'org-tag-alist)) | |
316 | file link-name todo-kwds done-kwds tags drawers entry kwds dwds twds) | |
317 | ||
8d642074 CD |
318 | (org-prepare-agenda-buffers (mapcar 'car files-alist)) |
319 | (setq done-kwds (org-uniquify org-done-keywords-for-agenda)) | |
320 | (setq todo-kwds (org-delete-all | |
321 | done-kwds | |
322 | (org-uniquify org-todo-keywords-for-agenda))) | |
323 | (setq drawers (org-uniquify org-drawers-for-agenda)) | |
324 | (setq tags (org-uniquify | |
325 | (delq nil | |
326 | (mapcar | |
327 | (lambda (e) | |
328 | (cond ((stringp e) e) | |
329 | ((listp e) | |
330 | (if (stringp (car e)) (car e) nil)) | |
331 | (t nil))) | |
332 | org-tag-alist-for-agenda)))) | |
333 | (with-temp-file | |
334 | (expand-file-name org-mobile-index-file org-mobile-directory) | |
8bfe682a CD |
335 | (while (setq entry (pop def-todo)) |
336 | (insert "#+READONLY\n") | |
337 | (setq kwds (mapcar (lambda (x) (if (string-match "(" x) | |
338 | (substring x 0 (match-beginning 0)) | |
339 | x)) | |
340 | (cdr entry))) | |
341 | (insert "#+TODO: " (mapconcat 'identity kwds " ") "\n") | |
342 | (setq dwds (member "|" kwds) | |
343 | twds (org-delete-all dwds kwds) | |
344 | todo-kwds (org-delete-all twds todo-kwds) | |
345 | done-kwds (org-delete-all dwds done-kwds))) | |
346 | (when (or todo-kwds done-kwds) | |
347 | (insert "#+TODO: " (mapconcat 'identity todo-kwds " ") " | " | |
348 | (mapconcat 'identity done-kwds " ") "\n")) | |
349 | (setq def-tags (mapcar | |
350 | (lambda (x) | |
351 | (cond ((null x) nil) | |
352 | ((stringp x) x) | |
353 | ((eq (car x) :startgroup) "{") | |
354 | ((eq (car x) :endgroup) "}") | |
355 | ((eq (car x) :newline) nil) | |
356 | ((listp x) (car x)) | |
357 | (t nil))) | |
358 | def-tags)) | |
359 | (setq def-tags (delq nil def-tags)) | |
360 | (setq tags (org-delete-all def-tags tags)) | |
361 | (setq tags (sort tags (lambda (a b) (string< (downcase a) (downcase b))))) | |
362 | (setq tags (append def-tags tags nil)) | |
363 | (insert "#+TAGS: " (mapconcat 'identity tags " ") "\n") | |
364 | (insert "#+DRAWERS: " (mapconcat 'identity drawers " ") "\n") | |
365 | (insert "#+ALLPRIORITIES: A B C" "\n") | |
366 | (when (file-exists-p (expand-file-name | |
367 | org-mobile-directory "agendas.org")) | |
368 | (insert "* [[file:agendas.org][Agenda Views]]\n")) | |
8d642074 CD |
369 | (while (setq entry (pop files-alist)) |
370 | (setq file (car entry) | |
371 | link-name (cdr entry)) | |
372 | (insert (format "* [[file:%s][%s]]\n" | |
373 | link-name link-name))) | |
8bfe682a CD |
374 | (push (cons org-mobile-index-file (md5 (buffer-string))) |
375 | org-mobile-checksum-files)))) | |
8d642074 CD |
376 | |
377 | (defun org-mobile-copy-agenda-files () | |
378 | "Copy all agenda files to the stage or WebDAV directory." | |
379 | (let ((files-alist org-mobile-files-alist) | |
8bfe682a | 380 | file buf entry link-name target-path target-dir check) |
8d642074 CD |
381 | (while (setq entry (pop files-alist)) |
382 | (setq file (car entry) link-name (cdr entry)) | |
383 | (when (file-exists-p file) | |
384 | (setq target-path (expand-file-name link-name org-mobile-directory) | |
385 | target-dir (file-name-directory target-path)) | |
386 | (unless (file-directory-p target-dir) | |
8bfe682a CD |
387 | (make-directory target-dir 'parents)) |
388 | (copy-file file target-path 'ok-if-exists) | |
389 | (setq check (shell-command-to-string | |
390 | (concat org-mobile-checksum-binary " " | |
391 | (shell-quote-argument (expand-file-name file))))) | |
392 | (when (string-match "[a-fA-F0-9]\\{30,40\\}" check) | |
393 | (push (cons link-name (match-string 0 check)) | |
394 | org-mobile-checksum-files)))) | |
8d642074 CD |
395 | (setq file (expand-file-name org-mobile-capture-file |
396 | org-mobile-directory)) | |
8bfe682a CD |
397 | (save-excursion |
398 | (setq buf (find-file file)) | |
399 | (and (= (point-min) (point-max)) (insert "\n")) | |
400 | (save-buffer) | |
401 | (push (cons org-mobile-capture-file (md5 (buffer-string))) | |
402 | org-mobile-checksum-files)) | |
403 | (kill-buffer buf))) | |
8d642074 CD |
404 | |
405 | (defun org-mobile-write-checksums () | |
406 | "Create checksums for all files in `org-mobile-directory'. | |
407 | The table of checksums is written to the file mobile-checksums." | |
8bfe682a CD |
408 | (let ((sumfile (expand-file-name "checksums.dat" org-mobile-directory)) |
409 | (files org-mobile-checksum-files) | |
410 | entry file sum) | |
411 | (with-temp-file sumfile | |
412 | (set-buffer-file-coding-system 'undecided-unix nil) | |
413 | (while (setq entry (pop files)) | |
414 | (setq file (car entry) sum (cdr entry)) | |
415 | (insert (format "%s %s\n" sum file)))))) | |
8d642074 CD |
416 | |
417 | (defun org-mobile-sumo-agenda-command () | |
418 | "Return an agenda custom command that comprises all custom commands." | |
419 | (let ((custom-list | |
420 | ;; normalize different versions | |
421 | (delq nil | |
422 | (mapcar | |
423 | (lambda (x) | |
424 | (cond ((stringp (cdr x)) nil) | |
425 | ((stringp (nth 1 x)) x) | |
426 | ((not (nth 1 x)) (cons (car x) (cons "" (cddr x)))) | |
427 | (t (cons (car x) (cons "" (cdr x)))))) | |
428 | org-agenda-custom-commands))) | |
429 | new e key desc type match settings cmds gkey gdesc gsettings cnt) | |
430 | (while (setq e (pop custom-list)) | |
431 | (cond | |
432 | ((stringp (cdr e)) | |
433 | ;; this is a description entry - skip it | |
434 | ) | |
435 | ((eq (nth 2 e) 'search) | |
436 | ;; Search view is interactive, skip | |
437 | ) | |
438 | ((memq (nth 2 e) '(todo-tree tags-tree occur-tree)) | |
439 | ;; These are trees, not really agenda commands | |
440 | ) | |
441 | ((memq (nth 2 e) '(agenda todo tags)) | |
442 | ;; a normal command | |
443 | (setq key (car e) desc (nth 1 e) type (nth 2 e) match (nth 3 e) | |
444 | settings (nth 4 e)) | |
445 | (setq settings | |
446 | (cons (list 'org-agenda-title-append | |
8bfe682a | 447 | (concat "<after>KEYS=" key " TITLE: " |
8d642074 CD |
448 | (if (and (stringp desc) (> (length desc) 0)) |
449 | desc (symbol-name type)) | |
8bfe682a | 450 | " " match "</after>")) |
8d642074 CD |
451 | settings)) |
452 | (push (list type match settings) new)) | |
453 | ((symbolp (nth 2 e)) | |
454 | ;; A user-defined function, not sure how to handle that yet | |
455 | ) | |
456 | (t | |
457 | ;; a block agenda | |
458 | (setq gkey (car e) gdesc (nth 1 e) gsettings (nth 3 e) cmds (nth 2 e)) | |
459 | (setq cnt 0) | |
460 | (while (setq e (pop cmds)) | |
461 | (setq type (car e) match (nth 1 e) settings (nth 2 e)) | |
462 | (setq settings (append gsettings settings)) | |
463 | (setq settings | |
464 | (cons (list 'org-agenda-title-append | |
8bfe682a | 465 | (concat "<after>KEYS=" gkey "#" (number-to-string |
8d642074 | 466 | (setq cnt (1+ cnt))) |
8bfe682a | 467 | " TITLE: " gdesc " " match "</after>")) |
8d642074 CD |
468 | settings)) |
469 | (push (list type match settings) new))))) | |
8bfe682a CD |
470 | (and new (list "X" "SUMO" (reverse new) |
471 | '((org-agenda-compact-blocks nil)))))) | |
472 | ||
473 | (defvar org-mobile-creating-agendas nil) | |
474 | (defun org-mobile-write-agenda-for-mobile (file) | |
475 | (let ((all (buffer-string)) in-date id pl prefix line app short m sexp) | |
476 | (with-temp-file file | |
477 | (org-mode) | |
478 | (insert "#+READONLY\n") | |
479 | (insert all) | |
480 | (goto-char (point-min)) | |
481 | (while (not (eobp)) | |
482 | (cond | |
483 | ((looking-at "[ \t]*$")) ; keep empty lines | |
484 | ((looking-at "=+$") | |
485 | ;; remove underlining | |
486 | (delete-region (point) (point-at-eol))) | |
487 | ((get-text-property (point) 'org-agenda-structural-header) | |
488 | (setq in-date nil) | |
489 | (setq app (get-text-property (point) | |
490 | 'org-agenda-title-append)) | |
491 | (setq short (get-text-property (point) | |
492 | 'short-heading)) | |
493 | (when (and short (looking-at ".+")) | |
494 | (replace-match short) | |
495 | (beginning-of-line 1)) | |
496 | (when app | |
497 | (end-of-line 1) | |
498 | (insert app) | |
499 | (beginning-of-line 1)) | |
500 | (insert "* ")) | |
501 | ((get-text-property (point) 'org-agenda-date-header) | |
502 | (setq in-date t) | |
503 | (insert "** ")) | |
504 | ((setq m (or (get-text-property (point) 'org-hd-marker) | |
505 | (get-text-property (point) 'org-marker))) | |
506 | (setq sexp (member (get-text-property (point) 'type) | |
507 | '("diary" "sexp"))) | |
508 | (if (setq pl (get-text-property (point) 'prefix-length)) | |
509 | (progn | |
510 | (setq prefix (org-trim (buffer-substring | |
511 | (point) (+ (point) pl))) | |
512 | line (org-trim (buffer-substring | |
513 | (+ (point) pl) | |
514 | (point-at-eol)))) | |
515 | (delete-region (point-at-bol) (point-at-eol)) | |
516 | (insert line "<before>" prefix "</before>") | |
517 | (beginning-of-line 1)) | |
518 | (and (looking-at "[ \t]+") (replace-match ""))) | |
519 | (insert (if in-date "*** " "** ")) | |
520 | (end-of-line 1) | |
521 | (insert "\n") | |
522 | (unless sexp | |
523 | (insert (org-agenda-get-some-entry-text | |
524 | m 10 " " 'planning) | |
525 | "\n") | |
526 | (when (setq id | |
527 | (if (org-bound-and-true-p | |
528 | org-mobile-force-id-on-agenda-items) | |
529 | (org-id-get m 'create) | |
530 | (org-entry-get m "ID"))) | |
531 | (insert " :PROPERTIES:\n :ORIGINAL_ID: " id | |
532 | "\n :END:\n"))))) | |
533 | (beginning-of-line 2)) | |
534 | (push (cons (file-name-nondirectory file) (md5 (buffer-string))) | |
535 | org-mobile-checksum-files)) | |
536 | (message "Agenda written to Org file %s" file))) | |
8d642074 CD |
537 | |
538 | ;;;###autoload | |
539 | (defun org-mobile-create-sumo-agenda () | |
540 | "Create a file that contains all custom agenda views." | |
541 | (interactive) | |
542 | (let* ((file (expand-file-name "agendas.org" | |
543 | org-mobile-directory)) | |
8bfe682a | 544 | (sumo (org-mobile-sumo-agenda-command)) |
8d642074 | 545 | (org-agenda-custom-commands |
8bfe682a CD |
546 | (list (append sumo (list (list file))))) |
547 | (org-mobile-creating-agendas t)) | |
8d642074 CD |
548 | (unless (file-writable-p file) |
549 | (error "Cannot write to file %s" file)) | |
8bfe682a CD |
550 | (when sumo |
551 | (org-store-agenda-views)))) | |
8d642074 CD |
552 | |
553 | (defun org-mobile-move-capture () | |
554 | "Move the contents of the capture file to the inbox file. | |
555 | Return a marker to the location where the new content has been added. | |
8bfe682a | 556 | If nothing new has been added, return nil." |
8d642074 CD |
557 | (interactive) |
558 | (let ((inbox-buffer (find-file-noselect org-mobile-inbox-for-pull)) | |
559 | (capture-buffer (find-file-noselect | |
560 | (expand-file-name org-mobile-capture-file | |
561 | org-mobile-directory))) | |
562 | (insertion-point (make-marker)) | |
563 | not-empty content) | |
81ad75af | 564 | (with-current-buffer capture-buffer |
8d642074 CD |
565 | (setq content (buffer-string)) |
566 | (setq not-empty (string-match "\\S-" content)) | |
567 | (when not-empty | |
568 | (set-buffer inbox-buffer) | |
569 | (widen) | |
570 | (goto-char (point-max)) | |
571 | (or (bolp) (newline)) | |
572 | (move-marker insertion-point | |
573 | (prog1 (point) (insert content))) | |
574 | (save-buffer) | |
575 | (set-buffer capture-buffer) | |
576 | (erase-buffer) | |
8bfe682a CD |
577 | (save-buffer) |
578 | (org-mobile-update-checksum-for-capture-file (buffer-string)))) | |
8d642074 CD |
579 | (kill-buffer capture-buffer) |
580 | (if not-empty insertion-point))) | |
581 | ||
8bfe682a CD |
582 | (defun org-mobile-update-checksum-for-capture-file (buffer-string) |
583 | (let* ((file (expand-file-name "checksums.dat" org-mobile-directory)) | |
584 | (buffer (find-file-noselect file))) | |
585 | (when buffer | |
586 | (with-current-buffer buffer | |
587 | (when (re-search-forward (concat "\\([0-9a-fA-F]\\{30,\\}\\).*?" | |
588 | (regexp-quote org-mobile-capture-file) | |
589 | "[ \t]*$") nil t) | |
590 | (goto-char (match-beginning 1)) | |
591 | (delete-region (match-beginning 1) (match-end 1)) | |
592 | (insert (md5 buffer-string)) | |
593 | (save-buffer))) | |
594 | (kill-buffer buffer)))) | |
595 | ||
596 | (defun org-mobile-apply (&optional beg end) | |
597 | "Apply all change requests in the current buffer. | |
8d642074 CD |
598 | If BEG and END are given, only do this in that region." |
599 | (interactive) | |
600 | (require 'org-archive) | |
601 | (setq org-mobile-last-flagged-files nil) | |
602 | (setq beg (or beg (point-min)) end (or end (point-max))) | |
8bfe682a CD |
603 | |
604 | ;; Remove all Note IDs | |
8d642074 | 605 | (goto-char beg) |
8bfe682a CD |
606 | (while (re-search-forward "^\\*\\* Note ID: [-0-9A-F]+[ \t]*\n" end t) |
607 | (replace-match "")) | |
608 | ||
609 | ;; Find all the referenced entries, without making any changes yet | |
8d642074 | 610 | (let ((marker (make-marker)) |
8bfe682a | 611 | (bos-marker (make-marker)) |
8d642074 | 612 | (end (move-marker (make-marker) end)) |
8bfe682a CD |
613 | (cnt-new 0) |
614 | (cnt-edit 0) | |
615 | (cnt-flag 0) | |
616 | (cnt-error 0) | |
617 | buf-list | |
618 | id-pos org-mobile-error) | |
619 | ||
620 | ;; Count the new captures | |
621 | (goto-char beg) | |
622 | (while (re-search-forward "^\\* \\(.*\\)" end t) | |
623 | (and (>= (- (match-end 1) (match-beginning 1)) 2) | |
624 | (not (equal (downcase (substring (match-string 1) 0 2)) "f(")) | |
625 | (incf cnt-new))) | |
626 | ||
627 | (goto-char beg) | |
8d642074 | 628 | (while (re-search-forward |
8bfe682a CD |
629 | "^\\*+[ \t]+F(\\([^():\n]*\\)\\(:\\([^()\n]*\\)\\)?)[ \t]+\\[\\[\\(\\(id\\|olp\\):\\([^]\n]+\\)\\)" end t) |
630 | (setq id-pos (condition-case msg | |
631 | (org-mobile-locate-entry (match-string 4)) | |
632 | (error (nth 1 msg)))) | |
633 | (when (and (markerp id-pos) | |
634 | (not (member (marker-buffer id-pos) buf-list))) | |
635 | (org-mobile-timestamp-buffer (marker-buffer id-pos)) | |
636 | (push (marker-buffer id-pos) buf-list)) | |
637 | ||
638 | (if (or (not id-pos) (stringp id-pos)) | |
639 | (progn | |
640 | (goto-char (+ 2 (point-at-bol))) | |
641 | (insert id-pos " ") | |
642 | (incf cnt-error)) | |
643 | (add-text-properties (point-at-bol) (point-at-eol) | |
644 | (list 'org-mobile-marker | |
645 | (or id-pos "Linked entry not found"))))) | |
646 | ||
647 | ;; OK, now go back and start applying | |
648 | (goto-char beg) | |
649 | (while (re-search-forward "^\\*+[ \t]+F(\\([^():\n]*\\)\\(:\\([^()\n]*\\)\\)?)" end t) | |
8d642074 | 650 | (catch 'next |
8bfe682a CD |
651 | (setq id-pos (get-text-property (point-at-bol) 'org-mobile-marker)) |
652 | (if (not (markerp id-pos)) | |
653 | (progn | |
654 | (incf cnt-error) | |
655 | (insert "UNKNOWN PROBLEM")) | |
656 | (let* ((action (match-string 1)) | |
657 | (data (and (match-end 3) (match-string 3))) | |
658 | (bos (point-at-bol)) | |
659 | (eos (save-excursion (org-end-of-subtree t t))) | |
660 | (cmd (if (equal action "") | |
661 | '(progn | |
662 | (incf cnt-flag) | |
663 | (org-toggle-tag "FLAGGED" 'on) | |
664 | (and note | |
665 | (org-entry-put nil "THEFLAGGINGNOTE" note))) | |
666 | (incf cnt-edit) | |
667 | (cdr (assoc action org-mobile-action-alist)))) | |
668 | (note (and (equal action "") | |
669 | (buffer-substring (1+ (point-at-eol)) eos))) | |
670 | (org-inhibit-logging 'note) ;; Do not take notes interactively | |
671 | old new) | |
672 | (goto-char bos) | |
673 | (move-marker bos-marker (point)) | |
674 | (if (re-search-forward "^** Old value[ \t]*$" eos t) | |
675 | (setq old (buffer-substring | |
676 | (1+ (match-end 0)) | |
677 | (progn (outline-next-heading) (point))))) | |
678 | (if (re-search-forward "^** New value[ \t]*$" eos t) | |
679 | (setq new (buffer-substring | |
680 | (1+ (match-end 0)) | |
681 | (progn (outline-next-heading) | |
682 | (if (eobp) (org-back-over-empty-lines)) | |
683 | (point))))) | |
684 | (setq old (and old (if (string-match "\\S-" old) old nil))) | |
685 | (setq new (and new (if (string-match "\\S-" new) new nil))) | |
686 | (if (and note (> (length note) 0)) | |
687 | ;; Make Note into a single line, to fit into a property | |
688 | (setq note (mapconcat 'identity | |
689 | (org-split-string (org-trim note) "\n") | |
690 | "\\n"))) | |
691 | (unless (equal data "body") | |
692 | (setq new (and new (org-trim new)) | |
693 | old (and old (org-trim old)))) | |
694 | (goto-char (+ 2 bos-marker)) | |
695 | (unless (markerp id-pos) | |
696 | (insert "BAD REFERENCE ") | |
697 | (incf cnt-error) | |
698 | (throw 'next t)) | |
699 | (unless cmd | |
700 | (insert "BAD FLAG ") | |
701 | (incf cnt-error) | |
702 | (throw 'next t)) | |
703 | ;; Remember this place so that we can return | |
704 | (move-marker marker (point)) | |
705 | (setq org-mobile-error nil) | |
706 | (save-excursion | |
707 | (condition-case msg | |
708 | (org-with-point-at id-pos | |
709 | (progn | |
8d642074 CD |
710 | (eval cmd) |
711 | (if (member "FLAGGED" (org-get-tags)) | |
712 | (add-to-list 'org-mobile-last-flagged-files | |
713 | (buffer-file-name (current-buffer)))))) | |
8bfe682a CD |
714 | (error (setq org-mobile-error msg)))) |
715 | (when org-mobile-error | |
716 | (switch-to-buffer (marker-buffer marker)) | |
717 | (goto-char marker) | |
718 | (incf cnt-error) | |
719 | (insert (if (stringp (nth 1 org-mobile-error)) | |
720 | (nth 1 org-mobile-error) | |
721 | "EXECUTION FAILED") | |
722 | " ") | |
723 | (throw 'next t)) | |
724 | ;; If we get here, the action has been applied successfully | |
725 | ;; So remove the entry | |
726 | (goto-char bos-marker) | |
727 | (delete-region (point) (org-end-of-subtree t t)))))) | |
728 | (save-buffer) | |
8d642074 | 729 | (move-marker marker nil) |
8bfe682a CD |
730 | (move-marker end nil) |
731 | (message "%d new, %d edits, %d flags, %d errors" cnt-new | |
732 | cnt-edit cnt-flag cnt-error) | |
733 | (sit-for 1))) | |
734 | ||
735 | (defun org-mobile-timestamp-buffer (buf) | |
736 | "Time stamp buffer BUF, just to make sure its checksum will change." | |
737 | (with-current-buffer buf | |
738 | (save-excursion | |
739 | (save-restriction | |
740 | (widen) | |
741 | (goto-char (point-min)) | |
742 | (if (re-search-forward | |
743 | "^\\([ \t]*\\)#\\+LAST_MOBILE_CHANGE:.*\n?" nil t) | |
744 | (progn | |
745 | (goto-char (match-end 1)) | |
746 | (delete-region (point) (match-end 0))) | |
747 | (if (looking-at ".*?-\\*-.*-\\*-") | |
748 | (forward-line 1))) | |
749 | (insert "#+LAST_MOBILE_CHANGE: " | |
750 | (format-time-string "%Y-%m-%d %T") "\n"))))) | |
8d642074 CD |
751 | |
752 | (defun org-mobile-smart-read () | |
753 | "Parse the entry at point for shortcuts and expand them. | |
754 | These shortcuts are meant for fast and easy typing on the limited | |
755 | keyboards of a mobile device. Below we show a list of the shortcuts | |
756 | currently implemented. | |
757 | ||
758 | The entry is expected to contain an inactive time stamp indicating when | |
759 | the entry was created. When setting dates and | |
760 | times (for example for deadlines), the time strings are interpreted | |
761 | relative to that creation date. | |
8bfe682a | 762 | Abbreviations are expected to take up entire lines, just because it is so |
8d642074 CD |
763 | easy to type RET on a mobile device. Abbreviations start with one or two |
764 | letters, followed immediately by a dot and then additional information. | |
765 | Generally the entire shortcut line is removed after action have been taken. | |
766 | Time stamps will be constructed using `org-read-date'. So for example a | |
767 | line \"dd. 2tue\" will set a deadline on the second Tuesday after the | |
768 | creation date. | |
769 | ||
770 | Here are the shortcuts currently implemented: | |
771 | ||
772 | dd. string set deadline | |
773 | ss. string set scheduling | |
774 | tt. string set time tamp, here. | |
775 | ti. string set inactive time | |
776 | ||
777 | tg. tag1 tag2 tag3 set all these tags, change case where necessary | |
778 | td. kwd set this todo keyword, change case where necessary | |
779 | ||
780 | FIXME: Hmmm, not sure if we can make his work against the | |
781 | auto-correction feature. Needs a bit more thinking. So this function | |
782 | is currently a noop.") | |
783 | ||
8bfe682a CD |
784 | |
785 | (defun org-find-olp (path) | |
786 | "Return a marker pointing to the entry at outline path OLP. | |
787 | If anything goes wrong, the return value will instead an error message, | |
788 | as a string." | |
789 | (let* ((file (pop path)) | |
790 | (buffer (find-file-noselect file)) | |
791 | (level 1) | |
792 | (lmin 1) | |
793 | (lmax 1) | |
794 | limit re end found pos heading cnt) | |
795 | (unless buffer (error "File not found :%s" file)) | |
796 | (with-current-buffer buffer | |
797 | (save-excursion | |
798 | (save-restriction | |
799 | (widen) | |
800 | (setq limit (point-max)) | |
801 | (goto-char (point-min)) | |
802 | (while (setq heading (pop path)) | |
803 | (setq re (format org-complex-heading-regexp-format | |
804 | (regexp-quote heading))) | |
805 | (setq cnt 0 pos (point)) | |
806 | (while (re-search-forward re end t) | |
807 | (setq level (- (match-end 1) (match-beginning 1))) | |
808 | (if (and (>= level lmin) (<= level lmax)) | |
809 | (setq found (match-beginning 0) cnt (1+ cnt)))) | |
810 | (when (= cnt 0) (error "Heading not found on level %d: %s" | |
811 | lmax heading)) | |
812 | (when (> cnt 1) (error "Heading not unique on level %d: %s" | |
813 | lmax heading)) | |
814 | (goto-char found) | |
815 | (setq lmin (1+ level) lmax (+ lmin (if org-odd-levels-only 1 0))) | |
816 | (setq end (save-excursion (org-end-of-subtree t t)))) | |
817 | (when (org-on-heading-p) | |
818 | (move-marker (make-marker) (point)))))))) | |
819 | ||
820 | (defun org-mobile-locate-entry (link) | |
821 | (if (string-match "\\`id:\\(.*\\)$" link) | |
822 | (org-id-find (match-string 1 link) 'marker) | |
823 | (if (not (string-match "\\`olp:\\(.*?\\):\\(.*\\)$" link)) | |
824 | nil | |
825 | (let ((file (match-string 1 link)) | |
826 | (path (match-string 2 link)) | |
827 | (table '((?: . "%3a") (?\[ . "%5b") (?\] . "%5d") (?/ . "%2f")))) | |
828 | (setq file (org-link-unescape file table)) | |
829 | (setq file (expand-file-name file org-directory)) | |
830 | (setq path (mapcar (lambda (x) (org-link-unescape x table)) | |
831 | (org-split-string path "/"))) | |
832 | (org-find-olp (cons file path)))))) | |
833 | ||
834 | (defun org-mobile-edit (what old new) | |
835 | "Edit item WHAT in the current entry by replacing OLD with NEW. | |
836 | WHAT can be \"heading\", \"todo\", \"tags\", \"priority\", or \"body\". | |
837 | The edit only takes place if the current value is equal (except for | |
838 | white space) the OLD. If this is so, OLD will be replace by NEW | |
839 | and the command will return t. If something goes wrong, a string will | |
840 | be returned that indicates what went wrong." | |
841 | (let (current old1 new1) | |
842 | (if (stringp what) (setq what (intern what))) | |
843 | ||
844 | (cond | |
845 | ||
846 | ((memq what '(todo todostate)) | |
847 | (setq current (org-get-todo-state)) | |
848 | (cond | |
849 | ((equal new "DONEARCHIVE") | |
850 | (org-todo 'done) | |
851 | (org-archive-subtree-default)) | |
852 | ((equal new current) t) ; nothing needs to be done | |
853 | ((or (equal current old) | |
854 | (eq org-mobile-force-mobile-change t) | |
855 | (memq 'todo org-mobile-force-mobile-change)) | |
856 | (org-todo (or new 'none)) t) | |
857 | (t (error "State before change was expected as \"%s\", but is \"%s\"" | |
858 | old current)))) | |
859 | ||
860 | ((eq what 'tags) | |
861 | (setq current (org-get-tags) | |
862 | new1 (and new (org-split-string new ":+")) | |
863 | old1 (and old (org-split-string old ":+"))) | |
864 | (cond | |
865 | ((org-mobile-tags-same-p current new1) t) ; no change needed | |
866 | ((or (org-mobile-tags-same-p current old1) | |
867 | (eq org-mobile-force-mobile-change t) | |
868 | (memq 'tags org-mobile-force-mobile-change)) | |
869 | (org-set-tags-to new1) t) | |
870 | (t (error "Tags before change were expected as \"%s\", but are \"%s\"" | |
871 | (or old "") (or current ""))))) | |
872 | ||
873 | ((eq what 'priority) | |
874 | (when (looking-at org-complex-heading-regexp) | |
875 | (setq current (and (match-end 3) (substring (match-string 3) 2 3))) | |
876 | (cond | |
877 | ((equal current new) t) ; no action required | |
878 | ((or (equal current old) | |
879 | (eq org-mobile-force-mobile-change t) | |
880 | (memq 'tags org-mobile-force-mobile-change)) | |
881 | (org-priority (and new (string-to-char new)))) | |
882 | (t (error "Priority was expected to be %s, but is %s" | |
883 | old current))))) | |
884 | ||
885 | ((eq what 'heading) | |
886 | (when (looking-at org-complex-heading-regexp) | |
887 | (setq current (match-string 4)) | |
888 | (cond | |
889 | ((equal current new) t) ; no action required | |
890 | ((or (equal current old) | |
891 | (eq org-mobile-force-mobile-change t) | |
892 | (memq 'heading org-mobile-force-mobile-change)) | |
893 | (goto-char (match-beginning 4)) | |
894 | (insert new) | |
895 | (delete-region (point) (+ (point) (length current))) | |
896 | (org-set-tags nil 'align)) | |
897 | (t (error "Heading changed in MobileOrg and on the computer"))))) | |
898 | ||
899 | ((eq what 'body) | |
900 | (setq current (buffer-substring (min (1+ (point-at-eol)) (point-max)) | |
901 | (save-excursion (outline-next-heading) | |
902 | (point)))) | |
903 | (if (not (string-match "\\S-" current)) (setq current nil)) | |
904 | (cond | |
905 | ((org-mobile-bodies-same-p current new) t) ; no action necessary | |
906 | ((or (org-mobile-bodies-same-p current old) | |
907 | (eq org-mobile-force-mobile-change t) | |
908 | (memq 'body org-mobile-force-mobile-change)) | |
909 | (save-excursion | |
910 | (end-of-line 1) | |
911 | (insert "\n" new) | |
912 | (or (bolp) (insert "\n")) | |
913 | (delete-region (point) (progn (org-back-to-heading t) | |
914 | (outline-next-heading) | |
915 | (point)))) | |
916 | t) | |
917 | (t (error "Body was changed in MobileOrg and on the computer"))))))) | |
918 | ||
919 | ||
920 | (defun org-mobile-tags-same-p (list1 list2) | |
921 | "Are the two tag lists the same?" | |
922 | (not (or (org-delete-all list1 list2) | |
923 | (org-delete-all list2 list1)))) | |
924 | ||
925 | (defun org-mobile-bodies-same-p (a b) | |
926 | "Compare if A and B are visually equal strings. | |
927 | We first remove leading and trailing white space from the entire strings. | |
928 | Then we split the strings into lines and remove leading/trailing whitespace | |
929 | from each line. Then we compare. | |
930 | A and B must be strings or nil." | |
931 | (cond | |
932 | ((and (not a) (not b)) t) | |
933 | ((or (not a) (not b)) nil) | |
934 | (t (setq a (org-trim a) b (org-trim b)) | |
935 | (setq a (mapconcat 'identity (org-split-string a "[ \t]*\n[ \t]*") "\n")) | |
936 | (setq b (mapconcat 'identity (org-split-string b "[ \t]*\n[ \t]*") "\n")) | |
937 | (equal a b)))) | |
938 | ||
8d642074 CD |
939 | (provide 'org-mobile) |
940 | ||
941 | ;; arch-tag: ace0e26c-58f2-4309-8a61-05ec1535f658 | |
942 | ||
943 | ;;; org-mobile.el ends here | |
944 |