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