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