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