org-mobile.el 41 KB

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