org-mobile.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. (file-name-sans-extension 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)
  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. (defun org-mobile-write-checksums ()
  178. "Create checksums for all files in `org-mobile-directory'.
  179. The table of checksums is written to the file mobile-checksums."
  180. (let ((cmd (cond ((executable-find "shasum"))
  181. ((executable-find "sha1sum"))
  182. ((executable-find "md5sum"))
  183. ((executable-find "md5")))))
  184. (if (not cmd)
  185. (message "Checksums could not be generated: no executable")
  186. (with-temp-buffer
  187. (cd org-mobile-directory)
  188. (if (equal 0 (shell-command (concat cmd " *.org > checksums.dat")))
  189. (message "Checksums written")
  190. (message "Checksums could not be generated"))))))
  191. (defun org-mobile-sumo-agenda-command ()
  192. "Return an agenda custom command that comprises all custom commands."
  193. (let ((custom-list
  194. ;; normalize different versions
  195. (delq nil
  196. (mapcar
  197. (lambda (x)
  198. (cond ((stringp (cdr x)) nil)
  199. ((stringp (nth 1 x)) x)
  200. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  201. (t (cons (car x) (cons "" (cdr x))))))
  202. org-agenda-custom-commands)))
  203. new e key desc type match settings cmds gkey gdesc gsettings cnt)
  204. (while (setq e (pop custom-list))
  205. (cond
  206. ((stringp (cdr e))
  207. ;; this is a description entry - skip it
  208. )
  209. ((eq (nth 2 e) 'search)
  210. ;; Search view is interactive, skip
  211. )
  212. ((memq (nth 2 e) '(todo-tree tags-tree occur-tree))
  213. ;; These are trees, not really agenda commands
  214. )
  215. ((memq (nth 2 e) '(agenda todo tags))
  216. ;; a normal command
  217. (setq key (car e) desc (nth 1 e) type (nth 2 e) match (nth 3 e)
  218. settings (nth 4 e))
  219. (setq settings
  220. (cons (list 'org-agenda-title-append
  221. (concat "<break>KEYS=" key " TITLE: "
  222. (if (and (stringp desc) (> (length desc) 0))
  223. desc (symbol-name type))
  224. " " match))
  225. settings))
  226. (push (list type match settings) new))
  227. ((symbolp (nth 2 e))
  228. ;; A user-defined function, not sure how to handle that yet
  229. )
  230. (t
  231. ;; a block agenda
  232. (setq gkey (car e) gdesc (nth 1 e) gsettings (nth 3 e) cmds (nth 2 e))
  233. (setq cnt 0)
  234. (while (setq e (pop cmds))
  235. (setq type (car e) match (nth 1 e) settings (nth 2 e))
  236. (setq settings (append gsettings settings))
  237. (setq settings
  238. (cons (list 'org-agenda-title-append
  239. (concat "<break>KEYS=" gkey "#" (number-to-string
  240. (setq cnt (1+ cnt)))
  241. " TITLE: " gdesc " " match))
  242. settings))
  243. (push (list type match settings) new)))))
  244. (list "X" "SUMO" (reverse new) nil)))
  245. ;;;###autoload
  246. (defun org-mobile-create-sumo-agenda ()
  247. "Create a file that contains all custom agenda views."
  248. (interactive)
  249. (let* ((file (expand-file-name "agendas.org"
  250. org-mobile-directory))
  251. (org-agenda-custom-commands
  252. (list (append (org-mobile-sumo-agenda-command)
  253. (list (list file))))))
  254. (unless (file-writable-p file)
  255. (error "Cannot write to file %s" file))
  256. (org-batch-store-agenda-views)))
  257. (defun org-mobile-move-capture ()
  258. "Move the contents of the capture file to the inbox file.
  259. Return a marker to the location where the new content has been added.
  260. If nothing new has beed added, return nil."
  261. (interactive)
  262. (let ((inbox-buffer (find-file-noselect org-mobile-inbox-for-pull))
  263. (capture-buffer (find-file-noselect
  264. (expand-file-name org-mobile-capture-file
  265. org-mobile-directory)))
  266. (insertion-point (make-marker))
  267. not-empty content)
  268. (save-excursion
  269. (set-buffer capture-buffer)
  270. (setq content (buffer-string))
  271. (setq not-empty (string-match "\\S-" content))
  272. (when not-empty
  273. (set-buffer inbox-buffer)
  274. (widen)
  275. (goto-char (point-max))
  276. (or (bolp) (newline))
  277. (move-marker insertion-point
  278. (prog1 (point) (insert content)))
  279. (save-buffer)
  280. (set-buffer capture-buffer)
  281. (erase-buffer)
  282. (save-buffer)))
  283. (kill-buffer capture-buffer)
  284. (if not-empty insertion-point)))
  285. (defun org-mobile-apply-flags (&optional beg end)
  286. "Apply all flags in the current buffer.
  287. If BEG and END are given, only do this in that region."
  288. (interactive)
  289. (setq org-mobile-last-flagged-files nil)
  290. (setq beg (or beg (point-min)) end (or end (point-max)))
  291. (goto-char beg)
  292. (let ((marker (make-marker))
  293. (end (move-marker (make-marker) end))
  294. action id id-pos cmd text)
  295. (while (re-search-forward
  296. "^\\*+[ \t]+F(\\([^()\n]*\\))[ \t]+\\[\\[id:\\([^]\n ]+\\)" end t)
  297. (goto-char (- (match-beginning 1) 2))
  298. (catch 'next
  299. (setq action (match-string 1)
  300. id (match-string 2)
  301. cmd (if (equal action "")
  302. '(progn
  303. (org-toggle-tag "FLAGGED" 'on)
  304. (and text (org-entry-put nil "THEFLAGGINGNOTE" text)))
  305. (cdr (assoc action org-mobile-action-alist)))
  306. text (org-trim (buffer-substring (1+ (point-at-eol))
  307. (save-excursion
  308. (org-end-of-subtree t))))
  309. id-pos (org-id-find id 'marker))
  310. (if (> (length text) 0)
  311. ;; Make TEXT into a single line, to fit into a property
  312. (setq text (mapconcat 'identity
  313. (org-split-string text "\n")
  314. "\\n"))
  315. (setq text nil))
  316. (unless id-pos
  317. (insert "BAD ID REFERENCE ")
  318. (throw 'next t))
  319. (unless cmd
  320. (insert "BAD FLAG ")
  321. (throw 'next t))
  322. (move-marker marker (point))
  323. (save-excursion
  324. (condition-case nil
  325. (org-with-point-at id-pos
  326. (progn
  327. (eval cmd)
  328. (if (member "FLAGGED" (org-get-tags))
  329. (add-to-list 'org-mobile-last-flagged-files
  330. (buffer-file-name (current-buffer))))))
  331. (error
  332. (progn
  333. (switch-to-buffer (marker-buffer marker))
  334. (goto-char marker)
  335. (insert "EXECUTION FAILED ")
  336. (throw 'next t)))))
  337. ;; If we get here, the action has been applied successfully
  338. ;; So remove the entry
  339. (org-back-to-heading t)
  340. (delete-region (point) (org-end-of-subtree t t))))
  341. (move-marker marker nil)
  342. (move-marker end nil)))
  343. (defun org-mobile-smart-read ()
  344. "Parse the entry at point for shortcuts and expand them.
  345. These shortcuts are meant for fast and easy typing on the limited
  346. keyboards of a mobile device. Below we show a list of the shortcuts
  347. currently implemented.
  348. The entry is expected to contain an inactive time stamp indicating when
  349. the entry was created. When setting dates and
  350. times (for example for deadlines), the time strings are interpreted
  351. relative to that creation date.
  352. Abbreviations are expected to take up entire lines, jst because it is so
  353. easy to type RET on a mobile device. Abbreviations start with one or two
  354. letters, followed immediately by a dot and then additional information.
  355. Generally the entire shortcut line is removed after action have been taken.
  356. Time stamps will be constructed using `org-read-date'. So for example a
  357. line \"dd. 2tue\" will set a deadline on the second Tuesday after the
  358. creation date.
  359. Here are the shortcuts currently implemented:
  360. dd. string set deadline
  361. ss. string set scheduling
  362. tt. string set time tamp, here.
  363. ti. string set inactive time
  364. tg. tag1 tag2 tag3 set all these tags, change case where necessary
  365. td. kwd set this todo keyword, change case where necessary
  366. FIXME: Hmmm, not sure if we can make his work against the
  367. auto-correction feature. Needs a bit more thinking. So this function
  368. is currently a noop.")
  369. (provide 'org-mobile)
  370. ;; arch-tag: ace0e26c-58f2-4309-8a61-05ec1535f658
  371. ;;; org-mobile.el ends here