org-mobile.el 41 KB

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