org-mobile.el 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. ;;; org-mobile.el --- Code for asymmetric sync with a mobile device
  2. ;; Copyright (C) 2009 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. ;; Version: 6.31trans
  8. ;;
  9. ;; This file is part of GNU Emacs.
  10. ;;
  11. ;; GNU Emacs is free software: you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation, either version 3 of the License, or
  14. ;; (at your option) any later version.
  15. ;;
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;;
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;;
  26. ;;; Commentary:
  27. ;;
  28. ;; This file contains the code to interact with Richard Moreland's iPhone
  29. ;; application MobileOrg. This code is documented in Appendix B of the
  30. ;; Org-mode manual. The code is not specific for the iPhone, however.
  31. ;; Any external viewer/flagging/editing application that uses the same
  32. ;; conventions could be used.
  33. (require 'org)
  34. (require 'org-agenda)
  35. (defgroup org-mobile nil
  36. "Options concerning support for a viewer/editor on a mobile device."
  37. :tag "Org Mobile"
  38. :group 'org)
  39. (defcustom org-mobile-files '(org-agenda-files)
  40. "Files to be staged for MobileOrg.
  41. This is basically a list of filesand directories. Files will be staged
  42. directly. Directories will be search for files with the extension `.org'.
  43. In addition to this, the list may also contain the following symbols:
  44. org-agenda-files
  45. This means, include the complete, unrestricted list of files given in
  46. the variable `org-agenda-files'.
  47. org-agenda-text-search-extra-files
  48. Include the files given in the variable
  49. `org-agenda-text-search-extra-files'"
  50. :group 'org-mobile
  51. :type '(list :greedy t
  52. (option (const :tag "org-agenda-files" org-agenda-files))
  53. (option (const :tag "org-agenda-text-search-extra-files"
  54. org-agenda-text-search-extra-files))
  55. (repeat :inline t :tag "Additional files"
  56. (file))))
  57. (defcustom org-mobile-directory ""
  58. "The WebDAV directory where the interaction with the mobile takes place."
  59. :group 'org-mobile
  60. :type 'directory)
  61. (defcustom org-mobile-inbox-for-pull "~/org/from-mobile.org"
  62. "The file where captured notes and flags will be appended to.
  63. During the execution of `org-mobile-pull', the file
  64. `org-mobile-capture-file' will be emptied it's contents have
  65. been appended to the file given here."
  66. :group 'org-mobile
  67. :type 'file)
  68. (defconst org-mobile-capture-file "mobileorg.org"
  69. "The capture file where the mobile stores captured notes and flags.
  70. This should not be changed, because MobileOrg assumes this name.")
  71. (defcustom org-mobile-index-file "index.org"
  72. "The index file with inks to all Org files that should be loaded by MobileOrg.
  73. Relative to `org-mobile-directory'. The Address field in the MobileOrg setup
  74. should point to this file."
  75. :group 'org-mobile
  76. :type 'file)
  77. (defcustom org-mobile-force-id-on-agenda-items t
  78. "Non-nil means make all agenda items carry and ID."
  79. :group 'org-mobile
  80. :type 'boolean)
  81. (defcustom org-mobile-force-mobile-change nil
  82. "Non-nil means, force the change made on the mobile device.
  83. So even if there have been changes to the computer version of the entry,
  84. force the new value set on the mobile.
  85. When nil, mark the entry from the mobile with an error message.
  86. Instead of nil or t, this variable can also be a list of symbols, indicating
  87. the editing types for which the mobile version should always dominate."
  88. :group 'org-mobile
  89. :type '(choice
  90. (const :tag "Always" t)
  91. (const :tag "Never" nil)
  92. (set :greedy t :tag "Specify"
  93. (const todo)
  94. (const tags)
  95. (const priority)
  96. (const heading)
  97. (const body))))
  98. (defcustom org-mobile-action-alist
  99. '(("edit" . (org-mobile-edit data old new)))
  100. "Alist with flags and actions for mobile sync.
  101. When flagging an entry, MobileOrg will create entries that look like
  102. * F(action:data) [[id:entry-id][entry title]]
  103. This alist defines that the ACTION in the parentheses of F() should mean,
  104. i.e. what action should be taken. The :data part in the parenthesis is
  105. optional. If present, the string after the colon will be passed to the
  106. action form as the `data' variable.
  107. The car of each elements of the alist is an actions string. The cdr is
  108. an Emacs Lisp form that will be evaluated with the cursor on the headline
  109. of that entry.
  110. For now, it is not recommended to change this variable."
  111. :group 'org-mobile
  112. :type '(repeat
  113. (cons (string :tag "Action flag")
  114. (sexp :tag "Action form"))))
  115. (defvar org-mobile-pre-push-hook nil
  116. "Hook run before running `org-mobile-push'.
  117. This could be used to clean up `org-mobile-directory', for example to
  118. remove files that used to be included in the agenda but no longer are.
  119. The presence of such files would not really be a problem, but after time
  120. they may accumulate.")
  121. (defvar org-mobile-post-push-hook nil
  122. "Hook run after running `org-mobile-push'.
  123. If Emacs does not have direct write access to the WebDAV directory used
  124. by the mobile device, this hook should be used to copy all files from the
  125. local staging directory `org-mobile-directory' to the WebDAV directory,
  126. for example using `rsync' or `scp'.")
  127. (defvar org-mobile-pre-pull-hook nil
  128. "Hook run before executing `org-mobile-pull'.
  129. If Emacs does not have direct write access to the WebDAV directory used
  130. by the mobile device, this hook should be used to copy the capture file
  131. `mobileorg.org' from the WebDAV location to the local staging
  132. directory `org-mobile-directory'.")
  133. (defvar org-mobile-post-pull-hook nil
  134. "Hook run after running `org-mobile-pull'.
  135. If Emacs does not have direct write access to the WebDAV directory used
  136. by the mobile device, this hook should be used to copy the emptied
  137. capture file `mobileorg.org' back to the WebDAV directory, for example
  138. using `rsync' or `scp'.")
  139. (defvar org-mobile-last-flagged-files nil
  140. "List of files containing entreis flagged in the latest pull.")
  141. (defvar org-mobile-files-alist nil)
  142. (defvar org-mobile-checksum-files nil)
  143. (defun org-mobile-prepare-file-lists ()
  144. (setq org-mobile-files-alist (org-mobile-files-alist))
  145. (setq org-mobile-checksum-files (mapcar 'cdr org-mobile-files-alist)))
  146. (defun org-mobile-files-alist ()
  147. "Expand the list in `org-mobile-files' to a list of existing files."
  148. (let* ((files
  149. (apply 'append (mapcar
  150. (lambda (f)
  151. (cond
  152. ((eq f 'org-agenda-files) (org-agenda-files t))
  153. ((eq f 'org-agenda-text-search-extra-files)
  154. org-agenda-text-search-extra-files)
  155. ((and (stringp f) (file-directory-p f))
  156. (directory-files f 'full "\\.org\\'"))
  157. ((and (stringp f) (file-exists-p f))
  158. (list f))
  159. (t nil)))
  160. org-mobile-files)))
  161. (orgdir-uname (file-name-as-directory (file-truename org-directory)))
  162. (orgdir-re (concat "\\`" (regexp-quote orgdir-uname)))
  163. uname seen rtn file link-name)
  164. ;; Make the files unique, and determine the name under which they will
  165. ;; be listed.
  166. (while (setq file (pop files))
  167. (setq uname (file-truename file))
  168. (unless (member uname seen)
  169. (push uname seen)
  170. (if (string-match orgdir-re uname)
  171. (setq link-name (substring uname (match-end 0)))
  172. (setq link-name (file-name-nondirectory uname)))
  173. (push (cons file link-name) rtn)))
  174. (nreverse rtn)))
  175. ;;;###autoload
  176. (defun org-mobile-push ()
  177. "Push the current state of Org affairs to the WebDAV directory.
  178. This will create the index file, copy all agenda files there, and also
  179. create all custom agenda views, for upload to the mobile phone."
  180. (interactive)
  181. (org-mobile-check-setup)
  182. (org-mobile-prepare-file-lists)
  183. (run-hooks 'org-mobile-pre-push-hook)
  184. (org-mobile-create-sumo-agenda)
  185. (org-save-all-org-buffers) ; to save any IDs created by this process
  186. (org-mobile-copy-agenda-files)
  187. (org-mobile-create-index-file)
  188. (org-mobile-write-checksums)
  189. (run-hooks 'org-mobile-post-push-hook)
  190. (message "Files for mobile viewer staged"))
  191. ;;;###autoload
  192. (defun org-mobile-pull ()
  193. "Pull the contents of `org-mobile-capture-file' and integrate them.
  194. Apply all flagged actions, flag entries to be flagged and then call an
  195. agenda view showing the flagged items."
  196. (interactive)
  197. (org-mobile-check-setup)
  198. (run-hooks 'org-mobile-pre-pull-hook)
  199. (let ((insertion-marker (org-mobile-move-capture)))
  200. (if (not (markerp insertion-marker))
  201. (message "No new items")
  202. (org-with-point-at insertion-marker
  203. (org-mobile-apply (point) (point-max)))
  204. (move-marker insertion-marker nil)
  205. (run-hooks 'org-mobile-post-pull-hook)
  206. (when org-mobile-last-flagged-files
  207. ;; Make an agenda view of flagged entries, but only in the files
  208. ;; where stuff has been added.
  209. (put 'org-agenda-files 'org-restrict org-mobile-last-flagged-files)
  210. (let ((org-agenda-keep-restriced-file-list t))
  211. (org-agenda nil "?"))))))
  212. (defun org-mobile-check-setup ()
  213. "Check if org-mobile-directory has been set up."
  214. (when (or (not org-mobile-directory)
  215. (not (stringp org-mobile-directory))
  216. (not (string-match "\\S-" org-mobile-directory))
  217. (not (file-exists-p org-mobile-directory))
  218. (not (file-directory-p org-mobile-directory)))
  219. (error
  220. "Variable `org-mobile-directory' must point to an existing directory"))
  221. (when (or (not org-mobile-inbox-for-pull)
  222. (not (stringp org-mobile-inbox-for-pull))
  223. (not (string-match "\\S-" org-mobile-inbox-for-pull))
  224. (not (file-exists-p
  225. (file-name-directory org-mobile-inbox-for-pull))))
  226. (error
  227. "Variable `org-mobile-inbox-for-pull' must point to a file in an existing directory")))
  228. (defun org-mobile-create-index-file ()
  229. "Write the index file in the WebDAV directory."
  230. (let ((files-alist org-mobile-files-alist)
  231. (def-todo (default-value 'org-todo-keywords))
  232. (def-tags (default-value 'org-tag-alist))
  233. file link-name todo-kwds done-kwds tags drawers entry kwds dwds twds)
  234. (org-prepare-agenda-buffers (mapcar 'car files-alist))
  235. (setq done-kwds (org-uniquify org-done-keywords-for-agenda))
  236. (setq todo-kwds (org-delete-all
  237. done-kwds
  238. (org-uniquify org-todo-keywords-for-agenda)))
  239. (setq drawers (org-uniquify org-drawers-for-agenda))
  240. (setq tags (org-uniquify
  241. (delq nil
  242. (mapcar
  243. (lambda (e)
  244. (cond ((stringp e) e)
  245. ((listp e)
  246. (if (stringp (car e)) (car e) nil))
  247. (t nil)))
  248. org-tag-alist-for-agenda))))
  249. (with-temp-file
  250. (expand-file-name org-mobile-index-file org-mobile-directory)
  251. (while (setq entry (pop def-todo))
  252. (insert "#+READONLY\n")
  253. (setq kwds (mapcar (lambda (x) (if (string-match "(" x)
  254. (substring x 0 (match-beginning 0))
  255. x))
  256. (cdr entry)))
  257. (insert "#+TODO: " (mapconcat 'identity kwds " ") "\n")
  258. (setq dwds (member "|" kwds)
  259. twds (org-delete-all dwds kwds)
  260. todo-kwds (org-delete-all twds todo-kwds)
  261. done-kwds (org-delete-all dwds done-kwds)))
  262. (when (or todo-kwds done-kwds)
  263. (insert "#+TODO: " (mapconcat 'identity todo-kwds " ") " | "
  264. (mapconcat 'identity done-kwds " ") "\n"))
  265. (setq def-tags (mapcar
  266. (lambda (x)
  267. (cond ((null x) nil)
  268. ((stringp x) x)
  269. ((eq (car x) :startgroup) "{")
  270. ((eq (car x) :endgroup) "}")
  271. ((eq (car x) :newline) nil)
  272. ((listp x) (car x))
  273. (t nil)))
  274. def-tags))
  275. (setq def-tags (delq nil def-tags))
  276. (setq tags (org-delete-all def-tags tags))
  277. (setq tags (sort tags (lambda (a b) (string< (downcase a) (downcase b)))))
  278. (setq tags (append def-tags tags nil))
  279. (insert "#+TAGS: " (mapconcat 'identity tags " ") "\n")
  280. (insert "#+DRAWERS: " (mapconcat 'identity drawers " ") "\n")
  281. (insert "#+ALLPRIORITIES: A B C" "\n")
  282. (insert "* [[file:agendas.org][Agenda Views]]\n")
  283. (while (setq entry (pop files-alist))
  284. (setq file (car entry)
  285. link-name (cdr entry))
  286. (insert (format "* [[file:%s][%s]]\n"
  287. link-name link-name))))))
  288. (defun org-mobile-copy-agenda-files ()
  289. "Copy all agenda files to the stage or WebDAV directory."
  290. (let ((files-alist org-mobile-files-alist)
  291. file buf entry link-name target-path target-dir)
  292. (while (setq entry (pop files-alist))
  293. (setq file (car entry) link-name (cdr entry))
  294. (when (file-exists-p file)
  295. (setq target-path (expand-file-name link-name org-mobile-directory)
  296. target-dir (file-name-directory target-path))
  297. (unless (file-directory-p target-dir)
  298. (make-directory target-dir 'parents))
  299. (copy-file file target-path 'ok-if-exists)))
  300. (setq file (expand-file-name org-mobile-capture-file
  301. org-mobile-directory))
  302. (unless (file-exists-p file)
  303. (save-excursion
  304. (setq buf (find-file file))
  305. (insert "\n")
  306. (save-buffer))
  307. (kill-buffer buf))))
  308. (defun org-mobile-write-checksums ()
  309. "Create checksums for all files in `org-mobile-directory'.
  310. The table of checksums is written to the file mobile-checksums."
  311. (let ((cmd (cond ((executable-find "shasum"))
  312. ((executable-find "sha1sum"))
  313. ((executable-find "md5sum"))
  314. ((executable-find "md5"))))
  315. (files org-mobile-checksum-files))
  316. (if (not cmd)
  317. (message "Checksums could not be generated: no executable")
  318. (with-temp-buffer
  319. (cd org-mobile-directory)
  320. (if (file-exists-p "agendas.org")
  321. (push "agendas.org" files))
  322. (if (file-exists-p "mobileorg.org")
  323. (push "mobileorg.org" files))
  324. (setq cmd (concat cmd " " (mapconcat 'shell-quote-argument files " ")
  325. " > checksums.dat"))
  326. (if (equal 0 (shell-command cmd))
  327. (message "Checksums written")
  328. (message "Checksums could not be generated"))))))
  329. (defun org-mobile-sumo-agenda-command ()
  330. "Return an agenda custom command that comprises all custom commands."
  331. (let ((custom-list
  332. ;; normalize different versions
  333. (delq nil
  334. (mapcar
  335. (lambda (x)
  336. (cond ((stringp (cdr x)) nil)
  337. ((stringp (nth 1 x)) x)
  338. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  339. (t (cons (car x) (cons "" (cdr x))))))
  340. org-agenda-custom-commands)))
  341. new e key desc type match settings cmds gkey gdesc gsettings cnt)
  342. (while (setq e (pop custom-list))
  343. (cond
  344. ((stringp (cdr e))
  345. ;; this is a description entry - skip it
  346. )
  347. ((eq (nth 2 e) 'search)
  348. ;; Search view is interactive, skip
  349. )
  350. ((memq (nth 2 e) '(todo-tree tags-tree occur-tree))
  351. ;; These are trees, not really agenda commands
  352. )
  353. ((memq (nth 2 e) '(agenda todo tags))
  354. ;; a normal command
  355. (setq key (car e) desc (nth 1 e) type (nth 2 e) match (nth 3 e)
  356. settings (nth 4 e))
  357. (setq settings
  358. (cons (list 'org-agenda-title-append
  359. (concat "<after>KEYS=" key " TITLE: "
  360. (if (and (stringp desc) (> (length desc) 0))
  361. desc (symbol-name type))
  362. " " match "</after>"))
  363. settings))
  364. (push (list type match settings) new))
  365. ((symbolp (nth 2 e))
  366. ;; A user-defined function, not sure how to handle that yet
  367. )
  368. (t
  369. ;; a block agenda
  370. (setq gkey (car e) gdesc (nth 1 e) gsettings (nth 3 e) cmds (nth 2 e))
  371. (setq cnt 0)
  372. (while (setq e (pop cmds))
  373. (setq type (car e) match (nth 1 e) settings (nth 2 e))
  374. (setq settings (append gsettings settings))
  375. (setq settings
  376. (cons (list 'org-agenda-title-append
  377. (concat "<after>KEYS=" gkey "#" (number-to-string
  378. (setq cnt (1+ cnt)))
  379. " TITLE: " gdesc " " match "</after>"))
  380. settings))
  381. (push (list type match settings) new)))))
  382. (list "X" "SUMO" (reverse new) nil)))
  383. (defvar org-mobile-creating-agendas nil)
  384. (defun org-mobile-write-agenda-for-mobile (file)
  385. (let ((all (buffer-string)) in-date id pl prefix line app short m sexp)
  386. (with-temp-file file
  387. (org-mode)
  388. (insert "#+READONLY\n")
  389. (insert all)
  390. (goto-char (point-min))
  391. (while (not (eobp))
  392. (cond
  393. ((looking-at "[ \t]*$")) ; keep empty lines
  394. ((looking-at "=+$")
  395. ;; remove underlining
  396. (delete-region (point) (point-at-eol)))
  397. ((get-text-property (point) 'org-agenda-structural-header)
  398. (setq in-date nil)
  399. (setq app (get-text-property (point)
  400. 'org-agenda-title-append))
  401. (setq short (get-text-property (point)
  402. 'short-heading))
  403. (when (and short (looking-at ".+"))
  404. (replace-match short)
  405. (beginning-of-line 1))
  406. (when app
  407. (end-of-line 1)
  408. (insert app)
  409. (beginning-of-line 1))
  410. (insert "* "))
  411. ((get-text-property (point) 'org-agenda-date-header)
  412. (setq in-date t)
  413. (insert "** "))
  414. ((setq m (or (get-text-property (point) 'org-hd-marker)
  415. (get-text-property (point) 'org-marker)))
  416. (setq sexp (member (get-text-property (point) 'type)
  417. '("diary" "sexp")))
  418. (if (setq pl (get-text-property (point) 'prefix-length))
  419. (progn
  420. (setq prefix (org-trim (buffer-substring
  421. (point) (+ (point) pl)))
  422. line (org-trim (buffer-substring
  423. (+ (point) pl)
  424. (point-at-eol))))
  425. (delete-region (point-at-bol) (point-at-eol))
  426. (insert line "<before>" prefix "</before>")
  427. (beginning-of-line 1))
  428. (and (looking-at "[ \t]+") (replace-match "")))
  429. (insert (if in-date "*** " "** "))
  430. (end-of-line 1)
  431. (insert "\n")
  432. (unless sexp
  433. (insert (org-agenda-get-some-entry-text
  434. m 10 " " 'planning)
  435. "\n")
  436. (when (setq id
  437. (if (org-bound-and-true-p
  438. org-mobile-force-id-on-agenda-items)
  439. (org-id-get m 'create)
  440. (org-entry-get m "ID")))
  441. (insert " :PROPERTIES:\n :ORIGINAL_ID: " id
  442. "\n :END:\n")))))
  443. (beginning-of-line 2)))
  444. (message "Agenda written to Org file %s" file)))
  445. ;;;###autoload
  446. (defun org-mobile-create-sumo-agenda ()
  447. "Create a file that contains all custom agenda views."
  448. (interactive)
  449. (let* ((file (expand-file-name "agendas.org"
  450. org-mobile-directory))
  451. (org-agenda-custom-commands
  452. (list (append (org-mobile-sumo-agenda-command)
  453. (list (list file)))))
  454. (org-mobile-creating-agendas t))
  455. (unless (file-writable-p file)
  456. (error "Cannot write to file %s" file))
  457. (org-store-agenda-views)))
  458. (defun org-mobile-move-capture ()
  459. "Move the contents of the capture file to the inbox file.
  460. Return a marker to the location where the new content has been added.
  461. If nothing new has beed added, return nil."
  462. (interactive)
  463. (let ((inbox-buffer (find-file-noselect org-mobile-inbox-for-pull))
  464. (capture-buffer (find-file-noselect
  465. (expand-file-name org-mobile-capture-file
  466. org-mobile-directory)))
  467. (insertion-point (make-marker))
  468. not-empty content)
  469. (save-excursion
  470. (set-buffer capture-buffer)
  471. (setq content (buffer-string))
  472. (setq not-empty (string-match "\\S-" content))
  473. (when not-empty
  474. (set-buffer inbox-buffer)
  475. (widen)
  476. (goto-char (point-max))
  477. (or (bolp) (newline))
  478. (move-marker insertion-point
  479. (prog1 (point) (insert content)))
  480. (save-buffer)
  481. (set-buffer capture-buffer)
  482. (erase-buffer)
  483. (save-buffer)))
  484. (kill-buffer capture-buffer)
  485. (if not-empty insertion-point)))
  486. (defun org-mobile-apply (&optional beg end)
  487. "Apply all change requests in the current buffer.
  488. If BEG and END are given, only do this in that region."
  489. (interactive)
  490. (require 'org-archive)
  491. (setq org-mobile-last-flagged-files nil)
  492. (setq beg (or beg (point-min)) end (or end (point-max)))
  493. (goto-char beg)
  494. ;; Remove all Note IDs
  495. (while (re-search-forward "^\\*\\* Note ID: [-0-9A-F]+[ \t]*\n" nil t)
  496. (replace-match ""))
  497. ;; Find all the referenced entries, without making any changes yet
  498. (let ((marker (make-marker))
  499. (bos-marker (make-marker))
  500. (end (move-marker (make-marker) end))
  501. buf-list
  502. id-pos org-mobile-error)
  503. (while (re-search-forward
  504. "^\\*+[ \t]+F(\\([^():\n]*\\)\\(:\\([^()\n]*\\)\\)?)[ \t]+\\[\\[\\(\\(id\\|olp\\):\\([^]\n]+\\)\\)" end t)
  505. (setq id-pos (condition-case msg
  506. (org-mobile-locate-entry (match-string 4))
  507. (error (nth 1 msg))))
  508. (when (and (markerp id-pos)
  509. (not (member (marker-buffer id-pos) buf-list)))
  510. (org-mobile-timestamp-buffer (marker-buffer id-pos))
  511. (push (marker-buffer id-pos) buf-list))
  512. (if (or (not id-pos) (stringp id-pos))
  513. (progn
  514. (goto-char (+ 2 (point-at-bol)))
  515. (insert id-pos " "))
  516. (add-text-properties (point-at-bol) (point-at-eol)
  517. (list 'org-mobile-marker
  518. (or id-pos "Linked entry not found")))))
  519. ;; OK, now go back and start applying
  520. (goto-char beg)
  521. (while (re-search-forward "^\\*+[ \t]+F(\\([^():\n]*\\)\\(:\\([^()\n]*\\)\\)?)" end t)
  522. (catch 'next
  523. (setq id-pos (get-text-property (point-at-bol) 'org-mobile-marker))
  524. (if (not (markerp id-pos))
  525. (progn
  526. (insert "UNKNOWN PROBLEM"))
  527. (let* ((action (match-string 1))
  528. (data (and (match-end 3) (match-string 3)))
  529. (bos (point-at-bol))
  530. (eos (org-end-of-subtree t t))
  531. (cmd (if (equal action "")
  532. '(progn
  533. (org-toggle-tag "FLAGGED" 'on)
  534. (and note
  535. (org-entry-put nil "THEFLAGGINGNOTE" note)))
  536. (cdr (assoc action org-mobile-action-alist))))
  537. (note (and (equal action "")
  538. (buffer-substring (1+ (point-at-eol)) eos)))
  539. (org-inhibit-logging 'note) ;; Do not take notes interactively
  540. old new)
  541. (goto-char bos)
  542. (move-marker bos-marker (point))
  543. (if (re-search-forward "^** Old value[ \t]*$" eos t)
  544. (setq old (buffer-substring
  545. (1+ (match-end 0))
  546. (progn (outline-next-heading) (point)))))
  547. (if (re-search-forward "^** New value[ \t]*$" eos t)
  548. (setq new (buffer-substring
  549. (1+ (match-end 0))
  550. (progn (outline-next-heading)
  551. (if (eobp) (org-back-over-empty-lines))
  552. (point)))))
  553. (setq old (if (string-match "\\S-" old) old nil))
  554. (setq new (if (string-match "\\S-" new) new nil))
  555. (if (and note (> (length note) 0))
  556. ;; Make Note into a single line, to fit into a property
  557. (setq note (mapconcat 'identity
  558. (org-split-string (org-trim note) "\n")
  559. "\\n")))
  560. (unless (equal data "body")
  561. (setq new (and new (org-trim new))
  562. old (and old (org-trim old))))
  563. (goto-char (+ 2 bos-marker))
  564. (unless (markerp id-pos)
  565. (insert "BAD REFERENCE ")
  566. (throw 'next t))
  567. (unless cmd
  568. (insert "BAD FLAG ")
  569. (throw 'next t))
  570. ;; Remember this place so tha we can return
  571. (move-marker marker (point))
  572. (setq org-mobile-error nil)
  573. (save-excursion
  574. (condition-case msg
  575. (org-with-point-at id-pos
  576. (progn
  577. (eval cmd)
  578. (if (member "FLAGGED" (org-get-tags))
  579. (add-to-list 'org-mobile-last-flagged-files
  580. (buffer-file-name (current-buffer))))))
  581. (error (setq org-mobile-error msg))))
  582. (when org-mobile-error
  583. (switch-to-buffer (marker-buffer marker))
  584. (goto-char marker)
  585. (insert (if (stringp (nth 1 org-mobile-error))
  586. (nth 1 org-mobile-error)
  587. "EXECUTION FAILED")
  588. " ")
  589. (throw 'next t))
  590. ;; If we get here, the action has been applied successfully
  591. ;; So remove the entry
  592. (goto-char bos-marker)
  593. (delete-region (point) (org-end-of-subtree t t))))))
  594. (move-marker marker nil)
  595. (move-marker end nil)))
  596. (defun org-mobile-timestamp-buffer (buf)
  597. "Time stamp buffer BUF, just to make sure its checksum will change."
  598. (with-current-buffer buf
  599. (save-excursion
  600. (save-restriction
  601. (widen)
  602. (goto-char (point-min))
  603. (when (re-search-forward
  604. "^\\([ \t]*\\)#\\+LAST_MOBILE_CHANGE:.*\n?" nil t)
  605. (goto-char (match-end 1))
  606. (delete-region (point) (match-end 0)))
  607. (insert "#+LAST_MOBILE_CHANGE: "
  608. (format-time-string "%Y-%m-%d %T") "\n")))))
  609. (defun org-mobile-smart-read ()
  610. "Parse the entry at point for shortcuts and expand them.
  611. These shortcuts are meant for fast and easy typing on the limited
  612. keyboards of a mobile device. Below we show a list of the shortcuts
  613. currently implemented.
  614. The entry is expected to contain an inactive time stamp indicating when
  615. the entry was created. When setting dates and
  616. times (for example for deadlines), the time strings are interpreted
  617. relative to that creation date.
  618. Abbreviations are expected to take up entire lines, jst because it is so
  619. easy to type RET on a mobile device. Abbreviations start with one or two
  620. letters, followed immediately by a dot and then additional information.
  621. Generally the entire shortcut line is removed after action have been taken.
  622. Time stamps will be constructed using `org-read-date'. So for example a
  623. line \"dd. 2tue\" will set a deadline on the second Tuesday after the
  624. creation date.
  625. Here are the shortcuts currently implemented:
  626. dd. string set deadline
  627. ss. string set scheduling
  628. tt. string set time tamp, here.
  629. ti. string set inactive time
  630. tg. tag1 tag2 tag3 set all these tags, change case where necessary
  631. td. kwd set this todo keyword, change case where necessary
  632. FIXME: Hmmm, not sure if we can make his work against the
  633. auto-correction feature. Needs a bit more thinking. So this function
  634. is currently a noop.")
  635. (defun org-find-olp (path)
  636. "Return a marker pointing to the entry at outline path OLP.
  637. If anything goes wrong, the return value will instead an error message,
  638. as a string."
  639. (let* ((file (pop path))
  640. (buffer (find-file-noselect file))
  641. (level 1)
  642. (lmin 1)
  643. (lmax 1)
  644. limit re end found pos heading cnt)
  645. (unless buffer (error "File not found :%s" file))
  646. (with-current-buffer buffer
  647. (save-excursion
  648. (save-restriction
  649. (widen)
  650. (setq limit (point-max))
  651. (goto-char (point-min))
  652. (while (setq heading (pop path))
  653. (setq re (format org-complex-heading-regexp-format
  654. (regexp-quote heading)))
  655. (setq cnt 0 pos (point))
  656. (while (re-search-forward re end t)
  657. (setq level (- (match-end 1) (match-beginning 1)))
  658. (if (and (>= level lmin) (<= level lmax))
  659. (setq found (match-beginning 0) cnt (1+ cnt))))
  660. (when (= cnt 0) (error "Heading not found on level %d: %s"
  661. lmax heading))
  662. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  663. lmax heading))
  664. (goto-char found)
  665. (setq lmin (1+ level) lmax (+ lmin (if org-odd-levels-only 1 0)))
  666. (setq end (save-excursion (org-end-of-subtree t t))))
  667. (when (org-on-heading-p)
  668. (move-marker (make-marker) (point))))))))
  669. (defun org-mobile-locate-entry (link)
  670. (if (string-match "\\`id:\\(.*\\)$" link)
  671. (org-id-find (match-string 1 link) 'marker)
  672. (if (not (string-match "\\`olp:\\(.*?\\):\\(.*\\)$" link))
  673. nil
  674. (let ((file (match-string 1 link))
  675. (path (match-string 2 link))
  676. (table '((?: . "%3a") (?\[ . "%5b") (?\] . "%5d") (?/ . "%2f"))))
  677. (setq file (org-link-unescape file table))
  678. (setq path (mapcar (lambda (x) (org-link-unescape x table))
  679. (org-split-string path "/")))
  680. (org-find-olp (cons file path))))))
  681. (defun org-mobile-edit (what old new)
  682. "Edit item WHAT in the current entry by replacing OLD wih NEW.
  683. WHAT can be \"heading\", \"todo\", \"tags\", \"priority\", or \"body\".
  684. The edit only takes place if the current value is equal (except for
  685. white space) the OLD. If this is so, OLD will be replace by NEW
  686. and the command will return t. If something goes wrong, a string will
  687. be returned that indicates what went wrong."
  688. (let (current old1 new1)
  689. (if (stringp what) (setq what (intern what)))
  690. (case what
  691. ((todo todostate)
  692. (setq current (org-get-todo-state))
  693. (cond
  694. ((equal new "DONEARCHIVE")
  695. (org-todo 'done)
  696. (org-archive-subtree-default))
  697. ((equal new current) t) ; nothing needs to be done
  698. ((or (equal current old)
  699. (eq org-mobile-force-mobile-change t)
  700. (memq 'todo org-mobile-force-mobile-change))
  701. (org-todo new) t)
  702. (t (error "State before change was expected as \"%s\", but is \"%s\""
  703. old current))))
  704. (tags
  705. (setq current (org-get-tags)
  706. new1 (and new (org-split-string new ":+"))
  707. old1 (and old (org-split-string old ":+")))
  708. (cond
  709. ((org-mobile-tags-same-p current new1) t) ; no change needed
  710. ((or (org-mobile-tags-same-p current old1)
  711. (eq org-mobile-force-mobile-change t)
  712. (memq 'tags org-mobile-force-mobile-change))
  713. (org-set-tags-to new1) t)
  714. (t (error "State before change was expected as \"%s\", but is \"%s\""
  715. (or old "") (or current "")))))
  716. (priority
  717. (when (looking-at org-complex-heading-regexp)
  718. (setq current (and (match-end 3) (substring (match-string 3) 2 3)))
  719. (cond
  720. ((equal current new) t) ; no action required
  721. ((or (equal current old)
  722. (eq org-mobile-force-mobile-change t)
  723. (memq 'tags org-mobile-force-mobile-change))
  724. (org-priority (and new (string-to-char new))))
  725. (t (error "Priority was expected to be %s, but is %s"
  726. old current)))))
  727. (heading
  728. (when (looking-at org-complex-heading-regexp)
  729. (setq current (match-string 4))
  730. (cond
  731. ((equal current new) t) ; no action required
  732. ((or (equal current old)
  733. (eq org-mobile-force-mobile-change t)
  734. (memq 'heading org-mobile-force-mobile-change))
  735. (goto-char (match-beginning 4))
  736. (insert new)
  737. (delete-region (point) (+ (point) (length current)))
  738. (org-set-tags nil 'align))
  739. (t (error "Heading changed in MobileOrg and on the computer")))))
  740. (body
  741. (setq current (buffer-substring (min (1+ (point-at-eol)) (point-max))
  742. (save-excursion (outline-next-heading)
  743. (point))))
  744. (if (not (string-match "\\S-" current)) (setq current nil))
  745. (cond
  746. ((org-mobile-bodies-same-p current new) t) ; no ation necesary
  747. ((or (org-mobile-bodies-same-p current old)
  748. (eq org-mobile-force-mobile-change t)
  749. (memq 'body org-mobile-force-mobile-change))
  750. (save-excursion
  751. (end-of-line 1)
  752. (insert "\n" new)
  753. (or (bolp) (insert "\n"))
  754. (delete-region (point) (progn (org-back-to-heading t)
  755. (outline-next-heading)
  756. (point))))
  757. t)
  758. (t (error "Body was changed in MobileOrg and on the computer")))))))
  759. (defun org-mobile-tags-same-p (list1 list2)
  760. "Are the two tag lists the same?"
  761. (not (or (org-delete-all list1 list2)
  762. (org-delete-all list2 list1))))
  763. (defun org-mobile-bodies-same-p (a b)
  764. "Compare if A and B are visually equal strings.
  765. We first remove leading and trailing white space from the entire strings.
  766. Then we split the strings into lines and remove leading/trailing whitespace
  767. from each line. Then we compare.
  768. A and B must be strings or nil."
  769. (cond
  770. ((and (not a) (not b)) t)
  771. ((or (not a) (not b)) nil)
  772. (t (setq a (org-trim a) b (org-trim b))
  773. (setq a (mapconcat 'identity (org-split-string a "[ \t]*\n[ \t]*") "\n"))
  774. (setq b (mapconcat 'identity (org-split-string b "[ \t]*\n[ \t]*") "\n"))
  775. (equal a b))))
  776. (provide 'org-mobile)
  777. ;; arch-tag: ace0e26c-58f2-4309-8a61-05ec1535f658
  778. ;;; org-mobile.el ends here