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