org-mobile.el 42 KB

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