org-mobile.el 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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. (when (file-exists-p (expand-file-name
  283. org-mobile-directory "agendas.org"))
  284. (insert "* [[file:agendas.org][Agenda Views]]\n"))
  285. (while (setq entry (pop files-alist))
  286. (setq file (car entry)
  287. link-name (cdr entry))
  288. (insert (format "* [[file:%s][%s]]\n"
  289. link-name link-name))))))
  290. (defun org-mobile-copy-agenda-files ()
  291. "Copy all agenda files to the stage or WebDAV directory."
  292. (let ((files-alist org-mobile-files-alist)
  293. file buf entry link-name target-path target-dir)
  294. (while (setq entry (pop files-alist))
  295. (setq file (car entry) link-name (cdr entry))
  296. (when (file-exists-p file)
  297. (setq target-path (expand-file-name link-name org-mobile-directory)
  298. target-dir (file-name-directory target-path))
  299. (unless (file-directory-p target-dir)
  300. (make-directory target-dir 'parents))
  301. (copy-file file target-path 'ok-if-exists)))
  302. (setq file (expand-file-name org-mobile-capture-file
  303. org-mobile-directory))
  304. (unless (file-exists-p file)
  305. (save-excursion
  306. (setq buf (find-file file))
  307. (insert "\n")
  308. (save-buffer))
  309. (kill-buffer buf))))
  310. (defun org-mobile-write-checksums ()
  311. "Create checksums for all files in `org-mobile-directory'.
  312. The table of checksums is written to the file mobile-checksums."
  313. (let ((cmd (cond ((executable-find "shasum"))
  314. ((executable-find "sha1sum"))
  315. ((executable-find "md5sum"))
  316. ((executable-find "md5"))))
  317. (files org-mobile-checksum-files))
  318. (if (not cmd)
  319. (message "Checksums could not be generated: no executable")
  320. (with-temp-buffer
  321. (cd org-mobile-directory)
  322. (if (file-exists-p "agendas.org")
  323. (push "agendas.org" files))
  324. (if (file-exists-p "mobileorg.org")
  325. (push "mobileorg.org" files))
  326. (setq cmd (concat cmd " " (mapconcat 'shell-quote-argument files " ")
  327. " > checksums.dat"))
  328. (if (equal 0 (shell-command cmd))
  329. (message "Checksums written")
  330. (message "Checksums could not be generated"))))))
  331. (defun org-mobile-sumo-agenda-command ()
  332. "Return an agenda custom command that comprises all custom commands."
  333. (let ((custom-list
  334. ;; normalize different versions
  335. (delq nil
  336. (mapcar
  337. (lambda (x)
  338. (cond ((stringp (cdr x)) nil)
  339. ((stringp (nth 1 x)) x)
  340. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  341. (t (cons (car x) (cons "" (cdr x))))))
  342. org-agenda-custom-commands)))
  343. new e key desc type match settings cmds gkey gdesc gsettings cnt)
  344. (while (setq e (pop custom-list))
  345. (cond
  346. ((stringp (cdr e))
  347. ;; this is a description entry - skip it
  348. )
  349. ((eq (nth 2 e) 'search)
  350. ;; Search view is interactive, skip
  351. )
  352. ((memq (nth 2 e) '(todo-tree tags-tree occur-tree))
  353. ;; These are trees, not really agenda commands
  354. )
  355. ((memq (nth 2 e) '(agenda todo tags))
  356. ;; a normal command
  357. (setq key (car e) desc (nth 1 e) type (nth 2 e) match (nth 3 e)
  358. settings (nth 4 e))
  359. (setq settings
  360. (cons (list 'org-agenda-title-append
  361. (concat "<after>KEYS=" key " TITLE: "
  362. (if (and (stringp desc) (> (length desc) 0))
  363. desc (symbol-name type))
  364. " " match "</after>"))
  365. settings))
  366. (push (list type match settings) new))
  367. ((symbolp (nth 2 e))
  368. ;; A user-defined function, not sure how to handle that yet
  369. )
  370. (t
  371. ;; a block agenda
  372. (setq gkey (car e) gdesc (nth 1 e) gsettings (nth 3 e) cmds (nth 2 e))
  373. (setq cnt 0)
  374. (while (setq e (pop cmds))
  375. (setq type (car e) match (nth 1 e) settings (nth 2 e))
  376. (setq settings (append gsettings settings))
  377. (setq settings
  378. (cons (list 'org-agenda-title-append
  379. (concat "<after>KEYS=" gkey "#" (number-to-string
  380. (setq cnt (1+ cnt)))
  381. " TITLE: " gdesc " " match "</after>"))
  382. settings))
  383. (push (list type match settings) new)))))
  384. (and new (list "X" "SUMO" (reverse new)
  385. '((org-agenda-compact-blocks nil))))))
  386. (defvar org-mobile-creating-agendas nil)
  387. (defun org-mobile-write-agenda-for-mobile (file)
  388. (let ((all (buffer-string)) in-date id pl prefix line app short m sexp)
  389. (with-temp-file file
  390. (org-mode)
  391. (insert "#+READONLY\n")
  392. (insert all)
  393. (goto-char (point-min))
  394. (while (not (eobp))
  395. (cond
  396. ((looking-at "[ \t]*$")) ; keep empty lines
  397. ((looking-at "=+$")
  398. ;; remove underlining
  399. (delete-region (point) (point-at-eol)))
  400. ((get-text-property (point) 'org-agenda-structural-header)
  401. (setq in-date nil)
  402. (setq app (get-text-property (point)
  403. 'org-agenda-title-append))
  404. (setq short (get-text-property (point)
  405. 'short-heading))
  406. (when (and short (looking-at ".+"))
  407. (replace-match short)
  408. (beginning-of-line 1))
  409. (when app
  410. (end-of-line 1)
  411. (insert app)
  412. (beginning-of-line 1))
  413. (insert "* "))
  414. ((get-text-property (point) 'org-agenda-date-header)
  415. (setq in-date t)
  416. (insert "** "))
  417. ((setq m (or (get-text-property (point) 'org-hd-marker)
  418. (get-text-property (point) 'org-marker)))
  419. (setq sexp (member (get-text-property (point) 'type)
  420. '("diary" "sexp")))
  421. (if (setq pl (get-text-property (point) 'prefix-length))
  422. (progn
  423. (setq prefix (org-trim (buffer-substring
  424. (point) (+ (point) pl)))
  425. line (org-trim (buffer-substring
  426. (+ (point) pl)
  427. (point-at-eol))))
  428. (delete-region (point-at-bol) (point-at-eol))
  429. (insert line "<before>" prefix "</before>")
  430. (beginning-of-line 1))
  431. (and (looking-at "[ \t]+") (replace-match "")))
  432. (insert (if in-date "*** " "** "))
  433. (end-of-line 1)
  434. (insert "\n")
  435. (unless sexp
  436. (insert (org-agenda-get-some-entry-text
  437. m 10 " " 'planning)
  438. "\n")
  439. (when (setq id
  440. (if (org-bound-and-true-p
  441. org-mobile-force-id-on-agenda-items)
  442. (org-id-get m 'create)
  443. (org-entry-get m "ID")))
  444. (insert " :PROPERTIES:\n :ORIGINAL_ID: " id
  445. "\n :END:\n")))))
  446. (beginning-of-line 2)))
  447. (message "Agenda written to Org file %s" file)))
  448. ;;;###autoload
  449. (defun org-mobile-create-sumo-agenda ()
  450. "Create a file that contains all custom agenda views."
  451. (interactive)
  452. (let* ((file (expand-file-name "agendas.org"
  453. org-mobile-directory))
  454. (sumo (org-mobile-sumo-agenda-command))
  455. (org-agenda-custom-commands
  456. (list (append sumo (list (list file)))))
  457. (org-mobile-creating-agendas t))
  458. (unless (file-writable-p file)
  459. (error "Cannot write to file %s" file))
  460. (when sumo
  461. (org-store-agenda-views))))
  462. (defun org-mobile-move-capture ()
  463. "Move the contents of the capture file to the inbox file.
  464. Return a marker to the location where the new content has been added.
  465. If nothing new has beed added, return nil."
  466. (interactive)
  467. (let ((inbox-buffer (find-file-noselect org-mobile-inbox-for-pull))
  468. (capture-buffer (find-file-noselect
  469. (expand-file-name org-mobile-capture-file
  470. org-mobile-directory)))
  471. (insertion-point (make-marker))
  472. not-empty content)
  473. (save-excursion
  474. (set-buffer capture-buffer)
  475. (setq content (buffer-string))
  476. (setq not-empty (string-match "\\S-" content))
  477. (when not-empty
  478. (set-buffer inbox-buffer)
  479. (widen)
  480. (goto-char (point-max))
  481. (or (bolp) (newline))
  482. (move-marker insertion-point
  483. (prog1 (point) (insert content)))
  484. (save-buffer)
  485. (set-buffer capture-buffer)
  486. (erase-buffer)
  487. (save-buffer)))
  488. (kill-buffer capture-buffer)
  489. (if not-empty insertion-point)))
  490. (defun org-mobile-apply (&optional beg end)
  491. "Apply all change requests in the current buffer.
  492. If BEG and END are given, only do this in that region."
  493. (interactive)
  494. (require 'org-archive)
  495. (setq org-mobile-last-flagged-files nil)
  496. (setq beg (or beg (point-min)) end (or end (point-max)))
  497. ;; Remove all Note IDs
  498. (goto-char beg)
  499. (while (re-search-forward "^\\*\\* Note ID: [-0-9A-F]+[ \t]*\n" nil t)
  500. (replace-match ""))
  501. ;; Find all the referenced entries, without making any changes yet
  502. (goto-char beg)
  503. (let ((marker (make-marker))
  504. (bos-marker (make-marker))
  505. (end (move-marker (make-marker) end))
  506. buf-list
  507. id-pos org-mobile-error)
  508. (while (re-search-forward
  509. "^\\*+[ \t]+F(\\([^():\n]*\\)\\(:\\([^()\n]*\\)\\)?)[ \t]+\\[\\[\\(\\(id\\|olp\\):\\([^]\n]+\\)\\)" end t)
  510. (setq id-pos (condition-case msg
  511. (org-mobile-locate-entry (match-string 4))
  512. (error (nth 1 msg))))
  513. (when (and (markerp id-pos)
  514. (not (member (marker-buffer id-pos) buf-list)))
  515. (org-mobile-timestamp-buffer (marker-buffer id-pos))
  516. (push (marker-buffer id-pos) buf-list))
  517. (if (or (not id-pos) (stringp id-pos))
  518. (progn
  519. (goto-char (+ 2 (point-at-bol)))
  520. (insert id-pos " "))
  521. (add-text-properties (point-at-bol) (point-at-eol)
  522. (list 'org-mobile-marker
  523. (or id-pos "Linked entry not found")))))
  524. ;; OK, now go back and start applying
  525. (goto-char beg)
  526. (while (re-search-forward "^\\*+[ \t]+F(\\([^():\n]*\\)\\(:\\([^()\n]*\\)\\)?)" end t)
  527. (catch 'next
  528. (setq id-pos (get-text-property (point-at-bol) 'org-mobile-marker))
  529. (if (not (markerp id-pos))
  530. (progn
  531. (insert "UNKNOWN PROBLEM"))
  532. (let* ((action (match-string 1))
  533. (data (and (match-end 3) (match-string 3)))
  534. (bos (point-at-bol))
  535. (eos (org-end-of-subtree t t))
  536. (cmd (if (equal action "")
  537. '(progn
  538. (org-toggle-tag "FLAGGED" 'on)
  539. (and note
  540. (org-entry-put nil "THEFLAGGINGNOTE" note)))
  541. (cdr (assoc action org-mobile-action-alist))))
  542. (note (and (equal action "")
  543. (buffer-substring (1+ (point-at-eol)) eos)))
  544. (org-inhibit-logging 'note) ;; Do not take notes interactively
  545. old new)
  546. (goto-char bos)
  547. (move-marker bos-marker (point))
  548. (if (re-search-forward "^** Old value[ \t]*$" eos t)
  549. (setq old (buffer-substring
  550. (1+ (match-end 0))
  551. (progn (outline-next-heading) (point)))))
  552. (if (re-search-forward "^** New value[ \t]*$" eos t)
  553. (setq new (buffer-substring
  554. (1+ (match-end 0))
  555. (progn (outline-next-heading)
  556. (if (eobp) (org-back-over-empty-lines))
  557. (point)))))
  558. (setq old (if (string-match "\\S-" old) old nil))
  559. (setq new (if (string-match "\\S-" new) new nil))
  560. (if (and note (> (length note) 0))
  561. ;; Make Note into a single line, to fit into a property
  562. (setq note (mapconcat 'identity
  563. (org-split-string (org-trim note) "\n")
  564. "\\n")))
  565. (unless (equal data "body")
  566. (setq new (and new (org-trim new))
  567. old (and old (org-trim old))))
  568. (goto-char (+ 2 bos-marker))
  569. (unless (markerp id-pos)
  570. (insert "BAD REFERENCE ")
  571. (throw 'next t))
  572. (unless cmd
  573. (insert "BAD FLAG ")
  574. (throw 'next t))
  575. ;; Remember this place so tha we can return
  576. (move-marker marker (point))
  577. (setq org-mobile-error nil)
  578. (save-excursion
  579. (condition-case msg
  580. (org-with-point-at id-pos
  581. (progn
  582. (eval cmd)
  583. (if (member "FLAGGED" (org-get-tags))
  584. (add-to-list 'org-mobile-last-flagged-files
  585. (buffer-file-name (current-buffer))))))
  586. (error (setq org-mobile-error msg))))
  587. (when org-mobile-error
  588. (switch-to-buffer (marker-buffer marker))
  589. (goto-char marker)
  590. (insert (if (stringp (nth 1 org-mobile-error))
  591. (nth 1 org-mobile-error)
  592. "EXECUTION FAILED")
  593. " ")
  594. (throw 'next t))
  595. ;; If we get here, the action has been applied successfully
  596. ;; So remove the entry
  597. (goto-char bos-marker)
  598. (delete-region (point) (org-end-of-subtree t t))))))
  599. (move-marker marker nil)
  600. (move-marker end nil)))
  601. (defun org-mobile-timestamp-buffer (buf)
  602. "Time stamp buffer BUF, just to make sure its checksum will change."
  603. (with-current-buffer buf
  604. (save-excursion
  605. (save-restriction
  606. (widen)
  607. (goto-char (point-min))
  608. (when (re-search-forward
  609. "^\\([ \t]*\\)#\\+LAST_MOBILE_CHANGE:.*\n?" nil t)
  610. (goto-char (match-end 1))
  611. (delete-region (point) (match-end 0)))
  612. (insert "#+LAST_MOBILE_CHANGE: "
  613. (format-time-string "%Y-%m-%d %T") "\n")))))
  614. (defun org-mobile-smart-read ()
  615. "Parse the entry at point for shortcuts and expand them.
  616. These shortcuts are meant for fast and easy typing on the limited
  617. keyboards of a mobile device. Below we show a list of the shortcuts
  618. currently implemented.
  619. The entry is expected to contain an inactive time stamp indicating when
  620. the entry was created. When setting dates and
  621. times (for example for deadlines), the time strings are interpreted
  622. relative to that creation date.
  623. Abbreviations are expected to take up entire lines, jst because it is so
  624. easy to type RET on a mobile device. Abbreviations start with one or two
  625. letters, followed immediately by a dot and then additional information.
  626. Generally the entire shortcut line is removed after action have been taken.
  627. Time stamps will be constructed using `org-read-date'. So for example a
  628. line \"dd. 2tue\" will set a deadline on the second Tuesday after the
  629. creation date.
  630. Here are the shortcuts currently implemented:
  631. dd. string set deadline
  632. ss. string set scheduling
  633. tt. string set time tamp, here.
  634. ti. string set inactive time
  635. tg. tag1 tag2 tag3 set all these tags, change case where necessary
  636. td. kwd set this todo keyword, change case where necessary
  637. FIXME: Hmmm, not sure if we can make his work against the
  638. auto-correction feature. Needs a bit more thinking. So this function
  639. is currently a noop.")
  640. (defun org-find-olp (path)
  641. "Return a marker pointing to the entry at outline path OLP.
  642. If anything goes wrong, the return value will instead an error message,
  643. as a string."
  644. (let* ((file (pop path))
  645. (buffer (find-file-noselect file))
  646. (level 1)
  647. (lmin 1)
  648. (lmax 1)
  649. limit re end found pos heading cnt)
  650. (unless buffer (error "File not found :%s" file))
  651. (with-current-buffer buffer
  652. (save-excursion
  653. (save-restriction
  654. (widen)
  655. (setq limit (point-max))
  656. (goto-char (point-min))
  657. (while (setq heading (pop path))
  658. (setq re (format org-complex-heading-regexp-format
  659. (regexp-quote heading)))
  660. (setq cnt 0 pos (point))
  661. (while (re-search-forward re end t)
  662. (setq level (- (match-end 1) (match-beginning 1)))
  663. (if (and (>= level lmin) (<= level lmax))
  664. (setq found (match-beginning 0) cnt (1+ cnt))))
  665. (when (= cnt 0) (error "Heading not found on level %d: %s"
  666. lmax heading))
  667. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  668. lmax heading))
  669. (goto-char found)
  670. (setq lmin (1+ level) lmax (+ lmin (if org-odd-levels-only 1 0)))
  671. (setq end (save-excursion (org-end-of-subtree t t))))
  672. (when (org-on-heading-p)
  673. (move-marker (make-marker) (point))))))))
  674. (defun org-mobile-locate-entry (link)
  675. (if (string-match "\\`id:\\(.*\\)$" link)
  676. (org-id-find (match-string 1 link) 'marker)
  677. (if (not (string-match "\\`olp:\\(.*?\\):\\(.*\\)$" link))
  678. nil
  679. (let ((file (match-string 1 link))
  680. (path (match-string 2 link))
  681. (table '((?: . "%3a") (?\[ . "%5b") (?\] . "%5d") (?/ . "%2f"))))
  682. (setq file (org-link-unescape file table))
  683. (setq path (mapcar (lambda (x) (org-link-unescape x table))
  684. (org-split-string path "/")))
  685. (org-find-olp (cons file path))))))
  686. (defun org-mobile-edit (what old new)
  687. "Edit item WHAT in the current entry by replacing OLD wih NEW.
  688. WHAT can be \"heading\", \"todo\", \"tags\", \"priority\", or \"body\".
  689. The edit only takes place if the current value is equal (except for
  690. white space) the OLD. If this is so, OLD will be replace by NEW
  691. and the command will return t. If something goes wrong, a string will
  692. be returned that indicates what went wrong."
  693. (let (current old1 new1)
  694. (if (stringp what) (setq what (intern what)))
  695. (cond
  696. ((memq what '(todo todostate))
  697. (setq current (org-get-todo-state))
  698. (cond
  699. ((equal new "DONEARCHIVE")
  700. (org-todo 'done)
  701. (org-archive-subtree-default))
  702. ((equal new current) t) ; nothing needs to be done
  703. ((or (equal current old)
  704. (eq org-mobile-force-mobile-change t)
  705. (memq 'todo org-mobile-force-mobile-change))
  706. (org-todo new) t)
  707. (t (error "State before change was expected as \"%s\", but is \"%s\""
  708. old current))))
  709. ((eq what 'tags)
  710. (setq current (org-get-tags)
  711. new1 (and new (org-split-string new ":+"))
  712. old1 (and old (org-split-string old ":+")))
  713. (cond
  714. ((org-mobile-tags-same-p current new1) t) ; no change needed
  715. ((or (org-mobile-tags-same-p current old1)
  716. (eq org-mobile-force-mobile-change t)
  717. (memq 'tags org-mobile-force-mobile-change))
  718. (org-set-tags-to new1) t)
  719. (t (error "Tags before change were expected as \"%s\", but are \"%s\""
  720. (or old "") (or current "")))))
  721. ((eq what 'priority)
  722. (when (looking-at org-complex-heading-regexp)
  723. (setq current (and (match-end 3) (substring (match-string 3) 2 3)))
  724. (cond
  725. ((equal current new) t) ; no action required
  726. ((or (equal current old)
  727. (eq org-mobile-force-mobile-change t)
  728. (memq 'tags org-mobile-force-mobile-change))
  729. (org-priority (and new (string-to-char new))))
  730. (t (error "Priority was expected to be %s, but is %s"
  731. old current)))))
  732. ((eq what 'heading)
  733. (when (looking-at org-complex-heading-regexp)
  734. (setq current (match-string 4))
  735. (cond
  736. ((equal current new) t) ; no action required
  737. ((or (equal current old)
  738. (eq org-mobile-force-mobile-change t)
  739. (memq 'heading org-mobile-force-mobile-change))
  740. (goto-char (match-beginning 4))
  741. (insert new)
  742. (delete-region (point) (+ (point) (length current)))
  743. (org-set-tags nil 'align))
  744. (t (error "Heading changed in MobileOrg and on the computer")))))
  745. ((eq what 'body)
  746. (setq current (buffer-substring (min (1+ (point-at-eol)) (point-max))
  747. (save-excursion (outline-next-heading)
  748. (point))))
  749. (if (not (string-match "\\S-" current)) (setq current nil))
  750. (cond
  751. ((org-mobile-bodies-same-p current new) t) ; no ation necesary
  752. ((or (org-mobile-bodies-same-p current old)
  753. (eq org-mobile-force-mobile-change t)
  754. (memq 'body org-mobile-force-mobile-change))
  755. (save-excursion
  756. (end-of-line 1)
  757. (insert "\n" new)
  758. (or (bolp) (insert "\n"))
  759. (delete-region (point) (progn (org-back-to-heading t)
  760. (outline-next-heading)
  761. (point))))
  762. t)
  763. (t (error "Body was changed in MobileOrg and on the computer")))))))
  764. (defun org-mobile-tags-same-p (list1 list2)
  765. "Are the two tag lists the same?"
  766. (not (or (org-delete-all list1 list2)
  767. (org-delete-all list2 list1))))
  768. (defun org-mobile-bodies-same-p (a b)
  769. "Compare if A and B are visually equal strings.
  770. We first remove leading and trailing white space from the entire strings.
  771. Then we split the strings into lines and remove leading/trailing whitespace
  772. from each line. Then we compare.
  773. A and B must be strings or nil."
  774. (cond
  775. ((and (not a) (not b)) t)
  776. ((or (not a) (not b)) nil)
  777. (t (setq a (org-trim a) b (org-trim b))
  778. (setq a (mapconcat 'identity (org-split-string a "[ \t]*\n[ \t]*") "\n"))
  779. (setq b (mapconcat 'identity (org-split-string b "[ \t]*\n[ \t]*") "\n"))
  780. (equal a b))))
  781. (provide 'org-mobile)
  782. ;; arch-tag: ace0e26c-58f2-4309-8a61-05ec1535f658
  783. ;;; org-mobile.el ends here