org2rem.el 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. ;;; org2rem.el --- Convert org appointments into reminders
  2. ;; Copyright (C) 2006-2012 Free Software Foundation, Inc.
  3. ;; Author: Bastien Guerry and Shatad Pratap
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 6.09a
  7. ;;
  8. ;; This file is not part of GNU Emacs.
  9. ;;
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  21. ;;
  22. ;;; Commentary:
  23. ;; (require 'org2rem)
  24. ;; To export, do
  25. ;;
  26. ;; M-x org2rem-combine-agenda-files
  27. ;;
  28. ;; Then you can use reming like this:
  29. ;;
  30. ;; $ remind ~/org.rem
  31. ;;
  32. ;; If you want to use this regualrly, try in .emacs
  33. ;;
  34. ;; (add-hook 'org-mode-hook
  35. ;; (lambda() (add-hook 'after-save-hook
  36. ;; 'org-export-remind-all-agenda-files t t)))
  37. (require 'org)
  38. (require 'org-agenda)
  39. (require 'org-exp)
  40. (eval-and-compile
  41. (require 'cl))
  42. (defgroup org2rem nil
  43. "Options specific for Remind export of Org-mode files."
  44. :tag "Org Export Remind"
  45. :group 'org-export)
  46. (defcustom org-combined-agenda-remind-file "~/org.rem"
  47. "The file name for the Remind file covering all agenda files.
  48. This file is created with the command \\[org2rem-all-agenda-files].
  49. The file name should be absolute, the file will be overwritten without warning."
  50. :group 'org2rem
  51. :type 'file)
  52. (defcustom org-remind-combined-name "OrgMode"
  53. "Calendar name for the combined Remind representing all agenda files."
  54. :group 'org2rem
  55. :type 'string)
  56. (defcustom org-remind-use-deadline '(event-if-not-todo todo-due)
  57. "Contexts where Remind export should use a deadline time stamp.
  58. This is a list with several symbols in it. Valid symbol are:
  59. event-if-todo Deadlines in TODO entries become calendar events.
  60. event-if-not-todo Deadlines in non-TODO entries become calendar events.
  61. todo-due Use deadlines in TODO entries as due-dates"
  62. :group 'org2rem
  63. :type '(set :greedy t
  64. (const :tag "Deadlines in non-TODO entries become events"
  65. event-if-not-todo)
  66. (const :tag "Deadline in TODO entries become events"
  67. event-if-todo)
  68. (const :tag "Deadlines in TODO entries become due-dates"
  69. todo-due)))
  70. (defcustom org-remind-use-scheduled '(todo-start)
  71. "Contexts where Remind export should use a scheduling time stamp.
  72. This is a list with several symbols in it. Valid symbol are:
  73. event-if-todo Scheduling time stamps in TODO entries become an event.
  74. event-if-not-todo Scheduling time stamps in non-TODO entries become an event.
  75. todo-start Scheduling time stamps in TODO entries become start date.
  76. Some calendar applications show TODO entries only after
  77. that date."
  78. :group 'org2rem
  79. :type '(set :greedy t
  80. (const :tag
  81. "SCHEDULED timestamps in non-TODO entries become events"
  82. event-if-not-todo)
  83. (const :tag "SCHEDULED timestamps in TODO entries become events"
  84. event-if-todo)
  85. (const :tag "SCHEDULED in TODO entries become start date"
  86. todo-start)))
  87. (defcustom org-remind-categories '(local-tags category)
  88. "Items that should be entered into the categories field.
  89. This is a list of symbols, the following are valid:
  90. category The Org-mode category of the current file or tree
  91. todo-state The todo state, if any
  92. local-tags The tags, defined in the current line
  93. all-tags All tags, including inherited ones."
  94. :group 'org2rem
  95. :type '(repeat
  96. (choice
  97. (const :tag "The file or tree category" category)
  98. (const :tag "The TODO state" todo-state)
  99. (const :tag "Tags defined in current line" local-tags)
  100. (const :tag "All tags, including inherited ones" all-tags))))
  101. (defcustom org-remind-include-todo nil
  102. "Non-nil means export to remind files should also cover TODO items."
  103. :group 'org2rem
  104. :type '(choice
  105. (const :tag "None" nil)
  106. (const :tag "Unfinished" t)
  107. (const :tag "All" all)))
  108. (defcustom org-remind-include-sexps t
  109. "Non-nil means export to Remind files should also cover sexp entries.
  110. These are entries like in the diary, but directly in an Org-mode file."
  111. :group 'org2rem
  112. :type 'boolean)
  113. (defcustom org-remind-deadline-over-scheduled t
  114. "Non-nil means use deadline as target when both deadline and
  115. scheduled present, vice-versa. Default is Non-nil."
  116. :group 'org2rem
  117. :type 'boolean)
  118. (defcustom org-remind-escape-percentage t
  119. "Non-nil means % will be escaped, vice-versa. Default is Non-nil."
  120. :group 'org2rem
  121. :type 'boolean)
  122. (defcustom org-remind-extra-warn-days 3
  123. "Extra days Remind keep reminding."
  124. :group 'org2rem
  125. :type 'number)
  126. (defcustom org-remind-advanced-warn-days 3
  127. "Advanced days Remind start reminding."
  128. :group 'org2rem
  129. :type 'number)
  130. (defcustom org-remind-suppress-last-newline nil
  131. "Non-nil means suppress last newline REM body. Default is nil."
  132. :group 'org2rem
  133. :type 'boolean)
  134. (defcustom org-remind-include-body 100
  135. "Amount of text below headline to be included in Remind export.
  136. This is a number of characters that should maximally be included.
  137. Properties, scheduling and clocking lines will always be removed.
  138. The text will be inserted into the DESCRIPTION field."
  139. :group 'org2rem
  140. :type '(choice
  141. (const :tag "Nothing" nil)
  142. (const :tag "Everything" t)
  143. (integer :tag "Max characters")))
  144. (defcustom org-remind-store-UID nil
  145. "Non-nil means store any created UIDs in properties.
  146. The Remind standard requires that all entries have a unique identifyer.
  147. Org will create these identifiers as needed. When this variable is non-nil,
  148. the created UIDs will be stored in the ID property of the entry. Then the
  149. next time this entry is exported, it will be exported with the same UID,
  150. superceeding the previous form of it. This is essential for
  151. synchronization services.
  152. This variable is not turned on by default because we want to avoid creating
  153. a property drawer in every entry if people are only playing with this feature,
  154. or if they are only using it locally."
  155. :group 'org2rem
  156. :type 'boolean)
  157. ;;;; Exporting
  158. ;;; Remind export
  159. ;;;###autoload
  160. (defun org2rem-this-file ()
  161. "Export current file as an Remind file.
  162. The Remind file will be located in the same directory as the Org-mode
  163. file, but with extension `.rem'."
  164. (interactive)
  165. (org2rem nil buffer-file-name))
  166. ;;;###autoload
  167. (defun org2rem-all-agenda-files ()
  168. "Export all files in `org-agenda-files' to Remind .rem files.
  169. Each Remind file will be located in the same directory as the Org-mode
  170. file, but with extension `.rem'."
  171. (interactive)
  172. (apply 'org2rem nil (org-agenda-files t)))
  173. ;;;###autoload
  174. (defun org2rem-combine-agenda-files ()
  175. "Export all files in `org-agenda-files' to a single combined Remind file.
  176. The file is stored under the name `org-combined-agenda-remind-file'."
  177. (interactive)
  178. (apply 'org2rem t (org-agenda-files t)))
  179. (defun org2rem (combine &rest files)
  180. "Create Remind files for all elements of FILES.
  181. If COMBINE is non-nil, combine all calendar entries into a single large
  182. file and store it under the name `org-combined-agenda-remind-file'."
  183. (save-excursion
  184. (org-prepare-agenda-buffers files)
  185. (let* ((dir (org-export-directory
  186. :ical (list :publishing-directory
  187. org-export-publishing-directory)))
  188. file rem-file rem-buffer category started org-agenda-new-buffers)
  189. (and (get-buffer "*rem-tmp*") (kill-buffer "*rem-tmp*"))
  190. (when combine
  191. (setq rem-file
  192. (if (file-name-absolute-p org-combined-agenda-remind-file)
  193. org-combined-agenda-remind-file
  194. (expand-file-name org-combined-agenda-remind-file dir))
  195. rem-buffer (org-get-agenda-file-buffer rem-file))
  196. (set-buffer rem-buffer) (erase-buffer))
  197. (while (setq file (pop files))
  198. (catch 'nextfile
  199. (org-check-agenda-file file)
  200. (set-buffer (org-get-agenda-file-buffer file))
  201. (unless combine
  202. (setq rem-file (concat (file-name-as-directory dir)
  203. (file-name-sans-extension
  204. (file-name-nondirectory buffer-file-name))
  205. ".rem"))
  206. (setq rem-buffer (org-get-agenda-file-buffer rem-file))
  207. (with-current-buffer rem-buffer (erase-buffer)))
  208. (setq category (or org-category
  209. (file-name-sans-extension
  210. (file-name-nondirectory buffer-file-name))))
  211. (if (symbolp category) (setq category (symbol-name category)))
  212. (let ((standard-output rem-buffer))
  213. (if combine
  214. (and (not started) (setq started t)
  215. (org-start-remind-file org-remind-combined-name))
  216. (org-start-remind-file category))
  217. (org-print-remind-entries combine)
  218. (when (or (and combine (not files)) (not combine))
  219. (org-finish-remind-file)
  220. (set-buffer rem-buffer)
  221. (run-hooks 'org-before-save-Remind-file-hook)
  222. (save-buffer)
  223. (run-hooks 'org-after-save-Remind-file-hook)
  224. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  225. ))))
  226. (org-release-buffers org-agenda-new-buffers))))
  227. (defvar org-before-save-Remind-file-hook nil
  228. "Hook run before an Remind file has been saved.
  229. This can be used to modify the result of the export.")
  230. (defvar org-after-save-Remind-file-hook nil
  231. "Hook run after an Remind file has been saved.
  232. The Remind buffer is still current when this hook is run.
  233. A good way to use this is to tell a desktop calenndar application to re-read
  234. the Remind file.")
  235. (defvar org-agenda-default-appointment-duration) ; defined in org-agenda.el
  236. (defun org-print-remind-entries (&optional combine)
  237. "Print Remind entries for the current Org-mode file to `standard-output'.
  238. When COMBINE is non nil, add the category to each line."
  239. (require 'org-agenda)
  240. (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
  241. (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  242. (dts (org-rem-ts-to-string
  243. (format-time-string (cdr org-time-stamp-formats) (current-time))
  244. "start time:"))
  245. hd ts ts2 state status (inc t) pos b sexp rrule
  246. scheduledp deadlinep todo prefix due start
  247. tmp pri categories entry location summary desc uid
  248. remind-aw remind-ew (org-rem-ew org-remind-extra-warn-days)
  249. (org-rem-aw org-remind-advanced-warn-days)
  250. trigger diff-days (dos org-remind-deadline-over-scheduled)
  251. (suppress-last-newline org-remind-suppress-last-newline)
  252. (sexp-buffer (get-buffer-create "*rem-tmp*")))
  253. (org-refresh-category-properties)
  254. (save-excursion
  255. (goto-char (point-min))
  256. (while (re-search-forward re1 nil t)
  257. (catch :skip
  258. (org-agenda-skip)
  259. (when (boundp 'org-remind-verify-function)
  260. (unless (funcall org-remind-verify-function)
  261. (outline-next-heading)
  262. (backward-char 1)
  263. (throw :skip nil)))
  264. (setq pos (match-beginning 0)
  265. ts (match-string 0)
  266. inc t
  267. hd (condition-case nil
  268. (org-remind-cleanup-string
  269. (org-get-heading))
  270. (error (throw :skip nil)))
  271. summary (org-remind-cleanup-string
  272. (org-entry-get nil "SUMMARY"))
  273. desc (org-remind-cleanup-string
  274. (or (org-entry-get nil "DESCRIPTION")
  275. (and org-remind-include-body (org-get-entry)))
  276. t org-remind-include-body)
  277. location (org-remind-cleanup-string
  278. (org-entry-get nil "LOCATION"))
  279. uid (if org-remind-store-UID
  280. (org-id-get-create)
  281. (or (org-id-get) (org-id-new)))
  282. categories (org-export-get-remind-categories)
  283. deadlinep nil scheduledp nil)
  284. (if (looking-at re2)
  285. (progn
  286. (goto-char (match-end 0))
  287. (setq ts2 (match-string 1)
  288. inc (not (string-match "[0-9]\\{1,2\\}:[0-9][0-9]" ts2))))
  289. (setq tmp (buffer-substring (max (point-min)
  290. (- pos org-ds-keyword-length))
  291. pos)
  292. ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
  293. (progn
  294. (setq inc nil)
  295. (replace-match "\\1" t nil ts))
  296. ts)
  297. deadlinep (string-match org-deadline-regexp tmp)
  298. scheduledp (string-match org-scheduled-regexp tmp)
  299. todo (org-get-todo-state)
  300. ;; donep (org-entry-is-done-p)
  301. ))
  302. (when (and
  303. deadlinep
  304. (if todo
  305. (not (memq 'event-if-todo org-remind-use-deadline))
  306. (not (memq 'event-if-not-todo org-remind-use-deadline))))
  307. (throw :skip t))
  308. (when (and
  309. scheduledp
  310. (if todo
  311. (not (memq 'event-if-todo org-remind-use-scheduled))
  312. (not (memq 'event-if-not-todo org-remind-use-scheduled))))
  313. (throw :skip t))
  314. (setq prefix (if deadlinep "DEADLINE-" (if scheduledp "SCHEDULED-" "TS-")))
  315. (if (or (string-match org-tr-regexp hd)
  316. (string-match org-ts-regexp hd))
  317. (setq hd (replace-match "" t t hd)))
  318. (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts)
  319. (setq rrule ;is recurrence value. later give it good name.
  320. (* (string-to-number
  321. (cdr (assoc
  322. (match-string 2 ts)
  323. '(("d" . "1")("w" . "7")
  324. ("m" . "0")("y" . "0")))))
  325. (string-to-number (match-string 1 ts))))
  326. (setq rrule nil))
  327. (setq summary (or summary hd))
  328. (if (string-match org-bracket-link-regexp summary)
  329. (setq summary
  330. (replace-match (if (match-end 3)
  331. (match-string 3 summary)
  332. (match-string 1 summary))
  333. t t summary)))
  334. (if deadlinep (setq summary (concat "DEADLINE: " summary)))
  335. (if scheduledp (setq summary (concat "SCHEDULED: " summary)))
  336. (if (string-match "\\`<%%" ts)
  337. (with-current-buffer sexp-buffer
  338. (insert (substring ts 1 -1) " " summary "\n"))
  339. (princ (format "\n## BEGIN:EVENT
  340. ## UID: %s
  341. REM %s %s MSG EVENT:%s%s %s%s%%
  342. ## CATEGORIES:%s
  343. ## END:EVENT\n"
  344. (concat prefix uid)
  345. (org-rem-ts-to-string ts nil nil rrule)
  346. (org-rem-ts-to-string ts2 "UNTIL " inc)
  347. summary
  348. (if (and desc (string-match "\\S-" desc))
  349. (concat "%_\\\n" desc) "")
  350. (if (and location (string-match "\\S-" location))
  351. (concat "\nLOCATION: " location) "")
  352. (if suppress-last-newline "" "%_")
  353. categories)))))
  354. (when (and org-remind-include-sexps
  355. (condition-case nil (require 'remind) (error nil))
  356. (fboundp 'remind-export-region))
  357. ;; Get all the literal sexps
  358. (goto-char (point-min))
  359. (while (re-search-forward "^&?%%(" nil t)
  360. (catch :skip
  361. (org-agenda-skip)
  362. (setq b (match-beginning 0))
  363. (goto-char (1- (match-end 0)))
  364. (forward-sexp 1)
  365. (end-of-line 1)
  366. (setq sexp (buffer-substring b (point)))
  367. (with-current-buffer sexp-buffer
  368. (insert sexp "\n"))))
  369. ;; (princ (org-diary-to-rem-string sexp-buffer))
  370. (kill-buffer sexp-buffer))
  371. (when org-remind-include-todo
  372. (setq prefix "TODO-")
  373. (goto-char (point-min))
  374. (while (re-search-forward org-todo-line-regexp nil t)
  375. (catch :skip
  376. (org-agenda-skip)
  377. (when (boundp 'org-remind-verify-function)
  378. (unless (funcall org-remind-verify-function)
  379. (outline-next-heading)
  380. (backward-char 1)
  381. (throw :skip nil)))
  382. (setq state (match-string 2))
  383. (setq status (if (member state org-done-keywords)
  384. "COMPLETED" "NEEDS-ACTION"))
  385. (when (and state
  386. (or (not (member state org-done-keywords))
  387. (eq org-remind-include-todo 'all))
  388. (not (member org-archive-tag (org-get-tags-at)))
  389. )
  390. (setq hd (match-string 3)
  391. summary (org-remind-cleanup-string
  392. (org-entry-get nil "SUMMARY"))
  393. desc (org-remind-cleanup-string
  394. (or (org-entry-get nil "DESCRIPTION")
  395. (and org-remind-include-body (org-get-entry)))
  396. t org-remind-include-body)
  397. location (org-remind-cleanup-string
  398. (org-entry-get nil "LOCATION"))
  399. due (and (member 'todo-due org-remind-use-deadline)
  400. (org-entry-get nil "DEADLINE"))
  401. start (and (member 'todo-start org-remind-use-scheduled)
  402. (org-entry-get nil "SCHEDULED"))
  403. categories (org-export-get-remind-categories)
  404. uid (if org-remind-store-UID
  405. (org-id-get-create)
  406. (or (org-id-get) (org-id-new))))
  407. (if (and due start)
  408. (setq diff-days (org-rem-time-diff-days due start)))
  409. (setq remind-aw
  410. (if due
  411. (if diff-days
  412. (if (> diff-days 0)
  413. (if dos diff-days 0)
  414. (if dos 0 diff-days))
  415. 1000)))
  416. (if (and (numberp org-rem-aw) (> org-rem-aw 0))
  417. (setq remind-aw (+ (or remind-aw 0) org-rem-aw)))
  418. (setq remind-ew
  419. (if due
  420. (if diff-days
  421. (if (> diff-days 0) due nil)
  422. due)))
  423. (setq trigger (if dos (if due due start) (if start start due)))
  424. ;; (and trigger (setq trigger (org-rem-ts-to-string trigger nil nil 1 remind-aw)))
  425. (if trigger
  426. (setq trigger (concat
  427. (format "[trigger('%s')] *%d "
  428. (org-rem-ts-to-remind-date-type trigger) 1)
  429. (if remind-aw (format "++%d" remind-aw)))))
  430. (and due (setq due (org-rem-ts-to-remind-date-type due)))
  431. (and start (setq start (org-rem-ts-to-remind-date-type start)))
  432. (and remind-ew (setq remind-ew (org-rem-ts-to-remind-date-type remind-ew)))
  433. (if (string-match org-bracket-link-regexp hd)
  434. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  435. (match-string 1 hd))
  436. t t hd)))
  437. (if (string-match org-priority-regexp hd)
  438. (setq pri (string-to-char (match-string 2 hd))
  439. hd (concat (substring hd 0 (match-beginning 1))
  440. (substring hd (match-end 1))))
  441. (setq pri org-default-priority))
  442. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  443. (- org-lowest-priority org-highest-priority))))))
  444. (princ (format "\n## BEGIN:TODO
  445. ## UID: %s
  446. REM %s %s %s MSG TODO: %s%s%s%s%s%s%%
  447. ## CATEGORIES:%s
  448. ## SEQUENCE:1
  449. ## STATUS:%s
  450. ## END:TODO\n"
  451. (concat prefix uid)
  452. (or trigger "") ;; dts)
  453. (if remind-ew (format "UNTIL [trigger('%s' + %d)]" remind-ew (or org-rem-ew 0)) "")
  454. (if pri (format "PRIORITY %d" pri) "")
  455. (or summary hd)
  456. (if (and desc (string-match "\\S-" desc))
  457. (concat "%_\\\nDESCRIPTION: " desc) "")
  458. (if (and location (string-match "\\S-" location))
  459. (concat "LOCATION: " location) "")
  460. (if start
  461. (concat
  462. "%_\\\n['" start "' - today()] "
  463. "days over, for scheduled date - "
  464. "[trigger('" start "')]") "")
  465. (if due
  466. (concat
  467. "%_\\\n[today() - '" due "'] "
  468. "days left, to deadline date - "
  469. "[trigger('" due "')]") "")
  470. (if suppress-last-newline "" "%_")
  471. categories
  472. status)))))))))
  473. (defun org-export-get-remind-categories ()
  474. "Get categories according to `org-remind-categories'."
  475. (let ((cs org-remind-categories) c rtn tmp)
  476. (while (setq c (pop cs))
  477. (cond
  478. ((eq c 'category) (push (org-get-category) rtn))
  479. ((eq c 'todo-state)
  480. (setq tmp (org-get-todo-state))
  481. (and tmp (push tmp rtn)))
  482. ((eq c 'local-tags)
  483. (setq rtn (append (nreverse (org-get-local-tags-at (point))) rtn)))
  484. ((eq c 'all-tags)
  485. (setq rtn (append (nreverse (org-get-tags-at (point))) rtn)))))
  486. (mapconcat 'identity (nreverse rtn) ",")))
  487. (defun org-remind-cleanup-string (s &optional is-body maxlength)
  488. "Take out stuff and quote what needs to be quoted.
  489. When IS-BODY is non-nil, assume that this is the body of an item, clean up
  490. whitespace, newlines, drawers, and timestamps, and cut it down to MAXLENGTH
  491. characters."
  492. (if (or (not s) (string-match "^[ \t\n]*$" s))
  493. nil
  494. (when is-body
  495. (let ((re (concat "\\(" org-drawer-regexp "\\)[^\000]*?:END:.*\n?"))
  496. (re2 (concat "^[ \t]*" org-keyword-time-regexp ".*\n?")))
  497. (while (string-match re s) (setq s (replace-match "" t t s)))
  498. (while (string-match re2 s) (setq s (replace-match "" t t s)))))
  499. (if org-remind-escape-percentage
  500. (let ((start 0))
  501. (while (string-match "\\([%]\\)" s start)
  502. (setq start (+ (match-beginning 0) 2)
  503. s (replace-match "\\1\\1" nil nil s)))))
  504. (let ((start 0))
  505. (while (string-match "\\([\n]\\)" s start)
  506. (setq start (+ (match-beginning 0) 4) ;; less than 4 is not correct.
  507. s (replace-match "%_\\\\\\1" nil nil s))))
  508. (let ((start 0))
  509. (while (string-match "\\([[]\\)" s start)
  510. (setq start (+ (match-beginning 0) 5)
  511. s (replace-match (concat "\[" "\"" "\\1" "\"" "\]") nil nil s))))
  512. ;;; (when is-body
  513. ;;; (while (string-match "[ \t]*\n[ \t]*" s)
  514. ;;; (setq s (replace-match "%_" t t s))))
  515. (setq s (org-trim s))
  516. (if is-body
  517. (if maxlength
  518. (if (and (numberp maxlength)
  519. (> (length s) maxlength))
  520. (setq s (substring s 0 maxlength)))))
  521. s))
  522. (defun org-get-entry ()
  523. "Clean-up description string."
  524. (save-excursion
  525. (org-back-to-heading t)
  526. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  527. (defun org-start-remind-file (name)
  528. "Start an Remind file by inserting the header."
  529. (let ((user user-full-name)
  530. (name (or name "unknown"))
  531. (timezone (cadr (current-time-zone))))
  532. (princ
  533. (format "# -*- Mode: shell-script; auto-fill-mode: nil -*-
  534. ## BEGIN: Reminders
  535. ## VERSION:2.0
  536. ## Emacs with Org-mode
  537. ## Calendar:%s
  538. ## Created by: %s
  539. ## Timezone:%s
  540. ## Calscale:Gregorian\n" name user timezone))))
  541. (defun org-finish-remind-file ()
  542. "Finish an Remind file by inserting the END statement."
  543. (princ "\n## END:Reminders\n"))
  544. (defun org-rem-ts-to-remind-date-type (s)
  545. (format-time-string
  546. "%Y-%m-%d"
  547. (apply 'encode-time (butlast (org-parse-time-string s) 3))))
  548. ;; (defun org-rem-date-type-to-string (s keyword &optional inc day-repeat day-advance-warn)
  549. ;; (if trigger
  550. ;; (setq trigger
  551. ;; (concat
  552. ;; (format "[trigger('%s')] *%d "
  553. ;; (org-rem-ts-to-remind-date-type trigger) day-repeat)
  554. ;; (if day-advance-warn (format "++%d" day-advance-warn))))))
  555. ;; (format-time-string "%Y"
  556. ;; (apply 'encode-time (butlast (org-parse-time-string "<2008-11-20 Thu 10:30>") 3)))
  557. (defun org-rem-ts-to-string (s keyword &optional inc day-repeat day-advance-warn)
  558. "Take a time string S and convert it to Remind format.
  559. KEYWORD is added in front, to make a complete line like DTSTART....
  560. When INC is non-nil, increase the hour by two (if time string contains
  561. a time), or the day by one (if it does not contain a time)."
  562. (let ((t1 (org-parse-time-string s 'nodefault))
  563. t2 fmt have-time time)
  564. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  565. (setq t2 t1 have-time t)
  566. (setq t2 (org-parse-time-string s)))
  567. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  568. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  569. (when inc
  570. (if have-time
  571. (if org-agenda-default-appointment-duration
  572. (setq mi (+ org-agenda-default-appointment-duration mi))
  573. (setq h (+ 2 h)))
  574. (setq d (1+ d))))
  575. (setq time (encode-time s mi h d m y)))
  576. (setq fmt (concat
  577. "%d %b %Y"
  578. (if day-advance-warn (format " ++%d" day-advance-warn))
  579. (if day-repeat (format " *%d" day-repeat))
  580. (if have-time " AT %H:%M")))
  581. (concat keyword (format-time-string fmt time))))
  582. (defun org-rem-time-diff-days (end start)
  583. (floor (/ (apply '- (mapcar
  584. (lambda (s)
  585. (let*
  586. ((t1 (org-parse-time-string s))
  587. (s (car t1)) (mi (nth 1 t1))
  588. (h (nth 2 t1)) (d (nth 3 t1))
  589. (m (nth 4 t1)) (y (nth 5 t1)))
  590. (float-time (encode-time s mi h d m y))))
  591. (list end start))) (* 24 60 60))))
  592. (provide 'org2rem)
  593. ;;; org-exp.el ends here