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