org-mobile.el 42 KB

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