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