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