Merged from jeho@jeho.org--2005 (patch 54-55), mwolson@gnu.org--2006 (patch 16)
[planner-el.git] / planner-ical.el
blob0932f43a6503ad4495b38d8f80986054d830aea4
1 ;;; planner-ical.el --- Import/Export planner tasks in the icalendar standard (RFC 2445)
3 ;; Copyright (C) 2005 Chris Parsons
5 ;; Emacs Lisp Archive Entry
6 ;; Filename: planner-ical.el
7 ;; Author: Chris Parsons <chris.p@rsons.org>
8 ;; Maintainer: Chris Parsons <chris.p@rsons.org>
9 ;; Keywords: planner, ical
10 ;; URL: http://sacha.free.net.ph/notebook/wiki/PlannerMode.php
12 ;; This file is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; This file is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Commentary:
29 ;; This module allows you to export planner tasks and notes in the
30 ;; icalendar format (RFC 2445). For a description of the standard, see
31 ;; http://www.ietf.org/rfc/rfc2445.txt.
33 ;; In future, it is hoped that tasks will be importable also.
35 ;; Note that this is very early days - currently we only export VTODO
36 ;; items from just one page.
38 ;; Usage:
40 ;; Call planner-ical-export-page and specify the page, or
41 ;; planner-ical-export-this-page. This produces a buffer. Save it
42 ;; somewhere.
44 (require 'planner)
45 (require 'icalendar)
47 ;;; Code:
49 ;;;_+ User variables
51 (defgroup planner-ical nil
52 "iCal (RFC 2445) support for planner.el."
53 :prefix "planner-ical"
54 :group 'planner)
56 (defcustom planner-ical-export-buffer "*Planner iCal Export*"
57 "Buffer name for iCal exports from `planner-ical-export'."
58 :type 'string
59 :group 'planner-timeclock-summary)
61 (defun planner-ical-export-page (page &optional file)
62 "Export PAGE's tasks in the Ical format.
63 If FILE is non-nil, results are saved to that file.
64 If FILE is nil, results are displayed in a `planner-ical-export-buffer'."
65 (interactive (list (planner-read-name (planner-file-alist)
66 "Enter the page to export: ")))
67 (if file
68 (with-temp-file file
69 (planner-ical-export (if (listp page) page (list page))))
70 (switch-to-buffer (get-buffer-create planner-ical-export-buffer))
71 (erase-buffer)
72 (planner-ical-export (if (listp page) page (list page)))))
74 (defun planner-ical-export (pages)
75 "Export the given plan page to iCalendar format. The result
76 will be added to the current buffer."
77 (let ((tasks (planner-extract-tasks pages))
78 result)
79 (while tasks
80 (when (not (string= (planner-task-status (car tasks)) "X"))
81 (let* ((task (car tasks))
82 cat-list
83 cat-list-no-dates
84 (header (format "\nBEGIN:VTODO\nUID:emacs-planner-%x"
85 (sxhash (planner-task-description task))))
86 (my-task-date (planner-task-date task))
87 (task-date
88 (if my-task-date
89 (planner-filename-to-calendar-date my-task-date)
90 nil))
91 (task-categories
92 (if (featurep 'planner-multi)
93 (progn (setq cat-list
94 (planner-multi-task-link-as-list task))
95 (setq cat-list-no-dates nil)
96 (while cat-list
97 (let ((cat (car cat-list)))
98 (when (not (string-match planner-date-regexp
99 cat))
100 (setq cat-list-no-dates
101 (cons cat cat-list-no-dates)))
102 (setq cat-list (cdr cat-list))))
103 (mapconcat
104 (function (lambda (x)
105 (when x (replace-regexp-in-string
106 "\\(\\[\\[\\|\\]\\]\\)" ""
107 x))))
108 cat-list-no-dates ", "))
109 (planner-task-plan task)))
110 (task-due
111 (when task-date (concat "\nDUE:" (icalendar--date-to-isodate
112 task-date))))
113 (contents
114 (concat task-due
115 "\nSUMMARY:" (planner-task-description task)
116 "\nCATEGORIES:" task-categories)))
118 (setq result (concat result header contents "\nEND:VTODO"))))
119 (setq tasks (cdr tasks)))
120 (let ((coding-system-for-write 'utf-8))
121 (insert "BEGIN:VCALENDAR")
122 (insert "\nPRODID:-//Emacs//NONSGML planner-ical.el//EN")
123 (insert "\nVERSION:2.0")
124 (when result (insert result))
125 (insert "\nEND:VCALENDAR\n"))))
127 (defun planner-ical-export-this-page ()
128 "Display the tasks on the current page in iCal format."
129 (interactive)
130 (if (planner-derived-mode-p 'planner-mode)
131 (planner-ical-export-page (planner-page-name))
132 (error "Not in planner page")))
134 (provide 'planner-ical)
136 ;;; planner-ical.el ends here