Merge commit 'johnw/master'
[org-mode.git] / lisp / org-mobile.el
bloba5414ead712ee652001c2abb9ce03bb837f1bd97
1 ;;; org-mobile.el --- Code for asymmetric sync with a mobile device
2 ;; Copyright (C) 2009 Free Software Foundation, Inc.
3 ;;
4 ;; Author: Carsten Dominik <carsten at orgmode dot org>
5 ;; Keywords: outlines, hypermedia, calendar, wp
6 ;; Homepage: http://orgmode.org
7 ;; Version: 6.32trans
8 ;;
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;;; Commentary:
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.
31 ;; Any external viewer/flagging/editing application that uses the same
32 ;; conventions could be used.
34 (require 'org)
35 (require 'org-agenda)
36 (eval-when-compile (require 'cl))
38 (defgroup org-mobile nil
39 "Options concerning support for a viewer/editor on a mobile device."
40 :tag "Org Mobile"
41 :group 'org)
43 (defcustom org-mobile-files '(org-agenda-files)
44 "Files to be staged for MobileOrg.
45 This is basically a list of filesand directories. Files will be staged
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:
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))))
63 (defcustom org-mobile-directory ""
64 "The WebDAV directory where the interaction with the mobile takes place."
65 :group 'org-mobile
66 :type 'directory)
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
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."
74 :group 'org-mobile
75 :type 'file)
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.")
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)
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)
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))))
111 (defcustom org-mobile-action-alist
112 '(("edit" . (org-mobile-edit data old new)))
113 "Alist with flags and actions for mobile sync.
114 When flagging an entry, MobileOrg will create entries that look like
116 * F(action:data) [[id:entry-id][entry title]]
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
124 of that entry.
126 For now, it is not recommended to change this variable."
127 :group 'org-mobile
128 :type '(repeat
129 (cons (string :tag "Action flag")
130 (sexp :tag "Action form"))))
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)
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.")
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'.")
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'.")
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'.")
168 (defvar org-mobile-last-flagged-files nil
169 "List of files containing entreis flagged in the latest pull.")
171 (defvar org-mobile-files-alist nil)
172 (defvar org-mobile-checksum-files nil)
174 (defun org-mobile-prepare-file-lists ()
175 (setq org-mobile-files-alist (org-mobile-files-alist))
176 (setq org-mobile-checksum-files nil))
178 (defun org-mobile-files-alist ()
179 "Expand the list in `org-mobile-files' to a list of existing files."
180 (let* ((files
181 (apply 'append (mapcar
182 (lambda (f)
183 (cond
184 ((eq f 'org-agenda-files) (org-agenda-files t))
185 ((eq f 'org-agenda-text-search-extra-files)
186 org-agenda-text-search-extra-files)
187 ((and (stringp f) (file-directory-p f))
188 (directory-files f 'full "\\.org\\'"))
189 ((and (stringp f) (file-exists-p f))
190 (list f))
191 (t nil)))
192 org-mobile-files)))
193 (orgdir-uname (file-name-as-directory (file-truename org-directory)))
194 (orgdir-re (concat "\\`" (regexp-quote orgdir-uname)))
195 uname seen rtn file link-name)
196 ;; Make the files unique, and determine the name under which they will
197 ;; be listed.
198 (while (setq file (pop files))
199 (if (not (file-name-absolute-p file))
200 (setq file (expand-file-name file org-directory)))
201 (setq uname (file-truename file))
202 (unless (member uname seen)
203 (push uname seen)
204 (if (string-match orgdir-re uname)
205 (setq link-name (substring uname (match-end 0)))
206 (setq link-name (file-name-nondirectory uname)))
207 (push (cons file link-name) rtn)))
208 (nreverse rtn)))
210 ;;;###autoload
211 (defun org-mobile-push ()
212 "Push the current state of Org affairs to the WebDAV directory.
213 This will create the index file, copy all agenda files there, and also
214 create all custom agenda views, for upload to the mobile phone."
215 (interactive)
216 (org-mobile-check-setup)
217 (org-mobile-prepare-file-lists)
218 (run-hooks 'org-mobile-pre-push-hook)
219 (org-mobile-create-sumo-agenda)
220 (org-save-all-org-buffers) ; to save any IDs created by this process
221 (org-mobile-copy-agenda-files)
222 (org-mobile-create-index-file)
223 (org-mobile-write-checksums)
224 (run-hooks 'org-mobile-post-push-hook)
225 (message "Files for mobile viewer staged"))
227 ;;;###autoload
228 (defun org-mobile-pull ()
229 "Pull the contents of `org-mobile-capture-file' and integrate them.
230 Apply all flagged actions, flag entries to be flagged and then call an
231 agenda view showing the flagged items."
232 (interactive)
233 (org-mobile-check-setup)
234 (run-hooks 'org-mobile-pre-pull-hook)
235 (let ((insertion-marker (org-mobile-move-capture)))
236 (if (not (markerp insertion-marker))
237 (message "No new items")
238 (org-with-point-at insertion-marker
239 (org-mobile-apply (point) (point-max)))
240 (move-marker insertion-marker nil)
241 (run-hooks 'org-mobile-post-pull-hook)
242 (when org-mobile-last-flagged-files
243 ;; Make an agenda view of flagged entries, but only in the files
244 ;; where stuff has been added.
245 (put 'org-agenda-files 'org-restrict org-mobile-last-flagged-files)
246 (let ((org-agenda-keep-restriced-file-list t))
247 (org-agenda nil "?"))))))
249 (defun org-mobile-check-setup ()
250 "Check if org-mobile-directory has been set up."
251 (unless (and org-directory
252 (stringp org-directory)
253 (string-match "\\S-" org-directory)
254 (file-exists-p org-directory)
255 (file-directory-p org-directory))
256 (error
257 "Please set `org-directory' to the directory where your org files live"))
258 (unless (and org-mobile-directory
259 (stringp org-mobile-directory)
260 (string-match "\\S-" org-mobile-directory)
261 (file-exists-p org-mobile-directory)
262 (file-directory-p org-mobile-directory))
263 (error
264 "Variable `org-mobile-directory' must point to an existing directory"))
265 (unless (and org-mobile-inbox-for-pull
266 (stringp org-mobile-inbox-for-pull)
267 (string-match "\\S-" org-mobile-inbox-for-pull)
268 (file-exists-p
269 (file-name-directory org-mobile-inbox-for-pull)))
270 (error
271 "Variable `org-mobile-inbox-for-pull' must point to a file in an existing directory")))
273 (defun org-mobile-create-index-file ()
274 "Write the index file in the WebDAV directory."
275 (let ((files-alist (sort (copy-sequence org-mobile-files-alist)
276 (lambda (a b) (string< (cdr a) (cdr b)))))
277 (def-todo (default-value 'org-todo-keywords))
278 (def-tags (default-value 'org-tag-alist))
279 file link-name todo-kwds done-kwds tags drawers entry kwds dwds twds)
281 (org-prepare-agenda-buffers (mapcar 'car files-alist))
282 (setq done-kwds (org-uniquify org-done-keywords-for-agenda))
283 (setq todo-kwds (org-delete-all
284 done-kwds
285 (org-uniquify org-todo-keywords-for-agenda)))
286 (setq drawers (org-uniquify org-drawers-for-agenda))
287 (setq tags (org-uniquify
288 (delq nil
289 (mapcar
290 (lambda (e)
291 (cond ((stringp e) e)
292 ((listp e)
293 (if (stringp (car e)) (car e) nil))
294 (t nil)))
295 org-tag-alist-for-agenda))))
296 (with-temp-file
297 (expand-file-name org-mobile-index-file org-mobile-directory)
298 (while (setq entry (pop def-todo))
299 (insert "#+READONLY\n")
300 (setq kwds (mapcar (lambda (x) (if (string-match "(" x)
301 (substring x 0 (match-beginning 0))
303 (cdr entry)))
304 (insert "#+TODO: " (mapconcat 'identity kwds " ") "\n")
305 (setq dwds (member "|" kwds)
306 twds (org-delete-all dwds kwds)
307 todo-kwds (org-delete-all twds todo-kwds)
308 done-kwds (org-delete-all dwds done-kwds)))
309 (when (or todo-kwds done-kwds)
310 (insert "#+TODO: " (mapconcat 'identity todo-kwds " ") " | "
311 (mapconcat 'identity done-kwds " ") "\n"))
312 (setq def-tags (mapcar
313 (lambda (x)
314 (cond ((null x) nil)
315 ((stringp x) x)
316 ((eq (car x) :startgroup) "{")
317 ((eq (car x) :endgroup) "}")
318 ((eq (car x) :newline) nil)
319 ((listp x) (car x))
320 (t nil)))
321 def-tags))
322 (setq def-tags (delq nil def-tags))
323 (setq tags (org-delete-all def-tags tags))
324 (setq tags (sort tags (lambda (a b) (string< (downcase a) (downcase b)))))
325 (setq tags (append def-tags tags nil))
326 (insert "#+TAGS: " (mapconcat 'identity tags " ") "\n")
327 (insert "#+DRAWERS: " (mapconcat 'identity drawers " ") "\n")
328 (insert "#+ALLPRIORITIES: A B C" "\n")
329 (when (file-exists-p (expand-file-name
330 org-mobile-directory "agendas.org"))
331 (insert "* [[file:agendas.org][Agenda Views]]\n"))
332 (while (setq entry (pop files-alist))
333 (setq file (car entry)
334 link-name (cdr entry))
335 (insert (format "* [[file:%s][%s]]\n"
336 link-name link-name)))
337 (push (cons org-mobile-index-file (md5 (buffer-string)))
338 org-mobile-checksum-files))))
340 (defun org-mobile-copy-agenda-files ()
341 "Copy all agenda files to the stage or WebDAV directory."
342 (let ((files-alist org-mobile-files-alist)
343 file buf entry link-name target-path target-dir check)
344 (while (setq entry (pop files-alist))
345 (setq file (car entry) link-name (cdr entry))
346 (when (file-exists-p file)
347 (setq target-path (expand-file-name link-name org-mobile-directory)
348 target-dir (file-name-directory target-path))
349 (unless (file-directory-p target-dir)
350 (make-directory target-dir 'parents))
351 (copy-file file target-path 'ok-if-exists)
352 (setq check (shell-command-to-string
353 (concat org-mobile-checksum-binary " "
354 (shell-quote-argument (expand-file-name file)))))
355 (when (string-match "[a-fA-F0-9]\\{30,40\\}" check)
356 (push (cons link-name (match-string 0 check))
357 org-mobile-checksum-files))))
358 (setq file (expand-file-name org-mobile-capture-file
359 org-mobile-directory))
360 (save-excursion
361 (setq buf (find-file file))
362 (and (= (point-min) (point-max)) (insert "\n"))
363 (save-buffer)
364 (push (cons org-mobile-capture-file (md5 (buffer-string)))
365 org-mobile-checksum-files))
366 (kill-buffer buf)))
368 (defun org-mobile-write-checksums ()
369 "Create checksums for all files in `org-mobile-directory'.
370 The table of checksums is written to the file mobile-checksums."
371 (let ((sumfile (expand-file-name "checksums.dat" org-mobile-directory))
372 (files org-mobile-checksum-files)
373 entry file sum)
374 (with-temp-file sumfile
375 (while (setq entry (pop files))
376 (setq file (car entry) sum (cdr entry))
377 (insert (format "%s %s\n" sum file))))))
379 (defun org-mobile-sumo-agenda-command ()
380 "Return an agenda custom command that comprises all custom commands."
381 (let ((custom-list
382 ;; normalize different versions
383 (delq nil
384 (mapcar
385 (lambda (x)
386 (cond ((stringp (cdr x)) nil)
387 ((stringp (nth 1 x)) x)
388 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
389 (t (cons (car x) (cons "" (cdr x))))))
390 org-agenda-custom-commands)))
391 new e key desc type match settings cmds gkey gdesc gsettings cnt)
392 (while (setq e (pop custom-list))
393 (cond
394 ((stringp (cdr e))
395 ;; this is a description entry - skip it
397 ((eq (nth 2 e) 'search)
398 ;; Search view is interactive, skip
400 ((memq (nth 2 e) '(todo-tree tags-tree occur-tree))
401 ;; These are trees, not really agenda commands
403 ((memq (nth 2 e) '(agenda todo tags))
404 ;; a normal command
405 (setq key (car e) desc (nth 1 e) type (nth 2 e) match (nth 3 e)
406 settings (nth 4 e))
407 (setq settings
408 (cons (list 'org-agenda-title-append
409 (concat "<after>KEYS=" key " TITLE: "
410 (if (and (stringp desc) (> (length desc) 0))
411 desc (symbol-name type))
412 " " match "</after>"))
413 settings))
414 (push (list type match settings) new))
415 ((symbolp (nth 2 e))
416 ;; A user-defined function, not sure how to handle that yet
419 ;; a block agenda
420 (setq gkey (car e) gdesc (nth 1 e) gsettings (nth 3 e) cmds (nth 2 e))
421 (setq cnt 0)
422 (while (setq e (pop cmds))
423 (setq type (car e) match (nth 1 e) settings (nth 2 e))
424 (setq settings (append gsettings settings))
425 (setq settings
426 (cons (list 'org-agenda-title-append
427 (concat "<after>KEYS=" gkey "#" (number-to-string
428 (setq cnt (1+ cnt)))
429 " TITLE: " gdesc " " match "</after>"))
430 settings))
431 (push (list type match settings) new)))))
432 (and new (list "X" "SUMO" (reverse new)
433 '((org-agenda-compact-blocks nil))))))
435 (defvar org-mobile-creating-agendas nil)
436 (defun org-mobile-write-agenda-for-mobile (file)
437 (let ((all (buffer-string)) in-date id pl prefix line app short m sexp)
438 (with-temp-file file
439 (org-mode)
440 (insert "#+READONLY\n")
441 (insert all)
442 (goto-char (point-min))
443 (while (not (eobp))
444 (cond
445 ((looking-at "[ \t]*$")) ; keep empty lines
446 ((looking-at "=+$")
447 ;; remove underlining
448 (delete-region (point) (point-at-eol)))
449 ((get-text-property (point) 'org-agenda-structural-header)
450 (setq in-date nil)
451 (setq app (get-text-property (point)
452 'org-agenda-title-append))
453 (setq short (get-text-property (point)
454 'short-heading))
455 (when (and short (looking-at ".+"))
456 (replace-match short)
457 (beginning-of-line 1))
458 (when app
459 (end-of-line 1)
460 (insert app)
461 (beginning-of-line 1))
462 (insert "* "))
463 ((get-text-property (point) 'org-agenda-date-header)
464 (setq in-date t)
465 (insert "** "))
466 ((setq m (or (get-text-property (point) 'org-hd-marker)
467 (get-text-property (point) 'org-marker)))
468 (setq sexp (member (get-text-property (point) 'type)
469 '("diary" "sexp")))
470 (if (setq pl (get-text-property (point) 'prefix-length))
471 (progn
472 (setq prefix (org-trim (buffer-substring
473 (point) (+ (point) pl)))
474 line (org-trim (buffer-substring
475 (+ (point) pl)
476 (point-at-eol))))
477 (delete-region (point-at-bol) (point-at-eol))
478 (insert line "<before>" prefix "</before>")
479 (beginning-of-line 1))
480 (and (looking-at "[ \t]+") (replace-match "")))
481 (insert (if in-date "*** " "** "))
482 (end-of-line 1)
483 (insert "\n")
484 (unless sexp
485 (insert (org-agenda-get-some-entry-text
486 m 10 " " 'planning)
487 "\n")
488 (when (setq id
489 (if (org-bound-and-true-p
490 org-mobile-force-id-on-agenda-items)
491 (org-id-get m 'create)
492 (org-entry-get m "ID")))
493 (insert " :PROPERTIES:\n :ORIGINAL_ID: " id
494 "\n :END:\n")))))
495 (beginning-of-line 2))
496 (push (cons (file-name-nondirectory file) (md5 (buffer-string)))
497 org-mobile-checksum-files))
498 (message "Agenda written to Org file %s" file)))
500 ;;;###autoload
501 (defun org-mobile-create-sumo-agenda ()
502 "Create a file that contains all custom agenda views."
503 (interactive)
504 (let* ((file (expand-file-name "agendas.org"
505 org-mobile-directory))
506 (sumo (org-mobile-sumo-agenda-command))
507 (org-agenda-custom-commands
508 (list (append sumo (list (list file)))))
509 (org-mobile-creating-agendas t))
510 (unless (file-writable-p file)
511 (error "Cannot write to file %s" file))
512 (when sumo
513 (org-store-agenda-views))))
515 (defun org-mobile-move-capture ()
516 "Move the contents of the capture file to the inbox file.
517 Return a marker to the location where the new content has been added.
518 If nothing new has beed added, return nil."
519 (interactive)
520 (let ((inbox-buffer (find-file-noselect org-mobile-inbox-for-pull))
521 (capture-buffer (find-file-noselect
522 (expand-file-name org-mobile-capture-file
523 org-mobile-directory)))
524 (insertion-point (make-marker))
525 not-empty content)
526 (save-excursion
527 (set-buffer capture-buffer)
528 (setq content (buffer-string))
529 (setq not-empty (string-match "\\S-" content))
530 (when not-empty
531 (set-buffer inbox-buffer)
532 (widen)
533 (goto-char (point-max))
534 (or (bolp) (newline))
535 (move-marker insertion-point
536 (prog1 (point) (insert content)))
537 (save-buffer)
538 (set-buffer capture-buffer)
539 (erase-buffer)
540 (save-buffer)
541 (org-mobile-update-checksum-for-capture-file (buffer-string))))
542 (kill-buffer capture-buffer)
543 (if not-empty insertion-point)))
545 (defun org-mobile-update-checksum-for-capture-file (buffer-string)
546 (let* ((file (expand-file-name "checksums.dat" org-mobile-directory))
547 (buffer (find-file-noselect file)))
548 (when buffer
549 (with-current-buffer buffer
550 (when (re-search-forward (concat "\\([0-9a-fA-F]\\{30,\\}\\).*?"
551 (regexp-quote org-mobile-capture-file)
552 "[ \t]*$") nil t)
553 (goto-char (match-beginning 1))
554 (delete-region (match-beginning 1) (match-end 1))
555 (insert (md5 buffer-string))
556 (save-buffer)))
557 (kill-buffer buffer))))
559 (defun org-mobile-apply (&optional beg end)
560 "Apply all change requests in the current buffer.
561 If BEG and END are given, only do this in that region."
562 (interactive)
563 (require 'org-archive)
564 (setq org-mobile-last-flagged-files nil)
565 (setq beg (or beg (point-min)) end (or end (point-max)))
567 ;; Remove all Note IDs
568 (goto-char beg)
569 (while (re-search-forward "^\\*\\* Note ID: [-0-9A-F]+[ \t]*\n" end t)
570 (replace-match ""))
572 ;; Find all the referenced entries, without making any changes yet
573 (let ((marker (make-marker))
574 (bos-marker (make-marker))
575 (end (move-marker (make-marker) end))
576 (cnt-new 0)
577 (cnt-edit 0)
578 (cnt-flag 0)
579 (cnt-error 0)
580 buf-list
581 id-pos org-mobile-error)
583 ;; Count the new captures
584 (goto-char beg)
585 (while (re-search-forward "^\\* \\(.*\\)" end t)
586 (and (>= (- (match-end 1) (match-beginning 1)) 2)
587 (not (equal (downcase (substring (match-string 1) 0 2)) "f("))
588 (incf cnt-new)))
590 (goto-char beg)
591 (while (re-search-forward
592 "^\\*+[ \t]+F(\\([^():\n]*\\)\\(:\\([^()\n]*\\)\\)?)[ \t]+\\[\\[\\(\\(id\\|olp\\):\\([^]\n]+\\)\\)" end t)
593 (setq id-pos (condition-case msg
594 (org-mobile-locate-entry (match-string 4))
595 (error (nth 1 msg))))
596 (when (and (markerp id-pos)
597 (not (member (marker-buffer id-pos) buf-list)))
598 (org-mobile-timestamp-buffer (marker-buffer id-pos))
599 (push (marker-buffer id-pos) buf-list))
601 (if (or (not id-pos) (stringp id-pos))
602 (progn
603 (goto-char (+ 2 (point-at-bol)))
604 (insert id-pos " ")
605 (incf cnt-error))
606 (add-text-properties (point-at-bol) (point-at-eol)
607 (list 'org-mobile-marker
608 (or id-pos "Linked entry not found")))))
610 ;; OK, now go back and start applying
611 (goto-char beg)
612 (while (re-search-forward "^\\*+[ \t]+F(\\([^():\n]*\\)\\(:\\([^()\n]*\\)\\)?)" end t)
613 (catch 'next
614 (setq id-pos (get-text-property (point-at-bol) 'org-mobile-marker))
615 (if (not (markerp id-pos))
616 (progn
617 (incf cnt-error)
618 (insert "UNKNOWN PROBLEM"))
619 (let* ((action (match-string 1))
620 (data (and (match-end 3) (match-string 3)))
621 (bos (point-at-bol))
622 (eos (save-excursion (org-end-of-subtree t t)))
623 (cmd (if (equal action "")
624 '(progn
625 (incf cnt-flag)
626 (org-toggle-tag "FLAGGED" 'on)
627 (and note
628 (org-entry-put nil "THEFLAGGINGNOTE" note)))
629 (incf cnt-edit)
630 (cdr (assoc action org-mobile-action-alist))))
631 (note (and (equal action "")
632 (buffer-substring (1+ (point-at-eol)) eos)))
633 (org-inhibit-logging 'note) ;; Do not take notes interactively
634 old new)
635 (goto-char bos)
636 (move-marker bos-marker (point))
637 (if (re-search-forward "^** Old value[ \t]*$" eos t)
638 (setq old (buffer-substring
639 (1+ (match-end 0))
640 (progn (outline-next-heading) (point)))))
641 (if (re-search-forward "^** New value[ \t]*$" eos t)
642 (setq new (buffer-substring
643 (1+ (match-end 0))
644 (progn (outline-next-heading)
645 (if (eobp) (org-back-over-empty-lines))
646 (point)))))
647 (setq old (and old (if (string-match "\\S-" old) old nil)))
648 (setq new (and new (if (string-match "\\S-" new) new nil)))
649 (if (and note (> (length note) 0))
650 ;; Make Note into a single line, to fit into a property
651 (setq note (mapconcat 'identity
652 (org-split-string (org-trim note) "\n")
653 "\\n")))
654 (unless (equal data "body")
655 (setq new (and new (org-trim new))
656 old (and old (org-trim old))))
657 (goto-char (+ 2 bos-marker))
658 (unless (markerp id-pos)
659 (insert "BAD REFERENCE ")
660 (incf cnt-error)
661 (throw 'next t))
662 (unless cmd
663 (insert "BAD FLAG ")
664 (incf cnt-error)
665 (throw 'next t))
666 ;; Remember this place so tha we can return
667 (move-marker marker (point))
668 (setq org-mobile-error nil)
669 (save-excursion
670 (condition-case msg
671 (org-with-point-at id-pos
672 (progn
673 (eval cmd)
674 (if (member "FLAGGED" (org-get-tags))
675 (add-to-list 'org-mobile-last-flagged-files
676 (buffer-file-name (current-buffer))))))
677 (error (setq org-mobile-error msg))))
678 (when org-mobile-error
679 (switch-to-buffer (marker-buffer marker))
680 (goto-char marker)
681 (incf cnt-error)
682 (insert (if (stringp (nth 1 org-mobile-error))
683 (nth 1 org-mobile-error)
684 "EXECUTION FAILED")
685 " ")
686 (throw 'next t))
687 ;; If we get here, the action has been applied successfully
688 ;; So remove the entry
689 (goto-char bos-marker)
690 (delete-region (point) (org-end-of-subtree t t))))))
691 (save-buffer)
692 (move-marker marker nil)
693 (move-marker end nil)
694 (message "%d new, %d edits, %d flags, %d errors" cnt-new
695 cnt-edit cnt-flag cnt-error)
696 (sit-for 1)))
698 (defun org-mobile-timestamp-buffer (buf)
699 "Time stamp buffer BUF, just to make sure its checksum will change."
700 (with-current-buffer buf
701 (save-excursion
702 (save-restriction
703 (widen)
704 (goto-char (point-min))
705 (when (re-search-forward
706 "^\\([ \t]*\\)#\\+LAST_MOBILE_CHANGE:.*\n?" nil t)
707 (goto-char (match-end 1))
708 (delete-region (point) (match-end 0)))
709 (insert "#+LAST_MOBILE_CHANGE: "
710 (format-time-string "%Y-%m-%d %T") "\n")))))
712 (defun org-mobile-smart-read ()
713 "Parse the entry at point for shortcuts and expand them.
714 These shortcuts are meant for fast and easy typing on the limited
715 keyboards of a mobile device. Below we show a list of the shortcuts
716 currently implemented.
718 The entry is expected to contain an inactive time stamp indicating when
719 the entry was created. When setting dates and
720 times (for example for deadlines), the time strings are interpreted
721 relative to that creation date.
722 Abbreviations are expected to take up entire lines, jst because it is so
723 easy to type RET on a mobile device. Abbreviations start with one or two
724 letters, followed immediately by a dot and then additional information.
725 Generally the entire shortcut line is removed after action have been taken.
726 Time stamps will be constructed using `org-read-date'. So for example a
727 line \"dd. 2tue\" will set a deadline on the second Tuesday after the
728 creation date.
730 Here are the shortcuts currently implemented:
732 dd. string set deadline
733 ss. string set scheduling
734 tt. string set time tamp, here.
735 ti. string set inactive time
737 tg. tag1 tag2 tag3 set all these tags, change case where necessary
738 td. kwd set this todo keyword, change case where necessary
740 FIXME: Hmmm, not sure if we can make his work against the
741 auto-correction feature. Needs a bit more thinking. So this function
742 is currently a noop.")
745 (defun org-find-olp (path)
746 "Return a marker pointing to the entry at outline path OLP.
747 If anything goes wrong, the return value will instead an error message,
748 as a string."
749 (let* ((file (pop path))
750 (buffer (find-file-noselect file))
751 (level 1)
752 (lmin 1)
753 (lmax 1)
754 limit re end found pos heading cnt)
755 (unless buffer (error "File not found :%s" file))
756 (with-current-buffer buffer
757 (save-excursion
758 (save-restriction
759 (widen)
760 (setq limit (point-max))
761 (goto-char (point-min))
762 (while (setq heading (pop path))
763 (setq re (format org-complex-heading-regexp-format
764 (regexp-quote heading)))
765 (setq cnt 0 pos (point))
766 (while (re-search-forward re end t)
767 (setq level (- (match-end 1) (match-beginning 1)))
768 (if (and (>= level lmin) (<= level lmax))
769 (setq found (match-beginning 0) cnt (1+ cnt))))
770 (when (= cnt 0) (error "Heading not found on level %d: %s"
771 lmax heading))
772 (when (> cnt 1) (error "Heading not unique on level %d: %s"
773 lmax heading))
774 (goto-char found)
775 (setq lmin (1+ level) lmax (+ lmin (if org-odd-levels-only 1 0)))
776 (setq end (save-excursion (org-end-of-subtree t t))))
777 (when (org-on-heading-p)
778 (move-marker (make-marker) (point))))))))
780 (defun org-mobile-locate-entry (link)
781 (if (string-match "\\`id:\\(.*\\)$" link)
782 (org-id-find (match-string 1 link) 'marker)
783 (if (not (string-match "\\`olp:\\(.*?\\):\\(.*\\)$" link))
785 (let ((file (match-string 1 link))
786 (path (match-string 2 link))
787 (table '((?: . "%3a") (?\[ . "%5b") (?\] . "%5d") (?/ . "%2f"))))
788 (setq file (org-link-unescape file table))
789 (setq file (expand-file-name file org-directory))
790 (setq path (mapcar (lambda (x) (org-link-unescape x table))
791 (org-split-string path "/")))
792 (org-find-olp (cons file path))))))
794 (defun org-mobile-edit (what old new)
795 "Edit item WHAT in the current entry by replacing OLD wih NEW.
796 WHAT can be \"heading\", \"todo\", \"tags\", \"priority\", or \"body\".
797 The edit only takes place if the current value is equal (except for
798 white space) the OLD. If this is so, OLD will be replace by NEW
799 and the command will return t. If something goes wrong, a string will
800 be returned that indicates what went wrong."
801 (let (current old1 new1)
802 (if (stringp what) (setq what (intern what)))
804 (cond
806 ((memq what '(todo todostate))
807 (setq current (org-get-todo-state))
808 (cond
809 ((equal new "DONEARCHIVE")
810 (org-todo 'done)
811 (org-archive-subtree-default))
812 ((equal new current) t) ; nothing needs to be done
813 ((or (equal current old)
814 (eq org-mobile-force-mobile-change t)
815 (memq 'todo org-mobile-force-mobile-change))
816 (org-todo (or new 'none)) t)
817 (t (error "State before change was expected as \"%s\", but is \"%s\""
818 old current))))
820 ((eq what 'tags)
821 (setq current (org-get-tags)
822 new1 (and new (org-split-string new ":+"))
823 old1 (and old (org-split-string old ":+")))
824 (cond
825 ((org-mobile-tags-same-p current new1) t) ; no change needed
826 ((or (org-mobile-tags-same-p current old1)
827 (eq org-mobile-force-mobile-change t)
828 (memq 'tags org-mobile-force-mobile-change))
829 (org-set-tags-to new1) t)
830 (t (error "Tags before change were expected as \"%s\", but are \"%s\""
831 (or old "") (or current "")))))
833 ((eq what 'priority)
834 (when (looking-at org-complex-heading-regexp)
835 (setq current (and (match-end 3) (substring (match-string 3) 2 3)))
836 (cond
837 ((equal current new) t) ; no action required
838 ((or (equal current old)
839 (eq org-mobile-force-mobile-change t)
840 (memq 'tags org-mobile-force-mobile-change))
841 (org-priority (and new (string-to-char new))))
842 (t (error "Priority was expected to be %s, but is %s"
843 old current)))))
845 ((eq what 'heading)
846 (when (looking-at org-complex-heading-regexp)
847 (setq current (match-string 4))
848 (cond
849 ((equal current new) t) ; no action required
850 ((or (equal current old)
851 (eq org-mobile-force-mobile-change t)
852 (memq 'heading org-mobile-force-mobile-change))
853 (goto-char (match-beginning 4))
854 (insert new)
855 (delete-region (point) (+ (point) (length current)))
856 (org-set-tags nil 'align))
857 (t (error "Heading changed in MobileOrg and on the computer")))))
859 ((eq what 'body)
860 (setq current (buffer-substring (min (1+ (point-at-eol)) (point-max))
861 (save-excursion (outline-next-heading)
862 (point))))
863 (if (not (string-match "\\S-" current)) (setq current nil))
864 (cond
865 ((org-mobile-bodies-same-p current new) t) ; no ation necesary
866 ((or (org-mobile-bodies-same-p current old)
867 (eq org-mobile-force-mobile-change t)
868 (memq 'body org-mobile-force-mobile-change))
869 (save-excursion
870 (end-of-line 1)
871 (insert "\n" new)
872 (or (bolp) (insert "\n"))
873 (delete-region (point) (progn (org-back-to-heading t)
874 (outline-next-heading)
875 (point))))
877 (t (error "Body was changed in MobileOrg and on the computer")))))))
880 (defun org-mobile-tags-same-p (list1 list2)
881 "Are the two tag lists the same?"
882 (not (or (org-delete-all list1 list2)
883 (org-delete-all list2 list1))))
885 (defun org-mobile-bodies-same-p (a b)
886 "Compare if A and B are visually equal strings.
887 We first remove leading and trailing white space from the entire strings.
888 Then we split the strings into lines and remove leading/trailing whitespace
889 from each line. Then we compare.
890 A and B must be strings or nil."
891 (cond
892 ((and (not a) (not b)) t)
893 ((or (not a) (not b)) nil)
894 (t (setq a (org-trim a) b (org-trim b))
895 (setq a (mapconcat 'identity (org-split-string a "[ \t]*\n[ \t]*") "\n"))
896 (setq b (mapconcat 'identity (org-split-string b "[ \t]*\n[ \t]*") "\n"))
897 (equal a b))))
899 (provide 'org-mobile)
901 ;; arch-tag: ace0e26c-58f2-4309-8a61-05ec1535f658
903 ;;; org-mobile.el ends here