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'."
73 (defcustom org-mobile-directory
""
74 "The WebDAV directory where the interaction with the mobile takes place."
78 (defcustom org-mobile-use-encryption nil
79 "Non-nil means keep only encrypted files on the WebDAV server.
80 Encryption uses AES-256, with a password given in
81 `org-mobile-encryption-password'.
82 When nil, plain files are kept on the server.
83 Turning on encryption requires to set the same password in the MobileOrg
84 application. Before turning this on, check of MobileOrg does already
85 support it - at the time of this writing it did not yet."
89 (defcustom org-mobile-encryption-tempfile
"~/orgtmpcrypt"
90 "File that is being used as a temporary file for encryption.
91 This must be local file on your local machine (not on the WebDAV server).
92 You might want to put this file into a directory where only you have access."
96 (defcustom org-mobile-encryption-password
""
97 "Password for encrypting files uploaded to the server.
98 This is a single password which is used for AES-256 encryption. The same
99 password must also be set in the MobileOrg application. All Org files,
100 including mobileorg.org will be encrypted using this password.
102 SECURITY CONSIDERATIONS:
104 Note that, when Org runs the encryption commands, the password could
105 be visible briefly on your system with the `ps' command. So this method is
106 only intended to keep the files secure on the server, not on your own machine.
108 Also, if you set this variable in an init file (.emacs or .emacs.d/init.el
109 or custom.el...) and if that file is stored in a way so that other can read
110 it, this also limits the security of this approach. You can also leave
111 this variable empty - Org will then ask for the password once per Emacs
114 :type
'(string :tag
"Password"))
116 (defvar org-mobile-encryption-password-session nil
)
118 (defun org-mobile-encryption-password ()
119 (or (org-string-nw-p org-mobile-encryption-password
)
120 (org-string-nw-p org-mobile-encryption-password-session
)
121 (setq org-mobile-encryption-password-session
122 (read-passwd "Password for MobileOrg: " t
))))
124 (defcustom org-mobile-inbox-for-pull
"~/org/from-mobile.org"
125 "The file where captured notes and flags will be appended to.
126 During the execution of `org-mobile-pull', the file
127 `org-mobile-capture-file' will be emptied it's contents have
128 been appended to the file given here. This file should be in
129 `org-directory', and not in the staging area or on the web server."
133 (defconst org-mobile-capture-file
"mobileorg.org"
134 "The capture file where the mobile stores captured notes and flags.
135 This should not be changed, because MobileOrg assumes this name.")
137 (defcustom org-mobile-index-file
"index.org"
138 "The index file with links to all Org files that should be loaded by MobileOrg.
139 Relative to `org-mobile-directory'. The Address field in the MobileOrg setup
140 should point to this file."
144 (defcustom org-mobile-agendas
'all
145 "The agendas that should be pushed to MobileOrg.
148 default the weekly agenda and the global TODO list
149 custom all custom agendas defined by the user
150 all the custom agendas and the default ones
151 list a list of selection key(s) as string."
154 (const :tag
"Default Agendas" default
)
155 (const :tag
"Custom Agendas" custom
)
156 (const :tag
"Default and Custom Agendas" all
)
157 (repeat :tag
"Selected"
158 (string :tag
"Selection Keys"))))
160 (defcustom org-mobile-force-id-on-agenda-items t
161 "Non-nil means make all agenda items carry an ID."
165 (defcustom org-mobile-force-mobile-change nil
166 "Non-nil means force the change made on the mobile device.
167 So even if there have been changes to the computer version of the entry,
168 force the new value set on the mobile.
169 When nil, mark the entry from the mobile with an error message.
170 Instead of nil or t, this variable can also be a list of symbols, indicating
171 the editing types for which the mobile version should always dominate."
174 (const :tag
"Always" t
)
175 (const :tag
"Never" nil
)
176 (set :greedy t
:tag
"Specify"
183 (defcustom org-mobile-action-alist
184 '(("edit" .
(org-mobile-edit data old new
)))
185 "Alist with flags and actions for mobile sync.
186 When flagging an entry, MobileOrg will create entries that look like
188 * F(action:data) [[id:entry-id][entry title]]
190 This alist defines that the ACTION in the parentheses of F() should mean,
191 i.e. what action should be taken. The :data part in the parenthesis is
192 optional. If present, the string after the colon will be passed to the
193 action form as the `data' variable.
194 The car of each elements of the alist is an actions string. The cdr is
195 an Emacs Lisp form that will be evaluated with the cursor on the headline
198 For now, it is not recommended to change this variable."
201 (cons (string :tag
"Action flag")
202 (sexp :tag
"Action form"))))
204 (defcustom org-mobile-checksum-binary
(or (executable-find "shasum")
205 (executable-find "sha1sum")
206 (executable-find "md5sum")
207 (executable-find "md5"))
208 "Executable used for computing checksums of agenda files."
212 (defvar org-mobile-pre-push-hook nil
213 "Hook run before running `org-mobile-push'.
214 This could be used to clean up `org-mobile-directory', for example to
215 remove files that used to be included in the agenda but no longer are.
216 The presence of such files would not really be a problem, but after time
217 they may accumulate.")
219 (defvar org-mobile-post-push-hook nil
220 "Hook run after running `org-mobile-push'.
221 If Emacs does not have direct write access to the WebDAV directory used
222 by the mobile device, this hook should be used to copy all files from the
223 local staging directory `org-mobile-directory' to the WebDAV directory,
224 for example using `rsync' or `scp'.")
226 (defvar org-mobile-pre-pull-hook nil
227 "Hook run before executing `org-mobile-pull'.
228 If Emacs does not have direct write access to the WebDAV directory used
229 by the mobile device, this hook should be used to copy the capture file
230 `mobileorg.org' from the WebDAV location to the local staging
231 directory `org-mobile-directory'.")
233 (defvar org-mobile-post-pull-hook nil
234 "Hook run after running `org-mobile-pull'.
235 If Emacs does not have direct write access to the WebDAV directory used
236 by the mobile device, this hook should be used to copy the emptied
237 capture file `mobileorg.org' back to the WebDAV directory, for example
238 using `rsync' or `scp'.")
240 (defvar org-mobile-last-flagged-files nil
241 "List of files containing entries flagged in the latest pull.")
243 (defvar org-mobile-files-alist nil
)
244 (defvar org-mobile-checksum-files nil
)
246 (defun org-mobile-prepare-file-lists ()
247 (setq org-mobile-files-alist
(org-mobile-files-alist))
248 (setq org-mobile-checksum-files nil
))
250 (defun org-mobile-files-alist ()
251 "Expand the list in `org-mobile-files' to a list of existing files.
252 Also exclude files matching `org-mobile-files-exclude-regexp'."
253 (let* ((include-archives
254 (and (member 'org-agenda-text-search-extra-files org-mobile-files
)
255 (member 'agenda-archives org-agenda-text-search-extra-files
)
262 ((eq f
'org-agenda-files
)
263 (org-agenda-files t include-archives
))
264 ((eq f
'org-agenda-text-search-extra-files
)
265 (delq 'agenda-archives
267 org-agenda-text-search-extra-files
)))
268 ((and (stringp f
) (file-directory-p f
))
269 (directory-files f
'full
"\\.org\\'"))
270 ((and (stringp f
) (file-exists-p f
))
277 (unless (and (not (string= org-mobile-files-exclude-regexp
""))
278 (string-match org-mobile-files-exclude-regexp f
))
281 (orgdir-uname (file-name-as-directory (file-truename org-directory
)))
282 (orgdir-re (concat "\\`" (regexp-quote orgdir-uname
)))
283 uname seen rtn file link-name
)
284 ;; Make the files unique, and determine the name under which they will
286 (while (setq file
(pop files
))
287 (if (not (file-name-absolute-p file
))
288 (setq file
(expand-file-name file org-directory
)))
289 (setq uname
(file-truename file
))
290 (unless (member uname seen
)
292 (if (string-match orgdir-re uname
)
293 (setq link-name
(substring uname
(match-end 0)))
294 (setq link-name
(file-name-nondirectory uname
)))
295 (push (cons file link-name
) rtn
)))
299 (defun org-mobile-push ()
300 "Push the current state of Org affairs to the WebDAV directory.
301 This will create the index file, copy all agenda files there, and also
302 create all custom agenda views, for upload to the mobile phone."
304 (let ((a-buffer (get-buffer org-agenda-buffer-name
)))
305 (let ((org-agenda-buffer-name "*SUMO*")
306 (org-agenda-tag-filter org-agenda-tag-filter
)
307 (org-agenda-redo-command org-agenda-redo-command
))
309 (save-window-excursion
310 (run-hooks 'org-mobile-pre-push-hook
)
311 (org-mobile-check-setup)
312 (org-mobile-prepare-file-lists)
313 (message "Creating agendas...")
314 (let ((inhibit-redisplay t
)) (org-mobile-create-sumo-agenda))
315 (message "Creating agendas...done")
316 (org-save-all-org-buffers) ; to save any IDs created by this process
317 (message "Copying files...")
318 (org-mobile-copy-agenda-files)
319 (message "Writing index file...")
320 (org-mobile-create-index-file)
321 (message "Writing checksums...")
322 (org-mobile-write-checksums)
323 (run-hooks 'org-mobile-post-push-hook
))))
325 (when (and a-buffer
(buffer-live-p a-buffer
))
326 (if (not (get-buffer-window a-buffer
))
327 (kill-buffer a-buffer
)
328 (let ((cw (selected-window)))
329 (select-window (get-buffer-window a-buffer
))
331 (select-window cw
)))))
332 (message "Files for mobile viewer staged"))
334 (defvar org-mobile-before-process-capture-hook nil
335 "Hook that is run after content was moved to `org-mobile-inbox-for-pull'.
336 The inbox file is visited by the current buffer, and the buffer is
337 narrowed to the newly captured data.")
340 (defun org-mobile-pull ()
341 "Pull the contents of `org-mobile-capture-file' and integrate them.
342 Apply all flagged actions, flag entries to be flagged and then call an
343 agenda view showing the flagged items."
345 (org-mobile-check-setup)
346 (run-hooks 'org-mobile-pre-pull-hook
)
347 (let ((insertion-marker (org-mobile-move-capture)))
348 (if (not (markerp insertion-marker
))
349 (message "No new items")
350 (org-with-point-at insertion-marker
352 (narrow-to-region (point) (point-max))
353 (run-hooks 'org-mobile-before-process-capture-hook
)))
354 (org-with-point-at insertion-marker
355 (org-mobile-apply (point) (point-max)))
356 (move-marker insertion-marker nil
)
357 (run-hooks 'org-mobile-post-pull-hook
)
358 (when org-mobile-last-flagged-files
359 ;; Make an agenda view of flagged entries, but only in the files
360 ;; where stuff has been added.
361 (put 'org-agenda-files
'org-restrict org-mobile-last-flagged-files
)
362 (let ((org-agenda-keep-restricted-file-list t
))
363 (org-agenda nil
"?"))))))
365 (defun org-mobile-check-setup ()
366 "Check if org-mobile-directory has been set up."
367 (org-mobile-cleanup-encryption-tempfile)
368 (unless (and org-directory
369 (stringp org-directory
)
370 (string-match "\\S-" org-directory
)
371 (file-exists-p org-directory
)
372 (file-directory-p org-directory
))
374 "Please set `org-directory' to the directory where your org files live"))
375 (unless (and org-mobile-directory
376 (stringp org-mobile-directory
)
377 (string-match "\\S-" org-mobile-directory
)
378 (file-exists-p org-mobile-directory
)
379 (file-directory-p org-mobile-directory
))
381 "Variable `org-mobile-directory' must point to an existing directory"))
382 (unless (and org-mobile-inbox-for-pull
383 (stringp org-mobile-inbox-for-pull
)
384 (string-match "\\S-" org-mobile-inbox-for-pull
)
386 (file-name-directory org-mobile-inbox-for-pull
)))
388 "Variable `org-mobile-inbox-for-pull' must point to a file in an existing directory"))
389 (unless (and org-mobile-checksum-binary
390 (string-match "\\S-" org-mobile-checksum-binary
))
391 (error "No executable found to compute checksums"))
392 (when org-mobile-use-encryption
393 (unless (string-match "\\S-" (org-mobile-encryption-password))
395 "To use encryption, you must set `org-mobile-encryption-password'"))
396 (unless (file-writable-p org-mobile-encryption-tempfile
)
397 (error "Cannot write to encryption tempfile %s"
398 org-mobile-encryption-tempfile
))
399 (unless (executable-find "openssl")
400 (error "openssl is needed to encrypt files"))))
402 (defun org-mobile-create-index-file ()
403 "Write the index file in the WebDAV directory."
404 (let ((files-alist (sort (copy-sequence org-mobile-files-alist
)
405 (lambda (a b
) (string< (cdr a
) (cdr b
)))))
406 (def-todo (default-value 'org-todo-keywords
))
407 (def-tags (default-value 'org-tag-alist
))
408 (target-file (expand-file-name org-mobile-index-file
409 org-mobile-directory
))
410 file link-name todo-kwds done-kwds tags drawers entry kwds dwds twds
)
412 (org-prepare-agenda-buffers (mapcar 'car files-alist
))
413 (setq done-kwds
(org-uniquify org-done-keywords-for-agenda
))
414 (setq todo-kwds
(org-delete-all
416 (org-uniquify org-todo-keywords-for-agenda
)))
417 (setq drawers
(org-uniquify org-drawers-for-agenda
))
418 (setq tags
(org-uniquify
422 (cond ((stringp e
) e
)
424 (if (stringp (car e
)) (car e
) nil
))
426 org-tag-alist-for-agenda
))))
428 (if org-mobile-use-encryption
429 org-mobile-encryption-tempfile
431 (while (setq entry
(pop def-todo
))
432 (insert "#+READONLY\n")
433 (setq kwds
(mapcar (lambda (x) (if (string-match "(" x
)
434 (substring x
0 (match-beginning 0))
437 (insert "#+TODO: " (mapconcat 'identity kwds
" ") "\n")
438 (setq dwds
(member "|" kwds
)
439 twds
(org-delete-all dwds kwds
)
440 todo-kwds
(org-delete-all twds todo-kwds
)
441 done-kwds
(org-delete-all dwds done-kwds
)))
442 (when (or todo-kwds done-kwds
)
443 (insert "#+TODO: " (mapconcat 'identity todo-kwds
" ") " | "
444 (mapconcat 'identity done-kwds
" ") "\n"))
445 (setq def-tags
(mapcar
449 ((eq (car x
) :startgroup
) "{")
450 ((eq (car x
) :endgroup
) "}")
451 ((eq (car x
) :newline
) nil
)
455 (setq def-tags
(delq nil def-tags
))
456 (setq tags
(org-delete-all def-tags tags
))
457 (setq tags
(sort tags
(lambda (a b
) (string< (downcase a
) (downcase b
)))))
458 (setq tags
(append def-tags tags nil
))
459 (insert "#+TAGS: " (mapconcat 'identity tags
" ") "\n")
460 (insert "#+DRAWERS: " (mapconcat 'identity drawers
" ") "\n")
461 (insert "#+ALLPRIORITIES: A B C" "\n")
462 (when (file-exists-p (expand-file-name
463 org-mobile-directory
"agendas.org"))
464 (insert "* [[file:agendas.org][Agenda Views]]\n"))
465 (while (setq entry
(pop files-alist
))
466 (setq file
(car entry
)
467 link-name
(cdr entry
))
468 (insert (format "* [[file:%s][%s]]\n"
469 link-name link-name
)))
470 (push (cons org-mobile-index-file
(md5 (buffer-string)))
471 org-mobile-checksum-files
))
472 (when org-mobile-use-encryption
473 (org-mobile-encrypt-and-move org-mobile-encryption-tempfile
475 (org-mobile-cleanup-encryption-tempfile))))
477 (defun org-mobile-copy-agenda-files ()
478 "Copy all agenda files to the stage or WebDAV directory."
479 (let ((files-alist org-mobile-files-alist
)
480 file buf entry link-name target-path target-dir check
)
481 (while (setq entry
(pop files-alist
))
482 (setq file
(car entry
) link-name
(cdr entry
))
483 (when (file-exists-p file
)
484 (setq target-path
(expand-file-name link-name org-mobile-directory
)
485 target-dir
(file-name-directory target-path
))
486 (unless (file-directory-p target-dir
)
487 (make-directory target-dir
'parents
))
488 (if org-mobile-use-encryption
489 (org-mobile-encrypt-and-move file target-path
)
490 (copy-file file target-path
'ok-if-exists
))
491 (setq check
(shell-command-to-string
492 (concat org-mobile-checksum-binary
" "
493 (shell-quote-argument (expand-file-name file
)))))
494 (when (string-match "[a-fA-F0-9]\\{30,40\\}" check
)
495 (push (cons link-name
(match-string 0 check
))
496 org-mobile-checksum-files
))))
498 (setq file
(expand-file-name org-mobile-capture-file
499 org-mobile-directory
))
501 (setq buf
(find-file file
))
502 (when (and (= (point-min) (point-max)))
505 (when org-mobile-use-encryption
506 (write-file org-mobile-encryption-tempfile
)
507 (org-mobile-encrypt-and-move org-mobile-encryption-tempfile file
)))
508 (push (cons org-mobile-capture-file
(md5 (buffer-string)))
509 org-mobile-checksum-files
))
510 (org-mobile-cleanup-encryption-tempfile)
513 (defun org-mobile-write-checksums ()
514 "Create checksums for all files in `org-mobile-directory'.
515 The table of checksums is written to the file mobile-checksums."
516 (let ((sumfile (expand-file-name "checksums.dat" org-mobile-directory
))
517 (files org-mobile-checksum-files
)
519 (with-temp-file sumfile
520 (set-buffer-file-coding-system 'undecided-unix nil
)
521 (while (setq entry
(pop files
))
522 (setq file
(car entry
) sum
(cdr entry
))
523 (insert (format "%s %s\n" sum file
))))))
525 (defun org-mobile-sumo-agenda-command ()
526 "Return an agenda custom command that comprises all custom commands."
528 ;; normalize different versions
532 (cond ((stringp (cdr x
)) nil
)
533 ((stringp (nth 1 x
)) x
)
534 ((not (nth 1 x
)) (cons (car x
) (cons "" (cddr x
))))
535 (t (cons (car x
) (cons "" (cdr x
))))))
536 org-agenda-custom-commands
)))
537 (default-list '(("a" "Agenda" agenda
) ("t" "All TODO" alltodo
)))
538 thelist new e key desc type match settings cmds gkey gdesc gsettings cnt
)
540 ((eq org-mobile-agendas
'custom
)
541 (setq thelist custom-list
))
542 ((eq org-mobile-agendas
'default
)
543 (setq thelist default-list
))
544 ((eq org-mobile-agendas
'all
)
545 (setq thelist custom-list
)
546 (unless (assoc "t" thelist
) (push '("t" "ALL TODO" alltodo
) thelist
))
547 (unless (assoc "a" thelist
) (push '("a" "Agenda" agenda
) thelist
)))
548 ((listp org-mobile-agendas
)
549 (setq thelist
(append custom-list default-list
))
550 (setq thelist
(delq nil
(mapcar (lambda (k) (assoc k thelist
))
551 org-mobile-agendas
)))))
552 (while (setq e
(pop thelist
))
555 ;; this is a description entry - skip it
557 ((eq (nth 2 e
) 'search
)
558 ;; Search view is interactive, skip
560 ((memq (nth 2 e
) '(todo-tree tags-tree occur-tree
))
561 ;; These are trees, not really agenda commands
563 ((and (memq (nth 2 e
) '(todo tags tags-todo
))
565 (not (string-match "\\S-" (nth 3 e
)))))
566 ;; These would be interactive because the match string is empty
568 ((memq (nth 2 e
) '(agenda alltodo todo tags tags-todo
))
570 (setq key
(car e
) desc
(nth 1 e
) type
(nth 2 e
) match
(nth 3 e
)
573 (cons (list 'org-agenda-title-append
574 (concat "<after>KEYS=" key
" TITLE: "
575 (if (and (stringp desc
) (> (length desc
) 0))
576 desc
(symbol-name type
))
577 " " match
"</after>"))
579 (push (list type match settings
) new
))
580 ((or (functionp (nth 2 e
)) (symbolp (nth 2 e
)))
581 ;; A user-defined function, which can do anything, so simply
586 (setq gkey
(car e
) gdesc
(nth 1 e
) gsettings
(nth 3 e
) cmds
(nth 2 e
))
588 (while (setq e
(pop cmds
))
589 (setq type
(car e
) match
(nth 1 e
) settings
(nth 2 e
))
590 (setq settings
(append gsettings settings
))
592 (cons (list 'org-agenda-title-append
593 (concat "<after>KEYS=" gkey
"#" (number-to-string
595 " TITLE: " gdesc
" " match
"</after>"))
597 (push (list type match settings
) new
)))))
598 (and new
(list "X" "SUMO" (reverse new
)
599 '((org-agenda-compact-blocks nil
))))))
601 (defvar org-mobile-creating-agendas nil
)
602 (defun org-mobile-write-agenda-for-mobile (file)
603 (let ((all (buffer-string)) in-date id pl prefix line app short m sexp
)
606 (insert "#+READONLY\n")
608 (goto-char (point-min))
611 ((looking-at "[ \t]*$")) ; keep empty lines
613 ;; remove underlining
614 (delete-region (point) (point-at-eol)))
615 ((get-text-property (point) 'org-agenda-structural-header
)
617 (setq app
(get-text-property (point)
618 'org-agenda-title-append
))
619 (setq short
(get-text-property (point)
621 (when (and short
(looking-at ".+"))
622 (replace-match short
)
623 (beginning-of-line 1))
627 (beginning-of-line 1))
629 ((get-text-property (point) 'org-agenda-date-header
)
632 ((setq m
(or (get-text-property (point) 'org-hd-marker
)
633 (get-text-property (point) 'org-marker
)))
634 (setq sexp
(member (get-text-property (point) 'type
)
636 (if (setq pl
(text-property-any (point) (point-at-eol) 'org-heading t
))
638 (setq prefix
(org-trim (buffer-substring
640 line
(org-trim (buffer-substring
643 (delete-region (point-at-bol) (point-at-eol))
644 (insert line
"<before>" prefix
"</before>")
645 (beginning-of-line 1))
646 (and (looking-at "[ \t]+") (replace-match "")))
647 (insert (if in-date
"*** " "** "))
651 (insert (org-agenda-get-some-entry-text
655 (if (org-bound-and-true-p
656 org-mobile-force-id-on-agenda-items
)
657 (org-id-get m
'create
)
658 (or (org-entry-get m
"ID")
659 (org-mobile-get-outline-path-link m
))))
660 (insert " :PROPERTIES:\n :ORIGINAL_ID: " id
662 (beginning-of-line 2))
663 (push (cons "agendas.org" (md5 (buffer-string)))
664 org-mobile-checksum-files
))
665 (message "Agenda written to Org file %s" file
)))
667 (defun org-mobile-get-outline-path-link (pom)
668 (org-with-point-at pom
670 (org-mobile-escape-olp (file-name-nondirectory buffer-file-name
))
672 (mapconcat 'org-mobile-escape-olp
673 (org-get-outline-path)
676 (org-mobile-escape-olp (nth 4 (org-heading-components))))))
678 (defun org-mobile-escape-olp (s)
679 (let ((table '(?
: ?
/)))
680 (org-link-escape s table
)))
683 (defun org-mobile-create-sumo-agenda ()
684 "Create a file that contains all custom agenda views."
686 (let* ((file (expand-file-name "agendas.org"
687 org-mobile-directory
))
688 (file1 (if org-mobile-use-encryption
689 org-mobile-encryption-tempfile
691 (sumo (org-mobile-sumo-agenda-command))
692 (org-agenda-custom-commands
693 (list (append sumo
(list (list file1
)))))
694 (org-mobile-creating-agendas t
))
695 (unless (file-writable-p file1
)
696 (error "Cannot write to file %s" file1
))
698 (org-store-agenda-views))
699 (when org-mobile-use-encryption
700 (org-mobile-encrypt-and-move file1 file
)
702 (org-mobile-cleanup-encryption-tempfile))))
704 (defun org-mobile-encrypt-and-move (infile outfile
)
705 "Encrypt INFILE locally to INFILE_enc, then move it to OUTFILE.
706 We do this in two steps so that remote paths will work, even if the
707 encryption program does not understand them."
708 (let ((encfile (concat infile
"_enc")))
709 (org-mobile-encrypt-file infile encfile
)
711 (copy-file encfile outfile
'ok-if-exists
)
712 (delete-file encfile
))))
714 (defun org-mobile-encrypt-file (infile outfile
)
715 "Encrypt INFILE to OUTFILE, using `org-mobile-encryption-password'."
717 (format "openssl enc -aes-256-cbc -salt -pass %s -in %s -out %s"
718 (shell-quote-argument (concat "pass:"
719 (org-mobile-encryption-password)))
720 (shell-quote-argument (expand-file-name infile
))
721 (shell-quote-argument (expand-file-name outfile
)))))
723 (defun org-mobile-decrypt-file (infile outfile
)
724 "Decrypt INFILE to OUTFILE, using `org-mobile-encryption-password'."
726 (format "openssl enc -d -aes-256-cbc -salt -pass %s -in %s -out %s"
727 (shell-quote-argument (concat "pass:"
728 (org-mobile-encryption-password)))
729 (shell-quote-argument (expand-file-name infile
))
730 (shell-quote-argument (expand-file-name outfile
)))))
732 (defun org-mobile-cleanup-encryption-tempfile ()
733 "Remove the encryption tempfile if it exists."
734 (and (stringp org-mobile-encryption-tempfile
)
735 (file-exists-p org-mobile-encryption-tempfile
)
736 (delete-file org-mobile-encryption-tempfile
)))
738 (defun org-mobile-move-capture ()
739 "Move the contents of the capture file to the inbox file.
740 Return a marker to the location where the new content has been added.
741 If nothing new has been added, return nil."
744 (capture-file (expand-file-name org-mobile-capture-file
745 org-mobile-directory
))
746 (inbox-buffer (find-file-noselect org-mobile-inbox-for-pull
))
748 (if (not org-mobile-use-encryption
)
749 (find-file-noselect capture-file
)
750 (org-mobile-cleanup-encryption-tempfile)
751 (setq encfile
(concat org-mobile-encryption-tempfile
"_enc"))
752 (copy-file capture-file encfile
)
753 (org-mobile-decrypt-file encfile org-mobile-encryption-tempfile
)
754 (find-file-noselect org-mobile-encryption-tempfile
)))
755 (insertion-point (make-marker))
757 (with-current-buffer capture-buffer
758 (setq content
(buffer-string))
759 (setq not-empty
(string-match "\\S-" content
))
761 (set-buffer inbox-buffer
)
763 (goto-char (point-max))
764 (or (bolp) (newline))
765 (move-marker insertion-point
766 (prog1 (point) (insert content
)))
768 (set-buffer capture-buffer
)
771 (org-mobile-update-checksum-for-capture-file (buffer-string))))
772 (kill-buffer capture-buffer
)
773 (when org-mobile-use-encryption
774 (org-mobile-encrypt-and-move org-mobile-encryption-tempfile
776 (org-mobile-cleanup-encryption-tempfile))
777 (if not-empty insertion-point
)))
779 (defun org-mobile-update-checksum-for-capture-file (buffer-string)
780 "Find the checksum line and modify it to match BUFFER-STRING."
781 (let* ((file (expand-file-name "checksums.dat" org-mobile-directory
))
782 (buffer (find-file-noselect file
)))
784 (with-current-buffer buffer
785 (when (re-search-forward (concat "\\([0-9a-fA-F]\\{30,\\}\\).*?"
786 (regexp-quote org-mobile-capture-file
)
788 (goto-char (match-beginning 1))
789 (delete-region (match-beginning 1) (match-end 1))
790 (insert (md5 buffer-string
))
792 (kill-buffer buffer
))))
794 (defun org-mobile-apply (&optional beg end
)
795 "Apply all change requests in the current buffer.
796 If BEG and END are given, only do this in that region."
798 (require 'org-archive
)
799 (setq org-mobile-last-flagged-files nil
)
800 (setq beg
(or beg
(point-min)) end
(or end
(point-max)))
802 ;; Remove all Note IDs
804 (while (re-search-forward "^\\*\\* Note ID: [-0-9A-F]+[ \t]*\n" end t
)
807 ;; Find all the referenced entries, without making any changes yet
808 (let ((marker (make-marker))
809 (bos-marker (make-marker))
810 (end (move-marker (make-marker) end
))
816 id-pos org-mobile-error
)
818 ;; Count the new captures
820 (while (re-search-forward "^\\* \\(.*\\)" end t
)
821 (and (>= (- (match-end 1) (match-beginning 1)) 2)
822 (not (equal (downcase (substring (match-string 1) 0 2)) "f("))
826 (while (re-search-forward
827 "^\\*+[ \t]+F(\\([^():\n]*\\)\\(:\\([^()\n]*\\)\\)?)[ \t]+\\[\\[\\(\\(id\\|olp\\):\\([^]\n]+\\)\\)" end t
)
828 (setq id-pos
(condition-case msg
829 (org-mobile-locate-entry (match-string 4))
830 (error (nth 1 msg
))))
831 (when (and (markerp id-pos
)
832 (not (member (marker-buffer id-pos
) buf-list
)))
833 (org-mobile-timestamp-buffer (marker-buffer id-pos
))
834 (push (marker-buffer id-pos
) buf-list
))
836 (if (or (not id-pos
) (stringp id-pos
))
838 (goto-char (+ 2 (point-at-bol)))
841 (add-text-properties (point-at-bol) (point-at-eol)
842 (list 'org-mobile-marker
843 (or id-pos
"Linked entry not found")))))
845 ;; OK, now go back and start applying
847 (while (re-search-forward "^\\*+[ \t]+F(\\([^():\n]*\\)\\(:\\([^()\n]*\\)\\)?)" end t
)
849 (setq id-pos
(get-text-property (point-at-bol) 'org-mobile-marker
))
850 (if (not (markerp id-pos
))
853 (insert "UNKNOWN PROBLEM"))
854 (let* ((action (match-string 1))
855 (data (and (match-end 3) (match-string 3)))
857 (eos (save-excursion (org-end-of-subtree t t
)))
858 (cmd (if (equal action
"")
861 (org-toggle-tag "FLAGGED" 'on
)
863 (org-entry-put nil
"THEFLAGGINGNOTE" note
)))
865 (cdr (assoc action org-mobile-action-alist
))))
866 (note (and (equal action
"")
867 (buffer-substring (1+ (point-at-eol)) eos
)))
868 (org-inhibit-logging 'note
) ;; Do not take notes interactively
871 (move-marker bos-marker
(point))
872 (if (re-search-forward "^** Old value[ \t]*$" eos t
)
873 (setq old
(buffer-substring
875 (progn (outline-next-heading) (point)))))
876 (if (re-search-forward "^** New value[ \t]*$" eos t
)
877 (setq new
(buffer-substring
879 (progn (outline-next-heading)
880 (if (eobp) (org-back-over-empty-lines))
882 (setq old
(and old
(if (string-match "\\S-" old
) old nil
)))
883 (setq new
(and new
(if (string-match "\\S-" new
) new nil
)))
884 (if (and note
(> (length note
) 0))
885 ;; Make Note into a single line, to fit into a property
886 (setq note
(mapconcat 'identity
887 (org-split-string (org-trim note
) "\n")
889 (unless (equal data
"body")
890 (setq new
(and new
(org-trim new
))
891 old
(and old
(org-trim old
))))
892 (goto-char (+ 2 bos-marker
))
893 (unless (markerp id-pos
)
894 (insert "BAD REFERENCE ")
901 ;; Remember this place so that we can return
902 (move-marker marker
(point))
903 (setq org-mobile-error nil
)
906 (org-with-point-at id-pos
909 (if (member "FLAGGED" (org-get-tags))
910 (add-to-list 'org-mobile-last-flagged-files
911 (buffer-file-name (current-buffer))))))
912 (error (setq org-mobile-error msg
))))
913 (when org-mobile-error
914 (org-pop-to-buffer-same-window (marker-buffer marker
))
917 (insert (if (stringp (nth 1 org-mobile-error
))
918 (nth 1 org-mobile-error
)
922 ;; If we get here, the action has been applied successfully
923 ;; So remove the entry
924 (goto-char bos-marker
)
925 (delete-region (point) (org-end-of-subtree t t
))))))
927 (move-marker marker nil
)
928 (move-marker end nil
)
929 (message "%d new, %d edits, %d flags, %d errors" cnt-new
930 cnt-edit cnt-flag cnt-error
)
933 (defun org-mobile-timestamp-buffer (buf)
934 "Time stamp buffer BUF, just to make sure its checksum will change."
935 (with-current-buffer buf
939 (goto-char (point-min))
940 (if (re-search-forward
941 "^\\([ \t]*\\)#\\+LAST_MOBILE_CHANGE:.*\n?" nil t
)
943 (goto-char (match-end 1))
944 (delete-region (point) (match-end 0)))
945 (if (looking-at ".*?-\\*-.*-\\*-")
947 (insert "#+LAST_MOBILE_CHANGE: "
948 (format-time-string "%Y-%m-%d %T") "\n")))))
950 (defun org-mobile-smart-read ()
951 "Parse the entry at point for shortcuts and expand them.
952 These shortcuts are meant for fast and easy typing on the limited
953 keyboards of a mobile device. Below we show a list of the shortcuts
954 currently implemented.
956 The entry is expected to contain an inactive time stamp indicating when
957 the entry was created. When setting dates and
958 times (for example for deadlines), the time strings are interpreted
959 relative to that creation date.
960 Abbreviations are expected to take up entire lines, just because it is so
961 easy to type RET on a mobile device. Abbreviations start with one or two
962 letters, followed immediately by a dot and then additional information.
963 Generally the entire shortcut line is removed after action have been taken.
964 Time stamps will be constructed using `org-read-date'. So for example a
965 line \"dd. 2tue\" will set a deadline on the second Tuesday after the
968 Here are the shortcuts currently implemented:
970 dd. string set deadline
971 ss. string set scheduling
972 tt. string set time tamp, here.
973 ti. string set inactive time
975 tg. tag1 tag2 tag3 set all these tags, change case where necessary
976 td. kwd set this todo keyword, change case where necessary
978 FIXME: Hmmm, not sure if we can make his work against the
979 auto-correction feature. Needs a bit more thinking. So this function
980 is currently a noop.")
982 (defun org-mobile-locate-entry (link)
983 (if (string-match "\\`id:\\(.*\\)$" link
)
984 (org-id-find (match-string 1 link
) 'marker
)
985 (if (not (string-match "\\`olp:\\(.*?\\):\\(.*\\)$" link
))
987 (let ((file (match-string 1 link
))
988 (path (match-string 2 link
)))
989 (setq file
(org-link-unescape file
))
990 (setq file
(expand-file-name file org-directory
))
991 (setq path
(mapcar 'org-link-unescape
992 (org-split-string path
"/")))
993 (org-find-olp (cons file path
))))))
995 (defun org-mobile-edit (what old new
)
996 "Edit item WHAT in the current entry by replacing OLD with NEW.
997 WHAT can be \"heading\", \"todo\", \"tags\", \"priority\", or \"body\".
998 The edit only takes place if the current value is equal (except for
999 white space) the OLD. If this is so, OLD will be replace by NEW
1000 and the command will return t. If something goes wrong, a string will
1001 be returned that indicates what went wrong."
1002 (let (current old1 new1
)
1003 (if (stringp what
) (setq what
(intern what
)))
1007 ((memq what
'(todo todostate
))
1008 (setq current
(org-get-todo-state))
1010 ((equal new
"DONEARCHIVE")
1012 (org-archive-subtree-default))
1013 ((equal new current
) t
) ; nothing needs to be done
1014 ((or (equal current old
)
1015 (eq org-mobile-force-mobile-change t
)
1016 (memq 'todo org-mobile-force-mobile-change
))
1017 (org-todo (or new
'none
)) t
)
1018 (t (error "State before change was expected as \"%s\", but is \"%s\""
1022 (setq current
(org-get-tags)
1023 new1
(and new
(org-split-string new
":+"))
1024 old1
(and old
(org-split-string old
":+")))
1026 ((org-mobile-tags-same-p current new1
) t
) ; no change needed
1027 ((or (org-mobile-tags-same-p current old1
)
1028 (eq org-mobile-force-mobile-change t
)
1029 (memq 'tags org-mobile-force-mobile-change
))
1030 (org-set-tags-to new1
) t
)
1031 (t (error "Tags before change were expected as \"%s\", but are \"%s\""
1032 (or old
"") (or current
"")))))
1034 ((eq what
'priority
)
1035 (when (looking-at org-complex-heading-regexp
)
1036 (setq current
(and (match-end 3) (substring (match-string 3) 2 3)))
1038 ((equal current new
) t
) ; no action required
1039 ((or (equal current old
)
1040 (eq org-mobile-force-mobile-change t
)
1041 (memq 'tags org-mobile-force-mobile-change
))
1042 (org-priority (and new
(string-to-char new
))))
1043 (t (error "Priority was expected to be %s, but is %s"
1047 (when (looking-at org-complex-heading-regexp
)
1048 (setq current
(match-string 4))
1050 ((equal current new
) t
) ; no action required
1051 ((or (equal current old
)
1052 (eq org-mobile-force-mobile-change t
)
1053 (memq 'heading org-mobile-force-mobile-change
))
1054 (goto-char (match-beginning 4))
1056 (delete-region (point) (+ (point) (length current
)))
1057 (org-set-tags nil
'align
))
1058 (t (error "Heading changed in MobileOrg and on the computer")))))
1061 (setq current
(buffer-substring (min (1+ (point-at-eol)) (point-max))
1062 (save-excursion (outline-next-heading)
1064 (if (not (string-match "\\S-" current
)) (setq current nil
))
1066 ((org-mobile-bodies-same-p current new
) t
) ; no action necessary
1067 ((or (org-mobile-bodies-same-p current old
)
1068 (eq org-mobile-force-mobile-change t
)
1069 (memq 'body org-mobile-force-mobile-change
))
1073 (or (bolp) (insert "\n"))
1074 (delete-region (point) (progn (org-back-to-heading t
)
1075 (outline-next-heading)
1078 (t (error "Body was changed in MobileOrg and on the computer")))))))
1080 (defun org-mobile-tags-same-p (list1 list2
)
1081 "Are the two tag lists the same?"
1082 (not (or (org-delete-all list1 list2
)
1083 (org-delete-all list2 list1
))))
1085 (defun org-mobile-bodies-same-p (a b
)
1086 "Compare if A and B are visually equal strings.
1087 We first remove leading and trailing white space from the entire strings.
1088 Then we split the strings into lines and remove leading/trailing whitespace
1089 from each line. Then we compare.
1090 A and B must be strings or nil."
1092 ((and (not a
) (not b
)) t
)
1093 ((or (not a
) (not b
)) nil
)
1094 (t (setq a
(org-trim a
) b
(org-trim b
))
1095 (setq a
(mapconcat 'identity
(org-split-string a
"[ \t]*\n[ \t]*") "\n"))
1096 (setq b
(mapconcat 'identity
(org-split-string b
"[ \t]*\n[ \t]*") "\n"))
1099 (provide 'org-mobile
)
1101 ;;; org-mobile.el ends here