org-mobile.el 41 KB

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