org-mobile.el 42 KB

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