org-mobile.el 42 KB

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