org-mobile.el 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  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) 'org-agenda-title-append))
  582. (setq short (get-text-property (point) 'short-heading))
  583. (when (and short (looking-at ".+"))
  584. (replace-match short nil t)
  585. (beginning-of-line 1))
  586. (when app
  587. (end-of-line 1)
  588. (insert app)
  589. (beginning-of-line 1))
  590. (insert "* "))
  591. ((get-text-property (point) 'org-agenda-date-header)
  592. (setq in-date t)
  593. (insert "** "))
  594. ((setq m (or (get-text-property (point) 'org-hd-marker)
  595. (get-text-property (point) 'org-marker)))
  596. (setq sexp (member (get-text-property (point) 'type)
  597. '("diary" "sexp")))
  598. (if (setq pl (text-property-any (point) (point-at-eol) 'org-heading t))
  599. (progn
  600. (setq prefix (org-trim (buffer-substring
  601. (point) pl))
  602. line (org-trim (buffer-substring
  603. pl
  604. (point-at-eol))))
  605. (delete-region (point-at-bol) (point-at-eol))
  606. (insert line "<before>" prefix "</before>")
  607. (beginning-of-line 1))
  608. (and (looking-at "[ \t]+") (replace-match "")))
  609. (insert (if in-date "*** " "** "))
  610. (end-of-line 1)
  611. (insert "\n")
  612. (unless sexp
  613. (insert (org-agenda-get-some-entry-text
  614. m 10 " " 'planning)
  615. "\n")
  616. (when (setq id
  617. (if (org-bound-and-true-p
  618. org-mobile-force-id-on-agenda-items)
  619. (org-id-get m 'create)
  620. (or (org-entry-get m "ID")
  621. (org-mobile-get-outline-path-link m))))
  622. (insert " :PROPERTIES:\n :ORIGINAL_ID: " id
  623. "\n :END:\n")))))
  624. (beginning-of-line 2))
  625. (push (cons "agendas.org" (md5 (buffer-string)))
  626. org-mobile-checksum-files))
  627. (message "Agenda written to Org file %s" file)))
  628. (defun org-mobile-get-outline-path-link (pom)
  629. (org-with-point-at pom
  630. (concat "olp:"
  631. (org-mobile-escape-olp (file-name-nondirectory buffer-file-name))
  632. "/"
  633. (mapconcat 'org-mobile-escape-olp
  634. (org-get-outline-path)
  635. "/")
  636. "/"
  637. (org-mobile-escape-olp (nth 4 (org-heading-components))))))
  638. (defun org-mobile-escape-olp (s)
  639. (let ((table '(?: ?/)))
  640. (org-link-escape s table)))
  641. (defun org-mobile-create-sumo-agenda ()
  642. "Create a file that contains all custom agenda views."
  643. (interactive)
  644. (let* ((file (expand-file-name "agendas.org"
  645. org-mobile-directory))
  646. (file1 (if org-mobile-use-encryption
  647. org-mobile-encryption-tempfile
  648. file))
  649. (sumo (org-mobile-sumo-agenda-command))
  650. (org-agenda-custom-commands
  651. (list (append sumo (list (list file1)))))
  652. (org-mobile-creating-agendas t))
  653. (unless (file-writable-p file1)
  654. (error "Cannot write to file %s" file1))
  655. (when sumo
  656. (org-store-agenda-views))
  657. (when org-mobile-use-encryption
  658. (org-mobile-encrypt-and-move file1 file)
  659. (delete-file file1)
  660. (org-mobile-cleanup-encryption-tempfile))))
  661. (defun org-mobile-encrypt-and-move (infile outfile)
  662. "Encrypt INFILE locally to INFILE_enc, then move it to OUTFILE.
  663. We do this in two steps so that remote paths will work, even if the
  664. encryption program does not understand them."
  665. (let ((encfile (concat infile "_enc")))
  666. (org-mobile-encrypt-file infile encfile)
  667. (when outfile
  668. (copy-file encfile outfile 'ok-if-exists)
  669. (delete-file encfile))))
  670. (defun org-mobile-encrypt-file (infile outfile)
  671. "Encrypt INFILE to OUTFILE, using `org-mobile-encryption-password'."
  672. (shell-command
  673. (format "openssl enc -aes-256-cbc -salt -pass %s -in %s -out %s"
  674. (shell-quote-argument (concat "pass:"
  675. (org-mobile-encryption-password)))
  676. (shell-quote-argument (expand-file-name infile))
  677. (shell-quote-argument (expand-file-name outfile)))))
  678. (defun org-mobile-decrypt-file (infile outfile)
  679. "Decrypt INFILE to OUTFILE, using `org-mobile-encryption-password'."
  680. (shell-command
  681. (format "openssl enc -d -aes-256-cbc -salt -pass %s -in %s -out %s"
  682. (shell-quote-argument (concat "pass:"
  683. (org-mobile-encryption-password)))
  684. (shell-quote-argument (expand-file-name infile))
  685. (shell-quote-argument (expand-file-name outfile)))))
  686. (defun org-mobile-cleanup-encryption-tempfile ()
  687. "Remove the encryption tempfile if it exists."
  688. (and (stringp org-mobile-encryption-tempfile)
  689. (file-exists-p org-mobile-encryption-tempfile)
  690. (delete-file org-mobile-encryption-tempfile)))
  691. (defun org-mobile-move-capture ()
  692. "Move the contents of the capture file to the inbox file.
  693. Return a marker to the location where the new content has been added.
  694. If nothing new has been added, return nil."
  695. (interactive)
  696. (let* ((encfile nil)
  697. (capture-file (expand-file-name org-mobile-capture-file
  698. org-mobile-directory))
  699. (inbox-buffer (find-file-noselect org-mobile-inbox-for-pull))
  700. (capture-buffer
  701. (if (not org-mobile-use-encryption)
  702. (find-file-noselect capture-file)
  703. (org-mobile-cleanup-encryption-tempfile)
  704. (setq encfile (concat org-mobile-encryption-tempfile "_enc"))
  705. (copy-file capture-file encfile)
  706. (org-mobile-decrypt-file encfile org-mobile-encryption-tempfile)
  707. (find-file-noselect org-mobile-encryption-tempfile)))
  708. (insertion-point (make-marker))
  709. not-empty content)
  710. (with-current-buffer capture-buffer
  711. (setq content (buffer-string))
  712. (setq not-empty (string-match "\\S-" content))
  713. (when not-empty
  714. (set-buffer inbox-buffer)
  715. (widen)
  716. (goto-char (point-max))
  717. (or (bolp) (newline))
  718. (move-marker insertion-point
  719. (prog1 (point) (insert content)))
  720. (save-buffer)
  721. (set-buffer capture-buffer)
  722. (erase-buffer)
  723. (save-buffer)
  724. (org-mobile-update-checksum-for-capture-file (buffer-string))))
  725. (kill-buffer capture-buffer)
  726. (when org-mobile-use-encryption
  727. (org-mobile-encrypt-and-move org-mobile-encryption-tempfile
  728. capture-file)
  729. (org-mobile-cleanup-encryption-tempfile))
  730. (if not-empty insertion-point)))
  731. (defun org-mobile-update-checksum-for-capture-file (buffer-string)
  732. "Find the checksum line and modify it to match BUFFER-STRING."
  733. (let* ((file (expand-file-name "checksums.dat" org-mobile-directory))
  734. (buffer (find-file-noselect file)))
  735. (when buffer
  736. (with-current-buffer buffer
  737. (when (re-search-forward (concat "\\([0-9a-fA-F]\\{30,\\}\\).*?"
  738. (regexp-quote org-mobile-capture-file)
  739. "[ \t]*$") nil t)
  740. (goto-char (match-beginning 1))
  741. (delete-region (match-beginning 1) (match-end 1))
  742. (insert (md5 buffer-string))
  743. (save-buffer)))
  744. (kill-buffer buffer))))
  745. (defun org-mobile-apply (&optional beg end)
  746. "Apply all change requests in the current buffer.
  747. If BEG and END are given, only do this in that region."
  748. (interactive)
  749. (require 'org-archive)
  750. (setq org-mobile-last-flagged-files nil)
  751. (setq beg (or beg (point-min)) end (or end (point-max)))
  752. ;; Remove all Note IDs
  753. (goto-char beg)
  754. (while (re-search-forward "^\\*\\* Note ID: [-0-9A-F]+[ \t]*\n" end t)
  755. (replace-match ""))
  756. ;; Find all the referenced entries, without making any changes yet
  757. (let ((marker (make-marker))
  758. (bos-marker (make-marker))
  759. (end (move-marker (make-marker) end))
  760. (cnt-new 0)
  761. (cnt-edit 0)
  762. (cnt-flag 0)
  763. (cnt-error 0)
  764. buf-list
  765. id-pos org-mobile-error)
  766. ;; Count the new captures
  767. (goto-char beg)
  768. (while (re-search-forward "^\\* \\(.*\\)" end t)
  769. (and (>= (- (match-end 1) (match-beginning 1)) 2)
  770. (not (equal (downcase (substring (match-string 1) 0 2)) "f("))
  771. (incf cnt-new)))
  772. ;; Find and apply the edits
  773. (goto-char beg)
  774. (while (re-search-forward
  775. "^\\*+[ \t]+F(\\([^():\n]*\\)\\(:\\([^()\n]*\\)\\)?)[ \t]+\\[\\[\\(\\(id\\|olp\\):\\([^]\n]+\\)\\)" end t)
  776. (catch 'next
  777. (let* ((action (match-string 1))
  778. (data (and (match-end 3) (match-string 3)))
  779. (id-pos (condition-case msg
  780. (org-mobile-locate-entry (match-string 4))
  781. (error (nth 1 msg))))
  782. (bos (point-at-bol))
  783. (eos (save-excursion (org-end-of-subtree t t)))
  784. (cmd (if (equal action "")
  785. '(progn
  786. (incf cnt-flag)
  787. (org-toggle-tag "FLAGGED" 'on)
  788. (and note
  789. (org-entry-put nil "THEFLAGGINGNOTE" note)))
  790. (incf cnt-edit)
  791. (cdr (assoc action org-mobile-action-alist))))
  792. (note (and (equal action "")
  793. (buffer-substring (1+ (point-at-eol)) eos)))
  794. (org-inhibit-logging 'note) ;; Do not take notes interactively
  795. old new)
  796. (goto-char bos)
  797. (when (and (markerp id-pos)
  798. (not (member (marker-buffer id-pos) buf-list)))
  799. (org-mobile-timestamp-buffer (marker-buffer id-pos))
  800. (push (marker-buffer id-pos) buf-list))
  801. (unless (markerp id-pos)
  802. (goto-char (+ 2 (point-at-bol)))
  803. (if (stringp id-pos)
  804. (insert id-pos " ")
  805. (insert "BAD REFERENCE "))
  806. (incf cnt-error)
  807. (throw 'next t))
  808. (unless cmd
  809. (insert "BAD FLAG ")
  810. (incf cnt-error)
  811. (throw 'next t))
  812. (move-marker bos-marker (point))
  813. (if (re-search-forward "^** Old value[ \t]*$" eos t)
  814. (setq old (buffer-substring
  815. (1+ (match-end 0))
  816. (progn (outline-next-heading) (point)))))
  817. (if (re-search-forward "^** New value[ \t]*$" eos t)
  818. (setq new (buffer-substring
  819. (1+ (match-end 0))
  820. (progn (outline-next-heading)
  821. (if (eobp) (org-back-over-empty-lines))
  822. (point)))))
  823. (setq old (and old (if (string-match "\\S-" old) old nil)))
  824. (setq new (and new (if (string-match "\\S-" new) new nil)))
  825. (if (and note (> (length note) 0))
  826. ;; Make Note into a single line, to fit into a property
  827. (setq note (mapconcat 'identity
  828. (org-split-string (org-trim note) "\n")
  829. "\\n")))
  830. (unless (equal data "body")
  831. (setq new (and new (org-trim new))
  832. old (and old (org-trim old))))
  833. (goto-char (+ 2 bos-marker))
  834. ;; Remember this place so that we can return
  835. (move-marker marker (point))
  836. (setq org-mobile-error nil)
  837. (save-excursion
  838. (condition-case msg
  839. (org-with-point-at id-pos
  840. (progn
  841. (eval cmd)
  842. (unless (member data (list "delete" "archive" "archive-sibling" "addheading"))
  843. (if (member "FLAGGED" (org-get-tags))
  844. (add-to-list 'org-mobile-last-flagged-files
  845. (buffer-file-name (current-buffer)))))))
  846. (error (setq org-mobile-error msg))))
  847. (when org-mobile-error
  848. (org-pop-to-buffer-same-window (marker-buffer marker))
  849. (goto-char marker)
  850. (incf cnt-error)
  851. (insert (if (stringp (nth 1 org-mobile-error))
  852. (nth 1 org-mobile-error)
  853. "EXECUTION FAILED")
  854. " ")
  855. (throw 'next t))
  856. ;; If we get here, the action has been applied successfully
  857. ;; So remove the entry
  858. (goto-char bos-marker)
  859. (delete-region (point) (org-end-of-subtree t t)))))
  860. (save-buffer)
  861. (move-marker marker nil)
  862. (move-marker end nil)
  863. (message "%d new, %d edits, %d flags, %d errors" cnt-new
  864. cnt-edit cnt-flag cnt-error)
  865. (sit-for 1)))
  866. (defun org-mobile-timestamp-buffer (buf)
  867. "Time stamp buffer BUF, just to make sure its checksum will change."
  868. (with-current-buffer buf
  869. (save-excursion
  870. (save-restriction
  871. (widen)
  872. (goto-char (point-min))
  873. (if (re-search-forward
  874. "^\\([ \t]*\\)#\\+LAST_MOBILE_CHANGE:.*\n?" nil t)
  875. (progn
  876. (goto-char (match-end 1))
  877. (delete-region (point) (match-end 0)))
  878. (if (looking-at ".*?-\\*-.*-\\*-")
  879. (forward-line 1)))
  880. (insert "#+LAST_MOBILE_CHANGE: "
  881. (format-time-string "%Y-%m-%d %T") "\n")))))
  882. (defun org-mobile-smart-read ()
  883. "Parse the entry at point for shortcuts and expand them.
  884. These shortcuts are meant for fast and easy typing on the limited
  885. keyboards of a mobile device. Below we show a list of the shortcuts
  886. currently implemented.
  887. The entry is expected to contain an inactive time stamp indicating when
  888. the entry was created. When setting dates and
  889. times (for example for deadlines), the time strings are interpreted
  890. relative to that creation date.
  891. Abbreviations are expected to take up entire lines, just because it is so
  892. easy to type RET on a mobile device. Abbreviations start with one or two
  893. letters, followed immediately by a dot and then additional information.
  894. Generally the entire shortcut line is removed after action have been taken.
  895. Time stamps will be constructed using `org-read-date'. So for example a
  896. line \"dd. 2tue\" will set a deadline on the second Tuesday after the
  897. creation date.
  898. Here are the shortcuts currently implemented:
  899. dd. string set deadline
  900. ss. string set scheduling
  901. tt. string set time tamp, here.
  902. ti. string set inactive time
  903. tg. tag1 tag2 tag3 set all these tags, change case where necessary
  904. td. kwd set this todo keyword, change case where necessary
  905. FIXME: Hmmm, not sure if we can make his work against the
  906. auto-correction feature. Needs a bit more thinking. So this function
  907. is currently a noop.")
  908. (defun org-mobile-locate-entry (link)
  909. (if (string-match "\\`id:\\(.*\\)$" link)
  910. (org-id-find (match-string 1 link) 'marker)
  911. (if (not (string-match "\\`olp:\\(.*?\\):\\(.*\\)$" link))
  912. ; not found with path, but maybe it is to be inserted
  913. ; in top level of the file?
  914. (if (not (string-match "\\`olp:\\(.*?\\)$" link))
  915. nil
  916. (let ((file (match-string 1 link)))
  917. (setq file (org-link-unescape file))
  918. (setq file (expand-file-name file org-directory))
  919. (save-excursion
  920. (find-file file)
  921. (goto-char (point-max))
  922. (newline)
  923. (goto-char (point-max))
  924. (point-marker))))
  925. (let ((file (match-string 1 link))
  926. (path (match-string 2 link)))
  927. (setq file (org-link-unescape file))
  928. (setq file (expand-file-name file org-directory))
  929. (setq path (mapcar 'org-link-unescape
  930. (org-split-string path "/")))
  931. (org-find-olp (cons file path))))))
  932. (defun org-mobile-edit (what old new)
  933. "Edit item WHAT in the current entry by replacing OLD with NEW.
  934. WHAT can be \"heading\", \"todo\", \"tags\", \"priority\", or \"body\".
  935. The edit only takes place if the current value is equal (except for
  936. white space) the OLD. If this is so, OLD will be replace by NEW
  937. and the command will return t. If something goes wrong, a string will
  938. be returned that indicates what went wrong."
  939. (let (current old1 new1 level)
  940. (if (stringp what) (setq what (intern what)))
  941. (cond
  942. ((memq what '(todo todostate))
  943. (setq current (org-get-todo-state))
  944. (cond
  945. ((equal new "DONEARCHIVE")
  946. (org-todo 'done)
  947. (org-archive-subtree-default))
  948. ((equal new current) t) ; nothing needs to be done
  949. ((or (equal current old)
  950. (eq org-mobile-force-mobile-change t)
  951. (memq 'todo org-mobile-force-mobile-change))
  952. (org-todo (or new 'none)) t)
  953. (t (error "State before change was expected as \"%s\", but is \"%s\""
  954. old current))))
  955. ((eq what 'tags)
  956. (setq current (org-get-tags)
  957. new1 (and new (org-split-string new ":+"))
  958. old1 (and old (org-split-string old ":+")))
  959. (cond
  960. ((org-mobile-tags-same-p current new1) t) ; no change needed
  961. ((or (org-mobile-tags-same-p current old1)
  962. (eq org-mobile-force-mobile-change t)
  963. (memq 'tags org-mobile-force-mobile-change))
  964. (org-set-tags-to new1) t)
  965. (t (error "Tags before change were expected as \"%s\", but are \"%s\""
  966. (or old "") (or current "")))))
  967. ((eq what 'priority)
  968. (when (looking-at org-complex-heading-regexp)
  969. (setq current (and (match-end 3) (substring (match-string 3) 2 3)))
  970. (cond
  971. ((equal current new) t) ; no action required
  972. ((or (equal current old)
  973. (eq org-mobile-force-mobile-change t)
  974. (memq 'tags org-mobile-force-mobile-change))
  975. (org-priority (and new (string-to-char new))))
  976. (t (error "Priority was expected to be %s, but is %s"
  977. old current)))))
  978. ((eq what 'heading)
  979. (when (looking-at org-complex-heading-regexp)
  980. (setq current (match-string 4))
  981. (cond
  982. ((equal current new) t) ; no action required
  983. ((or (equal current old)
  984. (eq org-mobile-force-mobile-change t)
  985. (memq 'heading org-mobile-force-mobile-change))
  986. (goto-char (match-beginning 4))
  987. (insert new)
  988. (delete-region (point) (+ (point) (length current)))
  989. (org-set-tags nil 'align))
  990. (t (error "Heading changed in MobileOrg and on the computer")))))
  991. ((eq what 'addheading)
  992. (if (org-on-heading-p) ; if false we are in top-level of file
  993. (progn
  994. (end-of-line 1)
  995. (org-insert-heading-respect-content)
  996. (org-demote))
  997. (beginning-of-line)
  998. (insert "* "))
  999. (insert new))
  1000. ((eq what 'refile)
  1001. (org-copy-subtree)
  1002. (org-with-point-at (org-mobile-locate-entry new)
  1003. (if (org-on-heading-p) ; if false we are in top-level of file
  1004. (progn
  1005. (setq level (org-get-valid-level (funcall outline-level) 1))
  1006. (org-end-of-subtree t t)
  1007. (org-paste-subtree level))
  1008. (org-paste-subtree 1)))
  1009. (org-cut-subtree))
  1010. ((eq what 'delete)
  1011. (org-cut-subtree))
  1012. ((eq what 'archive)
  1013. (org-archive-subtree))
  1014. ((eq what 'archive-sibling)
  1015. (org-archive-to-archive-sibling))
  1016. ((eq what 'body)
  1017. (setq current (buffer-substring (min (1+ (point-at-eol)) (point-max))
  1018. (save-excursion (outline-next-heading)
  1019. (point))))
  1020. (if (not (string-match "\\S-" current)) (setq current nil))
  1021. (cond
  1022. ((org-mobile-bodies-same-p current new) t) ; no action necessary
  1023. ((or (org-mobile-bodies-same-p current old)
  1024. (eq org-mobile-force-mobile-change t)
  1025. (memq 'body org-mobile-force-mobile-change))
  1026. (save-excursion
  1027. (end-of-line 1)
  1028. (insert "\n" new)
  1029. (or (bolp) (insert "\n"))
  1030. (delete-region (point) (progn (org-back-to-heading t)
  1031. (outline-next-heading)
  1032. (point))))
  1033. t)
  1034. (t (error "Body was changed in MobileOrg and on the computer")))))))
  1035. (defun org-mobile-tags-same-p (list1 list2)
  1036. "Are the two tag lists the same?"
  1037. (not (or (org-delete-all list1 list2)
  1038. (org-delete-all list2 list1))))
  1039. (defun org-mobile-bodies-same-p (a b)
  1040. "Compare if A and B are visually equal strings.
  1041. We first remove leading and trailing white space from the entire strings.
  1042. Then we split the strings into lines and remove leading/trailing whitespace
  1043. from each line. Then we compare.
  1044. A and B must be strings or nil."
  1045. (cond
  1046. ((and (not a) (not b)) t)
  1047. ((or (not a) (not b)) nil)
  1048. (t (setq a (org-trim a) b (org-trim b))
  1049. (setq a (mapconcat 'identity (org-split-string a "[ \t]*\n[ \t]*") "\n"))
  1050. (setq b (mapconcat 'identity (org-split-string b "[ \t]*\n[ \t]*") "\n"))
  1051. (equal a b))))
  1052. (provide 'org-mobile)
  1053. ;; Local variables:
  1054. ;; generated-autoload-file: "org-loaddefs.el"
  1055. ;; End:
  1056. ;;; org-mobile.el ends here