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