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