org-e-latex: Change syntax for images attributes
[org-mode.git] / lisp / org-mobile.el
blobffdd66513bef5f0577d9145657759c531c1d7d13
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', only if new items were found.
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)
322 (org-agenda-files (mapcar 'car org-mobile-files-alist)))
323 (org-mobile-create-sumo-agenda))
324 (message "Creating agendas...done")
325 (org-save-all-org-buffers) ; to save any IDs created by this process
326 (message "Copying files...")
327 (org-mobile-copy-agenda-files)
328 (message "Writing index file...")
329 (org-mobile-create-index-file)
330 (message "Writing checksums...")
331 (org-mobile-write-checksums)
332 (run-hooks 'org-mobile-post-push-hook))))
333 (redraw-display)
334 (when (and a-buffer (buffer-live-p a-buffer))
335 (if (not (get-buffer-window a-buffer))
336 (kill-buffer a-buffer)
337 (let ((cw (selected-window)))
338 (select-window (get-buffer-window a-buffer))
339 (org-agenda-redo)
340 (select-window cw)))))
341 (message "Files for mobile viewer staged"))
343 (defvar org-mobile-before-process-capture-hook nil
344 "Hook that is run after content was moved to `org-mobile-inbox-for-pull'.
345 The inbox file is visited by the current buffer, and the buffer is
346 narrowed to the newly captured data.")
348 ;;;###autoload
349 (defun org-mobile-pull ()
350 "Pull the contents of `org-mobile-capture-file' and integrate them.
351 Apply all flagged actions, flag entries to be flagged and then call an
352 agenda view showing the flagged items."
353 (interactive)
354 (org-mobile-check-setup)
355 (run-hooks 'org-mobile-pre-pull-hook)
356 (let ((insertion-marker (org-mobile-move-capture)))
357 (if (not (markerp insertion-marker))
358 (message "No new items")
359 (org-with-point-at insertion-marker
360 (save-restriction
361 (narrow-to-region (point) (point-max))
362 (run-hooks 'org-mobile-before-process-capture-hook)))
363 (org-with-point-at insertion-marker
364 (org-mobile-apply (point) (point-max)))
365 (move-marker insertion-marker nil)
366 (run-hooks 'org-mobile-post-pull-hook)
367 (when org-mobile-last-flagged-files
368 ;; Make an agenda view of flagged entries, but only in the files
369 ;; where stuff has been added.
370 (put 'org-agenda-files 'org-restrict org-mobile-last-flagged-files)
371 (let ((org-agenda-keep-restricted-file-list t))
372 (org-agenda nil "?"))))))
374 (defun org-mobile-check-setup ()
375 "Check if org-mobile-directory has been set up."
376 (org-mobile-cleanup-encryption-tempfile)
377 (unless (and org-directory
378 (stringp org-directory)
379 (string-match "\\S-" org-directory)
380 (file-exists-p org-directory)
381 (file-directory-p org-directory))
382 (error
383 "Please set `org-directory' to the directory where your org files live"))
384 (unless (and org-mobile-directory
385 (stringp org-mobile-directory)
386 (string-match "\\S-" org-mobile-directory)
387 (file-exists-p org-mobile-directory)
388 (file-directory-p org-mobile-directory))
389 (error
390 "Variable `org-mobile-directory' must point to an existing directory"))
391 (unless (and org-mobile-inbox-for-pull
392 (stringp org-mobile-inbox-for-pull)
393 (string-match "\\S-" org-mobile-inbox-for-pull)
394 (file-exists-p
395 (file-name-directory org-mobile-inbox-for-pull)))
396 (error
397 "Variable `org-mobile-inbox-for-pull' must point to a file in an existing directory"))
398 (unless (and org-mobile-checksum-binary
399 (string-match "\\S-" org-mobile-checksum-binary))
400 (error "No executable found to compute checksums"))
401 (when org-mobile-use-encryption
402 (unless (string-match "\\S-" (org-mobile-encryption-password))
403 (error
404 "To use encryption, you must set `org-mobile-encryption-password'"))
405 (unless (file-writable-p org-mobile-encryption-tempfile)
406 (error "Cannot write to encryption tempfile %s"
407 org-mobile-encryption-tempfile))
408 (unless (executable-find "openssl")
409 (error "OpenSSL is needed to encrypt files"))))
411 (defun org-mobile-create-index-file ()
412 "Write the index file in the WebDAV directory."
413 (let ((files-alist (sort (copy-sequence org-mobile-files-alist)
414 (lambda (a b) (string< (cdr a) (cdr b)))))
415 (def-todo (default-value 'org-todo-keywords))
416 (def-tags (default-value 'org-tag-alist))
417 (target-file (expand-file-name org-mobile-index-file
418 org-mobile-directory))
419 file link-name todo-kwds done-kwds tags drawers entry kwds dwds twds)
421 (org-agenda-prepare-buffers (mapcar 'car files-alist))
422 (setq done-kwds (org-uniquify org-done-keywords-for-agenda))
423 (setq todo-kwds (org-delete-all
424 done-kwds
425 (org-uniquify org-todo-keywords-for-agenda)))
426 (setq drawers (org-uniquify org-drawers-for-agenda))
427 (setq tags (mapcar 'car (org-global-tags-completion-table
428 (mapcar 'car files-alist))))
429 (with-temp-file
430 (if org-mobile-use-encryption
431 org-mobile-encryption-tempfile
432 target-file)
433 (while (setq entry (pop def-todo))
434 (insert "#+READONLY\n")
435 (setq kwds (mapcar (lambda (x) (if (string-match "(" x)
436 (substring x 0 (match-beginning 0))
438 (cdr entry)))
439 (insert "#+TODO: " (mapconcat 'identity kwds " ") "\n")
440 (setq dwds (member "|" kwds)
441 twds (org-delete-all dwds kwds)
442 todo-kwds (org-delete-all twds todo-kwds)
443 done-kwds (org-delete-all dwds done-kwds)))
444 (when (or todo-kwds done-kwds)
445 (insert "#+TODO: " (mapconcat 'identity todo-kwds " ") " | "
446 (mapconcat 'identity done-kwds " ") "\n"))
447 (setq def-tags (mapcar
448 (lambda (x)
449 (cond ((null x) nil)
450 ((stringp x) x)
451 ((eq (car x) :startgroup) "{")
452 ((eq (car x) :endgroup) "}")
453 ((eq (car x) :newline) nil)
454 ((listp x) (car x))))
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 "</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 (defun org-mobile-create-sumo-agenda ()
684 "Create a file that contains all custom agenda views."
685 (interactive)
686 (let* ((file (expand-file-name "agendas.org"
687 org-mobile-directory))
688 (file1 (if org-mobile-use-encryption
689 org-mobile-encryption-tempfile
690 file))
691 (sumo (org-mobile-sumo-agenda-command))
692 (org-agenda-custom-commands
693 (list (append sumo (list (list file1)))))
694 (org-mobile-creating-agendas t))
695 (unless (file-writable-p file1)
696 (error "Cannot write to file %s" file1))
697 (when sumo
698 (org-store-agenda-views))
699 (when org-mobile-use-encryption
700 (org-mobile-encrypt-and-move file1 file)
701 (delete-file file1)
702 (org-mobile-cleanup-encryption-tempfile))))
704 (defun org-mobile-encrypt-and-move (infile outfile)
705 "Encrypt INFILE locally to INFILE_enc, then move it to OUTFILE.
706 We do this in two steps so that remote paths will work, even if the
707 encryption program does not understand them."
708 (let ((encfile (concat infile "_enc")))
709 (org-mobile-encrypt-file infile encfile)
710 (when outfile
711 (copy-file encfile outfile 'ok-if-exists)
712 (delete-file encfile))))
714 (defun org-mobile-encrypt-file (infile outfile)
715 "Encrypt INFILE to OUTFILE, using `org-mobile-encryption-password'."
716 (shell-command
717 (format "openssl enc -aes-256-cbc -salt -pass %s -in %s -out %s"
718 (shell-quote-argument (concat "pass:"
719 (org-mobile-encryption-password)))
720 (shell-quote-argument (expand-file-name infile))
721 (shell-quote-argument (expand-file-name outfile)))))
723 (defun org-mobile-decrypt-file (infile outfile)
724 "Decrypt INFILE to OUTFILE, using `org-mobile-encryption-password'."
725 (shell-command
726 (format "openssl enc -d -aes-256-cbc -salt -pass %s -in %s -out %s"
727 (shell-quote-argument (concat "pass:"
728 (org-mobile-encryption-password)))
729 (shell-quote-argument (expand-file-name infile))
730 (shell-quote-argument (expand-file-name outfile)))))
732 (defun org-mobile-cleanup-encryption-tempfile ()
733 "Remove the encryption tempfile if it exists."
734 (and (stringp org-mobile-encryption-tempfile)
735 (file-exists-p org-mobile-encryption-tempfile)
736 (delete-file org-mobile-encryption-tempfile)))
738 (defun org-mobile-move-capture ()
739 "Move the contents of the capture file to the inbox file.
740 Return a marker to the location where the new content has been added.
741 If nothing new has been added, return nil."
742 (interactive)
743 (let* ((encfile nil)
744 (capture-file (expand-file-name org-mobile-capture-file
745 org-mobile-directory))
746 (inbox-buffer (find-file-noselect org-mobile-inbox-for-pull))
747 (capture-buffer
748 (if (not org-mobile-use-encryption)
749 (find-file-noselect capture-file)
750 (org-mobile-cleanup-encryption-tempfile)
751 (setq encfile (concat org-mobile-encryption-tempfile "_enc"))
752 (copy-file capture-file encfile)
753 (org-mobile-decrypt-file encfile org-mobile-encryption-tempfile)
754 (find-file-noselect org-mobile-encryption-tempfile)))
755 (insertion-point (make-marker))
756 not-empty content)
757 (with-current-buffer capture-buffer
758 (setq content (buffer-string))
759 (setq not-empty (string-match "\\S-" content))
760 (when not-empty
761 (set-buffer inbox-buffer)
762 (widen)
763 (goto-char (point-max))
764 (or (bolp) (newline))
765 (move-marker insertion-point
766 (prog1 (point) (insert content)))
767 (save-buffer)
768 (set-buffer capture-buffer)
769 (erase-buffer)
770 (save-buffer)
771 (org-mobile-update-checksum-for-capture-file (buffer-string))))
772 (kill-buffer capture-buffer)
773 (when org-mobile-use-encryption
774 (org-mobile-encrypt-and-move org-mobile-encryption-tempfile
775 capture-file)
776 (org-mobile-cleanup-encryption-tempfile))
777 (if not-empty insertion-point)))
779 (defun org-mobile-update-checksum-for-capture-file (buffer-string)
780 "Find the checksum line and modify it to match BUFFER-STRING."
781 (let* ((file (expand-file-name "checksums.dat" org-mobile-directory))
782 (buffer (find-file-noselect file)))
783 (when buffer
784 (with-current-buffer buffer
785 (when (re-search-forward (concat "\\([0-9a-fA-F]\\{30,\\}\\).*?"
786 (regexp-quote org-mobile-capture-file)
787 "[ \t]*$") nil t)
788 (goto-char (match-beginning 1))
789 (delete-region (match-beginning 1) (match-end 1))
790 (insert (md5 buffer-string))
791 (save-buffer)))
792 (kill-buffer buffer))))
794 (defun org-mobile-apply (&optional beg end)
795 "Apply all change requests in the current buffer.
796 If BEG and END are given, only do this in that region."
797 (interactive)
798 (require 'org-archive)
799 (setq org-mobile-last-flagged-files nil)
800 (setq beg (or beg (point-min)) end (or end (point-max)))
802 ;; Remove all Note IDs
803 (goto-char beg)
804 (while (re-search-forward "^\\*\\* Note ID: [-0-9A-F]+[ \t]*\n" end t)
805 (replace-match ""))
807 ;; Find all the referenced entries, without making any changes yet
808 (let ((marker (make-marker))
809 (bos-marker (make-marker))
810 (end (move-marker (make-marker) end))
811 (cnt-new 0)
812 (cnt-edit 0)
813 (cnt-flag 0)
814 (cnt-error 0)
815 buf-list
816 id-pos org-mobile-error)
818 ;; Count the new captures
819 (goto-char beg)
820 (while (re-search-forward "^\\* \\(.*\\)" end t)
821 (and (>= (- (match-end 1) (match-beginning 1)) 2)
822 (not (equal (downcase (substring (match-string 1) 0 2)) "f("))
823 (incf cnt-new)))
825 ;; Find and apply the edits
826 (goto-char beg)
827 (while (re-search-forward
828 "^\\*+[ \t]+F(\\([^():\n]*\\)\\(:\\([^()\n]*\\)\\)?)[ \t]+\\[\\[\\(\\(id\\|olp\\):\\([^]\n]+\\)\\)" end t)
829 (catch 'next
830 (let* ((action (match-string 1))
831 (data (and (match-end 3) (match-string 3)))
832 (id-pos (condition-case msg
833 (org-mobile-locate-entry (match-string 4))
834 (error (nth 1 msg))))
835 (bos (point-at-bol))
836 (eos (save-excursion (org-end-of-subtree t t)))
837 (cmd (if (equal action "")
838 '(progn
839 (incf cnt-flag)
840 (org-toggle-tag "FLAGGED" 'on)
841 (and note
842 (org-entry-put nil "THEFLAGGINGNOTE" note)))
843 (incf cnt-edit)
844 (cdr (assoc action org-mobile-action-alist))))
845 (note (and (equal action "")
846 (buffer-substring (1+ (point-at-eol)) eos)))
847 (org-inhibit-logging 'note) ;; Do not take notes interactively
848 old new)
850 (goto-char bos)
851 (when (and (markerp id-pos)
852 (not (member (marker-buffer id-pos) buf-list)))
853 (org-mobile-timestamp-buffer (marker-buffer id-pos))
854 (push (marker-buffer id-pos) buf-list))
855 (unless (markerp id-pos)
856 (goto-char (+ 2 (point-at-bol)))
857 (if (stringp id-pos)
858 (insert id-pos " ")
859 (insert "BAD REFERENCE "))
860 (incf cnt-error)
861 (throw 'next t))
862 (unless cmd
863 (insert "BAD FLAG ")
864 (incf cnt-error)
865 (throw 'next t))
866 (move-marker bos-marker (point))
867 (if (re-search-forward "^** Old value[ \t]*$" eos t)
868 (setq old (buffer-substring
869 (1+ (match-end 0))
870 (progn (outline-next-heading) (point)))))
871 (if (re-search-forward "^** New value[ \t]*$" eos t)
872 (setq new (buffer-substring
873 (1+ (match-end 0))
874 (progn (outline-next-heading)
875 (if (eobp) (org-back-over-empty-lines))
876 (point)))))
877 (setq old (and old (if (string-match "\\S-" old) old nil)))
878 (setq new (and new (if (string-match "\\S-" new) new nil)))
879 (if (and note (> (length note) 0))
880 ;; Make Note into a single line, to fit into a property
881 (setq note (mapconcat 'identity
882 (org-split-string (org-trim note) "\n")
883 "\\n")))
884 (unless (equal data "body")
885 (setq new (and new (org-trim new))
886 old (and old (org-trim old))))
887 (goto-char (+ 2 bos-marker))
888 ;; Remember this place so that we can return
889 (move-marker marker (point))
890 (setq org-mobile-error nil)
891 (save-excursion
892 (condition-case msg
893 (org-with-point-at id-pos
894 (progn
895 (eval cmd)
896 (unless (member data (list "delete" "archive" "archive-sibling" "addheading"))
897 (if (member "FLAGGED" (org-get-tags))
898 (add-to-list 'org-mobile-last-flagged-files
899 (buffer-file-name (current-buffer)))))))
900 (error (setq org-mobile-error msg))))
901 (when org-mobile-error
902 (org-pop-to-buffer-same-window (marker-buffer marker))
903 (goto-char marker)
904 (incf cnt-error)
905 (insert (if (stringp (nth 1 org-mobile-error))
906 (nth 1 org-mobile-error)
907 "EXECUTION FAILED")
908 " ")
909 (throw 'next t))
910 ;; If we get here, the action has been applied successfully
911 ;; So remove the entry
912 (goto-char bos-marker)
913 (delete-region (point) (org-end-of-subtree t t)))))
914 (save-buffer)
915 (move-marker marker nil)
916 (move-marker end nil)
917 (message "%d new, %d edits, %d flags, %d errors" cnt-new
918 cnt-edit cnt-flag cnt-error)
919 (sit-for 1)))
921 (defun org-mobile-timestamp-buffer (buf)
922 "Time stamp buffer BUF, just to make sure its checksum will change."
923 (with-current-buffer buf
924 (save-excursion
925 (save-restriction
926 (widen)
927 (goto-char (point-min))
928 (if (re-search-forward
929 "^\\([ \t]*\\)#\\+LAST_MOBILE_CHANGE:.*\n?" nil t)
930 (progn
931 (goto-char (match-end 1))
932 (delete-region (point) (match-end 0)))
933 (if (looking-at ".*?-\\*-.*-\\*-")
934 (forward-line 1)))
935 (insert "#+LAST_MOBILE_CHANGE: "
936 (format-time-string "%Y-%m-%d %T") "\n")))))
938 (defun org-mobile-smart-read ()
939 "Parse the entry at point for shortcuts and expand them.
940 These shortcuts are meant for fast and easy typing on the limited
941 keyboards of a mobile device. Below we show a list of the shortcuts
942 currently implemented.
944 The entry is expected to contain an inactive time stamp indicating when
945 the entry was created. When setting dates and
946 times (for example for deadlines), the time strings are interpreted
947 relative to that creation date.
948 Abbreviations are expected to take up entire lines, just because it is so
949 easy to type RET on a mobile device. Abbreviations start with one or two
950 letters, followed immediately by a dot and then additional information.
951 Generally the entire shortcut line is removed after action have been taken.
952 Time stamps will be constructed using `org-read-date'. So for example a
953 line \"dd. 2tue\" will set a deadline on the second Tuesday after the
954 creation date.
956 Here are the shortcuts currently implemented:
958 dd. string set deadline
959 ss. string set scheduling
960 tt. string set time tamp, here.
961 ti. string set inactive time
963 tg. tag1 tag2 tag3 set all these tags, change case where necessary
964 td. kwd set this todo keyword, change case where necessary
966 FIXME: Hmmm, not sure if we can make his work against the
967 auto-correction feature. Needs a bit more thinking. So this function
968 is currently a noop.")
970 (defun org-mobile-locate-entry (link)
971 (if (string-match "\\`id:\\(.*\\)$" link)
972 (org-id-find (match-string 1 link) 'marker)
973 (if (not (string-match "\\`olp:\\(.*?\\):\\(.*\\)$" link))
974 ; not found with path, but maybe it is to be inserted
975 ; in top level of the file?
976 (if (not (string-match "\\`olp:\\(.*?\\)$" link))
978 (let ((file (match-string 1 link)))
979 (setq file (org-link-unescape file))
980 (setq file (expand-file-name file org-directory))
981 (save-excursion
982 (find-file file)
983 (goto-char (point-max))
984 (newline)
985 (goto-char (point-max))
986 (move-marker (make-marker) (point)))))
987 (let ((file (match-string 1 link))
988 (path (match-string 2 link)))
989 (setq file (org-link-unescape file))
990 (setq file (expand-file-name file org-directory))
991 (setq path (mapcar 'org-link-unescape
992 (org-split-string path "/")))
993 (org-find-olp (cons file path))))))
995 (defun org-mobile-edit (what old new)
996 "Edit item WHAT in the current entry by replacing OLD with NEW.
997 WHAT can be \"heading\", \"todo\", \"tags\", \"priority\", or \"body\".
998 The edit only takes place if the current value is equal (except for
999 white space) the OLD. If this is so, OLD will be replace by NEW
1000 and the command will return t. If something goes wrong, a string will
1001 be returned that indicates what went wrong."
1002 (let (current old1 new1 level)
1003 (if (stringp what) (setq what (intern what)))
1005 (cond
1007 ((memq what '(todo todostate))
1008 (setq current (org-get-todo-state))
1009 (cond
1010 ((equal new "DONEARCHIVE")
1011 (org-todo 'done)
1012 (org-archive-subtree-default))
1013 ((equal new current) t) ; nothing needs to be done
1014 ((or (equal current old)
1015 (eq org-mobile-force-mobile-change t)
1016 (memq 'todo org-mobile-force-mobile-change))
1017 (org-todo (or new 'none)) t)
1018 (t (error "State before change was expected as \"%s\", but is \"%s\""
1019 old current))))
1021 ((eq what 'tags)
1022 (setq current (org-get-tags)
1023 new1 (and new (org-split-string new ":+"))
1024 old1 (and old (org-split-string old ":+")))
1025 (cond
1026 ((org-mobile-tags-same-p current new1) t) ; no change needed
1027 ((or (org-mobile-tags-same-p current old1)
1028 (eq org-mobile-force-mobile-change t)
1029 (memq 'tags org-mobile-force-mobile-change))
1030 (org-set-tags-to new1) t)
1031 (t (error "Tags before change were expected as \"%s\", but are \"%s\""
1032 (or old "") (or current "")))))
1034 ((eq what 'priority)
1035 (when (looking-at org-complex-heading-regexp)
1036 (setq current (and (match-end 3) (substring (match-string 3) 2 3)))
1037 (cond
1038 ((equal current new) t) ; no action required
1039 ((or (equal current old)
1040 (eq org-mobile-force-mobile-change t)
1041 (memq 'tags org-mobile-force-mobile-change))
1042 (org-priority (and new (string-to-char new))))
1043 (t (error "Priority was expected to be %s, but is %s"
1044 old current)))))
1046 ((eq what 'heading)
1047 (when (looking-at org-complex-heading-regexp)
1048 (setq current (match-string 4))
1049 (cond
1050 ((equal current new) t) ; no action required
1051 ((or (equal current old)
1052 (eq org-mobile-force-mobile-change t)
1053 (memq 'heading org-mobile-force-mobile-change))
1054 (goto-char (match-beginning 4))
1055 (insert new)
1056 (delete-region (point) (+ (point) (length current)))
1057 (org-set-tags nil 'align))
1058 (t (error "Heading changed in MobileOrg and on the computer")))))
1060 ((eq what 'addheading)
1061 (if (org-on-heading-p) ; if false we are in top-level of file
1062 (progn
1063 (end-of-line 1)
1064 (org-insert-heading-respect-content)
1065 (org-demote))
1066 (beginning-of-line)
1067 (insert "* "))
1068 (insert new))
1070 ((eq what 'refile)
1071 (org-copy-subtree)
1072 (org-with-point-at (org-mobile-locate-entry new)
1073 (if (org-on-heading-p) ; if false we are in top-level of file
1074 (progn
1075 (setq level (org-get-valid-level (funcall outline-level) 1))
1076 (org-end-of-subtree t t)
1077 (org-paste-subtree level))
1078 (org-paste-subtree 1)))
1079 (org-cut-subtree))
1081 ((eq what 'delete)
1082 (org-cut-subtree))
1084 ((eq what 'archive)
1085 (org-archive-subtree))
1087 ((eq what 'archive-sibling)
1088 (org-archive-to-archive-sibling))
1090 ((eq what 'body)
1091 (setq current (buffer-substring (min (1+ (point-at-eol)) (point-max))
1092 (save-excursion (outline-next-heading)
1093 (point))))
1094 (if (not (string-match "\\S-" current)) (setq current nil))
1095 (cond
1096 ((org-mobile-bodies-same-p current new) t) ; no action necessary
1097 ((or (org-mobile-bodies-same-p current old)
1098 (eq org-mobile-force-mobile-change t)
1099 (memq 'body org-mobile-force-mobile-change))
1100 (save-excursion
1101 (end-of-line 1)
1102 (insert "\n" new)
1103 (or (bolp) (insert "\n"))
1104 (delete-region (point) (progn (org-back-to-heading t)
1105 (outline-next-heading)
1106 (point))))
1108 (t (error "Body was changed in MobileOrg and on the computer")))))))
1110 (defun org-mobile-tags-same-p (list1 list2)
1111 "Are the two tag lists the same?"
1112 (not (or (org-delete-all list1 list2)
1113 (org-delete-all list2 list1))))
1115 (defun org-mobile-bodies-same-p (a b)
1116 "Compare if A and B are visually equal strings.
1117 We first remove leading and trailing white space from the entire strings.
1118 Then we split the strings into lines and remove leading/trailing whitespace
1119 from each line. Then we compare.
1120 A and B must be strings or nil."
1121 (cond
1122 ((and (not a) (not b)) t)
1123 ((or (not a) (not b)) nil)
1124 (t (setq a (org-trim a) b (org-trim b))
1125 (setq a (mapconcat 'identity (org-split-string a "[ \t]*\n[ \t]*") "\n"))
1126 (setq b (mapconcat 'identity (org-split-string b "[ \t]*\n[ \t]*") "\n"))
1127 (equal a b))))
1129 (provide 'org-mobile)
1131 ;; Local variables:
1132 ;; generated-autoload-file: "org-loaddefs.el"
1133 ;; End:
1135 ;;; org-mobile.el ends here