org-mobile.el 42 KB

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