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 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:
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 entries 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"))
255 (defvar org-mobile-before-process-capture-hook nil
256 "Hook that is run after content was moved to `org-mobile-inbox-for-pull'.
257 The inbox file is in the current buffer, and the buffer is arrowed to the
261 (defun org-mobile-pull ()
262 "Pull the contents of `org-mobile-capture-file' and integrate them.
263 Apply all flagged actions, flag entries to be flagged and then call an
264 agenda view showing the flagged items."
266 (org-mobile-check-setup)
267 (run-hooks 'org-mobile-pre-pull-hook
)
268 (let ((insertion-marker (org-mobile-move-capture)))
269 (if (not (markerp insertion-marker
))
270 (message "No new items")
271 (org-with-point-at insertion-marker
273 (narrow-to-region (point) (point-max))
274 (run-hooks 'org-mobile-before-process-capture-hook
)))
275 (org-with-point-at insertion-marker
276 (org-mobile-apply (point) (point-max)))
277 (move-marker insertion-marker nil
)
278 (run-hooks 'org-mobile-post-pull-hook
)
279 (when org-mobile-last-flagged-files
280 ;; Make an agenda view of flagged entries, but only in the files
281 ;; where stuff has been added.
282 (put 'org-agenda-files
'org-restrict org-mobile-last-flagged-files
)
283 (let ((org-agenda-keep-restricted-file-list t
))
284 (org-agenda nil
"?"))))))
286 (defun org-mobile-check-setup ()
287 "Check if org-mobile-directory has been set up."
288 (unless (and org-directory
289 (stringp org-directory
)
290 (string-match "\\S-" org-directory
)
291 (file-exists-p org-directory
)
292 (file-directory-p org-directory
))
294 "Please set `org-directory' to the directory where your org files live"))
295 (unless (and org-mobile-directory
296 (stringp org-mobile-directory
)
297 (string-match "\\S-" org-mobile-directory
)
298 (file-exists-p org-mobile-directory
)
299 (file-directory-p org-mobile-directory
))
301 "Variable `org-mobile-directory' must point to an existing directory"))
302 (unless (and org-mobile-inbox-for-pull
303 (stringp org-mobile-inbox-for-pull
)
304 (string-match "\\S-" org-mobile-inbox-for-pull
)
306 (file-name-directory org-mobile-inbox-for-pull
)))
308 "Variable `org-mobile-inbox-for-pull' must point to a file in an existing directory")))
310 (defun org-mobile-create-index-file ()
311 "Write the index file in the WebDAV directory."
312 (let ((files-alist (sort (copy-sequence org-mobile-files-alist
)
313 (lambda (a b
) (string< (cdr a
) (cdr b
)))))
314 (def-todo (default-value 'org-todo-keywords
))
315 (def-tags (default-value 'org-tag-alist
))
316 file link-name todo-kwds done-kwds tags drawers entry kwds dwds twds
)
318 (org-prepare-agenda-buffers (mapcar 'car files-alist
))
319 (setq done-kwds
(org-uniquify org-done-keywords-for-agenda
))
320 (setq todo-kwds
(org-delete-all
322 (org-uniquify org-todo-keywords-for-agenda
)))
323 (setq drawers
(org-uniquify org-drawers-for-agenda
))
324 (setq tags
(org-uniquify
328 (cond ((stringp e
) e
)
330 (if (stringp (car e
)) (car e
) nil
))
332 org-tag-alist-for-agenda
))))
334 (expand-file-name org-mobile-index-file org-mobile-directory
)
335 (while (setq entry
(pop def-todo
))
336 (insert "#+READONLY\n")
337 (setq kwds
(mapcar (lambda (x) (if (string-match "(" x
)
338 (substring x
0 (match-beginning 0))
341 (insert "#+TODO: " (mapconcat 'identity kwds
" ") "\n")
342 (setq dwds
(member "|" kwds
)
343 twds
(org-delete-all dwds kwds
)
344 todo-kwds
(org-delete-all twds todo-kwds
)
345 done-kwds
(org-delete-all dwds done-kwds
)))
346 (when (or todo-kwds done-kwds
)
347 (insert "#+TODO: " (mapconcat 'identity todo-kwds
" ") " | "
348 (mapconcat 'identity done-kwds
" ") "\n"))
349 (setq def-tags
(mapcar
353 ((eq (car x
) :startgroup
) "{")
354 ((eq (car x
) :endgroup
) "}")
355 ((eq (car x
) :newline
) nil
)
359 (setq def-tags
(delq nil def-tags
))
360 (setq tags
(org-delete-all def-tags tags
))
361 (setq tags
(sort tags
(lambda (a b
) (string< (downcase a
) (downcase b
)))))
362 (setq tags
(append def-tags tags nil
))
363 (insert "#+TAGS: " (mapconcat 'identity tags
" ") "\n")
364 (insert "#+DRAWERS: " (mapconcat 'identity drawers
" ") "\n")
365 (insert "#+ALLPRIORITIES: A B C" "\n")
366 (when (file-exists-p (expand-file-name
367 org-mobile-directory
"agendas.org"))
368 (insert "* [[file:agendas.org][Agenda Views]]\n"))
369 (while (setq entry
(pop files-alist
))
370 (setq file
(car entry
)
371 link-name
(cdr entry
))
372 (insert (format "* [[file:%s][%s]]\n"
373 link-name link-name
)))
374 (push (cons org-mobile-index-file
(md5 (buffer-string)))
375 org-mobile-checksum-files
))))
377 (defun org-mobile-copy-agenda-files ()
378 "Copy all agenda files to the stage or WebDAV directory."
379 (let ((files-alist org-mobile-files-alist
)
380 file buf entry link-name target-path target-dir check
)
381 (while (setq entry
(pop files-alist
))
382 (setq file
(car entry
) link-name
(cdr entry
))
383 (when (file-exists-p file
)
384 (setq target-path
(expand-file-name link-name org-mobile-directory
)
385 target-dir
(file-name-directory target-path
))
386 (unless (file-directory-p target-dir
)
387 (make-directory target-dir
'parents
))
388 (copy-file file target-path
'ok-if-exists
)
389 (setq check
(shell-command-to-string
390 (concat org-mobile-checksum-binary
" "
391 (shell-quote-argument (expand-file-name file
)))))
392 (when (string-match "[a-fA-F0-9]\\{30,40\\}" check
)
393 (push (cons link-name
(match-string 0 check
))
394 org-mobile-checksum-files
))))
395 (setq file
(expand-file-name org-mobile-capture-file
396 org-mobile-directory
))
398 (setq buf
(find-file file
))
399 (and (= (point-min) (point-max)) (insert "\n"))
401 (push (cons org-mobile-capture-file
(md5 (buffer-string)))
402 org-mobile-checksum-files
))
405 (defun org-mobile-write-checksums ()
406 "Create checksums for all files in `org-mobile-directory'.
407 The table of checksums is written to the file mobile-checksums."
408 (let ((sumfile (expand-file-name "checksums.dat" org-mobile-directory
))
409 (files org-mobile-checksum-files
)
411 (with-temp-file sumfile
412 (set-buffer-file-coding-system 'undecided-unix nil
)
413 (while (setq entry
(pop files
))
414 (setq file
(car entry
) sum
(cdr entry
))
415 (insert (format "%s %s\n" sum file
))))))
417 (defun org-mobile-sumo-agenda-command ()
418 "Return an agenda custom command that comprises all custom commands."
420 ;; normalize different versions
424 (cond ((stringp (cdr x
)) nil
)
425 ((stringp (nth 1 x
)) x
)
426 ((not (nth 1 x
)) (cons (car x
) (cons "" (cddr x
))))
427 (t (cons (car x
) (cons "" (cdr x
))))))
428 org-agenda-custom-commands
)))
429 new e key desc type match settings cmds gkey gdesc gsettings cnt
)
430 (while (setq e
(pop custom-list
))
433 ;; this is a description entry - skip it
435 ((eq (nth 2 e
) 'search
)
436 ;; Search view is interactive, skip
438 ((memq (nth 2 e
) '(todo-tree tags-tree occur-tree
))
439 ;; These are trees, not really agenda commands
441 ((memq (nth 2 e
) '(agenda todo tags
))
443 (setq key
(car e
) desc
(nth 1 e
) type
(nth 2 e
) match
(nth 3 e
)
446 (cons (list 'org-agenda-title-append
447 (concat "<after>KEYS=" key
" TITLE: "
448 (if (and (stringp desc
) (> (length desc
) 0))
449 desc
(symbol-name type
))
450 " " match
"</after>"))
452 (push (list type match settings
) new
))
454 ;; A user-defined function, not sure how to handle that yet
458 (setq gkey
(car e
) gdesc
(nth 1 e
) gsettings
(nth 3 e
) cmds
(nth 2 e
))
460 (while (setq e
(pop cmds
))
461 (setq type
(car e
) match
(nth 1 e
) settings
(nth 2 e
))
462 (setq settings
(append gsettings settings
))
464 (cons (list 'org-agenda-title-append
465 (concat "<after>KEYS=" gkey
"#" (number-to-string
467 " TITLE: " gdesc
" " match
"</after>"))
469 (push (list type match settings
) new
)))))
470 (and new
(list "X" "SUMO" (reverse new
)
471 '((org-agenda-compact-blocks nil
))))))
473 (defvar org-mobile-creating-agendas nil
)
474 (defun org-mobile-write-agenda-for-mobile (file)
475 (let ((all (buffer-string)) in-date id pl prefix line app short m sexp
)
478 (insert "#+READONLY\n")
480 (goto-char (point-min))
483 ((looking-at "[ \t]*$")) ; keep empty lines
485 ;; remove underlining
486 (delete-region (point) (point-at-eol)))
487 ((get-text-property (point) 'org-agenda-structural-header
)
489 (setq app
(get-text-property (point)
490 'org-agenda-title-append
))
491 (setq short
(get-text-property (point)
493 (when (and short
(looking-at ".+"))
494 (replace-match short
)
495 (beginning-of-line 1))
499 (beginning-of-line 1))
501 ((get-text-property (point) 'org-agenda-date-header
)
504 ((setq m
(or (get-text-property (point) 'org-hd-marker
)
505 (get-text-property (point) 'org-marker
)))
506 (setq sexp
(member (get-text-property (point) 'type
)
508 (if (setq pl
(get-text-property (point) 'prefix-length
))
510 (setq prefix
(org-trim (buffer-substring
511 (point) (+ (point) pl
)))
512 line
(org-trim (buffer-substring
515 (delete-region (point-at-bol) (point-at-eol))
516 (insert line
"<before>" prefix
"</before>")
517 (beginning-of-line 1))
518 (and (looking-at "[ \t]+") (replace-match "")))
519 (insert (if in-date
"*** " "** "))
523 (insert (org-agenda-get-some-entry-text
527 (if (org-bound-and-true-p
528 org-mobile-force-id-on-agenda-items
)
529 (org-id-get m
'create
)
530 (org-entry-get m
"ID")))
531 (insert " :PROPERTIES:\n :ORIGINAL_ID: " id
533 (beginning-of-line 2))
534 (push (cons (file-name-nondirectory file
) (md5 (buffer-string)))
535 org-mobile-checksum-files
))
536 (message "Agenda written to Org file %s" file
)))
539 (defun org-mobile-create-sumo-agenda ()
540 "Create a file that contains all custom agenda views."
542 (let* ((file (expand-file-name "agendas.org"
543 org-mobile-directory
))
544 (sumo (org-mobile-sumo-agenda-command))
545 (org-agenda-custom-commands
546 (list (append sumo
(list (list file
)))))
547 (org-mobile-creating-agendas t
))
548 (unless (file-writable-p file
)
549 (error "Cannot write to file %s" file
))
551 (org-store-agenda-views))))
553 (defun org-mobile-move-capture ()
554 "Move the contents of the capture file to the inbox file.
555 Return a marker to the location where the new content has been added.
556 If nothing new has been added, return nil."
558 (let ((inbox-buffer (find-file-noselect org-mobile-inbox-for-pull
))
559 (capture-buffer (find-file-noselect
560 (expand-file-name org-mobile-capture-file
561 org-mobile-directory
)))
562 (insertion-point (make-marker))
564 (with-current-buffer capture-buffer
565 (setq content
(buffer-string))
566 (setq not-empty
(string-match "\\S-" content
))
568 (set-buffer inbox-buffer
)
570 (goto-char (point-max))
571 (or (bolp) (newline))
572 (move-marker insertion-point
573 (prog1 (point) (insert content
)))
575 (set-buffer capture-buffer
)
578 (org-mobile-update-checksum-for-capture-file (buffer-string))))
579 (kill-buffer capture-buffer
)
580 (if not-empty insertion-point
)))
582 (defun org-mobile-update-checksum-for-capture-file (buffer-string)
583 (let* ((file (expand-file-name "checksums.dat" org-mobile-directory
))
584 (buffer (find-file-noselect file
)))
586 (with-current-buffer buffer
587 (when (re-search-forward (concat "\\([0-9a-fA-F]\\{30,\\}\\).*?"
588 (regexp-quote org-mobile-capture-file
)
590 (goto-char (match-beginning 1))
591 (delete-region (match-beginning 1) (match-end 1))
592 (insert (md5 buffer-string
))
594 (kill-buffer buffer
))))
596 (defun org-mobile-apply (&optional beg end
)
597 "Apply all change requests in the current buffer.
598 If BEG and END are given, only do this in that region."
600 (require 'org-archive
)
601 (setq org-mobile-last-flagged-files nil
)
602 (setq beg
(or beg
(point-min)) end
(or end
(point-max)))
604 ;; Remove all Note IDs
606 (while (re-search-forward "^\\*\\* Note ID: [-0-9A-F]+[ \t]*\n" end t
)
609 ;; Find all the referenced entries, without making any changes yet
610 (let ((marker (make-marker))
611 (bos-marker (make-marker))
612 (end (move-marker (make-marker) end
))
618 id-pos org-mobile-error
)
620 ;; Count the new captures
622 (while (re-search-forward "^\\* \\(.*\\)" end t
)
623 (and (>= (- (match-end 1) (match-beginning 1)) 2)
624 (not (equal (downcase (substring (match-string 1) 0 2)) "f("))
628 (while (re-search-forward
629 "^\\*+[ \t]+F(\\([^():\n]*\\)\\(:\\([^()\n]*\\)\\)?)[ \t]+\\[\\[\\(\\(id\\|olp\\):\\([^]\n]+\\)\\)" end t
)
630 (setq id-pos
(condition-case msg
631 (org-mobile-locate-entry (match-string 4))
632 (error (nth 1 msg
))))
633 (when (and (markerp id-pos
)
634 (not (member (marker-buffer id-pos
) buf-list
)))
635 (org-mobile-timestamp-buffer (marker-buffer id-pos
))
636 (push (marker-buffer id-pos
) buf-list
))
638 (if (or (not id-pos
) (stringp id-pos
))
640 (goto-char (+ 2 (point-at-bol)))
643 (add-text-properties (point-at-bol) (point-at-eol)
644 (list 'org-mobile-marker
645 (or id-pos
"Linked entry not found")))))
647 ;; OK, now go back and start applying
649 (while (re-search-forward "^\\*+[ \t]+F(\\([^():\n]*\\)\\(:\\([^()\n]*\\)\\)?)" end t
)
651 (setq id-pos
(get-text-property (point-at-bol) 'org-mobile-marker
))
652 (if (not (markerp id-pos
))
655 (insert "UNKNOWN PROBLEM"))
656 (let* ((action (match-string 1))
657 (data (and (match-end 3) (match-string 3)))
659 (eos (save-excursion (org-end-of-subtree t t
)))
660 (cmd (if (equal action
"")
663 (org-toggle-tag "FLAGGED" 'on
)
665 (org-entry-put nil
"THEFLAGGINGNOTE" note
)))
667 (cdr (assoc action org-mobile-action-alist
))))
668 (note (and (equal action
"")
669 (buffer-substring (1+ (point-at-eol)) eos
)))
670 (org-inhibit-logging 'note
) ;; Do not take notes interactively
673 (move-marker bos-marker
(point))
674 (if (re-search-forward "^** Old value[ \t]*$" eos t
)
675 (setq old
(buffer-substring
677 (progn (outline-next-heading) (point)))))
678 (if (re-search-forward "^** New value[ \t]*$" eos t
)
679 (setq new
(buffer-substring
681 (progn (outline-next-heading)
682 (if (eobp) (org-back-over-empty-lines))
684 (setq old
(and old
(if (string-match "\\S-" old
) old nil
)))
685 (setq new
(and new
(if (string-match "\\S-" new
) new nil
)))
686 (if (and note
(> (length note
) 0))
687 ;; Make Note into a single line, to fit into a property
688 (setq note
(mapconcat 'identity
689 (org-split-string (org-trim note
) "\n")
691 (unless (equal data
"body")
692 (setq new
(and new
(org-trim new
))
693 old
(and old
(org-trim old
))))
694 (goto-char (+ 2 bos-marker
))
695 (unless (markerp id-pos
)
696 (insert "BAD REFERENCE ")
703 ;; Remember this place so that we can return
704 (move-marker marker
(point))
705 (setq org-mobile-error nil
)
708 (org-with-point-at id-pos
711 (if (member "FLAGGED" (org-get-tags))
712 (add-to-list 'org-mobile-last-flagged-files
713 (buffer-file-name (current-buffer))))))
714 (error (setq org-mobile-error msg
))))
715 (when org-mobile-error
716 (switch-to-buffer (marker-buffer marker
))
719 (insert (if (stringp (nth 1 org-mobile-error
))
720 (nth 1 org-mobile-error
)
724 ;; If we get here, the action has been applied successfully
725 ;; So remove the entry
726 (goto-char bos-marker
)
727 (delete-region (point) (org-end-of-subtree t t
))))))
729 (move-marker marker nil
)
730 (move-marker end nil
)
731 (message "%d new, %d edits, %d flags, %d errors" cnt-new
732 cnt-edit cnt-flag cnt-error
)
735 (defun org-mobile-timestamp-buffer (buf)
736 "Time stamp buffer BUF, just to make sure its checksum will change."
737 (with-current-buffer buf
741 (goto-char (point-min))
742 (if (re-search-forward
743 "^\\([ \t]*\\)#\\+LAST_MOBILE_CHANGE:.*\n?" nil t
)
745 (goto-char (match-end 1))
746 (delete-region (point) (match-end 0)))
747 (if (looking-at ".*?-\\*-.*-\\*-")
749 (insert "#+LAST_MOBILE_CHANGE: "
750 (format-time-string "%Y-%m-%d %T") "\n")))))
752 (defun org-mobile-smart-read ()
753 "Parse the entry at point for shortcuts and expand them.
754 These shortcuts are meant for fast and easy typing on the limited
755 keyboards of a mobile device. Below we show a list of the shortcuts
756 currently implemented.
758 The entry is expected to contain an inactive time stamp indicating when
759 the entry was created. When setting dates and
760 times (for example for deadlines), the time strings are interpreted
761 relative to that creation date.
762 Abbreviations are expected to take up entire lines, just because it is so
763 easy to type RET on a mobile device. Abbreviations start with one or two
764 letters, followed immediately by a dot and then additional information.
765 Generally the entire shortcut line is removed after action have been taken.
766 Time stamps will be constructed using `org-read-date'. So for example a
767 line \"dd. 2tue\" will set a deadline on the second Tuesday after the
770 Here are the shortcuts currently implemented:
772 dd. string set deadline
773 ss. string set scheduling
774 tt. string set time tamp, here.
775 ti. string set inactive time
777 tg. tag1 tag2 tag3 set all these tags, change case where necessary
778 td. kwd set this todo keyword, change case where necessary
780 FIXME: Hmmm, not sure if we can make his work against the
781 auto-correction feature. Needs a bit more thinking. So this function
782 is currently a noop.")
785 (defun org-find-olp (path)
786 "Return a marker pointing to the entry at outline path OLP.
787 If anything goes wrong, the return value will instead an error message,
789 (let* ((file (pop path
))
790 (buffer (find-file-noselect file
))
794 limit re end found pos heading cnt
)
795 (unless buffer
(error "File not found :%s" file
))
796 (with-current-buffer buffer
800 (setq limit
(point-max))
801 (goto-char (point-min))
802 (while (setq heading
(pop path
))
803 (setq re
(format org-complex-heading-regexp-format
804 (regexp-quote heading
)))
805 (setq cnt
0 pos
(point))
806 (while (re-search-forward re end t
)
807 (setq level
(- (match-end 1) (match-beginning 1)))
808 (if (and (>= level lmin
) (<= level lmax
))
809 (setq found
(match-beginning 0) cnt
(1+ cnt
))))
810 (when (= cnt
0) (error "Heading not found on level %d: %s"
812 (when (> cnt
1) (error "Heading not unique on level %d: %s"
815 (setq lmin
(1+ level
) lmax
(+ lmin
(if org-odd-levels-only
1 0)))
816 (setq end
(save-excursion (org-end-of-subtree t t
))))
817 (when (org-on-heading-p)
818 (move-marker (make-marker) (point))))))))
820 (defun org-mobile-locate-entry (link)
821 (if (string-match "\\`id:\\(.*\\)$" link
)
822 (org-id-find (match-string 1 link
) 'marker
)
823 (if (not (string-match "\\`olp:\\(.*?\\):\\(.*\\)$" link
))
825 (let ((file (match-string 1 link
))
826 (path (match-string 2 link
))
827 (table '((?
: .
"%3a") (?\
[ .
"%5b") (?\
] .
"%5d") (?
/ .
"%2f"))))
828 (setq file
(org-link-unescape file table
))
829 (setq file
(expand-file-name file org-directory
))
830 (setq path
(mapcar (lambda (x) (org-link-unescape x table
))
831 (org-split-string path
"/")))
832 (org-find-olp (cons file path
))))))
834 (defun org-mobile-edit (what old new
)
835 "Edit item WHAT in the current entry by replacing OLD with NEW.
836 WHAT can be \"heading\", \"todo\", \"tags\", \"priority\", or \"body\".
837 The edit only takes place if the current value is equal (except for
838 white space) the OLD. If this is so, OLD will be replace by NEW
839 and the command will return t. If something goes wrong, a string will
840 be returned that indicates what went wrong."
841 (let (current old1 new1
)
842 (if (stringp what
) (setq what
(intern what
)))
846 ((memq what
'(todo todostate
))
847 (setq current
(org-get-todo-state))
849 ((equal new
"DONEARCHIVE")
851 (org-archive-subtree-default))
852 ((equal new current
) t
) ; nothing needs to be done
853 ((or (equal current old
)
854 (eq org-mobile-force-mobile-change t
)
855 (memq 'todo org-mobile-force-mobile-change
))
856 (org-todo (or new
'none
)) t
)
857 (t (error "State before change was expected as \"%s\", but is \"%s\""
861 (setq current
(org-get-tags)
862 new1
(and new
(org-split-string new
":+"))
863 old1
(and old
(org-split-string old
":+")))
865 ((org-mobile-tags-same-p current new1
) t
) ; no change needed
866 ((or (org-mobile-tags-same-p current old1
)
867 (eq org-mobile-force-mobile-change t
)
868 (memq 'tags org-mobile-force-mobile-change
))
869 (org-set-tags-to new1
) t
)
870 (t (error "Tags before change were expected as \"%s\", but are \"%s\""
871 (or old
"") (or current
"")))))
874 (when (looking-at org-complex-heading-regexp
)
875 (setq current
(and (match-end 3) (substring (match-string 3) 2 3)))
877 ((equal current new
) t
) ; no action required
878 ((or (equal current old
)
879 (eq org-mobile-force-mobile-change t
)
880 (memq 'tags org-mobile-force-mobile-change
))
881 (org-priority (and new
(string-to-char new
))))
882 (t (error "Priority was expected to be %s, but is %s"
886 (when (looking-at org-complex-heading-regexp
)
887 (setq current
(match-string 4))
889 ((equal current new
) t
) ; no action required
890 ((or (equal current old
)
891 (eq org-mobile-force-mobile-change t
)
892 (memq 'heading org-mobile-force-mobile-change
))
893 (goto-char (match-beginning 4))
895 (delete-region (point) (+ (point) (length current
)))
896 (org-set-tags nil
'align
))
897 (t (error "Heading changed in MobileOrg and on the computer")))))
900 (setq current
(buffer-substring (min (1+ (point-at-eol)) (point-max))
901 (save-excursion (outline-next-heading)
903 (if (not (string-match "\\S-" current
)) (setq current nil
))
905 ((org-mobile-bodies-same-p current new
) t
) ; no action necessary
906 ((or (org-mobile-bodies-same-p current old
)
907 (eq org-mobile-force-mobile-change t
)
908 (memq 'body org-mobile-force-mobile-change
))
912 (or (bolp) (insert "\n"))
913 (delete-region (point) (progn (org-back-to-heading t
)
914 (outline-next-heading)
917 (t (error "Body was changed in MobileOrg and on the computer")))))))
920 (defun org-mobile-tags-same-p (list1 list2
)
921 "Are the two tag lists the same?"
922 (not (or (org-delete-all list1 list2
)
923 (org-delete-all list2 list1
))))
925 (defun org-mobile-bodies-same-p (a b
)
926 "Compare if A and B are visually equal strings.
927 We first remove leading and trailing white space from the entire strings.
928 Then we split the strings into lines and remove leading/trailing whitespace
929 from each line. Then we compare.
930 A and B must be strings or nil."
932 ((and (not a
) (not b
)) t
)
933 ((or (not a
) (not b
)) nil
)
934 (t (setq a
(org-trim a
) b
(org-trim b
))
935 (setq a
(mapconcat 'identity
(org-split-string a
"[ \t]*\n[ \t]*") "\n"))
936 (setq b
(mapconcat 'identity
(org-split-string b
"[ \t]*\n[ \t]*") "\n"))
939 (provide 'org-mobile
)
941 ;; arch-tag: ace0e26c-58f2-4309-8a61-05ec1535f658
943 ;;; org-mobile.el ends here