org-mobile.el 40 KB

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