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