1 ;;; org-mobile.el --- Code for asymmetric sync with a mobile device
2 ;; Copyright (C) 2009-2012 Free Software Foundation, Inc.
4 ;; Author: Carsten Dominik <carsten at orgmode dot org>
5 ;; Keywords: outlines, hypermedia, calendar, wp
6 ;; Homepage: http://orgmode.org
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;; This file contains the code to interact with Richard Moreland's iPhone
28 ;; application MobileOrg, as well as with the Android version by Matthew Jones.
29 ;; This code is documented in Appendix B of the Org-mode manual. The code is
30 ;; not specific for the iPhone and Android - any external
31 ;; viewer/flagging/editing application that uses the same conventions could
38 (eval-when-compile (require 'cl
))
40 (declare-function org-pop-to-buffer-same-window
41 "org-compat" (&optional buffer-or-name norecord label
))
43 (defgroup org-mobile nil
44 "Options concerning support for a viewer/editor on a mobile device."
48 (defcustom org-mobile-files
'(org-agenda-files)
49 "Files to be staged for MobileOrg.
50 This is basically a list of files and directories. Files will be staged
51 directly. Directories will be search for files with the extension `.org'.
52 In addition to this, the list may also contain the following symbols:
55 This means include the complete, unrestricted list of files given in
56 the variable `org-agenda-files'.
57 org-agenda-text-search-extra-files
58 Include the files given in the variable
59 `org-agenda-text-search-extra-files'"
61 :type
'(list :greedy t
62 (option (const :tag
"org-agenda-files" org-agenda-files
))
63 (option (const :tag
"org-agenda-text-search-extra-files"
64 org-agenda-text-search-extra-files
))
65 (repeat :inline t
:tag
"Additional files"
68 (defcustom org-mobile-files-exclude-regexp
""
69 "A regexp to exclude files from `org-mobile-files'."
74 (defcustom org-mobile-directory
""
75 "The WebDAV directory where the interaction with the mobile takes place."
79 (defcustom org-mobile-use-encryption nil
80 "Non-nil means keep only encrypted files on the WebDAV server.
81 Encryption uses AES-256, with a password given in
82 `org-mobile-encryption-password'.
83 When nil, plain files are kept on the server.
84 Turning on encryption requires to set the same password in the MobileOrg
85 application. Before turning this on, check of MobileOrg does already
86 support it - at the time of this writing it did not yet."
91 (defcustom org-mobile-encryption-tempfile
"~/orgtmpcrypt"
92 "File that is being used as a temporary file for encryption.
93 This must be local file on your local machine (not on the WebDAV server).
94 You might want to put this file into a directory where only you have access."
99 (defcustom org-mobile-encryption-password
""
100 "Password for encrypting files uploaded to the server.
101 This is a single password which is used for AES-256 encryption. The same
102 password must also be set in the MobileOrg application. All Org files,
103 including mobileorg.org will be encrypted using this password.
105 SECURITY CONSIDERATIONS:
107 Note that, when Org runs the encryption commands, the password could
108 be visible briefly on your system with the `ps' command. So this method is
109 only intended to keep the files secure on the server, not on your own machine.
111 Also, if you set this variable in an init file (.emacs or .emacs.d/init.el
112 or custom.el...) and if that file is stored in a way so that other can read
113 it, this also limits the security of this approach. You can also leave
114 this variable empty - Org will then ask for the password once per Emacs
118 :type
'(string :tag
"Password"))
120 (defvar org-mobile-encryption-password-session nil
)
122 (defun org-mobile-encryption-password ()
123 (or (org-string-nw-p org-mobile-encryption-password
)
124 (org-string-nw-p org-mobile-encryption-password-session
)
125 (setq org-mobile-encryption-password-session
126 (read-passwd "Password for MobileOrg: " t
))))
128 (defcustom org-mobile-inbox-for-pull
"~/org/from-mobile.org"
129 "The file where captured notes and flags will be appended to.
130 During the execution of `org-mobile-pull', the file
131 `org-mobile-capture-file' will be emptied it's contents have
132 been appended to the file given here. This file should be in
133 `org-directory', and not in the staging area or on the web server."
137 (defconst org-mobile-capture-file
"mobileorg.org"
138 "The capture file where the mobile stores captured notes and flags.
139 This should not be changed, because MobileOrg assumes this name.")
141 (defcustom org-mobile-index-file
"index.org"
142 "The index file with links to all Org files that should be loaded by MobileOrg.
143 Relative to `org-mobile-directory'. The Address field in the MobileOrg setup
144 should point to this file."
148 (defcustom org-mobile-agendas
'all
149 "The agendas that should be pushed to MobileOrg.
152 default the weekly agenda and the global TODO list
153 custom all custom agendas defined by the user
154 all the custom agendas and the default ones
155 list a list of selection key(s) as string."
159 (const :tag
"Default Agendas" default
)
160 (const :tag
"Custom Agendas" custom
)
161 (const :tag
"Default and Custom Agendas" all
)
162 (repeat :tag
"Selected"
163 (string :tag
"Selection Keys"))))
165 (defcustom org-mobile-force-id-on-agenda-items t
166 "Non-nil means make all agenda items carry an ID."
170 (defcustom org-mobile-force-mobile-change nil
171 "Non-nil means force the change made on the mobile device.
172 So even if there have been changes to the computer version of the entry,
173 force the new value set on the mobile.
174 When nil, mark the entry from the mobile with an error message.
175 Instead of nil or t, this variable can also be a list of symbols, indicating
176 the editing types for which the mobile version should always dominate."
179 (const :tag
"Always" t
)
180 (const :tag
"Never" nil
)
181 (set :greedy t
:tag
"Specify"
188 (defcustom org-mobile-action-alist
189 '(("edit" .
(org-mobile-edit data old new
)))
190 "Alist with flags and actions for mobile sync.
191 When flagging an entry, MobileOrg will create entries that look like
193 * F(action:data) [[id:entry-id][entry title]]
195 This alist defines that the ACTION in the parentheses of F() should mean,
196 i.e. what action should be taken. The :data part in the parenthesis is
197 optional. If present, the string after the colon will be passed to the
198 action form as the `data' variable.
199 The car of each elements of the alist is an actions string. The cdr is
200 an Emacs Lisp form that will be evaluated with the cursor on the headline
203 For now, it is not recommended to change this variable."
206 (cons (string :tag
"Action flag")
207 (sexp :tag
"Action form"))))
209 (defcustom org-mobile-checksum-binary
(or (executable-find "shasum")
210 (executable-find "sha1sum")
211 (executable-find "md5sum")
212 (executable-find "md5"))
213 "Executable used for computing checksums of agenda files."
217 (defvar org-mobile-pre-push-hook nil
218 "Hook run before running `org-mobile-push'.
219 This could be used to clean up `org-mobile-directory', for example to
220 remove files that used to be included in the agenda but no longer are.
221 The presence of such files would not really be a problem, but after time
222 they may accumulate.")
224 (defvar org-mobile-post-push-hook nil
225 "Hook run after running `org-mobile-push'.
226 If Emacs does not have direct write access to the WebDAV directory used
227 by the mobile device, this hook should be used to copy all files from the
228 local staging directory `org-mobile-directory' to the WebDAV directory,
229 for example using `rsync' or `scp'.")
231 (defvar org-mobile-pre-pull-hook nil
232 "Hook run before executing `org-mobile-pull'.
233 If Emacs does not have direct write access to the WebDAV directory used
234 by the mobile device, this hook should be used to copy the capture file
235 `mobileorg.org' from the WebDAV location to the local staging
236 directory `org-mobile-directory'.")
238 (defvar org-mobile-post-pull-hook nil
239 "Hook run after running `org-mobile-pull'.
240 If Emacs does not have direct write access to the WebDAV directory used
241 by the mobile device, this hook should be used to copy the emptied
242 capture file `mobileorg.org' back to the WebDAV directory, for example
243 using `rsync' or `scp'.")
245 (defvar org-mobile-last-flagged-files nil
246 "List of files containing entries flagged in the latest pull.")
248 (defvar org-mobile-files-alist nil
)
249 (defvar org-mobile-checksum-files nil
)
251 (defun org-mobile-prepare-file-lists ()
252 (setq org-mobile-files-alist
(org-mobile-files-alist))
253 (setq org-mobile-checksum-files nil
))
255 (defun org-mobile-files-alist ()
256 "Expand the list in `org-mobile-files' to a list of existing files.
257 Also exclude files matching `org-mobile-files-exclude-regexp'."
258 (let* ((include-archives
259 (and (member 'org-agenda-text-search-extra-files org-mobile-files
)
260 (member 'agenda-archives org-agenda-text-search-extra-files
)
267 ((eq f
'org-agenda-files
)
268 (org-agenda-files t include-archives
))
269 ((eq f
'org-agenda-text-search-extra-files
)
270 (delq 'agenda-archives
272 org-agenda-text-search-extra-files
)))
273 ((and (stringp f
) (file-directory-p f
))
274 (directory-files f
'full
"\\.org\\'"))
275 ((and (stringp f
) (file-exists-p f
))
282 (unless (and (not (string= org-mobile-files-exclude-regexp
""))
283 (string-match org-mobile-files-exclude-regexp f
))
286 (orgdir-uname (file-name-as-directory (file-truename org-directory
)))
287 (orgdir-re (concat "\\`" (regexp-quote orgdir-uname
)))
288 uname seen rtn file link-name
)
289 ;; Make the files unique, and determine the name under which they will
291 (while (setq file
(pop files
))
292 (if (not (file-name-absolute-p file
))
293 (setq file
(expand-file-name file org-directory
)))
294 (setq uname
(file-truename file
))
295 (unless (member uname seen
)
297 (if (string-match orgdir-re uname
)
298 (setq link-name
(substring uname
(match-end 0)))
299 (setq link-name
(file-name-nondirectory uname
)))
300 (push (cons file link-name
) rtn
)))
303 (defvar org-agenda-filter
)
306 (defun org-mobile-push ()
307 "Push the current state of Org affairs to the WebDAV directory.
308 This will create the index file, copy all agenda files there, and also
309 create all custom agenda views, for upload to the mobile phone."
311 (let ((a-buffer (get-buffer org-agenda-buffer-name
)))
312 (let ((org-agenda-buffer-name "*SUMO*")
313 (org-agenda-tag-filter org-agenda-tag-filter
)
314 (org-agenda-redo-command org-agenda-redo-command
))
316 (save-window-excursion
317 (run-hooks 'org-mobile-pre-push-hook
)
318 (org-mobile-check-setup)
319 (org-mobile-prepare-file-lists)
320 (message "Creating agendas...")
321 (let ((inhibit-redisplay t
)) (org-mobile-create-sumo-agenda))
322 (message "Creating agendas...done")
323 (org-save-all-org-buffers) ; to save any IDs created by this process
324 (message "Copying files...")
325 (org-mobile-copy-agenda-files)
326 (message "Writing index file...")
327 (org-mobile-create-index-file)
328 (message "Writing checksums...")
329 (org-mobile-write-checksums)
330 (run-hooks 'org-mobile-post-push-hook
))))
332 (when (and a-buffer
(buffer-live-p a-buffer
))
333 (if (not (get-buffer-window a-buffer
))
334 (kill-buffer a-buffer
)
335 (let ((cw (selected-window)))
336 (select-window (get-buffer-window a-buffer
))
338 (select-window cw
)))))
339 (message "Files for mobile viewer staged"))
341 (defvar org-mobile-before-process-capture-hook nil
342 "Hook that is run after content was moved to `org-mobile-inbox-for-pull'.
343 The inbox file is visited by the current buffer, and the buffer is
344 narrowed to the newly captured data.")
347 (defun org-mobile-pull ()
348 "Pull the contents of `org-mobile-capture-file' and integrate them.
349 Apply all flagged actions, flag entries to be flagged and then call an
350 agenda view showing the flagged items."
352 (org-mobile-check-setup)
353 (run-hooks 'org-mobile-pre-pull-hook
)
354 (let ((insertion-marker (org-mobile-move-capture)))
355 (if (not (markerp insertion-marker
))
356 (message "No new items")
357 (org-with-point-at insertion-marker
359 (narrow-to-region (point) (point-max))
360 (run-hooks 'org-mobile-before-process-capture-hook
)))
361 (org-with-point-at insertion-marker
362 (org-mobile-apply (point) (point-max)))
363 (move-marker insertion-marker nil
)
364 (run-hooks 'org-mobile-post-pull-hook
)
365 (when org-mobile-last-flagged-files
366 ;; Make an agenda view of flagged entries, but only in the files
367 ;; where stuff has been added.
368 (put 'org-agenda-files
'org-restrict org-mobile-last-flagged-files
)
369 (let ((org-agenda-keep-restricted-file-list t
))
370 (org-agenda nil
"?"))))))
372 (defun org-mobile-check-setup ()
373 "Check if org-mobile-directory has been set up."
374 (org-mobile-cleanup-encryption-tempfile)
375 (unless (and org-directory
376 (stringp org-directory
)
377 (string-match "\\S-" org-directory
)
378 (file-exists-p org-directory
)
379 (file-directory-p org-directory
))
381 "Please set `org-directory' to the directory where your org files live"))
382 (unless (and org-mobile-directory
383 (stringp org-mobile-directory
)
384 (string-match "\\S-" org-mobile-directory
)
385 (file-exists-p org-mobile-directory
)
386 (file-directory-p org-mobile-directory
))
388 "Variable `org-mobile-directory' must point to an existing directory"))
389 (unless (and org-mobile-inbox-for-pull
390 (stringp org-mobile-inbox-for-pull
)
391 (string-match "\\S-" org-mobile-inbox-for-pull
)
393 (file-name-directory org-mobile-inbox-for-pull
)))
395 "Variable `org-mobile-inbox-for-pull' must point to a file in an existing directory"))
396 (unless (and org-mobile-checksum-binary
397 (string-match "\\S-" org-mobile-checksum-binary
))
398 (error "No executable found to compute checksums"))
399 (when org-mobile-use-encryption
400 (unless (string-match "\\S-" (org-mobile-encryption-password))
402 "To use encryption, you must set `org-mobile-encryption-password'"))
403 (unless (file-writable-p org-mobile-encryption-tempfile
)
404 (error "Cannot write to encryption tempfile %s"
405 org-mobile-encryption-tempfile
))
406 (unless (executable-find "openssl")
407 (error "openssl is needed to encrypt files"))))
409 (defun org-mobile-create-index-file ()
410 "Write the index file in the WebDAV directory."
411 (let ((files-alist (sort (copy-sequence org-mobile-files-alist
)
412 (lambda (a b
) (string< (cdr a
) (cdr b
)))))
413 (def-todo (default-value 'org-todo-keywords
))
414 (def-tags (default-value 'org-tag-alist
))
415 (target-file (expand-file-name org-mobile-index-file
416 org-mobile-directory
))
417 file link-name todo-kwds done-kwds tags drawers entry kwds dwds twds
)
419 (org-prepare-agenda-buffers (mapcar 'car files-alist
))
420 (setq done-kwds
(org-uniquify org-done-keywords-for-agenda
))
421 (setq todo-kwds
(org-delete-all
423 (org-uniquify org-todo-keywords-for-agenda
)))
424 (setq drawers
(org-uniquify org-drawers-for-agenda
))
425 (setq tags
(org-uniquify
429 (cond ((stringp e
) e
)
431 (if (stringp (car e
)) (car e
) nil
))
433 org-tag-alist-for-agenda
))))
435 (if org-mobile-use-encryption
436 org-mobile-encryption-tempfile
438 (while (setq entry
(pop def-todo
))
439 (insert "#+READONLY\n")
440 (setq kwds
(mapcar (lambda (x) (if (string-match "(" x
)
441 (substring x
0 (match-beginning 0))
444 (insert "#+TODO: " (mapconcat 'identity kwds
" ") "\n")
445 (setq dwds
(member "|" kwds
)
446 twds
(org-delete-all dwds kwds
)
447 todo-kwds
(org-delete-all twds todo-kwds
)
448 done-kwds
(org-delete-all dwds done-kwds
)))
449 (when (or todo-kwds done-kwds
)
450 (insert "#+TODO: " (mapconcat 'identity todo-kwds
" ") " | "
451 (mapconcat 'identity done-kwds
" ") "\n"))
452 (setq def-tags
(mapcar
456 ((eq (car x
) :startgroup
) "{")
457 ((eq (car x
) :endgroup
) "}")
458 ((eq (car x
) :newline
) nil
)
462 (setq def-tags
(delq nil def-tags
))
463 (setq tags
(org-delete-all def-tags tags
))
464 (setq tags
(sort tags
(lambda (a b
) (string< (downcase a
) (downcase b
)))))
465 (setq tags
(append def-tags tags nil
))
466 (insert "#+TAGS: " (mapconcat 'identity tags
" ") "\n")
467 (insert "#+DRAWERS: " (mapconcat 'identity drawers
" ") "\n")
468 (insert "#+ALLPRIORITIES: A B C" "\n")
469 (when (file-exists-p (expand-file-name
470 org-mobile-directory
"agendas.org"))
471 (insert "* [[file:agendas.org][Agenda Views]]\n"))
472 (while (setq entry
(pop files-alist
))
473 (setq file
(car entry
)
474 link-name
(cdr entry
))
475 (insert (format "* [[file:%s][%s]]\n"
476 link-name link-name
)))
477 (push (cons org-mobile-index-file
(md5 (buffer-string)))
478 org-mobile-checksum-files
))
479 (when org-mobile-use-encryption
480 (org-mobile-encrypt-and-move org-mobile-encryption-tempfile
482 (org-mobile-cleanup-encryption-tempfile))))
484 (defun org-mobile-copy-agenda-files ()
485 "Copy all agenda files to the stage or WebDAV directory."
486 (let ((files-alist org-mobile-files-alist
)
487 file buf entry link-name target-path target-dir check
)
488 (while (setq entry
(pop files-alist
))
489 (setq file
(car entry
) link-name
(cdr entry
))
490 (when (file-exists-p file
)
491 (setq target-path
(expand-file-name link-name org-mobile-directory
)
492 target-dir
(file-name-directory target-path
))
493 (unless (file-directory-p target-dir
)
494 (make-directory target-dir
'parents
))
495 (if org-mobile-use-encryption
496 (org-mobile-encrypt-and-move file target-path
)
497 (copy-file file target-path
'ok-if-exists
))
498 (setq check
(shell-command-to-string
499 (concat org-mobile-checksum-binary
" "
500 (shell-quote-argument (expand-file-name file
)))))
501 (when (string-match "[a-fA-F0-9]\\{30,40\\}" check
)
502 (push (cons link-name
(match-string 0 check
))
503 org-mobile-checksum-files
))))
505 (setq file
(expand-file-name org-mobile-capture-file
506 org-mobile-directory
))
508 (setq buf
(find-file file
))
509 (when (and (= (point-min) (point-max)))
512 (when org-mobile-use-encryption
513 (write-file org-mobile-encryption-tempfile
)
514 (org-mobile-encrypt-and-move org-mobile-encryption-tempfile file
)))
515 (push (cons org-mobile-capture-file
(md5 (buffer-string)))
516 org-mobile-checksum-files
))
517 (org-mobile-cleanup-encryption-tempfile)
520 (defun org-mobile-write-checksums ()
521 "Create checksums for all files in `org-mobile-directory'.
522 The table of checksums is written to the file mobile-checksums."
523 (let ((sumfile (expand-file-name "checksums.dat" org-mobile-directory
))
524 (files org-mobile-checksum-files
)
526 (with-temp-file sumfile
527 (set-buffer-file-coding-system 'undecided-unix nil
)
528 (while (setq entry
(pop files
))
529 (setq file
(car entry
) sum
(cdr entry
))
530 (insert (format "%s %s\n" sum file
))))))
532 (defun org-mobile-sumo-agenda-command ()
533 "Return an agenda custom command that comprises all custom commands."
535 ;; normalize different versions
539 (cond ((stringp (cdr x
)) nil
)
540 ((stringp (nth 1 x
)) x
)
541 ((not (nth 1 x
)) (cons (car x
) (cons "" (cddr x
))))
542 (t (cons (car x
) (cons "" (cdr x
))))))
543 org-agenda-custom-commands
)))
544 (default-list '(("a" "Agenda" agenda
) ("t" "All TODO" alltodo
)))
545 thelist new e key desc type match settings cmds gkey gdesc gsettings cnt
)
547 ((eq org-mobile-agendas
'custom
)
548 (setq thelist custom-list
))
549 ((eq org-mobile-agendas
'default
)
550 (setq thelist default-list
))
551 ((eq org-mobile-agendas
'all
)
552 (setq thelist custom-list
)
553 (unless (assoc "t" thelist
) (push '("t" "ALL TODO" alltodo
) thelist
))
554 (unless (assoc "a" thelist
) (push '("a" "Agenda" agenda
) thelist
)))
555 ((listp org-mobile-agendas
)
556 (setq thelist
(append custom-list default-list
))
557 (setq thelist
(delq nil
(mapcar (lambda (k) (assoc k thelist
))
558 org-mobile-agendas
)))))
559 (while (setq e
(pop thelist
))
562 ;; this is a description entry - skip it
564 ((eq (nth 2 e
) 'search
)
565 ;; Search view is interactive, skip
567 ((memq (nth 2 e
) '(todo-tree tags-tree occur-tree
))
568 ;; These are trees, not really agenda commands
570 ((and (memq (nth 2 e
) '(todo tags tags-todo
))
572 (not (string-match "\\S-" (nth 3 e
)))))
573 ;; These would be interactive because the match string is empty
575 ((memq (nth 2 e
) '(agenda alltodo todo tags tags-todo
))
577 (setq key
(car e
) desc
(nth 1 e
) type
(nth 2 e
) match
(nth 3 e
)
580 (cons (list 'org-agenda-title-append
581 (concat "<after>KEYS=" key
" TITLE: "
582 (if (and (stringp desc
) (> (length desc
) 0))
583 desc
(symbol-name type
))
586 (push (list type match settings
) new
))
587 ((or (functionp (nth 2 e
)) (symbolp (nth 2 e
)))
588 ;; A user-defined function, which can do anything, so simply
593 (setq gkey
(car e
) gdesc
(nth 1 e
) gsettings
(nth 3 e
) cmds
(nth 2 e
))
595 (while (setq e
(pop cmds
))
596 (setq type
(car e
) match
(nth 1 e
) settings
(nth 2 e
))
597 (setq settings
(append gsettings settings
))
599 (cons (list 'org-agenda-title-append
600 (concat "<after>KEYS=" gkey
"#" (number-to-string
602 " TITLE: " gdesc
" " match
"</after>"))
604 (push (list type match settings
) new
)))))
605 (and new
(list "X" "SUMO" (reverse new
)
606 '((org-agenda-compact-blocks nil
))))))
608 (defvar org-mobile-creating-agendas nil
)
609 (defun org-mobile-write-agenda-for-mobile (file)
610 (let ((all (buffer-string)) in-date id pl prefix line app short m sexp
)
613 (insert "#+READONLY\n")
615 (goto-char (point-min))
618 ((looking-at "[ \t]*$")) ; keep empty lines
620 ;; remove underlining
621 (delete-region (point) (point-at-eol)))
622 ((get-text-property (point) 'org-agenda-structural-header
)
624 (setq app
(get-text-property (point)
625 'org-agenda-title-append
))
626 (setq short
(get-text-property (point)
628 (when (and short
(looking-at ".+"))
629 (replace-match short
)
630 (beginning-of-line 1))
634 (beginning-of-line 1))
636 ((get-text-property (point) 'org-agenda-date-header
)
639 ((setq m
(or (get-text-property (point) 'org-hd-marker
)
640 (get-text-property (point) 'org-marker
)))
641 (setq sexp
(member (get-text-property (point) 'type
)
643 (if (setq pl
(text-property-any (point) (point-at-eol) 'org-heading t
))
645 (setq prefix
(org-trim (buffer-substring
647 line
(org-trim (buffer-substring
650 (delete-region (point-at-bol) (point-at-eol))
651 (insert line
"<before>" prefix
"</before>")
652 (beginning-of-line 1))
653 (and (looking-at "[ \t]+") (replace-match "")))
654 (insert (if in-date
"*** " "** "))
658 (insert (org-agenda-get-some-entry-text
662 (if (org-bound-and-true-p
663 org-mobile-force-id-on-agenda-items
)
664 (org-id-get m
'create
)
665 (or (org-entry-get m
"ID")
666 (org-mobile-get-outline-path-link m
))))
667 (insert " :PROPERTIES:\n :ORIGINAL_ID: " id
669 (beginning-of-line 2))
670 (push (cons "agendas.org" (md5 (buffer-string)))
671 org-mobile-checksum-files
))
672 (message "Agenda written to Org file %s" file
)))
674 (defun org-mobile-get-outline-path-link (pom)
675 (org-with-point-at pom
677 (org-mobile-escape-olp (file-name-nondirectory buffer-file-name
))
679 (mapconcat 'org-mobile-escape-olp
680 (org-get-outline-path)
683 (org-mobile-escape-olp (nth 4 (org-heading-components))))))
685 (defun org-mobile-escape-olp (s)
686 (let ((table '(?
: ?
/)))
687 (org-link-escape s table
)))
690 (defun org-mobile-create-sumo-agenda ()
691 "Create a file that contains all custom agenda views."
693 (let* ((file (expand-file-name "agendas.org"
694 org-mobile-directory
))
695 (file1 (if org-mobile-use-encryption
696 org-mobile-encryption-tempfile
698 (sumo (org-mobile-sumo-agenda-command))
699 (org-agenda-custom-commands
700 (list (append sumo
(list (list file1
)))))
701 (org-mobile-creating-agendas t
))
702 (unless (file-writable-p file1
)
703 (error "Cannot write to file %s" file1
))
705 (org-store-agenda-views))
706 (when org-mobile-use-encryption
707 (org-mobile-encrypt-and-move file1 file
)
709 (org-mobile-cleanup-encryption-tempfile))))
711 (defun org-mobile-encrypt-and-move (infile outfile
)
712 "Encrypt INFILE locally to INFILE_enc, then move it to OUTFILE.
713 We do this in two steps so that remote paths will work, even if the
714 encryption program does not understand them."
715 (let ((encfile (concat infile
"_enc")))
716 (org-mobile-encrypt-file infile encfile
)
718 (copy-file encfile outfile
'ok-if-exists
)
719 (delete-file encfile
))))
721 (defun org-mobile-encrypt-file (infile outfile
)
722 "Encrypt INFILE to OUTFILE, using `org-mobile-encryption-password'."
724 (format "openssl enc -aes-256-cbc -salt -pass %s -in %s -out %s"
725 (shell-quote-argument (concat "pass:"
726 (org-mobile-encryption-password)))
727 (shell-quote-argument (expand-file-name infile
))
728 (shell-quote-argument (expand-file-name outfile
)))))
730 (defun org-mobile-decrypt-file (infile outfile
)
731 "Decrypt INFILE to OUTFILE, using `org-mobile-encryption-password'."
733 (format "openssl enc -d -aes-256-cbc -salt -pass %s -in %s -out %s"
734 (shell-quote-argument (concat "pass:"
735 (org-mobile-encryption-password)))
736 (shell-quote-argument (expand-file-name infile
))
737 (shell-quote-argument (expand-file-name outfile
)))))
739 (defun org-mobile-cleanup-encryption-tempfile ()
740 "Remove the encryption tempfile if it exists."
741 (and (stringp org-mobile-encryption-tempfile
)
742 (file-exists-p org-mobile-encryption-tempfile
)
743 (delete-file org-mobile-encryption-tempfile
)))
745 (defun org-mobile-move-capture ()
746 "Move the contents of the capture file to the inbox file.
747 Return a marker to the location where the new content has been added.
748 If nothing new has been added, return nil."
751 (capture-file (expand-file-name org-mobile-capture-file
752 org-mobile-directory
))
753 (inbox-buffer (find-file-noselect org-mobile-inbox-for-pull
))
755 (if (not org-mobile-use-encryption
)
756 (find-file-noselect capture-file
)
757 (org-mobile-cleanup-encryption-tempfile)
758 (setq encfile
(concat org-mobile-encryption-tempfile
"_enc"))
759 (copy-file capture-file encfile
)
760 (org-mobile-decrypt-file encfile org-mobile-encryption-tempfile
)
761 (find-file-noselect org-mobile-encryption-tempfile
)))
762 (insertion-point (make-marker))
764 (with-current-buffer capture-buffer
765 (setq content
(buffer-string))
766 (setq not-empty
(string-match "\\S-" content
))
768 (set-buffer inbox-buffer
)
770 (goto-char (point-max))
771 (or (bolp) (newline))
772 (move-marker insertion-point
773 (prog1 (point) (insert content
)))
775 (set-buffer capture-buffer
)
778 (org-mobile-update-checksum-for-capture-file (buffer-string))))
779 (kill-buffer capture-buffer
)
780 (when org-mobile-use-encryption
781 (org-mobile-encrypt-and-move org-mobile-encryption-tempfile
783 (org-mobile-cleanup-encryption-tempfile))
784 (if not-empty insertion-point
)))
786 (defun org-mobile-update-checksum-for-capture-file (buffer-string)
787 "Find the checksum line and modify it to match BUFFER-STRING."
788 (let* ((file (expand-file-name "checksums.dat" org-mobile-directory
))
789 (buffer (find-file-noselect file
)))
791 (with-current-buffer buffer
792 (when (re-search-forward (concat "\\([0-9a-fA-F]\\{30,\\}\\).*?"
793 (regexp-quote org-mobile-capture-file
)
795 (goto-char (match-beginning 1))
796 (delete-region (match-beginning 1) (match-end 1))
797 (insert (md5 buffer-string
))
799 (kill-buffer buffer
))))
801 (defun org-mobile-apply (&optional beg end
)
802 "Apply all change requests in the current buffer.
803 If BEG and END are given, only do this in that region."
805 (require 'org-archive
)
806 (setq org-mobile-last-flagged-files nil
)
807 (setq beg
(or beg
(point-min)) end
(or end
(point-max)))
809 ;; Remove all Note IDs
811 (while (re-search-forward "^\\*\\* Note ID: [-0-9A-F]+[ \t]*\n" end t
)
814 ;; Find all the referenced entries, without making any changes yet
815 (let ((marker (make-marker))
816 (bos-marker (make-marker))
817 (end (move-marker (make-marker) end
))
823 id-pos org-mobile-error
)
825 ;; Count the new captures
827 (while (re-search-forward "^\\* \\(.*\\)" end t
)
828 (and (>= (- (match-end 1) (match-beginning 1)) 2)
829 (not (equal (downcase (substring (match-string 1) 0 2)) "f("))
833 (while (re-search-forward
834 "^\\*+[ \t]+F(\\([^():\n]*\\)\\(:\\([^()\n]*\\)\\)?)[ \t]+\\[\\[\\(\\(id\\|olp\\):\\([^]\n]+\\)\\)" end t
)
835 (setq id-pos
(condition-case msg
836 (org-mobile-locate-entry (match-string 4))
837 (error (nth 1 msg
))))
838 (when (and (markerp id-pos
)
839 (not (member (marker-buffer id-pos
) buf-list
)))
840 (org-mobile-timestamp-buffer (marker-buffer id-pos
))
841 (push (marker-buffer id-pos
) buf-list
))
843 (if (or (not id-pos
) (stringp id-pos
))
845 (goto-char (+ 2 (point-at-bol)))
848 (add-text-properties (point-at-bol) (point-at-eol)
849 (list 'org-mobile-marker
850 (or id-pos
"Linked entry not found")))))
852 ;; OK, now go back and start applying
854 (while (re-search-forward "^\\*+[ \t]+F(\\([^():\n]*\\)\\(:\\([^()\n]*\\)\\)?)" end t
)
856 (setq id-pos
(get-text-property (point-at-bol) 'org-mobile-marker
))
857 (if (not (markerp id-pos
))
860 (insert "UNKNOWN PROBLEM"))
861 (let* ((action (match-string 1))
862 (data (and (match-end 3) (match-string 3)))
864 (eos (save-excursion (org-end-of-subtree t t
)))
865 (cmd (if (equal action
"")
868 (org-toggle-tag "FLAGGED" 'on
)
870 (org-entry-put nil
"THEFLAGGINGNOTE" note
)))
872 (cdr (assoc action org-mobile-action-alist
))))
873 (note (and (equal action
"")
874 (buffer-substring (1+ (point-at-eol)) eos
)))
875 (org-inhibit-logging 'note
) ;; Do not take notes interactively
878 (move-marker bos-marker
(point))
879 (if (re-search-forward "^** Old value[ \t]*$" eos t
)
880 (setq old
(buffer-substring
882 (progn (outline-next-heading) (point)))))
883 (if (re-search-forward "^** New value[ \t]*$" eos t
)
884 (setq new
(buffer-substring
886 (progn (outline-next-heading)
887 (if (eobp) (org-back-over-empty-lines))
889 (setq old
(and old
(if (string-match "\\S-" old
) old nil
)))
890 (setq new
(and new
(if (string-match "\\S-" new
) new nil
)))
891 (if (and note
(> (length note
) 0))
892 ;; Make Note into a single line, to fit into a property
893 (setq note
(mapconcat 'identity
894 (org-split-string (org-trim note
) "\n")
896 (unless (equal data
"body")
897 (setq new
(and new
(org-trim new
))
898 old
(and old
(org-trim old
))))
899 (goto-char (+ 2 bos-marker
))
900 (unless (markerp id-pos
)
901 (insert "BAD REFERENCE ")
908 ;; Remember this place so that we can return
909 (move-marker marker
(point))
910 (setq org-mobile-error nil
)
913 (org-with-point-at id-pos
916 (if (member "FLAGGED" (org-get-tags))
917 (add-to-list 'org-mobile-last-flagged-files
918 (buffer-file-name (current-buffer))))))
919 (error (setq org-mobile-error msg
))))
920 (when org-mobile-error
921 (org-pop-to-buffer-same-window (marker-buffer marker
))
924 (insert (if (stringp (nth 1 org-mobile-error
))
925 (nth 1 org-mobile-error
)
929 ;; If we get here, the action has been applied successfully
930 ;; So remove the entry
931 (goto-char bos-marker
)
932 (delete-region (point) (org-end-of-subtree t t
))))))
934 (move-marker marker nil
)
935 (move-marker end nil
)
936 (message "%d new, %d edits, %d flags, %d errors" cnt-new
937 cnt-edit cnt-flag cnt-error
)
940 (defun org-mobile-timestamp-buffer (buf)
941 "Time stamp buffer BUF, just to make sure its checksum will change."
942 (with-current-buffer buf
946 (goto-char (point-min))
947 (if (re-search-forward
948 "^\\([ \t]*\\)#\\+LAST_MOBILE_CHANGE:.*\n?" nil t
)
950 (goto-char (match-end 1))
951 (delete-region (point) (match-end 0)))
952 (if (looking-at ".*?-\\*-.*-\\*-")
954 (insert "#+LAST_MOBILE_CHANGE: "
955 (format-time-string "%Y-%m-%d %T") "\n")))))
957 (defun org-mobile-smart-read ()
958 "Parse the entry at point for shortcuts and expand them.
959 These shortcuts are meant for fast and easy typing on the limited
960 keyboards of a mobile device. Below we show a list of the shortcuts
961 currently implemented.
963 The entry is expected to contain an inactive time stamp indicating when
964 the entry was created. When setting dates and
965 times (for example for deadlines), the time strings are interpreted
966 relative to that creation date.
967 Abbreviations are expected to take up entire lines, just because it is so
968 easy to type RET on a mobile device. Abbreviations start with one or two
969 letters, followed immediately by a dot and then additional information.
970 Generally the entire shortcut line is removed after action have been taken.
971 Time stamps will be constructed using `org-read-date'. So for example a
972 line \"dd. 2tue\" will set a deadline on the second Tuesday after the
975 Here are the shortcuts currently implemented:
977 dd. string set deadline
978 ss. string set scheduling
979 tt. string set time tamp, here.
980 ti. string set inactive time
982 tg. tag1 tag2 tag3 set all these tags, change case where necessary
983 td. kwd set this todo keyword, change case where necessary
985 FIXME: Hmmm, not sure if we can make his work against the
986 auto-correction feature. Needs a bit more thinking. So this function
987 is currently a noop.")
989 (defun org-mobile-locate-entry (link)
990 (if (string-match "\\`id:\\(.*\\)$" link
)
991 (org-id-find (match-string 1 link
) 'marker
)
992 (if (not (string-match "\\`olp:\\(.*?\\):\\(.*\\)$" link
))
994 (let ((file (match-string 1 link
))
995 (path (match-string 2 link
)))
996 (setq file
(org-link-unescape file
))
997 (setq file
(expand-file-name file org-directory
))
998 (setq path
(mapcar 'org-link-unescape
999 (org-split-string path
"/")))
1000 (org-find-olp (cons file path
))))))
1002 (defun org-mobile-edit (what old new
)
1003 "Edit item WHAT in the current entry by replacing OLD with NEW.
1004 WHAT can be \"heading\", \"todo\", \"tags\", \"priority\", or \"body\".
1005 The edit only takes place if the current value is equal (except for
1006 white space) the OLD. If this is so, OLD will be replace by NEW
1007 and the command will return t. If something goes wrong, a string will
1008 be returned that indicates what went wrong."
1009 (let (current old1 new1
)
1010 (if (stringp what
) (setq what
(intern what
)))
1014 ((memq what
'(todo todostate
))
1015 (setq current
(org-get-todo-state))
1017 ((equal new
"DONEARCHIVE")
1019 (org-archive-subtree-default))
1020 ((equal new current
) t
) ; nothing needs to be done
1021 ((or (equal current old
)
1022 (eq org-mobile-force-mobile-change t
)
1023 (memq 'todo org-mobile-force-mobile-change
))
1024 (org-todo (or new
'none
)) t
)
1025 (t (error "State before change was expected as \"%s\", but is \"%s\""
1029 (setq current
(org-get-tags)
1030 new1
(and new
(org-split-string new
":+"))
1031 old1
(and old
(org-split-string old
":+")))
1033 ((org-mobile-tags-same-p current new1
) t
) ; no change needed
1034 ((or (org-mobile-tags-same-p current old1
)
1035 (eq org-mobile-force-mobile-change t
)
1036 (memq 'tags org-mobile-force-mobile-change
))
1037 (org-set-tags-to new1
) t
)
1038 (t (error "Tags before change were expected as \"%s\", but are \"%s\""
1039 (or old
"") (or current
"")))))
1041 ((eq what
'priority
)
1042 (when (looking-at org-complex-heading-regexp
)
1043 (setq current
(and (match-end 3) (substring (match-string 3) 2 3)))
1045 ((equal current new
) t
) ; no action required
1046 ((or (equal current old
)
1047 (eq org-mobile-force-mobile-change t
)
1048 (memq 'tags org-mobile-force-mobile-change
))
1049 (org-priority (and new
(string-to-char new
))))
1050 (t (error "Priority was expected to be %s, but is %s"
1054 (when (looking-at org-complex-heading-regexp
)
1055 (setq current
(match-string 4))
1057 ((equal current new
) t
) ; no action required
1058 ((or (equal current old
)
1059 (eq org-mobile-force-mobile-change t
)
1060 (memq 'heading org-mobile-force-mobile-change
))
1061 (goto-char (match-beginning 4))
1063 (delete-region (point) (+ (point) (length current
)))
1064 (org-set-tags nil
'align
))
1065 (t (error "Heading changed in MobileOrg and on the computer")))))
1068 (setq current
(buffer-substring (min (1+ (point-at-eol)) (point-max))
1069 (save-excursion (outline-next-heading)
1071 (if (not (string-match "\\S-" current
)) (setq current nil
))
1073 ((org-mobile-bodies-same-p current new
) t
) ; no action necessary
1074 ((or (org-mobile-bodies-same-p current old
)
1075 (eq org-mobile-force-mobile-change t
)
1076 (memq 'body org-mobile-force-mobile-change
))
1080 (or (bolp) (insert "\n"))
1081 (delete-region (point) (progn (org-back-to-heading t
)
1082 (outline-next-heading)
1085 (t (error "Body was changed in MobileOrg and on the computer")))))))
1087 (defun org-mobile-tags-same-p (list1 list2
)
1088 "Are the two tag lists the same?"
1089 (not (or (org-delete-all list1 list2
)
1090 (org-delete-all list2 list1
))))
1092 (defun org-mobile-bodies-same-p (a b
)
1093 "Compare if A and B are visually equal strings.
1094 We first remove leading and trailing white space from the entire strings.
1095 Then we split the strings into lines and remove leading/trailing whitespace
1096 from each line. Then we compare.
1097 A and B must be strings or nil."
1099 ((and (not a
) (not b
)) t
)
1100 ((or (not a
) (not b
)) nil
)
1101 (t (setq a
(org-trim a
) b
(org-trim b
))
1102 (setq a
(mapconcat 'identity
(org-split-string a
"[ \t]*\n[ \t]*") "\n"))
1103 (setq b
(mapconcat 'identity
(org-split-string b
"[ \t]*\n[ \t]*") "\n"))
1106 (provide 'org-mobile
)
1108 ;;; org-mobile.el ends here