org.el: Display images in link descriptions.
[org-mode.git] / lisp / org-mobile.el
blob54b6e037ef66abd8f71854d55876e60bd0be7bdf
1 ;;; org-mobile.el --- Code for asymmetric sync with a mobile device
2 ;; Copyright (C) 2009-2013 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-allpriorities "A B C"
80 "Default set of priority cookies for the index file."
81 :version "24.4"
82 :package-version '(Org . "8.0")
83 :type 'string
84 :group 'org-mobile)
86 (defcustom org-mobile-use-encryption nil
87 "Non-nil means keep only encrypted files on the WebDAV server.
88 Encryption uses AES-256, with a password given in
89 `org-mobile-encryption-password'.
90 When nil, plain files are kept on the server.
91 Turning on encryption requires to set the same password in the MobileOrg
92 application. Before turning this on, check of MobileOrg does already
93 support it - at the time of this writing it did not yet."
94 :group 'org-mobile
95 :version "24.1"
96 :type 'boolean)
98 (defcustom org-mobile-encryption-tempfile "~/orgtmpcrypt"
99 "File that is being used as a temporary file for encryption.
100 This must be local file on your local machine (not on the WebDAV server).
101 You might want to put this file into a directory where only you have access."
102 :group 'org-mobile
103 :version "24.1"
104 :type 'directory)
106 (defcustom org-mobile-encryption-password ""
107 "Password for encrypting files uploaded to the server.
108 This is a single password which is used for AES-256 encryption. The same
109 password must also be set in the MobileOrg application. All Org files,
110 including mobileorg.org will be encrypted using this password.
112 SECURITY CONSIDERATIONS:
114 Note that, when Org runs the encryption commands, the password could
115 be visible briefly on your system with the `ps' command. So this method is
116 only intended to keep the files secure on the server, not on your own machine.
118 Also, if you set this variable in an init file (.emacs or .emacs.d/init.el
119 or custom.el...) and if that file is stored in a way so that other can read
120 it, this also limits the security of this approach. You can also leave
121 this variable empty - Org will then ask for the password once per Emacs
122 session."
123 :group 'org-mobile
124 :version "24.1"
125 :type '(string :tag "Password"))
127 (defvar org-mobile-encryption-password-session nil)
129 (defun org-mobile-encryption-password ()
130 (or (org-string-nw-p org-mobile-encryption-password)
131 (org-string-nw-p org-mobile-encryption-password-session)
132 (setq org-mobile-encryption-password-session
133 (read-passwd "Password for MobileOrg: " t))))
135 (defcustom org-mobile-inbox-for-pull "~/org/from-mobile.org"
136 "The file where captured notes and flags will be appended to.
137 During the execution of `org-mobile-pull', the file
138 `org-mobile-capture-file' will be emptied it's contents have
139 been appended to the file given here. This file should be in
140 `org-directory', and not in the staging area or on the web server."
141 :group 'org-mobile
142 :type 'file)
144 (defconst org-mobile-capture-file "mobileorg.org"
145 "The capture file where the mobile stores captured notes and flags.
146 This should not be changed, because MobileOrg assumes this name.")
148 (defcustom org-mobile-index-file "index.org"
149 "The index file with links to all Org files that should be loaded by MobileOrg.
150 Relative to `org-mobile-directory'. The Address field in the MobileOrg setup
151 should point to this file."
152 :group 'org-mobile
153 :type 'file)
155 (defcustom org-mobile-agendas 'all
156 "The agendas that should be pushed to MobileOrg.
157 Allowed values:
159 default the weekly agenda and the global TODO list
160 custom all custom agendas defined by the user
161 all the custom agendas and the default ones
162 list a list of selection key(s) as string."
163 :group 'org-mobile
164 :version "24.1"
165 :type '(choice
166 (const :tag "Default Agendas" default)
167 (const :tag "Custom Agendas" custom)
168 (const :tag "Default and Custom Agendas" all)
169 (repeat :tag "Selected"
170 (string :tag "Selection Keys"))))
172 (defcustom org-mobile-force-id-on-agenda-items t
173 "Non-nil means make all agenda items carry an ID."
174 :group 'org-mobile
175 :type 'boolean)
177 (defcustom org-mobile-force-mobile-change nil
178 "Non-nil means force the change made on the mobile device.
179 So even if there have been changes to the computer version of the entry,
180 force the new value set on the mobile.
181 When nil, mark the entry from the mobile with an error message.
182 Instead of nil or t, this variable can also be a list of symbols, indicating
183 the editing types for which the mobile version should always dominate."
184 :group 'org-mobile
185 :type '(choice
186 (const :tag "Always" t)
187 (const :tag "Never" nil)
188 (set :greedy t :tag "Specify"
189 (const todo)
190 (const tags)
191 (const priority)
192 (const heading)
193 (const body))))
195 (defcustom org-mobile-action-alist
196 '(("edit" . (org-mobile-edit data old new)))
197 "Alist with flags and actions for mobile sync.
198 When flagging an entry, MobileOrg will create entries that look like
200 * F(action:data) [[id:entry-id][entry title]]
202 This alist defines that the ACTION in the parentheses of F() should mean,
203 i.e. what action should be taken. The :data part in the parenthesis is
204 optional. If present, the string after the colon will be passed to the
205 action form as the `data' variable.
206 The car of each elements of the alist is an actions string. The cdr is
207 an Emacs Lisp form that will be evaluated with the cursor on the headline
208 of that entry.
210 For now, it is not recommended to change this variable."
211 :group 'org-mobile
212 :type '(repeat
213 (cons (string :tag "Action flag")
214 (sexp :tag "Action form"))))
216 (defcustom org-mobile-checksum-binary (or (executable-find "shasum")
217 (executable-find "sha1sum")
218 (executable-find "md5sum")
219 (executable-find "md5"))
220 "Executable used for computing checksums of agenda files."
221 :group 'org-mobile
222 :type 'string)
224 (defvar org-mobile-pre-push-hook nil
225 "Hook run before running `org-mobile-push'.
226 This could be used to clean up `org-mobile-directory', for example to
227 remove files that used to be included in the agenda but no longer are.
228 The presence of such files would not really be a problem, but after time
229 they may accumulate.")
231 (defvar org-mobile-post-push-hook nil
232 "Hook run after running `org-mobile-push'.
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 all files from the
235 local staging directory `org-mobile-directory' to the WebDAV directory,
236 for example using `rsync' or `scp'.")
238 (defvar org-mobile-pre-pull-hook nil
239 "Hook run before executing `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 capture file
242 `mobileorg.org' from the WebDAV location to the local staging
243 directory `org-mobile-directory'.")
245 (defvar org-mobile-post-pull-hook nil
246 "Hook run after running `org-mobile-pull', only if new items were found.
247 If Emacs does not have direct write access to the WebDAV directory used
248 by the mobile device, this hook should be used to copy the emptied
249 capture file `mobileorg.org' back to the WebDAV directory, for example
250 using `rsync' or `scp'.")
252 (defvar org-mobile-last-flagged-files nil
253 "List of files containing entries flagged in the latest pull.")
255 (defvar org-mobile-files-alist nil)
256 (defvar org-mobile-checksum-files nil)
258 (defun org-mobile-prepare-file-lists ()
259 (setq org-mobile-files-alist (org-mobile-files-alist))
260 (setq org-mobile-checksum-files nil))
262 (defun org-mobile-files-alist ()
263 "Expand the list in `org-mobile-files' to a list of existing files.
264 Also exclude files matching `org-mobile-files-exclude-regexp'."
265 (let* ((include-archives
266 (and (member 'org-agenda-text-search-extra-files org-mobile-files)
267 (member 'agenda-archives org-agenda-text-search-extra-files)
269 (files
270 (apply 'append
271 (mapcar
272 (lambda (f)
273 (cond
274 ((eq f 'org-agenda-files)
275 (org-agenda-files t include-archives))
276 ((eq f 'org-agenda-text-search-extra-files)
277 (delq 'agenda-archives
278 (copy-sequence
279 org-agenda-text-search-extra-files)))
280 ((and (stringp f) (file-directory-p f))
281 (directory-files f 'full "\\.org\\'"))
282 ((and (stringp f) (file-exists-p f))
283 (list f))
284 (t nil)))
285 org-mobile-files)))
286 (files (delq
288 (mapcar (lambda (f)
289 (unless (and (not (string= org-mobile-files-exclude-regexp ""))
290 (string-match org-mobile-files-exclude-regexp f))
291 (identity f)))
292 files)))
293 (orgdir-uname (file-name-as-directory (file-truename org-directory)))
294 (orgdir-re (concat "\\`" (regexp-quote orgdir-uname)))
295 uname seen rtn file link-name)
296 ;; Make the files unique, and determine the name under which they will
297 ;; be listed.
298 (while (setq file (pop files))
299 (if (not (file-name-absolute-p file))
300 (setq file (expand-file-name file org-directory)))
301 (setq uname (file-truename file))
302 (unless (member uname seen)
303 (push uname seen)
304 (if (string-match orgdir-re uname)
305 (setq link-name (substring uname (match-end 0)))
306 (setq link-name (file-name-nondirectory uname)))
307 (push (cons file link-name) rtn)))
308 (nreverse rtn)))
310 ;;;###autoload
311 (defun org-mobile-push ()
312 "Push the current state of Org affairs to the target directory.
313 This will create the index file, copy all agenda files there, and also
314 create all custom agenda views, for upload to the mobile phone."
315 (interactive)
316 (let ((a-buffer (get-buffer org-agenda-buffer-name)))
317 (let ((org-agenda-curbuf-name org-agenda-buffer-name)
318 (org-agenda-buffer-name "*SUMO*")
319 (org-agenda-tag-filter org-agenda-tag-filter)
320 (org-agenda-redo-command org-agenda-redo-command))
321 (save-excursion
322 (save-restriction
323 (save-window-excursion
324 (run-hooks 'org-mobile-pre-push-hook)
325 (org-mobile-check-setup)
326 (org-mobile-prepare-file-lists)
327 (message "Creating agendas...")
328 (let ((inhibit-redisplay t)
329 (org-agenda-files (mapcar 'car org-mobile-files-alist)))
330 (org-mobile-create-sumo-agenda))
331 (message "Creating agendas...done")
332 (org-save-all-org-buffers) ; to save any IDs created by this process
333 (message "Copying files...")
334 (org-mobile-copy-agenda-files)
335 (message "Writing index file...")
336 (org-mobile-create-index-file)
337 (message "Writing checksums...")
338 (org-mobile-write-checksums)
339 (run-hooks 'org-mobile-post-push-hook))))
340 (setq org-agenda-buffer-name org-agenda-curbuf-name
341 org-agenda-this-buffer-name org-agenda-curbuf-name))
342 (redraw-display)
343 (when (buffer-live-p a-buffer)
344 (if (not (get-buffer-window a-buffer))
345 (kill-buffer a-buffer)
346 (let ((cw (selected-window)))
347 (select-window (get-buffer-window a-buffer))
348 (org-agenda-redo)
349 (select-window cw)))))
350 (message "Files for mobile viewer staged"))
352 (defvar org-mobile-before-process-capture-hook nil
353 "Hook that is run after content was moved to `org-mobile-inbox-for-pull'.
354 The inbox file is visited by the current buffer, and the buffer is
355 narrowed to the newly captured data.")
357 ;;;###autoload
358 (defun org-mobile-pull ()
359 "Pull the contents of `org-mobile-capture-file' and integrate them.
360 Apply all flagged actions, flag entries to be flagged and then call an
361 agenda view showing the flagged items."
362 (interactive)
363 (org-mobile-check-setup)
364 (run-hooks 'org-mobile-pre-pull-hook)
365 (let ((insertion-marker (org-mobile-move-capture)))
366 (if (not (markerp insertion-marker))
367 (message "No new items")
368 (org-with-point-at insertion-marker
369 (save-restriction
370 (narrow-to-region (point) (point-max))
371 (run-hooks 'org-mobile-before-process-capture-hook)))
372 (org-with-point-at insertion-marker
373 (org-mobile-apply (point) (point-max)))
374 (move-marker insertion-marker nil)
375 (run-hooks 'org-mobile-post-pull-hook)
376 (when org-mobile-last-flagged-files
377 ;; Make an agenda view of flagged entries, but only in the files
378 ;; where stuff has been added.
379 (put 'org-agenda-files 'org-restrict org-mobile-last-flagged-files)
380 (let ((org-agenda-keep-restricted-file-list t))
381 (org-agenda nil "?"))))))
383 (defun org-mobile-check-setup ()
384 "Check if org-mobile-directory has been set up."
385 (org-mobile-cleanup-encryption-tempfile)
386 (unless (and org-directory
387 (stringp org-directory)
388 (string-match "\\S-" org-directory)
389 (file-exists-p org-directory)
390 (file-directory-p org-directory))
391 (error
392 "Please set `org-directory' to the directory where your org files live"))
393 (unless (and org-mobile-directory
394 (stringp org-mobile-directory)
395 (string-match "\\S-" org-mobile-directory)
396 (file-exists-p org-mobile-directory)
397 (file-directory-p org-mobile-directory))
398 (error
399 "Variable `org-mobile-directory' must point to an existing directory"))
400 (unless (and org-mobile-inbox-for-pull
401 (stringp org-mobile-inbox-for-pull)
402 (string-match "\\S-" org-mobile-inbox-for-pull)
403 (file-exists-p
404 (file-name-directory org-mobile-inbox-for-pull)))
405 (error
406 "Variable `org-mobile-inbox-for-pull' must point to a file in an existing directory"))
407 (unless (and org-mobile-checksum-binary
408 (string-match "\\S-" org-mobile-checksum-binary))
409 (error "No executable found to compute checksums"))
410 (when org-mobile-use-encryption
411 (unless (string-match "\\S-" (org-mobile-encryption-password))
412 (error
413 "To use encryption, you must set `org-mobile-encryption-password'"))
414 (unless (file-writable-p org-mobile-encryption-tempfile)
415 (error "Cannot write to encryption tempfile %s"
416 org-mobile-encryption-tempfile))
417 (unless (executable-find "openssl")
418 (error "OpenSSL is needed to encrypt files"))))
420 (defun org-mobile-create-index-file ()
421 "Write the index file in the WebDAV directory."
422 (let ((files-alist (sort (copy-sequence org-mobile-files-alist)
423 (lambda (a b) (string< (cdr a) (cdr b)))))
424 (def-todo (default-value 'org-todo-keywords))
425 (def-tags (default-value 'org-tag-alist))
426 (target-file (expand-file-name org-mobile-index-file
427 org-mobile-directory))
428 file link-name todo-kwds done-kwds tags entry kwds dwds twds)
429 (when (stringp (car def-todo))
430 (setq def-todo (list (cons 'sequence def-todo))))
431 (org-agenda-prepare-buffers (mapcar 'car files-alist))
432 (setq done-kwds (org-uniquify org-done-keywords-for-agenda))
433 (setq todo-kwds (org-delete-all
434 done-kwds
435 (org-uniquify org-todo-keywords-for-agenda)))
436 (setq tags (mapcar 'car (org-global-tags-completion-table
437 (mapcar 'car files-alist))))
438 (with-temp-file
439 (if org-mobile-use-encryption
440 org-mobile-encryption-tempfile
441 target-file)
442 (while (setq entry (pop def-todo))
443 (insert "#+READONLY\n")
444 (setq kwds (mapcar (lambda (x) (if (string-match "(" x)
445 (substring x 0 (match-beginning 0))
447 (cdr entry)))
448 (insert "#+TODO: " (mapconcat 'identity kwds " ") "\n")
449 (setq dwds (member "|" kwds)
450 twds (org-delete-all dwds kwds)
451 todo-kwds (org-delete-all twds todo-kwds)
452 done-kwds (org-delete-all dwds done-kwds)))
453 (when (or todo-kwds done-kwds)
454 (insert "#+TODO: " (mapconcat 'identity todo-kwds " ") " | "
455 (mapconcat 'identity done-kwds " ") "\n"))
456 (setq def-tags (mapcar
457 (lambda (x)
458 (cond ((null x) nil)
459 ((stringp x) x)
460 ((eq (car x) :startgroup) "{")
461 ((eq (car x) :endgroup) "}")
462 ((eq (car x) :newline) nil)
463 ((listp x) (car x))))
464 def-tags))
465 (setq def-tags (delq nil def-tags))
466 (setq tags (org-delete-all def-tags tags))
467 (setq tags (sort tags (lambda (a b) (string< (downcase a) (downcase b)))))
468 (setq tags (append def-tags tags nil))
469 (insert "#+TAGS: " (mapconcat 'identity tags " ") "\n")
470 (insert "#+ALLPRIORITIES: " org-mobile-allpriorities "\n")
471 (when (file-exists-p (expand-file-name
472 org-mobile-directory "agendas.org"))
473 (insert "* [[file:agendas.org][Agenda Views]]\n"))
474 (while (setq entry (pop files-alist))
475 (setq file (car entry)
476 link-name (cdr entry))
477 (insert (format "* [[file:%s][%s]]\n"
478 link-name link-name)))
479 (push (cons org-mobile-index-file (md5 (buffer-string)))
480 org-mobile-checksum-files))
481 (when org-mobile-use-encryption
482 (org-mobile-encrypt-and-move org-mobile-encryption-tempfile
483 target-file)
484 (org-mobile-cleanup-encryption-tempfile))))
486 (defun org-mobile-copy-agenda-files ()
487 "Copy all agenda files to the stage or WebDAV directory."
488 (let ((files-alist org-mobile-files-alist)
489 file buf entry link-name target-path target-dir check)
490 (while (setq entry (pop files-alist))
491 (setq file (car entry) link-name (cdr entry))
492 (when (file-exists-p file)
493 (setq target-path (expand-file-name link-name org-mobile-directory)
494 target-dir (file-name-directory target-path))
495 (unless (file-directory-p target-dir)
496 (make-directory target-dir 'parents))
497 (if org-mobile-use-encryption
498 (org-mobile-encrypt-and-move file target-path)
499 (copy-file file target-path 'ok-if-exists))
500 (setq check (shell-command-to-string
501 (concat org-mobile-checksum-binary " "
502 (shell-quote-argument (expand-file-name file)))))
503 (when (string-match "[a-fA-F0-9]\\{30,40\\}" check)
504 (push (cons link-name (match-string 0 check))
505 org-mobile-checksum-files))))
507 (setq file (expand-file-name org-mobile-capture-file
508 org-mobile-directory))
509 (save-excursion
510 (setq buf (find-file file))
511 (when (and (= (point-min) (point-max)))
512 (insert "\n")
513 (save-buffer)
514 (when org-mobile-use-encryption
515 (write-file org-mobile-encryption-tempfile)
516 (org-mobile-encrypt-and-move org-mobile-encryption-tempfile file)))
517 (push (cons org-mobile-capture-file (md5 (buffer-string)))
518 org-mobile-checksum-files))
519 (org-mobile-cleanup-encryption-tempfile)
520 (kill-buffer buf)))
522 (defun org-mobile-write-checksums ()
523 "Create checksums for all files in `org-mobile-directory'.
524 The table of checksums is written to the file mobile-checksums."
525 (let ((sumfile (expand-file-name "checksums.dat" org-mobile-directory))
526 (files org-mobile-checksum-files)
527 entry file sum)
528 (with-temp-file sumfile
529 (set-buffer-file-coding-system 'undecided-unix nil)
530 (while (setq entry (pop files))
531 (setq file (car entry) sum (cdr entry))
532 (insert (format "%s %s\n" sum file))))))
534 (defun org-mobile-sumo-agenda-command ()
535 "Return an agenda custom command that comprises all custom commands."
536 (let ((custom-list
537 ;; normalize different versions
538 (delq nil
539 (mapcar
540 (lambda (x)
541 (cond ((stringp (cdr x)) nil)
542 ((stringp (nth 1 x)) x)
543 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
544 (t (cons (car x) (cons "" (cdr x))))))
545 org-agenda-custom-commands)))
546 (default-list '(("a" "Agenda" agenda) ("t" "All TODO" alltodo)))
547 thelist atitle new e key desc type match settings cmds gkey gdesc gsettings cnt)
548 (cond
549 ((eq org-mobile-agendas 'custom)
550 (setq thelist custom-list))
551 ((eq org-mobile-agendas 'default)
552 (setq thelist default-list))
553 ((eq org-mobile-agendas 'all)
554 (setq thelist custom-list)
555 (unless (assoc "t" thelist) (push '("t" "ALL TODO" alltodo) thelist))
556 (unless (assoc "a" thelist) (push '("a" "Agenda" agenda) thelist)))
557 ((listp org-mobile-agendas)
558 (setq thelist (append custom-list default-list))
559 (setq thelist (delq nil (mapcar (lambda (k) (assoc k thelist))
560 org-mobile-agendas)))))
561 (while (setq e (pop thelist))
562 (cond
563 ((stringp (cdr e))
564 ;; this is a description entry - skip it
566 ((eq (nth 2 e) 'search)
567 ;; Search view is interactive, skip
569 ((memq (nth 2 e) '(todo-tree tags-tree occur-tree))
570 ;; These are trees, not really agenda commands
572 ((and (memq (nth 2 e) '(todo tags tags-todo))
573 (or (null (nth 3 e))
574 (not (string-match "\\S-" (nth 3 e)))))
575 ;; These would be interactive because the match string is empty
577 ((memq (nth 2 e) '(agenda alltodo todo tags tags-todo))
578 ;; a normal command
579 (setq key (car e) desc (nth 1 e) type (nth 2 e) match (nth 3 e)
580 settings (nth 4 e))
581 (setq settings
582 (cons (list 'org-agenda-title-append
583 (concat "<after>KEYS=" key " TITLE: "
584 (if (and (stringp desc) (> (length desc) 0))
585 desc (symbol-name type))
586 "</after>"))
587 settings))
588 (push (list type match settings) new))
589 ((or (functionp (nth 2 e)) (symbolp (nth 2 e)))
590 ;; A user-defined function, which can do anything, so simply
591 ;; ignore it.
594 ;; a block agenda
595 (setq gkey (car e) gdesc (nth 1 e) gsettings (nth 3 e) cmds (nth 2 e))
596 (setq cnt 0)
597 (while (setq e (pop cmds))
598 (setq type (car e) match (nth 1 e) settings (nth 2 e))
599 (setq atitle (if (string= "" gdesc) match gdesc))
600 (setq settings (append gsettings settings))
601 (setq settings
602 (cons (list 'org-agenda-title-append
603 (concat "<after>KEYS=" gkey "#" (number-to-string
604 (setq cnt (1+ cnt)))
605 " TITLE: " atitle "</after>"))
606 settings))
607 (push (list type match settings) new)))))
608 (and new (list "X" "SUMO" (reverse new)
609 '((org-agenda-compact-blocks nil))))))
611 (defvar org-mobile-creating-agendas nil)
612 (defun org-mobile-write-agenda-for-mobile (file)
613 (let ((all (buffer-string)) in-date id pl prefix line app short m sexp)
614 (with-temp-file file
615 (org-mode)
616 (insert "#+READONLY\n")
617 (insert all)
618 (goto-char (point-min))
619 (while (not (eobp))
620 (cond
621 ((looking-at "[ \t]*$")) ; keep empty lines
622 ((looking-at "=+$")
623 ;; remove underlining
624 (delete-region (point) (point-at-eol)))
625 ((get-text-property (point) 'org-agenda-structural-header)
626 (setq in-date nil)
627 (setq app (get-text-property (point) 'org-agenda-title-append))
628 (setq short (get-text-property (point) 'short-heading))
629 (when (and short (looking-at ".+"))
630 (replace-match short nil t)
631 (beginning-of-line 1))
632 (when app
633 (end-of-line 1)
634 (insert app)
635 (beginning-of-line 1))
636 (insert "* "))
637 ((get-text-property (point) 'org-agenda-date-header)
638 (setq in-date t)
639 (insert "** "))
640 ((setq m (or (get-text-property (point) 'org-hd-marker)
641 (get-text-property (point) 'org-marker)))
642 (setq sexp (member (get-text-property (point) 'type)
643 '("diary" "sexp")))
644 (if (setq pl (text-property-any (point) (point-at-eol) 'org-heading t))
645 (progn
646 (setq prefix (org-trim (buffer-substring
647 (point) pl))
648 line (org-trim (buffer-substring
650 (point-at-eol))))
651 (delete-region (point-at-bol) (point-at-eol))
652 (insert line "<before>" prefix "</before>")
653 (beginning-of-line 1))
654 (and (looking-at "[ \t]+") (replace-match "")))
655 (insert (if in-date "*** " "** "))
656 (end-of-line 1)
657 (insert "\n")
658 (unless sexp
659 (insert (org-agenda-get-some-entry-text
660 m 10 " " 'planning)
661 "\n")
662 (when (setq id
663 (if (org-bound-and-true-p
664 org-mobile-force-id-on-agenda-items)
665 (org-id-get m 'create)
666 (or (org-entry-get m "ID")
667 (org-mobile-get-outline-path-link m))))
668 (insert " :PROPERTIES:\n :ORIGINAL_ID: " id
669 "\n :END:\n")))))
670 (beginning-of-line 2))
671 (push (cons "agendas.org" (md5 (buffer-string)))
672 org-mobile-checksum-files))
673 (message "Agenda written to Org file %s" file)))
675 (defun org-mobile-get-outline-path-link (pom)
676 (org-with-point-at pom
677 (concat "olp:"
678 (org-mobile-escape-olp (file-name-nondirectory buffer-file-name))
680 (mapconcat 'org-mobile-escape-olp
681 (org-get-outline-path)
682 "/")
684 (org-mobile-escape-olp (nth 4 (org-heading-components))))))
686 (defun org-mobile-escape-olp (s)
687 (let ((table '(?: ?/)))
688 (org-link-escape s table)))
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 ;; Find and apply the edits
833 (goto-char beg)
834 (while (re-search-forward
835 "^\\*+[ \t]+F(\\([^():\n]*\\)\\(:\\([^()\n]*\\)\\)?)[ \t]+\\[\\[\\(\\(id\\|olp\\):\\([^]\n]+\\)\\)" end t)
836 (catch 'next
837 (let* ((action (match-string 1))
838 (data (and (match-end 3) (match-string 3)))
839 (id-pos (condition-case msg
840 (org-mobile-locate-entry (match-string 4))
841 (error (nth 1 msg))))
842 (bos (point-at-bol))
843 (eos (save-excursion (org-end-of-subtree t t)))
844 (cmd (if (equal action "")
845 '(progn
846 (incf cnt-flag)
847 (org-toggle-tag "FLAGGED" 'on)
848 (and note
849 (org-entry-put nil "THEFLAGGINGNOTE" note)))
850 (incf cnt-edit)
851 (cdr (assoc action org-mobile-action-alist))))
852 (note (and (equal action "")
853 (buffer-substring (1+ (point-at-eol)) eos)))
854 (org-inhibit-logging 'note) ;; Do not take notes interactively
855 old new)
857 (goto-char bos)
858 (when (and (markerp id-pos)
859 (not (member (marker-buffer id-pos) buf-list)))
860 (org-mobile-timestamp-buffer (marker-buffer id-pos))
861 (push (marker-buffer id-pos) buf-list))
862 (unless (markerp id-pos)
863 (goto-char (+ 2 (point-at-bol)))
864 (if (stringp id-pos)
865 (insert id-pos " ")
866 (insert "BAD REFERENCE "))
867 (incf cnt-error)
868 (throw 'next t))
869 (unless cmd
870 (insert "BAD FLAG ")
871 (incf cnt-error)
872 (throw 'next t))
873 (move-marker bos-marker (point))
874 (if (re-search-forward "^** Old value[ \t]*$" eos t)
875 (setq old (buffer-substring
876 (1+ (match-end 0))
877 (progn (outline-next-heading) (point)))))
878 (if (re-search-forward "^** New value[ \t]*$" eos t)
879 (setq new (buffer-substring
880 (1+ (match-end 0))
881 (progn (outline-next-heading)
882 (if (eobp) (org-back-over-empty-lines))
883 (point)))))
884 (setq old (and old (if (string-match "\\S-" old) old nil)))
885 (setq new (and new (if (string-match "\\S-" new) new nil)))
886 (if (and note (> (length note) 0))
887 ;; Make Note into a single line, to fit into a property
888 (setq note (mapconcat 'identity
889 (org-split-string (org-trim note) "\n")
890 "\\n")))
891 (unless (equal data "body")
892 (setq new (and new (org-trim new))
893 old (and old (org-trim old))))
894 (goto-char (+ 2 bos-marker))
895 ;; Remember this place so that we can return
896 (move-marker marker (point))
897 (setq org-mobile-error nil)
898 (save-excursion
899 (condition-case msg
900 (org-with-point-at id-pos
901 (progn
902 (eval cmd)
903 (unless (member data (list "delete" "archive" "archive-sibling" "addheading"))
904 (if (member "FLAGGED" (org-get-tags))
905 (add-to-list 'org-mobile-last-flagged-files
906 (buffer-file-name (current-buffer)))))))
907 (error (setq org-mobile-error msg))))
908 (when org-mobile-error
909 (org-pop-to-buffer-same-window (marker-buffer marker))
910 (goto-char marker)
911 (incf cnt-error)
912 (insert (if (stringp (nth 1 org-mobile-error))
913 (nth 1 org-mobile-error)
914 "EXECUTION FAILED")
915 " ")
916 (throw 'next t))
917 ;; If we get here, the action has been applied successfully
918 ;; So remove the entry
919 (goto-char bos-marker)
920 (delete-region (point) (org-end-of-subtree t t)))))
921 (save-buffer)
922 (move-marker marker nil)
923 (move-marker end nil)
924 (message "%d new, %d edits, %d flags, %d errors" cnt-new
925 cnt-edit cnt-flag cnt-error)
926 (sit-for 1)))
928 (defun org-mobile-timestamp-buffer (buf)
929 "Time stamp buffer BUF, just to make sure its checksum will change."
930 (with-current-buffer buf
931 (save-excursion
932 (save-restriction
933 (widen)
934 (goto-char (point-min))
935 (if (re-search-forward
936 "^\\([ \t]*\\)#\\+LAST_MOBILE_CHANGE:.*\n?" nil t)
937 (progn
938 (goto-char (match-end 1))
939 (delete-region (point) (match-end 0)))
940 (if (looking-at ".*?-\\*-.*-\\*-")
941 (forward-line 1)))
942 (insert "#+LAST_MOBILE_CHANGE: "
943 (format-time-string "%Y-%m-%d %T") "\n")))))
945 (defun org-mobile-smart-read ()
946 "Parse the entry at point for shortcuts and expand them.
947 These shortcuts are meant for fast and easy typing on the limited
948 keyboards of a mobile device. Below we show a list of the shortcuts
949 currently implemented.
951 The entry is expected to contain an inactive time stamp indicating when
952 the entry was created. When setting dates and
953 times (for example for deadlines), the time strings are interpreted
954 relative to that creation date.
955 Abbreviations are expected to take up entire lines, just because it is so
956 easy to type RET on a mobile device. Abbreviations start with one or two
957 letters, followed immediately by a dot and then additional information.
958 Generally the entire shortcut line is removed after action have been taken.
959 Time stamps will be constructed using `org-read-date'. So for example a
960 line \"dd. 2tue\" will set a deadline on the second Tuesday after the
961 creation date.
963 Here are the shortcuts currently implemented:
965 dd. string set deadline
966 ss. string set scheduling
967 tt. string set time tamp, here.
968 ti. string set inactive time
970 tg. tag1 tag2 tag3 set all these tags, change case where necessary
971 td. kwd set this todo keyword, change case where necessary
973 FIXME: Hmmm, not sure if we can make his work against the
974 auto-correction feature. Needs a bit more thinking. So this function
975 is currently a noop.")
977 (defun org-mobile-locate-entry (link)
978 (if (string-match "\\`id:\\(.*\\)$" link)
979 (org-id-find (match-string 1 link) 'marker)
980 (if (not (string-match "\\`olp:\\(.*?\\):\\(.*\\)$" link))
981 ; not found with path, but maybe it is to be inserted
982 ; in top level of the file?
983 (if (not (string-match "\\`olp:\\(.*?\\)$" link))
985 (let ((file (match-string 1 link)))
986 (setq file (org-link-unescape file))
987 (setq file (expand-file-name file org-directory))
988 (save-excursion
989 (find-file file)
990 (goto-char (point-max))
991 (newline)
992 (goto-char (point-max))
993 (point-marker))))
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 level)
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 'addheading)
1068 (if (org-at-heading-p) ; if false we are in top-level of file
1069 (progn
1070 ;; Workaround a `org-insert-heading-respect-content' bug
1071 ;; which prevents correct insertion when point is invisible
1072 (org-show-subtree)
1073 (end-of-line 1)
1074 (org-insert-heading-respect-content '(16) t)
1075 (org-demote))
1076 (beginning-of-line)
1077 (insert "* "))
1078 (insert new))
1080 ((eq what 'refile)
1081 (org-copy-subtree)
1082 (org-with-point-at (org-mobile-locate-entry new)
1083 (if (org-at-heading-p) ; if false we are in top-level of file
1084 (progn
1085 (setq level (org-get-valid-level (funcall outline-level) 1))
1086 (org-end-of-subtree t t)
1087 (org-paste-subtree level))
1088 (org-paste-subtree 1)))
1089 (org-cut-subtree))
1091 ((eq what 'delete)
1092 (org-cut-subtree))
1094 ((eq what 'archive)
1095 (org-archive-subtree))
1097 ((eq what 'archive-sibling)
1098 (org-archive-to-archive-sibling))
1100 ((eq what 'body)
1101 (setq current (buffer-substring (min (1+ (point-at-eol)) (point-max))
1102 (save-excursion (outline-next-heading)
1103 (point))))
1104 (if (not (string-match "\\S-" current)) (setq current nil))
1105 (cond
1106 ((org-mobile-bodies-same-p current new) t) ; no action necessary
1107 ((or (org-mobile-bodies-same-p current old)
1108 (eq org-mobile-force-mobile-change t)
1109 (memq 'body org-mobile-force-mobile-change))
1110 (save-excursion
1111 (end-of-line 1)
1112 (insert "\n" new)
1113 (or (bolp) (insert "\n"))
1114 (delete-region (point) (progn (org-back-to-heading t)
1115 (outline-next-heading)
1116 (point))))
1118 (t (error "Body was changed in MobileOrg and on the computer")))))))
1120 (defun org-mobile-tags-same-p (list1 list2)
1121 "Are the two tag lists the same?"
1122 (not (or (org-delete-all list1 list2)
1123 (org-delete-all list2 list1))))
1125 (defun org-mobile-bodies-same-p (a b)
1126 "Compare if A and B are visually equal strings.
1127 We first remove leading and trailing white space from the entire strings.
1128 Then we split the strings into lines and remove leading/trailing whitespace
1129 from each line. Then we compare.
1130 A and B must be strings or nil."
1131 (cond
1132 ((and (not a) (not b)) t)
1133 ((or (not a) (not b)) nil)
1134 (t (setq a (org-trim a) b (org-trim b))
1135 (setq a (mapconcat 'identity (org-split-string a "[ \t]*\n[ \t]*") "\n"))
1136 (setq b (mapconcat 'identity (org-split-string b "[ \t]*\n[ \t]*") "\n"))
1137 (equal a b))))
1139 (provide 'org-mobile)
1141 ;; Local variables:
1142 ;; generated-autoload-file: "org-loaddefs.el"
1143 ;; End:
1145 ;;; org-mobile.el ends here