org-mobile.el 15 KB

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