Add documentation for org-crypt.el
[org-mode.git] / lisp / org-mobile.el
blob7b1bcbe8624ca28dedf3e3ecc31beb4f550ac05d
1 ;;; org-mobile.el --- Code for asymmetric sync with a mobile device
2 ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
3 ;;
4 ;; Author: Carsten Dominik <carsten at orgmode dot org>
5 ;; Keywords: outlines, hypermedia, calendar, wp
6 ;; Homepage: http://orgmode.org
7 ;; Version: 7.5
8 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;;; Commentary:
28 ;; This file contains the code to interact with Richard Moreland's iPhone
29 ;; application MobileOrg, as well as with the Android version by Matthew Jones.
30 ;; This code is documented in Appendix B of the Org-mode manual. The code is
31 ;; not specific for the iPhone and Android - any external
32 ;; viewer/flagging/editing application that uses the same conventions could
33 ;; be used.
35 (require 'org)
36 (require 'org-agenda)
37 ;;; Code:
39 (eval-when-compile (require 'cl))
41 (defgroup org-mobile nil
42 "Options concerning support for a viewer/editor on a mobile device."
43 :tag "Org Mobile"
44 :group 'org)
46 (defcustom org-mobile-files '(org-agenda-files)
47 "Files to be staged for MobileOrg.
48 This is basically a list of files and directories. Files will be staged
49 directly. Directories will be search for files with the extension `.org'.
50 In addition to this, the list may also contain the following symbols:
52 org-agenda-files
53 This means include the complete, unrestricted list of files given in
54 the variable `org-agenda-files'.
55 org-agenda-text-search-extra-files
56 Include the files given in the variable
57 `org-agenda-text-search-extra-files'"
58 :group 'org-mobile
59 :type '(list :greedy t
60 (option (const :tag "org-agenda-files" org-agenda-files))
61 (option (const :tag "org-agenda-text-search-extra-files"
62 org-agenda-text-search-extra-files))
63 (repeat :inline t :tag "Additional files"
64 (file))))
66 (defcustom org-mobile-files-exclude-regexp ""
67 "A regexp to exclude files from `org-mobile-files'."
68 :group 'org-mobile
69 :type 'regexp)
71 (defcustom org-mobile-directory ""
72 "The WebDAV directory where the interaction with the mobile takes place."
73 :group 'org-mobile
74 :type 'directory)
76 (defcustom org-mobile-use-encryption nil
77 "Non-nil means keep only encrypted files on the WebDAV server.
78 Encryption uses AES-256, with a password given in
79 `org-mobile-encryption-password'.
80 When nil, plain files are kept on the server.
81 Turning on encryption requires to set the same password in the MobileOrg
82 application. Before turning this on, check of MobileOrg does already
83 support it - at the time of this writing it did not yet."
84 :group 'org-mobile
85 :type 'boolean)
87 (defcustom org-mobile-encryption-tempfile "~/orgtmpcrypt"
88 "File that is being used as a temporary file for encryption.
89 This must be local file on your local machine (not on the WebDAV server).
90 You might want to put this file into a directory where only you have access."
91 :group 'org-mobile
92 :type 'directory)
94 (defcustom org-mobile-encryption-password ""
95 "Password for encrypting files uploaded to the server.
96 This is a single password which is used for AES-256 encryption. The same
97 password must also be set in the MobileOrg application. All Org files,
98 including mobileorg.org will be encrypted using this password.
100 SECURITY CONSIDERATIONS:
102 Note that, when Org runs the encryption commands, the password could
103 be visible briefly on your system with the `ps' command. So this method is
104 only intended to keep the files secure on the server, not on your own machine.
106 Also, if you set this variable in an init file (.emacs or .emacs.d/init.el
107 or custom.el...) and if that file is stored in a way so that other can read
108 it, this also limits the security of this approach. You can also leave
109 this variable empty - Org will then ask for the password once per Emacs
110 session."
111 :group 'org-mobile
112 :type '(string :tag "Password"))
114 (defvar org-mobile-encryption-password-session nil)
116 (defun org-mobile-encryption-password ()
117 (or (org-string-nw-p org-mobile-encryption-password)
118 (org-string-nw-p org-mobile-encryption-password-session)
119 (setq org-mobile-encryption-password-session
120 (read-passwd "Password for MobileOrg: " t))))
122 (defcustom org-mobile-inbox-for-pull "~/org/from-mobile.org"
123 "The file where captured notes and flags will be appended to.
124 During the execution of `org-mobile-pull', the file
125 `org-mobile-capture-file' will be emptied it's contents have
126 been appended to the file given here. This file should be in
127 `org-directory', and not in the staging area or on the web server."
128 :group 'org-mobile
129 :type 'file)
131 (defconst org-mobile-capture-file "mobileorg.org"
132 "The capture file where the mobile stores captured notes and flags.
133 This should not be changed, because MobileOrg assumes this name.")
135 (defcustom org-mobile-index-file "index.org"
136 "The index file with links to all Org files that should be loaded by MobileOrg.
137 Relative to `org-mobile-directory'. The Address field in the MobileOrg setup
138 should point to this file."
139 :group 'org-mobile
140 :type 'file)
142 (defcustom org-mobile-agendas 'all
143 "The agendas that should be pushed to MobileOrg.
144 Allowed values:
146 default the weekly agenda and the global TODO list
147 custom all custom agendas defined by the user
148 all the custom agendas and the default ones
149 list a list of selection key(s) as string."
150 :group 'org-mobile
151 :type '(choice
152 (const :tag "Default Agendas" default)
153 (const :tag "Custom Agendas" custom)
154 (const :tag "Default and Custom Agendas" all)
155 (repeat :tag "Selected"
156 (string :tag "Selection Keys"))))
158 (defcustom org-mobile-force-id-on-agenda-items t
159 "Non-nil means make all agenda items carry an ID."
160 :group 'org-mobile
161 :type 'boolean)
163 (defcustom org-mobile-force-mobile-change nil
164 "Non-nil means force the change made on the mobile device.
165 So even if there have been changes to the computer version of the entry,
166 force the new value set on the mobile.
167 When nil, mark the entry from the mobile with an error message.
168 Instead of nil or t, this variable can also be a list of symbols, indicating
169 the editing types for which the mobile version should always dominate."
170 :group 'org-mobile
171 :type '(choice
172 (const :tag "Always" t)
173 (const :tag "Never" nil)
174 (set :greedy t :tag "Specify"
175 (const todo)
176 (const tags)
177 (const priority)
178 (const heading)
179 (const body))))
181 (defcustom org-mobile-action-alist
182 '(("edit" . (org-mobile-edit data old new)))
183 "Alist with flags and actions for mobile sync.
184 When flagging an entry, MobileOrg will create entries that look like
186 * F(action:data) [[id:entry-id][entry title]]
188 This alist defines that the ACTION in the parentheses of F() should mean,
189 i.e. what action should be taken. The :data part in the parenthesis is
190 optional. If present, the string after the colon will be passed to the
191 action form as the `data' variable.
192 The car of each elements of the alist is an actions string. The cdr is
193 an Emacs Lisp form that will be evaluated with the cursor on the headline
194 of that entry.
196 For now, it is not recommended to change this variable."
197 :group 'org-mobile
198 :type '(repeat
199 (cons (string :tag "Action flag")
200 (sexp :tag "Action form"))))
202 (defcustom org-mobile-checksum-binary (or (executable-find "shasum")
203 (executable-find "sha1sum")
204 (executable-find "md5sum")
205 (executable-find "md5"))
206 "Executable used for computing checksums of agenda files."
207 :group 'org-mobile
208 :type 'string)
210 (defvar org-mobile-pre-push-hook nil
211 "Hook run before running `org-mobile-push'.
212 This could be used to clean up `org-mobile-directory', for example to
213 remove files that used to be included in the agenda but no longer are.
214 The presence of such files would not really be a problem, but after time
215 they may accumulate.")
217 (defvar org-mobile-post-push-hook nil
218 "Hook run after running `org-mobile-push'.
219 If Emacs does not have direct write access to the WebDAV directory used
220 by the mobile device, this hook should be used to copy all files from the
221 local staging directory `org-mobile-directory' to the WebDAV directory,
222 for example using `rsync' or `scp'.")
224 (defvar org-mobile-pre-pull-hook nil
225 "Hook run before executing `org-mobile-pull'.
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 the capture file
228 `mobileorg.org' from the WebDAV location to the local staging
229 directory `org-mobile-directory'.")
231 (defvar org-mobile-post-pull-hook nil
232 "Hook run after running `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 emptied
235 capture file `mobileorg.org' back to the WebDAV directory, for example
236 using `rsync' or `scp'.")
238 (defvar org-mobile-last-flagged-files nil
239 "List of files containing entries flagged in the latest pull.")
241 (defvar org-mobile-files-alist nil)
242 (defvar org-mobile-checksum-files nil)
244 (defun org-mobile-prepare-file-lists ()
245 (setq org-mobile-files-alist (org-mobile-files-alist))
246 (setq org-mobile-checksum-files nil))
248 (defun org-mobile-files-alist ()
249 "Expand the list in `org-mobile-files' to a list of existing files.
250 Also exclude files matching `org-mobile-files-exclude-regexp'."
251 (let* ((include-archives
252 (and (member 'org-agenda-text-search-extra-files org-mobile-files)
253 (member 'agenda-archives org-agenda-text-search-extra-files)
255 (files
256 (apply 'append
257 (mapcar
258 (lambda (f)
259 (cond
260 ((eq f 'org-agenda-files)
261 (org-agenda-files t include-archives))
262 ((eq f 'org-agenda-text-search-extra-files)
263 (delq 'agenda-archives
264 (copy-sequence
265 org-agenda-text-search-extra-files)))
266 ((and (stringp f) (file-directory-p f))
267 (directory-files f 'full "\\.org\\'"))
268 ((and (stringp f) (file-exists-p f))
269 (list f))
270 (t nil)))
271 org-mobile-files)))
272 (files (delete
273 nil
274 (mapcar (lambda (f)
275 (unless (and (not (string= org-mobile-files-exclude-regexp ""))
276 (string-match org-mobile-files-exclude-regexp f))
277 (identity f)))
278 files)))
279 (orgdir-uname (file-name-as-directory (file-truename org-directory)))
280 (orgdir-re (concat "\\`" (regexp-quote orgdir-uname)))
281 uname seen rtn file link-name)
282 ;; Make the files unique, and determine the name under which they will
283 ;; be listed.
284 (while (setq file (pop files))
285 (if (not (file-name-absolute-p file))
286 (setq file (expand-file-name file org-directory)))
287 (setq uname (file-truename file))
288 (unless (member uname seen)
289 (push uname seen)
290 (if (string-match orgdir-re uname)
291 (setq link-name (substring uname (match-end 0)))
292 (setq link-name (file-name-nondirectory uname)))
293 (push (cons file link-name) rtn)))
294 (nreverse rtn)))
296 ;;;###autoload
297 (defun org-mobile-push ()
298 "Push the current state of Org affairs to the WebDAV directory.
299 This will create the index file, copy all agenda files there, and also
300 create all custom agenda views, for upload to the mobile phone."
301 (interactive)
302 (let ((a-buffer (get-buffer org-agenda-buffer-name)))
303 (let ((org-agenda-buffer-name "*SUMO*")
304 (org-agenda-filter org-agenda-filter)
305 (org-agenda-redo-command org-agenda-redo-command))
306 (save-excursion
307 (save-window-excursion
308 (run-hooks 'org-mobile-pre-push-hook)
309 (org-mobile-check-setup)
310 (org-mobile-prepare-file-lists)
311 (message "Creating agendas...")
312 (let ((inhibit-redisplay t)) (org-mobile-create-sumo-agenda))
313 (message "Creating agendas...done")
314 (org-save-all-org-buffers) ; to save any IDs created by this process
315 (message "Copying files...")
316 (org-mobile-copy-agenda-files)
317 (message "Writing index file...")
318 (org-mobile-create-index-file)
319 (message "Writing checksums...")
320 (org-mobile-write-checksums)
321 (run-hooks 'org-mobile-post-push-hook))))
322 (redraw-display)
323 (when (and a-buffer (buffer-live-p a-buffer))
324 (if (not (get-buffer-window a-buffer))
325 (kill-buffer a-buffer)
326 (let ((cw (selected-window)))
327 (select-window (get-buffer-window a-buffer))
328 (org-agenda-redo)
329 (select-window cw)))))
330 (message "Files for mobile viewer staged"))
332 (defvar org-mobile-before-process-capture-hook nil
333 "Hook that is run after content was moved to `org-mobile-inbox-for-pull'.
334 The inbox file is visited by the current buffer, and the buffer is
335 narrowed to the newly captured data.")
337 ;;;###autoload
338 (defun org-mobile-pull ()
339 "Pull the contents of `org-mobile-capture-file' and integrate them.
340 Apply all flagged actions, flag entries to be flagged and then call an
341 agenda view showing the flagged items."
342 (interactive)
343 (org-mobile-check-setup)
344 (run-hooks 'org-mobile-pre-pull-hook)
345 (let ((insertion-marker (org-mobile-move-capture)))
346 (if (not (markerp insertion-marker))
347 (message "No new items")
348 (org-with-point-at insertion-marker
349 (save-restriction
350 (narrow-to-region (point) (point-max))
351 (run-hooks 'org-mobile-before-process-capture-hook)))
352 (org-with-point-at insertion-marker
353 (org-mobile-apply (point) (point-max)))
354 (move-marker insertion-marker nil)
355 (run-hooks 'org-mobile-post-pull-hook)
356 (when org-mobile-last-flagged-files
357 ;; Make an agenda view of flagged entries, but only in the files
358 ;; where stuff has been added.
359 (put 'org-agenda-files 'org-restrict org-mobile-last-flagged-files)
360 (let ((org-agenda-keep-restricted-file-list t))
361 (org-agenda nil "?"))))))
363 (defun org-mobile-check-setup ()
364 "Check if org-mobile-directory has been set up."
365 (org-mobile-cleanup-encryption-tempfile)
366 (unless (and org-directory
367 (stringp org-directory)
368 (string-match "\\S-" org-directory)
369 (file-exists-p org-directory)
370 (file-directory-p org-directory))
371 (error
372 "Please set `org-directory' to the directory where your org files live"))
373 (unless (and org-mobile-directory
374 (stringp org-mobile-directory)
375 (string-match "\\S-" org-mobile-directory)
376 (file-exists-p org-mobile-directory)
377 (file-directory-p org-mobile-directory))
378 (error
379 "Variable `org-mobile-directory' must point to an existing directory"))
380 (unless (and org-mobile-inbox-for-pull
381 (stringp org-mobile-inbox-for-pull)
382 (string-match "\\S-" org-mobile-inbox-for-pull)
383 (file-exists-p
384 (file-name-directory org-mobile-inbox-for-pull)))
385 (error
386 "Variable `org-mobile-inbox-for-pull' must point to a file in an existing directory"))
387 (unless (and org-mobile-checksum-binary
388 (string-match "\\S-" org-mobile-checksum-binary))
389 (error "No executable found to compute checksums"))
390 (when org-mobile-use-encryption
391 (unless (string-match "\\S-" (org-mobile-encryption-password))
392 (error
393 "To use encryption, you must set `org-mobile-encryption-password'"))
394 (unless (file-writable-p org-mobile-encryption-tempfile)
395 (error "Cannot write to encryption tempfile %s"
396 org-mobile-encryption-tempfile))
397 (unless (executable-find "openssl")
398 (error "openssl is needed to encrypt files"))))
400 (defun org-mobile-create-index-file ()
401 "Write the index file in the WebDAV directory."
402 (let ((files-alist (sort (copy-sequence org-mobile-files-alist)
403 (lambda (a b) (string< (cdr a) (cdr b)))))
404 (def-todo (default-value 'org-todo-keywords))
405 (def-tags (default-value 'org-tag-alist))
406 (target-file (expand-file-name org-mobile-index-file
407 org-mobile-directory))
408 file link-name todo-kwds done-kwds tags drawers entry kwds dwds twds)
410 (org-prepare-agenda-buffers (mapcar 'car files-alist))
411 (setq done-kwds (org-uniquify org-done-keywords-for-agenda))
412 (setq todo-kwds (org-delete-all
413 done-kwds
414 (org-uniquify org-todo-keywords-for-agenda)))
415 (setq drawers (org-uniquify org-drawers-for-agenda))
416 (setq tags (org-uniquify
417 (delq nil
418 (mapcar
419 (lambda (e)
420 (cond ((stringp e) e)
421 ((listp e)
422 (if (stringp (car e)) (car e) nil))
423 (t nil)))
424 org-tag-alist-for-agenda))))
425 (with-temp-file
426 (if org-mobile-use-encryption
427 org-mobile-encryption-tempfile
428 target-file)
429 (while (setq entry (pop def-todo))
430 (insert "#+READONLY\n")
431 (setq kwds (mapcar (lambda (x) (if (string-match "(" x)
432 (substring x 0 (match-beginning 0))
434 (cdr entry)))
435 (insert "#+TODO: " (mapconcat 'identity kwds " ") "\n")
436 (setq dwds (member "|" kwds)
437 twds (org-delete-all dwds kwds)
438 todo-kwds (org-delete-all twds todo-kwds)
439 done-kwds (org-delete-all dwds done-kwds)))
440 (when (or todo-kwds done-kwds)
441 (insert "#+TODO: " (mapconcat 'identity todo-kwds " ") " | "
442 (mapconcat 'identity done-kwds " ") "\n"))
443 (setq def-tags (mapcar
444 (lambda (x)
445 (cond ((null x) nil)
446 ((stringp x) x)
447 ((eq (car x) :startgroup) "{")
448 ((eq (car x) :endgroup) "}")
449 ((eq (car x) :newline) nil)
450 ((listp x) (car x))
451 (t nil)))
452 def-tags))
453 (setq def-tags (delq nil def-tags))
454 (setq tags (org-delete-all def-tags tags))
455 (setq tags (sort tags (lambda (a b) (string< (downcase a) (downcase b)))))
456 (setq tags (append def-tags tags nil))
457 (insert "#+TAGS: " (mapconcat 'identity tags " ") "\n")
458 (insert "#+DRAWERS: " (mapconcat 'identity drawers " ") "\n")
459 (insert "#+ALLPRIORITIES: A B C" "\n")
460 (when (file-exists-p (expand-file-name
461 org-mobile-directory "agendas.org"))
462 (insert "* [[file:agendas.org][Agenda Views]]\n"))
463 (while (setq entry (pop files-alist))
464 (setq file (car entry)
465 link-name (cdr entry))
466 (insert (format "* [[file:%s][%s]]\n"
467 link-name link-name)))
468 (push (cons org-mobile-index-file (md5 (buffer-string)))
469 org-mobile-checksum-files))
470 (when org-mobile-use-encryption
471 (org-mobile-encrypt-and-move org-mobile-encryption-tempfile
472 target-file)
473 (org-mobile-cleanup-encryption-tempfile))))
475 (defun org-mobile-copy-agenda-files ()
476 "Copy all agenda files to the stage or WebDAV directory."
477 (let ((files-alist org-mobile-files-alist)
478 file buf entry link-name target-path target-dir check)
479 (while (setq entry (pop files-alist))
480 (setq file (car entry) link-name (cdr entry))
481 (when (file-exists-p file)
482 (setq target-path (expand-file-name link-name org-mobile-directory)
483 target-dir (file-name-directory target-path))
484 (unless (file-directory-p target-dir)
485 (make-directory target-dir 'parents))
486 (if org-mobile-use-encryption
487 (org-mobile-encrypt-and-move file target-path)
488 (copy-file file target-path 'ok-if-exists))
489 (setq check (shell-command-to-string
490 (concat org-mobile-checksum-binary " "
491 (shell-quote-argument (expand-file-name file)))))
492 (when (string-match "[a-fA-F0-9]\\{30,40\\}" check)
493 (push (cons link-name (match-string 0 check))
494 org-mobile-checksum-files))))
496 (setq file (expand-file-name org-mobile-capture-file
497 org-mobile-directory))
498 (save-excursion
499 (setq buf (find-file file))
500 (when (and (= (point-min) (point-max)))
501 (insert "\n")
502 (save-buffer)
503 (when org-mobile-use-encryption
504 (write-file org-mobile-encryption-tempfile)
505 (org-mobile-encrypt-and-move org-mobile-encryption-tempfile file)))
506 (push (cons org-mobile-capture-file (md5 (buffer-string)))
507 org-mobile-checksum-files))
508 (org-mobile-cleanup-encryption-tempfile)
509 (kill-buffer buf)))
511 (defun org-mobile-write-checksums ()
512 "Create checksums for all files in `org-mobile-directory'.
513 The table of checksums is written to the file mobile-checksums."
514 (let ((sumfile (expand-file-name "checksums.dat" org-mobile-directory))
515 (files org-mobile-checksum-files)
516 entry file sum)
517 (with-temp-file sumfile
518 (set-buffer-file-coding-system 'undecided-unix nil)
519 (while (setq entry (pop files))
520 (setq file (car entry) sum (cdr entry))
521 (insert (format "%s %s\n" sum file))))))
523 (defun org-mobile-sumo-agenda-command ()
524 "Return an agenda custom command that comprises all custom commands."
525 (let ((custom-list
526 ;; normalize different versions
527 (delq nil
528 (mapcar
529 (lambda (x)
530 (cond ((stringp (cdr x)) nil)
531 ((stringp (nth 1 x)) x)
532 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
533 (t (cons (car x) (cons "" (cdr x))))))
534 org-agenda-custom-commands)))
535 (default-list '(("a" "Agenda" agenda) ("t" "All TODO" alltodo)))
536 thelist new e key desc type match settings cmds gkey gdesc gsettings cnt)
537 (cond
538 ((eq org-mobile-agendas 'custom)
539 (setq thelist custom-list))
540 ((eq org-mobile-agendas 'default)
541 (setq thelist default-list))
542 ((eq org-mobile-agendas 'all)
543 (setq thelist custom-list)
544 (unless (assoc "t" thelist) (push '("t" "ALL TODO" alltodo) thelist))
545 (unless (assoc "a" thelist) (push '("a" "Agenda" agenda) thelist)))
546 ((listp org-mobile-agendas)
547 (setq thelist (append custom-list default-list))
548 (setq thelist (delq nil (mapcar (lambda (k) (assoc k thelist))
549 org-mobile-agendas)))))
550 (while (setq e (pop thelist))
551 (cond
552 ((stringp (cdr e))
553 ;; this is a description entry - skip it
555 ((eq (nth 2 e) 'search)
556 ;; Search view is interactive, skip
558 ((memq (nth 2 e) '(todo-tree tags-tree occur-tree))
559 ;; These are trees, not really agenda commands
561 ((and (memq (nth 2 e) '(todo tags tags-todo))
562 (or (null (nth 3 e))
563 (not (string-match "\\S-" (nth 3 e)))))
564 ;; These would be interactive because the match string is empty
566 ((memq (nth 2 e) '(agenda alltodo todo tags tags-todo))
567 ;; a normal command
568 (setq key (car e) desc (nth 1 e) type (nth 2 e) match (nth 3 e)
569 settings (nth 4 e))
570 (setq settings
571 (cons (list 'org-agenda-title-append
572 (concat "<after>KEYS=" key " TITLE: "
573 (if (and (stringp desc) (> (length desc) 0))
574 desc (symbol-name type))
575 " " match "</after>"))
576 settings))
577 (push (list type match settings) new))
578 ((symbolp (nth 2 e))
579 ;; A user-defined function, not sure how to handle that yet
582 ;; a block agenda
583 (setq gkey (car e) gdesc (nth 1 e) gsettings (nth 3 e) cmds (nth 2 e))
584 (setq cnt 0)
585 (while (setq e (pop cmds))
586 (setq type (car e) match (nth 1 e) settings (nth 2 e))
587 (setq settings (append gsettings settings))
588 (setq settings
589 (cons (list 'org-agenda-title-append
590 (concat "<after>KEYS=" gkey "#" (number-to-string
591 (setq cnt (1+ cnt)))
592 " TITLE: " gdesc " " match "</after>"))
593 settings))
594 (push (list type match settings) new)))))
595 (and new (list "X" "SUMO" (reverse new)
596 '((org-agenda-compact-blocks nil))))))
598 (defvar org-mobile-creating-agendas nil)
599 (defun org-mobile-write-agenda-for-mobile (file)
600 (let ((all (buffer-string)) in-date id pl prefix line app short m sexp)
601 (with-temp-file file
602 (org-mode)
603 (insert "#+READONLY\n")
604 (insert all)
605 (goto-char (point-min))
606 (while (not (eobp))
607 (cond
608 ((looking-at "[ \t]*$")) ; keep empty lines
609 ((looking-at "=+$")
610 ;; remove underlining
611 (delete-region (point) (point-at-eol)))
612 ((get-text-property (point) 'org-agenda-structural-header)
613 (setq in-date nil)
614 (setq app (get-text-property (point)
615 'org-agenda-title-append))
616 (setq short (get-text-property (point)
617 'short-heading))
618 (when (and short (looking-at ".+"))
619 (replace-match short)
620 (beginning-of-line 1))
621 (when app
622 (end-of-line 1)
623 (insert app)
624 (beginning-of-line 1))
625 (insert "* "))
626 ((get-text-property (point) 'org-agenda-date-header)
627 (setq in-date t)
628 (insert "** "))
629 ((setq m (or (get-text-property (point) 'org-hd-marker)
630 (get-text-property (point) 'org-marker)))
631 (setq sexp (member (get-text-property (point) 'type)
632 '("diary" "sexp")))
633 (if (setq pl (text-property-any (point) (point-at-eol) 'org-heading t))
634 (progn
635 (setq prefix (org-trim (buffer-substring
636 (point) pl))
637 line (org-trim (buffer-substring
639 (point-at-eol))))
640 (delete-region (point-at-bol) (point-at-eol))
641 (insert line "<before>" prefix "</before>")
642 (beginning-of-line 1))
643 (and (looking-at "[ \t]+") (replace-match "")))
644 (insert (if in-date "*** " "** "))
645 (end-of-line 1)
646 (insert "\n")
647 (unless sexp
648 (insert (org-agenda-get-some-entry-text
649 m 10 " " 'planning)
650 "\n")
651 (when (setq id
652 (if (org-bound-and-true-p
653 org-mobile-force-id-on-agenda-items)
654 (org-id-get m 'create)
655 (or (org-entry-get m "ID")
656 (org-mobile-get-outline-path-link m))))
657 (insert " :PROPERTIES:\n :ORIGINAL_ID: " id
658 "\n :END:\n")))))
659 (beginning-of-line 2))
660 (push (cons "agendas.org" (md5 (buffer-string)))
661 org-mobile-checksum-files))
662 (message "Agenda written to Org file %s" file)))
664 (defun org-mobile-get-outline-path-link (pom)
665 (org-with-point-at pom
666 (concat "olp:"
667 (org-mobile-escape-olp (file-name-nondirectory buffer-file-name))
669 (mapconcat 'org-mobile-escape-olp
670 (org-get-outline-path)
671 "/")
673 (org-mobile-escape-olp (nth 4 (org-heading-components))))))
675 (defun org-mobile-escape-olp (s)
676 (let ((table '(?: ?/)))
677 (org-link-escape s table)))
679 ;;;###autoload
680 (defun org-mobile-create-sumo-agenda ()
681 "Create a file that contains all custom agenda views."
682 (interactive)
683 (let* ((file (expand-file-name "agendas.org"
684 org-mobile-directory))
685 (file1 (if org-mobile-use-encryption
686 org-mobile-encryption-tempfile
687 file))
688 (sumo (org-mobile-sumo-agenda-command))
689 (org-agenda-custom-commands
690 (list (append sumo (list (list file1)))))
691 (org-mobile-creating-agendas t))
692 (unless (file-writable-p file1)
693 (error "Cannot write to file %s" file1))
694 (when sumo
695 (org-store-agenda-views))
696 (when org-mobile-use-encryption
697 (org-mobile-encrypt-and-move file1 file)
698 (delete-file file1)
699 (org-mobile-cleanup-encryption-tempfile))))
701 (defun org-mobile-encrypt-and-move (infile outfile)
702 "Encrypt INFILE locally to INFILE_enc, then move it to OUTFILE.
703 We do this in two steps so that remote paths will work, even if the
704 encryption program does not understand them."
705 (let ((encfile (concat infile "_enc")))
706 (org-mobile-encrypt-file infile encfile)
707 (when outfile
708 (copy-file encfile outfile 'ok-if-exists)
709 (delete-file encfile))))
711 (defun org-mobile-encrypt-file (infile outfile)
712 "Encrypt INFILE to OUTFILE, using `org-mobile-encryption-password'."
713 (shell-command
714 (format "openssl enc -aes-256-cbc -salt -pass %s -in %s -out %s"
715 (shell-quote-argument (concat "pass:"
716 (org-mobile-encryption-password)))
717 (shell-quote-argument (expand-file-name infile))
718 (shell-quote-argument (expand-file-name outfile)))))
720 (defun org-mobile-decrypt-file (infile outfile)
721 "Decrypt INFILE to OUTFILE, using `org-mobile-encryption-password'."
722 (shell-command
723 (format "openssl enc -d -aes-256-cbc -salt -pass %s -in %s -out %s"
724 (shell-quote-argument (concat "pass:"
725 (org-mobile-encryption-password)))
726 (shell-quote-argument (expand-file-name infile))
727 (shell-quote-argument (expand-file-name outfile)))))
729 (defun org-mobile-cleanup-encryption-tempfile ()
730 "Remove the encryption tempfile if it exists."
731 (and (stringp org-mobile-encryption-tempfile)
732 (file-exists-p org-mobile-encryption-tempfile)
733 (delete-file org-mobile-encryption-tempfile)))
735 (defun org-mobile-move-capture ()
736 "Move the contents of the capture file to the inbox file.
737 Return a marker to the location where the new content has been added.
738 If nothing new has been added, return nil."
739 (interactive)
740 (let* ((encfile nil)
741 (capture-file (expand-file-name org-mobile-capture-file
742 org-mobile-directory))
743 (inbox-buffer (find-file-noselect org-mobile-inbox-for-pull))
744 (capture-buffer
745 (if (not org-mobile-use-encryption)
746 (find-file-noselect capture-file)
747 (org-mobile-cleanup-encryption-tempfile)
748 (setq encfile (concat org-mobile-encryption-tempfile "_enc"))
749 (copy-file capture-file encfile)
750 (org-mobile-decrypt-file encfile org-mobile-encryption-tempfile)
751 (find-file-noselect org-mobile-encryption-tempfile)))
752 (insertion-point (make-marker))
753 not-empty content)
754 (with-current-buffer capture-buffer
755 (setq content (buffer-string))
756 (setq not-empty (string-match "\\S-" content))
757 (when not-empty
758 (set-buffer inbox-buffer)
759 (widen)
760 (goto-char (point-max))
761 (or (bolp) (newline))
762 (move-marker insertion-point
763 (prog1 (point) (insert content)))
764 (save-buffer)
765 (set-buffer capture-buffer)
766 (erase-buffer)
767 (save-buffer)
768 (org-mobile-update-checksum-for-capture-file (buffer-string))))
769 (kill-buffer capture-buffer)
770 (when org-mobile-use-encryption
771 (org-mobile-encrypt-and-move org-mobile-encryption-tempfile
772 capture-file)
773 (org-mobile-cleanup-encryption-tempfile))
774 (if not-empty insertion-point)))
776 (defun org-mobile-update-checksum-for-capture-file (buffer-string)
777 "Find the checksum line and modify it to match BUFFER-STRING."
778 (let* ((file (expand-file-name "checksums.dat" org-mobile-directory))
779 (buffer (find-file-noselect file)))
780 (when buffer
781 (with-current-buffer buffer
782 (when (re-search-forward (concat "\\([0-9a-fA-F]\\{30,\\}\\).*?"
783 (regexp-quote org-mobile-capture-file)
784 "[ \t]*$") nil t)
785 (goto-char (match-beginning 1))
786 (delete-region (match-beginning 1) (match-end 1))
787 (insert (md5 buffer-string))
788 (save-buffer)))
789 (kill-buffer buffer))))
791 (defun org-mobile-apply (&optional beg end)
792 "Apply all change requests in the current buffer.
793 If BEG and END are given, only do this in that region."
794 (interactive)
795 (require 'org-archive)
796 (setq org-mobile-last-flagged-files nil)
797 (setq beg (or beg (point-min)) end (or end (point-max)))
799 ;; Remove all Note IDs
800 (goto-char beg)
801 (while (re-search-forward "^\\*\\* Note ID: [-0-9A-F]+[ \t]*\n" end t)
802 (replace-match ""))
804 ;; Find all the referenced entries, without making any changes yet
805 (let ((marker (make-marker))
806 (bos-marker (make-marker))
807 (end (move-marker (make-marker) end))
808 (cnt-new 0)
809 (cnt-edit 0)
810 (cnt-flag 0)
811 (cnt-error 0)
812 buf-list
813 id-pos org-mobile-error)
815 ;; Count the new captures
816 (goto-char beg)
817 (while (re-search-forward "^\\* \\(.*\\)" end t)
818 (and (>= (- (match-end 1) (match-beginning 1)) 2)
819 (not (equal (downcase (substring (match-string 1) 0 2)) "f("))
820 (incf cnt-new)))
822 (goto-char beg)
823 (while (re-search-forward
824 "^\\*+[ \t]+F(\\([^():\n]*\\)\\(:\\([^()\n]*\\)\\)?)[ \t]+\\[\\[\\(\\(id\\|olp\\):\\([^]\n]+\\)\\)" end t)
825 (setq id-pos (condition-case msg
826 (org-mobile-locate-entry (match-string 4))
827 (error (nth 1 msg))))
828 (when (and (markerp id-pos)
829 (not (member (marker-buffer id-pos) buf-list)))
830 (org-mobile-timestamp-buffer (marker-buffer id-pos))
831 (push (marker-buffer id-pos) buf-list))
833 (if (or (not id-pos) (stringp id-pos))
834 (progn
835 (goto-char (+ 2 (point-at-bol)))
836 (insert id-pos " ")
837 (incf cnt-error))
838 (add-text-properties (point-at-bol) (point-at-eol)
839 (list 'org-mobile-marker
840 (or id-pos "Linked entry not found")))))
842 ;; OK, now go back and start applying
843 (goto-char beg)
844 (while (re-search-forward "^\\*+[ \t]+F(\\([^():\n]*\\)\\(:\\([^()\n]*\\)\\)?)" end t)
845 (catch 'next
846 (setq id-pos (get-text-property (point-at-bol) 'org-mobile-marker))
847 (if (not (markerp id-pos))
848 (progn
849 (incf cnt-error)
850 (insert "UNKNOWN PROBLEM"))
851 (let* ((action (match-string 1))
852 (data (and (match-end 3) (match-string 3)))
853 (bos (point-at-bol))
854 (eos (save-excursion (org-end-of-subtree t t)))
855 (cmd (if (equal action "")
856 '(progn
857 (incf cnt-flag)
858 (org-toggle-tag "FLAGGED" 'on)
859 (and note
860 (org-entry-put nil "THEFLAGGINGNOTE" note)))
861 (incf cnt-edit)
862 (cdr (assoc action org-mobile-action-alist))))
863 (note (and (equal action "")
864 (buffer-substring (1+ (point-at-eol)) eos)))
865 (org-inhibit-logging 'note) ;; Do not take notes interactively
866 old new)
867 (goto-char bos)
868 (move-marker bos-marker (point))
869 (if (re-search-forward "^** Old value[ \t]*$" eos t)
870 (setq old (buffer-substring
871 (1+ (match-end 0))
872 (progn (outline-next-heading) (point)))))
873 (if (re-search-forward "^** New value[ \t]*$" eos t)
874 (setq new (buffer-substring
875 (1+ (match-end 0))
876 (progn (outline-next-heading)
877 (if (eobp) (org-back-over-empty-lines))
878 (point)))))
879 (setq old (and old (if (string-match "\\S-" old) old nil)))
880 (setq new (and new (if (string-match "\\S-" new) new nil)))
881 (if (and note (> (length note) 0))
882 ;; Make Note into a single line, to fit into a property
883 (setq note (mapconcat 'identity
884 (org-split-string (org-trim note) "\n")
885 "\\n")))
886 (unless (equal data "body")
887 (setq new (and new (org-trim new))
888 old (and old (org-trim old))))
889 (goto-char (+ 2 bos-marker))
890 (unless (markerp id-pos)
891 (insert "BAD REFERENCE ")
892 (incf cnt-error)
893 (throw 'next t))
894 (unless cmd
895 (insert "BAD FLAG ")
896 (incf cnt-error)
897 (throw 'next t))
898 ;; Remember this place so that we can return
899 (move-marker marker (point))
900 (setq org-mobile-error nil)
901 (save-excursion
902 (condition-case msg
903 (org-with-point-at id-pos
904 (progn
905 (eval cmd)
906 (if (member "FLAGGED" (org-get-tags))
907 (add-to-list 'org-mobile-last-flagged-files
908 (buffer-file-name (current-buffer))))))
909 (error (setq org-mobile-error msg))))
910 (when org-mobile-error
911 (switch-to-buffer (marker-buffer marker))
912 (goto-char marker)
913 (incf cnt-error)
914 (insert (if (stringp (nth 1 org-mobile-error))
915 (nth 1 org-mobile-error)
916 "EXECUTION FAILED")
917 " ")
918 (throw 'next t))
919 ;; If we get here, the action has been applied successfully
920 ;; So remove the entry
921 (goto-char bos-marker)
922 (delete-region (point) (org-end-of-subtree t t))))))
923 (save-buffer)
924 (move-marker marker nil)
925 (move-marker end nil)
926 (message "%d new, %d edits, %d flags, %d errors" cnt-new
927 cnt-edit cnt-flag cnt-error)
928 (sit-for 1)))
930 (defun org-mobile-timestamp-buffer (buf)
931 "Time stamp buffer BUF, just to make sure its checksum will change."
932 (with-current-buffer buf
933 (save-excursion
934 (save-restriction
935 (widen)
936 (goto-char (point-min))
937 (if (re-search-forward
938 "^\\([ \t]*\\)#\\+LAST_MOBILE_CHANGE:.*\n?" nil t)
939 (progn
940 (goto-char (match-end 1))
941 (delete-region (point) (match-end 0)))
942 (if (looking-at ".*?-\\*-.*-\\*-")
943 (forward-line 1)))
944 (insert "#+LAST_MOBILE_CHANGE: "
945 (format-time-string "%Y-%m-%d %T") "\n")))))
947 (defun org-mobile-smart-read ()
948 "Parse the entry at point for shortcuts and expand them.
949 These shortcuts are meant for fast and easy typing on the limited
950 keyboards of a mobile device. Below we show a list of the shortcuts
951 currently implemented.
953 The entry is expected to contain an inactive time stamp indicating when
954 the entry was created. When setting dates and
955 times (for example for deadlines), the time strings are interpreted
956 relative to that creation date.
957 Abbreviations are expected to take up entire lines, just because it is so
958 easy to type RET on a mobile device. Abbreviations start with one or two
959 letters, followed immediately by a dot and then additional information.
960 Generally the entire shortcut line is removed after action have been taken.
961 Time stamps will be constructed using `org-read-date'. So for example a
962 line \"dd. 2tue\" will set a deadline on the second Tuesday after the
963 creation date.
965 Here are the shortcuts currently implemented:
967 dd. string set deadline
968 ss. string set scheduling
969 tt. string set time tamp, here.
970 ti. string set inactive time
972 tg. tag1 tag2 tag3 set all these tags, change case where necessary
973 td. kwd set this todo keyword, change case where necessary
975 FIXME: Hmmm, not sure if we can make his work against the
976 auto-correction feature. Needs a bit more thinking. So this function
977 is currently a noop.")
979 (defun org-mobile-locate-entry (link)
980 (if (string-match "\\`id:\\(.*\\)$" link)
981 (org-id-find (match-string 1 link) 'marker)
982 (if (not (string-match "\\`olp:\\(.*?\\):\\(.*\\)$" link))
984 (let ((file (match-string 1 link))
985 (path (match-string 2 link)))
986 (setq file (org-link-unescape file))
987 (setq file (expand-file-name file org-directory))
988 (setq path (mapcar 'org-link-unescape
989 (org-split-string path "/")))
990 (org-find-olp (cons file path))))))
992 (defun org-mobile-edit (what old new)
993 "Edit item WHAT in the current entry by replacing OLD with NEW.
994 WHAT can be \"heading\", \"todo\", \"tags\", \"priority\", or \"body\".
995 The edit only takes place if the current value is equal (except for
996 white space) the OLD. If this is so, OLD will be replace by NEW
997 and the command will return t. If something goes wrong, a string will
998 be returned that indicates what went wrong."
999 (let (current old1 new1)
1000 (if (stringp what) (setq what (intern what)))
1002 (cond
1004 ((memq what '(todo todostate))
1005 (setq current (org-get-todo-state))
1006 (cond
1007 ((equal new "DONEARCHIVE")
1008 (org-todo 'done)
1009 (org-archive-subtree-default))
1010 ((equal new current) t) ; nothing needs to be done
1011 ((or (equal current old)
1012 (eq org-mobile-force-mobile-change t)
1013 (memq 'todo org-mobile-force-mobile-change))
1014 (org-todo (or new 'none)) t)
1015 (t (error "State before change was expected as \"%s\", but is \"%s\""
1016 old current))))
1018 ((eq what 'tags)
1019 (setq current (org-get-tags)
1020 new1 (and new (org-split-string new ":+"))
1021 old1 (and old (org-split-string old ":+")))
1022 (cond
1023 ((org-mobile-tags-same-p current new1) t) ; no change needed
1024 ((or (org-mobile-tags-same-p current old1)
1025 (eq org-mobile-force-mobile-change t)
1026 (memq 'tags org-mobile-force-mobile-change))
1027 (org-set-tags-to new1) t)
1028 (t (error "Tags before change were expected as \"%s\", but are \"%s\""
1029 (or old "") (or current "")))))
1031 ((eq what 'priority)
1032 (when (looking-at org-complex-heading-regexp)
1033 (setq current (and (match-end 3) (substring (match-string 3) 2 3)))
1034 (cond
1035 ((equal current new) t) ; no action required
1036 ((or (equal current old)
1037 (eq org-mobile-force-mobile-change t)
1038 (memq 'tags org-mobile-force-mobile-change))
1039 (org-priority (and new (string-to-char new))))
1040 (t (error "Priority was expected to be %s, but is %s"
1041 old current)))))
1043 ((eq what 'heading)
1044 (when (looking-at org-complex-heading-regexp)
1045 (setq current (match-string 4))
1046 (cond
1047 ((equal current new) t) ; no action required
1048 ((or (equal current old)
1049 (eq org-mobile-force-mobile-change t)
1050 (memq 'heading org-mobile-force-mobile-change))
1051 (goto-char (match-beginning 4))
1052 (insert new)
1053 (delete-region (point) (+ (point) (length current)))
1054 (org-set-tags nil 'align))
1055 (t (error "Heading changed in MobileOrg and on the computer")))))
1057 ((eq what 'body)
1058 (setq current (buffer-substring (min (1+ (point-at-eol)) (point-max))
1059 (save-excursion (outline-next-heading)
1060 (point))))
1061 (if (not (string-match "\\S-" current)) (setq current nil))
1062 (cond
1063 ((org-mobile-bodies-same-p current new) t) ; no action necessary
1064 ((or (org-mobile-bodies-same-p current old)
1065 (eq org-mobile-force-mobile-change t)
1066 (memq 'body org-mobile-force-mobile-change))
1067 (save-excursion
1068 (end-of-line 1)
1069 (insert "\n" new)
1070 (or (bolp) (insert "\n"))
1071 (delete-region (point) (progn (org-back-to-heading t)
1072 (outline-next-heading)
1073 (point))))
1075 (t (error "Body was changed in MobileOrg and on the computer")))))))
1077 (defun org-mobile-tags-same-p (list1 list2)
1078 "Are the two tag lists the same?"
1079 (not (or (org-delete-all list1 list2)
1080 (org-delete-all list2 list1))))
1082 (defun org-mobile-bodies-same-p (a b)
1083 "Compare if A and B are visually equal strings.
1084 We first remove leading and trailing white space from the entire strings.
1085 Then we split the strings into lines and remove leading/trailing whitespace
1086 from each line. Then we compare.
1087 A and B must be strings or nil."
1088 (cond
1089 ((and (not a) (not b)) t)
1090 ((or (not a) (not b)) nil)
1091 (t (setq a (org-trim a) b (org-trim b))
1092 (setq a (mapconcat 'identity (org-split-string a "[ \t]*\n[ \t]*") "\n"))
1093 (setq b (mapconcat 'identity (org-split-string b "[ \t]*\n[ \t]*") "\n"))
1094 (equal a b))))
1096 (provide 'org-mobile)
1098 ;; arch-tag: ace0e26c-58f2-4309-8a61-05ec1535f658
1100 ;;; org-mobile.el ends here