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