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