NNDiary commit
[gnus] / lisp / gnus-diary.el
1 ;;; gnus-diary.el --- Wrapper around the NNDiary Gnus backend
2
3 ;; Copyright (C) 1999 Didier Verna.
4
5 ;; PRCS: $Id: gnus-diary.el 1.8 Tue, 04 Sep 2001 11:32:13 +0200 didier $
6
7 ;; Author:        Didier Verna <didier@xemacs.org>
8 ;; Maintainer:    Didier Verna <didier@xemacs.org>
9 ;; Created:       Tue Jul 20 10:42:55 1999 under XEmacs 21.2 (beta 18)
10 ;; Last Revision: Wed Aug  8 14:38:14 2001
11 ;; Keywords:      calendar mail news
12
13 ;; This file is part of NNDiary.
14
15 ;; NNDiary is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2 of the License, or
18 ;; (at your option) any later version.
19
20 ;; NNDiary is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 ;; GNU General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with this program; if not, write to the Free Software
27 ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28
29
30 ;;; Commentary:
31
32 ;; Contents management by FCM version 0.1.
33
34 ;; Description:
35 ;; ===========
36
37 ;; Gnus-Diary is a wrapper around the NNDiary Gnus backend.  It is here to
38 ;; make your nndiary-user life easier in differnet ways.  So, you don't have
39 ;; to use it if you don't want to.  But, really, you should.
40
41 ;; Gnus-Diary offers the following improvements on top of the NNDiary backend:
42
43 ;; - A nice summary line format:
44 ;;   Displaying diary messages in standard summary line format (usually
45 ;;   something like "<From Joe>: <Subject>") is pretty useless.  Most of the
46 ;;   time, you're the one who wrote the message, and you mostly want to see
47 ;;   the event's date.  Gnus-Diary offers you a nice summary line format which
48 ;;   will do this.  By default, a summary line will appear like this:
49 ;;
50 ;;     <Event Date>: <Subject> <Remaining time>
51 ;;
52 ;;   for example, here's how Joe's birthday is displayed in my
53 ;;   "nndiary:birhdays" summary buffer (the message is expirable, but will
54 ;;   never be deleted, as it specifies a regular event):
55 ;;
56 ;;   E  Sat, Sep 22 01, 12:00: Joe's birthday (in 6 months, 1 week)
57
58 ;;  - More article sorting functions:
59 ;;    Gnus-Diary adds a new sorting function called
60 ;;    `gnus-summary-sort-by-schedule'.  This function lets you organize your
61 ;;    diary summary buffers from the closest event to the farthest one.
62
63 ;;  - Automatic generation of diary group parameters:
64 ;;    When you create a new diary group, or visit one, Gnus-Diary checks your
65 ;;    group parameters, and if needed, sets the summary line format to the
66 ;;    diary-specific value, adds the diary-specific sorting functions, and
67 ;;    also adds the different `X-Diary-*' headers to the group's
68 ;;    posting-style.  It is then easier to send a diary message, because if
69 ;;    you use `C-u a' or `C-u m' on a diary group to prepare a message, these
70 ;;    headers will be inserted automatically (but not filled with proper
71 ;;    values yet).
72
73
74 ;; Usage:
75 ;; =====
76
77 ;; 0/ Don't use any `gnus-user-format-function-[d|D]'.  Gnus-Diary provides
78 ;;    both of these (sorry if you used them before).
79 ;; 1/ Add '(require 'gnus-diary) to your gnusrc file.
80 ;; 2/ Customize your gnus-diary options to suit your needs.
81
82
83
84 ;; Bugs / Todo:
85 ;; ===========
86
87 ;; * Provide `gnus-group-diary-mail' and `gnus-group-post-diary-news' (or
88 ;;   something like that), that would do just like `gnus-group-mail' and
89 ;;   `gnus-group-post-news', but also prompt for diary header values with
90 ;;   completion etc.
91 ;; * Maybe not actually: we could just have a function that converts *any*
92 ;;   message to a diary one, by prompting the schedule. You could then forward
93 ;;   a message and make it a diary one etc.
94
95 ;;; Code:
96
97 (require 'nndiary)
98
99 (defgroup gnus-diary nil
100   "Utilities on top of the nndiary backend for Gnus.")
101
102 (defcustom gnus-diary-summary-line-format "%U%R%z%I %uD: %(%s%) (%ud)\n"
103   "*Summary line format for nndiary groups."
104   :type 'string
105   :group 'gnus-diary
106   :group 'gnus-summary-format)
107
108 (defcustom gnus-diary-time-format "%a, %b %e %y, %H:%M"
109   "*Time format to display appointements in nndiary summary buffers.
110 Please refer to `format-time-string' for information on possible values."
111   :type 'string
112   :group 'gnus-diary)
113
114 (defcustom gnus-diary-delay-format-function 'gnus-diary-delay-format-english
115   "*Function called to format a diary delay string.
116 It is passed two arguments.  The first one is non nil if the delay is in
117 the past.  The second one is of the form ((NUM . UNIT) ...) where NUM is
118 an integer and UNIT is one of 'year 'month 'week 'day 'hour or 'minute.
119 It should return strings like \"In 2 months, 3 weeks\", \"3 hours,
120 1 minute ago\" and so on.
121
122 There are currently two built-in format functions:
123 `gnus-diary-delay-format-english' (the default)
124 `gnus-diary-delay-format-french'"
125   :type '(choice (const  :tag "english" gnus-diary-delay-format-english)
126                  (const  :tag "french"  gnus-diary-delay-format-french)
127                  (symbol :tag "other"))
128   :group 'gnus-diary)
129
130 (defconst gnus-diary-version nndiary-version
131   "Current Diary backend version.")
132
133
134 ;; Summary line format ======================================================
135
136 (defun gnus-diary-delay-format-french (past delay)
137   (if (null delay)
138       "maintenant!"
139     ;; Keep only a precision of two degrees
140     (and (> (length delay) 1) (setf (nthcdr 2 delay) nil))
141     (concat (if past "il y a " "dans ")
142             (let ((str "")
143                   del)
144               (while (setq del (pop delay))
145                 (setq str (concat str
146                                   (int-to-string (car del)) " "
147                                   (cond ((eq (cdr del) 'year)
148                                          "an")
149                                         ((eq (cdr del) 'month)
150                                          "mois")
151                                         ((eq (cdr del) 'week)
152                                          "semaine")
153                                         ((eq (cdr del) 'day)
154                                          "jour")
155                                         ((eq (cdr del) 'hour)
156                                          "heure")
157                                         ((eq (cdr del) 'minute)
158                                          "minute"))
159                                   (unless (or (eq (cdr del) 'month)
160                                               (= (car del) 1))
161                                     "s")
162                                   (if delay ", "))))
163               str))))
164
165
166 (defun gnus-diary-delay-format-english (past delay)
167   (if (null delay)
168       "now!"
169     ;; Keep only a precision of two degrees
170     (and (> (length delay) 1) (setf (nthcdr 2 delay) nil))
171     (concat (unless past "in ")
172             (let ((str "")
173                   del)
174               (while (setq del (pop delay))
175                 (setq str (concat str
176                                   (int-to-string (car del)) " "
177                                   (symbol-name (cdr del))
178                                   (and (> (car del) 1) "s")
179                                   (if delay ", "))))
180               str)
181             (and past " ago"))))
182
183
184 (defun gnus-diary-header-schedule (headers)
185   ;; Same as `nndiary-schedule', but given a set of headers HEADERS
186   (mapcar
187    (lambda (elt)
188      (let ((head (cdr (assoc (intern (format "X-Diary-%s" (car elt)))
189                              headers))))
190        (when head
191          (nndiary-parse-schedule-value head (cadr elt) (caddr elt)))))
192    nndiary-headers))
193
194 ;; #### NOTE: Gnus sometimes gives me a HEADER not corresponding to any
195 ;; message, with all fields set to nil here. I don't know what it is for, and
196 ;; I just ignore it.
197 (defun gnus-user-format-function-d (header)
198   ;; Returns an aproximative delay string for the next occurence of this
199   ;; message. The delay is given only in the first non zero unit.
200   ;; Code partly stolen from article-make-date-line
201   (let* ((extras (mail-header-extra header))
202          (sched (gnus-diary-header-schedule extras))
203          (occur (nndiary-next-occurence sched (current-time)))
204          (now (current-time))
205          (real-time (subtract-time occur now)))
206     (if (null real-time)
207         "?????"
208       (let* ((sec (+ (* (float (car real-time)) 65536) (cadr real-time)))
209              (past (< sec 0))
210              delay)
211         (and past (setq sec (- sec)))
212         (unless (zerop sec)
213           ;; This is a bit convoluted, but basically we go through the time
214           ;; units for years, weeks, etc, and divide things to see whether
215           ;; that results in positive answers.
216           (let ((units `((year . ,(* 365.25 24 3600))
217                          (month . ,(* 31 24 3600))
218                          (week . ,(* 7 24 3600))
219                          (day . ,(* 24 3600))
220                          (hour . 3600)
221                          (minute . 60)))
222                 unit num)
223             (while (setq unit (pop units))
224               (unless (zerop (setq num (ffloor (/ sec (cdr unit)))))
225                 (setq delay (append delay `((,(floor num) . ,(car unit))))))
226               (setq sec (- sec (* num (cdr unit)))))))
227         (funcall gnus-diary-delay-format-function past delay)))
228     ))
229
230 ;; #### NOTE: Gnus sometimes gives me a HEADER not corresponding to any
231 ;; message, with all fields set to nil here. I don't know what it is for, and
232 ;; I just ignore it.
233 (defun gnus-user-format-function-D (header)
234   ;; Returns a formatted time string for the next occurence of this message.
235   (let* ((extras (mail-header-extra header))
236          (sched (gnus-diary-header-schedule extras))
237          (occur (nndiary-next-occurence sched (current-time))))
238     (format-time-string gnus-diary-time-format occur)))
239
240
241 ;; Article sorting functions ================================================
242
243 (defun gnus-article-sort-by-schedule (h1 h2)
244   (let* ((now (current-time))
245          (e1 (mail-header-extra h1))
246          (e2 (mail-header-extra h2))
247          (s1 (gnus-diary-header-schedule e1))
248          (s2 (gnus-diary-header-schedule e2))
249          (o1 (nndiary-next-occurence s1 now))
250          (o2 (nndiary-next-occurence s2 now)))
251     (if (and (= (car o1) (car o2)) (= (cadr o1) (cadr o2)))
252         (< (mail-header-number h1) (mail-header-number h2))
253       (time-less-p o1 o2))))
254
255
256 (defun gnus-thread-sort-by-schedule (h1 h2)
257   (gnus-article-sort-by-schedule (gnus-thread-header h1)
258                                  (gnus-thread-header h2)))
259
260 (defun gnus-summary-sort-by-schedule (&optional reverse)
261   "Sort nndiary summary buffers by schedule of appointements.
262 Optional prefix (or REVERSE argument) means sort in reverse order."
263   (interactive "P")
264   (gnus-summary-sort 'schedule reverse))
265
266 (add-hook 'gnus-summary-menu-hook
267           (lambda ()
268             (easy-menu-add-item gnus-summary-misc-menu
269                                 '("Sort")
270                                 ["Sort by schedule"
271                                  gnus-summary-sort-by-schedule
272                                  (eq (car (gnus-find-method-for-group
273                                            gnus-newsgroup-name))
274                                      'nndiary)]
275                                 "Sort by number")))
276
277 ;; Group parameters autosetting =============================================
278
279 (defun gnus-diary-update-group-parameters (group)
280   ;; Ensure that nndiary groups have convenient group parameters:
281   ;; - a posting style containing X-Diary headers
282   ;; - a nice summary line format
283   ;; - NNDiary specific sorting by schedule functions
284   ;; In general, try not to mess with what the user might have modified.
285   (let ((posting-style (gnus-group-get-parameter group 'posting-style t)))
286     ;; Posting style:
287     (mapcar (lambda (elt)
288               (let ((header (format "X-Diary-%s" (car elt))))
289                 (unless (assoc header posting-style)
290                   (setq posting-style (append posting-style
291                                               `((,header "*")))))
292                 ))
293             nndiary-headers)
294     (gnus-group-set-parameter group 'posting-style posting-style)
295     ;; Summary line format:
296     (unless (gnus-group-get-parameter group 'gnus-summary-line-format t)
297       (gnus-group-set-parameter group 'gnus-summary-line-format
298                                 `(,gnus-diary-summary-line-format)))
299     ;; Sorting by schedule:
300     (unless (gnus-group-get-parameter group 'gnus-article-sort-functions)
301       (gnus-group-set-parameter group 'gnus-article-sort-functions
302                                 '((append gnus-article-sort-functions
303                                           (list
304                                            'gnus-article-sort-by-schedule)))))
305     (unless (gnus-group-get-parameter group 'gnus-thread-sort-functions)
306       (gnus-group-set-parameter group 'gnus-thread-sort-functions
307                                 '((append gnus-thread-sort-functions
308                                           (list
309                                            'gnus-thread-sort-by-schedule)))))
310     ))
311
312 ;; Called when a group is subscribed. This is needed because groups created
313 ;; because of mail splitting are *not* created with the backend function.
314 ;; Thus, `nndiary-request-create-group-hooks' is inoperative.
315 (defun gnus-diary-maybe-update-group-parameters (group)
316   (when (eq (car (gnus-find-method-for-group group)) 'nndiary)
317     (gnus-diary-update-group-parameters group)))
318
319 (add-hook 'nndiary-request-create-group-hooks
320           'gnus-diary-update-group-parameters)
321 ;; Now that we have `gnus-subscribe-newsgroup-hooks', this is not needed
322 ;; anymore. Maybe I should remove this completely.
323 (add-hook 'nndiary-request-update-info-hooks
324           'gnus-diary-update-group-parameters)
325 (add-hook 'gnus-subscribe-newsgroup-hooks
326           'gnus-diary-maybe-update-group-parameters)
327
328 (defun gnus-diary-version ()
329   "Current Diary backend version."
330   (interactive)
331   (message "NNDiary version %s" nndiary-version))
332
333 (provide 'gnus-diary)
334
335 ;;; gnus-diary.el ends here