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