org-mobile.el 42 KB

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