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