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