org-mobile.el 42 KB

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