org-mobile.el 41 KB

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