org-mobile.el 38 KB

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