org-mobile.el 41 KB

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