org-e-odt.el: Some cleanups
[org-mode.git] / lisp / org-mobile.el
blob946e821ddc20dc18b41763fc953a98de1f0f04dd
1 ;;; org-mobile.el --- Code for asymmetric sync with a mobile device
2 ;; Copyright (C) 2009-2012 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 ;;
8 ;; This file is part of GNU Emacs.
9 ;;
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
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
32 ;; be used.
34 (require 'org)
35 (require 'org-agenda)
36 ;;; Code:
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."
45 :tag "Org Mobile"
46 :group 'org)
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:
54 org-agenda-files
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'"
60 :group 'org-mobile
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"
66 (file))))
68 (defcustom org-mobile-files-exclude-regexp ""
69 "A regexp to exclude files from `org-mobile-files'."
70 :group 'org-mobile
71 :version "24.1"
72 :type 'regexp)
74 (defcustom org-mobile-directory ""
75 "The WebDAV directory where the interaction with the mobile takes place."
76 :group 'org-mobile
77 :type 'directory)
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."
87 :group 'org-mobile
88 :version "24.1"
89 :type 'boolean)
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."
95 :group 'org-mobile
96 :version "24.1"
97 :type 'directory)
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
115 session."
116 :group 'org-mobile
117 :version "24.1"
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."
134 :group 'org-mobile
135 :type 'file)
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."
145 :group 'org-mobile
146 :type 'file)
148 (defcustom org-mobile-agendas 'all
149 "The agendas that should be pushed to MobileOrg.
150 Allowed values:
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."
156 :group 'org-mobile
157 :version "24.1"
158 :type '(choice
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."
167 :group 'org-mobile
168 :type 'boolean)
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."
177 :group 'org-mobile
178 :type '(choice
179 (const :tag "Always" t)
180 (const :tag "Never" nil)
181 (set :greedy t :tag "Specify"
182 (const todo)
183 (const tags)
184 (const priority)
185 (const heading)
186 (const body))))
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
201 of that entry.
203 For now, it is not recommended to change this variable."
204 :group 'org-mobile
205 :type '(repeat
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."
214 :group 'org-mobile
215 :type 'string)
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)
262 (files
263 (apply 'append
264 (mapcar
265 (lambda (f)
266 (cond
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
271 (copy-sequence
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))
276 (list f))
277 (t nil)))
278 org-mobile-files)))
279 (files (delete
281 (mapcar (lambda (f)
282 (unless (and (not (string= org-mobile-files-exclude-regexp ""))
283 (string-match org-mobile-files-exclude-regexp f))
284 (identity f)))
285 files)))
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
290 ;; be listed.
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)
296 (push 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)))
301 (nreverse rtn)))
303 (defvar org-agenda-filter)
305 ;;;###autoload
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."
310 (interactive)
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))
315 (save-excursion
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))))
331 (redraw-display)
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))
337 (org-agenda-redo)
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.")
346 ;;;###autoload
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."
351 (interactive)
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
358 (save-restriction
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))
380 (error
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))
387 (error
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)
392 (file-exists-p
393 (file-name-directory org-mobile-inbox-for-pull)))
394 (error
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))
401 (error
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
422 done-kwds
423 (org-uniquify org-todo-keywords-for-agenda)))
424 (setq drawers (org-uniquify org-drawers-for-agenda))
425 (setq tags (org-uniquify
426 (delq nil
427 (mapcar
428 (lambda (e)
429 (cond ((stringp e) e)
430 ((listp e)
431 (if (stringp (car e)) (car e) nil))
432 (t nil)))
433 org-tag-alist-for-agenda))))
434 (with-temp-file
435 (if org-mobile-use-encryption
436 org-mobile-encryption-tempfile
437 target-file)
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))
443 (cdr entry)))
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
453 (lambda (x)
454 (cond ((null x) nil)
455 ((stringp x) x)
456 ((eq (car x) :startgroup) "{")
457 ((eq (car x) :endgroup) "}")
458 ((eq (car x) :newline) nil)
459 ((listp x) (car x))
460 (t nil)))
461 def-tags))
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
481 target-file)
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))
507 (save-excursion
508 (setq buf (find-file file))
509 (when (and (= (point-min) (point-max)))
510 (insert "\n")
511 (save-buffer)
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)
518 (kill-buffer buf)))
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)
525 entry file sum)
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."
534 (let ((custom-list
535 ;; normalize different versions
536 (delq nil
537 (mapcar
538 (lambda (x)
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)
546 (cond
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))
560 (cond
561 ((stringp (cdr e))
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))
571 (or (null (nth 3 e))
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))
576 ;; a normal command
577 (setq key (car e) desc (nth 1 e) type (nth 2 e) match (nth 3 e)
578 settings (nth 4 e))
579 (setq settings
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))
584 "</after>"))
585 settings))
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
589 ;; ignore it.
592 ;; a block agenda
593 (setq gkey (car e) gdesc (nth 1 e) gsettings (nth 3 e) cmds (nth 2 e))
594 (setq cnt 0)
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))
598 (setq settings
599 (cons (list 'org-agenda-title-append
600 (concat "<after>KEYS=" gkey "#" (number-to-string
601 (setq cnt (1+ cnt)))
602 " TITLE: " gdesc " " match "</after>"))
603 settings))
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)
611 (with-temp-file file
612 (org-mode)
613 (insert "#+READONLY\n")
614 (insert all)
615 (goto-char (point-min))
616 (while (not (eobp))
617 (cond
618 ((looking-at "[ \t]*$")) ; keep empty lines
619 ((looking-at "=+$")
620 ;; remove underlining
621 (delete-region (point) (point-at-eol)))
622 ((get-text-property (point) 'org-agenda-structural-header)
623 (setq in-date nil)
624 (setq app (get-text-property (point)
625 'org-agenda-title-append))
626 (setq short (get-text-property (point)
627 'short-heading))
628 (when (and short (looking-at ".+"))
629 (replace-match short)
630 (beginning-of-line 1))
631 (when app
632 (end-of-line 1)
633 (insert app)
634 (beginning-of-line 1))
635 (insert "* "))
636 ((get-text-property (point) 'org-agenda-date-header)
637 (setq in-date t)
638 (insert "** "))
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)
642 '("diary" "sexp")))
643 (if (setq pl (text-property-any (point) (point-at-eol) 'org-heading t))
644 (progn
645 (setq prefix (org-trim (buffer-substring
646 (point) pl))
647 line (org-trim (buffer-substring
649 (point-at-eol))))
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 "*** " "** "))
655 (end-of-line 1)
656 (insert "\n")
657 (unless sexp
658 (insert (org-agenda-get-some-entry-text
659 m 10 " " 'planning)
660 "\n")
661 (when (setq id
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
668 "\n :END:\n")))))
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
676 (concat "olp:"
677 (org-mobile-escape-olp (file-name-nondirectory buffer-file-name))
679 (mapconcat 'org-mobile-escape-olp
680 (org-get-outline-path)
681 "/")
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)))
689 ;;;###autoload
690 (defun org-mobile-create-sumo-agenda ()
691 "Create a file that contains all custom agenda views."
692 (interactive)
693 (let* ((file (expand-file-name "agendas.org"
694 org-mobile-directory))
695 (file1 (if org-mobile-use-encryption
696 org-mobile-encryption-tempfile
697 file))
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))
704 (when sumo
705 (org-store-agenda-views))
706 (when org-mobile-use-encryption
707 (org-mobile-encrypt-and-move file1 file)
708 (delete-file file1)
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)
717 (when outfile
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'."
723 (shell-command
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'."
732 (shell-command
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."
749 (interactive)
750 (let* ((encfile 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))
754 (capture-buffer
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))
763 not-empty content)
764 (with-current-buffer capture-buffer
765 (setq content (buffer-string))
766 (setq not-empty (string-match "\\S-" content))
767 (when not-empty
768 (set-buffer inbox-buffer)
769 (widen)
770 (goto-char (point-max))
771 (or (bolp) (newline))
772 (move-marker insertion-point
773 (prog1 (point) (insert content)))
774 (save-buffer)
775 (set-buffer capture-buffer)
776 (erase-buffer)
777 (save-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
782 capture-file)
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)))
790 (when buffer
791 (with-current-buffer buffer
792 (when (re-search-forward (concat "\\([0-9a-fA-F]\\{30,\\}\\).*?"
793 (regexp-quote org-mobile-capture-file)
794 "[ \t]*$") nil t)
795 (goto-char (match-beginning 1))
796 (delete-region (match-beginning 1) (match-end 1))
797 (insert (md5 buffer-string))
798 (save-buffer)))
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."
804 (interactive)
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
810 (goto-char beg)
811 (while (re-search-forward "^\\*\\* Note ID: [-0-9A-F]+[ \t]*\n" end t)
812 (replace-match ""))
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))
818 (cnt-new 0)
819 (cnt-edit 0)
820 (cnt-flag 0)
821 (cnt-error 0)
822 buf-list
823 id-pos org-mobile-error)
825 ;; Count the new captures
826 (goto-char beg)
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("))
830 (incf cnt-new)))
832 (goto-char beg)
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))
844 (progn
845 (goto-char (+ 2 (point-at-bol)))
846 (insert id-pos " ")
847 (incf cnt-error))
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
853 (goto-char beg)
854 (while (re-search-forward "^\\*+[ \t]+F(\\([^():\n]*\\)\\(:\\([^()\n]*\\)\\)?)" end t)
855 (catch 'next
856 (setq id-pos (get-text-property (point-at-bol) 'org-mobile-marker))
857 (if (not (markerp id-pos))
858 (progn
859 (incf cnt-error)
860 (insert "UNKNOWN PROBLEM"))
861 (let* ((action (match-string 1))
862 (data (and (match-end 3) (match-string 3)))
863 (bos (point-at-bol))
864 (eos (save-excursion (org-end-of-subtree t t)))
865 (cmd (if (equal action "")
866 '(progn
867 (incf cnt-flag)
868 (org-toggle-tag "FLAGGED" 'on)
869 (and note
870 (org-entry-put nil "THEFLAGGINGNOTE" note)))
871 (incf cnt-edit)
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
876 old new)
877 (goto-char bos)
878 (move-marker bos-marker (point))
879 (if (re-search-forward "^** Old value[ \t]*$" eos t)
880 (setq old (buffer-substring
881 (1+ (match-end 0))
882 (progn (outline-next-heading) (point)))))
883 (if (re-search-forward "^** New value[ \t]*$" eos t)
884 (setq new (buffer-substring
885 (1+ (match-end 0))
886 (progn (outline-next-heading)
887 (if (eobp) (org-back-over-empty-lines))
888 (point)))))
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")
895 "\\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 ")
902 (incf cnt-error)
903 (throw 'next t))
904 (unless cmd
905 (insert "BAD FLAG ")
906 (incf cnt-error)
907 (throw 'next t))
908 ;; Remember this place so that we can return
909 (move-marker marker (point))
910 (setq org-mobile-error nil)
911 (save-excursion
912 (condition-case msg
913 (org-with-point-at id-pos
914 (progn
915 (eval cmd)
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))
922 (goto-char marker)
923 (incf cnt-error)
924 (insert (if (stringp (nth 1 org-mobile-error))
925 (nth 1 org-mobile-error)
926 "EXECUTION FAILED")
927 " ")
928 (throw 'next t))
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))))))
933 (save-buffer)
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)
938 (sit-for 1)))
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
943 (save-excursion
944 (save-restriction
945 (widen)
946 (goto-char (point-min))
947 (if (re-search-forward
948 "^\\([ \t]*\\)#\\+LAST_MOBILE_CHANGE:.*\n?" nil t)
949 (progn
950 (goto-char (match-end 1))
951 (delete-region (point) (match-end 0)))
952 (if (looking-at ".*?-\\*-.*-\\*-")
953 (forward-line 1)))
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
973 creation date.
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)))
1012 (cond
1014 ((memq what '(todo todostate))
1015 (setq current (org-get-todo-state))
1016 (cond
1017 ((equal new "DONEARCHIVE")
1018 (org-todo 'done)
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\""
1026 old current))))
1028 ((eq what 'tags)
1029 (setq current (org-get-tags)
1030 new1 (and new (org-split-string new ":+"))
1031 old1 (and old (org-split-string old ":+")))
1032 (cond
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)))
1044 (cond
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"
1051 old current)))))
1053 ((eq what 'heading)
1054 (when (looking-at org-complex-heading-regexp)
1055 (setq current (match-string 4))
1056 (cond
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))
1062 (insert new)
1063 (delete-region (point) (+ (point) (length current)))
1064 (org-set-tags nil 'align))
1065 (t (error "Heading changed in MobileOrg and on the computer")))))
1067 ((eq what 'body)
1068 (setq current (buffer-substring (min (1+ (point-at-eol)) (point-max))
1069 (save-excursion (outline-next-heading)
1070 (point))))
1071 (if (not (string-match "\\S-" current)) (setq current nil))
1072 (cond
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))
1077 (save-excursion
1078 (end-of-line 1)
1079 (insert "\n" new)
1080 (or (bolp) (insert "\n"))
1081 (delete-region (point) (progn (org-back-to-heading t)
1082 (outline-next-heading)
1083 (point))))
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."
1098 (cond
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"))
1104 (equal a b))))
1106 (provide 'org-mobile)
1108 ;;; org-mobile.el ends here