org-mobile.el 39 KB

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