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