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