org-mobile.el 18 KB

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