planner-publish: Change :after to :before-end
[planner-el.git] / planner-export-diary.el
blob36ccfaf894e03079d3977cfdfe40d79d9bd92640
1 ;;; planner-export-diary.el --- Diary export for the Emacs Planner
3 ;; Copyright (C) 2001, 2004 Free Software Foundation, Inc.
4 ;; Parts copyright (C) 2004 Xin Wei Hu (huxw AT knight DOT 6test DOT edu DOT cn)
6 ;; Emacs Lisp Archive Entry
7 ;; Filename: planner-export-diary.el
8 ;; Keywords: hypermedia
9 ;; Author: John Wiegley <johnw@gnu.org>
10 ;; Description: Use Emacs for life planning
11 ;; URL: http://www.plannerlove.com/
12 ;; Compatibility: Emacs20, Emacs21, XEmacs21
14 ;; This file is part of Planner. It is not part of GNU Emacs.
16 ;; Planner is free software; you can redistribute it and/or modify it
17 ;; under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation; either version 2, or (at your option)
19 ;; any later version.
21 ;; Planner is distributed in the hope that it will be useful, but
22 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 ;; General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with Planner; see the file COPYING. If not, write to the
28 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29 ;; Boston, MA 02110-1301, USA.
31 ;;; Commentary:
33 ;; Use this package if you want to export diary entries from your
34 ;; planner to diary files. Customize `planner-export-diary-file'
35 ;; (default: ~/diary.planner). Add the following line to your
36 ;; main `diary-file' (default: ~/diary):
38 ;; #include ~/diary.planner
40 ;; and then call M-x planner-export-diary-future whenever you want
41 ;; future diary entries published. You can automatically publish
42 ;; entries by adding either of the following to your .emacs:
44 ;; a) Publish future entries on startup
46 ;; (planner-export-diary-future)
48 ;; b) Publish future entries whenever you save a planner file
50 ;; (add-hook 'planner-mode-hook 'planner-export-diary-setup)
52 ;;;_ + Contributors
54 ;; Xin Wei Hu (huxw AT knight DOT 6test DOT edu DOT cn) did some work
55 ;; on `planner-export-diary-future' and
56 ;; `planner-export-diary-get-schedule-entries'.
58 ;; Yann Hodique helped port this to Muse.
60 ;;; Code:
62 (require 'planner)
64 (defvar planner-export-diary-file "~/diary.planner"
65 "*Name of the file into which schedules are exported from planner.
66 This file will be automatically overwritten every time planner
67 entries are exported, so be careful.")
69 (defvar planner-export-diary-number-of-days 3
70 "*Number of days to export diary appointments for.")
72 ;;;###autoload
73 (defun planner-export-diary-future ()
74 "Exports only `planner-export-number-of-days' days of entries.
75 This function can be put into your `after-save-hook'."
76 (interactive)
77 (planner-export-diary (planner-today)
78 (planner-calculate-date-from-day-offset
79 (planner-today)
80 (1- planner-export-diary-number-of-days))))
82 ;;;###autoload
83 (defun planner-export-diary-setup ()
84 "Add `planner-export-diary-future' to `after-save-hook' in planner buffers.
85 You can add this function to `planner-mode-hook'."
86 (add-hook 'after-save-hook
87 'planner-export-diary-future
88 nil t))
90 (defun planner-export-diary-get-schedule-entries (files)
91 "Return a list containing the planner schedule entries in FILES.
92 Entries in the returned list are of the form [DATE START END
93 DATA]. FILES is an alist of planner pages and their filenames."
94 (with-temp-buffer
95 (cd (planner-directory))
96 (let ((list '())
97 start end data)
98 (while (car files)
99 (insert-file-contents (cdar files))
100 (goto-char (point-min))
101 (while (re-search-forward
102 "^\\s-*\\([0-9]+:[0-9]+\\)\\s-*|\\s-*\\(.+\\)" nil t)
103 (setq start (match-string 1))
104 (setq data (match-string 2))
105 (setq end nil)
106 (when (string-match "\\([0-9]+:[0-9]+\\)\\s-*|\\s-*" data)
107 (setq end (match-string 1 data))
108 (setq data (replace-match "" nil t data)))
109 (setq list (cons
110 (vector (caar files)
111 start end data)
112 list)))
113 (setq files (cdr files))
114 (erase-buffer))
115 list)))
117 (defun planner-export-diary-format-schedule-entries-for-diary (list)
118 "Format LIST as diary entries.
119 LIST should contain entries of the form [DATE START END
120 DATA]."
121 (mapconcat (lambda (item)
122 (concat
123 (let ((date (planner-filename-to-calendar-date (elt item 0))))
124 (format "%02d/%02d/%04d"
125 (elt date 0)
126 (elt date 1)
127 (elt date 2)))
129 (elt item 1)
131 (elt item 3)))
132 list "\n"))
134 ;;;###autoload
135 (defun planner-export-diary (&optional from to)
136 "Exports all the schedules or the ones from FROM to TO (inclusive)."
137 (interactive)
138 (with-temp-file planner-export-diary-file
139 (insert (planner-export-diary-format-schedule-entries-for-diary
140 (planner-export-diary-get-schedule-entries
141 (planner-get-day-pages from to))))))
143 (provide 'planner-export-diary)
145 ;;; planner-export-diary.el ends here