org-mobile.el 16 KB

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