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