org-mobile.el 42 KB

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