org-mobile.el 42 KB

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