org-mobile.el 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  1. ;;; org-mobile.el --- Code for asymmetric sync with a mobile device
  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. ;;; 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 setting 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 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 (split-string (org-tag-alist-to-string def-tags t)))
  415. (setq tags (org-delete-all def-tags tags))
  416. (setq tags (sort tags (lambda (a b) (string< (downcase a) (downcase b)))))
  417. (setq tags (append def-tags tags nil))
  418. (insert "#+TAGS: " (mapconcat 'identity tags " ") "\n")
  419. (insert "#+ALLPRIORITIES: " org-mobile-allpriorities "\n")
  420. (when (file-exists-p (expand-file-name
  421. org-mobile-directory "agendas.org"))
  422. (insert "* [[file:agendas.org][Agenda Views]]\n"))
  423. (while (setq entry (pop files-alist))
  424. (setq file (car entry)
  425. link-name (cdr entry))
  426. (insert (format "* [[file:%s][%s]]\n"
  427. link-name link-name)))
  428. (push (cons org-mobile-index-file (md5 (buffer-string)))
  429. org-mobile-checksum-files))
  430. (when org-mobile-use-encryption
  431. (org-mobile-encrypt-and-move org-mobile-encryption-tempfile
  432. target-file)
  433. (org-mobile-cleanup-encryption-tempfile))))
  434. (defun org-mobile-copy-agenda-files ()
  435. "Copy all agenda files to the stage or WebDAV directory."
  436. (let ((files-alist org-mobile-files-alist)
  437. file buf entry link-name target-path target-dir check)
  438. (while (setq entry (pop files-alist))
  439. (setq file (car entry) link-name (cdr entry))
  440. (when (file-exists-p file)
  441. (setq target-path (expand-file-name link-name org-mobile-directory)
  442. target-dir (file-name-directory target-path))
  443. (unless (file-directory-p target-dir)
  444. (make-directory target-dir 'parents))
  445. (if org-mobile-use-encryption
  446. (org-mobile-encrypt-and-move file target-path)
  447. (copy-file file target-path 'ok-if-exists))
  448. (setq check (shell-command-to-string
  449. (concat (shell-quote-argument org-mobile-checksum-binary)
  450. " "
  451. (shell-quote-argument (expand-file-name file)))))
  452. (when (string-match "[a-fA-F0-9]\\{30,40\\}" check)
  453. (push (cons link-name (match-string 0 check))
  454. org-mobile-checksum-files))))
  455. (setq file (expand-file-name org-mobile-capture-file
  456. org-mobile-directory))
  457. (save-excursion
  458. (setq buf (find-file file))
  459. (when (and (= (point-min) (point-max)))
  460. (insert "\n")
  461. (save-buffer)
  462. (when org-mobile-use-encryption
  463. (write-file org-mobile-encryption-tempfile)
  464. (org-mobile-encrypt-and-move org-mobile-encryption-tempfile file)))
  465. (push (cons org-mobile-capture-file (md5 (buffer-string)))
  466. org-mobile-checksum-files))
  467. (org-mobile-cleanup-encryption-tempfile)
  468. (kill-buffer buf)))
  469. (defun org-mobile-write-checksums ()
  470. "Create checksums for all files in `org-mobile-directory'.
  471. The table of checksums is written to the file mobile-checksums."
  472. (let ((sumfile (expand-file-name "checksums.dat" org-mobile-directory))
  473. (files org-mobile-checksum-files)
  474. entry file sum)
  475. (with-temp-file sumfile
  476. (set-buffer-file-coding-system 'undecided-unix nil)
  477. (while (setq entry (pop files))
  478. (setq file (car entry) sum (cdr entry))
  479. (insert (format "%s %s\n" sum file))))))
  480. (defun org-mobile-sumo-agenda-command ()
  481. "Return an agenda custom command that comprises all custom commands."
  482. (let ((custom-list
  483. ;; normalize different versions
  484. (delq nil
  485. (mapcar
  486. (lambda (x)
  487. (cond ((stringp (cdr x)) nil)
  488. ((stringp (nth 1 x)) x)
  489. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  490. (t (cons (car x) (cons "" (cdr x))))))
  491. org-agenda-custom-commands)))
  492. (default-list '(("a" "Agenda" agenda) ("t" "All TODO" alltodo)))
  493. thelist atitle new e key desc type match settings cmds gkey gdesc gsettings cnt)
  494. (cond
  495. ((eq org-mobile-agendas 'custom)
  496. (setq thelist custom-list))
  497. ((eq org-mobile-agendas 'default)
  498. (setq thelist default-list))
  499. ((eq org-mobile-agendas 'all)
  500. (setq thelist custom-list)
  501. (unless (assoc "t" thelist) (push '("t" "ALL TODO" alltodo) thelist))
  502. (unless (assoc "a" thelist) (push '("a" "Agenda" agenda) thelist)))
  503. ((listp org-mobile-agendas)
  504. (setq thelist (append custom-list default-list))
  505. (setq thelist (delq nil (mapcar (lambda (k) (assoc k thelist))
  506. org-mobile-agendas)))))
  507. (while (setq e (pop thelist))
  508. (cond
  509. ((stringp (cdr e))
  510. ;; this is a description entry - skip it
  511. )
  512. ((eq (nth 2 e) 'search)
  513. ;; Search view is interactive, skip
  514. )
  515. ((memq (nth 2 e) '(todo-tree tags-tree occur-tree))
  516. ;; These are trees, not really agenda commands
  517. )
  518. ((and (memq (nth 2 e) '(todo tags tags-todo))
  519. (or (null (nth 3 e))
  520. (not (string-match "\\S-" (nth 3 e)))))
  521. ;; These would be interactive because the match string is empty
  522. )
  523. ((memq (nth 2 e) '(agenda alltodo todo tags tags-todo))
  524. ;; a normal command
  525. (setq key (car e) desc (nth 1 e) type (nth 2 e) match (nth 3 e)
  526. settings (nth 4 e))
  527. (setq settings
  528. (cons (list 'org-agenda-title-append
  529. (concat "<after>KEYS=" key " TITLE: "
  530. (if (and (stringp desc) (> (length desc) 0))
  531. desc (symbol-name type))
  532. "</after>"))
  533. settings))
  534. (push (list type match settings) new))
  535. ((or (functionp (nth 2 e)) (symbolp (nth 2 e)))
  536. ;; A user-defined function, which can do anything, so simply
  537. ;; ignore it.
  538. )
  539. (t
  540. ;; a block agenda
  541. (setq gkey (car e) gdesc (nth 1 e) gsettings (nth 3 e) cmds (nth 2 e))
  542. (setq cnt 0)
  543. (while (setq e (pop cmds))
  544. (setq type (car e) match (nth 1 e) settings (nth 2 e))
  545. (setq atitle (if (string= "" gdesc) match gdesc))
  546. (setq settings (append gsettings settings))
  547. (setq settings
  548. (cons (list 'org-agenda-title-append
  549. (concat "<after>KEYS=" gkey "#" (number-to-string
  550. (setq cnt (1+ cnt)))
  551. " TITLE: " atitle "</after>"))
  552. settings))
  553. (push (list type match settings) new)))))
  554. (and new (list "X" "SUMO" (reverse new)
  555. '((org-agenda-compact-blocks nil))))))
  556. (defvar org-mobile-creating-agendas nil)
  557. (defun org-mobile-write-agenda-for-mobile (file)
  558. (let ((all (buffer-string)) in-date id pl prefix line app short m sexp)
  559. (with-temp-file file
  560. (org-mode)
  561. (insert "#+READONLY\n")
  562. (insert all)
  563. (goto-char (point-min))
  564. (while (not (eobp))
  565. (cond
  566. ((looking-at "[ \t]*$")) ; keep empty lines
  567. ((looking-at "=+$")
  568. ;; remove underlining
  569. (delete-region (point) (point-at-eol)))
  570. ((get-text-property (point) 'org-agenda-structural-header)
  571. (setq in-date nil)
  572. (setq app (get-text-property (point) 'org-agenda-title-append))
  573. (setq short (get-text-property (point) 'short-heading))
  574. (when (and short (looking-at ".+"))
  575. (replace-match short nil t)
  576. (beginning-of-line 1))
  577. (when app
  578. (end-of-line 1)
  579. (insert app)
  580. (beginning-of-line 1))
  581. (insert "* "))
  582. ((get-text-property (point) 'org-agenda-date-header)
  583. (setq in-date t)
  584. (insert "** "))
  585. ((setq m (or (get-text-property (point) 'org-hd-marker)
  586. (get-text-property (point) 'org-marker)))
  587. (setq sexp (member (get-text-property (point) 'type)
  588. '("diary" "sexp")))
  589. (if (setq pl (text-property-any (point) (point-at-eol) 'org-heading t))
  590. (progn
  591. (setq prefix (org-trim (buffer-substring
  592. (point) pl))
  593. line (org-trim (buffer-substring
  594. pl
  595. (point-at-eol))))
  596. (delete-region (point-at-bol) (point-at-eol))
  597. (insert line "<before>" prefix "</before>")
  598. (beginning-of-line 1))
  599. (and (looking-at "[ \t]+") (replace-match "")))
  600. (insert (if in-date "*** " "** "))
  601. (end-of-line 1)
  602. (insert "\n")
  603. (unless sexp
  604. (insert (org-agenda-get-some-entry-text
  605. m 10 " " 'planning)
  606. "\n")
  607. (when (setq id
  608. (if (org-bound-and-true-p
  609. org-mobile-force-id-on-agenda-items)
  610. (org-id-get m 'create)
  611. (or (org-entry-get m "ID")
  612. (org-mobile-get-outline-path-link m))))
  613. (insert " :PROPERTIES:\n :ORIGINAL_ID: " id
  614. "\n :END:\n")))))
  615. (beginning-of-line 2))
  616. (push (cons "agendas.org" (md5 (buffer-string)))
  617. org-mobile-checksum-files))
  618. (message "Agenda written to Org file %s" file)))
  619. (defun org-mobile-get-outline-path-link (pom)
  620. (org-with-point-at pom
  621. (concat "olp:"
  622. (org-mobile-escape-olp (file-name-nondirectory buffer-file-name))
  623. "/"
  624. (mapconcat 'org-mobile-escape-olp
  625. (org-get-outline-path)
  626. "/")
  627. "/"
  628. (org-mobile-escape-olp (nth 4 (org-heading-components))))))
  629. (defun org-mobile-escape-olp (s)
  630. (let ((table '(?: ?/)))
  631. (org-link-escape s table)))
  632. (defun org-mobile-create-sumo-agenda ()
  633. "Create a file that contains all custom agenda views."
  634. (interactive)
  635. (let* ((file (expand-file-name "agendas.org"
  636. org-mobile-directory))
  637. (file1 (if org-mobile-use-encryption
  638. org-mobile-encryption-tempfile
  639. file))
  640. (sumo (org-mobile-sumo-agenda-command))
  641. (org-agenda-custom-commands
  642. (list (append sumo (list (list file1)))))
  643. (org-mobile-creating-agendas t))
  644. (unless (file-writable-p file1)
  645. (error "Cannot write to file %s" file1))
  646. (when sumo
  647. (org-store-agenda-views))
  648. (when org-mobile-use-encryption
  649. (org-mobile-encrypt-and-move file1 file)
  650. (delete-file file1)
  651. (org-mobile-cleanup-encryption-tempfile))))
  652. (defun org-mobile-encrypt-and-move (infile outfile)
  653. "Encrypt INFILE locally to INFILE_enc, then move it to OUTFILE.
  654. We do this in two steps so that remote paths will work, even if the
  655. encryption program does not understand them."
  656. (let ((encfile (concat infile "_enc")))
  657. (org-mobile-encrypt-file infile encfile)
  658. (when outfile
  659. (copy-file encfile outfile 'ok-if-exists)
  660. (delete-file encfile))))
  661. (defun org-mobile-encrypt-file (infile outfile)
  662. "Encrypt INFILE to OUTFILE, using `org-mobile-encryption-password'."
  663. (shell-command
  664. (format "openssl enc -aes-256-cbc -salt -pass %s -in %s -out %s"
  665. (shell-quote-argument (concat "pass:"
  666. (org-mobile-encryption-password)))
  667. (shell-quote-argument (expand-file-name infile))
  668. (shell-quote-argument (expand-file-name outfile)))))
  669. (defun org-mobile-decrypt-file (infile outfile)
  670. "Decrypt INFILE to OUTFILE, using `org-mobile-encryption-password'."
  671. (shell-command
  672. (format "openssl enc -d -aes-256-cbc -salt -pass %s -in %s -out %s"
  673. (shell-quote-argument (concat "pass:"
  674. (org-mobile-encryption-password)))
  675. (shell-quote-argument (expand-file-name infile))
  676. (shell-quote-argument (expand-file-name outfile)))))
  677. (defun org-mobile-cleanup-encryption-tempfile ()
  678. "Remove the encryption tempfile if it exists."
  679. (and (stringp org-mobile-encryption-tempfile)
  680. (file-exists-p org-mobile-encryption-tempfile)
  681. (delete-file org-mobile-encryption-tempfile)))
  682. (defun org-mobile-move-capture ()
  683. "Move the contents of the capture file to the inbox file.
  684. Return a marker to the location where the new content has been added.
  685. If nothing new has been added, return nil."
  686. (interactive)
  687. (let* ((encfile nil)
  688. (capture-file (expand-file-name org-mobile-capture-file
  689. org-mobile-directory))
  690. (inbox-buffer (find-file-noselect org-mobile-inbox-for-pull))
  691. (capture-buffer
  692. (if (not org-mobile-use-encryption)
  693. (find-file-noselect capture-file)
  694. (org-mobile-cleanup-encryption-tempfile)
  695. (setq encfile (concat org-mobile-encryption-tempfile "_enc"))
  696. (copy-file capture-file encfile)
  697. (org-mobile-decrypt-file encfile org-mobile-encryption-tempfile)
  698. (find-file-noselect org-mobile-encryption-tempfile)))
  699. (insertion-point (make-marker))
  700. not-empty content)
  701. (with-current-buffer capture-buffer
  702. (setq content (buffer-string))
  703. (setq not-empty (string-match "\\S-" content))
  704. (when not-empty
  705. (set-buffer inbox-buffer)
  706. (widen)
  707. (goto-char (point-max))
  708. (or (bolp) (newline))
  709. (move-marker insertion-point
  710. (prog1 (point) (insert content)))
  711. (save-buffer)
  712. (set-buffer capture-buffer)
  713. (erase-buffer)
  714. (save-buffer)
  715. (org-mobile-update-checksum-for-capture-file (buffer-string))))
  716. (kill-buffer capture-buffer)
  717. (when org-mobile-use-encryption
  718. (org-mobile-encrypt-and-move org-mobile-encryption-tempfile
  719. capture-file)
  720. (org-mobile-cleanup-encryption-tempfile))
  721. (if not-empty insertion-point)))
  722. (defun org-mobile-update-checksum-for-capture-file (buffer-string)
  723. "Find the checksum line and modify it to match BUFFER-STRING."
  724. (let* ((file (expand-file-name "checksums.dat" org-mobile-directory))
  725. (buffer (find-file-noselect file)))
  726. (when buffer
  727. (with-current-buffer buffer
  728. (when (re-search-forward (concat "\\([0-9a-fA-F]\\{30,\\}\\).*?"
  729. (regexp-quote org-mobile-capture-file)
  730. "[ \t]*$") nil t)
  731. (goto-char (match-beginning 1))
  732. (delete-region (match-beginning 1) (match-end 1))
  733. (insert (md5 buffer-string))
  734. (save-buffer)))
  735. (kill-buffer buffer))))
  736. (defun org-mobile-apply (&optional beg end)
  737. "Apply all change requests in the current buffer.
  738. If BEG and END are given, only do this in that region."
  739. (interactive)
  740. (require 'org-archive)
  741. (setq org-mobile-last-flagged-files nil)
  742. (setq beg (or beg (point-min)) end (or end (point-max)))
  743. ;; Remove all Note IDs
  744. (goto-char beg)
  745. (while (re-search-forward "^\\*\\* Note ID: [-0-9A-F]+[ \t]*\n" end t)
  746. (replace-match ""))
  747. ;; Find all the referenced entries, without making any changes yet
  748. (let ((marker (make-marker))
  749. (bos-marker (make-marker))
  750. (end (move-marker (make-marker) end))
  751. (cnt-new 0)
  752. (cnt-edit 0)
  753. (cnt-flag 0)
  754. (cnt-error 0)
  755. buf-list
  756. id-pos org-mobile-error)
  757. ;; Count the new captures
  758. (goto-char beg)
  759. (while (re-search-forward "^\\* \\(.*\\)" end t)
  760. (and (>= (- (match-end 1) (match-beginning 1)) 2)
  761. (not (equal (downcase (substring (match-string 1) 0 2)) "f("))
  762. (incf cnt-new)))
  763. ;; Find and apply the edits
  764. (goto-char beg)
  765. (while (re-search-forward
  766. "^\\*+[ \t]+F(\\([^():\n]*\\)\\(:\\([^()\n]*\\)\\)?)[ \t]+\\[\\[\\(\\(id\\|olp\\):\\([^]\n]+\\)\\)" end t)
  767. (catch 'next
  768. (let* ((action (match-string 1))
  769. (data (and (match-end 3) (match-string 3)))
  770. (id-pos (condition-case msg
  771. (org-mobile-locate-entry (match-string 4))
  772. (error (nth 1 msg))))
  773. (bos (point-at-bol))
  774. (eos (save-excursion (org-end-of-subtree t t)))
  775. (cmd (if (equal action "")
  776. '(progn
  777. (incf cnt-flag)
  778. (org-toggle-tag "FLAGGED" 'on)
  779. (and note
  780. (org-entry-put nil "THEFLAGGINGNOTE" note)))
  781. (incf cnt-edit)
  782. (cdr (assoc action org-mobile-action-alist))))
  783. (note (and (equal action "")
  784. (buffer-substring (1+ (point-at-eol)) eos)))
  785. (org-inhibit-logging 'note) ;; Do not take notes interactively
  786. old new)
  787. (goto-char bos)
  788. (when (and (markerp id-pos)
  789. (not (member (marker-buffer id-pos) buf-list)))
  790. (org-mobile-timestamp-buffer (marker-buffer id-pos))
  791. (push (marker-buffer id-pos) buf-list))
  792. (unless (markerp id-pos)
  793. (goto-char (+ 2 (point-at-bol)))
  794. (if (stringp id-pos)
  795. (insert id-pos " ")
  796. (insert "BAD REFERENCE "))
  797. (incf cnt-error)
  798. (throw 'next t))
  799. (unless cmd
  800. (insert "BAD FLAG ")
  801. (incf cnt-error)
  802. (throw 'next t))
  803. (move-marker bos-marker (point))
  804. (if (re-search-forward "^** Old value[ \t]*$" eos t)
  805. (setq old (buffer-substring
  806. (1+ (match-end 0))
  807. (progn (outline-next-heading) (point)))))
  808. (if (re-search-forward "^** New value[ \t]*$" eos t)
  809. (setq new (buffer-substring
  810. (1+ (match-end 0))
  811. (progn (outline-next-heading)
  812. (if (eobp) (org-back-over-empty-lines))
  813. (point)))))
  814. (setq old (and old (if (string-match "\\S-" old) old nil)))
  815. (setq new (and new (if (string-match "\\S-" new) new nil)))
  816. (if (and note (> (length note) 0))
  817. ;; Make Note into a single line, to fit into a property
  818. (setq note (mapconcat 'identity
  819. (org-split-string (org-trim note) "\n")
  820. "\\n")))
  821. (unless (equal data "body")
  822. (setq new (and new (org-trim new))
  823. old (and old (org-trim old))))
  824. (goto-char (+ 2 bos-marker))
  825. ;; Remember this place so that we can return
  826. (move-marker marker (point))
  827. (setq org-mobile-error nil)
  828. (save-excursion
  829. (condition-case msg
  830. (org-with-point-at id-pos
  831. (progn
  832. (eval cmd)
  833. (unless (member data (list "delete" "archive" "archive-sibling" "addheading"))
  834. (if (member "FLAGGED" (org-get-tags))
  835. (add-to-list 'org-mobile-last-flagged-files
  836. (buffer-file-name (current-buffer)))))))
  837. (error (setq org-mobile-error msg))))
  838. (when org-mobile-error
  839. (org-pop-to-buffer-same-window (marker-buffer marker))
  840. (goto-char marker)
  841. (incf cnt-error)
  842. (insert (if (stringp (nth 1 org-mobile-error))
  843. (nth 1 org-mobile-error)
  844. "EXECUTION FAILED")
  845. " ")
  846. (throw 'next t))
  847. ;; If we get here, the action has been applied successfully
  848. ;; So remove the entry
  849. (goto-char bos-marker)
  850. (delete-region (point) (org-end-of-subtree t t)))))
  851. (save-buffer)
  852. (move-marker marker nil)
  853. (move-marker end nil)
  854. (message "%d new, %d edits, %d flags, %d errors" cnt-new
  855. cnt-edit cnt-flag cnt-error)
  856. (sit-for 1)))
  857. (defun org-mobile-timestamp-buffer (buf)
  858. "Time stamp buffer BUF, just to make sure its checksum will change."
  859. (with-current-buffer buf
  860. (save-excursion
  861. (save-restriction
  862. (widen)
  863. (goto-char (point-min))
  864. (if (re-search-forward
  865. "^\\([ \t]*\\)#\\+LAST_MOBILE_CHANGE:.*\n?" nil t)
  866. (progn
  867. (goto-char (match-end 1))
  868. (delete-region (point) (match-end 0)))
  869. (if (looking-at ".*?-\\*-.*-\\*-")
  870. (forward-line 1)))
  871. (insert "#+LAST_MOBILE_CHANGE: "
  872. (format-time-string "%Y-%m-%d %T") "\n")))))
  873. (defun org-mobile-smart-read ()
  874. "Parse the entry at point for shortcuts and expand them.
  875. These shortcuts are meant for fast and easy typing on the limited
  876. keyboards of a mobile device. Below we show a list of the shortcuts
  877. currently implemented.
  878. The entry is expected to contain an inactive time stamp indicating when
  879. the entry was created. When setting dates and
  880. times (for example for deadlines), the time strings are interpreted
  881. relative to that creation date.
  882. Abbreviations are expected to take up entire lines, just because it is so
  883. easy to type RET on a mobile device. Abbreviations start with one or two
  884. letters, followed immediately by a dot and then additional information.
  885. Generally the entire shortcut line is removed after action have been taken.
  886. Time stamps will be constructed using `org-read-date'. So for example a
  887. line \"dd. 2tue\" will set a deadline on the second Tuesday after the
  888. creation date.
  889. Here are the shortcuts currently implemented:
  890. dd. string set deadline
  891. ss. string set scheduling
  892. tt. string set time tamp, here.
  893. ti. string set inactive time
  894. tg. tag1 tag2 tag3 set all these tags, change case where necessary
  895. td. kwd set this todo keyword, change case where necessary
  896. FIXME: Hmmm, not sure if we can make his work against the
  897. auto-correction feature. Needs a bit more thinking. So this function
  898. is currently a noop.")
  899. (defun org-mobile-locate-entry (link)
  900. (if (string-match "\\`id:\\(.*\\)$" link)
  901. (org-id-find (match-string 1 link) 'marker)
  902. (if (not (string-match "\\`olp:\\(.*?\\):\\(.*\\)$" link))
  903. ; not found with path, but maybe it is to be inserted
  904. ; in top level of the file?
  905. (if (not (string-match "\\`olp:\\(.*?\\)$" link))
  906. nil
  907. (let ((file (match-string 1 link)))
  908. (setq file (org-link-unescape file))
  909. (setq file (expand-file-name file org-directory))
  910. (save-excursion
  911. (find-file file)
  912. (goto-char (point-max))
  913. (newline)
  914. (goto-char (point-max))
  915. (point-marker))))
  916. (let ((file (match-string 1 link))
  917. (path (match-string 2 link)))
  918. (setq file (org-link-unescape file))
  919. (setq file (expand-file-name file org-directory))
  920. (setq path (mapcar 'org-link-unescape
  921. (org-split-string path "/")))
  922. (org-find-olp (cons file path))))))
  923. (defun org-mobile-edit (what old new)
  924. "Edit item WHAT in the current entry by replacing OLD with NEW.
  925. WHAT can be \"heading\", \"todo\", \"tags\", \"priority\", or \"body\".
  926. The edit only takes place if the current value is equal (except for
  927. white space) the OLD. If this is so, OLD will be replace by NEW
  928. and the command will return t. If something goes wrong, a string will
  929. be returned that indicates what went wrong."
  930. (let (current old1 new1 level)
  931. (if (stringp what) (setq what (intern what)))
  932. (cond
  933. ((memq what '(todo todostate))
  934. (setq current (org-get-todo-state))
  935. (cond
  936. ((equal new "DONEARCHIVE")
  937. (org-todo 'done)
  938. (org-archive-subtree-default))
  939. ((equal new current) t) ; nothing needs to be done
  940. ((or (equal current old)
  941. (eq org-mobile-force-mobile-change t)
  942. (memq 'todo org-mobile-force-mobile-change))
  943. (org-todo (or new 'none)) t)
  944. (t (error "State before change was expected as \"%s\", but is \"%s\""
  945. old current))))
  946. ((eq what 'tags)
  947. (setq current (org-get-tags)
  948. new1 (and new (org-split-string new ":+"))
  949. old1 (and old (org-split-string old ":+")))
  950. (cond
  951. ((org-mobile-tags-same-p current new1) t) ; no change needed
  952. ((or (org-mobile-tags-same-p current old1)
  953. (eq org-mobile-force-mobile-change t)
  954. (memq 'tags org-mobile-force-mobile-change))
  955. (org-set-tags-to new1) t)
  956. (t (error "Tags before change were expected as \"%s\", but are \"%s\""
  957. (or old "") (or current "")))))
  958. ((eq what 'priority)
  959. (when (looking-at org-complex-heading-regexp)
  960. (setq current (and (match-end 3) (substring (match-string 3) 2 3)))
  961. (cond
  962. ((equal current new) t) ; no action required
  963. ((or (equal current old)
  964. (eq org-mobile-force-mobile-change t)
  965. (memq 'tags org-mobile-force-mobile-change))
  966. (org-priority (and new (string-to-char new))))
  967. (t (error "Priority was expected to be %s, but is %s"
  968. old current)))))
  969. ((eq what 'heading)
  970. (when (looking-at org-complex-heading-regexp)
  971. (setq current (match-string 4))
  972. (cond
  973. ((equal current new) t) ; no action required
  974. ((or (equal current old)
  975. (eq org-mobile-force-mobile-change t)
  976. (memq 'heading org-mobile-force-mobile-change))
  977. (goto-char (match-beginning 4))
  978. (insert new)
  979. (delete-region (point) (+ (point) (length current)))
  980. (org-set-tags nil 'align))
  981. (t (error "Heading changed in MobileOrg and on the computer")))))
  982. ((eq what 'addheading)
  983. (if (org-at-heading-p) ; if false we are in top-level of file
  984. (progn
  985. ;; Workaround a `org-insert-heading-respect-content' bug
  986. ;; which prevents correct insertion when point is invisible
  987. (org-show-subtree)
  988. (end-of-line 1)
  989. (org-insert-heading-respect-content t)
  990. (org-demote))
  991. (beginning-of-line)
  992. (insert "* "))
  993. (insert new))
  994. ((eq what 'refile)
  995. (org-copy-subtree)
  996. (org-with-point-at (org-mobile-locate-entry new)
  997. (if (org-at-heading-p) ; if false we are in top-level of file
  998. (progn
  999. (setq level (org-get-valid-level (funcall outline-level) 1))
  1000. (org-end-of-subtree t t)
  1001. (org-paste-subtree level))
  1002. (org-paste-subtree 1)))
  1003. (org-cut-subtree))
  1004. ((eq what 'delete)
  1005. (org-cut-subtree))
  1006. ((eq what 'archive)
  1007. (org-archive-subtree))
  1008. ((eq what 'archive-sibling)
  1009. (org-archive-to-archive-sibling))
  1010. ((eq what 'body)
  1011. (setq current (buffer-substring (min (1+ (point-at-eol)) (point-max))
  1012. (save-excursion (outline-next-heading)
  1013. (point))))
  1014. (if (not (string-match "\\S-" current)) (setq current nil))
  1015. (cond
  1016. ((org-mobile-bodies-same-p current new) t) ; no action necessary
  1017. ((or (org-mobile-bodies-same-p current old)
  1018. (eq org-mobile-force-mobile-change t)
  1019. (memq 'body org-mobile-force-mobile-change))
  1020. (save-excursion
  1021. (end-of-line 1)
  1022. (insert "\n" new)
  1023. (or (bolp) (insert "\n"))
  1024. (delete-region (point) (progn (org-back-to-heading t)
  1025. (outline-next-heading)
  1026. (point))))
  1027. t)
  1028. (t (error "Body was changed in MobileOrg and on the computer")))))))
  1029. (defun org-mobile-tags-same-p (list1 list2)
  1030. "Are the two tag lists the same?"
  1031. (not (or (org-delete-all list1 list2)
  1032. (org-delete-all list2 list1))))
  1033. (defun org-mobile-bodies-same-p (a b)
  1034. "Compare if A and B are visually equal strings.
  1035. We first remove leading and trailing white space from the entire strings.
  1036. Then we split the strings into lines and remove leading/trailing whitespace
  1037. from each line. Then we compare.
  1038. A and B must be strings or nil."
  1039. (cond
  1040. ((and (not a) (not b)) t)
  1041. ((or (not a) (not b)) nil)
  1042. (t (setq a (org-trim a) b (org-trim b))
  1043. (setq a (mapconcat 'identity (org-split-string a "[ \t]*\n[ \t]*") "\n"))
  1044. (setq b (mapconcat 'identity (org-split-string b "[ \t]*\n[ \t]*") "\n"))
  1045. (equal a b))))
  1046. (provide 'org-mobile)
  1047. ;; Local variables:
  1048. ;; generated-autoload-file: "org-loaddefs.el"
  1049. ;; End:
  1050. ;;; org-mobile.el ends here