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