1 ;;; org-mobile.el --- Code for Asymmetric Sync With a Mobile Device -*- lexical-binding: t; -*-
2 ;; Copyright (C) 2009-2018 Free Software Foundation, Inc.
4 ;; Author: Carsten Dominik <carsten at orgmode dot org>
5 ;; Keywords: outlines, hypermedia, calendar, wp
6 ;; Homepage: https://orgmode.org
8 ;; This file is part of GNU Emacs.
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 <https://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;; This file contains the code to interact with a mobile application,
28 ;; such as Richard Moreland's iPhone application MobileOrg, or the
29 ;; Android version by Matthew Jones. This code is documented in
30 ;; Appendix B of the Org manual. The code is not specific for the
31 ;; iPhone and Android - any external viewer/flagging/editing
32 ;; application that uses the same conventions could be used.
38 (defvar org-agenda-keep-restricted-file-list
)
42 (defgroup org-mobile nil
43 "Options concerning support for a viewer/editor on a mobile device."
47 (defcustom org-mobile-files
'(org-agenda-files)
48 "Files to be staged for the mobile application.
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:
55 This means include the complete, unrestricted list of files given in
56 the variable `org-agenda-files'.
58 `org-agenda-text-search-extra-files'
59 Include the files given in the variable
60 `org-agenda-text-search-extra-files'"
62 :type
'(list :greedy t
63 (option (const :tag
"org-agenda-files" org-agenda-files
))
64 (option (const :tag
"org-agenda-text-search-extra-files"
65 org-agenda-text-search-extra-files
))
66 (repeat :inline t
:tag
"Additional files"
69 (defcustom org-mobile-files-exclude-regexp
""
70 "A regexp to exclude files from `org-mobile-files'."
75 (defcustom org-mobile-directory
""
76 "The WebDAV directory where the interaction with the mobile takes place."
80 (defcustom org-mobile-allpriorities
"A B C"
81 "Default set of priority cookies for the index file."
83 :package-version
'(Org .
"8.0")
87 (defcustom org-mobile-use-encryption nil
88 "Non-nil means keep only encrypted files on the WebDAV server.
90 Encryption uses AES-256, with a password given in
91 `org-mobile-encryption-password'. When nil, plain files are kept
94 Turning on encryption requires setting the same password in the
95 mobile application. Before turning this on, check if the mobile
96 application does support it."
101 (defcustom org-mobile-encryption-tempfile
"~/orgtmpcrypt"
102 "File that is being used as a temporary file for encryption.
103 This must be local file on your local machine (not on the WebDAV server).
104 You might want to put this file into a directory where only you have access."
109 (defcustom org-mobile-encryption-password
""
110 "Password for encrypting files uploaded to the server.
112 This is a single password which is used for AES-256 encryption. The same
113 password must also be set in the mobile application. All Org files,
114 including \"mobileorg.org\" will be encrypted using this password.
116 SECURITY CONSIDERATIONS:
118 Note that, when Org runs the encryption commands, the password could
119 be visible briefly on your system with the `ps' command. So this method is
120 only intended to keep the files secure on the server, not on your own machine.
122 Also, if you set this variable in an init file (.emacs or .emacs.d/init.el
123 or custom.el...) and if that file is stored in a way so that other can read
124 it, this also limits the security of this approach. You can also leave
125 this variable empty - Org will then ask for the password once per Emacs
129 :type
'(string :tag
"Password"))
131 (defvar org-mobile-encryption-password-session nil
)
133 (defun org-mobile-encryption-password ()
134 (or (org-string-nw-p org-mobile-encryption-password
)
135 (org-string-nw-p org-mobile-encryption-password-session
)
136 (setq org-mobile-encryption-password-session
137 (read-passwd "Password for mobile application: " t
))))
139 (defcustom org-mobile-inbox-for-pull
"~/org/from-mobile.org"
140 "The file where captured notes and flags will be appended to.
141 During the execution of `org-mobile-pull', the file
142 `org-mobile-capture-file' is emptied as soon as its contents have
143 been appended to the file given here. This file should be in
144 `org-directory', and not in the staging area or on the web server."
148 (defconst org-mobile-capture-file
"mobileorg.org"
149 "The capture file where the mobile stores captured notes and flags.
150 This must not be changed, because the mobile application assumes this name.")
152 (defcustom org-mobile-index-file
"index.org"
153 "Index file with links to all Org files.
154 It should be loaded by the mobile application. The file name is
155 relative to `org-mobile-directory'. The \"Address\" field in the
156 mobile application setup should point to this file."
160 (defcustom org-mobile-agendas
'all
161 "The agendas that should be pushed to the mobile application.
165 `default' the weekly agenda and the global TODO list
166 `custom' all custom agendas defined by the user
167 `all' the custom agendas and the default ones
168 `list' a list of selection key(s) as string."
172 (const :tag
"Default Agendas" default
)
173 (const :tag
"Custom Agendas" custom
)
174 (const :tag
"Default and Custom Agendas" all
)
175 (repeat :tag
"Selected"
176 (string :tag
"Selection Keys"))))
178 (defcustom org-mobile-force-id-on-agenda-items t
179 "Non-nil means make all agenda items carry an ID."
183 (defcustom org-mobile-force-mobile-change nil
184 "Non-nil means force the change made on the mobile device.
185 So even if there have been changes to the computer version of the entry,
186 force the new value set on the mobile.
187 When nil, mark the entry from the mobile with an error message.
188 Instead of nil or t, this variable can also be a list of symbols, indicating
189 the editing types for which the mobile version should always dominate."
192 (const :tag
"Always" t
)
193 (const :tag
"Never" nil
)
194 (set :greedy t
:tag
"Specify"
201 (defcustom org-mobile-checksum-binary
(or (executable-find "shasum")
202 (executable-find "sha1sum")
203 (executable-find "md5sum")
204 (executable-find "md5"))
205 "Executable used for computing checksums of agenda files."
209 (defvar org-mobile-pre-push-hook nil
210 "Hook run before running `org-mobile-push'.
211 This could be used to clean up `org-mobile-directory', for example to
212 remove files that used to be included in the agenda but no longer are.
213 The presence of such files would not really be a problem, but after time
214 they may accumulate.")
216 (defvar org-mobile-post-push-hook nil
217 "Hook run after running `org-mobile-push'.
218 If Emacs does not have direct write access to the WebDAV directory used
219 by the mobile device, this hook should be used to copy all files from the
220 local staging directory `org-mobile-directory' to the WebDAV directory,
221 for example using `rsync' or `scp'.")
223 (defvar org-mobile-pre-pull-hook nil
224 "Hook run before executing `org-mobile-pull'.
225 If Emacs does not have direct write access to the WebDAV directory used
226 by the mobile device, this hook should be used to copy the capture file
227 `mobileorg.org' from the WebDAV location to the local staging
228 directory `org-mobile-directory'.")
230 (defvar org-mobile-post-pull-hook nil
231 "Hook run after running `org-mobile-pull', only if new items were found.
232 If Emacs does not have direct write access to the WebDAV directory used
233 by the mobile device, this hook should be used to copy the emptied
234 capture file `mobileorg.org' back to the WebDAV directory, for example
235 using `rsync' or `scp'.")
237 (defconst org-mobile-action-alist
'(("edit" . org-mobile-edit
))
238 "Alist with flags and actions for mobile sync.
240 When flagging an entry, the mobile application creates entries
243 * F(action:data) [[id:entry-id][entry title]]
245 This alist defines that the ACTION in the parentheses of F()
246 should mean, i.e. what action should be taken. The :data part in
247 the parenthesis is optional. If present, the string after the
248 colon will be passed to the action function as the first argument
251 The car of each elements of the alist is an actions string. The
252 cdr is a function that is called with the cursor on the headline
253 of that entry. It should accept three arguments, the :data part,
254 the old and new values for the entry.")
256 (defvar org-mobile-last-flagged-files nil
257 "List of files containing entries flagged in the latest pull.")
259 (defvar org-mobile-files-alist nil
)
260 (defvar org-mobile-checksum-files nil
)
262 (defun org-mobile-prepare-file-lists ()
263 (setq org-mobile-files-alist
(org-mobile-files-alist))
264 (setq org-mobile-checksum-files nil
))
266 (defun org-mobile-files-alist ()
267 "Expand the list in `org-mobile-files' to a list of existing files.
268 Also exclude files matching `org-mobile-files-exclude-regexp'."
269 (let* ((include-archives
270 (and (member 'org-agenda-text-search-extra-files org-mobile-files
)
271 (member 'agenda-archives org-agenda-text-search-extra-files
)
278 ((eq f
'org-agenda-files
)
279 (org-agenda-files t include-archives
))
280 ((eq f
'org-agenda-text-search-extra-files
)
281 (delq 'agenda-archives
283 org-agenda-text-search-extra-files
)))
284 ((and (stringp f
) (file-directory-p f
))
285 (directory-files f
'full
"\\.org\\'"))
286 ((and (stringp f
) (file-exists-p f
))
293 (unless (and (not (string= org-mobile-files-exclude-regexp
""))
294 (string-match org-mobile-files-exclude-regexp f
))
297 (orgdir-uname (file-name-as-directory (file-truename org-directory
)))
298 (orgdir-re (concat "\\`" (regexp-quote orgdir-uname
)))
299 uname seen rtn file link-name
)
300 ;; Make the files unique, and determine the name under which they will
302 (while (setq file
(pop files
))
303 (if (not (file-name-absolute-p file
))
304 (setq file
(expand-file-name file org-directory
)))
305 (setq uname
(file-truename file
))
306 (unless (member uname seen
)
308 (if (string-match orgdir-re uname
)
309 (setq link-name
(substring uname
(match-end 0)))
310 (setq link-name
(file-name-nondirectory uname
)))
311 (push (cons file link-name
) rtn
)))
315 (defun org-mobile-push ()
316 "Push the current state of Org affairs to the target directory.
317 This will create the index file, copy all agenda files there, and also
318 create all custom agenda views, for upload to the mobile phone."
320 (let ((org-agenda-buffer-name "*SUMO*")
321 (org-agenda-tag-filter org-agenda-tag-filter
)
322 (org-agenda-redo-command org-agenda-redo-command
))
323 ;; Offer to save agenda-related buffers before pushing, preventing
324 ;; "Non-existent agenda file" prompt for lock files (see #19448).
325 (let ((agenda-buffers (org-buffer-list 'agenda
)))
326 (save-some-buffers nil
327 (lambda () (memq (current-buffer) agenda-buffers
))))
330 (save-window-excursion
331 (run-hooks 'org-mobile-pre-push-hook
)
332 (org-mobile-check-setup)
333 (org-mobile-prepare-file-lists)
334 (message "Creating agendas...")
335 (let ((inhibit-redisplay t
)
336 (org-agenda-files (mapcar 'car org-mobile-files-alist
)))
337 (org-mobile-create-sumo-agenda))
338 (message "Creating agendas...done")
339 (org-save-all-org-buffers) ; to save any IDs created by this process
340 (message "Copying files...")
341 (org-mobile-copy-agenda-files)
342 (message "Writing index file...")
343 (org-mobile-create-index-file)
344 (message "Writing checksums...")
345 (org-mobile-write-checksums)
346 (run-hooks 'org-mobile-post-push-hook
)))))
347 (org-agenda-maybe-redo)
348 (message "Files for mobile viewer staged"))
350 (defvar org-mobile-before-process-capture-hook nil
351 "Hook that is run after content was moved to `org-mobile-inbox-for-pull'.
352 The inbox file is visited by the current buffer, and the buffer is
353 narrowed to the newly captured data.")
356 (defun org-mobile-pull ()
357 "Pull the contents of `org-mobile-capture-file' and integrate them.
358 Apply all flagged actions, flag entries to be flagged and then call an
359 agenda view showing the flagged items."
361 (org-mobile-check-setup)
362 (run-hooks 'org-mobile-pre-pull-hook
)
363 (let ((insertion-marker (org-mobile-move-capture)))
364 (if (not (markerp insertion-marker
))
365 (message "No new items")
366 (org-with-point-at insertion-marker
368 (narrow-to-region (point) (point-max))
369 (run-hooks 'org-mobile-before-process-capture-hook
)))
370 (org-with-point-at insertion-marker
371 (org-mobile-apply (point) (point-max)))
372 (move-marker insertion-marker nil
)
373 (run-hooks 'org-mobile-post-pull-hook
)
374 (when org-mobile-last-flagged-files
375 ;; Make an agenda view of flagged entries, but only in the files
376 ;; where stuff has been added.
377 (put 'org-agenda-files
'org-restrict org-mobile-last-flagged-files
)
378 (let ((org-agenda-keep-restricted-file-list t
))
379 (org-agenda nil
"?"))))))
381 (defun org-mobile-check-setup ()
382 "Check if org-mobile-directory has been set up."
383 (org-mobile-cleanup-encryption-tempfile)
384 (unless (and org-directory
385 (stringp org-directory
)
386 (string-match "\\S-" org-directory
)
387 (file-exists-p org-directory
)
388 (file-directory-p org-directory
))
390 "Please set `org-directory' to the directory where your org files live"))
391 (unless (and org-mobile-directory
392 (stringp org-mobile-directory
)
393 (string-match "\\S-" org-mobile-directory
)
394 (file-exists-p org-mobile-directory
)
395 (file-directory-p org-mobile-directory
))
397 "Variable `org-mobile-directory' must point to an existing directory"))
398 (unless (and org-mobile-inbox-for-pull
399 (stringp org-mobile-inbox-for-pull
)
400 (string-match "\\S-" org-mobile-inbox-for-pull
)
402 (file-name-directory org-mobile-inbox-for-pull
)))
404 "Variable `org-mobile-inbox-for-pull' must point to a file in an existing directory"))
405 (unless (and org-mobile-checksum-binary
406 (string-match "\\S-" org-mobile-checksum-binary
))
407 (error "No executable found to compute checksums"))
408 (when org-mobile-use-encryption
409 (unless (string-match "\\S-" (org-mobile-encryption-password))
411 "To use encryption, you must set `org-mobile-encryption-password'"))
412 (unless (file-writable-p org-mobile-encryption-tempfile
)
413 (error "Cannot write to encryption tempfile %s"
414 org-mobile-encryption-tempfile
))
415 (unless (executable-find "openssl")
416 (error "OpenSSL is needed to encrypt files"))))
418 (defun org-mobile-create-index-file ()
419 "Write the index file in the WebDAV directory."
420 (let ((files-alist (sort (copy-sequence org-mobile-files-alist
)
421 (lambda (a b
) (string< (cdr a
) (cdr b
)))))
422 (def-todo (default-value 'org-todo-keywords
))
423 (def-tags org-tag-alist
)
424 (target-file (expand-file-name org-mobile-index-file
425 org-mobile-directory
))
426 todo-kwds done-kwds tags
)
427 (when (stringp (car def-todo
))
428 (setq def-todo
(list (cons 'sequence def-todo
))))
429 (org-agenda-prepare-buffers (mapcar 'car files-alist
))
430 (setq done-kwds
(org-uniquify org-done-keywords-for-agenda
))
431 (setq todo-kwds
(org-delete-all
433 (org-uniquify org-todo-keywords-for-agenda
)))
434 (setq tags
(mapcar 'car
(org-global-tags-completion-table
435 (mapcar 'car files-alist
))))
436 (with-temp-file (if org-mobile-use-encryption org-mobile-encryption-tempfile
438 (insert "#+READONLY\n")
439 (dolist (entry def-todo
)
440 (let ((kwds (mapcar (lambda (x)
441 (if (string-match "(" x
)
442 (substring x
0 (match-beginning 0))
445 (insert "#+TODO: " (mapconcat #'identity kwds
" ") "\n")
446 (let* ((dwds (or (member "|" kwds
) (last kwds
)))
447 (twds (org-delete-all dwds kwds
)))
448 (setq todo-kwds
(org-delete-all twds todo-kwds
))
449 (setq done-kwds
(org-delete-all dwds done-kwds
)))))
450 (when (or todo-kwds done-kwds
)
451 (insert "#+TODO: " (mapconcat 'identity todo-kwds
" ") " | "
452 (mapconcat 'identity done-kwds
" ") "\n"))
453 (setq def-tags
(split-string (org-tag-alist-to-string def-tags t
)))
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 "#+ALLPRIORITIES: " org-mobile-allpriorities
"\n")
459 (when (file-exists-p (expand-file-name
460 org-mobile-directory
"agendas.org"))
461 (insert "* [[file:agendas.org][Agenda Views]]\n"))
462 (pcase-dolist (`(,_ .
,link-name
) files-alist
)
463 (insert (format "* [[file:%s][%s]]\n" link-name link-name
)))
464 (push (cons org-mobile-index-file
(md5 (buffer-string)))
465 org-mobile-checksum-files
))
466 (when org-mobile-use-encryption
467 (org-mobile-encrypt-and-move org-mobile-encryption-tempfile
469 (org-mobile-cleanup-encryption-tempfile))))
471 (defun org-mobile-copy-agenda-files ()
472 "Copy all agenda files to the stage or WebDAV directory."
473 (let ((files-alist org-mobile-files-alist
)
474 file buf entry link-name target-path target-dir check
)
475 (while (setq entry
(pop files-alist
))
476 (setq file
(car entry
) link-name
(cdr entry
))
477 (when (file-exists-p file
)
478 (setq target-path
(expand-file-name link-name org-mobile-directory
)
479 target-dir
(file-name-directory target-path
))
480 (unless (file-directory-p target-dir
)
481 (make-directory target-dir
'parents
))
482 (if org-mobile-use-encryption
483 (org-mobile-encrypt-and-move file target-path
)
484 (copy-file file target-path
'ok-if-already-exists
))
485 (setq check
(shell-command-to-string
486 (concat (shell-quote-argument org-mobile-checksum-binary
)
488 (shell-quote-argument (expand-file-name file
)))))
489 (when (string-match "[a-fA-F0-9]\\{30,40\\}" check
)
490 (push (cons link-name
(match-string 0 check
))
491 org-mobile-checksum-files
))))
493 (setq file
(expand-file-name org-mobile-capture-file
494 org-mobile-directory
))
496 (setq buf
(find-file file
))
497 (when (and (= (point-min) (point-max)))
500 (when org-mobile-use-encryption
501 (write-file org-mobile-encryption-tempfile
)
502 (org-mobile-encrypt-and-move org-mobile-encryption-tempfile file
)))
503 (push (cons org-mobile-capture-file
(md5 (buffer-string)))
504 org-mobile-checksum-files
))
505 (org-mobile-cleanup-encryption-tempfile)
508 (defun org-mobile-write-checksums ()
509 "Create checksums for all files in `org-mobile-directory'.
510 The table of checksums is written to the file mobile-checksums."
511 (let ((sumfile (expand-file-name "checksums.dat" org-mobile-directory
))
512 (files org-mobile-checksum-files
)
514 (with-temp-file sumfile
515 (set-buffer-file-coding-system 'undecided-unix nil
)
516 (while (setq entry
(pop files
))
517 (setq file
(car entry
) sum
(cdr entry
))
518 (insert (format "%s %s\n" sum file
))))))
520 (defun org-mobile-sumo-agenda-command ()
521 "Return an agenda custom command that comprises all custom commands."
523 ;; normalize different versions
527 (cond ((stringp (cdr x
)) nil
)
528 ((stringp (nth 1 x
)) x
)
529 ((not (nth 1 x
)) (cons (car x
) (cons "" (cddr x
))))
530 (t (cons (car x
) (cons "" (cdr x
))))))
531 org-agenda-custom-commands
)))
532 (default-list '(("a" "Agenda" agenda
) ("t" "All TODO" alltodo
)))
533 thelist atitle new e key desc type match settings cmds gkey gdesc gsettings cnt
)
535 ((eq org-mobile-agendas
'custom
)
536 (setq thelist custom-list
))
537 ((eq org-mobile-agendas
'default
)
538 (setq thelist default-list
))
539 ((eq org-mobile-agendas
'all
)
540 (setq thelist custom-list
)
541 (unless (assoc "t" thelist
) (push '("t" "ALL TODO" alltodo
) thelist
))
542 (unless (assoc "a" thelist
) (push '("a" "Agenda" agenda
) thelist
)))
543 ((listp org-mobile-agendas
)
544 (setq thelist
(append custom-list default-list
))
545 (setq thelist
(delq nil
(mapcar (lambda (k) (assoc k thelist
))
546 org-mobile-agendas
)))))
547 (while (setq e
(pop thelist
))
550 ;; this is a description entry - skip it
552 ((eq (nth 2 e
) 'search
)
553 ;; Search view is interactive, skip
555 ((memq (nth 2 e
) '(todo-tree tags-tree occur-tree
))
556 ;; These are trees, not really agenda commands
558 ((and (memq (nth 2 e
) '(todo tags tags-todo
))
560 (not (string-match "\\S-" (nth 3 e
)))))
561 ;; These would be interactive because the match string is empty
563 ((memq (nth 2 e
) '(agenda alltodo todo tags tags-todo
))
565 (setq key
(car e
) desc
(nth 1 e
) type
(nth 2 e
) match
(nth 3 e
)
568 (cons (list 'org-agenda-title-append
569 (concat "<after>KEYS=" key
" TITLE: "
570 (if (and (stringp desc
) (> (length desc
) 0))
571 desc
(symbol-name type
))
574 (push (list type match settings
) new
))
575 ((or (functionp (nth 2 e
)) (symbolp (nth 2 e
)))
576 ;; A user-defined function, which can do anything, so simply
581 (setq gkey
(car e
) gdesc
(nth 1 e
) gsettings
(nth 3 e
) cmds
(nth 2 e
))
583 (while (setq e
(pop cmds
))
584 (setq type
(car e
) match
(nth 1 e
) settings
(nth 2 e
))
585 (setq atitle
(if (string= "" gdesc
) match gdesc
))
586 (setq settings
(append gsettings settings
))
588 (cons (list 'org-agenda-title-append
589 (concat "<after>KEYS=" gkey
"#" (number-to-string
591 " TITLE: " atitle
"</after>"))
593 (push (list type match settings
) new
)))))
594 (and new
(list "X" "SUMO" (reverse new
)
595 '((org-agenda-compact-blocks nil
))))))
597 (defvar org-mobile-creating-agendas nil
)
598 (defun org-mobile-write-agenda-for-mobile (file)
599 (let ((all (buffer-string)) in-date id pl prefix line app short m sexp
)
602 (insert "#+READONLY\n")
604 (goto-char (point-min))
607 ((looking-at "[ \t]*$")) ; keep empty lines
609 ;; remove underlining
610 (delete-region (point) (point-at-eol)))
611 ((get-text-property (point) 'org-agenda-structural-header
)
613 (setq app
(get-text-property (point) 'org-agenda-title-append
))
614 (setq short
(get-text-property (point) 'short-heading
))
615 (when (and short
(looking-at ".+"))
616 (replace-match short nil t
)
617 (beginning-of-line 1))
621 (beginning-of-line 1))
623 ((get-text-property (point) 'org-agenda-date-header
)
626 ((setq m
(or (get-text-property (point) 'org-hd-marker
)
627 (get-text-property (point) 'org-marker
)))
628 (setq sexp
(member (get-text-property (point) 'type
)
630 (if (setq pl
(text-property-any (point) (point-at-eol) 'org-heading t
))
632 (setq prefix
(org-trim (buffer-substring
634 line
(org-trim (buffer-substring
637 (delete-region (point-at-bol) (point-at-eol))
638 (insert line
"<before>" prefix
"</before>")
639 (beginning-of-line 1))
640 (and (looking-at "[ \t]+") (replace-match "")))
641 (insert (if in-date
"*** " "** "))
645 (insert (org-agenda-get-some-entry-text
649 (if (bound-and-true-p
650 org-mobile-force-id-on-agenda-items
)
651 (org-id-get m
'create
)
652 (or (org-entry-get m
"ID")
653 (org-mobile-get-outline-path-link m
))))
654 (insert " :PROPERTIES:\n :ORIGINAL_ID: " id
656 (beginning-of-line 2))
657 (push (cons "agendas.org" (md5 (buffer-string)))
658 org-mobile-checksum-files
))
659 (message "Agenda written to Org file %s" file
)))
661 (defun org-mobile-get-outline-path-link (pom)
662 (org-with-point-at pom
664 (org-mobile-escape-olp (file-name-nondirectory buffer-file-name
))
666 (mapconcat 'org-mobile-escape-olp
667 (org-get-outline-path)
670 (org-mobile-escape-olp (nth 4 (org-heading-components))))))
672 (defun org-mobile-escape-olp (s)
673 (let ((table '(?
: ?
/)))
674 (org-link-escape s table
)))
676 (defun org-mobile-create-sumo-agenda ()
677 "Create a file that contains all custom agenda views."
679 (let* ((file (expand-file-name "agendas.org"
680 org-mobile-directory
))
681 (file1 (if org-mobile-use-encryption
682 org-mobile-encryption-tempfile
684 (sumo (org-mobile-sumo-agenda-command))
685 (org-agenda-custom-commands
686 (list (append sumo
(list (list file1
)))))
687 (org-mobile-creating-agendas t
))
688 (unless (file-writable-p file1
)
689 (error "Cannot write to file %s" file1
))
691 (org-store-agenda-views))
692 (when org-mobile-use-encryption
693 (org-mobile-encrypt-and-move file1 file
)
695 (org-mobile-cleanup-encryption-tempfile))))
697 (defun org-mobile-encrypt-and-move (infile outfile
)
698 "Encrypt INFILE locally to INFILE_enc, then move it to OUTFILE.
699 We do this in two steps so that remote paths will work, even if the
700 encryption program does not understand them."
701 (let ((encfile (concat infile
"_enc")))
702 (org-mobile-encrypt-file infile encfile
)
704 (copy-file encfile outfile
'ok-if-already-exists
)
705 (delete-file encfile
))))
707 (defun org-mobile-encrypt-file (infile outfile
)
708 "Encrypt INFILE to OUTFILE, using `org-mobile-encryption-password'."
710 (format "openssl enc -md md5 -aes-256-cbc -salt -pass %s -in %s -out %s"
711 (shell-quote-argument (concat "pass:"
712 (org-mobile-encryption-password)))
713 (shell-quote-argument (expand-file-name infile
))
714 (shell-quote-argument (expand-file-name outfile
)))))
716 (defun org-mobile-decrypt-file (infile outfile
)
717 "Decrypt INFILE to OUTFILE, using `org-mobile-encryption-password'."
719 (format "openssl enc -md md5 -d -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-cleanup-encryption-tempfile ()
726 "Remove the encryption tempfile if it exists."
727 (and (stringp org-mobile-encryption-tempfile
)
728 (file-exists-p org-mobile-encryption-tempfile
)
729 (delete-file org-mobile-encryption-tempfile
)))
731 (defun org-mobile-move-capture ()
732 "Move the contents of the capture file to the inbox file.
733 Return a marker to the location where the new content has been added.
734 If nothing new has been added, return nil."
737 (capture-file (expand-file-name org-mobile-capture-file
738 org-mobile-directory
))
739 (inbox-buffer (find-file-noselect org-mobile-inbox-for-pull
))
741 (if (not org-mobile-use-encryption
)
742 (find-file-noselect capture-file
)
743 (org-mobile-cleanup-encryption-tempfile)
744 (setq encfile
(concat org-mobile-encryption-tempfile
"_enc"))
745 (copy-file capture-file encfile
)
746 (org-mobile-decrypt-file encfile org-mobile-encryption-tempfile
)
747 (find-file-noselect org-mobile-encryption-tempfile
)))
748 (insertion-point (make-marker))
750 (with-current-buffer capture-buffer
751 (setq content
(buffer-string))
752 (setq not-empty
(string-match "\\S-" content
))
754 (set-buffer inbox-buffer
)
756 (goto-char (point-max))
757 (or (bolp) (newline))
758 (move-marker insertion-point
759 (prog1 (point) (insert content
)))
761 (set-buffer capture-buffer
)
764 (org-mobile-update-checksum-for-capture-file (buffer-string))))
765 (kill-buffer capture-buffer
)
766 (when org-mobile-use-encryption
767 (org-mobile-encrypt-and-move org-mobile-encryption-tempfile
769 (org-mobile-cleanup-encryption-tempfile))
770 (if not-empty insertion-point
)))
772 (defun org-mobile-update-checksum-for-capture-file (buffer-string)
773 "Find the checksum line and modify it to match BUFFER-STRING."
774 (let* ((file (expand-file-name "checksums.dat" org-mobile-directory
))
775 (buffer (find-file-noselect file
)))
777 (with-current-buffer buffer
778 (when (re-search-forward (concat "\\([0-9a-fA-F]\\{30,\\}\\).*?"
779 (regexp-quote org-mobile-capture-file
)
781 (goto-char (match-beginning 1))
782 (delete-region (match-beginning 1) (match-end 1))
783 (insert (md5 buffer-string
))
785 (kill-buffer buffer
))))
787 (defun org-mobile-apply (&optional beg end
)
788 "Apply all change requests in the current buffer.
789 If BEG and END are given, only do this in that region."
791 (require 'org-archive
)
792 (setq org-mobile-last-flagged-files nil
)
793 (setq beg
(or beg
(point-min)) end
(or end
(point-max)))
795 ;; Remove all Note IDs
797 (while (re-search-forward "^\\*\\* Note ID: [-0-9A-F]+[ \t]*\n" end t
)
800 ;; Find all the referenced entries, without making any changes yet
801 (let ((marker (make-marker))
802 (bos-marker (make-marker))
803 (end (move-marker (make-marker) end
))
811 ;; Count the new captures
813 (while (re-search-forward "^\\* \\(.*\\)" end t
)
814 (and (>= (- (match-end 1) (match-beginning 1)) 2)
815 (not (equal (downcase (substring (match-string 1) 0 2)) "f("))
818 ;; Find and apply the edits
820 (while (re-search-forward
821 "^\\*+[ \t]+F(\\([^():\n]*\\)\\(:\\([^()\n]*\\)\\)?)[ \t]+\\[\\[\\(\\(id\\|olp\\):\\([^]\n]+\\)\\)" end t
)
823 (let* ((action (match-string 1))
824 (data (and (match-end 3) (match-string 3)))
825 (id-pos (condition-case msg
826 (org-mobile-locate-entry (match-string 4))
827 (error (nth 1 msg
))))
828 (bos (line-beginning-position))
829 (eos (save-excursion (org-end-of-subtree t t
)))
830 (cmd (if (equal action
"")
831 (let ((note (buffer-substring-no-properties
832 (line-beginning-position 2) eos
)))
833 (lambda (_data _old _new
)
835 (org-toggle-tag "FLAGGED" 'on
)
837 nil
"THEFLAGGINGNOTE"
838 (replace-regexp-in-string "\n" "\\\\n" note
))))
840 (cdr (assoc action org-mobile-action-alist
))))
841 ;; Do not take notes interactively.
842 (org-inhibit-logging 'note
)
846 (when (and (markerp id-pos
)
847 (not (member (marker-buffer id-pos
) buf-list
)))
848 (org-mobile-timestamp-buffer (marker-buffer id-pos
))
849 (push (marker-buffer id-pos
) buf-list
))
850 (unless (markerp id-pos
)
851 (goto-char (+ 2 (point-at-bol)))
854 (insert "BAD REFERENCE "))
861 (move-marker bos-marker
(point))
862 (if (re-search-forward "^** Old value[ \t]*$" eos t
)
863 (setq old
(buffer-substring
865 (progn (outline-next-heading) (point)))))
866 (if (re-search-forward "^** New value[ \t]*$" eos t
)
867 (setq new
(buffer-substring
869 (progn (outline-next-heading)
870 (if (eobp) (org-back-over-empty-lines))
872 (setq old
(org-string-nw-p old
))
873 (setq new
(org-string-nw-p new
))
874 (unless (equal data
"body")
875 (setq new
(and new
(org-trim new
)))
876 (setq old
(and old
(org-trim old
))))
877 (goto-char (+ 2 bos-marker
))
878 ;; Remember this place so that we can return
879 (move-marker marker
(point))
880 (setq org-mobile-error nil
)
882 (org-with-point-at id-pos
883 (funcall cmd data old new
)
884 (unless (member data
'("delete" "archive" "archive-sibling"
886 (when (member "FLAGGED" (org-get-tags nil t
))
887 (add-to-list 'org-mobile-last-flagged-files
888 (buffer-file-name)))))
889 (error (setq org-mobile-error msg
)))
890 (when org-mobile-error
891 (pop-to-buffer-same-window (marker-buffer marker
))
894 (insert (if (stringp (nth 1 org-mobile-error
))
895 (nth 1 org-mobile-error
)
899 ;; If we get here, the action has been applied successfully
900 ;; So remove the entry
901 (goto-char bos-marker
)
902 (delete-region (point) (org-end-of-subtree t t
)))))
904 (move-marker marker nil
)
905 (move-marker end nil
)
906 (message "%d new, %d edits, %d flags, %d errors"
907 cnt-new cnt-edit cnt-flag cnt-error
)
910 (defun org-mobile-timestamp-buffer (buf)
911 "Time stamp buffer BUF, just to make sure its checksum will change."
912 (with-current-buffer buf
916 (goto-char (point-min))
917 (if (re-search-forward
918 "^\\([ \t]*\\)#\\+LAST_MOBILE_CHANGE:.*\n?" nil t
)
920 (goto-char (match-end 1))
921 (delete-region (point) (match-end 0)))
922 (if (looking-at ".*?-\\*-.*-\\*-")
924 (insert "#+LAST_MOBILE_CHANGE: "
925 (format-time-string "%Y-%m-%d %T") "\n")))))
927 (defun org-mobile-smart-read ()
928 "Parse the entry at point for shortcuts and expand them.
929 These shortcuts are meant for fast and easy typing on the limited
930 keyboards of a mobile device. Below we show a list of the shortcuts
931 currently implemented.
933 The entry is expected to contain an inactive time stamp indicating when
934 the entry was created. When setting dates and
935 times (for example for deadlines), the time strings are interpreted
936 relative to that creation date.
937 Abbreviations are expected to take up entire lines, just because it is so
938 easy to type RET on a mobile device. Abbreviations start with one or two
939 letters, followed immediately by a dot and then additional information.
940 Generally the entire shortcut line is removed after action have been taken.
941 Time stamps will be constructed using `org-read-date'. So for example a
942 line \"dd. 2tue\" will set a deadline on the second Tuesday after the
945 Here are the shortcuts currently implemented:
947 dd. string set deadline
948 ss. string set scheduling
949 tt. string set time tamp, here.
950 ti. string set inactive time
952 tg. tag1 tag2 tag3 set all these tags, change case where necessary
953 td. kwd set this todo keyword, change case where necessary
955 FIXME: Hmmm, not sure if we can make his work against the
956 auto-correction feature. Needs a bit more thinking. So this function
957 is currently a noop.")
959 (defun org-mobile-locate-entry (link)
960 (if (string-match "\\`id:\\(.*\\)$" link
)
961 (org-id-find (match-string 1 link
) 'marker
)
962 (if (not (string-match "\\`olp:\\(.*?\\):\\(.*\\)$" link
))
963 ; not found with path, but maybe it is to be inserted
964 ; in top level of the file?
965 (if (not (string-match "\\`olp:\\(.*?\\)$" link
))
967 (let ((file (match-string 1 link
)))
968 (setq file
(org-link-unescape file
))
969 (setq file
(expand-file-name file org-directory
))
972 (goto-char (point-max))
974 (goto-char (point-max))
976 (let ((file (match-string 1 link
))
977 (path (match-string 2 link
)))
978 (setq file
(org-link-unescape file
))
979 (setq file
(expand-file-name file org-directory
))
980 (setq path
(mapcar 'org-link-unescape
981 (org-split-string path
"/")))
982 (org-find-olp (cons file path
))))))
984 (defun org-mobile-edit (what old new
)
985 "Edit item WHAT in the current entry by replacing OLD with NEW.
986 WHAT can be \"heading\", \"todo\", \"tags\", \"priority\", or \"body\".
987 The edit only takes place if the current value is equal (except for
988 white space) the OLD. If this is so, OLD will be replace by NEW
989 and the command will return t. If something goes wrong, a string will
990 be returned that indicates what went wrong."
991 (let (current old1 new1 level
)
992 (if (stringp what
) (setq what
(intern what
)))
996 ((memq what
'(todo todostate
))
997 (setq current
(org-get-todo-state))
999 ((equal new
"DONEARCHIVE")
1001 (org-archive-subtree-default))
1002 ((equal new current
) t
) ; nothing needs to be done
1003 ((or (equal current old
)
1004 (eq org-mobile-force-mobile-change t
)
1005 (memq 'todo org-mobile-force-mobile-change
))
1006 (org-todo (or new
'none
)) t
)
1007 (t (error "State before change was expected as \"%s\", but is \"%s\""
1011 (setq current
(org-get-tags nil t
)
1012 new1
(and new
(org-split-string new
":+"))
1013 old1
(and old
(org-split-string old
":+")))
1015 ((org-mobile-tags-same-p current new1
) t
) ; no change needed
1016 ((or (org-mobile-tags-same-p current old1
)
1017 (eq org-mobile-force-mobile-change t
)
1018 (memq 'tags org-mobile-force-mobile-change
))
1019 (org-set-tags new1
) t
)
1020 (t (error "Tags before change were expected as \"%s\", but are \"%s\""
1021 (or old
"") (or current
"")))))
1023 ((eq what
'priority
)
1024 (let ((case-fold-search nil
))
1025 (when (looking-at org-complex-heading-regexp
)
1026 (let ((current (and (match-end 3) (substring (match-string 3) 2 3))))
1028 ((equal current new
) t
) ;no action required
1029 ((or (equal current old
)
1030 (eq org-mobile-force-mobile-change t
)
1031 (memq 'tags org-mobile-force-mobile-change
))
1032 (org-priority (and new
(string-to-char new
))))
1033 (t (error "Priority was expected to be %s, but is %s"
1037 (let ((case-fold-search nil
))
1038 (when (looking-at org-complex-heading-regexp
)
1039 (let ((current (match-string 4)))
1041 ((equal current new
) t
) ;no action required
1042 ((or (equal current old
)
1043 (eq org-mobile-force-mobile-change t
)
1044 (memq 'heading org-mobile-force-mobile-change
))
1045 (goto-char (match-beginning 4))
1047 (delete-region (point) (+ (point) (length current
)))
1051 "Heading changed in the mobile device and on the computer")))))))
1053 ((eq what
'addheading
)
1054 (if (org-at-heading-p) ; if false we are in top-level of file
1056 ;; Workaround a `org-insert-heading-respect-content' bug
1057 ;; which prevents correct insertion when point is invisible
1060 (org-insert-heading-respect-content t
)
1068 (org-with-point-at (org-mobile-locate-entry new
)
1069 (if (org-at-heading-p) ; if false we are in top-level of file
1071 (setq level
(org-get-valid-level (funcall outline-level
) 1))
1072 (org-end-of-subtree t t
)
1073 (org-paste-subtree level
))
1074 (org-paste-subtree 1)))
1081 (org-archive-subtree))
1083 ((eq what
'archive-sibling
)
1084 (org-archive-to-archive-sibling))
1087 (setq current
(buffer-substring (min (1+ (point-at-eol)) (point-max))
1088 (save-excursion (outline-next-heading)
1090 (if (not (string-match "\\S-" current
)) (setq current nil
))
1092 ((org-mobile-bodies-same-p current new
) t
) ; no action necessary
1093 ((or (org-mobile-bodies-same-p current old
)
1094 (eq org-mobile-force-mobile-change t
)
1095 (memq 'body org-mobile-force-mobile-change
))
1099 (or (bolp) (insert "\n"))
1100 (delete-region (point) (progn (org-back-to-heading t
)
1101 (outline-next-heading)
1105 "Body was changed in the mobile device and on the computer")))))))
1107 (defun org-mobile-tags-same-p (list1 list2
)
1108 "Are the two tag lists the same?"
1109 (not (or (org-delete-all list1 list2
)
1110 (org-delete-all list2 list1
))))
1112 (defun org-mobile-bodies-same-p (a b
)
1113 "Compare if A and B are visually equal strings.
1114 We first remove leading and trailing white space from the entire strings.
1115 Then we split the strings into lines and remove leading/trailing whitespace
1116 from each line. Then we compare.
1117 A and B must be strings or nil."
1119 ((and (not a
) (not b
)) t
)
1120 ((or (not a
) (not b
)) nil
)
1121 (t (setq a
(org-trim a
) b
(org-trim b
))
1122 (setq a
(mapconcat 'identity
(org-split-string a
"[ \t]*\n[ \t]*") "\n"))
1123 (setq b
(mapconcat 'identity
(org-split-string b
"[ \t]*\n[ \t]*") "\n"))
1126 (provide 'org-mobile
)
1129 ;; generated-autoload-file: "org-loaddefs.el"
1132 ;;; org-mobile.el ends here