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