Merge branch 'maint-agenda-optimization' into maint
[org-mode.git] / lisp / org-mobile.el
blobd2e7c217f761c4c75c4e9370a440cda4d3a9d62c
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-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 target 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-curbuf-name org-agenda-buffer-name)
313 (org-agenda-buffer-name "*SUMO*")
314 (org-agenda-tag-filter org-agenda-tag-filter)
315 (org-agenda-redo-command org-agenda-redo-command))
316 (save-excursion
317 (save-window-excursion
318 (run-hooks 'org-mobile-pre-push-hook)
319 (org-mobile-check-setup)
320 (org-mobile-prepare-file-lists)
321 (message "Creating agendas...")
322 (let ((inhibit-redisplay t)
323 (org-agenda-files (mapcar 'car org-mobile-files-alist)))
324 (org-mobile-create-sumo-agenda))
325 (message "Creating agendas...done")
326 (org-save-all-org-buffers) ; to save any IDs created by this process
327 (message "Copying files...")
328 (org-mobile-copy-agenda-files)
329 (message "Writing index file...")
330 (org-mobile-create-index-file)
331 (message "Writing checksums...")
332 (org-mobile-write-checksums)
333 (run-hooks 'org-mobile-post-push-hook)))
334 (setq org-agenda-buffer-name org-agenda-curbuf-name
335 org-agenda-this-buffer-name org-agenda-curbuf-name))
336 (redraw-display)
337 (when (buffer-live-p a-buffer)
338 (if (not (get-buffer-window a-buffer))
339 (kill-buffer a-buffer)
340 (let ((cw (selected-window)))
341 (select-window (get-buffer-window a-buffer))
342 (org-agenda-redo)
343 (select-window cw)))))
344 (message "Files for mobile viewer staged"))
346 (defvar org-mobile-before-process-capture-hook nil
347 "Hook that is run after content was moved to `org-mobile-inbox-for-pull'.
348 The inbox file is visited by the current buffer, and the buffer is
349 narrowed to the newly captured data.")
351 ;;;###autoload
352 (defun org-mobile-pull ()
353 "Pull the contents of `org-mobile-capture-file' and integrate them.
354 Apply all flagged actions, flag entries to be flagged and then call an
355 agenda view showing the flagged items."
356 (interactive)
357 (org-mobile-check-setup)
358 (run-hooks 'org-mobile-pre-pull-hook)
359 (let ((insertion-marker (org-mobile-move-capture)))
360 (if (not (markerp insertion-marker))
361 (message "No new items")
362 (org-with-point-at insertion-marker
363 (save-restriction
364 (narrow-to-region (point) (point-max))
365 (run-hooks 'org-mobile-before-process-capture-hook)))
366 (org-with-point-at insertion-marker
367 (org-mobile-apply (point) (point-max)))
368 (move-marker insertion-marker nil)
369 (run-hooks 'org-mobile-post-pull-hook)
370 (when org-mobile-last-flagged-files
371 ;; Make an agenda view of flagged entries, but only in the files
372 ;; where stuff has been added.
373 (put 'org-agenda-files 'org-restrict org-mobile-last-flagged-files)
374 (let ((org-agenda-keep-restricted-file-list t))
375 (org-agenda nil "?"))))))
377 (defun org-mobile-check-setup ()
378 "Check if org-mobile-directory has been set up."
379 (org-mobile-cleanup-encryption-tempfile)
380 (unless (and org-directory
381 (stringp org-directory)
382 (string-match "\\S-" org-directory)
383 (file-exists-p org-directory)
384 (file-directory-p org-directory))
385 (error
386 "Please set `org-directory' to the directory where your org files live"))
387 (unless (and org-mobile-directory
388 (stringp org-mobile-directory)
389 (string-match "\\S-" org-mobile-directory)
390 (file-exists-p org-mobile-directory)
391 (file-directory-p org-mobile-directory))
392 (error
393 "Variable `org-mobile-directory' must point to an existing directory"))
394 (unless (and org-mobile-inbox-for-pull
395 (stringp org-mobile-inbox-for-pull)
396 (string-match "\\S-" org-mobile-inbox-for-pull)
397 (file-exists-p
398 (file-name-directory org-mobile-inbox-for-pull)))
399 (error
400 "Variable `org-mobile-inbox-for-pull' must point to a file in an existing directory"))
401 (unless (and org-mobile-checksum-binary
402 (string-match "\\S-" org-mobile-checksum-binary))
403 (error "No executable found to compute checksums"))
404 (when org-mobile-use-encryption
405 (unless (string-match "\\S-" (org-mobile-encryption-password))
406 (error
407 "To use encryption, you must set `org-mobile-encryption-password'"))
408 (unless (file-writable-p org-mobile-encryption-tempfile)
409 (error "Cannot write to encryption tempfile %s"
410 org-mobile-encryption-tempfile))
411 (unless (executable-find "openssl")
412 (error "OpenSSL is needed to encrypt files"))))
414 (defun org-mobile-create-index-file ()
415 "Write the index file in the WebDAV directory."
416 (let ((files-alist (sort (copy-sequence org-mobile-files-alist)
417 (lambda (a b) (string< (cdr a) (cdr b)))))
418 (def-todo (default-value 'org-todo-keywords))
419 (def-tags (default-value 'org-tag-alist))
420 (target-file (expand-file-name org-mobile-index-file
421 org-mobile-directory))
422 file link-name todo-kwds done-kwds tags drawers entry kwds dwds twds)
423 (when (stringp (car def-todo))
424 (setq def-todo (list (cons 'sequence def-todo))))
425 (org-agenda-prepare-buffers (mapcar 'car files-alist))
426 (setq done-kwds (org-uniquify org-done-keywords-for-agenda))
427 (setq todo-kwds (org-delete-all
428 done-kwds
429 (org-uniquify org-todo-keywords-for-agenda)))
430 (setq drawers (org-uniquify org-drawers-for-agenda))
431 (setq tags (mapcar 'car (org-global-tags-completion-table
432 (mapcar 'car files-alist))))
433 (with-temp-file
434 (if org-mobile-use-encryption
435 org-mobile-encryption-tempfile
436 target-file)
437 (while (setq entry (pop def-todo))
438 (insert "#+READONLY\n")
439 (setq kwds (mapcar (lambda (x) (if (string-match "(" x)
440 (substring x 0 (match-beginning 0))
442 (cdr entry)))
443 (insert "#+TODO: " (mapconcat 'identity kwds " ") "\n")
444 (setq dwds (member "|" kwds)
445 twds (org-delete-all dwds kwds)
446 todo-kwds (org-delete-all twds todo-kwds)
447 done-kwds (org-delete-all dwds done-kwds)))
448 (when (or todo-kwds done-kwds)
449 (insert "#+TODO: " (mapconcat 'identity todo-kwds " ") " | "
450 (mapconcat 'identity done-kwds " ") "\n"))
451 (setq def-tags (mapcar
452 (lambda (x)
453 (cond ((null x) nil)
454 ((stringp x) x)
455 ((eq (car x) :startgroup) "{")
456 ((eq (car x) :endgroup) "}")
457 ((eq (car x) :newline) nil)
458 ((listp x) (car x))))
459 def-tags))
460 (setq def-tags (delq nil def-tags))
461 (setq tags (org-delete-all def-tags tags))
462 (setq tags (sort tags (lambda (a b) (string< (downcase a) (downcase b)))))
463 (setq tags (append def-tags tags nil))
464 (insert "#+TAGS: " (mapconcat 'identity tags " ") "\n")
465 (insert "#+DRAWERS: " (mapconcat 'identity drawers " ") "\n")
466 (insert "#+ALLPRIORITIES: A B C" "\n")
467 (when (file-exists-p (expand-file-name
468 org-mobile-directory "agendas.org"))
469 (insert "* [[file:agendas.org][Agenda Views]]\n"))
470 (while (setq entry (pop files-alist))
471 (setq file (car entry)
472 link-name (cdr entry))
473 (insert (format "* [[file:%s][%s]]\n"
474 link-name link-name)))
475 (push (cons org-mobile-index-file (md5 (buffer-string)))
476 org-mobile-checksum-files))
477 (when org-mobile-use-encryption
478 (org-mobile-encrypt-and-move org-mobile-encryption-tempfile
479 target-file)
480 (org-mobile-cleanup-encryption-tempfile))))
482 (defun org-mobile-copy-agenda-files ()
483 "Copy all agenda files to the stage or WebDAV directory."
484 (let ((files-alist org-mobile-files-alist)
485 file buf entry link-name target-path target-dir check)
486 (while (setq entry (pop files-alist))
487 (setq file (car entry) link-name (cdr entry))
488 (when (file-exists-p file)
489 (setq target-path (expand-file-name link-name org-mobile-directory)
490 target-dir (file-name-directory target-path))
491 (unless (file-directory-p target-dir)
492 (make-directory target-dir 'parents))
493 (if org-mobile-use-encryption
494 (org-mobile-encrypt-and-move file target-path)
495 (copy-file file target-path 'ok-if-exists))
496 (setq check (shell-command-to-string
497 (concat org-mobile-checksum-binary " "
498 (shell-quote-argument (expand-file-name file)))))
499 (when (string-match "[a-fA-F0-9]\\{30,40\\}" check)
500 (push (cons link-name (match-string 0 check))
501 org-mobile-checksum-files))))
503 (setq file (expand-file-name org-mobile-capture-file
504 org-mobile-directory))
505 (save-excursion
506 (setq buf (find-file file))
507 (when (and (= (point-min) (point-max)))
508 (insert "\n")
509 (save-buffer)
510 (when org-mobile-use-encryption
511 (write-file org-mobile-encryption-tempfile)
512 (org-mobile-encrypt-and-move org-mobile-encryption-tempfile file)))
513 (push (cons org-mobile-capture-file (md5 (buffer-string)))
514 org-mobile-checksum-files))
515 (org-mobile-cleanup-encryption-tempfile)
516 (kill-buffer buf)))
518 (defun org-mobile-write-checksums ()
519 "Create checksums for all files in `org-mobile-directory'.
520 The table of checksums is written to the file mobile-checksums."
521 (let ((sumfile (expand-file-name "checksums.dat" org-mobile-directory))
522 (files org-mobile-checksum-files)
523 entry file sum)
524 (with-temp-file sumfile
525 (set-buffer-file-coding-system 'undecided-unix nil)
526 (while (setq entry (pop files))
527 (setq file (car entry) sum (cdr entry))
528 (insert (format "%s %s\n" sum file))))))
530 (defun org-mobile-sumo-agenda-command ()
531 "Return an agenda custom command that comprises all custom commands."
532 (let ((custom-list
533 ;; normalize different versions
534 (delq nil
535 (mapcar
536 (lambda (x)
537 (cond ((stringp (cdr x)) nil)
538 ((stringp (nth 1 x)) x)
539 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
540 (t (cons (car x) (cons "" (cdr x))))))
541 org-agenda-custom-commands)))
542 (default-list '(("a" "Agenda" agenda) ("t" "All TODO" alltodo)))
543 thelist atitle new e key desc type match settings cmds gkey gdesc gsettings cnt)
544 (cond
545 ((eq org-mobile-agendas 'custom)
546 (setq thelist custom-list))
547 ((eq org-mobile-agendas 'default)
548 (setq thelist default-list))
549 ((eq org-mobile-agendas 'all)
550 (setq thelist custom-list)
551 (unless (assoc "t" thelist) (push '("t" "ALL TODO" alltodo) thelist))
552 (unless (assoc "a" thelist) (push '("a" "Agenda" agenda) thelist)))
553 ((listp org-mobile-agendas)
554 (setq thelist (append custom-list default-list))
555 (setq thelist (delq nil (mapcar (lambda (k) (assoc k thelist))
556 org-mobile-agendas)))))
557 (while (setq e (pop thelist))
558 (cond
559 ((stringp (cdr e))
560 ;; this is a description entry - skip it
562 ((eq (nth 2 e) 'search)
563 ;; Search view is interactive, skip
565 ((memq (nth 2 e) '(todo-tree tags-tree occur-tree))
566 ;; These are trees, not really agenda commands
568 ((and (memq (nth 2 e) '(todo tags tags-todo))
569 (or (null (nth 3 e))
570 (not (string-match "\\S-" (nth 3 e)))))
571 ;; These would be interactive because the match string is empty
573 ((memq (nth 2 e) '(agenda alltodo todo tags tags-todo))
574 ;; a normal command
575 (setq key (car e) desc (nth 1 e) type (nth 2 e) match (nth 3 e)
576 settings (nth 4 e))
577 (setq settings
578 (cons (list 'org-agenda-title-append
579 (concat "<after>KEYS=" key " TITLE: "
580 (if (and (stringp desc) (> (length desc) 0))
581 desc (symbol-name type))
582 "</after>"))
583 settings))
584 (push (list type match settings) new))
585 ((or (functionp (nth 2 e)) (symbolp (nth 2 e)))
586 ;; A user-defined function, which can do anything, so simply
587 ;; ignore it.
590 ;; a block agenda
591 (setq gkey (car e) gdesc (nth 1 e) gsettings (nth 3 e) cmds (nth 2 e))
592 (setq cnt 0)
593 (while (setq e (pop cmds))
594 (setq type (car e) match (nth 1 e) settings (nth 2 e))
595 (setq atitle (if (string= "" gdesc) match gdesc))
596 (setq settings (append gsettings settings))
597 (setq settings
598 (cons (list 'org-agenda-title-append
599 (concat "<after>KEYS=" gkey "#" (number-to-string
600 (setq cnt (1+ cnt)))
601 " TITLE: " atitle "</after>"))
602 settings))
603 (push (list type match settings) new)))))
604 (and new (list "X" "SUMO" (reverse new)
605 '((org-agenda-compact-blocks nil))))))
607 (defvar org-mobile-creating-agendas nil)
608 (defun org-mobile-write-agenda-for-mobile (file)
609 (let ((all (buffer-string)) in-date id pl prefix line app short m sexp)
610 (with-temp-file file
611 (org-mode)
612 (insert "#+READONLY\n")
613 (insert all)
614 (goto-char (point-min))
615 (while (not (eobp))
616 (cond
617 ((looking-at "[ \t]*$")) ; keep empty lines
618 ((looking-at "=+$")
619 ;; remove underlining
620 (delete-region (point) (point-at-eol)))
621 ((get-text-property (point) 'org-agenda-structural-header)
622 (setq in-date nil)
623 (setq app (get-text-property (point)
624 'org-agenda-title-append))
625 (setq short (get-text-property (point)
626 'short-heading))
627 (when (and short (looking-at ".+"))
628 (replace-match short)
629 (beginning-of-line 1))
630 (when app
631 (end-of-line 1)
632 (insert app)
633 (beginning-of-line 1))
634 (insert "* "))
635 ((get-text-property (point) 'org-agenda-date-header)
636 (setq in-date t)
637 (insert "** "))
638 ((setq m (or (get-text-property (point) 'org-hd-marker)
639 (get-text-property (point) 'org-marker)))
640 (setq sexp (member (get-text-property (point) 'type)
641 '("diary" "sexp")))
642 (if (setq pl (text-property-any (point) (point-at-eol) 'org-heading t))
643 (progn
644 (setq prefix (org-trim (buffer-substring
645 (point) pl))
646 line (org-trim (buffer-substring
648 (point-at-eol))))
649 (delete-region (point-at-bol) (point-at-eol))
650 (insert line "<before>" prefix "</before>")
651 (beginning-of-line 1))
652 (and (looking-at "[ \t]+") (replace-match "")))
653 (insert (if in-date "*** " "** "))
654 (end-of-line 1)
655 (insert "\n")
656 (unless sexp
657 (insert (org-agenda-get-some-entry-text
658 m 10 " " 'planning)
659 "\n")
660 (when (setq id
661 (if (org-bound-and-true-p
662 org-mobile-force-id-on-agenda-items)
663 (org-id-get m 'create)
664 (or (org-entry-get m "ID")
665 (org-mobile-get-outline-path-link m))))
666 (insert " :PROPERTIES:\n :ORIGINAL_ID: " id
667 "\n :END:\n")))))
668 (beginning-of-line 2))
669 (push (cons "agendas.org" (md5 (buffer-string)))
670 org-mobile-checksum-files))
671 (message "Agenda written to Org file %s" file)))
673 (defun org-mobile-get-outline-path-link (pom)
674 (org-with-point-at pom
675 (concat "olp:"
676 (org-mobile-escape-olp (file-name-nondirectory buffer-file-name))
678 (mapconcat 'org-mobile-escape-olp
679 (org-get-outline-path)
680 "/")
682 (org-mobile-escape-olp (nth 4 (org-heading-components))))))
684 (defun org-mobile-escape-olp (s)
685 (let ((table '(?: ?/)))
686 (org-link-escape s table)))
688 (defun org-mobile-create-sumo-agenda ()
689 "Create a file that contains all custom agenda views."
690 (interactive)
691 (let* ((file (expand-file-name "agendas.org"
692 org-mobile-directory))
693 (file1 (if org-mobile-use-encryption
694 org-mobile-encryption-tempfile
695 file))
696 (sumo (org-mobile-sumo-agenda-command))
697 (org-agenda-custom-commands
698 (list (append sumo (list (list file1)))))
699 (org-mobile-creating-agendas t))
700 (unless (file-writable-p file1)
701 (error "Cannot write to file %s" file1))
702 (when sumo
703 (org-store-agenda-views))
704 (when org-mobile-use-encryption
705 (org-mobile-encrypt-and-move file1 file)
706 (delete-file file1)
707 (org-mobile-cleanup-encryption-tempfile))))
709 (defun org-mobile-encrypt-and-move (infile outfile)
710 "Encrypt INFILE locally to INFILE_enc, then move it to OUTFILE.
711 We do this in two steps so that remote paths will work, even if the
712 encryption program does not understand them."
713 (let ((encfile (concat infile "_enc")))
714 (org-mobile-encrypt-file infile encfile)
715 (when outfile
716 (copy-file encfile outfile 'ok-if-exists)
717 (delete-file encfile))))
719 (defun org-mobile-encrypt-file (infile outfile)
720 "Encrypt INFILE to OUTFILE, using `org-mobile-encryption-password'."
721 (shell-command
722 (format "openssl enc -aes-256-cbc -salt -pass %s -in %s -out %s"
723 (shell-quote-argument (concat "pass:"
724 (org-mobile-encryption-password)))
725 (shell-quote-argument (expand-file-name infile))
726 (shell-quote-argument (expand-file-name outfile)))))
728 (defun org-mobile-decrypt-file (infile outfile)
729 "Decrypt INFILE to OUTFILE, using `org-mobile-encryption-password'."
730 (shell-command
731 (format "openssl enc -d -aes-256-cbc -salt -pass %s -in %s -out %s"
732 (shell-quote-argument (concat "pass:"
733 (org-mobile-encryption-password)))
734 (shell-quote-argument (expand-file-name infile))
735 (shell-quote-argument (expand-file-name outfile)))))
737 (defun org-mobile-cleanup-encryption-tempfile ()
738 "Remove the encryption tempfile if it exists."
739 (and (stringp org-mobile-encryption-tempfile)
740 (file-exists-p org-mobile-encryption-tempfile)
741 (delete-file org-mobile-encryption-tempfile)))
743 (defun org-mobile-move-capture ()
744 "Move the contents of the capture file to the inbox file.
745 Return a marker to the location where the new content has been added.
746 If nothing new has been added, return nil."
747 (interactive)
748 (let* ((encfile nil)
749 (capture-file (expand-file-name org-mobile-capture-file
750 org-mobile-directory))
751 (inbox-buffer (find-file-noselect org-mobile-inbox-for-pull))
752 (capture-buffer
753 (if (not org-mobile-use-encryption)
754 (find-file-noselect capture-file)
755 (org-mobile-cleanup-encryption-tempfile)
756 (setq encfile (concat org-mobile-encryption-tempfile "_enc"))
757 (copy-file capture-file encfile)
758 (org-mobile-decrypt-file encfile org-mobile-encryption-tempfile)
759 (find-file-noselect org-mobile-encryption-tempfile)))
760 (insertion-point (make-marker))
761 not-empty content)
762 (with-current-buffer capture-buffer
763 (setq content (buffer-string))
764 (setq not-empty (string-match "\\S-" content))
765 (when not-empty
766 (set-buffer inbox-buffer)
767 (widen)
768 (goto-char (point-max))
769 (or (bolp) (newline))
770 (move-marker insertion-point
771 (prog1 (point) (insert content)))
772 (save-buffer)
773 (set-buffer capture-buffer)
774 (erase-buffer)
775 (save-buffer)
776 (org-mobile-update-checksum-for-capture-file (buffer-string))))
777 (kill-buffer capture-buffer)
778 (when org-mobile-use-encryption
779 (org-mobile-encrypt-and-move org-mobile-encryption-tempfile
780 capture-file)
781 (org-mobile-cleanup-encryption-tempfile))
782 (if not-empty insertion-point)))
784 (defun org-mobile-update-checksum-for-capture-file (buffer-string)
785 "Find the checksum line and modify it to match BUFFER-STRING."
786 (let* ((file (expand-file-name "checksums.dat" org-mobile-directory))
787 (buffer (find-file-noselect file)))
788 (when buffer
789 (with-current-buffer buffer
790 (when (re-search-forward (concat "\\([0-9a-fA-F]\\{30,\\}\\).*?"
791 (regexp-quote org-mobile-capture-file)
792 "[ \t]*$") nil t)
793 (goto-char (match-beginning 1))
794 (delete-region (match-beginning 1) (match-end 1))
795 (insert (md5 buffer-string))
796 (save-buffer)))
797 (kill-buffer buffer))))
799 (defun org-mobile-apply (&optional beg end)
800 "Apply all change requests in the current buffer.
801 If BEG and END are given, only do this in that region."
802 (interactive)
803 (require 'org-archive)
804 (setq org-mobile-last-flagged-files nil)
805 (setq beg (or beg (point-min)) end (or end (point-max)))
807 ;; Remove all Note IDs
808 (goto-char beg)
809 (while (re-search-forward "^\\*\\* Note ID: [-0-9A-F]+[ \t]*\n" end t)
810 (replace-match ""))
812 ;; Find all the referenced entries, without making any changes yet
813 (let ((marker (make-marker))
814 (bos-marker (make-marker))
815 (end (move-marker (make-marker) end))
816 (cnt-new 0)
817 (cnt-edit 0)
818 (cnt-flag 0)
819 (cnt-error 0)
820 buf-list
821 id-pos org-mobile-error)
823 ;; Count the new captures
824 (goto-char beg)
825 (while (re-search-forward "^\\* \\(.*\\)" end t)
826 (and (>= (- (match-end 1) (match-beginning 1)) 2)
827 (not (equal (downcase (substring (match-string 1) 0 2)) "f("))
828 (incf cnt-new)))
830 ;; Find and apply the edits
831 (goto-char beg)
832 (while (re-search-forward
833 "^\\*+[ \t]+F(\\([^():\n]*\\)\\(:\\([^()\n]*\\)\\)?)[ \t]+\\[\\[\\(\\(id\\|olp\\):\\([^]\n]+\\)\\)" end t)
834 (catch 'next
835 (let* ((action (match-string 1))
836 (data (and (match-end 3) (match-string 3)))
837 (id-pos (condition-case msg
838 (org-mobile-locate-entry (match-string 4))
839 (error (nth 1 msg))))
840 (bos (point-at-bol))
841 (eos (save-excursion (org-end-of-subtree t t)))
842 (cmd (if (equal action "")
843 '(progn
844 (incf cnt-flag)
845 (org-toggle-tag "FLAGGED" 'on)
846 (and note
847 (org-entry-put nil "THEFLAGGINGNOTE" note)))
848 (incf cnt-edit)
849 (cdr (assoc action org-mobile-action-alist))))
850 (note (and (equal action "")
851 (buffer-substring (1+ (point-at-eol)) eos)))
852 (org-inhibit-logging 'note) ;; Do not take notes interactively
853 old new)
855 (goto-char bos)
856 (when (and (markerp id-pos)
857 (not (member (marker-buffer id-pos) buf-list)))
858 (org-mobile-timestamp-buffer (marker-buffer id-pos))
859 (push (marker-buffer id-pos) buf-list))
860 (unless (markerp id-pos)
861 (goto-char (+ 2 (point-at-bol)))
862 (if (stringp id-pos)
863 (insert id-pos " ")
864 (insert "BAD REFERENCE "))
865 (incf cnt-error)
866 (throw 'next t))
867 (unless cmd
868 (insert "BAD FLAG ")
869 (incf cnt-error)
870 (throw 'next t))
871 (move-marker bos-marker (point))
872 (if (re-search-forward "^** Old value[ \t]*$" eos t)
873 (setq old (buffer-substring
874 (1+ (match-end 0))
875 (progn (outline-next-heading) (point)))))
876 (if (re-search-forward "^** New value[ \t]*$" eos t)
877 (setq new (buffer-substring
878 (1+ (match-end 0))
879 (progn (outline-next-heading)
880 (if (eobp) (org-back-over-empty-lines))
881 (point)))))
882 (setq old (and old (if (string-match "\\S-" old) old nil)))
883 (setq new (and new (if (string-match "\\S-" new) new nil)))
884 (if (and note (> (length note) 0))
885 ;; Make Note into a single line, to fit into a property
886 (setq note (mapconcat 'identity
887 (org-split-string (org-trim note) "\n")
888 "\\n")))
889 (unless (equal data "body")
890 (setq new (and new (org-trim new))
891 old (and old (org-trim old))))
892 (goto-char (+ 2 bos-marker))
893 ;; Remember this place so that we can return
894 (move-marker marker (point))
895 (setq org-mobile-error nil)
896 (save-excursion
897 (condition-case msg
898 (org-with-point-at id-pos
899 (progn
900 (eval cmd)
901 (unless (member data (list "delete" "archive" "archive-sibling" "addheading"))
902 (if (member "FLAGGED" (org-get-tags))
903 (add-to-list 'org-mobile-last-flagged-files
904 (buffer-file-name (current-buffer)))))))
905 (error (setq org-mobile-error msg))))
906 (when org-mobile-error
907 (org-pop-to-buffer-same-window (marker-buffer marker))
908 (goto-char marker)
909 (incf cnt-error)
910 (insert (if (stringp (nth 1 org-mobile-error))
911 (nth 1 org-mobile-error)
912 "EXECUTION FAILED")
913 " ")
914 (throw 'next t))
915 ;; If we get here, the action has been applied successfully
916 ;; So remove the entry
917 (goto-char bos-marker)
918 (delete-region (point) (org-end-of-subtree t t)))))
919 (save-buffer)
920 (move-marker marker nil)
921 (move-marker end nil)
922 (message "%d new, %d edits, %d flags, %d errors" cnt-new
923 cnt-edit cnt-flag cnt-error)
924 (sit-for 1)))
926 (defun org-mobile-timestamp-buffer (buf)
927 "Time stamp buffer BUF, just to make sure its checksum will change."
928 (with-current-buffer buf
929 (save-excursion
930 (save-restriction
931 (widen)
932 (goto-char (point-min))
933 (if (re-search-forward
934 "^\\([ \t]*\\)#\\+LAST_MOBILE_CHANGE:.*\n?" nil t)
935 (progn
936 (goto-char (match-end 1))
937 (delete-region (point) (match-end 0)))
938 (if (looking-at ".*?-\\*-.*-\\*-")
939 (forward-line 1)))
940 (insert "#+LAST_MOBILE_CHANGE: "
941 (format-time-string "%Y-%m-%d %T") "\n")))))
943 (defun org-mobile-smart-read ()
944 "Parse the entry at point for shortcuts and expand them.
945 These shortcuts are meant for fast and easy typing on the limited
946 keyboards of a mobile device. Below we show a list of the shortcuts
947 currently implemented.
949 The entry is expected to contain an inactive time stamp indicating when
950 the entry was created. When setting dates and
951 times (for example for deadlines), the time strings are interpreted
952 relative to that creation date.
953 Abbreviations are expected to take up entire lines, just because it is so
954 easy to type RET on a mobile device. Abbreviations start with one or two
955 letters, followed immediately by a dot and then additional information.
956 Generally the entire shortcut line is removed after action have been taken.
957 Time stamps will be constructed using `org-read-date'. So for example a
958 line \"dd. 2tue\" will set a deadline on the second Tuesday after the
959 creation date.
961 Here are the shortcuts currently implemented:
963 dd. string set deadline
964 ss. string set scheduling
965 tt. string set time tamp, here.
966 ti. string set inactive time
968 tg. tag1 tag2 tag3 set all these tags, change case where necessary
969 td. kwd set this todo keyword, change case where necessary
971 FIXME: Hmmm, not sure if we can make his work against the
972 auto-correction feature. Needs a bit more thinking. So this function
973 is currently a noop.")
975 (defun org-mobile-locate-entry (link)
976 (if (string-match "\\`id:\\(.*\\)$" link)
977 (org-id-find (match-string 1 link) 'marker)
978 (if (not (string-match "\\`olp:\\(.*?\\):\\(.*\\)$" link))
979 ; not found with path, but maybe it is to be inserted
980 ; in top level of the file?
981 (if (not (string-match "\\`olp:\\(.*?\\)$" link))
983 (let ((file (match-string 1 link)))
984 (setq file (org-link-unescape file))
985 (setq file (expand-file-name file org-directory))
986 (save-excursion
987 (find-file file)
988 (goto-char (point-max))
989 (newline)
990 (goto-char (point-max))
991 (point-marker))))
992 (let ((file (match-string 1 link))
993 (path (match-string 2 link)))
994 (setq file (org-link-unescape file))
995 (setq file (expand-file-name file org-directory))
996 (setq path (mapcar 'org-link-unescape
997 (org-split-string path "/")))
998 (org-find-olp (cons file path))))))
1000 (defun org-mobile-edit (what old new)
1001 "Edit item WHAT in the current entry by replacing OLD with NEW.
1002 WHAT can be \"heading\", \"todo\", \"tags\", \"priority\", or \"body\".
1003 The edit only takes place if the current value is equal (except for
1004 white space) the OLD. If this is so, OLD will be replace by NEW
1005 and the command will return t. If something goes wrong, a string will
1006 be returned that indicates what went wrong."
1007 (let (current old1 new1 level)
1008 (if (stringp what) (setq what (intern what)))
1010 (cond
1012 ((memq what '(todo todostate))
1013 (setq current (org-get-todo-state))
1014 (cond
1015 ((equal new "DONEARCHIVE")
1016 (org-todo 'done)
1017 (org-archive-subtree-default))
1018 ((equal new current) t) ; nothing needs to be done
1019 ((or (equal current old)
1020 (eq org-mobile-force-mobile-change t)
1021 (memq 'todo org-mobile-force-mobile-change))
1022 (org-todo (or new 'none)) t)
1023 (t (error "State before change was expected as \"%s\", but is \"%s\""
1024 old current))))
1026 ((eq what 'tags)
1027 (setq current (org-get-tags)
1028 new1 (and new (org-split-string new ":+"))
1029 old1 (and old (org-split-string old ":+")))
1030 (cond
1031 ((org-mobile-tags-same-p current new1) t) ; no change needed
1032 ((or (org-mobile-tags-same-p current old1)
1033 (eq org-mobile-force-mobile-change t)
1034 (memq 'tags org-mobile-force-mobile-change))
1035 (org-set-tags-to new1) t)
1036 (t (error "Tags before change were expected as \"%s\", but are \"%s\""
1037 (or old "") (or current "")))))
1039 ((eq what 'priority)
1040 (when (looking-at org-complex-heading-regexp)
1041 (setq current (and (match-end 3) (substring (match-string 3) 2 3)))
1042 (cond
1043 ((equal current new) t) ; no action required
1044 ((or (equal current old)
1045 (eq org-mobile-force-mobile-change t)
1046 (memq 'tags org-mobile-force-mobile-change))
1047 (org-priority (and new (string-to-char new))))
1048 (t (error "Priority was expected to be %s, but is %s"
1049 old current)))))
1051 ((eq what 'heading)
1052 (when (looking-at org-complex-heading-regexp)
1053 (setq current (match-string 4))
1054 (cond
1055 ((equal current new) t) ; no action required
1056 ((or (equal current old)
1057 (eq org-mobile-force-mobile-change t)
1058 (memq 'heading org-mobile-force-mobile-change))
1059 (goto-char (match-beginning 4))
1060 (insert new)
1061 (delete-region (point) (+ (point) (length current)))
1062 (org-set-tags nil 'align))
1063 (t (error "Heading changed in MobileOrg and on the computer")))))
1065 ((eq what 'addheading)
1066 (if (org-on-heading-p) ; if false we are in top-level of file
1067 (progn
1068 (end-of-line 1)
1069 (org-insert-heading-respect-content)
1070 (org-demote))
1071 (beginning-of-line)
1072 (insert "* "))
1073 (insert new))
1075 ((eq what 'refile)
1076 (org-copy-subtree)
1077 (org-with-point-at (org-mobile-locate-entry new)
1078 (if (org-on-heading-p) ; if false we are in top-level of file
1079 (progn
1080 (setq level (org-get-valid-level (funcall outline-level) 1))
1081 (org-end-of-subtree t t)
1082 (org-paste-subtree level))
1083 (org-paste-subtree 1)))
1084 (org-cut-subtree))
1086 ((eq what 'delete)
1087 (org-cut-subtree))
1089 ((eq what 'archive)
1090 (org-archive-subtree))
1092 ((eq what 'archive-sibling)
1093 (org-archive-to-archive-sibling))
1095 ((eq what 'body)
1096 (setq current (buffer-substring (min (1+ (point-at-eol)) (point-max))
1097 (save-excursion (outline-next-heading)
1098 (point))))
1099 (if (not (string-match "\\S-" current)) (setq current nil))
1100 (cond
1101 ((org-mobile-bodies-same-p current new) t) ; no action necessary
1102 ((or (org-mobile-bodies-same-p current old)
1103 (eq org-mobile-force-mobile-change t)
1104 (memq 'body org-mobile-force-mobile-change))
1105 (save-excursion
1106 (end-of-line 1)
1107 (insert "\n" new)
1108 (or (bolp) (insert "\n"))
1109 (delete-region (point) (progn (org-back-to-heading t)
1110 (outline-next-heading)
1111 (point))))
1113 (t (error "Body was changed in MobileOrg and on the computer")))))))
1115 (defun org-mobile-tags-same-p (list1 list2)
1116 "Are the two tag lists the same?"
1117 (not (or (org-delete-all list1 list2)
1118 (org-delete-all list2 list1))))
1120 (defun org-mobile-bodies-same-p (a b)
1121 "Compare if A and B are visually equal strings.
1122 We first remove leading and trailing white space from the entire strings.
1123 Then we split the strings into lines and remove leading/trailing whitespace
1124 from each line. Then we compare.
1125 A and B must be strings or nil."
1126 (cond
1127 ((and (not a) (not b)) t)
1128 ((or (not a) (not b)) nil)
1129 (t (setq a (org-trim a) b (org-trim b))
1130 (setq a (mapconcat 'identity (org-split-string a "[ \t]*\n[ \t]*") "\n"))
1131 (setq b (mapconcat 'identity (org-split-string b "[ \t]*\n[ \t]*") "\n"))
1132 (equal a b))))
1134 (provide 'org-mobile)
1136 ;; Local variables:
1137 ;; generated-autoload-file: "org-loaddefs.el"
1138 ;; End:
1140 ;;; org-mobile.el ends here