org-mobile.el 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  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.32trans
  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/flagging/editing application that uses the same
  32. ;; conventions could be used.
  33. (require 'org)
  34. (require 'org-agenda)
  35. (eval-when-compile (require 'cl))
  36. (defgroup org-mobile nil
  37. "Options concerning support for a viewer/editor on a mobile device."
  38. :tag "Org Mobile"
  39. :group 'org)
  40. (defcustom org-mobile-files '(org-agenda-files)
  41. "Files to be staged for MobileOrg.
  42. This is basically a list of files and directories. Files will be staged
  43. directly. Directories will be search for files with the extension `.org'.
  44. In addition to this, the list may also contain the following symbols:
  45. org-agenda-files
  46. This means, include the complete, unrestricted list of files given in
  47. the variable `org-agenda-files'.
  48. org-agenda-text-search-extra-files
  49. Include the files given in the variable
  50. `org-agenda-text-search-extra-files'"
  51. :group 'org-mobile
  52. :type '(list :greedy t
  53. (option (const :tag "org-agenda-files" org-agenda-files))
  54. (option (const :tag "org-agenda-text-search-extra-files"
  55. org-agenda-text-search-extra-files))
  56. (repeat :inline t :tag "Additional files"
  57. (file))))
  58. (defcustom org-mobile-directory ""
  59. "The WebDAV directory where the interaction with the mobile takes place."
  60. :group 'org-mobile
  61. :type 'directory)
  62. (defcustom org-mobile-inbox-for-pull "~/org/from-mobile.org"
  63. "The file where captured notes and flags will be appended to.
  64. During the execution of `org-mobile-pull', the file
  65. `org-mobile-capture-file' will be emptied it's contents have
  66. been appended to the file given here. This file should be in
  67. `org-directory', and not in the staging area or on the web server."
  68. :group 'org-mobile
  69. :type 'file)
  70. (defconst org-mobile-capture-file "mobileorg.org"
  71. "The capture file where the mobile stores captured notes and flags.
  72. This should not be changed, because MobileOrg assumes this name.")
  73. (defcustom org-mobile-index-file "index.org"
  74. "The index file with inks to all Org files that should be loaded by MobileOrg.
  75. Relative to `org-mobile-directory'. The Address field in the MobileOrg setup
  76. should point to this file."
  77. :group 'org-mobile
  78. :type 'file)
  79. (defcustom org-mobile-force-id-on-agenda-items t
  80. "Non-nil means make all agenda items carry and ID."
  81. :group 'org-mobile
  82. :type 'boolean)
  83. (defcustom org-mobile-force-mobile-change nil
  84. "Non-nil means, force the change made on the mobile device.
  85. So even if there have been changes to the computer version of the entry,
  86. force the new value set on the mobile.
  87. When nil, mark the entry from the mobile with an error message.
  88. Instead of nil or t, this variable can also be a list of symbols, indicating
  89. the editing types for which the mobile version should always dominate."
  90. :group 'org-mobile
  91. :type '(choice
  92. (const :tag "Always" t)
  93. (const :tag "Never" nil)
  94. (set :greedy t :tag "Specify"
  95. (const todo)
  96. (const tags)
  97. (const priority)
  98. (const heading)
  99. (const body))))
  100. (defcustom org-mobile-action-alist
  101. '(("edit" . (org-mobile-edit data old new)))
  102. "Alist with flags and actions for mobile sync.
  103. When flagging an entry, MobileOrg will create entries that look like
  104. * F(action:data) [[id:entry-id][entry title]]
  105. This alist defines that the ACTION in the parentheses of F() should mean,
  106. i.e. what action should be taken. The :data part in the parenthesis is
  107. optional. If present, the string after the colon will be passed to the
  108. action form as the `data' variable.
  109. The car of each elements of the alist is an actions string. The cdr is
  110. an Emacs Lisp form that will be evaluated with the cursor on the headline
  111. of that entry.
  112. For now, it is not recommended to change this variable."
  113. :group 'org-mobile
  114. :type '(repeat
  115. (cons (string :tag "Action flag")
  116. (sexp :tag "Action form"))))
  117. (defcustom org-mobile-checksum-binary (or (executable-find "shasum")
  118. (executable-find "sha1sum")
  119. (executable-find "md5sum")
  120. (executable-find "md5"))
  121. "Executable used for computing checksums of agenda files."
  122. :group 'org-mobile
  123. :type 'string)
  124. (defvar org-mobile-pre-push-hook nil
  125. "Hook run before running `org-mobile-push'.
  126. This could be used to clean up `org-mobile-directory', for example to
  127. remove files that used to be included in the agenda but no longer are.
  128. The presence of such files would not really be a problem, but after time
  129. they may accumulate.")
  130. (defvar org-mobile-post-push-hook nil
  131. "Hook run after running `org-mobile-push'.
  132. If Emacs does not have direct write access to the WebDAV directory used
  133. by the mobile device, this hook should be used to copy all files from the
  134. local staging directory `org-mobile-directory' to the WebDAV directory,
  135. for example using `rsync' or `scp'.")
  136. (defvar org-mobile-pre-pull-hook nil
  137. "Hook run before executing `org-mobile-pull'.
  138. If Emacs does not have direct write access to the WebDAV directory used
  139. by the mobile device, this hook should be used to copy the capture file
  140. `mobileorg.org' from the WebDAV location to the local staging
  141. directory `org-mobile-directory'.")
  142. (defvar org-mobile-post-pull-hook nil
  143. "Hook run after running `org-mobile-pull'.
  144. If Emacs does not have direct write access to the WebDAV directory used
  145. by the mobile device, this hook should be used to copy the emptied
  146. capture file `mobileorg.org' back to the WebDAV directory, for example
  147. using `rsync' or `scp'.")
  148. (defvar org-mobile-last-flagged-files nil
  149. "List of files containing entries flagged in the latest pull.")
  150. (defvar org-mobile-files-alist nil)
  151. (defvar org-mobile-checksum-files nil)
  152. (defun org-mobile-prepare-file-lists ()
  153. (setq org-mobile-files-alist (org-mobile-files-alist))
  154. (setq org-mobile-checksum-files nil))
  155. (defun org-mobile-files-alist ()
  156. "Expand the list in `org-mobile-files' to a list of existing files."
  157. (let* ((include-archives
  158. (and (member 'org-agenda-text-search-extra-files org-mobile-files)
  159. (member 'agenda-archives org-agenda-text-search-extra-files)
  160. t))
  161. (files
  162. (apply 'append
  163. (mapcar
  164. (lambda (f)
  165. (cond
  166. ((eq f 'org-agenda-files)
  167. (org-agenda-files t include-archives))
  168. ((eq f 'org-agenda-text-search-extra-files)
  169. (delq 'agenda-archives
  170. (copy-sequence
  171. org-agenda-text-search-extra-files)))
  172. ((and (stringp f) (file-directory-p f))
  173. (directory-files f 'full "\\.org\\'"))
  174. ((and (stringp f) (file-exists-p f))
  175. (list f))
  176. (t nil)))
  177. org-mobile-files)))
  178. (orgdir-uname (file-name-as-directory (file-truename org-directory)))
  179. (orgdir-re (concat "\\`" (regexp-quote orgdir-uname)))
  180. uname seen rtn file link-name)
  181. ;; Make the files unique, and determine the name under which they will
  182. ;; be listed.
  183. (while (setq file (pop files))
  184. (if (not (file-name-absolute-p file))
  185. (setq file (expand-file-name file org-directory)))
  186. (setq uname (file-truename file))
  187. (unless (member uname seen)
  188. (push uname seen)
  189. (if (string-match orgdir-re uname)
  190. (setq link-name (substring uname (match-end 0)))
  191. (setq link-name (file-name-nondirectory uname)))
  192. (push (cons file link-name) rtn)))
  193. (nreverse rtn)))
  194. ;;;###autoload
  195. (defun org-mobile-push ()
  196. "Push the current state of Org affairs to the WebDAV directory.
  197. This will create the index file, copy all agenda files there, and also
  198. create all custom agenda views, for upload to the mobile phone."
  199. (interactive)
  200. (let ((a-buffer (get-buffer org-agenda-buffer-name)))
  201. (let ((org-agenda-buffer-name "*SUMO*")
  202. (org-agenda-filter org-agenda-filter)
  203. (org-agenda-redo-command org-agenda-redo-command))
  204. (save-excursion
  205. (save-window-excursion
  206. (org-mobile-check-setup)
  207. (org-mobile-prepare-file-lists)
  208. (run-hooks 'org-mobile-pre-push-hook)
  209. (message "Creating agendas...")
  210. (let ((inhibit-redisplay t)) (org-mobile-create-sumo-agenda))
  211. (message "Creating agendas...done")
  212. (org-save-all-org-buffers) ; to save any IDs created by this process
  213. (message "Copying files...")
  214. (org-mobile-copy-agenda-files)
  215. (message "Writing index file...")
  216. (org-mobile-create-index-file)
  217. (message "Writing checksums...")
  218. (org-mobile-write-checksums)
  219. (run-hooks 'org-mobile-post-push-hook))))
  220. (redraw-display)
  221. (when (and a-buffer (buffer-live-p a-buffer))
  222. (if (not (get-buffer-window a-buffer))
  223. (kill-buffer a-buffer)
  224. (let ((cw (selected-window)))
  225. (select-window (get-buffer-window a-buffer))
  226. (org-agenda-redo)
  227. (select-window cw)))))
  228. (message "Files for mobile viewer staged"))
  229. (defvar org-mobile-before-process-capture-hook nil
  230. "Hook that is run after content was moved to `org-mobile-inbox-for-pull'.
  231. The inbox file is in the current buffer, and the buffer is arrowed to the
  232. new captured data.")
  233. ;;;###autoload
  234. (defun org-mobile-pull ()
  235. "Pull the contents of `org-mobile-capture-file' and integrate them.
  236. Apply all flagged actions, flag entries to be flagged and then call an
  237. agenda view showing the flagged items."
  238. (interactive)
  239. (org-mobile-check-setup)
  240. (run-hooks 'org-mobile-pre-pull-hook)
  241. (let ((insertion-marker (org-mobile-move-capture)))
  242. (if (not (markerp insertion-marker))
  243. (message "No new items")
  244. (org-with-point-at insertion-marker
  245. (save-restriction
  246. (narrow-to-region (point) (point-max))
  247. (run-hooks 'org-mobile-before-process-capture-hook)))
  248. (org-with-point-at insertion-marker
  249. (org-mobile-apply (point) (point-max)))
  250. (move-marker insertion-marker nil)
  251. (run-hooks 'org-mobile-post-pull-hook)
  252. (when org-mobile-last-flagged-files
  253. ;; Make an agenda view of flagged entries, but only in the files
  254. ;; where stuff has been added.
  255. (put 'org-agenda-files 'org-restrict org-mobile-last-flagged-files)
  256. (let ((org-agenda-keep-restricted-file-list t))
  257. (org-agenda nil "?"))))))
  258. (defun org-mobile-check-setup ()
  259. "Check if org-mobile-directory has been set up."
  260. (unless (and org-directory
  261. (stringp org-directory)
  262. (string-match "\\S-" org-directory)
  263. (file-exists-p org-directory)
  264. (file-directory-p org-directory))
  265. (error
  266. "Please set `org-directory' to the directory where your org files live"))
  267. (unless (and org-mobile-directory
  268. (stringp org-mobile-directory)
  269. (string-match "\\S-" org-mobile-directory)
  270. (file-exists-p org-mobile-directory)
  271. (file-directory-p org-mobile-directory))
  272. (error
  273. "Variable `org-mobile-directory' must point to an existing directory"))
  274. (unless (and org-mobile-inbox-for-pull
  275. (stringp org-mobile-inbox-for-pull)
  276. (string-match "\\S-" org-mobile-inbox-for-pull)
  277. (file-exists-p
  278. (file-name-directory org-mobile-inbox-for-pull)))
  279. (error
  280. "Variable `org-mobile-inbox-for-pull' must point to a file in an existing directory")))
  281. (defun org-mobile-create-index-file ()
  282. "Write the index file in the WebDAV directory."
  283. (let ((files-alist (sort (copy-sequence org-mobile-files-alist)
  284. (lambda (a b) (string< (cdr a) (cdr b)))))
  285. (def-todo (default-value 'org-todo-keywords))
  286. (def-tags (default-value 'org-tag-alist))
  287. file link-name todo-kwds done-kwds tags drawers entry kwds dwds twds)
  288. (org-prepare-agenda-buffers (mapcar 'car files-alist))
  289. (setq done-kwds (org-uniquify org-done-keywords-for-agenda))
  290. (setq todo-kwds (org-delete-all
  291. done-kwds
  292. (org-uniquify org-todo-keywords-for-agenda)))
  293. (setq drawers (org-uniquify org-drawers-for-agenda))
  294. (setq tags (org-uniquify
  295. (delq nil
  296. (mapcar
  297. (lambda (e)
  298. (cond ((stringp e) e)
  299. ((listp e)
  300. (if (stringp (car e)) (car e) nil))
  301. (t nil)))
  302. org-tag-alist-for-agenda))))
  303. (with-temp-file
  304. (expand-file-name org-mobile-index-file org-mobile-directory)
  305. (while (setq entry (pop def-todo))
  306. (insert "#+READONLY\n")
  307. (setq kwds (mapcar (lambda (x) (if (string-match "(" x)
  308. (substring x 0 (match-beginning 0))
  309. x))
  310. (cdr entry)))
  311. (insert "#+TODO: " (mapconcat 'identity kwds " ") "\n")
  312. (setq dwds (member "|" kwds)
  313. twds (org-delete-all dwds kwds)
  314. todo-kwds (org-delete-all twds todo-kwds)
  315. done-kwds (org-delete-all dwds done-kwds)))
  316. (when (or todo-kwds done-kwds)
  317. (insert "#+TODO: " (mapconcat 'identity todo-kwds " ") " | "
  318. (mapconcat 'identity done-kwds " ") "\n"))
  319. (setq def-tags (mapcar
  320. (lambda (x)
  321. (cond ((null x) nil)
  322. ((stringp x) x)
  323. ((eq (car x) :startgroup) "{")
  324. ((eq (car x) :endgroup) "}")
  325. ((eq (car x) :newline) nil)
  326. ((listp x) (car x))
  327. (t nil)))
  328. def-tags))
  329. (setq def-tags (delq nil def-tags))
  330. (setq tags (org-delete-all def-tags tags))
  331. (setq tags (sort tags (lambda (a b) (string< (downcase a) (downcase b)))))
  332. (setq tags (append def-tags tags nil))
  333. (insert "#+TAGS: " (mapconcat 'identity tags " ") "\n")
  334. (insert "#+DRAWERS: " (mapconcat 'identity drawers " ") "\n")
  335. (insert "#+ALLPRIORITIES: A B C" "\n")
  336. (when (file-exists-p (expand-file-name
  337. org-mobile-directory "agendas.org"))
  338. (insert "* [[file:agendas.org][Agenda Views]]\n"))
  339. (while (setq entry (pop files-alist))
  340. (setq file (car entry)
  341. link-name (cdr entry))
  342. (insert (format "* [[file:%s][%s]]\n"
  343. link-name link-name)))
  344. (push (cons org-mobile-index-file (md5 (buffer-string)))
  345. org-mobile-checksum-files))))
  346. (defun org-mobile-copy-agenda-files ()
  347. "Copy all agenda files to the stage or WebDAV directory."
  348. (let ((files-alist org-mobile-files-alist)
  349. file buf entry link-name target-path target-dir check)
  350. (while (setq entry (pop files-alist))
  351. (setq file (car entry) link-name (cdr entry))
  352. (when (file-exists-p file)
  353. (setq target-path (expand-file-name link-name org-mobile-directory)
  354. target-dir (file-name-directory target-path))
  355. (unless (file-directory-p target-dir)
  356. (make-directory target-dir 'parents))
  357. (copy-file file target-path 'ok-if-exists)
  358. (setq check (shell-command-to-string
  359. (concat org-mobile-checksum-binary " "
  360. (shell-quote-argument (expand-file-name file)))))
  361. (when (string-match "[a-fA-F0-9]\\{30,40\\}" check)
  362. (push (cons link-name (match-string 0 check))
  363. org-mobile-checksum-files))))
  364. (setq file (expand-file-name org-mobile-capture-file
  365. org-mobile-directory))
  366. (save-excursion
  367. (setq buf (find-file file))
  368. (and (= (point-min) (point-max)) (insert "\n"))
  369. (save-buffer)
  370. (push (cons org-mobile-capture-file (md5 (buffer-string)))
  371. org-mobile-checksum-files))
  372. (kill-buffer buf)))
  373. (defun org-mobile-write-checksums ()
  374. "Create checksums for all files in `org-mobile-directory'.
  375. The table of checksums is written to the file mobile-checksums."
  376. (let ((sumfile (expand-file-name "checksums.dat" org-mobile-directory))
  377. (files org-mobile-checksum-files)
  378. entry file sum)
  379. (with-temp-file sumfile
  380. (set-buffer-file-coding-system 'undecided-unix nil)
  381. (while (setq entry (pop files))
  382. (setq file (car entry) sum (cdr entry))
  383. (insert (format "%s %s\n" sum file))))))
  384. (defun org-mobile-sumo-agenda-command ()
  385. "Return an agenda custom command that comprises all custom commands."
  386. (let ((custom-list
  387. ;; normalize different versions
  388. (delq nil
  389. (mapcar
  390. (lambda (x)
  391. (cond ((stringp (cdr x)) nil)
  392. ((stringp (nth 1 x)) x)
  393. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  394. (t (cons (car x) (cons "" (cdr x))))))
  395. org-agenda-custom-commands)))
  396. new e key desc type match settings cmds gkey gdesc gsettings cnt)
  397. (while (setq e (pop custom-list))
  398. (cond
  399. ((stringp (cdr e))
  400. ;; this is a description entry - skip it
  401. )
  402. ((eq (nth 2 e) 'search)
  403. ;; Search view is interactive, skip
  404. )
  405. ((memq (nth 2 e) '(todo-tree tags-tree occur-tree))
  406. ;; These are trees, not really agenda commands
  407. )
  408. ((memq (nth 2 e) '(agenda todo tags))
  409. ;; a normal command
  410. (setq key (car e) desc (nth 1 e) type (nth 2 e) match (nth 3 e)
  411. settings (nth 4 e))
  412. (setq settings
  413. (cons (list 'org-agenda-title-append
  414. (concat "<after>KEYS=" key " TITLE: "
  415. (if (and (stringp desc) (> (length desc) 0))
  416. desc (symbol-name type))
  417. " " match "</after>"))
  418. settings))
  419. (push (list type match settings) new))
  420. ((symbolp (nth 2 e))
  421. ;; A user-defined function, not sure how to handle that yet
  422. )
  423. (t
  424. ;; a block agenda
  425. (setq gkey (car e) gdesc (nth 1 e) gsettings (nth 3 e) cmds (nth 2 e))
  426. (setq cnt 0)
  427. (while (setq e (pop cmds))
  428. (setq type (car e) match (nth 1 e) settings (nth 2 e))
  429. (setq settings (append gsettings settings))
  430. (setq settings
  431. (cons (list 'org-agenda-title-append
  432. (concat "<after>KEYS=" gkey "#" (number-to-string
  433. (setq cnt (1+ cnt)))
  434. " TITLE: " gdesc " " match "</after>"))
  435. settings))
  436. (push (list type match settings) new)))))
  437. (and new (list "X" "SUMO" (reverse new)
  438. '((org-agenda-compact-blocks nil))))))
  439. (defvar org-mobile-creating-agendas nil)
  440. (defun org-mobile-write-agenda-for-mobile (file)
  441. (let ((all (buffer-string)) in-date id pl prefix line app short m sexp)
  442. (with-temp-file file
  443. (org-mode)
  444. (insert "#+READONLY\n")
  445. (insert all)
  446. (goto-char (point-min))
  447. (while (not (eobp))
  448. (cond
  449. ((looking-at "[ \t]*$")) ; keep empty lines
  450. ((looking-at "=+$")
  451. ;; remove underlining
  452. (delete-region (point) (point-at-eol)))
  453. ((get-text-property (point) 'org-agenda-structural-header)
  454. (setq in-date nil)
  455. (setq app (get-text-property (point)
  456. 'org-agenda-title-append))
  457. (setq short (get-text-property (point)
  458. 'short-heading))
  459. (when (and short (looking-at ".+"))
  460. (replace-match short)
  461. (beginning-of-line 1))
  462. (when app
  463. (end-of-line 1)
  464. (insert app)
  465. (beginning-of-line 1))
  466. (insert "* "))
  467. ((get-text-property (point) 'org-agenda-date-header)
  468. (setq in-date t)
  469. (insert "** "))
  470. ((setq m (or (get-text-property (point) 'org-hd-marker)
  471. (get-text-property (point) 'org-marker)))
  472. (setq sexp (member (get-text-property (point) 'type)
  473. '("diary" "sexp")))
  474. (if (setq pl (get-text-property (point) 'prefix-length))
  475. (progn
  476. (setq prefix (org-trim (buffer-substring
  477. (point) (+ (point) pl)))
  478. line (org-trim (buffer-substring
  479. (+ (point) pl)
  480. (point-at-eol))))
  481. (delete-region (point-at-bol) (point-at-eol))
  482. (insert line "<before>" prefix "</before>")
  483. (beginning-of-line 1))
  484. (and (looking-at "[ \t]+") (replace-match "")))
  485. (insert (if in-date "*** " "** "))
  486. (end-of-line 1)
  487. (insert "\n")
  488. (unless sexp
  489. (insert (org-agenda-get-some-entry-text
  490. m 10 " " 'planning)
  491. "\n")
  492. (when (setq id
  493. (if (org-bound-and-true-p
  494. org-mobile-force-id-on-agenda-items)
  495. (org-id-get m 'create)
  496. (org-entry-get m "ID")))
  497. (insert " :PROPERTIES:\n :ORIGINAL_ID: " id
  498. "\n :END:\n")))))
  499. (beginning-of-line 2))
  500. (push (cons (file-name-nondirectory file) (md5 (buffer-string)))
  501. org-mobile-checksum-files))
  502. (message "Agenda written to Org file %s" file)))
  503. ;;;###autoload
  504. (defun org-mobile-create-sumo-agenda ()
  505. "Create a file that contains all custom agenda views."
  506. (interactive)
  507. (let* ((file (expand-file-name "agendas.org"
  508. org-mobile-directory))
  509. (sumo (org-mobile-sumo-agenda-command))
  510. (org-agenda-custom-commands
  511. (list (append sumo (list (list file)))))
  512. (org-mobile-creating-agendas t))
  513. (unless (file-writable-p file)
  514. (error "Cannot write to file %s" file))
  515. (when sumo
  516. (org-store-agenda-views))))
  517. (defun org-mobile-move-capture ()
  518. "Move the contents of the capture file to the inbox file.
  519. Return a marker to the location where the new content has been added.
  520. If nothing new has been added, return nil."
  521. (interactive)
  522. (let ((inbox-buffer (find-file-noselect org-mobile-inbox-for-pull))
  523. (capture-buffer (find-file-noselect
  524. (expand-file-name org-mobile-capture-file
  525. org-mobile-directory)))
  526. (insertion-point (make-marker))
  527. not-empty content)
  528. (with-current-buffer capture-buffer
  529. (setq content (buffer-string))
  530. (setq not-empty (string-match "\\S-" content))
  531. (when not-empty
  532. (set-buffer inbox-buffer)
  533. (widen)
  534. (goto-char (point-max))
  535. (or (bolp) (newline))
  536. (move-marker insertion-point
  537. (prog1 (point) (insert content)))
  538. (save-buffer)
  539. (set-buffer capture-buffer)
  540. (erase-buffer)
  541. (save-buffer)
  542. (org-mobile-update-checksum-for-capture-file (buffer-string))))
  543. (kill-buffer capture-buffer)
  544. (if not-empty insertion-point)))
  545. (defun org-mobile-update-checksum-for-capture-file (buffer-string)
  546. (let* ((file (expand-file-name "checksums.dat" org-mobile-directory))
  547. (buffer (find-file-noselect file)))
  548. (when buffer
  549. (with-current-buffer buffer
  550. (when (re-search-forward (concat "\\([0-9a-fA-F]\\{30,\\}\\).*?"
  551. (regexp-quote org-mobile-capture-file)
  552. "[ \t]*$") nil t)
  553. (goto-char (match-beginning 1))
  554. (delete-region (match-beginning 1) (match-end 1))
  555. (insert (md5 buffer-string))
  556. (save-buffer)))
  557. (kill-buffer buffer))))
  558. (defun org-mobile-apply (&optional beg end)
  559. "Apply all change requests in the current buffer.
  560. If BEG and END are given, only do this in that region."
  561. (interactive)
  562. (require 'org-archive)
  563. (setq org-mobile-last-flagged-files nil)
  564. (setq beg (or beg (point-min)) end (or end (point-max)))
  565. ;; Remove all Note IDs
  566. (goto-char beg)
  567. (while (re-search-forward "^\\*\\* Note ID: [-0-9A-F]+[ \t]*\n" end t)
  568. (replace-match ""))
  569. ;; Find all the referenced entries, without making any changes yet
  570. (let ((marker (make-marker))
  571. (bos-marker (make-marker))
  572. (end (move-marker (make-marker) end))
  573. (cnt-new 0)
  574. (cnt-edit 0)
  575. (cnt-flag 0)
  576. (cnt-error 0)
  577. buf-list
  578. id-pos org-mobile-error)
  579. ;; Count the new captures
  580. (goto-char beg)
  581. (while (re-search-forward "^\\* \\(.*\\)" end t)
  582. (and (>= (- (match-end 1) (match-beginning 1)) 2)
  583. (not (equal (downcase (substring (match-string 1) 0 2)) "f("))
  584. (incf cnt-new)))
  585. (goto-char beg)
  586. (while (re-search-forward
  587. "^\\*+[ \t]+F(\\([^():\n]*\\)\\(:\\([^()\n]*\\)\\)?)[ \t]+\\[\\[\\(\\(id\\|olp\\):\\([^]\n]+\\)\\)" end t)
  588. (setq id-pos (condition-case msg
  589. (org-mobile-locate-entry (match-string 4))
  590. (error (nth 1 msg))))
  591. (when (and (markerp id-pos)
  592. (not (member (marker-buffer id-pos) buf-list)))
  593. (org-mobile-timestamp-buffer (marker-buffer id-pos))
  594. (push (marker-buffer id-pos) buf-list))
  595. (if (or (not id-pos) (stringp id-pos))
  596. (progn
  597. (goto-char (+ 2 (point-at-bol)))
  598. (insert id-pos " ")
  599. (incf cnt-error))
  600. (add-text-properties (point-at-bol) (point-at-eol)
  601. (list 'org-mobile-marker
  602. (or id-pos "Linked entry not found")))))
  603. ;; OK, now go back and start applying
  604. (goto-char beg)
  605. (while (re-search-forward "^\\*+[ \t]+F(\\([^():\n]*\\)\\(:\\([^()\n]*\\)\\)?)" end t)
  606. (catch 'next
  607. (setq id-pos (get-text-property (point-at-bol) 'org-mobile-marker))
  608. (if (not (markerp id-pos))
  609. (progn
  610. (incf cnt-error)
  611. (insert "UNKNOWN PROBLEM"))
  612. (let* ((action (match-string 1))
  613. (data (and (match-end 3) (match-string 3)))
  614. (bos (point-at-bol))
  615. (eos (save-excursion (org-end-of-subtree t t)))
  616. (cmd (if (equal action "")
  617. '(progn
  618. (incf cnt-flag)
  619. (org-toggle-tag "FLAGGED" 'on)
  620. (and note
  621. (org-entry-put nil "THEFLAGGINGNOTE" note)))
  622. (incf cnt-edit)
  623. (cdr (assoc action org-mobile-action-alist))))
  624. (note (and (equal action "")
  625. (buffer-substring (1+ (point-at-eol)) eos)))
  626. (org-inhibit-logging 'note) ;; Do not take notes interactively
  627. old new)
  628. (goto-char bos)
  629. (move-marker bos-marker (point))
  630. (if (re-search-forward "^** Old value[ \t]*$" eos t)
  631. (setq old (buffer-substring
  632. (1+ (match-end 0))
  633. (progn (outline-next-heading) (point)))))
  634. (if (re-search-forward "^** New value[ \t]*$" eos t)
  635. (setq new (buffer-substring
  636. (1+ (match-end 0))
  637. (progn (outline-next-heading)
  638. (if (eobp) (org-back-over-empty-lines))
  639. (point)))))
  640. (setq old (and old (if (string-match "\\S-" old) old nil)))
  641. (setq new (and new (if (string-match "\\S-" new) new nil)))
  642. (if (and note (> (length note) 0))
  643. ;; Make Note into a single line, to fit into a property
  644. (setq note (mapconcat 'identity
  645. (org-split-string (org-trim note) "\n")
  646. "\\n")))
  647. (unless (equal data "body")
  648. (setq new (and new (org-trim new))
  649. old (and old (org-trim old))))
  650. (goto-char (+ 2 bos-marker))
  651. (unless (markerp id-pos)
  652. (insert "BAD REFERENCE ")
  653. (incf cnt-error)
  654. (throw 'next t))
  655. (unless cmd
  656. (insert "BAD FLAG ")
  657. (incf cnt-error)
  658. (throw 'next t))
  659. ;; Remember this place so that we can return
  660. (move-marker marker (point))
  661. (setq org-mobile-error nil)
  662. (save-excursion
  663. (condition-case msg
  664. (org-with-point-at id-pos
  665. (progn
  666. (eval cmd)
  667. (if (member "FLAGGED" (org-get-tags))
  668. (add-to-list 'org-mobile-last-flagged-files
  669. (buffer-file-name (current-buffer))))))
  670. (error (setq org-mobile-error msg))))
  671. (when org-mobile-error
  672. (switch-to-buffer (marker-buffer marker))
  673. (goto-char marker)
  674. (incf cnt-error)
  675. (insert (if (stringp (nth 1 org-mobile-error))
  676. (nth 1 org-mobile-error)
  677. "EXECUTION FAILED")
  678. " ")
  679. (throw 'next t))
  680. ;; If we get here, the action has been applied successfully
  681. ;; So remove the entry
  682. (goto-char bos-marker)
  683. (delete-region (point) (org-end-of-subtree t t))))))
  684. (save-buffer)
  685. (move-marker marker nil)
  686. (move-marker end nil)
  687. (message "%d new, %d edits, %d flags, %d errors" cnt-new
  688. cnt-edit cnt-flag cnt-error)
  689. (sit-for 1)))
  690. (defun org-mobile-timestamp-buffer (buf)
  691. "Time stamp buffer BUF, just to make sure its checksum will change."
  692. (with-current-buffer buf
  693. (save-excursion
  694. (save-restriction
  695. (widen)
  696. (goto-char (point-min))
  697. (when (re-search-forward
  698. "^\\([ \t]*\\)#\\+LAST_MOBILE_CHANGE:.*\n?" nil t)
  699. (goto-char (match-end 1))
  700. (delete-region (point) (match-end 0)))
  701. (insert "#+LAST_MOBILE_CHANGE: "
  702. (format-time-string "%Y-%m-%d %T") "\n")))))
  703. (defun org-mobile-smart-read ()
  704. "Parse the entry at point for shortcuts and expand them.
  705. These shortcuts are meant for fast and easy typing on the limited
  706. keyboards of a mobile device. Below we show a list of the shortcuts
  707. currently implemented.
  708. The entry is expected to contain an inactive time stamp indicating when
  709. the entry was created. When setting dates and
  710. times (for example for deadlines), the time strings are interpreted
  711. relative to that creation date.
  712. Abbreviations are expected to take up entire lines, just because it is so
  713. easy to type RET on a mobile device. Abbreviations start with one or two
  714. letters, followed immediately by a dot and then additional information.
  715. Generally the entire shortcut line is removed after action have been taken.
  716. Time stamps will be constructed using `org-read-date'. So for example a
  717. line \"dd. 2tue\" will set a deadline on the second Tuesday after the
  718. creation date.
  719. Here are the shortcuts currently implemented:
  720. dd. string set deadline
  721. ss. string set scheduling
  722. tt. string set time tamp, here.
  723. ti. string set inactive time
  724. tg. tag1 tag2 tag3 set all these tags, change case where necessary
  725. td. kwd set this todo keyword, change case where necessary
  726. FIXME: Hmmm, not sure if we can make his work against the
  727. auto-correction feature. Needs a bit more thinking. So this function
  728. is currently a noop.")
  729. (defun org-find-olp (path)
  730. "Return a marker pointing to the entry at outline path OLP.
  731. If anything goes wrong, the return value will instead an error message,
  732. as a string."
  733. (let* ((file (pop path))
  734. (buffer (find-file-noselect file))
  735. (level 1)
  736. (lmin 1)
  737. (lmax 1)
  738. limit re end found pos heading cnt)
  739. (unless buffer (error "File not found :%s" file))
  740. (with-current-buffer buffer
  741. (save-excursion
  742. (save-restriction
  743. (widen)
  744. (setq limit (point-max))
  745. (goto-char (point-min))
  746. (while (setq heading (pop path))
  747. (setq re (format org-complex-heading-regexp-format
  748. (regexp-quote heading)))
  749. (setq cnt 0 pos (point))
  750. (while (re-search-forward re end t)
  751. (setq level (- (match-end 1) (match-beginning 1)))
  752. (if (and (>= level lmin) (<= level lmax))
  753. (setq found (match-beginning 0) cnt (1+ cnt))))
  754. (when (= cnt 0) (error "Heading not found on level %d: %s"
  755. lmax heading))
  756. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  757. lmax heading))
  758. (goto-char found)
  759. (setq lmin (1+ level) lmax (+ lmin (if org-odd-levels-only 1 0)))
  760. (setq end (save-excursion (org-end-of-subtree t t))))
  761. (when (org-on-heading-p)
  762. (move-marker (make-marker) (point))))))))
  763. (defun org-mobile-locate-entry (link)
  764. (if (string-match "\\`id:\\(.*\\)$" link)
  765. (org-id-find (match-string 1 link) 'marker)
  766. (if (not (string-match "\\`olp:\\(.*?\\):\\(.*\\)$" link))
  767. nil
  768. (let ((file (match-string 1 link))
  769. (path (match-string 2 link))
  770. (table '((?: . "%3a") (?\[ . "%5b") (?\] . "%5d") (?/ . "%2f"))))
  771. (setq file (org-link-unescape file table))
  772. (setq file (expand-file-name file org-directory))
  773. (setq path (mapcar (lambda (x) (org-link-unescape x table))
  774. (org-split-string path "/")))
  775. (org-find-olp (cons file path))))))
  776. (defun org-mobile-edit (what old new)
  777. "Edit item WHAT in the current entry by replacing OLD with NEW.
  778. WHAT can be \"heading\", \"todo\", \"tags\", \"priority\", or \"body\".
  779. The edit only takes place if the current value is equal (except for
  780. white space) the OLD. If this is so, OLD will be replace by NEW
  781. and the command will return t. If something goes wrong, a string will
  782. be returned that indicates what went wrong."
  783. (let (current old1 new1)
  784. (if (stringp what) (setq what (intern what)))
  785. (cond
  786. ((memq what '(todo todostate))
  787. (setq current (org-get-todo-state))
  788. (cond
  789. ((equal new "DONEARCHIVE")
  790. (org-todo 'done)
  791. (org-archive-subtree-default))
  792. ((equal new current) t) ; nothing needs to be done
  793. ((or (equal current old)
  794. (eq org-mobile-force-mobile-change t)
  795. (memq 'todo org-mobile-force-mobile-change))
  796. (org-todo (or new 'none)) t)
  797. (t (error "State before change was expected as \"%s\", but is \"%s\""
  798. old current))))
  799. ((eq what 'tags)
  800. (setq current (org-get-tags)
  801. new1 (and new (org-split-string new ":+"))
  802. old1 (and old (org-split-string old ":+")))
  803. (cond
  804. ((org-mobile-tags-same-p current new1) t) ; no change needed
  805. ((or (org-mobile-tags-same-p current old1)
  806. (eq org-mobile-force-mobile-change t)
  807. (memq 'tags org-mobile-force-mobile-change))
  808. (org-set-tags-to new1) t)
  809. (t (error "Tags before change were expected as \"%s\", but are \"%s\""
  810. (or old "") (or current "")))))
  811. ((eq what 'priority)
  812. (when (looking-at org-complex-heading-regexp)
  813. (setq current (and (match-end 3) (substring (match-string 3) 2 3)))
  814. (cond
  815. ((equal current new) t) ; no action required
  816. ((or (equal current old)
  817. (eq org-mobile-force-mobile-change t)
  818. (memq 'tags org-mobile-force-mobile-change))
  819. (org-priority (and new (string-to-char new))))
  820. (t (error "Priority was expected to be %s, but is %s"
  821. old current)))))
  822. ((eq what 'heading)
  823. (when (looking-at org-complex-heading-regexp)
  824. (setq current (match-string 4))
  825. (cond
  826. ((equal current new) t) ; no action required
  827. ((or (equal current old)
  828. (eq org-mobile-force-mobile-change t)
  829. (memq 'heading org-mobile-force-mobile-change))
  830. (goto-char (match-beginning 4))
  831. (insert new)
  832. (delete-region (point) (+ (point) (length current)))
  833. (org-set-tags nil 'align))
  834. (t (error "Heading changed in MobileOrg and on the computer")))))
  835. ((eq what 'body)
  836. (setq current (buffer-substring (min (1+ (point-at-eol)) (point-max))
  837. (save-excursion (outline-next-heading)
  838. (point))))
  839. (if (not (string-match "\\S-" current)) (setq current nil))
  840. (cond
  841. ((org-mobile-bodies-same-p current new) t) ; no action necessary
  842. ((or (org-mobile-bodies-same-p current old)
  843. (eq org-mobile-force-mobile-change t)
  844. (memq 'body org-mobile-force-mobile-change))
  845. (save-excursion
  846. (end-of-line 1)
  847. (insert "\n" new)
  848. (or (bolp) (insert "\n"))
  849. (delete-region (point) (progn (org-back-to-heading t)
  850. (outline-next-heading)
  851. (point))))
  852. t)
  853. (t (error "Body was changed in MobileOrg and on the computer")))))))
  854. (defun org-mobile-tags-same-p (list1 list2)
  855. "Are the two tag lists the same?"
  856. (not (or (org-delete-all list1 list2)
  857. (org-delete-all list2 list1))))
  858. (defun org-mobile-bodies-same-p (a b)
  859. "Compare if A and B are visually equal strings.
  860. We first remove leading and trailing white space from the entire strings.
  861. Then we split the strings into lines and remove leading/trailing whitespace
  862. from each line. Then we compare.
  863. A and B must be strings or nil."
  864. (cond
  865. ((and (not a) (not b)) t)
  866. ((or (not a) (not b)) nil)
  867. (t (setq a (org-trim a) b (org-trim b))
  868. (setq a (mapconcat 'identity (org-split-string a "[ \t]*\n[ \t]*") "\n"))
  869. (setq b (mapconcat 'identity (org-split-string b "[ \t]*\n[ \t]*") "\n"))
  870. (equal a b))))
  871. (provide 'org-mobile)
  872. ;; arch-tag: ace0e26c-58f2-4309-8a61-05ec1535f658
  873. ;;; org-mobile.el ends here