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