org-mobile.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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.30trans
  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.
  31. (require 'org)
  32. (require 'org-agenda)
  33. (defgroup org-mobile nil
  34. "Options concerning support for a viewer on a mobile device."
  35. :tag "Org Mobile"
  36. :group 'org)
  37. (defcustom org-mobile-directory ""
  38. "The WebDAV directory where the interaction with the mobile takes place."
  39. :group 'org-mobile
  40. :type 'directory)
  41. (defcustom org-mobile-inbox-for-pull "~/org/from-mobile.org"
  42. "The file where captured notes and flags will be appended to.
  43. During the execution of `org-mobile-pull', the file
  44. `org-mobile-capture-file' will be emptied it's contents have
  45. been appended to the file given here."
  46. :group 'org-mobile
  47. :type 'file)
  48. (defconst org-mobile-capture-file "mobileorg.org"
  49. "The capture file where the mobile stores captured notes and flags.
  50. This should not be changed, because MobileOrg assumes this name.")
  51. (defcustom org-mobile-index-file "index.org"
  52. "The index file with inks to all Org files that should be loaded by MobileOrg.
  53. Relative to `org-mobile-directory'. The Address field in the MobileOrg setup
  54. should point to this file."
  55. :group 'org-mobile
  56. :type 'file)
  57. (defcustom org-mobile-force-id-on-agenda-items t
  58. "Non-nil means make all agenda items carry and ID."
  59. :group 'org-mobile
  60. :type 'boolean)
  61. (defcustom org-mobile-action-alist
  62. '(("d" . (org-todo 'done))
  63. ("a" . (org-archive-subtree-default))
  64. ("d-a" . (progn (org-todo 'done) (org-archive-subtree-default))))
  65. "Alist with flags and actions for mobile sync.
  66. When flagging an entry, MobileOrg will create entries that look like
  67. * F(action) [[id:entry-id][entry title]]
  68. This alist defines that the action in the parentheses of F() should mean,
  69. i.e. what action should be taken. The car of each elements of the alist
  70. is an actions string. The cdr is an Emacs Lisp form that will be evaluated
  71. with the cursor on the headline of that entry."
  72. :group 'org-mobile
  73. :type '(repeat
  74. (cons (string :tag "Action flag")
  75. (sexp :tag "Action form"))))
  76. (defvar org-mobile-pre-push-hook nil
  77. "Hook run before running `org-mobile-push'.
  78. This could be used to clean up `org-mobile-directory', for example to
  79. remove files that used to be included in the agenda but no longer are.
  80. The presence of such files would not really be a problem, but after time
  81. they may accumulate.")
  82. (defvar org-mobile-post-push-hook nil
  83. "Hook run after running `org-mobile-push'.
  84. If Emacs does not have direct write access to the WebDAV directory used
  85. by the mobile device, this hook should be used to copy all files from the
  86. local staging directory `org-mobile-directory' to the WebDAV directory,
  87. for example using `rsync' or `scp'.")
  88. (defvar org-mobile-pre-pull-hook nil
  89. "Hook run before executing `org-mobile-pull'.
  90. If Emacs does not have direct write access to the WebDAV directory used
  91. by the mobile device, this hook should be used to copy the capture file
  92. `mobileorg.org' from the WebDAV location to the local staging
  93. directory `org-mobile-directory'.")
  94. (defvar org-mobile-post-pull-hook nil
  95. "Hook run after running `org-mobile-pull'.
  96. If Emacs does not have direct write access to the WebDAV directory used
  97. by the mobile device, this hook should be used to copy the emptied
  98. capture file `mobileorg.org' back to the WebDAV directory, for example
  99. using `rsync' or `scp'.")
  100. (defvar org-mobile-last-flagged-files nil
  101. "List of files containing entreis flagged in the latest pull.")
  102. ;;;###autoload
  103. (defun org-mobile-push ()
  104. "Push the current state of Org affairs to the WebDAV directory.
  105. This will create the index file, copy all agenda files there, and also
  106. create all custom agenda views, for upload to the mobile phone."
  107. (interactive)
  108. (org-mobile-check-setup)
  109. (run-hooks 'org-mobile-pre-push-hook)
  110. (org-mobile-create-sumo-agenda)
  111. (org-save-all-org-buffers) ; to save any IDs created by this process
  112. (org-mobile-copy-agenda-files)
  113. (org-mobile-create-index-file)
  114. (org-mobile-write-checksums)
  115. (run-hooks 'org-mobile-post-push-hook)
  116. (message "Files for mobile viewer staged"))
  117. ;;;###autoload
  118. (defun org-mobile-pull ()
  119. "Pull the contents of `org-mobile-capture-file' and integrate them.
  120. Apply all flagged actions, flag entries to be flagged and then call an
  121. agenda view showing the flagged items."
  122. (interactive)
  123. (org-mobile-check-setup)
  124. (run-hooks 'org-mobile-pre-pull-hook)
  125. (let ((insertion-marker (org-mobile-move-capture)))
  126. (if (not (markerp insertion-marker))
  127. (message "No new items")
  128. (org-with-point-at insertion-marker
  129. (org-mobile-apply-flags (point) (point-max)))
  130. (move-marker insertion-marker nil)
  131. (run-hooks 'org-mobile-post-pull-hook)
  132. (when org-mobile-last-flagged-files
  133. ;; Make an agenda view of flagged entries, but only in the files
  134. ;; where stuff has been added.
  135. (put 'org-agenda-files 'org-restrict org-mobile-last-flagged-files)
  136. (let ((org-agenda-keep-restriced-file-list t))
  137. (org-agenda nil "?"))))))
  138. (defun org-mobile-check-setup ()
  139. "Check if org-mobile-directory has been set up."
  140. (when (or (not org-mobile-directory)
  141. (not (stringp org-mobile-directory))
  142. (not (string-match "\\S-" org-mobile-directory))
  143. (not (file-exists-p org-mobile-directory))
  144. (not (file-directory-p org-mobile-directory)))
  145. (error
  146. "Variable `org-mobile-directory' must point to an existing directory"))
  147. (when (or (not org-mobile-inbox-for-pull)
  148. (not (stringp org-mobile-inbox-for-pull))
  149. (not (string-match "\\S-" org-mobile-inbox-for-pull))
  150. (not (file-exists-p
  151. (file-name-directory org-mobile-inbox-for-pull))))
  152. (error
  153. "Variable `org-mobile-inbox-for-pull' must point to a file in an existing directory")))
  154. (defun org-mobile-create-index-file ()
  155. "Write the index file in the WebDAV directory."
  156. (interactive)
  157. (let ((files (org-agenda-files t))
  158. file todo-kwds done-kwds drawers)
  159. (org-prepare-agenda-buffers (org-agenda-files t))
  160. (setq done-kwds (org-uniquify org-done-keywords-for-agenda))
  161. (setq todo-kwds (org-delete-all
  162. done-kwds
  163. (org-uniquify org-todo-keywords-for-agenda)))
  164. (setq drawers (org-uniquify org-drawers-for-agenda))
  165. (with-temp-file
  166. (expand-file-name org-mobile-index-file org-mobile-directory)
  167. (insert "#+TODO: " (mapconcat 'identity todo-kwds " ") " | "
  168. (mapconcat 'identity done-kwds " ") "\n"
  169. "#+DRAWERS: " (mapconcat 'identity drawers " ") "\n")
  170. (insert "* [[file:agendas.org][Agenda Views]]\n")
  171. (while (setq file (pop files))
  172. (insert (format "* [[file:%s][%s]]\n"
  173. (file-name-nondirectory file)
  174. (capitalize
  175. (file-name-sans-extension
  176. (file-name-nondirectory file))))))
  177. (insert (format "* [[file:%s][Captured before last sync]]\n"
  178. org-mobile-capture-file)))))
  179. (defun org-mobile-copy-agenda-files ()
  180. "Copy all agenda files to the stage or WebDAV directory."
  181. (let ((files (org-agenda-files t)) file buf)
  182. (while (setq file (pop files))
  183. (if (file-exists-p file)
  184. (copy-file file (expand-file-name (file-name-nondirectory file)
  185. org-mobile-directory)
  186. 'ok-if-exists)))
  187. (setq file (expand-file-name org-mobile-capture-file
  188. org-mobile-directory))
  189. (unless (file-exists-p file)
  190. (save-excursion
  191. (setq buf (find-file file))
  192. (insert "\n")
  193. (save-buffer)
  194. (kill-buffer buf)))))
  195. (defun org-mobile-write-checksums ()
  196. "Create checksums for all files in `org-mobile-directory'.
  197. The table of checksums is written to the file mobile-checksums."
  198. (let ((cmd (cond ((executable-find "shasum"))
  199. ((executable-find "sha1sum"))
  200. ((executable-find "md5sum"))
  201. ((executable-find "md5")))))
  202. (if (not cmd)
  203. (message "Checksums could not be generated: no executable")
  204. (with-temp-buffer
  205. (cd org-mobile-directory)
  206. (if (equal 0 (shell-command (concat cmd " *.org > checksums.dat")))
  207. (message "Checksums written")
  208. (message "Checksums could not be generated"))))))
  209. (defun org-mobile-sumo-agenda-command ()
  210. "Return an agenda custom command that comprises all custom commands."
  211. (let ((custom-list
  212. ;; normalize different versions
  213. (delq nil
  214. (mapcar
  215. (lambda (x)
  216. (cond ((stringp (cdr x)) nil)
  217. ((stringp (nth 1 x)) x)
  218. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  219. (t (cons (car x) (cons "" (cdr x))))))
  220. org-agenda-custom-commands)))
  221. new e key desc type match settings cmds gkey gdesc gsettings cnt)
  222. (while (setq e (pop custom-list))
  223. (cond
  224. ((stringp (cdr e))
  225. ;; this is a description entry - skip it
  226. )
  227. ((eq (nth 2 e) 'search)
  228. ;; Search view is interactive, skip
  229. )
  230. ((memq (nth 2 e) '(todo-tree tags-tree occur-tree))
  231. ;; These are trees, not really agenda commands
  232. )
  233. ((memq (nth 2 e) '(agenda todo tags))
  234. ;; a normal command
  235. (setq key (car e) desc (nth 1 e) type (nth 2 e) match (nth 3 e)
  236. settings (nth 4 e))
  237. (setq settings
  238. (cons (list 'org-agenda-title-append
  239. (concat "<break>KEYS=" key " TITLE: "
  240. (if (and (stringp desc) (> (length desc) 0))
  241. desc (symbol-name type))
  242. " " match))
  243. settings))
  244. (push (list type match settings) new))
  245. ((symbolp (nth 2 e))
  246. ;; A user-defined function, not sure how to handle that yet
  247. )
  248. (t
  249. ;; a block agenda
  250. (setq gkey (car e) gdesc (nth 1 e) gsettings (nth 3 e) cmds (nth 2 e))
  251. (setq cnt 0)
  252. (while (setq e (pop cmds))
  253. (setq type (car e) match (nth 1 e) settings (nth 2 e))
  254. (setq settings (append gsettings settings))
  255. (setq settings
  256. (cons (list 'org-agenda-title-append
  257. (concat "<break>KEYS=" gkey "#" (number-to-string
  258. (setq cnt (1+ cnt)))
  259. " TITLE: " gdesc " " match))
  260. settings))
  261. (push (list type match settings) new)))))
  262. (list "X" "SUMO" (reverse new) nil)))
  263. ;;;###autoload
  264. (defun org-mobile-create-sumo-agenda ()
  265. "Create a file that contains all custom agenda views."
  266. (interactive)
  267. (let* ((file (expand-file-name "agendas.org"
  268. org-mobile-directory))
  269. (org-agenda-custom-commands
  270. (list (append (org-mobile-sumo-agenda-command)
  271. (list (list file))))))
  272. (unless (file-writable-p file)
  273. (error "Cannot write to file %s" file))
  274. (org-batch-store-agenda-views)))
  275. (defun org-mobile-move-capture ()
  276. "Move the contents of the capture file to the inbox file.
  277. Return a marker to the location where the new content has been added.
  278. If nothing new has beed added, return nil."
  279. (interactive)
  280. (let ((inbox-buffer (find-file-noselect org-mobile-inbox-for-pull))
  281. (capture-buffer (find-file-noselect
  282. (expand-file-name org-mobile-capture-file
  283. org-mobile-directory)))
  284. (insertion-point (make-marker))
  285. not-empty content)
  286. (save-excursion
  287. (set-buffer capture-buffer)
  288. (setq content (buffer-string))
  289. (setq not-empty (string-match "\\S-" content))
  290. (when not-empty
  291. (set-buffer inbox-buffer)
  292. (widen)
  293. (goto-char (point-max))
  294. (or (bolp) (newline))
  295. (move-marker insertion-point
  296. (prog1 (point) (insert content)))
  297. (save-buffer)
  298. (set-buffer capture-buffer)
  299. (erase-buffer)
  300. (save-buffer)))
  301. (kill-buffer capture-buffer)
  302. (if not-empty insertion-point)))
  303. (defun org-mobile-apply-flags (&optional beg end)
  304. "Apply all flags in the current buffer.
  305. If BEG and END are given, only do this in that region."
  306. (interactive)
  307. (require 'org-archive)
  308. (setq org-mobile-last-flagged-files nil)
  309. (setq beg (or beg (point-min)) end (or end (point-max)))
  310. (goto-char beg)
  311. (let ((marker (make-marker))
  312. (end (move-marker (make-marker) end))
  313. action id id-pos cmd text)
  314. (while (re-search-forward
  315. "^\\*+[ \t]+F(\\([^()\n]*\\))[ \t]+\\[\\[id:\\([^]\n ]+\\)" end t)
  316. (goto-char (- (match-beginning 1) 2))
  317. (catch 'next
  318. (setq action (match-string 1)
  319. id (match-string 2)
  320. cmd (if (equal action "")
  321. '(progn
  322. (org-toggle-tag "FLAGGED" 'on)
  323. (and text (org-entry-put nil "THEFLAGGINGNOTE" text)))
  324. (cdr (assoc action org-mobile-action-alist)))
  325. text (org-trim (buffer-substring (1+ (point-at-eol))
  326. (save-excursion
  327. (org-end-of-subtree t))))
  328. id-pos (org-id-find id 'marker))
  329. (if (> (length text) 0)
  330. ;; Make TEXT into a single line, to fit into a property
  331. (setq text (mapconcat 'identity
  332. (org-split-string text "\n")
  333. "\\n"))
  334. (setq text nil))
  335. (unless id-pos
  336. (insert "BAD ID REFERENCE ")
  337. (throw 'next t))
  338. (unless cmd
  339. (insert "BAD FLAG ")
  340. (throw 'next t))
  341. (move-marker marker (point))
  342. (save-excursion
  343. (condition-case nil
  344. (org-with-point-at id-pos
  345. (progn
  346. (eval cmd)
  347. (if (member "FLAGGED" (org-get-tags))
  348. (add-to-list 'org-mobile-last-flagged-files
  349. (buffer-file-name (current-buffer))))))
  350. (error
  351. (progn
  352. (switch-to-buffer (marker-buffer marker))
  353. (goto-char marker)
  354. (insert "EXECUTION FAILED ")
  355. (throw 'next t)))))
  356. ;; If we get here, the action has been applied successfully
  357. ;; So remove the entry
  358. (org-back-to-heading t)
  359. (delete-region (point) (org-end-of-subtree t t))))
  360. (move-marker marker nil)
  361. (move-marker end nil)))
  362. (defun org-mobile-smart-read ()
  363. "Parse the entry at point for shortcuts and expand them.
  364. These shortcuts are meant for fast and easy typing on the limited
  365. keyboards of a mobile device. Below we show a list of the shortcuts
  366. currently implemented.
  367. The entry is expected to contain an inactive time stamp indicating when
  368. the entry was created. When setting dates and
  369. times (for example for deadlines), the time strings are interpreted
  370. relative to that creation date.
  371. Abbreviations are expected to take up entire lines, jst because it is so
  372. easy to type RET on a mobile device. Abbreviations start with one or two
  373. letters, followed immediately by a dot and then additional information.
  374. Generally the entire shortcut line is removed after action have been taken.
  375. Time stamps will be constructed using `org-read-date'. So for example a
  376. line \"dd. 2tue\" will set a deadline on the second Tuesday after the
  377. creation date.
  378. Here are the shortcuts currently implemented:
  379. dd. string set deadline
  380. ss. string set scheduling
  381. tt. string set time tamp, here.
  382. ti. string set inactive time
  383. tg. tag1 tag2 tag3 set all these tags, change case where necessary
  384. td. kwd set this todo keyword, change case where necessary
  385. FIXME: Hmmm, not sure if we can make his work against the
  386. auto-correction feature. Needs a bit more thinking. So this function
  387. is currently a noop.")
  388. (provide 'org-mobile)
  389. ;; arch-tag: ace0e26c-58f2-4309-8a61-05ec1535f658
  390. ;;; org-mobile.el ends here