1 ;;; nndiary.el --- A diary back end for Gnus
3 ;; Copyright (C) 1999-2014 Free Software Foundation, Inc.
5 ;; Author: Didier Verna <didier@xemacs.org>
6 ;; Maintainer: Didier Verna <didier@xemacs.org>
7 ;; Created: Fri Jul 16 18:55:42 1999
8 ;; Keywords: calendar mail news
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs 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 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs 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. If not, see <http://www.gnu.org/licenses/>.
28 ;; Contents management by FCM version 0.1.
33 ;; nndiary is a mail back end designed to handle mails as diary event
34 ;; reminders. It is now fully documented in the Gnus manual.
40 ;; * Respooling doesn't work because contrary to the request-scan function,
41 ;; Gnus won't allow me to override the split methods when calling the
42 ;; respooling back end functions.
43 ;; * There's a bug in the time zone mechanism with variable TZ locations.
44 ;; * We could allow a keyword like `ask' in X-Diary-* headers, that would mean
45 ;; "ask for value upon reception of the message".
46 ;; * We could add an optional header X-Diary-Reminders to specify a special
47 ;; reminders value for this message. Suggested by Jody Klymak.
48 ;; * We should check messages validity in other circumstances than just
49 ;; moving an article from somewhere else (request-accept). For instance,
50 ;; when editing / saving and so on.
56 ;; * nnoo. NNDiary is very similar to nnml. This makes the idea of using nnoo
57 ;; (to derive nndiary from nnml) natural. However, my experience with nnoo
58 ;; is that for reasonably complex back ends like this one, nnoo is a burden
59 ;; rather than an help. It's tricky to use, not everything can be inherited,
60 ;; what can be inherited and when is not very clear, and you've got to be
61 ;; very careful because a little mistake can fuck up your other back ends,
62 ;; especially because their variables will be use instead of your real ones.
63 ;; Finally, I found it easier to just clone the needed parts of nnml, and
64 ;; tracking nnml updates is not a big deal.
66 ;; IMHO, nnoo is actually badly designed. A much simpler, and yet more
67 ;; powerful one would be to make *real* functions and variables for a new
68 ;; back end based on another. Lisp is a reflexive language so that's a very
69 ;; easy thing to do: inspect the function's form, replace occurrences of
70 ;; <nnfrom> (even in strings) with <nnto>, and you're done.
72 ;; * nndiary-get-new-mail, nndiary-mail-source and nndiary-split-methods:
73 ;; NNDiary has some experimental parts, in the sense Gnus normally uses only
74 ;; one mail back ends for mail retrieval and splitting. This back end is
75 ;; also an attempt to make it behave differently. For Gnus developers: as
76 ;; you can see if you snarf into the code, that was not a very difficult
77 ;; thing to do. Something should be done about the respooling breakage
86 (eval-when-compile (require 'cl
))
91 ;; Compatibility Functions =================================================
94 (if (fboundp 'signal-error
)
95 (defun nndiary-error (&rest args
)
96 (apply #'signal-error
'nndiary args
))
97 (defun nndiary-error (&rest args
)
98 (apply #'error args
))))
101 ;; Back End behavior customization ===========================================
103 (defgroup nndiary nil
104 "The Gnus Diary back end."
108 (defcustom nndiary-mail-sources
109 `((file :path
,(expand-file-name "~/.nndiary")))
110 "*NNDiary specific mail sources.
111 This variable is used by nndiary in place of the standard `mail-sources'
112 variable when `nndiary-get-new-mail' is set to non-nil. These sources
113 must contain diary messages ONLY."
118 (defcustom nndiary-split-methods
'(("diary" ""))
119 "*NNDiary specific split methods.
120 This variable is used by nndiary in place of the standard
121 `nnmail-split-methods' variable when `nndiary-get-new-mail' is set to
125 :type
'(choice (repeat :tag
"Alist" (group (string :tag
"Name") regexp
))
126 (function-item nnmail-split-fancy
)
127 (function :tag
"Other")))
130 (defcustom nndiary-reminders
'((0 . day
))
131 "*Different times when you want to be reminded of your appointments.
132 Diary articles will appear again, as if they'd been just received.
134 Entries look like (3 . day) which means something like \"Please
135 Hortense, would you be so kind as to remind me of my appointments 3 days
136 before the date, thank you very much. Anda, hmmm... by the way, are you
137 doing anything special tonight ?\".
139 The units of measure are 'minute 'hour 'day 'week 'month and 'year (no,
140 not 'century, sorry).
142 NOTE: the units of measure actually express dates, not durations: if you
143 use 'week, messages will pop up on Sundays at 00:00 (or Mondays if
144 `nndiary-week-starts-on-monday' is non-nil) and *not* 7 days before the
145 appointment, if you use 'month, messages will pop up on the first day of
146 each months, at 00:00 and so on.
148 If you really want to specify a duration (like 24 hours exactly), you can
149 use the equivalent in minutes (the smallest unit). A fuzz of 60 seconds
150 maximum in the reminder is not that painful, I think. Although this
151 scheme might appear somewhat weird at a first glance, it is very powerful.
152 In order to make this clear, here are some examples:
154 - '(0 . day): this is the default value of `nndiary-reminders'. It means
155 pop up the appointments of the day each morning at 00:00.
157 - '(1 . day): this means pop up the appointments the day before, at 00:00.
159 - '(6 . hour): for an appointment at 18:30, this would pop up the
160 appointment message at 12:00.
162 - '(360 . minute): for an appointment at 18:30 and 15 seconds, this would
163 pop up the appointment message at 12:30."
165 :type
'(repeat (cons :format
"%v\n"
166 (integer :format
"%v")
167 (choice :format
"%[%v(s)%] before...\n"
169 (const :format
"%v" minute
)
170 (const :format
"%v" hour
)
171 (const :format
"%v" day
)
172 (const :format
"%v" week
)
173 (const :format
"%v" month
)
174 (const :format
"%v" year
)))))
176 (defcustom nndiary-week-starts-on-monday nil
177 "*Whether a week starts on monday (otherwise, sunday)."
182 (define-obsolete-variable-alias 'nndiary-request-create-group-hooks
183 'nndiary-request-create-group-functions
"24.3")
184 (defcustom nndiary-request-create-group-functions nil
185 "*Hook run after `nndiary-request-create-group' is executed.
186 The hook functions will be called with the full group name as argument."
190 (define-obsolete-variable-alias 'nndiary-request-update-info-hooks
191 'nndiary-request-update-info-functions
"24.3")
192 (defcustom nndiary-request-update-info-functions nil
193 "*Hook run after `nndiary-request-update-info-group' is executed.
194 The hook functions will be called with the full group name as argument."
198 (define-obsolete-variable-alias 'nndiary-request-accept-article-hooks
199 'nndiary-request-accept-article-functions
"24.3")
200 (defcustom nndiary-request-accept-article-functions nil
201 "*Hook run before accepting an article.
202 Executed near the beginning of `nndiary-request-accept-article'.
203 The hook functions will be called with the article in the current buffer."
207 (defcustom nndiary-check-directory-twice t
208 "*If t, check directories twice to avoid NFS failures."
213 ;; Back End declaration ======================================================
215 ;; Well, most of this is nnml clonage.
217 (nnoo-declare nndiary
)
219 (defvoo nndiary-directory
(nnheader-concat gnus-directory
"diary/")
220 "Spool directory for the nndiary back end.")
222 (defvoo nndiary-active-file
223 (expand-file-name "active" nndiary-directory
)
224 "Active file for the nndiary back end.")
226 (defvoo nndiary-newsgroups-file
227 (expand-file-name "newsgroups" nndiary-directory
)
228 "Newsgroups description file for the nndiary back end.")
230 (defvoo nndiary-get-new-mail nil
231 "Whether nndiary gets new mail and split it.
232 Contrary to traditional mail back ends, this variable can be set to t
233 even if your primary mail back end also retrieves mail. In such a case,
234 NDiary uses its own mail-sources and split-methods.")
236 (defvoo nndiary-nov-is-evil nil
237 "If non-nil, Gnus will never use nov databases for nndiary groups.
238 Using nov databases will speed up header fetching considerably.
239 This variable shouldn't be flipped much. If you have, for some reason,
240 set this to t, and want to set it to nil again, you should always run
241 the `nndiary-generate-nov-databases' command. The function will go
242 through all nnml directories and generate nov databases for them
243 all. This may very well take some time.")
245 (defvoo nndiary-prepare-save-mail-hook nil
246 "*Hook run narrowed to an article before saving.")
248 (defvoo nndiary-inhibit-expiry nil
249 "If non-nil, inhibit expiry.")
253 (defconst nndiary-version
"0.2-b14"
254 "Current Diary back end version.")
256 (defun nndiary-version ()
257 "Current Diary back end version."
259 (message "NNDiary version %s" nndiary-version
))
261 (defvoo nndiary-nov-file-name
".overview")
263 (defvoo nndiary-current-directory nil
)
264 (defvoo nndiary-current-group nil
)
265 (defvoo nndiary-status-string
"" )
266 (defvoo nndiary-nov-buffer-alist nil
)
267 (defvoo nndiary-group-alist nil
)
268 (defvoo nndiary-active-timestamp nil
)
269 (defvoo nndiary-article-file-alist nil
)
271 (defvoo nndiary-generate-active-function
'nndiary-generate-active-info
)
272 (defvoo nndiary-nov-buffer-file-name nil
)
273 (defvoo nndiary-file-coding-system nnmail-file-coding-system
)
275 (defconst nndiary-headers
282 ("Time-Zone" (("Y" -
43200)
359 ;; List of NNDiary headers that specify the time spec. Each header name is
360 ;; followed by either two integers (specifying a range of possible values
361 ;; for this header) or one list (specifying all the possible values for this
362 ;; header). In the latter case, the list does NOT include the unspecified
364 ;; For time zone values, we have symbolic time zone names associated with
365 ;; the (relative) number of seconds ahead GMT.
368 (defsubst nndiary-schedule
()
373 (setq head
(nth 0 elt
))
374 (nndiary-parse-schedule (nth 0 elt
) (nth 1 elt
) (nth 2 elt
)))
377 (nnheader-report 'nndiary
"X-Diary-%s header parse error: %s."
382 ;;; Interface functions =====================================================
384 (nnoo-define-basics nndiary
)
386 (deffoo nndiary-retrieve-headers
(sequence &optional group server fetch-old
)
387 (when (nndiary-possibly-change-directory group server
)
388 (with-current-buffer nntp-server-buffer
391 (number (length sequence
))
393 (file-name-coding-system nnmail-pathname-coding-system
)
395 (nndiary-check-directory-twice
396 (and nndiary-check-directory-twice
397 ;; To speed up, disable it in some case.
398 (or (not (numberp nnmail-large-newsgroup
))
399 (<= number nnmail-large-newsgroup
)))))
400 (if (stringp (car sequence
))
402 (if (nndiary-retrieve-headers-with-nov sequence fetch-old
)
405 (setq article
(car sequence
))
406 (setq file
(nndiary-article-to-file article
))
409 (not (file-directory-p file
)))
410 (insert (format "221 %d Article retrieved.\n" article
))
412 (nnheader-insert-head file
)
414 (if (search-forward "\n\n" nil t
)
416 (goto-char (point-max))
419 (delete-region (point) (point-max)))
420 (setq sequence
(cdr sequence
))
421 (setq count
(1+ count
))
422 (and (numberp nnmail-large-newsgroup
)
423 (> number nnmail-large-newsgroup
)
425 (nnheader-message 6 "nndiary: Receiving headers... %d%%"
426 (/ (* count
100) number
))))
428 (and (numberp nnmail-large-newsgroup
)
429 (> number nnmail-large-newsgroup
)
430 (nnheader-message 6 "nndiary: Receiving headers...done"))
432 (nnheader-fold-continuation-lines)
435 (deffoo nndiary-open-server
(server &optional defs
)
436 (nnoo-change-server 'nndiary server defs
)
437 (when (not (file-exists-p nndiary-directory
))
438 (ignore-errors (make-directory nndiary-directory t
)))
440 ((not (file-exists-p nndiary-directory
))
441 (nndiary-close-server)
442 (nnheader-report 'nndiary
"Couldn't create directory: %s"
444 ((not (file-directory-p (file-truename nndiary-directory
)))
445 (nndiary-close-server)
446 (nnheader-report 'nndiary
"Not a directory: %s" nndiary-directory
))
448 (nnheader-report 'nndiary
"Opened server %s using directory %s"
449 server nndiary-directory
)
452 (deffoo nndiary-request-regenerate
(server)
453 (nndiary-possibly-change-directory nil server
)
454 (nndiary-generate-nov-databases server
)
457 (deffoo nndiary-request-article
(id &optional group server buffer
)
458 (nndiary-possibly-change-directory group server
)
459 (let* ((nntp-server-buffer (or buffer nntp-server-buffer
))
460 (file-name-coding-system nnmail-pathname-coding-system
)
461 path gpath group-num
)
463 (when (and (setq group-num
(nndiary-find-group-number id
))
465 (assq (cdr group-num
)
466 (nnheader-article-to-file-alist
468 (nnmail-group-pathname
470 nndiary-directory
))))))
471 (setq path
(concat gpath
(int-to-string (cdr group-num
)))))
472 (setq path
(nndiary-article-to-file id
)))
475 (nnheader-report 'nndiary
"No such article: %s" id
))
476 ((not (file-exists-p path
))
477 (nnheader-report 'nndiary
"No such file: %s" path
))
478 ((file-directory-p path
)
479 (nnheader-report 'nndiary
"File is a directory: %s" path
))
480 ((not (save-excursion (let ((nnmail-file-coding-system
481 nndiary-file-coding-system
))
482 (nnmail-find-file path
))))
483 (nnheader-report 'nndiary
"Couldn't read file: %s" path
))
485 (nnheader-report 'nndiary
"Article %s retrieved" id
)
486 ;; We return the article number.
487 (cons (if group-num
(car group-num
) group
)
488 (string-to-number (file-name-nondirectory path
)))))))
490 (deffoo nndiary-request-group
(group &optional server dont-check info
)
491 (let ((file-name-coding-system nnmail-pathname-coding-system
))
493 ((not (nndiary-possibly-change-directory group server
))
494 (nnheader-report 'nndiary
"Invalid group (no such directory)"))
495 ((not (file-exists-p nndiary-current-directory
))
496 (nnheader-report 'nndiary
"Directory %s does not exist"
497 nndiary-current-directory
))
498 ((not (file-directory-p nndiary-current-directory
))
499 (nnheader-report 'nndiary
"%s is not a directory"
500 nndiary-current-directory
))
502 (nnheader-report 'nndiary
"Group %s selected" group
)
505 (nnheader-re-read-dir nndiary-current-directory
)
506 (nnmail-activate 'nndiary
)
507 (let ((active (nth 1 (assoc group nndiary-group-alist
))))
509 (nnheader-report 'nndiary
"No such group: %s" group
)
510 (nnheader-report 'nndiary
"Selected group %s" group
)
511 (nnheader-insert "211 %d %d %d %s\n"
512 (max (1+ (- (cdr active
) (car active
))) 0)
513 (car active
) (cdr active
) group
)))))))
515 (deffoo nndiary-request-scan
(&optional group server
)
516 ;; Use our own mail sources and split methods while Gnus doesn't let us have
517 ;; multiple back ends for retrieving mail.
518 (let ((mail-sources nndiary-mail-sources
)
519 (nnmail-split-methods nndiary-split-methods
))
520 (setq nndiary-article-file-alist nil
)
521 (nndiary-possibly-change-directory group server
)
522 (nnmail-get-new-mail 'nndiary
'nndiary-save-nov nndiary-directory group
)))
524 (deffoo nndiary-close-group
(group &optional server
)
525 (setq nndiary-article-file-alist nil
)
528 (deffoo nndiary-request-create-group
(group &optional server args
)
529 (nndiary-possibly-change-directory nil server
)
530 (nnmail-activate 'nndiary
)
532 ((assoc group nndiary-group-alist
)
534 ((and (file-exists-p (nnmail-group-pathname group nndiary-directory
))
535 (not (file-directory-p (nnmail-group-pathname
536 group nndiary-directory
))))
537 (nnheader-report 'nndiary
"%s is a file"
538 (nnmail-group-pathname group nndiary-directory
)))
541 (push (list group
(setq active
(cons 1 0)))
543 (nndiary-possibly-create-directory group
)
544 (nndiary-possibly-change-directory group server
)
545 (let ((articles (nnheader-directory-articles nndiary-current-directory
)))
547 (setcar active
(apply 'min articles
))
548 (setcdr active
(apply 'max articles
))))
549 (nnmail-save-active nndiary-group-alist nndiary-active-file
)
550 (run-hook-with-args 'nndiary-request-create-group-functions
551 (gnus-group-prefixed-name group
552 (list "nndiary" server
)))
556 (deffoo nndiary-request-list
(&optional server
)
558 (let ((nnmail-file-coding-system nnmail-active-file-coding-system
)
559 (file-name-coding-system nnmail-pathname-coding-system
))
560 (nnmail-find-file nndiary-active-file
))
561 (setq nndiary-group-alist
(nnmail-get-active))
564 (deffoo nndiary-request-newgroups
(date &optional server
)
565 (nndiary-request-list server
))
567 (deffoo nndiary-request-list-newsgroups
(&optional server
)
569 (nnmail-find-file nndiary-newsgroups-file
)))
571 (deffoo nndiary-request-expire-articles
(articles group
&optional server force
)
572 (nndiary-possibly-change-directory group server
)
573 (let ((active-articles
574 (nnheader-directory-articles nndiary-current-directory
))
576 (nnmail-activate 'nndiary
)
577 ;; Articles not listed in active-articles are already gone,
578 ;; so don't try to expire them.
579 (setq articles
(gnus-intersection articles active-articles
))
581 (setq article
(nndiary-article-to-file (setq number
(pop articles
))))
582 (if (and (nndiary-deletable-article-p group number
)
583 ;; Don't use nnmail-expired-article-p. Our notion of expiration
584 ;; is a bit peculiar ...
585 (or force
(nndiary-expired-article-p article
)))
587 ;; Allow a special target group.
588 (unless (eq nnmail-expiry-target
'delete
)
590 (nndiary-request-article number group server
(current-buffer))
591 (let ((nndiary-current-directory nil
))
592 (nnmail-expiry-target-group nnmail-expiry-target group
)))
593 (nndiary-possibly-change-directory group server
))
594 (nnheader-message 5 "Deleting article %s in %s" number group
)
596 (funcall nnmail-delete-file-function article
)
597 (file-error (push number rest
)))
598 (setq active-articles
(delq number active-articles
))
599 (nndiary-nov-delete-article group number
))
601 (let ((active (nth 1 (assoc group nndiary-group-alist
))))
603 (setcar active
(or (and active-articles
604 (apply 'min active-articles
))
606 (nnmail-save-active nndiary-group-alist nndiary-active-file
))
608 (nconc rest articles
)))
610 (deffoo nndiary-request-move-article
611 (article group server accept-form
&optional last move-is-internal
)
612 (let ((buf (get-buffer-create " *nndiary move*"))
614 (nndiary-possibly-change-directory group server
)
615 (nndiary-update-file-alist)
617 (nndiary-deletable-article-p group article
)
618 (nndiary-request-article article group server
)
619 (let (nndiary-current-directory
620 nndiary-current-group
621 nndiary-article-file-alist
)
622 (with-current-buffer buf
623 (insert-buffer-substring nntp-server-buffer
)
624 (setq result
(eval accept-form
))
625 (kill-buffer (current-buffer))
628 (nndiary-possibly-change-directory group server
)
630 (funcall nnmail-delete-file-function
631 (nndiary-article-to-file article
))
633 (nndiary-nov-delete-article group article
)
636 (nnmail-save-active nndiary-group-alist nndiary-active-file
))))
639 (deffoo nndiary-request-accept-article
(group &optional server last
)
640 (nndiary-possibly-change-directory group server
)
641 (nnmail-check-syntax)
642 (run-hooks 'nndiary-request-accept-article-functions
)
643 (when (nndiary-schedule)
645 (when nnmail-cache-accepted-message-ids
646 (nnmail-cache-insert (nnmail-fetch-field "message-id")
648 (nnmail-fetch-field "subject")))
651 (nnmail-activate 'nndiary
)
653 (car (nndiary-save-mail
654 (list (cons group
(nndiary-active-number group
))))))
656 (nnmail-save-active nndiary-group-alist nndiary-active-file
)
657 (and last
(nndiary-save-nov))))
659 (nnmail-activate 'nndiary
)
660 (if (and (not (setq result
661 (nnmail-article-group 'nndiary-active-number
)))
662 (yes-or-no-p "Moved to `junk' group; delete article? "))
664 (setq result
(car (nndiary-save-mail result
))))
666 (nnmail-save-active nndiary-group-alist nndiary-active-file
)
667 (when nnmail-cache-accepted-message-ids
668 (nnmail-cache-close))
669 (nndiary-save-nov))))
673 (deffoo nndiary-request-post
(&optional server
)
674 (nnmail-do-request-post 'nndiary-request-accept-article server
))
676 (deffoo nndiary-request-replace-article
(article group buffer
)
677 (nndiary-possibly-change-directory group
)
678 (with-current-buffer buffer
679 (nndiary-possibly-create-directory group
)
680 (let ((chars (nnmail-insert-lines))
681 (art (concat (int-to-string article
) "\t"))
685 (point-min) (point-max)
686 (or (nndiary-article-to-file article
)
687 (expand-file-name (int-to-string article
)
688 nndiary-current-directory
))
689 nil
(if (nnheader-be-verbose 5) nil
'nomesg
))
691 (setq headers
(nndiary-parse-head chars article
))
692 ;; Replace the NOV line in the NOV file.
693 (with-current-buffer (nndiary-open-nov group
)
694 (goto-char (point-min))
695 (if (or (looking-at art
)
696 (search-forward (concat "\n" art
) nil t
))
697 ;; Delete the old NOV line.
698 (delete-region (progn (beginning-of-line) (point))
699 (progn (forward-line 1) (point)))
700 ;; The line isn't here, so we have to find out where
701 ;; we should insert it. (This situation should never
702 ;; occur, but one likes to make sure...)
703 (while (and (looking-at "[0-9]+\t")
706 (match-beginning 0) (match-end 0)))
708 (zerop (forward-line 1)))))
710 (nnheader-insert-nov headers
)
714 (deffoo nndiary-request-delete-group
(group &optional force server
)
715 (nndiary-possibly-change-directory group server
)
717 ;; Delete all articles in GROUP.
720 nndiary-current-directory t
721 (concat nnheader-numerical-short-files
722 "\\|" (regexp-quote nndiary-nov-file-name
) "$")))
725 (setq article
(pop articles
))
726 (when (file-writable-p article
)
727 (nnheader-message 5 "Deleting article %s in %s..." article group
)
728 (funcall nnmail-delete-file-function article
))))
729 ;; Try to delete the directory itself.
730 (ignore-errors (delete-directory nndiary-current-directory
)))
731 ;; Remove the group from all structures.
732 (setq nndiary-group-alist
733 (delq (assoc group nndiary-group-alist
) nndiary-group-alist
)
734 nndiary-current-group nil
735 nndiary-current-directory nil
)
736 ;; Save the active file.
737 (nnmail-save-active nndiary-group-alist nndiary-active-file
)
740 (deffoo nndiary-request-rename-group
(group new-name
&optional server
)
741 (nndiary-possibly-change-directory group server
)
742 (let ((new-dir (nnmail-group-pathname new-name nndiary-directory
))
743 (old-dir (nnmail-group-pathname group nndiary-directory
)))
745 (make-directory new-dir t
)
747 ;; We move the articles file by file instead of renaming
748 ;; the directory -- there may be subgroups in this group.
749 ;; One might be more clever, I guess.
750 (let ((files (nnheader-article-to-file-alist old-dir
)))
753 (concat old-dir
(cdar files
))
754 (concat new-dir
(cdar files
)))
756 ;; Move .overview file.
757 (let ((overview (concat old-dir nndiary-nov-file-name
)))
758 (when (file-exists-p overview
)
759 (rename-file overview
(concat new-dir nndiary-nov-file-name
))))
760 (when (<= (length (directory-files old-dir
)) 2)
761 (ignore-errors (delete-directory old-dir
)))
762 ;; That went ok, so we change the internal structures.
763 (let ((entry (assoc group nndiary-group-alist
)))
765 (setcar entry new-name
))
766 (setq nndiary-current-directory nil
767 nndiary-current-group nil
)
768 ;; Save the new group alist.
769 (nnmail-save-active nndiary-group-alist nndiary-active-file
)
772 (deffoo nndiary-set-status
(article name value
&optional group server
)
773 (nndiary-possibly-change-directory group server
)
774 (let ((file (nndiary-article-to-file article
)))
776 ((not (file-exists-p file
))
777 (nnheader-report 'nndiary
"File %s does not exist" file
))
780 (nnheader-insert-file-contents file
)
781 (nnmail-replace-status name value
))
785 ;;; Interface optional functions ============================================
787 (deffoo nndiary-request-update-info
(group info
&optional server
)
788 (nndiary-possibly-change-directory group
)
789 (let ((timestamp (gnus-group-parameter-value (gnus-info-params info
)
792 (nnheader-report 'nndiary
"Group %s doesn't have a timestamp" group
)
794 ;; Figure out which articles should be re-new'ed
795 (let ((articles (nndiary-flatten (gnus-info-read info
) 0))
796 article file unread buf
)
798 (setq buf
(nnheader-set-temp-buffer " *nndiary update*"))
799 (while (setq article
(pop articles
))
800 (setq file
(concat nndiary-current-directory
801 (int-to-string article
)))
802 (and (file-exists-p file
)
803 (nndiary-renew-article-p file timestamp
)
804 (push article unread
)))
805 ;;(message "unread: %s" unread)
808 (setq unread
(sort unread
'<))
810 (gnus-info-set-read info
(gnus-update-read-articles
811 (gnus-info-group info
) unread t
)))
813 (run-hook-with-args 'nndiary-request-update-info-functions
814 (gnus-info-group info
))
819 ;;; Internal functions ======================================================
821 (defun nndiary-article-to-file (article)
822 (nndiary-update-file-alist)
824 (if (setq file
(cdr (assq article nndiary-article-file-alist
)))
825 (expand-file-name file nndiary-current-directory
)
826 ;; Just to make sure nothing went wrong when reading over NFS --
828 (if nndiary-check-directory-twice
830 (setq file
(expand-file-name (number-to-string article
)
831 nndiary-current-directory
)))
832 (nndiary-update-file-alist t
)
835 (defun nndiary-deletable-article-p (group article
)
836 "Say whether ARTICLE in GROUP can be deleted."
838 (when (setq path
(nndiary-article-to-file article
))
839 (when (file-writable-p path
)
840 (or (not nnmail-keep-last-article
)
841 (not (eq (cdr (nth 1 (assoc group nndiary-group-alist
)))
844 ;; Find an article number in the current group given the Message-ID.
845 (defun nndiary-find-group-number (id)
846 (with-current-buffer (get-buffer-create " *nndiary id*")
847 (let ((alist nndiary-group-alist
)
849 ;; We want to look through all .overview files, but we want to
850 ;; start with the one in the current directory. It seems most
851 ;; likely that the article we are looking for is in that group.
852 (if (setq number
(nndiary-find-id nndiary-current-group id
))
853 (cons nndiary-current-group number
)
854 ;; It wasn't there, so we look through the other groups as well.
855 (while (and (not number
)
857 (or (string= (caar alist
) nndiary-current-group
)
858 (setq number
(nndiary-find-id (caar alist
) id
)))
860 (setq alist
(cdr alist
))))
862 (cons (caar alist
) number
))))))
864 (defun nndiary-find-id (group id
)
866 (let ((nov (expand-file-name nndiary-nov-file-name
867 (nnmail-group-pathname group
870 (when (file-exists-p nov
)
871 (nnheader-insert-file-contents nov
)
872 (while (and (not found
)
873 (search-forward id nil t
)) ; We find the ID.
874 ;; And the id is in the fourth field.
875 (if (not (and (search-backward "\t" nil t
4)
876 (not (search-backward"\t" (point-at-bol) t
))))
880 ;; We return the article number.
882 (ignore-errors (read (current-buffer))))))
885 (defun nndiary-retrieve-headers-with-nov (articles &optional fetch-old
)
886 (if (or gnus-nov-is-evil nndiary-nov-is-evil
)
888 (let ((nov (expand-file-name nndiary-nov-file-name
889 nndiary-current-directory
)))
890 (when (file-exists-p nov
)
891 (with-current-buffer nntp-server-buffer
893 (nnheader-insert-file-contents nov
)
895 (not (numberp fetch-old
)))
896 t
; Don't remove anything.
897 (nnheader-nov-delete-outside-range
898 (if fetch-old
(max 1 (- (car articles
) fetch-old
))
900 (car (last articles
)))
903 (defun nndiary-possibly-change-directory (group &optional server
)
905 (not (nndiary-server-opened server
)))
906 (nndiary-open-server server
))
909 (let ((pathname (nnmail-group-pathname group nndiary-directory
))
910 (file-name-coding-system nnmail-pathname-coding-system
))
911 (when (not (equal pathname nndiary-current-directory
))
912 (setq nndiary-current-directory pathname
913 nndiary-current-group group
914 nndiary-article-file-alist nil
))
915 (file-exists-p nndiary-current-directory
))))
917 (defun nndiary-possibly-create-directory (group)
918 (let ((dir (nnmail-group-pathname group nndiary-directory
)))
919 (unless (file-exists-p dir
)
920 (make-directory (directory-file-name dir
) t
)
921 (nnheader-message 5 "Creating mail directory %s" dir
))))
923 (defun nndiary-save-mail (group-art)
924 "Called narrowed to an article."
926 (setq chars
(nnmail-insert-lines))
927 (nnmail-insert-xref group-art
)
928 (run-hooks 'nnmail-prepare-save-mail-hook
)
929 (run-hooks 'nndiary-prepare-save-mail-hook
)
930 (goto-char (point-min))
931 (while (looking-at "From ")
932 (replace-match "X-From-Line: ")
934 ;; We save the article in all the groups it belongs in.
938 (nndiary-possibly-create-directory (caar ga
))
939 (let ((file (concat (nnmail-group-pathname
940 (caar ga
) nndiary-directory
)
941 (int-to-string (cdar ga
)))))
943 ;; It was already saved, so we just make a hard link.
944 (funcall nnmail-crosspost-link-function first file t
)
946 (nnmail-write-region (point-min) (point-max) file nil
947 (if (nnheader-be-verbose 5) nil
'nomesg
))
950 ;; Generate a nov line for this article. We generate the nov
951 ;; line after saving, because nov generation destroys the
953 (setq headers
(nndiary-parse-head chars
))
954 ;; Output the nov line to all nov databases that should have it.
955 (let ((ga group-art
))
957 (nndiary-add-nov (caar ga
) (cdar ga
) headers
)
961 (defun nndiary-active-number (group)
962 "Compute the next article number in GROUP."
963 (let ((active (cadr (assoc group nndiary-group-alist
))))
964 ;; The group wasn't known to nndiary, so we just create an active
967 ;; Perhaps the active file was corrupt? See whether
968 ;; there are any articles in this group.
969 (nndiary-possibly-create-directory group
)
970 (nndiary-possibly-change-directory group
)
971 (unless nndiary-article-file-alist
972 (setq nndiary-article-file-alist
974 (nnheader-article-to-file-alist nndiary-current-directory
)
975 'car-less-than-car
)))
977 (if nndiary-article-file-alist
978 (cons (caar nndiary-article-file-alist
)
979 (caar (last nndiary-article-file-alist
)))
981 (push (list group active
) nndiary-group-alist
))
982 (setcdr active
(1+ (cdr active
)))
983 (while (file-exists-p
984 (expand-file-name (int-to-string (cdr active
))
985 (nnmail-group-pathname group nndiary-directory
)))
986 (setcdr active
(1+ (cdr active
))))
989 (defun nndiary-add-nov (group article headers
)
990 "Add a nov line for the GROUP base."
991 (with-current-buffer (nndiary-open-nov group
)
992 (goto-char (point-max))
993 (mail-header-set-number headers article
)
994 (nnheader-insert-nov headers
)))
996 (defsubst nndiary-header-value
()
997 (buffer-substring (match-end 0) (progn (end-of-line) (point))))
999 (defun nndiary-parse-head (chars &optional number
)
1000 "Parse the head of the current buffer."
1003 (unless (zerop (buffer-size))
1005 (goto-char (point-min))
1006 (if (search-forward "\n\n" nil t
) (1- (point)) (point-max))))
1007 (let ((headers (nnheader-parse-naked-head)))
1008 (mail-header-set-chars headers chars
)
1009 (mail-header-set-number headers number
)
1012 (defun nndiary-open-nov (group)
1013 (or (cdr (assoc group nndiary-nov-buffer-alist
))
1014 (let ((buffer (get-buffer-create (format " *nndiary overview %s*"
1016 (with-current-buffer buffer
1017 (set (make-local-variable 'nndiary-nov-buffer-file-name
)
1019 nndiary-nov-file-name
1020 (nnmail-group-pathname group nndiary-directory
)))
1022 (when (file-exists-p nndiary-nov-buffer-file-name
)
1023 (nnheader-insert-file-contents nndiary-nov-buffer-file-name
)))
1024 (push (cons group buffer
) nndiary-nov-buffer-alist
)
1027 (defun nndiary-save-nov ()
1029 (while nndiary-nov-buffer-alist
1030 (when (buffer-name (cdar nndiary-nov-buffer-alist
))
1031 (set-buffer (cdar nndiary-nov-buffer-alist
))
1032 (when (buffer-modified-p)
1033 (nnmail-write-region 1 (point-max) nndiary-nov-buffer-file-name
1035 (set-buffer-modified-p nil
)
1036 (kill-buffer (current-buffer)))
1037 (setq nndiary-nov-buffer-alist
(cdr nndiary-nov-buffer-alist
)))))
1040 (defun nndiary-generate-nov-databases (&optional server
)
1041 "Generate NOV databases in all nndiary directories."
1042 (interactive (list (or (nnoo-current-server 'nndiary
) "")))
1043 ;; Read the active file to make sure we don't re-use articles
1044 ;; numbers in empty groups.
1045 (nnmail-activate 'nndiary
)
1046 (unless (nndiary-server-opened server
)
1047 (nndiary-open-server server
))
1048 (setq nndiary-directory
(expand-file-name nndiary-directory
))
1049 ;; Recurse down the directories.
1050 (nndiary-generate-nov-databases-1 nndiary-directory nil t
)
1051 ;; Save the active file.
1052 (nnmail-save-active nndiary-group-alist nndiary-active-file
))
1054 (defun nndiary-generate-nov-databases-1 (dir &optional seen no-active
)
1055 "Regenerate the NOV database in DIR."
1056 (interactive "DRegenerate NOV in: ")
1057 (setq dir
(file-name-as-directory dir
))
1058 ;; Only scan this sub-tree if we haven't been here yet.
1059 (unless (member (file-truename dir
) seen
)
1060 (push (file-truename dir
) seen
)
1061 ;; We descend recursively
1062 (let ((dirs (directory-files dir t nil t
))
1064 (while (setq dir
(pop dirs
))
1065 (when (and (not (string-match "^\\." (file-name-nondirectory dir
)))
1066 (file-directory-p dir
))
1067 (nndiary-generate-nov-databases-1 dir seen
))))
1068 ;; Do this directory.
1069 (let ((nndiary-files (sort (nnheader-article-to-file-alist dir
)
1070 'car-less-than-car
)))
1071 (if (not nndiary-files
)
1072 (let* ((group (nnheader-file-to-group
1073 (directory-file-name dir
) nndiary-directory
))
1074 (info (cadr (assoc group nndiary-group-alist
))))
1076 (setcar info
(1+ (cdr info
)))))
1077 (funcall nndiary-generate-active-function dir
)
1078 ;; Generate the nov file.
1079 (nndiary-generate-nov-file dir nndiary-files
)
1081 (nnmail-save-active nndiary-group-alist nndiary-active-file
))))))
1083 (defvar nndiary-files
) ; dynamically bound in nndiary-generate-nov-databases-1
1084 (defun nndiary-generate-active-info (dir)
1085 ;; Update the active info for this group.
1086 (let* ((group (nnheader-file-to-group
1087 (directory-file-name dir
) nndiary-directory
))
1088 (entry (assoc group nndiary-group-alist
))
1089 (last (or (caadr entry
) 0)))
1090 (setq nndiary-group-alist
(delq entry nndiary-group-alist
))
1092 (cons (or (caar nndiary-files
) (1+ last
))
1094 (or (caar (last nndiary-files
))
1096 nndiary-group-alist
)))
1098 (defun nndiary-generate-nov-file (dir files
)
1099 (let* ((dir (file-name-as-directory dir
))
1100 (nov (concat dir nndiary-nov-file-name
))
1101 (nov-buffer (get-buffer-create " *nov*"))
1103 ;; Init the nov buffer.
1104 (with-current-buffer nov-buffer
1105 (buffer-disable-undo)
1107 (set-buffer nntp-server-buffer
)
1108 ;; Delete the old NOV file.
1109 (when (file-exists-p nov
)
1110 (funcall nnmail-delete-file-function nov
))
1112 (unless (file-directory-p (setq file
(concat dir
(cdar files
))))
1114 (nnheader-insert-file-contents file
)
1116 (goto-char (point-min))
1118 (search-forward "\n\n" nil t
)
1119 (setq chars
(- (point-max) (point)))
1120 (max 1 (1- (point)))))
1121 (unless (zerop (buffer-size))
1122 (goto-char (point-min))
1123 (setq headers
(nndiary-parse-head chars
(caar files
)))
1124 (with-current-buffer nov-buffer
1125 (goto-char (point-max))
1126 (nnheader-insert-nov headers
)))
1128 (setq files
(cdr files
)))
1129 (with-current-buffer nov-buffer
1130 (nnmail-write-region 1 (point-max) nov nil
'nomesg
)
1131 (kill-buffer (current-buffer))))))
1133 (defun nndiary-nov-delete-article (group article
)
1134 (with-current-buffer (nndiary-open-nov group
)
1135 (when (nnheader-find-nov-line article
)
1136 (delete-region (point) (progn (forward-line 1) (point)))
1138 (let ((active (cadr (assoc group nndiary-group-alist
)))
1142 (setf (car active
) (1+ (cdr active
)))
1143 (when (and (setq num
(ignore-errors (read (current-buffer))))
1145 (setf (car active
) num
)))))))
1148 (defun nndiary-update-file-alist (&optional force
)
1149 (when (or (not nndiary-article-file-alist
)
1151 (setq nndiary-article-file-alist
1152 (nnheader-article-to-file-alist nndiary-current-directory
))))
1155 (defun nndiary-string-to-number (str min
&optional max
)
1156 ;; Like `string-to-number' but barf if STR is not exactly an integer, and not
1157 ;; within the specified bounds.
1158 ;; Signals are caught by `nndiary-schedule'.
1159 (if (not (string-match "^[ \t]*[0-9]+[ \t]*$" str
))
1160 (nndiary-error "not an integer value")
1162 (let ((val (string-to-number str
)))
1163 (and (or (< val min
)
1164 (and max
(> val max
)))
1165 (nndiary-error "value out of range"))
1168 (defun nndiary-parse-schedule-value (str min-or-values max
)
1169 ;; Parse the schedule string STR, or signal an error.
1170 ;; Signals are caught by `nndiary-schedule'.
1171 (if (string-match "[ \t]*\\*[ \t]*" str
)
1175 (if (listp min-or-values
)
1176 ;; min-or-values is values
1177 ;; #### NOTE: this is actually only a hack for time zones.
1178 (let ((val (and (string-match "[ \t]*\\([^ \t]+\\)[ \t]*" str
)
1179 (match-string 1 str
))))
1180 (if (and val
(setq val
(assoc val min-or-values
)))
1182 (nndiary-error "invalid syntax")))
1183 ;; min-or-values is min
1186 (let ((res (split-string val
"-")))
1189 (nndiary-string-to-number (car res
) min-or-values max
))
1191 ;; don't know if crontab accepts this, but ensure
1192 ;; that BEG is <= END
1193 (let ((beg (nndiary-string-to-number (car res
) min-or-values max
))
1194 (end (nndiary-string-to-number (cadr res
) min-or-values max
)))
1202 (nndiary-error "invalid syntax")))
1204 (split-string str
",")))
1207 ;; ### FIXME: remove this function if it's used only once.
1208 (defun nndiary-parse-schedule (head min-or-values max
)
1209 ;; Parse the cron-like value of header X-Diary-HEAD in current buffer.
1210 ;; - Returns nil if `*'
1211 ;; - Otherwise returns a list of integers and/or ranges (BEG . END)
1212 ;; The exception is the Timze-Zone value which is always of the form (STR).
1213 ;; Signals are caught by `nndiary-schedule'.
1214 (let ((header (format "^X-Diary-%s: \\(.*\\)$" head
)))
1215 (goto-char (point-min))
1216 (if (not (re-search-forward header nil t
))
1217 (nndiary-error "header missing")
1219 (nndiary-parse-schedule-value (match-string 1) min-or-values max
))
1222 (defun nndiary-max (spec)
1223 ;; Returns the max of specification SPEC, or nil for permanent schedules.
1228 (while (setq elt
(pop elts
))
1230 (and (> elt max
) (setq max elt
))
1231 (and (> (cdr elt
) max
) (setq max
(cdr elt
)))))
1234 (defun nndiary-flatten (spec min
&optional max
)
1235 ;; flatten the spec by expanding ranges to all possible values.
1238 ;; this happens when I flatten something else than one of my
1239 ;; schedules (a list of read articles for instance).
1248 (while (setq elt
(pop elts
))
1253 (while (<= n
(cdr elt
))
1255 (setq n
(1+ n
))))))))
1258 (defun nndiary-unflatten (spec)
1259 ;; opposite of flatten: build ranges if possible
1260 (setq spec
(sort spec
'<))
1262 (while (setq min
(pop spec
))
1264 (while (and (car spec
) (= (car spec
) (1+ max
)))
1268 (setq res
(append res
(list min
)))
1269 (setq res
(append res
(list (cons min max
))))))
1272 (defun nndiary-compute-reminders (date)
1273 ;; Returns a list of times corresponding to the reminders of date DATE.
1274 ;; See the comment in `nndiary-reminders' about rounding.
1275 (let* ((reminders nndiary-reminders
)
1276 (date-elts (decode-time date
))
1277 ;; ### NOTE: out-of-range values are accepted by encode-time. This
1278 ;; makes our life easier.
1279 (monday (- (nth 3 date-elts
)
1280 (if nndiary-week-starts-on-monday
1281 (if (zerop (nth 6 date-elts
))
1283 (- (nth 6 date-elts
) 1))
1284 (nth 6 date-elts
))))
1286 ;; remove the DOW and DST entries
1287 (setcdr (nthcdr 5 date-elts
) (nthcdr 8 date-elts
))
1288 (while (setq reminder
(pop reminders
))
1290 (cond ((eq (cdr reminder
) 'minute
)
1292 (apply 'encode-time
0 (nthcdr 1 date-elts
))
1293 (seconds-to-time (* (car reminder
) 60.0))))
1294 ((eq (cdr reminder
) 'hour
)
1296 (apply 'encode-time
0 0 (nthcdr 2 date-elts
))
1297 (seconds-to-time (* (car reminder
) 3600.0))))
1298 ((eq (cdr reminder
) 'day
)
1300 (apply 'encode-time
0 0 0 (nthcdr 3 date-elts
))
1301 (seconds-to-time (* (car reminder
) 86400.0))))
1302 ((eq (cdr reminder
) 'week
)
1304 (apply 'encode-time
0 0 0 monday
(nthcdr 4 date-elts
))
1305 (seconds-to-time (* (car reminder
) 604800.0))))
1306 ((eq (cdr reminder
) 'month
)
1308 (apply 'encode-time
0 0 0 1 (nthcdr 4 date-elts
))
1309 (seconds-to-time (* (car reminder
) 18748800.0))))
1310 ((eq (cdr reminder
) 'year
)
1312 (apply 'encode-time
0 0 0 1 1 (nthcdr 5 date-elts
))
1313 (seconds-to-time (* (car reminder
) 400861056.0)))))
1315 (sort res
'time-less-p
)))
1317 ;; FIXME: "occurrence" is misspelled in this function name.
1319 (defun nndiary-last-occurence (sched)
1320 ;; Returns the last occurrence of schedule SCHED as an Emacs time struct, or
1321 ;; nil for permanent schedule or errors.
1322 (let ((minute (nndiary-max (nth 0 sched
)))
1323 (hour (nndiary-max (nth 1 sched
)))
1324 (year (nndiary-max (nth 4 sched
)))
1325 (time-zone (or (and (nth 6 sched
) (car (nth 6 sched
)))
1326 (current-time-zone))))
1328 (or minute
(setq minute
59))
1329 (or hour
(setq hour
23))
1330 ;; I'll just compute all possible values and test them by decreasing
1331 ;; order until one succeeds. This is probably quite rude, but I got
1332 ;; bored in finding a good algorithm for doing that ;-)
1333 ;; ### FIXME: remove identical entries.
1334 (let ((dom-list (nth 2 sched
))
1335 (month-list (sort (nndiary-flatten (nth 3 sched
) 1 12) '>))
1336 (year-list (sort (nndiary-flatten (nth 4 sched
) 1971) '>))
1337 (dow-list (nth 5 sched
)))
1338 ;; Special case: an asterisk in one of the days specifications means
1339 ;; that only the other should be taken into account. If both are
1340 ;; unspecified, you would get all possible days in both.
1341 (cond ((null dow-list
)
1342 ;; this gets all days if dom-list is nil
1343 (setq dom-list
(nndiary-flatten dom-list
1 31)))
1345 ;; this also gets all days if dow-list is nil
1346 (setq dow-list
(nndiary-flatten dow-list
0 6)))
1348 (setq dom-list
(nndiary-flatten dom-list
1 31))
1349 (setq dow-list
(nndiary-flatten dow-list
0 6))))
1352 (while (setq year
(pop year-list
))
1353 (let ((months month-list
)
1355 (while (setq month
(pop months
))
1356 ;; Now we must merge the Dows with the Doms. To do that, we
1357 ;; have to know which day is the 1st one for this month.
1358 ;; Maybe there's simpler, but decode-time(encode-time) will
1359 ;; give us the answer.
1360 (let ((first (nth 6 (decode-time
1361 (encode-time 0 0 0 1 month year
1363 (max (cond ((= month
2)
1364 (if (date-leap-year-p year
) 29 28))
1366 (if (zerop (% month
2)) 30 31))
1368 (if (zerop (% month
2)) 31 30))))
1372 ;; first, review the doms to see if they are valid.
1373 (while (setq day
(pop doms
))
1376 ;; second add all possible dows
1377 (while (setq day
(pop dows
))
1379 (setq day
(1+ (- day first
)))
1380 (and (< day
0) (setq day
(+ 7 day
)))
1383 (setq day
(+ 7 day
))))
1384 ;; Finally, if we have some days, they are valid
1388 (encode-time 0 minute hour
1389 (car days
) month year time-zone
)))
1391 ;; There's an upper limit, but we didn't find any last occurrence.
1392 ;; This means that the schedule is undecidable. This can happen if
1393 ;; you happen to say something like "each Feb 31 until 2038".
1395 (nnheader-report 'nndiary
"Undecidable schedule")
1399 ;; FIXME: "occurrence" is misspelled in this function name.
1401 (defun nndiary-next-occurence (sched now
)
1402 ;; Returns the next occurrence of schedule SCHED, starting from time NOW.
1403 ;; If there's no next occurrence, returns the last one (if any) which is then
1405 (let* ((today (decode-time now
))
1406 (this-minute (nth 1 today
))
1407 (this-hour (nth 2 today
))
1408 (this-day (nth 3 today
))
1409 (this-month (nth 4 today
))
1410 (this-year (nth 5 today
))
1411 (minute-list (sort (nndiary-flatten (nth 0 sched
) 0 59) '<))
1412 (hour-list (sort (nndiary-flatten (nth 1 sched
) 0 23) '<))
1413 (dom-list (nth 2 sched
))
1414 (month-list (sort (nndiary-flatten (nth 3 sched
) 1 12) '<))
1415 (years (if (nth 4 sched
)
1416 (sort (nndiary-flatten (nth 4 sched
) 1971) '<)
1418 (dow-list (nth 5 sched
))
1419 (year (1- this-year
))
1420 (time-zone (or (and (nth 6 sched
) (car (nth 6 sched
)))
1421 (current-time-zone))))
1422 ;; Special case: an asterisk in one of the days specifications means that
1423 ;; only the other should be taken into account. If both are unspecified,
1424 ;; you would get all possible days in both.
1425 (cond ((null dow-list
)
1426 ;; this gets all days if dom-list is nil
1427 (setq dom-list
(nndiary-flatten dom-list
1 31)))
1429 ;; this also gets all days if dow-list is nil
1430 (setq dow-list
(nndiary-flatten dow-list
0 6)))
1432 (setq dom-list
(nndiary-flatten dom-list
1 31))
1433 (setq dow-list
(nndiary-flatten dow-list
0 6))))
1434 ;; Remove past years.
1435 (unless (eq years t
)
1436 (while (and (car years
) (< (car years
) this-year
))
1439 ;; Because we might not be limited in years, we must guard against
1440 ;; infinite loops. Appart from cases like Feb 31, there are probably
1441 ;; other ones, (no monday XXX 2nd etc). I don't know any algorithm to
1442 ;; decide this, so I assume that if we reach 10 years later, the
1443 ;; schedule is undecidable.
1446 (while (if (eq years t
)
1447 (and (setq year
(1+ year
))
1448 (<= year
(+ 10 this-year
)))
1449 (setq year
(pop years
)))
1450 (let ((months month-list
)
1452 ;; Remove past months for this year.
1453 (and (= year this-year
)
1454 (while (and (car months
) (< (car months
) this-month
))
1456 (while (setq month
(pop months
))
1457 ;; Now we must merge the Dows with the Doms. To do that, we
1458 ;; have to know which day is the 1st one for this month.
1459 ;; Maybe there's simpler, but decode-time(encode-time) will
1460 ;; give us the answer.
1461 (let ((first (nth 6 (decode-time
1462 (encode-time 0 0 0 1 month year
1464 (max (cond ((= month
2)
1465 (if (date-leap-year-p year
) 29 28))
1467 (if (zerop (% month
2)) 30 31))
1469 (if (zerop (% month
2)) 31 30))))
1473 ;; first, review the doms to see if they are valid.
1474 (while (setq day
(pop doms
))
1477 ;; second add all possible dows
1478 (while (setq day
(pop dows
))
1480 (setq day
(1+ (- day first
)))
1481 (and (< day
0) (setq day
(+ 7 day
)))
1484 (setq day
(+ 7 day
))))
1485 ;; Aaaaaaall right. Now we have a valid list of DAYS for
1486 ;; this month and this year.
1488 (setq days
(sort days
'<))
1489 ;; Remove past days for this year and this month.
1490 (and (= year this-year
)
1491 (= month this-month
)
1492 (while (and (car days
) (< (car days
) this-day
))
1494 (while (setq day
(pop days
))
1495 (let ((hours hour-list
)
1497 ;; Remove past hours for this year, this month and
1499 (and (= year this-year
)
1500 (= month this-month
)
1502 (while (and (car hours
)
1503 (< (car hours
) this-hour
))
1505 (while (setq hour
(pop hours
))
1506 (let ((minutes minute-list
)
1508 ;; Remove past hours for this year, this month,
1509 ;; this day and this hour.
1510 (and (= year this-year
)
1511 (= month this-month
)
1514 (while (and (car minutes
)
1515 (< (car minutes
) this-minute
))
1517 (while (setq minute
(pop minutes
))
1518 ;; Ouch! Here, we've got a complete valid
1519 ;; schedule. It's a good one if it's in the
1521 (let ((time (encode-time 0 minute hour day
1524 (and (time-less-p now time
)
1525 (throw 'found time
)))
1530 (nndiary-last-occurence sched
))
1532 (nndiary-last-occurence sched
))
1535 (defun nndiary-expired-article-p (file)
1537 (if (nnheader-insert-head file
)
1538 (let ((sched (nndiary-schedule)))
1539 ;; An article has expired if its last schedule (if any) is in the
1540 ;; past. A permanent schedule never expires.
1542 (setq sched
(nndiary-last-occurence sched
))
1543 (time-less-p sched
(current-time))))
1545 (nnheader-report 'nndiary
"Could not read file %s" file
)
1549 (defun nndiary-renew-article-p (file timestamp
)
1551 (if (nnheader-insert-head file
)
1552 (let ((now (current-time))
1553 (sched (nndiary-schedule)))
1554 ;; The article should be re-considered as unread if there's a reminder
1555 ;; between the group timestamp and the current time.
1556 (when (and sched
(setq sched
(nndiary-next-occurence sched now
)))
1557 (let ((reminders ;; add the next occurrence itself at the end.
1558 (append (nndiary-compute-reminders sched
) (list sched
))))
1559 (while (and reminders
(time-less-p (car reminders
) timestamp
))
1561 ;; The reminders might be empty if the last date is in the past,
1562 ;; or we've got at least the next occurrence itself left. All past
1563 ;; dates are renewed.
1565 (time-less-p (car reminders
) now
)))
1568 (nnheader-report 'nndiary
"Could not read file %s" file
)
1571 ;; The end... ===============================================================
1573 (dolist (header nndiary-headers
)
1574 (setq header
(intern (format "X-Diary-%s" (car header
))))
1575 ;; Required for building NOV databases and some other stuff.
1576 (add-to-list 'gnus-extra-headers header
)
1577 (add-to-list 'nnmail-extra-headers header
))
1579 (unless (assoc "nndiary" gnus-valid-select-methods
)
1580 (gnus-declare-backend "nndiary" 'post-mail
'respool
'address
))
1584 ;;; nndiary.el ends here