Commit | Line | Data |
---|---|---|
8d642074 | 1 | ;;; org-mobile.el --- Code for asymmetric sync with a mobile device |
cbd20947 | 2 | ;; Copyright (C) 2009-2011 Free Software Foundation, Inc. |
8d642074 CD |
3 | ;; |
4 | ;; Author: Carsten Dominik <carsten at orgmode dot org> | |
5 | ;; Keywords: outlines, hypermedia, calendar, wp | |
6 | ;; Homepage: http://orgmode.org | |
3ab2c837 | 7 | ;; Version: 7.7 |
8d642074 CD |
8 | ;; |
9 | ;; This file is part of GNU Emacs. | |
10 | ;; | |
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. | |
15 | ;; | |
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. | |
20 | ;; | |
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/>. | |
23 | ;; | |
24 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
25 | ;; | |
26 | ;;; Commentary: | |
27 | ;; | |
28 | ;; This file contains the code to interact with Richard Moreland's iPhone | |
afe98dfa CD |
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 | |
33 | ;; be used. | |
8d642074 CD |
34 | |
35 | (require 'org) | |
36 | (require 'org-agenda) | |
86fbb8ca CD |
37 | ;;; Code: |
38 | ||
8bfe682a | 39 | (eval-when-compile (require 'cl)) |
8d642074 CD |
40 | |
41 | (defgroup org-mobile nil | |
8bfe682a | 42 | "Options concerning support for a viewer/editor on a mobile device." |
8d642074 CD |
43 | :tag "Org Mobile" |
44 | :group 'org) | |
45 | ||
46 | (defcustom org-mobile-files '(org-agenda-files) | |
47 | "Files to be staged for MobileOrg. | |
8bfe682a | 48 | This is basically a list of files and directories. Files will be staged |
8d642074 CD |
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: | |
51 | ||
52 | org-agenda-files | |
ed21c5c8 | 53 | This means include the complete, unrestricted list of files given in |
8d642074 CD |
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'" | |
58 | :group 'org-mobile | |
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" | |
64 | (file)))) | |
65 | ||
3ab2c837 BG |
66 | (defcustom org-mobile-files-exclude-regexp "" |
67 | "A regexp to exclude files from `org-mobile-files'." | |
68 | :group 'org-mobile | |
69 | :type 'regexp) | |
70 | ||
8d642074 CD |
71 | (defcustom org-mobile-directory "" |
72 | "The WebDAV directory where the interaction with the mobile takes place." | |
73 | :group 'org-mobile | |
74 | :type 'directory) | |
75 | ||
ed21c5c8 | 76 | (defcustom org-mobile-use-encryption nil |
86fbb8ca | 77 | "Non-nil means keep only encrypted files on the WebDAV server. |
ed21c5c8 CD |
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 | |
86fbb8ca CD |
82 | application. Before turning this on, check of MobileOrg does already |
83 | support it - at the time of this writing it did not yet." | |
ed21c5c8 CD |
84 | :group 'org-mobile |
85 | :type 'boolean) | |
86 | ||
87 | (defcustom org-mobile-encryption-tempfile "~/orgtmpcrypt" | |
88 | "File that is being used as a temporary file for encryption. | |
86fbb8ca | 89 | This must be local file on your local machine (not on the WebDAV server). |
ed21c5c8 CD |
90 | You might want to put this file into a directory where only you have access." |
91 | :group 'org-mobile | |
92 | :type 'directory) | |
93 | ||
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. | |
afe98dfa CD |
99 | |
100 | SECURITY CONSIDERATIONS: | |
101 | ||
86fbb8ca | 102 | Note that, when Org runs the encryption commands, the password could |
afe98dfa CD |
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. | |
105 | ||
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 | |
110 | session." | |
ed21c5c8 CD |
111 | :group 'org-mobile |
112 | :type '(string :tag "Password")) | |
113 | ||
afe98dfa CD |
114 | (defvar org-mobile-encryption-password-session nil) |
115 | ||
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)))) | |
121 | ||
8d642074 CD |
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 | |
8bfe682a CD |
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." | |
8d642074 CD |
128 | :group 'org-mobile |
129 | :type 'file) | |
130 | ||
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.") | |
134 | ||
135 | (defcustom org-mobile-index-file "index.org" | |
3ab2c837 | 136 | "The index file with links to all Org files that should be loaded by MobileOrg. |
8d642074 CD |
137 | Relative to `org-mobile-directory'. The Address field in the MobileOrg setup |
138 | should point to this file." | |
139 | :group 'org-mobile | |
140 | :type 'file) | |
141 | ||
ed21c5c8 CD |
142 | (defcustom org-mobile-agendas 'all |
143 | "The agendas that should be pushed to MobileOrg. | |
144 | Allowed values: | |
145 | ||
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." | |
150 | :group 'org-mobile | |
151 | :type '(choice | |
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")))) | |
157 | ||
8d642074 | 158 | (defcustom org-mobile-force-id-on-agenda-items t |
afe98dfa | 159 | "Non-nil means make all agenda items carry an ID." |
8d642074 CD |
160 | :group 'org-mobile |
161 | :type 'boolean) | |
162 | ||
8bfe682a | 163 | (defcustom org-mobile-force-mobile-change nil |
ed21c5c8 | 164 | "Non-nil means force the change made on the mobile device. |
8bfe682a CD |
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." | |
170 | :group 'org-mobile | |
171 | :type '(choice | |
172 | (const :tag "Always" t) | |
173 | (const :tag "Never" nil) | |
174 | (set :greedy t :tag "Specify" | |
175 | (const todo) | |
176 | (const tags) | |
177 | (const priority) | |
178 | (const heading) | |
179 | (const body)))) | |
180 | ||
8d642074 | 181 | (defcustom org-mobile-action-alist |
8bfe682a | 182 | '(("edit" . (org-mobile-edit data old new))) |
8d642074 CD |
183 | "Alist with flags and actions for mobile sync. |
184 | When flagging an entry, MobileOrg will create entries that look like | |
185 | ||
186 | * F(action:data) [[id:entry-id][entry title]] | |
187 | ||
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 | |
8bfe682a CD |
194 | of that entry. |
195 | ||
196 | For now, it is not recommended to change this variable." | |
8d642074 CD |
197 | :group 'org-mobile |
198 | :type '(repeat | |
199 | (cons (string :tag "Action flag") | |
200 | (sexp :tag "Action form")))) | |
201 | ||
8bfe682a CD |
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." | |
207 | :group 'org-mobile | |
208 | :type 'string) | |
209 | ||
8d642074 CD |
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.") | |
216 | ||
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'.") | |
223 | ||
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'.") | |
230 | ||
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'.") | |
237 | ||
238 | (defvar org-mobile-last-flagged-files nil | |
8bfe682a | 239 | "List of files containing entries flagged in the latest pull.") |
8d642074 CD |
240 | |
241 | (defvar org-mobile-files-alist nil) | |
242 | (defvar org-mobile-checksum-files nil) | |
243 | ||
244 | (defun org-mobile-prepare-file-lists () | |
245 | (setq org-mobile-files-alist (org-mobile-files-alist)) | |
8bfe682a | 246 | (setq org-mobile-checksum-files nil)) |
8d642074 CD |
247 | |
248 | (defun org-mobile-files-alist () | |
3ab2c837 BG |
249 | "Expand the list in `org-mobile-files' to a list of existing files. |
250 | Also exclude files matching `org-mobile-files-exclude-regexp'." | |
8bfe682a CD |
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) | |
254 | t)) | |
255 | (files | |
256 | (apply 'append | |
257 | (mapcar | |
258 | (lambda (f) | |
259 | (cond | |
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 | |
264 | (copy-sequence | |
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)) | |
269 | (list f)) | |
270 | (t nil))) | |
271 | org-mobile-files))) | |
3ab2c837 BG |
272 | (files (delete |
273 | nil | |
274 | (mapcar (lambda (f) | |
275 | (unless (and (not (string= org-mobile-files-exclude-regexp "")) | |
276 | (string-match org-mobile-files-exclude-regexp f)) | |
277 | (identity f))) | |
278 | files))) | |
8d642074 CD |
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 | |
283 | ;; be listed. | |
284 | (while (setq file (pop files)) | |
8bfe682a CD |
285 | (if (not (file-name-absolute-p file)) |
286 | (setq file (expand-file-name file org-directory))) | |
8d642074 CD |
287 | (setq uname (file-truename file)) |
288 | (unless (member uname seen) | |
289 | (push 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))) | |
294 | (nreverse rtn))) | |
295 | ||
296 | ;;;###autoload | |
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." | |
301 | (interactive) | |
8bfe682a CD |
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)) | |
306 | (save-excursion | |
307 | (save-window-excursion | |
3ab2c837 | 308 | (run-hooks 'org-mobile-pre-push-hook) |
8bfe682a CD |
309 | (org-mobile-check-setup) |
310 | (org-mobile-prepare-file-lists) | |
8bfe682a CD |
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)))) | |
322 | (redraw-display) | |
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)) | |
8bfe682a CD |
328 | (org-agenda-redo) |
329 | (select-window cw))))) | |
8d642074 | 330 | (message "Files for mobile viewer staged")) |
ed21c5c8 | 331 | |
8bfe682a CD |
332 | (defvar org-mobile-before-process-capture-hook nil |
333 | "Hook that is run after content was moved to `org-mobile-inbox-for-pull'. | |
ed21c5c8 CD |
334 | The inbox file is visited by the current buffer, and the buffer is |
335 | narrowed to the newly captured data.") | |
8d642074 CD |
336 | |
337 | ;;;###autoload | |
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." | |
342 | (interactive) | |
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 | |
8bfe682a CD |
349 | (save-restriction |
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))) | |
8d642074 CD |
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) | |
8bfe682a | 360 | (let ((org-agenda-keep-restricted-file-list t)) |
8d642074 CD |
361 | (org-agenda nil "?")))))) |
362 | ||
363 | (defun org-mobile-check-setup () | |
364 | "Check if org-mobile-directory has been set up." | |
afe98dfa | 365 | (org-mobile-cleanup-encryption-tempfile) |
8bfe682a CD |
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)) | |
371 | (error | |
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)) | |
8d642074 CD |
378 | (error |
379 | "Variable `org-mobile-directory' must point to an existing directory")) | |
8bfe682a CD |
380 | (unless (and org-mobile-inbox-for-pull |
381 | (stringp org-mobile-inbox-for-pull) | |
382 | (string-match "\\S-" org-mobile-inbox-for-pull) | |
383 | (file-exists-p | |
384 | (file-name-directory org-mobile-inbox-for-pull))) | |
8d642074 | 385 | (error |
ed21c5c8 | 386 | "Variable `org-mobile-inbox-for-pull' must point to a file in an existing directory")) |
86fbb8ca CD |
387 | (unless (and org-mobile-checksum-binary |
388 | (string-match "\\S-" org-mobile-checksum-binary)) | |
389 | (error "No executable found to compute checksums")) | |
ed21c5c8 | 390 | (when org-mobile-use-encryption |
afe98dfa | 391 | (unless (string-match "\\S-" (org-mobile-encryption-password)) |
ed21c5c8 CD |
392 | (error |
393 | "To use encryption, you must set `org-mobile-encryption-password'")) | |
394 | (unless (file-writable-p org-mobile-encryption-tempfile) | |
86fbb8ca | 395 | (error "Cannot write to encryption tempfile %s" |
ed21c5c8 CD |
396 | org-mobile-encryption-tempfile)) |
397 | (unless (executable-find "openssl") | |
86fbb8ca | 398 | (error "openssl is needed to encrypt files")))) |
8d642074 CD |
399 | |
400 | (defun org-mobile-create-index-file () | |
401 | "Write the index file in the WebDAV directory." | |
8bfe682a CD |
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)) | |
afe98dfa CD |
406 | (target-file (expand-file-name org-mobile-index-file |
407 | org-mobile-directory)) | |
8bfe682a | 408 | file link-name todo-kwds done-kwds tags drawers entry kwds dwds twds) |
ed21c5c8 | 409 | |
8d642074 CD |
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 | |
413 | done-kwds | |
414 | (org-uniquify org-todo-keywords-for-agenda))) | |
415 | (setq drawers (org-uniquify org-drawers-for-agenda)) | |
416 | (setq tags (org-uniquify | |
417 | (delq nil | |
418 | (mapcar | |
419 | (lambda (e) | |
420 | (cond ((stringp e) e) | |
421 | ((listp e) | |
422 | (if (stringp (car e)) (car e) nil)) | |
423 | (t nil))) | |
424 | org-tag-alist-for-agenda)))) | |
425 | (with-temp-file | |
afe98dfa CD |
426 | (if org-mobile-use-encryption |
427 | org-mobile-encryption-tempfile | |
428 | target-file) | |
8bfe682a CD |
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)) | |
433 | x)) | |
434 | (cdr entry))) | |
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 | |
444 | (lambda (x) | |
445 | (cond ((null x) nil) | |
446 | ((stringp x) x) | |
447 | ((eq (car x) :startgroup) "{") | |
448 | ((eq (car x) :endgroup) "}") | |
449 | ((eq (car x) :newline) nil) | |
450 | ((listp x) (car x)) | |
451 | (t nil))) | |
452 | def-tags)) | |
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")) | |
8d642074 CD |
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))) | |
8bfe682a | 468 | (push (cons org-mobile-index-file (md5 (buffer-string))) |
afe98dfa CD |
469 | org-mobile-checksum-files)) |
470 | (when org-mobile-use-encryption | |
471 | (org-mobile-encrypt-and-move org-mobile-encryption-tempfile | |
472 | target-file) | |
473 | (org-mobile-cleanup-encryption-tempfile)))) | |
8d642074 CD |
474 | |
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) | |
8bfe682a | 478 | file buf entry link-name target-path target-dir check) |
8d642074 CD |
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) | |
8bfe682a | 485 | (make-directory target-dir 'parents)) |
ed21c5c8 CD |
486 | (if org-mobile-use-encryption |
487 | (org-mobile-encrypt-and-move file target-path) | |
488 | (copy-file file target-path 'ok-if-exists)) | |
8bfe682a CD |
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)))) | |
afe98dfa | 495 | |
8d642074 CD |
496 | (setq file (expand-file-name org-mobile-capture-file |
497 | org-mobile-directory)) | |
8bfe682a CD |
498 | (save-excursion |
499 | (setq buf (find-file file)) | |
afe98dfa CD |
500 | (when (and (= (point-min) (point-max))) |
501 | (insert "\n") | |
502 | (save-buffer) | |
503 | (when org-mobile-use-encryption | |
504 | (write-file org-mobile-encryption-tempfile) | |
505 | (org-mobile-encrypt-and-move org-mobile-encryption-tempfile file))) | |
8bfe682a CD |
506 | (push (cons org-mobile-capture-file (md5 (buffer-string))) |
507 | org-mobile-checksum-files)) | |
afe98dfa | 508 | (org-mobile-cleanup-encryption-tempfile) |
8bfe682a | 509 | (kill-buffer buf))) |
8d642074 CD |
510 | |
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." | |
8bfe682a CD |
514 | (let ((sumfile (expand-file-name "checksums.dat" org-mobile-directory)) |
515 | (files org-mobile-checksum-files) | |
516 | entry file sum) | |
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)))))) | |
8d642074 CD |
522 | |
523 | (defun org-mobile-sumo-agenda-command () | |
524 | "Return an agenda custom command that comprises all custom commands." | |
525 | (let ((custom-list | |
526 | ;; normalize different versions | |
527 | (delq nil | |
528 | (mapcar | |
529 | (lambda (x) | |
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))) | |
ed21c5c8 CD |
535 | (default-list '(("a" "Agenda" agenda) ("t" "All TODO" alltodo))) |
536 | thelist new e key desc type match settings cmds gkey gdesc gsettings cnt) | |
537 | (cond | |
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)) | |
8d642074 CD |
551 | (cond |
552 | ((stringp (cdr e)) | |
553 | ;; this is a description entry - skip it | |
554 | ) | |
555 | ((eq (nth 2 e) 'search) | |
556 | ;; Search view is interactive, skip | |
557 | ) | |
558 | ((memq (nth 2 e) '(todo-tree tags-tree occur-tree)) | |
559 | ;; These are trees, not really agenda commands | |
560 | ) | |
ed21c5c8 CD |
561 | ((and (memq (nth 2 e) '(todo tags tags-todo)) |
562 | (or (null (nth 3 e)) | |
563 | (not (string-match "\\S-" (nth 3 e))))) | |
564 | ;; These would be interactive because the match string is empty | |
565 | ) | |
566 | ((memq (nth 2 e) '(agenda alltodo todo tags tags-todo)) | |
8d642074 CD |
567 | ;; a normal command |
568 | (setq key (car e) desc (nth 1 e) type (nth 2 e) match (nth 3 e) | |
569 | settings (nth 4 e)) | |
570 | (setq settings | |
571 | (cons (list 'org-agenda-title-append | |
8bfe682a | 572 | (concat "<after>KEYS=" key " TITLE: " |
8d642074 CD |
573 | (if (and (stringp desc) (> (length desc) 0)) |
574 | desc (symbol-name type)) | |
8bfe682a | 575 | " " match "</after>")) |
8d642074 CD |
576 | settings)) |
577 | (push (list type match settings) new)) | |
3ab2c837 BG |
578 | ((or (functionp (nth 2 e)) (symbolp (nth 2 e))) |
579 | ;; A user-defined function, which can do anything, so simply | |
580 | ;; ignore it. | |
8d642074 CD |
581 | ) |
582 | (t | |
583 | ;; a block agenda | |
584 | (setq gkey (car e) gdesc (nth 1 e) gsettings (nth 3 e) cmds (nth 2 e)) | |
585 | (setq cnt 0) | |
586 | (while (setq e (pop cmds)) | |
587 | (setq type (car e) match (nth 1 e) settings (nth 2 e)) | |
588 | (setq settings (append gsettings settings)) | |
589 | (setq settings | |
590 | (cons (list 'org-agenda-title-append | |
8bfe682a | 591 | (concat "<after>KEYS=" gkey "#" (number-to-string |
8d642074 | 592 | (setq cnt (1+ cnt))) |
8bfe682a | 593 | " TITLE: " gdesc " " match "</after>")) |
8d642074 CD |
594 | settings)) |
595 | (push (list type match settings) new))))) | |
8bfe682a CD |
596 | (and new (list "X" "SUMO" (reverse new) |
597 | '((org-agenda-compact-blocks nil)))))) | |
598 | ||
599 | (defvar org-mobile-creating-agendas nil) | |
600 | (defun org-mobile-write-agenda-for-mobile (file) | |
601 | (let ((all (buffer-string)) in-date id pl prefix line app short m sexp) | |
602 | (with-temp-file file | |
603 | (org-mode) | |
604 | (insert "#+READONLY\n") | |
605 | (insert all) | |
606 | (goto-char (point-min)) | |
607 | (while (not (eobp)) | |
608 | (cond | |
609 | ((looking-at "[ \t]*$")) ; keep empty lines | |
610 | ((looking-at "=+$") | |
611 | ;; remove underlining | |
612 | (delete-region (point) (point-at-eol))) | |
613 | ((get-text-property (point) 'org-agenda-structural-header) | |
614 | (setq in-date nil) | |
615 | (setq app (get-text-property (point) | |
616 | 'org-agenda-title-append)) | |
617 | (setq short (get-text-property (point) | |
618 | 'short-heading)) | |
619 | (when (and short (looking-at ".+")) | |
620 | (replace-match short) | |
621 | (beginning-of-line 1)) | |
622 | (when app | |
623 | (end-of-line 1) | |
624 | (insert app) | |
625 | (beginning-of-line 1)) | |
626 | (insert "* ")) | |
627 | ((get-text-property (point) 'org-agenda-date-header) | |
628 | (setq in-date t) | |
629 | (insert "** ")) | |
630 | ((setq m (or (get-text-property (point) 'org-hd-marker) | |
631 | (get-text-property (point) 'org-marker))) | |
632 | (setq sexp (member (get-text-property (point) 'type) | |
633 | '("diary" "sexp"))) | |
3ab2c837 | 634 | (if (setq pl (text-property-any (point) (point-at-eol) 'org-heading t)) |
8bfe682a CD |
635 | (progn |
636 | (setq prefix (org-trim (buffer-substring | |
3ab2c837 | 637 | (point) pl)) |
8bfe682a | 638 | line (org-trim (buffer-substring |
3ab2c837 | 639 | pl |
8bfe682a CD |
640 | (point-at-eol)))) |
641 | (delete-region (point-at-bol) (point-at-eol)) | |
642 | (insert line "<before>" prefix "</before>") | |
643 | (beginning-of-line 1)) | |
644 | (and (looking-at "[ \t]+") (replace-match ""))) | |
645 | (insert (if in-date "*** " "** ")) | |
646 | (end-of-line 1) | |
647 | (insert "\n") | |
648 | (unless sexp | |
649 | (insert (org-agenda-get-some-entry-text | |
650 | m 10 " " 'planning) | |
651 | "\n") | |
652 | (when (setq id | |
653 | (if (org-bound-and-true-p | |
654 | org-mobile-force-id-on-agenda-items) | |
655 | (org-id-get m 'create) | |
afe98dfa CD |
656 | (or (org-entry-get m "ID") |
657 | (org-mobile-get-outline-path-link m)))) | |
8bfe682a CD |
658 | (insert " :PROPERTIES:\n :ORIGINAL_ID: " id |
659 | "\n :END:\n"))))) | |
660 | (beginning-of-line 2)) | |
afe98dfa | 661 | (push (cons "agendas.org" (md5 (buffer-string))) |
8bfe682a CD |
662 | org-mobile-checksum-files)) |
663 | (message "Agenda written to Org file %s" file))) | |
8d642074 | 664 | |
afe98dfa CD |
665 | (defun org-mobile-get-outline-path-link (pom) |
666 | (org-with-point-at pom | |
667 | (concat "olp:" | |
668 | (org-mobile-escape-olp (file-name-nondirectory buffer-file-name)) | |
669 | "/" | |
670 | (mapconcat 'org-mobile-escape-olp | |
671 | (org-get-outline-path) | |
672 | "/") | |
673 | "/" | |
674 | (org-mobile-escape-olp (nth 4 (org-heading-components)))))) | |
675 | ||
676 | (defun org-mobile-escape-olp (s) | |
3ab2c837 | 677 | (let ((table '(?: ?/))) |
afe98dfa CD |
678 | (org-link-escape s table))) |
679 | ||
8d642074 CD |
680 | ;;;###autoload |
681 | (defun org-mobile-create-sumo-agenda () | |
682 | "Create a file that contains all custom agenda views." | |
683 | (interactive) | |
684 | (let* ((file (expand-file-name "agendas.org" | |
685 | org-mobile-directory)) | |
ed21c5c8 CD |
686 | (file1 (if org-mobile-use-encryption |
687 | org-mobile-encryption-tempfile | |
688 | file)) | |
8bfe682a | 689 | (sumo (org-mobile-sumo-agenda-command)) |
8d642074 | 690 | (org-agenda-custom-commands |
ed21c5c8 | 691 | (list (append sumo (list (list file1))))) |
8bfe682a | 692 | (org-mobile-creating-agendas t)) |
ed21c5c8 CD |
693 | (unless (file-writable-p file1) |
694 | (error "Cannot write to file %s" file1)) | |
8bfe682a | 695 | (when sumo |
ed21c5c8 CD |
696 | (org-store-agenda-views)) |
697 | (when org-mobile-use-encryption | |
afe98dfa CD |
698 | (org-mobile-encrypt-and-move file1 file) |
699 | (delete-file file1) | |
700 | (org-mobile-cleanup-encryption-tempfile)))) | |
ed21c5c8 CD |
701 | |
702 | (defun org-mobile-encrypt-and-move (infile outfile) | |
703 | "Encrypt INFILE locally to INFILE_enc, then move it to OUTFILE. | |
704 | We do this in two steps so that remote paths will work, even if the | |
705 | encryption program does not understand them." | |
706 | (let ((encfile (concat infile "_enc"))) | |
707 | (org-mobile-encrypt-file infile encfile) | |
708 | (when outfile | |
709 | (copy-file encfile outfile 'ok-if-exists) | |
710 | (delete-file encfile)))) | |
711 | ||
712 | (defun org-mobile-encrypt-file (infile outfile) | |
713 | "Encrypt INFILE to OUTFILE, using `org-mobile-encryption-password'." | |
714 | (shell-command | |
715 | (format "openssl enc -aes-256-cbc -salt -pass %s -in %s -out %s" | |
afe98dfa CD |
716 | (shell-quote-argument (concat "pass:" |
717 | (org-mobile-encryption-password))) | |
ed21c5c8 CD |
718 | (shell-quote-argument (expand-file-name infile)) |
719 | (shell-quote-argument (expand-file-name outfile))))) | |
720 | ||
721 | (defun org-mobile-decrypt-file (infile outfile) | |
722 | "Decrypt INFILE to OUTFILE, using `org-mobile-encryption-password'." | |
723 | (shell-command | |
724 | (format "openssl enc -d -aes-256-cbc -salt -pass %s -in %s -out %s" | |
afe98dfa CD |
725 | (shell-quote-argument (concat "pass:" |
726 | (org-mobile-encryption-password))) | |
ed21c5c8 CD |
727 | (shell-quote-argument (expand-file-name infile)) |
728 | (shell-quote-argument (expand-file-name outfile))))) | |
8d642074 | 729 | |
afe98dfa CD |
730 | (defun org-mobile-cleanup-encryption-tempfile () |
731 | "Remove the encryption tempfile if it exists." | |
732 | (and (stringp org-mobile-encryption-tempfile) | |
733 | (file-exists-p org-mobile-encryption-tempfile) | |
734 | (delete-file org-mobile-encryption-tempfile))) | |
735 | ||
8d642074 CD |
736 | (defun org-mobile-move-capture () |
737 | "Move the contents of the capture file to the inbox file. | |
738 | Return a marker to the location where the new content has been added. | |
8bfe682a | 739 | If nothing new has been added, return nil." |
8d642074 | 740 | (interactive) |
ed21c5c8 CD |
741 | (let* ((encfile nil) |
742 | (capture-file (expand-file-name org-mobile-capture-file | |
743 | org-mobile-directory)) | |
744 | (inbox-buffer (find-file-noselect org-mobile-inbox-for-pull)) | |
745 | (capture-buffer | |
746 | (if (not org-mobile-use-encryption) | |
747 | (find-file-noselect capture-file) | |
afe98dfa | 748 | (org-mobile-cleanup-encryption-tempfile) |
ed21c5c8 CD |
749 | (setq encfile (concat org-mobile-encryption-tempfile "_enc")) |
750 | (copy-file capture-file encfile) | |
751 | (org-mobile-decrypt-file encfile org-mobile-encryption-tempfile) | |
752 | (find-file-noselect org-mobile-encryption-tempfile))) | |
753 | (insertion-point (make-marker)) | |
754 | not-empty content) | |
81ad75af | 755 | (with-current-buffer capture-buffer |
8d642074 CD |
756 | (setq content (buffer-string)) |
757 | (setq not-empty (string-match "\\S-" content)) | |
758 | (when not-empty | |
759 | (set-buffer inbox-buffer) | |
760 | (widen) | |
761 | (goto-char (point-max)) | |
762 | (or (bolp) (newline)) | |
763 | (move-marker insertion-point | |
764 | (prog1 (point) (insert content))) | |
765 | (save-buffer) | |
766 | (set-buffer capture-buffer) | |
767 | (erase-buffer) | |
8bfe682a CD |
768 | (save-buffer) |
769 | (org-mobile-update-checksum-for-capture-file (buffer-string)))) | |
8d642074 | 770 | (kill-buffer capture-buffer) |
ed21c5c8 CD |
771 | (when org-mobile-use-encryption |
772 | (org-mobile-encrypt-and-move org-mobile-encryption-tempfile | |
afe98dfa CD |
773 | capture-file) |
774 | (org-mobile-cleanup-encryption-tempfile)) | |
8d642074 CD |
775 | (if not-empty insertion-point))) |
776 | ||
8bfe682a | 777 | (defun org-mobile-update-checksum-for-capture-file (buffer-string) |
ed21c5c8 | 778 | "Find the checksum line and modify it to match BUFFER-STRING." |
8bfe682a CD |
779 | (let* ((file (expand-file-name "checksums.dat" org-mobile-directory)) |
780 | (buffer (find-file-noselect file))) | |
781 | (when buffer | |
782 | (with-current-buffer buffer | |
783 | (when (re-search-forward (concat "\\([0-9a-fA-F]\\{30,\\}\\).*?" | |
784 | (regexp-quote org-mobile-capture-file) | |
785 | "[ \t]*$") nil t) | |
786 | (goto-char (match-beginning 1)) | |
787 | (delete-region (match-beginning 1) (match-end 1)) | |
788 | (insert (md5 buffer-string)) | |
789 | (save-buffer))) | |
790 | (kill-buffer buffer)))) | |
791 | ||
792 | (defun org-mobile-apply (&optional beg end) | |
793 | "Apply all change requests in the current buffer. | |
8d642074 CD |
794 | If BEG and END are given, only do this in that region." |
795 | (interactive) | |
796 | (require 'org-archive) | |
797 | (setq org-mobile-last-flagged-files nil) | |
798 | (setq beg (or beg (point-min)) end (or end (point-max))) | |
8bfe682a CD |
799 | |
800 | ;; Remove all Note IDs | |
8d642074 | 801 | (goto-char beg) |
8bfe682a CD |
802 | (while (re-search-forward "^\\*\\* Note ID: [-0-9A-F]+[ \t]*\n" end t) |
803 | (replace-match "")) | |
804 | ||
805 | ;; Find all the referenced entries, without making any changes yet | |
8d642074 | 806 | (let ((marker (make-marker)) |
8bfe682a | 807 | (bos-marker (make-marker)) |
8d642074 | 808 | (end (move-marker (make-marker) end)) |
8bfe682a CD |
809 | (cnt-new 0) |
810 | (cnt-edit 0) | |
811 | (cnt-flag 0) | |
812 | (cnt-error 0) | |
813 | buf-list | |
814 | id-pos org-mobile-error) | |
815 | ||
816 | ;; Count the new captures | |
817 | (goto-char beg) | |
818 | (while (re-search-forward "^\\* \\(.*\\)" end t) | |
819 | (and (>= (- (match-end 1) (match-beginning 1)) 2) | |
820 | (not (equal (downcase (substring (match-string 1) 0 2)) "f(")) | |
821 | (incf cnt-new))) | |
822 | ||
823 | (goto-char beg) | |
8d642074 | 824 | (while (re-search-forward |
8bfe682a CD |
825 | "^\\*+[ \t]+F(\\([^():\n]*\\)\\(:\\([^()\n]*\\)\\)?)[ \t]+\\[\\[\\(\\(id\\|olp\\):\\([^]\n]+\\)\\)" end t) |
826 | (setq id-pos (condition-case msg | |
827 | (org-mobile-locate-entry (match-string 4)) | |
828 | (error (nth 1 msg)))) | |
829 | (when (and (markerp id-pos) | |
830 | (not (member (marker-buffer id-pos) buf-list))) | |
831 | (org-mobile-timestamp-buffer (marker-buffer id-pos)) | |
832 | (push (marker-buffer id-pos) buf-list)) | |
833 | ||
834 | (if (or (not id-pos) (stringp id-pos)) | |
835 | (progn | |
836 | (goto-char (+ 2 (point-at-bol))) | |
837 | (insert id-pos " ") | |
838 | (incf cnt-error)) | |
839 | (add-text-properties (point-at-bol) (point-at-eol) | |
840 | (list 'org-mobile-marker | |
841 | (or id-pos "Linked entry not found"))))) | |
842 | ||
843 | ;; OK, now go back and start applying | |
844 | (goto-char beg) | |
845 | (while (re-search-forward "^\\*+[ \t]+F(\\([^():\n]*\\)\\(:\\([^()\n]*\\)\\)?)" end t) | |
8d642074 | 846 | (catch 'next |
8bfe682a CD |
847 | (setq id-pos (get-text-property (point-at-bol) 'org-mobile-marker)) |
848 | (if (not (markerp id-pos)) | |
849 | (progn | |
850 | (incf cnt-error) | |
851 | (insert "UNKNOWN PROBLEM")) | |
852 | (let* ((action (match-string 1)) | |
853 | (data (and (match-end 3) (match-string 3))) | |
854 | (bos (point-at-bol)) | |
855 | (eos (save-excursion (org-end-of-subtree t t))) | |
856 | (cmd (if (equal action "") | |
857 | '(progn | |
858 | (incf cnt-flag) | |
859 | (org-toggle-tag "FLAGGED" 'on) | |
860 | (and note | |
861 | (org-entry-put nil "THEFLAGGINGNOTE" note))) | |
862 | (incf cnt-edit) | |
863 | (cdr (assoc action org-mobile-action-alist)))) | |
864 | (note (and (equal action "") | |
865 | (buffer-substring (1+ (point-at-eol)) eos))) | |
866 | (org-inhibit-logging 'note) ;; Do not take notes interactively | |
867 | old new) | |
868 | (goto-char bos) | |
869 | (move-marker bos-marker (point)) | |
870 | (if (re-search-forward "^** Old value[ \t]*$" eos t) | |
871 | (setq old (buffer-substring | |
872 | (1+ (match-end 0)) | |
873 | (progn (outline-next-heading) (point))))) | |
874 | (if (re-search-forward "^** New value[ \t]*$" eos t) | |
875 | (setq new (buffer-substring | |
876 | (1+ (match-end 0)) | |
877 | (progn (outline-next-heading) | |
878 | (if (eobp) (org-back-over-empty-lines)) | |
879 | (point))))) | |
880 | (setq old (and old (if (string-match "\\S-" old) old nil))) | |
881 | (setq new (and new (if (string-match "\\S-" new) new nil))) | |
882 | (if (and note (> (length note) 0)) | |
883 | ;; Make Note into a single line, to fit into a property | |
884 | (setq note (mapconcat 'identity | |
885 | (org-split-string (org-trim note) "\n") | |
886 | "\\n"))) | |
887 | (unless (equal data "body") | |
888 | (setq new (and new (org-trim new)) | |
889 | old (and old (org-trim old)))) | |
890 | (goto-char (+ 2 bos-marker)) | |
891 | (unless (markerp id-pos) | |
892 | (insert "BAD REFERENCE ") | |
893 | (incf cnt-error) | |
894 | (throw 'next t)) | |
895 | (unless cmd | |
896 | (insert "BAD FLAG ") | |
897 | (incf cnt-error) | |
898 | (throw 'next t)) | |
899 | ;; Remember this place so that we can return | |
900 | (move-marker marker (point)) | |
901 | (setq org-mobile-error nil) | |
902 | (save-excursion | |
903 | (condition-case msg | |
904 | (org-with-point-at id-pos | |
905 | (progn | |
8d642074 CD |
906 | (eval cmd) |
907 | (if (member "FLAGGED" (org-get-tags)) | |
908 | (add-to-list 'org-mobile-last-flagged-files | |
909 | (buffer-file-name (current-buffer)))))) | |
8bfe682a CD |
910 | (error (setq org-mobile-error msg)))) |
911 | (when org-mobile-error | |
c3313451 | 912 | (switch-to-buffer (marker-buffer marker)) |
8bfe682a CD |
913 | (goto-char marker) |
914 | (incf cnt-error) | |
915 | (insert (if (stringp (nth 1 org-mobile-error)) | |
916 | (nth 1 org-mobile-error) | |
917 | "EXECUTION FAILED") | |
918 | " ") | |
919 | (throw 'next t)) | |
920 | ;; If we get here, the action has been applied successfully | |
921 | ;; So remove the entry | |
922 | (goto-char bos-marker) | |
923 | (delete-region (point) (org-end-of-subtree t t)))))) | |
924 | (save-buffer) | |
8d642074 | 925 | (move-marker marker nil) |
8bfe682a CD |
926 | (move-marker end nil) |
927 | (message "%d new, %d edits, %d flags, %d errors" cnt-new | |
928 | cnt-edit cnt-flag cnt-error) | |
929 | (sit-for 1))) | |
930 | ||
931 | (defun org-mobile-timestamp-buffer (buf) | |
932 | "Time stamp buffer BUF, just to make sure its checksum will change." | |
933 | (with-current-buffer buf | |
934 | (save-excursion | |
935 | (save-restriction | |
936 | (widen) | |
937 | (goto-char (point-min)) | |
938 | (if (re-search-forward | |
939 | "^\\([ \t]*\\)#\\+LAST_MOBILE_CHANGE:.*\n?" nil t) | |
940 | (progn | |
941 | (goto-char (match-end 1)) | |
942 | (delete-region (point) (match-end 0))) | |
943 | (if (looking-at ".*?-\\*-.*-\\*-") | |
944 | (forward-line 1))) | |
945 | (insert "#+LAST_MOBILE_CHANGE: " | |
946 | (format-time-string "%Y-%m-%d %T") "\n"))))) | |
8d642074 CD |
947 | |
948 | (defun org-mobile-smart-read () | |
949 | "Parse the entry at point for shortcuts and expand them. | |
950 | These shortcuts are meant for fast and easy typing on the limited | |
951 | keyboards of a mobile device. Below we show a list of the shortcuts | |
952 | currently implemented. | |
953 | ||
954 | The entry is expected to contain an inactive time stamp indicating when | |
955 | the entry was created. When setting dates and | |
956 | times (for example for deadlines), the time strings are interpreted | |
957 | relative to that creation date. | |
8bfe682a | 958 | Abbreviations are expected to take up entire lines, just because it is so |
8d642074 CD |
959 | easy to type RET on a mobile device. Abbreviations start with one or two |
960 | letters, followed immediately by a dot and then additional information. | |
961 | Generally the entire shortcut line is removed after action have been taken. | |
962 | Time stamps will be constructed using `org-read-date'. So for example a | |
963 | line \"dd. 2tue\" will set a deadline on the second Tuesday after the | |
964 | creation date. | |
965 | ||
966 | Here are the shortcuts currently implemented: | |
967 | ||
968 | dd. string set deadline | |
969 | ss. string set scheduling | |
970 | tt. string set time tamp, here. | |
971 | ti. string set inactive time | |
972 | ||
973 | tg. tag1 tag2 tag3 set all these tags, change case where necessary | |
974 | td. kwd set this todo keyword, change case where necessary | |
975 | ||
976 | FIXME: Hmmm, not sure if we can make his work against the | |
977 | auto-correction feature. Needs a bit more thinking. So this function | |
978 | is currently a noop.") | |
979 | ||
8bfe682a CD |
980 | (defun org-mobile-locate-entry (link) |
981 | (if (string-match "\\`id:\\(.*\\)$" link) | |
982 | (org-id-find (match-string 1 link) 'marker) | |
983 | (if (not (string-match "\\`olp:\\(.*?\\):\\(.*\\)$" link)) | |
984 | nil | |
985 | (let ((file (match-string 1 link)) | |
3ab2c837 BG |
986 | (path (match-string 2 link))) |
987 | (setq file (org-link-unescape file)) | |
8bfe682a | 988 | (setq file (expand-file-name file org-directory)) |
3ab2c837 | 989 | (setq path (mapcar 'org-link-unescape |
8bfe682a CD |
990 | (org-split-string path "/"))) |
991 | (org-find-olp (cons file path)))))) | |
992 | ||
993 | (defun org-mobile-edit (what old new) | |
994 | "Edit item WHAT in the current entry by replacing OLD with NEW. | |
995 | WHAT can be \"heading\", \"todo\", \"tags\", \"priority\", or \"body\". | |
996 | The edit only takes place if the current value is equal (except for | |
997 | white space) the OLD. If this is so, OLD will be replace by NEW | |
998 | and the command will return t. If something goes wrong, a string will | |
999 | be returned that indicates what went wrong." | |
1000 | (let (current old1 new1) | |
1001 | (if (stringp what) (setq what (intern what))) | |
1002 | ||
1003 | (cond | |
1004 | ||
1005 | ((memq what '(todo todostate)) | |
1006 | (setq current (org-get-todo-state)) | |
1007 | (cond | |
1008 | ((equal new "DONEARCHIVE") | |
1009 | (org-todo 'done) | |
1010 | (org-archive-subtree-default)) | |
1011 | ((equal new current) t) ; nothing needs to be done | |
1012 | ((or (equal current old) | |
1013 | (eq org-mobile-force-mobile-change t) | |
1014 | (memq 'todo org-mobile-force-mobile-change)) | |
1015 | (org-todo (or new 'none)) t) | |
1016 | (t (error "State before change was expected as \"%s\", but is \"%s\"" | |
1017 | old current)))) | |
ed21c5c8 | 1018 | |
8bfe682a CD |
1019 | ((eq what 'tags) |
1020 | (setq current (org-get-tags) | |
1021 | new1 (and new (org-split-string new ":+")) | |
1022 | old1 (and old (org-split-string old ":+"))) | |
1023 | (cond | |
1024 | ((org-mobile-tags-same-p current new1) t) ; no change needed | |
1025 | ((or (org-mobile-tags-same-p current old1) | |
1026 | (eq org-mobile-force-mobile-change t) | |
1027 | (memq 'tags org-mobile-force-mobile-change)) | |
1028 | (org-set-tags-to new1) t) | |
1029 | (t (error "Tags before change were expected as \"%s\", but are \"%s\"" | |
1030 | (or old "") (or current ""))))) | |
ed21c5c8 | 1031 | |
8bfe682a CD |
1032 | ((eq what 'priority) |
1033 | (when (looking-at org-complex-heading-regexp) | |
1034 | (setq current (and (match-end 3) (substring (match-string 3) 2 3))) | |
1035 | (cond | |
1036 | ((equal current new) t) ; no action required | |
1037 | ((or (equal current old) | |
1038 | (eq org-mobile-force-mobile-change t) | |
1039 | (memq 'tags org-mobile-force-mobile-change)) | |
1040 | (org-priority (and new (string-to-char new)))) | |
1041 | (t (error "Priority was expected to be %s, but is %s" | |
1042 | old current))))) | |
1043 | ||
1044 | ((eq what 'heading) | |
1045 | (when (looking-at org-complex-heading-regexp) | |
1046 | (setq current (match-string 4)) | |
1047 | (cond | |
1048 | ((equal current new) t) ; no action required | |
1049 | ((or (equal current old) | |
1050 | (eq org-mobile-force-mobile-change t) | |
1051 | (memq 'heading org-mobile-force-mobile-change)) | |
1052 | (goto-char (match-beginning 4)) | |
1053 | (insert new) | |
1054 | (delete-region (point) (+ (point) (length current))) | |
1055 | (org-set-tags nil 'align)) | |
1056 | (t (error "Heading changed in MobileOrg and on the computer"))))) | |
ed21c5c8 | 1057 | |
8bfe682a CD |
1058 | ((eq what 'body) |
1059 | (setq current (buffer-substring (min (1+ (point-at-eol)) (point-max)) | |
1060 | (save-excursion (outline-next-heading) | |
1061 | (point)))) | |
1062 | (if (not (string-match "\\S-" current)) (setq current nil)) | |
1063 | (cond | |
1064 | ((org-mobile-bodies-same-p current new) t) ; no action necessary | |
1065 | ((or (org-mobile-bodies-same-p current old) | |
1066 | (eq org-mobile-force-mobile-change t) | |
1067 | (memq 'body org-mobile-force-mobile-change)) | |
1068 | (save-excursion | |
1069 | (end-of-line 1) | |
1070 | (insert "\n" new) | |
1071 | (or (bolp) (insert "\n")) | |
1072 | (delete-region (point) (progn (org-back-to-heading t) | |
1073 | (outline-next-heading) | |
1074 | (point)))) | |
1075 | t) | |
1076 | (t (error "Body was changed in MobileOrg and on the computer"))))))) | |
ed21c5c8 | 1077 | |
8bfe682a CD |
1078 | (defun org-mobile-tags-same-p (list1 list2) |
1079 | "Are the two tag lists the same?" | |
1080 | (not (or (org-delete-all list1 list2) | |
1081 | (org-delete-all list2 list1)))) | |
1082 | ||
1083 | (defun org-mobile-bodies-same-p (a b) | |
1084 | "Compare if A and B are visually equal strings. | |
1085 | We first remove leading and trailing white space from the entire strings. | |
1086 | Then we split the strings into lines and remove leading/trailing whitespace | |
1087 | from each line. Then we compare. | |
1088 | A and B must be strings or nil." | |
1089 | (cond | |
1090 | ((and (not a) (not b)) t) | |
1091 | ((or (not a) (not b)) nil) | |
1092 | (t (setq a (org-trim a) b (org-trim b)) | |
1093 | (setq a (mapconcat 'identity (org-split-string a "[ \t]*\n[ \t]*") "\n")) | |
1094 | (setq b (mapconcat 'identity (org-split-string b "[ \t]*\n[ \t]*") "\n")) | |
1095 | (equal a b)))) | |
1096 | ||
8d642074 CD |
1097 | (provide 'org-mobile) |
1098 | ||
5b409b39 | 1099 | |
8d642074 CD |
1100 | |
1101 | ;;; org-mobile.el ends here | |
1102 |