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