1 ;;; org-mobile.el --- Code for asymmetric sync with a mobile device
2 ;; Copyright (C) 2009 Free Software Foundation, Inc.
4 ;; Author: Carsten Dominik <carsten at orgmode dot org>
5 ;; Keywords: outlines, hypermedia, calendar, wp
6 ;; Homepage: http://orgmode.org
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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.
36 (eval-when-compile (require 'cl
))
38 (defgroup org-mobile nil
39 "Options concerning support for a viewer/editor on a mobile device."
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:
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'"
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"
63 (defcustom org-mobile-directory
""
64 "The WebDAV directory where the interaction with the mobile takes place."
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."
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."
88 (defcustom org-mobile-force-id-on-agenda-items t
89 "Non-nil means make all agenda items carry and ID."
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."
102 (const :tag
"Always" t
)
103 (const :tag
"Never" nil
)
104 (set :greedy t
:tag
"Specify"
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
126 For now, it is not recommended to change this variable."
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."
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* ((include-archives
181 (and (member 'org-agenda-text-search-extra-files org-mobile-files
)
182 (member 'agenda-archives org-agenda-text-search-extra-files
)
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
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
))
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
206 (while (setq file
(pop files
))
207 (if (not (file-name-absolute-p file
))
208 (setq file
(expand-file-name file org-directory
)))
209 (setq uname
(file-truename file
))
210 (unless (member 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
)))
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."
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
))
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
))))
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
))
252 (select-window cw
)))))
253 (message "Files for mobile viewer staged"))
256 (defun org-mobile-pull ()
257 "Pull the contents of `org-mobile-capture-file' and integrate them.
258 Apply all flagged actions, flag entries to be flagged and then call an
259 agenda view showing the flagged items."
261 (org-mobile-check-setup)
262 (run-hooks 'org-mobile-pre-pull-hook
)
263 (let ((insertion-marker (org-mobile-move-capture)))
264 (if (not (markerp insertion-marker
))
265 (message "No new items")
266 (org-with-point-at insertion-marker
267 (org-mobile-apply (point) (point-max)))
268 (move-marker insertion-marker nil
)
269 (run-hooks 'org-mobile-post-pull-hook
)
270 (when org-mobile-last-flagged-files
271 ;; Make an agenda view of flagged entries, but only in the files
272 ;; where stuff has been added.
273 (put 'org-agenda-files
'org-restrict org-mobile-last-flagged-files
)
274 (let ((org-agenda-keep-restriced-file-list t
))
275 (org-agenda nil
"?"))))))
277 (defun org-mobile-check-setup ()
278 "Check if org-mobile-directory has been set up."
279 (unless (and org-directory
280 (stringp org-directory
)
281 (string-match "\\S-" org-directory
)
282 (file-exists-p org-directory
)
283 (file-directory-p org-directory
))
285 "Please set `org-directory' to the directory where your org files live"))
286 (unless (and org-mobile-directory
287 (stringp org-mobile-directory
)
288 (string-match "\\S-" org-mobile-directory
)
289 (file-exists-p org-mobile-directory
)
290 (file-directory-p org-mobile-directory
))
292 "Variable `org-mobile-directory' must point to an existing directory"))
293 (unless (and org-mobile-inbox-for-pull
294 (stringp org-mobile-inbox-for-pull
)
295 (string-match "\\S-" org-mobile-inbox-for-pull
)
297 (file-name-directory org-mobile-inbox-for-pull
)))
299 "Variable `org-mobile-inbox-for-pull' must point to a file in an existing directory")))
301 (defun org-mobile-create-index-file ()
302 "Write the index file in the WebDAV directory."
303 (let ((files-alist (sort (copy-sequence org-mobile-files-alist
)
304 (lambda (a b
) (string< (cdr a
) (cdr b
)))))
305 (def-todo (default-value 'org-todo-keywords
))
306 (def-tags (default-value 'org-tag-alist
))
307 file link-name todo-kwds done-kwds tags drawers entry kwds dwds twds
)
309 (org-prepare-agenda-buffers (mapcar 'car files-alist
))
310 (setq done-kwds
(org-uniquify org-done-keywords-for-agenda
))
311 (setq todo-kwds
(org-delete-all
313 (org-uniquify org-todo-keywords-for-agenda
)))
314 (setq drawers
(org-uniquify org-drawers-for-agenda
))
315 (setq tags
(org-uniquify
319 (cond ((stringp e
) e
)
321 (if (stringp (car e
)) (car e
) nil
))
323 org-tag-alist-for-agenda
))))
325 (expand-file-name org-mobile-index-file org-mobile-directory
)
326 (while (setq entry
(pop def-todo
))
327 (insert "#+READONLY\n")
328 (setq kwds
(mapcar (lambda (x) (if (string-match "(" x
)
329 (substring x
0 (match-beginning 0))
332 (insert "#+TODO: " (mapconcat 'identity kwds
" ") "\n")
333 (setq dwds
(member "|" kwds
)
334 twds
(org-delete-all dwds kwds
)
335 todo-kwds
(org-delete-all twds todo-kwds
)
336 done-kwds
(org-delete-all dwds done-kwds
)))
337 (when (or todo-kwds done-kwds
)
338 (insert "#+TODO: " (mapconcat 'identity todo-kwds
" ") " | "
339 (mapconcat 'identity done-kwds
" ") "\n"))
340 (setq def-tags
(mapcar
344 ((eq (car x
) :startgroup
) "{")
345 ((eq (car x
) :endgroup
) "}")
346 ((eq (car x
) :newline
) nil
)
350 (setq def-tags
(delq nil def-tags
))
351 (setq tags
(org-delete-all def-tags tags
))
352 (setq tags
(sort tags
(lambda (a b
) (string< (downcase a
) (downcase b
)))))
353 (setq tags
(append def-tags tags nil
))
354 (insert "#+TAGS: " (mapconcat 'identity tags
" ") "\n")
355 (insert "#+DRAWERS: " (mapconcat 'identity drawers
" ") "\n")
356 (insert "#+ALLPRIORITIES: A B C" "\n")
357 (when (file-exists-p (expand-file-name
358 org-mobile-directory
"agendas.org"))
359 (insert "* [[file:agendas.org][Agenda Views]]\n"))
360 (while (setq entry
(pop files-alist
))
361 (setq file
(car entry
)
362 link-name
(cdr entry
))
363 (insert (format "* [[file:%s][%s]]\n"
364 link-name link-name
)))
365 (push (cons org-mobile-index-file
(md5 (buffer-string)))
366 org-mobile-checksum-files
))))
368 (defun org-mobile-copy-agenda-files ()
369 "Copy all agenda files to the stage or WebDAV directory."
370 (let ((files-alist org-mobile-files-alist
)
371 file buf entry link-name target-path target-dir check
)
372 (while (setq entry
(pop files-alist
))
373 (setq file
(car entry
) link-name
(cdr entry
))
374 (when (file-exists-p file
)
375 (setq target-path
(expand-file-name link-name org-mobile-directory
)
376 target-dir
(file-name-directory target-path
))
377 (unless (file-directory-p target-dir
)
378 (make-directory target-dir
'parents
))
379 (copy-file file target-path
'ok-if-exists
)
380 (setq check
(shell-command-to-string
381 (concat org-mobile-checksum-binary
" "
382 (shell-quote-argument (expand-file-name file
)))))
383 (when (string-match "[a-fA-F0-9]\\{30,40\\}" check
)
384 (push (cons link-name
(match-string 0 check
))
385 org-mobile-checksum-files
))))
386 (setq file
(expand-file-name org-mobile-capture-file
387 org-mobile-directory
))
389 (setq buf
(find-file file
))
390 (and (= (point-min) (point-max)) (insert "\n"))
392 (push (cons org-mobile-capture-file
(md5 (buffer-string)))
393 org-mobile-checksum-files
))
396 (defun org-mobile-write-checksums ()
397 "Create checksums for all files in `org-mobile-directory'.
398 The table of checksums is written to the file mobile-checksums."
399 (let ((sumfile (expand-file-name "checksums.dat" org-mobile-directory
))
400 (files org-mobile-checksum-files
)
402 (with-temp-file sumfile
403 (while (setq entry
(pop files
))
404 (setq file
(car entry
) sum
(cdr entry
))
405 (insert (format "%s %s\n" sum file
))))))
407 (defun org-mobile-sumo-agenda-command ()
408 "Return an agenda custom command that comprises all custom commands."
410 ;; normalize different versions
414 (cond ((stringp (cdr x
)) nil
)
415 ((stringp (nth 1 x
)) x
)
416 ((not (nth 1 x
)) (cons (car x
) (cons "" (cddr x
))))
417 (t (cons (car x
) (cons "" (cdr x
))))))
418 org-agenda-custom-commands
)))
419 new e key desc type match settings cmds gkey gdesc gsettings cnt
)
420 (while (setq e
(pop custom-list
))
423 ;; this is a description entry - skip it
425 ((eq (nth 2 e
) 'search
)
426 ;; Search view is interactive, skip
428 ((memq (nth 2 e
) '(todo-tree tags-tree occur-tree
))
429 ;; These are trees, not really agenda commands
431 ((memq (nth 2 e
) '(agenda todo tags
))
433 (setq key
(car e
) desc
(nth 1 e
) type
(nth 2 e
) match
(nth 3 e
)
436 (cons (list 'org-agenda-title-append
437 (concat "<after>KEYS=" key
" TITLE: "
438 (if (and (stringp desc
) (> (length desc
) 0))
439 desc
(symbol-name type
))
440 " " match
"</after>"))
442 (push (list type match settings
) new
))
444 ;; A user-defined function, not sure how to handle that yet
448 (setq gkey
(car e
) gdesc
(nth 1 e
) gsettings
(nth 3 e
) cmds
(nth 2 e
))
450 (while (setq e
(pop cmds
))
451 (setq type
(car e
) match
(nth 1 e
) settings
(nth 2 e
))
452 (setq settings
(append gsettings settings
))
454 (cons (list 'org-agenda-title-append
455 (concat "<after>KEYS=" gkey
"#" (number-to-string
457 " TITLE: " gdesc
" " match
"</after>"))
459 (push (list type match settings
) new
)))))
460 (and new
(list "X" "SUMO" (reverse new
)
461 '((org-agenda-compact-blocks nil
))))))
463 (defvar org-mobile-creating-agendas nil
)
464 (defun org-mobile-write-agenda-for-mobile (file)
465 (let ((all (buffer-string)) in-date id pl prefix line app short m sexp
)
468 (insert "#+READONLY\n")
470 (goto-char (point-min))
473 ((looking-at "[ \t]*$")) ; keep empty lines
475 ;; remove underlining
476 (delete-region (point) (point-at-eol)))
477 ((get-text-property (point) 'org-agenda-structural-header
)
479 (setq app
(get-text-property (point)
480 'org-agenda-title-append
))
481 (setq short
(get-text-property (point)
483 (when (and short
(looking-at ".+"))
484 (replace-match short
)
485 (beginning-of-line 1))
489 (beginning-of-line 1))
491 ((get-text-property (point) 'org-agenda-date-header
)
494 ((setq m
(or (get-text-property (point) 'org-hd-marker
)
495 (get-text-property (point) 'org-marker
)))
496 (setq sexp
(member (get-text-property (point) 'type
)
498 (if (setq pl
(get-text-property (point) 'prefix-length
))
500 (setq prefix
(org-trim (buffer-substring
501 (point) (+ (point) pl
)))
502 line
(org-trim (buffer-substring
505 (delete-region (point-at-bol) (point-at-eol))
506 (insert line
"<before>" prefix
"</before>")
507 (beginning-of-line 1))
508 (and (looking-at "[ \t]+") (replace-match "")))
509 (insert (if in-date
"*** " "** "))
513 (insert (org-agenda-get-some-entry-text
517 (if (org-bound-and-true-p
518 org-mobile-force-id-on-agenda-items
)
519 (org-id-get m
'create
)
520 (org-entry-get m
"ID")))
521 (insert " :PROPERTIES:\n :ORIGINAL_ID: " id
523 (beginning-of-line 2))
524 (push (cons (file-name-nondirectory file
) (md5 (buffer-string)))
525 org-mobile-checksum-files
))
526 (message "Agenda written to Org file %s" file
)))
529 (defun org-mobile-create-sumo-agenda ()
530 "Create a file that contains all custom agenda views."
532 (let* ((file (expand-file-name "agendas.org"
533 org-mobile-directory
))
534 (sumo (org-mobile-sumo-agenda-command))
535 (org-agenda-custom-commands
536 (list (append sumo
(list (list file
)))))
537 (org-mobile-creating-agendas t
))
538 (unless (file-writable-p file
)
539 (error "Cannot write to file %s" file
))
541 (org-store-agenda-views))))
543 (defun org-mobile-move-capture ()
544 "Move the contents of the capture file to the inbox file.
545 Return a marker to the location where the new content has been added.
546 If nothing new has beed added, return nil."
548 (let ((inbox-buffer (find-file-noselect org-mobile-inbox-for-pull
))
549 (capture-buffer (find-file-noselect
550 (expand-file-name org-mobile-capture-file
551 org-mobile-directory
)))
552 (insertion-point (make-marker))
554 (with-current-buffer capture-buffer
555 (setq content
(buffer-string))
556 (setq not-empty
(string-match "\\S-" content
))
558 (set-buffer inbox-buffer
)
560 (goto-char (point-max))
561 (or (bolp) (newline))
562 (move-marker insertion-point
563 (prog1 (point) (insert content
)))
565 (set-buffer capture-buffer
)
568 (org-mobile-update-checksum-for-capture-file (buffer-string))))
569 (kill-buffer capture-buffer
)
570 (if not-empty insertion-point
)))
572 (defun org-mobile-update-checksum-for-capture-file (buffer-string)
573 (let* ((file (expand-file-name "checksums.dat" org-mobile-directory
))
574 (buffer (find-file-noselect file
)))
576 (with-current-buffer buffer
577 (when (re-search-forward (concat "\\([0-9a-fA-F]\\{30,\\}\\).*?"
578 (regexp-quote org-mobile-capture-file
)
580 (goto-char (match-beginning 1))
581 (delete-region (match-beginning 1) (match-end 1))
582 (insert (md5 buffer-string
))
584 (kill-buffer buffer
))))
586 (defun org-mobile-apply (&optional beg end
)
587 "Apply all change requests in the current buffer.
588 If BEG and END are given, only do this in that region."
590 (require 'org-archive
)
591 (setq org-mobile-last-flagged-files nil
)
592 (setq beg
(or beg
(point-min)) end
(or end
(point-max)))
594 ;; Remove all Note IDs
596 (while (re-search-forward "^\\*\\* Note ID: [-0-9A-F]+[ \t]*\n" end t
)
599 ;; Find all the referenced entries, without making any changes yet
600 (let ((marker (make-marker))
601 (bos-marker (make-marker))
602 (end (move-marker (make-marker) end
))
608 id-pos org-mobile-error
)
610 ;; Count the new captures
612 (while (re-search-forward "^\\* \\(.*\\)" end t
)
613 (and (>= (- (match-end 1) (match-beginning 1)) 2)
614 (not (equal (downcase (substring (match-string 1) 0 2)) "f("))
618 (while (re-search-forward
619 "^\\*+[ \t]+F(\\([^():\n]*\\)\\(:\\([^()\n]*\\)\\)?)[ \t]+\\[\\[\\(\\(id\\|olp\\):\\([^]\n]+\\)\\)" end t
)
620 (setq id-pos
(condition-case msg
621 (org-mobile-locate-entry (match-string 4))
622 (error (nth 1 msg
))))
623 (when (and (markerp id-pos
)
624 (not (member (marker-buffer id-pos
) buf-list
)))
625 (org-mobile-timestamp-buffer (marker-buffer id-pos
))
626 (push (marker-buffer id-pos
) buf-list
))
628 (if (or (not id-pos
) (stringp id-pos
))
630 (goto-char (+ 2 (point-at-bol)))
633 (add-text-properties (point-at-bol) (point-at-eol)
634 (list 'org-mobile-marker
635 (or id-pos
"Linked entry not found")))))
637 ;; OK, now go back and start applying
639 (while (re-search-forward "^\\*+[ \t]+F(\\([^():\n]*\\)\\(:\\([^()\n]*\\)\\)?)" end t
)
641 (setq id-pos
(get-text-property (point-at-bol) 'org-mobile-marker
))
642 (if (not (markerp id-pos
))
645 (insert "UNKNOWN PROBLEM"))
646 (let* ((action (match-string 1))
647 (data (and (match-end 3) (match-string 3)))
649 (eos (save-excursion (org-end-of-subtree t t
)))
650 (cmd (if (equal action
"")
653 (org-toggle-tag "FLAGGED" 'on
)
655 (org-entry-put nil
"THEFLAGGINGNOTE" note
)))
657 (cdr (assoc action org-mobile-action-alist
))))
658 (note (and (equal action
"")
659 (buffer-substring (1+ (point-at-eol)) eos
)))
660 (org-inhibit-logging 'note
) ;; Do not take notes interactively
663 (move-marker bos-marker
(point))
664 (if (re-search-forward "^** Old value[ \t]*$" eos t
)
665 (setq old
(buffer-substring
667 (progn (outline-next-heading) (point)))))
668 (if (re-search-forward "^** New value[ \t]*$" eos t
)
669 (setq new
(buffer-substring
671 (progn (outline-next-heading)
672 (if (eobp) (org-back-over-empty-lines))
674 (setq old
(and old
(if (string-match "\\S-" old
) old nil
)))
675 (setq new
(and new
(if (string-match "\\S-" new
) new nil
)))
676 (if (and note
(> (length note
) 0))
677 ;; Make Note into a single line, to fit into a property
678 (setq note
(mapconcat 'identity
679 (org-split-string (org-trim note
) "\n")
681 (unless (equal data
"body")
682 (setq new
(and new
(org-trim new
))
683 old
(and old
(org-trim old
))))
684 (goto-char (+ 2 bos-marker
))
685 (unless (markerp id-pos
)
686 (insert "BAD REFERENCE ")
693 ;; Remember this place so tha we can return
694 (move-marker marker
(point))
695 (setq org-mobile-error nil
)
698 (org-with-point-at id-pos
701 (if (member "FLAGGED" (org-get-tags))
702 (add-to-list 'org-mobile-last-flagged-files
703 (buffer-file-name (current-buffer))))))
704 (error (setq org-mobile-error msg
))))
705 (when org-mobile-error
706 (switch-to-buffer (marker-buffer marker
))
709 (insert (if (stringp (nth 1 org-mobile-error
))
710 (nth 1 org-mobile-error
)
714 ;; If we get here, the action has been applied successfully
715 ;; So remove the entry
716 (goto-char bos-marker
)
717 (delete-region (point) (org-end-of-subtree t t
))))))
719 (move-marker marker nil
)
720 (move-marker end nil
)
721 (message "%d new, %d edits, %d flags, %d errors" cnt-new
722 cnt-edit cnt-flag cnt-error
)
725 (defun org-mobile-timestamp-buffer (buf)
726 "Time stamp buffer BUF, just to make sure its checksum will change."
727 (with-current-buffer buf
731 (goto-char (point-min))
732 (when (re-search-forward
733 "^\\([ \t]*\\)#\\+LAST_MOBILE_CHANGE:.*\n?" nil t
)
734 (goto-char (match-end 1))
735 (delete-region (point) (match-end 0)))
736 (insert "#+LAST_MOBILE_CHANGE: "
737 (format-time-string "%Y-%m-%d %T") "\n")))))
739 (defun org-mobile-smart-read ()
740 "Parse the entry at point for shortcuts and expand them.
741 These shortcuts are meant for fast and easy typing on the limited
742 keyboards of a mobile device. Below we show a list of the shortcuts
743 currently implemented.
745 The entry is expected to contain an inactive time stamp indicating when
746 the entry was created. When setting dates and
747 times (for example for deadlines), the time strings are interpreted
748 relative to that creation date.
749 Abbreviations are expected to take up entire lines, jst because it is so
750 easy to type RET on a mobile device. Abbreviations start with one or two
751 letters, followed immediately by a dot and then additional information.
752 Generally the entire shortcut line is removed after action have been taken.
753 Time stamps will be constructed using `org-read-date'. So for example a
754 line \"dd. 2tue\" will set a deadline on the second Tuesday after the
757 Here are the shortcuts currently implemented:
759 dd. string set deadline
760 ss. string set scheduling
761 tt. string set time tamp, here.
762 ti. string set inactive time
764 tg. tag1 tag2 tag3 set all these tags, change case where necessary
765 td. kwd set this todo keyword, change case where necessary
767 FIXME: Hmmm, not sure if we can make his work against the
768 auto-correction feature. Needs a bit more thinking. So this function
769 is currently a noop.")
772 (defun org-find-olp (path)
773 "Return a marker pointing to the entry at outline path OLP.
774 If anything goes wrong, the return value will instead an error message,
776 (let* ((file (pop path
))
777 (buffer (find-file-noselect file
))
781 limit re end found pos heading cnt
)
782 (unless buffer
(error "File not found :%s" file
))
783 (with-current-buffer buffer
787 (setq limit
(point-max))
788 (goto-char (point-min))
789 (while (setq heading
(pop path
))
790 (setq re
(format org-complex-heading-regexp-format
791 (regexp-quote heading
)))
792 (setq cnt
0 pos
(point))
793 (while (re-search-forward re end t
)
794 (setq level
(- (match-end 1) (match-beginning 1)))
795 (if (and (>= level lmin
) (<= level lmax
))
796 (setq found
(match-beginning 0) cnt
(1+ cnt
))))
797 (when (= cnt
0) (error "Heading not found on level %d: %s"
799 (when (> cnt
1) (error "Heading not unique on level %d: %s"
802 (setq lmin
(1+ level
) lmax
(+ lmin
(if org-odd-levels-only
1 0)))
803 (setq end
(save-excursion (org-end-of-subtree t t
))))
804 (when (org-on-heading-p)
805 (move-marker (make-marker) (point))))))))
807 (defun org-mobile-locate-entry (link)
808 (if (string-match "\\`id:\\(.*\\)$" link
)
809 (org-id-find (match-string 1 link
) 'marker
)
810 (if (not (string-match "\\`olp:\\(.*?\\):\\(.*\\)$" link
))
812 (let ((file (match-string 1 link
))
813 (path (match-string 2 link
))
814 (table '((?
: .
"%3a") (?\
[ .
"%5b") (?\
] .
"%5d") (?
/ .
"%2f"))))
815 (setq file
(org-link-unescape file table
))
816 (setq file
(expand-file-name file org-directory
))
817 (setq path
(mapcar (lambda (x) (org-link-unescape x table
))
818 (org-split-string path
"/")))
819 (org-find-olp (cons file path
))))))
821 (defun org-mobile-edit (what old new
)
822 "Edit item WHAT in the current entry by replacing OLD wih NEW.
823 WHAT can be \"heading\", \"todo\", \"tags\", \"priority\", or \"body\".
824 The edit only takes place if the current value is equal (except for
825 white space) the OLD. If this is so, OLD will be replace by NEW
826 and the command will return t. If something goes wrong, a string will
827 be returned that indicates what went wrong."
828 (let (current old1 new1
)
829 (if (stringp what
) (setq what
(intern what
)))
833 ((memq what
'(todo todostate
))
834 (setq current
(org-get-todo-state))
836 ((equal new
"DONEARCHIVE")
838 (org-archive-subtree-default))
839 ((equal new current
) t
) ; nothing needs to be done
840 ((or (equal current old
)
841 (eq org-mobile-force-mobile-change t
)
842 (memq 'todo org-mobile-force-mobile-change
))
843 (org-todo (or new
'none
)) t
)
844 (t (error "State before change was expected as \"%s\", but is \"%s\""
848 (setq current
(org-get-tags)
849 new1
(and new
(org-split-string new
":+"))
850 old1
(and old
(org-split-string old
":+")))
852 ((org-mobile-tags-same-p current new1
) t
) ; no change needed
853 ((or (org-mobile-tags-same-p current old1
)
854 (eq org-mobile-force-mobile-change t
)
855 (memq 'tags org-mobile-force-mobile-change
))
856 (org-set-tags-to new1
) t
)
857 (t (error "Tags before change were expected as \"%s\", but are \"%s\""
858 (or old
"") (or current
"")))))
861 (when (looking-at org-complex-heading-regexp
)
862 (setq current
(and (match-end 3) (substring (match-string 3) 2 3)))
864 ((equal current new
) t
) ; no action required
865 ((or (equal current old
)
866 (eq org-mobile-force-mobile-change t
)
867 (memq 'tags org-mobile-force-mobile-change
))
868 (org-priority (and new
(string-to-char new
))))
869 (t (error "Priority was expected to be %s, but is %s"
873 (when (looking-at org-complex-heading-regexp
)
874 (setq current
(match-string 4))
876 ((equal current new
) t
) ; no action required
877 ((or (equal current old
)
878 (eq org-mobile-force-mobile-change t
)
879 (memq 'heading org-mobile-force-mobile-change
))
880 (goto-char (match-beginning 4))
882 (delete-region (point) (+ (point) (length current
)))
883 (org-set-tags nil
'align
))
884 (t (error "Heading changed in MobileOrg and on the computer")))))
887 (setq current
(buffer-substring (min (1+ (point-at-eol)) (point-max))
888 (save-excursion (outline-next-heading)
890 (if (not (string-match "\\S-" current
)) (setq current nil
))
892 ((org-mobile-bodies-same-p current new
) t
) ; no ation necesary
893 ((or (org-mobile-bodies-same-p current old
)
894 (eq org-mobile-force-mobile-change t
)
895 (memq 'body org-mobile-force-mobile-change
))
899 (or (bolp) (insert "\n"))
900 (delete-region (point) (progn (org-back-to-heading t
)
901 (outline-next-heading)
904 (t (error "Body was changed in MobileOrg and on the computer")))))))
907 (defun org-mobile-tags-same-p (list1 list2
)
908 "Are the two tag lists the same?"
909 (not (or (org-delete-all list1 list2
)
910 (org-delete-all list2 list1
))))
912 (defun org-mobile-bodies-same-p (a b
)
913 "Compare if A and B are visually equal strings.
914 We first remove leading and trailing white space from the entire strings.
915 Then we split the strings into lines and remove leading/trailing whitespace
916 from each line. Then we compare.
917 A and B must be strings or nil."
919 ((and (not a
) (not b
)) t
)
920 ((or (not a
) (not b
)) nil
)
921 (t (setq a
(org-trim a
) b
(org-trim b
))
922 (setq a
(mapconcat 'identity
(org-split-string a
"[ \t]*\n[ \t]*") "\n"))
923 (setq b
(mapconcat 'identity
(org-split-string b
"[ \t]*\n[ \t]*") "\n"))
926 (provide 'org-mobile
)
928 ;; arch-tag: ace0e26c-58f2-4309-8a61-05ec1535f658
930 ;;; org-mobile.el ends here