2003-02-24 Reiner Steib <Reiner.Steib@gmx.de>
[gnus] / lisp / gnus-start.el
1 ;;; gnus-start.el --- startup functions for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (require 'gnus)
30 (require 'gnus-win)
31 (require 'gnus-int)
32 (require 'gnus-spec)
33 (require 'gnus-range)
34 (require 'gnus-util)
35 (require 'message)
36 (eval-when-compile (require 'cl))
37
38 (defcustom gnus-startup-file (nnheader-concat gnus-home-directory ".newsrc")
39   "Your `.newsrc' file.
40 `.newsrc-SERVER' will be used instead if that exists."
41   :group 'gnus-start
42   :type 'file)
43
44 (defcustom gnus-backup-startup-file 'never
45   "Whether to create backup files.
46 This variable takes the same values as the `version-control'
47 variable."
48   :group 'gnus-start
49   :type '(choice (const :tag "Never" never)
50                  (const :tag "If existing" nil)
51                  (other :tag "Always" t)))
52
53 (defcustom gnus-init-file (nnheader-concat gnus-home-directory ".gnus")
54   "Your Gnus Emacs-Lisp startup file name.
55 If a file with the `.el' or `.elc' suffixes exists, it will be read instead."
56   :group 'gnus-start
57   :type 'file)
58
59 (defcustom gnus-site-init-file
60   (condition-case nil
61       (concat (file-name-directory
62                (directory-file-name installation-directory))
63               "site-lisp/gnus-init")
64     (error nil))
65   "The site-wide Gnus Emacs-Lisp startup file name, or nil if none.
66 If a file with the `.el' or `.elc' suffixes exists, it will be read instead."
67   :group 'gnus-start
68   :type '(choice file (const nil)))
69
70 (defcustom gnus-default-subscribed-newsgroups nil
71   "List of newsgroups to subscribe, when a user runs Gnus the first time.
72 The value should be a list of strings.
73 If it is t, Gnus will not do anything special the first time it is
74 started; it'll just use the normal newsgroups subscription methods."
75   :group 'gnus-start
76   :type '(choice (repeat string) (const :tag "Nothing special" t)))
77
78 (defcustom gnus-use-dribble-file t
79   "*Non-nil means that Gnus will use a dribble file to store user updates.
80 If Emacs should crash without saving the .newsrc files, complete
81 information can be restored from the dribble file."
82   :group 'gnus-dribble-file
83   :type 'boolean)
84
85 (defcustom gnus-dribble-directory nil
86   "*The directory where dribble files will be saved.
87 If this variable is nil, the directory where the .newsrc files are
88 saved will be used."
89   :group 'gnus-dribble-file
90   :type '(choice directory (const nil)))
91
92 (defcustom gnus-check-new-newsgroups 'ask-server
93   "*Non-nil means that Gnus will run `gnus-find-new-newsgroups' at startup.
94 This normally finds new newsgroups by comparing the active groups the
95 servers have already reported with those Gnus already knows, either alive
96 or killed.
97
98 When any of the following are true, `gnus-find-new-newsgroups' will instead
99 ask the servers (primary, secondary, and archive servers) to list new
100 groups since the last time it checked:
101   1. This variable is `ask-server'.
102   2. This variable is a list of select methods (see below).
103   3. `gnus-read-active-file' is nil or `some'.
104   4. A prefix argument is given to `gnus-find-new-newsgroups' interactively.
105
106 Thus, if this variable is `ask-server' or a list of select methods or
107 `gnus-read-active-file' is nil or `some', then the killed list is no
108 longer necessary, so you could safely set `gnus-save-killed-list' to nil.
109
110 This variable can be a list of select methods which Gnus will query with
111 the `ask-server' method in addition to the primary, secondary, and archive
112 servers.
113
114 Eg.
115   (setq gnus-check-new-newsgroups
116         '((nntp \"some.server\") (nntp \"other.server\")))
117
118 If this variable is nil, then you have to tell Gnus explicitly to
119 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups]."
120   :group 'gnus-start
121   :type '(choice (const :tag "no" nil)
122                  (const :tag "by brute force" t)
123                  (const :tag "ask servers" ask-server)
124                  (repeat :menu-tag "ask additional servers"
125                          :tag "ask additional servers"
126                          :value ((nntp ""))
127                          (sexp :format "%v"))))
128
129 (defcustom gnus-check-bogus-newsgroups nil
130   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
131 If this variable is nil, then you have to tell Gnus explicitly to
132 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups]."
133   :group 'gnus-start-server
134   :type 'boolean)
135
136 (defcustom gnus-read-active-file 'some
137   "*Non-nil means that Gnus will read the entire active file at startup.
138 If this variable is nil, Gnus will only know about the groups in your
139 `.newsrc' file.
140
141 If this variable is `some', Gnus will try to only read the relevant
142 parts of the active file from the server.  Not all servers support
143 this, and it might be quite slow with other servers, but this should
144 generally be faster than both the t and nil value.
145
146 If you set this variable to nil or `some', you probably still want to
147 be told about new newsgroups that arrive.  To do that, set
148 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
149 properly with all servers."
150   :group 'gnus-start-server
151   :type '(choice (const nil)
152                  (const some)
153                  (const t)))
154
155 (defconst gnus-level-subscribed 5
156   "Groups with levels less than or equal to this variable are subscribed.")
157
158 (defconst gnus-level-unsubscribed 7
159   "Groups with levels less than or equal to this variable are unsubscribed.
160 Groups with levels less than `gnus-level-subscribed', which should be
161 less than this variable, are subscribed.")
162
163 (defconst gnus-level-zombie 8
164   "Groups with this level are zombie groups.")
165
166 (defconst gnus-level-killed 9
167   "Groups with this level are killed.")
168
169 (defcustom gnus-level-default-subscribed 3
170   "*New subscribed groups will be subscribed at this level."
171   :group 'gnus-group-levels
172   :type 'integer)
173
174 (defcustom gnus-level-default-unsubscribed 6
175   "*New unsubscribed groups will be unsubscribed at this level."
176   :group 'gnus-group-levels
177   :type 'integer)
178
179 (defcustom gnus-activate-level (1+ gnus-level-subscribed)
180   "*Groups higher than this level won't be activated on startup.
181 Setting this variable to something low might save lots of time when
182 you have many groups that you aren't interested in."
183   :group 'gnus-group-levels
184   :type 'integer)
185
186 (defcustom gnus-activate-foreign-newsgroups 4
187   "*If nil, Gnus will not check foreign newsgroups at startup.
188 If it is non-nil, it should be a number between one and nine.  Foreign
189 newsgroups that have a level lower or equal to this number will be
190 activated on startup.  For instance, if you want to active all
191 subscribed newsgroups, but not the rest, you'd set this variable to
192 `gnus-level-subscribed'.
193
194 If you subscribe to lots of newsgroups from different servers, startup
195 might take a while.  By setting this variable to nil, you'll save time,
196 but you won't be told how many unread articles there are in the
197 groups."
198   :group 'gnus-group-levels
199   :type '(choice integer
200                  (const :tag "none" nil)))
201
202 (defcustom gnus-read-newsrc-file t
203   "*Non-nil means that Gnus will read the `.newsrc' file.
204 Gnus always reads its own startup file, which is called
205 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
206 be readily understood by other newsreaders.  If you don't plan on
207 using other newsreaders, set this variable to nil to save some time on
208 entry."
209   :version "21.1"
210   :group 'gnus-newsrc
211   :type 'boolean)
212
213 (defcustom gnus-save-newsrc-file t
214   "*Non-nil means that Gnus will save the `.newsrc' file.
215 Gnus always saves its own startup file, which is called
216 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
217 be readily understood by other newsreaders.  If you don't plan on
218 using other newsreaders, set this variable to nil to save some time on
219 exit."
220   :group 'gnus-newsrc
221   :type 'boolean)
222
223 (defcustom gnus-save-killed-list t
224   "*If non-nil, save the list of killed groups to the startup file.
225 If you set this variable to nil, you'll save both time (when starting
226 and quitting) and space (both memory and disk), but it will also mean
227 that Gnus has no record of which groups are new and which are old, so
228 the automatic new newsgroups subscription methods become meaningless.
229
230 You should always set `gnus-check-new-newsgroups' to `ask-server' or
231 nil if you set this variable to nil.
232
233 This variable can also be a regexp.  In that case, all groups that do
234 not match this regexp will be removed before saving the list."
235   :group 'gnus-newsrc
236   :type 'boolean)
237
238 (defcustom gnus-ignored-newsgroups
239   (mapconcat 'identity
240              '("^to\\."                 ; not "real" groups
241                "^[0-9. \t]+\\( \\|$\\)" ; all digits in name
242                "^[\"][]\"[#'()]"        ; bogus characters
243                )
244              "\\|")
245   "*A regexp to match uninteresting newsgroups in the active file.
246 Any lines in the active file matching this regular expression are
247 removed from the newsgroup list before anything else is done to it,
248 thus making them effectively non-existent."
249   :group 'gnus-group-new
250   :type 'regexp)
251
252 (defcustom gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
253   "*Function(s) called with a group name when new group is detected.
254 A few pre-made functions are supplied: `gnus-subscribe-randomly'
255 inserts new groups at the beginning of the list of groups;
256 `gnus-subscribe-alphabetically' inserts new groups in strict
257 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
258 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
259 for your decision; `gnus-subscribe-killed' kills all new groups;
260 `gnus-subscribe-zombies' will make all new groups into zombies;
261 `gnus-subscribe-topics' will enter groups into the topics that
262 claim them."
263   :group 'gnus-group-new
264   :type '(radio (function-item gnus-subscribe-randomly)
265                 (function-item gnus-subscribe-alphabetically)
266                 (function-item gnus-subscribe-hierarchically)
267                 (function-item gnus-subscribe-interactively)
268                 (function-item gnus-subscribe-killed)
269                 (function-item gnus-subscribe-zombies)
270                 (function-item gnus-subscribe-topics)
271                 function
272                 (repeat function)))
273
274 (defcustom gnus-subscribe-newsgroup-hooks nil
275   "*Hooks run after you subscribe to a new group. The hooks will be called
276 with new group's name as argument."
277   :group 'gnus-group-new
278   :type 'hook)
279
280 (defcustom gnus-subscribe-options-newsgroup-method
281   'gnus-subscribe-alphabetically
282   "*Function(s) called to subscribe newsgroups mentioned on \"options -n\" lines.
283 If, for instance, you want to subscribe to all newsgroups in the
284 \"no\" and \"alt\" hierarchies, you'd put the following in your
285 .newsrc file:
286
287 options -n no.all alt.all
288
289 Gnus will the subscribe all new newsgroups in these hierarchies with
290 the subscription method in this variable."
291   :group 'gnus-group-new
292   :type '(radio (function-item gnus-subscribe-randomly)
293                 (function-item gnus-subscribe-alphabetically)
294                 (function-item gnus-subscribe-hierarchically)
295                 (function-item gnus-subscribe-interactively)
296                 (function-item gnus-subscribe-killed)
297                 (function-item gnus-subscribe-zombies)
298                 (function-item gnus-subscribe-topics)
299                 function
300                 (repeat function)))
301
302 (defcustom gnus-subscribe-hierarchical-interactive nil
303   "*If non-nil, Gnus will offer to subscribe hierarchically.
304 When a new hierarchy appears, Gnus will ask the user:
305
306 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
307
308 If the user pressed `d', Gnus will descend the hierarchy, `y' will
309 subscribe to all newsgroups in the hierarchy and `s' will skip this
310 hierarchy in its entirety."
311   :group 'gnus-group-new
312   :type 'boolean)
313
314 (defcustom gnus-auto-subscribed-groups
315   "^nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl\\|^nnmaildir"
316   "*All new groups that match this regexp will be subscribed automatically.
317 Note that this variable only deals with new groups.  It has no effect
318 whatsoever on old groups.
319
320 New groups that match this regexp will not be handled by
321 `gnus-subscribe-newsgroup-method'.  Instead, they will
322 be subscribed using `gnus-subscribe-options-newsgroup-method'."
323   :group 'gnus-group-new
324   :type 'regexp)
325
326 (defcustom gnus-options-subscribe nil
327   "*All new groups matching this regexp will be subscribed unconditionally.
328 Note that this variable deals only with new newsgroups.  This variable
329 does not affect old newsgroups.
330
331 New groups that match this regexp will not be handled by
332 `gnus-subscribe-newsgroup-method'.  Instead, they will
333 be subscribed using `gnus-subscribe-options-newsgroup-method'."
334   :group 'gnus-group-new
335   :type '(choice regexp
336                  (const :tag "none" nil)))
337
338 (defcustom gnus-options-not-subscribe nil
339   "*All new groups matching this regexp will be ignored.
340 Note that this variable deals only with new newsgroups.  This variable
341 does not affect old (already subscribed) newsgroups."
342   :group 'gnus-group-new
343   :type '(choice regexp
344                  (const :tag "none" nil)))
345
346 (defcustom gnus-modtime-botch nil
347   "*Non-nil means .newsrc should be deleted prior to save.
348 Its use is due to the bogus appearance that .newsrc was modified on
349 disc."
350   :group 'gnus-newsrc
351   :type 'boolean)
352
353 (defcustom gnus-check-bogus-groups-hook nil
354   "A hook run after removing bogus groups."
355   :group 'gnus-start-server
356   :type 'hook)
357
358 (defcustom gnus-startup-hook nil
359   "A hook called at startup.
360 This hook is called after Gnus is connected to the NNTP server."
361   :group 'gnus-start
362   :type 'hook)
363
364 (defcustom gnus-before-startup-hook nil
365   "A hook called at before startup.
366 This hook is called as the first thing when Gnus is started."
367   :group 'gnus-start
368   :type 'hook)
369
370 (defcustom gnus-started-hook nil
371   "A hook called as the last thing after startup."
372   :group 'gnus-start
373   :type 'hook)
374
375 (defcustom gnus-setup-news-hook 
376   '(gnus-fixup-nnimap-unread-after-getting-new-news)
377   "A hook after reading the .newsrc file, but before generating the buffer."
378   :group 'gnus-start
379   :type 'hook)
380
381 (defcustom gnus-get-new-news-hook nil
382   "A hook run just before Gnus checks for new news."
383   :group 'gnus-group-new
384   :type 'hook)
385
386 (defcustom gnus-after-getting-new-news-hook
387   '(gnus-display-time-event-handler 
388     gnus-fixup-nnimap-unread-after-getting-new-news)
389   "*A hook run after Gnus checks for new news when Gnus is already running."
390   :group 'gnus-group-new
391   :type 'hook)
392
393 (defcustom gnus-save-newsrc-hook nil
394   "A hook called before saving any of the newsrc files."
395   :group 'gnus-newsrc
396   :type 'hook)
397
398 (defcustom gnus-save-quick-newsrc-hook nil
399   "A hook called just before saving the quick newsrc file.
400 Can be used to turn version control on or off."
401   :group 'gnus-newsrc
402   :type 'hook)
403
404 (defcustom gnus-save-standard-newsrc-hook nil
405   "A hook called just before saving the standard newsrc file.
406 Can be used to turn version control on or off."
407   :group 'gnus-newsrc
408   :type 'hook)
409
410 (defcustom gnus-group-mode-hook nil
411   "Hook for Gnus group mode."
412   :group 'gnus-group-various
413   :options '(gnus-topic-mode)
414   :type 'hook)
415
416 (defcustom gnus-always-read-dribble-file nil
417   "Unconditionally read the dribble file."
418   :group 'gnus-newsrc
419   :type 'boolean)
420
421 ;;; Internal variables
422
423 (defvar gnus-ding-file-coding-system mm-universal-coding-system
424   "Coding system for ding file.")
425
426 (defvar gnus-newsrc-file-version nil)
427 (defvar gnus-override-subscribe-method nil)
428 (defvar gnus-dribble-buffer nil)
429 (defvar gnus-newsrc-options nil
430   "Options line in the .newsrc file.")
431
432 (defvar gnus-newsrc-options-n nil
433   "List of regexps representing groups to be subscribed/ignored unconditionally.")
434
435 (defvar gnus-newsrc-last-checked-date nil
436   "Date Gnus last asked server for new newsgroups.")
437
438 (defvar gnus-current-startup-file nil
439   "Startup file for the current host.")
440
441 ;; Byte-compiler warning.
442 (defvar gnus-group-line-format)
443
444 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
445 (defvar gnus-init-inhibit nil)
446 (defun gnus-read-init-file (&optional inhibit-next)
447   ;; Don't load .gnus if the -q option was used.
448   (when init-file-user
449     (if gnus-init-inhibit
450         (setq gnus-init-inhibit nil)
451       (setq gnus-init-inhibit inhibit-next)
452       (dolist (file (list gnus-site-init-file gnus-init-file))
453         (when (and file
454                    (locate-library file))
455           (if (or debug-on-error debug-on-quit)
456               (load file nil t)
457             (condition-case var
458                 (load file nil t)
459               (error
460                (error "Error in %s: %s" file (cadr var))))))))))
461
462 ;; For subscribing new newsgroup
463
464 (defun gnus-subscribe-hierarchical-interactive (groups)
465   (let ((groups (sort groups 'string<))
466         prefixes prefix start ans group starts)
467     (while groups
468       (setq prefixes (list "^"))
469       (while (and groups prefixes)
470         (while (not (string-match (car prefixes) (car groups)))
471           (setq prefixes (cdr prefixes)))
472         (setq prefix (car prefixes))
473         (setq start (1- (length prefix)))
474         (if (and (string-match "[^\\.]\\." (car groups) start)
475                  (cdr groups)
476                  (setq prefix
477                        (concat "^" (substring (car groups) 0 (match-end 0))))
478                  (string-match prefix (cadr groups)))
479             (progn
480               (push prefix prefixes)
481               (message "Descend hierarchy %s? ([y]nsq): "
482                        (substring prefix 1 (1- (length prefix))))
483               (while (not (memq (setq ans (read-char-exclusive))
484                                 '(?y ?\n ?\r ?n ?s ?q)))
485                 (ding)
486                 (message "Descend hierarchy %s? ([y]nsq): "
487                          (substring prefix 1 (1- (length prefix)))))
488               (cond ((= ans ?n)
489                      (while (and groups
490                                  (string-match prefix
491                                                (setq group (car groups))))
492                        (push group gnus-killed-list)
493                        (gnus-sethash group group gnus-killed-hashtb)
494                        (setq groups (cdr groups)))
495                      (setq starts (cdr starts)))
496                     ((= ans ?s)
497                      (while (and groups
498                                  (string-match prefix
499                                                (setq group (car groups))))
500                        (gnus-sethash group group gnus-killed-hashtb)
501                        (gnus-subscribe-alphabetically (car groups))
502                        (setq groups (cdr groups)))
503                      (setq starts (cdr starts)))
504                     ((= ans ?q)
505                      (while groups
506                        (setq group (car groups))
507                        (push group gnus-killed-list)
508                        (gnus-sethash group group gnus-killed-hashtb)
509                        (setq groups (cdr groups))))
510                     (t nil)))
511           (message "Subscribe %s? ([n]yq)" (car groups))
512           (while (not (memq (setq ans (read-char-exclusive))
513                             '(?y ?\n ?\r ?q ?n)))
514             (ding)
515             (message "Subscribe %s? ([n]yq)" (car groups)))
516           (setq group (car groups))
517           (cond ((= ans ?y)
518                  (gnus-subscribe-alphabetically (car groups))
519                  (gnus-sethash group group gnus-killed-hashtb))
520                 ((= ans ?q)
521                  (while groups
522                    (setq group (car groups))
523                    (push group gnus-killed-list)
524                    (gnus-sethash group group gnus-killed-hashtb)
525                    (setq groups (cdr groups))))
526                 (t
527                  (push group gnus-killed-list)
528                  (gnus-sethash group group gnus-killed-hashtb)))
529           (setq groups (cdr groups)))))))
530
531 (defun gnus-subscribe-randomly (newsgroup)
532   "Subscribe new NEWSGROUP by making it the first newsgroup."
533   (gnus-subscribe-newsgroup newsgroup))
534
535 (defun gnus-subscribe-alphabetically (newgroup)
536   "Subscribe new NEWSGROUP and insert it in alphabetical order."
537   (let ((groups (cdr gnus-newsrc-alist))
538         before)
539     (while (and (not before) groups)
540       (if (string< newgroup (caar groups))
541           (setq before (caar groups))
542         (setq groups (cdr groups))))
543     (gnus-subscribe-newsgroup newgroup before)))
544
545 (defun gnus-subscribe-hierarchically (newgroup)
546   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
547   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
548   (save-excursion
549     (set-buffer (nnheader-find-file-noselect gnus-current-startup-file))
550     (prog1
551         (let ((groupkey newgroup) before)
552           (while (and (not before) groupkey)
553             (goto-char (point-min))
554             (let ((groupkey-re
555                    (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
556               (while (and (re-search-forward groupkey-re nil t)
557                           (progn
558                             (setq before (match-string 1))
559                             (string< before newgroup)))))
560             ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
561             (setq groupkey
562                   (when (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
563                     (substring groupkey (match-beginning 1) (match-end 1)))))
564           (gnus-subscribe-newsgroup newgroup before))
565       (kill-buffer (current-buffer)))))
566
567 (defun gnus-subscribe-interactively (group)
568   "Subscribe the new GROUP interactively.
569 It is inserted in hierarchical newsgroup order if subscribed.  If not,
570 it is killed."
571   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
572       (gnus-subscribe-hierarchically group)
573     (push group gnus-killed-list)))
574
575 (defun gnus-subscribe-zombies (group)
576   "Make the new GROUP into a zombie group."
577   (push group gnus-zombie-list))
578
579 (defun gnus-subscribe-killed (group)
580   "Make the new GROUP a killed group."
581   (push group gnus-killed-list))
582
583 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
584   "Subscribe new NEWSGROUP.
585 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
586 the first newsgroup."
587   (save-excursion
588     (goto-char (point-min))
589     ;; We subscribe the group by changing its level to `subscribed'.
590     (gnus-group-change-level
591      newsgroup gnus-level-default-subscribed
592      gnus-level-killed (gnus-gethash (or next "dummy.group")
593                                      gnus-newsrc-hashtb))
594     (gnus-message 5 "Subscribe newsgroup: %s" newsgroup)
595     (run-hook-with-args 'gnus-subscribe-newsgroup-hooks newsgroup)
596     t))
597
598 (defun gnus-read-active-file-p ()
599   "Say whether the active file has been read from `gnus-select-method'."
600   (memq gnus-select-method gnus-have-read-active-file))
601
602 ;;; General various misc type functions.
603
604 ;; Silence byte-compiler.
605 (defvar gnus-current-headers)
606 (defvar gnus-thread-indent-array)
607 (defvar gnus-newsgroup-name)
608 (defvar gnus-newsgroup-headers)
609 (defvar gnus-group-list-mode)
610 (defvar gnus-group-mark-positions)
611 (defvar gnus-newsgroup-data)
612 (defvar gnus-newsgroup-unreads)
613 (defvar nnoo-state-alist)
614 (defvar gnus-current-select-method)
615
616 (defun gnus-close-all-servers ()
617   "Close all servers."
618   (interactive)
619   (dolist (server gnus-opened-servers)
620     (gnus-close-server (car server))))
621
622 (defun gnus-clear-system ()
623   "Clear all variables and buffers."
624   ;; Clear Gnus variables.
625   (let ((variables (remove 'gnus-format-specs gnus-variable-list)))
626     (while variables
627       (set (car variables) nil)
628       (setq variables (cdr variables))))
629   ;; Clear other internal variables.
630   (setq gnus-list-of-killed-groups nil
631         gnus-have-read-active-file nil
632         gnus-newsrc-alist nil
633         gnus-newsrc-hashtb nil
634         gnus-killed-list nil
635         gnus-zombie-list nil
636         gnus-killed-hashtb nil
637         gnus-active-hashtb nil
638         gnus-moderated-hashtb nil
639         gnus-description-hashtb nil
640         gnus-current-headers nil
641         gnus-thread-indent-array nil
642         gnus-newsgroup-headers nil
643         gnus-newsgroup-name nil
644         gnus-server-alist nil
645         gnus-group-list-mode nil
646         gnus-opened-servers nil
647         gnus-group-mark-positions nil
648         gnus-newsgroup-data nil
649         gnus-newsgroup-unreads nil
650         nnoo-state-alist nil
651         gnus-current-select-method nil
652         nnmail-split-history nil
653         gnus-ephemeral-servers nil)
654   (gnus-shutdown 'gnus)
655   ;; Kill the startup file.
656   (and gnus-current-startup-file
657        (get-file-buffer gnus-current-startup-file)
658        (kill-buffer (get-file-buffer gnus-current-startup-file)))
659   ;; Clear the dribble buffer.
660   (gnus-dribble-clear)
661   ;; Kill global KILL file buffer.
662   (when (get-file-buffer (gnus-newsgroup-kill-file nil))
663     (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
664   (gnus-kill-buffer nntp-server-buffer)
665   ;; Kill Gnus buffers.
666   (dolist (buffer (gnus-buffers))
667     (gnus-kill-buffer buffer))
668   ;; Remove Gnus frames.
669   (gnus-kill-gnus-frames))
670
671 (defun gnus-no-server-1 (&optional arg slave)
672   "Read network news.
673 If ARG is a positive number, Gnus will use that as the
674 startup level.  If ARG is nil, Gnus will be started at level 2.
675 If ARG is non-nil and not a positive number, Gnus will
676 prompt the user for the name of an NNTP server to use.
677 As opposed to `gnus', this command will not connect to the local server."
678   (interactive "P")
679   (let ((val (or arg (1- gnus-level-default-subscribed))))
680     (gnus val t slave)
681     (make-local-variable 'gnus-group-use-permanent-levels)
682     (setq gnus-group-use-permanent-levels val)))
683
684 (defun gnus-1 (&optional arg dont-connect slave)
685   "Read network news.
686 If ARG is non-nil and a positive number, Gnus will use that as the
687 startup level.  If ARG is non-nil and not a positive number, Gnus will
688 prompt the user for the name of an NNTP server to use."
689   (interactive "P")
690
691   (if (gnus-alive-p)
692       (progn
693         (switch-to-buffer gnus-group-buffer)
694         (gnus-group-get-new-news
695          (and (numberp arg)
696               (> arg 0)
697               (max (car gnus-group-list-mode) arg))))
698
699     (gnus-clear-system)
700     (gnus-splash)
701     (gnus-run-hooks 'gnus-before-startup-hook)
702     (nnheader-init-server-buffer)
703     (setq gnus-slave slave)
704     (gnus-read-init-file)
705     (if gnus-agent
706         (gnus-agentize))
707
708     (when gnus-simple-splash
709       (setq gnus-simple-splash nil)
710       (cond
711        ((featurep 'xemacs)
712         (gnus-xmas-splash))
713        ((and window-system
714              (= (frame-height) (1+ (window-height))))
715         (gnus-x-splash))))
716
717     (let ((level (and (numberp arg) (> arg 0) arg))
718           did-connect)
719       (unwind-protect
720           (progn
721             (unless dont-connect
722               (setq did-connect
723                     (gnus-start-news-server (and arg (not level))))))
724         (if (and (not dont-connect)
725                  (not did-connect))
726             (gnus-group-quit)
727           (gnus-run-hooks 'gnus-startup-hook)
728           ;; NNTP server is successfully open.
729
730           ;; Find the current startup file name.
731           (setq gnus-current-startup-file
732                 (gnus-make-newsrc-file gnus-startup-file))
733
734           ;; Read the dribble file.
735           (when (or gnus-slave gnus-use-dribble-file)
736             (gnus-dribble-read-file))
737
738           ;; Allow using GroupLens predictions.
739           (when gnus-use-grouplens
740             (bbb-login)
741             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
742
743           ;; Do the actual startup.
744           (if gnus-agent
745               (gnus-request-create-group "queue" '(nndraft "")))
746           (gnus-request-create-group "drafts" '(nndraft ""))
747           (gnus-setup-news nil level dont-connect)
748           (gnus-run-hooks 'gnus-setup-news-hook)
749           (gnus-start-draft-setup)
750           ;; Generate the group buffer.
751           (gnus-group-list-groups level)
752           (gnus-group-first-unread-group)
753           (gnus-configure-windows 'group)
754           (gnus-group-set-mode-line)
755           (gnus-run-hooks 'gnus-started-hook))))))
756
757 (defun gnus-start-draft-setup ()
758   "Make sure the draft group exists."
759   (gnus-request-create-group "drafts" '(nndraft ""))
760   (unless (gnus-gethash "nndraft:drafts" gnus-newsrc-hashtb)
761     (let ((gnus-level-default-subscribed 1))
762       (gnus-subscribe-group "nndraft:drafts" nil '(nndraft "")))
763     (gnus-group-set-parameter
764      "nndraft:drafts" 'gnus-dummy '((gnus-draft-mode)))))
765
766 ;;;###autoload
767 (defun gnus-unload ()
768   "Unload all Gnus features.
769 \(For some value of `all' or `Gnus'.)  Currently, features whose names
770 have prefixes `gnus-', `nn', `mm-' or `rfc' are unloaded.  Use
771 cautiously -- unloading may cause trouble."
772   (interactive)
773   (dolist (feature features)
774     (if (string-match "^\\(gnus-\\|nn\\|mm-\\|rfc\\)" (symbol-name feature))
775         (unload-feature feature 'force))))
776
777 \f
778 ;;;
779 ;;; Dribble file
780 ;;;
781
782 (defvar gnus-dribble-ignore nil)
783 (defvar gnus-dribble-eval-file nil)
784
785 (defun gnus-dribble-file-name ()
786   "Return the dribble file for the current .newsrc."
787   (concat
788    (if gnus-dribble-directory
789        (concat (file-name-as-directory gnus-dribble-directory)
790                (file-name-nondirectory gnus-current-startup-file))
791      gnus-current-startup-file)
792    "-dribble"))
793
794 (defun gnus-dribble-enter (string)
795   "Enter STRING into the dribble buffer."
796   (when (and (not gnus-dribble-ignore)
797              gnus-dribble-buffer
798              (buffer-name gnus-dribble-buffer))
799     (let ((obuf (current-buffer)))
800       (set-buffer gnus-dribble-buffer)
801       (goto-char (point-max))
802       (insert string "\n")
803       ;; This has been commented by Josh Huber <huber@alum.wpi.edu>
804       ;; It causes problems with both XEmacs and Emacs 21, and doesn't
805       ;; seem to be of much value. (FIXME: remove this after we make sure
806       ;; it's not needed).
807       ;; (set-window-point (get-buffer-window (current-buffer)) (point-max))
808       (bury-buffer gnus-dribble-buffer)
809       (save-excursion
810         (set-buffer gnus-group-buffer)
811         (gnus-group-set-mode-line))
812       (set-buffer obuf))))
813
814 (defun gnus-dribble-touch ()
815   "Touch the dribble buffer."
816   (gnus-dribble-enter ""))
817
818 (defun gnus-dribble-read-file ()
819   "Read the dribble file from disk."
820   (let ((dribble-file (gnus-dribble-file-name)))
821     (save-excursion
822       (set-buffer (setq gnus-dribble-buffer
823                         (gnus-get-buffer-create
824                          (file-name-nondirectory dribble-file))))
825       (erase-buffer)
826       (setq buffer-file-name dribble-file)
827       (auto-save-mode t)
828       (buffer-disable-undo)
829       (bury-buffer (current-buffer))
830       (set-buffer-modified-p nil)
831       (let ((auto (make-auto-save-file-name))
832             (gnus-dribble-ignore t)
833             (purpose nil)
834             modes)
835         (when (or (file-exists-p auto) (file-exists-p dribble-file))
836           ;; Load whichever file is newest -- the auto save file
837           ;; or the "real" file.
838           (if (file-newer-than-file-p auto dribble-file)
839               (nnheader-insert-file-contents auto)
840             (nnheader-insert-file-contents dribble-file))
841           (unless (zerop (buffer-size))
842             (set-buffer-modified-p t))
843           ;; Set the file modes to reflect the .newsrc file modes.
844           (save-buffer)
845           (when (and (file-exists-p gnus-current-startup-file)
846                      (file-exists-p dribble-file)
847                      (setq modes (file-modes gnus-current-startup-file)))
848             (set-file-modes dribble-file modes))
849           (goto-char (point-min))
850           (when (search-forward "Gnus was exited on purpose" nil t)
851             (setq purpose t))
852           ;; Possibly eval the file later.
853           (when (or gnus-always-read-dribble-file
854                     (gnus-y-or-n-p
855                      (if purpose
856                          "Gnus exited on purpose without saving; read auto-save file anyway? "
857                      "Gnus auto-save file exists.  Do you want to read it? ")))
858             (setq gnus-dribble-eval-file t)))))))
859
860 (defun gnus-dribble-eval-file ()
861   (when gnus-dribble-eval-file
862     (setq gnus-dribble-eval-file nil)
863     (save-excursion
864       (let ((gnus-dribble-ignore t))
865         (set-buffer gnus-dribble-buffer)
866         (eval-buffer (current-buffer))))))
867
868 (defun gnus-dribble-delete-file ()
869   (when (file-exists-p (gnus-dribble-file-name))
870     (delete-file (gnus-dribble-file-name)))
871   (when gnus-dribble-buffer
872     (save-excursion
873       (set-buffer gnus-dribble-buffer)
874       (let ((auto (make-auto-save-file-name)))
875         (when (file-exists-p auto)
876           (delete-file auto))
877         (erase-buffer)
878         (set-buffer-modified-p nil)))))
879
880 (defun gnus-dribble-save ()
881   (when (and gnus-dribble-buffer
882              (buffer-name gnus-dribble-buffer))
883     (save-excursion
884       (set-buffer gnus-dribble-buffer)
885       (save-buffer))))
886
887 (defun gnus-dribble-clear ()
888   (when (gnus-buffer-exists-p gnus-dribble-buffer)
889     (save-excursion
890       (set-buffer gnus-dribble-buffer)
891       (erase-buffer)
892       (set-buffer-modified-p nil)
893       (setq buffer-saved-size (buffer-size)))))
894
895 \f
896 ;;;
897 ;;; Active & Newsrc File Handling
898 ;;;
899
900 (defun gnus-setup-news (&optional rawfile level dont-connect)
901   "Setup news information.
902 If RAWFILE is non-nil, the .newsrc file will also be read.
903 If LEVEL is non-nil, the news will be set up at level LEVEL."
904   (require 'nnmail)
905   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile))))
906         ;; Binding this variable will inhibit multiple fetchings
907         ;; of the same mail source.
908         (nnmail-fetched-sources (list t)))
909
910     (when init
911       ;; Clear some variables to re-initialize news information.
912       (setq gnus-newsrc-alist nil
913             gnus-active-hashtb nil)
914       ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
915       (gnus-read-newsrc-file rawfile))
916
917     ;; Make sure the archive server is available to all and sundry.
918     (when gnus-message-archive-method
919       (unless (assoc "archive" gnus-server-alist)
920         (push `("archive"
921                 nnfolder
922                 "archive"
923                 (nnfolder-directory
924                  ,(nnheader-concat message-directory "archive"))
925                 (nnfolder-active-file
926                  ,(nnheader-concat message-directory "archive/active"))
927                 (nnfolder-get-new-mail nil)
928                 (nnfolder-inhibit-expiry t))
929               gnus-server-alist)))
930
931     ;; If we don't read the complete active file, we fill in the
932     ;; hashtb here.
933     (when (or (null gnus-read-active-file)
934               (eq gnus-read-active-file 'some))
935       (gnus-update-active-hashtb-from-killed))
936
937     ;; Read the active file and create `gnus-active-hashtb'.
938     ;; If `gnus-read-active-file' is nil, then we just create an empty
939     ;; hash table.  The partial filling out of the hash table will be
940     ;; done in `gnus-get-unread-articles'.
941     (and gnus-read-active-file
942          (not level)
943          (gnus-read-active-file nil dont-connect))
944
945     (unless gnus-active-hashtb
946       (setq gnus-active-hashtb (gnus-make-hashtable 4096)))
947
948     ;; Initialize the cache.
949     (when gnus-use-cache
950       (gnus-cache-open))
951
952     ;; Possibly eval the dribble file.
953     (and init
954          (or gnus-use-dribble-file gnus-slave)
955          (gnus-dribble-eval-file))
956
957     ;; Slave Gnusii should then clear the dribble buffer.
958     (when (and init gnus-slave)
959       (gnus-dribble-clear))
960
961     (gnus-update-format-specifications)
962
963     ;; See whether we need to read the description file.
964     (when (and (boundp 'gnus-group-line-format)
965                (stringp gnus-group-line-format)
966                (let ((case-fold-search nil))
967                  (string-match "%[-,0-9]*D" gnus-group-line-format))
968                (not gnus-description-hashtb)
969                (not dont-connect)
970                gnus-read-active-file)
971       (gnus-read-all-descriptions-files))
972
973     ;; Find new newsgroups and treat them.
974     (when (and init gnus-check-new-newsgroups (not level)
975                (gnus-check-server gnus-select-method)
976                (not gnus-slave)
977                gnus-plugged)
978       (gnus-find-new-newsgroups))
979
980     ;; Check and remove bogus newsgroups.
981     (when (and init gnus-check-bogus-newsgroups
982                gnus-read-active-file (not level)
983                (gnus-server-opened gnus-select-method))
984       (gnus-check-bogus-newsgroups))
985
986     ;; We might read in new NoCeM messages here.
987     (when (and gnus-use-nocem
988                (not level)
989                (not dont-connect))
990       (gnus-nocem-scan-groups))
991
992     ;; Read any slave files.
993     (gnus-master-read-slave-newsrc)
994
995     ;; Find the number of unread articles in each non-dead group.
996     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
997       (gnus-get-unread-articles level))))
998
999 (defun gnus-call-subscribe-functions (method group)
1000   "Call METHOD to subscribe GROUP.
1001 If no function returns `non-nil', call `gnus-subscribe-zombies'."
1002   (unless (cond
1003            ((gnus-functionp method)
1004             (funcall method group))
1005            ((listp method)
1006             (catch 'found
1007               (dolist (func method)
1008                 (if (funcall func group)
1009                     (throw 'found t)))
1010               nil))
1011            (t nil))
1012     (gnus-subscribe-zombies group)))
1013
1014 (defun gnus-find-new-newsgroups (&optional arg)
1015   "Search for new newsgroups and add them.
1016 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method'.
1017 The `-n' option line from .newsrc is respected.
1018
1019 With 1 C-u, use the `ask-server' method to query the server for new
1020 groups.
1021 With 2 C-u's, use most complete method possible to query the server
1022 for new groups, and subscribe the new groups as zombies."
1023   (interactive "p")
1024   (let* ((gnus-subscribe-newsgroup-method
1025           gnus-subscribe-newsgroup-method)
1026          (check (cond
1027                  ((or (and (= (or arg 1) 4)
1028                            (not (listp gnus-check-new-newsgroups)))
1029                       (null gnus-read-active-file)
1030                       (eq gnus-read-active-file 'some))
1031                   'ask-server)
1032                  ((= (or arg 1) 16)
1033                   (setq gnus-subscribe-newsgroup-method
1034                         'gnus-subscribe-zombies)
1035                   t)
1036                  (t gnus-check-new-newsgroups))))
1037     (unless (gnus-check-first-time-used)
1038       (if (or (consp check)
1039               (eq check 'ask-server))
1040           ;; Ask the server for new groups.
1041           (gnus-ask-server-for-new-groups)
1042         ;; Go through the active hashtb and look for new groups.
1043         (let ((groups 0)
1044               group new-newsgroups)
1045           (gnus-message 5 "Looking for new newsgroups...")
1046           (unless gnus-have-read-active-file
1047             (gnus-read-active-file))
1048           (setq gnus-newsrc-last-checked-date (message-make-date))
1049           (unless gnus-killed-hashtb
1050             (gnus-make-hashtable-from-killed))
1051           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
1052           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
1053           (mapatoms
1054            (lambda (sym)
1055              (if (or (null (setq group (symbol-name sym)))
1056                      (not (boundp sym))
1057                      (null (symbol-value sym))
1058                      (gnus-gethash group gnus-killed-hashtb)
1059                      (gnus-gethash group gnus-newsrc-hashtb))
1060                  ()
1061                (let ((do-sub (gnus-matches-options-n group)))
1062                  (cond
1063                   ((eq do-sub 'subscribe)
1064                    (setq groups (1+ groups))
1065                    (gnus-sethash group group gnus-killed-hashtb)
1066                    (gnus-call-subscribe-functions
1067                     gnus-subscribe-options-newsgroup-method group))
1068                   ((eq do-sub 'ignore)
1069                    nil)
1070                   (t
1071                    (setq groups (1+ groups))
1072                    (gnus-sethash group group gnus-killed-hashtb)
1073                    (if gnus-subscribe-hierarchical-interactive
1074                        (push group new-newsgroups)
1075                      (gnus-call-subscribe-functions
1076                       gnus-subscribe-newsgroup-method group)))))))
1077            gnus-active-hashtb)
1078           (when new-newsgroups
1079             (gnus-subscribe-hierarchical-interactive new-newsgroups))
1080           (if (> groups 0)
1081               (gnus-message 5 "%d new newsgroup%s arrived."
1082                             groups (if (> groups 1) "s have" " has"))
1083             (gnus-message 5 "No new newsgroups.")))))))
1084
1085 (defun gnus-matches-options-n (group)
1086   ;; Returns `subscribe' if the group is to be unconditionally
1087   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
1088   ;; no match for the group.
1089
1090   ;; First we check the two user variables.
1091   (cond
1092    ((and gnus-options-subscribe
1093          (string-match gnus-options-subscribe group))
1094     'subscribe)
1095    ((and gnus-auto-subscribed-groups
1096          (string-match gnus-auto-subscribed-groups group))
1097     'subscribe)
1098    ((and gnus-options-not-subscribe
1099          (string-match gnus-options-not-subscribe group))
1100     'ignore)
1101    ;; Then we go through the list that was retrieved from the .newsrc
1102    ;; file.  This list has elements on the form
1103    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
1104    ;; is in the reverse order of the options line) is returned.
1105    (t
1106     (let ((regs gnus-newsrc-options-n))
1107       (while (and regs
1108                   (not (string-match (caar regs) group)))
1109         (setq regs (cdr regs)))
1110       (and regs (cdar regs))))))
1111
1112 (defun gnus-ask-server-for-new-groups ()
1113   (let* ((new-date (message-make-date))
1114          (date (or gnus-newsrc-last-checked-date new-date))
1115          (methods (cons gnus-select-method
1116                         (nconc
1117                          (when (gnus-archive-server-wanted-p)
1118                            (list "archive"))
1119                          (append
1120                           (and (consp gnus-check-new-newsgroups)
1121                                gnus-check-new-newsgroups)
1122                           gnus-secondary-select-methods))))
1123          (groups 0)
1124          group new-newsgroups got-new method hashtb
1125          gnus-override-subscribe-method)
1126     (unless gnus-killed-hashtb
1127       (gnus-make-hashtable-from-killed))
1128     ;; Go through both primary and secondary select methods and
1129     ;; request new newsgroups.
1130     (while (setq method (gnus-server-get-method nil (pop methods)))
1131       (setq new-newsgroups nil
1132             gnus-override-subscribe-method method)
1133       (when (and (gnus-check-server method)
1134                  (gnus-request-newgroups date method))
1135         (save-excursion
1136           (setq got-new t
1137                 hashtb (gnus-make-hashtable 100))
1138           (set-buffer nntp-server-buffer)
1139           ;; Enter all the new groups into a hashtable.
1140           (gnus-active-to-gnus-format method hashtb 'ignore))
1141         ;; Now all new groups from `method' are in `hashtb'.
1142         (mapatoms
1143          (lambda (group-sym)
1144            (if (or (null (setq group (symbol-name group-sym)))
1145                    (not (boundp group-sym))
1146                    (null (symbol-value group-sym))
1147                    (gnus-gethash group gnus-newsrc-hashtb)
1148                    (member group gnus-zombie-list)
1149                    (member group gnus-killed-list))
1150                ;; The group is already known.
1151                ()
1152              ;; Make this group active.
1153              (when (symbol-value group-sym)
1154                (gnus-set-active group (symbol-value group-sym)))
1155              ;; Check whether we want it or not.
1156              (let ((do-sub (gnus-matches-options-n group)))
1157                (cond
1158                 ((eq do-sub 'subscribe)
1159                  (incf groups)
1160                  (gnus-sethash group group gnus-killed-hashtb)
1161                  (gnus-call-subscribe-functions
1162                   gnus-subscribe-options-newsgroup-method group))
1163                 ((eq do-sub 'ignore)
1164                  nil)
1165                 (t
1166                  (incf groups)
1167                  (gnus-sethash group group gnus-killed-hashtb)
1168                  (if gnus-subscribe-hierarchical-interactive
1169                      (push group new-newsgroups)
1170                    (gnus-call-subscribe-functions
1171                     gnus-subscribe-newsgroup-method group)))))))
1172          hashtb))
1173       (when new-newsgroups
1174         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
1175     (if (> groups 0)
1176         (gnus-message 5 "%d new newsgroup%s arrived"
1177                       groups (if (> groups 1) "s have" " has"))
1178       (gnus-message 5 "No new newsgroups"))
1179     (when got-new
1180       (setq gnus-newsrc-last-checked-date new-date))
1181     got-new))
1182
1183 (defun gnus-check-first-time-used ()
1184   (catch 'ended
1185     ;; First check if any of the following files exist.  If they do,
1186     ;; it's not the first time the user has used Gnus.
1187     (dolist (file (list (concat gnus-current-startup-file ".el")
1188                         (concat gnus-current-startup-file ".eld")
1189                         (concat gnus-startup-file ".el")
1190                         (concat gnus-startup-file ".eld")))
1191       (when (file-exists-p file)
1192         (throw 'ended nil)))
1193     (gnus-message 6 "First time user; subscribing you to default groups")
1194     (unless (gnus-read-active-file-p)
1195       (let ((gnus-read-active-file t))
1196         (gnus-read-active-file)))
1197     (setq gnus-newsrc-last-checked-date (message-make-date))
1198     ;; Subscribe to the default newsgroups.
1199     (let ((groups (or gnus-default-subscribed-newsgroups
1200                       gnus-backup-default-subscribed-newsgroups))
1201           group)
1202       (if (eq groups t)
1203           ;; If t, we subscribe (or not) all groups as if they were new.
1204           (mapatoms
1205            (lambda (sym)
1206              (when (setq group (symbol-name sym))
1207                (let ((do-sub (gnus-matches-options-n group)))
1208                  (cond
1209                   ((eq do-sub 'subscribe)
1210                    (gnus-sethash group group gnus-killed-hashtb)
1211                    (gnus-call-subscribe-functions
1212                     gnus-subscribe-options-newsgroup-method group))
1213                   ((eq do-sub 'ignore)
1214                    nil)
1215                   (t
1216                    (push group gnus-killed-list))))))
1217            gnus-active-hashtb)
1218         (dolist (group groups)
1219           ;; Only subscribe the default groups that are activated.
1220           (when (gnus-active group)
1221             (gnus-group-change-level
1222              group gnus-level-default-subscribed gnus-level-killed)))
1223         (save-excursion
1224           (set-buffer gnus-group-buffer)
1225           ;; Don't error if the group already exists. This happens when a
1226           ;; first-time user types 'F'. -- didier
1227           (gnus-group-make-help-group t))
1228         (when gnus-novice-user
1229           (gnus-message 7 "`A k' to list killed groups"))))))
1230
1231 (defun gnus-subscribe-group (group &optional previous method)
1232   "Subcribe GROUP and put it after PREVIOUS."
1233   (gnus-group-change-level
1234    (if method
1235        (list t group gnus-level-default-subscribed nil nil method)
1236      group)
1237    gnus-level-default-subscribed gnus-level-killed previous t)
1238   t)
1239
1240 ;; `gnus-group-change-level' is the fundamental function for changing
1241 ;; subscription levels of newsgroups.  This might mean just changing
1242 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
1243 ;; again, which subscribes/unsubscribes a group, which is equally
1244 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
1245 ;; from 8-9 to 1-7 means that you remove the group from the list of
1246 ;; killed (or zombie) groups and add them to the (kinda) subscribed
1247 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
1248 ;; which is trivial.
1249 ;; ENTRY can either be a string (newsgroup name) or a list (if
1250 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
1251 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
1252 ;; entries.
1253 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
1254 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
1255 ;; after.
1256 (defun gnus-group-change-level (entry level &optional oldlevel
1257                                       previous fromkilled)
1258   (let (group info active num)
1259     ;; Glean what info we can from the arguments
1260     (if (consp entry)
1261         (if fromkilled (setq group (nth 1 entry))
1262           (setq group (car (nth 2 entry))))
1263       (setq group entry))
1264     (when (and (stringp entry)
1265                oldlevel
1266                (< oldlevel gnus-level-zombie))
1267       (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
1268     (if (and (not oldlevel)
1269              (consp entry))
1270         (setq oldlevel (gnus-info-level (nth 2 entry)))
1271       (setq oldlevel (or oldlevel gnus-level-killed)))
1272     (when (stringp previous)
1273       (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
1274
1275     (if (and (>= oldlevel gnus-level-zombie)
1276              (gnus-gethash group gnus-newsrc-hashtb))
1277         ;; We are trying to subscribe a group that is already
1278         ;; subscribed.
1279         ()                              ; Do nothing.
1280
1281       (unless (gnus-ephemeral-group-p group)
1282         (gnus-dribble-enter
1283          (format "(gnus-group-change-level %S %S %S %S %S)"
1284                  group level oldlevel (car (nth 2 previous)) fromkilled)))
1285
1286       ;; Then we remove the newgroup from any old structures, if needed.
1287       ;; If the group was killed, we remove it from the killed or zombie
1288       ;; list.  If not, and it is in fact going to be killed, we remove
1289       ;; it from the newsrc hash table and assoc.
1290       (cond
1291        ((>= oldlevel gnus-level-zombie)
1292         ;; oldlevel could be wrong.
1293         (setq gnus-zombie-list (delete group gnus-zombie-list))
1294         (setq gnus-killed-list (delete group gnus-killed-list)))
1295        (t
1296         (when (and (>= level gnus-level-zombie)
1297                    entry)
1298           (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
1299           (when (nth 3 entry)
1300             (setcdr (gnus-gethash (car (nth 3 entry))
1301                                   gnus-newsrc-hashtb)
1302                     (cdr entry)))
1303           (setcdr (cdr entry) (cdddr entry)))))
1304
1305       ;; Finally we enter (if needed) the list where it is supposed to
1306       ;; go, and change the subscription level.  If it is to be killed,
1307       ;; we enter it into the killed or zombie list.
1308       (cond
1309        ((>= level gnus-level-zombie)
1310         ;; Remove from the hash table.
1311         (gnus-sethash group nil gnus-newsrc-hashtb)
1312         ;; We do not enter foreign groups into the list of dead
1313         ;; groups.
1314         (unless (gnus-group-foreign-p group)
1315           (if (= level gnus-level-zombie)
1316               (push group gnus-zombie-list)
1317             (if (= oldlevel gnus-level-killed)
1318                 ;; Remove from active hashtb.
1319                 (unintern group gnus-active-hashtb)
1320               ;; Don't add it into killed-list if it was killed.
1321               (push group gnus-killed-list)))))
1322        (t
1323         ;; If the list is to be entered into the newsrc assoc, and
1324         ;; it was killed, we have to create an entry in the newsrc
1325         ;; hashtb format and fix the pointers in the newsrc assoc.
1326         (if (< oldlevel gnus-level-zombie)
1327             ;; It was alive, and it is going to stay alive, so we
1328             ;; just change the level and don't change any pointers or
1329             ;; hash table entries.
1330             (setcar (cdaddr entry) level)
1331           (if (listp entry)
1332               (setq info (cdr entry)
1333                     num (car entry))
1334             (setq active (gnus-active group))
1335             (setq num
1336                   (if active (- (1+ (cdr active)) (car active)) t))
1337             ;; Shorten the select method if possible, if we need to
1338             ;; store it at all (native groups).
1339             (let ((method (gnus-method-simplify
1340                            (or gnus-override-subscribe-method
1341                                (gnus-group-method group)))))
1342               (if method
1343                   (setq info (list group level nil nil method))
1344                 (setq info (list group level nil)))))
1345           (unless previous
1346             (setq previous
1347                   (let ((p gnus-newsrc-alist))
1348                     (while (cddr p)
1349                       (setq p (cdr p)))
1350                     p)))
1351           (setq entry (cons info (cddr previous)))
1352           (if (cdr previous)
1353               (progn
1354                 (setcdr (cdr previous) entry)
1355                 (gnus-sethash group (cons num (cdr previous))
1356                               gnus-newsrc-hashtb))
1357             (setcdr previous entry)
1358             (gnus-sethash group (cons num previous)
1359                           gnus-newsrc-hashtb))
1360           (when (cdr entry)
1361             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry))
1362           (gnus-dribble-enter
1363            (format
1364             "(gnus-group-set-info '%S)" info)))))
1365       (when gnus-group-change-level-function
1366         (funcall gnus-group-change-level-function
1367                  group level oldlevel previous)))))
1368
1369 (defun gnus-kill-newsgroup (newsgroup)
1370   "Obsolete function.  Kills a newsgroup."
1371   (gnus-group-change-level
1372    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
1373
1374 (defun gnus-check-bogus-newsgroups (&optional confirm)
1375   "Remove bogus newsgroups.
1376 If CONFIRM is non-nil, the user has to confirm the deletion of every
1377 newsgroup."
1378   (let ((newsrc (cdr gnus-newsrc-alist))
1379         bogus group entry info)
1380     (gnus-message 5 "Checking bogus newsgroups...")
1381     (unless (gnus-read-active-file-p)
1382       (gnus-read-active-file t))
1383     (when (gnus-read-active-file-p)
1384       ;; Find all bogus newsgroup that are subscribed.
1385       (while newsrc
1386         (setq info (pop newsrc)
1387               group (gnus-info-group info))
1388         (unless (or (gnus-active group) ; Active
1389                     (and (gnus-info-method info)
1390                          (not (gnus-secondary-method-p
1391                                (gnus-info-method info))))) ; Foreign
1392           ;; Found a bogus newsgroup.
1393           (push group bogus)))
1394       (if confirm
1395           (map-y-or-n-p
1396            "Remove bogus group %s? "
1397            (lambda (group)
1398              ;; Remove all bogus subscribed groups by first killing them, and
1399              ;; then removing them from the list of killed groups.
1400              (when (setq entry (gnus-gethash group gnus-newsrc-hashtb))
1401                (gnus-group-change-level entry gnus-level-killed)
1402                (setq gnus-killed-list (delete group gnus-killed-list))))
1403            bogus '("group" "groups" "remove"))
1404         (while (setq group (pop bogus))
1405           ;; Remove all bogus subscribed groups by first killing them, and
1406           ;; then removing them from the list of killed groups.
1407           (when (setq entry (gnus-gethash group gnus-newsrc-hashtb))
1408             (gnus-group-change-level entry gnus-level-killed)
1409             (setq gnus-killed-list (delete group gnus-killed-list)))))
1410       ;; Then we remove all bogus groups from the list of killed and
1411       ;; zombie groups.  They are removed without confirmation.
1412       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
1413             killed)
1414         (while dead-lists
1415           (setq killed (symbol-value (car dead-lists)))
1416           (while killed
1417             (unless (gnus-active (setq group (pop killed)))
1418               ;; The group is bogus.
1419               ;; !!!Slow as hell.
1420               (set (car dead-lists)
1421                    (delete group (symbol-value (car dead-lists))))))
1422           (setq dead-lists (cdr dead-lists))))
1423       (gnus-run-hooks 'gnus-check-bogus-groups-hook)
1424       (gnus-message 5 "Checking bogus newsgroups...done"))))
1425
1426 (defun gnus-check-duplicate-killed-groups ()
1427   "Remove duplicates from the list of killed groups."
1428   (interactive)
1429   (let ((killed gnus-killed-list))
1430     (while killed
1431       (gnus-message 9 "%d" (length killed))
1432       (setcdr killed (delete (car killed) (cdr killed)))
1433       (setq killed (cdr killed)))))
1434
1435 ;; We want to inline a function from gnus-cache, so we cheat here:
1436 (eval-when-compile
1437   (defvar gnus-cache-active-hashtb)
1438   (defun gnus-cache-possibly-alter-active (group active)
1439     "Alter the ACTIVE info for GROUP to reflect the articles in the cache."
1440     (when gnus-cache-active-hashtb
1441       (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
1442         (when cache-active
1443           (when (< (car cache-active) (car active))
1444             (setcar active (car cache-active)))
1445           (when (> (cdr cache-active) (cdr active))
1446             (setcdr active (cdr cache-active))))))))
1447
1448 (defun gnus-activate-group (group &optional scan dont-check method)
1449   ;; Check whether a group has been activated or not.
1450   ;; If SCAN, request a scan of that group as well.
1451   (let ((method (or method (inline (gnus-find-method-for-group group))))
1452         active)
1453     (and (inline (gnus-check-server method))
1454          ;; We escape all bugs and quit here to make it possible to
1455          ;; continue if a group is so out-there that it reports bugs
1456          ;; and stuff.
1457          (progn
1458            (and scan
1459                 (gnus-check-backend-function 'request-scan (car method))
1460                 (gnus-request-scan group method))
1461            t)
1462          (if (or debug-on-error debug-on-quit)
1463              (inline (gnus-request-group group dont-check method))
1464            (condition-case ()
1465                (inline (gnus-request-group group dont-check method))
1466              ;;(error nil)
1467              (quit
1468               (message "Quit activating %s" group)
1469               nil)))
1470          (unless dont-check
1471            (setq active (gnus-parse-active))
1472            ;; If there are no articles in the group, the GROUP
1473            ;; command may have responded with the `(0 . 0)'.  We
1474            ;; ignore this if we already have an active entry
1475            ;; for the group.
1476            (if (and (zerop (car active))
1477                     (zerop (cdr active))
1478                     (gnus-active group))
1479                (gnus-active group)
1480              (gnus-set-active group active)
1481              ;; Return the new active info.
1482              active)))))
1483
1484 (defun gnus-get-unread-articles-in-group (info active &optional update)
1485   (when active
1486     ;; Allow the backend to update the info in the group.
1487     (when (and update
1488                (gnus-request-update-info
1489                 info (inline (gnus-find-method-for-group
1490                               (gnus-info-group info)))))
1491       (gnus-activate-group (gnus-info-group info) nil t))
1492
1493     (let* ((range (gnus-info-read info))
1494            (num 0))
1495       ;; If a cache is present, we may have to alter the active info.
1496       (when (and gnus-use-cache info)
1497         (inline (gnus-cache-possibly-alter-active
1498                  (gnus-info-group info) active)))
1499       ;; Modify the list of read articles according to what articles
1500       ;; are available; then tally the unread articles and add the
1501       ;; number to the group hash table entry.
1502       (cond
1503        ((zerop (cdr active))
1504         (setq num 0))
1505        ((not range)
1506         (setq num (- (1+ (cdr active)) (car active))))
1507        ((not (listp (cdr range)))
1508         ;; Fix a single (num . num) range according to the
1509         ;; active hash table.
1510         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
1511         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
1512         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
1513         ;; Compute number of unread articles.
1514         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
1515        (t
1516         ;; The read list is a list of ranges.  Fix them according to
1517         ;; the active hash table.
1518         ;; First peel off any elements that are below the lower
1519         ;; active limit.
1520         (while (and (cdr range)
1521                     (>= (car active)
1522                         (or (and (atom (cadr range)) (cadr range))
1523                             (caadr range))))
1524           (if (numberp (car range))
1525               (setcar range
1526                       (cons (car range)
1527                             (or (and (numberp (cadr range))
1528                                      (cadr range))
1529                                 (cdadr range))))
1530             (setcdr (car range)
1531                     (or (and (numberp (nth 1 range)) (nth 1 range))
1532                         (cdadr range))))
1533           (setcdr range (cddr range)))
1534         ;; Adjust the first element to be the same as the lower limit.
1535         (when (and (not (atom (car range)))
1536                    (< (cdar range) (car active)))
1537           (setcdr (car range) (1- (car active))))
1538         ;; Then we want to peel off any elements that are higher
1539         ;; than the upper active limit.
1540         (let ((srange range))
1541           ;; Go past all valid elements.
1542           (while (and (cdr srange)
1543                       (<= (or (and (atom (cadr srange))
1544                                    (cadr srange))
1545                               (caadr srange))
1546                           (cdr active)))
1547             (setq srange (cdr srange)))
1548           (when (cdr srange)
1549             ;; Nuke all remaining invalid elements.
1550             (setcdr srange nil))
1551
1552           ;; Adjust the final element.
1553           (when (and (not (atom (car srange)))
1554                      (> (cdar srange) (cdr active)))
1555             (setcdr (car srange) (cdr active))))
1556         ;; Compute the number of unread articles.
1557         (while range
1558           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
1559                                       (cdar range)))
1560                               (or (and (atom (car range)) (car range))
1561                                   (caar range)))))
1562           (setq range (cdr range)))
1563         (setq num (max 0 (- (cdr active) num)))))
1564       ;; Set the number of unread articles.
1565       (when (and info
1566                  (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb))
1567         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
1568       num)))
1569
1570 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
1571 ;; and compute how many unread articles there are in each group.
1572 (defun gnus-get-unread-articles (&optional level)
1573   (setq gnus-server-method-cache nil)
1574   (let* ((newsrc (cdr gnus-newsrc-alist))
1575          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
1576          (foreign-level
1577           (min
1578            (cond ((and gnus-activate-foreign-newsgroups
1579                        (not (numberp gnus-activate-foreign-newsgroups)))
1580                   (1+ gnus-level-subscribed))
1581                  ((numberp gnus-activate-foreign-newsgroups)
1582                   gnus-activate-foreign-newsgroups)
1583                  (t 0))
1584            level))
1585          scanned-methods info group active method retrieve-groups)
1586     (gnus-message 6 "Checking new news...")
1587
1588     (while newsrc
1589       (setq active (gnus-active (setq group (gnus-info-group
1590                                              (setq info (pop newsrc))))))
1591
1592       ;; Check newsgroups.  If the user doesn't want to check them, or
1593       ;; they can't be checked (for instance, if the news server can't
1594       ;; be reached) we just set the number of unread articles in this
1595       ;; newsgroup to t.  This means that Gnus thinks that there are
1596       ;; unread articles, but it has no idea how many.
1597
1598       ;; To be more explicit:
1599       ;; >0 for an active group with messages
1600       ;; 0 for an active group with no unread messages
1601       ;; nil for non-foreign groups that the user has requested not be checked
1602       ;; t for unchecked foreign groups or bogus groups, or groups that can't
1603       ;;   be checked, for one reason or other.
1604       (if (and (setq method (gnus-info-method info))
1605                (not (inline
1606                       (gnus-server-equal
1607                        gnus-select-method
1608                        (setq method (gnus-server-get-method nil method)))))
1609                (not (gnus-secondary-method-p method)))
1610           ;; These groups are foreign.  Check the level.
1611           (when (and (<= (gnus-info-level info) foreign-level)
1612                      (setq active (gnus-activate-group group 'scan)))
1613             ;; Let the Gnus agent save the active file.
1614             (when (and gnus-agent active (gnus-online method))
1615               (gnus-agent-save-group-info
1616                method (gnus-group-real-name group) active))
1617             (unless (inline (gnus-virtual-group-p group))
1618               (inline (gnus-close-group group)))
1619             (when (fboundp (intern (concat (symbol-name (car method))
1620                                            "-request-update-info")))
1621               (inline (gnus-request-update-info info method))))
1622         ;; These groups are native or secondary.
1623         (cond
1624          ;; We don't want these groups.
1625          ((> (gnus-info-level info) level)
1626           (setq active 'ignore))
1627          ;; Activate groups.
1628          ((not gnus-read-active-file)
1629           (if (gnus-check-backend-function 'retrieve-groups group)
1630               ;; if server support gnus-retrieve-groups we push
1631               ;; the group onto retrievegroups for later checking
1632               (if (assoc method retrieve-groups)
1633                   (setcdr (assoc method retrieve-groups)
1634                           (cons group (cdr (assoc method retrieve-groups))))
1635                 (push (list method group) retrieve-groups))
1636             ;; hack: `nnmail-get-new-mail' changes the mail-source depending
1637             ;; on the group, so we must perform a scan for every group
1638             ;; if the users has any directory mail sources.
1639             ;; hack: if `nnmail-scan-directory-mail-source-once' is non-nil,
1640             ;; for it scan all spool files even when the groups are
1641             ;; not required.
1642             (if (and
1643                  (or nnmail-scan-directory-mail-source-once
1644                      (null (assq 'directory
1645                                  (or mail-sources
1646                                      (if (listp nnmail-spool-file)
1647                                          nnmail-spool-file
1648                                        (list nnmail-spool-file))))))
1649                  (member method scanned-methods))
1650                 (setq active (gnus-activate-group group))
1651               (setq active (gnus-activate-group group 'scan))
1652               (push method scanned-methods))
1653             (when active
1654               (gnus-close-group group))))))
1655
1656       ;; Get the number of unread articles in the group.
1657       (cond
1658        ((eq active 'ignore)
1659         ;; Don't do anything.
1660         )
1661        (active
1662         (inline (gnus-get-unread-articles-in-group info active t)))
1663        (t
1664         ;; The group couldn't be reached, so we nix out the number of
1665         ;; unread articles and stuff.
1666         (gnus-set-active group nil)
1667         (let ((tmp (gnus-gethash group gnus-newsrc-hashtb)))
1668           (when tmp
1669             (setcar tmp t))))))
1670
1671     ;; iterate through groups on methods which support gnus-retrieve-groups
1672     ;; and fetch a partial active file and use it to find new news.
1673     (dolist (rg retrieve-groups)
1674       (let ((method (or (car rg) gnus-select-method))
1675             (groups (cdr rg)))
1676         (when (gnus-check-server method)
1677           ;; Request that the backend scan its incoming messages.
1678           (when (gnus-check-backend-function 'request-scan (car method))
1679             (gnus-request-scan nil method))
1680           (gnus-read-active-file-2
1681            (mapcar (lambda (group) (gnus-group-real-name group)) groups)
1682            method)
1683           (dolist (group groups)
1684             (cond
1685              ((setq active (gnus-active (gnus-info-group
1686                                          (setq info (gnus-get-info group)))))
1687               (inline (gnus-get-unread-articles-in-group info active t)))
1688              (t
1689               ;; The group couldn't be reached, so we nix out the number of
1690               ;; unread articles and stuff.
1691               (gnus-set-active group nil)
1692               (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))))))
1693
1694     (gnus-message 6 "Checking new news...done")))
1695
1696 ;; Create a hash table out of the newsrc alist.  The `car's of the
1697 ;; alist elements are used as keys.
1698 (defun gnus-make-hashtable-from-newsrc-alist ()
1699   (let ((alist gnus-newsrc-alist)
1700         (ohashtb gnus-newsrc-hashtb)
1701         prev)
1702     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
1703     (setq alist
1704           (setq prev (setq gnus-newsrc-alist
1705                            (if (equal (caar gnus-newsrc-alist)
1706                                       "dummy.group")
1707                                gnus-newsrc-alist
1708                              (cons (list "dummy.group" 0 nil) alist)))))
1709     (while alist
1710       (gnus-sethash
1711        (caar alist)
1712        ;; Preserve number of unread articles in groups.
1713        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
1714              prev)
1715        gnus-newsrc-hashtb)
1716       (setq prev alist
1717             alist (cdr alist)))))
1718
1719 (defun gnus-make-hashtable-from-killed ()
1720   "Create a hash table from the killed and zombie lists."
1721   (let ((lists '(gnus-killed-list gnus-zombie-list))
1722         list)
1723     (setq gnus-killed-hashtb
1724           (gnus-make-hashtable
1725            (+ (length gnus-killed-list) (length gnus-zombie-list))))
1726     (while lists
1727       (setq list (symbol-value (pop lists)))
1728       (while list
1729         (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
1730
1731 (defun gnus-parse-active ()
1732   "Parse active info in the nntp server buffer."
1733   (save-excursion
1734     (set-buffer nntp-server-buffer)
1735     (goto-char (point-min))
1736     ;; Parse the result we got from `gnus-request-group'.
1737     (when (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
1738       (goto-char (match-beginning 1))
1739       (cons (read (current-buffer))
1740             (read (current-buffer))))))
1741
1742 (defun gnus-make-articles-unread (group articles)
1743   "Mark ARTICLES in GROUP as unread."
1744   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
1745                           (gnus-gethash (gnus-group-real-name group)
1746                                         gnus-newsrc-hashtb))))
1747          (ranges (gnus-info-read info))
1748          news article)
1749     (while articles
1750       (when (gnus-member-of-range
1751              (setq article (pop articles)) ranges)
1752         (push article news)))
1753     (when news
1754       ;; Enter this list into the group info.
1755       (gnus-info-set-read
1756        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
1757
1758       ;; Set the number of unread articles in gnus-newsrc-hashtb.
1759       (gnus-get-unread-articles-in-group info (gnus-active group))
1760
1761       ;; Insert the change into the group buffer and the dribble file.
1762       (gnus-group-update-group group t))))
1763
1764 (defun gnus-make-ascending-articles-unread (group articles)
1765   "Mark ascending ARTICLES in GROUP as unread."
1766   (let* ((entry (or (gnus-gethash group gnus-newsrc-hashtb)
1767                     (gnus-gethash (gnus-group-real-name group)
1768                                   gnus-newsrc-hashtb)))
1769          (info (nth 2 entry))
1770          (ranges (gnus-info-read info))
1771          (r ranges)
1772          modified)
1773
1774     (while articles
1775       (let ((article (pop articles))) ; get the next article to remove from ranges
1776         (while (let ((range (car ranges))) ; note the current range
1777                  (if (atom range)       ; single value range
1778                      (cond ((not range)
1779                             ;; the articles extend past the end of the ranges
1780                             ;; OK - I'm done
1781                             (setq articles nil))
1782                            ((< range article)
1783                             ;; this range preceeds the article. Leave the range unmodified.
1784                             (pop ranges)
1785                             ranges)
1786                            ((= range article)
1787                             ;; this range exactly matches the article; REMOVE THE RANGE.
1788                             ;; NOTE: When the range being removed is the last range, the list is corrupted by inserting null at its end.
1789                             (setcar ranges (cadr ranges))
1790                             (setcdr ranges (cddr ranges))
1791                             (setq modified (if (car ranges) t 'remove-null))
1792                             nil))
1793                    (let ((min (car range))
1794                          (max (cdr range)))
1795                      ;; I have a min/max range to consider
1796                      (cond ((> min max) ; invalid range introduced by splitter
1797                             (setcar ranges (cadr ranges))
1798                             (setcdr ranges (cddr ranges))
1799                             (setq modified (if (car ranges) t 'remove-null))
1800                             ranges)
1801                            ((= min max)
1802                             ;; replace min/max range with a single-value range
1803                             (setcar ranges min)
1804                             ranges)
1805                            ((< max article)
1806                             ;; this range preceeds the article. Leave the range unmodified.
1807                             (pop ranges)
1808                             ranges)
1809                            ((< article min)
1810                             ;; this article preceeds the range.  Return null to move to the
1811                             ;; next article
1812                             nil)
1813                            (t
1814                             ;; this article splits the range into two parts
1815                             (setcdr ranges (cons (cons (1+ article) max) (cdr ranges)))
1816                             (setcdr range (1- article))
1817                             (setq modified t)
1818                             ranges))))))))
1819                   
1820     (when modified
1821       (when (eq modified 'remove-null)
1822         (setq r (delq nil r)))
1823       ;; Enter this list into the group info.
1824       (gnus-info-set-read info r)
1825
1826       ;; Set the number of unread articles in gnus-newsrc-hashtb.
1827       (gnus-get-unread-articles-in-group info (gnus-active group))
1828
1829       ;; Insert the change into the group buffer and the dribble file.
1830       (gnus-group-update-group group t))))
1831
1832 ;; Enter all dead groups into the hashtb.
1833 (defun gnus-update-active-hashtb-from-killed ()
1834   (let ((hashtb (setq gnus-active-hashtb (gnus-make-hashtable 4096)))
1835         (lists (list gnus-killed-list gnus-zombie-list))
1836         killed)
1837     (while lists
1838       (setq killed (car lists))
1839       (while killed
1840         (gnus-sethash (car killed) nil hashtb)
1841         (setq killed (cdr killed)))
1842       (setq lists (cdr lists)))))
1843
1844 (defun gnus-get-killed-groups ()
1845   "Go through the active hashtb and mark all unknown groups as killed."
1846   ;; First make sure active file has been read.
1847   (unless (gnus-read-active-file-p)
1848     (let ((gnus-read-active-file t))
1849       (gnus-read-active-file)))
1850   (unless gnus-killed-hashtb
1851     (gnus-make-hashtable-from-killed))
1852   ;; Go through all newsgroups that are known to Gnus - enlarge kill list.
1853   (mapatoms
1854    (lambda (sym)
1855      (let ((groups 0)
1856            (group (symbol-name sym)))
1857        (if (or (null group)
1858                (gnus-gethash group gnus-killed-hashtb)
1859                (gnus-gethash group gnus-newsrc-hashtb))
1860            ()
1861          (let ((do-sub (gnus-matches-options-n group)))
1862            (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
1863                ()
1864              (setq groups (1+ groups))
1865              (push group gnus-killed-list)
1866              (gnus-sethash group group gnus-killed-hashtb))))))
1867    gnus-active-hashtb)
1868   (gnus-dribble-touch))
1869
1870 ;; Get the active file(s) from the backend(s).
1871 (defun gnus-read-active-file (&optional force not-native)
1872   (gnus-group-set-mode-line)
1873   (let ((methods
1874          (mapcar
1875           (lambda (m) (if (stringp m) (gnus-server-get-method nil m) m))
1876           (append
1877            (if (and (not not-native)
1878                     (gnus-check-server gnus-select-method))
1879                ;; The native server is available.
1880                (cons gnus-select-method gnus-secondary-select-methods)
1881              ;; The native server is down, so we just do the
1882              ;; secondary ones.
1883              gnus-secondary-select-methods)
1884            ;; Also read from the archive server.
1885            (when (gnus-archive-server-wanted-p)
1886              (list "archive")))))
1887         method)
1888     (setq gnus-have-read-active-file nil)
1889     (save-excursion
1890       (set-buffer nntp-server-buffer)
1891       (while (setq method (pop methods))
1892         ;; Only do each method once, in case the methods appear more
1893         ;; than once in this list.
1894         (unless (member method methods)
1895           (if (or debug-on-error debug-on-quit)
1896               (gnus-read-active-file-1 method force)
1897             (condition-case ()
1898                 (gnus-read-active-file-1 method force)
1899               ;; We catch C-g so that we can continue past servers
1900               ;; that do not respond.
1901               (quit
1902                (message "Quit reading the active file")
1903                nil))))))))
1904
1905 (defun gnus-read-active-file-1 (method force)
1906   (let (where mesg)
1907     (setq where (nth 1 method)
1908           mesg (format "Reading active file%s via %s..."
1909                        (if (and where (not (zerop (length where))))
1910                            (concat " from " where) "")
1911                        (car method)))
1912     (gnus-message 5 mesg)
1913     (when (gnus-check-server method)
1914       ;; Request that the backend scan its incoming messages.
1915       (when (gnus-check-backend-function 'request-scan (car method))
1916         (gnus-request-scan nil method))
1917       (cond
1918        ((and (eq gnus-read-active-file 'some)
1919              (gnus-check-backend-function 'retrieve-groups (car method))
1920              (not force))
1921         (let ((newsrc (cdr gnus-newsrc-alist))
1922               (gmethod (gnus-server-get-method nil method))
1923               groups info)
1924           (while (setq info (pop newsrc))
1925             (when (inline
1926                     (gnus-server-equal
1927                      (inline
1928                        (gnus-find-method-for-group
1929                         (gnus-info-group info) info))
1930                      gmethod))
1931               (push (gnus-group-real-name (gnus-info-group info))
1932                     groups)))
1933           (gnus-read-active-file-2 groups method)))
1934        ((null method)
1935         t)
1936        (t
1937         (if (not (gnus-request-list method))
1938             (unless (equal method gnus-message-archive-method)
1939               (gnus-error 1 "Cannot read active file from %s server"
1940                           (car method)))
1941           (gnus-message 5 mesg)
1942           (gnus-active-to-gnus-format method gnus-active-hashtb nil t)
1943           ;; We mark this active file as read.
1944           (push method gnus-have-read-active-file)
1945           (gnus-message 5 "%sdone" mesg)))))))
1946
1947 (defun gnus-read-active-file-2 (groups method)
1948   "Read an active file for GROUPS in METHOD using gnus-retrieve-groups."
1949   (when groups
1950     (save-excursion
1951       (set-buffer nntp-server-buffer)
1952       (gnus-check-server method)
1953       (let ((list-type (gnus-retrieve-groups groups method)))
1954         (cond ((not list-type)
1955                (gnus-error
1956                 1.2 "Cannot read partial active file from %s server."
1957                 (car method)))
1958               ((eq list-type 'active)
1959                (gnus-active-to-gnus-format method gnus-active-hashtb nil t))
1960               (t
1961                (gnus-groups-to-gnus-format method gnus-active-hashtb t)))))))
1962
1963 ;; Read an active file and place the results in `gnus-active-hashtb'.
1964 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors
1965                                              real-active)
1966   (unless method
1967     (setq method gnus-select-method))
1968   (let ((cur (current-buffer))
1969         (hashtb (or hashtb
1970                     (if (and gnus-active-hashtb
1971                              (not (equal method gnus-select-method)))
1972                         gnus-active-hashtb
1973                       (setq gnus-active-hashtb
1974                             (if (equal method gnus-select-method)
1975                                 (gnus-make-hashtable
1976                                  (count-lines (point-min) (point-max)))
1977                               (gnus-make-hashtable 4096)))))))
1978     ;; Delete unnecessary lines.
1979     (goto-char (point-min))
1980     (cond
1981      ((string= gnus-ignored-newsgroups "")
1982       (delete-matching-lines "^to\\."))
1983      (t
1984       (delete-matching-lines (concat "^to\\.\\|" gnus-ignored-newsgroups))))
1985
1986     (goto-char (point-min))
1987     (unless (re-search-forward "[\\\"]" nil t)
1988       ;; Make the group names readable as a lisp expression even if they
1989       ;; contain special characters.
1990       (goto-char (point-max))
1991       (while (re-search-backward "[][';?()#]" nil t)
1992         (insert ?\\)))
1993
1994     ;; Let the Gnus agent save the active file.
1995     (when (and gnus-agent real-active (gnus-online method))
1996       (gnus-agent-save-active method))
1997
1998     ;; If these are groups from a foreign select method, we insert the
1999     ;; group prefix in front of the group names.
2000     (when (not (gnus-server-equal
2001                 (gnus-server-get-method nil method)
2002                 (gnus-server-get-method nil gnus-select-method)))
2003       (let ((prefix (gnus-group-prefixed-name "" method)))
2004         (goto-char (point-min))
2005         (while (and (not (eobp))
2006                     (progn
2007                       (when (= (following-char) ?\")
2008                         (forward-char 1))
2009                       (insert prefix)
2010                       (zerop (forward-line 1)))))))
2011     ;; Store the active file in a hash table.
2012     (goto-char (point-min))
2013     (let (group max min)
2014       (while (not (eobp))
2015         (condition-case ()
2016             (progn
2017               (narrow-to-region (point) (gnus-point-at-eol))
2018               ;; group gets set to a symbol interned in the hash table
2019               ;; (what a hack!!) - jwz
2020               (setq group (let ((obarray hashtb)) (read cur)))
2021               ;; ### The extended group name scheme makes
2022               ;; the previous optimization strategy sort of pointless...
2023               (when (stringp group)
2024                 (setq group (intern group hashtb)))
2025               (if (and (numberp (setq max (read cur)))
2026                        (numberp (setq min (read cur)))
2027                        (progn
2028                          (skip-chars-forward " \t")
2029                          (not
2030                           (or (eq (char-after) ?=)
2031                               (eq (char-after) ?x)
2032                               (eq (char-after) ?j)))))
2033                   (progn
2034                     (set group (cons min max))
2035                     ;; if group is moderated, stick in moderation table
2036                     (when (eq (char-after) ?m)
2037                       (unless gnus-moderated-hashtb
2038                         (setq gnus-moderated-hashtb (gnus-make-hashtable)))
2039                       (gnus-sethash (symbol-name group) t
2040                                     gnus-moderated-hashtb)))
2041                 (set group nil)))
2042           (error
2043            (and group
2044                 (symbolp group)
2045                 (set group nil))
2046            (unless ignore-errors
2047              (gnus-message 3 "Warning - invalid active: %s"
2048                            (buffer-substring
2049                             (gnus-point-at-bol) (gnus-point-at-eol))))))
2050         (widen)
2051         (forward-line 1)))))
2052
2053 (defun gnus-groups-to-gnus-format (method &optional hashtb real-active)
2054   ;; Parse a "groups" active file.
2055   (let ((cur (current-buffer))
2056         (hashtb (or hashtb
2057                     (if (and method gnus-active-hashtb)
2058                         gnus-active-hashtb
2059                       (setq gnus-active-hashtb
2060                             (gnus-make-hashtable
2061                              (count-lines (point-min) (point-max)))))))
2062         (prefix (and method
2063                      (not (gnus-server-equal
2064                            (gnus-server-get-method nil method)
2065                            (gnus-server-get-method nil gnus-select-method)))
2066                      (gnus-group-prefixed-name "" method))))
2067
2068     ;; Let the Gnus agent save the active file.
2069     (if (and gnus-agent
2070              real-active
2071              (gnus-online method)
2072              (gnus-agent-method-p method))
2073         (progn
2074           (gnus-agent-save-groups method)
2075           (gnus-active-to-gnus-format method hashtb nil real-active))
2076
2077       (goto-char (point-min))
2078       ;; We split this into to separate loops, one with the prefix
2079       ;; and one without to speed the reading up somewhat.
2080       (if prefix
2081           (let (min max opoint group)
2082             (while (not (eobp))
2083               (condition-case ()
2084                   (progn
2085                     (read cur) (read cur)
2086                     (setq min (read cur)
2087                           max (read cur)
2088                           opoint (point))
2089                     (skip-chars-forward " \t")
2090                     (insert prefix)
2091                     (goto-char opoint)
2092                     (set (let ((obarray hashtb)) (read cur))
2093                          (cons min max)))
2094                 (error (and group (symbolp group) (set group nil))))
2095               (forward-line 1)))
2096         (let (min max group)
2097           (while (not (eobp))
2098             (condition-case ()
2099                 (when (eq (char-after) ?2)
2100                   (read cur) (read cur)
2101                   (setq min (read cur)
2102                         max (read cur))
2103                   (set (setq group (let ((obarray hashtb)) (read cur)))
2104                        (cons min max)))
2105               (error (and group (symbolp group) (set group nil))))
2106             (forward-line 1)))))))
2107
2108 (defun gnus-read-newsrc-file (&optional force)
2109   "Read startup file.
2110 If FORCE is non-nil, the .newsrc file is read."
2111   ;; Reset variables that might be defined in the .newsrc.eld file.
2112   (let ((variables (remove 'gnus-format-specs gnus-variable-list)))
2113     (while variables
2114       (set (car variables) nil)
2115       (setq variables (cdr variables))))
2116   (let* ((newsrc-file gnus-current-startup-file)
2117          (quick-file (concat newsrc-file ".el")))
2118     (save-excursion
2119       ;; We always load the .newsrc.eld file.  If always contains
2120       ;; much information that can not be gotten from the .newsrc
2121       ;; file (ticked articles, killed groups, foreign methods, etc.)
2122       (gnus-read-newsrc-el-file quick-file)
2123
2124       (when (and gnus-read-newsrc-file
2125                  (file-exists-p gnus-current-startup-file)
2126                  (or force
2127                      (and (file-newer-than-file-p newsrc-file quick-file)
2128                           (file-newer-than-file-p newsrc-file
2129                                                   (concat quick-file "d")))
2130                      (not gnus-newsrc-alist)))
2131         ;; We read the .newsrc file.  Note that if there if a
2132         ;; .newsrc.eld file exists, it has already been read, and
2133         ;; the `gnus-newsrc-hashtb' has been created.  While reading
2134         ;; the .newsrc file, Gnus will only use the information it
2135         ;; can find there for changing the data already read -
2136         ;; i. e., reading the .newsrc file will not trash the data
2137         ;; already read (except for read articles).
2138         (save-excursion
2139           (gnus-message 5 "Reading %s..." newsrc-file)
2140           (set-buffer (nnheader-find-file-noselect newsrc-file))
2141           (buffer-disable-undo)
2142           (gnus-newsrc-to-gnus-format)
2143           (kill-buffer (current-buffer))
2144           (gnus-message 5 "Reading %s...done" newsrc-file)))
2145
2146       ;; Convert old to new.
2147       (gnus-convert-old-newsrc))))
2148
2149 (defun gnus-convert-old-newsrc ()
2150   "Convert old newsrc into the new format, if needed."
2151   (let ((fcv (and gnus-newsrc-file-version
2152                   (gnus-continuum-version gnus-newsrc-file-version))))
2153     (cond
2154      ;; No .newsrc.eld file was loaded.
2155      ((null fcv) nil)
2156      ;; Gnus 5 .newsrc.eld was loaded.
2157      ((< fcv (gnus-continuum-version "September Gnus v0.1"))
2158       (gnus-convert-old-ticks)))))
2159
2160 (defun gnus-convert-old-ticks ()
2161   (let ((newsrc (cdr gnus-newsrc-alist))
2162         marks info dormant ticked)
2163     (while (setq info (pop newsrc))
2164       (when (setq marks (gnus-info-marks info))
2165         (setq dormant (cdr (assq 'dormant marks))
2166               ticked (cdr (assq 'tick marks)))
2167         (when (or dormant ticked)
2168           (gnus-info-set-read
2169            info
2170            (gnus-add-to-range
2171             (gnus-info-read info)
2172             (nconc (gnus-uncompress-range dormant)
2173                    (gnus-uncompress-range ticked)))))))))
2174
2175 (defun gnus-load (file)
2176   "Load FILE, but in such a way that read errors can be reported."
2177   (with-temp-buffer
2178     (insert-file-contents file)
2179     (while (not (eobp))
2180       (condition-case type
2181           (let ((form (read (current-buffer))))
2182             (eval form))
2183         (error
2184          (unless (eq (car type) 'end-of-file)
2185            (let ((error (format "Error in %s line %d" file
2186                                 (count-lines (point-min) (point)))))
2187              (ding)
2188              (unless (gnus-yes-or-no-p (concat error "; continue? "))
2189                (error "%s" error)))))))))
2190
2191 (defun gnus-read-newsrc-el-file (file)
2192   (let ((ding-file (concat file "d")))
2193     (when (file-exists-p ding-file)
2194       ;; We always, always read the .eld file.
2195       (gnus-message 5 "Reading %s..." ding-file)
2196       (let (gnus-newsrc-assoc)
2197         (let ((coding-system-for-read gnus-ding-file-coding-system))
2198           (gnus-load ding-file))
2199         ;; Older versions of `gnus-format-specs' are no longer valid
2200         ;; in Oort Gnus 0.01.
2201         (let ((version
2202                (and gnus-newsrc-file-version
2203                     (gnus-continuum-version gnus-newsrc-file-version))))
2204           (when (or (not version)
2205                     (< version 5.090009))
2206             (setq gnus-format-specs gnus-default-format-specs)))
2207         (when gnus-newsrc-assoc
2208           (setq gnus-newsrc-alist gnus-newsrc-assoc))))
2209     (gnus-make-hashtable-from-newsrc-alist)
2210     (when (file-newer-than-file-p file ding-file)
2211       ;; Old format quick file
2212       (gnus-message 5 "Reading %s..." file)
2213       ;; The .el file is newer than the .eld file, so we read that one
2214       ;; as well.
2215       (gnus-read-old-newsrc-el-file file))))
2216
2217 ;; Parse the old-style quick startup file
2218 (defun gnus-read-old-newsrc-el-file (file)
2219   (let (newsrc killed marked group m info)
2220     (prog1
2221         (let ((gnus-killed-assoc nil)
2222               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
2223           (prog1
2224               (ignore-errors
2225                 (load file t t t))
2226             (setq newsrc gnus-newsrc-assoc
2227                   killed gnus-killed-assoc
2228                   marked gnus-marked-assoc)))
2229       (setq gnus-newsrc-alist nil)
2230       (while (setq group (pop newsrc))
2231         (if (setq info (gnus-get-info (car group)))
2232             (progn
2233               (gnus-info-set-read info (cddr group))
2234               (gnus-info-set-level
2235                info (if (nth 1 group) gnus-level-default-subscribed
2236                       gnus-level-default-unsubscribed))
2237               (push info gnus-newsrc-alist))
2238           (push (setq info
2239                       (list (car group)
2240                             (if (nth 1 group) gnus-level-default-subscribed
2241                               gnus-level-default-unsubscribed)
2242                             (cddr group)))
2243                 gnus-newsrc-alist))
2244         ;; Copy marks into info.
2245         (when (setq m (assoc (car group) marked))
2246           (unless (nthcdr 3 info)
2247             (nconc info (list nil)))
2248           (gnus-info-set-marks
2249            info (list (cons 'tick (gnus-compress-sequence
2250                                    (sort (cdr m) '<) t))))))
2251       (setq newsrc killed)
2252       (while newsrc
2253         (setcar newsrc (caar newsrc))
2254         (setq newsrc (cdr newsrc)))
2255       (setq gnus-killed-list killed))
2256     ;; The .el file version of this variable does not begin with
2257     ;; "options", while the .eld version does, so we just add it if it
2258     ;; isn't there.
2259     (when
2260         gnus-newsrc-options
2261       (when (not (string-match "^ *options" gnus-newsrc-options))
2262         (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
2263       (when (not (string-match "\n$" gnus-newsrc-options))
2264         (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
2265       ;; Finally, if we read some options lines, we parse them.
2266       (unless (string= gnus-newsrc-options "")
2267         (gnus-newsrc-parse-options gnus-newsrc-options)))
2268
2269     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
2270     (gnus-make-hashtable-from-newsrc-alist)))
2271
2272 (defun gnus-make-newsrc-file (file)
2273   "Make server dependent file name by catenating FILE and server host name."
2274   (let* ((file (expand-file-name file nil))
2275          (real-file (concat file "-" (nth 1 gnus-select-method))))
2276     (if (or (file-exists-p real-file)
2277             (file-exists-p (concat real-file ".el"))
2278             (file-exists-p (concat real-file ".eld")))
2279         real-file
2280       file)))
2281
2282 (defun gnus-newsrc-to-gnus-format ()
2283   (setq gnus-newsrc-options "")
2284   (setq gnus-newsrc-options-n nil)
2285
2286   (unless gnus-active-hashtb
2287     (setq gnus-active-hashtb (gnus-make-hashtable 4096)))
2288   (let ((buf (current-buffer))
2289         (already-read (> (length gnus-newsrc-alist) 1))
2290         group subscribed options-symbol newsrc Options-symbol
2291         symbol reads num1)
2292     (goto-char (point-min))
2293     ;; We intern the symbol `options' in the active hashtb so that we
2294     ;; can `eq' against it later.
2295     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
2296     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
2297
2298     (while (not (eobp))
2299       ;; We first read the first word on the line by narrowing and
2300       ;; then reading into `gnus-active-hashtb'.  Most groups will
2301       ;; already exist in that hashtb, so this will save some string
2302       ;; space.
2303       (narrow-to-region
2304        (point)
2305        (progn (skip-chars-forward "^ \t!:\n") (point)))
2306       (goto-char (point-min))
2307       (setq symbol
2308             (and (/= (point-min) (point-max))
2309                  (let ((obarray gnus-active-hashtb)) (read buf))))
2310       (widen)
2311       ;; Now, the symbol we have read is either `options' or a group
2312       ;; name.  If it is an options line, we just add it to a string.
2313       (cond
2314        ((or (eq symbol options-symbol)
2315             (eq symbol Options-symbol))
2316         (setq gnus-newsrc-options
2317               ;; This concating is quite inefficient, but since our
2318               ;; thorough studies show that approx 99.37% of all
2319               ;; .newsrc files only contain a single options line, we
2320               ;; don't give a damn, frankly, my dear.
2321               (concat gnus-newsrc-options
2322                       (buffer-substring
2323                        (gnus-point-at-bol)
2324                        ;; Options may continue on the next line.
2325                        (or (and (re-search-forward "^[^ \t]" nil 'move)
2326                                 (progn (beginning-of-line) (point)))
2327                            (point)))))
2328         (forward-line -1))
2329        (symbol
2330         ;; Group names can be just numbers.
2331         (when (numberp symbol)
2332           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
2333         (unless (boundp symbol)
2334           (set symbol nil))
2335         ;; It was a group name.
2336         (setq subscribed (eq (char-after) ?:)
2337               group (symbol-name symbol)
2338               reads nil)
2339         (if (eolp)
2340             ;; If the line ends here, this is clearly a buggy line, so
2341             ;; we put point a the beginning of line and let the cond
2342             ;; below do the error handling.
2343             (beginning-of-line)
2344           ;; We skip to the beginning of the ranges.
2345           (skip-chars-forward "!: \t"))
2346         ;; We are now at the beginning of the list of read articles.
2347         ;; We read them range by range.
2348         (while
2349             (cond
2350              ((looking-at "[0-9]+")
2351               ;; We narrow and read a number instead of buffer-substring/
2352               ;; string-to-int because it's faster.  narrow/widen is
2353               ;; faster than save-restriction/narrow, and save-restriction
2354               ;; produces a garbage object.
2355               (setq num1 (progn
2356                            (narrow-to-region (match-beginning 0) (match-end 0))
2357                            (read buf)))
2358               (widen)
2359               ;; If the next character is a dash, then this is a range.
2360               (if (eq (char-after) ?-)
2361                   (progn
2362                     ;; We read the upper bound of the range.
2363                     (forward-char 1)
2364                     (if (not (looking-at "[0-9]+"))
2365                         ;; This is a buggy line, by we pretend that
2366                         ;; it's kinda OK.  Perhaps the user should be
2367                         ;; dinged?
2368                         (push num1 reads)
2369                       (push
2370                        (cons num1
2371                              (progn
2372                                (narrow-to-region (match-beginning 0)
2373                                                  (match-end 0))
2374                                (read buf)))
2375                        reads)
2376                       (widen)))
2377                 ;; It was just a simple number, so we add it to the
2378                 ;; list of ranges.
2379                 (push num1 reads))
2380               ;; If the next char in ?\n, then we have reached the end
2381               ;; of the line and return nil.
2382               (not (eq (char-after) ?\n)))
2383              ((eq (char-after) ?\n)
2384               ;; End of line, so we end.
2385               nil)
2386              (t
2387               ;; Not numbers and not eol, so this might be a buggy
2388               ;; line...
2389               (unless (eobp)
2390                 ;; If it was eob instead of ?\n, we allow it.
2391                 ;; The line was buggy.
2392                 (setq group nil)
2393                 (gnus-error 3.1 "Mangled line: %s"
2394                             (buffer-substring (gnus-point-at-bol)
2395                                               (gnus-point-at-eol))))
2396               nil))
2397           ;; Skip past ", ".  Spaces are invalid in these ranges, but
2398           ;; we allow them, because it's a common mistake to put a
2399           ;; space after the comma.
2400           (skip-chars-forward ", "))
2401
2402         ;; We have already read .newsrc.eld, so we gently update the
2403         ;; data in the hash table with the information we have just
2404         ;; read.
2405         (when group
2406           (let ((info (gnus-get-info group))
2407                 level)
2408             (if info
2409                 ;; There is an entry for this file in the alist.
2410                 (progn
2411                   (gnus-info-set-read info (nreverse reads))
2412                   ;; We update the level very gently.  In fact, we
2413                   ;; only change it if there's been a status change
2414                   ;; from subscribed to unsubscribed, or vice versa.
2415                   (setq level (gnus-info-level info))
2416                   (cond ((and (<= level gnus-level-subscribed)
2417                               (not subscribed))
2418                          (setq level (if reads
2419                                          gnus-level-default-unsubscribed
2420                                        (1+ gnus-level-default-unsubscribed))))
2421                         ((and (> level gnus-level-subscribed) subscribed)
2422                          (setq level gnus-level-default-subscribed)))
2423                   (gnus-info-set-level info level))
2424               ;; This is a new group.
2425               (setq info (list group
2426                                (if subscribed
2427                                    gnus-level-default-subscribed
2428                                  (if reads
2429                                      (1+ gnus-level-subscribed)
2430                                    gnus-level-default-unsubscribed))
2431                                (nreverse reads))))
2432             (push info newsrc)))))
2433       (forward-line 1))
2434
2435     (setq newsrc (nreverse newsrc))
2436
2437     (if (not already-read)
2438         ()
2439       ;; We now have two newsrc lists - `newsrc', which is what we
2440       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
2441       ;; what we've read from .newsrc.eld.  We have to merge these
2442       ;; lists.  We do this by "attaching" any (foreign) groups in the
2443       ;; gnus-newsrc-alist to the (native) group that precedes them.
2444       (let ((rc (cdr gnus-newsrc-alist))
2445             (prev gnus-newsrc-alist)
2446             entry mentry)
2447         (while rc
2448           (or (null (nth 4 (car rc)))   ; It's a native group.
2449               (assoc (caar rc) newsrc)  ; It's already in the alist.
2450               (if (setq entry (assoc (caar prev) newsrc))
2451                   (setcdr (setq mentry (memq entry newsrc))
2452                           (cons (car rc) (cdr mentry)))
2453                 (push (car rc) newsrc)))
2454           (setq prev rc
2455                 rc (cdr rc)))))
2456
2457     (setq gnus-newsrc-alist newsrc)
2458     ;; We make the newsrc hashtb.
2459     (gnus-make-hashtable-from-newsrc-alist)
2460
2461     ;; Finally, if we read some options lines, we parse them.
2462     (unless (string= gnus-newsrc-options "")
2463       (gnus-newsrc-parse-options gnus-newsrc-options))))
2464
2465 ;; Parse options lines to find "options -n !all rec.all" and stuff.
2466 ;; The return value will be a list on the form
2467 ;; ((regexp1 . ignore)
2468 ;;  (regexp2 . subscribe)...)
2469 ;; When handling new newsgroups, groups that match a `ignore' regexp
2470 ;; will be ignored, and groups that match a `subscribe' regexp will be
2471 ;; subscribed.  A line like
2472 ;; options -n !all rec.all
2473 ;; will lead to a list that looks like
2474 ;; (("^rec\\..+" . subscribe)
2475 ;;  ("^.+" . ignore))
2476 ;; So all "rec.*" groups will be subscribed, while all the other
2477 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
2478 ;; different from "options -n rec.all !all".
2479 (defun gnus-newsrc-parse-options (options)
2480   (let (out eol)
2481     (save-excursion
2482       (gnus-set-work-buffer)
2483       (insert (regexp-quote options))
2484       ;; First we treat all continuation lines.
2485       (goto-char (point-min))
2486       (while (re-search-forward "\n[ \t]+" nil t)
2487         (replace-match " " t t))
2488       ;; Then we transform all "all"s into ".+"s.
2489       (goto-char (point-min))
2490       (while (re-search-forward "\\ball\\b" nil t)
2491         (replace-match ".+" t t))
2492       (goto-char (point-min))
2493       ;; We remove all other options than the "-n" ones.
2494       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
2495         (replace-match " ")
2496         (forward-char -1))
2497       (goto-char (point-min))
2498
2499       ;; We are only interested in "options -n" lines - we
2500       ;; ignore the other option lines.
2501       (while (re-search-forward "[ \t]-n" nil t)
2502         (setq eol
2503               (or (save-excursion
2504                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
2505                          (- (point) 2)))
2506                   (gnus-point-at-eol)))
2507         ;; Search for all "words"...
2508         (while (re-search-forward "[^ \t,\n]+" eol t)
2509           (if (eq (char-after (match-beginning 0)) ?!)
2510               ;; If the word begins with a bang (!), this is a "not"
2511               ;; spec.  We put this spec (minus the bang) and the
2512               ;; symbol `ignore' into the list.
2513               (push (cons (concat
2514                            "^" (buffer-substring
2515                                 (1+ (match-beginning 0))
2516                                 (match-end 0))
2517                            "\\($\\|\\.\\)")
2518                           'ignore)
2519                     out)
2520             ;; There was no bang, so this is a "yes" spec.
2521             (push (cons (concat "^" (match-string 0) "\\($\\|\\.\\)")
2522                         'subscribe)
2523                   out))))
2524
2525       (setq gnus-newsrc-options-n out))))
2526
2527 (defun gnus-save-newsrc-file (&optional force)
2528   "Save .newsrc file."
2529   ;; Note: We cannot save .newsrc file if all newsgroups are removed
2530   ;; from the variable gnus-newsrc-alist.
2531   (when (and (or gnus-newsrc-alist gnus-killed-list)
2532              gnus-current-startup-file)
2533     (save-excursion
2534       (if (and (or gnus-use-dribble-file gnus-slave)
2535                (not force)
2536                (or (not gnus-dribble-buffer)
2537                    (not (buffer-name gnus-dribble-buffer))
2538                    (zerop (save-excursion
2539                             (set-buffer gnus-dribble-buffer)
2540                             (buffer-size)))))
2541           (gnus-message 4 "(No changes need to be saved)")
2542         (gnus-run-hooks 'gnus-save-newsrc-hook)
2543         (if gnus-slave
2544             (gnus-slave-save-newsrc)
2545           ;; Save .newsrc.
2546           (when gnus-save-newsrc-file
2547             (gnus-message 8 "Saving %s..." gnus-current-startup-file)
2548             (gnus-gnus-to-newsrc-format)
2549             (gnus-message 8 "Saving %s...done" gnus-current-startup-file))
2550           ;; Save .newsrc.eld.
2551           (set-buffer (gnus-get-buffer-create " *Gnus-newsrc*"))
2552           (make-local-variable 'version-control)
2553           (setq version-control gnus-backup-startup-file)
2554           (setq buffer-file-name
2555                 (concat gnus-current-startup-file ".eld"))
2556           (setq default-directory (file-name-directory buffer-file-name))
2557           (buffer-disable-undo)
2558           (erase-buffer)
2559           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
2560           (gnus-gnus-to-quick-newsrc-format)
2561           (gnus-run-hooks 'gnus-save-quick-newsrc-hook)
2562           (let ((coding-system-for-write gnus-ding-file-coding-system))
2563             (save-buffer))
2564           (gnus-kill-buffer (current-buffer))
2565           (gnus-message
2566            5 "Saving %s.eld...done" gnus-current-startup-file))
2567         (gnus-dribble-delete-file)
2568         (gnus-group-set-mode-line)))))
2569
2570 (defun gnus-gnus-to-quick-newsrc-format ()
2571   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
2572   (let ((print-quoted t)
2573         (print-escape-newlines t))
2574
2575     (insert ";; -*- emacs-lisp -*-\n")
2576     (insert ";; Gnus startup file.\n")
2577     (insert "\
2578 ;; Never delete this file -- if you want to force Gnus to read the
2579 ;; .newsrc file (if you have one), touch .newsrc instead.\n")
2580     (insert "(setq gnus-newsrc-file-version "
2581             (gnus-prin1-to-string gnus-version) ")\n")
2582     (let* ((gnus-killed-list
2583             (if (and gnus-save-killed-list
2584                      (stringp gnus-save-killed-list))
2585                 (gnus-strip-killed-list)
2586               gnus-killed-list))
2587            (variables
2588             (if gnus-save-killed-list gnus-variable-list
2589               ;; Remove the `gnus-killed-list' from the list of variables
2590               ;; to be saved, if required.
2591               (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
2592            ;; Peel off the "dummy" group.
2593            (gnus-newsrc-alist (cdr gnus-newsrc-alist))
2594            variable)
2595       ;; Insert the variables into the file.
2596       (while variables
2597         (when (and (boundp (setq variable (pop variables)))
2598                    (symbol-value variable))
2599           (insert "(setq " (symbol-name variable) " '")
2600           (gnus-prin1 (symbol-value variable))
2601           (insert ")\n"))))))
2602
2603 (defun gnus-strip-killed-list ()
2604   "Return the killed list minus the groups that match `gnus-save-killed-list'."
2605   (let ((list gnus-killed-list)
2606         olist)
2607     (while list
2608       (when (string-match gnus-save-killed-list (car list))
2609         (push (car list) olist))
2610       (pop list))
2611     (nreverse olist)))
2612
2613 (defun gnus-gnus-to-newsrc-format ()
2614   ;; Generate and save the .newsrc file.
2615   (save-excursion
2616     (set-buffer (create-file-buffer gnus-current-startup-file))
2617     (let ((newsrc (cdr gnus-newsrc-alist))
2618           (standard-output (current-buffer))
2619           info ranges range method)
2620       (setq buffer-file-name gnus-current-startup-file)
2621       (setq default-directory (file-name-directory buffer-file-name))
2622       (buffer-disable-undo)
2623       (erase-buffer)
2624       ;; Write options.
2625       (when gnus-newsrc-options
2626         (insert gnus-newsrc-options))
2627       ;; Write subscribed and unsubscribed.
2628       (while (setq info (pop newsrc))
2629         ;; Don't write foreign groups to .newsrc.
2630         (when (or (null (setq method (gnus-info-method info)))
2631                   (equal method "native")
2632                   (inline (gnus-server-equal method gnus-select-method)))
2633           (insert (gnus-info-group info)
2634                   (if (> (gnus-info-level info) gnus-level-subscribed)
2635                       "!" ":"))
2636           (when (setq ranges (gnus-info-read info))
2637             (insert " ")
2638             (if (not (listp (cdr ranges)))
2639                 (if (= (car ranges) (cdr ranges))
2640                     (princ (car ranges))
2641                   (princ (car ranges))
2642                   (insert "-")
2643                   (princ (cdr ranges)))
2644               (while (setq range (pop ranges))
2645                 (if (or (atom range) (= (car range) (cdr range)))
2646                     (princ (or (and (atom range) range) (car range)))
2647                   (princ (car range))
2648                   (insert "-")
2649                   (princ (cdr range)))
2650                 (when ranges
2651                   (insert ",")))))
2652           (insert "\n")))
2653       (make-local-variable 'version-control)
2654       (setq version-control 'never)
2655       ;; It has been reported that sometime the modtime on the .newsrc
2656       ;; file seems to be off.  We really do want to overwrite it, so
2657       ;; we clear the modtime here before saving.  It's a bit odd,
2658       ;; though...
2659       ;; sometimes the modtime clear isn't sufficient.  most brute force:
2660       ;; delete the silly thing entirely first.  but this fails to provide
2661       ;; such niceties as .newsrc~ creation.
2662       (if gnus-modtime-botch
2663           (delete-file gnus-startup-file)
2664         (clear-visited-file-modtime))
2665       (gnus-run-hooks 'gnus-save-standard-newsrc-hook)
2666       (save-buffer)
2667       (kill-buffer (current-buffer)))))
2668
2669 \f
2670 ;;;
2671 ;;; Slave functions.
2672 ;;;
2673
2674 (defvar gnus-slave-mode nil)
2675
2676 (defun gnus-slave-mode ()
2677   "Minor mode for slave Gnusae."
2678   (gnus-add-minor-mode 'gnus-slave-mode " Slave" (make-sparse-keymap))
2679   (gnus-run-hooks 'gnus-slave-mode-hook))
2680
2681 (defun gnus-slave-save-newsrc ()
2682   (save-excursion
2683     (set-buffer gnus-dribble-buffer)
2684     (let ((slave-name
2685            (mm-make-temp-file (concat gnus-current-startup-file "-slave-")))
2686           (modes (ignore-errors
2687                    (file-modes (concat gnus-current-startup-file ".eld")))))
2688       (let ((coding-system-for-write gnus-ding-file-coding-system))
2689         (gnus-write-buffer slave-name))
2690       (when modes
2691         (set-file-modes slave-name modes)))))
2692
2693 (defun gnus-master-read-slave-newsrc ()
2694   (let ((slave-files
2695          (directory-files
2696           (file-name-directory gnus-current-startup-file)
2697           t (concat
2698              "^" (regexp-quote
2699                   (concat
2700                    (file-name-nondirectory gnus-current-startup-file)
2701                    "-slave-")))
2702           t))
2703         file)
2704     (if (not slave-files)
2705         ()                              ; There are no slave files to read.
2706       (gnus-message 7 "Reading slave newsrcs...")
2707       (save-excursion
2708         (set-buffer (gnus-get-buffer-create " *gnus slave*"))
2709         (setq slave-files
2710               (sort (mapcar (lambda (file)
2711                               (list (nth 5 (file-attributes file)) file))
2712                             slave-files)
2713                     (lambda (f1 f2)
2714                       (or (< (caar f1) (caar f2))
2715                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
2716         (while slave-files
2717           (erase-buffer)
2718           (setq file (nth 1 (car slave-files)))
2719           (nnheader-insert-file-contents file)
2720           (when (condition-case ()
2721                     (progn
2722                       (eval-buffer (current-buffer))
2723                       t)
2724                   (error
2725                    (gnus-error 3.2 "Possible error in %s" file)
2726                    nil))
2727             (unless gnus-slave          ; Slaves shouldn't delete these files.
2728               (ignore-errors
2729                 (delete-file file))))
2730           (setq slave-files (cdr slave-files))))
2731       (gnus-dribble-touch)
2732       (gnus-message 7 "Reading slave newsrcs...done"))))
2733
2734 \f
2735 ;;;
2736 ;;; Group description.
2737 ;;;
2738
2739 (defun gnus-read-all-descriptions-files ()
2740   (let ((methods (cons gnus-select-method
2741                        (nconc
2742                         (when (gnus-archive-server-wanted-p)
2743                           (list "archive"))
2744                         gnus-secondary-select-methods))))
2745     (while methods
2746       (gnus-read-descriptions-file (car methods))
2747       (setq methods (cdr methods)))
2748     t))
2749
2750 (defun gnus-read-descriptions-file (&optional method)
2751   (let ((method (or method gnus-select-method))
2752         group)
2753     (when (stringp method)
2754       (setq method (gnus-server-to-method method)))
2755     ;; We create the hashtable whether we manage to read the desc file
2756     ;; to avoid trying to re-read after a failed read.
2757     (unless gnus-description-hashtb
2758       (setq gnus-description-hashtb
2759             (gnus-make-hashtable (length gnus-active-hashtb))))
2760     ;; Mark this method's desc file as read.
2761     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
2762                   gnus-description-hashtb)
2763
2764     (gnus-message 5 "Reading descriptions file via %s..." (car method))
2765     (cond
2766      ((null (gnus-get-function method 'request-list-newsgroups t))
2767       t)
2768      ((not (gnus-check-server method))
2769       (gnus-message 1 "Couldn't open server")
2770       nil)
2771      ((not (gnus-request-list-newsgroups method))
2772       (gnus-message 1 "Couldn't read newsgroups descriptions")
2773       nil)
2774      (t
2775       (save-excursion
2776         (save-restriction
2777           (set-buffer nntp-server-buffer)
2778           (goto-char (point-min))
2779           (when (or (search-forward "\n.\n" nil t)
2780                     (goto-char (point-max)))
2781             (beginning-of-line)
2782             (narrow-to-region (point-min) (point)))
2783           ;; If these are groups from a foreign select method, we insert the
2784           ;; group prefix in front of the group names.
2785           (and method (not (inline
2786                              (gnus-server-equal
2787                               (gnus-server-get-method nil method)
2788                               (gnus-server-get-method
2789                                nil gnus-select-method))))
2790                (let ((prefix (gnus-group-prefixed-name "" method)))
2791                  (goto-char (point-min))
2792                  (while (and (not (eobp))
2793                              (progn (insert prefix)
2794                                     (zerop (forward-line 1)))))))
2795           (goto-char (point-min))
2796           (while (not (eobp))
2797             ;; If we get an error, we set group to 0, which is not a
2798             ;; symbol...
2799             (setq group
2800                   (condition-case ()
2801                       (let ((obarray gnus-description-hashtb))
2802                         ;; Group is set to a symbol interned in this
2803                         ;; hash table.
2804                         (read nntp-server-buffer))
2805                     (error 0)))
2806             (skip-chars-forward " \t")
2807             ;; ...  which leads to this line being effectively ignored.
2808             (when (symbolp group)
2809               (let* ((str (buffer-substring
2810                            (point) (progn (end-of-line) (point))))
2811                      (name (symbol-name group))
2812                      (charset
2813                       (or (gnus-group-name-charset method name)
2814                           (gnus-parameter-charset name)
2815                           gnus-default-charset)))
2816                 (when (and str charset (featurep 'mule))
2817                   (setq str (mm-decode-coding-string str charset)))
2818                 (set group str)))
2819             (forward-line 1))))
2820       (gnus-message 5 "Reading descriptions file...done")
2821       t))))
2822
2823 (defun gnus-group-get-description (group)
2824   "Get the description of a group by sending XGTITLE to the server."
2825   (when (gnus-request-group-description group)
2826     (save-excursion
2827       (set-buffer nntp-server-buffer)
2828       (goto-char (point-min))
2829       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
2830         (match-string 1)))))
2831
2832 ;;;###autoload
2833 (defun gnus-declare-backend (name &rest abilities)
2834   "Declare backend NAME with ABILITIES as a Gnus backend."
2835   (setq gnus-valid-select-methods
2836         (nconc gnus-valid-select-methods
2837                (list (apply 'list name abilities))))
2838   (gnus-redefine-select-method-widget))
2839
2840 (defun gnus-set-default-directory ()
2841   "Set the default directory in the current buffer to `gnus-default-directory'.
2842 If this variable is nil, don't do anything."
2843   (setq default-directory
2844         (if (and gnus-default-directory
2845                  (file-exists-p gnus-default-directory))
2846             (file-name-as-directory (expand-file-name gnus-default-directory))
2847           default-directory)))
2848
2849 (defun gnus-display-time-event-handler ()
2850   "Like `display-time-event-handler', but test `display-time-timer'."
2851   (when (gnus-boundp 'display-time-timer)
2852     (display-time-event-handler)))
2853
2854 ;;;###autoload
2855 (defun gnus-fixup-nnimap-unread-after-getting-new-news ()
2856   (let (server group info)
2857     (mapatoms
2858      (lambda (sym)
2859        (when (and (setq group (symbol-name sym))
2860                   (gnus-group-entry group)
2861                   (setq info (symbol-value sym)))
2862          (gnus-sethash group (cons (nth 2 info) (cdr (gnus-group-entry group)))
2863                        gnus-newsrc-hashtb)))
2864      (if (boundp 'nnimap-mailbox-info)
2865          (symbol-value 'nnimap-mailbox-info)
2866        (make-vector 1 0)))))
2867
2868
2869 (provide 'gnus-start)
2870
2871 ;;; gnus-start.el ends here
2872
2873