1 ;;; org-mobile.el --- Code for asymmetric sync with a mobile device
2 ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
4 ;; Author: Carsten Dominik <carsten at orgmode dot org>
5 ;; Keywords: outlines, hypermedia, calendar, wp
6 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;; This file contains the code to interact with Richard Moreland's iPhone
29 ;; application MobileOrg, as well as with the Android version by Matthew Jones.
30 ;; This code is documented in Appendix B of the Org-mode manual. The code is
31 ;; not specific for the iPhone and Android - any external
32 ;; viewer/flagging/editing application that uses the same conventions could
39 (eval-when-compile (require 'cl
))
41 (defgroup org-mobile nil
42 "Options concerning support for a viewer/editor on a mobile device."
46 (defcustom org-mobile-files
'(org-agenda-files)
47 "Files to be staged for MobileOrg.
48 This is basically a list of files and directories. Files will be staged
49 directly. Directories will be search for files with the extension `.org'.
50 In addition to this, the list may also contain the following symbols:
53 This means include the complete, unrestricted list of files given in
54 the variable `org-agenda-files'.
55 org-agenda-text-search-extra-files
56 Include the files given in the variable
57 `org-agenda-text-search-extra-files'"
59 :type
'(list :greedy t
60 (option (const :tag
"org-agenda-files" org-agenda-files
))
61 (option (const :tag
"org-agenda-text-search-extra-files"
62 org-agenda-text-search-extra-files
))
63 (repeat :inline t
:tag
"Additional files"
66 (defcustom org-mobile-files-exclude-regexp
""
67 "A regexp to exclude files from `org-mobile-files'."
71 (defcustom org-mobile-directory
""
72 "The WebDAV directory where the interaction with the mobile takes place."
76 (defcustom org-mobile-use-encryption nil
77 "Non-nil means keep only encrypted files on the WebDAV server.
78 Encryption uses AES-256, with a password given in
79 `org-mobile-encryption-password'.
80 When nil, plain files are kept on the server.
81 Turning on encryption requires to set the same password in the MobileOrg
82 application. Before turning this on, check of MobileOrg does already
83 support it - at the time of this writing it did not yet."
87 (defcustom org-mobile-encryption-tempfile
"~/orgtmpcrypt"
88 "File that is being used as a temporary file for encryption.
89 This must be local file on your local machine (not on the WebDAV server).
90 You might want to put this file into a directory where only you have access."
94 (defcustom org-mobile-encryption-password
""
95 "Password for encrypting files uploaded to the server.
96 This is a single password which is used for AES-256 encryption. The same
97 password must also be set in the MobileOrg application. All Org files,
98 including mobileorg.org will be encrypted using this password.
100 SECURITY CONSIDERATIONS:
102 Note that, when Org runs the encryption commands, the password could
103 be visible briefly on your system with the `ps' command. So this method is
104 only intended to keep the files secure on the server, not on your own machine.
106 Also, if you set this variable in an init file (.emacs or .emacs.d/init.el
107 or custom.el...) and if that file is stored in a way so that other can read
108 it, this also limits the security of this approach. You can also leave
109 this variable empty - Org will then ask for the password once per Emacs
112 :type
'(string :tag
"Password"))
114 (defvar org-mobile-encryption-password-session nil
)
116 (defun org-mobile-encryption-password ()
117 (or (org-string-nw-p org-mobile-encryption-password
)
118 (org-string-nw-p org-mobile-encryption-password-session
)
119 (setq org-mobile-encryption-password-session
120 (read-passwd "Password for MobileOrg: " t
))))
122 (defcustom org-mobile-inbox-for-pull
"~/org/from-mobile.org"
123 "The file where captured notes and flags will be appended to.
124 During the execution of `org-mobile-pull', the file
125 `org-mobile-capture-file' will be emptied it's contents have
126 been appended to the file given here. This file should be in
127 `org-directory', and not in the staging area or on the web server."
131 (defconst org-mobile-capture-file
"mobileorg.org"
132 "The capture file where the mobile stores captured notes and flags.
133 This should not be changed, because MobileOrg assumes this name.")
135 (defcustom org-mobile-index-file
"index.org"
136 "The index file with links to all Org files that should be loaded by MobileOrg.
137 Relative to `org-mobile-directory'. The Address field in the MobileOrg setup
138 should point to this file."
142 (defcustom org-mobile-agendas
'all
143 "The agendas that should be pushed to MobileOrg.
146 default the weekly agenda and the global TODO list
147 custom all custom agendas defined by the user
148 all the custom agendas and the default ones
149 list a list of selection key(s) as string."
152 (const :tag
"Default Agendas" default
)
153 (const :tag
"Custom Agendas" custom
)
154 (const :tag
"Default and Custom Agendas" all
)
155 (repeat :tag
"Selected"
156 (string :tag
"Selection Keys"))))
158 (defcustom org-mobile-force-id-on-agenda-items t
159 "Non-nil means make all agenda items carry an ID."
163 (defcustom org-mobile-force-mobile-change nil
164 "Non-nil means force the change made on the mobile device.
165 So even if there have been changes to the computer version of the entry,
166 force the new value set on the mobile.
167 When nil, mark the entry from the mobile with an error message.
168 Instead of nil or t, this variable can also be a list of symbols, indicating
169 the editing types for which the mobile version should always dominate."
172 (const :tag
"Always" t
)
173 (const :tag
"Never" nil
)
174 (set :greedy t
:tag
"Specify"
181 (defcustom org-mobile-action-alist
182 '(("edit" .
(org-mobile-edit data old new
)))
183 "Alist with flags and actions for mobile sync.
184 When flagging an entry, MobileOrg will create entries that look like
186 * F(action:data) [[id:entry-id][entry title]]
188 This alist defines that the ACTION in the parentheses of F() should mean,
189 i.e. what action should be taken. The :data part in the parenthesis is
190 optional. If present, the string after the colon will be passed to the
191 action form as the `data' variable.
192 The car of each elements of the alist is an actions string. The cdr is
193 an Emacs Lisp form that will be evaluated with the cursor on the headline
196 For now, it is not recommended to change this variable."
199 (cons (string :tag
"Action flag")
200 (sexp :tag
"Action form"))))
202 (defcustom org-mobile-checksum-binary
(or (executable-find "shasum")
203 (executable-find "sha1sum")
204 (executable-find "md5sum")
205 (executable-find "md5"))
206 "Executable used for computing checksums of agenda files."
210 (defvar org-mobile-pre-push-hook nil
211 "Hook run before running `org-mobile-push'.
212 This could be used to clean up `org-mobile-directory', for example to
213 remove files that used to be included in the agenda but no longer are.
214 The presence of such files would not really be a problem, but after time
215 they may accumulate.")
217 (defvar org-mobile-post-push-hook nil
218 "Hook run after running `org-mobile-push'.
219 If Emacs does not have direct write access to the WebDAV directory used
220 by the mobile device, this hook should be used to copy all files from the
221 local staging directory `org-mobile-directory' to the WebDAV directory,
222 for example using `rsync' or `scp'.")
224 (defvar org-mobile-pre-pull-hook nil
225 "Hook run before executing `org-mobile-pull'.
226 If Emacs does not have direct write access to the WebDAV directory used
227 by the mobile device, this hook should be used to copy the capture file
228 `mobileorg.org' from the WebDAV location to the local staging
229 directory `org-mobile-directory'.")
231 (defvar org-mobile-post-pull-hook nil
232 "Hook run after running `org-mobile-pull'.
233 If Emacs does not have direct write access to the WebDAV directory used
234 by the mobile device, this hook should be used to copy the emptied
235 capture file `mobileorg.org' back to the WebDAV directory, for example
236 using `rsync' or `scp'.")
238 (defvar org-mobile-last-flagged-files nil
239 "List of files containing entries flagged in the latest pull.")
241 (defvar org-mobile-files-alist nil
)
242 (defvar org-mobile-checksum-files nil
)
244 (defun org-mobile-prepare-file-lists ()
245 (setq org-mobile-files-alist
(org-mobile-files-alist))
246 (setq org-mobile-checksum-files nil
))
248 (defun org-mobile-files-alist ()
249 "Expand the list in `org-mobile-files' to a list of existing files.
250 Also exclude files matching `org-mobile-files-exclude-regexp'."
251 (let* ((include-archives
252 (and (member 'org-agenda-text-search-extra-files org-mobile-files
)
253 (member 'agenda-archives org-agenda-text-search-extra-files
)
260 ((eq f
'org-agenda-files
)
261 (org-agenda-files t include-archives
))
262 ((eq f
'org-agenda-text-search-extra-files
)
263 (delq 'agenda-archives
265 org-agenda-text-search-extra-files
)))
266 ((and (stringp f
) (file-directory-p f
))
267 (directory-files f
'full
"\\.org\\'"))
268 ((and (stringp f
) (file-exists-p f
))
275 (unless (and (not (string= org-mobile-files-exclude-regexp
""))
276 (string-match org-mobile-files-exclude-regexp f
))
279 (orgdir-uname (file-name-as-directory (file-truename org-directory
)))
280 (orgdir-re (concat "\\`" (regexp-quote orgdir-uname
)))
281 uname seen rtn file link-name
)
282 ;; Make the files unique, and determine the name under which they will
284 (while (setq file
(pop files
))
285 (if (not (file-name-absolute-p file
))
286 (setq file
(expand-file-name file org-directory
)))
287 (setq uname
(file-truename file
))
288 (unless (member uname seen
)
290 (if (string-match orgdir-re uname
)
291 (setq link-name
(substring uname
(match-end 0)))
292 (setq link-name
(file-name-nondirectory uname
)))
293 (push (cons file link-name
) rtn
)))
297 (defun org-mobile-push ()
298 "Push the current state of Org affairs to the WebDAV directory.
299 This will create the index file, copy all agenda files there, and also
300 create all custom agenda views, for upload to the mobile phone."
302 (let ((a-buffer (get-buffer org-agenda-buffer-name
)))
303 (let ((org-agenda-buffer-name "*SUMO*")
304 (org-agenda-filter org-agenda-filter
)
305 (org-agenda-redo-command org-agenda-redo-command
))
307 (save-window-excursion
308 (run-hooks 'org-mobile-pre-push-hook
)
309 (org-mobile-check-setup)
310 (org-mobile-prepare-file-lists)
311 (message "Creating agendas...")
312 (let ((inhibit-redisplay t
)) (org-mobile-create-sumo-agenda))
313 (message "Creating agendas...done")
314 (org-save-all-org-buffers) ; to save any IDs created by this process
315 (message "Copying files...")
316 (org-mobile-copy-agenda-files)
317 (message "Writing index file...")
318 (org-mobile-create-index-file)
319 (message "Writing checksums...")
320 (org-mobile-write-checksums)
321 (run-hooks 'org-mobile-post-push-hook
))))
323 (when (and a-buffer
(buffer-live-p a-buffer
))
324 (if (not (get-buffer-window a-buffer
))
325 (kill-buffer a-buffer
)
326 (let ((cw (selected-window)))
327 (select-window (get-buffer-window a-buffer
))
329 (select-window cw
)))))
330 (message "Files for mobile viewer staged"))
332 (defvar org-mobile-before-process-capture-hook nil
333 "Hook that is run after content was moved to `org-mobile-inbox-for-pull'.
334 The inbox file is visited by the current buffer, and the buffer is
335 narrowed to the newly captured data.")
338 (defun org-mobile-pull ()
339 "Pull the contents of `org-mobile-capture-file' and integrate them.
340 Apply all flagged actions, flag entries to be flagged and then call an
341 agenda view showing the flagged items."
343 (org-mobile-check-setup)
344 (run-hooks 'org-mobile-pre-pull-hook
)
345 (let ((insertion-marker (org-mobile-move-capture)))
346 (if (not (markerp insertion-marker
))
347 (message "No new items")
348 (org-with-point-at insertion-marker
350 (narrow-to-region (point) (point-max))
351 (run-hooks 'org-mobile-before-process-capture-hook
)))
352 (org-with-point-at insertion-marker
353 (org-mobile-apply (point) (point-max)))
354 (move-marker insertion-marker nil
)
355 (run-hooks 'org-mobile-post-pull-hook
)
356 (when org-mobile-last-flagged-files
357 ;; Make an agenda view of flagged entries, but only in the files
358 ;; where stuff has been added.
359 (put 'org-agenda-files
'org-restrict org-mobile-last-flagged-files
)
360 (let ((org-agenda-keep-restricted-file-list t
))
361 (org-agenda nil
"?"))))))
363 (defun org-mobile-check-setup ()
364 "Check if org-mobile-directory has been set up."
365 (org-mobile-cleanup-encryption-tempfile)
366 (unless (and org-directory
367 (stringp org-directory
)
368 (string-match "\\S-" org-directory
)
369 (file-exists-p org-directory
)
370 (file-directory-p org-directory
))
372 "Please set `org-directory' to the directory where your org files live"))
373 (unless (and org-mobile-directory
374 (stringp org-mobile-directory
)
375 (string-match "\\S-" org-mobile-directory
)
376 (file-exists-p org-mobile-directory
)
377 (file-directory-p org-mobile-directory
))
379 "Variable `org-mobile-directory' must point to an existing directory"))
380 (unless (and org-mobile-inbox-for-pull
381 (stringp org-mobile-inbox-for-pull
)
382 (string-match "\\S-" org-mobile-inbox-for-pull
)
384 (file-name-directory org-mobile-inbox-for-pull
)))
386 "Variable `org-mobile-inbox-for-pull' must point to a file in an existing directory"))
387 (unless (and org-mobile-checksum-binary
388 (string-match "\\S-" org-mobile-checksum-binary
))
389 (error "No executable found to compute checksums"))
390 (when org-mobile-use-encryption
391 (unless (string-match "\\S-" (org-mobile-encryption-password))
393 "To use encryption, you must set `org-mobile-encryption-password'"))
394 (unless (file-writable-p org-mobile-encryption-tempfile
)
395 (error "Cannot write to encryption tempfile %s"
396 org-mobile-encryption-tempfile
))
397 (unless (executable-find "openssl")
398 (error "openssl is needed to encrypt files"))))
400 (defun org-mobile-create-index-file ()
401 "Write the index file in the WebDAV directory."
402 (let ((files-alist (sort (copy-sequence org-mobile-files-alist
)
403 (lambda (a b
) (string< (cdr a
) (cdr b
)))))
404 (def-todo (default-value 'org-todo-keywords
))
405 (def-tags (default-value 'org-tag-alist
))
406 (target-file (expand-file-name org-mobile-index-file
407 org-mobile-directory
))
408 file link-name todo-kwds done-kwds tags drawers entry kwds dwds twds
)
410 (org-prepare-agenda-buffers (mapcar 'car files-alist
))
411 (setq done-kwds
(org-uniquify org-done-keywords-for-agenda
))
412 (setq todo-kwds
(org-delete-all
414 (org-uniquify org-todo-keywords-for-agenda
)))
415 (setq drawers
(org-uniquify org-drawers-for-agenda
))
416 (setq tags
(org-uniquify
420 (cond ((stringp e
) e
)
422 (if (stringp (car e
)) (car e
) nil
))
424 org-tag-alist-for-agenda
))))
426 (if org-mobile-use-encryption
427 org-mobile-encryption-tempfile
429 (while (setq entry
(pop def-todo
))
430 (insert "#+READONLY\n")
431 (setq kwds
(mapcar (lambda (x) (if (string-match "(" x
)
432 (substring x
0 (match-beginning 0))
435 (insert "#+TODO: " (mapconcat 'identity kwds
" ") "\n")
436 (setq dwds
(member "|" kwds
)
437 twds
(org-delete-all dwds kwds
)
438 todo-kwds
(org-delete-all twds todo-kwds
)
439 done-kwds
(org-delete-all dwds done-kwds
)))
440 (when (or todo-kwds done-kwds
)
441 (insert "#+TODO: " (mapconcat 'identity todo-kwds
" ") " | "
442 (mapconcat 'identity done-kwds
" ") "\n"))
443 (setq def-tags
(mapcar
447 ((eq (car x
) :startgroup
) "{")
448 ((eq (car x
) :endgroup
) "}")
449 ((eq (car x
) :newline
) nil
)
453 (setq def-tags
(delq nil def-tags
))
454 (setq tags
(org-delete-all def-tags tags
))
455 (setq tags
(sort tags
(lambda (a b
) (string< (downcase a
) (downcase b
)))))
456 (setq tags
(append def-tags tags nil
))
457 (insert "#+TAGS: " (mapconcat 'identity tags
" ") "\n")
458 (insert "#+DRAWERS: " (mapconcat 'identity drawers
" ") "\n")
459 (insert "#+ALLPRIORITIES: A B C" "\n")
460 (when (file-exists-p (expand-file-name
461 org-mobile-directory
"agendas.org"))
462 (insert "* [[file:agendas.org][Agenda Views]]\n"))
463 (while (setq entry
(pop files-alist
))
464 (setq file
(car entry
)
465 link-name
(cdr entry
))
466 (insert (format "* [[file:%s][%s]]\n"
467 link-name 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
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 org-mobile-checksum-binary
" "
491 (shell-quote-argument (expand-file-name file
)))))
492 (when (string-match "[a-fA-F0-9]\\{30,40\\}" check
)
493 (push (cons link-name
(match-string 0 check
))
494 org-mobile-checksum-files
))))
496 (setq file
(expand-file-name org-mobile-capture-file
497 org-mobile-directory
))
499 (setq buf
(find-file file
))
500 (when (and (= (point-min) (point-max)))
503 (when org-mobile-use-encryption
504 (write-file org-mobile-encryption-tempfile
)
505 (org-mobile-encrypt-and-move org-mobile-encryption-tempfile file
)))
506 (push (cons org-mobile-capture-file
(md5 (buffer-string)))
507 org-mobile-checksum-files
))
508 (org-mobile-cleanup-encryption-tempfile)
511 (defun org-mobile-write-checksums ()
512 "Create checksums for all files in `org-mobile-directory'.
513 The table of checksums is written to the file mobile-checksums."
514 (let ((sumfile (expand-file-name "checksums.dat" org-mobile-directory
))
515 (files org-mobile-checksum-files
)
517 (with-temp-file sumfile
518 (set-buffer-file-coding-system 'undecided-unix nil
)
519 (while (setq entry
(pop files
))
520 (setq file
(car entry
) sum
(cdr entry
))
521 (insert (format "%s %s\n" sum file
))))))
523 (defun org-mobile-sumo-agenda-command ()
524 "Return an agenda custom command that comprises all custom commands."
526 ;; normalize different versions
530 (cond ((stringp (cdr x
)) nil
)
531 ((stringp (nth 1 x
)) x
)
532 ((not (nth 1 x
)) (cons (car x
) (cons "" (cddr x
))))
533 (t (cons (car x
) (cons "" (cdr x
))))))
534 org-agenda-custom-commands
)))
535 (default-list '(("a" "Agenda" agenda
) ("t" "All TODO" alltodo
)))
536 thelist new e key desc type match settings cmds gkey gdesc gsettings cnt
)
538 ((eq org-mobile-agendas
'custom
)
539 (setq thelist custom-list
))
540 ((eq org-mobile-agendas
'default
)
541 (setq thelist default-list
))
542 ((eq org-mobile-agendas
'all
)
543 (setq thelist custom-list
)
544 (unless (assoc "t" thelist
) (push '("t" "ALL TODO" alltodo
) thelist
))
545 (unless (assoc "a" thelist
) (push '("a" "Agenda" agenda
) thelist
)))
546 ((listp org-mobile-agendas
)
547 (setq thelist
(append custom-list default-list
))
548 (setq thelist
(delq nil
(mapcar (lambda (k) (assoc k thelist
))
549 org-mobile-agendas
)))))
550 (while (setq e
(pop thelist
))
553 ;; this is a description entry - skip it
555 ((eq (nth 2 e
) 'search
)
556 ;; Search view is interactive, skip
558 ((memq (nth 2 e
) '(todo-tree tags-tree occur-tree
))
559 ;; These are trees, not really agenda commands
561 ((and (memq (nth 2 e
) '(todo tags tags-todo
))
563 (not (string-match "\\S-" (nth 3 e
)))))
564 ;; These would be interactive because the match string is empty
566 ((memq (nth 2 e
) '(agenda alltodo todo tags tags-todo
))
568 (setq key
(car e
) desc
(nth 1 e
) type
(nth 2 e
) match
(nth 3 e
)
571 (cons (list 'org-agenda-title-append
572 (concat "<after>KEYS=" key
" TITLE: "
573 (if (and (stringp desc
) (> (length desc
) 0))
574 desc
(symbol-name type
))
575 " " match
"</after>"))
577 (push (list type match settings
) new
))
579 ;; A user-defined function, not sure how to handle that yet
583 (setq gkey
(car e
) gdesc
(nth 1 e
) gsettings
(nth 3 e
) cmds
(nth 2 e
))
585 (while (setq e
(pop cmds
))
586 (setq type
(car e
) match
(nth 1 e
) settings
(nth 2 e
))
587 (setq settings
(append gsettings settings
))
589 (cons (list 'org-agenda-title-append
590 (concat "<after>KEYS=" gkey
"#" (number-to-string
592 " TITLE: " gdesc
" " match
"</after>"))
594 (push (list type match settings
) new
)))))
595 (and new
(list "X" "SUMO" (reverse new
)
596 '((org-agenda-compact-blocks nil
))))))
598 (defvar org-mobile-creating-agendas nil
)
599 (defun org-mobile-write-agenda-for-mobile (file)
600 (let ((all (buffer-string)) in-date id pl prefix line app short m sexp
)
603 (insert "#+READONLY\n")
605 (goto-char (point-min))
608 ((looking-at "[ \t]*$")) ; keep empty lines
610 ;; remove underlining
611 (delete-region (point) (point-at-eol)))
612 ((get-text-property (point) 'org-agenda-structural-header
)
614 (setq app
(get-text-property (point)
615 'org-agenda-title-append
))
616 (setq short
(get-text-property (point)
618 (when (and short
(looking-at ".+"))
619 (replace-match short
)
620 (beginning-of-line 1))
624 (beginning-of-line 1))
626 ((get-text-property (point) 'org-agenda-date-header
)
629 ((setq m
(or (get-text-property (point) 'org-hd-marker
)
630 (get-text-property (point) 'org-marker
)))
631 (setq sexp
(member (get-text-property (point) 'type
)
633 (if (setq pl
(text-property-any (point) (point-at-eol) 'org-heading t
))
635 (setq prefix
(org-trim (buffer-substring
637 line
(org-trim (buffer-substring
640 (delete-region (point-at-bol) (point-at-eol))
641 (insert line
"<before>" prefix
"</before>")
642 (beginning-of-line 1))
643 (and (looking-at "[ \t]+") (replace-match "")))
644 (insert (if in-date
"*** " "** "))
648 (insert (org-agenda-get-some-entry-text
652 (if (org-bound-and-true-p
653 org-mobile-force-id-on-agenda-items
)
654 (org-id-get m
'create
)
655 (or (org-entry-get m
"ID")
656 (org-mobile-get-outline-path-link m
))))
657 (insert " :PROPERTIES:\n :ORIGINAL_ID: " id
659 (beginning-of-line 2))
660 (push (cons "agendas.org" (md5 (buffer-string)))
661 org-mobile-checksum-files
))
662 (message "Agenda written to Org file %s" file
)))
664 (defun org-mobile-get-outline-path-link (pom)
665 (org-with-point-at pom
667 (org-mobile-escape-olp (file-name-nondirectory buffer-file-name
))
669 (mapconcat 'org-mobile-escape-olp
670 (org-get-outline-path)
673 (org-mobile-escape-olp (nth 4 (org-heading-components))))))
675 (defun org-mobile-escape-olp (s)
676 (let ((table '(?
: ?
/)))
677 (org-link-escape s table
)))
680 (defun org-mobile-create-sumo-agenda ()
681 "Create a file that contains all custom agenda views."
683 (let* ((file (expand-file-name "agendas.org"
684 org-mobile-directory
))
685 (file1 (if org-mobile-use-encryption
686 org-mobile-encryption-tempfile
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
))
695 (org-store-agenda-views))
696 (when org-mobile-use-encryption
697 (org-mobile-encrypt-and-move file1 file
)
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
)
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'."
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'."
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."
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
))
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))
754 (with-current-buffer capture-buffer
755 (setq content
(buffer-string))
756 (setq not-empty
(string-match "\\S-" content
))
758 (set-buffer inbox-buffer
)
760 (goto-char (point-max))
761 (or (bolp) (newline))
762 (move-marker insertion-point
763 (prog1 (point) (insert content
)))
765 (set-buffer capture-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
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
)))
781 (with-current-buffer buffer
782 (when (re-search-forward (concat "\\([0-9a-fA-F]\\{30,\\}\\).*?"
783 (regexp-quote org-mobile-capture-file
)
785 (goto-char (match-beginning 1))
786 (delete-region (match-beginning 1) (match-end 1))
787 (insert (md5 buffer-string
))
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."
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
801 (while (re-search-forward "^\\*\\* Note ID: [-0-9A-F]+[ \t]*\n" end t
)
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
))
813 id-pos org-mobile-error
)
815 ;; Count the new captures
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("))
823 (while (re-search-forward
824 "^\\*+[ \t]+F(\\([^():\n]*\\)\\(:\\([^()\n]*\\)\\)?)[ \t]+\\[\\[\\(\\(id\\|olp\\):\\([^]\n]+\\)\\)" end t
)
825 (setq id-pos
(condition-case msg
826 (org-mobile-locate-entry (match-string 4))
827 (error (nth 1 msg
))))
828 (when (and (markerp id-pos
)
829 (not (member (marker-buffer id-pos
) buf-list
)))
830 (org-mobile-timestamp-buffer (marker-buffer id-pos
))
831 (push (marker-buffer id-pos
) buf-list
))
833 (if (or (not id-pos
) (stringp id-pos
))
835 (goto-char (+ 2 (point-at-bol)))
838 (add-text-properties (point-at-bol) (point-at-eol)
839 (list 'org-mobile-marker
840 (or id-pos
"Linked entry not found")))))
842 ;; OK, now go back and start applying
844 (while (re-search-forward "^\\*+[ \t]+F(\\([^():\n]*\\)\\(:\\([^()\n]*\\)\\)?)" end t
)
846 (setq id-pos
(get-text-property (point-at-bol) 'org-mobile-marker
))
847 (if (not (markerp id-pos
))
850 (insert "UNKNOWN PROBLEM"))
851 (let* ((action (match-string 1))
852 (data (and (match-end 3) (match-string 3)))
854 (eos (save-excursion (org-end-of-subtree t t
)))
855 (cmd (if (equal action
"")
858 (org-toggle-tag "FLAGGED" 'on
)
860 (org-entry-put nil
"THEFLAGGINGNOTE" note
)))
862 (cdr (assoc action org-mobile-action-alist
))))
863 (note (and (equal action
"")
864 (buffer-substring (1+ (point-at-eol)) eos
)))
865 (org-inhibit-logging 'note
) ;; Do not take notes interactively
868 (move-marker bos-marker
(point))
869 (if (re-search-forward "^** Old value[ \t]*$" eos t
)
870 (setq old
(buffer-substring
872 (progn (outline-next-heading) (point)))))
873 (if (re-search-forward "^** New value[ \t]*$" eos t
)
874 (setq new
(buffer-substring
876 (progn (outline-next-heading)
877 (if (eobp) (org-back-over-empty-lines))
879 (setq old
(and old
(if (string-match "\\S-" old
) old nil
)))
880 (setq new
(and new
(if (string-match "\\S-" new
) new nil
)))
881 (if (and note
(> (length note
) 0))
882 ;; Make Note into a single line, to fit into a property
883 (setq note
(mapconcat 'identity
884 (org-split-string (org-trim note
) "\n")
886 (unless (equal data
"body")
887 (setq new
(and new
(org-trim new
))
888 old
(and old
(org-trim old
))))
889 (goto-char (+ 2 bos-marker
))
890 (unless (markerp id-pos
)
891 (insert "BAD REFERENCE ")
898 ;; Remember this place so that we can return
899 (move-marker marker
(point))
900 (setq org-mobile-error nil
)
903 (org-with-point-at id-pos
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 (switch-to-buffer (marker-buffer marker
))
914 (insert (if (stringp (nth 1 org-mobile-error
))
915 (nth 1 org-mobile-error
)
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
))))))
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
)
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
936 (goto-char (point-min))
937 (if (re-search-forward
938 "^\\([ \t]*\\)#\\+LAST_MOBILE_CHANGE:.*\n?" nil t
)
940 (goto-char (match-end 1))
941 (delete-region (point) (match-end 0)))
942 (if (looking-at ".*?-\\*-.*-\\*-")
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
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
))
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
)
1000 (if (stringp what
) (setq what
(intern what
)))
1004 ((memq what
'(todo todostate
))
1005 (setq current
(org-get-todo-state))
1007 ((equal new
"DONEARCHIVE")
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\""
1019 (setq current
(org-get-tags)
1020 new1
(and new
(org-split-string new
":+"))
1021 old1
(and old
(org-split-string old
":+")))
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)))
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"
1044 (when (looking-at org-complex-heading-regexp
)
1045 (setq current
(match-string 4))
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))
1053 (delete-region (point) (+ (point) (length current
)))
1054 (org-set-tags nil
'align
))
1055 (t (error "Heading changed in MobileOrg and on the computer")))))
1058 (setq current
(buffer-substring (min (1+ (point-at-eol)) (point-max))
1059 (save-excursion (outline-next-heading)
1061 (if (not (string-match "\\S-" current
)) (setq current nil
))
1063 ((org-mobile-bodies-same-p current new
) t
) ; no action necessary
1064 ((or (org-mobile-bodies-same-p current old
)
1065 (eq org-mobile-force-mobile-change t
)
1066 (memq 'body org-mobile-force-mobile-change
))
1070 (or (bolp) (insert "\n"))
1071 (delete-region (point) (progn (org-back-to-heading t
)
1072 (outline-next-heading)
1075 (t (error "Body was changed in MobileOrg and on the computer")))))))
1077 (defun org-mobile-tags-same-p (list1 list2
)
1078 "Are the two tag lists the same?"
1079 (not (or (org-delete-all list1 list2
)
1080 (org-delete-all list2 list1
))))
1082 (defun org-mobile-bodies-same-p (a b
)
1083 "Compare if A and B are visually equal strings.
1084 We first remove leading and trailing white space from the entire strings.
1085 Then we split the strings into lines and remove leading/trailing whitespace
1086 from each line. Then we compare.
1087 A and B must be strings or nil."
1089 ((and (not a
) (not b
)) t
)
1090 ((or (not a
) (not b
)) nil
)
1091 (t (setq a
(org-trim a
) b
(org-trim b
))
1092 (setq a
(mapconcat 'identity
(org-split-string a
"[ \t]*\n[ \t]*") "\n"))
1093 (setq b
(mapconcat 'identity
(org-split-string b
"[ \t]*\n[ \t]*") "\n"))
1096 (provide 'org-mobile
)
1098 ;; arch-tag: ace0e26c-58f2-4309-8a61-05ec1535f658
1100 ;;; org-mobile.el ends here