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