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