1 ;;; gnus-diary.el --- Wrapper around the NNDiary Gnus backend
3 ;; Copyright (c) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4 ;; Copyright (C) 1999, 2000, 2001 Didier Verna.
6 ;; Author: Didier Verna <didier@xemacs.org>
7 ;; Maintainer: Didier Verna <didier@xemacs.org>
8 ;; Created: Tue Jul 20 10:42:55 1999
9 ;; Keywords: calendar mail news
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published
15 ;; by the Free Software Foundation; either version 2 of the License,
16 ;; or (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 ;; General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with this program; if not, write to the Free Software
25 ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26 ;; MA 02110-1301, USA.
31 ;; Contents management by FCM version 0.1.
36 ;; Gnus-Diary is a wrapper around the NNDiary Gnus backend. It is here to
37 ;; make your nndiary-user life easier in different ways. So, you don't have
38 ;; to use it if you don't want to. But, really, you should.
40 ;; Gnus-Diary offers the following features on top of the NNDiary backend:
42 ;; - A nice summary line format:
43 ;; Displaying diary messages in standard summary line format (usually
44 ;; something like "<From Joe>: <Subject>") is pretty useless. Most of the
45 ;; time, you're the one who wrote the message, and you mostly want to see
46 ;; the event's date. Gnus-Diary offers you a nice summary line format
47 ;; which will do this. By default, a summary line will appear like this:
49 ;; <Event Date>: <Subject> <Remaining time>
51 ;; for example, here's how Joe's birthday is displayed in my
52 ;; "nndiary:birhdays" summary buffer (the message is expirable, but will
53 ;; never be deleted, as it specifies a regular event):
55 ;; E Sat, Sep 22 01, 12:00: Joe's birthday (in 6 months, 1 week)
57 ;; - More article sorting functions:
58 ;; Gnus-Diary adds a new sorting function called
59 ;; `gnus-summary-sort-by-schedule'. This function lets you organize your
60 ;; diary summary buffers from the closest event to the farthest one.
62 ;; - Automatic generation of diary group parameters:
63 ;; When you create a new diary group, or visit one, Gnus-Diary checks your
64 ;; group parameters, and if needed, sets the summary line format to the
65 ;; diary-specific value, adds the diary-specific sorting functions, and
66 ;; also adds the different `X-Diary-*' headers to the group's
67 ;; posting-style. It is then easier to send a diary message, because if
68 ;; you use `C-u a' or `C-u m' on a diary group to prepare a message, these
69 ;; headers will be inserted automatically (but not filled with proper
72 ;; - An interactive mail-to-diary convertion function:
73 ;; The function `gnus-diary-check-message' ensures that the current message
74 ;; contains all the required diary headers, and prompts you for values /
75 ;; correction if needed. This function is hooked in the nndiary backend so
76 ;; that moving an article to an nndiary group will trigger it
77 ;; automatically. It is also bound to `C-c D c' in message-mode and
78 ;; article-edit-mode in order to ease the process of converting a usual
79 ;; mail to a diary one. This function takes a prefix argument which will
80 ;; force prompting of all diary headers, regardless of their
81 ;; presence/validity. That way, you can very easily reschedule a diary
82 ;; message for instance.
88 ;; 0/ Don't use any `gnus-user-format-function-[d|D]'. Gnus-Diary provides
89 ;; both of these (sorry if you used them before).
90 ;; 1/ Add '(require 'gnus-diary) to your gnusrc file.
91 ;; 2/ Customize your gnus-diary options to suit your needs.
105 (defgroup gnus-diary nil
106 "Utilities on top of the nndiary backend for Gnus."
110 (defcustom gnus-diary-summary-line-format "%U%R%z %uD: %(%s%) (%ud)\n"
111 "*Summary line format for nndiary groups."
114 :group 'gnus-summary-format)
116 (defcustom gnus-diary-time-format "%a, %b %e %y, %H:%M"
117 "*Time format to display appointements in nndiary summary buffers.
118 Please refer to `format-time-string' for information on possible values."
122 (defcustom gnus-diary-delay-format-function 'gnus-diary-delay-format-english
123 "*Function called to format a diary delay string.
124 It is passed two arguments. The first one is non nil if the delay is in
125 the past. The second one is of the form ((NUM . UNIT) ...) where NUM is
126 an integer and UNIT is one of 'year 'month 'week 'day 'hour or 'minute.
127 It should return strings like \"In 2 months, 3 weeks\", \"3 hours,
128 1 minute ago\" and so on.
130 There are currently two built-in format functions:
131 `gnus-diary-delay-format-english' (the default)
132 `gnus-diary-delay-format-french'"
133 :type '(choice (const :tag "english" gnus-diary-delay-format-english)
134 (const :tag "french" gnus-diary-delay-format-french)
135 (symbol :tag "other"))
138 (defconst gnus-diary-version nndiary-version
139 "Current Diary backend version.")
142 ;; Compatibility functions ==================================================
145 (if (fboundp 'kill-entire-line)
146 (defalias 'gnus-diary-kill-entire-line 'kill-entire-line)
147 (defun gnus-diary-kill-entire-line ()
149 (let ((kill-whole-line t))
153 ;; Summary line format ======================================================
155 (defun gnus-diary-delay-format-french (past delay)
158 ;; Keep only a precision of two degrees
159 (and (> (length delay) 1) (setcdr (cdr delay) nil))
160 (concat (if past "il y a " "dans ")
163 (while (setq del (pop delay))
164 (setq str (concat str
165 (int-to-string (car del)) " "
166 (cond ((eq (cdr del) 'year)
168 ((eq (cdr del) 'month)
170 ((eq (cdr del) 'week)
174 ((eq (cdr del) 'hour)
176 ((eq (cdr del) 'minute)
178 (unless (or (eq (cdr del) 'month)
185 (defun gnus-diary-delay-format-english (past delay)
188 ;; Keep only a precision of two degrees
189 (and (> (length delay) 1) (setcdr (cdr delay) nil))
190 (concat (unless past "in ")
193 (while (setq del (pop delay))
194 (setq str (concat str
195 (int-to-string (car del)) " "
196 (symbol-name (cdr del))
197 (and (> (car del) 1) "s")
203 (defun gnus-diary-header-schedule (headers)
204 ;; Same as `nndiary-schedule', but given a set of headers HEADERS
207 (let ((head (cdr (assoc (intern (format "X-Diary-%s" (car elt)))
210 (nndiary-parse-schedule-value head (cadr elt) (car (cddr elt))))))
213 ;; #### NOTE: Gnus sometimes gives me a HEADER not corresponding to any
214 ;; message, with all fields set to nil here. I don't know what it is for, and
216 (defun gnus-user-format-function-d (header)
217 ;; Returns an aproximative delay string for the next occurence of this
218 ;; message. The delay is given only in the first non zero unit.
219 ;; Code partly stolen from article-make-date-line
220 (let* ((extras (mail-header-extra header))
221 (sched (gnus-diary-header-schedule extras))
222 (occur (nndiary-next-occurence sched (current-time)))
224 (real-time (subtract-time occur now)))
227 (let* ((sec (+ (* (float (car real-time)) 65536) (cadr real-time)))
230 (and past (setq sec (- sec)))
232 ;; This is a bit convoluted, but basically we go through the time
233 ;; units for years, weeks, etc, and divide things to see whether
234 ;; that results in positive answers.
235 (let ((units `((year . ,(* 365.25 24 3600))
236 (month . ,(* 31 24 3600))
237 (week . ,(* 7 24 3600))
242 (while (setq unit (pop units))
243 (unless (zerop (setq num (ffloor (/ sec (cdr unit)))))
244 (setq delay (append delay `((,(floor num) . ,(car unit))))))
245 (setq sec (- sec (* num (cdr unit)))))))
246 (funcall gnus-diary-delay-format-function past delay)))
249 ;; #### NOTE: Gnus sometimes gives me a HEADER not corresponding to any
250 ;; message, with all fields set to nil here. I don't know what it is for, and
252 (defun gnus-user-format-function-D (header)
253 ;; Returns a formatted time string for the next occurence of this message.
254 (let* ((extras (mail-header-extra header))
255 (sched (gnus-diary-header-schedule extras))
256 (occur (nndiary-next-occurence sched (current-time))))
257 (format-time-string gnus-diary-time-format occur)))
260 ;; Article sorting functions ================================================
262 (defun gnus-article-sort-by-schedule (h1 h2)
263 (let* ((now (current-time))
264 (e1 (mail-header-extra h1))
265 (e2 (mail-header-extra h2))
266 (s1 (gnus-diary-header-schedule e1))
267 (s2 (gnus-diary-header-schedule e2))
268 (o1 (nndiary-next-occurence s1 now))
269 (o2 (nndiary-next-occurence s2 now)))
270 (if (and (= (car o1) (car o2)) (= (cadr o1) (cadr o2)))
271 (< (mail-header-number h1) (mail-header-number h2))
272 (time-less-p o1 o2))))
275 (defun gnus-thread-sort-by-schedule (h1 h2)
276 (gnus-article-sort-by-schedule (gnus-thread-header h1)
277 (gnus-thread-header h2)))
279 (defun gnus-summary-sort-by-schedule (&optional reverse)
280 "Sort nndiary summary buffers by schedule of appointements.
281 Optional prefix (or REVERSE argument) means sort in reverse order."
283 (gnus-summary-sort 'schedule reverse))
285 (defvar gnus-summary-misc-menu) ;; Avoid byte compiler warning.
286 ;; The function `easy-menu-add-item' is not available under Emacs
287 ;; versions prior to 20.3. Could anyone try to emulate it?
288 (if (eval-when-compile
290 (or (fboundp 'easy-menu-add-item)
292 (defalias 'easy-menu-add-item 'ignore)
294 (add-hook 'gnus-summary-menu-hook
296 (easy-menu-add-item gnus-summary-misc-menu
299 gnus-summary-sort-by-schedule
300 (eq (car (gnus-find-method-for-group
301 gnus-newsgroup-name))
308 ;; Group parameters autosetting =============================================
310 (defun gnus-diary-update-group-parameters (group)
311 ;; Ensure that nndiary groups have convenient group parameters:
312 ;; - a posting style containing X-Diary headers
313 ;; - a nice summary line format
314 ;; - NNDiary specific sorting by schedule functions
315 ;; In general, try not to mess with what the user might have modified.
316 (let ((posting-style (gnus-group-get-parameter group 'posting-style t)))
318 (mapcar (lambda (elt)
319 (let ((header (format "X-Diary-%s" (car elt))))
320 (unless (assoc header posting-style)
321 (setq posting-style (append posting-style
325 (gnus-group-set-parameter group 'posting-style posting-style)
326 ;; Summary line format:
327 (unless (gnus-group-get-parameter group 'gnus-summary-line-format t)
328 (gnus-group-set-parameter group 'gnus-summary-line-format
329 `(,gnus-diary-summary-line-format)))
330 ;; Sorting by schedule:
331 (unless (gnus-group-get-parameter group 'gnus-article-sort-functions)
332 (gnus-group-set-parameter group 'gnus-article-sort-functions
333 '((append gnus-article-sort-functions
335 'gnus-article-sort-by-schedule)))))
336 (unless (gnus-group-get-parameter group 'gnus-thread-sort-functions)
337 (gnus-group-set-parameter group 'gnus-thread-sort-functions
338 '((append gnus-thread-sort-functions
340 'gnus-thread-sort-by-schedule)))))
343 ;; Called when a group is subscribed. This is needed because groups created
344 ;; because of mail splitting are *not* created with the backend function.
345 ;; Thus, `nndiary-request-create-group-hooks' is inoperative.
346 (defun gnus-diary-maybe-update-group-parameters (group)
347 (when (eq (car (gnus-find-method-for-group group)) 'nndiary)
348 (gnus-diary-update-group-parameters group)))
350 (add-hook 'nndiary-request-create-group-hooks
351 'gnus-diary-update-group-parameters)
352 ;; Now that we have `gnus-subscribe-newsgroup-hooks', this is not needed
353 ;; anymore. Maybe I should remove this completely.
354 (add-hook 'nndiary-request-update-info-hooks
355 'gnus-diary-update-group-parameters)
356 (add-hook 'gnus-subscribe-newsgroup-hooks
357 'gnus-diary-maybe-update-group-parameters)
360 ;; Diary Message Checking ===================================================
362 (defvar gnus-diary-header-value-history nil
363 ;; History variable for header value prompting
366 (defun gnus-diary-narrow-to-headers ()
367 "Narrow the current buffer to the header part.
368 Point is left at the beginning of the region.
369 The buffer is assumed to contain a message, but the format is unknown."
370 (cond ((eq major-mode 'message-mode)
371 (message-narrow-to-headers))
373 (goto-char (point-min))
374 (when (search-forward "\n\n" nil t)
375 (narrow-to-region (point-min) (- (point) 1))
376 (goto-char (point-min))))
379 (defun gnus-diary-add-header (str)
380 "Add a header to the current buffer.
381 The buffer is assumed to contain a message, but the format is unknown."
382 (cond ((eq major-mode 'message-mode)
383 (message-add-header str))
386 (gnus-diary-narrow-to-headers)
387 (goto-char (point-max))
388 (if (string-match "\n$" str)
393 (defun gnus-diary-check-message (arg)
394 "Ensure that the current message is a valid for NNDiary.
395 This function checks that all NNDiary required headers are present and
396 valid, and prompts for values / correction otherwise.
398 If ARG (or prefix) is non-nil, force prompting for all fields."
403 (let ((header (concat "X-Diary-" (car head)))
406 ;; First, try to find the header, and checks for validity:
408 (gnus-diary-narrow-to-headers)
409 (when (re-search-forward (concat "^" header ":") nil t)
410 (unless (eq (char-after) ? )
412 (setq value (buffer-substring (point) (point-at-eol)))
413 (and (string-match "[ \t]*\\([^ \t]+\\)[ \t]*" value)
414 (setq value (match-string 1 value)))
416 (nndiary-parse-schedule-value value
417 (nth 1 head) (nth 2 head))
420 ;; #### NOTE: this (along with the `gnus-diary-add-header'
421 ;; function) could be rewritten in a better way, in particular
422 ;; not to blindly remove an already present header and reinsert
423 ;; it somewhere else afterwards.
424 (when (or ask invalid)
425 (gnus-diary-kill-entire-line))
427 ;; Now, loop until a valid value is provided:
428 (while (or ask (not value) invalid)
429 (let ((prompt (concat (and invalid
430 (prog1 "(current value invalid) "
434 (if (listp (nth 1 head))
435 (completing-read prompt (cons '("*" nil) (nth 1 head))
437 gnus-diary-header-value-history)
438 (read-string prompt value
439 gnus-diary-header-value-history))))
443 (nndiary-parse-schedule-value value
444 (nth 1 head) (nth 2 head))
447 (gnus-diary-add-header (concat header ": " value))
452 (add-hook 'nndiary-request-accept-article-hooks
453 (lambda () (gnus-diary-check-message nil)))
455 (define-key message-mode-map "\C-cDc" 'gnus-diary-check-message)
456 (define-key gnus-article-edit-mode-map "\C-cDc" 'gnus-diary-check-message)
459 ;; The end ==================================================================
461 (defun gnus-diary-version ()
462 "Current Diary backend version."
464 (message "NNDiary version %s" nndiary-version))
466 (define-key message-mode-map "\C-cDv" 'gnus-diary-version)
467 (define-key gnus-article-edit-mode-map "\C-cDv" 'gnus-diary-version)
470 (provide 'gnus-diary)
472 ;;; gnus-diary.el ends here