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