*** empty log message ***
[gnus] / lisp / gnus.el
1 ;;; gnus.el --- a newsreader for GNU Emacs
2 ;; Copyright (C) 1987,88,89,90,93,94,95 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;; Although Gnus looks suspiciously like GNUS, it isn't quite the same
27 ;; beast. Most internal structures have been changed. If you have
28 ;; written packages that depend on any of the hash tables,
29 ;; `gnus-newsrc-alist', `gnus-killed-assoc', marked lists, the .newsrc
30 ;; buffer, or internal knowledge of the `nntp-header-' macros, or
31 ;; dependence on the buffers having a certain format, your code will
32 ;; fail.
33
34 ;;; Code:
35
36 (eval '(run-hooks 'gnus-load-hook))
37
38 (require 'mail-utils)
39 (require 'timezone)
40 (require 'nnheader)
41
42 (eval-when-compile (require 'cl))
43
44 ;; Site dependent variables. These variables should be defined in
45 ;; paths.el.
46
47 (defvar gnus-default-nntp-server nil
48   "Specify a default NNTP server.
49 This variable should be defined in paths.el, and should never be set
50 by the user.
51 If you want to change servers, you should use `gnus-select-method'.
52 See the documentation to that variable.")
53
54 (defconst gnus-backup-default-subscribed-newsgroups 
55   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
56   "Default default new newsgroups the first time Gnus is run.
57 Should be set in paths.el, and shouldn't be touched by the user.")
58
59 (defvar gnus-local-domain nil
60   "Local domain name without a host name.
61 The DOMAINNAME environment variable is used instead if it is defined.
62 If the `system-name' function returns the full Internet name, there is
63 no need to set this variable.")
64
65 (defvar gnus-local-organization nil
66   "String with a description of what organization (if any) the user belongs to.
67 The ORGANIZATION environment variable is used instead if it is defined.
68 If this variable contains a function, this function will be called
69 with the current newsgroup name as the argument. The function should
70 return a string.
71
72 In any case, if the string (either in the variable, in the environment
73 variable, or returned by the function) is a file name, the contents of
74 this file will be used as the organization.")
75
76 (defvar gnus-use-generic-from nil
77   "If nil, the full host name will be the system name prepended to the domain name.
78 If this is a string, the full host name will be this string.
79 If this is non-nil, non-string, the domain name will be used as the
80 full host name.")
81
82 (defvar gnus-use-generic-path nil
83   "If nil, use the NNTP server name in the Path header.
84 If stringp, use this; if non-nil, use no host name (user name only).")
85
86
87 ;; Customization variables
88
89 ;; Don't touch this variable.
90 (defvar gnus-nntp-service "nntp"
91   "*NNTP service name (\"nntp\" or 119).
92 This is an obsolete variable, which is scarcely used. If you use an
93 nntp server for your newsgroup and want to change the port number
94 used to 899, you would say something along these lines:
95
96  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
97
98 (defvar gnus-nntpserver-file "/etc/nntpserver"
99   "*A file with only the name of the nntp server in it.")
100
101 ;; This function is used to check both the environment variable
102 ;; NNTPSERVER and the /etc/nntpserver file to see whether one can find
103 ;; an nntp server name default.
104 (defun gnus-getenv-nntpserver ()
105   (or (getenv "NNTPSERVER")
106       (and (file-readable-p gnus-nntpserver-file)
107            (save-excursion
108              (set-buffer (get-buffer-create " *gnus nntp*"))
109              (buffer-disable-undo (current-buffer))
110              (insert-file-contents gnus-nntpserver-file)
111              (let ((name (buffer-string)))
112                (prog1
113                    (if (string-match "^[ \t\n]*$" name)
114                        nil
115                      name)
116                  (kill-buffer (current-buffer))))))))
117                  
118 (defvar gnus-select-method 
119   (nconc
120    (list 'nntp (or (condition-case ()
121                        (gnus-getenv-nntpserver)
122                      (error nil))
123                    (if (and gnus-default-nntp-server
124                             (not (string= gnus-default-nntp-server "")))
125                        gnus-default-nntp-server)
126                    (system-name)))
127    (if (or (null gnus-nntp-service)
128            (equal gnus-nntp-service "nntp"))
129        nil 
130      (list gnus-nntp-service)))
131   "*Default method for selecting a newsgroup.
132 This variable should be a list, where the first element is how the
133 news is to be fetched, the second is the address. 
134
135 For instance, if you want to get your news via NNTP from
136 \"flab.flab.edu\", you could say:
137
138 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
139
140 If you want to use your local spool, say:
141
142 (setq gnus-select-method (list 'nnspool (system-name)))
143
144 If you use this variable, you must set `gnus-nntp-server' to nil.
145
146 There is a lot more to know about select methods and virtual servers -
147 see the manual for details.")
148
149 ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
150 (defvar gnus-post-method nil
151   "*Preferred method for posting USENET news.
152 If this variable is nil, Gnus will use the current method to decide
153 which method to use when posting.  If it is non-nil, it will override
154 the current method.  This method will not be used in mail groups and
155 the like, only in \"real\" newsgroups.
156
157 The value must be a valid method as discussed in the documentation of
158 `gnus-select-method'.")
159
160 (defvar gnus-refer-article-method nil
161   "*Preferred method for fetching an article by Message-ID.
162 If you are reading news from the local spool (with nnspool), fetching
163 articles by Message-ID is painfully slow. By setting this method to an
164 nntp method, you might get acceptable results.
165
166 The value of this variable must be a valid select method as discussed
167 in the documentation of `gnus-select-method'")
168
169 (defvar gnus-secondary-select-methods nil
170   "*A list of secondary methods that will be used for reading news.
171 This is a list where each element is a complete select method (see
172 `gnus-select-method').  
173
174 If, for instance, you want to read your mail with the nnml backend,
175 you could set this variable:
176
177 (setq gnus-secondary-select-methods '((nnml \"\")))")
178
179 (defvar gnus-secondary-servers nil
180   "*List of NNTP servers that the user can choose between interactively.
181 To make Gnus query you for a server, you have to give `gnus' a
182 non-numeric prefix - `C-u M-x gnus', in short.")
183
184 (defvar gnus-nntp-server nil
185   "*The name of the host running the NNTP server.
186 This variable is semi-obsolete. Use the `gnus-select-method'
187 variable instead.")
188
189 (defvar gnus-startup-file "~/.newsrc"
190   "*Your `.newsrc' file.
191 `.newsrc-SERVER' will be used instead if that exists.")
192
193 (defvar gnus-init-file "~/.gnus"
194   "*Your Gnus elisp startup file.
195 If a file with the .el or .elc suffixes exist, it will be read
196 instead.") 
197
198 (defvar gnus-group-faq-directory
199   '("/ftp@mirrors.aol.com:/pub/rtfm/usenet/"
200     "/ftp@ftp.uu.net:/usenet/news.answers/"
201     "/ftp@ftp.seas.gwu.edu:/pub/rtfm/"
202     "/ftp@rtfm.mit.edu:/pub/usenet/news.answers/"
203     "/ftp@ftp.uni-paderborn.de:/pub/FAQ/"
204     "/ftp@ftp.Germany.EU.net:/pub/newsarchive/news.answers/"
205     "/ftp@ftp.sunet.se:/pub/usenet/"
206     "/ftp@nctuccca.edu.tw:/USENET/FAQ/"
207     "/ftp@hwarang.postech.ac.kr:/pub/usenet/news.answers/"
208     "/ftp@ftp.hk.super.net:/mirror/faqs/")
209   "*Directory where the group FAQs are stored.
210 This will most commonly be on a remote machine, and the file will be
211 fetched by ange-ftp.
212
213 This variable can also be a list of directories.  In that case, the
214 first element in the list will be used by default, and the others will
215 be used as backup sites.
216
217 Note that Gnus uses an aol machine as the default directory.  If this
218 feels fundamentally unclean, just think of it as a way to finally get
219 something of value back from them.
220
221 If the default site is too slow, try one of these:
222
223    North America: ftp.uu.net                     /usenet/news.answers
224                   mirrors.aol.com                /pub/rtfm/usenet
225                   ftp.seas.gwu.edu               /pub/rtfm
226                   rtfm.mit.edu                   /pub/usenet/news.answers
227    Europe:        ftp.uni-paderborn.de           /pub/FAQ
228                   ftp.Germany.EU.net             /pub/newsarchive/news.answers
229                   ftp.sunet.se                   /pub/usenet
230    Asia:          nctuccca.edu.tw                /USENET/FAQ
231                   hwarang.postech.ac.kr          /pub/usenet/news.answers
232                   ftp.hk.super.net               /mirror/faqs")
233
234 (defvar gnus-group-archive-directory
235   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/" 
236   "*The address of the (ding) archives.")
237
238 (defvar gnus-group-recent-archive-directory
239   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list-recent/"
240   "*The address of the most recent (ding) articles.")
241
242 (defvar gnus-default-subscribed-newsgroups nil
243   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
244 It should be a list of strings.
245 If it is `t', Gnus will not do anything special the first time it is
246 started; it'll just use the normal newsgroups subscription methods.")
247
248 (defvar gnus-use-cross-reference t
249   "*Non-nil means that cross referenced articles will be marked as read.
250 If nil, ignore cross references.  If t, mark articles as read in
251 subscribed newsgroups. If neither t nor nil, mark as read in all
252 newsgroups.") 
253
254 (defvar gnus-use-dribble-file t
255   "*Non-nil means that Gnus will use a dribble file to store user updates.
256 If Emacs should crash without saving the .newsrc files, complete
257 information can be restored from the dribble file.")
258
259 (defvar gnus-dribble-directory nil
260   "*The directory where dribble files will be saved.
261 If this variable is nil, the directory where the .newsrc files are
262 saved will be used.")
263
264 (defvar gnus-asynchronous nil
265   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
266
267 (defvar gnus-score-file-single-match-alist nil
268   "*Alist mapping regexps to lists of score files.
269 Each element of this alist should be of the form
270         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
271
272 If the name of a group is matched by REGEXP, the corresponding scorefiles
273 will be used for that group.
274 The first match found is used, subsequent matching entries are ignored (to
275 use multiple matches, see gnus-score-file-multiple-match-alist).
276
277 These score files are loaded in addition to any files returned by
278 gnus-score-find-score-files-function (which see).")
279
280 (defvar gnus-score-file-multiple-match-alist nil
281   "*Alist mapping regexps to lists of score files.
282 Each element of this alist should be of the form
283         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
284
285 If the name of a group is matched by REGEXP, the corresponding scorefiles
286 will be used for that group.
287 If multiple REGEXPs match a group, the score files corresponding to each
288 match will be used (for only one match to be used, see
289 gnus-score-file-single-match-alist).
290
291 These score files are loaded in addition to any files returned by
292 gnus-score-find-score-files-function (which see).")
293
294 (defvar gnus-score-file-suffix "SCORE"
295   "*Suffix of the score files.")
296
297 (defvar gnus-adaptive-file-suffix "ADAPT"
298   "*Suffix of the adaptive score files.")
299
300 (defvar gnus-score-find-score-files-function 'gnus-score-find-bnews
301   "*Function used to find score files.
302 The function will be called with the group name as the argument, and
303 should return a list of score files to apply to that group.  The score
304 files do not actually have to exist.
305
306 Predefined values are:
307
308 gnus-score-find-single: Only apply the group's own score file.
309 gnus-score-find-hierarchical: Also apply score files from parent groups.
310 gnus-score-find-bnews: Apply score files whose names matches.
311
312 See the documentation to these functions for more information.
313
314 This variable can also be a list of functions to be called.  Each
315 function should either return a list of score files, or a list of
316 score alists.")
317
318 (defvar gnus-score-interactive-default-score 1000
319   "*Scoring commands will raise/lower the score with this number as the default.")
320
321 (defvar gnus-large-newsgroup 200
322   "*The number of articles which indicates a large newsgroup.
323 If the number of articles in a newsgroup is greater than this value,
324 confirmation is required for selecting the newsgroup.")
325
326 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
327 (defvar gnus-no-groups-message "No news is horrible news"
328   "*Message displayed by Gnus when no groups are available.")
329
330 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
331   "*Non-nil means that the default name of a file to save articles in is the group name.
332 If it's nil, the directory form of the group name is used instead.
333
334 If this variable is a list, and the list contains the element
335 `not-score', long file names will not be used for score files; if it
336 contains the element `not-save', long file names will not be used for
337 saving; and if it contains the element `not-kill', long file names
338 will not be used for kill files.")
339
340 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
341   "*Name of the directory articles will be saved in (default \"~/News\").
342 Initialized from the SAVEDIR environment variable.")
343
344 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
345   "*Name of the directory where kill files will be stored (default \"~/News\").
346 Initialized from the SAVEDIR environment variable.")
347
348 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
349   "*A function to save articles in your favorite format.
350 The function must be interactively callable (in other words, it must
351 be an Emacs command).
352
353 Gnus provides the following functions:
354
355 * gnus-summary-save-in-rmail (Rmail format)
356 * gnus-summary-save-in-mail (Unix mail format)
357 * gnus-summary-save-in-folder (MH folder)
358 * gnus-summary-save-in-file (article format).
359 * gnus-summary-save-in-vm (use VM's folder format).")
360
361 (defvar gnus-prompt-before-saving 'always
362   "*This variable says how much prompting is to be done when saving articles.
363 If it is nil, no prompting will be done, and the articles will be
364 saved to the default files.  If this variable is `always', each and
365 every article that is saved will be preceded by a prompt, even when
366 saving large batches of articles.  If this variable is neither nil not
367 `always', there the user will be prompted once for a file name for
368 each invocation of the saving commands.")
369
370 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
371   "*A function generating a file name to save articles in Rmail format.
372 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
373
374 (defvar gnus-mail-save-name (function gnus-plain-save-name)
375   "*A function generating a file name to save articles in Unix mail format.
376 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
377
378 (defvar gnus-folder-save-name (function gnus-folder-save-name)
379   "*A function generating a file name to save articles in MH folder.
380 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
381
382 (defvar gnus-file-save-name (function gnus-numeric-save-name)
383   "*A function generating a file name to save articles in article format.
384 The function is called with NEWSGROUP, HEADERS, and optional
385 LAST-FILE.")
386
387 (defvar gnus-split-methods nil
388   "*Variable used to suggest where articles are to be saved.
389 The syntax of this variable is the same as `nnmail-split-methods'.  
390
391 For instance, if you would like to save articles related to Gnus in
392 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
393 you could set this variable to something like:
394
395  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
396    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))")
397
398 (defvar gnus-save-score nil
399   "*If non-nil, save group scoring info.")
400
401 (defvar gnus-use-adaptive-scoring nil
402   "*If non-nil, use some adaptive scoring scheme.")
403
404 (defvar gnus-use-cache nil
405   "*If non-nil, Gnus will cache (some) articles locally.")
406
407 (defvar gnus-keep-backlog nil
408   "*If non-nil, Gnus will keep read articles for later re-retrieval.
409 If it is a number N, then Gnus will only keep the last N articles
410 read.  If it is neither nil nor a number, Gnus will keep all read
411 articles.  This is not a good idea.")
412
413 (defvar gnus-use-nocem nil
414   "*If non-nil, Gnus will read NoCeM cancel messages.")
415
416 (defvar gnus-use-demon nil
417   "If non-nil, Gnus might use some demons.")
418
419 (defvar gnus-use-scoring t
420   "*If non-nil, enable scoring.")
421
422 (defvar gnus-fetch-old-headers nil
423   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
424 If an unread article in the group refers to an older, already read (or
425 just marked as read) article, the old article will not normally be
426 displayed in the Summary buffer.  If this variable is non-nil, Gnus
427 will attempt to grab the headers to the old articles, and thereby
428 build complete threads.  If it has the value `some', only enough
429 headers to connect otherwise loose threads will be displayed.
430 This variable can also be a number.  In that case, no more than that
431 number of old headers will be fetched. 
432
433 The server has to support NOV for any of this to work.")
434
435 ;see gnus-cus.el
436 ;(defvar gnus-visual t
437 ;  "*If non-nil, will do various highlighting.
438 ;If nil, no mouse highlights (or any other highlights) will be
439 ;performed.  This might speed up Gnus some when generating large group
440 ;and summary buffers.")
441
442 (defvar gnus-novice-user t
443   "*Non-nil means that you are a usenet novice.
444 If non-nil, verbose messages may be displayed and confirmations may be
445 required.")
446
447 (defvar gnus-expert-user nil
448   "*Non-nil means that you will never be asked for confirmation about anything.
449 And that means *anything*.")
450
451 (defvar gnus-verbose 7
452   "*Integer that says how verbose Gnus should be.
453 The higher the number, the more messages Gnus will flash to say what
454 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
455 display most important messages; and at ten, Gnus will keep on
456 jabbering all the time.")
457
458 (defvar gnus-keep-same-level nil
459   "*Non-nil means that the next newsgroup after the current will be on the same level.
460 When you type, for instance, `n' after reading the last article in the
461 current newsgroup, you will go to the next newsgroup. If this variable
462 is nil, the next newsgroup will be the next from the group
463 buffer. 
464 If this variable is non-nil, Gnus will either put you in the
465 next newsgroup with the same level, or, if no such newsgroup is
466 available, the next newsgroup with the lowest possible level higher
467 than the current level.
468 If this variable is `best', Gnus will make the next newsgroup the one
469 with the best level.")
470
471 (defvar gnus-summary-make-false-root 'adopt
472   "*nil means that Gnus won't gather loose threads.
473 If the root of a thread has expired or been read in a previous
474 session, the information necessary to build a complete thread has been
475 lost. Instead of having many small sub-threads from this original thread
476 scattered all over the summary buffer, Gnus can gather them. 
477
478 If non-nil, Gnus will try to gather all loose sub-threads from an
479 original thread into one large thread.
480
481 If this variable is non-nil, it should be one of `none', `adopt',
482 `dummy' or `empty'.
483
484 If this variable is `none', Gnus will not make a false root, but just
485 present the sub-threads after another.
486 If this variable is `dummy', Gnus will create a dummy root that will
487 have all the sub-threads as children.
488 If this variable is `adopt', Gnus will make one of the \"children\"
489 the parent and mark all the step-children as such.
490 If this variable is `empty', the \"children\" are printed with empty
491 subject fields.  (Or rather, they will be printed with a string
492 given by the `gnus-summary-same-subject' variable.)")
493
494 (defvar gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
495   "*A regexp to match subjects to be excluded from loose thread gathering.
496 As loose thread gathering is done on subjects only, that means that
497 there can be many false gatherings performed.  By rooting out certain
498 common subjects, gathering might become saner.")
499
500 (defvar gnus-summary-gather-subject-limit nil
501   "*Maximum length of subject comparisons when gathering loose threads.
502 Use nil to compare full subjects.  Setting this variable to a low
503 number will help gather threads that have been corrupted by
504 newsreaders chopping off subject lines, but it might also mean that
505 unrelated articles that have subject that happen to begin with the
506 same few characters will be incorrectly gathered.
507
508 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
509 comparing subjects.")
510
511 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
512 (defvar gnus-summary-same-subject ""
513   "*String indicating that the current article has the same subject as the previous.
514 This variable will only be used if the value of
515 `gnus-summary-make-false-root' is `empty'.")
516
517 (defvar gnus-summary-goto-unread t
518   "*If non-nil, marking commands will go to the next unread article.")
519
520 (defvar gnus-group-goto-unread t
521   "*If non-nil, movement commands will go to the next unread and subscribed group.")
522
523 (defvar gnus-check-new-newsgroups t
524   "*Non-nil means that Gnus will add new newsgroups at startup.
525 If this variable is `ask-server', Gnus will ask the server for new
526 groups since the last time it checked. This means that the killed list
527 is no longer necessary, so you could set `gnus-save-killed-list' to
528 nil. 
529
530 A variant is to have this variable be a list of select methods. Gnus
531 will then use the `ask-server' method on all these select methods to
532 query for new groups from all those servers.
533
534 Eg.
535   (setq gnus-check-new-newsgroups 
536         '((nntp \"some.server\") (nntp \"other.server\")))
537
538 If this variable is nil, then you have to tell Gnus explicitly to
539 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
540
541 (defvar gnus-check-bogus-newsgroups nil
542   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
543 If this variable is nil, then you have to tell Gnus explicitly to
544 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
545
546 (defvar gnus-read-active-file t
547   "*Non-nil means that Gnus will read the entire active file at startup.
548 If this variable is nil, Gnus will only know about the groups in your
549 `.newsrc' file.
550
551 If this variable is `some', Gnus will try to only read the relevant
552 parts of the active file from the server.  Not all servers support
553 this, and it might be quite slow with other servers, but this should
554 generally be faster than both the t and nil value.
555
556 If you set this variable to nil or `some', you probably still want to
557 be told about new newsgroups that arrive.  To do that, set
558 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
559 properly with all servers.")
560
561 (defvar gnus-level-subscribed 5
562   "*Groups with levels less than or equal to this variable are subscribed.")
563
564 (defvar gnus-level-unsubscribed 7
565   "*Groups with levels less than or equal to this variable are unsubscribed.
566 Groups with levels less than `gnus-level-subscribed', which should be
567 less than this variable, are subscribed.")
568
569 (defvar gnus-level-zombie 8
570   "*Groups with this level are zombie groups.")
571
572 (defvar gnus-level-killed 9
573   "*Groups with this level are killed.")
574
575 (defvar gnus-level-default-subscribed 3
576   "*New subscribed groups will be subscribed at this level.")
577
578 (defvar gnus-level-default-unsubscribed 6
579   "*New unsubscribed groups will be unsubscribed at this level.")
580
581 (defvar gnus-activate-foreign-newsgroups 4
582   "*If nil, Gnus will not check foreign newsgroups at startup.
583 If it is non-nil, it should be a number between one and nine. Foreign
584 newsgroups that have a level lower or equal to this number will be
585 activated on startup. For instance, if you want to active all
586 subscribed newsgroups, but not the rest, you'd set this variable to 
587 `gnus-level-subscribed'.
588
589 If you subscribe to lots of newsgroups from different servers, startup
590 might take a while. By setting this variable to nil, you'll save time,
591 but you won't be told how many unread articles there are in the
592 groups.")
593
594 (defvar gnus-save-newsrc-file t
595   "*Non-nil means that Gnus will save the `.newsrc' file.
596 Gnus always saves its own startup file, which is called
597 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
598 be readily understood by other newsreaders.  If you don't plan on
599 using other newsreaders, set this variable to nil to save some time on
600 exit.")
601
602 (defvar gnus-save-killed-list t
603   "*If non-nil, save the list of killed groups to the startup file.
604 This will save both time (when starting and quitting) and space (both
605 memory and disk), but it will also mean that Gnus has no record of
606 which groups are new and which are old, so the automatic new
607 newsgroups subscription methods become meaningless. You should always
608 set `gnus-check-new-newsgroups' to `ask-server' or nil if you set this
609 variable to nil.")
610
611 (defvar gnus-interactive-catchup t
612   "*If non-nil, require your confirmation when catching up a group.")
613
614 (defvar gnus-interactive-post t
615   "*If non-nil, group name will be asked for when posting.")
616
617 (defvar gnus-interactive-exit t
618   "*If non-nil, require your confirmation when exiting Gnus.")
619
620 (defvar gnus-kill-killed t
621   "*If non-nil, Gnus will apply kill files to already killed articles.
622 If it is nil, Gnus will never apply kill files to articles that have
623 already been through the scoring process, which might very well save lots
624 of time.")
625
626 (defvar gnus-extract-address-components 'gnus-extract-address-components
627   "*Function for extracting address components from a From header.
628 Two pre-defined function exist: `gnus-extract-address-components',
629 which is the default, quite fast, and too simplistic solution, and
630 `mail-extract-address-components', which works much better, but is
631 slower.")
632
633 (defvar gnus-summary-default-score 0
634   "*Default article score level.
635 If this variable is nil, scoring will be disabled.")
636
637 (defvar gnus-summary-zcore-fuzz 0
638   "*Fuzziness factor for the zcore in the summary buffer.
639 Articles with scores closer than this to `gnus-summary-default-score'
640 will not be marked.")
641
642 (defvar gnus-simplify-subject-fuzzy-regexp nil
643   "*Regular expression or list of regular expressions that will be removed
644 from subject strings if fuzzy subject simplification is selected.")
645
646 (defvar gnus-group-default-list-level gnus-level-subscribed
647   "*Default listing level. 
648 Ignored if `gnus-group-use-permanent-levels' is non-nil.")
649
650 (defvar gnus-group-use-permanent-levels nil
651   "*If non-nil, once you set a level, Gnus will use this level.")
652
653 (defvar gnus-show-mime nil
654   "*If non-nil, do mime processing of articles.
655 The articles will simply be fed to the function given by
656 `gnus-show-mime-method'.")
657
658 (defvar gnus-strict-mime t
659   "*If nil, decode MIME header even if there is not Mime-Version field.")
660  
661 (defvar gnus-show-mime-method (function metamail-buffer)
662   "*Function to process a MIME message.
663 The function is called from the article buffer.")
664
665 (defvar gnus-show-threads t
666   "*If non-nil, display threads in summary mode.")
667
668 (defvar gnus-thread-hide-subtree nil
669   "*If non-nil, hide all threads initially.
670 If threads are hidden, you have to run the command
671 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
672 to expose hidden threads.")
673
674 (defvar gnus-thread-hide-killed t
675   "*If non-nil, hide killed threads automatically.")
676
677 (defvar gnus-thread-ignore-subject nil
678   "*If non-nil, ignore subjects and do all threading based on the Reference header.
679 If nil, which is the default, articles that have different subjects
680 from their parents will start separate threads.")
681
682 (defvar gnus-thread-operation-ignore-subject t
683   "*If non-nil, subjects will be ignored when doing thread commands.
684 This affects commands like `gnus-summary-kill-thread' and
685 `gnus-summary-lower-thread'.  
686
687 If this variable is nil, articles in the same thread with different
688 subjects will not be included in the operation in question.  If this
689 variable is `fuzzy', only articles that have subjects that are fuzzily
690 equal will be included.")
691
692 (defvar gnus-thread-indent-level 4
693   "*Number that says how much each sub-thread should be indented.")
694
695 (defvar gnus-ignored-newsgroups 
696   (purecopy (mapconcat 'identity
697                        '("^to\\."       ; not "real" groups
698                          "^[0-9. \t]+ " ; all digits in name
699                          "[][\"#'()]"   ; bogus characters
700                          )
701                        "\\|"))
702   "*A regexp to match uninteresting newsgroups in the active file.
703 Any lines in the active file matching this regular expression are
704 removed from the newsgroup list before anything else is done to it,
705 thus making them effectively non-existent.")
706
707 (defvar gnus-ignored-headers
708   "^Path:\\|^Posting-Version:\\|^Article-I.D.:\\|^Expires:\\|^Date-Received:\\|^References:\\|^Control:\\|^Xref:\\|^Lines:\\|^Posted:\\|^Relay-Version:\\|^Message-ID:\\|^Nf-ID:\\|^Nf-From:\\|^Approved:\\|^Sender:\\|^Received:\\|^Mail-from:"
709   "*All headers that match this regexp will be hidden.
710 If `gnus-visible-headers' is non-nil, this variable will be ignored.")
711
712 (defvar gnus-visible-headers "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:"
713   "*All headers that do not match this regexp will be hidden.
714 If this variable is non-nil, `gnus-ignored-headers' will be ignored.")
715
716 (defvar gnus-sorted-header-list
717   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:" 
718     "^Cc:" "^Date:" "^Organization:")
719   "*This variable is a list of regular expressions.
720 If it is non-nil, headers that match the regular expressions will
721 be placed first in the article buffer in the sequence specified by
722 this list.")
723
724 (defvar gnus-show-all-headers nil
725   "*If non-nil, don't hide any headers.")
726
727 (defvar gnus-save-all-headers t
728   "*If non-nil, don't remove any headers before saving.")
729
730 (defvar gnus-saved-headers gnus-visible-headers
731   "*Headers to keep if `gnus-save-all-headers' is nil.
732 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
733 If that variable is nil, however, all headers that match this regexp
734 will be kept while the rest will be deleted before saving.")
735
736 (defvar gnus-inhibit-startup-message nil
737   "*If non-nil, the startup message will not be displayed.")
738
739 (defvar gnus-signature-separator "^-- *$"
740   "Regexp matching signature separator.")
741
742 (defvar gnus-auto-extend-newsgroup t
743   "*If non-nil, extend newsgroup forward and backward when requested.")
744
745 (defvar gnus-auto-select-first t
746   "*If non-nil, select the first unread article when entering a group.
747 If you want to prevent automatic selection of the first unread article
748 in some newsgroups, set the variable to nil in
749 `gnus-select-group-hook'.") 
750
751 (defvar gnus-auto-select-next t
752   "*If non-nil, offer to go to the next group from the end of the previous.
753 If the value is t and the next newsgroup is empty, Gnus will exit
754 summary mode and go back to group mode.  If the value is neither nil
755 nor t, Gnus will select the following unread newsgroup.  In
756 particular, if the value is the symbol `quietly', the next unread
757 newsgroup will be selected without any confirmation, and if it is
758 `almost-quietly', the next group will be selected without any
759 confirmation if you are located on the last article in the group.")
760
761 (defvar gnus-auto-select-same nil
762   "*If non-nil, select the next article with the same subject.")
763
764 (defvar gnus-summary-check-current nil
765   "*If non-nil, consider the current article when moving.
766 The \"unread\" movement commands will stay on the same line if the
767 current article is unread.")
768
769 (defvar gnus-auto-center-summary t
770   "*If non-nil, always center the current summary buffer.")
771
772 (defvar gnus-break-pages t
773   "*If non-nil, do page breaking on articles.
774 The page delimiter is specified by the `gnus-page-delimiter'
775 variable.")
776
777 (defvar gnus-page-delimiter "^\^L"
778   "*Regexp describing what to use as article page delimiters.
779 The default value is \"^\^L\", which is a form linefeed at the
780 beginning of a line.")
781
782 (defvar gnus-use-full-window t
783   "*If non-nil, use the entire Emacs screen.")
784
785 (defvar gnus-window-configuration nil
786   "Obsolete variable.  See `gnus-buffer-configuration'.")
787
788 (defvar gnus-buffer-configuration
789   '((group ([group 1.0 point] 
790             (if gnus-carpal [group-carpal 4])))
791     (summary ([summary 1.0 point]
792               (if gnus-carpal [summary-carpal 4])))
793     (article ([summary 0.25 point] 
794               (if gnus-carpal [summary-carpal 4]) 
795               [article 1.0]))
796     (server ([server 1.0 point]
797              (if gnus-carpal [server-carpal 2])))
798     (browse ([browse 1.0 point]
799              (if gnus-carpal [browse-carpal 2])))
800     (group-mail ([mail 1.0 point]))
801     (summary-mail ([mail 1.0 point]))
802     (summary-reply ([article 0.5]
803                     [mail 1.0 point]))
804     (info ([nil 1.0 point]))
805     (summary-faq ([summary 0.25]
806                   [faq 1.0 point]))
807     (edit-group ([group 0.5]
808                  [edit-group 1.0 point]))
809     (edit-server ([server 0.5]
810                   [edit-server 1.0 point]))
811     (edit-score ([summary 0.25]
812                  [edit-score 1.0 point]))
813     (post ([post 1.0 point]))
814     (reply ([article 0.5]
815             [mail 1.0 point]))
816     (mail-forward ([mail 1.0 point]))
817     (post-forward ([post 1.0 point]))
818     (reply-yank ([mail 1.0 point]))
819     (mail-bounce ([article 0.5]
820                   [mail 1.0 point]))
821     (draft ([draft 1.0 point]))
822     (pipe ([summary 0.25 point] 
823            (if gnus-carpal [summary-carpal 4]) 
824            ["*Shell Command Output*" 1.0]))
825     (followup ([article 0.5]
826                [post 1.0 point]))
827     (followup-yank ([post 1.0 point])))
828   "Window configuration for all possible Gnus buffers.
829 This variable is a list of lists.  Each of these lists has a NAME and
830 a RULE.  The NAMEs are commonsense names like `group', which names a
831 rule used when displaying the group buffer; `summary', which names a
832 rule for what happens when you enter a group and do not display an
833 article buffer; and so on.  See the value of this variable for a
834 complete list of NAMEs.
835
836 Each RULE is a list of vectors.  The first element in this vector is
837 the name of the buffer to be displayed; the second element is the
838 percentage of the screen this buffer is to occupy (a number in the
839 0.0-0.99 range); the optional third element is `point', which should
840 be present to denote which buffer point is to go to after making this
841 buffer configuration.")
842
843 (defvar gnus-window-to-buffer
844   '((group . gnus-group-buffer)
845     (summary . gnus-summary-buffer)
846     (article . gnus-article-buffer)
847     (server . gnus-server-buffer)
848     (browse . "*Gnus Browse Server*")
849     (edit-group . gnus-group-edit-buffer)
850     (edit-server . gnus-server-edit-buffer)
851     (group-carpal . gnus-carpal-group-buffer)
852     (summary-carpal . gnus-carpal-summary-buffer)
853     (server-carpal . gnus-carpal-server-buffer)
854     (browse-carpal . gnus-carpal-browse-buffer)
855     (edit-score . gnus-score-edit-buffer)
856     (mail . gnus-mail-buffer)
857     (post . gnus-post-news-buffer)
858     (faq . gnus-faq-buffer)
859     (draft . gnus-draft-buffer))
860   "Mapping from short symbols to buffer names or buffer variables.")
861
862 (defvar gnus-carpal nil
863   "*If non-nil, display clickable icons.")
864
865 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
866   "*Function called with a group name when new group is detected.
867 A few pre-made functions are supplied: `gnus-subscribe-randomly'
868 inserts new groups at the beginning of the list of groups;
869 `gnus-subscribe-alphabetically' inserts new groups in strict
870 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
871 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
872 for your decision.")
873
874 ;; Suggested by a bug report by Hallvard B Furuseth.
875 ;; <h.b.furuseth@usit.uio.no>. 
876 (defvar gnus-subscribe-options-newsgroup-method
877   (function gnus-subscribe-alphabetically)
878   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
879 If, for instance, you want to subscribe to all newsgroups in the
880 \"no\" and \"alt\" hierarchies, you'd put the following in your
881 .newsrc file:
882
883 options -n no.all alt.all
884
885 Gnus will the subscribe all new newsgroups in these hierarchies with
886 the subscription method in this variable.")
887
888 (defvar gnus-subscribe-hierarchical-interactive nil
889   "*If non-nil, Gnus will offer to subscribe hierarchically.
890 When a new hierarchy appears, Gnus will ask the user:
891
892 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
893
894 If the user pressed `d', Gnus will descend the hierarchy, `y' will
895 subscribe to all newsgroups in the hierarchy and `s' will skip this
896 hierarchy in its entirety.")
897
898 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
899   "*Function used for sorting the group buffer.
900 This function will be called with group info entries as the arguments
901 for the groups to be sorted.  Pre-made functions include
902 `gnus-group-sort-by-alphabet', `gnus-group-sort-by-unread' and
903 `gnus-group-sort-by-level'.
904
905 This variable can also be a list of sorting functions.  In that case,
906 the most significant sort function should be the last function in the
907 list.")
908
909 ;; Mark variables suggested by Thomas Michanek
910 ;; <Thomas.Michanek@telelogic.se>. 
911 (defvar gnus-unread-mark ? 
912   "*Mark used for unread articles.")
913 (defvar gnus-ticked-mark ?!
914   "*Mark used for ticked articles.")
915 (defvar gnus-dormant-mark ??
916   "*Mark used for dormant articles.")
917 (defvar gnus-del-mark ?r
918   "*Mark used for del'd articles.")
919 (defvar gnus-read-mark ?R
920   "*Mark used for read articles.")
921 (defvar gnus-expirable-mark ?E
922   "*Mark used for expirable articles.")
923 (defvar gnus-killed-mark ?K
924   "*Mark used for killed articles.")
925 (defvar gnus-kill-file-mark ?X
926   "*Mark used for articles killed by kill files.")
927 (defvar gnus-low-score-mark ?Y
928   "*Mark used for articles with a low score.")
929 (defvar gnus-catchup-mark ?C
930   "*Mark used for articles that are caught up.")
931 (defvar gnus-replied-mark ?A
932   "*Mark used for articles that have been replied to.")
933 (defvar gnus-process-mark ?# 
934   "*Process mark.")
935 (defvar gnus-ancient-mark ?O
936   "*Mark used for ancient articles.")
937 (defvar gnus-canceled-mark ?G
938   "*Mark used for canceled articles.")
939 (defvar gnus-score-over-mark ?+
940   "*Score mark used for articles with high scores.")
941 (defvar gnus-score-below-mark ?-
942   "*Score mark used for articles with low scores.")
943 (defvar gnus-empty-thread-mark ? 
944   "*There is no thread under the article.")
945 (defvar gnus-not-empty-thread-mark ?=
946   "*There is a thread under the article.")
947
948 (defvar gnus-view-pseudo-asynchronously nil
949   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
950
951 (defvar gnus-view-pseudos nil
952   "*If `automatic', pseudo-articles will be viewed automatically.
953 If `not-confirm', pseudos will be viewed automatically, and the user
954 will not be asked to confirm the command.")
955
956 (defvar gnus-view-pseudos-separately t
957   "*If non-nil, one pseudo-article will be created for each file to be viewed.
958 If nil, all files that use the same viewing command will be given as a
959 list of parameters to that command.")
960
961 (defvar gnus-group-line-format "%M%S%p%5y: %(%g%)\n"
962   "*Format of group lines.
963 It works along the same lines as a normal formatting string,
964 with some simple extensions.
965
966 %M    Only marked articles (character, \"*\" or \" \")
967 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
968 %L    Level of subscribedness (integer)
969 %N    Number of unread articles (integer)
970 %I    Number of dormant articles (integer)
971 %i    Number of ticked and dormant (integer)
972 %T    Number of ticked articles (integer)
973 %R    Number of read articles (integer)
974 %t    Total number of articles (integer)
975 %y    Number of unread, unticked articles (integer)
976 %G    Group name (string)
977 %g    Qualified group name (string)
978 %D    Group description (string)
979 %s    Select method (string)
980 %o    Moderated group (char, \"m\")
981 %p    Process mark (char)
982 %O    Moderated group (string, \"(m)\" or \"\")
983 %n    Select from where (string)
984 %z    A string that look like `<%s:%n>' if a foreign select method is used
985 %u    User defined specifier. The next character in the format string should
986       be a letter.  Gnus will call the function gnus-user-format-function-X,
987       where X is the letter following %u. The function will be passed the
988       current header as argument. The function should return a string, which
989       will be inserted into the buffer just like information from any other
990       group specifier.
991
992 Text between %( and %) will be highlighted with `gnus-mouse-face' when
993 the mouse point move inside the area.  There can only be one such area.
994
995 Note that this format specification is not always respected. For
996 reasons of efficiency, when listing killed groups, this specification
997 is ignored altogether. If the spec is changed considerably, your
998 output may end up looking strange when listing both alive and killed
999 groups.
1000
1001 If you use %o or %O, reading the active file will be slower and quite
1002 a bit of extra memory will be used. %D will also worsen performance.
1003 Also note that if you change the format specification to include any
1004 of these specs, you must probably re-start Gnus to see them go into
1005 effect.") 
1006
1007 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
1008   "*The format specification of the lines in the summary buffer.
1009
1010 It works along the same lines as a normal formatting string,
1011 with some simple extensions.
1012
1013 %N   Article number, left padded with spaces (string)
1014 %S   Subject (string)
1015 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
1016 %n   Name of the poster (string)
1017 %a   Extracted name of the poster (string)
1018 %A   Extracted address of the poster (string)
1019 %F   Contents of the From: header (string)
1020 %x   Contents of the Xref: header (string)
1021 %D   Date of the article (string)
1022 %d   Date of the article (string) in DD-MMM format
1023 %M   Message-id of the article (string)
1024 %r   References of the article (string)
1025 %c   Number of characters in the article (integer)
1026 %L   Number of lines in the article (integer)
1027 %I   Indentation based on thread level (a string of spaces)
1028 %T   A string with two possible values: 80 spaces if the article
1029      is on thread level two or larger and 0 spaces on level one
1030 %R   \"R\" if this article has been replied to, \" \" otherwise (character)
1031 %U   Status of this article (character, \"D\", \"K\", \"-\" or \" \")
1032 %[   Opening bracket (character, \"[\" or \"<\")
1033 %]   Closing bracket (character, \"]\" or \">\")
1034 %>   Spaces of length thread-level (string)
1035 %<   Spaces of length (- 20 thread-level) (string)
1036 %i   Article score (number)
1037 %z   Article zcore (character)
1038 %t   Number of articles under the current thread (number).
1039 %e   Whether the thread is empty or not (character).
1040 %u   User defined specifier. The next character in the format string should
1041      be a letter.  Gnus will call the function gnus-user-format-function-X,
1042      where X is the letter following %u. The function will be passed the
1043      current header as argument. The function should return a string, which
1044      will be inserted into the summary just like information from any other
1045      summary specifier.
1046
1047 Text between %( and %) will be highlighted with `gnus-mouse-face'
1048 when the mouse point is placed inside the area.  There can only be one
1049 such area.
1050
1051 The %U (status), %R (replied) and %z (zcore) specs have to be handled
1052 with care. For reasons of efficiency, Gnus will compute what column
1053 these characters will end up in, and \"hard-code\" that. This means that
1054 it is illegal to have these specs after a variable-length spec. Well,
1055 you might not be arrested, but your summary buffer will look strange,
1056 which is bad enough.
1057
1058 The smart choice is to have these specs as for to the left as
1059 possible. 
1060
1061 This restriction may disappear in later versions of Gnus.")
1062
1063 (defvar gnus-summary-dummy-line-format "*  :                          : %S\n"
1064   "*The format specification for the dummy roots in the summary buffer.
1065 It works along the same lines as a normal formatting string,
1066 with some simple extensions.
1067
1068 %S  The subject")
1069
1070 (defvar gnus-summary-mode-line-format "Gnus  %G/%A %Z"
1071   "*The format specification for the summary mode line.")
1072
1073 (defvar gnus-article-mode-line-format "Gnus  %G/%A %S"
1074   "*The format specification for the article mode line.")
1075
1076 (defvar gnus-group-mode-line-format "Gnus  List of groups   {%M:%S}  "
1077   "*The format specification for the group mode line.")
1078
1079 (defvar gnus-valid-select-methods
1080   '(("nntp" post address prompt-address)
1081     ("nnspool" post)
1082     ("nnvirtual" none virtual prompt-address) 
1083     ("nnmbox" mail respool) 
1084     ("nnml" mail respool)
1085     ("nnmh" mail respool) 
1086     ("nndir" none prompt-address address)
1087     ("nneething" none prompt-address)
1088     ("nndigest" none) 
1089     ("nndoc" none prompt-address) 
1090     ("nnbabyl" mail respool) 
1091     ("nnkiboze" post virtual) 
1092     ("nnsoup" post)
1093     ("nnfolder" mail respool))
1094   "An alist of valid select methods.
1095 The first element of each list lists should be a string with the name
1096 of the select method. The other elements may be be the category of
1097 this method (ie. `post', `mail', `none' or whatever) or other
1098 properties that this method has (like being respoolable).
1099 If you implement a new select method, all you should have to change is
1100 this variable. I think.")
1101
1102 (defvar gnus-updated-mode-lines '(group article summary)
1103   "*List of buffers that should update their mode lines.
1104 The list may contain the symbols `group', `article' and `summary'. If
1105 the corresponding symbol is present, Gnus will keep that mode line
1106 updated with information that may be pertinent. 
1107 If this variable is nil, screen refresh may be quicker.")
1108
1109 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
1110 (defvar gnus-mode-non-string-length 21
1111   "*Max length of mode-line non-string contents.
1112 If this is nil, Gnus will take space as is needed, leaving the rest
1113 of the modeline intact.")
1114
1115 ;see gnus-cus.el
1116 ;(defvar gnus-mouse-face 'highlight
1117 ;  "*Face used for mouse highlighting in Gnus.
1118 ;No mouse highlights will be done if `gnus-visual' is nil.")
1119
1120 (defvar gnus-summary-mark-below nil
1121   "*Mark all articles with a score below this variable as read.
1122 This variable is local to each summary buffer and usually set by the
1123 score file.")  
1124
1125 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1126   "*List of functions used for sorting threads in the summary buffer.
1127 By default, threads are sorted by article number.
1128
1129 Each function takes two threads and return non-nil if the first thread
1130 should be sorted before the other.  If you use more than one function,
1131 the primary sort function should be the last.
1132
1133 Ready-mady functions include `gnus-thread-sort-by-number',
1134 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1135 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1136 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1137
1138 (defvar gnus-thread-score-function '+
1139   "*Function used for calculating the total score of a thread.
1140
1141 The function is called with the scores of the article and each
1142 subthread and should then return the score of the thread.
1143
1144 Some functions you can use are `+', `max', or `min'.")
1145
1146 (defvar gnus-auto-subscribed-groups 
1147   "^nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl"
1148   "*All new groups that match this regexp will be subscribed automatically.
1149 Note that this variable only deals with new groups.  It has no effect
1150 whatsoever on old groups.")
1151
1152 (defvar gnus-options-subscribe nil
1153   "*All new groups matching this regexp will be subscribed unconditionally.
1154 Note that this variable deals only with new newsgroups.  This variable
1155 does not affect old newsgroups.")
1156
1157 (defvar gnus-options-not-subscribe nil
1158   "*All new groups matching this regexp will be ignored.
1159 Note that this variable deals only with new newsgroups.  This variable
1160 does not affect old (already subscribed) newsgroups.")
1161
1162 (defvar gnus-auto-expirable-newsgroups nil
1163   "*Groups in which to automatically mark read articles as expirable.
1164 If non-nil, this should be a regexp that should match all groups in
1165 which to perform auto-expiry.  This only makes sense for mail groups.")
1166
1167 (defvar gnus-total-expirable-newsgroups nil
1168   "*Groups in which to perform expiry of all read articles.
1169 Use with extreme caution.  All groups that match this regexp will be
1170 expiring - which means that all read articles will be deleted after
1171 (say) one week.  (This only goes for mail groups and the like, of
1172 course.)")
1173
1174 (defvar gnus-hidden-properties '(invisible t intangible t)
1175   "Property list to use for hiding text.")
1176
1177 (defvar gnus-modtime-botch nil
1178   "*Non-nil means .newsrc should be deleted prior to save.  Its use is
1179 due to the bogus appearance that .newsrc was modified on disc.")
1180
1181 ;; Hooks.
1182
1183 (defvar gnus-group-mode-hook nil
1184   "*A hook for Gnus group mode.")
1185
1186 (defvar gnus-summary-mode-hook nil
1187   "*A hook for Gnus summary mode.
1188 This hook is run before any variables are set in the summary buffer.")
1189
1190 (defvar gnus-article-mode-hook nil
1191   "*A hook for Gnus article mode.")
1192
1193 (defun gnus-summary-prepare-exit-hook nil
1194   "*A hook called when preparing to exit from the summary buffer.
1195 It calls `gnus-summary-expire-articles' by default.")
1196 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1197
1198 (defun gnus-summary-exit-hook nil
1199   "*A hook called on exit from the summary buffer.")
1200
1201 (defvar gnus-open-server-hook nil
1202   "*A hook called just before opening connection to the news server.")
1203
1204 (defvar gnus-load-hook nil
1205   "*A hook run while Gnus is loaded.")
1206
1207 (defvar gnus-startup-hook nil
1208   "*A hook called at startup.
1209 This hook is called after Gnus is connected to the NNTP server.")
1210
1211 (defvar gnus-get-new-news-hook nil
1212   "*A hook run just before Gnus checks for new news.")
1213
1214 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1215   "*A function that is called to generate the group buffer.
1216 The function is called with three arguments: The first is a number;
1217 all group with a level less or equal to that number should be listed,
1218 if the second is non-nil, empty groups should also be displayed. If
1219 the third is non-nil, it is a number. No groups with a level lower
1220 than this number should be displayed.
1221
1222 The only current function implemented are `gnus-group-prepare-flat'
1223 \(which does the normal boring group display) and
1224 `gnus-group-prepare-topics' (which does a folding display accoring to
1225 topics).")
1226
1227 (defvar gnus-group-prepare-hook nil
1228   "*A hook called after the group buffer has been generated.
1229 If you want to modify the group buffer, you can use this hook.")
1230
1231 (defvar gnus-summary-prepare-hook nil
1232   "*A hook called after the summary buffer has been generated.
1233 If you want to modify the summary buffer, you can use this hook.")
1234
1235 (defvar gnus-article-prepare-hook nil
1236   "*A hook called after an article has been prepared in the article buffer.
1237 If you want to run a special decoding program like nkf, use this hook.")
1238
1239 ;(defvar gnus-article-display-hook nil
1240 ;  "*A hook called after the article is displayed in the article buffer.
1241 ;The hook is designed to change the contents of the article
1242 ;buffer. Typical functions that this hook may contain are
1243 ;`gnus-article-hide-headers' (hide selected headers),
1244 ;`gnus-article-maybe-highlight' (perform fancy article highlighting), 
1245 ;`gnus-article-hide-signature' (hide signature) and
1246 ;`gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1247 ;(add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1248 ;(add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1249 ;(add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1250
1251 (defvar gnus-article-x-face-command
1252   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1253   "String or function to be executed to display an X-Face header.
1254 If it is a string, the command will be executed in a sub-shell
1255 asynchronously. The compressed face will be piped to this command.") 
1256
1257 (defvar gnus-article-x-face-too-ugly nil
1258   "Regexp matching posters whose face shouldn't be shown automatically.")
1259
1260 (defvar gnus-select-group-hook nil
1261   "*A hook called when a newsgroup is selected.
1262
1263 If you'd like to simplify subjects like the
1264 `gnus-summary-next-same-subject' command does, you can use the
1265 following hook:
1266
1267  (setq gnus-select-group-hook
1268       (list
1269         (lambda ()
1270           (mapcar (lambda (header)
1271                      (mail-header-set-subject
1272                       header
1273                       (gnus-simplify-subject
1274                        (mail-header-subject header) 're-only)))
1275                   gnus-newsgroup-headers))))")
1276
1277 (defvar gnus-select-article-hook
1278   '(gnus-summary-show-thread)
1279   "*A hook called when an article is selected.
1280 The default hook shows conversation thread subtrees of the selected
1281 article automatically using `gnus-summary-show-thread'.")
1282
1283 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1284   "*A hook called to apply kill files to a group.
1285 This hook is intended to apply a kill file to the selected newsgroup.
1286 The function `gnus-apply-kill-file' is called by default.
1287
1288 Since a general kill file is too heavy to use only for a few
1289 newsgroups, I recommend you to use a lighter hook function. For
1290 example, if you'd like to apply a kill file to articles which contains
1291 a string `rmgroup' in subject in newsgroup `control', you can use the
1292 following hook:
1293
1294  (setq gnus-apply-kill-hook
1295       (list
1296         (lambda ()
1297           (cond ((string-match \"control\" gnus-newsgroup-name)
1298                  (gnus-kill \"Subject\" \"rmgroup\")
1299                  (gnus-expunge \"X\"))))))")
1300
1301 (defvar gnus-visual-mark-article-hook 
1302   (list 'gnus-highlight-selected-summary)
1303   "*Hook run after selecting an article in the summary buffer.
1304 It is meant to be used for highlighting the article in some way.  It
1305 is not run if `gnus-visual' is nil.")
1306
1307 (defvar gnus-exit-group-hook nil
1308   "*A hook called when exiting (not quitting) summary mode.")
1309
1310 (defvar gnus-suspend-gnus-hook nil
1311   "*A hook called when suspending (not exiting) Gnus.")
1312
1313 (defvar gnus-exit-gnus-hook nil
1314   "*A hook called when exiting Gnus.")
1315
1316 (defvar gnus-save-newsrc-hook nil
1317   "*A hook called before saving any of the newsrc files.")
1318
1319 (defvar gnus-save-quick-newsrc-hook nil
1320   "*A hook called just before saving the quick newsrc file.
1321 Can be used to turn version control on or off.")
1322
1323 (defvar gnus-save-standard-newsrc-hook nil
1324   "*A hook called just before saving the standard newsrc file.
1325 Can be used to turn version control on or off.")
1326
1327 (defvar gnus-summary-update-hook 
1328   (list 'gnus-summary-highlight-line)
1329   "*A hook called when a summary line is changed.
1330 The hook will not be called if `gnus-visual' is nil.
1331
1332 The default function `gnus-summary-highlight-line' will
1333 highlight the line according to the `gnus-summary-highlight'
1334 variable.")
1335
1336 (defvar gnus-mark-article-hook (list 'gnus-summary-mark-unread-as-read)
1337   "*A hook called when an article is selected for the first time.
1338 The hook is intended to mark an article as read (or unread)
1339 automatically when it is selected.")
1340
1341 ;; Remove any hilit infestation.
1342 (add-hook 'gnus-startup-hook
1343           (lambda ()
1344             (remove-hook 'gnus-summary-prepare-hook
1345                          'hilit-rehighlight-buffer-quietly)
1346             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1347             (setq gnus-mark-article-hook '(gnus-summary-mark-unread-as-read))
1348             (remove-hook 'gnus-article-prepare-hook
1349                          'hilit-rehighlight-buffer-quietly)))
1350
1351
1352 \f
1353 ;; Internal variables
1354
1355 ;; Avoid highlighting in kill files.
1356 (defvar gnus-summary-inhibit-highlight nil)
1357 (defvar gnus-newsgroup-selected-overlay nil)
1358
1359 (defvar gnus-article-mode-map nil)
1360 (defvar gnus-dribble-buffer nil)
1361 (defvar gnus-headers-retrieved-by nil)
1362 (defvar gnus-article-reply nil)
1363 (defvar gnus-override-method nil)
1364 (defvar gnus-article-check-size nil)
1365
1366 (defvar gnus-nocem-hashtb nil)
1367
1368 (defvar gnus-current-score-file nil)
1369 (defvar gnus-internal-global-score-files nil)
1370 (defvar gnus-score-file-list nil)
1371
1372 (defvar gnus-opened-servers nil)
1373
1374 (defvar gnus-current-move-group nil)
1375
1376 (defvar gnus-newsgroup-dependencies nil)
1377 (defvar gnus-newsgroup-async nil)
1378 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1379
1380 (defvar gnus-newsgroup-adaptive nil)
1381
1382 (defvar gnus-summary-display-table nil)
1383
1384 (defconst gnus-group-line-format-alist
1385   (` ((?M gnus-tmp-marked ?c)
1386       (?S gnus-tmp-subscribed ?c)
1387       (?L gnus-tmp-level ?d)
1388       (?N gnus-tmp-number ?s)
1389       (?I gnus-tmp-number-of-dormant ?d)
1390       (?T gnus-tmp-number-of-ticked ?d)
1391       (?R gnus-tmp-number-of-read ?s)
1392       (?t gnus-tmp-number-total ?d)
1393       (?y gnus-tmp-number-of-unread-unticked ?s)
1394       (?i gnus-tmp-number-of-ticked-and-dormant ?d)
1395       (?g gnus-tmp-group ?s)
1396       (?G gnus-tmp-qualified-group ?s)
1397       (?D gnus-tmp-newsgroup-description ?s)
1398       (?o gnus-tmp-moderated ?c)
1399       (?O gnus-tmp-moderated-string ?s)
1400       (?p gnus-tmp-process-marked ?c)
1401       (?s gnus-tmp-news-server ?s)
1402       (?n gnus-tmp-news-method ?s)
1403       (?z gnus-tmp-news-method-string ?s)
1404       (?u gnus-tmp-user-defined ?s))))
1405
1406 (defconst gnus-summary-line-format-alist 
1407   (` ((?N gnus-tmp-number ?d)
1408       (?S gnus-tmp-subject ?s)
1409       (?s gnus-tmp-subject-or-nil ?s)
1410       (?n gnus-tmp-name ?s)
1411       (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1412           ?s)
1413       (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from)) 
1414               gnus-tmp-from) ?s)
1415       (?F gnus-tmp-from ?s)
1416       (?x (, (macroexpand '(mail-header-xref gnus-tmp-header))) ?s)
1417       (?D (, (macroexpand '(mail-header-date gnus-tmp-header))) ?s)
1418       (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1419       (?M (, (macroexpand '(mail-header-id gnus-tmp-header))) ?s)
1420       (?r (, (macroexpand '(mail-header-references gnus-tmp-header))) ?s)
1421       (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1422       (?L gnus-tmp-lines ?d)
1423       (?I gnus-tmp-indentation ?s)
1424       (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1425       (?R gnus-tmp-replied ?c)
1426       (?\[ gnus-tmp-opening-bracket ?c)
1427       (?\] gnus-tmp-closing-bracket ?c)
1428       (?\> (make-string gnus-tmp-level ? ) ?s)
1429       (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1430       (?i gnus-tmp-score ?d)
1431       (?z gnus-tmp-score-char ?c)
1432       (?U gnus-tmp-unread ?c)
1433       (?t (gnus-summary-number-of-articles-in-thread 
1434            (and (boundp 'thread) (car thread)))
1435           ?d)
1436       (?e (gnus-summary-number-of-articles-in-thread 
1437            (and (boundp 'thread) (car thread)) t)
1438           ?c)
1439       (?u gnus-tmp-user-defined ?s)))
1440   "An alist of format specifications that can appear in summary lines,
1441 and what variables they correspond with, along with the type of the
1442 variable (string, integer, character, etc).")
1443
1444 (defconst gnus-summary-dummy-line-format-alist
1445   (` ((?S gnus-tmp-subject ?s)
1446       (?N gnus-tmp-number ?d)
1447       (?u gnus-tmp-user-defined ?s))))
1448
1449 (defconst gnus-summary-mode-line-format-alist 
1450   (` ((?G gnus-tmp-group-name ?s)
1451       (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1452       (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1453       (?A gnus-tmp-article-number ?d)
1454       (?Z gnus-tmp-unread-and-unselected ?s)
1455       (?V gnus-version ?s)
1456       (?U gnus-tmp-unread ?d)
1457       (?S gnus-tmp-subject ?s)
1458       (?e gnus-tmp-unselected ?d)
1459       (?u gnus-tmp-user-defined ?s)
1460       (?d (length gnus-newsgroup-dormant) ?d)
1461       (?t (length gnus-newsgroup-marked) ?d)
1462       (?r (length gnus-newsgroup-reads) ?d)
1463       (?E gnus-newsgroup-expunged-tally ?d)
1464       (?s (gnus-current-score-file-nondirectory) ?s))))
1465
1466 (defconst gnus-group-mode-line-format-alist 
1467   (` ((?S gnus-tmp-news-server ?s)
1468       (?M gnus-tmp-news-method ?s)
1469       (?u gnus-tmp-user-defined ?s))))
1470
1471 (defvar gnus-have-read-active-file nil)
1472
1473 (defconst gnus-maintainer
1474   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1475   "The mail address of the Gnus maintainers.")
1476
1477 (defconst gnus-version "September Gnus v0.10"
1478   "Version number for this version of Gnus.")
1479
1480 (defvar gnus-info-nodes
1481   '((gnus-group-mode            "(gnus)The Group Buffer")
1482     (gnus-summary-mode          "(gnus)The Summary Buffer")
1483     (gnus-article-mode          "(gnus)The Article Buffer"))
1484   "Assoc list of major modes and related Info nodes.")
1485
1486 (defvar gnus-documentation-group-file "~/dgnus/lisp/doc.txt"
1487   "The location of the Gnus documentation group.")
1488
1489 (defvar gnus-group-buffer "*Group*")
1490 (defvar gnus-summary-buffer "*Summary*")
1491 (defvar gnus-article-buffer "*Article*")
1492 (defvar gnus-server-buffer "*Server*")
1493
1494 (defvar gnus-work-buffer " *gnus work*")
1495
1496 (defvar gnus-original-article-buffer " *Original Article*")
1497 (defvar gnus-original-article nil)
1498
1499 (defvar gnus-buffer-list nil
1500   "Gnus buffers that should be killed on exit.")
1501
1502 (defvar gnus-server-alist nil
1503   "List of available servers.")
1504
1505 (defvar gnus-slave nil
1506   "Whether this Gnus is a slave or not.")
1507
1508 (defvar gnus-variable-list
1509   '(gnus-newsrc-options gnus-newsrc-options-n
1510     gnus-newsrc-last-checked-date 
1511     gnus-newsrc-alist gnus-server-alist
1512     gnus-killed-list gnus-zombie-list)
1513   "Gnus variables saved in the quick startup file.")
1514
1515 (defvar gnus-newsrc-options nil
1516   "Options line in the .newsrc file.")
1517
1518 (defvar gnus-newsrc-options-n nil
1519   "List of regexps representing groups to be subscribed/ignored unconditionally.") 
1520
1521 (defvar gnus-newsrc-last-checked-date nil
1522   "Date Gnus last asked server for new newsgroups.")
1523
1524 (defvar gnus-newsrc-alist nil
1525   "Assoc list of read articles.
1526 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1527
1528 (defvar gnus-newsrc-hashtb nil
1529   "Hashtable of gnus-newsrc-alist.")
1530
1531 (defvar gnus-killed-list nil
1532   "List of killed newsgroups.")
1533
1534 (defvar gnus-killed-hashtb nil
1535   "Hash table equivalent of gnus-killed-list.")
1536
1537 (defvar gnus-zombie-list nil
1538   "List of almost dead newsgroups.")
1539
1540 (defvar gnus-description-hashtb nil
1541   "Descriptions of newsgroups.")
1542
1543 (defvar gnus-list-of-killed-groups nil
1544   "List of newsgroups that have recently been killed by the user.")
1545
1546 (defvar gnus-active-hashtb nil
1547   "Hashtable of active articles.")
1548
1549 (defvar gnus-moderated-list nil
1550   "List of moderated newsgroups.")
1551
1552 (defvar gnus-group-marked nil)
1553
1554 (defvar gnus-current-startup-file nil
1555   "Startup file for the current host.")
1556
1557 (defvar gnus-last-search-regexp nil
1558   "Default regexp for article search command.")
1559
1560 (defvar gnus-last-shell-command nil
1561   "Default shell command on article.")
1562
1563 (defvar gnus-current-select-method nil
1564   "The current method for selecting a newsgroup.")
1565
1566 (defvar gnus-group-list-mode nil)
1567
1568 (defvar gnus-article-internal-prepare-hook nil)
1569
1570 (defvar gnus-newsgroup-name nil)
1571 (defvar gnus-newsgroup-begin nil)
1572 (defvar gnus-newsgroup-end nil)
1573 (defvar gnus-newsgroup-last-rmail nil)
1574 (defvar gnus-newsgroup-last-mail nil)
1575 (defvar gnus-newsgroup-last-folder nil)
1576 (defvar gnus-newsgroup-last-file nil)
1577 (defvar gnus-newsgroup-auto-expire nil)
1578 (defvar gnus-newsgroup-active nil)
1579
1580 (defvar gnus-newsgroup-data nil)
1581 (defvar gnus-newsgroup-data-reverse nil)
1582 (defvar gnus-newsgroup-limit nil)
1583 (defvar gnus-newsgroup-limits nil)
1584
1585 (defvar gnus-newsgroup-unreads nil
1586   "List of unread articles in the current newsgroup.")
1587
1588 (defvar gnus-newsgroup-unselected nil
1589   "List of unselected unread articles in the current newsgroup.")
1590
1591 (defvar gnus-newsgroup-reads nil
1592   "Alist of read articles and article marks in the current newsgroup.")
1593
1594 (defvar gnus-newsgroup-expunged-tally nil)
1595
1596 (defvar gnus-newsgroup-marked nil
1597   "List of ticked articles in the current newsgroup (a subset of unread art).")
1598
1599 (defvar gnus-newsgroup-killed nil
1600   "List of ranges of articles that have been through the scoring process.")
1601
1602 (defvar gnus-newsgroup-kill-headers nil)
1603
1604 (defvar gnus-newsgroup-replied nil
1605   "List of articles that have been replied to in the current newsgroup.")
1606
1607 (defvar gnus-newsgroup-expirable nil
1608   "List of articles in the current newsgroup that can be expired.")
1609
1610 (defvar gnus-newsgroup-processable nil
1611   "List of articles in the current newsgroup that can be processed.")
1612
1613 (defvar gnus-newsgroup-bookmarks nil
1614   "List of articles in the current newsgroup that have bookmarks.")
1615
1616 (defvar gnus-newsgroup-dormant nil
1617   "List of dormant articles in the current newsgroup.")
1618
1619 (defvar gnus-newsgroup-scored nil
1620   "List of scored articles in the current newsgroup.")
1621
1622 (defvar gnus-newsgroup-headers nil
1623   "List of article headers in the current newsgroup.")
1624
1625 (defvar gnus-newsgroup-ancient nil
1626   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1627
1628 (defvar gnus-current-article nil)
1629 (defvar gnus-article-current nil)
1630 (defvar gnus-current-headers nil)
1631 (defvar gnus-have-all-headers nil)
1632 (defvar gnus-last-article nil)
1633 (defvar gnus-newsgroup-history nil)
1634 (defvar gnus-current-kill-article nil)
1635
1636 ;; Save window configuration.
1637 (defvar gnus-prev-winconf nil)
1638
1639
1640 ;; Format specs.  The chunks below are the machine-generated forms
1641 ;; that are to be evaled as the result of the default format strings.
1642 ;; We write them in here to get them byte-compiled.  That way the
1643 ;; default actions will be quite fast, while still retaining the full
1644 ;; flexibility of the user-defined format specs. 
1645
1646 ;; First we have lots of dummy defvars to let the compiler know these
1647 ;; are realyl dynamic variables.
1648
1649 (defvar gnus-tmp-unread)
1650 (defvar gnus-tmp-replied)
1651 (defvar gnus-tmp-score-char)
1652 (defvar gnus-tmp-indentation)
1653 (defvar gnus-tmp-opening-bracket)
1654 (defvar gnus-tmp-lines)
1655 (defvar gnus-tmp-name)
1656 (defvar gnus-tmp-closing-bracket)
1657 (defvar gnus-tmp-subject-or-nil)
1658 (defvar gnus-tmp-subject)
1659 (defvar gnus-tmp-marked)
1660 (defvar gnus-tmp-subscribed)
1661 (defvar gnus-tmp-process-marked)
1662 (defvar gnus-tmp-number-of-unread-unticked)
1663 (defvar gnus-tmp-group-name)
1664 (defvar gnus-tmp-group)
1665 (defvar gnus-tmp-article-number)
1666 (defvar gnus-tmp-unread-and-unselected)
1667 (defvar gnus-tmp-news-method)
1668 (defvar gnus-tmp-news-server)
1669 (defvar gnus-tmp-article-number)
1670 (defvar gnus-mouse-face)
1671
1672 (defun gnus-byte-code (func)
1673   (let ((fval (symbol-function func)))
1674     (if (byte-code-function-p fval)
1675         (list 'byte-code (aref fval 1) (aref fval 2) (aref fval 3))
1676       (list 'eval (cons 'progn (cdr (cdr fval)))))))
1677
1678 (defun gnus-summary-line-format-spec ()
1679   (insert gnus-tmp-unread gnus-tmp-replied 
1680           gnus-tmp-score-char gnus-tmp-indentation)
1681   (let ((b (point)))
1682     (insert gnus-tmp-opening-bracket 
1683             (format "%4d: %-20s" 
1684                     gnus-tmp-lines 
1685                     (if (> (length gnus-tmp-name) 20) 
1686                         (substring gnus-tmp-name 0 20) 
1687                       gnus-tmp-name))
1688             gnus-tmp-closing-bracket
1689             " " gnus-tmp-subject-or-nil "\n")
1690     (put-text-property b (+ b 28) 'mouse-face gnus-mouse-face)))
1691 (defvar gnus-summary-line-format-spec 
1692   (gnus-byte-code 'gnus-summary-line-format-spec))
1693
1694 (defun gnus-summary-dummy-line-format-spec ()
1695   (insert "*  :                          : " gnus-tmp-subject "\n"))
1696 (defvar gnus-summary-dummy-line-format-spec 
1697   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
1698
1699 (defun gnus-group-line-format-spec ()
1700   (insert gnus-tmp-marked gnus-tmp-subscribed 
1701           gnus-tmp-process-marked
1702    (format "%5s: " gnus-tmp-number-of-unread-unticked))
1703   (let ((b (point)))
1704     (insert gnus-tmp-group "\n")
1705     (put-text-property b (1- (point)) 'mouse-face gnus-mouse-face)))
1706 (defvar gnus-group-line-format-spec 
1707   (gnus-byte-code 'gnus-group-line-format-spec))
1708
1709 (defun gnus-summary-mode-line-format-spec ()
1710 (format "Gnus  %s/%d %s" gnus-tmp-group-name gnus-tmp-article-number gnus-tmp-unread-and-unselected))
1711 (defvar gnus-summary-mode-line-format-spec
1712   (gnus-byte-code 'gnus-summary-mode-line-format-spec))
1713
1714 (defun gnus-group-mode-line-format-spec ()
1715 (format "Gnus  List of groups   {%s:%s}  " gnus-tmp-news-method gnus-tmp-news-server))
1716 (defvar gnus-group-mode-line-format-spec 
1717   (gnus-byte-code 'gnus-group-mode-line-format-spec))
1718
1719 (defun gnus-article-mode-line-format-spec ()
1720 (format "Gnus  %s/%d %s" gnus-tmp-group-name gnus-tmp-article-number gnus-tmp-subject))
1721 (defvar gnus-article-mode-line-format-spec
1722   (gnus-byte-code 'gnus-article-mode-line-format-spec))
1723
1724 (defvar gnus-old-specs 
1725   '((article-mode . "Gnus  %G/%A %S")
1726     (group-mode . "Gnus  List of groups   {%M:%S}  ")
1727     (summary-mode . "Gnus  %G/%A %Z")
1728     (group . "%M%S%p%5y: %(%g%)\n")
1729     (summary-dummy . "*  :                          : %S\n")
1730     (summary . "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n")))
1731
1732 ;;; Phew. All that gruft is over, fortunately.  Perhaps one should
1733 ;;; hand-optimize the functions above, though.
1734
1735 (defvar gnus-summary-mark-positions nil)
1736 (defvar gnus-group-mark-positions nil)
1737
1738 (defvar gnus-summary-expunge-below nil)
1739 (defvar gnus-reffed-article-number nil)
1740
1741 ;;; Let the byte-compiler know that we know about this variable.
1742 (defvar rmail-default-rmail-file)
1743
1744 (defvar gnus-cache-removeable-articles nil)
1745
1746 (defconst gnus-summary-local-variables 
1747   '(gnus-newsgroup-name 
1748     gnus-newsgroup-begin gnus-newsgroup-end 
1749     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail 
1750     gnus-newsgroup-last-folder gnus-newsgroup-last-file 
1751     gnus-newsgroup-auto-expire gnus-newsgroup-unreads 
1752     gnus-newsgroup-unselected gnus-newsgroup-marked
1753     gnus-newsgroup-reads
1754     gnus-newsgroup-replied gnus-newsgroup-expirable
1755     gnus-newsgroup-processable gnus-newsgroup-killed
1756     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1757     gnus-newsgroup-headers 
1758     gnus-current-article gnus-current-headers gnus-have-all-headers
1759     gnus-last-article gnus-article-internal-prepare-hook
1760     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1761     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1762     gnus-newsgroup-async
1763     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below 
1764     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1765     gnus-newsgroup-history gnus-newsgroup-ancient
1766     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1767     (gnus-newsgroup-expunged-tally . 0)
1768     gnus-cache-removeable-articles
1769     gnus-newsgroup-data gnus-newsgroup-data-reverse
1770     gnus-newsgroup-limit gnus-newsgroup-limits)
1771   "Variables that are buffer-local to the summary buffers.")
1772
1773 (defconst gnus-bug-message
1774   "Sending a bug report to the Gnus Towers.
1775 ========================================
1776
1777 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1778 be sent to the Gnus Bug Exterminators. 
1779
1780 At the bottom of the buffer you'll see lots of variable settings.
1781 Please do not delete those.  They will tell the Bug People what your
1782 environment is, so that it will be easier to locate the bugs.
1783
1784 If you have found a bug that makes Emacs go \"beep\", set
1785 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET') 
1786 and include the backtrace in your bug report.
1787
1788 Please describe the bug in annoying, painstaking detail.
1789
1790 Thank you for your help in stamping out bugs.
1791 ")
1792
1793 ;;; End of variables.
1794
1795 ;; Define some autoload functions Gnus might use.
1796 (eval-and-compile
1797
1798   ;; Various 
1799   (autoload 'metamail-buffer "metamail")
1800   (autoload 'Info-goto-node "info")
1801   (autoload 'hexl-hex-string-to-integer "hexl")
1802   (autoload 'pp "pp")
1803   (autoload 'pp-to-string "pp")
1804   (autoload 'pp-eval-expression "pp")
1805   (autoload 'mail-extract-address-components "mail-extr")
1806
1807   (autoload 'nnmail-split-fancy "nnmail")
1808   (autoload 'nnmail-article-group "nnmail")
1809   (autoload 'nnvirtual-catchup-group "nnvirtual")
1810
1811   ;; timezone
1812   (autoload 'timezone-make-date-arpa-standard "timezone")
1813   (autoload 'timezone-fix-time "timezone")
1814   (autoload 'timezone-make-sortable-date "timezone")
1815   (autoload 'timezone-make-time-string "timezone")
1816
1817   ;; rmail & friends
1818   (autoload 'mail-position-on-field "sendmail")
1819   (autoload 'mail-setup "sendmail")
1820   (autoload 'rmail-output "rmailout")
1821   (autoload 'news-mail-other-window "rnewspost")
1822   (autoload 'news-reply-yank-original "rnewspost")
1823   (autoload 'news-caesar-buffer-body "rnewspost")
1824   (autoload 'rmail-insert-rmail-file-header "rmail")
1825   (autoload 'rmail-count-new-messages "rmail")
1826   (autoload 'rmail-show-message "rmail")
1827
1828   ;; gnus-soup
1829   (autoload 'gnus-group-brew-soup "gnus-soup" nil t)
1830   (autoload 'gnus-brew-soup "gnus-soup" nil t)
1831   (autoload 'gnus-soup-add-article "gnus-soup" nil t)
1832   (autoload 'gnus-soup-send-replies "gnus-soup" nil t)
1833   (autoload 'gnus-soup-save-areas "gnus-soup" nil t)
1834   (autoload 'gnus-soup-pack-packet "gnus-soup" nil t)
1835   (autoload 'nnsoup-pack-replies "nnsoup" nil t)
1836
1837   ;; gnus-mh
1838   (autoload 'gnus-mh-mail-setup "gnus-mh")
1839   (autoload 'gnus-summary-save-in-folder "gnus-mh")
1840   (autoload 'gnus-summary-save-article-folder "gnus-mh")
1841   (autoload 'gnus-Folder-save-name "gnus-mh")
1842   (autoload 'gnus-folder-save-name "gnus-mh")
1843
1844   ;; gnus-vis misc
1845   (autoload 'gnus-group-make-menu-bar "gnus-vis")
1846   (autoload 'gnus-summary-make-menu-bar "gnus-vis")
1847   (autoload 'gnus-server-make-menu-bar "gnus-vis")
1848   (autoload 'gnus-article-make-menu-bar "gnus-vis")
1849   (autoload 'gnus-browse-make-menu-bar "gnus-vis")
1850   (autoload 'gnus-highlight-selected-summary "gnus-vis")
1851   (autoload 'gnus-summary-highlight-line "gnus-vis")
1852   (autoload 'gnus-carpal-setup-buffer "gnus-vis")
1853
1854   ;; gnus-demon
1855   (autoload 'gnus-demon-add-nocem "gnus-demon")
1856   (autoload 'gnus-demon-add-scanmail "gnus-demon")
1857   (autoload 'gnus-demon-add-disconnection "gnus-demon")
1858   (autoload 'gnus-demon-add-handler "gnus-demon")
1859   (autoload 'gnus-demon-remove-handler "gnus-demon")
1860   (autoload 'gnus-demon-init "gnus-demon" nil t)
1861   (autoload 'gnus-demon-cancel "gnus-demon" nil t)
1862
1863   ;; gnus-nocem
1864   (autoload 'gnus-nocem-scan-groups "gnus-nocem")
1865   (autoload 'gnus-nocem-close "gnus-nocem")
1866
1867   ;; gnus-vis article
1868   (autoload 'gnus-article-push-button "gnus-vis" nil t)
1869   (autoload 'gnus-article-press-button "gnus-vis" nil t)
1870   (autoload 'gnus-article-highlight "gnus-vis" nil t)
1871   (autoload 'gnus-article-highlight-some "gnus-vis" nil t)
1872   (autoload 'gnus-article-hide "gnus-vis" nil t)
1873   (autoload 'gnus-article-hide-signature "gnus-vis" nil t)
1874   (autoload 'gnus-article-highlight-headers "gnus-vis" nil t)
1875   (autoload 'gnus-article-highlight-signature "gnus-vis" nil t)
1876   (autoload 'gnus-article-add-buttons "gnus-vis" nil t)
1877   (autoload 'gnus-article-add-buttons-to-head "gnus-vis" nil t)
1878   (autoload 'gnus-article-next-button "gnus-vis" nil t)
1879   (autoload 'gnus-article-add-button "gnus-vis")
1880
1881   ;; gnus-cite
1882   (autoload 'gnus-article-highlight-citation "gnus-cite" nil t)
1883   (autoload 'gnus-article-hide-citation-maybe "gnus-cite" nil t)
1884   (autoload 'gnus-article-hide-citation "gnus-cite" nil t)
1885
1886   ;; gnus-kill
1887   (autoload 'gnus-kill "gnus-kill")
1888   (autoload 'gnus-apply-kill-file-internal "gnus-kill")
1889   (autoload 'gnus-kill-file-edit-file "gnus-kill")
1890   (autoload 'gnus-kill-file-raise-followups-to-author "gnus-kill")
1891   (autoload 'gnus-execute "gnus-kill")
1892   (autoload 'gnus-expunge "gnus-kill")
1893
1894   ;; gnus-cache
1895   (autoload 'gnus-cache-possibly-enter-article "gnus-cache")
1896   (autoload 'gnus-cache-save-buffers "gnus-cache")
1897   (autoload 'gnus-cache-possibly-remove-articles "gnus-cache")
1898   (autoload 'gnus-cache-request-article "gnus-cache")
1899   (autoload 'gnus-cache-retrieve-headers "gnus-cache")
1900   (autoload 'gnus-cache-possibly-alter-active "gnus-cache")
1901   (autoload 'gnus-jog-cache "gnus-cache" nil t)
1902   (autoload 'gnus-cache-enter-remove-article "gnus-cache")
1903
1904   ;; gnus-score
1905   (autoload 'gnus-summary-increase-score "gnus-score" nil t)
1906   (autoload 'gnus-summary-lower-score "gnus-score" nil t)
1907   (autoload 'gnus-summary-score-map "gnus-score" nil nil 'keymap)
1908   (autoload 'gnus-score-save "gnus-score")
1909   (autoload 'gnus-score-headers "gnus-score")
1910   (autoload 'gnus-current-score-file-nondirectory "gnus-score")
1911   (autoload 'gnus-score-adaptive "gnus-score")
1912   (autoload 'gnus-score-find-trace "gnus-score")
1913   (autoload 'gnus-score-flush-cache "gnus-score" nil t)
1914   (autoload 'gnus-score-close "gnus-score" nil t)
1915
1916   ;; gnus-edit
1917   (autoload 'gnus-score-customize "gnus-edit" nil t)
1918
1919   ;; gnus-topic
1920   (autoload 'gnus-topic-fold "gnus-topic")
1921   (autoload 'gnus-group-prepare-topics "gnus-topic")
1922
1923   ;; gnus-uu
1924   (autoload 'gnus-uu-extract-map "gnus-uu" nil nil 'keymap)
1925   (autoload 'gnus-uu-mark-map "gnus-uu" nil nil 'keymap)
1926   (autoload 'gnus-uu-digest-mail-forward "gnus-uu" nil t)
1927   (autoload 'gnus-uu-digest-post-forward "gnus-uu" nil t)
1928   (autoload 'gnus-uu-mark-series "gnus-uu" nil t)
1929   (autoload 'gnus-uu-mark-region "gnus-uu" nil t)
1930   (autoload 'gnus-uu-mark-by-regexp "gnus-uu" nil t)
1931   (autoload 'gnus-uu-mark-all "gnus-uu" nil t)
1932   (autoload 'gnus-uu-mark-sparse "gnus-uu" nil t)
1933   (autoload 'gnus-uu-mark-thread "gnus-uu" nil t)
1934   (autoload 'gnus-uu-decode-uu "gnus-uu" nil t)
1935   (autoload 'gnus-uu-decode-uu-and-save "gnus-uu" nil t)
1936   (autoload 'gnus-uu-decode-unshar "gnus-uu" nil t)
1937   (autoload 'gnus-uu-decode-unshar-and-save "gnus-uu" nil t)
1938   (autoload 'gnus-uu-decode-save "gnus-uu" nil t)
1939   (autoload 'gnus-uu-decode-binhex "gnus-uu" nil t)
1940   (autoload 'gnus-uu-decode-uu-view "gnus-uu" nil t)
1941   (autoload 'gnus-uu-decode-uu-and-save-view "gnus-uu" nil t)
1942   (autoload 'gnus-uu-decode-unshar-view "gnus-uu" nil t)
1943   (autoload 'gnus-uu-decode-unshar-and-save-view "gnus-uu" nil t)
1944   (autoload 'gnus-uu-decode-save-view "gnus-uu" nil t)
1945   (autoload 'gnus-uu-decode-binhex-view "gnus-uu" nil t)
1946
1947   ;; gnus-msg
1948   (autoload 'gnus-summary-send-map "gnus-msg" nil nil 'keymap)
1949   (autoload 'gnus-group-post-news "gnus-msg" nil t)
1950   (autoload 'gnus-group-mail "gnus-msg" nil t)
1951   (autoload 'gnus-summary-post-news "gnus-msg" nil t)
1952   (autoload 'gnus-summary-followup "gnus-msg" nil t)
1953   (autoload 'gnus-summary-followup-with-original "gnus-msg" nil t)
1954   (autoload 'gnus-summary-followup-and-reply "gnus-msg" nil t)
1955   (autoload 'gnus-summary-followup-and-reply-with-original "gnus-msg" nil t)
1956   (autoload 'gnus-summary-cancel-article "gnus-msg" nil t)
1957   (autoload 'gnus-summary-supersede-article "gnus-msg" nil t)
1958   (autoload 'gnus-post-news "gnus-msg" nil t)
1959   (autoload 'gnus-inews-news "gnus-msg" nil t)
1960   (autoload 'gnus-cancel-news "gnus-msg" nil t)
1961   (autoload 'gnus-summary-reply "gnus-msg" nil t)
1962   (autoload 'gnus-summary-reply-with-original "gnus-msg" nil t)
1963   (autoload 'gnus-summary-mail-forward "gnus-msg" nil t)
1964   (autoload 'gnus-summary-mail-other-window "gnus-msg" nil t)
1965   (autoload 'gnus-mail-yank-original "gnus-msg")
1966   (autoload 'gnus-mail-send-and-exit "gnus-msg")
1967   (autoload 'gnus-sendmail-setup-mail "gnus-msg")
1968   (autoload 'gnus-article-mail "gnus-msg")
1969   (autoload 'gnus-bug "gnus-msg" nil t)
1970   (autoload 'gnus-inews-message-id "gnus-msg")
1971
1972   ;; gnus-vm
1973   (autoload 'gnus-summary-save-in-vm "gnus-vm" nil t)
1974   (autoload 'gnus-summary-save-article-vm "gnus-vm" nil t)
1975   (autoload 'gnus-mail-forward-using-vm "gnus-vm")
1976   (autoload 'gnus-mail-reply-using-vm "gnus-vm")
1977   (autoload 'gnus-mail-other-window-using-vm "gnus-vm" nil t)
1978   (autoload 'gnus-yank-article "gnus-vm" nil t)
1979
1980   )
1981
1982 \f
1983
1984 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1985 ;; If you want the cursor to go somewhere else, set these two
1986 ;; functions in some startup hook to whatever you want.
1987 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
1988 (defalias 'gnus-group-position-point 'gnus-goto-colon)
1989
1990 ;;; Various macros and substs.
1991
1992 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
1993   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
1994   (` (let ((GnusStartBufferWindow (selected-window)))
1995        (unwind-protect
1996            (progn
1997              (pop-to-buffer (, buffer))
1998              (,@ forms))
1999          (select-window GnusStartBufferWindow)))))
2000
2001 (defmacro gnus-gethash (string hashtable)
2002   "Get hash value of STRING in HASHTABLE."
2003   ;;(` (symbol-value (abbrev-symbol (, string) (, hashtable))))
2004   ;;(` (abbrev-expansion (, string) (, hashtable)))
2005   (` (symbol-value (intern-soft (, string) (, hashtable)))))
2006
2007 (defmacro gnus-sethash (string value hashtable)
2008   "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
2009   ;; We cannot use define-abbrev since it only accepts string as value.
2010   ;; (set (intern string hashtable) value))
2011   (` (set (intern (, string) (, hashtable)) (, value))))
2012
2013 (defmacro gnus-buffer-substring (beg end)
2014   (` (buffer-substring (match-beginning (, beg)) (match-end (, end)))))
2015
2016 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2017 ;;   function `substring' might cut on a middle of multi-octet
2018 ;;   character.
2019 (defun gnus-truncate-string (str width)
2020   (substring str 0 width))
2021
2022 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>. A safe way
2023 ;; to limit the length of a string. This function is necessary since
2024 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
2025 (defsubst gnus-limit-string (str width)
2026   (if (> (length str) width)
2027       (substring str 0 width)
2028     str))
2029
2030 (defsubst gnus-simplify-subject-re (subject)
2031   "Remove \"Re:\" from subject lines."
2032   (let ((case-fold-search t))
2033     (if (string-match "^re: *" subject)
2034         (substring subject (match-end 0))
2035       subject)))
2036
2037 (defsubst gnus-goto-char (point)
2038   (and point (goto-char point)))
2039
2040 (defmacro gnus-buffer-exists-p (buffer)
2041   (` (and (, buffer)
2042           (funcall (if (stringp (, buffer)) 'get-buffer 'buffer-name)
2043                    (, buffer)))))
2044
2045 (defmacro gnus-kill-buffer (buffer)
2046   (` (if (gnus-buffer-exists-p (, buffer))
2047          (kill-buffer (, buffer)))))
2048
2049 (defsubst gnus-point-at-bol ()
2050   "Return point at the beginning of the line."
2051   (let ((p (point)))
2052     (beginning-of-line)
2053     (prog1
2054         (point)
2055       (goto-char p))))
2056
2057 (defsubst gnus-point-at-eol ()
2058   "Return point at the end of the line."
2059   (let ((p (point)))
2060     (end-of-line)
2061     (prog1
2062         (point)
2063       (goto-char p))))
2064
2065 ;; Delete the current line (and the next N lines.);
2066 (defmacro gnus-delete-line (&optional n)
2067   (` (delete-region (progn (beginning-of-line) (point))
2068                     (progn (forward-line (, (or n 1))) (point)))))
2069
2070 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2071 (defvar gnus-init-inhibit nil)
2072 (defun gnus-read-init-file (&optional inhibit-next)
2073   (if gnus-init-inhibit
2074       (setq gnus-init-inhibit nil)
2075     (setq gnus-init-inhibit inhibit-next)
2076     (and gnus-init-file
2077          (or (and (file-exists-p gnus-init-file) 
2078                   ;; Don't try to load a directory.
2079                   (not (file-directory-p gnus-init-file)))
2080              (file-exists-p (concat gnus-init-file ".el"))
2081              (file-exists-p (concat gnus-init-file ".elc")))
2082          (load gnus-init-file nil t))))
2083
2084 ;;; Load the user startup file.
2085 ;; (eval '(gnus-read-init-file 'inhibit))
2086
2087 ;;; Load the compatability functions. 
2088
2089 (require 'gnus-cus)
2090 (require 'gnus-ems)
2091
2092 \f
2093 ;;;
2094 ;;; Gnus Utility Functions
2095 ;;;
2096
2097 (defun gnus-extract-address-components (from)
2098   (let (name address)
2099     ;; First find the address - the thing with the @ in it.  This may
2100     ;; not be accurate in mail addresses, but does the trick most of
2101     ;; the time in news messages.
2102     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2103         (setq address (substring from (match-beginning 0) (match-end 0))))
2104     ;; Then we check whether the "name <address>" format is used.
2105     (and address
2106          (string-match (concat "<" (regexp-quote address) ">") from)
2107          (and (setq name (substring from 0 (1- (match-beginning 0))))
2108               ;; Strip any quotes from the name.
2109               (string-match "\".*\"" name)
2110               (setq name (substring name 1 (1- (match-end 0))))))
2111     ;; If not, then "address (name)" is used.
2112     (or name
2113         (and (string-match "(.+)" from)
2114              (setq name (substring from (1+ (match-beginning 0)) 
2115                                    (1- (match-end 0)))))
2116         (and (string-match "()" from)
2117              (setq name address))
2118         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2119         ;; XOVER might not support folded From headers.
2120         (and (string-match "(.*" from)
2121              (setq name (substring from (1+ (match-beginning 0)) 
2122                                    (match-end 0)))))
2123     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2124     (list (or name from) (or address from))))
2125
2126 (defun gnus-fetch-field (field)
2127   "Return the value of the header FIELD of current article."
2128   (save-excursion
2129     (save-restriction
2130       (let ((case-fold-search t))
2131         (gnus-narrow-to-headers)
2132         (mail-fetch-field field)))))
2133
2134 (defun gnus-goto-colon ()
2135   (beginning-of-line)
2136   (search-forward ":" (gnus-point-at-eol) t))
2137
2138 (defun gnus-narrow-to-headers ()
2139   (widen)
2140   (save-excursion
2141     (narrow-to-region
2142      (goto-char (point-min))
2143      (if (search-forward "\n\n" nil t)
2144          (1- (point))
2145        (point-max)))))
2146
2147 (defun gnus-update-format-specifications ()
2148   (gnus-make-thread-indent-array)
2149
2150   (let ((formats '(summary summary-dummy group 
2151                            summary-mode group-mode article-mode))
2152         old-format new-format)
2153     (while formats
2154       (setq new-format (symbol-value
2155                         (intern (format "gnus-%s-line-format" (car formats)))))
2156       (or (and (setq old-format (cdr (assq (car formats) gnus-old-specs)))
2157                (equal old-format new-format))
2158           (set (intern (format "gnus-%s-line-format-spec" (car formats)))
2159                (gnus-parse-format
2160                 new-format
2161                 (symbol-value 
2162                  (intern (format "gnus-%s-line-format-alist"
2163                                  (if (eq (car formats) 'article-mode)
2164                                      'summary-mode (car formats)))))
2165                 (not (string-match "mode$" (symbol-name (car formats)))))))
2166       (setq gnus-old-specs (cons (cons (car formats) new-format)
2167                                  (delq (car formats) gnus-old-specs)))
2168       (setq formats (cdr formats))))
2169       
2170   (gnus-update-group-mark-positions)
2171   (gnus-update-summary-mark-positions)
2172
2173   (if (and (string-match "%D" gnus-group-line-format)
2174            (not gnus-description-hashtb)
2175            gnus-read-active-file)
2176       (gnus-read-all-descriptions-files)))
2177
2178 (defun gnus-update-summary-mark-positions ()
2179   (save-excursion
2180     (let ((gnus-replied-mark 129)
2181           (gnus-score-below-mark 130)
2182           (gnus-score-over-mark 130)
2183           (thread nil)
2184           pos)
2185       (gnus-set-work-buffer)
2186       (gnus-summary-insert-line 
2187        nil [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2188       (goto-char (point-min))
2189       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2190                                          (- (point) 2)))))
2191       (goto-char (point-min))
2192       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
2193                                           (- (point) 2))) pos))
2194       (goto-char (point-min))
2195       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
2196                                         (- (point) 2))) pos))
2197       (setq gnus-summary-mark-positions pos))))
2198
2199 (defun gnus-update-group-mark-positions ()
2200   (save-excursion
2201     (let ((gnus-process-mark 128)
2202           (gnus-group-marked '("dummy.group")))
2203       (gnus-sethash "dummy.group" '(0 . 0) gnus-active-hashtb)
2204       (gnus-set-work-buffer)
2205       (gnus-group-insert-group-line nil "dummy.group" 0 nil 0 nil)
2206       (goto-char (point-min))
2207       (setq gnus-group-mark-positions
2208             (list (cons 'process (and (search-forward "\200" nil t)
2209                                       (- (point) 2))))))))
2210
2211 (defun gnus-mouse-face-function (form)
2212   (` (let ((string (, form)))
2213        (put-text-property 0 (length string) 'mouse-face gnus-mouse-face string)
2214        string)))
2215
2216 (defun gnus-max-width-function (el max-width)
2217   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2218   (` (let* ((val (eval (, el)))
2219             (valstr (if (numberp val)
2220                         (int-to-string val) val)))
2221        (if (> (length valstr) (, max-width))
2222            (substring valstr 0 (, max-width))
2223          valstr))))
2224
2225 (defun gnus-parse-format (format spec-alist &optional insert)
2226   ;; This function parses the FORMAT string with the help of the
2227   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2228   ;; string.  If the FORMAT string contains the specifiers %( and %)
2229   ;; the text between them will have the mouse-face text property.
2230   (let ((result
2231          (if (string-match "\\`\\(.*\\)%(\\(.*\\)%)\\(.*\n?\\)\\'" format)
2232              (if (and gnus-visual gnus-mouse-face)
2233                  (let ((pre (substring format (match-beginning 1)
2234                                        (match-end 1)))
2235                        (button (substring format (match-beginning 2)
2236                                           (match-end 2)))
2237                        (post (substring format (match-beginning 3)
2238                                         (match-end 3))))
2239                    (list 'concat
2240                          (gnus-parse-simple-format pre spec-alist)
2241                          (gnus-mouse-face-function 
2242                           (gnus-parse-simple-format button spec-alist))
2243                          (gnus-parse-simple-format post spec-alist)))
2244                (gnus-parse-simple-format
2245                 (concat (substring format (match-beginning 1) (match-end 1))
2246                         (substring format (match-beginning 2) (match-end 2))
2247                         (substring format (match-beginning 3) (match-end 3)))
2248                 spec-alist))
2249            (gnus-parse-simple-format format spec-alist))))
2250     (if insert (list 'insert result) result)))
2251
2252 (defun gnus-parse-simple-format (format spec-alist)
2253   ;; This function parses the FORMAT string with the help of the
2254   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2255   ;; string. The list will consist of the symbol `format', a format
2256   ;; specification string, and a list of forms depending on the
2257   ;; SPEC-ALIST.
2258   (let ((max-width 0)
2259         spec flist fstring newspec elem beg)
2260     (save-excursion
2261       (gnus-set-work-buffer)
2262       (insert format)
2263       (goto-char (point-min))
2264       (while (re-search-forward "%[-0-9]*\\(,[0-9]*\\)*\\(.\\)\\(.\\)?" nil t)
2265         (setq spec (string-to-char (buffer-substring (match-beginning 2)
2266                                                      (match-end 2))))
2267         ;; First check if there are any specs that look anything like
2268         ;; "%12,12A", ie. with a "max width specification". These have
2269         ;; to be treated specially.
2270         (if (setq beg (match-beginning 1))
2271             (setq max-width 
2272                   (string-to-int 
2273                    (buffer-substring (1+ (match-beginning 1)) (match-end 1))))
2274           (setq max-width 0)
2275           (setq beg (match-beginning 2)))
2276         ;; Find the specification from `spec-alist'.
2277         (if (not (setq elem (cdr (assq spec spec-alist))))
2278             (setq elem '("*" ?s)))
2279         ;; Treat user defined format specifiers specially
2280         (and (eq (car elem) 'gnus-tmp-user-defined)
2281              (setq elem
2282                    (list 
2283                     (list (intern (concat "gnus-user-format-function-"
2284                                           (buffer-substring
2285                                            (match-beginning 3)
2286                                            (match-end 3))))
2287                           'header)
2288                     ?s))
2289              (delete-region (match-beginning 3) (match-end 3)))
2290         (if (not (zerop max-width))
2291             (let ((el (car elem)))
2292               (cond ((= (car (cdr elem)) ?c) 
2293                      (setq el (list 'char-to-string el)))
2294                     ((= (car (cdr elem)) ?d)
2295                      (numberp el) (setq el (list 'int-to-string el))))
2296               (setq flist (cons (gnus-max-width-function el max-width)
2297                                 flist))
2298               (setq newspec ?s))
2299           (setq flist (cons (car elem) flist))
2300           (setq newspec (car (cdr elem))))
2301         ;; Remove the old specification (and possibly a ",12" string).
2302         (delete-region beg (match-end 2))
2303         ;; Insert the new specification.
2304         (goto-char beg)
2305         (insert newspec))
2306       (setq fstring (buffer-substring 1 (point-max))))
2307     (cons 'format (cons fstring (nreverse flist)))))
2308
2309 (defun gnus-set-work-buffer ()
2310   (if (get-buffer gnus-work-buffer)
2311       (progn
2312         (set-buffer gnus-work-buffer)
2313         (erase-buffer))
2314     (set-buffer (get-buffer-create gnus-work-buffer))
2315     (kill-all-local-variables)
2316     (buffer-disable-undo (current-buffer))
2317     (gnus-add-current-to-buffer-list)))
2318
2319 ;; Article file names when saving.
2320
2321 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2322   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2323 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2324 Otherwise, it is like ~/News/news/group/num."
2325   (let ((default
2326           (expand-file-name
2327            (concat (if (gnus-use-long-file-name 'not-save)
2328                        (gnus-capitalize-newsgroup newsgroup)
2329                      (gnus-newsgroup-directory-form newsgroup))
2330                    "/" (int-to-string (mail-header-number headers)))
2331            (or gnus-article-save-directory "~/News"))))
2332     (if (and last-file
2333              (string-equal (file-name-directory default)
2334                            (file-name-directory last-file))
2335              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2336         default
2337       (or last-file default))))
2338
2339 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2340   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2341 If variable `gnus-use-long-file-name' is non-nil, it is
2342 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2343   (let ((default
2344           (expand-file-name
2345            (concat (if (gnus-use-long-file-name 'not-save)
2346                        newsgroup
2347                      (gnus-newsgroup-directory-form newsgroup))
2348                    "/" (int-to-string (mail-header-number headers)))
2349            (or gnus-article-save-directory "~/News"))))
2350     (if (and last-file
2351              (string-equal (file-name-directory default)
2352                            (file-name-directory last-file))
2353              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2354         default
2355       (or last-file default))))
2356
2357 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2358   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2359 If variable `gnus-use-long-file-name' is non-nil, it is
2360 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2361   (or last-file
2362       (expand-file-name
2363        (if (gnus-use-long-file-name 'not-save)
2364            (gnus-capitalize-newsgroup newsgroup)
2365          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2366        (or gnus-article-save-directory "~/News"))))
2367
2368 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2369   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2370 If variable `gnus-use-long-file-name' is non-nil, it is
2371 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2372   (or last-file
2373       (expand-file-name
2374        (if (gnus-use-long-file-name 'not-save)
2375            newsgroup
2376          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2377        (or gnus-article-save-directory "~/News"))))
2378
2379 ;; For subscribing new newsgroup
2380
2381 (defun gnus-subscribe-hierarchical-interactive (groups)
2382   (let ((groups (sort groups 'string<))
2383         prefixes prefix start ans group starts)
2384     (while groups
2385       (setq prefixes (list "^"))
2386       (while (and groups prefixes)
2387         (while (not (string-match (car prefixes) (car groups)))
2388           (setq prefixes (cdr prefixes)))
2389         (setq prefix (car prefixes))
2390         (setq start (1- (length prefix)))
2391         (if (and (string-match "[^\\.]\\." (car groups) start)
2392                  (cdr groups)
2393                  (setq prefix 
2394                        (concat "^" (substring (car groups) 0 (match-end 0))))
2395                  (string-match prefix (car (cdr groups))))
2396             (progn
2397               (setq prefixes (cons prefix prefixes))
2398               (message "Descend hierarchy %s? ([y]nsq): " 
2399                        (substring prefix 1 (1- (length prefix))))
2400               (setq ans (read-char))
2401               (cond ((= ans ?n)
2402                      (while (and groups 
2403                                  (string-match prefix 
2404                                                (setq group (car groups))))
2405                        (setq gnus-killed-list 
2406                              (cons group gnus-killed-list))
2407                        (gnus-sethash group group gnus-killed-hashtb)
2408                        (setq groups (cdr groups)))
2409                      (setq starts (cdr starts)))
2410                     ((= ans ?s)
2411                      (while (and groups 
2412                                  (string-match prefix 
2413                                                (setq group (car groups))))
2414                        (gnus-sethash group group gnus-killed-hashtb)
2415                        (gnus-subscribe-alphabetically (car groups))
2416                        (setq groups (cdr groups)))
2417                      (setq starts (cdr starts)))
2418                     ((= ans ?q)
2419                      (while groups
2420                        (setq group (car groups))
2421                        (setq gnus-killed-list (cons group gnus-killed-list))
2422                        (gnus-sethash group group gnus-killed-hashtb)
2423                        (setq groups (cdr groups))))
2424                     (t nil)))
2425           (message "Subscribe %s? ([n]yq)" (car groups))
2426           (setq ans (read-char))
2427           (setq group (car groups))
2428           (cond ((= ans ?y)
2429                  (gnus-subscribe-alphabetically (car groups))
2430                  (gnus-sethash group group gnus-killed-hashtb))
2431                 ((= ans ?q)
2432                  (while groups
2433                    (setq group (car groups))
2434                    (setq gnus-killed-list (cons group gnus-killed-list))
2435                    (gnus-sethash group group gnus-killed-hashtb)
2436                    (setq groups (cdr groups))))
2437                 (t 
2438                  (setq gnus-killed-list (cons group gnus-killed-list))
2439                  (gnus-sethash group group gnus-killed-hashtb)))
2440           (setq groups (cdr groups)))))))
2441
2442 (defun gnus-subscribe-randomly (newsgroup)
2443   "Subscribe new NEWSGROUP by making it the first newsgroup."
2444   (gnus-subscribe-newsgroup newsgroup))
2445
2446 (defun gnus-subscribe-alphabetically (newgroup)
2447   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2448   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2449   (let ((groups (cdr gnus-newsrc-alist))
2450         before)
2451     (while (and (not before) groups)
2452       (if (string< newgroup (car (car groups)))
2453           (setq before (car (car groups)))
2454         (setq groups (cdr groups))))
2455     (gnus-subscribe-newsgroup newgroup before)))
2456
2457 (defun gnus-subscribe-hierarchically (newgroup)
2458   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2459   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2460   (save-excursion
2461     (set-buffer (find-file-noselect gnus-current-startup-file))
2462     (let ((groupkey newgroup)
2463           before)
2464       (while (and (not before) groupkey)
2465         (goto-char (point-min))
2466         (let ((groupkey-re
2467                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2468           (while (and (re-search-forward groupkey-re nil t)
2469                       (progn
2470                         (setq before (buffer-substring
2471                                       (match-beginning 1) (match-end 1)))
2472                         (string< before newgroup)))))
2473         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2474         (setq groupkey
2475               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2476                   (substring groupkey (match-beginning 1) (match-end 1)))))
2477       (gnus-subscribe-newsgroup newgroup before))))
2478
2479 (defun gnus-subscribe-interactively (newsgroup)
2480   "Subscribe new NEWSGROUP interactively.
2481 It is inserted in hierarchical newsgroup order if subscribed. If not,
2482 it is killed."
2483   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " newsgroup))
2484       (gnus-subscribe-hierarchically newsgroup)
2485     (setq gnus-killed-list (cons newsgroup gnus-killed-list))))
2486
2487 (defun gnus-subscribe-zombies (newsgroup)
2488   "Make new NEWSGROUP a zombie group."
2489   (setq gnus-zombie-list (cons newsgroup gnus-zombie-list)))
2490
2491 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2492   "Subscribe new NEWSGROUP.
2493 If NEXT is non-nil, it is inserted before NEXT. Otherwise it is made
2494 the first newsgroup."
2495   ;; We subscribe the group by changing its level to `subscribed'.
2496   (gnus-group-change-level 
2497    newsgroup gnus-level-default-subscribed
2498    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2499   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2500
2501 ;; For directories
2502
2503 (defun gnus-newsgroup-directory-form (newsgroup)
2504   "Make hierarchical directory name from NEWSGROUP name."
2505   (let ((newsgroup (gnus-newsgroup-saveable-name newsgroup))
2506         (len (length newsgroup))
2507         idx)
2508     ;; If this is a foreign group, we don't want to translate the
2509     ;; entire name.  
2510     (if (setq idx (string-match ":" newsgroup))
2511         (aset newsgroup idx ?/)
2512       (setq idx 0))
2513     ;; Replace all occurrences of `.' with `/'.
2514     (while (< idx len)
2515       (if (= (aref newsgroup idx) ?.)
2516           (aset newsgroup idx ?/))
2517       (setq idx (1+ idx)))
2518     newsgroup))
2519
2520 (defun gnus-newsgroup-saveable-name (group)
2521   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
2522   ;; with dots.
2523   (gnus-replace-chars-in-string group ?/ ?.))
2524
2525 (defun gnus-make-directory (dir)
2526   "Make DIRECTORY recursively."
2527   ;; Why don't we use `(make-directory dir 'parents)'? That's just one
2528   ;; of the many mysteries of the universe.
2529   (let* ((dir (expand-file-name dir default-directory))
2530          dirs err)
2531     (if (string-match "/$" dir)
2532         (setq dir (substring dir 0 (match-beginning 0))))
2533     ;; First go down the path until we find a directory that exists.
2534     (while (not (file-exists-p dir))
2535       (setq dirs (cons dir dirs))
2536       (string-match "/[^/]+$" dir)
2537       (setq dir (substring dir 0 (match-beginning 0))))
2538     ;; Then create all the subdirs.
2539     (while (and dirs (not err))
2540       (condition-case ()
2541           (make-directory (car dirs))
2542         (error (setq err t)))
2543       (setq dirs (cdr dirs)))
2544     ;; We return whether we were successful or not. 
2545     (not dirs)))
2546
2547 (defun gnus-capitalize-newsgroup (newsgroup)
2548   "Capitalize NEWSGROUP name."
2549   (and (not (zerop (length newsgroup)))
2550        (concat (char-to-string (upcase (aref newsgroup 0)))
2551                (substring newsgroup 1))))
2552
2553 ;; Var
2554
2555 (defun gnus-simplify-subject (subject &optional re-only)
2556   "Remove `Re:' and words in parentheses.
2557 If optional argument RE-ONLY is non-nil, strip `Re:' only."
2558   (let ((case-fold-search t))           ;Ignore case.
2559     ;; Remove `Re:' and `Re^N:'.
2560     (if (string-match "^re:[ \t]*" subject)
2561         (setq subject (substring subject (match-end 0))))
2562     ;; Remove words in parentheses from end.
2563     (or re-only
2564         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2565           (setq subject (substring subject 0 (match-beginning 0)))))
2566     ;; Return subject string.
2567     subject))
2568
2569 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
2570 ;; all whitespace.
2571 (defun gnus-simplify-subject-fuzzy (subject)
2572   (let ((case-fold-search t))
2573     (save-excursion
2574       (gnus-set-work-buffer)
2575       (insert subject)
2576       (inline (gnus-simplify-buffer-fuzzy))
2577       (buffer-string))))
2578
2579 (defun gnus-simplify-buffer-fuzzy ()
2580   ;; Fix by Stainless Steel Rat <ratinox@ccs.neu.edu>.
2581   (goto-char (point-min))
2582   (while (or
2583           (looking-at "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*")
2584           (looking-at "^[[].*:[ \t].*[]]$"))
2585     (goto-char (point-min))
2586     (while (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*"
2587                               nil t)
2588       (replace-match "" t t))
2589     (goto-char (point-min))
2590     (while (re-search-forward "^[[].*:[ \t].*[]]$" nil t)
2591       (let ((beg (match-beginning 0))
2592             (end (match-end 0)))
2593         (goto-char end) (delete-char -1)
2594         (goto-char beg) (delete-region beg (re-search-forward ":" nil t))
2595         )))
2596   (goto-char (point-min))
2597   (while (re-search-forward "[ \t\n]*([^()]*)[ \t\n]*$" nil t)
2598     (replace-match "" t t))
2599   (goto-char (point-min))
2600   (while (re-search-forward "[ \t]+" nil t)
2601     (replace-match " " t t))
2602   (goto-char (point-min))
2603   (while (re-search-forward "[ \t]+$" nil t)
2604     (replace-match "" t t))
2605   (goto-char (point-min))
2606   (while (re-search-forward "^[ \t]+" nil t)
2607     (replace-match "" t t))
2608   (goto-char (point-min))
2609   (if gnus-simplify-subject-fuzzy-regexp
2610       (if (listp gnus-simplify-subject-fuzzy-regexp)
2611           (let ((list gnus-simplify-subject-fuzzy-regexp))
2612             (while list
2613               (goto-char (point-min))
2614               (while (re-search-forward (car list) nil t)
2615                 (replace-match "" t t))
2616               (setq list (cdr list))))
2617         (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
2618           (replace-match "" t t))
2619         )))
2620
2621 ;; Add the current buffer to the list of buffers to be killed on exit. 
2622 (defun gnus-add-current-to-buffer-list ()
2623   (or (memq (current-buffer) gnus-buffer-list)
2624       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
2625
2626 (defun gnus-string> (s1 s2)
2627   (not (or (string< s1 s2)
2628            (string= s1 s2))))
2629
2630 ;; Functions accessing headers.
2631 ;; Functions are more convenient than macros in some cases.
2632
2633 (defun gnus-header-number (header)
2634   (mail-header-number header))
2635
2636 (defun gnus-header-subject (header)
2637   (mail-header-subject header))
2638
2639 (defun gnus-header-from (header)
2640   (mail-header-from header))
2641
2642 (defun gnus-header-xref (header)
2643   (mail-header-xref header))
2644
2645 (defun gnus-header-lines (header)
2646   (mail-header-lines header))
2647
2648 (defun gnus-header-date (header)
2649   (mail-header-date header))
2650
2651 (defun gnus-header-id (header)
2652   (mail-header-id header))
2653
2654 (defun gnus-header-message-id (header)
2655   (mail-header-id header))
2656
2657 (defun gnus-header-chars (header)
2658   (mail-header-chars header))
2659
2660 (defun gnus-header-references (header)
2661   (mail-header-references header))
2662
2663 ;;; General various misc type functions.
2664
2665 (defun gnus-clear-system ()
2666   "Clear all variables and buffers."
2667   ;; Clear Gnus variables.
2668   (let ((variables gnus-variable-list))
2669     (while variables
2670       (set (car variables) nil)
2671       (setq variables (cdr variables))))
2672   ;; Clear other internal variables.
2673   (setq gnus-list-of-killed-groups nil
2674         gnus-have-read-active-file nil
2675         gnus-newsrc-alist nil
2676         gnus-newsrc-hashtb nil
2677         gnus-killed-list nil
2678         gnus-zombie-list nil
2679         gnus-killed-hashtb nil
2680         gnus-active-hashtb nil
2681         gnus-moderated-list nil
2682         gnus-description-hashtb nil
2683         gnus-newsgroup-headers nil
2684         gnus-newsgroup-name nil
2685         gnus-server-alist nil
2686         gnus-current-select-method nil)
2687   ;; Reset any score variables.
2688   (and gnus-use-scoring (gnus-score-close))
2689   ;; Kill the startup file.
2690   (and gnus-current-startup-file
2691        (get-file-buffer gnus-current-startup-file)
2692        (kill-buffer (get-file-buffer gnus-current-startup-file)))
2693   ;; Save any cache buffers.
2694   (and gnus-use-cache (gnus-cache-save-buffers))
2695   ;; Clear the dribble buffer.
2696   (gnus-dribble-clear)
2697   ;; Close down NoCeM.
2698   (and gnus-use-nocem (gnus-nocem-close))
2699   ;; Shut down the demons.
2700   (and gnus-use-demon (gnus-demon-cancel))
2701   ;; Kill global KILL file buffer.
2702   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
2703       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
2704   (gnus-kill-buffer nntp-server-buffer)
2705   ;; Kill Gnus buffers.
2706   (while gnus-buffer-list
2707     (gnus-kill-buffer (car gnus-buffer-list))
2708     (setq gnus-buffer-list (cdr gnus-buffer-list))))
2709
2710 (defun gnus-windows-old-to-new (setting)
2711   (if (symbolp setting)
2712       (setq setting 
2713             (cond ((eq setting 'SelectArticle)
2714                    'article)
2715                   ((eq setting 'SelectSubject)
2716                    'summary)
2717                   ((eq setting 'SelectNewsgroup)
2718                    'group)
2719                   (t setting))))
2720   (if (or (listp setting)
2721           (not (and gnus-window-configuration
2722                     (memq setting '(group summary article)))))
2723       setting
2724     (let* ((setting (if (eq setting 'group) 
2725                         (if (assq 'newsgroup gnus-window-configuration)
2726                             'newsgroup
2727                           'newsgroups) setting))
2728            (elem (car (cdr (assq setting gnus-window-configuration))))
2729            (total (apply '+ elem))
2730            (types '(group summary article))
2731            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
2732            (i 0)
2733            perc
2734            out)
2735       (while (< i 3)
2736         (or (zerop (nth i elem))
2737             (progn
2738               (setq perc  (/ (* 1.0 (nth 0 elem)) total))
2739               (setq out (cons (if (eq pbuf (nth i types))
2740                                   (vector (nth i types) perc 'point)
2741                                 (vector (nth i types) perc))
2742                               out))))
2743         (setq i (1+ i)))
2744       (list (nreverse out)))))
2745            
2746 (defun gnus-add-configuration (conf)
2747   (setq gnus-buffer-configuration 
2748         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
2749                          gnus-buffer-configuration))))
2750
2751 (defun gnus-configure-windows (setting &optional force)
2752   (setq setting (gnus-windows-old-to-new setting))
2753   (let ((r (if (symbolp setting)
2754                (cdr (assq setting gnus-buffer-configuration))
2755              setting))
2756         (in-buf (current-buffer))
2757         rule val w height hor ohor heights sub jump-buffer
2758         rel total to-buf all-visible)
2759     (or r (error "No such setting: %s" setting))
2760
2761     (if (and (not force) (setq all-visible (gnus-all-windows-visible-p r)))
2762         ;; All the windows mentioned are already visible, so we just
2763         ;; put point in the assigned buffer, and do not touch the
2764         ;; winconf. 
2765         (select-window (get-buffer-window all-visible t))
2766          
2767
2768       ;; Either remove all windows or just remove all Gnus windows.
2769       (if gnus-use-full-window
2770           (delete-other-windows)
2771         (gnus-remove-some-windows)
2772         (switch-to-buffer nntp-server-buffer))
2773
2774       (while r
2775         (setq hor (car r)
2776               ohor nil)
2777
2778         ;; We have to do the (possible) horizontal splitting before the
2779         ;; vertical. 
2780         (if (and (listp (car hor)) 
2781                  (eq (car (car hor)) 'horizontal))
2782             (progn
2783               (split-window 
2784                nil
2785                (if (integerp (nth 1 (car hor)))
2786                    (nth 1 (car hor))
2787                  (- (frame-width) (floor (* (frame-width) (nth 1 (car hor))))))
2788                t)
2789               (setq hor (cdr hor))))
2790
2791         ;; Go through the rules and eval the elements that are to be
2792         ;; evaled.  
2793         (while hor
2794           (if (setq val (if (vectorp (car hor)) (car hor) (eval (car hor))))
2795               (progn
2796                 ;; Expand short buffer name.
2797                 (setq w (aref val 0))
2798                 (and (setq w (cdr (assq w gnus-window-to-buffer)))
2799                      (progn
2800                        (setq val (apply 'vector (mapcar 'identity val)))
2801                        (aset val 0 w)))
2802                 (setq ohor (cons val ohor))))
2803           (setq hor (cdr hor)))
2804         (setq rule (cons (nreverse ohor) rule))
2805         (setq r (cdr r)))
2806       (setq rule (nreverse rule))
2807
2808       ;; We tally the window sizes.
2809       (setq total (window-height))
2810       (while rule
2811         (setq hor (car rule))
2812         (if (and (listp (car hor)) (eq (car (car hor)) 'horizontal))
2813             (setq hor (cdr hor)))
2814         (setq sub 0)
2815         (while hor
2816           (setq rel (aref (car hor) 1)
2817                 heights (cons
2818                          (cond ((and (floatp rel) (= 1.0 rel))
2819                                 'x)
2820                                ((integerp rel)
2821                                 rel)
2822                                (t
2823                                 (max (floor (* total rel)) 4)))
2824                          heights)
2825                 sub (+ sub (if (numberp (car heights)) (car heights) 0))
2826                 hor (cdr hor)))
2827         (setq heights (nreverse heights)
2828               hor (car rule))
2829
2830         ;; We then go through these heighs and create windows for them.
2831         (while heights
2832           (setq height (car heights)
2833                 heights (cdr heights))
2834           (and (eq height 'x)
2835                (setq height (- total sub)))
2836           (and heights
2837                (split-window nil height))
2838           (setq to-buf (aref (car hor) 0))
2839           (switch-to-buffer 
2840            (cond ((not to-buf)
2841                   in-buf)
2842                  ((symbolp to-buf)
2843                   (symbol-value (aref (car hor) 0)))
2844                  (t
2845                   (aref (car hor) 0))))
2846           (and (> (length (car hor)) 2)
2847                (eq (aref (car hor) 2) 'point)
2848                (setq jump-buffer (current-buffer)))
2849           (other-window 1)
2850           (setq hor (cdr hor)))
2851       
2852         (setq rule (cdr rule)))
2853
2854       ;; Finally, we pop to the buffer that's supposed to have point. 
2855       (or jump-buffer (error "Missing `point' in spec for %s" setting))
2856
2857       (select-window (get-buffer-window jump-buffer t))
2858       (set-buffer jump-buffer))))
2859
2860 (defun gnus-all-windows-visible-p (rule)
2861   (let (invisible hor jump-buffer val buffer)
2862     ;; Go through the rules and eval the elements that are to be
2863     ;; evaled.  
2864     (while (and rule (not invisible))
2865       (setq hor (car rule)
2866             rule (cdr rule))
2867       (while (and hor (not invisible))
2868         (if (setq val (if (vectorp (car hor)) 
2869                           (car hor)
2870                         (if (not (eq (car (car hor)) 'horizontal))
2871                             (eval (car hor)))))
2872             (progn
2873               ;; Expand short buffer name.
2874               (setq buffer (or (cdr (assq (aref val 0) gnus-window-to-buffer))
2875                                (aref val 0)))
2876               (setq buffer (if (symbolp buffer) (symbol-value buffer)
2877                              buffer))
2878               (and (> (length val) 2) (eq 'point (aref val 2))
2879                    (setq jump-buffer buffer))
2880               (setq invisible (not (and buffer (get-buffer-window buffer))))))
2881         (setq hor (cdr hor))))
2882     (and (not invisible) jump-buffer)))
2883
2884 (defun gnus-window-top-edge (&optional window)
2885   (nth 1 (window-edges window)))
2886
2887 (defun gnus-remove-some-windows ()
2888   (let ((buffers gnus-window-to-buffer)
2889         buf bufs lowest-buf lowest)
2890     (save-excursion
2891       ;; Remove windows on all known Gnus buffers.
2892       (while buffers
2893         (setq buf (cdr (car buffers)))
2894         (if (symbolp buf)
2895             (setq buf (and (boundp buf) (symbol-value buf))))
2896         (and buf 
2897              (get-buffer-window buf)
2898              (progn
2899                (setq bufs (cons buf bufs))
2900                (pop-to-buffer buf)
2901                (if (or (not lowest)
2902                        (< (gnus-window-top-edge) lowest))
2903                    (progn
2904                      (setq lowest (gnus-window-top-edge))
2905                      (setq lowest-buf buf)))))
2906         (setq buffers (cdr buffers)))
2907       ;; Remove windows on *all* summary buffers.
2908       (let (wins)
2909         (walk-windows
2910          (lambda (win)
2911            (let ((buf (window-buffer win)))
2912              (if (string-match  "^\\*Summary" (buffer-name buf))
2913                  (progn
2914                    (setq bufs (cons buf bufs))
2915                    (pop-to-buffer buf)
2916                    (if (or (not lowest)
2917                            (< (gnus-window-top-edge) lowest))
2918                        (progn
2919                          (setq lowest-buf buf)
2920                          (setq lowest (gnus-window-top-edge))))))))))
2921       (and lowest-buf 
2922            (progn
2923              (pop-to-buffer lowest-buf)
2924              (switch-to-buffer nntp-server-buffer)))
2925       (while bufs
2926         (and (not (eq (car bufs) lowest-buf))
2927              (delete-windows-on (car bufs)))
2928         (setq bufs (cdr bufs))))))
2929                           
2930 (defun gnus-version ()
2931   "Version numbers of this version of Gnus."
2932   (interactive)
2933   (let ((methods gnus-valid-select-methods)
2934         (mess gnus-version)
2935         meth)
2936     ;; Go through all the legal select methods and add their version
2937     ;; numbers to the total version string. Only the backends that are
2938     ;; currently in use will have their message numbers taken into
2939     ;; consideration. 
2940     (while methods
2941       (setq meth (intern (concat (car (car methods)) "-version")))
2942       (and (boundp meth)
2943            (stringp (symbol-value meth))
2944            (setq mess (concat mess "; " (symbol-value meth))))
2945       (setq methods (cdr methods)))
2946     (gnus-message 2 mess)))
2947
2948 (defun gnus-info-find-node ()
2949   "Find Info documentation of Gnus."
2950   (interactive)
2951   ;; Enlarge info window if needed.
2952   (let ((mode major-mode))
2953     (gnus-configure-windows 'info)
2954     (Info-goto-node (car (cdr (assq mode gnus-info-nodes))))))
2955
2956 (defun gnus-replace-chars-in-string (string &rest pairs)
2957   "Replace characters in STRING from FROM to TO."
2958   (let ((string (substring string 0))   ;Copy string.
2959         (len (length string))
2960         (idx 0)
2961         sym to)
2962     (or (zerop (% (length pairs) 2)) 
2963         (error "Odd number of translation pairs"))
2964     (setplist 'sym pairs)
2965     ;; Replace all occurrences of FROM with TO.
2966     (while (< idx len)
2967       (if (setq to (get 'sym (aref string idx)))
2968           (aset string idx to))
2969       (setq idx (1+ idx)))
2970     string))
2971
2972 (defun gnus-days-between (date1 date2)
2973   ;; Return the number of days between date1 and date2.
2974   (- (gnus-day-number date1) (gnus-day-number date2)))
2975
2976 (defun gnus-day-number (date)
2977   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
2978                      (timezone-parse-date date))))
2979     (timezone-absolute-from-gregorian 
2980      (nth 1 dat) (nth 2 dat) (car dat))))
2981
2982 ;; Returns a floating point number that says how many seconds have
2983 ;; lapsed between Jan 1 12:00:00 1970 and DATE.
2984 (defun gnus-seconds-since-epoch (date)
2985   (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2986                         (timezone-parse-date date)))
2987          (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
2988                         (timezone-parse-time
2989                          (aref (timezone-parse-date date) 3))))
2990          (edate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2991                         (timezone-parse-date "Jan 1 12:00:00 1970")))
2992          (tday (- (timezone-absolute-from-gregorian 
2993                    (nth 1 tdate) (nth 2 tdate) (nth 0 tdate))
2994                   (timezone-absolute-from-gregorian 
2995                    (nth 1 edate) (nth 2 edate) (nth 0 edate)))))
2996     (+ (nth 2 ttime)
2997        (* (nth 1 ttime) 60)
2998        (* 1.0 (nth 0 ttime) 60 60)
2999        (* 1.0 tday 60 60 24))))
3000
3001 (defun gnus-file-newer-than (file date)
3002   (let ((fdate (nth 5 (file-attributes file))))
3003     (or (> (car fdate) (car date))
3004         (and (= (car fdate) (car date))
3005              (> (nth 1 fdate) (nth 1 date))))))
3006
3007 (defun gnus-group-read-only-p (&optional group)
3008   "Check whether GROUP supports editing or not.
3009 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3010 that that variable is buffer-local to the summary buffers."
3011   (let ((group (or group gnus-newsgroup-name)))
3012     (not (gnus-check-backend-function 'request-replace-article group))))
3013
3014 (defun gnus-group-total-expirable-p (group)
3015   "Check whether GROUP is total-expirable or not."
3016   (let ((params (nth 5 (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))))
3017     (or (memq 'total-expire params) 
3018         (cdr (assq 'total-expire params)) ; (total-expire . t)
3019         (and gnus-total-expirable-newsgroups ; Check var.
3020              (string-match gnus-total-expirable-newsgroups group)))))
3021
3022 (defun gnus-group-auto-expirable-p (group)
3023   "Check whether GROUP is total-expirable or not."
3024   (let ((params (nth 5 (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))))
3025     (or (memq 'auto-expire params) 
3026         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3027         (and gnus-auto-expirable-newsgroups ; Check var.
3028              (string-match gnus-auto-expirable-newsgroups group)))))
3029
3030 (defun gnus-subject-equal (s1 s2)
3031   "Check whether two subjects are equal."
3032   (cond
3033    ((null gnus-summary-gather-subject-limit)
3034     (equal (gnus-simplify-subject-re s1)
3035            (gnus-simplify-subject-re s2)))
3036    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3037     (equal (gnus-simplify-subject-fuzzy s1)
3038            (gnus-simplify-subject-fuzzy s2)))
3039    ((numberp gnus-summary-gather-subject-limit)
3040     (equal (gnus-limit-string s1 gnus-summary-gather-subject-limit)
3041            (gnus-limit-string s2 gnus-summary-gather-subject-limit)))
3042    (t
3043     (equal s1 s2))))
3044
3045 ;; Returns a list of writable groups.
3046 (defun gnus-writable-groups ()
3047   (let ((alist gnus-newsrc-alist)
3048         groups)
3049     (while alist
3050       (or (gnus-group-read-only-p (car (car alist)))
3051           (setq groups (cons (car (car alist)) groups)))
3052       (setq alist (cdr alist)))
3053     (nreverse groups)))
3054
3055 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3056 ;; the echo area.
3057 (defun gnus-y-or-n-p (prompt)
3058   (prog1
3059       (y-or-n-p prompt)
3060     (message "")))
3061
3062 (defun gnus-yes-or-no-p (prompt)
3063   (prog1
3064       (yes-or-no-p prompt)
3065     (message "")))
3066
3067 ;; Check whether to use long file names.
3068 (defun gnus-use-long-file-name (symbol)
3069   ;; The variable has to be set...
3070   (and gnus-use-long-file-name
3071        ;; If it isn't a list, then we return t.
3072        (or (not (listp gnus-use-long-file-name))
3073            ;; If it is a list, and the list contains `symbol', we
3074            ;; return nil.  
3075            (not (memq symbol gnus-use-long-file-name)))))
3076
3077 ;; I suspect there's a better way, but I haven't taken the time to do
3078 ;; it yet. -erik selberg@cs.washington.edu
3079 (defun gnus-dd-mmm (messy-date)
3080   "Return a string like DD-MMM from a big messy string"
3081   (let ((datevec (timezone-parse-date messy-date)))
3082     (format "%2s-%s"
3083             (or (aref datevec 2) "??")
3084             (capitalize
3085              (or (car 
3086                   (nth (1- (string-to-number (aref datevec 1)))
3087                        timezone-months-assoc))
3088                  "???")))))
3089
3090 ;; Make a hash table (default and minimum size is 255).
3091 ;; Optional argument HASHSIZE specifies the table size.
3092 (defun gnus-make-hashtable (&optional hashsize)
3093   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3094
3095 ;; Make a number that is suitable for hashing; bigger than MIN and one
3096 ;; less than 2^x.
3097 (defun gnus-create-hash-size (min)
3098   (let ((i 1))
3099     (while (< i min)
3100       (setq i (* 2 i)))
3101     (1- i)))
3102
3103 ;; Show message if message has a lower level than `gnus-verbose'. 
3104 ;; Guide-line for numbers:
3105 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3106 ;; for things that take a long time, 7 - not very important messages
3107 ;; on stuff, 9 - messages inside loops.
3108 (defun gnus-message (level &rest args)
3109   (if (<= level gnus-verbose)
3110       (apply 'message args)
3111     ;; We have to do this format thingie here even if the result isn't
3112     ;; shown - the return value has to be the same as the return value
3113     ;; from `message'.
3114     (apply 'format args)))
3115
3116 ;; Generate a unique new group name.
3117 (defun gnus-generate-new-group-name (leaf)
3118   (let ((name leaf)
3119         (num 0))
3120     (while (gnus-gethash name gnus-newsrc-hashtb)
3121       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3122     name))
3123
3124 ;; Find out whether the gnus-visual TYPE is wanted.
3125 (defun gnus-visual-p (&optional type class)
3126   (and gnus-visual                      ; Has to be non-nil, at least.
3127        (if (not type)                   ; We don't care about type.
3128            gnus-visual
3129          (if (listp gnus-visual)        ; It's a list, so we check it.
3130              (or (memq type gnus-visual)
3131                  (memq class gnus-visual))
3132            t))))
3133
3134 (defun gnus-parent-id (references)
3135   "Return the last Message-ID in REFERENCES."
3136   (and references
3137        (string-match "\\(<[^<>]+>\\) *$" references)
3138        (substring references (match-beginning 1) (match-end 1))))
3139
3140 ;;; List and range functions
3141
3142 (defun gnus-last-element (list)
3143   "Return last element of LIST."
3144   (while (cdr list)
3145     (setq list (cdr list)))
3146   (car list))
3147
3148 (defun gnus-copy-sequence (list)
3149   "Do a complete, total copy of a list."
3150   (if (and (consp list) (not (consp (cdr list))))
3151       (cons (car list) (cdr list))
3152     (mapcar (lambda (elem) (if (consp elem) 
3153                                (if (consp (cdr elem))
3154                                    (gnus-copy-sequence elem)
3155                                  (cons (car elem) (cdr elem)))
3156                              elem))
3157             list)))
3158
3159 (defun gnus-set-difference (list1 list2)
3160   "Return a list of elements of LIST1 that do not appear in LIST2."
3161   (let ((list1 (copy-sequence list1)))
3162     (while list2
3163       (setq list1 (delq (car list2) list1))
3164       (setq list2 (cdr list2)))
3165     list1))
3166
3167 (defun gnus-sorted-complement (list1 list2)
3168   "Return a list of elements of LIST1 that do not appear in LIST2.
3169 Both lists have to be sorted over <."
3170   (let (out)
3171     (if (or (null list1) (null list2))
3172         (or list1 list2)
3173       (while (and list1 list2)
3174         (cond ((= (car list1) (car list2))
3175                (setq list1 (cdr list1)
3176                      list2 (cdr list2)))
3177               ((< (car list1) (car list2))
3178                (setq out (cons (car list1) out))
3179                (setq list1 (cdr list1)))
3180               (t
3181                (setq out (cons (car list2) out))
3182                (setq list2 (cdr list2)))))
3183       (nconc (nreverse out) (or list1 list2)))))
3184
3185 (defun gnus-intersection (list1 list2)      
3186   (let ((result nil))
3187     (while list2
3188       (if (memq (car list2) list1)
3189           (setq result (cons (car list2) result)))
3190       (setq list2 (cdr list2)))
3191     result))
3192
3193 (defun gnus-sorted-intersection (list1 list2)
3194   ;; LIST1 and LIST2 have to be sorted over <.
3195   (let (out)
3196     (while (and list1 list2)
3197       (cond ((= (car list1) (car list2))
3198              (setq out (cons (car list1) out)
3199                    list1 (cdr list1)
3200                    list2 (cdr list2)))
3201             ((< (car list1) (car list2))
3202              (setq list1 (cdr list1)))
3203             (t
3204              (setq list2 (cdr list2)))))
3205     (nreverse out)))
3206
3207 (defun gnus-set-sorted-intersection (list1 list2)
3208   ;; LIST1 and LIST2 have to be sorted over <.
3209   ;; This function modifies LIST1.
3210   (let* ((top (cons nil list1))
3211          (prev top))
3212     (while (and list1 list2)
3213       (cond ((= (car list1) (car list2))
3214              (setq prev list1
3215                    list1 (cdr list1)
3216                    list2 (cdr list2)))
3217             ((< (car list1) (car list2))
3218              (setcdr prev (cdr list1))
3219              (setq list1 (cdr list1)))
3220             (t
3221              (setq list2 (cdr list2)))))
3222     (setcdr prev nil)
3223     (cdr top)))
3224
3225 (defun gnus-compress-sequence (numbers &optional always-list)
3226   "Convert list of numbers to a list of ranges or a single range.
3227 If ALWAYS-LIST is non-nil, this function will always release a list of
3228 ranges."
3229   (let* ((first (car numbers))
3230          (last (car numbers))
3231          result)
3232     (if (null numbers)
3233         nil
3234       (if (not (listp (cdr numbers)))
3235           numbers
3236         (while numbers
3237           (cond ((= last (car numbers)) nil) ;Omit duplicated number
3238                 ((= (1+ last) (car numbers)) ;Still in sequence
3239                  (setq last (car numbers)))
3240                 (t                      ;End of one sequence
3241                  (setq result 
3242                        (cons (if (= first last) first
3243                                (cons first last)) result))
3244                  (setq first (car numbers))
3245                  (setq last  (car numbers))))
3246           (setq numbers (cdr numbers)))
3247         (if (and (not always-list) (null result))
3248             (if (= first last) (list first) (cons first last))
3249           (nreverse (cons (if (= first last) first (cons first last))
3250                           result)))))))
3251
3252 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
3253 (defun gnus-uncompress-range (ranges)
3254   "Expand a list of ranges into a list of numbers.
3255 RANGES is either a single range on the form `(num . num)' or a list of
3256 these ranges."
3257   (let (first last result)
3258     (cond 
3259      ((null ranges)
3260       nil)
3261      ((not (listp (cdr ranges)))
3262       (setq first (car ranges))
3263       (setq last (cdr ranges))
3264       (while (<= first last)
3265         (setq result (cons first result))
3266         (setq first (1+ first)))
3267       (nreverse result))
3268      (t
3269       (while ranges
3270         (if (atom (car ranges))
3271             (if (numberp (car ranges))
3272                 (setq result (cons (car ranges) result)))
3273           (setq first (car (car ranges)))
3274           (setq last  (cdr (car ranges)))
3275           (while (<= first last)
3276             (setq result (cons first result))
3277             (setq first (1+ first))))
3278         (setq ranges (cdr ranges)))
3279       (nreverse result)))))
3280
3281 (defun gnus-add-to-range (ranges list)
3282   "Return a list of ranges that has all articles from both RANGES and LIST.
3283 Note: LIST has to be sorted over `<'."
3284   (if (not ranges)
3285       (gnus-compress-sequence list t)
3286     (setq list (copy-sequence list))
3287     (or (listp (cdr ranges))
3288         (setq ranges (list ranges)))
3289     (let ((out ranges)
3290           ilist lowest highest temp)
3291       (while (and ranges list)
3292         (setq ilist list)
3293         (setq lowest (or (and (atom (car ranges)) (car ranges))
3294                          (car (car ranges))))
3295         (while (and list (cdr list) (< (car (cdr list)) lowest))
3296           (setq list (cdr list)))
3297         (if (< (car ilist) lowest)
3298             (progn
3299               (setq temp list)
3300               (setq list (cdr list))
3301               (setcdr temp nil)
3302               (setq out (nconc (gnus-compress-sequence ilist t) out))))
3303         (setq highest (or (and (atom (car ranges)) (car ranges))
3304                           (cdr (car ranges))))
3305         (while (and list (<= (car list) highest))
3306           (setq list (cdr list)))
3307         (setq ranges (cdr ranges)))
3308       (if list
3309           (setq out (nconc (gnus-compress-sequence list t) out)))
3310       (setq out (sort out (lambda (r1 r2) 
3311                             (< (or (and (atom r1) r1) (car r1))
3312                                (or (and (atom r2) r2) (car r2))))))
3313       (setq ranges out)
3314       (while ranges
3315         (if (atom (car ranges))
3316             (if (cdr ranges)
3317                 (if (atom (car (cdr ranges)))
3318                     (if (= (1+ (car ranges)) (car (cdr ranges)))
3319                         (progn
3320                           (setcar ranges (cons (car ranges) 
3321                                                (car (cdr ranges))))
3322                           (setcdr ranges (cdr (cdr ranges)))))
3323                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
3324                       (progn
3325                         (setcar (car (cdr ranges)) (car ranges))
3326                         (setcar ranges (car (cdr ranges)))
3327                         (setcdr ranges (cdr (cdr ranges)))))))
3328           (if (cdr ranges)
3329               (if (atom (car (cdr ranges)))
3330                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
3331                       (progn
3332                         (setcdr (car ranges) (car (cdr ranges)))
3333                         (setcdr ranges (cdr (cdr ranges)))))
3334                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
3335                     (progn
3336                       (setcdr (car ranges) (cdr (car (cdr ranges))))
3337                       (setcdr ranges (cdr (cdr ranges))))))))
3338         (setq ranges (cdr ranges)))
3339       out)))
3340
3341 (defun gnus-remove-from-range (ranges list)
3342   "Return a list of ranges that has all articles from LIST removed from RANGES.
3343 Note: LIST has to be sorted over `<'."
3344   ;; !!! This function shouldn't look like this, but I've got a headache.
3345   (gnus-compress-sequence 
3346    (gnus-sorted-complement
3347     (gnus-uncompress-range ranges) list)))
3348
3349 (defun gnus-member-of-range (number ranges)
3350   (if (not (listp (cdr ranges)))
3351       (and (>= number (car ranges)) 
3352            (<= number (cdr ranges)))
3353     (let ((not-stop t))
3354       (while (and ranges 
3355                   (if (numberp (car ranges))
3356                       (>= number (car ranges))
3357                     (>= number (car (car ranges))))
3358                   not-stop)
3359         (if (if (numberp (car ranges))
3360                 (= number (car ranges))
3361               (and (>= number (car (car ranges)))
3362                    (<= number (cdr (car ranges)))))
3363             (setq not-stop nil))
3364         (setq ranges (cdr ranges)))
3365       (not not-stop))))
3366
3367 \f
3368 ;;;
3369 ;;; Gnus group mode
3370 ;;;
3371
3372 (defvar gnus-group-mode-map nil)
3373 (defvar gnus-group-group-map nil)
3374 (defvar gnus-group-mark-map nil)
3375 (defvar gnus-group-list-map nil)
3376 (defvar gnus-group-sub-map nil)
3377 (defvar gnus-group-score-map nil)
3378 (put 'gnus-group-mode 'mode-class 'special)
3379
3380 (if gnus-group-mode-map
3381     nil
3382   (setq gnus-group-mode-map (make-keymap))
3383   (suppress-keymap gnus-group-mode-map)
3384   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
3385   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
3386   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
3387   (define-key gnus-group-mode-map "\M-\r" 'gnus-group-quick-select-group)
3388   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
3389   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
3390   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
3391   (define-key gnus-group-mode-map "\177" 'gnus-group-prev-unread-group)
3392   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
3393   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
3394   (define-key gnus-group-mode-map
3395     "\M-n" 'gnus-group-next-unread-group-same-level)
3396   (define-key gnus-group-mode-map 
3397     "\M-p" 'gnus-group-prev-unread-group-same-level)
3398   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
3399   (define-key gnus-group-mode-map "." 'gnus-group-first-unread-group)
3400   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
3401   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
3402   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
3403   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
3404   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
3405   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
3406   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
3407   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
3408   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
3409   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
3410   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
3411   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
3412   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
3413   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
3414   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
3415   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
3416   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
3417   (define-key gnus-group-mode-map "\C-c\M-\C-a" 'gnus-group-description-apropos)
3418   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
3419   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
3420   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
3421   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
3422   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
3423   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
3424   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
3425   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
3426   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
3427   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
3428   (define-key gnus-group-mode-map "V" 'gnus-version)
3429   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
3430   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
3431   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
3432   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
3433   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
3434   (define-key gnus-group-mode-map "\M-f" 'gnus-group-fetch-faq)
3435   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
3436   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
3437   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group-method)
3438   (define-key gnus-group-mode-map "^" 'gnus-group-enter-server-mode)
3439   (define-key gnus-group-mode-map gnus-mouse-2 'gnus-mouse-pick-group)
3440   (define-key gnus-group-mode-map "<" 'beginning-of-buffer)
3441   (define-key gnus-group-mode-map ">" 'end-of-buffer)
3442   (define-key gnus-group-mode-map "\C-c\C-b" 'gnus-bug)
3443   (define-key gnus-group-mode-map "\C-c\C-s" 'gnus-group-sort-groups)
3444
3445   (define-key gnus-group-mode-map "#" 'gnus-group-mark-group)
3446   (define-key gnus-group-mode-map "\M-#" 'gnus-group-unmark-group)
3447   (define-prefix-command 'gnus-group-mark-map)
3448   (define-key gnus-group-mode-map "M" 'gnus-group-mark-map)
3449   (define-key gnus-group-mark-map "m" 'gnus-group-mark-group)
3450   (define-key gnus-group-mark-map "u" 'gnus-group-unmark-group)
3451   (define-key gnus-group-mark-map "w" 'gnus-group-mark-region)
3452
3453   (define-prefix-command 'gnus-group-group-map)
3454   (define-key gnus-group-mode-map "G" 'gnus-group-group-map)
3455   (define-key gnus-group-group-map "d" 'gnus-group-make-directory-group)
3456   (define-key gnus-group-group-map "h" 'gnus-group-make-help-group)
3457   (define-key gnus-group-group-map "a" 'gnus-group-make-archive-group)
3458   (define-key gnus-group-group-map "k" 'gnus-group-make-kiboze-group)
3459   (define-key gnus-group-group-map "m" 'gnus-group-make-group)
3460   (define-key gnus-group-group-map "E" 'gnus-group-edit-group)
3461   (define-key gnus-group-group-map "e" 'gnus-group-edit-group-method)
3462   (define-key gnus-group-group-map "p" 'gnus-group-edit-group-parameters)
3463   (define-key gnus-group-group-map "v" 'gnus-group-add-to-virtual)
3464   (define-key gnus-group-group-map "V" 'gnus-group-make-empty-virtual)
3465   (define-key gnus-group-group-map "D" 'gnus-group-enter-directory)
3466   (define-key gnus-group-group-map "f" 'gnus-group-make-doc-group)
3467   (define-key gnus-group-group-map "r" 'gnus-group-rename-group)
3468   (define-key gnus-group-group-map "\177" 'gnus-group-delete-group)
3469   (define-key gnus-group-group-map "sb" 'gnus-group-brew-soup)
3470   (define-key gnus-group-group-map "sw" 'gnus-soup-save-areas)
3471   (define-key gnus-group-group-map "ss" 'gnus-soup-send-replies)
3472   (define-key gnus-group-group-map "sp" 'gnus-soup-pack-packet)
3473   (define-key gnus-group-group-map "sr" 'nnsoup-pack-replies)
3474
3475   (define-prefix-command 'gnus-group-list-map)
3476   (define-key gnus-group-mode-map "A" 'gnus-group-list-map)
3477   (define-key gnus-group-list-map "k" 'gnus-group-list-killed)
3478   (define-key gnus-group-list-map "z" 'gnus-group-list-zombies)
3479   (define-key gnus-group-list-map "s" 'gnus-group-list-groups)
3480   (define-key gnus-group-list-map "u" 'gnus-group-list-all-groups)
3481   (define-key gnus-group-list-map "A" 'gnus-group-list-active)
3482   (define-key gnus-group-list-map "a" 'gnus-group-apropos)
3483   (define-key gnus-group-list-map "d" 'gnus-group-description-apropos)
3484   (define-key gnus-group-list-map "m" 'gnus-group-list-matching)
3485   (define-key gnus-group-list-map "M" 'gnus-group-list-all-matching)
3486
3487   (define-prefix-command 'gnus-group-score-map)
3488   (define-key gnus-group-mode-map "W" 'gnus-group-score-map)
3489   (define-key gnus-group-score-map "f" 'gnus-score-flush-cache)
3490
3491   (define-prefix-command 'gnus-group-sub-map)
3492   (define-key gnus-group-mode-map "S" 'gnus-group-sub-map)
3493   (define-key gnus-group-sub-map "l" 'gnus-group-set-current-level)
3494   (define-key gnus-group-sub-map "t" 'gnus-group-unsubscribe-current-group)
3495   (define-key gnus-group-sub-map "s" 'gnus-group-unsubscribe-group)
3496   (define-key gnus-group-sub-map "k" 'gnus-group-kill-group)
3497   (define-key gnus-group-sub-map "y" 'gnus-group-yank-group)
3498   (define-key gnus-group-sub-map "w" 'gnus-group-kill-region)
3499   (define-key gnus-group-sub-map "z" 'gnus-group-kill-all-zombies))
3500
3501 (defun gnus-group-mode ()
3502   "Major mode for reading news.
3503
3504 All normal editing commands are switched off.
3505 \\<gnus-group-mode-map>
3506 The group buffer lists (some of) the groups available.  For instance,
3507 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
3508 lists all zombie groups. 
3509
3510 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe 
3511 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'. 
3512
3513 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
3514
3515 The following commands are available:
3516
3517 \\{gnus-group-mode-map}"
3518   (interactive)
3519   (if (gnus-visual-p 'group-menu 'menu) (gnus-group-make-menu-bar))
3520   (kill-all-local-variables)
3521   (setq mode-line-modified "-- ")
3522   (make-local-variable 'mode-line-format)
3523   (setq mode-line-format (copy-sequence mode-line-format))
3524   (and (equal (nth 3 mode-line-format) "   ")
3525        (setcar (nthcdr 3 mode-line-format) ""))
3526   (setq major-mode 'gnus-group-mode)
3527   (setq mode-name "Group")
3528   (gnus-group-set-mode-line)
3529   (setq mode-line-process nil)
3530   (use-local-map gnus-group-mode-map)
3531   (buffer-disable-undo (current-buffer))
3532   (setq truncate-lines t)
3533   (setq buffer-read-only t)
3534   (run-hooks 'gnus-group-mode-hook))
3535
3536 (defun gnus-mouse-pick-group (e)
3537   (interactive "e")
3538   (mouse-set-point e)
3539   (gnus-group-read-group nil))
3540
3541 ;; Look at LEVEL and find out what the level is really supposed to be.
3542 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
3543 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
3544 (defun gnus-group-default-level (&optional level number-or-nil)
3545   (cond  
3546    (gnus-group-use-permanent-levels
3547     (setq gnus-group-default-list-level 
3548           (or level gnus-group-default-list-level))
3549     (or gnus-group-default-list-level gnus-level-subscribed))
3550    (number-or-nil
3551     level)
3552    (t
3553     (or level gnus-group-default-list-level gnus-level-subscribed))))
3554   
3555
3556 (defvar gnus-tmp-prev-perm nil)
3557
3558 ;;;###autoload
3559 (defun gnus-no-server (&optional arg)
3560   "Read network news.
3561 If ARG is a positive number, Gnus will use that as the
3562 startup level. If ARG is nil, Gnus will be started at level 2. 
3563 If ARG is non-nil and not a positive number, Gnus will
3564 prompt the user for the name of an NNTP server to use.
3565 As opposed to `gnus', this command will not connect to the local server."
3566   (interactive "P")
3567   (let ((perm
3568          (cons gnus-group-use-permanent-levels gnus-group-default-list-level)))
3569     (setq gnus-tmp-prev-perm nil)
3570     (setq gnus-group-use-permanent-levels t)
3571     (gnus (or arg (1- gnus-level-default-subscribed)) t)
3572     (setq gnus-tmp-prev-perm perm)))
3573
3574 ;;;###autoload
3575 (defun gnus-slave (&optional arg)
3576   "Read news as a slave."
3577   (interactive "P")
3578   (gnus arg nil 'slave))
3579
3580 ;;;###autoload
3581 (defun gnus (&optional arg dont-connect slave)
3582   "Read network news.
3583 If ARG is non-nil and a positive number, Gnus will use that as the
3584 startup level. If ARG is non-nil and not a positive number, Gnus will
3585 prompt the user for the name of an NNTP server to use."
3586   (interactive "P")
3587   (if (get-buffer gnus-group-buffer)
3588       (progn
3589         (switch-to-buffer gnus-group-buffer)
3590         (gnus-group-get-new-news))
3591
3592     (gnus-clear-system)
3593
3594     (nnheader-init-server-buffer)
3595     ;; We do this if `gnus-no-server' has been run.
3596     (if gnus-tmp-prev-perm 
3597         (setq gnus-group-use-permanent-levels (car gnus-tmp-prev-perm)
3598               gnus-group-default-list-level (cdr gnus-tmp-prev-perm)
3599               gnus-tmp-prev-perm nil))
3600     (gnus-read-init-file)
3601
3602     (setq gnus-slave slave)
3603
3604     (gnus-group-setup-buffer)
3605     (let ((buffer-read-only nil))
3606       (erase-buffer)
3607       (if (not gnus-inhibit-startup-message)
3608           (progn
3609             (gnus-group-startup-message)
3610             (sit-for 0))))
3611     
3612     (let ((level (and arg (numberp arg) (> arg 0) arg))
3613           did-connect)
3614       (unwind-protect
3615           (progn
3616             (or dont-connect 
3617                 (setq did-connect
3618                       (gnus-start-news-server (and arg (not level))))))
3619         (if (and (not dont-connect) 
3620                  (not did-connect))
3621             (gnus-group-quit)
3622           (run-hooks 'gnus-startup-hook)
3623           ;; NNTP server is successfully open. 
3624
3625           ;; Find the current startup file name.
3626           (setq gnus-current-startup-file 
3627                 (gnus-make-newsrc-file gnus-startup-file))
3628
3629           ;; Read the dribble file.
3630           (and (or gnus-slave gnus-use-dribble-file) (gnus-dribble-read-file))
3631
3632           (gnus-summary-make-display-table)
3633           (gnus-setup-news nil level)
3634           (gnus-group-list-groups level)
3635           (gnus-configure-windows 'group))))))
3636
3637 (defun gnus-unload ()
3638   "Unload all Gnus features."
3639   (interactive)
3640   (or (boundp 'load-history)
3641       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
3642   (let ((history load-history)
3643         feature)
3644     (while history
3645       (and (string-match "^gnus" (car (car history)))
3646            (setq feature (cdr (assq 'provide (car history))))
3647            (unload-feature feature 'force))
3648       (setq history (cdr history)))))
3649
3650 (defun gnus-compile ()
3651   "Byte-compile the Gnus startup file.
3652 This will also compile the user-defined format specs."
3653   (interactive)
3654   (let ((file (make-temp-name "/tmp/gnuss")))
3655     (save-excursion
3656       (gnus-message 7 "Compiling user file...")
3657       (nnheader-set-temp-buffer " *compile gnus*")
3658       (and (file-exists-p gnus-init-file)
3659            (insert-file gnus-init-file))
3660       (goto-char (point-max))
3661
3662       (let ((formats '(summary summary-dummy group 
3663                                summary-mode group-mode article-mode))
3664             format fs)
3665         
3666         (while formats
3667           (setq format (symbol-name (car formats))
3668                 formats (cdr formats)
3669                 fs (cons (symbol-value 
3670                           (intern (format "gnus-%s-line-format" format)))
3671                          fs))
3672           (insert "(defun gnus-" format "-line-format-spec ()\n")
3673           (insert 
3674            (prin1-to-string
3675             (symbol-value 
3676              (intern (format "gnus-%s-line-format-spec" format)))))
3677           (insert ")\n")
3678           (insert "(setq gnus-" format 
3679                   "-line-format-spec (list 'gnus-byte-code 'gnus-"
3680                   format "-line-format-spec))\n"))
3681
3682         (insert "(setq gnus-old-specs '" (prin1-to-string fs) "\n")
3683
3684         (write-region (point-min) (point-max) file nil 'silent)
3685         (byte-compile-file file)
3686         (rename-file
3687          (concat file ".elc") 
3688          (concat gnus-init-file 
3689                  (if (string-match "\\.el$" gnus-init-file) "c" ".elc"))
3690          t)
3691         (delete-file file)
3692         (kill-buffer (current-buffer)))
3693       (gnus-message 7 "Compiling user file...done"))))
3694
3695 (defun gnus-indent-rigidly (start end arg)
3696   "Indent rigidly using only spaces and no tabs."
3697   (save-excursion
3698     (save-restriction
3699       (narrow-to-region start end)
3700       (indent-rigidly start end arg)
3701       (goto-char (point-min))
3702       (while (search-forward "\t" nil t)
3703         (replace-match "        " t t)))))
3704
3705 (defun gnus-group-startup-message (&optional x y)
3706   "Insert startup message in current buffer."
3707   ;; Insert the message.
3708   (erase-buffer)
3709   (insert
3710    (format "              %s
3711           _    ___ _             _      
3712           _ ___ __ ___  __    _ ___     
3713           __   _     ___    __  ___     
3714               _           ___     _     
3715              _  _ __             _      
3716              ___   __            _      
3717                    __           _       
3718                     _      _   _        
3719                    _      _    _        
3720                       _  _    _         
3721                   __  ___               
3722                  _   _ _     _          
3723                 _   _                   
3724               _    _                    
3725              _    _                     
3726             _                         
3727           __                             
3728
3729
3730            gnus-version))
3731   ;; And then hack it.
3732   (gnus-indent-rigidly (point-min) (point-max) 
3733                        (/ (max (- (window-width) (or x 46)) 0) 2))
3734   (goto-char (point-min))
3735   (forward-line 1)
3736   (let* ((pheight (count-lines (point-min) (point-max)))
3737          (wheight (window-height))
3738          (rest (- wheight  pheight)))
3739     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
3740   ;; Fontify some.
3741   (goto-char (point-min))
3742   (and (search-forward "Praxis" nil t)
3743        (put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
3744   (goto-char (point-min)))
3745
3746 (defun gnus-group-startup-message-old (&optional x y)
3747   "Insert startup message in current buffer."
3748   ;; Insert the message.
3749   (erase-buffer)
3750   (insert
3751    (format "
3752      %s
3753            A newsreader 
3754       for GNU Emacs
3755
3756         Based on GNUS 
3757              written by 
3758      Masanobu UMEDA
3759
3760        A Praxis Release
3761       larsi@ifi.uio.no
3762
3763            gnus-version))
3764   ;; And then hack it.
3765   ;; 18 is the longest line.
3766   (indent-rigidly (point-min) (point-max) 
3767                   (/ (max (- (window-width) (or x 28)) 0) 2))
3768   (goto-char (point-min))
3769   ;; +4 is fuzzy factor.
3770   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2))
3771
3772   ;; Fontify some.
3773   (goto-char (point-min))
3774   (search-forward "Praxis")
3775   (put-text-property (match-beginning 0) (match-end 0) 'face 'bold)
3776   (goto-char (point-min)))
3777
3778 (defun gnus-group-setup-buffer ()
3779   (or (get-buffer gnus-group-buffer)
3780       (progn
3781         (switch-to-buffer gnus-group-buffer)
3782         (gnus-add-current-to-buffer-list)
3783         (gnus-group-mode)
3784         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
3785
3786 (defun gnus-group-list-groups (&optional level unread)
3787   "List newsgroups with level LEVEL or lower that have unread articles.
3788 Default is all subscribed groups.
3789 If argument UNREAD is non-nil, groups with no unread articles are also
3790 listed." 
3791   (interactive (list (if current-prefix-arg
3792                          (prefix-numeric-value current-prefix-arg)
3793                        (or
3794                         (gnus-group-default-level nil t)
3795                         gnus-group-default-list-level
3796                         gnus-level-subscribed))))
3797   (or level
3798       (setq level (car gnus-group-list-mode)
3799             unread (cdr gnus-group-list-mode)))
3800   (setq level (gnus-group-default-level level))
3801   (gnus-group-setup-buffer)             ;May call from out of group buffer
3802   (let ((case-fold-search nil)
3803         (group (gnus-group-group-name)))
3804     (funcall gnus-group-prepare-function level unread nil)
3805     (if (zerop (buffer-size))
3806         (gnus-message 5 gnus-no-groups-message)
3807       (goto-char (point-min))
3808       (if (not group)
3809           ;; Go to the first group with unread articles.
3810           (gnus-group-search-forward nil nil nil t)
3811         ;; Find the right group to put point on. If the current group
3812         ;; has disapeared in the new listing, try to find the next
3813         ;; one. If no next one can be found, just leave point at the
3814         ;; first newsgroup in the buffer.
3815         (if (not (gnus-goto-char
3816                   (text-property-any (point-min) (point-max) 
3817                                      'gnus-group (intern group))))
3818             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
3819               (while (and newsrc
3820                           (not (gnus-goto-char 
3821                                 (text-property-any 
3822                                  (point-min) (point-max) 'gnus-group 
3823                                  (intern (car (car newsrc)))))))
3824                 (setq newsrc (cdr newsrc)))
3825               (or newsrc (progn (goto-char (point-max))
3826                                 (forward-line -1))))))
3827       ;; Adjust cursor point.
3828       (gnus-group-position-point))))
3829
3830 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
3831   "List all newsgroups with unread articles of level LEVEL or lower.
3832 If ALL is non-nil, list groups that have no unread articles.
3833 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
3834 If REGEXP, only list groups matching REGEXP."
3835   (set-buffer gnus-group-buffer)
3836   (let ((buffer-read-only nil)
3837         (newsrc (cdr gnus-newsrc-alist))
3838         (lowest (or lowest 1))
3839         info clevel unread group)
3840     (erase-buffer)
3841     (if (< lowest gnus-level-zombie)
3842         ;; List living groups.
3843         (while newsrc
3844           (setq info (car newsrc)
3845                 group (car info)
3846                 newsrc (cdr newsrc)
3847                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
3848           (and unread                   ; This group might be bogus
3849                (or (not regexp)
3850                    (string-match regexp group))
3851                (<= (setq clevel (car (cdr info))) level) 
3852                (>= clevel lowest)
3853                (or all                  ; We list all groups?
3854                    (eq unread t)        ; We list unactivated groups
3855                    (> unread 0)         ; We list groups with unread articles
3856                    (cdr (assq 'tick (nth 3 info)))) ; And groups with tickeds
3857                (gnus-group-insert-group-line 
3858                 nil group (car (cdr info)) (nth 3 info) unread (nth 4 info)))))
3859
3860     ;; List dead groups.
3861     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
3862          (gnus-group-prepare-flat-list-dead 
3863           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 
3864           gnus-level-zombie ?Z
3865           regexp))
3866     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
3867          (gnus-group-prepare-flat-list-dead 
3868           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 
3869           gnus-level-killed ?K regexp))
3870
3871     (gnus-group-set-mode-line)
3872     (setq gnus-group-list-mode (cons level all))
3873     (run-hooks 'gnus-group-prepare-hook)))
3874
3875 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
3876   ;; List zombies and killed lists somehwat faster, which was
3877   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>. It does
3878   ;; this by ignoring the group format specification altogether.
3879   (let (group beg)
3880     (while groups
3881       (setq group (car groups)
3882             groups (cdr groups))
3883       (if (or (not regexp)
3884               (string-match regexp group))
3885           (progn
3886             (setq beg (point))
3887             (insert (format " %c     *: %s\n" mark group))
3888             (add-text-properties 
3889              beg (1+ beg) 
3890              (list 'gnus-group (intern group)
3891                    'gnus-unread t
3892                    'gnus-level level)))))))
3893
3894 (defun gnus-group-real-name (group)
3895   "Find the real name of a foreign newsgroup."
3896   (if (string-match ":[^:]+$" group)
3897       (substring group (1+ (match-beginning 0)))
3898     group))
3899
3900 (defun gnus-group-prefixed-name (group method)
3901   "Return the whole name from GROUP and METHOD."
3902   (and (stringp method) (setq method (gnus-server-to-method method)))
3903   (concat (format "%s" (car method))
3904           (if (and 
3905                (assoc (format "%s" (car method)) (gnus-methods-using 'address))
3906                (not (string= (nth 1 method) "")))
3907               (concat "+" (nth 1 method)))
3908           ":" group))
3909
3910 (defun gnus-group-real-prefix (group)
3911   "Return the prefix of the current group name."
3912   (if (string-match "^[^:]+:" group)
3913       (substring group 0 (match-end 0))
3914     ""))
3915
3916 (defun gnus-group-method-name (group)
3917   "Return the method used for selecting GROUP."
3918   (let ((prefix (gnus-group-real-prefix group)))
3919     (if (equal prefix "")
3920         gnus-select-method
3921       (if (string-match "^[^\\+]+\\+" prefix)
3922           (list (intern (substring prefix 0 (1- (match-end 0))))
3923                 (substring prefix (match-end 0) (1- (length prefix))))
3924         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
3925
3926 (defun gnus-group-foreign-p (group)
3927   "Return nil if GROUP is native, non-nil if it is foreign."
3928   (string-match ":" group))
3929
3930 (defun gnus-group-topic-p ()
3931   "Return non-nil if the current line is a topic."
3932   (get-text-property (gnus-point-at-bol) 'gnus-topic))
3933
3934 (defun gnus-group-parameter-value (params symbol)
3935   "Return the value of SYMBOL in group PARAMS."
3936   (or (car (memq symbol params))        ; It's either a simple symbol
3937       (cdr (assq symbol params))))      ; or a cons.
3938
3939 (defun gnus-group-add-parameter (group param)
3940   "Add parameter PARAM to GROUP."
3941   (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
3942     (if (not info)
3943         () ; This is a dead group. We just ignore it.
3944       ;; Cons the new param to the old one and update.
3945       (gnus-group-set-info (cons param (nth 5 info)) group 'params))))
3946
3947 (defun gnus-group-set-info (info &optional method-only-group part)
3948   (let* ((entry (gnus-gethash
3949                  (or method-only-group (car info)) gnus-newsrc-hashtb))
3950          (part-info info)
3951          (info (if method-only-group (nth 2 entry) info)))
3952     (if (not method-only-group)
3953         ()
3954       (or entry
3955           (error "Trying to change non-existent group %s" method-only-group))
3956       ;; We have recevied parts of the actual group info - either the
3957       ;; select method or the group parameters.  We first check
3958       ;; whether we have to extend the info, and if so, do that.
3959       (let ((len (length info))
3960             (total (if (eq part 'method) 5 6)))
3961         (and (< len total)
3962              (setcdr (nthcdr (1- len) info)
3963                      (make-list (- total len) nil)))
3964         ;; Then we enter the new info.
3965         (setcar (nthcdr (1- total) info) part-info)))
3966     ;; We uncompress some lists of marked articles.
3967     (let (marked)
3968       (if (not (setq marked (nth 3 info)))
3969           ()
3970         (while marked
3971           (or (eq 'score (car (car marked)))
3972               (eq 'bookmark (car (car marked)))
3973               (eq 'killed (car (car marked)))
3974               (setcdr (car marked) 
3975                       (gnus-uncompress-range (cdr (car marked)))))
3976           (setq marked (cdr marked)))))
3977     (if entry
3978         ()
3979       ;; This is a new group, so we just create it.
3980       (save-excursion
3981         (set-buffer gnus-group-buffer)
3982         (if (nth 4 info)
3983             ;; It's a foreign group...
3984             (gnus-group-make-group 
3985              (gnus-group-real-name (car info))
3986              (prin1-to-string (car (nth 4 info)))
3987              (nth 1 (nth 4 info)))
3988           ;; It's a native group.
3989           (gnus-group-make-group (car info)))
3990         (gnus-message 6 "Note: New group created")
3991         (setq entry 
3992               (gnus-gethash (gnus-group-prefixed-name 
3993                              (gnus-group-real-name (car info))
3994                              (or (nth 4 info) gnus-select-method))
3995                             gnus-newsrc-hashtb))))
3996     ;; Whether it was a new group or not, we now have the entry, so we
3997     ;; can do the update.
3998     (if entry
3999         (progn
4000           (setcar (nthcdr 2 entry) info)
4001           (if (and (not (eq (car entry) t)) 
4002                    (gnus-gethash (car info) gnus-active-hashtb))
4003               (let ((marked (nth 3 info)))
4004                 (setcar entry 
4005                         (max 0 (- (length (gnus-list-of-unread-articles 
4006                                            (car info)))
4007                                   (length (cdr (assq 'tick marked)))
4008                                   (length (cdr (assq 'dormant marked)))))))))
4009       (error "No such group: %s" (car info)))))
4010
4011 (defun gnus-group-set-method-info (group select-method)
4012   (gnus-group-set-info select-method group 'method))
4013
4014 (defun gnus-group-set-params-info (group params)
4015   (gnus-group-set-info params group 'params))
4016
4017 (defun gnus-group-update-group-line ()
4018   "Update the current line in the group buffer."
4019   (let* ((buffer-read-only nil)
4020          (group (gnus-group-group-name))
4021          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
4022     (and entry
4023          (gnus-dribble-enter 
4024           (concat "(gnus-group-set-info '" 
4025                   (prin1-to-string (nth 2 entry)) ")")))
4026     (gnus-delete-line)
4027     (gnus-group-insert-group-line-info group)
4028     (forward-line -1)
4029     (gnus-group-position-point)))
4030
4031 (defun gnus-group-insert-group-line-info (group)
4032   "Insert GROUP on the current line."
4033   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
4034         active info)
4035     (if entry
4036         (progn
4037           (setq info (nth 2 entry))
4038           (gnus-group-insert-group-line 
4039            nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
4040       (setq active (gnus-gethash group gnus-active-hashtb))
4041       (gnus-group-insert-group-line 
4042        nil group 
4043        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
4044        nil (if active (- (1+ (cdr active)) (car active)) 0) nil))))
4045
4046 (defun gnus-group-insert-group-line 
4047   (gformat gnus-tmp-group gnus-tmp-level gnus-tmp-marked gnus-tmp-number
4048            gnus-tmp-method)
4049   (let* ((gformat (or gformat gnus-group-line-format-spec))
4050          (gnus-tmp-active (gnus-gethash gnus-tmp-group gnus-active-hashtb))
4051          (gnus-tmp-number-total 
4052           (if gnus-tmp-active 
4053               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
4054             0))
4055          (gnus-tmp-number-of-dormant 
4056           (length (cdr (assq 'dormant gnus-tmp-marked))))
4057          (gnus-tmp-number-of-ticked
4058           (length (cdr (assq 'tick gnus-tmp-marked))))
4059          (gnus-tmp-number-of-ticked-and-dormant
4060           (+ gnus-tmp-number-of-ticked gnus-tmp-number-of-dormant))
4061          (gnus-tmp-number-of-unread-unticked 
4062           (if (numberp gnus-tmp-number) (int-to-string (max 0 gnus-tmp-number))
4063             "*"))
4064          (gnus-tmp-number-of-read
4065           (if (numberp gnus-tmp-number)
4066               (max 0 (- gnus-tmp-number-total gnus-tmp-number))
4067             "*"))
4068          (gnus-tmp-subscribed
4069           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
4070                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
4071                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
4072                 (t ?K)))
4073          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
4074          (gnus-tmp-newsgroup-description 
4075           (if gnus-description-hashtb
4076               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
4077             ""))
4078          (gnus-tmp-moderated
4079           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
4080          (gnus-tmp-moderated-string 
4081           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
4082          (gnus-tmp-method
4083           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
4084          (gnus-tmp-news-server (or (car (cdr gnus-tmp-method)) ""))
4085          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
4086          (gnus-tmp-news-method-string 
4087           (if gnus-tmp-method
4088               (format "(%s:%s)" (car gnus-tmp-method)
4089                       (car (cdr gnus-tmp-method))) ""))
4090          (gnus-tmp-marked 
4091           (if (and (numberp gnus-tmp-number) 
4092                    (zerop gnus-tmp-number)
4093                    (> gnus-tmp-number-of-ticked 0))
4094               ?* ? ))
4095          (gnus-tmp-number
4096           (if (eq gnus-tmp-number t) "*" 
4097             (+ gnus-tmp-number gnus-tmp-number-of-dormant 
4098                gnus-tmp-number-of-ticked)))
4099          (gnus-tmp-process-marked
4100           (if (member gnus-tmp-group gnus-group-marked)
4101               gnus-process-mark ? ))
4102          (buffer-read-only nil)
4103          header                         ; passed as parameter to user-funcs.
4104          b)
4105     (beginning-of-line)
4106     (setq b (point))
4107     ;; Insert the text.
4108     (eval gformat)
4109
4110     (add-text-properties 
4111      b (1+ b) (list 'gnus-group (intern gnus-tmp-group)
4112                     'gnus-unread (if (numberp gnus-tmp-number)
4113                                      (string-to-int 
4114                                       gnus-tmp-number-of-unread-unticked)
4115                                    t)
4116                     'gnus-marked gnus-tmp-marked
4117                     'gnus-level gnus-tmp-level))))
4118
4119 (defun gnus-group-update-group (group &optional visible-only)
4120   "Update all lines where GROUP appear.
4121 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
4122 already." 
4123   (save-excursion
4124     (set-buffer gnus-group-buffer)
4125     ;; The buffer may be narrowed.
4126     (save-restriction
4127       (widen)
4128       (let ((ident (intern group))
4129             (loc (point-min))
4130             found buffer-read-only visible)
4131         ;; Enter the current status into the dribble buffer.
4132         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
4133           (if entry
4134               (gnus-dribble-enter 
4135                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
4136                        ")"))))
4137         ;; Find all group instances.  If topics are in use, each group
4138         ;; may be listed in more than once.
4139         (while (setq loc (text-property-any 
4140                           loc (point-max) 'gnus-group ident))
4141           (setq found t)
4142           (goto-char loc)
4143           (gnus-delete-line)
4144           (gnus-group-insert-group-line-info group)
4145           (setq loc (1+ loc)))
4146         (if (or found visible-only)
4147             ()
4148           ;; No such line in the buffer, find out where it's supposed to
4149           ;; go, and insert it there (or at the end of the buffer).
4150           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
4151           (let ((entry (cdr (cdr (gnus-gethash group gnus-newsrc-hashtb)))))
4152             (while (and entry (car entry)
4153                         (not
4154                          (gnus-goto-char
4155                           (text-property-any
4156                            (point-min) (point-max) 
4157                            'gnus-group (intern (car (car entry)))))))
4158               (setq entry (cdr entry)))
4159             (or entry (goto-char (point-max))))
4160           ;; Finally insert the line.
4161           (gnus-group-insert-group-line-info group))
4162         (gnus-group-set-mode-line)))))
4163
4164 (defun gnus-group-set-mode-line ()
4165   (if (memq 'group gnus-updated-mode-lines)
4166       (let* ((gformat (or gnus-group-mode-line-format-spec
4167                           (setq gnus-group-mode-line-format-spec
4168                                 (gnus-parse-format 
4169                                  gnus-group-mode-line-format 
4170                                  gnus-group-mode-line-format-alist))))
4171              (gnus-tmp-news-server (car (cdr gnus-select-method)))
4172              (gnus-tmp-news-method (car gnus-select-method))
4173              (max-len 60)
4174              (mode-string (eval gformat)))
4175         (setq mode-string (eval gformat))
4176         (if (> (length mode-string) max-len) 
4177             (setq mode-string (substring mode-string 0 (- max-len 4))))
4178         (setq mode-line-buffer-identification mode-string)
4179         (set-buffer-modified-p t))))
4180
4181 (defun gnus-group-group-name ()
4182   "Get the name of the newsgroup on the current line."
4183   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
4184     (and group (symbol-name group))))
4185
4186 (defun gnus-group-group-level ()
4187   "Get the level of the newsgroup on the current line."
4188   (get-text-property (gnus-point-at-bol) 'gnus-level))
4189
4190 (defun gnus-group-group-unread ()
4191   "Get the number of unread articles of the newsgroup on the current line."
4192   (get-text-property (gnus-point-at-bol) 'gnus-unread))
4193
4194 (defun gnus-group-search-forward (&optional backward all level first-too)
4195   "Find the next newsgroup with unread articles.
4196 If BACKWARD is non-nil, find the previous newsgroup instead.
4197 If ALL is non-nil, just find any newsgroup.
4198 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
4199 group exists.
4200 If FIRST-TOO, the current line is also eligible as a target."
4201   (let ((way (if backward -1 1))
4202         (low gnus-level-killed)
4203         (beg (point))
4204         pos found lev)
4205     (if (and backward (progn (beginning-of-line)) (bobp))
4206         nil
4207       (or first-too (forward-line way))
4208       (while (and 
4209               (not (eobp))
4210               (not (setq 
4211                     found 
4212                     (and (or all
4213                              (and
4214                               (let ((unread 
4215                                      (get-text-property (point) 'gnus-unread)))
4216                                 (or (eq unread t) (and unread (> unread 0))))
4217                               (setq lev (get-text-property (point)
4218                                                            'gnus-level))
4219                               (<= lev gnus-level-subscribed)))
4220                          (or (not level)
4221                              (and (setq lev (get-text-property (point)
4222                                                                'gnus-level))
4223                                   (or (= lev level)
4224                                       (and (< lev low)
4225                                            (< level lev)
4226                                            (progn
4227                                              (setq low lev)
4228                                              (setq pos (point))
4229                                              nil))))))))
4230               (zerop (forward-line way)))))
4231     (if found 
4232         (progn (gnus-group-position-point) t)
4233       (goto-char (or pos beg))
4234       (and pos t))))
4235
4236 ;;; Gnus group mode commands
4237
4238 ;; Group marking.
4239
4240 (defun gnus-group-mark-group (n &optional unmark no-advance)
4241   "Mark the current group."
4242   (interactive "p")
4243   (let ((buffer-read-only nil)
4244         group)
4245     (while 
4246         (and (> n 0) 
4247              (setq group (gnus-group-group-name))
4248              (progn
4249                (beginning-of-line)
4250                (forward-char 
4251                 (or (cdr (assq 'process gnus-group-mark-positions)) 2))
4252                (delete-char 1)
4253                (if unmark
4254                    (progn
4255                      (insert " ")
4256                      (setq gnus-group-marked (delete group gnus-group-marked)))
4257                  (insert "#")
4258                  (setq gnus-group-marked
4259                        (cons group (delete group gnus-group-marked))))
4260                t)
4261              (or no-advance (zerop (gnus-group-next-group 1))))
4262       (setq n (1- n)))
4263     (gnus-summary-position-point)
4264     n))
4265
4266 (defun gnus-group-unmark-group (n)
4267   "Remove the mark from the current group."
4268   (interactive "p")
4269   (gnus-group-mark-group n 'unmark))
4270
4271 (defun gnus-group-mark-region (unmark beg end)
4272   "Mark all groups between point and mark.
4273 If UNMARK, remove the mark instead."
4274   (interactive "P\nr")
4275   (let ((num (count-lines beg end)))
4276     (save-excursion
4277       (goto-char beg)
4278       (- num (gnus-group-mark-group num unmark)))))
4279
4280 (defun gnus-group-remove-mark (group)
4281   (and (gnus-group-goto-group group)
4282        (save-excursion
4283          (gnus-group-mark-group 1 'unmark t))))
4284
4285 ;; Return a list of groups to work on.  Take into consideration N (the
4286 ;; prefix) and the list of marked groups.
4287 (defun gnus-group-process-prefix (n)
4288   (cond (n
4289          (setq n (prefix-numeric-value n))
4290          ;; There is a prefix, so we return a list of the N next
4291          ;; groups. 
4292          (let ((way (if (< n 0) -1 1))
4293                (n (abs n))
4294                group groups)
4295            (save-excursion
4296              (while (and (> n 0)
4297                          (setq group (gnus-group-group-name)))
4298                (setq groups (cons group groups))
4299                (setq n (1- n))
4300                (forward-line way)))
4301            (nreverse groups)))
4302         (gnus-group-marked
4303          ;; No prefix, but a list of marked articles.
4304          (reverse gnus-group-marked))
4305         (t
4306          ;; Neither marked articles or a prefix, so we return the
4307          ;; current group.
4308          (let ((group (gnus-group-group-name)))
4309            (and group (list group))))))
4310
4311 ;; Selecting groups.
4312
4313 (defun gnus-group-read-group (&optional all no-article group)
4314   "Read news in this newsgroup.
4315 If the prefix argument ALL is non-nil, already read articles become
4316 readable. If the optional argument NO-ARTICLE is non-nil, no article
4317 will be auto-selected upon group entry."
4318   (interactive "P")
4319   (if (gnus-group-topic-p)
4320       (gnus-topic-fold) ; This is a topic line.
4321     ;; And this is a real group.
4322     (let ((group (or group (gnus-group-group-name)))
4323           number active marked entry)
4324       (or group (error "No group on current line"))
4325       (setq marked 
4326             (nth 3 (nth 2 (setq entry (gnus-gethash
4327                                        group gnus-newsrc-hashtb)))))
4328       ;; This group might be a dead group. In that case we have to get
4329       ;; the number of unread articles from `gnus-active-hashtb'.
4330       (if entry
4331           (setq number (car entry))
4332         (if (setq active (gnus-gethash group gnus-active-hashtb))
4333             (setq number (- (1+ (cdr active)) (car active)))))
4334       (gnus-summary-read-group 
4335        group (or all (and (numberp number) 
4336                           (zerop (+ number (length (cdr (assq 'tick marked)))
4337                                     (length (cdr (assq 'dormant marked)))))))
4338        no-article))))
4339
4340 (defun gnus-group-select-group (&optional all)
4341   "Select this newsgroup.
4342 No article is selected automatically.
4343 If argument ALL is non-nil, already read articles become readable."
4344   (interactive "P")
4345   (gnus-group-read-group all t))
4346
4347 (defun gnus-group-quick-select-group (&optional all)
4348   "Select the current group \"quickly\". 
4349 This means that no highlighting or scoring will be performed."
4350   (interactive "P")
4351   (let (gnus-visual
4352         gnus-score-find-score-files-function
4353         gnus-apply-kill-hook
4354         gnus-summary-expunge-below)
4355     (gnus-group-read-group all t)))
4356
4357 ;;;###autoload
4358 (defun gnus-fetch-group (group)
4359   "Start Gnus if necessary and enter GROUP.
4360 Returns whether the fetching was successful or not."
4361   (interactive "sGroup name: ")
4362   (or (get-buffer gnus-group-buffer)
4363       (gnus))
4364   (gnus-group-select-group))
4365
4366 (defun gnus-group-select-group-all ()
4367   "Select the current group and display all articles in it."
4368   (interactive)
4369   (gnus-group-select-group 'all))
4370
4371 ;; Enter a group that is not in the group buffer. Non-nil is returned
4372 ;; if selection was successful.
4373 (defun gnus-group-read-ephemeral-group 
4374   (group method &optional activate quit-config)
4375   (let ((group (if (gnus-group-foreign-p group) group
4376                  (gnus-group-prefixed-name group method))))
4377     (gnus-sethash 
4378      group
4379      (list t nil (list group gnus-level-default-subscribed nil nil 
4380                        (append method
4381                                (list
4382                                 (list 'quit-config 
4383                                       (if quit-config quit-config
4384                                         (cons (current-buffer) 'summary)))))))
4385      gnus-newsrc-hashtb)
4386     (set-buffer gnus-group-buffer)
4387     (or (gnus-check-server method)
4388         (error "Unable to contact server: %s" (gnus-status-message method)))
4389     (if activate (or (gnus-request-group group)
4390                      (error "Couldn't request group")))
4391     (condition-case ()
4392         (gnus-group-read-group t t group)
4393       (error nil)
4394       (quit nil))
4395     (not (equal major-mode 'gnus-group-mode))))
4396   
4397 (defun gnus-group-jump-to-group (group)
4398   "Jump to newsgroup GROUP."
4399   (interactive 
4400    (list (completing-read 
4401           "Group: " gnus-active-hashtb nil 
4402           (memq gnus-select-method gnus-have-read-active-file))))
4403
4404   (if (equal group "")
4405       (error "Empty group name"))
4406
4407   (let ((b (text-property-any 
4408             (point-min) (point-max) 'gnus-group (intern group))))
4409     (if b
4410         ;; Either go to the line in the group buffer...
4411         (goto-char b)
4412       ;; ... or insert the line.
4413       (or
4414        (gnus-gethash group gnus-active-hashtb)
4415        (gnus-activate-group group)
4416        (error "%s error: %s" group (gnus-status-message group)))
4417
4418       (gnus-group-update-group group)
4419       (goto-char (text-property-any 
4420                   (point-min) (point-max) 'gnus-group (intern group)))))
4421   ;; Adjust cursor point.
4422   (gnus-group-position-point))
4423
4424 (defun gnus-group-goto-group (group)
4425   "Goto to newsgroup GROUP."
4426   (let ((b (text-property-any (point-min) (point-max) 
4427                               'gnus-group (intern group))))
4428     (and b (goto-char b))))
4429
4430 (defun gnus-group-next-group (n)
4431   "Go to next N'th newsgroup.
4432 If N is negative, search backward instead.
4433 Returns the difference between N and the number of skips actually
4434 done."
4435   (interactive "p")
4436   (gnus-group-next-unread-group n t))
4437
4438 (defun gnus-group-next-unread-group (n &optional all level)
4439   "Go to next N'th unread newsgroup.
4440 If N is negative, search backward instead.
4441 If ALL is non-nil, choose any newsgroup, unread or not.
4442 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
4443 such group can be found, the next group with a level higher than
4444 LEVEL.
4445 Returns the difference between N and the number of skips actually
4446 made."
4447   (interactive "p")
4448   (let ((backward (< n 0))
4449         (n (abs n)))
4450     (while (and (> n 0)
4451                 (gnus-group-search-forward 
4452                  backward (or (not gnus-group-goto-unread) all) level))
4453       (setq n (1- n)))
4454     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
4455                                (if level " on this level or higher" "")))
4456     n))
4457
4458 (defun gnus-group-prev-group (n)
4459   "Go to previous N'th newsgroup.
4460 Returns the difference between N and the number of skips actually
4461 done."
4462   (interactive "p")
4463   (gnus-group-next-unread-group (- n) t))
4464
4465 (defun gnus-group-prev-unread-group (n)
4466   "Go to previous N'th unread newsgroup.
4467 Returns the difference between N and the number of skips actually
4468 done."  
4469   (interactive "p")
4470   (gnus-group-next-unread-group (- n)))
4471
4472 (defun gnus-group-next-unread-group-same-level (n)
4473   "Go to next N'th unread newsgroup on the same level.
4474 If N is negative, search backward instead.
4475 Returns the difference between N and the number of skips actually
4476 done."
4477   (interactive "p")
4478   (gnus-group-next-unread-group n t (gnus-group-group-level))
4479   (gnus-group-position-point))
4480
4481 (defun gnus-group-prev-unread-group-same-level (n)
4482   "Go to next N'th unread newsgroup on the same level.
4483 Returns the difference between N and the number of skips actually
4484 done."
4485   (interactive "p")
4486   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
4487   (gnus-group-position-point))
4488
4489 (defun gnus-group-best-unread-group (&optional exclude-group)
4490   "Go to the group with the highest level.
4491 If EXCLUDE-GROUP, do not go to that group."
4492   (interactive)
4493   (goto-char (point-min))
4494   (let ((best 100000)
4495         unread best-point)
4496     (while (setq unread (get-text-property (point) 'gnus-unread))
4497       (if (and (numberp unread) (> unread 0))
4498           (progn
4499             (if (and (< (get-text-property (point) 'gnus-level) best)
4500                      (or (not exclude-group)
4501                          (not (equal exclude-group (gnus-group-group-name)))))
4502                 (progn 
4503                   (setq best (get-text-property (point) 'gnus-level))
4504                   (setq best-point (point))))))
4505       (forward-line 1))
4506     (if best-point (goto-char best-point))
4507     (gnus-summary-position-point)
4508     (and best-point (gnus-group-group-name))))
4509
4510 (defun gnus-group-first-unread-group ()
4511   "Go to the first group with unread articles."
4512   (interactive)
4513   (prog1
4514       (let ((opoint (point))
4515             unread)
4516         (goto-char (point-min))
4517         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
4518                 (not (zerop unread))    ; Has unread articles.
4519                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
4520             (point)                     ; Success.
4521           (goto-char opoint)
4522           nil))                         ; Not success.
4523     (gnus-group-position-point)))
4524
4525 (defun gnus-group-enter-server-mode ()
4526   "Jump to the server buffer."
4527   (interactive)
4528   (gnus-server-setup-buffer)
4529   (gnus-configure-windows 'server)
4530   (gnus-server-prepare))
4531
4532 (defun gnus-group-make-group (name &optional method address)
4533   "Add a new newsgroup.
4534 The user will be prompted for a NAME, for a select METHOD, and an
4535 ADDRESS."
4536   (interactive
4537    (cons 
4538     (read-string "Group name: ")
4539     (let ((method
4540            (completing-read 
4541             "Method: " (append gnus-valid-select-methods gnus-server-alist)
4542             nil t)))
4543       (if (assoc method gnus-valid-select-methods)
4544           (list method
4545                 (if (memq 'prompt-address
4546                           (assoc method gnus-valid-select-methods))
4547                     (read-string "Address: ")
4548                   ""))
4549         (list method nil)))))
4550   
4551   (save-excursion
4552     (set-buffer gnus-group-buffer)
4553     (let* ((meth (and method (if address (list (intern method) address) 
4554                                method)))
4555            (nname (if method (gnus-group-prefixed-name name meth) name))
4556            info)
4557       (and (gnus-gethash nname gnus-newsrc-hashtb)
4558            (error "Group %s already exists" nname))
4559       (gnus-group-change-level 
4560        (setq info (list t nname gnus-level-default-subscribed nil nil meth))
4561        gnus-level-default-subscribed gnus-level-killed 
4562        (and (gnus-group-group-name)
4563             (gnus-gethash (gnus-group-group-name)
4564                           gnus-newsrc-hashtb))
4565        t)
4566       (gnus-sethash nname (cons 1 0) gnus-active-hashtb)
4567       (gnus-dribble-enter 
4568        (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")"))
4569       (gnus-group-insert-group-line-info nname)
4570
4571       (if (assoc method gnus-valid-select-methods)
4572           (require (intern method)))
4573       (and (gnus-check-backend-function 'request-create-group nname)
4574            (gnus-request-create-group nname))
4575       t)))
4576
4577 (defun gnus-group-delete-group (group &optional force)
4578   "Delete the current group.
4579 If FORCE (the prefix) is non-nil, all the articles in the group will
4580 be deleted.  This is \"deleted\" as in \"removed forever from the face
4581 of the Earth\".  There is no undo."
4582   (interactive 
4583    (list (gnus-group-group-name)
4584          current-prefix-arg))
4585   (or group (error "No group to rename"))
4586   (or (gnus-check-backend-function 'request-delete-group group)
4587       (error "This backend does not support group deletion"))
4588   (prog1
4589       (if (not (gnus-yes-or-no-p
4590                 (format
4591                  "Do you really want to delete %s%s? " 
4592                  group (if force " and all its contents" ""))))
4593           () ; Whew!
4594         (gnus-message 6 "Deleting group %s..." group)
4595         (if (not (gnus-request-delete-group group force))
4596             (progn
4597               (gnus-message 3 "Couldn't delete group %s" group)
4598               (ding))
4599           (gnus-message 6 "Deleting group %s...done" group)
4600           (gnus-group-goto-group group)
4601           (gnus-group-kill-group 1 t)
4602           t))
4603     (gnus-group-position-point)))
4604
4605 (defun gnus-group-rename-group (group new-name)
4606   (interactive
4607    (list
4608     (gnus-group-group-name)
4609     (progn
4610       (or (gnus-check-backend-function 
4611            'request-rename-group (gnus-group-group-name))
4612           (error "This backend does not support renaming groups"))
4613       (read-string "New group name: "))))
4614
4615   (or (gnus-check-backend-function 'request-rename-group group)
4616       (error "This backend does not support renaming groups"))
4617
4618   (or group (error "No group to rename"))
4619   (and (string-match "^[ \t]*$" new-name) 
4620        (error "Not a valid group name"))
4621
4622   ;; We find the proper prefixed name.
4623   (setq new-name
4624         (gnus-group-prefixed-name 
4625          (gnus-group-real-name new-name)
4626          (nth 4 (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))))
4627
4628   (gnus-message 6 "Renaming group %s to %s..." group new-name)
4629   (prog1
4630       (if (not (gnus-request-rename-group group new-name))
4631           (progn
4632             (gnus-message 3 "Couldn't rename group %s to %s" group new-name)
4633             (ding))
4634         ;; We rename the group internally by killing it...
4635         (gnus-group-goto-group group)
4636         (gnus-group-kill-group)
4637         ;; ... changing its name ...
4638         (setcar (cdr (car gnus-list-of-killed-groups))
4639                 new-name)
4640         ;; ... and then yanking it.  Magic!
4641         (gnus-group-yank-group) 
4642         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
4643         new-name)
4644     (gnus-group-position-point)))
4645
4646
4647 (defun gnus-group-edit-group (group &optional part)
4648   "Edit the group on the current line."
4649   (interactive (list (gnus-group-group-name)))
4650   (let ((done-func '(lambda () 
4651                       "Exit editing mode and update the information."
4652                       (interactive)
4653                       (gnus-group-edit-group-done 'part 'group)))
4654         (part (or part 'info))
4655         (winconf (current-window-configuration))
4656         info)
4657     (or group (error "No group on current line"))
4658     (or (setq info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
4659         (error "Killed group; can't be edited"))
4660     (set-buffer (get-buffer-create gnus-group-edit-buffer))
4661     (gnus-configure-windows 'edit-group)
4662     (gnus-add-current-to-buffer-list)
4663     (emacs-lisp-mode)
4664     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4665     (use-local-map (copy-keymap emacs-lisp-mode-map))
4666     (local-set-key "\C-c\C-c" done-func)
4667     (make-local-variable 'gnus-prev-winconf)
4668     (setq gnus-prev-winconf winconf)
4669     ;; We modify the func to let it know what part it is editing.
4670     (setcar (cdr (nth 4 done-func)) (list 'quote part))
4671     (setcar (cdr (cdr (nth 4 done-func))) group)
4672     (erase-buffer)
4673     (insert
4674      (cond 
4675       ((eq part 'method)
4676        ";; Type `C-c C-c' after editing the select method.\n\n")
4677       ((eq part 'params)
4678        ";; Type `C-c C-c' after editing the group parameters.\n\n")
4679       ((eq part 'info)
4680        ";; Type `C-c C-c' after editing the group info.\n\n")))
4681     (let ((cinfo (gnus-copy-sequence info))
4682           marked)
4683       (if (not (setq marked (nth 3 cinfo)))
4684           ()
4685         (while marked
4686           (or (eq 'score (car (car marked)))
4687               (eq 'bookmark (car (car marked)))
4688               (eq 'killed (car (car marked)))
4689               (not (numberp (car (cdr (car marked)))))
4690               (setcdr (car marked) 
4691                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
4692           (setq marked (cdr marked))))
4693       (insert 
4694        (pp-to-string
4695         (cond ((eq part 'method)
4696                (or (nth 4 info) "native"))
4697               ((eq part 'params)
4698                (nth 5 info))
4699               (t
4700                cinfo)))
4701        "\n"))))
4702
4703 (defun gnus-group-edit-group-method (group)
4704   "Edit the select method of GROUP."
4705   (interactive (list (gnus-group-group-name)))
4706   (gnus-group-edit-group group 'method))
4707
4708 (defun gnus-group-edit-group-parameters (group)
4709   "Edit the group parameters of GROUP."
4710   (interactive (list (gnus-group-group-name)))
4711   (gnus-group-edit-group group 'params))
4712
4713 (defun gnus-group-edit-group-done (part group)
4714   "Get info from buffer, update variables and jump to the group buffer."
4715   (set-buffer (get-buffer-create gnus-group-edit-buffer))
4716   (goto-char (point-min))
4717   (let ((form (read (current-buffer)))
4718         (winconf gnus-prev-winconf))
4719     (if (eq part 'info) 
4720         (gnus-group-set-info form)
4721       (gnus-group-set-info form group part))
4722     (kill-buffer (current-buffer))
4723     (and winconf (set-window-configuration winconf))
4724     (set-buffer gnus-group-buffer)
4725     (gnus-group-update-group (gnus-group-group-name))
4726     (gnus-group-position-point)))
4727
4728 (defun gnus-group-make-help-group ()
4729   "Create the Gnus documentation group."
4730   (interactive)
4731   (let ((path load-path)
4732         name)
4733     (and (gnus-gethash (setq name (gnus-group-prefixed-name
4734                                    "gnus-help" '(nndoc "gnus-help")))
4735                        gnus-newsrc-hashtb)
4736          (error "Documentation group already exists"))
4737     (while (and path
4738                 (not (file-exists-p (concat (file-name-as-directory (car path))
4739                                             "doc.txt"))))
4740       (setq path (cdr path)))
4741     (or path (error "Couldn't find doc group"))
4742     (gnus-group-make-group 
4743      (gnus-group-real-name name)
4744      (list 'nndoc name
4745            (list 'nndoc-address 
4746                  (concat (file-name-as-directory (car path)) "doc.txt"))
4747            (list 'nndoc-article-type 'mbox)))
4748     (forward-line -1))
4749   (gnus-group-position-point))
4750
4751 (defun gnus-group-make-doc-group (file type)
4752   "Create a group that uses a single file as the source."
4753   (interactive 
4754    (list (read-file-name "File name: ") 
4755          (let ((err "")
4756                found char)
4757            (while (not found)
4758              (message "%sFile type (mbox, babyl, digest) [mbd]: " err)
4759              (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
4760                                ((= char ?b) 'babyl)
4761                                ((= char ?d) 'digest)
4762                                (t (setq err (format "%c unknown. " char))
4763                                   nil))))
4764            found)))
4765   (let* ((file (expand-file-name file))
4766          (name (gnus-generate-new-group-name
4767                 (gnus-group-prefixed-name
4768                  (file-name-nondirectory file) '(nndoc "")))))
4769     (gnus-group-make-group 
4770      (gnus-group-real-name name)
4771      (list 'nndoc name
4772            (list 'nndoc-address file)
4773            (list 'nndoc-article-type type)))
4774     (forward-line -1)
4775     (gnus-group-position-point)))
4776
4777 (defun gnus-group-make-archive-group (&optional all)
4778   "Create the (ding) Gnus archive group of the most recent articles.
4779 Given a prefix, create a full group."
4780   (interactive "P")
4781   (let ((group (gnus-group-prefixed-name 
4782                 (if all "ding.archives" "ding.recent") '(nndir ""))))
4783     (and (gnus-gethash group gnus-newsrc-hashtb)
4784          (error "Archive group already exists"))
4785     (gnus-group-make-group
4786      (gnus-group-real-name group)
4787      (list 'nndir (if all "hpc" "edu")
4788            (list 'nndir-directory  
4789                  (if all gnus-group-archive-directory 
4790                    gnus-group-recent-archive-directory)))))
4791   (forward-line -1)
4792   (gnus-group-position-point))
4793
4794 (defun gnus-group-make-directory-group (dir)
4795   "Create an nndir group.
4796 The user will be prompted for a directory. The contents of this
4797 directory will be used as a newsgroup. The directory should contain
4798 mail messages or news articles in files that have numeric names."
4799   (interactive
4800    (list (read-file-name "Create group from directory: ")))
4801   (or (file-exists-p dir) (error "No such directory"))
4802   (or (file-directory-p dir) (error "Not a directory"))
4803   (let ((ext "")
4804         (i 0)
4805         group)
4806     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
4807       (setq group
4808             (gnus-group-prefixed-name 
4809              (concat (file-name-as-directory (directory-file-name dir))
4810                      ext)
4811              '(nndir "")))
4812       (setq ext (format "<%d>" (setq i (1+ i)))))
4813     (gnus-group-make-group 
4814      (gnus-group-real-name group)
4815      (list 'nndir group (list 'nndir-directory dir))))
4816   (forward-line -1)
4817   (gnus-group-position-point))
4818
4819 (defun gnus-group-make-kiboze-group (group address scores)
4820   "Create an nnkiboze group.
4821 The user will be prompted for a name, a regexp to match groups, and
4822 score file entries for articles to include in the group."
4823   (interactive
4824    (list
4825     (read-string "nnkiboze group name: ")
4826     (read-string "Source groups (regexp): ")
4827     (let ((headers (mapcar (lambda (group) (list group))
4828                            '("subject" "from" "number" "date" "message-id"
4829                              "references" "chars" "lines" "xref"
4830                              "followup" "all" "body" "head")))
4831           scores header regexp regexps)
4832       (while (not (equal "" (setq header (completing-read 
4833                                           "Match on header: " headers nil t))))
4834         (setq regexps nil)
4835         (while (not (equal "" (setq regexp (read-string 
4836                                             (format "Match on %s (string): "
4837                                                     header)))))
4838           (setq regexps (cons (list regexp nil nil 'r) regexps)))
4839         (setq scores (cons (cons header regexps) scores)))
4840       scores)))
4841   (gnus-group-make-group group "nnkiboze" address)
4842   (save-excursion
4843     (gnus-set-work-buffer)
4844     (let (emacs-lisp-mode-hook)
4845       (pp scores (current-buffer)))
4846     (write-region (point-min) (point-max) 
4847                   (gnus-score-file-name (concat "nnkiboze:" group))))
4848   (forward-line -1)
4849   (gnus-group-position-point))
4850
4851 (defun gnus-group-add-to-virtual (n vgroup)
4852   "Add the current group to a virtual group."
4853   (interactive
4854    (list current-prefix-arg
4855          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
4856                           "nnvirtual:")))
4857   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
4858       (error "%s is not an nnvirtual group" vgroup))
4859   (let* ((groups (gnus-group-process-prefix n))
4860          (method (nth 4 (nth 2 (gnus-gethash vgroup gnus-newsrc-hashtb)))))
4861     (setcar (cdr method)
4862             (concat 
4863              (nth 1 method) "\\|"
4864              (mapconcat 
4865               (lambda (s) 
4866                 (gnus-group-remove-mark s)
4867                 (concat "\\(^" (regexp-quote s) "$\\)"))
4868               groups "\\|"))))
4869   (gnus-group-position-point))
4870
4871 (defun gnus-group-make-empty-virtual (group)
4872   "Create a new, fresh, empty virtual group."
4873   (interactive "sCreate new, empty virtual group: ")
4874   (let* ((method (list 'nnvirtual "^$"))
4875          (pgroup (gnus-group-prefixed-name group method)))
4876     ;; Check whether it exists already.
4877     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
4878          (error "Group %s already exists." pgroup))
4879     ;; Subscribe the new group after the group on the current line.
4880     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
4881     (gnus-group-update-group pgroup)
4882     (forward-line -1)
4883     (gnus-group-position-point)))
4884
4885 (defun gnus-group-enter-directory (dir)
4886   "Enter an ephemeral nneething group."
4887   (interactive "DDirectory to read: ")
4888   (let* ((method (list 'nneething dir))
4889          (leaf (gnus-group-prefixed-name
4890                 (file-name-nondirectory (directory-file-name dir))
4891                 method))
4892          (name (gnus-generate-new-group-name leaf)))
4893     (let ((nneething-read-only t))
4894       (or (gnus-group-read-ephemeral-group 
4895            name method t
4896            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
4897                                       'summary 'group)))
4898           (error "Couldn't enter %s" dir)))))
4899
4900 ;; Group sorting commands
4901 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
4902
4903 (defun gnus-group-sort-groups ()
4904   "Sort the group buffer according to `gnus-group-sort-function'."
4905   (interactive)
4906   (let ((funcs (if (listp gnus-group-sort-function) gnus-group-sort-function
4907                  (list gnus-group-sort-function))))
4908     ;; We peel off the dummy group off the alist.
4909     (and (equal (car (car gnus-newsrc-alist)) "dummy.group")
4910          (setq gnus-newsrc-alist (cdr gnus-newsrc-alist)))
4911     ;; Do the sorting.
4912     (while funcs
4913       (setq gnus-newsrc-alist 
4914             (sort gnus-newsrc-alist (car funcs)))
4915       (setq funcs (cdr funcs))))
4916   (gnus-make-hashtable-from-newsrc-alist)
4917   (gnus-group-list-groups))
4918
4919 (defun gnus-group-sort-by-alphabet (info1 info2)
4920   (string< (car info1) (car info2)))
4921
4922 (defun gnus-group-sort-by-unread (info1 info2)
4923   (let ((n1 (car (gnus-gethash (car info1) gnus-newsrc-hashtb)))
4924         (n2 (car (gnus-gethash (car info2) gnus-newsrc-hashtb))))
4925     (< (or (and (numberp n1) n1) 0)
4926        (or (and (numberp n2) n2) 0))))
4927
4928 (defun gnus-group-sort-by-level (info1 info2)
4929   (< (nth 1 info1) (nth 1 info2)))
4930
4931 (defun gnus-group-sort-by-method (info1 info2)
4932   (string< (symbol-name (car (gnus-find-method-for-group (car info1))))
4933            (symbol-name (car (gnus-find-method-for-group (car info2))))))
4934
4935 ;; Group catching up.
4936
4937 (defun gnus-group-catchup-current (&optional n all)
4938   "Mark all articles not marked as unread in current newsgroup as read.
4939 If prefix argument N is numeric, the ARG next newsgroups will be
4940 caught up. If ALL is non-nil, marked articles will also be marked as
4941 read. Cross references (Xref: header) of articles are ignored.
4942 The difference between N and actual number of newsgroups that were
4943 caught up is returned."
4944   (interactive "P")
4945   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
4946                gnus-expert-user
4947                (gnus-y-or-n-p
4948                 (if all
4949                     "Do you really want to mark all articles as read? "
4950                   "Mark all unread articles as read? "))))
4951       n
4952     (let ((groups (gnus-group-process-prefix n))
4953           (ret 0))
4954       (while groups
4955         ;; Virtual groups have to be given special treatment. 
4956         (let ((method (gnus-find-method-for-group (car groups))))
4957           (if (eq 'nnvirtual (car method))
4958               (nnvirtual-catchup-group
4959                (gnus-group-real-name (car groups)) (nth 1 method) all)))
4960         (gnus-group-remove-mark (car groups))
4961         (if (prog1
4962                 (gnus-group-goto-group (car groups))
4963               (gnus-group-catchup (car groups) all))
4964             (gnus-group-update-group-line)
4965           (setq ret (1+ ret)))
4966         (setq groups (cdr groups)))
4967       (gnus-group-next-unread-group 1)
4968       ret)))
4969
4970 (defun gnus-group-catchup-current-all (&optional n)
4971   "Mark all articles in current newsgroup as read.
4972 Cross references (Xref: header) of articles are ignored."
4973   (interactive "P")
4974   (gnus-group-catchup-current n 'all))
4975
4976 (defun gnus-group-catchup (group &optional all)
4977   "Mark all articles in GROUP as read.
4978 If ALL is non-nil, all articles are marked as read.
4979 The return value is the number of articles that were marked as read,
4980 or nil if no action could be taken."
4981   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4982          (num (car entry))
4983          (marked (nth 3 (nth 2 entry))))
4984     (if (not (numberp (car entry)))
4985         (gnus-message 1 "Can't catch up; non-active group")
4986       ;; Do the updating only if the newsgroup isn't killed.
4987       (if (not entry)
4988           ()
4989         (gnus-update-read-articles 
4990          group (and (not all) (append (cdr (assq 'tick marked))
4991                                       (cdr (assq 'dormant marked))))
4992          nil (and (not all) (cdr (assq 'tick marked))))
4993         (and all 
4994              (setq marked (nth 3 (nth 2 entry)))
4995              (setcar (nthcdr 3 (nth 2 entry)) 
4996                      (delq (assq 'dormant marked) 
4997                            (nth 3 (nth 2 entry)))))))
4998     num))
4999
5000 (defun gnus-group-expire-articles (&optional n)
5001   "Expire all expirable articles in the current newsgroup."
5002   (interactive "P")
5003   (let ((groups (gnus-group-process-prefix n))
5004         group)
5005     (or groups (error "No groups to expire"))
5006     (while groups
5007       (setq group (car groups)
5008             groups (cdr groups))
5009       (gnus-group-remove-mark group)
5010       (if (not (gnus-check-backend-function 'request-expire-articles group))
5011           ()
5012         (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
5013                (expirable (if (gnus-group-total-expirable-p group)
5014                               (cons nil (gnus-list-of-read-articles group))
5015                             (assq 'expire (nth 3 info)))))
5016           (and expirable 
5017                (setcdr expirable
5018                        (gnus-request-expire-articles 
5019                         (cdr expirable) group))))))))
5020
5021 (defun gnus-group-expire-all-groups ()
5022   "Expire all expirable articles in all newsgroups."
5023   (interactive)
5024   (save-excursion
5025     (gnus-message 5 "Expiring...")
5026     (let ((gnus-group-marked (mapcar (lambda (info) (car info))
5027                                      (cdr gnus-newsrc-alist))))
5028       (gnus-group-expire-articles nil)))
5029   (gnus-group-position-point)
5030   (gnus-message 5 "Expiring...done"))
5031
5032 (defun gnus-group-set-current-level (n level)
5033   "Set the level of the next N groups to LEVEL."
5034   (interactive 
5035    (list
5036     (prefix-numeric-value current-prefix-arg)
5037     (string-to-int
5038      (read-string (format "Level (default %s): " (gnus-group-group-level))
5039                   (int-to-string (gnus-group-group-level))))))
5040   (or (and (>= level 1) (<= level gnus-level-killed))
5041       (error "Illegal level: %d" level))
5042   (let ((groups (gnus-group-process-prefix n))
5043         group)
5044     (while groups
5045       (setq group (car groups)
5046             groups (cdr groups))
5047       (gnus-group-remove-mark group)
5048       (gnus-message 6 "Changed level of %s from %d to %d" 
5049                     group (gnus-group-group-level) level)
5050       (gnus-group-change-level group level
5051                                (gnus-group-group-level))
5052       (gnus-group-update-group-line)))
5053   (gnus-group-position-point))
5054
5055 (defun gnus-group-unsubscribe-current-group (&optional n)
5056   "Toggle subscription of the current group.
5057 If given numerical prefix, toggle the N next groups."
5058   (interactive "P")
5059   (let ((groups (gnus-group-process-prefix n))
5060         group)
5061     (while groups
5062       (setq group (car groups)
5063             groups (cdr groups))
5064       (gnus-group-remove-mark group)
5065       (gnus-group-unsubscribe-group
5066        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
5067                  gnus-level-default-unsubscribed
5068                gnus-level-default-subscribed))
5069       (gnus-group-update-group-line))
5070     (gnus-group-next-group 1)))
5071
5072 (defun gnus-group-unsubscribe-group (group &optional level)
5073   "Toggle subscribe from/to unsubscribe GROUP.
5074 New newsgroup is added to .newsrc automatically."
5075   (interactive
5076    (list (completing-read
5077           "Group: " gnus-active-hashtb nil 
5078           (memq gnus-select-method gnus-have-read-active-file))))
5079   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
5080     (cond
5081      ((string-match "^[ \t]$" group)
5082       (error "Empty group name"))
5083      (newsrc
5084       ;; Toggle subscription flag.
5085       (gnus-group-change-level 
5086        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc)) 
5087                                       gnus-level-subscribed) 
5088                                   (1+ gnus-level-subscribed)
5089                                 gnus-level-default-subscribed)))
5090       (gnus-group-update-group group))
5091      ((and (stringp group)
5092            (or (not (memq gnus-select-method gnus-have-read-active-file))
5093                (gnus-gethash group gnus-active-hashtb)))
5094       ;; Add new newsgroup.
5095       (gnus-group-change-level 
5096        group 
5097        (if level level gnus-level-default-subscribed) 
5098        (or (and (member group gnus-zombie-list) 
5099                 gnus-level-zombie) 
5100            gnus-level-killed)
5101        (and (gnus-group-group-name)
5102             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
5103       (gnus-group-update-group group))
5104      (t (error "No such newsgroup: %s" group)))
5105     (gnus-group-position-point)))
5106
5107 (defun gnus-group-transpose-groups (n)
5108   "Move the current newsgroup up N places.
5109 If given a negative prefix, move down instead. The difference between
5110 N and the number of steps taken is returned." 
5111   (interactive "p")
5112   (or (gnus-group-group-name)
5113       (error "No group on current line"))
5114   (gnus-group-kill-group 1)
5115   (prog1
5116       (forward-line (- n))
5117     (gnus-group-yank-group)
5118     (gnus-group-position-point)))
5119
5120 (defun gnus-group-kill-all-zombies ()
5121   "Kill all zombie newsgroups."
5122   (interactive)
5123   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
5124   (setq gnus-zombie-list nil)
5125   (gnus-group-list-groups))
5126
5127 (defun gnus-group-kill-region (begin end)
5128   "Kill newsgroups in current region (excluding current point).
5129 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
5130   (interactive "r")
5131   (let ((lines
5132          ;; Count lines.
5133          (save-excursion
5134            (count-lines
5135             (progn
5136               (goto-char begin)
5137               (beginning-of-line)
5138               (point))
5139             (progn
5140               (goto-char end)
5141               (beginning-of-line)
5142               (point))))))
5143     (goto-char begin)
5144     (beginning-of-line)                 ;Important when LINES < 1
5145     (gnus-group-kill-group lines)))
5146
5147 (defun gnus-group-kill-group (&optional n discard)
5148   "The the next N groups.
5149 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
5150 However, only groups that were alive can be yanked; already killed 
5151 groups or zombie groups can't be yanked.
5152 The return value is the name of the (last) group that was killed."
5153   (interactive "P")
5154   (let ((buffer-read-only nil)
5155         (groups (gnus-group-process-prefix n))
5156         group entry level)
5157     (if (or t (< (length groups) 10))
5158         ;; This is faster when there are few groups.
5159         (while groups
5160           (setq group (car groups)
5161                 groups (cdr groups))
5162           (gnus-group-remove-mark group)
5163           (setq level (gnus-group-group-level))
5164           (gnus-delete-line)
5165           (if (and (not discard)
5166                    (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
5167               (setq gnus-list-of-killed-groups 
5168                     (cons (cons (car entry) (nth 2 entry)) 
5169                           gnus-list-of-killed-groups)))
5170           (gnus-group-change-level 
5171            (if entry entry group) gnus-level-killed (if entry nil level)))
5172       ;; If there are lots and lots of groups to be killed, we use
5173       ;; this thing instead.
5174       ;; !!! Not written.
5175       )
5176       
5177     (gnus-group-position-point)
5178     group))
5179
5180 (defun gnus-group-yank-group (&optional arg)
5181   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
5182 inserting it before the current newsgroup.  The numeric ARG specifies
5183 how many newsgroups are to be yanked.  The name of the (last)
5184 newsgroup yanked is returned."
5185   (interactive "p")
5186   (if (not arg) (setq arg 1))
5187   (let (info group prev)
5188     (while (>= (setq arg (1- arg)) 0)
5189       (if (not (setq info (car gnus-list-of-killed-groups)))
5190           (error "No more newsgroups to yank"))
5191       (setq group (nth 2 info))
5192       ;; Find which newsgroup to insert this one before - search
5193       ;; backward until something suitable is found. If there are no
5194       ;; other newsgroups in this buffer, just make this newsgroup the
5195       ;; first newsgroup.
5196       (setq prev (gnus-group-group-name))
5197       (gnus-group-change-level 
5198        info (nth 2 info) gnus-level-killed 
5199        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
5200        t)
5201       (gnus-group-insert-group-line-info (nth 1 info))
5202       (setq gnus-list-of-killed-groups 
5203             (cdr gnus-list-of-killed-groups)))
5204     (forward-line -1)
5205     (gnus-group-position-point)
5206     group))
5207       
5208 (defun gnus-group-list-all-groups (&optional arg)
5209   "List all newsgroups with level ARG or lower.
5210 Default is gnus-level-unsubscribed, which lists all subscribed and most
5211 unsubscribed groups."
5212   (interactive "P")
5213   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
5214
5215 (defun gnus-group-list-killed ()
5216   "List all killed newsgroups in the group buffer."
5217   (interactive)
5218   (if (not gnus-killed-list)
5219       (gnus-message 6 "No killed groups")
5220     (let (gnus-group-list-mode)
5221       (funcall gnus-group-prepare-function 
5222                gnus-level-killed t gnus-level-killed))
5223     (goto-char (point-min)))
5224   (gnus-group-position-point))
5225
5226 (defun gnus-group-list-zombies ()
5227   "List all zombie newsgroups in the group buffer."
5228   (interactive)
5229   (if (not gnus-zombie-list)
5230       (gnus-message 6 "No zombie groups")
5231     (let (gnus-group-list-mode)
5232       (funcall gnus-group-prepare-function
5233                gnus-level-zombie t gnus-level-zombie))
5234     (goto-char (point-min)))
5235   (gnus-group-position-point))
5236
5237 (defun gnus-group-list-active ()
5238   "List all groups that are available from the server(s)."
5239   (interactive)
5240   ;; First we make sure that we have really read the active file. 
5241   (or gnus-have-read-active-file
5242       (gnus-read-active-file))
5243   ;; Find all groups and sort them.
5244   (let ((groups 
5245          (sort 
5246           (let (list)
5247             (mapatoms
5248              (lambda (sym)
5249                (and (symbol-value sym)
5250                     (setq list (cons (symbol-name sym) list))))
5251              gnus-active-hashtb)
5252             list)
5253           'string<))
5254         (buffer-read-only nil))
5255     (erase-buffer)
5256     (while groups
5257       (gnus-group-insert-group-line-info (car groups))
5258       (setq groups (cdr groups)))
5259     (goto-char (point-min))))
5260
5261 (defun gnus-group-get-new-news (&optional arg)
5262   "Get newly arrived articles.
5263 If ARG is a number, it specifies which levels you are interested in
5264 re-scanning.  If ARG is non-nil and not a number, this will force
5265 \"hard\" re-reading of the active files from all servers."
5266   (interactive "P")
5267   (run-hooks 'gnus-get-new-news-hook)
5268   ;; We might read in new NoCeM messages here.
5269   (and gnus-use-nocem (gnus-nocem-scan-groups))
5270   ;; If ARG is not a number, then we read the active file.
5271   (and arg
5272        (not (numberp arg))
5273        (progn
5274          (let ((gnus-read-active-file t))
5275            (gnus-read-active-file))
5276          (setq arg nil)))
5277
5278   (setq arg (gnus-group-default-level arg t))
5279   (if (and gnus-read-active-file (not arg))
5280       (progn
5281         (gnus-read-active-file)
5282         (gnus-get-unread-articles (or arg (1+ gnus-level-subscribed))))
5283     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
5284       (gnus-get-unread-articles (or arg (1+ gnus-level-subscribed)))))
5285   (gnus-group-list-groups))
5286
5287 (defun gnus-group-get-new-news-this-group (&optional n)
5288   "Check for newly arrived news in the current group (and the N-1 next groups).
5289 The difference between N and the number of newsgroup checked is returned.
5290 If N is negative, this group and the N-1 previous groups will be checked."
5291   (interactive "P")
5292   (let* ((groups (gnus-group-process-prefix n))
5293          (ret (if (numberp n) (- n (length groups)) 0))
5294          group)
5295     (while groups
5296       (setq group (car groups)
5297             groups (cdr groups))
5298       (gnus-group-remove-mark group)
5299       (or (gnus-get-new-news-in-group group)
5300           (progn 
5301             (ding) 
5302             (message "%s error: %s" group (gnus-status-message group))
5303             (sit-for 2))))
5304     (gnus-group-next-unread-group 1 t)
5305     (gnus-summary-position-point)
5306     ret))
5307
5308 (defun gnus-get-new-news-in-group (group)
5309   (and group 
5310        (gnus-activate-group group 'scan)
5311        (progn
5312          (gnus-get-unread-articles-in-group 
5313           (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
5314           (gnus-gethash group gnus-active-hashtb))
5315          (gnus-group-update-group-line)
5316          t)))
5317
5318 (defun gnus-group-fetch-faq (group &optional faq-dir)
5319   "Fetch the FAQ for the current group."
5320   (interactive 
5321    (list
5322     (gnus-group-real-name (gnus-group-group-name))
5323     (cond (current-prefix-arg
5324            (completing-read 
5325             "Faq dir: " (and (listp gnus-group-faq-directory) 
5326                              gnus-group-faq-directory))))))
5327   (or faq-dir
5328       (setq faq-dir (if (listp gnus-group-faq-directory)
5329                         (car gnus-group-faq-directory)
5330                       gnus-group-faq-directory)))
5331   (or group (error "No group name given"))
5332   (let ((file (concat (file-name-as-directory faq-dir)
5333                       (gnus-group-real-name group))))
5334     (if (not (file-exists-p file))
5335         (error "No such file: %s" file)
5336       (find-file file))))
5337   
5338 (defun gnus-group-describe-group (force &optional group)
5339   "Display a description of the current newsgroup."
5340   (interactive (list current-prefix-arg (gnus-group-group-name)))
5341   (and force (setq gnus-description-hashtb nil))
5342   (let ((method (gnus-find-method-for-group group))
5343         desc)
5344     (or group (error "No group name given"))
5345     (and (or (and gnus-description-hashtb
5346                   ;; We check whether this group's method has been
5347                   ;; queried for a description file.  
5348                   (gnus-gethash 
5349                    (gnus-group-prefixed-name "" method) 
5350                    gnus-description-hashtb))
5351              (setq desc (gnus-group-get-description group))
5352              (gnus-read-descriptions-file method))
5353          (message
5354           (or desc (gnus-gethash group gnus-description-hashtb)
5355               "No description available")))))
5356
5357 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5358 (defun gnus-group-describe-all-groups (&optional force)
5359   "Pop up a buffer with descriptions of all newsgroups."
5360   (interactive "P")
5361   (and force (setq gnus-description-hashtb nil))
5362   (if (not (or gnus-description-hashtb
5363                (gnus-read-all-descriptions-files)))
5364       (error "Couldn't request descriptions file"))
5365   (let ((buffer-read-only nil)
5366         b)
5367     (erase-buffer)
5368     (mapatoms
5369      (lambda (group)
5370        (setq b (point))
5371        (insert (format "      *: %-20s %s\n" (symbol-name group)
5372                        (symbol-value group)))
5373        (add-text-properties 
5374         b (1+ b) (list 'gnus-group group
5375                        'gnus-unread t 'gnus-marked nil
5376                        'gnus-level (1+ gnus-level-subscribed))))
5377      gnus-description-hashtb)
5378     (goto-char (point-min))
5379     (gnus-group-position-point)))
5380
5381 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
5382 (defun gnus-group-apropos (regexp &optional search-description)
5383   "List all newsgroups that have names that match a regexp."
5384   (interactive "sGnus apropos (regexp): ")
5385   (let ((prev "")
5386         (obuf (current-buffer))
5387         groups des)
5388     ;; Go through all newsgroups that are known to Gnus.
5389     (mapatoms 
5390      (lambda (group)
5391        (and (symbol-name group)
5392             (string-match regexp (symbol-name group))
5393             (setq groups (cons (symbol-name group) groups))))
5394      gnus-active-hashtb)
5395     ;; Go through all descriptions that are known to Gnus. 
5396     (if search-description
5397         (mapatoms 
5398          (lambda (group)
5399            (and (string-match regexp (symbol-value group))
5400                 (gnus-gethash (symbol-name group) gnus-active-hashtb)
5401                 (setq groups (cons (symbol-name group) groups))))
5402          gnus-description-hashtb))
5403     (if (not groups)
5404         (gnus-message 3 "No groups matched \"%s\"." regexp)
5405       ;; Print out all the groups.
5406       (save-excursion
5407         (pop-to-buffer "*Gnus Help*")
5408         (buffer-disable-undo (current-buffer))
5409         (erase-buffer)
5410         (setq groups (sort groups 'string<))
5411         (while groups
5412           ;; Groups may be entered twice into the list of groups.
5413           (if (not (string= (car groups) prev))
5414               (progn
5415                 (insert (setq prev (car groups)) "\n")
5416                 (if (and gnus-description-hashtb
5417                          (setq des (gnus-gethash (car groups) 
5418                                                  gnus-description-hashtb)))
5419                     (insert "  " des "\n"))))
5420           (setq groups (cdr groups)))
5421         (goto-char (point-min))))
5422     (pop-to-buffer obuf)))
5423
5424 (defun gnus-group-description-apropos (regexp)
5425   "List all newsgroups that have names or descriptions that match a regexp."
5426   (interactive "sGnus description apropos (regexp): ")
5427   (if (not (or gnus-description-hashtb
5428                (gnus-read-all-descriptions-files)))
5429       (error "Couldn't request descriptions file"))
5430   (gnus-group-apropos regexp t))
5431
5432 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5433 (defun gnus-group-list-matching (level regexp &optional all lowest) 
5434   "List all groups with unread articles that match REGEXP.
5435 If the prefix LEVEL is non-nil, it should be a number that says which
5436 level to cut off listing groups. 
5437 If ALL, also list groups with no unread articles.
5438 If LOWEST, don't list groups with level lower than LOWEST."
5439   (interactive "P\nsList newsgroups matching: ")
5440   (gnus-group-prepare-flat (or level gnus-level-subscribed)
5441                            all (or lowest 1) regexp)
5442   (goto-char (point-min))
5443   (gnus-group-position-point))
5444
5445 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
5446   "List all groups that match REGEXP.
5447 If the prefix LEVEL is non-nil, it should be a number that says which
5448 level to cut off listing groups. 
5449 If LOWEST, don't list groups with level lower than LOWEST."
5450   (interactive "P\nsList newsgroups matching: ")
5451   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
5452
5453 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
5454 (defun gnus-group-save-newsrc ()
5455   "Save the Gnus startup files."
5456   (interactive)
5457   (gnus-save-newsrc-file))
5458
5459 (defun gnus-group-restart (&optional arg)
5460   "Force Gnus to read the .newsrc file."
5461   (interactive "P")
5462   (gnus-save-newsrc-file)
5463   (gnus-setup-news 'force)
5464   (gnus-group-list-groups arg))
5465
5466 (defun gnus-group-read-init-file ()
5467   "Read the Gnus elisp init file."
5468   (interactive)
5469   (gnus-read-init-file))
5470
5471 (defun gnus-group-check-bogus-groups (&optional silent)
5472   "Check bogus newsgroups.
5473 If given a prefix, don't ask for confirmation before removing a bogus
5474 group."
5475   (interactive "P")
5476   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
5477   (gnus-group-list-groups))
5478
5479 (defun gnus-group-edit-global-kill (&optional article group)
5480   "Edit the global kill file.
5481 If GROUP, edit that local kill file instead."
5482   (interactive "P")
5483   (setq gnus-current-kill-article article)
5484   (gnus-kill-file-edit-file group)
5485   (gnus-message 
5486    6
5487    (substitute-command-keys
5488     "Editing a global kill file (Type \\[gnus-kill-file-exit] to exit)")))
5489
5490 (defun gnus-group-edit-local-kill (article group)
5491   "Edit a local kill file."
5492   (interactive (list nil (gnus-group-group-name)))
5493   (gnus-group-edit-global-kill article group))
5494
5495 (defun gnus-group-force-update ()
5496   "Update `.newsrc' file."
5497   (interactive)
5498   (gnus-save-newsrc-file))
5499
5500 (defun gnus-group-suspend ()
5501   "Suspend the current Gnus session.
5502 In fact, cleanup buffers except for group mode buffer.
5503 The hook gnus-suspend-gnus-hook is called before actually suspending."
5504   (interactive)
5505   (run-hooks 'gnus-suspend-gnus-hook)
5506   ;; Kill Gnus buffers except for group mode buffer.
5507   (let ((group-buf (get-buffer gnus-group-buffer)))
5508     ;; Do this on a separate list in case the user does a ^G before we finish
5509     (let ((gnus-buffer-list
5510            (delq group-buf (delq gnus-dribble-buffer
5511                                  (append gnus-buffer-list nil)))))
5512       (while gnus-buffer-list
5513         (gnus-kill-buffer (car gnus-buffer-list))
5514         (setq gnus-buffer-list (cdr gnus-buffer-list))))
5515     (if group-buf
5516         (progn
5517           (setq gnus-buffer-list (list group-buf))
5518           (bury-buffer group-buf)
5519           (delete-windows-on group-buf t)))))
5520
5521 (defun gnus-group-clear-dribble ()
5522   "Clear all information from the dribble buffer."
5523   (interactive)
5524   (gnus-dribble-clear))
5525
5526 (defun gnus-group-exit ()
5527   "Quit reading news after updating .newsrc.eld and .newsrc.
5528 The hook `gnus-exit-gnus-hook' is called before actually exiting."
5529   (interactive)
5530   (if (or noninteractive                ;For gnus-batch-kill
5531           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
5532           (not gnus-interactive-exit)   ;Without confirmation
5533           gnus-expert-user
5534           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
5535       (progn
5536         (run-hooks 'gnus-exit-gnus-hook)
5537         ;; Offer to save data from non-quitted summary buffers.
5538         (gnus-offer-save-summaries)
5539         ;; Save the newsrc file(s).
5540         (gnus-save-newsrc-file)
5541         ;; Kill-em-all.
5542         (gnus-close-backends)
5543         ;; Reset everything.
5544         (gnus-clear-system))))
5545
5546 (defun gnus-close-backends ()
5547   ;; Send a close request to all backends that support such a request. 
5548   (let ((methods gnus-valid-select-methods)
5549         func)
5550     (while methods
5551       (if (fboundp (setq func (intern (concat (car (car methods))
5552                                               "-request-close"))))
5553           (funcall func))
5554       (setq methods (cdr methods)))))
5555
5556 (defun gnus-group-quit ()
5557   "Quit reading news without updating .newsrc.eld or .newsrc.
5558 The hook `gnus-exit-gnus-hook' is called before actually exiting."
5559   (interactive)
5560   (if (or noninteractive                ;For gnus-batch-kill
5561           (zerop (buffer-size))
5562           (not (gnus-server-opened gnus-select-method))
5563           gnus-expert-user
5564           (not gnus-current-startup-file)
5565           (gnus-yes-or-no-p
5566            (format "Quit reading news without saving %s? "
5567                    (file-name-nondirectory gnus-current-startup-file))))
5568       (progn
5569         (run-hooks 'gnus-exit-gnus-hook)
5570         (if gnus-use-full-window
5571             (delete-other-windows)
5572           (gnus-remove-some-windows))
5573         (gnus-dribble-save)
5574         (gnus-close-backends)
5575         (gnus-clear-system))))
5576
5577 (defun gnus-offer-save-summaries ()
5578   (save-excursion
5579     (let ((buflist (buffer-list)) 
5580           buffers bufname)
5581       (while buflist
5582         (and (setq bufname (buffer-name (car buflist)))
5583              (string-match "Summary" bufname)
5584              (save-excursion
5585                (set-buffer bufname)
5586                ;; We check that this is, indeed, a summary buffer.
5587                (eq major-mode 'gnus-summary-mode))
5588              (setq buffers (cons bufname buffers)))
5589         (setq buflist (cdr buflist)))
5590       (and buffers
5591            (map-y-or-n-p 
5592             "Update summary buffer %s? "
5593             (lambda (buf)
5594               (set-buffer buf)
5595               (gnus-summary-exit))
5596             buffers)))))
5597
5598 (defun gnus-group-describe-briefly ()
5599   "Give a one line description of the group mode commands."
5600   (interactive)
5601   (gnus-message 7 (substitute-command-keys "\\<gnus-group-mode-map>\\[gnus-group-read-group]:Select  \\[gnus-group-next-unread-group]:Forward  \\[gnus-group-prev-unread-group]:Backward  \\[gnus-group-exit]:Exit  \\[gnus-info-find-node]:Run Info  \\[gnus-group-describe-briefly]:This help")))
5602
5603 (defun gnus-group-browse-foreign-server (method)
5604   "Browse a foreign news server.
5605 If called interactively, this function will ask for a select method
5606  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
5607 If not, METHOD should be a list where the first element is the method
5608 and the second element is the address."
5609   (interactive
5610    (list (let ((how (completing-read 
5611                      "Which backend: "
5612                      (append gnus-valid-select-methods gnus-server-alist)
5613                      nil t "nntp")))
5614            ;; We either got a backend name or a virtual server name.
5615            ;; If the first, we also need an address.
5616            (if (assoc how gnus-valid-select-methods)
5617                (list (intern how)
5618                      ;; Suggested by mapjph@bath.ac.uk.
5619                      (completing-read 
5620                       "Address: " 
5621                       (mapcar (lambda (server) (list server))
5622                               gnus-secondary-servers)))
5623              ;; We got a server name, so we find the method.
5624              (gnus-server-to-method how)))))
5625   (gnus-browse-foreign-server method))
5626
5627 \f
5628 ;;;
5629 ;;; Browse Server Mode
5630 ;;;
5631
5632 (defvar gnus-browse-mode-hook nil)
5633 (defvar gnus-browse-mode-map nil)
5634 (put 'gnus-browse-mode 'mode-class 'special)
5635
5636 (if gnus-browse-mode-map
5637     nil
5638   (setq gnus-browse-mode-map (make-keymap))
5639   (suppress-keymap gnus-browse-mode-map)
5640   (define-key gnus-browse-mode-map " " 'gnus-browse-read-group)
5641   (define-key gnus-browse-mode-map "=" 'gnus-browse-select-group)
5642   (define-key gnus-browse-mode-map "n" 'gnus-browse-next-group)
5643   (define-key gnus-browse-mode-map "p" 'gnus-browse-prev-group)
5644   (define-key gnus-browse-mode-map "\177" 'gnus-browse-prev-group)
5645   (define-key gnus-browse-mode-map "N" 'gnus-browse-next-group)
5646   (define-key gnus-browse-mode-map "P" 'gnus-browse-prev-group)
5647   (define-key gnus-browse-mode-map "\M-n" 'gnus-browse-next-group)
5648   (define-key gnus-browse-mode-map "\M-p" 'gnus-browse-prev-group)
5649   (define-key gnus-browse-mode-map "\r" 'gnus-browse-select-group)
5650   (define-key gnus-browse-mode-map "u" 'gnus-browse-unsubscribe-current-group)
5651   (define-key gnus-browse-mode-map "l" 'gnus-browse-exit)
5652   (define-key gnus-browse-mode-map "L" 'gnus-browse-exit)
5653   (define-key gnus-browse-mode-map "q" 'gnus-browse-exit)
5654   (define-key gnus-browse-mode-map "Q" 'gnus-browse-exit)
5655   (define-key gnus-browse-mode-map "\C-c\C-c" 'gnus-browse-exit)
5656   (define-key gnus-browse-mode-map "?" 'gnus-browse-describe-briefly)
5657   (define-key gnus-browse-mode-map "\C-c\C-i" 'gnus-info-find-node)
5658   )
5659
5660 (defvar gnus-browse-current-method nil)
5661 (defvar gnus-browse-return-buffer nil)
5662
5663 (defvar gnus-browse-buffer "*Gnus Browse Server*")
5664
5665 (defun gnus-browse-foreign-server (method &optional return-buffer)
5666   (setq gnus-browse-current-method method)
5667   (setq gnus-browse-return-buffer return-buffer)
5668   (let ((gnus-select-method method)
5669         groups group)
5670     (gnus-message 5 "Connecting to %s..." (nth 1 method))
5671     (or (gnus-check-server method)
5672         (error "Unable to contact server: %s" (gnus-status-message method)))
5673     (or (gnus-request-list method)
5674         (error "Couldn't request list: %s" (gnus-status-message method)))
5675     (get-buffer-create gnus-browse-buffer)
5676     (gnus-add-current-to-buffer-list)
5677     (and gnus-carpal (gnus-carpal-setup-buffer 'browse))
5678     (gnus-configure-windows 'browse)
5679     (buffer-disable-undo (current-buffer))
5680     (let ((buffer-read-only nil))
5681       (erase-buffer))
5682     (gnus-browse-mode)
5683     (setq mode-line-buffer-identification
5684           (format
5685            "Gnus  Browse Server {%s:%s}" (car method) (car (cdr method))))
5686     (save-excursion
5687       (set-buffer nntp-server-buffer)
5688       (let ((cur (current-buffer)))
5689         (goto-char (point-min))
5690         (or (string= gnus-ignored-newsgroups "")
5691             (delete-matching-lines gnus-ignored-newsgroups))
5692         (while (re-search-forward 
5693                 "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
5694           (goto-char (match-end 1))
5695           (setq groups (cons (cons (buffer-substring (match-beginning 1)
5696                                                      (match-end 1))
5697                                    (max 0 (- (1+ (read cur)) (read cur))))
5698                              groups)))))
5699     (setq groups (sort groups 
5700                        (lambda (l1 l2)
5701                          (string< (car l1) (car l2)))))
5702     (let ((buffer-read-only nil))
5703       (while groups
5704         (setq group (car groups))
5705         (insert 
5706          (format "K%7d: %s\n" (cdr group) (car group)))
5707         (setq groups (cdr groups))))
5708     (switch-to-buffer (current-buffer))
5709     (goto-char (point-min))
5710     (gnus-group-position-point)))
5711
5712 (defun gnus-browse-mode ()
5713   "Major mode for browsing a foreign server.
5714
5715 All normal editing commands are switched off.
5716
5717 \\<gnus-browse-mode-map>
5718 The only things you can do in this buffer is
5719
5720 1) `\\[gnus-browse-unsubscribe-current-group]' to subscribe to a group.
5721 The group will be inserted into the group buffer upon exit from this
5722 buffer.  
5723
5724 2) `\\[gnus-browse-read-group]' to read a group ephemerally.
5725
5726 3) `\\[gnus-browse-exit]' to return to the group buffer."
5727   (interactive)
5728   (kill-all-local-variables)
5729   (if (gnus-visual-p 'browse-menu 'menu) (gnus-browse-make-menu-bar))
5730   (setq mode-line-modified "-- ")
5731   (make-local-variable 'mode-line-format)
5732   (setq mode-line-format (copy-sequence mode-line-format))
5733   (and (equal (nth 3 mode-line-format) "   ")
5734        (setcar (nthcdr 3 mode-line-format) ""))
5735   (setq major-mode 'gnus-browse-mode)
5736   (setq mode-name "Browse Server")
5737   (setq mode-line-process nil)
5738   (use-local-map gnus-browse-mode-map)
5739   (buffer-disable-undo (current-buffer))
5740   (setq truncate-lines t)
5741   (setq buffer-read-only t)
5742   (run-hooks 'gnus-browse-mode-hook))
5743
5744 (defun gnus-browse-read-group (&optional no-article)
5745   "Enter the group at the current line."
5746   (interactive)
5747   (let ((group (gnus-browse-group-name)))
5748     (or (gnus-group-read-ephemeral-group 
5749          group gnus-browse-current-method nil
5750          (cons (current-buffer) 'browse))
5751         (error "Couldn't enter %s" group))))
5752
5753 (defun gnus-browse-select-group ()
5754   "Select the current group."
5755   (interactive)
5756   (gnus-browse-read-group 'no))
5757
5758 (defun gnus-browse-next-group (n)
5759   "Go to the next group."
5760   (interactive "p")
5761   (prog1
5762       (forward-line n)
5763     (gnus-group-position-point)))
5764
5765 (defun gnus-browse-prev-group (n)
5766   "Go to the next group."
5767   (interactive "p")
5768   (gnus-browse-next-group (- n)))
5769
5770 (defun gnus-browse-unsubscribe-current-group (arg)
5771   "(Un)subscribe to the next ARG groups."
5772   (interactive "p")
5773   (and (eobp)
5774        (error "No group at current line."))
5775   (let ((ward (if (< arg 0) -1 1))
5776         (arg (abs arg)))
5777     (while (and (> arg 0)
5778                 (not (eobp))
5779                 (gnus-browse-unsubscribe-group)
5780                 (zerop (gnus-browse-next-group ward)))
5781       (setq arg (1- arg)))
5782     (gnus-group-position-point)
5783     (if (/= 0 arg) (gnus-message 7 "No more newsgroups"))
5784     arg))
5785
5786 (defun gnus-browse-group-name ()
5787   (save-excursion
5788     (beginning-of-line)
5789     (if (not (re-search-forward ": \\(.*\\)$" (gnus-point-at-eol) t))
5790         ()
5791       (gnus-group-prefixed-name 
5792        (buffer-substring (match-beginning 1) (match-end 1))
5793        gnus-browse-current-method))))
5794   
5795 (defun gnus-browse-unsubscribe-group ()
5796   (let ((sub nil)
5797         (buffer-read-only nil)
5798         group)
5799     (save-excursion
5800       (beginning-of-line)
5801       (if (= (following-char) ?K) (setq sub t))
5802       (setq group (gnus-browse-group-name))
5803       (beginning-of-line)
5804       (delete-char 1)
5805       (if sub
5806           (progn
5807             (gnus-group-change-level 
5808              (list t group gnus-level-default-subscribed
5809                    nil nil gnus-browse-current-method) 
5810              gnus-level-default-subscribed gnus-level-killed
5811              (and (car (nth 1 gnus-newsrc-alist))
5812                   (gnus-gethash (car (nth 1 gnus-newsrc-alist))
5813                                 gnus-newsrc-hashtb))
5814              t)
5815             (insert ? ))
5816         (gnus-group-change-level 
5817          group gnus-level-killed gnus-level-default-subscribed)
5818         (insert ?K)))
5819     t))
5820
5821 (defun gnus-browse-exit ()
5822   "Quit browsing and return to the group buffer."
5823   (interactive)
5824   (if (eq major-mode 'gnus-browse-mode)
5825       (kill-buffer (current-buffer)))
5826   (if gnus-browse-return-buffer
5827       (gnus-configure-windows 'server 'force)
5828     (gnus-configure-windows 'group 'force)
5829     (gnus-group-list-groups nil)))
5830
5831 (defun gnus-browse-describe-briefly ()
5832   "Give a one line description of the group mode commands."
5833   (interactive)
5834   (gnus-message 6
5835                 (substitute-command-keys "\\<gnus-browse-mode-map>\\[gnus-group-next-group]:Forward  \\[gnus-group-prev-group]:Backward  \\[gnus-browse-exit]:Exit  \\[gnus-info-find-node]:Run Info  \\[gnus-browse-describe-briefly]:This help")))
5836       
5837 \f
5838 ;;;
5839 ;;; Gnus summary mode
5840 ;;;
5841
5842 (defvar gnus-summary-mode-map nil)
5843 (defvar gnus-summary-mark-map nil)
5844 (defvar gnus-summary-mscore-map nil)
5845 (defvar gnus-summary-article-map nil)
5846 (defvar gnus-summary-thread-map nil)
5847 (defvar gnus-summary-goto-map nil)
5848 (defvar gnus-summary-exit-map nil)
5849 (defvar gnus-summary-interest-map nil)
5850 (defvar gnus-summary-sort-map nil)
5851 (defvar gnus-summary-backend-map nil)
5852 (defvar gnus-summary-save-map nil)
5853 (defvar gnus-summary-wash-map nil)
5854 (defvar gnus-summary-wash-hide-map nil)
5855 (defvar gnus-summary-wash-highlight-map nil)
5856 (defvar gnus-summary-wash-time-map nil)
5857 (defvar gnus-summary-help-map nil)
5858 (defvar gnus-summary-limit-map nil)
5859
5860 (put 'gnus-summary-mode 'mode-class 'special)
5861
5862 (if gnus-summary-mode-map
5863     nil
5864   (setq gnus-summary-mode-map (make-keymap))
5865   (suppress-keymap gnus-summary-mode-map)
5866
5867   ;; Non-orthogonal keys
5868
5869   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
5870   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
5871   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
5872   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
5873   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
5874   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
5875   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
5876   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
5877   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
5878   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
5879   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
5880   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
5881   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
5882   (define-key gnus-summary-mode-map 
5883     "\M-s" 'gnus-summary-search-article-forward)
5884   (define-key gnus-summary-mode-map 
5885     "\M-r" 'gnus-summary-search-article-backward)
5886   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
5887   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
5888   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-article)
5889   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
5890   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
5891   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
5892   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
5893   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
5894   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
5895   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
5896   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
5897   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
5898   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
5899   (define-key gnus-summary-mode-map 
5900     "k" 'gnus-summary-kill-same-subject-and-select)
5901   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
5902   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
5903   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
5904   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
5905   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
5906   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
5907   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
5908   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
5909   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
5910   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
5911   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
5912   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
5913   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
5914   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
5915   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
5916   (define-key gnus-summary-mode-map "\C-w" 'gnus-summary-mark-region-as-read)
5917   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
5918   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
5919   (define-key gnus-summary-mode-map 
5920     "\C-c\M-\C-s" 'gnus-summary-show-all-expunged)
5921   (define-key gnus-summary-mode-map 
5922     "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
5923   (define-key gnus-summary-mode-map 
5924     "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
5925   (define-key gnus-summary-mode-map 
5926     "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
5927   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
5928   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
5929   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
5930   (define-key gnus-summary-mode-map 
5931     "\C-x\C-s" 'gnus-summary-reselect-current-group)
5932   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
5933   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
5934   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
5935   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
5936   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
5937   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
5938   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
5939   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
5940   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
5941   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
5942   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
5943   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
5944   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
5945   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
5946   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
5947   (define-key gnus-summary-mode-map "V" 'gnus-version)
5948   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
5949   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
5950   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
5951   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
5952   (define-key gnus-summary-mode-map gnus-mouse-2 'gnus-mouse-pick-article)
5953   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
5954   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
5955   (define-key gnus-summary-mode-map "x" 'gnus-summary-limit-to-unread)
5956   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
5957   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
5958   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
5959 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
5960   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
5961   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu-view)
5962   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
5963   (define-key gnus-summary-mode-map "v" 'gnus-summary-verbose-headers)
5964   (define-key gnus-summary-mode-map "\C-c\C-b" 'gnus-bug)
5965   (define-key gnus-summary-mode-map "/" 'gnus-summary-limit-to-subject)
5966
5967
5968   ;; Sort of orthogonal keymap
5969   (define-prefix-command 'gnus-summary-mark-map)
5970   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
5971   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
5972   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
5973   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
5974   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
5975   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
5976   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
5977   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
5978   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
5979   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
5980   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
5981   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
5982   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
5983   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
5984   (define-key gnus-summary-mark-map "S" 'gnus-summary-show-all-expunged)
5985   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
5986   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
5987   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
5988   (define-key gnus-summary-mark-map 
5989     "k" 'gnus-summary-kill-same-subject-and-select)
5990   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
5991
5992   (define-prefix-command 'gnus-summary-mscore-map)
5993   (define-key gnus-summary-mark-map "V" 'gnus-summary-mscore-map)
5994   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
5995   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
5996   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
5997   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
5998
5999   (define-key gnus-summary-mark-map "P" 'gnus-uu-mark-map)
6000   
6001   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
6002
6003   (define-prefix-command 'gnus-summary-limit-map)
6004   (define-key gnus-summary-mark-map "N" 'gnus-summary-limit-map)
6005   (define-key gnus-summary-limit-map "n" 'gnus-summary-limit-to-articles)
6006   (define-key gnus-summary-limit-map "w" 'gnus-summary-pop-limit)
6007   (define-key gnus-summary-limit-map "s" 'gnus-summary-limit-to-subject)
6008   (define-key gnus-summary-limit-map "u" 'gnus-summary-limit-to-unread)
6009   (define-key gnus-summary-limit-map "m" 'gnus-summary-limit-to-marks)
6010   (define-key gnus-summary-limit-map "v" 'gnus-summary-limit-to-score)
6011   (define-key gnus-summary-limit-map "D" 'gnus-summary-limit-include-dormant)
6012   (define-key gnus-summary-limit-map "d" 'gnus-summary-limit-exclude-dormant)
6013   (define-key gnus-summary-limit-map "c" 
6014     'gnus-summary-limit-exclude-childless-dormant)
6015
6016   (define-prefix-command 'gnus-summary-goto-map)
6017   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
6018   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
6019   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
6020   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
6021   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
6022   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
6023   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
6024   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
6025   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
6026   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
6027   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
6028   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
6029   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
6030   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
6031
6032
6033   (define-prefix-command 'gnus-summary-thread-map)
6034   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
6035   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
6036   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
6037   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
6038   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
6039   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
6040   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
6041   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
6042   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
6043   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
6044   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
6045   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
6046   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
6047   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
6048   (define-key gnus-summary-thread-map "\M-#" 'gnus-uu-unmark-thread)
6049
6050   
6051   (define-prefix-command 'gnus-summary-exit-map)
6052   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
6053   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
6054   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
6055   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
6056   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
6057   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
6058   (define-key gnus-summary-exit-map 
6059     "n" 'gnus-summary-catchup-and-goto-next-group)
6060   (define-key gnus-summary-exit-map "R" 'gnus-summary-reselect-current-group)
6061   (define-key gnus-summary-exit-map "G" 'gnus-summary-rescan-group)
6062   (define-key gnus-summary-exit-map "N" 'gnus-summary-next-group)
6063   (define-key gnus-summary-exit-map "P" 'gnus-summary-prev-group)
6064
6065
6066   (define-prefix-command 'gnus-summary-article-map)
6067   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
6068   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
6069   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
6070   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
6071   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
6072   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
6073   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
6074   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
6075   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
6076   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
6077   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
6078   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
6079   (define-key gnus-summary-article-map "R" 'gnus-summary-refer-references)
6080   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
6081   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
6082
6083
6084
6085   (define-prefix-command 'gnus-summary-wash-map)
6086   (define-key gnus-summary-mode-map "W" 'gnus-summary-wash-map)
6087
6088   (define-prefix-command 'gnus-summary-wash-hide-map)
6089   (define-key gnus-summary-wash-map "W" 'gnus-summary-wash-hide-map)
6090   (define-key gnus-summary-wash-hide-map "a" 'gnus-article-hide)
6091   (define-key gnus-summary-wash-hide-map "h" 'gnus-article-hide-headers)
6092   (define-key gnus-summary-wash-hide-map "s" 'gnus-article-hide-signature)
6093   (define-key gnus-summary-wash-hide-map "c" 'gnus-article-hide-citation)
6094   (define-key gnus-summary-wash-hide-map "p" 'gnus-article-hide-pgp)
6095   (define-key gnus-summary-wash-hide-map 
6096     "\C-c" 'gnus-article-hide-citation-maybe)
6097
6098   (define-prefix-command 'gnus-summary-wash-highlight-map)
6099   (define-key gnus-summary-wash-map "H" 'gnus-summary-wash-highlight-map)
6100   (define-key gnus-summary-wash-highlight-map "a" 'gnus-article-highlight)
6101   (define-key gnus-summary-wash-highlight-map 
6102     "h" 'gnus-article-highlight-headers)
6103   (define-key gnus-summary-wash-highlight-map
6104     "c" 'gnus-article-highlight-citation)
6105   (define-key gnus-summary-wash-highlight-map
6106     "s" 'gnus-article-highlight-signature)
6107
6108   (define-prefix-command 'gnus-summary-wash-time-map)
6109   (define-key gnus-summary-wash-map "T" 'gnus-summary-wash-time-map)
6110   (define-key gnus-summary-wash-time-map "z" 'gnus-article-date-ut)
6111   (define-key gnus-summary-wash-time-map "u" 'gnus-article-date-ut)
6112   (define-key gnus-summary-wash-time-map "l" 'gnus-article-date-local)
6113   (define-key gnus-summary-wash-time-map "e" 'gnus-article-date-lapsed)
6114   (define-key gnus-summary-wash-time-map "o" 'gnus-article-date-original)
6115
6116   (define-key gnus-summary-wash-map "b" 'gnus-article-add-buttons)
6117   (define-key gnus-summary-wash-map "B" 'gnus-article-add-buttons-to-head)
6118   (define-key gnus-summary-wash-map "o" 'gnus-article-treat-overstrike)
6119   (define-key gnus-summary-wash-map "w" 'gnus-article-word-wrap)
6120   (define-key gnus-summary-wash-map "c" 'gnus-article-remove-cr)
6121   (define-key gnus-summary-wash-map "q" 'gnus-article-de-quoted-unreadable)
6122   (define-key gnus-summary-wash-map "f" 'gnus-article-display-x-face)
6123   (define-key gnus-summary-wash-map "l" 'gnus-summary-stop-page-breaking)
6124   (define-key gnus-summary-wash-map "r" 'gnus-summary-caesar-message)
6125   (define-key gnus-summary-wash-map "t" 'gnus-summary-toggle-header)
6126   (define-key gnus-summary-wash-map "m" 'gnus-summary-toggle-mime)
6127
6128
6129   (define-prefix-command 'gnus-summary-help-map)
6130   (define-key gnus-summary-mode-map "H" 'gnus-summary-help-map)
6131   (define-key gnus-summary-help-map "v" 'gnus-version)
6132   (define-key gnus-summary-help-map "f" 'gnus-summary-fetch-faq)
6133   (define-key gnus-summary-help-map "d" 'gnus-summary-describe-group)
6134   (define-key gnus-summary-help-map "h" 'gnus-summary-describe-briefly)
6135   (define-key gnus-summary-help-map "i" 'gnus-info-find-node)
6136
6137
6138   (define-prefix-command 'gnus-summary-backend-map)
6139   (define-key gnus-summary-mode-map "B" 'gnus-summary-backend-map)
6140   (define-key gnus-summary-backend-map "e" 'gnus-summary-expire-articles)
6141   (define-key gnus-summary-backend-map "\M-\C-e" 
6142     'gnus-summary-expire-articles-now)
6143   (define-key gnus-summary-backend-map "\177" 'gnus-summary-delete-article)
6144   (define-key gnus-summary-backend-map "m" 'gnus-summary-move-article)
6145   (define-key gnus-summary-backend-map "r" 'gnus-summary-respool-article)
6146   (define-key gnus-summary-backend-map "w" 'gnus-summary-edit-article)
6147   (define-key gnus-summary-backend-map "c" 'gnus-summary-copy-article)
6148   (define-key gnus-summary-backend-map "q" 'gnus-summary-respool-query)
6149   (define-key gnus-summary-backend-map "i" 'gnus-summary-import-article)
6150
6151
6152   (define-prefix-command 'gnus-summary-save-map)
6153   (define-key gnus-summary-mode-map "O" 'gnus-summary-save-map)
6154   (define-key gnus-summary-save-map "o" 'gnus-summary-save-article)
6155   (define-key gnus-summary-save-map "m" 'gnus-summary-save-article-mail)
6156   (define-key gnus-summary-save-map "r" 'gnus-summary-save-article-rmail)
6157   (define-key gnus-summary-save-map "f" 'gnus-summary-save-article-file)
6158   (define-key gnus-summary-save-map "b" 'gnus-summary-save-article-body-file)
6159   (define-key gnus-summary-save-map "h" 'gnus-summary-save-article-folder)
6160   (define-key gnus-summary-save-map "v" 'gnus-summary-save-article-vm)
6161   (define-key gnus-summary-save-map "p" 'gnus-summary-pipe-output)
6162   (define-key gnus-summary-save-map "s" 'gnus-soup-add-article)
6163
6164   (define-key gnus-summary-mode-map "X" 'gnus-uu-extract-map)
6165
6166   (define-key gnus-summary-mode-map "\M-&" 'gnus-summary-universal-argument)
6167   (define-key gnus-summary-article-map "D" 'gnus-summary-enter-digest-group)
6168
6169   (define-key gnus-summary-mode-map "V" 'gnus-summary-score-map)
6170
6171   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-score)
6172   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-score)
6173   )
6174
6175
6176 \f
6177
6178 (defun gnus-summary-mode (&optional group)
6179   "Major mode for reading articles.
6180
6181 All normal editing commands are switched off.
6182 \\<gnus-summary-mode-map>
6183 Each line in this buffer represents one article.  To read an
6184 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
6185 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
6186 respectively.
6187
6188 You can also post articles and send mail from this buffer.  To 
6189 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
6190 of an article, type `\\[gnus-summary-reply]'.
6191
6192 There are approx. one gazillion commands you can execute in this 
6193 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
6194
6195 The following commands are available:
6196
6197 \\{gnus-summary-mode-map}"
6198   (interactive)
6199   (if (gnus-visual-p 'summary-menu 'menu) (gnus-summary-make-menu-bar))
6200   (kill-all-local-variables)
6201   (let ((locals gnus-summary-local-variables))
6202     (while locals
6203       (if (consp (car locals))
6204           (progn
6205             (make-local-variable (car (car locals)))
6206             (set (car (car locals)) (eval (cdr (car locals)))))
6207         (make-local-variable (car locals))
6208         (set (car locals) nil))
6209       (setq locals (cdr locals))))
6210   (gnus-make-thread-indent-array)
6211   (setq mode-line-modified "-- ")
6212   (make-local-variable 'mode-line-format)
6213   (setq mode-line-format (copy-sequence mode-line-format))
6214   (and (equal (nth 3 mode-line-format) "   ")
6215        (setcar (nthcdr 3 mode-line-format) ""))
6216   (setq major-mode 'gnus-summary-mode)
6217   (setq mode-name "Summary")
6218   (make-local-variable 'minor-mode-alist)
6219   (use-local-map gnus-summary-mode-map)
6220   (buffer-disable-undo (current-buffer))
6221   (setq buffer-read-only t)             ;Disable modification
6222   (setq truncate-lines t)
6223   (setq selective-display t)
6224   (setq selective-display-ellipses t)   ;Display `...'
6225   (setq buffer-display-table gnus-summary-display-table)
6226   (setq gnus-newsgroup-name group)
6227   (run-hooks 'gnus-summary-mode-hook))
6228
6229 (defun gnus-summary-make-display-table ()
6230   ;; Change the display table.  Odd characters have a tendency to mess
6231   ;; up nicely formatted displays - we make all possible glyphs
6232   ;; display only a single character.
6233
6234   ;; We start from the standard display table, if any.
6235   (setq gnus-summary-display-table 
6236         (or (copy-sequence standard-display-table)
6237             (make-display-table)))
6238   ;; Nix out all the control chars...
6239   (let ((i 32))
6240     (while (>= (setq i (1- i)) 0)
6241       (aset gnus-summary-display-table i [??])))
6242   ;; ... but not newline and cr, of course. (cr is necessary for the
6243   ;; selective display).  
6244   (aset gnus-summary-display-table ?\n nil)
6245   (aset gnus-summary-display-table ?\r nil)
6246   ;; We nix out any glyphs over 126 that are not set already.  
6247   (let ((i 256))
6248     (while (>= (setq i (1- i)) 127)
6249       ;; Only modify if the entry is nil.
6250       (or (aref gnus-summary-display-table i) 
6251           (aset gnus-summary-display-table i [??])))))
6252
6253 (defun gnus-summary-clear-local-variables ()
6254   (let ((locals gnus-summary-local-variables))
6255     (while locals
6256       (if (consp (car locals))
6257           (and (vectorp (car (car locals)))
6258                (set (car (car locals)) nil))
6259         (and (vectorp (car locals))
6260              (set (car locals) nil)))
6261       (setq locals (cdr locals)))))
6262
6263 ;; Summary data functions.
6264
6265 (defmacro gnus-data-number (data)
6266   (` (car (, data))))
6267
6268 (defmacro gnus-data-mark (data)
6269   (` (nth 1 (, data))))
6270
6271 (defmacro gnus-data-set-mark (data mark)
6272   (` (setcar (nthcdr 1 (, data)) (, mark))))
6273
6274 (defmacro gnus-data-pos (data)
6275   (` (nth 2 (, data))))
6276
6277 (defmacro gnus-data-header (data)
6278   (` (nth 3 (, data))))
6279
6280 (defmacro gnus-data-level (data)
6281   (` (nth 4 (, data))))
6282
6283 (defmacro gnus-data-unread-p (data)
6284   (` (= (nth 1 (, data)) gnus-unread-mark)))
6285
6286 (defmacro gnus-data-pseudo-p (data)
6287   (` (vectorp (nth 3 (, data)))))
6288
6289 (defmacro gnus-data-find (number)
6290   (` (assq (, number) gnus-newsgroup-data)))
6291
6292 (defmacro gnus-data-find-list (number &optional data)
6293   (` (memq (assq (, number) (, (or data 'gnus-newsgroup-data)))
6294            (, (or data 'gnus-newsgroup-data)))))
6295
6296 (defmacro gnus-data-make (number mark pos header level)
6297   (` (list (, number) (, mark) (, pos) (, header) (, level))))
6298
6299 (defun gnus-data-enter (after-article number mark pos header level offset)
6300   (let ((data (gnus-data-find-list after-article)))
6301     (or data (error "No such article: %d" after-article))
6302     (setcdr data (cons (gnus-data-make number mark pos header level)
6303                        (cdr data)))
6304     (setq gnus-newsgroup-data-reverse nil)
6305     (gnus-data-update-list (cdr (cdr data)) offset)))
6306
6307 (defun gnus-data-enter-list (after-article list offset)
6308   (when list
6309     (let ((data (and after-article (gnus-data-find-list after-article)))
6310           (ilist list))
6311       (or data (not after-article) (error "No such article: %d" after-article))
6312       ;; Find the last element in the list to be spliced into the main
6313       ;; list.  
6314       (while (cdr list)
6315         (setq list (cdr list)))
6316       (if (not data)
6317           (progn
6318             (setcdr list gnus-newsgroup-data)
6319             (setq gnus-newsgroup-data ilist)
6320             (gnus-data-update-list (cdr list) offset))
6321         (setcdr list (cdr data))
6322         (setcdr data ilist)
6323         (gnus-data-update-list (cdr data) offset))
6324       (setq gnus-newsgroup-data-reverse nil))))
6325
6326 (defun gnus-data-remove (article offset)
6327   (let ((data gnus-newsgroup-data))
6328     (if (= (gnus-data-number (car data)) article)
6329         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
6330               gnus-newsgroup-data-reverse nil)
6331       (while (cdr data)
6332         (and (= (gnus-data-number (car (cdr data))) article)
6333              (progn
6334                (setcdr data (cdr (cdr data)))
6335                (gnus-data-update-list (cdr data) offset)
6336                (setq data nil
6337                      gnus-newsgroup-data-reverse nil)))
6338         (setq data (cdr data))))))
6339
6340 (defmacro gnus-data-list (backward)
6341   (` (if (, backward)
6342          (or gnus-newsgroup-data-reverse
6343              (setq gnus-newsgroup-data-reverse
6344                    (reverse gnus-newsgroup-data)))
6345        gnus-newsgroup-data)))
6346
6347 (defun gnus-data-update-list (data offset)
6348   "Add OFFSET to the POS of all data entries in DATA."
6349   (while data
6350     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
6351     (setq data (cdr data))))
6352
6353 (defun gnus-summary-article-pseudo-p (article)
6354   "Say whether this article is a pseudo article or not."
6355   (not (vectorp (gnus-data-header (gnus-data-find article)))))
6356
6357 (defun gnus-article-parent-p (number)
6358   "Say whether this article is a parent or not."
6359   (let* ((data (gnus-data-find-list number)))
6360     (and (cdr data)                     ; There has to be an article after...
6361          (< (gnus-data-level (car data)) ; And it has to have a higher level.
6362             (gnus-data-level (nth 1 data))))))
6363     
6364 (defmacro gnus-summary-skip-intangible ()
6365   "If the current article is intangible, then jump to a different article."
6366   (let ((to (get-text-property (point) 'gnus-intangible)))
6367     (when to
6368       (gnus-summary-goto-subject to))))
6369
6370 (defmacro gnus-summary-article-intangible-p ()
6371   "Say whether this article is intangible or not."
6372   (get-text-property (point) 'gnus-intangible))
6373
6374 ;; Some summary mode macros.
6375
6376 (defmacro gnus-summary-article-number (&optional number-or-nil)
6377   "The article number of the article on the current line.
6378 If there isn's an article number here, then we return the current
6379 article number."
6380   (gnus-summary-skip-intangible)
6381   (if number-or-nil
6382       '(get-text-property (point) 'gnus-number)
6383     '(or (get-text-property (point) 'gnus-number) 
6384          gnus-current-article)))
6385
6386 (defmacro gnus-summary-article-header (&optional number)
6387   (` (gnus-data-header (gnus-data-find
6388                         (, (or number '(gnus-summary-article-number)))))))
6389
6390 (defmacro gnus-summary-thread-level (&optional number)
6391   (` (gnus-data-level (gnus-data-find
6392                        (, (or number '(gnus-summary-article-number)))))))
6393
6394 (defmacro gnus-summary-article-mark (&optional number)
6395   (` (gnus-data-mark (gnus-data-find
6396                       (, (or number '(gnus-summary-article-number)))))))
6397
6398 (defmacro gnus-summary-article-pos (&optional number)
6399   (` (gnus-data-pos (gnus-data-find
6400                      (, (or number '(gnus-summary-article-number)))))))
6401
6402 (defmacro gnus-summary-article-subject (&optional number)
6403   "Return current subject string or nil if nothing."
6404   (` (let ((headers 
6405             (, (if number
6406                    (` (gnus-data-header (assq (, number) gnus-newsgroup-data)))
6407                  '(gnus-data-header (assq (gnus-summary-article-number)
6408                                           gnus-newsgroup-data))))))
6409        (and headers
6410             (vectorp headers)
6411             (mail-header-subject headers)))))
6412
6413 (defmacro gnus-summary-article-score (&optional number)
6414   "Return current article score."
6415   (` (or (cdr (assq (, (or number '(gnus-summary-article-number)))
6416                     gnus-newsgroup-scored))
6417          gnus-summary-default-score 0)))
6418
6419 (defun gnus-summary-article-children (&optional number)
6420   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
6421          (level (gnus-data-level (car data)))
6422          l children)
6423     (while (and (setq data (cdr data))
6424                 (> (setq l (gnus-data-level (car data))) level))
6425       (and (= (1+ level) l)
6426            (setq children (cons (gnus-data-number (car data))
6427                                 children))))
6428     (nreverse children)))
6429
6430 (defun gnus-summary-article-parent (&optional number)
6431   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
6432                                     (gnus-data-list t)))
6433          (level (gnus-data-level (car data)))
6434          l)
6435     (if (zerop level)
6436         () ; This is a root.
6437       ;; We search until we find an article with a level less than
6438       ;; this one.  That function has to be the parent.
6439       (while (and (setq data (cdr data))
6440                   (not (< (gnus-data-level (car data)) level))))
6441       (and data (gnus-data-number (car data))))))
6442
6443
6444 ;; Various summary mode internalish functions.
6445
6446 (defun gnus-mouse-pick-article (e)
6447   (interactive "e")
6448   (mouse-set-point e)
6449   (gnus-summary-next-page nil t))
6450
6451 (defun gnus-summary-setup-buffer (group)
6452   "Initialize summary buffer."
6453   (let ((buffer (concat "*Summary " group "*")))
6454     (if (get-buffer buffer)
6455         (progn
6456           (set-buffer buffer)
6457           (not gnus-newsgroup-begin))
6458       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
6459       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
6460       (gnus-add-current-to-buffer-list)
6461       (gnus-summary-mode group)
6462       (and gnus-carpal (gnus-carpal-setup-buffer 'summary))
6463       (setq gnus-newsgroup-name group)
6464       t)))
6465
6466 (defun gnus-set-global-variables ()
6467   ;; Set the global equivalents of the summary buffer-local variables
6468   ;; to the latest values they had. These reflect the summary buffer
6469   ;; that was in action when the last article was fetched.
6470   (if (eq major-mode 'gnus-summary-mode) 
6471       (progn
6472         (setq gnus-summary-buffer (current-buffer))
6473         (let ((name gnus-newsgroup-name)
6474               (marked gnus-newsgroup-marked)
6475               (unread gnus-newsgroup-unreads)
6476               (headers gnus-current-headers)
6477               (data gnus-newsgroup-data)
6478               (score-file gnus-current-score-file))
6479           (save-excursion
6480             (set-buffer gnus-group-buffer)
6481             (setq gnus-newsgroup-name name)
6482             (setq gnus-newsgroup-marked marked)
6483             (setq gnus-newsgroup-unreads unread)
6484             (setq gnus-current-headers headers)
6485             (setq gnus-newsgroup-data data)
6486             (setq gnus-current-score-file score-file))))))
6487
6488 (defun gnus-summary-last-article-p (&optional article)
6489   "Return whether ARTICLE is the last article in the buffer."
6490   (if (not (setq article (or article (gnus-summary-article-number))))
6491       t ; All non-existant numbers are the last article. :-)
6492     (cdr (gnus-data-find-list article))))
6493     
6494 (defun gnus-summary-insert-dummy-line 
6495   (sformat gnus-tmp-subject gnus-tmp-number)
6496   "Insert a dummy root in the summary buffer."
6497   (or sformat (setq sformat gnus-summary-dummy-line-format-spec))
6498   (beginning-of-line)
6499   (add-text-properties
6500    (point) (progn (eval sformat) (point))
6501    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
6502
6503 (defvar gnus-thread-indent-array nil)
6504 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
6505 (defun gnus-make-thread-indent-array ()
6506   (let ((n 200))
6507     (if (and gnus-thread-indent-array
6508              (= gnus-thread-indent-level gnus-thread-indent-array-level))
6509         nil
6510       (setq gnus-thread-indent-array (make-vector 201 "")
6511             gnus-thread-indent-array-level gnus-thread-indent-level)
6512       (while (>= n 0)
6513         (aset gnus-thread-indent-array n
6514               (make-string (* n gnus-thread-indent-level) ? ))
6515         (setq n (1- n))))))
6516
6517 (defun gnus-summary-insert-line 
6518   (sformat gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread 
6519            gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
6520            &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
6521   (or sformat (setq sformat gnus-summary-line-format-spec))
6522   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
6523          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
6524          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
6525          (gnus-tmp-score-char
6526           (if (or (null gnus-summary-default-score)
6527                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
6528                       gnus-summary-zcore-fuzz)) ? 
6529             (if (< gnus-tmp-score gnus-summary-default-score)
6530                 gnus-score-below-mark gnus-score-over-mark)))
6531          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
6532                                  (gnus-tmp-replied gnus-replied-mark)
6533                                  (t gnus-unread-mark)))
6534          (gnus-tmp-from (mail-header-from gnus-tmp-header))
6535          (gnus-tmp-name 
6536           (cond 
6537            ((string-match "(.+)" gnus-tmp-from)
6538             (substring gnus-tmp-from 
6539                        (1+ (match-beginning 0)) (1- (match-end 0))))
6540            ((string-match "<[^>]+> *$" gnus-tmp-from)
6541             (let ((beg (match-beginning 0)))
6542               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
6543                        (substring gnus-tmp-from (1+ (match-beginning 0))
6544                                   (1- (match-end 0))))
6545                   (substring gnus-tmp-from 0 beg))))
6546            (t gnus-tmp-from)))
6547          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
6548          (gnus-tmp-number (mail-header-number gnus-tmp-header))
6549          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
6550          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
6551          (buffer-read-only nil))
6552     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
6553     (put-text-property
6554      (point)
6555      (progn (eval sformat) (point))
6556      'gnus-number gnus-tmp-number)))
6557
6558 (defun gnus-summary-update-line (&optional dont-update)
6559   ;; Update summary line after change.
6560   (or (not gnus-summary-default-score)
6561       gnus-summary-inhibit-highlight
6562       (let ((gnus-summary-inhibit-highlight t)
6563             (article (gnus-summary-article-number)))
6564         (progn
6565           (or dont-update
6566               (if (and gnus-summary-mark-below
6567                        (< (gnus-summary-article-score)
6568                           gnus-summary-mark-below))
6569                   (and (not (memq article gnus-newsgroup-marked))
6570                        (not (memq article gnus-newsgroup-dormant))
6571                        (memq article gnus-newsgroup-unreads)
6572                        (gnus-summary-mark-article-as-read gnus-low-score-mark))
6573                 (and (eq (gnus-summary-article-mark) gnus-low-score-mark)
6574                      (gnus-summary-mark-article-as-unread gnus-unread-mark))))
6575           (and (gnus-visual-p 'summary-highlight 'highlight)
6576                (run-hooks 'gnus-summary-update-hook))))))
6577
6578 (defun gnus-summary-update-lines (&optional beg end)
6579   ;; Mark article as read (or not) by taking into account scores.
6580   (let ((beg (or beg (point-min)))
6581         (end (or end (point-max))))
6582     (if (or (not gnus-summary-default-score)
6583             gnus-summary-inhibit-highlight)
6584         ()
6585       (let ((gnus-summary-inhibit-highlight t)
6586             article)
6587         (save-excursion
6588           (set-buffer gnus-summary-buffer)
6589           (goto-char beg)
6590           (beginning-of-line)
6591           (while (and (not (eobp)) (< (point) end))
6592             (if (and gnus-summary-mark-below
6593                      (< (or (cdr (assq 
6594                                   (setq article (gnus-summary-article-number))
6595                                   gnus-newsgroup-scored))
6596                             gnus-summary-default-score 0)
6597                         gnus-summary-mark-below))
6598                 ;; We want to possibly mark it as read...
6599                 (and (not (memq article gnus-newsgroup-marked))
6600                      (not (memq article gnus-newsgroup-dormant))
6601                      (memq article gnus-newsgroup-unreads)
6602                      (gnus-summary-mark-article-as-read gnus-low-score-mark))
6603               ;; We want to possibly mark it as unread.
6604               (and (eq (gnus-summary-article-mark article)
6605                        gnus-low-score-mark)
6606                    (gnus-summary-mark-article-as-unread gnus-unread-mark)))
6607             ;; Do the visual highlights at the same time.
6608             (and (gnus-visual-p 'summary-highlight 'highlight)
6609                  (run-hooks 'gnus-summary-update-hook))
6610             (forward-line 1)))))))
6611
6612 (defvar gnus-tmp-gathered nil)
6613
6614 (defun gnus-summary-number-of-articles-in-thread (thread &optional char)
6615   ;; Sum up all elements (and sub-elements) in a list.
6616   (let* ((number
6617           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
6618           (if (and (consp thread) (cdr thread))
6619               (apply
6620                '+ 1 (mapcar
6621                      'gnus-summary-number-of-articles-in-thread 
6622                      (cdr thread)))
6623             1)))
6624     (if char 
6625         (if (> number 1) gnus-not-empty-thread-mark
6626           gnus-empty-thread-mark)
6627       number)))
6628
6629 (defun gnus-summary-set-local-parameters (group)
6630  "Go through the local params of GROUP and set all variable specs in that list."
6631   (let ((params (nth 5 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6632         elem)
6633     (while params
6634       (setq elem (car params)
6635             params (cdr params))
6636       (and (consp elem)                 ; Has to be a cons.
6637            (consp (cdr elem))           ; The cdr has to be a list.
6638            (symbolp (car elem))         ; Has to be a symbol in there.
6639            (progn                       ; So we set it.
6640              (make-local-variable (car elem))
6641              (set (car elem) (eval (nth 1 elem))))))))
6642
6643 (defun gnus-summary-read-group 
6644   (group &optional show-all no-article kill-buffer no-display)
6645   "Start reading news in newsgroup GROUP.
6646 If SHOW-ALL is non-nil, already read articles are also listed.
6647 If NO-ARTICLE is non-nil, no article is selected initially.
6648 If NO-DISPLAY, don't generate a summary buffer."
6649   (gnus-message 5 "Retrieving newsgroup: %s..." group)
6650   (let* ((new-group (gnus-summary-setup-buffer group))
6651          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
6652                                                   group))))
6653          (did-select (and new-group (gnus-select-newsgroup group show-all))))
6654     (cond 
6655      ((not new-group)
6656       (gnus-set-global-variables)
6657       (gnus-kill-buffer kill-buffer)
6658       (gnus-configure-windows 'summary 'force)
6659       (gnus-set-mode-line 'summary)
6660       (gnus-summary-position-point)
6661       (message "")
6662       t)
6663      ((null did-select) 
6664       (and (eq major-mode 'gnus-summary-mode)
6665            (not (equal (current-buffer) kill-buffer))
6666            (progn
6667              (kill-buffer (current-buffer))
6668              (if (not quit-config)
6669                  (progn
6670                    (set-buffer gnus-group-buffer)
6671                    (gnus-group-jump-to-group group)
6672                    (gnus-group-next-unread-group 1))
6673                (if (not (buffer-name (car quit-config)))
6674                    (gnus-configure-windows 'group 'force)
6675                  (set-buffer (car quit-config))
6676                  (and (eq major-mode 'gnus-summary-mode)
6677                       (gnus-set-global-variables))
6678                  (gnus-configure-windows (cdr quit-config))))))
6679       (message "Can't select group")
6680       nil)
6681      ((eq did-select 'quit)
6682       (and (eq major-mode 'gnus-summary-mode)
6683            (not (equal (current-buffer) kill-buffer))
6684            (kill-buffer (current-buffer)))
6685       (gnus-kill-buffer kill-buffer)
6686       (if (not quit-config)
6687           (progn
6688             (set-buffer gnus-group-buffer)
6689             (gnus-group-jump-to-group group)
6690             (gnus-group-next-unread-group 1)
6691             (gnus-configure-windows 'group 'force))
6692         (if (not (buffer-name (car quit-config)))
6693             (gnus-configure-windows 'group 'force)
6694           (set-buffer (car quit-config))
6695           (and (eq major-mode 'gnus-summary-mode)
6696                (gnus-set-global-variables))
6697           (gnus-configure-windows (cdr quit-config))))
6698       (signal 'quit nil))
6699      (t
6700       (gnus-set-global-variables)
6701       ;; Save the active value in effect when the group was entered.
6702       (setq gnus-newsgroup-active 
6703             (gnus-copy-sequence
6704              (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6705       ;; You can change the summary buffer in some way with this hook.
6706       (run-hooks 'gnus-select-group-hook)
6707       ;; Set any local variables in the group parameters.
6708       (gnus-summary-set-local-parameters gnus-newsgroup-name)
6709       ;; Do score processing.
6710       (and gnus-use-scoring (gnus-possibly-score-headers))
6711       (gnus-update-format-specifications)
6712       ;; Find the initial limit.
6713       (gnus-summary-initial-limit)
6714       ;; Generate the summary buffer.
6715       (or no-display
6716           (gnus-summary-prepare))
6717       (if (zerop (buffer-size))
6718           (cond (gnus-newsgroup-dormant
6719                  (gnus-summary-limit-include-dormant))
6720                 ((and gnus-newsgroup-scored show-all)
6721                  (gnus-summary-show-all-expunged))))
6722       ;; Function `gnus-apply-kill-file' must be called in this hook.
6723       (run-hooks 'gnus-apply-kill-hook)
6724       (if (zerop (buffer-size))
6725           (progn
6726             ;; This newsgroup is empty.
6727             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
6728             (gnus-message 6 "No unread news")
6729             (gnus-kill-buffer kill-buffer)
6730             nil)
6731         ;; Hide conversation thread subtrees.  We cannot do this in
6732         ;; gnus-summary-prepare-hook since kill processing may not
6733         ;; work with hidden articles.
6734         (and gnus-show-threads
6735              gnus-thread-hide-subtree
6736              (gnus-summary-hide-all-threads))
6737         ;; Show first unread article if requested.
6738         (goto-char (point-min))
6739         (if (and (not no-article)
6740                  gnus-auto-select-first
6741                  (gnus-summary-first-unread-article))
6742             ()
6743           (gnus-configure-windows 'summary 'force))
6744         (gnus-set-mode-line 'summary)
6745         (gnus-summary-position-point)
6746         ;; If in async mode, we send some info to the backend.
6747         (and gnus-newsgroup-async
6748              (gnus-request-asynchronous 
6749               gnus-newsgroup-name gnus-newsgroup-data))
6750         (gnus-kill-buffer kill-buffer)
6751         (if (not (get-buffer-window gnus-group-buffer))
6752             ()
6753           ;; gotta use windows, because recenter does wierd stuff if
6754           ;; the current buffer ain't the displayed window.
6755           (let ((owin (selected-window))) 
6756             (select-window (get-buffer-window gnus-group-buffer))
6757             (and (gnus-group-goto-group group)
6758                  (recenter))
6759             (select-window owin))))
6760       t))))
6761
6762 (defun gnus-summary-prepare ()
6763   ;; Generate the summary buffer.
6764   (let ((buffer-read-only nil))
6765     (erase-buffer)
6766     (setq gnus-newsgroup-data nil
6767           gnus-newsgroup-data-reverse nil)
6768     (gnus-summary-prepare-threads 
6769      (if gnus-show-threads
6770          (gnus-gather-threads 
6771           (gnus-sort-threads 
6772            (gnus-make-threads)))
6773        gnus-newsgroup-headers))
6774     (gnus-summary-update-lines)
6775     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
6776     ;; Call hooks for modifying summary buffer.
6777     ;; Suggested by sven@tde.LTH.Se (Sven Mattisson).
6778     (goto-char (point-min))
6779     (run-hooks 'gnus-summary-prepare-hook)))
6780
6781 (defun gnus-gather-threads (threads)
6782   "Gather threads that have lost their roots."
6783   (if (not gnus-summary-make-false-root)
6784       threads 
6785     (let ((hashtb (gnus-make-hashtable 1023))
6786           (prev threads)
6787           (result threads)
6788           subject hthread whole-subject)
6789       (while threads
6790         (setq whole-subject 
6791               (setq subject (mail-header-subject (car (car threads)))))
6792         (if (and gnus-summary-gather-exclude-subject
6793                  (string-match gnus-summary-gather-exclude-subject
6794                                subject))
6795             () ; We don't want to do anything with this.
6796           (if gnus-summary-gather-subject-limit
6797               (or (and (numberp gnus-summary-gather-subject-limit)
6798                        (> (length subject) gnus-summary-gather-subject-limit)
6799                        (setq subject
6800                              (substring subject 0 
6801                                         gnus-summary-gather-subject-limit)))
6802                   (and (eq 'fuzzy gnus-summary-gather-subject-limit)
6803                        (setq subject (gnus-simplify-subject-fuzzy subject))))
6804             (setq subject (gnus-simplify-subject-re subject)))
6805           (if (setq hthread 
6806                     (gnus-gethash subject hashtb))
6807               (progn
6808                 (or (stringp (car (car hthread)))
6809                     (setcar hthread (list whole-subject (car hthread))))
6810                 (setcdr (car hthread) (nconc (cdr (car hthread)) 
6811                                              (list (car threads))))
6812                 (setcdr prev (cdr threads))
6813                 (setq threads prev))
6814             (gnus-sethash subject threads hashtb)))
6815         (setq prev threads)
6816         (setq threads (cdr threads)))
6817       result)))
6818
6819 (defun gnus-make-threads ()
6820   "Go through the dependency hashtb and find the roots.  Return all threads."
6821   ;; We might want to build some more threads first.
6822   (and gnus-fetch-old-headers
6823        (eq gnus-headers-retrieved-by 'nov)
6824        (gnus-build-old-threads))
6825   ;; Then we find all the roots and return all the threads.
6826   (let (threads)
6827     (mapatoms
6828      (lambda (refs)
6829        (or (car (symbol-value refs))
6830            (setq threads (append (cdr (symbol-value refs)) threads))))
6831      gnus-newsgroup-dependencies)
6832     threads))
6833   
6834 (defun gnus-build-old-threads ()
6835   ;; Look at all the articles that refer back to old articles, and
6836   ;; fetch the headers for the articles that aren't there. This will
6837   ;; build complete threads - if the roots haven't been expired by the
6838   ;; server, that is.
6839   (let (id heads)
6840     (mapatoms
6841      (lambda (refs)
6842        (if (not (car (symbol-value refs)))
6843            (progn
6844              (setq heads (cdr (symbol-value refs)))
6845              (while heads
6846                (if (not (memq (mail-header-number (car (car heads)))
6847                               gnus-newsgroup-dormant))
6848                    (progn
6849                      (setq id (symbol-name refs))
6850                      (while (and (setq id (gnus-build-get-header id))
6851                                  (not (car (gnus-gethash 
6852                                             id gnus-newsgroup-dependencies)))))
6853                      (setq heads nil))
6854                  (setq heads (cdr heads)))))))
6855      gnus-newsgroup-dependencies)))
6856
6857 (defun gnus-build-get-header (id)
6858   ;; Look through the buffer of NOV lines and find the header to
6859   ;; ID. Enter this line into the dependencies hash table, and return
6860   ;; the id of the parent article (if any).
6861   (let ((deps gnus-newsgroup-dependencies)
6862         found header)
6863     (prog1
6864         (save-excursion
6865           (set-buffer nntp-server-buffer)
6866           (goto-char (point-min))
6867           (while (and (not found) (search-forward id nil t))
6868             (beginning-of-line)
6869             (setq found (looking-at 
6870                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
6871                                  (regexp-quote id))))
6872             (or found (beginning-of-line 2)))
6873           (when found
6874             (let (ref)
6875               (beginning-of-line)
6876               (and
6877                (setq header (gnus-nov-parse-line 
6878                              (read (current-buffer)) deps))
6879                (gnus-parent-id (mail-header-references header))))))
6880       (when header
6881         (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers)
6882               gnus-newsgroup-ancient (cons (mail-header-number header)
6883                                            gnus-newsgroup-ancient))))))
6884
6885 (defun gnus-rebuild-thread (id)
6886   "Rebuild the thread containing ID."
6887   (let ((dep gnus-newsgroup-dependencies)
6888         (buffer-read-only nil)
6889         (orig (progn (beginning-of-line) (point)))
6890         (current (save-excursion
6891                    (and (zerop (forward-line -1))
6892                         (gnus-summary-article-number))))
6893         headers refs thread art data)
6894     (if (not gnus-show-threads)
6895         (setq thread (car (gnus-gethash (downcase id) dep)))
6896       ;; First go up in this thread until we find the root.
6897       (while (and id (setq headers
6898                            (car (setq art (gnus-gethash (downcase id) dep)))))
6899         (setq thread art)
6900         (setq id (gnus-parent-id (mail-header-references headers))))
6901       ;; We now have the root, so we remove this thread from the summary
6902       ;; buffer. 
6903       (gnus-remove-articles thread))
6904     (let ((beg (point)))
6905       ;; We then insert this thread into the summary buffer.
6906       (let (gnus-newsgroup-data)
6907         (goto-char orig)
6908         (gnus-summary-prepare-threads (list thread))
6909         (setq data (nreverse gnus-newsgroup-data)))
6910       ;; We splice the new data into the data structure.
6911       (gnus-data-enter-list current data (- (point) orig))
6912       ;; Do highlighting and stuff.
6913       (gnus-summary-update-lines beg (point)))))
6914
6915 (defun gnus-remove-articles (thread)
6916   "Remove THREAD from the summary buffer.
6917 Returns how many articles were removed."
6918   (let* ((points (sort (gnus-remove-articles-1 thread) '>))
6919          (result (length points)))
6920     (while points
6921       (goto-char (car points))
6922       (gnus-delete-line)
6923       (setq points (cdr points)))))
6924
6925 (defun gnus-remove-articles-1 (thread)
6926   (let* ((number (mail-header-number (car thread)))
6927          (pos (gnus-data-pos (gnus-data-find number))))
6928     (if pos
6929         (progn
6930           (goto-char pos)
6931           (gnus-data-remove number (- (progn (beginning-of-line) (point)) 
6932                                       (progn (forward-line 1) (point))))
6933           (cons pos (apply 'nconc
6934                            (mapcar (lambda (th) (gnus-remove-articles-1 th))
6935                                    (cdr thread)))))
6936       (apply 'nconc (mapcar (lambda (th) (gnus-remove-articles-1 th))
6937                             (cdr thread))))))
6938
6939 (defun gnus-sort-threads (threads)
6940   ;; Sort threads as specified in `gnus-thread-sort-functions'.
6941   (let ((fun gnus-thread-sort-functions))
6942     (while fun
6943       (gnus-message 6 "Sorting with %S..." fun)
6944       (setq threads (sort threads (car fun))
6945             fun (cdr fun))))
6946   (if gnus-thread-sort-functions
6947       (gnus-message 6 "Sorting...done"))
6948   threads)
6949
6950 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
6951 (defmacro gnus-thread-header (thread)
6952   ;; Return header of first article in THREAD.
6953   ;; Note that THREAD must never, evr be anything else than a variable -
6954   ;; using some other form will lead to serious barfage.
6955   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
6956   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
6957   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ; 
6958         (vector thread) 2))
6959
6960 (defun gnus-thread-sort-by-number (h1 h2)
6961   "Sort threads by root article number."
6962   (< (mail-header-number (gnus-thread-header h1))
6963      (mail-header-number (gnus-thread-header h2))))
6964
6965 (defun gnus-thread-sort-by-author (h1 h2)
6966   "Sort threads by root author."
6967   (string-lessp
6968    (let ((extract (funcall 
6969                    gnus-extract-address-components
6970                    (mail-header-from (gnus-thread-header h1)))))
6971      (or (car extract) (cdr extract)))
6972    (let ((extract (funcall
6973                    gnus-extract-address-components 
6974                    (mail-header-from (gnus-thread-header h2)))))
6975      (or (car extract) (cdr extract)))))
6976
6977 (defun gnus-thread-sort-by-subject (h1 h2)
6978   "Sort threads by root subject."
6979   (string-lessp
6980    (downcase (gnus-simplify-subject-re
6981               (mail-header-subject (gnus-thread-header h1))))
6982    (downcase (gnus-simplify-subject-re 
6983               (mail-header-subject (gnus-thread-header h2))))))
6984
6985 (defun gnus-thread-sort-by-date (h1 h2)
6986   "Sort threads by root article date."
6987   (string-lessp
6988    (gnus-sortable-date (mail-header-date (gnus-thread-header h1)))
6989    (gnus-sortable-date (mail-header-date (gnus-thread-header h2)))))
6990
6991 (defun gnus-thread-sort-by-score (h1 h2)
6992   "Sort threads by root article score.
6993 Unscored articles will be counted as having a score of zero."
6994   (> (or (cdr (assq (mail-header-number (gnus-thread-header h1))
6995                     gnus-newsgroup-scored))
6996          gnus-summary-default-score 0)
6997      (or (cdr (assq (mail-header-number (gnus-thread-header h2))
6998                     gnus-newsgroup-scored))
6999          gnus-summary-default-score 0)))
7000
7001 (defun gnus-thread-sort-by-total-score (h1 h2)
7002   "Sort threads by the sum of all scores in the thread.
7003 Unscored articles will be counted as having a score of zero."
7004   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
7005
7006 (defun gnus-thread-total-score (thread)
7007   ;;  This function find the total score of THREAD.
7008   (if (consp thread)
7009       (if (stringp (car thread))
7010           (apply gnus-thread-score-function 0
7011                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
7012         (gnus-thread-total-score-1 thread))
7013     (gnus-thread-total-score-1 (list thread))))
7014
7015 (defun gnus-thread-total-score-1 (root)
7016   ;; This function find the total score of the thread below ROOT.
7017   (setq root (car root))
7018   (apply gnus-thread-score-function
7019          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
7020              gnus-summary-default-score 0)
7021          (mapcar 'gnus-thread-total-score
7022                  (cdr (gnus-gethash (downcase (mail-header-id root))
7023                                     gnus-newsgroup-dependencies)))))
7024
7025 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
7026 (defvar gnus-tmp-prev-subject nil)
7027 (defvar gnus-tmp-false-parent nil)
7028 (defvar gnus-tmp-root-expunged nil)
7029 (defvar gnus-tmp-dummy-line nil)
7030
7031 (defun gnus-summary-prepare-threads (threads)
7032   "Prepare summary buffer from THREADS and indentation LEVEL.  
7033 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
7034 or a straight list of headers."
7035   (message "Generating summary...")
7036   (let ((level 0)
7037         thread header number subject stack state gnus-tmp-gathered mark
7038         new-roots new-adopts thread-end)
7039
7040     (setq gnus-tmp-prev-subject nil)
7041
7042     (if (vectorp (car threads))
7043         ;; If this is a straight (sic) list of headers, then a
7044         ;; threaded summary display isn't required, so we just create
7045         ;; an unthreaded one.
7046         (gnus-summary-prepare-unthreaded threads)
7047
7048       ;; Do the threaded display.
7049
7050       (while (or threads stack new-adopts new-roots)
7051
7052         (if (and (= level 0)
7053                  (progn (setq gnus-tmp-dummy-line nil) t)
7054                  (or (not stack)
7055                      (= (car (car stack)) 0))
7056                  (not gnus-tmp-false-parent)
7057                  (or new-adopts new-roots))
7058             (progn
7059               (if new-adopts
7060                   (setq level (if gnus-tmp-root-expunged 0 1)
7061                         thread (list (car new-adopts))
7062                         header (car (car thread))
7063                         new-adopts (cdr new-adopts))
7064                 (if new-roots
7065                     (setq thread (list (car new-roots))
7066                           header (car (car thread))
7067                           new-roots (cdr new-roots)))))
7068
7069           (if threads
7070               ;; If there are some threads, we do them before the
7071               ;; threads on the stack.
7072               (setq thread threads
7073                     header (car (car thread)))
7074             ;; There were no current threads, so we pop something off
7075             ;; the stack. 
7076             (setq state (car stack)
7077                   level (car state)
7078                   thread (cdr state)
7079                   stack (cdr stack)
7080                   header (car (car thread)))))
7081
7082         (setq gnus-tmp-false-parent nil)
7083         (setq gnus-tmp-root-expunged nil)
7084         (setq thread-end nil)
7085
7086         (if (stringp header)
7087             (progn
7088               ;; The header is a dummy root.
7089               (cond 
7090                ((eq gnus-summary-make-false-root 'adopt)
7091                 ;; We let the first article adopt the rest.
7092                 (setq new-adopts (nconc new-adopts
7093                                         (cdr (cdr (car thread)))))
7094                 (setq gnus-tmp-gathered 
7095                       (nconc (mapcar
7096                               (lambda (h) (mail-header-number (car h)))
7097                               (cdr (cdr (car thread))))
7098                              gnus-tmp-gathered))
7099                 (setcdr (cdr (car thread)) nil)
7100                 (setq level -1
7101                       gnus-tmp-false-parent t))
7102                ((eq gnus-summary-make-false-root 'empty)
7103                 ;; We print adopted articles with empty subject fields.
7104                 (setq gnus-tmp-gathered 
7105                       (nconc (mapcar
7106                               (lambda (h) (mail-header-number (car h)))
7107                               (cdr (cdr (car thread))))
7108                              gnus-tmp-gathered))
7109                 (setq level -1))
7110                ((eq gnus-summary-make-false-root 'dummy)
7111                 ;; We remember that we probably want to output a dummy
7112                 ;; root.   
7113                 (setq gnus-tmp-dummy-line header)
7114                 (setq gnus-tmp-prev-subject header))
7115                (t
7116                 ;; We do not make a root for the gathered
7117                 ;; sub-threads at all.  
7118                 (setq level -1))))
7119       
7120           (setq number (mail-header-number header)
7121                 subject (mail-header-subject header))
7122
7123           (cond 
7124            ((and (null gnus-thread-ignore-subject)
7125                  (not (zerop level))
7126                  gnus-tmp-prev-subject
7127                  (not (gnus-subject-equal gnus-tmp-prev-subject subject)))
7128             (setq new-roots (nconc new-roots (list (car thread)))
7129                   thread-end t
7130                   header nil))
7131            ((not (memq number gnus-newsgroup-limit))
7132             (setq gnus-tmp-gathered 
7133                   (nconc (mapcar
7134                           (lambda (h) (mail-header-number (car h)))
7135                           (cdr (car thread)))
7136                          gnus-tmp-gathered))
7137             (setq new-adopts (if (cdr (car thread))
7138                                  (append new-adopts 
7139                                          (cdr (car thread)))
7140                                new-adopts)
7141                   thread-end t
7142                   header nil)
7143             (if (zerop level)
7144                 (setq gnus-tmp-root-expunged t)))
7145            ((and gnus-summary-mark-below
7146                  (< (or (cdr (assq number gnus-newsgroup-scored))
7147                         gnus-summary-default-score 0)
7148                     gnus-summary-mark-below))
7149             (setq gnus-newsgroup-unreads 
7150                   (delq number gnus-newsgroup-unreads)
7151                   gnus-newsgroup-reads
7152                   (cons (cons number gnus-low-score-mark)
7153                         gnus-newsgroup-reads))))
7154           
7155           (and
7156            header
7157            (progn
7158              ;; We may have an old dummy line to output before this
7159              ;; article.  
7160              (when gnus-tmp-dummy-line
7161                (gnus-summary-insert-dummy-line 
7162                 nil gnus-tmp-dummy-line header))
7163
7164              ;; Compute the mark.
7165              (setq 
7166               mark
7167               (cond 
7168                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
7169                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
7170                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
7171                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
7172                (t (or (cdr (assq number gnus-newsgroup-reads))
7173                       gnus-ancient-mark))))
7174
7175              ;; Actually insert the line.
7176              (inline
7177                (gnus-summary-insert-line
7178                 nil header level nil mark
7179                 (memq number gnus-newsgroup-replied)
7180                 (memq number gnus-newsgroup-expirable)
7181                 (cond
7182                  ((and gnus-thread-ignore-subject
7183                        gnus-tmp-prev-subject
7184                        (not (gnus-subject-equal 
7185                              gnus-tmp-prev-subject subject)))
7186                   subject)
7187                  ((zerop level)
7188                   (if (and (eq gnus-summary-make-false-root 'empty)
7189                            (memq number gnus-tmp-gathered))
7190                       gnus-summary-same-subject
7191                     subject))
7192                  (t gnus-summary-same-subject))
7193                 (and (eq gnus-summary-make-false-root 'adopt)
7194                      (= level 1)
7195                      (memq number gnus-tmp-gathered))
7196                 (cdr (assq number gnus-newsgroup-scored))
7197                 (memq number gnus-newsgroup-processable)))
7198
7199              (setq gnus-newsgroup-data 
7200                    (cons (gnus-data-make number mark (- (point) 4)
7201                                          header level)
7202                          gnus-newsgroup-data))
7203
7204              (setq gnus-tmp-prev-subject subject))))
7205
7206         (if (nth 1 thread) 
7207             (setq stack (cons (cons (max 0 level) (nthcdr 1 thread)) stack)))
7208         (setq level (1+ level))
7209         (setq threads (if thread-end nil (cdr (car thread))))
7210         (or threads (setq level 0)))))
7211   (message "Generating summary...done"))
7212
7213 (defun gnus-summary-prepare-unthreaded (headers)
7214   "Generate an unthreaded summary buffer based on HEADERS."
7215   (let (header number mark)
7216
7217     (while headers
7218       (setq header (car headers)
7219             headers (cdr headers)
7220             number (mail-header-number header))
7221
7222       ;; We may have to root out some bad articles...
7223       (when (memq number gnus-newsgroup-limit)
7224         (setq mark
7225               (cond 
7226                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
7227                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
7228                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
7229                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
7230                (t (or (cdr (assq number gnus-newsgroup-reads))
7231                       gnus-ancient-mark))))
7232         (setq gnus-newsgroup-data 
7233               (cons (gnus-data-make number mark (1+ (point)) header 0)
7234                     gnus-newsgroup-data))
7235         (gnus-summary-insert-line
7236          nil header 0 nil mark (memq number gnus-newsgroup-replied)
7237          (memq number gnus-newsgroup-expirable)
7238          (mail-header-subject header) nil
7239          (cdr (assq number gnus-newsgroup-scored))
7240          (memq number gnus-newsgroup-processable))))))
7241
7242 (defun gnus-select-newsgroup (group &optional read-all)
7243   "Select newsgroup GROUP.
7244 If READ-ALL is non-nil, all articles in the group are selected."
7245   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
7246          (info (nth 2 entry))
7247          articles)
7248
7249     (or (gnus-check-server
7250          (setq gnus-current-select-method (gnus-find-method-for-group group)))
7251         (error "Couldn't open server"))
7252     
7253     (or (and entry (not (eq (car entry) t))) ; Either it's active...
7254         (gnus-activate-group group) ; Or we can activate it...
7255         (progn ; Or we bug out.
7256           (kill-buffer (current-buffer))
7257           (error "Couldn't request group %s: %s" 
7258                  group (gnus-status-message group))))
7259
7260     (setq gnus-newsgroup-name group)
7261     (setq gnus-newsgroup-unselected nil)
7262     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
7263
7264     (and gnus-asynchronous
7265          (gnus-check-backend-function 
7266           'request-asynchronous gnus-newsgroup-name)
7267          (setq gnus-newsgroup-async
7268                (gnus-request-asynchronous gnus-newsgroup-name)))
7269
7270     (setq articles (gnus-articles-to-read group read-all))
7271
7272     (cond 
7273      ((null articles) 
7274       (gnus-message 3 "Couldn't select newsgroup")
7275       'quit)
7276      ((eq articles 0) nil)
7277      (t
7278       ;; Init the dependencies hash table.
7279       (setq gnus-newsgroup-dependencies 
7280             (gnus-make-hashtable (length articles)))
7281       ;; Retrieve the headers and read them in.
7282       (gnus-message 5 "Fetching headers...")
7283       (setq gnus-newsgroup-headers 
7284             (if (eq 'nov 
7285                     (setq gnus-headers-retrieved-by
7286                           (gnus-retrieve-headers 
7287                            articles gnus-newsgroup-name
7288                            ;; We might want to fetch old headers, but
7289                            ;; not if there is only 1 article.
7290                            (and gnus-fetch-old-headers
7291                                 (or (and 
7292                                      (not (eq gnus-fetch-old-headers 'some))
7293                                      (not (numberp gnus-fetch-old-headers)))
7294                                     (> (length articles) 1))))))
7295                 (gnus-get-newsgroup-headers-xover articles)
7296               (gnus-get-newsgroup-headers)))
7297       (gnus-message 5 "Fetching headers...done")      
7298       ;; Remove canceled articles from the list of unread articles.
7299       (setq gnus-newsgroup-unreads
7300             (gnus-set-sorted-intersection 
7301              gnus-newsgroup-unreads
7302              (mapcar (lambda (headers) (mail-header-number headers))
7303                      gnus-newsgroup-headers)))
7304       ;; Set the initial limit.
7305       (setq gnus-newsgroup-limit (copy-sequence articles))
7306       ;; Adjust and set lists of article marks.
7307       (and info
7308            (let (marked)
7309              (gnus-adjust-marked-articles info)
7310              (setq gnus-newsgroup-marked 
7311                    (copy-sequence
7312                     (cdr (assq 'tick (setq marked (nth 3 info))))))
7313              (setq gnus-newsgroup-replied 
7314                    (copy-sequence (cdr (assq 'reply marked))))
7315              (setq gnus-newsgroup-expirable
7316                    (copy-sequence (cdr (assq 'expire marked))))
7317              (setq gnus-newsgroup-killed
7318                    (copy-sequence (cdr (assq 'killed marked))))
7319              (setq gnus-newsgroup-bookmarks 
7320                    (copy-sequence (cdr (assq 'bookmark marked))))
7321              (setq gnus-newsgroup-dormant 
7322                    (copy-sequence (cdr (assq 'dormant marked))))
7323              (setq gnus-newsgroup-scored 
7324                    (copy-sequence (cdr (assq 'score marked))))
7325              (setq gnus-newsgroup-processable nil)))
7326       ;; Check whether auto-expire is to be done in this group.
7327       (setq gnus-newsgroup-auto-expire
7328             (gnus-group-auto-expirable-p group))
7329       ;; First and last article in this newsgroup.
7330       (and gnus-newsgroup-headers
7331            (setq gnus-newsgroup-begin 
7332                  (mail-header-number (car gnus-newsgroup-headers)))
7333            (setq gnus-newsgroup-end
7334                  (mail-header-number
7335                   (gnus-last-element gnus-newsgroup-headers))))
7336       (setq gnus-reffed-article-number -1)
7337       ;; GROUP is successfully selected.
7338       (or gnus-newsgroup-headers t)))))
7339
7340 (defun gnus-articles-to-read (group read-all)
7341   ;; Find out what articles the user wants to read.
7342   (let* ((articles
7343           ;; Select all articles if `read-all' is non-nil, or if all the
7344           ;; unread articles are dormant articles.
7345           (if (or (and read-all (not (numberp read-all)))
7346                   (= (length gnus-newsgroup-unreads) 
7347                      (length gnus-newsgroup-dormant)))
7348               (gnus-uncompress-range 
7349                (gnus-gethash group gnus-active-hashtb))
7350             gnus-newsgroup-unreads))
7351          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
7352          (scored (length scored-list))
7353          (number (length articles))
7354          (marked (+ (length gnus-newsgroup-marked)
7355                     (length gnus-newsgroup-dormant)))
7356          (select
7357           (cond 
7358            ((numberp read-all)
7359             read-all)
7360            (t
7361             (condition-case ()
7362                 (cond ((and (or (<= scored marked)
7363                                 (= scored number))
7364                             (numberp gnus-large-newsgroup)
7365                             (> number gnus-large-newsgroup))
7366                        (let ((input
7367                               (read-string
7368                                (format
7369                                 "How many articles from %s (default %d): "
7370                                 gnus-newsgroup-name number))))
7371                          (if (string-match "^[ \t]*$" input)
7372                              number input)))
7373                       ((and (> scored marked) (< scored number))
7374                        (let ((input
7375                               (read-string
7376                                (format 
7377                                 "%s %s (%d scored, %d total): "
7378                                 "How many articles from"
7379                                 group scored number))))
7380                          (if (string-match "^[ \t]*$" input)
7381                              number input)))
7382                       (t number))
7383               (quit nil))))))
7384     (setq select (if (stringp select) (string-to-number select) select))
7385     (if (or (null select) (zerop select))
7386         select
7387       (if (and (not (zerop scored)) (<= (abs select) scored))
7388           (progn
7389             (setq articles (sort scored-list '<))
7390             (setq number (length articles)))
7391         (setq articles (copy-sequence articles)))
7392
7393       (if (< (abs select) number)
7394           (if (< select 0) 
7395               ;; Select the N oldest articles.
7396               (setcdr (nthcdr (1- (abs select)) articles) nil)
7397             ;; Select the N most recent articles.
7398             (setq articles (nthcdr (- number select) articles))))
7399       (setq gnus-newsgroup-unselected
7400             (gnus-sorted-intersection
7401              gnus-newsgroup-unreads
7402              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
7403       articles)))
7404
7405 (defun gnus-killed-articles (killed articles)
7406   (let (out)
7407     (while articles
7408       (if (inline (gnus-member-of-range (car articles) killed))
7409           (setq out (cons (car articles) out)))
7410       (setq articles (cdr articles)))
7411     out))
7412
7413 (defun gnus-adjust-marked-articles (info &optional active)
7414   "Remove all marked articles that are no longer legal."
7415   (let ((marked-lists (nth 3 info))
7416         (active (or active (gnus-gethash (car info) gnus-active-hashtb)))
7417         m prev)
7418     ;; There are many types of marked articles.
7419     (while marked-lists
7420       (setq m (cdr (setq prev (car marked-lists))))
7421       (cond ((or (eq 'tick (car prev)) (eq 'dormant (car prev)))
7422              ;; Make sure that all ticked articles are a subset of the
7423              ;; unread/unselected articles.
7424              (while m
7425                (if (or (memq (car m) gnus-newsgroup-unreads)
7426                        (memq (car m) gnus-newsgroup-unselected))
7427                    (setq prev m)
7428                  (setcdr prev (cdr m)))
7429                (setq m (cdr m))))
7430             ((eq 'score (car prev))
7431              ;; Scored articles should be a subset of
7432              ;; unread/unselected articles. 
7433              (while m
7434                (if (or (memq (car (car m)) gnus-newsgroup-unreads)
7435                        (memq (car (car m)) gnus-newsgroup-unreads))
7436                    (setq prev m)
7437                  (setcdr prev (cdr m)))
7438                (setq m (cdr m))))
7439             ((eq 'bookmark (car prev))
7440              ;; Bookmarks should be a subset of active articles.
7441              (while m
7442                (if (< (car (car m)) (car active))
7443                    (setcdr prev (cdr m))
7444                  (setq prev m))
7445                (setq m (cdr m))))
7446             ((eq 'killed (car prev))
7447              ;; Articles that have been through the kill process are
7448              ;; to be a subset of active articles.
7449              (while (and m (< (or (and (numberp (car m)) (car m))
7450                                   (cdr (car m)))
7451                               (car active)))
7452                (setcdr prev (cdr m))
7453                (setq m (cdr m)))
7454              (if (and m (< (or (and (numberp (car m)) (car m))
7455                                (car (car m)))
7456                            (car active))) 
7457                  (setcar (if (numberp (car m)) m (car m)) (car active))))
7458             ((or (eq 'reply (car prev)) (eq 'expire (car prev)))
7459              ;; The replied and expirable articles have to be articles
7460              ;; that are active. 
7461              (while m
7462                (if (< (car m) (car active))
7463                    (setcdr prev (cdr m))
7464                  (setq prev m))
7465                (setq m (cdr m)))))
7466       (setq marked-lists (cdr marked-lists)))
7467     ;; Remove all lists that are empty.
7468     (setq marked-lists (nth 3 info))
7469     (if marked-lists
7470         (progn
7471           (while (= 1 (length (car marked-lists)))
7472             (setq marked-lists (cdr marked-lists)))
7473           (setq m (cdr (setq prev marked-lists)))
7474           (while m
7475             (if (= 1 (length (car m)))
7476                 (setcdr prev (cdr m))
7477               (setq prev m))
7478             (setq m (cdr m)))
7479           (setcar (nthcdr 3 info) marked-lists)))
7480     ;; Finally, if there are no marked lists at all left, and if there
7481     ;; are no elements after the lists in the info list, we just chop
7482     ;; the info list off before the marked lists.
7483     (and (null marked-lists) 
7484          (not (nthcdr 4 info))
7485          (setcdr (nthcdr 2 info) nil)))
7486   info)
7487
7488 (defun gnus-set-marked-articles 
7489   (info ticked replied expirable killed dormant bookmark score) 
7490   "Enter the various lists of marked articles into the newsgroup info list."
7491   (let (newmarked)
7492     (and ticked (setq newmarked (cons (cons 'tick ticked) nil)))
7493     (and replied (setq newmarked (cons (cons 'reply replied) newmarked)))
7494     (and expirable (setq newmarked (cons (cons 'expire expirable) 
7495                                          newmarked)))
7496     (and killed (setq newmarked (cons (cons 'killed killed) newmarked)))
7497     (and dormant (setq newmarked (cons (cons 'dormant dormant) newmarked)))
7498     (and bookmark (setq newmarked (cons (cons 'bookmark bookmark) 
7499                                         newmarked)))
7500     (and score (setq newmarked (cons (cons 'score score) newmarked)))
7501     (if (nthcdr 3 info)
7502         (progn
7503           (setcar (nthcdr 3 info) newmarked)
7504           (and (not newmarked)
7505                (not (nthcdr 4 info))
7506                (setcdr (nthcdr 2 info) nil)))
7507       (if newmarked
7508           (setcdr (nthcdr 2 info) (list newmarked))))))
7509
7510 (defun gnus-add-marked-articles (group type articles &optional info force)
7511   ;; Add ARTICLES of TYPE to the info of GROUP.
7512   ;; If INFO is non-nil, use that info. If FORCE is non-nil, don't
7513   ;; add, but replace marked articles of TYPE with ARTICLES.
7514   (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
7515         marked m)
7516     (or (not info)
7517         (and (not (setq marked (nthcdr 3 info)))
7518              (setcdr (nthcdr 2 info) (list (list (cons type articles)))))
7519         (and (not (setq m (assq type (car marked))))
7520              (setcar marked (cons (cons type articles) (car marked))))
7521         (if force
7522             (setcdr m articles)
7523           (nconc m articles)))))
7524          
7525 (defun gnus-set-mode-line (where)
7526   "This function sets the mode line of the article or summary buffers.
7527 If WHERE is `summary', the summary mode line format will be used."
7528   (if (memq where gnus-updated-mode-lines)
7529       (let (mode-string)
7530         (save-excursion
7531           (set-buffer gnus-summary-buffer)
7532           (let* ((mformat (if (eq where 'article) 
7533                               gnus-article-mode-line-format-spec
7534                             gnus-summary-mode-line-format-spec))
7535                  (gnus-tmp-group-name gnus-newsgroup-name)
7536                  (gnus-tmp-article-number (or gnus-current-article 0))
7537                  (gnus-tmp-unread (- (length gnus-newsgroup-unreads)
7538                                      (length gnus-newsgroup-dormant)))
7539                  (gnus-tmp-unread-and-unticked 
7540                   (- gnus-tmp-unread (length gnus-newsgroup-marked)))
7541                  (gnus-tmp-unselected (length gnus-newsgroup-unselected))
7542                  (gnus-tmp-unread-and-unselected
7543                   (cond ((and (zerop gnus-tmp-unread-and-unticked)
7544                               (zerop gnus-tmp-unselected)) "")
7545                         ((zerop gnus-tmp-unselected) 
7546                          (format "{%d more}" gnus-tmp-unread-and-unticked))
7547                         (t (format "{%d(+%d) more}"
7548                                    gnus-tmp-unread-and-unticked
7549                                    gnus-tmp-unselected))))
7550                  (gnus-tmp-subject
7551                   (if gnus-current-headers
7552                       (mail-header-subject gnus-current-headers) ""))
7553                  (max-len (and gnus-mode-non-string-length
7554                                (- (frame-width) gnus-mode-non-string-length)))
7555                  header);; passed as argument to any user-format-funcs
7556             (setq mode-string (eval mformat))
7557             (or (numberp max-len)
7558                 (setq max-len (length mode-string)))
7559             (if (< max-len 4) (setq max-len 4))
7560             (if (> (length mode-string) max-len)
7561                 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
7562                 ;;  function `substring' might cut on a middle
7563                 ;;  of multi-octet character.
7564                 (setq mode-string 
7565                       (concat (gnus-truncate-string mode-string (- max-len 3))
7566                               "...")))
7567             (setq mode-string (format (format "%%-%ds" max-len)
7568                                       mode-string))))
7569         (setq mode-line-buffer-identification mode-string)
7570         (set-buffer-modified-p t))))
7571
7572 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads ticked dormant)
7573   "Go through the HEADERS list and add all Xrefs to a hash table.
7574 The resulting hash table is returned, or nil if no Xrefs were found."
7575   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
7576          (prefix (if (and 
7577                       (gnus-group-foreign-p from-newsgroup)
7578                       (not (memq 'virtual 
7579                                  (assoc (symbol-name (car from-method))
7580                                         gnus-valid-select-methods))))
7581                      (gnus-group-real-prefix from-newsgroup)))
7582          (xref-hashtb (make-vector 63 0))
7583          start group entry number xrefs header)
7584     (while headers
7585       (setq header (car headers))
7586       (if (and (setq xrefs (mail-header-xref header))
7587                (not (memq (setq number (mail-header-number header)) unreads))
7588                (not (memq number ticked))
7589                (not (memq number dormant)))
7590           (progn
7591             (setq start 0)
7592             (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
7593               (setq start (match-end 0))
7594               (setq group (concat prefix (substring xrefs (match-beginning 1) 
7595                                                     (match-end 1))))
7596               (setq number 
7597                     (string-to-int (substring xrefs (match-beginning 2) 
7598                                               (match-end 2))))
7599               (if (setq entry (gnus-gethash group xref-hashtb))
7600                   (setcdr entry (cons number (cdr entry)))
7601                 (gnus-sethash group (cons number nil) xref-hashtb)))))
7602       (setq headers (cdr headers)))
7603     (if start xref-hashtb nil)))
7604
7605 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads expirable
7606                                                ticked dormant)
7607   "Look through all the headers and mark the Xrefs as read."
7608   (let ((virtual (memq 'virtual 
7609                        (assoc (symbol-name (car (gnus-find-method-for-group 
7610                                                  from-newsgroup)))
7611                               gnus-valid-select-methods)))
7612         name entry info xref-hashtb idlist method
7613         nth4)
7614     (save-excursion
7615       (set-buffer gnus-group-buffer)
7616       (if (setq xref-hashtb 
7617                 (gnus-create-xref-hashtb 
7618                  from-newsgroup headers unreads ticked dormant))
7619           (mapatoms 
7620            (lambda (group)
7621              (if (string= from-newsgroup (setq name (symbol-name group)))
7622                  ()
7623                (setq idlist (symbol-value group))
7624                ;; Dead groups are not updated.
7625                (if (and (prog1 
7626                             (setq entry (gnus-gethash name gnus-newsrc-hashtb)
7627                                   info (nth 2 entry))
7628                           (if (stringp (setq nth4 (nth 4 info)))
7629                               (setq nth4 (gnus-server-to-method nth4))))
7630                         ;; Only do the xrefs if the group has the same
7631                         ;; select method as the group we have just read.
7632                         (or (gnus-methods-equal-p 
7633                              nth4 (gnus-find-method-for-group from-newsgroup))
7634                             virtual
7635                             (equal nth4 
7636                                    (setq method (gnus-find-method-for-group 
7637                                                  from-newsgroup)))
7638                             (and (equal (car nth4) (car method))
7639                                  (equal (nth 1 nth4) (nth 1 method))))
7640                         gnus-use-cross-reference
7641                         (or (not (eq gnus-use-cross-reference t))
7642                             virtual
7643                             ;; Only do cross-references on subscribed
7644                             ;; groups, if that is what is wanted.  
7645                             (<= (nth 1 info) gnus-level-subscribed)))
7646                    (gnus-group-make-articles-read name idlist expirable))))
7647            xref-hashtb)))))
7648
7649 (defun gnus-group-make-articles-read (group articles expirable)
7650   (let* ((num 0)
7651          (entry (gnus-gethash group gnus-newsrc-hashtb))
7652          (info (nth 2 entry))
7653          (active (gnus-gethash group gnus-active-hashtb))
7654          exps expirable range)
7655     ;; First peel off all illegal article numbers.
7656     (if active
7657         (let ((ids articles)
7658               (ticked (cdr (assq 'tick (nth 3 info))))
7659               (dormant (cdr (assq 'dormant (nth 3 info))))
7660               id first)
7661           (setq exps nil)
7662           (while ids
7663             (setq id (car ids))
7664             (if (and first (> id (cdr active)))
7665                 (progn
7666                   ;; We'll end up in this situation in one particular
7667                   ;; obscure situation. If you re-scan a group and get
7668                   ;; a new article that is cross-posted to a different
7669                   ;; group that has not been re-scanned, you might get
7670                   ;; crossposted article that has a higher number than
7671                   ;; Gnus believes possible. So we re-activate this
7672                   ;; group as well. This might mean doing the
7673                   ;; crossposting thingie will *increase* the number
7674                   ;; of articles in some groups. Tsk, tsk.
7675                   (setq active (or (gnus-activate-group group) active))))
7676             (if (or (> id (cdr active))
7677                     (< id (car active))
7678                     (memq id ticked)
7679                     (memq id dormant))
7680                 (setq articles (delq id articles)))
7681             (and (memq id expirable)
7682                  (setq exps (cons id exps)))
7683             (setq ids (cdr ids)))))
7684     ;; Update expirable articles.
7685     (gnus-add-marked-articles nil 'expirable exps info)
7686     (and active
7687          (null (nth 2 info))
7688          (> (car active) 1)
7689          (setcar (nthcdr 2 info) (cons 1 (1- (car active)))))
7690     (setcar (nthcdr 2 info)
7691             (setq range
7692                   (gnus-add-to-range 
7693                    (nth 2 info) 
7694                    (setq articles (sort articles '<)))))
7695     ;; Then we have to re-compute how many unread
7696     ;; articles there are in this group.
7697     (if active
7698         (progn
7699           (cond 
7700            ((not range)
7701             (setq num (- (1+ (cdr active)) (car active))))
7702            ((not (listp (cdr range)))
7703             (setq num (- (cdr active) (- (1+ (cdr range)) 
7704                                          (car range)))))
7705            (t
7706             (while range
7707               (if (numberp (car range))
7708                   (setq num (1+ num))
7709                 (setq num (+ num (- (1+ (cdr (car range)))
7710                                     (car (car range))))))
7711               (setq range (cdr range)))
7712             (setq num (- (cdr active) num))))
7713           ;; Update the number of unread articles.
7714           (setcar 
7715            entry 
7716            (max 0 (- num 
7717                      (length (cdr (assq 'tick (nth 3 info))))
7718                      (length 
7719                       (cdr (assq 'dormant (nth 3 info)))))))
7720           ;; Update the group buffer.
7721           (gnus-group-update-group group t)))))
7722
7723 (defun gnus-methods-equal-p (m1 m2)
7724   (let ((m1 (or m1 gnus-select-method))
7725         (m2 (or m2 gnus-select-method)))
7726     (or (equal m1 m2)
7727         (and (eq (car m1) (car m2))
7728              (or (not (memq 'address (assoc (symbol-name (car m1))
7729                                             gnus-valid-select-methods)))
7730                  (equal (nth 1 m1) (nth 1 m2)))))))
7731
7732 (defsubst gnus-header-value ()
7733   (buffer-substring (match-end 0) (gnus-point-at-eol)))
7734
7735 (defvar gnus-newsgroup-none-id 0)
7736
7737 (defun gnus-get-newsgroup-headers ()
7738   (setq gnus-article-internal-prepare-hook nil)
7739   (let ((cur nntp-server-buffer)
7740         (dependencies (save-excursion (set-buffer gnus-summary-buffer)
7741                                       gnus-newsgroup-dependencies))
7742         headers id id-dep ref-dep end ref)
7743     (save-excursion
7744       (set-buffer nntp-server-buffer)
7745       (let ((case-fold-search t)
7746             in-reply-to header number p lines)
7747         (goto-char (point-min))
7748         ;; Search to the beginning of the next header. Error messages
7749         ;; do not begin with 2 or 3.
7750         (while (re-search-forward "^[23][0-9]+ " nil t)
7751           (setq id nil
7752                 ref nil)
7753           ;; This implementation of this function, with nine
7754           ;; search-forwards instead of the one re-search-forward and
7755           ;; a case (which basically was the old function) is actually
7756           ;; about twice as fast, even though it looks messier. You
7757           ;; can't have everything, I guess. Speed and elegance
7758           ;; doesn't always go hand in hand.
7759           (setq 
7760            header
7761            (vector
7762             ;; Number.
7763             (prog1
7764                 (read cur)
7765               (end-of-line)
7766               (setq p (point))
7767               (narrow-to-region (point) 
7768                                 (or (and (search-forward "\n.\n" nil t)
7769                                          (- (point) 2))
7770                                     (point))))
7771             ;; Subject.
7772             (progn
7773               (goto-char p)
7774               (if (search-forward "\nsubject: " nil t)
7775                   (gnus-header-value) "(none)"))
7776             ;; From.
7777             (progn
7778               (goto-char p)
7779               (if (search-forward "\nfrom: " nil t)
7780                   (gnus-header-value) "(nobody)"))
7781             ;; Date.
7782             (progn
7783               (goto-char p)
7784               (if (search-forward "\ndate: " nil t)
7785                   (gnus-header-value) ""))
7786             ;; Message-ID.
7787             (progn
7788               (goto-char p)
7789               (if (search-forward "\nmessage-id: " nil t)
7790                   (setq id (gnus-header-value))
7791                 ;; If there was no message-id, we just fake one to make
7792                 ;; subsequent routines simpler.
7793                 (setq id (concat "none+" 
7794                                  (int-to-string 
7795                                   (setq gnus-newsgroup-none-id 
7796                                         (1+ gnus-newsgroup-none-id)))))))
7797             ;; References.
7798             (progn
7799               (goto-char p)
7800               (if (search-forward "\nreferences: " nil t)
7801                   (prog1
7802                       (gnus-header-value)
7803                     (setq end (match-end 0))
7804                     (save-excursion
7805                       (setq ref 
7806                             (downcase
7807                              (buffer-substring
7808                               (progn 
7809                                 (end-of-line)
7810                                 (search-backward ">" end t)
7811                                 (1+ (point)))
7812                               (progn
7813                                 (search-backward "<" end t)
7814                                 (point)))))))
7815                 ;; Get the references from the in-reply-to header if there
7816                 ;; were no references and the in-reply-to header looks
7817                 ;; promising. 
7818                 (if (and (search-forward "\nin-reply-to: " nil t)
7819                          (setq in-reply-to (gnus-header-value))
7820                          (string-match "<[^>]+>" in-reply-to))
7821                     (prog1
7822                         (setq ref (substring in-reply-to (match-beginning 0)
7823                                              (match-end 0)))
7824                       (setq ref (downcase ref))))
7825                 (setq ref "")))
7826             ;; Chars.
7827             0
7828             ;; Lines.
7829             (progn
7830               (goto-char p)
7831               (if (search-forward "\nlines: " nil t)
7832                   (if (numberp (setq lines (read cur)))
7833                       lines 0)
7834                 0))
7835             ;; Xref.
7836             (progn
7837               (goto-char p)
7838               (and (search-forward "\nxref: " nil t)
7839                    (gnus-header-value)))))
7840           (if (and gnus-nocem-hashtb
7841                    (gnus-gethash id gnus-nocem-hashtb))
7842               ;; Banned article.
7843               (setq header nil)
7844             ;; We do the threading while we read the headers. The
7845             ;; message-id and the last reference are both entered into
7846             ;; the same hash table. Some tippy-toeing around has to be
7847             ;; done in case an article has arrived before the article
7848             ;; which it refers to.
7849             (if (boundp (setq id-dep (intern (downcase id) dependencies)))
7850                 (if (car (symbol-value id-dep))
7851                     ;; An article with this Message-ID has already
7852                     ;; been seen, so we ignore this one, except we add
7853                     ;; any additional Xrefs (in case the two articles
7854                     ;; came from different servers).
7855                     (progn
7856                       (mail-header-set-xref 
7857                        (car (symbol-value id-dep))
7858                        (concat (or (mail-header-xref 
7859                                     (car (symbol-value id-dep))) "")
7860                                (or (mail-header-xref header) "")))
7861                       (setq header nil))
7862                   (setcar (symbol-value id-dep) header))
7863               (set id-dep (list header))))
7864           (if header
7865               (progn
7866                 (if (boundp (setq ref-dep (intern ref dependencies)))
7867                     (setcdr (symbol-value ref-dep) 
7868                             (nconc (cdr (symbol-value ref-dep))
7869                                    (list (symbol-value id-dep))))
7870                   (set ref-dep (list nil (symbol-value id-dep))))
7871                 (setq headers (cons header headers))))
7872           (goto-char (point-max))
7873           (widen))
7874         (nreverse headers)))))
7875
7876 ;; The following macros and functions were written by Felix Lee
7877 ;; <flee@cse.psu.edu>. 
7878
7879 (defmacro gnus-nov-read-integer ()
7880   '(prog1
7881        (if (= (following-char) ?\t)
7882            0
7883          (let ((num (condition-case nil (read buffer) (error nil))))
7884            (if (numberp num) num 0)))
7885      (or (eobp) (forward-char 1))))
7886
7887 (defmacro gnus-nov-skip-field ()
7888   '(search-forward "\t" eol 'move))
7889
7890 (defmacro gnus-nov-field ()
7891   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
7892
7893 ;; Goes through the xover lines and returns a list of vectors
7894 (defun gnus-get-newsgroup-headers-xover (sequence)
7895   "Parse the news overview data in the server buffer, and return a
7896 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
7897   ;; Get the Xref when the users reads the articles since most/some
7898   ;; NNTP servers do not include Xrefs when using XOVER.
7899   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
7900   (let ((cur nntp-server-buffer)
7901         (dependencies gnus-newsgroup-dependencies)
7902         number headers header)
7903     (save-excursion
7904       (set-buffer nntp-server-buffer)
7905       (goto-char (point-min))
7906       (while (and sequence (not (eobp)))
7907         (setq number (read cur))
7908         (while (and sequence (< (car sequence) number))
7909           (setq sequence (cdr sequence)))
7910         (and sequence 
7911              (eq number (car sequence))
7912              (progn
7913                (setq sequence (cdr sequence))
7914                (if (setq header 
7915                          (inline (gnus-nov-parse-line number dependencies)))
7916                    (setq headers (cons header headers)))))
7917         (forward-line 1))
7918       (setq headers (nreverse headers)))
7919     headers))
7920
7921 ;; This function has to be called with point after the article number
7922 ;; on the beginning of the line.
7923 (defun gnus-nov-parse-line (number dependencies)
7924   (let ((none 0)
7925         (eol (gnus-point-at-eol)) 
7926         (buffer (current-buffer))
7927         header ref id id-dep ref-dep)
7928
7929     ;; overview: [num subject from date id refs chars lines misc]
7930     (narrow-to-region (point) eol)
7931     (or (eobp) (forward-char))
7932
7933     (condition-case nil
7934         (setq header
7935               (vector 
7936                number                   ; number
7937                (gnus-nov-field)         ; subject
7938                (gnus-nov-field)         ; from
7939                (gnus-nov-field)         ; date
7940                (setq id (or (gnus-nov-field)
7941                             (concat "none+"
7942                                     (int-to-string 
7943                                      (setq none (1+ none)))))) ; id
7944                (progn
7945                  (save-excursion
7946                    (let ((beg (point)))
7947                      (search-forward "\t" eol)
7948                      (if (search-backward ">" beg t)
7949                          (setq ref 
7950                                (downcase 
7951                                 (buffer-substring 
7952                                  (1+ (point))
7953                                  (progn
7954                                    (search-backward "<" beg t)
7955                                    (point)))))
7956                        (setq ref nil))))
7957                  (gnus-nov-field))      ; refs
7958                (gnus-nov-read-integer)  ; chars
7959                (gnus-nov-read-integer)  ; lines
7960                (if (= (following-char) ?\n)
7961                    nil
7962                  (gnus-nov-field))      ; misc
7963                ))
7964       (error (progn 
7965                (ding)
7966                (message "Strange nov line.")
7967                (setq header nil)
7968                (goto-char eol))))
7969
7970     (widen)
7971
7972     ;; We build the thread tree.
7973     (and header
7974          (if (and gnus-nocem-hashtb
7975                   (gnus-gethash id gnus-nocem-hashtb))
7976              ;; Banned article.
7977              (setq header nil)
7978            (if (boundp (setq id-dep (intern (downcase id) dependencies)))
7979                (if (car (symbol-value id-dep))
7980                    ;; An article with this Message-ID has already been seen,
7981                    ;; so we ignore this one, except we add any additional
7982                    ;; Xrefs (in case the two articles came from different
7983                    ;; servers.
7984                    (progn
7985                      (mail-header-set-xref 
7986                       (car (symbol-value id-dep))
7987                       (concat (or (mail-header-xref 
7988                                    (car (symbol-value id-dep))) "")
7989                               (or (mail-header-xref header) "")))
7990                      (setq header nil))
7991                  (setcar (symbol-value id-dep) header))
7992              (set id-dep (list header)))))
7993     (if header
7994         (progn
7995           (if (boundp (setq ref-dep (intern (or ref "none") 
7996                                             dependencies)))
7997               (setcdr (symbol-value ref-dep) 
7998                       (nconc (cdr (symbol-value ref-dep))
7999                              (list (symbol-value id-dep))))
8000             (set ref-dep (list nil (symbol-value id-dep))))))
8001     header))
8002
8003 (defun gnus-article-get-xrefs ()
8004   "Fill in the Xref value in `gnus-current-headers', if necessary.
8005 This is meant to be called in `gnus-article-internal-prepare-hook'."
8006   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
8007                                  gnus-current-headers)))
8008     (or (not gnus-use-cross-reference)
8009         (not headers)
8010         (and (mail-header-xref headers)
8011              (not (string= (mail-header-xref headers) "")))
8012         (let ((case-fold-search t)
8013               xref)
8014           (save-restriction
8015             (gnus-narrow-to-headers)
8016             (goto-char (point-min))
8017             (if (or (and (eq (downcase (following-char)) ?x)
8018                          (looking-at "Xref:"))
8019                     (search-forward "\nXref:" nil t))
8020                 (progn
8021                   (goto-char (1+ (match-end 0)))
8022                   (setq xref (buffer-substring (point) 
8023                                                (progn (end-of-line) (point))))
8024                   (mail-header-set-xref headers xref))))))))
8025
8026 (defun gnus-summary-insert-subject (id)
8027   "Find article ID and insert the summary line for that article."
8028   (let ((header (gnus-read-header id))
8029         number)
8030     (if (not header)
8031         () ; We couldn't fetch ID.
8032       ;; Rebuild the thread that this article is part of and go to the
8033       ;; article we have fetched.
8034       (gnus-rebuild-thread (mail-header-id header))
8035       (gnus-summary-goto-subject (setq number (mail-header-number header)))
8036       (and (> number 0)
8037            (progn
8038              ;; We have to update the boundaries, possibly.
8039              (and (> number gnus-newsgroup-end)
8040                   (setq gnus-newsgroup-end number))
8041              (and (< number gnus-newsgroup-begin)
8042                   (setq gnus-newsgroup-begin number))
8043              (setq gnus-newsgroup-unselected
8044                    (delq number gnus-newsgroup-unselected))))
8045       ;; Report back a success.
8046       number)))
8047
8048 (defun gnus-summary-work-articles (n)
8049   "Return a list of articles to be worked upon. The prefix argument,
8050 the list of process marked articles, and the current article will be
8051 taken into consideration."
8052   (let (articles article)
8053     (if (and n (numberp n))
8054         (let ((backward (< n 0))
8055               (n (abs n)))
8056           (save-excursion
8057             (while 
8058                 (and (> n 0)
8059                      (setq articles 
8060                            (cons (setq article (gnus-summary-article-number))
8061                                  articles))
8062                      (if backward
8063                          (gnus-summary-find-prev nil article)
8064                        (gnus-summary-find-next nil article)))
8065               (setq n (1- n))))
8066           (sort articles (function <)))
8067       (or (reverse gnus-newsgroup-processable)
8068           (list (gnus-summary-article-number))))))
8069
8070 (defun gnus-summary-search-group (&optional backward use-level)
8071   "Search for next unread newsgroup.
8072 If optional argument BACKWARD is non-nil, search backward instead."
8073   (save-excursion
8074     (set-buffer gnus-group-buffer)
8075     (if (gnus-group-search-forward 
8076          backward nil (if use-level (gnus-group-group-level) nil))
8077         (gnus-group-group-name))))
8078
8079 (defun gnus-summary-best-group (&optional exclude-group)
8080   "Find the name of the best unread group.
8081 If EXCLUDE-GROUP, do not go to this group."
8082   (save-excursion
8083     (set-buffer gnus-group-buffer)
8084     (save-excursion
8085       (gnus-group-best-unread-group exclude-group))))
8086
8087 (defun gnus-summary-find-next (&optional unread article backward)
8088   (if backward (gnus-summary-find-prev)
8089     (let* ((article (or article (gnus-summary-article-number)))
8090            (arts (cdr (gnus-data-find-list article)))
8091            result)
8092       (if (setq result
8093                 (if unread
8094                     (progn
8095                       (while arts
8096                         (and (gnus-data-unread-p (car arts))
8097                              (setq result (car arts)
8098                                    arts nil))
8099                         (setq arts (cdr arts)))
8100                       result)
8101                   (car arts)))
8102           (progn
8103             (goto-char (gnus-data-pos result))
8104             (gnus-data-number result))))))
8105
8106 (defun gnus-summary-find-prev (&optional unread article)
8107   (let* ((article (or article (gnus-summary-article-number)))
8108          result arts)
8109     (setq arts (cdr (gnus-data-find-list article (gnus-data-list 'rev))))
8110     (if (setq result
8111               (if unread
8112                   (progn
8113                     (while arts
8114                       (and (gnus-data-unread-p (car arts))
8115                            (setq result (car arts)
8116                                  arts nil))
8117                       (setq arts (cdr arts)))
8118                     result)
8119                 (car arts)))
8120         (progn
8121           (goto-char (gnus-data-pos result))
8122           (gnus-data-number result)))))
8123
8124 (defun gnus-summary-find-subject (subject &optional unread backward article)
8125   (let* ((article (or article (gnus-summary-article-number)))
8126          (articles (gnus-data-list backward))
8127          (arts (cdr (gnus-data-find-list article articles)))
8128          result)
8129     (while arts
8130       (and (or (not unread)
8131                (gnus-data-unread-p (car arts)))
8132            (vectorp (gnus-data-header (car arts)))
8133            (gnus-subject-equal 
8134             subject (mail-header-subject (gnus-data-header (car arts))))
8135            (setq result (car arts)
8136                  arts nil))
8137       (setq arts (cdr arts)))
8138     (and result
8139          (goto-char (gnus-data-pos result))
8140          (gnus-data-number result))))
8141
8142 (defun gnus-summary-search-forward (&optional unread subject backward)
8143   (cond (subject
8144          (gnus-summary-find-subject subject unread backward))
8145         (backward
8146          (gnus-summary-find-prev unread))
8147         (t
8148          (gnus-summary-find-next unread))))
8149
8150 (defun gnus-summary-recenter ()
8151   "Center point in the summary window.
8152 If `gnus-auto-center-summary' is nil, or the article buffer isn't
8153 displayed, no centering will be performed." 
8154   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
8155   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
8156   (let* ((top (cond ((< (window-height) 4) 0)
8157                     ((< (window-height) 7) 1)
8158                     (t 2)))
8159          (height (1- (window-height)))
8160          (bottom (save-excursion (goto-char (point-max))
8161                                  (forward-line (- height))
8162                                  (point)))
8163          (window (get-buffer-window (current-buffer))))
8164     (and 
8165      ;; The user has to want it,
8166      gnus-auto-center-summary 
8167      ;; the article buffer must be displayed,
8168      (get-buffer-window gnus-article-buffer)
8169      ;; Set the window start to either `bottom', which is the biggest
8170      ;; possible valid number, or the second line from the top,
8171      ;; whichever is the least.
8172      (set-window-start
8173       window (min bottom (save-excursion (forward-line (- top)) (point)))))))
8174
8175 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
8176 (defun gnus-short-group-name (group &optional levels)
8177   "Collapse GROUP name LEVELS."
8178   (let* ((name "") (foreign "") (depth -1) (skip 1)
8179          (levels (or levels
8180                      (progn
8181                        (while (string-match "\\." group skip)
8182                          (setq skip (match-end 0)
8183                                depth (+ depth 1)))
8184                        depth))))
8185     (if (string-match ":" group)
8186         (setq foreign (substring group 0 (match-end 0))
8187               group (substring group (match-end 0))))
8188     (while group
8189       (if (and (string-match "\\." group) (> levels 0))
8190           (setq name (concat name (substring group 0 1))
8191                 group (substring group (match-end 0))
8192                 levels (- levels 1)
8193                 name (concat name "."))
8194         (setq name (concat foreign name group)
8195               group nil)))
8196     name))
8197
8198 (defun gnus-summary-jump-to-group (newsgroup)
8199   "Move point to NEWSGROUP in group mode buffer."
8200   ;; Keep update point of group mode buffer if visible.
8201   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
8202       (save-window-excursion
8203         ;; Take care of tree window mode.
8204         (if (get-buffer-window gnus-group-buffer)
8205             (pop-to-buffer gnus-group-buffer))
8206         (gnus-group-jump-to-group newsgroup))
8207     (save-excursion
8208       ;; Take care of tree window mode.
8209       (if (get-buffer-window gnus-group-buffer)
8210           (pop-to-buffer gnus-group-buffer)
8211         (set-buffer gnus-group-buffer))
8212       (gnus-group-jump-to-group newsgroup))))
8213
8214 ;; This function returns a list of article numbers based on the
8215 ;; difference between the ranges of read articles in this group and
8216 ;; the range of active articles.
8217 (defun gnus-list-of-unread-articles (group)
8218   (let* ((read (nth 2 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
8219          (active (gnus-gethash group gnus-active-hashtb))
8220          (last (cdr active))
8221          first nlast unread)
8222     ;; If none are read, then all are unread. 
8223     (if (not read)
8224         (setq first (car active))
8225       ;; If the range of read articles is a single range, then the
8226       ;; first unread article is the article after the last read
8227       ;; article. Sounds logical, doesn't it?
8228       (if (not (listp (cdr read)))
8229           (setq first (1+ (cdr read)))
8230         ;; `read' is a list of ranges.
8231         (if (/= (setq nlast (or (and (numberp (car read)) (car read)) 
8232                                 (car (car read)))) 1)
8233             (setq first 1))
8234         (while read
8235           (if first 
8236               (while (< first nlast)
8237                 (setq unread (cons first unread))
8238                 (setq first (1+ first))))
8239           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
8240           (setq nlast (if (atom (car (cdr read))) 
8241                           (car (cdr read))
8242                         (car (car (cdr read)))))
8243           (setq read (cdr read)))))
8244     ;; And add the last unread articles.
8245     (while (<= first last)
8246       (setq unread (cons first unread))
8247       (setq first (1+ first)))
8248     ;; Return the list of unread articles.
8249     (nreverse unread)))
8250
8251 (defun gnus-list-of-read-articles (group)
8252   (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
8253         (active (gnus-gethash group gnus-active-hashtb)))
8254     (and info active
8255          (gnus-sorted-complement 
8256           (gnus-uncompress-range active) 
8257           (gnus-list-of-unread-articles group)))))
8258
8259 ;; Various summary commands
8260
8261 (defun gnus-summary-universal-argument ()
8262   "Perform any operation on all articles marked with the process mark."
8263   (interactive)
8264   (gnus-set-global-variables)
8265   (let ((articles (reverse gnus-newsgroup-processable))
8266         func)
8267     (or articles (error "No articles marked"))
8268     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
8269         (error "Undefined key"))
8270     (while articles
8271       (gnus-summary-goto-subject (car articles))
8272       (command-execute func)
8273       (gnus-summary-remove-process-mark (car articles))
8274       (setq articles (cdr articles)))))
8275
8276 (defun gnus-summary-toggle-truncation (&optional arg)
8277   "Toggle truncation of summary lines.
8278 With arg, turn line truncation on iff arg is positive."
8279   (interactive "P")
8280   (setq truncate-lines
8281         (if (null arg) (not truncate-lines)
8282           (> (prefix-numeric-value arg) 0)))
8283   (redraw-display))
8284
8285 (defun gnus-summary-reselect-current-group (&optional all)
8286   "Once exit and then reselect the current newsgroup.
8287 The prefix argument ALL means to select all articles."
8288   (interactive "P")
8289   (gnus-set-global-variables)
8290   (let ((current-subject (gnus-summary-article-number))
8291         (group gnus-newsgroup-name))
8292     (setq gnus-newsgroup-begin nil)
8293     (gnus-summary-exit t)
8294     ;; We have to adjust the point of group mode buffer because the
8295     ;; current point was moved to the next unread newsgroup by
8296     ;; exiting.
8297     (gnus-summary-jump-to-group group)
8298     (gnus-group-read-group all t)
8299     (gnus-summary-goto-subject current-subject)))
8300
8301 (defun gnus-summary-rescan-group (&optional all)
8302   "Exit the newsgroup, ask for new articles, and select the newsgroup."
8303   (interactive "P")
8304   (gnus-set-global-variables)
8305   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
8306   (let ((group gnus-newsgroup-name))
8307     (gnus-summary-exit)
8308     (gnus-summary-jump-to-group group)
8309     (save-excursion
8310       (set-buffer gnus-group-buffer)
8311       (gnus-group-get-new-news-this-group 1))
8312     (gnus-summary-jump-to-group group)
8313     (gnus-group-read-group all)))
8314
8315 (defun gnus-summary-update-info ()
8316   (let* ((group gnus-newsgroup-name))
8317     (if gnus-newsgroup-kill-headers
8318         (setq gnus-newsgroup-killed
8319               (gnus-compress-sequence
8320                (nconc
8321                 (gnus-set-sorted-intersection
8322                  (gnus-uncompress-range gnus-newsgroup-killed)
8323                  (setq gnus-newsgroup-unselected
8324                        (sort gnus-newsgroup-unselected '<)))
8325                 (setq gnus-newsgroup-unreads
8326                       (sort gnus-newsgroup-unreads '<))) t)))
8327     (or (listp (cdr gnus-newsgroup-killed))
8328         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
8329     (let ((headers gnus-newsgroup-headers))
8330       (gnus-close-group group)
8331       (run-hooks 'gnus-exit-group-hook)
8332       (gnus-update-read-articles 
8333        group gnus-newsgroup-unreads gnus-newsgroup-unselected 
8334        gnus-newsgroup-marked
8335        t gnus-newsgroup-replied gnus-newsgroup-expirable
8336        gnus-newsgroup-killed gnus-newsgroup-dormant
8337        gnus-newsgroup-bookmarks 
8338        (and gnus-save-score gnus-newsgroup-scored))
8339       (and gnus-use-cross-reference
8340            (gnus-mark-xrefs-as-read 
8341             group headers gnus-newsgroup-unreads gnus-newsgroup-expirable
8342             gnus-newsgroup-marked gnus-newsgroup-dormant))
8343       ;; Do adaptive scoring, and possibly save score files.
8344       (and gnus-newsgroup-adaptive
8345            (gnus-score-adaptive))
8346       (and gnus-use-scoring 
8347            (fboundp 'gnus-score-save)
8348            (funcall 'gnus-score-save))
8349       ;; Do not switch windows but change the buffer to work.
8350       (set-buffer gnus-group-buffer)
8351       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
8352           (gnus-group-update-group group)))))
8353   
8354 (defun gnus-summary-exit (&optional temporary)
8355   "Exit reading current newsgroup, and then return to group selection mode.
8356 gnus-exit-group-hook is called with no arguments if that value is non-nil."
8357   (interactive)
8358   (gnus-set-global-variables)
8359   (gnus-kill-save-kill-buffer)
8360   (let* ((group gnus-newsgroup-name)
8361          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
8362                                                   gnus-newsgroup-name))))
8363          (mode major-mode)
8364          (buf (current-buffer)))
8365     (run-hooks 'gnus-summary-prepare-exit-hook)
8366     ;; Make all changes in this group permanent.
8367     (gnus-summary-update-info)          
8368     (set-buffer buf)
8369     (and gnus-use-cache (gnus-cache-possibly-remove-articles))
8370     ;; Make sure where I was, and go to next newsgroup.
8371     (set-buffer gnus-group-buffer)
8372     (or quit-config
8373         (progn
8374           (gnus-group-jump-to-group group)
8375           (gnus-group-next-unread-group 1)))
8376     (if temporary
8377         nil                             ;Nothing to do.
8378       ;; We set all buffer-local variables to nil. It is unclear why
8379       ;; this is needed, but if we don't, buffer-local variables are
8380       ;; not garbage-collected, it seems. This would the lead to en
8381       ;; ever-growing Emacs.
8382       (set-buffer buf)
8383       (gnus-summary-clear-local-variables)
8384       ;; We clear the global counterparts of the buffer-local
8385       ;; variables as well, just to be on the safe side.
8386       (gnus-configure-windows 'group 'force)
8387       (gnus-summary-clear-local-variables)
8388       ;; Return to group mode buffer. 
8389       (if (eq mode 'gnus-summary-mode)
8390           (gnus-kill-buffer buf))
8391       (if (get-buffer gnus-article-buffer)
8392           (bury-buffer gnus-article-buffer))
8393       (setq gnus-current-select-method gnus-select-method)
8394       (pop-to-buffer gnus-group-buffer)
8395       (if (not quit-config)
8396           (progn
8397             (gnus-group-jump-to-group group)
8398             (gnus-group-next-unread-group 1))
8399         (if (not (buffer-name (car quit-config)))
8400             (gnus-configure-windows 'group 'force)
8401           (set-buffer (car quit-config))
8402           (and (eq major-mode 'gnus-summary-mode)
8403                (gnus-set-global-variables))
8404           (gnus-configure-windows (cdr quit-config))))
8405       (run-hooks 'gnus-summary-exit-hook))))
8406
8407 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
8408 (defun gnus-summary-exit-no-update (&optional no-questions)
8409   "Quit reading current newsgroup without updating read article info."
8410   (interactive)
8411   (gnus-set-global-variables)
8412   (let* ((group gnus-newsgroup-name)
8413          (quit-config (nth 1 (assoc 'quit-config 
8414                                     (gnus-find-method-for-group group)))))
8415     (if (or no-questions
8416             gnus-expert-user
8417             (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
8418         (progn
8419           (gnus-close-group group)
8420           (gnus-summary-clear-local-variables)
8421           (set-buffer gnus-group-buffer)
8422           (gnus-summary-clear-local-variables)
8423           ;; Return to group selection mode.
8424           (gnus-configure-windows 'group 'force)
8425           (if (get-buffer gnus-summary-buffer)
8426               (kill-buffer gnus-summary-buffer))
8427           (if (get-buffer gnus-article-buffer)
8428               (bury-buffer gnus-article-buffer))
8429           (if (equal (gnus-group-group-name) group)
8430               (gnus-group-next-unread-group 1))
8431           (if quit-config
8432               (progn
8433                 (if (not (buffer-name (car quit-config)))
8434                     (gnus-configure-windows 'group 'force)
8435                   (set-buffer (car quit-config))
8436                   (and (eq major-mode 'gnus-summary-mode)
8437                        (gnus-set-global-variables))
8438                   (gnus-configure-windows (cdr quit-config)))))))))
8439
8440 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
8441 (defun gnus-summary-fetch-faq (&optional faq-dir)
8442   "Fetch the FAQ for the current group.
8443 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
8444 in."
8445   (interactive 
8446    (list
8447     (if current-prefix-arg
8448         (completing-read 
8449          "Faq dir: " (and (listp gnus-group-faq-directory)
8450                           gnus-group-faq-directory)))))
8451   (let (gnus-faq-buffer)
8452     (and (setq gnus-faq-buffer 
8453                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
8454          (gnus-configure-windows 'summary-faq))))
8455
8456 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
8457 (defun gnus-summary-describe-group (&optional force)
8458   "Describe the current newsgroup."
8459   (interactive "P")
8460   (gnus-group-describe-group force gnus-newsgroup-name))
8461
8462 (defun gnus-summary-describe-briefly ()
8463   "Describe summary mode commands briefly."
8464   (interactive)
8465   (gnus-message 6
8466                 (substitute-command-keys "\\<gnus-summary-mode-map>\\[gnus-summary-next-page]:Select  \\[gnus-summary-next-unread-article]:Forward  \\[gnus-summary-prev-unread-article]:Backward  \\[gnus-summary-exit]:Exit  \\[gnus-info-find-node]:Run Info  \\[gnus-summary-describe-briefly]:This help")))
8467
8468 ;; Walking around group mode buffer from summary mode.
8469
8470 (defun gnus-summary-next-group (&optional no-article target-group backward)
8471   "Exit current newsgroup and then select next unread newsgroup.
8472 If prefix argument NO-ARTICLE is non-nil, no article is selected
8473 initially. If NEXT-GROUP, go to this group. If BACKWARD, go to
8474 previous group instead."
8475   (interactive "P")
8476   (gnus-set-global-variables)
8477   (let ((current-group gnus-newsgroup-name)
8478         (current-buffer (current-buffer))
8479         entered)
8480     ;; First we semi-exit this group to update Xrefs and all variables.
8481     ;; We can't do a real exit, because the window conf must remain
8482     ;; the same in case the user is prompted for info, and we don't
8483     ;; want the window conf to change before that...
8484     (gnus-summary-exit t)
8485     (while (not entered)
8486       ;; Then we find what group we are supposed to enter.
8487       (set-buffer gnus-group-buffer)
8488       (gnus-group-jump-to-group current-group)
8489       (setq target-group 
8490             (or target-group        
8491                 (if (eq gnus-keep-same-level 'best) 
8492                     (gnus-summary-best-group gnus-newsgroup-name)
8493                   (gnus-summary-search-group backward gnus-keep-same-level))))
8494       (if (not target-group)
8495           ;; There are no further groups, so we return to the group
8496           ;; buffer.
8497           (progn
8498             (gnus-message 5 "Returning to the group buffer")
8499             (setq entered t)
8500             (set-buffer current-buffer)
8501             (gnus-summary-exit))
8502         ;; We try to enter the target group.
8503         (gnus-group-jump-to-group target-group)
8504         (let ((unreads (gnus-group-group-unread)))
8505           (if (and (or (eq t unreads)
8506                        (and unreads (not (zerop unreads))))
8507                    (gnus-summary-read-group
8508                     target-group nil no-article current-buffer))
8509               (setq entered t)
8510             (setq current-group target-group
8511                   target-group nil)))))))
8512
8513 (defun gnus-summary-prev-group (&optional no-article)
8514   "Exit current newsgroup and then select previous unread newsgroup.
8515 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
8516   (interactive "P")
8517   (gnus-summary-next-group no-article nil t))
8518
8519 ;; Walking around summary lines.
8520
8521 (defun gnus-summary-first-subject (&optional unread)
8522   "Go to the first unread subject.
8523 If UNREAD is non-nil, go to the first unread article.
8524 Returns the article selected or nil if there are no unread articles."
8525   (interactive "P")
8526   (prog1
8527       (cond 
8528        ;; Empty summary.
8529        ((null gnus-newsgroup-data)
8530         (gnus-message 3 "No articles in the group")
8531         nil)
8532        ;; Pick the first article.
8533        ((not unread)
8534         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
8535         (gnus-data-number (car gnus-newsgroup-data)))
8536        ;; No unread articles.
8537        ((null gnus-newsgroup-unreads)
8538         (gnus-message 3 "No more unread articles")
8539         nil)
8540        ;; Find the first unread article.
8541        (t
8542         (let ((data gnus-newsgroup-data))
8543           (while (and data
8544                       (not (gnus-data-unread-p (car data))))
8545             (setq data (cdr data)))
8546           (if data
8547               (progn
8548                 (goto-char (gnus-data-pos (car data)))
8549                 (gnus-data-number (car data)))))))
8550     (gnus-summary-position-point)))
8551
8552 (defun gnus-summary-next-subject (n &optional unread dont-display)
8553   "Go to next N'th summary line.
8554 If N is negative, go to the previous N'th subject line.
8555 If UNREAD is non-nil, only unread articles are selected.
8556 The difference between N and the actual number of steps taken is
8557 returned."
8558   (interactive "p")
8559   (let ((backward (< n 0))
8560         (n (abs n)))
8561     (while (and (> n 0)
8562                 (if backward
8563                     (gnus-summary-find-prev unread)
8564                   (gnus-summary-find-next unread)))
8565       (setq n (1- n)))
8566     (if (/= 0 n) (gnus-message 7 "No more%s articles"
8567                                (if unread " unread" "")))
8568     (or dont-display
8569         (progn
8570           (gnus-summary-recenter)
8571           (gnus-summary-position-point)))
8572     n))
8573
8574 (defun gnus-summary-next-unread-subject (n)
8575   "Go to next N'th unread summary line."
8576   (interactive "p")
8577   (gnus-summary-next-subject n t))
8578
8579 (defun gnus-summary-prev-subject (n &optional unread)
8580   "Go to previous N'th summary line.
8581 If optional argument UNREAD is non-nil, only unread article is selected."
8582   (interactive "p")
8583   (gnus-summary-next-subject (- n) unread))
8584
8585 (defun gnus-summary-prev-unread-subject (n)
8586   "Go to previous N'th unread summary line."
8587   (interactive "p")
8588   (gnus-summary-next-subject (- n) t))
8589
8590 (defun gnus-summary-goto-subject (article &optional force)
8591   "Go the subject line of ARTICLE.
8592 If FORCE, also allow jumping to articles not currently shown."
8593   (let ((b (point))
8594         (data (gnus-data-find article)))
8595     ;; We read in the article if we have to.
8596     (and (not data) 
8597          force
8598          (gnus-summary-insert-subject article)
8599          (setq data (gnus-data-find article)))
8600     (goto-char b)
8601     (if (not data)
8602         (progn
8603           (message "Can't find article %d" article)
8604           nil)
8605       (goto-char (gnus-data-pos data))
8606       article)))
8607
8608 ;; Walking around summary lines with displaying articles.
8609
8610 (defun gnus-summary-expand-window (&optional arg)
8611   "Make the summary buffer take up the entire Emacs frame.
8612 Given a prefix, will force an `article' buffer configuration."
8613   (interactive "P")
8614   (gnus-set-global-variables)
8615   (if arg
8616       (gnus-configure-windows 'article 'force)
8617     (gnus-configure-windows 'summary 'force)))
8618
8619 (defun gnus-summary-display-article (article &optional all-header)
8620   "Display ARTICLE in article buffer."
8621   (gnus-set-global-variables)
8622   (if (null article)
8623       nil
8624     (prog1
8625         (gnus-article-prepare article all-header)
8626       (gnus-summary-show-thread)
8627       (run-hooks 'gnus-select-article-hook)
8628       (gnus-summary-recenter)
8629       (gnus-summary-goto-subject article)
8630       ;; Successfully display article.
8631       (gnus-summary-update-line)
8632       (gnus-article-set-window-start 
8633        (cdr (assq article gnus-newsgroup-bookmarks)))
8634       t)))
8635
8636 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
8637   "Select the current article.
8638 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
8639 non-nil, the article will be re-fetched even if it already present in
8640 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
8641 be displayed."
8642   (let ((article (or article (gnus-summary-article-number)))
8643         (all-headers (not (not all-headers))) ;Must be T or NIL.
8644         did) 
8645     (and (not pseudo) 
8646          (gnus-summary-article-pseudo-p article)
8647          (error "This is a pseudo-article."))
8648     (prog1
8649         (save-excursion
8650           (set-buffer gnus-summary-buffer)
8651           (if (or (null gnus-current-article)
8652                   (null gnus-article-current)
8653                   (null (get-buffer gnus-article-buffer))
8654                   (not (eq article (cdr gnus-article-current)))
8655                   (not (equal (car gnus-article-current) gnus-newsgroup-name))
8656                   force)
8657               ;; The requested article is different from the current article.
8658               (progn
8659                 (gnus-summary-display-article article all-headers)
8660                 (setq did article))
8661             (if (or all-headers gnus-show-all-headers) 
8662                 (gnus-article-show-all-headers))
8663             nil))
8664       (if did 
8665           (gnus-article-set-window-start 
8666            (cdr (assq article gnus-newsgroup-bookmarks)))))))
8667
8668 (defun gnus-summary-set-current-mark (&optional current-mark)
8669   "Obsolete function."
8670   nil)
8671
8672 (defun gnus-summary-next-article (&optional unread subject backward)
8673   "Select the next article.
8674 If UNREAD, only unread articles are selected.
8675 If SUBJECT, only articles with SUBJECT are selected.
8676 If BACKWARD, the previous article is selected instead of the next."
8677   (interactive "P")
8678   (gnus-set-global-variables)
8679   (let (header)
8680     (cond
8681      ;; Is there such an article?
8682      ((and (gnus-summary-search-forward unread subject backward)
8683            (or (gnus-summary-display-article (gnus-summary-article-number))
8684                (eq (gnus-summary-article-mark) gnus-canceled-mark)))
8685       (gnus-summary-position-point))
8686      ;; If not, we try the first unread, if that is wanted.
8687      ((and subject
8688            gnus-auto-select-same
8689            (or (gnus-summary-first-unread-article)
8690                (eq (gnus-summary-article-mark) gnus-canceled-mark)))
8691       (gnus-summary-position-point)
8692       (gnus-message 6 "Wrapped"))
8693      ;; Try to get next/previous article not displayed in this group.
8694      ((and gnus-auto-extend-newsgroup
8695            (not unread) (not subject))
8696       (gnus-summary-goto-article 
8697        (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
8698        nil t))
8699      ;; Go to next/previous group.
8700      (t
8701       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
8702           (gnus-summary-jump-to-group gnus-newsgroup-name))
8703       (let ((cmd last-command-char)
8704             (group 
8705              (if (eq gnus-keep-same-level 'best) 
8706                  (gnus-summary-best-group gnus-newsgroup-name)
8707                (gnus-summary-search-group backward gnus-keep-same-level))))
8708         ;; For some reason, the group window gets selected. We change
8709         ;; it back.  
8710         (select-window (get-buffer-window (current-buffer)))
8711         ;; Keep just the event type of CMD.
8712                                         ;(and (listp cmd) (setq cmd (car cmd)))
8713         ;; Select next unread newsgroup automagically.
8714         (cond 
8715          ((not gnus-auto-select-next)
8716           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
8717          ((or (eq gnus-auto-select-next 'quietly)
8718               (and (eq gnus-auto-select-next 'almost-quietly)
8719                    (gnus-summary-last-article-p)))
8720           ;; Select quietly.
8721           (if (assoc 'quit-config (gnus-find-method-for-group 
8722                                    gnus-newsgroup-name))
8723               (gnus-summary-exit)
8724             (gnus-message 7 "No more%s articles (%s)..."
8725                           (if unread " unread" "") 
8726                           (if group (concat "selecting " group)
8727                             "exiting"))
8728             (gnus-summary-next-group nil group backward)))
8729          (t
8730           (let ((keystrokes '(?\C-n ?\C-p))
8731                 key)
8732             (while (or (null key) (memq key keystrokes))
8733               (gnus-message 
8734                7 "No more%s articles%s" (if unread " unread" "")
8735                (if (and group (not (assoc 'quit-config
8736                                           (gnus-find-method-for-group 
8737                                            gnus-newsgroup-name))))
8738                    (format " (Type %s for %s [%s])"
8739                            (single-key-description cmd) group
8740                            (car (gnus-gethash group gnus-newsrc-hashtb)))
8741                  (format " (Type %s to exit %s)"
8742                          (single-key-description cmd)
8743                          gnus-newsgroup-name)))
8744               ;; Confirm auto selection.
8745               (let* ((event (read-char-exclusive)))
8746                 (setq key (if (listp event) (car event) event))
8747                 (if (memq key keystrokes)
8748                     (let ((obuf (current-buffer)))
8749                       (switch-to-buffer gnus-group-buffer)
8750                       (and group
8751                            (gnus-group-jump-to-group group))
8752                       (condition-case ()
8753                           (execute-kbd-macro (char-to-string key))
8754                         (error (ding) nil))
8755                       (setq group (gnus-group-group-name))
8756                       (switch-to-buffer obuf)))))
8757             (if (equal key cmd)
8758                 (if (or (not group) (assoc 'quit-config
8759                                            (gnus-find-method-for-group
8760                                             gnus-newsgroup-name)))
8761                     (gnus-summary-exit)
8762                   (gnus-summary-next-group nil group backward))
8763               (execute-kbd-macro (char-to-string key)))))))))))
8764
8765 (defun gnus-summary-next-unread-article ()
8766   "Select unread article after current one."
8767   (interactive)
8768   (gnus-summary-next-article t (and gnus-auto-select-same
8769                                     (gnus-summary-article-subject))))
8770
8771 (defun gnus-summary-prev-article (&optional unread subject)
8772   "Select the article after the current one.
8773 If UNREAD is non-nil, only unread articles are selected."
8774   (interactive "P")
8775   (gnus-summary-next-article unread subject t))
8776
8777 (defun gnus-summary-prev-unread-article ()
8778   "Select unred article before current one."
8779   (interactive)
8780   (gnus-summary-prev-article t (and gnus-auto-select-same
8781                                     (gnus-summary-article-subject))))
8782
8783 (defun gnus-summary-next-page (&optional lines circular)
8784   "Show next page of selected article.
8785 If end of article, select next article.
8786 Argument LINES specifies lines to be scrolled up.
8787 If CIRCULAR is non-nil, go to the start of the article instead of 
8788 instead of selecting the next article when reaching the end of the
8789 current article." 
8790   (interactive "P")
8791   (setq gnus-summary-buffer (current-buffer))
8792   (gnus-set-global-variables)
8793   (let ((article (gnus-summary-article-number))
8794         (endp nil))
8795     (gnus-configure-windows 'article)
8796     (if (or (null gnus-current-article)
8797             (null gnus-article-current)
8798             (/= article (cdr gnus-article-current))
8799             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
8800         ;; Selected subject is different from current article's.
8801         (gnus-summary-display-article article)
8802       (gnus-eval-in-buffer-window
8803        gnus-article-buffer
8804        (setq endp (gnus-article-next-page lines)))
8805       (if endp
8806           (cond (circular
8807                  (gnus-summary-beginning-of-article))
8808                 (lines
8809                  (gnus-message 3 "End of message"))
8810                 ((null lines)
8811                  (gnus-summary-next-unread-article)))))
8812     (gnus-summary-recenter)
8813     (gnus-summary-position-point)))
8814
8815 (defun gnus-summary-prev-page (&optional lines)
8816   "Show previous page of selected article.
8817 Argument LINES specifies lines to be scrolled down."
8818   (interactive "P")
8819   (gnus-set-global-variables)
8820   (let ((article (gnus-summary-article-number)))
8821     (gnus-configure-windows 'article)
8822     (if (or (null gnus-current-article)
8823             (null gnus-article-current)
8824             (/= article (cdr gnus-article-current))
8825             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
8826         ;; Selected subject is different from current article's.
8827         (gnus-summary-display-article article)
8828       (gnus-summary-recenter)
8829       (gnus-eval-in-buffer-window gnus-article-buffer
8830                                   (gnus-article-prev-page lines))))
8831   (gnus-summary-position-point))
8832
8833 (defun gnus-summary-scroll-up (lines)
8834   "Scroll up (or down) one line current article.
8835 Argument LINES specifies lines to be scrolled up (or down if negative)."
8836   (interactive "p")
8837   (gnus-set-global-variables)
8838   (gnus-configure-windows 'article)
8839   (or (gnus-summary-select-article nil nil 'pseudo)
8840       (gnus-eval-in-buffer-window 
8841        gnus-article-buffer
8842        (cond ((> lines 0)
8843               (if (gnus-article-next-page lines)
8844                   (gnus-message 3 "End of message")))
8845              ((< lines 0)
8846               (gnus-article-prev-page (- lines))))))
8847   (gnus-summary-recenter)
8848   (gnus-summary-position-point))
8849
8850 (defun gnus-summary-next-same-subject ()
8851   "Select next article which has the same subject as current one."
8852   (interactive)
8853   (gnus-set-global-variables)
8854   (gnus-summary-next-article nil (gnus-summary-article-subject)))
8855
8856 (defun gnus-summary-prev-same-subject ()
8857   "Select previous article which has the same subject as current one."
8858   (interactive)
8859   (gnus-set-global-variables)
8860   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
8861
8862 (defun gnus-summary-next-unread-same-subject ()
8863   "Select next unread article which has the same subject as current one."
8864   (interactive)
8865   (gnus-set-global-variables)
8866   (gnus-summary-next-article t (gnus-summary-article-subject)))
8867
8868 (defun gnus-summary-prev-unread-same-subject ()
8869   "Select previous unread article which has the same subject as current one."
8870   (interactive)
8871   (gnus-set-global-variables)
8872   (gnus-summary-prev-article t (gnus-summary-article-subject)))
8873
8874 (defun gnus-summary-first-unread-article ()
8875   "Select the first unread article. 
8876 Return nil if there are no unread articles."
8877   (interactive)
8878   (gnus-set-global-variables)
8879   (prog1
8880       (if (gnus-summary-first-subject t)
8881           (progn
8882             (gnus-summary-show-thread)
8883             (gnus-summary-first-subject t)
8884             (gnus-summary-display-article (gnus-summary-article-number))))
8885     (gnus-summary-position-point)))
8886
8887 (defun gnus-summary-best-unread-article ()
8888   "Select the unread article with the highest score."
8889   (interactive)
8890   (gnus-set-global-variables)
8891   (let ((best -1000000)
8892         (data gnus-newsgroup-data)
8893         article score)
8894     (while data
8895       (and (gnus-data-unread-p (car data))
8896            (> (setq score 
8897                     (gnus-summary-article-score (gnus-data-number (car data))))
8898               best)
8899            (setq best score
8900                  article (gnus-data-number (car data))))
8901       (setq data (cdr data)))
8902     (if article
8903         (gnus-summary-goto-article article)
8904       (error "No unread articles"))
8905     (gnus-summary-position-point)))
8906
8907 (defun gnus-summary-goto-article (article &optional all-headers force)
8908   "Fetch ARTICLE and display it if it exists.
8909 If ALL-HEADERS is non-nil, no header lines are hidden."
8910   (interactive
8911    (list
8912     (string-to-int
8913      (completing-read 
8914       "Article number: "
8915       (mapcar (lambda (number) (list (int-to-string number)))
8916               gnus-newsgroup-limit)))
8917     current-prefix-arg
8918     t))
8919   (prog1
8920       (if (gnus-summary-goto-subject article force)
8921           (gnus-summary-display-article article all-headers)
8922         (message "Couldn't go to article %s" article) nil)
8923     (gnus-summary-position-point)))
8924
8925 (defun gnus-summary-goto-last-article ()
8926   "Go to the previously read article."
8927   (interactive)
8928   (prog1
8929       (and gnus-last-article
8930            (gnus-summary-goto-article gnus-last-article))
8931     (gnus-summary-position-point)))
8932
8933 (defun gnus-summary-pop-article (number)
8934   "Pop one article off the history and go to the previous.
8935 NUMBER articles will be popped off."
8936   (interactive "p")
8937   (let (to)
8938     (setq gnus-newsgroup-history
8939           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8940     (if to
8941         (gnus-summary-goto-article (car to))
8942       (error "Article history empty")))
8943   (gnus-summary-position-point))
8944
8945 ;; Summary commands and functions for limiting the summary buffer.
8946
8947 (defun gnus-summary-limit-to-articles (n)
8948   "Limit the summary buffer to the next N articles.
8949 If not given a prefix, use the process marked articles instead."
8950   (interactive "P")
8951   (gnus-set-global-variables)
8952   (prog1
8953       (let ((articles (gnus-summary-work-articles n)))
8954         (gnus-summary-limit articles))
8955     (gnus-summary-position-point)))
8956
8957 (defun gnus-summary-pop-limit (&optional total)
8958   "Restore the previous limit.
8959 If given a prefix, remove all limits."
8960   (interactive "P")
8961   (gnus-set-global-variables)
8962   (prog2
8963       (if total (setq gnus-newsgroup-limits 
8964                       (list (mapcar (lambda (h) (mail-header-number h))
8965                                     gnus-newsgroup-headers))))
8966       (gnus-summary-limit nil 'pop)
8967     (gnus-summary-position-point)))
8968
8969 (defun gnus-summary-limit-to-subject (subject)
8970   "Limit the summary buffer to articles that have subjects that match a regexp."
8971   (interactive "sRegexp: ")
8972   (if (equal "" subject)
8973       ()
8974     (prog1
8975         (let ((articles (gnus-summary-find-matching "subject" subject)))
8976           (or articles (error "Found no matches for \"%s\"" subject))
8977           (gnus-summary-limit articles))
8978       (gnus-summary-position-point))))
8979
8980 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8981 (make-obsolete 
8982  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8983
8984 (defun gnus-summary-limit-to-unread (&optional all)
8985   "Limit the summary buffer to articles that are not marked as read.
8986 If ALL is non-nil, limit strictly to unread articles."
8987   (interactive "P")
8988   (if all
8989       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8990     (gnus-summary-limit-to-marks
8991      ;; Concat all the marks that say that an article is read and have
8992      ;; those removed.  
8993      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8994            gnus-killed-mark gnus-kill-file-mark
8995            gnus-low-score-mark gnus-expirable-mark
8996            gnus-canceled-mark gnus-catchup-mark)
8997      'reverse)))
8998
8999 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
9000 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
9001
9002 (defun gnus-summary-limit-to-marks (marks &optional reverse)
9003   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
9004 If REVERSE, limit the summary buffer to articles that are not marked
9005 with MARKS.  MARKS can either be a string of marks or a list of marks. 
9006 Returns how many articles were removed."
9007   (interactive "sMarks: ")
9008   (gnus-set-global-variables)
9009   (prog1
9010       (let ((data gnus-newsgroup-data)
9011             (marks (if (listp marks) marks
9012                      (append marks nil))) ; Transform to list.
9013             articles)
9014         (while data
9015           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
9016                  (memq (gnus-data-mark (car data)) marks))
9017                (setq articles (cons (gnus-data-number (car data)) articles)))
9018           (setq data (cdr data)))
9019         (gnus-summary-limit articles))
9020     (gnus-summary-position-point)))
9021
9022 (defun gnus-summary-limit-to-score (&optional score)
9023   "Limit to articles with score at or above SCORE."
9024   (interactive "P")
9025   (gnus-set-global-variables)
9026   (setq score (if score
9027                   (prefix-numeric-value score)
9028                 (or gnus-summary-default-score 0)))
9029   (let ((data gnus-newsgroup-data)
9030         articles)
9031     (while data
9032       (and (>= (gnus-summary-article-score (gnus-data-number (car data)))
9033                score)
9034            (setq articles (cons (gnus-data-number (car data)) articles)))
9035       (setq data (cdr data))
9036       (prog1
9037           (gnus-summary-limit articles)
9038         (gnus-summary-position-point)))))
9039
9040 (defun gnus-summary-limit-include-dormant ()
9041   "Display all the hidden articles that are marked as dormant."
9042   (interactive)
9043   (gnus-set-global-variables)
9044   (or gnus-newsgroup-dormant 
9045       (error "There are no dormant articles in this group"))
9046   (prog1
9047       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
9048     (gnus-summary-position-point)))
9049
9050 (defun gnus-summary-limit-exclude-dormant ()
9051   "Hide all dormant articles."
9052   (interactive)
9053   (gnus-set-global-variables)
9054   (prog1
9055       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
9056     (gnus-summary-position-point)))
9057
9058 (defun gnus-summary-limit-exclude-childless-dormant ()
9059   "Hide all dormant articles that have no children."
9060   (interactive)
9061   (gnus-set-global-variables)
9062   (let ((data gnus-newsgroup-data)
9063         articles)
9064     ;; Find all articles that are either not dormant or have
9065     ;; children. 
9066     (while data
9067       (and (or (not (= (gnus-data-mark (car data)) gnus-dormant-mark))
9068                (gnus-article-parent-p (gnus-data-number (car data))))
9069            (setq articles (cons (gnus-data-number (car data))
9070                                 articles)))
9071       (setq data (cdr data)))
9072     ;; Do the limiting.
9073     (prog1
9074         (gnus-summary-limit articles)
9075       (gnus-summary-position-point))))
9076  
9077 (defun gnus-summary-limit (articles &optional pop)
9078   (if pop
9079       ;; We pop the previous limit off the stack and use that.
9080       (setq articles (car gnus-newsgroup-limits)
9081             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
9082     ;; We use the new limit, so we push the old limit on the stack. 
9083     (setq gnus-newsgroup-limits 
9084           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
9085   ;; Set the limit.
9086   (setq gnus-newsgroup-limit articles)
9087   (let ((total (length gnus-newsgroup-data))
9088         (data (gnus-data-find-list (gnus-summary-article-number)))
9089         found)
9090     ;; This will do all the work of generating the new summary buffer
9091     ;; according to the new limit.
9092     (gnus-summary-prepare)
9093     ;; Try to return to the article you were at, or on in the
9094     ;; neighborhood.  
9095     (if data
9096         ;; We try to find some article after the current one.
9097         (while data
9098           (and (gnus-summary-goto-subject (gnus-data-number (car data)))
9099                (setq data nil
9100                      found t))
9101           (setq data (cdr data))))
9102     (or found
9103         ;; If there is no data, that means that we were after the last
9104         ;; article. The same goes when we can't find any articles
9105         ;; after the current one.
9106         (progn
9107           (goto-char (point-max))
9108           (gnus-summary-find-prev)))
9109     ;; We return how many articles were removed from the summary
9110     ;; buffer as a result of the new limit.
9111     (- total (length gnus-newsgroup-data))))
9112
9113 (defun gnus-summary-initial-limit ()
9114   "Figure out what the initial limit is supposed to be on group entry.
9115 This entails weeding out unwanted dormants, low-scored articles,
9116 fetch-old-headers verbiage, and so on."
9117   ;; Most groups have nothing to remove.
9118   (if (and (null gnus-newsgroup-dormant)
9119            (not (eq gnus-fetch-old-headers 'some))
9120            (null gnus-summary-expunge-below))
9121       () ; Do nothing.
9122     (setq gnus-newsgroup-limits 
9123           (cons gnus-newsgroup-limit gnus-newsgroup-limits))
9124     (setq gnus-newsgroup-limit nil)
9125     (mapatoms
9126      (lambda (node)
9127        (if (null (car (symbol-value node)))
9128            (let ((nodes (cdr (symbol-value node))))
9129              (while nodes
9130                (gnus-summary-limit-children (car nodes))
9131                (setq nodes (cdr nodes))))))
9132      gnus-newsgroup-dependencies)
9133     (when (not gnus-newsgroup-limit)
9134       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
9135     gnus-newsgroup-limit))
9136
9137 (defun gnus-summary-limit-children (thread)
9138   "Return 1 if this subthread is visible and 0 if it is not."
9139   ;; First we get the number of visible children to this thread.  This
9140   ;; is done by recursing down the thread using this function, so this
9141   ;; will really go down to a leaf article first, before slowly
9142   ;; working its way up towards the root.
9143   (let ((children (apply '+ (mapcar (lambda (th)
9144                                       (gnus-summary-limit-children th))
9145                                     (cdr thread))))
9146         (number (mail-header-number (car thread))))
9147     (if (or 
9148          ;; If this article is dormant and has absolutely no visible
9149          ;; children, then this article isn't visible.
9150          (and (memq number gnus-newsgroup-dormant)
9151               (= children 0))
9152          ;; If this is a "fetch-old-headered" and there is only one
9153          ;; visible child (or less), then we don't want this article. 
9154          (and (eq gnus-fetch-old-headers 'some)
9155               (<= children 1))
9156          ;; If we use expunging, and this article is really
9157          ;; low-scored, then we don't want this article.
9158          (and gnus-summary-expunge-below
9159               (< (or (cdr (assq number gnus-newsgroup-scored)) 
9160                      gnus-summary-default-score)
9161                  gnus-summary-expunge-below)
9162               ;; We increase the expunge-tally here, but that has
9163               ;; nothing to do with the limits, really.
9164               (incf gnus-newsgroup-expunged-tally)))
9165         ;; Nope, invisible article.
9166         0
9167       ;; Ok, this article is to be visible, so we add it to the limit
9168       ;; and return 1.
9169       (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
9170       1)))
9171
9172 ;; Summary article oriented commands
9173
9174 (defun gnus-summary-refer-parent-article (n)
9175   "Refer parent article N times.
9176 The difference between N and the number of articles fetched is returned."
9177   (interactive "p")
9178   (gnus-set-global-variables)
9179   (while 
9180       (and 
9181        (> n 0)
9182        (let* ((header (gnus-summary-article-header))
9183               (ref 
9184                ;; If we try to find the parent of the currently
9185                ;; displayed article, then we take a look at the actual
9186                ;; References header, since this is slightly more
9187                ;; reliable than the References field we got from the
9188                ;; server. 
9189                (if (and (eq (mail-header-number header) 
9190                             (cdr gnus-article-current))
9191                         (equal gnus-newsgroup-name 
9192                                (car gnus-article-current)))
9193                    (save-excursion
9194                      (set-buffer gnus-original-article-buffer)
9195                      (gnus-narrow-to-headers)
9196                      (prog1
9197                          (mail-fetch-field "references")
9198                        (widen)))
9199                  ;; It's not the current article, so we take a bet on
9200                  ;; the value we got from the server. 
9201                  (mail-header-references header))))
9202          (if ref
9203              (or (gnus-summary-refer-article ref)
9204                  (gnus-message 1 "Couldn't find parent"))
9205            (gnus-message 1 "No references in article %d"
9206                          (gnus-summary-article-number))
9207            nil)))
9208     (setq n (1- n)))
9209   (gnus-summary-position-point)
9210   n)
9211
9212 (defun gnus-summary-refer-references ()
9213   "Fetch all articles mentioned in the References header.
9214 Return how many articles were fetched."
9215   (interactive)
9216   (gnus-set-global-variables)
9217   (let ((ref (mail-header-references (gnus-summary-article-header)))
9218         (n 0))
9219     (while (string-match "<[^]*>" ref)
9220       (setq n (1+ n))
9221       (gnus-summary-refer-article 
9222        (substring ref (match-beginning 0) (match-end 0)))
9223       (setq ref (substring ref (match-end 0))))
9224     n))
9225     
9226 (defun gnus-summary-refer-article (message-id)
9227   "Fetch an article specified by MESSAGE-ID."
9228   (interactive "sMessage-ID: ")
9229   (if (or (not (stringp message-id))
9230           (zerop (length message-id)))
9231       ()
9232     ;; Construct the correct Message-ID if necessary.
9233     ;; Suggested by tale@pawl.rpi.edu.
9234     (or (string-match "^<" message-id)
9235         (setq message-id (concat "<" message-id)))
9236     (or (string-match ">$" message-id)
9237         (setq message-id (concat message-id ">")))
9238     (let ((header (car (gnus-gethash (downcase message-id)
9239                                      gnus-newsgroup-dependencies))))
9240       (if header
9241           ;; The article is present in the buffer, to we just go to it.
9242           (gnus-summary-goto-article (mail-header-number header) nil t)
9243         ;; We fetch the article
9244         (let ((gnus-override-method gnus-refer-article-method)
9245               (gnus-ancient-mark gnus-read-mark)
9246               (tmp-point (window-start
9247                           (get-buffer-window gnus-article-buffer)))
9248               number tmp-buf)
9249           (and gnus-refer-article-method
9250                (gnus-check-server gnus-refer-article-method))
9251           (when (setq number (gnus-summary-insert-subject message-id))
9252             (gnus-summary-select-article nil nil nil number)))))))
9253
9254 (defun gnus-summary-enter-digest-group ()
9255   "Enter a digest group based on the current article."
9256   (interactive)
9257   (gnus-set-global-variables)
9258   (gnus-summary-select-article)
9259   ;; We do not want a narrowed article.
9260   (gnus-summary-stop-page-breaking)
9261   (let ((name (format "%s-%d" 
9262                       (gnus-group-prefixed-name 
9263                        gnus-newsgroup-name (list 'nndoc "")) 
9264                       gnus-current-article))
9265         (ogroup gnus-newsgroup-name)
9266         (buf (current-buffer)))
9267     (if (gnus-group-read-ephemeral-group 
9268          name (list 'nndoc name
9269                     (list 'nndoc-address (get-buffer gnus-article-buffer))
9270                     '(nndoc-article-type digest))
9271          t)
9272         (setcdr (nthcdr 4 (nth 2 (gnus-gethash name gnus-newsrc-hashtb)))
9273                 (list (list (cons 'to-group ogroup))))
9274       (switch-to-buffer buf)
9275       (gnus-set-global-variables)
9276       (gnus-configure-windows 'summary)
9277       (gnus-message 3 "Article not a digest?"))))
9278
9279 (defun gnus-summary-isearch-article (&optional regexp-p)
9280   "Do incremental search forward on the current article.
9281 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9282   (interactive "P")
9283   (gnus-set-global-variables)
9284   (gnus-summary-select-article)
9285   (gnus-eval-in-buffer-window 
9286    gnus-article-buffer
9287    (goto-char (point-min))
9288    (isearch-forward regexp-p)))
9289
9290 (defun gnus-summary-search-article-forward (regexp &optional backward)
9291   "Search for an article containing REGEXP forward.
9292 If BACKWARD, search backward instead."
9293   (interactive
9294    (list (read-string
9295           (format "Search article %s (regexp%s): "
9296                   (if current-prefix-arg "backward" "forward")
9297                   (if gnus-last-search-regexp
9298                       (concat ", default " gnus-last-search-regexp)
9299                     "")))
9300          current-prefix-arg))
9301   (gnus-set-global-variables)
9302   (if (string-equal regexp "")
9303       (setq regexp (or gnus-last-search-regexp ""))
9304     (setq gnus-last-search-regexp regexp))
9305   (if (gnus-summary-search-article regexp backward)
9306       (gnus-article-set-window-start 
9307        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
9308     (error "Search failed: \"%s\"" regexp)))
9309
9310 (defun gnus-summary-search-article-backward (regexp)
9311   "Search for an article containing REGEXP backward."
9312   (interactive
9313    (list (read-string
9314           (format "Search article backward (regexp%s): "
9315                   (if gnus-last-search-regexp
9316                       (concat ", default " gnus-last-search-regexp)
9317                     "")))))
9318   (gnus-summary-search-article-forward regexp 'backward))
9319
9320 (defun gnus-summary-search-article (regexp &optional backward)
9321   "Search for an article containing REGEXP.
9322 Optional argument BACKWARD means do search for backward.
9323 gnus-select-article-hook is not called during the search."
9324   (let ((gnus-select-article-hook nil)  ;Disable hook.
9325         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9326         (re-search
9327          (if backward
9328              (function re-search-backward) (function re-search-forward)))
9329         (found nil)
9330         (last nil))
9331     ;; Hidden thread subtrees must be searched for ,too.
9332     (gnus-summary-show-all-threads)
9333     ;; First of all, search current article.
9334     ;; We don't want to read article again from NNTP server nor reset
9335     ;; current point.
9336     (gnus-summary-select-article)
9337     (gnus-message 9 "Searching article: %d..." gnus-current-article)
9338     (setq last gnus-current-article)
9339     (gnus-eval-in-buffer-window
9340      gnus-article-buffer
9341      (save-restriction
9342        (widen)
9343        ;; Begin search from current point.
9344        (setq found (funcall re-search regexp nil t))))
9345     ;; Then search next articles.
9346     (while (and (not found)
9347                 (gnus-summary-display-article 
9348                  (if backward (gnus-summary-find-prev)
9349                    (gnus-summary-find-next))))
9350       (gnus-message 9 "Searching article: %d..." gnus-current-article)
9351       (gnus-eval-in-buffer-window
9352        gnus-article-buffer
9353        (save-restriction
9354          (widen)
9355          (goto-char (if backward (point-max) (point-min)))
9356          (setq found (funcall re-search regexp nil t)))))
9357     (message "")
9358     ;; Adjust article pointer.
9359     (or (eq last gnus-current-article)
9360         (setq gnus-last-article last))
9361     ;; Return T if found such article.
9362     found))
9363
9364 (defun gnus-summary-find-matching (header regexp &optional backward unread
9365                                           not-case-fold)
9366   "Return a list of all articles that match REGEXP on HEADER.
9367 The search stars on the current article and goes forwards unless
9368 BACKWARD is non-nil.  If UNREAD is non-nil, only unread articles will
9369 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9370 in the comparisons."
9371   (let ((data (gnus-data-find-list (gnus-summary-article-number 
9372                                     (gnus-data-list backward))))
9373         (func (intern (concat "gnus-header-" header)))
9374         (case-fold-search (not not-case-fold))
9375         articles d)
9376     (or (fboundp func) (error "%s is not a valid header" header))
9377     (while data
9378       (setq d (car data))
9379       (and (or (not unread)             ; We want all articles...
9380                (gnus-data-unread-p d))  ; Or just unreads.
9381            (vectorp (gnus-data-header d)) ; It's not a pseudo.
9382            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
9383            (setq articles (cons (gnus-data-number d) articles))) ; Success!
9384       (setq data (cdr data)))
9385     (nreverse articles)))
9386     
9387 (defun gnus-summary-execute-command (header regexp command &optional backward)
9388   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9389 If HEADER is an empty string (or nil), the match is done on the entire
9390 article. If BACKWARD (the prefix) is non-nil, search backward instead."
9391   (interactive
9392    (list (let ((completion-ignore-case t))
9393            (completing-read 
9394             "Header name: "
9395             (mapcar (lambda (string) (list string))
9396                     '("Number" "Subject" "From" "Lines" "Date"
9397                       "Message-ID" "Xref" "References"))
9398             nil 'require-match))
9399          (read-string "Regexp: ")
9400          (read-key-sequence "Command: ")
9401          current-prefix-arg))
9402   (gnus-set-global-variables)
9403   ;; Hidden thread subtrees must be searched as well.
9404   (gnus-summary-show-all-threads)
9405   ;; We don't want to change current point nor window configuration.
9406   (save-excursion
9407     (save-window-excursion
9408       (gnus-message 6 "Executing %s..." (key-description command))
9409       ;; We'd like to execute COMMAND interactively so as to give arguments.
9410       (gnus-execute header regexp
9411                     (` (lambda ()
9412                          (call-interactively '(, (key-binding command)))))
9413                     backward)
9414       (gnus-message 6 "Executing %s...done" (key-description command)))))
9415
9416 (defun gnus-summary-beginning-of-article ()
9417   "Scroll the article back to the beginning."
9418   (interactive)
9419   (gnus-set-global-variables)
9420   (gnus-summary-select-article)
9421   (gnus-configure-windows 'article)
9422   (gnus-eval-in-buffer-window
9423    gnus-article-buffer
9424    (widen)
9425    (goto-char (point-min))
9426    (and gnus-break-pages (gnus-narrow-to-page))))
9427
9428 (defun gnus-summary-end-of-article ()
9429   "Scroll to the end of the article."
9430   (interactive)
9431   (gnus-set-global-variables)
9432   (gnus-summary-select-article)
9433   (gnus-configure-windows 'article)
9434   (gnus-eval-in-buffer-window 
9435    gnus-article-buffer
9436    (widen)
9437    (goto-char (point-max))
9438    (recenter -3)
9439    (and gnus-break-pages (gnus-narrow-to-page))))
9440
9441 (defun gnus-summary-show-article (&optional arg)
9442   "Force re-fetching of the current article.
9443 If ARG (the prefix) is non-nil, show the raw article without any
9444 article massaging functions being run."
9445   (interactive "P")
9446   (gnus-set-global-variables)
9447   (if (not arg)
9448       ;; Select the article the normal way.
9449       (gnus-summary-select-article nil 'force)
9450     ;; Bind the article treatment functions to nil.
9451     (let ((gnus-have-all-headers t)
9452           gnus-article-display-hook
9453           gnus-article-prepare-hook
9454           gnus-visual)
9455       (gnus-summary-select-article nil 'force)))
9456   (gnus-configure-windows 'article)
9457   (gnus-summary-position-point))
9458
9459 (defun gnus-summary-verbose-headers (&optional arg)
9460   "Toggle permanent full header display.
9461 If ARG is a positive number, turn header display on.
9462 If ARG is a negative number, turn header display off."
9463   (interactive "P")
9464   (gnus-set-global-variables)
9465   (gnus-summary-toggle-header arg)
9466   (setq gnus-show-all-headers
9467         (cond ((or (not (numberp arg))
9468                    (zerop arg))
9469                (not gnus-show-all-headers))
9470               ((natnump arg)
9471                t))))
9472
9473 (defun gnus-summary-toggle-header (&optional arg)
9474   "Show the headers if they are hidden, or hide them if they are shown.
9475 If ARG is a positive number, show the entire header.
9476 If ARG is a negative number, hide the unwanted header lines."
9477   (interactive "P")
9478   (gnus-set-global-variables)
9479   (save-excursion
9480     (set-buffer gnus-article-buffer)
9481     (let* ((buffer-read-only nil)
9482            (inhibit-point-motion-hooks t) 
9483            (hidden (text-property-any (point-min) (search-forward "\n\n")
9484                                       'invisible t))
9485            e)
9486       (goto-char (point-min))
9487       (delete-region (point) (1- (search-forward "\n\n" nil t)))
9488       (goto-char (point-min))
9489       (save-excursion 
9490         (set-buffer gnus-original-article-buffer)
9491         (goto-char (point-min))
9492         (setq e (1- (search-forward "\n\n"))))
9493       (insert-buffer-substring gnus-original-article-buffer 1 e)
9494       (let ((hook (delete 'gnus-article-hide-headers-if-wanted
9495                           (delete 'gnus-article-hide-headers
9496                                   gnus-article-display-hook))))
9497         (run-hooks 'hook))
9498       (if (or (not hidden) (and (numberp arg) (< arg 0)))
9499           (gnus-article-hide-headers)))))
9500
9501 (defun gnus-summary-show-all-headers ()
9502   "Make all header lines visible."
9503   (interactive)
9504   (gnus-set-global-variables)
9505   (gnus-article-show-all-headers))
9506
9507 (defun gnus-summary-toggle-mime (&optional arg)
9508   "Toggle MIME processing.
9509 If ARG is a positive number, turn MIME processing on."
9510   (interactive "P")
9511   (gnus-set-global-variables)
9512   (setq gnus-show-mime
9513         (if (null arg) (not gnus-show-mime)
9514           (> (prefix-numeric-value arg) 0)))
9515   (gnus-summary-select-article t 'force))
9516
9517 (defun gnus-summary-caesar-message (&optional arg)
9518   "Caesar rotate the current article by 13.
9519 The numerical prefix specifies how manu places to rotate each letter
9520 forward."
9521   (interactive "P")
9522   (gnus-set-global-variables)
9523   (gnus-summary-select-article)
9524   (let ((mail-header-separator ""))
9525     (gnus-eval-in-buffer-window 
9526      gnus-article-buffer
9527      (save-restriction
9528        (widen)
9529        (let ((start (window-start)))
9530          (news-caesar-buffer-body arg)
9531          (set-window-start (get-buffer-window (current-buffer)) start))))))
9532
9533 (defun gnus-summary-stop-page-breaking ()
9534   "Stop page breaking in the current article."
9535   (interactive)
9536   (gnus-set-global-variables)
9537   (gnus-summary-select-article)
9538   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
9539
9540 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
9541
9542 (defun gnus-summary-move-article (&optional n to-newsgroup select-method)
9543   "Move the current article to a different newsgroup.
9544 If N is a positive number, move the N next articles.
9545 If N is a negative number, move the N previous articles.
9546 If N is nil and any articles have been marked with the process mark,
9547 move those articles instead.
9548 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
9549 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
9550 re-spool using this method.
9551 For this function to work, both the current newsgroup and the
9552 newsgroup that you want to move to have to support the `request-move'
9553 and `request-accept' functions. (Ie. mail newsgroups at present.)"
9554   (interactive "P")
9555   (gnus-set-global-variables)
9556   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
9557       (error "The current newsgroup does not support article moving"))
9558   (let ((articles (gnus-summary-work-articles n))
9559         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
9560         art-group to-method sel-met)
9561     (if (and (not to-newsgroup) (not select-method))
9562         (setq to-newsgroup
9563               (completing-read 
9564                (format "Where do you want to move %s? %s"
9565                        (if (> (length articles) 1)
9566                            (format "these %d articles" (length articles))
9567                          "this article")
9568                        (if gnus-current-move-group
9569                            (format "(default %s) " gnus-current-move-group)
9570                          ""))
9571                gnus-active-hashtb nil nil prefix)))
9572     (if to-newsgroup
9573         (progn
9574           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
9575               (setq to-newsgroup (or gnus-current-move-group "")))
9576           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
9577               (gnus-activate-group to-newsgroup)
9578               (error "No such group: %s" to-newsgroup))
9579           (setq gnus-current-move-group to-newsgroup)))
9580     (setq to-method (if select-method (list select-method "")
9581                       (gnus-find-method-for-group to-newsgroup)))
9582     (or (gnus-check-backend-function 'request-accept-article (car to-method))
9583         (error "%s does not support article copying" (car to-method)))
9584     (or (gnus-check-server to-method)
9585         (error "Can't open server %s" (car to-method)))
9586     (gnus-message 6 "Moving to %s: %s..." 
9587                   (or select-method to-newsgroup) articles)
9588     (while articles
9589       (if (setq art-group
9590                 (gnus-request-move-article 
9591                  (car articles)         ; Article to move
9592                  gnus-newsgroup-name    ; From newsgrouo
9593                  (nth 1 (gnus-find-method-for-group 
9594                          gnus-newsgroup-name)) ; Server
9595                  (list 'gnus-request-accept-article 
9596                        (if select-method
9597                            (list 'quote select-method)
9598                          to-newsgroup)
9599                        (not (cdr articles))) ; Accept form
9600                  (not (cdr articles)))) ; Only save nov last time
9601           (let* ((buffer-read-only nil)
9602                  (entry 
9603                   (or
9604                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
9605                    (gnus-gethash 
9606                     (gnus-group-prefixed-name 
9607                      (car art-group) 
9608                      (if select-method (list select-method "")
9609                        (gnus-find-method-for-group to-newsgroup)))
9610                     gnus-newsrc-hashtb)))
9611                  (info (nth 2 entry))
9612                  (article (car articles)))
9613             (gnus-summary-goto-subject article)
9614             (gnus-summary-mark-article article gnus-canceled-mark)
9615             ;; Update the group that has been moved to.
9616             (if (not info)
9617                 ()                      ; This group does not exist yet.
9618               (if (not (memq article gnus-newsgroup-unreads))
9619                   (setcar (cdr (cdr info))
9620                           (gnus-add-to-range (nth 2 info) 
9621                                              (list (cdr art-group)))))
9622
9623               ;; Copy any marks over to the new group.
9624               (let ((marks '((tick . gnus-newsgroup-marked)
9625                              (dormant . gnus-newsgroup-dormant)
9626                              (expire . gnus-newsgroup-expirable)
9627                              (bookmark . gnus-newsgroup-bookmarks)
9628                              (reply . gnus-newsgroup-replied)))
9629                     (to-article (cdr art-group)))
9630
9631                 ;; See whether the article is to be put in the cache.
9632                 (when gnus-use-cache
9633                   (gnus-cache-possibly-enter-article 
9634                    (car info) to-article
9635                    (let ((header (copy-sequence
9636                                   (gnus-summary-article-header article))))
9637                      (mail-header-set-number to-article header)
9638                      header)
9639                    (memq article gnus-newsgroup-marked)
9640                    (memq article gnus-newsgroup-dormant)
9641                    (memq article gnus-newsgroup-unreads)))
9642
9643                 (while marks
9644                   (if (memq article (symbol-value (cdr (car marks))))
9645                       (gnus-add-marked-articles 
9646                        (car info) (car (car marks)) (list to-article) info))
9647                   (setq marks (cdr marks)))))
9648             ;; Update marks.
9649             (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9650             (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9651             (setq gnus-newsgroup-dormant
9652                   (delq article gnus-newsgroup-dormant))
9653             (setq gnus-newsgroup-reads
9654                   (cons (cons article gnus-canceled-mark)
9655                         gnus-newsgroup-reads)))
9656         (gnus-message 1 "Couldn't move article %s" (car articles)))
9657       (gnus-summary-remove-process-mark (car articles))
9658       (setq articles (cdr articles)))))
9659
9660 (defun gnus-summary-respool-article (&optional n respool-method)
9661   "Respool the current article.
9662 The article will be squeezed through the mail spooling process again,
9663 which means that it will be put in some mail newsgroup or other
9664 depending on `nnmail-split-methods'.
9665 If N is a positive number, respool the N next articles.
9666 If N is a negative number, respool the N previous articles.
9667 If N is nil and any articles have been marked with the process mark,
9668 respool those articles instead.
9669
9670 Respooling can be done both from mail groups and \"real\" newsgroups.
9671 In the former case, the articles in question will be moved from the
9672 current group into whatever groups they are destined to.  In the
9673 latter case, they will be copied into the relevant groups."
9674   (interactive "P")
9675   (gnus-set-global-variables)
9676   (let ((respool-methods (gnus-methods-using 'respool))
9677         (methname 
9678          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
9679     (or respool-method
9680         (setq respool-method
9681               (completing-read
9682                "What method do you want to use when respooling? "
9683                respool-methods nil t methname)))
9684     (or (string= respool-method "")
9685         (if (assoc (symbol-name
9686                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
9687                    respool-methods)
9688             (gnus-summary-move-article n nil (intern respool-method))
9689           (gnus-summary-copy-article n nil (intern respool-method))))))
9690
9691 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
9692 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9693   "Move the current article to a different newsgroup.
9694 If N is a positive number, move the N next articles.
9695 If N is a negative number, move the N previous articles.
9696 If N is nil and any articles have been marked with the process mark,
9697 move those articles instead.
9698 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
9699 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
9700 re-spool using this method.
9701 For this function to work, the newsgroup that you want to move to have
9702 to support the `request-move' and `request-accept'
9703 functions. (Ie. mail newsgroups at present.)"
9704   (interactive "P")
9705   (gnus-set-global-variables)
9706   (let ((articles (gnus-summary-work-articles n))
9707         (copy-buf (get-buffer-create "*copy work*"))
9708         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
9709         art-group to-method)
9710     (buffer-disable-undo copy-buf)
9711     (if (and (not to-newsgroup) (not select-method))
9712         (setq to-newsgroup
9713               (completing-read 
9714                (format "Where do you want to copy %s? %s"
9715                        (if (> (length articles) 1)
9716                            (format "these %d articles" (length articles))
9717                          "this article")
9718                        (if gnus-current-move-group
9719                            (format "(default %s) " gnus-current-move-group)
9720                          ""))
9721                gnus-active-hashtb nil nil prefix)))
9722     (if to-newsgroup
9723         (progn
9724           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
9725               (setq to-newsgroup (or gnus-current-move-group "")))
9726           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
9727               (gnus-activate-group to-newsgroup)
9728               (error "No such group: %s" to-newsgroup))
9729           (setq gnus-current-move-group to-newsgroup)))
9730     (setq to-method (if select-method (list select-method "")
9731                       (gnus-find-method-for-group to-newsgroup)))
9732     (or (gnus-check-backend-function 'request-accept-article (car to-method))
9733         (error "%s does not support article copying" (car to-method)))
9734     (or (gnus-check-server to-method)
9735         (error "Can't open server %s" (car to-method)))
9736     (while articles
9737       (gnus-message 6 "Copying to %s: %s..." 
9738                     (or select-method to-newsgroup) articles)
9739       (if (setq art-group
9740                 (save-excursion
9741                   (set-buffer copy-buf)
9742                   (gnus-request-article-this-buffer
9743                    (car articles) gnus-newsgroup-name)
9744                   (gnus-request-accept-article
9745                    (if select-method select-method to-newsgroup)
9746                    (not (cdr articles)))))
9747           (let* ((entry 
9748                   (or
9749                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
9750                    (gnus-gethash 
9751                     (gnus-group-prefixed-name 
9752                      (car art-group) 
9753                      (if select-method (list select-method "")
9754                        (gnus-find-method-for-group to-newsgroup)))
9755                     gnus-newsrc-hashtb)))
9756                  (info (nth 2 entry))
9757                  (article (car articles)))
9758             ;; We copy the info over to the new group.
9759             (if (not info)
9760                 ()                      ; This group does not exist (yet).
9761               (if (not (memq article gnus-newsgroup-unreads))
9762                   (setcar (cdr (cdr info))
9763                           (gnus-add-to-range (nth 2 info) 
9764                                              (list (cdr art-group)))))
9765
9766               ;; Copy any marks over to the new group.
9767               (let ((marks '((tick . gnus-newsgroup-marked)
9768                              (dormant . gnus-newsgroup-dormant)
9769                              (expire . gnus-newsgroup-expirable)
9770                              (bookmark . gnus-newsgroup-bookmarks)
9771                              (reply . gnus-newsgroup-replied)))
9772                     (to-article (cdr art-group)))
9773
9774               ;; See whether the article is to be put in the cache.
9775               (when gnus-use-cache
9776                 (gnus-cache-possibly-enter-article 
9777                  (car info) to-article 
9778                  (let ((header (copy-sequence
9779                                 (gnus-summary-article-header article))))
9780                    (mail-header-set-number to-article header)
9781                    header)
9782                  (memq article gnus-newsgroup-marked)
9783                  (memq article gnus-newsgroup-dormant)
9784                  (memq article gnus-newsgroup-unreads)))
9785
9786               (while marks
9787                 (if (memq article (symbol-value (cdr (car marks))))
9788                     (gnus-add-marked-articles 
9789                      (car info) (car (car marks)) (list to-article) info))
9790                 (setq marks (cdr marks))))))
9791         (gnus-message 1 "Couldn't copy article %s" (car articles)))
9792       (gnus-summary-remove-process-mark (car articles))
9793       (setq articles (cdr articles)))
9794     (kill-buffer copy-buf)))
9795
9796 (defun gnus-summary-import-article (file)
9797   "Import a random file into a mail newsgroup."
9798   (interactive "fImport file: ")
9799   (let ((group gnus-newsgroup-name)
9800         atts lines)
9801     (or (gnus-check-backend-function 'request-accept-article group)
9802         (error "%s does not support article importing" group))
9803     (or (file-readable-p file)
9804         (not (file-regular-p file))
9805         (error "Can't read %s" file))
9806     (save-excursion
9807       (set-buffer (get-buffer-create " *import file*"))
9808       (buffer-disable-undo (current-buffer))
9809       (erase-buffer)
9810       (insert-file-contents file)
9811       (goto-char (point-min))
9812       (if (nnheader-article-p)
9813           ()
9814         (setq atts (file-attributes file)
9815               lines (count-lines (point-min) (point-max)))
9816         (insert "From: " (read-string "From: ") "\n"
9817                 "Subject: " (read-string "Subject: ") "\n"
9818                 "Date: " (current-time-string (nth 5 atts)) "\n"
9819                 "Message-ID: " (gnus-inews-message-id) "\n"
9820                 "Lines: " (int-to-string lines) "\n"
9821                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9822       (gnus-request-accept-article group t)
9823       (kill-buffer (current-buffer)))))
9824
9825 (defun gnus-summary-expire-articles ()
9826   "Expire all articles that are marked as expirable in the current group."
9827   (interactive)
9828   (if (not (gnus-check-backend-function 
9829             'request-expire-articles gnus-newsgroup-name))
9830       ()
9831     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9832            (expirable (if total
9833                           (gnus-list-of-read-articles gnus-newsgroup-name)
9834                         (setq gnus-newsgroup-expirable
9835                               (sort gnus-newsgroup-expirable '<))))
9836            es)
9837       (if (not expirable)
9838           ()
9839         (gnus-message 6 "Expiring articles...")
9840         ;; The list of articles that weren't expired is returned.
9841         (setq es (gnus-request-expire-articles expirable gnus-newsgroup-name))
9842         (or total (setq gnus-newsgroup-expirable es))
9843         ;; We go through the old list of expirable, and mark all
9844         ;; really expired articles as non-existant.
9845         (or (eq es expirable)           ;If nothing was expired, we don't mark.
9846             (let ((gnus-use-cache nil))
9847               (while expirable
9848                 (or (memq (car expirable) es)
9849                     (gnus-summary-mark-article
9850                      (car expirable) gnus-canceled-mark))
9851                 (setq expirable (cdr expirable)))))
9852         (gnus-message 6 "Expiring articles...done")))))
9853
9854 (defun gnus-summary-expire-articles-now ()
9855   "Expunge all expirable articles in the current group.
9856 This means that *all* articles that are marked as expirable will be
9857 deleted forever, right now."
9858   (interactive)
9859   (or gnus-expert-user
9860       (gnus-y-or-n-p
9861        "Are you really, really, really sure you want to expunge? ")
9862       (error "Phew!"))
9863   (let ((nnmail-expiry-wait -1)
9864         (nnmail-expiry-wait-function nil))
9865     (gnus-summary-expire-articles)))
9866
9867 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9868 (defun gnus-summary-delete-article (&optional n)
9869   "Delete the N next (mail) articles.
9870 This command actually deletes articles. This is not a marking
9871 command. The article will disappear forever from your life, never to
9872 return. 
9873 If N is negative, delete backwards.
9874 If N is nil and articles have been marked with the process mark,
9875 delete these instead."
9876   (interactive "P")
9877   (or (gnus-check-backend-function 'request-expire-articles 
9878                                    gnus-newsgroup-name)
9879       (error "The current newsgroup does not support article deletion."))
9880   ;; Compute the list of articles to delete.
9881   (let ((articles (gnus-summary-work-articles n))
9882         not-deleted)
9883     (if (and gnus-novice-user
9884              (not (gnus-y-or-n-p 
9885                    (format "Do you really want to delete %s forever? "
9886                            (if (> (length articles) 1) "these articles"
9887                              "this article")))))
9888         ()
9889       ;; Delete the articles.
9890       (setq not-deleted (gnus-request-expire-articles 
9891                          articles gnus-newsgroup-name 'force))
9892       (while articles
9893         (gnus-summary-remove-process-mark (car articles))       
9894         ;; The backend might not have been able to delete the article
9895         ;; after all.  
9896         (or (memq (car articles) not-deleted)
9897             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9898         (setq articles (cdr articles))))
9899     (gnus-summary-position-point)
9900     not-deleted))
9901
9902 (defun gnus-summary-edit-article (&optional force)
9903   "Enter into a buffer and edit the current article.
9904 This will have permanent effect only in mail groups.
9905 If FORCE is non-nil, allow editing of articles even in read-only
9906 groups."
9907   (interactive "P")
9908   (or force
9909       (not (gnus-group-read-only-p))
9910       (error "The current newsgroup does not support article editing."))
9911   (gnus-summary-select-article t)
9912   (gnus-configure-windows 'article)
9913   (select-window (get-buffer-window gnus-article-buffer))
9914   (gnus-message 6 "C-c C-c to end edits")
9915   (setq buffer-read-only nil)
9916   (text-mode)
9917   (use-local-map (copy-keymap (current-local-map)))
9918   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
9919   (buffer-enable-undo)
9920   (widen)
9921   (goto-char (point-min))
9922   (search-forward "\n\n" nil t))
9923
9924 (defun gnus-summary-edit-article-done ()
9925   "Make edits to the current article permanent."
9926   (interactive)
9927   (if (gnus-group-read-only-p)
9928       (progn
9929         (gnus-summary-edit-article-postpone)
9930         (message "The current newsgroup does not support article editing.")
9931         (ding))
9932     (let ((buf (buffer-substring-no-properties (point-min) (point-max))))
9933       (erase-buffer)
9934       (insert buf)
9935       (if (not (gnus-request-replace-article 
9936                 (cdr gnus-article-current) (car gnus-article-current) 
9937                 (current-buffer)))
9938           (error "Couldn't replace article.")
9939         (gnus-article-mode)
9940         (use-local-map gnus-article-mode-map)
9941         (setq buffer-read-only t)
9942         (buffer-disable-undo (current-buffer))
9943         (gnus-configure-windows 'summary))
9944       (and (gnus-visual-p 'summary-highlight 'highlight)
9945            (run-hooks 'gnus-visual-mark-article-hook)))))
9946
9947 (defun gnus-summary-edit-article-postpone ()
9948   "Postpone changes to the current article."
9949   (interactive)
9950   (gnus-article-mode)
9951   (use-local-map gnus-article-mode-map)
9952   (setq buffer-read-only t)
9953   (buffer-disable-undo (current-buffer))
9954   (gnus-configure-windows 'summary)
9955   (and (gnus-visual-p 'summary-highlight 'highlight)
9956        (run-hooks 'gnus-visual-mark-article-hook)))
9957
9958 (defun gnus-summary-respool-query ()
9959   "Query where the respool algorithm would put this article."
9960   (interactive)
9961   (gnus-summary-select-article)
9962   (save-excursion
9963     (set-buffer gnus-article-buffer)
9964     (save-restriction
9965       (goto-char (point-min))
9966       (search-forward "\n\n")
9967       (narrow-to-region (point-min) (point))
9968       (pp-eval-expression
9969        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
9970
9971 ;; Summary score commands.
9972
9973 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
9974
9975 (defun gnus-summary-raise-score (n)
9976   "Raise the score of the current article by N."
9977   (interactive "p")
9978   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
9979
9980 (defun gnus-summary-set-score (n)
9981   "Set the score of the current article to N."
9982   (interactive "p")
9983   ;; Skip dummy header line.
9984   (save-excursion
9985     (gnus-summary-show-thread)
9986     (let ((buffer-read-only nil))
9987       ;; Set score.
9988       (gnus-summary-update-mark
9989        (if (= n (or gnus-summary-default-score 0)) ? 
9990          (if (< n (or gnus-summary-default-score 0)) 
9991              gnus-score-below-mark gnus-score-over-mark)) 'score))
9992     (let* ((article (gnus-summary-article-number))
9993            (score (assq article gnus-newsgroup-scored)))
9994       (if score (setcdr score n)
9995         (setq gnus-newsgroup-scored 
9996               (cons (cons article n) gnus-newsgroup-scored))))
9997     (gnus-summary-update-line)))
9998
9999 (defun gnus-summary-current-score ()
10000   "Return the score of the current article."
10001   (interactive)
10002   (message "%s" (gnus-summary-article-score)))
10003
10004 ;; Summary marking commands.
10005
10006 (defun gnus-summary-raise-same-subject-and-select (score)
10007   "Raise articles which has the same subject with SCORE and select the next."
10008   (interactive "p")
10009   (let ((subject (gnus-summary-article-subject)))
10010     (gnus-summary-raise-score score)
10011     (while (gnus-summary-find-subject subject)
10012       (gnus-summary-raise-score score))
10013     (gnus-summary-next-article t)))
10014
10015 (defun gnus-summary-raise-same-subject (score)
10016   "Raise articles which has the same subject with SCORE."
10017   (interactive "p")
10018   (let ((subject (gnus-summary-article-subject)))
10019     (gnus-summary-raise-score score)
10020     (while (gnus-summary-find-subject subject)
10021       (gnus-summary-raise-score score))
10022     (gnus-summary-next-subject 1 t)))
10023
10024 (defun gnus-score-default (level)
10025   (if level (prefix-numeric-value level) 
10026     gnus-score-interactive-default-score))
10027
10028 (defun gnus-summary-raise-thread (&optional score)
10029   "Raise the score of the articles in the current thread with SCORE."
10030   (interactive "P")
10031   (setq score (gnus-score-default score))
10032   (let (e)
10033     (save-excursion
10034       (let ((articles (gnus-summary-articles-in-thread)))
10035         (while articles
10036           (gnus-summary-goto-subject (car articles))
10037           (gnus-summary-raise-score score)
10038           (setq articles (cdr articles))))
10039       (setq e (point)))
10040     (let ((gnus-summary-check-current t))
10041       (or (zerop (gnus-summary-next-subject 1 t))
10042           (goto-char e))))
10043   (gnus-summary-recenter)
10044   (gnus-summary-position-point)
10045   (gnus-set-mode-line 'summary))
10046
10047 (defun gnus-summary-lower-same-subject-and-select (score)
10048   "Raise articles which has the same subject with SCORE and select the next."
10049   (interactive "p")
10050   (gnus-summary-raise-same-subject-and-select (- score)))
10051
10052 (defun gnus-summary-lower-same-subject (score)
10053   "Raise articles which has the same subject with SCORE."
10054   (interactive "p")
10055   (gnus-summary-raise-same-subject (- score)))
10056
10057 (defun gnus-summary-lower-thread (&optional score)
10058   "Lower score of articles in the current thread with SCORE."
10059   (interactive "P")
10060   (gnus-summary-raise-thread (- (1- (gnus-score-default score)))))
10061
10062 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10063   "Mark articles which has the same subject as read, and then select the next.
10064 If UNMARK is positive, remove any kind of mark.
10065 If UNMARK is negative, tick articles."
10066   (interactive "P")
10067   (if unmark
10068       (setq unmark (prefix-numeric-value unmark)))
10069   (let ((count
10070          (gnus-summary-mark-same-subject
10071           (gnus-summary-article-subject) unmark)))
10072     ;; Select next unread article. If auto-select-same mode, should
10073     ;; select the first unread article.
10074     (gnus-summary-next-article t (and gnus-auto-select-same
10075                                       (gnus-summary-article-subject)))
10076     (gnus-message 7 "%d article%s marked as %s"
10077                   count (if (= count 1) " is" "s are")
10078                   (if unmark "unread" "read"))))
10079
10080 (defun gnus-summary-kill-same-subject (&optional unmark)
10081   "Mark articles which has the same subject as read. 
10082 If UNMARK is positive, remove any kind of mark.
10083 If UNMARK is negative, tick articles."
10084   (interactive "P")
10085   (if unmark
10086       (setq unmark (prefix-numeric-value unmark)))
10087   (let ((count
10088          (gnus-summary-mark-same-subject
10089           (gnus-summary-article-subject) unmark)))
10090     ;; If marked as read, go to next unread subject.
10091     (if (null unmark)
10092         ;; Go to next unread subject.
10093         (gnus-summary-next-subject 1 t))
10094     (gnus-message 7 "%d articles are marked as %s"
10095                   count (if unmark "unread" "read"))))
10096
10097 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10098   "Mark articles with same SUBJECT as read, and return marked number.
10099 If optional argument UNMARK is positive, remove any kinds of marks.
10100 If optional argument UNMARK is negative, mark articles as unread instead."
10101   (let ((count 1))
10102     (save-excursion
10103       (cond 
10104        ((null unmark)                   ; Mark as read.
10105         (while (and 
10106                 (progn
10107                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10108                   (gnus-summary-show-thread) t)
10109                 (gnus-summary-find-subject subject))
10110           (setq count (1+ count))))
10111        ((> unmark 0)                    ; Tick.
10112         (while (and
10113                 (progn
10114                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10115                   (gnus-summary-show-thread) t)
10116                 (gnus-summary-find-subject subject))
10117           (setq count (1+ count))))
10118        (t                               ; Mark as unread.
10119         (while (and
10120                 (progn
10121                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10122                   (gnus-summary-show-thread) t)
10123                 (gnus-summary-find-subject subject))
10124           (setq count (1+ count)))))
10125       (gnus-set-mode-line 'summary)
10126       ;; Return the number of marked articles.
10127       count)))
10128
10129 (defun gnus-summary-mark-as-processable (n &optional unmark)
10130   "Set the process mark on the next N articles.
10131 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10132 the process mark instead.  The difference between N and the actual
10133 number of articles marked is returned."
10134   (interactive "p")
10135   (let ((backward (< n 0))
10136         (n (abs n)))
10137     (while (and 
10138             (> n 0)
10139             (if unmark
10140                 (gnus-summary-remove-process-mark
10141                  (gnus-summary-article-number))
10142               (gnus-summary-set-process-mark (gnus-summary-article-number)))
10143             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10144       (setq n (1- n)))
10145     (if (/= 0 n) (gnus-message 7 "No more articles"))
10146     (gnus-summary-recenter)
10147     (gnus-summary-position-point)
10148     n))
10149
10150 (defun gnus-summary-unmark-as-processable (n)
10151   "Remove the process mark from the next N articles.
10152 If N is negative, mark backward instead.  The difference between N and
10153 the actual number of articles marked is returned."
10154   (interactive "p")
10155   (gnus-summary-mark-as-processable n t))
10156
10157 (defun gnus-summary-unmark-all-processable ()
10158   "Remove the process mark from all articles."
10159   (interactive)
10160   (save-excursion
10161     (while gnus-newsgroup-processable
10162       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10163   (gnus-summary-position-point))
10164
10165 (defun gnus-summary-mark-as-expirable (n)
10166   "Mark N articles forward as expirable.
10167 If N is negative, mark backward instead. The difference between N and
10168 the actual number of articles marked is returned."
10169   (interactive "p")
10170   (gnus-summary-mark-forward n gnus-expirable-mark))
10171
10172 (defun gnus-summary-mark-article-as-replied (article)
10173   "Mark ARTICLE replied and update the summary line."
10174   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
10175   (let ((buffer-read-only nil))
10176     (if (gnus-summary-goto-subject article)
10177         (progn
10178           (gnus-summary-update-mark gnus-replied-mark 'replied)
10179           t))))
10180
10181 (defun gnus-summary-set-bookmark (article)
10182   "Set a bookmark in current article."
10183   (interactive (list (gnus-summary-article-number)))
10184   (if (or (not (get-buffer gnus-article-buffer))
10185           (not gnus-current-article)
10186           (not gnus-article-current)
10187           (not (equal gnus-newsgroup-name (car gnus-article-current))))
10188       (error "No current article selected"))
10189   ;; Remove old bookmark, if one exists.
10190   (let ((old (assq article gnus-newsgroup-bookmarks)))
10191     (if old (setq gnus-newsgroup-bookmarks 
10192                   (delq old gnus-newsgroup-bookmarks))))
10193   ;; Set the new bookmark, which is on the form 
10194   ;; (article-number . line-number-in-body).
10195   (setq gnus-newsgroup-bookmarks 
10196         (cons 
10197          (cons article 
10198                (save-excursion
10199                  (set-buffer gnus-article-buffer)
10200                  (count-lines
10201                   (min (point)
10202                        (save-excursion
10203                          (goto-char (point-min))
10204                          (search-forward "\n\n" nil t)
10205                          (point)))
10206                   (point))))
10207          gnus-newsgroup-bookmarks))
10208   (gnus-message 6 "A bookmark has been added to the current article."))
10209
10210 (defun gnus-summary-remove-bookmark (article)
10211   "Remove the bookmark from the current article."
10212   (interactive (list (gnus-summary-article-number)))
10213   ;; Remove old bookmark, if one exists.
10214   (let ((old (assq article gnus-newsgroup-bookmarks)))
10215     (if old 
10216         (progn
10217           (setq gnus-newsgroup-bookmarks 
10218                 (delq old gnus-newsgroup-bookmarks))
10219           (gnus-message 6 "Removed bookmark."))
10220       (gnus-message 6 "No bookmark in current article."))))
10221
10222 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10223 (defun gnus-summary-mark-as-dormant (n)
10224   "Mark N articles forward as dormant.
10225 If N is negative, mark backward instead.  The difference between N and
10226 the actual number of articles marked is returned."
10227   (interactive "p")
10228   (gnus-summary-mark-forward n gnus-dormant-mark))
10229
10230 (defun gnus-summary-set-process-mark (article)
10231   "Set the process mark on ARTICLE and update the summary line."
10232   (setq gnus-newsgroup-processable (cons article gnus-newsgroup-processable))
10233   (let ((buffer-read-only nil))
10234     (if (gnus-summary-goto-subject article)
10235         (progn
10236           (gnus-summary-show-thread)
10237           (gnus-summary-update-mark gnus-process-mark 'replied)
10238           t))))
10239
10240 (defun gnus-summary-remove-process-mark (article)
10241   "Remove the process mark from ARTICLE and update the summary line."
10242   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10243   (let ((buffer-read-only nil))
10244     (if (gnus-summary-goto-subject article)
10245         (progn
10246           (gnus-summary-show-thread)
10247           (gnus-summary-update-mark ?  'replied)
10248           (if (memq article gnus-newsgroup-replied) 
10249               (gnus-summary-update-mark gnus-replied-mark 'replied))
10250           t))))
10251
10252 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10253   "Mark N articles as read forwards.
10254 If N is negative, mark backwards instead.
10255 Mark with MARK. If MARK is ? , ?! or ??, articles will be
10256 marked as unread. 
10257 The difference between N and the actual number of articles marked is
10258 returned."
10259   (interactive "p")
10260   (gnus-set-global-variables)
10261   (let ((backward (< n 0))
10262         (gnus-summary-goto-unread
10263          (and gnus-summary-goto-unread
10264               (not (memq mark (list gnus-unread-mark
10265                                     gnus-ticked-mark gnus-dormant-mark)))))
10266         (n (abs n))
10267         (mark (or mark gnus-del-mark)))
10268     (while (and (> n 0)
10269                 (gnus-summary-mark-article nil mark no-expire)
10270                 (zerop (gnus-summary-next-subject 
10271                         (if backward -1 1) gnus-summary-goto-unread t)))
10272       (setq n (1- n)))
10273     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10274     (gnus-summary-recenter)
10275     (gnus-summary-position-point)
10276     (gnus-set-mode-line 'summary)
10277     n))
10278
10279 (defun gnus-summary-mark-article-as-read (mark)
10280   "Mark the current article quickly as read with MARK."
10281   (let ((article (gnus-summary-article-number)))
10282     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10283     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10284     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10285     (setq gnus-newsgroup-reads
10286           (cons (cons article mark) gnus-newsgroup-reads))
10287     ;; Possibly remove from cache, if that is used. 
10288     (and gnus-use-cache (gnus-cache-enter-remove-article article))
10289     (and gnus-newsgroup-auto-expire 
10290          (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
10291              (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
10292              (= mark gnus-read-mark))
10293          (progn
10294            (setq mark gnus-expirable-mark)
10295            (setq gnus-newsgroup-expirable 
10296                  (cons article gnus-newsgroup-expirable))))
10297     ;; Fix the mark.
10298     (gnus-summary-update-mark mark 'unread)
10299     t))
10300
10301 (defun gnus-summary-mark-article-as-unread (mark)
10302   "Mark the current article quickly as unread with MARK."
10303   (let ((article (gnus-summary-article-number)))
10304     (or (memq article gnus-newsgroup-unreads)
10305         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
10306     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10307     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10308     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10309     (setq gnus-newsgroup-reads
10310           (delq (assq article gnus-newsgroup-reads)
10311                 gnus-newsgroup-reads))
10312     (if (= mark gnus-ticked-mark)
10313         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
10314     (if (= mark gnus-dormant-mark)
10315         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))
10316
10317     ;; See whether the article is to be put in the cache.
10318     (and gnus-use-cache
10319          (vectorp (gnus-summary-article-header article))
10320          (save-excursion
10321            (gnus-cache-possibly-enter-article 
10322             gnus-newsgroup-name article 
10323             (gnus-summary-article-header article)
10324             (= mark gnus-ticked-mark)
10325             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10326
10327     ;; Fix the mark.
10328     (gnus-summary-update-mark mark 'unread)
10329     t))
10330
10331 (defun gnus-summary-mark-article (&optional article mark no-expire)
10332   "Mark ARTICLE with MARK.  MARK can be any character.
10333 Four MARK strings are reserved: `? ' (unread), `?!' (ticked), `??'
10334 (dormant) and `?E' (expirable).
10335 If MARK is nil, then the default character `?D' is used.
10336 If ARTICLE is nil, then the article on the current line will be
10337 marked." 
10338   (and (stringp mark)
10339        (setq mark (aref mark 0)))
10340   ;; If no mark is given, then we check auto-expiring.
10341   (and (not no-expire)
10342        gnus-newsgroup-auto-expire 
10343        (or (not mark)
10344            (and (numberp mark) 
10345                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
10346                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
10347                     (= mark gnus-read-mark))))
10348        (setq mark gnus-expirable-mark))
10349   (let* ((mark (or mark gnus-del-mark))
10350          (article (or article (gnus-summary-article-number))))
10351     (or article (error "No article on current line"))
10352     (if (or (= mark gnus-unread-mark) 
10353             (= mark gnus-ticked-mark) 
10354             (= mark gnus-dormant-mark))
10355         (gnus-mark-article-as-unread article mark)
10356       (gnus-mark-article-as-read article mark))
10357
10358     ;; See whether the article is to be put in the cache.
10359     (and gnus-use-cache
10360          (not (= mark gnus-canceled-mark))
10361          (vectorp (gnus-summary-article-header article))
10362          (save-excursion
10363            (gnus-cache-possibly-enter-article 
10364             gnus-newsgroup-name article 
10365             (gnus-summary-article-header article)
10366             (= mark gnus-ticked-mark)
10367             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10368
10369     (if (gnus-summary-goto-subject article)
10370         (let ((buffer-read-only nil))
10371           (gnus-summary-show-thread)
10372           ;; Fix the mark.
10373           (gnus-summary-update-mark mark 'unread)
10374           t))))
10375
10376 (defun gnus-summary-update-mark (mark type)
10377   (beginning-of-line)
10378   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10379         (buffer-read-only nil)
10380         plist)
10381     (if (not forward)
10382         ()
10383       ;; Go to the right position on the line.
10384       (forward-char forward)
10385       ;; Replace the old mark with the new mark.
10386       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
10387       ;; Optionally update the marks by some user rule.
10388       (and (eq type 'unread)
10389            (progn
10390              (gnus-data-set-mark (gnus-data-find (gnus-summary-article-number))
10391                                  mark)
10392              (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10393   
10394 (defun gnus-mark-article-as-read (article &optional mark)
10395   "Enter ARTICLE in the pertinent lists and remove it from others."
10396   ;; Make the article expirable.
10397   (let ((mark (or mark gnus-del-mark)))
10398     (if (= mark gnus-expirable-mark)
10399         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
10400       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
10401     ;; Remove from unread and marked lists.
10402     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10403     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10404     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10405     (setq gnus-newsgroup-reads 
10406           (cons (cons article mark) gnus-newsgroup-reads))
10407     ;; Possibly remove from cache, if that is used. 
10408     (and gnus-use-cache (gnus-cache-enter-remove-article article))))
10409
10410 (defun gnus-mark-article-as-unread (article &optional mark)
10411   "Enter ARTICLE in the pertinent lists and remove it from others."
10412   (let ((mark (or mark gnus-ticked-mark)))
10413     ;; Add to unread list.
10414     (or (memq article gnus-newsgroup-unreads)
10415         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
10416     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10417     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10418     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10419     (setq gnus-newsgroup-reads
10420           (delq (assq article gnus-newsgroup-reads)
10421                 gnus-newsgroup-reads))
10422     (if (= mark gnus-ticked-mark)
10423         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
10424     (if (= mark gnus-dormant-mark)
10425         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))))
10426
10427 (defalias 'gnus-summary-mark-as-unread-forward 
10428   'gnus-summary-tick-article-forward)
10429 (make-obsolete 'gnus-summary-mark-as-unread-forward 
10430                'gnus-summary-tick-article-forward)
10431 (defun gnus-summary-tick-article-forward (n)
10432   "Tick N articles forwards.
10433 If N is negative, tick backwards instead.
10434 The difference between N and the number of articles ticked is returned."
10435   (interactive "p")
10436   (gnus-summary-mark-forward n gnus-ticked-mark))
10437
10438 (defalias 'gnus-summary-mark-as-unread-backward 
10439   'gnus-summary-tick-article-backward)
10440 (make-obsolete 'gnus-summary-mark-as-unread-backward 
10441                'gnus-summary-tick-article-backward)
10442 (defun gnus-summary-tick-article-backward (n)
10443   "Tick N articles backwards.
10444 The difference between N and the number of articles ticked is returned."
10445   (interactive "p")
10446   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10447
10448 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10449 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10450 (defun gnus-summary-tick-article (&optional article clear-mark)
10451   "Mark current article as unread.
10452 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10453 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10454   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10455                                        gnus-ticked-mark)))
10456
10457 (defun gnus-summary-mark-as-read-forward (n)
10458   "Mark N articles as read forwards.
10459 If N is negative, mark backwards instead.
10460 The difference between N and the actual number of articles marked is
10461 returned."
10462   (interactive "p")
10463   (gnus-summary-mark-forward n gnus-del-mark t))
10464
10465 (defun gnus-summary-mark-as-read-backward (n)
10466   "Mark the N articles as read backwards.
10467 The difference between N and the actual number of articles marked is
10468 returned."
10469   (interactive "p")
10470   (gnus-summary-mark-forward (- n) gnus-del-mark t))
10471
10472 (defun gnus-summary-mark-as-read (&optional article mark)
10473   "Mark current article as read.
10474 ARTICLE specifies the article to be marked as read.
10475 MARK specifies a string to be inserted at the beginning of the line."
10476   (gnus-summary-mark-article article mark))
10477
10478 (defun gnus-summary-clear-mark-forward (n)
10479   "Clear marks from N articles forward.
10480 If N is negative, clear backward instead.
10481 The difference between N and the number of marks cleared is returned."
10482   (interactive "p")
10483   (gnus-summary-mark-forward n gnus-unread-mark))
10484
10485 (defun gnus-summary-clear-mark-backward (n)
10486   "Clear marks from N articles backward.
10487 The difference between N and the number of marks cleared is returned."
10488   (interactive "p")
10489   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10490
10491 (defun gnus-summary-mark-unread-as-read ()
10492   "Intended to be used by `gnus-summary-mark-article-hook'."
10493   (or (memq gnus-current-article gnus-newsgroup-marked)
10494       (memq gnus-current-article gnus-newsgroup-dormant)
10495       (memq gnus-current-article gnus-newsgroup-expirable)
10496       (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10497
10498 (defun gnus-summary-mark-region-as-read (point mark all)
10499   "Mark all unread articles between point and mark as read.
10500 If given a prefix, mark all articles between point and mark as read,
10501 even ticked and dormant ones."
10502   (interactive "r\nP")
10503   (save-excursion
10504     (goto-char point)
10505     (beginning-of-line)
10506     (while (and 
10507             (< (point) mark)
10508             (progn
10509               (and
10510                (or all
10511                    (and
10512                     (not (memq (gnus-summary-article-number)
10513                                gnus-newsgroup-marked))
10514                     (not (memq (gnus-summary-article-number)
10515                                gnus-newsgroup-dormant))))
10516                (gnus-summary-mark-article
10517                 (gnus-summary-article-number) gnus-del-mark))
10518               t)
10519             (gnus-summary-find-next)))))
10520
10521 (defun gnus-summary-mark-below (score mark)
10522   "Mark articles with score less than SCORE with MARK."
10523   (interactive "P\ncMark: ")
10524   (gnus-set-global-variables)
10525   (setq score (if score
10526                   (prefix-numeric-value score)
10527                 (or gnus-summary-default-score 0)))
10528   (save-excursion
10529     (set-buffer gnus-summary-buffer)
10530     (goto-char (point-min))
10531     (while (not (eobp))
10532       (and (< (gnus-summary-article-score) score)
10533            (gnus-summary-mark-article nil mark))
10534       (gnus-summary-find-next))))
10535
10536 (defun gnus-summary-kill-below (&optional score)
10537   "Mark articles with score below SCORE as read."
10538   (interactive "P")
10539   (gnus-set-global-variables)
10540   (gnus-summary-mark-below score gnus-killed-mark))
10541
10542 (defun gnus-summary-clear-above (&optional score)
10543   "Clear all marks from articles with score above SCORE."
10544   (interactive "P")
10545   (gnus-set-global-variables)
10546   (gnus-summary-mark-above score gnus-unread-mark))
10547
10548 (defun gnus-summary-tick-above (&optional score)
10549   "Tick all articles with score above SCORE."
10550   (interactive "P")
10551   (gnus-set-global-variables)
10552   (gnus-summary-mark-above score gnus-ticked-mark))
10553
10554 (defun gnus-summary-mark-above (score mark)
10555   "Mark articles with score over SCORE with MARK."
10556   (interactive "P\ncMark: ")
10557   (gnus-set-global-variables)
10558   (setq score (if score
10559                   (prefix-numeric-value score)
10560                 (or gnus-summary-default-score 0)))
10561   (save-excursion
10562     (set-buffer gnus-summary-buffer)
10563     (goto-char (point-min))
10564     (while (and (progn
10565                   (if (> (gnus-summary-article-score) score)
10566                       (gnus-summary-mark-article nil mark))
10567                   t)
10568                 (gnus-summary-find-next)))))
10569
10570 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
10571 (defun gnus-summary-show-all-expunged ()
10572   "Display all the hidden articles that were expunged for low scores."
10573   (interactive)
10574   (gnus-set-global-variables)
10575   (let ((buffer-read-only nil))
10576     (let ((scored gnus-newsgroup-scored)
10577           headers h)
10578       (while scored
10579         (or (gnus-summary-goto-subject (car (car scored)))
10580             (and (setq h (gnus-summary-article-header (car (car scored))))
10581                  (< (cdr (car scored)) gnus-summary-expunge-below)
10582                  (setq headers (cons h headers))))
10583         (setq scored (cdr scored)))
10584       (or headers (error "No expunged articles hidden."))
10585       (goto-char (point-min))
10586       (save-excursion 
10587         (gnus-summary-update-lines 
10588          (point)
10589          (progn
10590            (gnus-summary-prepare-unthreaded (nreverse headers))
10591            (point)))))
10592     (goto-char (point-min))
10593     (gnus-summary-position-point)))
10594
10595 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
10596   "Mark all articles not marked as unread in this newsgroup as read.
10597 If prefix argument ALL is non-nil, all articles are marked as read.
10598 If QUIETLY is non-nil, no questions will be asked.
10599 If TO-HERE is non-nil, it should be a point in the buffer. All
10600 articles before this point will be marked as read.
10601 The number of articles marked as read is returned."
10602   (interactive "P")
10603   (gnus-set-global-variables)
10604   (prog1
10605       (if (or quietly
10606               (not gnus-interactive-catchup) ;Without confirmation?
10607               gnus-expert-user
10608               (gnus-y-or-n-p
10609                (if all
10610                    "Mark absolutely all articles as read? "
10611                  "Mark all unread articles as read? ")))
10612           (if (and not-mark 
10613                    (not gnus-newsgroup-adaptive)
10614                    (not gnus-newsgroup-auto-expire))
10615               (progn
10616                 (and all (setq gnus-newsgroup-marked nil
10617                                gnus-newsgroup-dormant nil))
10618                 (setq gnus-newsgroup-unreads 
10619                       (append gnus-newsgroup-marked gnus-newsgroup-dormant)))
10620             ;; We actually mark all articles as canceled, which we
10621             ;; have to do when using auto-expiry or adaptive scoring. 
10622             (gnus-summary-show-all-threads)
10623             (if (gnus-summary-first-subject (not all))
10624                 (while (and 
10625                         (if to-here (< (point) to-here) t)
10626                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10627                         (gnus-summary-find-next (not all)))))
10628             (or to-here
10629                 (setq gnus-newsgroup-unreads
10630                       (append gnus-newsgroup-marked
10631                               gnus-newsgroup-dormant)))))
10632     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
10633       (if (and (not to-here) (eq 'nnvirtual (car method)))
10634           (nnvirtual-catchup-group
10635            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
10636     (gnus-summary-position-point)))
10637
10638 (defun gnus-summary-catchup-to-here (&optional all)
10639   "Mark all unticked articles before the current one as read.
10640 If ALL is non-nil, also mark ticked and dormant articles as read."
10641   (interactive "P")
10642   (gnus-set-global-variables)
10643   (save-excursion
10644     (and (gnus-summary-find-prev)
10645          (progn
10646            (gnus-summary-catchup all t (point))
10647            (gnus-set-mode-line 'summary))))
10648   (gnus-summary-position-point))
10649
10650 (defun gnus-summary-catchup-all (&optional quietly)
10651   "Mark all articles in this newsgroup as read."
10652   (interactive "P")
10653   (gnus-set-global-variables)
10654   (gnus-summary-catchup t quietly))
10655
10656 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10657   "Mark all articles not marked as unread in this newsgroup as read, then exit.
10658 If prefix argument ALL is non-nil, all articles are marked as read."
10659   (interactive "P")
10660   (gnus-set-global-variables)
10661   (gnus-summary-catchup all quietly nil 'fast)
10662   ;; Select next newsgroup or exit.
10663   (if (eq gnus-auto-select-next 'quietly)
10664       (gnus-summary-next-group nil)
10665     (gnus-summary-exit)))
10666
10667 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10668   "Mark all articles in this newsgroup as read, and then exit."
10669   (interactive "P")
10670   (gnus-set-global-variables)
10671   (gnus-summary-catchup-and-exit t quietly))
10672
10673 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
10674 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10675   "Mark all articles in this group as read and select the next group.
10676 If given a prefix, mark all articles, unread as well as ticked, as
10677 read." 
10678   (interactive "P")
10679   (gnus-set-global-variables)
10680   (gnus-summary-catchup all)
10681   (gnus-summary-next-group))
10682
10683 ;; Thread-based commands.
10684
10685 (defun gnus-summary-articles-in-thread (&optional article)
10686   "Return a list of all articles in the current thread.
10687 If ARTICLE is non-nil, return all articles in the thread that starts
10688 with that article."
10689   (let* ((article (or article (gnus-summary-article-number)))
10690          (data (gnus-data-find-list article))
10691          (top-level (gnus-data-level (car data)))
10692          (top-subject 
10693           (cond ((null gnus-thread-operation-ignore-subject)
10694                  (gnus-simplify-subject-re
10695                   (mail-header-subject (gnus-data-header (car data)))))
10696                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10697                  (gnus-simplify-subject-fuzzy
10698                   (mail-header-subject (gnus-data-header (car data)))))
10699                 (t nil)))
10700          articles)
10701     (if (not data)
10702         ()                              ; This article doesn't exist.
10703       (while data
10704         (and (or (not top-subject)
10705                  (string= top-subject
10706                           (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10707                               (gnus-simplify-subject-fuzzy
10708                                (mail-header-subject 
10709                                 (gnus-data-header (car data))))
10710                             (gnus-simplify-subject-re
10711                              (mail-header-subject 
10712                               (gnus-data-header (car data)))))))
10713              (setq articles (cons (gnus-data-number (car data)) articles)))
10714         (if (and (setq data (cdr data))
10715                  (> (gnus-data-level (car data)) top-level))
10716             ()
10717           (setq data nil)))
10718       ;; Return the list of articles.
10719       (nreverse articles))))
10720
10721 (defun gnus-summary-toggle-threads (&optional arg)
10722   "Toggle showing conversation threads.
10723 If ARG is positive number, turn showing conversation threads on."
10724   (interactive "P")
10725   (gnus-set-global-variables)
10726   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10727     (setq gnus-show-threads
10728           (if (null arg) (not gnus-show-threads)
10729             (> (prefix-numeric-value arg) 0)))
10730     (gnus-summary-prepare)
10731     (gnus-summary-goto-subject current)
10732     (gnus-summary-position-point)))
10733
10734 (defun gnus-summary-show-all-threads ()
10735   "Show all threads."
10736   (interactive)
10737   (gnus-set-global-variables)
10738   (save-excursion
10739     (let ((buffer-read-only nil))
10740       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10741   (gnus-summary-position-point))
10742
10743 (defun gnus-summary-show-thread ()
10744   "Show thread subtrees.
10745 Returns nil if no thread was there to be shown."
10746   (interactive)
10747   (gnus-set-global-variables)
10748   (let ((buffer-read-only nil)
10749         (orig (point))
10750         ;; first goto end then to beg, to have point at beg after let
10751         (end (progn (end-of-line) (point)))
10752         (beg (progn (beginning-of-line) (point))))
10753     (prog1
10754         ;; Any hidden lines here?
10755         (search-forward "\r" end t)
10756       (subst-char-in-region beg end ?\^M ?\n t)
10757       (goto-char orig)
10758       (gnus-summary-position-point))))
10759
10760 (defun gnus-summary-hide-all-threads ()
10761   "Hide all thread subtrees."
10762   (interactive)
10763   (gnus-set-global-variables)
10764   (save-excursion
10765     (goto-char (point-min))
10766     (gnus-summary-hide-thread)
10767     (while (zerop (gnus-summary-next-thread 1))
10768       (gnus-summary-hide-thread)))
10769   (gnus-summary-position-point))
10770
10771 (defun gnus-summary-hide-thread ()
10772   "Hide thread subtrees.
10773 Returns nil if no threads were there to be hidden."
10774   (interactive)
10775   (gnus-set-global-variables)
10776   (let ((buffer-read-only nil)
10777         (start (point))
10778         (article (gnus-summary-article-number))
10779         (end (point)))
10780     ;; Go forward until either the buffer ends or the subthread
10781     ;; ends. 
10782     (if (eobp)
10783         ()
10784       (if (not (zerop (gnus-summary-next-thread 1)))
10785           ()
10786         (gnus-summary-find-prev)
10787         (prog1
10788             (save-excursion
10789               (search-backward "\n" start t))
10790           (subst-char-in-region start (point) ?\n ?\^M)
10791           (gnus-summary-goto-subject article)
10792           (gnus-summary-position-point))))))
10793
10794 (defun gnus-summary-go-to-next-thread (&optional previous)
10795   "Go to the same level (or less) next thread.
10796 If PREVIOUS is non-nil, go to previous thread instead.
10797 Return the article number moved to, or nil if moving was impossible."
10798   (let* ((level (gnus-summary-thread-level))
10799          (article (gnus-summary-article-number))
10800          (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
10801          oart)
10802     (while data
10803       (if (<= (gnus-data-level (car data)) level)
10804           (setq oart (gnus-data-number (car data))
10805                 data nil)
10806         (setq data (cdr data))))
10807     (and oart 
10808          (gnus-summary-goto-subject oart))))
10809
10810 (defun gnus-summary-next-thread (n)
10811   "Go to the same level next N'th thread.
10812 If N is negative, search backward instead.
10813 Returns the difference between N and the number of skips actually
10814 done."
10815   (interactive "p")
10816   (gnus-set-global-variables)
10817   (let ((backward (< n 0))
10818         (n (abs n)))
10819     (while (and (> n 0)
10820                 (gnus-summary-go-to-next-thread backward))
10821       (setq n (1- n)))
10822     (gnus-summary-position-point)
10823     (if (/= 0 n) (gnus-message 7 "No more threads"))
10824     n))
10825
10826 (defun gnus-summary-prev-thread (n)
10827   "Go to the same level previous N'th thread.
10828 Returns the difference between N and the number of skips actually
10829 done."
10830   (interactive "p")
10831   (gnus-set-global-variables)
10832   (gnus-summary-next-thread (- n)))
10833
10834 (defun gnus-summary-go-down-thread ()
10835   "Go down one level in the current thread."
10836   (let ((children (gnus-summary-article-children)))
10837     (and children
10838          (gnus-summary-goto-subject (car children)))))
10839
10840 (defun gnus-summary-go-up-thread ()
10841   "Go up one level in the current thread."
10842   (let ((parent (gnus-summary-article-parent)))
10843     (and parent
10844          (gnus-summary-goto-subject parent))))
10845
10846 (defun gnus-summary-down-thread (n)
10847   "Go down thread N steps.
10848 If N is negative, go up instead.
10849 Returns the difference between N and how many steps down that were
10850 taken."
10851   (interactive "p")
10852   (gnus-set-global-variables)
10853   (let ((up (< n 0))
10854         (n (abs n)))
10855     (while (and (> n 0)
10856                 (if up (gnus-summary-go-up-thread)
10857                   (gnus-summary-go-down-thread)))
10858       (setq n (1- n)))
10859     (gnus-summary-position-point)
10860     (if (/= 0 n) (gnus-message 7 "Can't go further"))
10861     n))
10862
10863 (defun gnus-summary-up-thread (n)
10864   "Go up thread N steps.
10865 If N is negative, go up instead.
10866 Returns the difference between N and how many steps down that were
10867 taken."
10868   (interactive "p")
10869   (gnus-set-global-variables)
10870   (gnus-summary-down-thread (- n)))
10871
10872 (defun gnus-summary-kill-thread (&optional unmark)
10873   "Mark articles under current thread as read.
10874 If the prefix argument is positive, remove any kinds of marks.
10875 If the prefix argument is negative, tick articles instead."
10876   (interactive "P")
10877   (gnus-set-global-variables)
10878   (if unmark
10879       (setq unmark (prefix-numeric-value unmark)))
10880   (let ((articles (gnus-summary-articles-in-thread)))
10881     (save-excursion
10882       ;; Expand the thread.
10883       (gnus-summary-show-thread)
10884       ;; Mark all the articles.
10885       (while articles
10886         (gnus-summary-goto-subject (car articles))
10887         (cond ((null unmark) 
10888                (gnus-summary-mark-article-as-read gnus-killed-mark))
10889               ((> unmark 0) 
10890                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10891               (t 
10892                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10893         (setq articles (cdr articles))))
10894     ;; Hide killed subtrees.
10895     (and (null unmark)
10896          gnus-thread-hide-killed
10897          (gnus-summary-hide-thread))
10898     ;; If marked as read, go to next unread subject.
10899     (if (null unmark)
10900         ;; Go to next unread subject.
10901         (gnus-summary-next-subject 1 t)))
10902   (gnus-set-mode-line 'summary))
10903
10904 ;; Summary sorting commands
10905
10906 (defun gnus-summary-sort-by-number (&optional reverse)
10907   "Sort summary buffer by article number.
10908 Argument REVERSE means reverse order."
10909   (interactive "P")
10910   (gnus-set-global-variables)
10911   (gnus-summary-sort 
10912    ;; `gnus-summary-article-number' is a macro, and `sort-subr' wants
10913    ;; a function, so we wrap it.
10914    (cons (lambda () (gnus-summary-article-number))
10915          'gnus-thread-sort-by-number) reverse))
10916
10917 (defun gnus-summary-sort-by-author (&optional reverse)
10918   "Sort summary buffer by author name alphabetically.
10919 If case-fold-search is non-nil, case of letters is ignored.
10920 Argument REVERSE means reverse order."
10921   (interactive "P")
10922   (gnus-set-global-variables)
10923   (gnus-summary-sort
10924    (cons
10925     (lambda ()
10926       (let* ((header (gnus-summary-article-header))
10927              (extract (funcall
10928                        gnus-extract-address-components
10929                        (mail-header-from header))))
10930         (concat (or (car extract) (cdr extract))
10931                 "\r" (mail-header-subject header))))
10932     'gnus-thread-sort-by-author)
10933    reverse))
10934
10935 (defun gnus-summary-sort-by-subject (&optional reverse)
10936   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
10937 If case-fold-search is non-nil, case of letters is ignored.
10938 Argument REVERSE means reverse order."
10939   (interactive "P")
10940   (gnus-set-global-variables)
10941   (gnus-summary-sort
10942    (cons
10943     (lambda ()
10944       (let* ((header (gnus-summary-article-header))
10945              (extract (funcall
10946                        gnus-extract-address-components
10947                        (mail-header-from header))))
10948         (concat 
10949          (downcase (gnus-simplify-subject (gnus-summary-article-subject) t))
10950          "\r" (or (car extract) (cdr extract)))))
10951     'gnus-thread-sort-by-subject)
10952    reverse))
10953
10954 (defun gnus-summary-sort-by-date (&optional reverse)
10955   "Sort summary buffer by date.
10956 Argument REVERSE means reverse order."
10957   (interactive "P")
10958   (gnus-set-global-variables)
10959   (gnus-summary-sort
10960    (cons
10961     (lambda ()
10962       (gnus-sortable-date
10963        (mail-header-date 
10964         (gnus-summary-article-header))))
10965     'gnus-thread-sort-by-date)
10966    reverse))
10967
10968 (defun gnus-summary-sort-by-score (&optional reverse)
10969   "Sort summary buffer by score.
10970 Argument REVERSE means reverse order."
10971   (interactive "P")
10972   (gnus-set-global-variables)
10973   (gnus-summary-sort 
10974    (cons (lambda () (gnus-summary-article-score))
10975          'gnus-thread-sort-by-score)
10976    (not reverse)))
10977
10978 (defvar gnus-summary-already-sorted nil)
10979 (defun gnus-summary-sort (predicate reverse)
10980   ;; Sort summary buffer by PREDICATE.  REVERSE means reverse order. 
10981   (if gnus-summary-already-sorted
10982       ()
10983     (let (buffer-read-only)
10984       (if (not gnus-show-threads)
10985           ;; We do untreaded sorting...
10986           (progn
10987             (goto-char (point-min))
10988             (sort-subr reverse 'forward-line 'end-of-line (car predicate)))
10989         ;; ... or we do threaded sorting.
10990         (let ((gnus-thread-sort-functions (list (cdr predicate)))
10991               (gnus-summary-prepare-hook nil)
10992               (gnus-summary-already-sorted nil))
10993           ;; We do that by simply regenerating the threads.
10994           (gnus-summary-prepare)
10995           (and gnus-show-threads
10996                gnus-thread-hide-subtree
10997                (gnus-summary-hide-all-threads))
10998           ;; If in async mode, we send some info to the backend.
10999           (and gnus-newsgroup-async
11000                (gnus-request-asynchronous 
11001                 gnus-newsgroup-name gnus-newsgroup-data)))))))
11002   
11003 (defun gnus-sortable-date (date)
11004   "Make sortable string by string-lessp from DATE.
11005 Timezone package is used."
11006   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
11007          (year (aref date 0))
11008          (month (aref date 1))
11009          (day (aref date 2)))
11010     (timezone-make-sortable-date 
11011      year month day 
11012      (timezone-make-time-string
11013       (aref date 3) (aref date 4) (aref date 5)))))
11014
11015
11016 ;; Summary saving commands.
11017
11018 (defun gnus-summary-save-article (&optional n)
11019   "Save the current article using the default saver function.
11020 If N is a positive number, save the N next articles.
11021 If N is a negative number, save the N previous articles.
11022 If N is nil and any articles have been marked with the process mark,
11023 save those articles instead.
11024 The variable `gnus-default-article-saver' specifies the saver function."
11025   (interactive "P")
11026   (gnus-set-global-variables)
11027   (let ((articles (gnus-summary-work-articles n))
11028         file)
11029     (while articles
11030       (let ((header (gnus-summary-article-header (car articles))))
11031         (if (vectorp header)
11032             (progn
11033               (save-window-excursion
11034                 (gnus-summary-select-article t nil nil (car articles)))
11035               (or gnus-save-all-headers
11036                   ;; Remove headers accoring to `gnus-saved-headers'.
11037                   (let ((gnus-visible-headers 
11038                          (or gnus-saved-headers gnus-visible-headers)))
11039                     (gnus-article-hide-headers t)))
11040               ;; Remove any X-Gnus lines.
11041               (save-excursion
11042                 (save-restriction
11043                   (set-buffer gnus-article-buffer)
11044                   (let ((buffer-read-only nil))
11045                     (goto-char (point-min))
11046                     (narrow-to-region (point) (or (search-forward "\n\n" nil t)
11047                                                   (point-max)))
11048                     (while (re-search-forward "^X-Gnus" nil t)
11049                       (beginning-of-line)
11050                       (delete-region (point)
11051                                      (progn (forward-line 1) (point))))
11052                     (widen))))
11053               (save-window-excursion
11054                 (if gnus-default-article-saver
11055                     (setq file (funcall
11056                                 gnus-default-article-saver
11057                                 (cond
11058                                  ((not gnus-prompt-before-saving)
11059                                   'default)
11060                                  ((eq gnus-prompt-before-saving 'always)
11061                                   nil)
11062                                  (t file))))
11063                   (error "No default saver is defined."))))
11064           (if (assq 'name header)
11065               (gnus-copy-file (cdr (assq 'name header)))
11066             (gnus-message 1 "Article %d is unsaveable" (car articles)))))
11067       (gnus-summary-remove-process-mark (car articles))
11068       (setq articles (cdr articles)))
11069     (gnus-summary-position-point)
11070     n))
11071
11072 (defun gnus-summary-pipe-output (&optional arg)
11073   "Pipe the current article to a subprocess.
11074 If N is a positive number, pipe the N next articles.
11075 If N is a negative number, pipe the N previous articles.
11076 If N is nil and any articles have been marked with the process mark,
11077 pipe those articles instead."
11078   (interactive "P")
11079   (gnus-set-global-variables)
11080   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
11081     (gnus-summary-save-article arg))
11082   (gnus-configure-windows 'pipe))
11083
11084 (defun gnus-summary-save-article-mail (&optional arg)
11085   "Append the current article to an mail file.
11086 If N is a positive number, save the N next articles.
11087 If N is a negative number, save the N previous articles.
11088 If N is nil and any articles have been marked with the process mark,
11089 save those articles instead."
11090   (interactive "P")
11091   (gnus-set-global-variables)
11092   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11093     (gnus-summary-save-article arg)))
11094
11095 (defun gnus-summary-save-article-rmail (&optional arg)
11096   "Append the current article to an rmail file.
11097 If N is a positive number, save the N next articles.
11098 If N is a negative number, save the N previous articles.
11099 If N is nil and any articles have been marked with the process mark,
11100 save those articles instead."
11101   (interactive "P")
11102   (gnus-set-global-variables)
11103   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11104     (gnus-summary-save-article arg)))
11105
11106 (defun gnus-summary-save-article-file (&optional arg)
11107   "Append the current article to a file.
11108 If N is a positive number, save the N next articles.
11109 If N is a negative number, save the N previous articles.
11110 If N is nil and any articles have been marked with the process mark,
11111 save those articles instead."
11112   (interactive "P")
11113   (gnus-set-global-variables)
11114   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11115     (gnus-summary-save-article arg)))
11116
11117 (defun gnus-summary-save-article-body-file (&optional arg)
11118   "Append the current article body to a file.
11119 If N is a positive number, save the N next articles.
11120 If N is a negative number, save the N previous articles.
11121 If N is nil and any articles have been marked with the process mark,
11122 save those articles instead."
11123   (interactive "P")
11124   (gnus-set-global-variables)
11125   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11126     (gnus-summary-save-article arg)))
11127
11128 (defun gnus-read-save-file-name (prompt default-name)
11129   (let ((methods gnus-split-methods)
11130         split-name)
11131     (if (not gnus-split-methods)
11132         ()
11133       (save-excursion
11134         (set-buffer gnus-article-buffer)
11135         (gnus-narrow-to-headers)
11136         (while methods
11137           (goto-char (point-min))
11138           (and (condition-case () 
11139                    (re-search-forward (car (car methods)) nil t)
11140                  (error nil))
11141                (setq split-name (cons (nth 1 (car methods)) split-name)))
11142           (setq methods (cdr methods)))
11143         (widen)))
11144     (cond ((null split-name)
11145            (read-file-name
11146             (concat prompt " (default "
11147                     (file-name-nondirectory default-name) ") ")
11148             (file-name-directory default-name)
11149             default-name))
11150           ((= 1 (length split-name))
11151            (read-file-name
11152             (concat prompt " (default " (car split-name) ") ")
11153             gnus-article-save-directory
11154             (concat gnus-article-save-directory (car split-name))))
11155           (t
11156            (setq split-name (mapcar (lambda (el) (list el))
11157                                     (nreverse split-name)))
11158            (let ((result (completing-read 
11159                           (concat prompt " ")
11160                           split-name nil nil)))
11161              (concat gnus-article-save-directory
11162                      (if (string= result "")
11163                          (car (car split-name))
11164                        result)))))))
11165
11166 (defun gnus-summary-save-in-rmail (&optional filename)
11167   "Append this article to Rmail file.
11168 Optional argument FILENAME specifies file name.
11169 Directory to save to is default to `gnus-article-save-directory' which
11170 is initialized from the SAVEDIR environment variable."
11171   (interactive)
11172   (gnus-set-global-variables)
11173   (let ((default-name
11174           (funcall gnus-rmail-save-name gnus-newsgroup-name
11175                    gnus-current-headers gnus-newsgroup-last-rmail)))
11176     (setq filename
11177           (cond ((eq filename 'default)
11178                  default-name)
11179                 (filename filename)
11180                 (t (gnus-read-save-file-name 
11181                     "Save in rmail file:" default-name))))
11182     (gnus-make-directory (file-name-directory filename))
11183     (gnus-eval-in-buffer-window 
11184      gnus-article-buffer
11185      (save-excursion
11186        (save-restriction
11187          (widen)
11188          (gnus-output-to-rmail filename))))
11189     ;; Remember the directory name to save articles
11190     (setq gnus-newsgroup-last-rmail filename)))
11191
11192 (defun gnus-summary-save-in-mail (&optional filename)
11193   "Append this article to Unix mail file.
11194 Optional argument FILENAME specifies file name.
11195 Directory to save to is default to `gnus-article-save-directory' which
11196 is initialized from the SAVEDIR environment variable."
11197   (interactive)
11198   (gnus-set-global-variables)
11199   (let ((default-name
11200           (funcall gnus-mail-save-name gnus-newsgroup-name
11201                    gnus-current-headers gnus-newsgroup-last-mail)))
11202     (setq filename
11203           (cond ((eq filename 'default)
11204                  default-name)
11205                 (filename filename)
11206                 (t (gnus-read-save-file-name 
11207                     "Save in Unix mail file:" default-name))))
11208     (setq filename
11209           (expand-file-name filename
11210                             (and default-name
11211                                  (file-name-directory default-name))))
11212     (gnus-make-directory (file-name-directory filename))
11213     (gnus-eval-in-buffer-window 
11214      gnus-article-buffer
11215      (save-excursion
11216        (save-restriction
11217          (widen)
11218          (if (and (file-readable-p filename) (rmail-file-p filename))
11219              (gnus-output-to-rmail filename)
11220            (rmail-output filename 1 t t)))))
11221     ;; Remember the directory name to save articles.
11222     (setq gnus-newsgroup-last-mail filename)))
11223
11224 (defun gnus-summary-save-in-file (&optional filename)
11225   "Append this article to file.
11226 Optional argument FILENAME specifies file name.
11227 Directory to save to is default to `gnus-article-save-directory' which
11228 is initialized from the SAVEDIR environment variable."
11229   (interactive)
11230   (gnus-set-global-variables)
11231   (let ((default-name
11232           (funcall gnus-file-save-name gnus-newsgroup-name
11233                    gnus-current-headers gnus-newsgroup-last-file)))
11234     (setq filename
11235           (cond ((eq filename 'default)
11236                  default-name)
11237                 (filename filename)
11238                 (t (gnus-read-save-file-name 
11239                     "Save in file:" default-name))))
11240     (gnus-make-directory (file-name-directory filename))
11241     (gnus-eval-in-buffer-window 
11242      gnus-article-buffer
11243      (save-excursion
11244        (save-restriction
11245          (widen)
11246          (gnus-output-to-file filename))))
11247     ;; Remember the directory name to save articles.
11248     (setq gnus-newsgroup-last-file filename)))
11249
11250 (defun gnus-summary-save-body-in-file (&optional filename)
11251   "Append this article body to a file.
11252 Optional argument FILENAME specifies file name.
11253 The directory to save in defaults to `gnus-article-save-directory' which
11254 is initialized from the SAVEDIR environment variable."
11255   (interactive)
11256   (gnus-set-global-variables)
11257   (let ((default-name
11258           (funcall gnus-file-save-name gnus-newsgroup-name
11259                    gnus-current-headers gnus-newsgroup-last-file)))
11260     (setq filename
11261           (cond ((eq filename 'default)
11262                  default-name)
11263                 (filename filename)
11264                 (t (gnus-read-save-file-name 
11265                     "Save body in file:" default-name))))
11266     (gnus-make-directory (file-name-directory filename))
11267     (gnus-eval-in-buffer-window 
11268      gnus-article-buffer
11269      (save-excursion
11270        (save-restriction
11271          (widen)
11272          (goto-char (point-min))
11273          (and (search-forward "\n\n" nil t)
11274               (narrow-to-region (point) (point-max)))
11275          (gnus-output-to-file filename))))
11276     ;; Remember the directory name to save articles.
11277     (setq gnus-newsgroup-last-file filename)))
11278
11279 (defun gnus-summary-save-in-pipe (&optional command)
11280   "Pipe this article to subprocess."
11281   (interactive)
11282   (gnus-set-global-variables)
11283   (setq command
11284         (cond ((eq command 'default)
11285                gnus-last-shell-command)
11286               (command command)
11287               (t (read-string "Shell command on article: "
11288                               gnus-last-shell-command))))
11289   (if (string-equal command "")
11290       (setq command gnus-last-shell-command))
11291   (gnus-eval-in-buffer-window 
11292    gnus-article-buffer
11293    (save-restriction
11294      (widen)
11295      (shell-command-on-region (point-min) (point-max) command nil)))
11296   (setq gnus-last-shell-command command))
11297
11298 ;; Summary extract commands
11299
11300 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11301   (let ((buffer-read-only nil)
11302         (article (gnus-summary-article-number))
11303         b)
11304     (or (gnus-summary-goto-subject article)
11305         (error (format "No such article: %d" article)))
11306     (gnus-summary-position-point)
11307     ;; If all commands are to be bunched up on one line, we collect
11308     ;; them here.  
11309     (if gnus-view-pseudos-separately
11310         ()
11311       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11312             files action)
11313         (while ps
11314           (setq action (cdr (assq 'action (car ps))))
11315           (setq files (list (cdr (assq 'name (car ps)))))
11316           (while (and ps (cdr ps)
11317                       (string= (or action "1")
11318                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
11319             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
11320             (setcdr ps (cdr (cdr ps))))
11321           (if (not files)
11322               ()
11323             (if (not (string-match "%s" action))
11324                 (setq files (cons " " files)))
11325             (setq files (cons " " files))
11326             (and (assq 'execute (car ps))
11327                  (setcdr (assq 'execute (car ps))
11328                          (funcall (if (string-match "%s" action)
11329                                       'format 'concat)
11330                                   action 
11331                                   (mapconcat (lambda (f) f) files " ")))))
11332           (setq ps (cdr ps)))))
11333     (if (and gnus-view-pseudos (not not-view))
11334         (while pslist
11335           (and (assq 'execute (car pslist))
11336                (gnus-execute-command (cdr (assq 'execute (car pslist)))
11337                                      (eq gnus-view-pseudos 'not-confirm)))
11338           (setq pslist (cdr pslist)))
11339       (save-excursion
11340         (while pslist
11341           (gnus-summary-goto-subject (or (cdr (assq 'article (car pslist)))
11342                                          (gnus-summary-article-number)))
11343           (beginning-of-line)
11344           (setq b (point))
11345           (put-text-property
11346            (point)
11347            (progn
11348              (insert "          " (file-name-nondirectory 
11349                                    (cdr (assq 'name (car pslist))))
11350                      ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11351              (point))
11352            'gnus-number gnus-reffed-article-number)
11353           (gnus-data-enter
11354            (gnus-summary-article-number) 
11355            gnus-reffed-article-number gnus-unread-mark 
11356            (progn (forward-line 1) (point))
11357            (car pslist) 0 (- (point) b))
11358           (forward-line -1)
11359           (setq gnus-newsgroup-unreads
11360                 (cons gnus-reffed-article-number gnus-newsgroup-unreads))
11361           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11362           (setq pslist (cdr pslist)))))))
11363
11364 (defun gnus-pseudos< (p1 p2)
11365   (let ((c1 (cdr (assq 'action p1)))
11366         (c2 (cdr (assq 'action p2))))
11367     (and c1 c2 (string< c1 c2))))
11368
11369 (defun gnus-request-pseudo-article (props)
11370   (cond ((assq 'execute props)
11371          (gnus-execute-command (cdr (assq 'execute props)))))
11372   (let ((gnus-current-article (gnus-summary-article-number)))
11373     (run-hooks 'gnus-mark-article-hook)))
11374
11375 (defun gnus-execute-command (command &optional automatic)
11376   (save-excursion
11377     (gnus-article-setup-buffer)
11378     (set-buffer gnus-article-buffer)
11379     (let ((command (if automatic command (read-string "Command: " command)))
11380           (buffer-read-only nil))
11381       (erase-buffer)
11382       (insert "$ " command "\n\n")
11383       (if gnus-view-pseudo-asynchronously
11384           (start-process "gnus-execute" nil "sh" "-c" command)
11385         (call-process "sh" nil t nil "-c" command)))))
11386
11387 (defun gnus-copy-file (file &optional to)
11388   "Copy FILE to TO."
11389   (interactive
11390    (list (read-file-name "Copy file: " default-directory)
11391          (read-file-name "Copy file to: " default-directory)))
11392   (gnus-set-global-variables)
11393   (or to (setq to (read-file-name "Copy file to: " default-directory)))
11394   (and (file-directory-p to) 
11395        (setq to (concat (file-name-as-directory to)
11396                         (file-name-nondirectory file))))
11397   (copy-file file to))
11398
11399 ;; Summary kill commands.
11400
11401 (defun gnus-summary-edit-global-kill (article)
11402   "Edit the \"global\" kill file."
11403   (interactive (list (gnus-summary-article-number)))
11404   (gnus-set-global-variables)
11405   (gnus-group-edit-global-kill article))
11406
11407 (defun gnus-summary-edit-local-kill ()
11408   "Edit a local kill file applied to the current newsgroup."
11409   (interactive)
11410   (gnus-set-global-variables)
11411   (setq gnus-current-headers (gnus-summary-article-header))
11412   (gnus-set-global-variables)
11413   (gnus-group-edit-local-kill 
11414    (gnus-summary-article-number) gnus-newsgroup-name))
11415
11416 \f
11417 ;;;
11418 ;;; Gnus article mode
11419 ;;;
11420
11421 (put 'gnus-article-mode 'mode-class 'special)
11422
11423 (if gnus-article-mode-map
11424     nil
11425   (setq gnus-article-mode-map (make-keymap))
11426   (suppress-keymap gnus-article-mode-map)
11427   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
11428   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
11429   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
11430   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
11431   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
11432   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
11433   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
11434   (define-key gnus-article-mode-map gnus-mouse-2 'gnus-article-push-button)
11435   (define-key gnus-article-mode-map "\r" 'gnus-article-press-button)
11436   (define-key gnus-article-mode-map "\t" 'gnus-article-next-button)
11437   (define-key gnus-article-mode-map "\C-c\C-b" 'gnus-bug)
11438   
11439   ;; Duplicate almost all summary keystrokes in the article mode map.
11440   (let ((commands 
11441          (list 
11442           "p" "N" "P" "\M-\C-n" "\M-\C-p"
11443           "\M-n" "\M-p" "." "," "\M-s" "\M-r" "<" ">" "j"
11444           "u" "!" "U" "d" "D" "E" "\M-u" "\M-U" "k" "\C-k" "\M-\C-k"
11445           "\M-\C-l" "e" "#" "\M-#" "\M-\C-t" "\M-\C-s" "\M-\C-h"
11446           "\M-\C-f" "\M-\C-b" "\M-\C-u" "\M-\C-d" "&" "\C-w"
11447           "\C-t" "?" "\C-c\M-\C-s" "\C-c\C-s\C-n" "\C-c\C-s\C-a"
11448           "\C-c\C-s\C-s" "\C-c\C-s\C-d" "\C-c\C-s\C-i" "\C-x\C-s"
11449           "\M-g" "w" "\C-c\C-r" "\M-t" "C"
11450           "o" "\C-o" "|" "\M-k" "\M-K" "V" "\C-c\C-d"
11451           "\C-c\C-i" "x" "X" "t" "g" "?" "l"
11452           "\C-c\C-v\C-v" "\C-d" "v" 
11453 ;;        "Mt" "M!" "Md" "Mr"
11454 ;;        "Mc" "M " "Me" "Mx" "M?" "Mb" "MB" "M#" "M\M-#" "M\M-r"
11455 ;;        "M\M-\C-r" "MD" "M\M-D" "MS" "MC" "MH" "M\C-c" "Mk" "MK"
11456 ;;        "Ms" "Mc" "Mu" "Mm" "Mk" "Gn" "Gp" "GN" "GP" "G\C-n" "G\C-p"
11457 ;;        "G\M-n" "G\M-p" "Gf" "Gb" "Gg" "Gl" "Gp" "Tk" "Tl" "Ti" "TT"
11458 ;;        "Ts" "TS" "Th" "TH" "Tn" "Tp" "Tu" "Td" "T#" "A " "An" "A\177" "Ap"
11459 ;;        "A\r" "A<" "A>" "Ab" "Ae" "A^" "Ar" "Aw" "Ac" "Ag" "At" "Am"
11460 ;;        "As" "Wh" "Ws" "Wc" "Wo" "Ww" "Wd" "Wq" "Wf" "Wt" "W\C-t"
11461 ;;        "WT" "WA" "Wa" "WH" "WC" "WS" "Wb" "Hv" "Hf" "Hd" "Hh" "Hi"
11462 ;;        "Be" "B\177" "Bm" "Br" "Bw" "Bc" "Bq" "Bi" "Oo" "Om" "Or"
11463 ;;        "Of" "Oh" "Ov" "Op" "Vu" "V\C-s" "V\C-r" "Vr" "V&" "VT" "Ve"
11464 ;;        "VD" "Vk" "VK" "Vsn" "Vsa" "Vss" "Vsd" "Vsi"
11465           )))
11466     (while commands
11467       (define-key gnus-article-mode-map (car commands) 
11468         'gnus-article-summary-command)
11469       (setq commands (cdr commands))))
11470
11471   (let ((commands (list "q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
11472 ;;                      "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP" 
11473                          "=" "n"  "^" "\M-^")))
11474     (while commands
11475       (define-key gnus-article-mode-map (car commands) 
11476         'gnus-article-summary-command-nosave)
11477       (setq commands (cdr commands)))))
11478
11479
11480 (defun gnus-article-mode ()
11481   "Major mode for displaying an article.
11482
11483 All normal editing commands are switched off.
11484
11485 The following commands are available:
11486
11487 \\<gnus-article-mode-map>
11488 \\[gnus-article-next-page]\t Scroll the article one page forwards
11489 \\[gnus-article-prev-page]\t Scroll the article one page backwards
11490 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
11491 \\[gnus-article-show-summary]\t Display the summary buffer
11492 \\[gnus-article-mail]\t Send a reply to the address near point
11493 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
11494 \\[gnus-info-find-node]\t Go to the Gnus info node"
11495   (interactive)
11496   (if (gnus-visual-p 'article-menu 'menu) (gnus-article-make-menu-bar))
11497   (kill-all-local-variables)
11498   (setq mode-line-modified "-- ")
11499   (make-local-variable 'mode-line-format)
11500   (setq mode-line-format (copy-sequence mode-line-format))
11501   (and (equal (nth 3 mode-line-format) "   ")
11502        (setcar (nthcdr 3 mode-line-format) ""))
11503   (setq mode-name "Article")
11504   (setq major-mode 'gnus-article-mode)
11505   (make-local-variable 'minor-mode-alist)
11506   (or (assq 'gnus-show-mime minor-mode-alist)
11507       (setq minor-mode-alist
11508             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
11509   (use-local-map gnus-article-mode-map)
11510   (make-local-variable 'page-delimiter)
11511   (setq page-delimiter gnus-page-delimiter)
11512   (buffer-disable-undo (current-buffer))
11513   (setq buffer-read-only t)             ;Disable modification
11514   (run-hooks 'gnus-article-mode-hook))
11515
11516 (defun gnus-article-setup-buffer ()
11517   "Initialize article mode buffer."
11518   ;; Returns the article buffer.
11519   (if (get-buffer gnus-article-buffer)
11520       (save-excursion
11521         (set-buffer gnus-article-buffer)
11522         (buffer-disable-undo (current-buffer))
11523         (setq buffer-read-only t)
11524         (gnus-add-current-to-buffer-list)
11525         (or (eq major-mode 'gnus-article-mode)
11526             (gnus-article-mode))
11527         (current-buffer))
11528     (save-excursion
11529       (set-buffer (get-buffer-create gnus-article-buffer))
11530       (gnus-add-current-to-buffer-list)
11531       (gnus-article-mode)
11532       (current-buffer))))
11533
11534 ;; Set article window start at LINE, where LINE is the number of lines
11535 ;; from the head of the article.
11536 (defun gnus-article-set-window-start (&optional line)
11537   (set-window-start 
11538    (get-buffer-window gnus-article-buffer)
11539    (save-excursion
11540      (set-buffer gnus-article-buffer)
11541      (goto-char (point-min))
11542      (if (not line)
11543          (point-min)
11544        (gnus-message 6 "Moved to bookmark")
11545        (search-forward "\n\n" nil t)
11546        (forward-line line)
11547        (point)))))
11548
11549 (defun gnus-request-article-this-buffer (article group)
11550   "Get an article and insert it into this buffer."
11551   (prog1
11552       (save-excursion
11553         (if (get-buffer gnus-original-article-buffer)
11554             (set-buffer (get-buffer gnus-original-article-buffer))
11555           (set-buffer (get-buffer-create gnus-original-article-buffer))
11556           (buffer-disable-undo (current-buffer))
11557           (setq major-mode 'gnus-original-article-mode)
11558           (gnus-add-current-to-buffer-list))
11559
11560         (setq group (or group gnus-newsgroup-name))
11561
11562         ;; Open server if it has closed.
11563         (gnus-check-server (gnus-find-method-for-group group))
11564
11565         ;; Using `gnus-request-article' directly will insert the article into
11566         ;; `nntp-server-buffer' - so we'll save some time by not having to
11567         ;; copy it from the server buffer into the article buffer.
11568
11569         ;; We only request an article by message-id when we do not have the
11570         ;; headers for it, so we'll have to get those.
11571         (and (stringp article) 
11572              (let ((gnus-override-method gnus-refer-article-method))
11573                (gnus-read-header article)))
11574
11575         ;; If the article number is negative, that means that this article
11576         ;; doesn't belong in this newsgroup (possibly), so we find its
11577         ;; message-id and request it by id instead of number.
11578         (if (not (numberp article))
11579             ()
11580           (save-excursion
11581             (set-buffer gnus-summary-buffer)
11582             (let ((header (gnus-summary-article-header article)))
11583               (if (< article 0)
11584                   (if (vectorp header)
11585                       ;; It's a real article.
11586                       (setq article (mail-header-id header))
11587                     ;; It is an extracted pseudo-article.
11588                     (setq article 'pseudo)
11589                     (gnus-request-pseudo-article header)))
11590
11591               (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
11592                 (if (not (eq (car method) 'nneething))
11593                     ()
11594                   (let ((dir (concat (file-name-as-directory (nth 1 method))
11595                                      (mail-header-subject header))))
11596                     (if (file-directory-p dir)
11597                         (progn
11598                           (setq article 'nneething)
11599                           (gnus-group-enter-directory dir)))))))))
11600
11601         (cond 
11602          ;; We first check `gnus-original-article-buffer'.
11603          ((and (equal (car gnus-original-article) group)
11604                (eq (cdr gnus-original-article) article))
11605           ;; We don't have to do anything, since it's already where we
11606           ;; want it.  
11607           'article)
11608          ;; Check the backlog.
11609          ((and gnus-keep-backlog
11610                (gnus-backlog-request-article group article (current-buffer)))
11611           'article)
11612          ;; Check the cache.
11613          ((and gnus-use-cache
11614                (numberp article)
11615                (gnus-cache-request-article article group))
11616           'article)
11617          ;; Get the article from the backend.
11618          ((or (stringp article) (numberp article))
11619           (erase-buffer)
11620           (let ((gnus-override-method 
11621                  (and (stringp article) gnus-refer-article-method)))
11622             (if (gnus-request-article article group (current-buffer))
11623                 (progn
11624                   (and gnus-keep-backlog 
11625                        (gnus-backlog-enter-article 
11626                         group article (current-buffer)))
11627                   'article))))
11628          ;; It was a pseudo.
11629          (t article)))
11630     (setq gnus-original-article (cons group article))
11631     (let (buffer-read-only)
11632       (erase-buffer)
11633       (insert-buffer-substring gnus-original-article-buffer))))
11634
11635 (defun gnus-read-header (id)
11636   "Read the headers of article ID and enter them into the Gnus system."
11637   (let ((group gnus-newsgroup-name)
11638         (headers gnus-newsgroup-headers)
11639         header where)
11640     ;; First we check to see whether the header in question is already
11641     ;; fetched. 
11642     (if (stringp id)
11643         ;; This is a Message-ID.
11644         (while headers
11645           (if (string= id (mail-header-id (car headers)))
11646               (setq header (car headers)
11647                     headers nil)
11648             (setq headers (cdr headers))))
11649       ;; This is an article number.
11650       (while headers
11651         (if (= id (mail-header-number (car headers)))
11652             (setq header (car headers)
11653                   headers nil)
11654           (setq headers (cdr headers)))))
11655     (if header
11656         ;; We have found the header.
11657         header
11658       ;; We have to really fetch the header to this article.
11659       (when (setq where
11660                   (if (gnus-check-backend-function 'request-head group)
11661                       (gnus-request-head id group)
11662                     (gnus-request-article id group)))
11663         (save-excursion
11664           (set-buffer nntp-server-buffer)
11665           (and (search-forward "\n\n" nil t)
11666                (delete-region (1- (point)) (point-max)))
11667           (goto-char (point-max))
11668           (insert ".\n")
11669           (goto-char (point-min))
11670           (insert "211 "
11671                   (int-to-string
11672                    (cond
11673                     ((numberp id)
11674                      id)
11675                     ((cdr where)
11676                      (cdr where))
11677                     (t
11678                      gnus-reffed-article-number)))
11679                   " Article retrieved.\n"))
11680         (if (not (setq header (car (gnus-get-newsgroup-headers))))
11681             () ; Malformed head.
11682           (if (and (stringp id)
11683                    (not (string= (gnus-group-real-name group)
11684                                  (car where))))
11685               ;; If we fetched by Message-ID and the article came
11686               ;; from a different group, we fudge some bogus article
11687               ;; numbers for this article.
11688               (mail-header-set-number header gnus-reffed-article-number))
11689           (decf gnus-reffed-article-number)
11690           (push header gnus-newsgroup-headers)
11691           (setq gnus-current-headers header)
11692           (push (mail-header-number header) gnus-newsgroup-limit)
11693           header)))))
11694
11695 (defun gnus-article-prepare (article &optional all-headers header)
11696   "Prepare ARTICLE in article mode buffer.
11697 ARTICLE should either be an article number or a Message-ID.
11698 If ARTICLE is an id, HEADER should be the article headers.
11699 If ALL-HEADERS is non-nil, no headers are hidden."
11700   (save-excursion
11701     ;; Make sure we start in a summary buffer.
11702     (or (eq major-mode 'gnus-summary-mode)
11703         (set-buffer gnus-summary-buffer))
11704     (setq gnus-summary-buffer (current-buffer))
11705     ;; Make sure the connection to the server is alive.
11706     (or (gnus-server-opened (gnus-find-method-for-group gnus-newsgroup-name))
11707         (progn
11708           (gnus-check-server 
11709            (gnus-find-method-for-group gnus-newsgroup-name))
11710           (gnus-request-group gnus-newsgroup-name t)))
11711     (let* ((article (if header (mail-header-number header) article))
11712            (summary-buffer (current-buffer))
11713            (internal-hook gnus-article-internal-prepare-hook)
11714            (group gnus-newsgroup-name)
11715            result)
11716       (save-excursion
11717         (gnus-article-setup-buffer)
11718         (set-buffer gnus-article-buffer)
11719         (if (not (setq result (let ((buffer-read-only nil))
11720                                 (gnus-request-article-this-buffer 
11721                                  article group))))
11722             ;; There is no such article.
11723             (save-excursion
11724               (if (not (numberp article))
11725                   ()
11726                 (setq gnus-article-current 
11727                       (cons gnus-newsgroup-name article))
11728                 (set-buffer gnus-summary-buffer)
11729                 (setq gnus-current-article article)
11730                 (gnus-summary-mark-article article gnus-canceled-mark))
11731               (gnus-message 1 "No such article (may be canceled)")
11732               (ding)
11733               nil)
11734           (if (or (eq result 'pseudo) (eq result 'nneething))
11735               (progn
11736                 (save-excursion
11737                   (set-buffer summary-buffer)
11738                   (setq gnus-last-article gnus-current-article
11739                         gnus-newsgroup-history (cons gnus-current-article
11740                                                      gnus-newsgroup-history)
11741                         gnus-current-article 0
11742                         gnus-current-headers nil
11743                         gnus-article-current nil)
11744                   (if (eq result 'nneething)
11745                       (gnus-configure-windows 'summary)
11746                     (gnus-configure-windows 'article))
11747                   (gnus-set-global-variables))
11748                 (gnus-set-mode-line 'article))
11749             ;; The result from the `request' was an actual article -
11750             ;; or at least some text that is now displayed in the
11751             ;; article buffer.
11752             (if (and (numberp article)
11753                      (not (eq article gnus-current-article)))
11754                 ;; Seems like a new article has been selected.
11755                 ;; `gnus-current-article' must be an article number.
11756                 (save-excursion
11757                   (set-buffer summary-buffer)
11758                   (setq gnus-last-article gnus-current-article
11759                         gnus-newsgroup-history (cons gnus-current-article
11760                                                      gnus-newsgroup-history)
11761                         gnus-current-article article
11762                         gnus-current-headers 
11763                         (gnus-summary-article-header gnus-current-article)
11764                         gnus-article-current 
11765                         (cons gnus-newsgroup-name gnus-current-article))
11766                   (gnus-summary-show-thread)
11767                   (run-hooks 'gnus-mark-article-hook)
11768                   (gnus-set-mode-line 'summary)
11769                   (and (gnus-visual-p 'article-highlight 'highlight)
11770                        (run-hooks 'gnus-visual-mark-article-hook))
11771                   ;; Set the global newsgroup variables here.
11772                   ;; Suggested by Jim Sisolak
11773                   ;; <sisolak@trans4.neep.wisc.edu>.
11774                   (gnus-set-global-variables)
11775                   (setq gnus-have-all-headers 
11776                         (or all-headers gnus-show-all-headers))
11777                   (and gnus-use-cache 
11778                        (vectorp (gnus-summary-article-header article))
11779                        (gnus-cache-possibly-enter-article
11780                         group article
11781                         (gnus-summary-article-header article)
11782                         (memq article gnus-newsgroup-marked)
11783                         (memq article gnus-newsgroup-dormant)
11784                         (memq article gnus-newsgroup-unreads)))))
11785             ;; Hooks for getting information from the article.
11786             ;; This hook must be called before being narrowed.
11787             (let (buffer-read-only)
11788               (run-hooks 'internal-hook)
11789               (run-hooks 'gnus-article-prepare-hook)
11790               ;; Decode MIME message.
11791               (if (and gnus-show-mime
11792                        (or (not gnus-strict-mime)
11793                            (gnus-fetch-field "Mime-Version")))
11794                   (funcall gnus-show-mime-method))
11795               ;; Perform the article display hooks.
11796               (run-hooks 'gnus-article-display-hook))
11797             ;; Do page break.
11798             (goto-char (point-min))
11799             (and gnus-break-pages (gnus-narrow-to-page))
11800             (gnus-set-mode-line 'article)
11801             (gnus-configure-windows 'article)
11802             (goto-char (point-min))
11803             t))))))
11804
11805 (defun gnus-article-show-all-headers ()
11806   "Show all article headers in article mode buffer."
11807   (save-excursion 
11808     (gnus-article-setup-buffer)
11809     (set-buffer gnus-article-buffer)
11810     (let ((buffer-read-only nil))
11811       (remove-text-properties (point-min) (point-max) 
11812                               gnus-hidden-properties))))
11813
11814 (defun gnus-article-hide-headers-if-wanted ()
11815   "Hide unwanted headers if `gnus-have-all-headers' is nil.
11816 Provided for backwards compatability."
11817   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
11818       (gnus-article-hide-headers)))
11819
11820 (defun gnus-article-hide-headers (&optional delete)
11821   "Hide unwanted headers and possibly sort them as well."
11822   (interactive "P")
11823   (save-excursion
11824     (set-buffer gnus-article-buffer)
11825     (save-restriction
11826       (let ((sorted gnus-sorted-header-list)
11827             (buffer-read-only nil)
11828             want-list beg want-l)
11829         ;; First we narrow to just the headers.
11830         (widen)
11831         (goto-char (point-min))
11832         ;; Hide any "From " lines at the beginning of (mail) articles. 
11833         (while (looking-at "From ")
11834           (forward-line 1))
11835         (or (bobp) 
11836             (add-text-properties (point-min) (point) gnus-hidden-properties))
11837         ;; Then treat the rest of the header lines.
11838         (narrow-to-region 
11839          (point) 
11840          (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
11841         ;; Then we use the two regular expressions
11842         ;; `gnus-ignored-headers' and `gnus-visible-headers' to
11843         ;; select which header lines is to remain visible in the
11844         ;; article buffer.
11845         (goto-char (point-min))
11846         (while (re-search-forward "^[^ \t]*:" nil t)
11847           (beginning-of-line)
11848           ;; We add the headers we want to keep to a list and delete
11849           ;; them from the buffer.
11850           (if (or (and (stringp gnus-visible-headers)
11851                        (looking-at gnus-visible-headers))
11852                   (and (not (stringp gnus-visible-headers))
11853                        (stringp gnus-ignored-headers)
11854                        (not (looking-at gnus-ignored-headers))))
11855               (progn
11856                 (setq beg (point))
11857                 (forward-line 1)
11858                 ;; Be sure to get multi-line headers...
11859                 (re-search-forward "^[^ \t]*:" nil t)
11860                 (beginning-of-line)
11861                 (setq want-list 
11862                       (cons (buffer-substring beg (point)) want-list))
11863                 (delete-region beg (point))
11864                 (goto-char beg))
11865             (forward-line 1)))
11866         ;; Next we perform the sorting by looking at
11867         ;; `gnus-sorted-header-list'. 
11868         (goto-char (point-min))
11869         (while (and sorted want-list)
11870           (setq want-l want-list)
11871           (while (and want-l
11872                       (not (string-match (car sorted) (car want-l))))
11873             (setq want-l (cdr want-l)))
11874           (if want-l 
11875               (progn
11876                 (insert (car want-l))
11877                 (setq want-list (delq (car want-l) want-list))))
11878           (setq sorted (cdr sorted)))
11879         ;; Any headers that were not matched by the sorted list we
11880         ;; just tack on the end of the visible header list.
11881         (while want-list
11882           (insert (car want-list))
11883           (setq want-list (cdr want-list)))
11884         ;; And finally we make the unwanted headers invisible.
11885         (if delete
11886             (delete-region (point) (point-max))
11887           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
11888           (add-text-properties (point) (point-max) gnus-hidden-properties))))))
11889
11890 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
11891 (defun gnus-article-treat-overstrike ()
11892   "Translate overstrikes into bold text."
11893   (interactive)
11894   (save-excursion
11895     (set-buffer gnus-article-buffer)
11896     (let ((buffer-read-only nil))
11897       (while (search-forward "\b" nil t)
11898         (let ((next (following-char))
11899               (previous (char-after (- (point) 2))))
11900           (cond ((eq next previous)
11901                  (put-text-property (- (point) 2) (point)
11902                                     'invisible t)
11903                  (put-text-property (point) (1+ (point))
11904                                     'face 'bold))
11905                 ((eq next ?_)
11906                  (put-text-property (1- (point)) (1+ (point))
11907                                     'invisible t)
11908                  (put-text-property (- (point) 2) (1- (point))
11909                                     'face 'underline))
11910                 ((eq previous ?_)
11911                  (put-text-property (- (point) 2) (point)
11912                                     'invisible t)
11913                  (put-text-property (point) (1+ (point))
11914                                     'face 'underline))))))))
11915
11916 (defun gnus-article-word-wrap ()
11917   "Format too long lines."
11918   (interactive)
11919   (save-excursion
11920     (set-buffer gnus-article-buffer)
11921     (let ((buffer-read-only nil)
11922           p)
11923       (widen)
11924       (goto-char (point-min))
11925       (search-forward "\n\n" nil t)
11926       (end-of-line 1)
11927       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
11928             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
11929             (adaptive-fill-mode t))
11930         (while (not (eobp))
11931           (and (>= (current-column) (min fill-column (window-width)))
11932                (/= (preceding-char) ?:)
11933                (fill-paragraph nil))
11934           (end-of-line 2))))))
11935
11936 (defun gnus-article-remove-cr ()
11937   "Remove carriage returns from an article."
11938   (interactive)
11939   (save-excursion
11940     (set-buffer gnus-article-buffer)
11941     (let ((buffer-read-only nil))
11942       (goto-char (point-min))
11943       (while (search-forward "\r" nil t)
11944         (replace-match "" t t)))))
11945
11946 (defun gnus-article-display-x-face (&optional force)
11947   "Look for an X-Face header and display it if present."
11948   (interactive (list 'force))
11949   (save-excursion
11950     (set-buffer gnus-article-buffer)
11951     ;; delete old process
11952     (and (process-status "gnus-x-face")
11953          (delete-process "gnus-x-face"))
11954     (let ((inhibit-point-motion-hooks t)
11955           (case-fold-search nil)
11956           from)
11957       (save-restriction
11958         (goto-char (point-min))
11959         (search-forward "\n\n")
11960         (narrow-to-region (point-min) (point))
11961         (goto-char (point-min))
11962         (setq from (mail-fetch-field "from"))
11963         (if (not (and gnus-article-x-face-command
11964                       (or force
11965                           (not gnus-article-x-face-too-ugly)
11966                           (and gnus-article-x-face-too-ugly from
11967                                (not (string-match gnus-article-x-face-too-ugly
11968                                                   from))))
11969                       (progn
11970                         (goto-char (point-min))
11971                         (re-search-forward "^X-Face: " nil t))))
11972             nil
11973           (let ((beg (point))
11974                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
11975             (if (symbolp gnus-article-x-face-command)
11976                 (and (or (fboundp gnus-article-x-face-command)
11977                          (error "%s is not a function"
11978                                 gnus-article-x-face-command))
11979                      (funcall gnus-article-x-face-command beg end))
11980               (let ((process-connection-type nil))
11981                 (process-kill-without-query
11982                  (start-process "gnus-x-face" nil "sh" "-c"
11983                                 gnus-article-x-face-command))
11984                 (process-send-region "gnus-x-face" beg end)
11985                 (process-send-eof "gnus-x-face")))))))))
11986
11987 (defun gnus-article-de-quoted-unreadable (&optional force)
11988   "Do a naive translation of a quoted-printable-encoded article.
11989 This is in no way, shape or form meant as a replacement for real MIME
11990 processing, but is simply a stop-gap measure until MIME support is
11991 written.
11992 If FORCE, decode the article whether it is marked as quoted-printable
11993 or not." 
11994   (interactive (list 'force))
11995   (save-excursion
11996     (set-buffer gnus-article-buffer)
11997     (let ((case-fold-search t)
11998           (buffer-read-only nil)
11999           (type (gnus-fetch-field "content-transfer-encoding")))
12000       (if (or force (and type (string-match "quoted-printable" type)))
12001           (progn
12002             (goto-char (point-min))
12003             (search-forward "\n\n" nil 'move)
12004             (gnus-mime-decode-quoted-printable (point) (point-max)))))))
12005
12006 (defun gnus-mime-decode-quoted-printable (from to)
12007   ;; Decode quoted-printable from region between FROM and TO.
12008   (save-excursion
12009     (goto-char from)
12010     (while (search-forward "=" to t)
12011       (cond ((eq (following-char) ?\n)
12012              (delete-char -1)
12013              (delete-char 1))
12014             ((looking-at "[0-9A-F][0-9A-F]")
12015              (delete-char -1)
12016              (insert (hexl-hex-string-to-integer
12017                       (buffer-substring (point) (+ 2 (point)))))
12018              (delete-char 2))
12019             ((looking-at "=")
12020              (delete-char 1))
12021             ((gnus-message 3 "Malformed MIME quoted-printable message"))))))
12022
12023 (defun gnus-article-hide-pgp ()
12024   "Hide any PGP headers and signatures in the current article."
12025   (interactive)
12026   (save-excursion
12027     (set-buffer gnus-article-buffer)
12028     (let (buffer-read-only)
12029       (goto-char (point-min))
12030       ;; Hide the "header".
12031       (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
12032            (add-text-properties (match-beginning 0) (match-end 0)
12033                                 gnus-hidden-properties))
12034       ;; Hide the actual signature.
12035       (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
12036            (add-text-properties 
12037             (match-beginning 0)
12038             (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
12039                 (match-end 0)
12040               ;; Perhaps we shouldn't hide to the end of the buffer
12041               ;; if there is no end to the signature?
12042               (point-max))
12043             gnus-hidden-properties)))))
12044       
12045
12046 (defvar gnus-article-time-units
12047   (list (cons 'year (* 365.25 24 60 60))
12048         (cons 'week (* 7 24 60 60))
12049         (cons 'day (* 24 60 60))
12050         (cons 'hour (* 60 60))
12051         (cons 'minute 60)
12052         (cons 'second 1)))
12053
12054 (defun gnus-article-date-ut (&optional type)
12055   "Convert DATE date to universal time in the current article.
12056 If TYPE is `local', convert to local time; if it is `lapsed', output
12057 how much time has lapsed since DATE."
12058   (interactive (list 'ut))
12059   (let ((date (mail-header-date (or gnus-current-headers 
12060                                     (gnus-summary-article-header) "")))
12061         (date-regexp "^Date: \\|^X-Sent: "))
12062     (if (or (not date)
12063             (string= date ""))
12064         ()
12065       (save-excursion
12066         (set-buffer gnus-article-buffer)
12067         (let ((buffer-read-only nil))
12068           (goto-char (point-min))
12069           (if (and (re-search-forward date-regexp nil t)
12070                    (progn 
12071                      (beginning-of-line)
12072                      (looking-at date-regexp)))
12073               (delete-region (gnus-point-at-bol)
12074                              (progn (end-of-line) (1+ (point))))
12075             (goto-char (point-min))
12076             (goto-char (- (search-forward "\n\n") 2)))
12077           (insert
12078            (cond 
12079             ((eq type 'local)
12080              (concat "Date: " (condition-case ()
12081                                   (timezone-make-date-arpa-standard date)
12082                                 (error date))
12083                      "\n"))
12084             ((eq type 'ut)
12085              (concat "Date: "
12086                      (condition-case ()
12087                          (timezone-make-date-arpa-standard date nil "UT")
12088                        (error date))
12089                      "\n"))
12090             ((eq type 'original)
12091              (concat "Date: " date "\n"))
12092             ((eq type 'lapsed)
12093              ;; If the date is seriously mangled, the timezone
12094              ;; functions are liable to bug out, so we condition-case
12095              ;; the entire thing.  
12096              (let* ((real-sec (condition-case ()
12097                                   (- (gnus-seconds-since-epoch 
12098                                       (timezone-make-date-arpa-standard
12099                                        (current-time-string) 
12100                                        (current-time-zone) "UT"))
12101                                      (gnus-seconds-since-epoch 
12102                                       (timezone-make-date-arpa-standard 
12103                                        date nil "UT")))
12104                                 (error 0)))
12105                     (sec (abs real-sec))
12106                     num prev)
12107                (if (zerop sec)
12108                    "X-Sent: Now\n"
12109                  (concat
12110                   "X-Sent: "
12111                   (mapconcat 
12112                    (lambda (unit)
12113                      (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
12114                          ""
12115                        (setq sec (- sec (* num (cdr unit))))
12116                        (prog1
12117                            (concat (if prev ", " "") (int-to-string 
12118                                                       (floor num))
12119                                    " " (symbol-name (car unit))
12120                                    (if (> num 1) "s" ""))
12121                          (setq prev t))))
12122                    gnus-article-time-units "")
12123                   (if (> real-sec 0)
12124                       " ago\n"
12125                     " in the future\n")))))
12126             (t
12127              (error "Unknown conversion type: %s" type)))))))))
12128
12129 (defun gnus-article-date-local ()
12130   "Convert the current article date to the local timezone."
12131   (interactive)
12132   (gnus-article-date-ut 'local))
12133
12134 (defun gnus-article-date-original ()
12135   "Convert the current article date to what it was originally.
12136 This is only useful if you have used some other date conversion
12137 function and want to see what the date was before converting."
12138   (interactive)
12139   (gnus-article-date-ut 'original))
12140
12141 (defun gnus-article-date-lapsed ()
12142   "Convert the current article date to time lapsed since it was sent."
12143   (interactive)
12144   (gnus-article-date-ut 'lapsed))
12145
12146 (defun gnus-article-maybe-highlight ()
12147   "Do some article highlighting if `gnus-visual' is non-nil."
12148   (if (gnus-visual-p 'article-highlight 'highlight)
12149       (gnus-article-highlight-some)))
12150
12151 ;; Article savers.
12152
12153 (defun gnus-output-to-rmail (file-name)
12154   "Append the current article to an Rmail file named FILE-NAME."
12155   (require 'rmail)
12156   ;; Most of these codes are borrowed from rmailout.el.
12157   (setq file-name (expand-file-name file-name))
12158   (setq rmail-default-rmail-file file-name)
12159   (let ((artbuf (current-buffer))
12160         (tmpbuf (get-buffer-create " *Gnus-output*")))
12161     (save-excursion
12162       (or (get-file-buffer file-name)
12163           (file-exists-p file-name)
12164           (if (gnus-yes-or-no-p
12165                (concat "\"" file-name "\" does not exist, create it? "))
12166               (let ((file-buffer (create-file-buffer file-name)))
12167                 (save-excursion
12168                   (set-buffer file-buffer)
12169                   (rmail-insert-rmail-file-header)
12170                   (let ((require-final-newline nil))
12171                     (write-region (point-min) (point-max) file-name t 1)))
12172                 (kill-buffer file-buffer))
12173             (error "Output file does not exist")))
12174       (set-buffer tmpbuf)
12175       (buffer-disable-undo (current-buffer))
12176       (erase-buffer)
12177       (insert-buffer-substring artbuf)
12178       (gnus-convert-article-to-rmail)
12179       ;; Decide whether to append to a file or to an Emacs buffer.
12180       (let ((outbuf (get-file-buffer file-name)))
12181         (if (not outbuf)
12182             (append-to-file (point-min) (point-max) file-name)
12183           ;; File has been visited, in buffer OUTBUF.
12184           (set-buffer outbuf)
12185           (let ((buffer-read-only nil)
12186                 (msg (and (boundp 'rmail-current-message)
12187                           (symbol-value 'rmail-current-message))))
12188             ;; If MSG is non-nil, buffer is in RMAIL mode.
12189             (if msg
12190                 (progn (widen)
12191                        (narrow-to-region (point-max) (point-max))))
12192             (insert-buffer-substring tmpbuf)
12193             (if msg
12194                 (progn
12195                   (goto-char (point-min))
12196                   (widen)
12197                   (search-backward "\^_")
12198                   (narrow-to-region (point) (point-max))
12199                   (goto-char (1+ (point-min)))
12200                   (rmail-count-new-messages t)
12201                   (rmail-show-message msg)))))))
12202     (kill-buffer tmpbuf)))
12203
12204 (defun gnus-output-to-file (file-name)
12205   "Append the current article to a file named FILE-NAME."
12206   (setq file-name (expand-file-name file-name))
12207   (let ((artbuf (current-buffer))
12208         (tmpbuf (get-buffer-create " *Gnus-output*")))
12209     (save-excursion
12210       (set-buffer tmpbuf)
12211       (buffer-disable-undo (current-buffer))
12212       (erase-buffer)
12213       (insert-buffer-substring artbuf)
12214       ;; Append newline at end of the buffer as separator, and then
12215       ;; save it to file.
12216       (goto-char (point-max))
12217       (insert "\n")
12218       (append-to-file (point-min) (point-max) file-name))
12219     (kill-buffer tmpbuf)))
12220
12221 (defun gnus-convert-article-to-rmail ()
12222   "Convert article in current buffer to Rmail message format."
12223   (let ((buffer-read-only nil))
12224     ;; Convert article directly into Babyl format.
12225     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
12226     (goto-char (point-min))
12227     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
12228     (while (search-forward "\n\^_" nil t) ;single char
12229       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
12230     (goto-char (point-max))
12231     (insert "\^_")))
12232
12233 (defun gnus-narrow-to-page (&optional arg)
12234   "Make text outside current page invisible except for page delimiter.
12235 A numeric arg specifies to move forward or backward by that many pages,
12236 thus showing a page other than the one point was originally in."
12237   (interactive "P")
12238   (setq arg (if arg (prefix-numeric-value arg) 0))
12239   (save-excursion
12240     (forward-page -1)                   ;Beginning of current page.
12241     (widen)
12242     (if (> arg 0)
12243         (forward-page arg)
12244       (if (< arg 0)
12245           (forward-page (1- arg))))
12246     ;; Find the end of the page.
12247     (forward-page)
12248     ;; If we stopped due to end of buffer, stay there.
12249     ;; If we stopped after a page delimiter, put end of restriction
12250     ;; at the beginning of that line.
12251     ;; These are commented out.
12252     ;;    (if (save-excursion (beginning-of-line)
12253     ;;                  (looking-at page-delimiter))
12254     ;;  (beginning-of-line))
12255     (narrow-to-region (point)
12256                       (progn
12257                         ;; Find the top of the page.
12258                         (forward-page -1)
12259                         ;; If we found beginning of buffer, stay there.
12260                         ;; If extra text follows page delimiter on same line,
12261                         ;; include it.
12262                         ;; Otherwise, show text starting with following line.
12263                         (if (and (eolp) (not (bobp)))
12264                             (forward-line 1))
12265                         (point)))))
12266
12267 (defun gnus-gmt-to-local ()
12268   "Rewrite Date header described in GMT to local in current buffer.
12269 Intended to be used with gnus-article-prepare-hook."
12270   (save-excursion
12271     (save-restriction
12272       (widen)
12273       (goto-char (point-min))
12274       (narrow-to-region (point-min)
12275                         (progn (search-forward "\n\n" nil 'move) (point)))
12276       (goto-char (point-min))
12277       (if (re-search-forward "^Date:[ \t]\\(.*\\)$" nil t)
12278           (let ((buffer-read-only nil)
12279                 (date (buffer-substring-no-properties
12280                        (match-beginning 1) (match-end 1))))
12281             (delete-region (match-beginning 1) (match-end 1))
12282             (insert
12283              (timezone-make-date-arpa-standard 
12284               date nil (current-time-zone))))))))
12285
12286 ;; Article mode commands
12287
12288 (defun gnus-article-next-page (&optional lines)
12289   "Show next page of current article.
12290 If end of article, return non-nil. Otherwise return nil.
12291 Argument LINES specifies lines to be scrolled up."
12292   (interactive "P")
12293   (move-to-window-line -1)
12294   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
12295   (if (save-excursion
12296         (end-of-line)
12297         (and (pos-visible-in-window-p)  ;Not continuation line.
12298              (eobp)))
12299       ;; Nothing in this page.
12300       (if (or (not gnus-break-pages)
12301               (save-excursion
12302                 (save-restriction
12303                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
12304           t                             ;Nothing more.
12305         (gnus-narrow-to-page 1)         ;Go to next page.
12306         nil)
12307     ;; More in this page.
12308     (condition-case ()
12309         (scroll-up lines)
12310       (end-of-buffer
12311        ;; Long lines may cause an end-of-buffer error.
12312        (goto-char (point-max))))
12313     nil))
12314
12315 (defun gnus-article-prev-page (&optional lines)
12316   "Show previous page of current article.
12317 Argument LINES specifies lines to be scrolled down."
12318   (interactive "P")
12319   (move-to-window-line 0)
12320   (if (and gnus-break-pages
12321            (bobp)
12322            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
12323       (progn
12324         (gnus-narrow-to-page -1)        ;Go to previous page.
12325         (goto-char (point-max))
12326         (recenter -1))
12327     (scroll-down lines)))
12328
12329 (defun gnus-article-refer-article ()
12330   "Read article specified by message-id around point."
12331   (interactive)
12332   (search-forward ">" nil t)            ;Move point to end of "<....>".
12333   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
12334       (let ((message-id
12335              (buffer-substring (match-beginning 1) (match-end 1))))
12336         (set-buffer gnus-summary-buffer)
12337         (gnus-summary-refer-article message-id))
12338     (error "No references around point")))
12339
12340 (defun gnus-article-show-summary ()
12341   "Reconfigure windows to show summary buffer."
12342   (interactive)
12343   (gnus-configure-windows 'article)
12344   (gnus-summary-goto-subject gnus-current-article))
12345
12346 (defun gnus-article-describe-briefly ()
12347   "Describe article mode commands briefly."
12348   (interactive)
12349   (gnus-message 6
12350                 (substitute-command-keys "\\<gnus-article-mode-map>\\[gnus-article-next-page]:Next page  \\[gnus-article-prev-page]:Prev page  \\[gnus-article-show-summary]:Show summary  \\[gnus-info-find-node]:Run Info  \\[gnus-article-describe-briefly]:This help")))
12351
12352 (defun gnus-article-summary-command ()
12353   "Execute the last keystroke in the summary buffer."
12354   (interactive)
12355   (let ((obuf (current-buffer))
12356         (owin (current-window-configuration))
12357         func)
12358     (switch-to-buffer gnus-summary-buffer 'norecord)
12359     (setq func (lookup-key (current-local-map) (this-command-keys)))
12360     (call-interactively func)
12361     (set-buffer obuf)
12362     (set-window-configuration owin)
12363     (set-window-point (get-buffer-window (current-buffer)) (point))))
12364
12365 (defun gnus-article-summary-command-nosave ()
12366   "Execute the last keystroke in the summary buffer."
12367   (interactive)
12368   (let (func)
12369     (pop-to-buffer gnus-summary-buffer 'norecord)
12370     (setq func (lookup-key (current-local-map) (this-command-keys)))
12371     (call-interactively func)))
12372
12373 \f
12374 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
12375
12376 ;;;###autoload
12377 (defalias 'gnus-batch-kill 'gnus-batch-score)
12378 ;;;###autoload
12379 (defun gnus-batch-score ()
12380   "Run batched scoring.
12381 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
12382 Newsgroups is a list of strings in Bnews format.  If you want to score
12383 the comp hierarchy, you'd say \"comp.all\". If you would not like to
12384 score the alt hierarchy, you'd say \"!alt.all\"."
12385   (interactive)
12386   (let* ((yes-and-no
12387           (gnus-newsrc-parse-options
12388            (apply (function concat)
12389                   (mapcar (lambda (g) (concat g " "))
12390                           command-line-args-left))))
12391          (gnus-expert-user t)
12392          (nnmail-spool-file nil)
12393          (gnus-use-dribble-file nil)
12394          (yes (car yes-and-no))
12395          (no (cdr yes-and-no))
12396          group newsrc entry
12397          ;; Disable verbose message.
12398          gnus-novice-user gnus-large-newsgroup)
12399     ;; Eat all arguments.
12400     (setq command-line-args-left nil)
12401     ;; Start Gnus.
12402     (gnus)
12403     ;; Apply kills to specified newsgroups in command line arguments.
12404     (setq newsrc (cdr gnus-newsrc-alist))
12405     (while newsrc
12406       (setq group (car (car newsrc)))
12407       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
12408       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
12409                (and (car entry)
12410                     (or (eq (car entry) t)
12411                         (not (zerop (car entry)))))
12412                (if yes (string-match yes group) t)
12413                (or (null no) (not (string-match no group))))
12414           (progn
12415             (gnus-summary-read-group group nil t)
12416             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
12417                  (gnus-summary-exit))))
12418       (setq newsrc (cdr newsrc)))
12419     ;; Exit Emacs.
12420     (switch-to-buffer gnus-group-buffer)
12421     (gnus-group-save-newsrc)))
12422
12423 (defun gnus-apply-kill-file ()
12424   "Apply a kill file to the current newsgroup.
12425 Returns the number of articles marked as read."
12426   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
12427           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
12428       (gnus-apply-kill-file-internal)
12429     0))
12430
12431 (defun gnus-kill-save-kill-buffer ()
12432   (save-excursion
12433     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
12434       (if (get-file-buffer file)
12435           (progn
12436             (set-buffer (get-file-buffer file))
12437             (and (buffer-modified-p) (save-buffer))
12438             (kill-buffer (current-buffer)))))))
12439
12440 (defvar gnus-kill-file-name "KILL"
12441   "Suffix of the kill files.")
12442
12443 (defun gnus-newsgroup-kill-file (newsgroup)
12444   "Return the name of a kill file name for NEWSGROUP.
12445 If NEWSGROUP is nil, return the global kill file name instead."
12446   (cond ((or (null newsgroup)
12447              (string-equal newsgroup ""))
12448          ;; The global KILL file is placed at top of the directory.
12449          (expand-file-name gnus-kill-file-name
12450                            (or gnus-kill-files-directory "~/News")))
12451         ((gnus-use-long-file-name 'not-kill)
12452          ;; Append ".KILL" to newsgroup name.
12453          (expand-file-name (concat (gnus-newsgroup-saveable-name newsgroup)
12454                                    "." gnus-kill-file-name)
12455                            (or gnus-kill-files-directory "~/News")))
12456         (t
12457          ;; Place "KILL" under the hierarchical directory.
12458          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
12459                                    "/" gnus-kill-file-name)
12460                            (or gnus-kill-files-directory "~/News")))))
12461
12462 \f
12463 ;;;
12464 ;;; Dribble file
12465 ;;;
12466
12467 (defvar gnus-dribble-ignore nil)
12468 (defvar gnus-dribble-eval-file nil)
12469
12470 (defun gnus-dribble-file-name ()
12471   (concat 
12472    (if gnus-dribble-directory
12473        (concat (file-name-as-directory gnus-dribble-directory)
12474                (file-name-nondirectory gnus-current-startup-file))
12475      gnus-current-startup-file)
12476    "-dribble"))
12477
12478 (defun gnus-dribble-enter (string)
12479   (if (and (not gnus-dribble-ignore)
12480            (or gnus-dribble-buffer
12481                gnus-slave)
12482            (buffer-name gnus-dribble-buffer))
12483       (let ((obuf (current-buffer)))
12484         (set-buffer gnus-dribble-buffer)
12485         (insert string "\n")
12486         (set-window-point (get-buffer-window (current-buffer)) (point-max))
12487         (set-buffer obuf))))
12488
12489 (defun gnus-dribble-read-file ()
12490   (let ((dribble-file (gnus-dribble-file-name)))
12491     (save-excursion 
12492       (set-buffer (setq gnus-dribble-buffer 
12493                         (get-buffer-create 
12494                          (file-name-nondirectory dribble-file))))
12495       (gnus-add-current-to-buffer-list)
12496       (erase-buffer)
12497       (set-visited-file-name dribble-file)
12498       (buffer-disable-undo (current-buffer))
12499       (bury-buffer (current-buffer))
12500       (set-buffer-modified-p nil)
12501       (let ((auto (make-auto-save-file-name))
12502             (gnus-dribble-ignore t))
12503         (if (or (file-exists-p auto) (file-exists-p dribble-file))
12504             (progn
12505               (if (file-newer-than-file-p auto dribble-file)
12506                   (setq dribble-file auto))
12507               (insert-file-contents dribble-file)
12508               (if (not (zerop (buffer-size)))
12509                   (set-buffer-modified-p t))
12510               (if (gnus-y-or-n-p 
12511                    "Auto-save file exists. Do you want to read it? ")
12512                   (setq gnus-dribble-eval-file t))))))))
12513
12514 (defun gnus-dribble-eval-file ()
12515   (if (not gnus-dribble-eval-file)
12516       ()
12517     (setq gnus-dribble-eval-file nil)
12518     (save-excursion
12519       (let ((gnus-dribble-ignore t))
12520         (set-buffer gnus-dribble-buffer)
12521         (eval-buffer (current-buffer))))))
12522
12523 (defun gnus-dribble-delete-file ()
12524   (if (file-exists-p (gnus-dribble-file-name))
12525       (delete-file (gnus-dribble-file-name)))
12526   (if gnus-dribble-buffer
12527       (save-excursion
12528         (set-buffer gnus-dribble-buffer)
12529         (let ((auto (make-auto-save-file-name)))
12530           (if (file-exists-p auto)
12531               (delete-file auto))
12532           (erase-buffer)
12533           (set-buffer-modified-p nil)))))
12534
12535 (defun gnus-dribble-save ()
12536   (if (and gnus-dribble-buffer
12537            (buffer-name gnus-dribble-buffer))
12538       (save-excursion
12539         (set-buffer gnus-dribble-buffer)
12540         (save-buffer))))
12541
12542 (defun gnus-dribble-clear ()
12543   (save-excursion
12544     (if (gnus-buffer-exists-p gnus-dribble-buffer)
12545         (progn
12546           (set-buffer gnus-dribble-buffer)
12547           (erase-buffer)
12548           (set-buffer-modified-p nil)
12549           (setq buffer-saved-size (buffer-size))))))
12550
12551 ;;;
12552 ;;; Server Communication
12553 ;;;
12554
12555 (defun gnus-start-news-server (&optional confirm)
12556   "Open a method for getting news.
12557 If CONFIRM is non-nil, the user will be asked for an NNTP server."
12558   (let (how)
12559     (if gnus-current-select-method
12560         ;; Stream is already opened.
12561         nil
12562       ;; Open NNTP server.
12563       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
12564       (if confirm
12565           (progn
12566             ;; Read server name with completion.
12567             (setq gnus-nntp-server
12568                   (completing-read "NNTP server: "
12569                                    (mapcar (lambda (server) (list server))
12570                                            (cons (list gnus-nntp-server)
12571                                                  gnus-secondary-servers))
12572                                    nil nil gnus-nntp-server))))
12573
12574       (if (and gnus-nntp-server 
12575                (stringp gnus-nntp-server)
12576                (not (string= gnus-nntp-server "")))
12577           (setq gnus-select-method
12578                 (cond ((or (string= gnus-nntp-server "")
12579                            (string= gnus-nntp-server "::"))
12580                        (list 'nnspool (system-name)))
12581                       ((string-match "^:" gnus-nntp-server)
12582                        (list 'nnmh gnus-nntp-server 
12583                              (list 'nnmh-directory 
12584                                    (file-name-as-directory
12585                                     (expand-file-name
12586                                      (concat "~/" (substring
12587                                                    gnus-nntp-server 1)))))
12588                              (list 'nnmh-get-new-mail nil)))
12589                       (t
12590                        (list 'nntp gnus-nntp-server)))))
12591
12592       (setq how (car gnus-select-method))
12593       (cond ((eq how 'nnspool)
12594              (require 'nnspool)
12595              (gnus-message 5 "Looking up local news spool..."))
12596             ((eq how 'nnmh)
12597              (require 'nnmh)
12598              (gnus-message 5 "Looking up mh spool..."))
12599             (t
12600              (require 'nntp)))
12601       (setq gnus-current-select-method gnus-select-method)
12602       (run-hooks 'gnus-open-server-hook)
12603       (or 
12604        ;; gnus-open-server-hook might have opened it
12605        (gnus-server-opened gnus-select-method)  
12606        (gnus-open-server gnus-select-method)
12607        (gnus-y-or-n-p
12608         (format
12609          "%s open error: '%s'. Continue? "
12610          (nth 1 gnus-select-method)
12611          (gnus-status-message gnus-select-method)))
12612        (progn
12613          (gnus-message 1 "Couldn't open server on %s" 
12614                        (nth 1 gnus-select-method))
12615          (ding)
12616          nil)))))
12617
12618 (defun gnus-check-server (&optional method)
12619   "If the news server is down, start it up again."
12620   (let ((method (if method method gnus-select-method)))
12621     (and (stringp method)
12622          (setq method (gnus-server-to-method method)))
12623     (if (gnus-server-opened method)
12624         ;; Stream is already opened.
12625         t
12626       ;; Open server.
12627       (gnus-message 5 "Opening server %s on %s..." (car method) (nth 1 method))
12628       (run-hooks 'gnus-open-server-hook)
12629       (prog1
12630           (gnus-open-server method)
12631         (message "")))))
12632
12633 (defun gnus-nntp-message (&optional message)
12634   "Check the status of the NNTP server.
12635 If the status of the server is clear and MESSAGE is non-nil, MESSAGE
12636 is returned insted of the status string."
12637   (let ((status (gnus-status-message (gnus-find-method-for-group 
12638                                       gnus-newsgroup-name)))
12639         (message (or message "")))
12640     (if (and (stringp status) (> (length status) 0))
12641         status message)))
12642
12643 (defun gnus-get-function (method function)
12644   (and (stringp method)
12645        (setq method (gnus-server-to-method method)))
12646   (let ((func (intern (format "%s-%s" (car method) function))))
12647     (if (not (fboundp func)) 
12648         (progn
12649           (require (car method))
12650           (if (not (fboundp func)) 
12651               (error "No such function: %s" func))))
12652     func))
12653
12654 ;;; Interface functions to the backends.
12655
12656 (defun gnus-open-server (method)
12657   (let ((elem (assoc method gnus-opened-servers)))
12658     ;; If this method was previously denied, we just return nil.
12659     (if (eq (cdr elem) 'denied)
12660         nil
12661       ;; Open the server.
12662       (let ((result
12663              (funcall (gnus-get-function method 'open-server)
12664                       (nth 1 method) (nthcdr 2 method))))
12665         ;; If this hasn't been opened before, we add it to the list.
12666         (or elem 
12667             (setq elem (list method nil)
12668                   gnus-opened-servers (cons elem gnus-opened-servers)))
12669         ;; Set the status of this server.
12670         (setcar (cdr elem) (if result 'ok 'denied))
12671         ;; Return the result from the "open" call.
12672         result))))
12673
12674 (defun gnus-close-server (method)
12675   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
12676
12677 (defun gnus-request-list (method)
12678   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
12679
12680 (defun gnus-request-list-newsgroups (method)
12681   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
12682
12683 (defun gnus-request-newgroups (date method)
12684   (funcall (gnus-get-function method 'request-newgroups) 
12685            date (nth 1 method)))
12686
12687 (defun gnus-server-opened (method)
12688   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
12689
12690 (defun gnus-status-message (method)
12691   (let ((method (if (stringp method) (gnus-find-method-for-group method)
12692                   method)))
12693     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
12694
12695 (defun gnus-request-group (group &optional dont-check)
12696   (let ((method (gnus-find-method-for-group group)))
12697     (funcall (gnus-get-function method 'request-group) 
12698              (gnus-group-real-name group) (nth 1 method) dont-check)))
12699
12700 (defun gnus-request-asynchronous (group &optional articles)
12701   (let ((method (gnus-find-method-for-group group)))
12702     (funcall (gnus-get-function method 'request-asynchronous) 
12703              (gnus-group-real-name group) (nth 1 method) articles)))
12704
12705 (defun gnus-list-active-group (group)
12706   (let ((method (gnus-find-method-for-group group))
12707         (func 'list-active-group))
12708     (and (gnus-check-backend-function func group)
12709          (funcall (gnus-get-function method func) 
12710                   (gnus-group-real-name group) (nth 1 method)))))
12711
12712 (defun gnus-request-group-description (group)
12713   (let ((method (gnus-find-method-for-group group))
12714         (func 'request-group-description))
12715     (and (gnus-check-backend-function func group)
12716          (funcall (gnus-get-function method func) 
12717                   (gnus-group-real-name group) (nth 1 method)))))
12718
12719 (defun gnus-close-group (group)
12720   (let ((method (gnus-find-method-for-group group)))
12721     (funcall (gnus-get-function method 'close-group) 
12722              (gnus-group-real-name group) (nth 1 method))))
12723
12724 (defun gnus-retrieve-headers (articles group &optional fetch-old)
12725   (let ((method (gnus-find-method-for-group group)))
12726     (if (and gnus-use-cache (numberp (car articles)))
12727         (gnus-cache-retrieve-headers articles group)
12728       (funcall (gnus-get-function method 'retrieve-headers) 
12729                articles (gnus-group-real-name group) (nth 1 method)
12730                fetch-old))))
12731
12732 (defun gnus-retrieve-groups (groups method)
12733   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
12734
12735 (defun gnus-request-article (article group &optional buffer)
12736   (let ((method (gnus-find-method-for-group group)))
12737     (funcall (gnus-get-function method 'request-article) 
12738              article (gnus-group-real-name group) (nth 1 method) buffer)))
12739
12740 (defun gnus-request-head (article group)
12741   (let ((method (gnus-find-method-for-group group)))
12742     (funcall (gnus-get-function method 'request-head) 
12743              article (gnus-group-real-name group) (nth 1 method))))
12744
12745 (defun gnus-request-body (article group)
12746   (let ((method (gnus-find-method-for-group group)))
12747     (funcall (gnus-get-function method 'request-body) 
12748              article (gnus-group-real-name group) (nth 1 method))))
12749
12750 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
12751 (defun gnus-request-post-buffer (post group subject header artbuf
12752                                       info follow-to respect-poster)
12753   (let* ((info (or info (and group (nth 2 (gnus-gethash 
12754                                            group gnus-newsrc-hashtb)))))
12755          (method
12756           (if (and gnus-post-method
12757                    ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
12758                    (memq 'post (assoc
12759                                 (format "%s" (car (gnus-find-method-for-group
12760                                                    gnus-newsgroup-name)))
12761                                 gnus-valid-select-methods)))
12762               gnus-post-method
12763             (gnus-find-method-for-group gnus-newsgroup-name))))
12764     (or (gnus-check-server method)
12765         (error "Can't open server %s:%s" (car method) (nth 1 method)))
12766     (let ((mail-self-blind nil)
12767           (mail-archive-file-name nil))
12768       (funcall (gnus-get-function method 'request-post-buffer) 
12769                post group subject header artbuf info follow-to
12770                respect-poster))))
12771
12772 (defun gnus-request-post (method &optional force)
12773   (and (stringp method)
12774        (setq method (gnus-server-to-method method)))
12775   (and (not force) gnus-post-method
12776        (memq 'post (assoc (format "%s" (car method))
12777                           gnus-valid-select-methods))
12778        (setq method gnus-post-method))
12779   (funcall (gnus-get-function method 'request-post) 
12780            (nth 1 method)))
12781
12782 (defun gnus-request-scan (group method)
12783   (let ((method (if group (gnus-find-method-for-group group) method)))
12784     (funcall (gnus-get-function method 'request-scan) 
12785              (and group (gnus-group-real-name group)) (nth 1 method))))
12786
12787 (defun gnus-request-update-info (info method)
12788   (funcall (gnus-get-function method 'request-update-info) 
12789            (gnus-group-real-name (car info)) info (nth 1 method)))
12790
12791 (defun gnus-request-expire-articles (articles group &optional force)
12792   (let ((method (gnus-find-method-for-group group)))
12793     (funcall (gnus-get-function method 'request-expire-articles) 
12794              articles (gnus-group-real-name group) (nth 1 method)
12795              force)))
12796
12797 (defun gnus-request-move-article 
12798   (article group server accept-function &optional last)
12799   (let ((method (gnus-find-method-for-group group)))
12800     (funcall (gnus-get-function method 'request-move-article) 
12801              article (gnus-group-real-name group) 
12802              (nth 1 method) accept-function last)))
12803
12804 (defun gnus-request-accept-article (group &optional last)
12805   (let ((func (if (symbolp group) group
12806                 (car (gnus-find-method-for-group group)))))
12807     (funcall (intern (format "%s-request-accept-article" func))
12808              (if (stringp group) (gnus-group-real-name group) group)
12809              last)))
12810
12811 (defun gnus-request-replace-article (article group buffer)
12812   (let ((func (car (gnus-find-method-for-group group))))
12813     (funcall (intern (format "%s-request-replace-article" func))
12814              article (gnus-group-real-name group) buffer)))
12815
12816 (defun gnus-request-create-group (group)
12817   (let ((method (gnus-find-method-for-group group)))
12818     (funcall (gnus-get-function method 'request-create-group) 
12819              (gnus-group-real-name group) (nth 1 method))))
12820
12821 (defun gnus-request-delete-group (group &optional force)
12822   (let ((method (gnus-find-method-for-group group)))
12823     (funcall (gnus-get-function method 'request-delete-group) 
12824              (gnus-group-real-name group) force (nth 1 method))))
12825
12826 (defun gnus-request-rename-group (group new-name)
12827   (let ((method (gnus-find-method-for-group group)))
12828     (funcall (gnus-get-function method 'request-rename-group) 
12829              (gnus-group-real-name group) 
12830              (gnus-group-real-name new-name) (nth 1 method))))
12831
12832 (defun gnus-member-of-valid (symbol group)
12833   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
12834   (memq symbol (assoc
12835                 (format "%s" (car (gnus-find-method-for-group group)))
12836                 gnus-valid-select-methods)))
12837
12838 (defun gnus-secondary-method-p (method)
12839   "Return whether METHOD is a secondary select method."
12840   (let ((methods gnus-secondary-select-methods)
12841         (gmethod (gnus-server-get-method nil method)))
12842     (while (and methods
12843                 (not (equal (gnus-server-get-method nil (car methods)) 
12844                             gmethod)))
12845       (setq methods (cdr methods)))
12846     methods))
12847
12848 (defun gnus-find-method-for-group (group &optional info)
12849   "Find the select method that GROUP uses."
12850   (or gnus-override-method
12851       (and (not group)
12852            gnus-select-method)
12853       (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
12854             method)
12855         (if (or (not info)
12856                 (not (setq method (nth 4 info))))
12857             (setq method gnus-select-method)
12858           (setq method
12859                 (cond ((stringp method)
12860                        (gnus-server-to-method method))
12861                       ((stringp (car method))
12862                        (gnus-server-extend-method group method))
12863                       (t
12864                        method))))
12865         (gnus-server-add-address method))))
12866
12867 (defun gnus-check-backend-function (func group)
12868   "Check whether GROUP supports function FUNC."
12869   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
12870                   group)))
12871     (fboundp (intern (format "%s-%s" method func)))))
12872
12873 (defun gnus-methods-using (feature)
12874   "Find all methods that have FEATURE."
12875   (let ((valids gnus-valid-select-methods)
12876         outs)
12877     (while valids
12878       (if (memq feature (car valids)) 
12879           (setq outs (cons (car valids) outs)))
12880       (setq valids (cdr valids)))
12881     outs))
12882
12883 ;;; 
12884 ;;; Active & Newsrc File Handling
12885 ;;;
12886
12887 ;; Newsrc related functions.
12888 ;; Gnus internal format of gnus-newsrc-alist:
12889 ;; (("alt.general" 3 (1 . 1))
12890 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
12891 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
12892 ;; The first item is the group name; the second is the subscription
12893 ;; level; the third is either a range of a list of ranges of read
12894 ;; articles, the optional fourth element is a list of marked articles,
12895 ;; the optional fifth element is the select method.
12896 ;;
12897 ;; Gnus internal format of gnus-newsrc-hashtb:
12898 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
12899 ;; This is the entry for "alt.misc". The first element is the number
12900 ;; of unread articles in "alt.misc". The cdr of this entry is the
12901 ;; element *before* "alt.misc" in gnus-newsrc-alist, which makes is
12902 ;; trivial to remove or add new elements into gnus-newsrc-alist
12903 ;; without scanning the entire list. So, to get the actual information
12904 ;; of "alt.misc", you'd say something like 
12905 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
12906 ;;
12907 ;; Gnus internal format of gnus-active-hashtb:
12908 ;; ((1 . 1))
12909 ;;  (5 . 10))
12910 ;;  (67 . 99)) ...)
12911 ;; The only element in each entry in this hash table is a range of
12912 ;; (possibly) available articles. (Articles in this range may have
12913 ;; been expired or canceled.)
12914 ;;
12915 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
12916 ;; ("alt.misc" "alt.test" "alt.general" ...)
12917
12918 (defun gnus-setup-news (&optional rawfile level)
12919   "Setup news information.
12920 If RAWFILE is non-nil, the .newsrc file will also be read.
12921 If LEVEL is non-nil, the news will be set up at level LEVEL."
12922   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
12923     ;; Clear some variables to re-initialize news information.
12924     (if init (setq gnus-newsrc-alist nil 
12925                    gnus-active-hashtb nil))
12926
12927     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
12928     (if init (gnus-read-newsrc-file rawfile))
12929
12930     ;; If we don't read the complete active file, we fill in the
12931     ;; hashtb here. 
12932     (if (or (null gnus-read-active-file)
12933             (eq gnus-read-active-file 'some))
12934         (gnus-update-active-hashtb-from-killed))
12935
12936     ;; Read the active file and create `gnus-active-hashtb'.
12937     ;; If `gnus-read-active-file' is nil, then we just create an empty
12938     ;; hash table. The partial filling out of the hash table will be
12939     ;; done in `gnus-get-unread-articles'.
12940     (and gnus-read-active-file 
12941          (not level)
12942          (gnus-read-active-file))
12943
12944     (or gnus-active-hashtb
12945         (setq gnus-active-hashtb (make-vector 4095 0)))
12946
12947     ;; Possibly eval the dribble file.
12948     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
12949
12950     (gnus-update-format-specifications)
12951
12952     ;; Find new newsgroups and treat them.
12953     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level)
12954              (gnus-server-opened gnus-select-method))
12955         (gnus-find-new-newsgroups))
12956
12957     ;; Find the number of unread articles in each non-dead group.
12958     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
12959       (gnus-get-unread-articles (or level (1+ gnus-level-subscribed))))
12960
12961     (if (and init gnus-check-bogus-newsgroups 
12962              gnus-read-active-file (not level)
12963              (gnus-server-opened gnus-select-method))
12964         (gnus-check-bogus-newsgroups))))
12965
12966 (defun gnus-find-new-newsgroups ()
12967   "Search for new newsgroups and add them.
12968 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
12969 The `-n' option line from .newsrc is respected."
12970   (interactive)
12971   (or (gnus-check-first-time-used)
12972       (if (or (consp gnus-check-new-newsgroups)
12973               (eq gnus-check-new-newsgroups 'ask-server))
12974           (gnus-ask-server-for-new-groups)
12975         (let ((groups 0)
12976               group new-newsgroups)
12977           (gnus-message 5 "Looking for new newsgroups...")
12978           (or gnus-have-read-active-file (gnus-read-active-file))
12979           (setq gnus-newsrc-last-checked-date (current-time-string))
12980           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
12981           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
12982           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
12983           (mapatoms
12984            (lambda (sym)
12985              (if (or (null (setq group (symbol-name sym)))
12986                      (null (symbol-value sym))
12987                      (gnus-gethash group gnus-killed-hashtb)
12988                      (gnus-gethash group gnus-newsrc-hashtb))
12989                  ()
12990                (let ((do-sub (gnus-matches-options-n group)))
12991                  (cond 
12992                   ((eq do-sub 'subscribe)
12993                    (setq groups (1+ groups))
12994                    (gnus-sethash group group gnus-killed-hashtb)
12995                    (funcall gnus-subscribe-options-newsgroup-method group))
12996                   ((eq do-sub 'ignore)
12997                    nil)
12998                   (t
12999                    (setq groups (1+ groups))
13000                    (gnus-sethash group group gnus-killed-hashtb)
13001                    (if gnus-subscribe-hierarchical-interactive
13002                        (setq new-newsgroups (cons group new-newsgroups))
13003                      (funcall gnus-subscribe-newsgroup-method group)))))))
13004            gnus-active-hashtb)
13005           (if new-newsgroups 
13006               (gnus-subscribe-hierarchical-interactive new-newsgroups))
13007           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
13008           (if (> groups 0)
13009               (gnus-message 6 "%d new newsgroup%s arrived." 
13010                             groups (if (> groups 1) "s have" " has"))
13011             (gnus-message 6 "No new newsgroups."))))))
13012
13013 (defun gnus-matches-options-n (group)
13014   ;; Returns `subscribe' if the group is to be uncoditionally
13015   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
13016   ;; no match for the group.
13017
13018   ;; First we check the two user variables.
13019   (cond
13020    ((and gnus-options-subscribe
13021          (string-match gnus-options-subscribe group))
13022     'subscribe)
13023    ((and gnus-auto-subscribed-groups 
13024          (string-match gnus-auto-subscribed-groups group))
13025     'subscribe)
13026    ((and gnus-options-not-subscribe
13027          (string-match gnus-options-not-subscribe group))
13028     'ignore)
13029    ;; Then we go through the list that was retrieved from the .newsrc
13030    ;; file.  This list has elements on the form 
13031    ;; `(REGEXP . {ignore,subscribe})'. The first match found (the list
13032    ;; is in the reverse order of the options line) is returned.
13033    (t
13034     (let ((regs gnus-newsrc-options-n))
13035       (while (and regs
13036                   (not (string-match (car (car regs)) group)))
13037         (setq regs (cdr regs)))
13038       (and regs (cdr (car regs)))))))
13039
13040 (defun gnus-ask-server-for-new-groups ()
13041   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
13042          (methods (cons gnus-select-method 
13043                         (append
13044                          (and (consp gnus-check-new-newsgroups)
13045                               gnus-check-new-newsgroups)
13046                          gnus-secondary-select-methods)))
13047          (groups 0)
13048          (new-date (current-time-string))
13049          (hashtb (gnus-make-hashtable 100))
13050          group new-newsgroups got-new method)
13051     ;; Go through both primary and secondary select methods and
13052     ;; request new newsgroups.  
13053     (while methods
13054       (setq method (gnus-server-get-method nil (car methods)))
13055       (and (gnus-check-server method)
13056            (gnus-request-newgroups date method)
13057            (save-excursion
13058              (setq got-new t)
13059              (set-buffer nntp-server-buffer)
13060              ;; Enter all the new groups in a hashtable.
13061              (gnus-active-to-gnus-format method hashtb 'ignore)))
13062       (setq methods (cdr methods)))
13063     (and got-new (setq gnus-newsrc-last-checked-date new-date))
13064     ;; Now all new groups from all select methods are in `hashtb'.
13065     (mapatoms
13066      (lambda (group-sym)
13067        (setq group (symbol-name group-sym))
13068        (if (or (null group)
13069                (null (symbol-value group-sym))
13070                (gnus-gethash group gnus-newsrc-hashtb)
13071                (member group gnus-zombie-list)
13072                (member group gnus-killed-list))
13073            ;; The group is already known.
13074            ()
13075          (and (symbol-value group-sym)
13076               (gnus-sethash group (symbol-value group-sym) gnus-active-hashtb))
13077          (let ((do-sub (gnus-matches-options-n group)))
13078            (cond ((eq do-sub 'subscribe)
13079                   (setq groups (1+ groups))
13080                   (gnus-sethash group group gnus-killed-hashtb)
13081                   (funcall 
13082                    gnus-subscribe-options-newsgroup-method group))
13083                  ((eq do-sub 'ignore)
13084                   nil)
13085                  (t
13086                   (setq groups (1+ groups))
13087                   (gnus-sethash group group gnus-killed-hashtb)
13088                   (if gnus-subscribe-hierarchical-interactive
13089                       (setq new-newsgroups (cons group new-newsgroups))
13090                     (funcall gnus-subscribe-newsgroup-method group)))))))
13091      hashtb)
13092     (if new-newsgroups 
13093         (gnus-subscribe-hierarchical-interactive new-newsgroups))
13094     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
13095     (if (> groups 0)
13096         (gnus-message 6 "%d new newsgroup%s arrived." 
13097                       groups (if (> groups 1) "s have" " has")))
13098     got-new))
13099
13100 (defun gnus-check-first-time-used ()
13101   (if (or (> (length gnus-newsrc-alist) 1)
13102           (file-exists-p gnus-startup-file)
13103           (file-exists-p (concat gnus-startup-file ".el"))
13104           (file-exists-p (concat gnus-startup-file ".eld")))
13105       nil
13106     (gnus-message 6 "First time user; subscribing you to default groups")
13107     (or gnus-have-read-active-file (gnus-read-active-file))
13108     (setq gnus-newsrc-last-checked-date (current-time-string))
13109     (let ((groups gnus-default-subscribed-newsgroups)
13110           group)
13111       (if (eq groups t)
13112           nil
13113         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
13114         (mapatoms
13115          (lambda (sym)
13116            (if (null (setq group (symbol-name sym)))
13117                ()
13118              (let ((do-sub (gnus-matches-options-n group)))
13119                (cond 
13120                 ((eq do-sub 'subscribe)
13121                  (gnus-sethash group group gnus-killed-hashtb)
13122                  (funcall gnus-subscribe-options-newsgroup-method group))
13123                 ((eq do-sub 'ignore)
13124                  nil)
13125                 (t
13126                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
13127          gnus-active-hashtb)
13128         (while groups
13129           (if (gnus-gethash (car groups) gnus-active-hashtb)
13130               (gnus-group-change-level 
13131                (car groups) gnus-level-default-subscribed gnus-level-killed))
13132           (setq groups (cdr groups)))
13133         (gnus-group-make-help-group)
13134         (and gnus-novice-user
13135              (gnus-message 7 "`A k' to list killed groups"))))))
13136
13137 (defun gnus-subscribe-group (group previous &optional method)
13138   (gnus-group-change-level 
13139    (if method
13140        (list t group gnus-level-default-subscribed nil nil method)
13141      group) 
13142    gnus-level-default-subscribed gnus-level-killed previous t))
13143
13144 ;; `gnus-group-change-level' is the fundamental function for changing
13145 ;; subscription levels of newsgroups. This might mean just changing
13146 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
13147 ;; again, which subscribes/unsubscribes a group, which is equally
13148 ;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
13149 ;; from 8-9 to 1-7 means that you remove the group from the list of
13150 ;; killed (or zombie) groups and add them to the (kinda) subscribed
13151 ;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
13152 ;; which is trivial.
13153 ;; ENTRY can either be a string (newsgroup name) or a list (if
13154 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
13155 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
13156 ;; entries. 
13157 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
13158 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
13159 ;; after. 
13160 (defun gnus-group-change-level (entry level &optional oldlevel
13161                                       previous fromkilled)
13162   (let (group info active num)
13163     ;; Glean what info we can from the arguments
13164     (if (consp entry)
13165         (if fromkilled (setq group (nth 1 entry))
13166           (setq group (car (nth 2 entry))))
13167       (setq group entry))
13168     (if (and (stringp entry)
13169              oldlevel 
13170              (< oldlevel gnus-level-zombie))
13171         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
13172     (if (and (not oldlevel)
13173              (consp entry))
13174         (setq oldlevel (car (cdr (nth 2 entry)))))
13175     (if (stringp previous)
13176         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
13177
13178     (if (and (>= oldlevel gnus-level-zombie)
13179              (gnus-gethash group gnus-newsrc-hashtb))
13180         ;; We are trying to subscribe a group that is already
13181         ;; subscribed. 
13182         ()                              ; Do nothing. 
13183
13184       (gnus-dribble-enter
13185        (format "(gnus-group-change-level %S %S %S %S %S)" 
13186                group level oldlevel (car (nth 2 previous)) fromkilled))
13187     
13188       ;; Then we remove the newgroup from any old structures, if needed.
13189       ;; If the group was killed, we remove it from the killed or zombie
13190       ;; list. If not, and it is in fact going to be killed, we remove
13191       ;; it from the newsrc hash table and assoc.
13192       (cond ((>= oldlevel gnus-level-zombie)
13193              (if (= oldlevel gnus-level-zombie)
13194                  (setq gnus-zombie-list (delete group gnus-zombie-list))
13195                (setq gnus-killed-list (delete group gnus-killed-list))))
13196             (t
13197              (if (and (>= level gnus-level-zombie)
13198                       entry)
13199                  (progn
13200                    (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
13201                    (if (nth 3 entry)
13202                        (setcdr (gnus-gethash (car (nth 3 entry))
13203                                              gnus-newsrc-hashtb)
13204                                (cdr entry)))
13205                    (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
13206
13207       ;; Finally we enter (if needed) the list where it is supposed to
13208       ;; go, and change the subscription level. If it is to be killed,
13209       ;; we enter it into the killed or zombie list.
13210       (cond ((>= level gnus-level-zombie)
13211              ;; Remove from the hash table.
13212              (gnus-sethash group nil gnus-newsrc-hashtb)
13213              (or (gnus-group-foreign-p group)
13214                  ;; We do not enter foreign groups into the list of dead
13215                  ;; groups.  
13216                  (if (= level gnus-level-zombie)
13217                      (setq gnus-zombie-list (cons group gnus-zombie-list))
13218                    (setq gnus-killed-list (cons group gnus-killed-list)))))
13219             (t
13220              ;; If the list is to be entered into the newsrc assoc, and
13221              ;; it was killed, we have to create an entry in the newsrc
13222              ;; hashtb format and fix the pointers in the newsrc assoc.
13223              (if (>= oldlevel gnus-level-zombie)
13224                  (progn
13225                    (if (listp entry)
13226                        (progn
13227                          (setq info (cdr entry))
13228                          (setq num (car entry)))
13229                      (setq active (gnus-gethash group gnus-active-hashtb))
13230                      (setq num 
13231                            (if active (- (1+ (cdr active)) (car active)) t))
13232                      ;; Check whether the group is foreign. If so, the
13233                      ;; foreign select method has to be entered into the
13234                      ;; info. 
13235                      (let ((method (gnus-group-method-name group)))
13236                        (if (eq method gnus-select-method)
13237                            (setq info (list group level nil))
13238                          (setq info (list group level nil nil method)))))
13239                    (or previous 
13240                        (setq previous 
13241                              (let ((p gnus-newsrc-alist))
13242                                (while (cdr (cdr p))
13243                                  (setq p (cdr p)))
13244                                p)))
13245                    (setq entry (cons info (cdr (cdr previous))))
13246                    (if (cdr previous)
13247                        (progn
13248                          (setcdr (cdr previous) entry)
13249                          (gnus-sethash group (cons num (cdr previous)) 
13250                                        gnus-newsrc-hashtb))
13251                      (setcdr previous entry)
13252                      (gnus-sethash group (cons num previous)
13253                                    gnus-newsrc-hashtb))
13254                    (if (cdr entry)
13255                        (setcdr (gnus-gethash (car (car (cdr entry)))
13256                                              gnus-newsrc-hashtb)
13257                                entry)))
13258                ;; It was alive, and it is going to stay alive, so we
13259                ;; just change the level and don't change any pointers or
13260                ;; hash table entries.
13261                (setcar (cdr (car (cdr (cdr entry)))) level)))))))
13262
13263 (defun gnus-kill-newsgroup (newsgroup)
13264   "Obsolete function. Kills a newsgroup."
13265   (gnus-group-change-level
13266    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
13267
13268 (defun gnus-check-bogus-newsgroups (&optional confirm)
13269   "Remove bogus newsgroups.
13270 If CONFIRM is non-nil, the user has to confirm the deletion of every
13271 newsgroup." 
13272   (let ((newsrc (cdr gnus-newsrc-alist))
13273         bogus group entry)
13274     (gnus-message 5 "Checking bogus newsgroups...")
13275     (or gnus-have-read-active-file (gnus-read-active-file))
13276     ;; Find all bogus newsgroup that are subscribed.
13277     (while newsrc
13278       (setq group (car (car newsrc)))
13279       (if (or (gnus-gethash group gnus-active-hashtb) ; Active
13280               (nth 4 (car newsrc))      ; Foreign
13281               (and confirm
13282                    (not (gnus-y-or-n-p
13283                          (format "Remove bogus newsgroup: %s " group)))))
13284           ;; Don't remove.
13285           ()
13286         ;; Found a bogus newsgroup.
13287         (setq bogus (cons group bogus)))
13288       (setq newsrc (cdr newsrc)))
13289     ;; Remove all bogus subscribed groups by first killing them, and
13290     ;; then removing them from the list of killed groups.
13291     (while bogus
13292       (and (setq entry (gnus-gethash (car bogus) gnus-newsrc-hashtb))
13293            (progn
13294              (gnus-group-change-level entry gnus-level-killed)
13295              (setq gnus-killed-list (delete (car bogus) gnus-killed-list))))
13296       (setq bogus (cdr bogus)))
13297     ;; Then we remove all bogus groups from the list of killed and
13298     ;; zombie groups. They are are removed without confirmation.
13299     (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
13300           killed)
13301       (while dead-lists
13302         (setq killed (symbol-value (car dead-lists)))
13303         (while killed
13304           (setq group (car killed))
13305           (or (gnus-gethash group gnus-active-hashtb)
13306               ;; The group is bogus.
13307               (set (car dead-lists)
13308                    (delete group (symbol-value (car dead-lists)))))
13309           (setq killed (cdr killed)))
13310         (setq dead-lists (cdr dead-lists))))
13311     (gnus-message 5 "Checking bogus newsgroups...done")))
13312
13313 (defun gnus-check-duplicate-killed-groups ()
13314   "Remove duplicates from the list of killed groups."
13315   (interactive)
13316   (let ((killed gnus-killed-list))
13317     (while killed
13318       (gnus-message 9 "%d" (length killed))
13319       (setcdr killed (delete (car killed) (cdr killed)))
13320       (setq killed (cdr killed)))))
13321
13322 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
13323 ;; and compute how many unread articles there are in each group.
13324 (defun gnus-get-unread-articles (&optional level) 
13325   (let* ((newsrc (cdr gnus-newsrc-alist))
13326          (level (or level (1+ gnus-level-subscribed)))
13327          (foreign-level
13328           (min 
13329            (cond ((and gnus-activate-foreign-newsgroups 
13330                        (not (numberp gnus-activate-foreign-newsgroups)))
13331                   (1+ gnus-level-subscribed))
13332                  ((numberp gnus-activate-foreign-newsgroups)
13333                   gnus-activate-foreign-newsgroups)
13334                  (t 0))
13335            level))
13336          (update
13337           (fboundp (intern (format "%s-request-update-info"
13338                                    (car gnus-select-method)))))
13339          info group active virtuals method fmethod)
13340     (gnus-message 5 "Checking new news...")
13341
13342     (while newsrc
13343       (setq info (car newsrc)
13344             group (car info)
13345             active (gnus-gethash group gnus-active-hashtb))
13346
13347       ;; Check newsgroups. If the user doesn't want to check them, or
13348       ;; they can't be checked (for instance, if the news server can't
13349       ;; be reached) we just set the number of unread articles in this
13350       ;; newsgroup to t. This means that Gnus thinks that there are
13351       ;; unread articles, but it has no idea how many.
13352       (if (and (setq method (nth 4 info))
13353                (not (gnus-server-equal
13354                      gnus-select-method
13355                      (prog1
13356                          (setq fmethod (gnus-server-get-method nil method))
13357                        ;; We do this here because it would be awkward
13358                        ;; to do it anywhere else.  Hell, it's pretty
13359                        ;; awkward here as well, but at least it's
13360                        ;; reasonable efficient. 
13361                        (and (fboundp (intern (format "%s-request-update-info"
13362                                                      (car fmethod))))
13363                             (<= (nth 1 info) foreign-level)
13364                             (gnus-request-update-info info method)))))
13365                (not (gnus-secondary-method-p method)))
13366           ;; These groups are foreign. Check the level.
13367           (if (<= (nth 1 info) foreign-level)
13368               (setq active (gnus-activate-group (car info) 'scan)))
13369
13370         ;; These groups are native or secondary. 
13371         (if (<= (nth 1 info) level)
13372             (progn
13373               (if (and update (not method))
13374                   (progn
13375                     ;; Allow updating of native groups as well, even
13376                     ;; though that's pretty unlikely.
13377                     (gnus-request-update-info info gnus-select-method)
13378                     (setq active (gnus-activate-group (car info) 'scan)))
13379                 (or gnus-read-active-file
13380                     (setq active (gnus-activate-group (car info) 'scan)))))))
13381       
13382       (if active
13383           (gnus-get-unread-articles-in-group info active)
13384         ;; The group couldn't be reached, so we nix out the number of
13385         ;; unread articles and stuff.
13386         (gnus-sethash group nil gnus-active-hashtb)
13387         (setcar (gnus-gethash group gnus-newsrc-hashtb) t))
13388
13389       (setq newsrc (cdr newsrc)))
13390
13391     (gnus-message 5 "Checking new news...done")))
13392
13393 ;; Create a hash table out of the newsrc alist. The `car's of the
13394 ;; alist elements are used as keys.
13395 (defun gnus-make-hashtable-from-newsrc-alist ()
13396   (let ((alist gnus-newsrc-alist)
13397         (ohashtb gnus-newsrc-hashtb)
13398         prev)
13399     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
13400     (setq alist 
13401           (setq prev (setq gnus-newsrc-alist 
13402                            (if (equal (car (car gnus-newsrc-alist))
13403                                       "dummy.group")
13404                                gnus-newsrc-alist
13405                              (cons (list "dummy.group" 0 nil) alist)))))
13406     (while alist
13407       (gnus-sethash (car (car alist)) 
13408                     (cons (and ohashtb (car (gnus-gethash 
13409                                              (car (car alist)) ohashtb))) 
13410                           prev) gnus-newsrc-hashtb)
13411       (setq prev alist
13412             alist (cdr alist)))))
13413
13414 (defun gnus-make-hashtable-from-killed ()
13415   "Create a hash table from the killed and zombie lists."
13416   (let ((lists '(gnus-killed-list gnus-zombie-list))
13417         list)
13418     (setq gnus-killed-hashtb 
13419           (gnus-make-hashtable 
13420            (+ (length gnus-killed-list) (length gnus-zombie-list))))
13421     (while lists
13422       (setq list (symbol-value (car lists)))
13423       (setq lists (cdr lists))
13424       (while list
13425         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
13426         (setq list (cdr list))))))
13427
13428 (defun gnus-get-unread-articles-in-group (info active)
13429   (let* ((range (nth 2 info))
13430          (num 0)
13431          (marked (nth 3 info)))
13432     ;; If a cache is present, we may have to alter the active info.
13433     (and gnus-use-cache
13434          (gnus-cache-possibly-alter-active (car info) active))
13435     ;; Modify the list of read articles according to what articles 
13436     ;; are available; then tally the unread articles and add the
13437     ;; number to the group hash table entry.
13438     (cond 
13439      ((zerop (cdr active))
13440       (setq num 0))
13441      ((not range)
13442       (setq num (- (1+ (cdr active)) (car active))))
13443      ((not (listp (cdr range)))
13444       ;; Fix a single (num . num) range according to the
13445       ;; active hash table.
13446       ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
13447       (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
13448       (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
13449       ;; Compute number of unread articles.
13450       (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
13451      (t
13452       ;; The read list is a list of ranges. Fix them according to
13453       ;; the active hash table.
13454       ;; First peel off any elements that are below the lower
13455       ;; active limit. 
13456       (while (and (cdr range) 
13457                   (>= (car active) 
13458                       (or (and (atom (car (cdr range))) (car (cdr range)))
13459                           (car (car (cdr range))))))
13460         (if (numberp (car range))
13461             (setcar range 
13462                     (cons (car range) 
13463                           (or (and (numberp (car (cdr range)))
13464                                    (car (cdr range))) 
13465                               (cdr (car (cdr range))))))
13466           (setcdr (car range) 
13467                   (or (and (numberp (nth 1 range)) (nth 1 range))
13468                       (cdr (car (cdr range))))))
13469         (setcdr range (cdr (cdr range))))
13470       ;; Adjust the first element to be the same as the lower limit. 
13471       (if (and (not (atom (car range))) 
13472                (< (cdr (car range)) (car active)))
13473           (setcdr (car range) (1- (car active))))
13474       ;; Then we want to peel off any elements that are higher
13475       ;; than the upper active limit.  
13476       (let ((srange range))
13477         ;; Go past all legal elements.
13478         (while (and (cdr srange) 
13479                     (<= (or (and (atom (car (cdr srange)))
13480                                  (car (cdr srange)))
13481                             (car (car (cdr srange)))) (cdr active)))
13482           (setq srange (cdr srange)))
13483         (if (cdr srange)
13484             ;; Nuke all remaining illegal elements.
13485             (setcdr srange nil))
13486
13487         ;; Adjust the final element.
13488         (if (and (not (atom (car srange)))
13489                  (> (cdr (car srange)) (cdr active)))
13490             (setcdr (car srange) (cdr active))))
13491       ;; Compute the number of unread articles.
13492       (while range
13493         (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
13494                                     (cdr (car range))))
13495                             (or (and (atom (car range)) (car range))
13496                                 (car (car range))))))
13497         (setq range (cdr range)))
13498       (setq num (max 0 (- (cdr active) num)))))
13499     (and info
13500          (progn
13501            (and (assq 'tick marked)
13502                 (inline (gnus-remove-illegal-marked-articles
13503                          (assq 'tick marked) (nth 2 info))))
13504            (and (assq 'dormant marked)
13505                 (inline (gnus-remove-illegal-marked-articles
13506                          (assq 'dormant marked) (nth 2 info))))
13507            (setcar
13508             (gnus-gethash (car info) gnus-newsrc-hashtb) 
13509             (setq num (max 0 (- num (length (cdr (assq 'tick marked)))
13510                                 (length (cdr (assq 'dormant marked)))))))))
13511     num))
13512
13513 (defun gnus-remove-illegal-marked-articles (marked ranges)
13514   (let ((m (cdr marked)))
13515     ;; Make sure that all ticked articles are a subset of the unread
13516     ;; articles. 
13517     (while m
13518       (if (gnus-member-of-range (car m) ranges)
13519           (setcdr marked (cdr m))
13520         (setq marked m))
13521       (setq m (cdr m)))))
13522
13523 (defun gnus-activate-group (group &optional scan)
13524   ;; Check whether a group has been activated or not.
13525   ;; If SCAN, request a scan of that group as well.
13526   (let ((method (gnus-find-method-for-group group))
13527         active)
13528     (and (gnus-check-server method)
13529          ;; We escape all bugs and quit here to make it possible to
13530          ;; continue if a group is so out-there that it reports bugs
13531          ;; and stuff.
13532          (progn
13533            (and scan
13534                 (gnus-check-backend-function 'request-scan (car method))
13535                 (gnus-request-scan group method))
13536            t)
13537          (condition-case ()
13538              (gnus-request-group group)
13539            (error nil)
13540            (quit nil))
13541          (save-excursion
13542            (set-buffer nntp-server-buffer)
13543            (goto-char (point-min))
13544            ;; Parse the result we got from `gnus-request-group'.
13545            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
13546                 (progn
13547                   (goto-char (match-beginning 1))
13548                   (gnus-sethash 
13549                    group (setq active (cons (read (current-buffer))
13550                                             (read (current-buffer))))
13551                    gnus-active-hashtb)
13552                   ;; Return the new active info.
13553                   active))))))
13554
13555 (defun gnus-update-read-articles 
13556   (group unread unselected ticked &optional domarks replied expirable killed
13557          dormant bookmark score)
13558   "Update the list of read and ticked articles in GROUP using the
13559 UNREAD and TICKED lists.
13560 Note: UNSELECTED has to be sorted over `<'.
13561 Returns whether the updating was successful."
13562   (let* ((active (or gnus-newsgroup-active 
13563                      (gnus-gethash group gnus-active-hashtb)))
13564          (entry (gnus-gethash group gnus-newsrc-hashtb))
13565          (info (nth 2 entry))
13566          (marked (nth 3 info))
13567          (prev 1)
13568          (unread (sort (copy-sequence unread) (function <)))
13569          read)
13570     (if (or (not info) (not active))
13571         ;; There is no info on this group if it was, in fact,
13572         ;; killed. Gnus stores no information on killed groups, so
13573         ;; there's nothing to be done. 
13574         ;; One could store the information somewhere temporarily,
13575         ;; perhaps... Hmmm... 
13576         ()
13577       ;; Remove any negative articles numbers.
13578       (while (and unread (< (car unread) 0))
13579         (setq unread (cdr unread)))
13580       ;; Remove any expired article numbers
13581       (while (and unread (< (car unread) (car active)))
13582         (setq unread (cdr unread)))
13583       (while (and ticked (< (car ticked) (car active)))
13584         (setq ticked (cdr ticked)))
13585       (while (and dormant (< (car dormant) (car active)))
13586         (setq dormant (cdr dormant)))
13587       (setq unread (sort (append unselected unread) '<))
13588       ;; Compute the ranges of read articles by looking at the list of
13589       ;; unread articles.  
13590       (while unread
13591         (if (/= (car unread) prev)
13592             (setq read (cons (if (= prev (1- (car unread))) prev
13593                                (cons prev (1- (car unread)))) read)))
13594         (setq prev (1+ (car unread)))
13595         (setq unread (cdr unread)))
13596       (if (<= prev (cdr active))
13597           (setq read (cons (cons prev (cdr active)) read)))
13598       ;; Enter this list into the group info.
13599       (setcar (cdr (cdr info)) 
13600               (if (> (length read) 1) (nreverse read) read))
13601       ;; Enter the list of ticked articles.
13602       (gnus-set-marked-articles 
13603        info ticked
13604        (if domarks replied (cdr (assq 'reply marked)))
13605        (if domarks expirable (cdr (assq 'expire marked)))
13606        (if domarks killed (cdr (assq 'killed marked)))
13607        (if domarks dormant (cdr (assq 'dormant marked)))
13608        (if domarks bookmark (cdr (assq 'bookmark marked)))
13609        (if domarks score (cdr (assq 'score marked))))
13610       ;; Set the number of unread articles in gnus-newsrc-hashtb.
13611       (gnus-get-unread-articles-in-group 
13612        info (gnus-gethash group gnus-active-hashtb))
13613       t)))
13614
13615 (defun gnus-make-articles-unread (group articles)
13616   "Mark ARTICLES in GROUP as unread."
13617   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
13618                           (gnus-gethash (gnus-group-real-name group)
13619                                         gnus-newsrc-hashtb))))
13620          (ranges (nth 2 info))
13621          news)
13622     (while articles
13623       (and (gnus-member-of-range (car articles) ranges)
13624            (setq news (cons (car articles) news)))
13625       (setq articles (cdr articles)))
13626     (if (not news)
13627         ()
13628       (setcar (nthcdr 2 info)
13629               (gnus-remove-from-range (nth 2 info) (nreverse news)))
13630       (gnus-group-update-group group t))))
13631
13632 ;; Enter all dead groups into the hashtb.
13633 (defun gnus-update-active-hashtb-from-killed ()
13634   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
13635         (lists (list gnus-killed-list gnus-zombie-list))
13636         killed)
13637     (while lists
13638       (setq killed (car lists))
13639       (while killed
13640         (gnus-sethash (car killed) nil hashtb)
13641         (setq killed (cdr killed)))
13642       (setq lists (cdr lists)))))
13643
13644 ;; Get the active file(s) from the backend(s).
13645 (defun gnus-read-active-file ()
13646   (gnus-group-set-mode-line)
13647   (let ((methods (if (gnus-check-server gnus-select-method)
13648                      ;; The native server is available.
13649                      (cons gnus-select-method gnus-secondary-select-methods)
13650                    ;; The native server is down, so we just do the
13651                    ;; secondary ones.   
13652                    gnus-secondary-select-methods))
13653         list-type)
13654     (setq gnus-have-read-active-file nil)
13655     (save-excursion
13656       (set-buffer nntp-server-buffer)
13657       (while methods
13658         (let* ((method (gnus-server-get-method nil (car methods)))
13659                (where (nth 1 method))
13660                (mesg (format "Reading active file%s via %s..."
13661                              (if (and where (not (zerop (length where))))
13662                                  (concat " from " where) "")
13663                              (car method))))
13664           (gnus-message 5 mesg)
13665           (if (not (gnus-check-server method))
13666               ()
13667             ;; Request that the backend scan its incoming messages.
13668             (and (gnus-check-backend-function 'request-scan (car method))
13669                  (gnus-request-scan nil method))
13670             (cond 
13671              ((and (eq gnus-read-active-file 'some)
13672                    (gnus-check-backend-function 'retrieve-groups (car method)))
13673               (let ((newsrc (cdr gnus-newsrc-alist))
13674                     (gmethod (gnus-server-get-method nil method))
13675                     groups)
13676                 (while newsrc
13677                   (and (gnus-server-equal 
13678                         (gnus-find-method-for-group 
13679                          (car (car newsrc)) (car newsrc))
13680                         gmethod)
13681                        (setq groups (cons (gnus-group-real-name 
13682                                            (car (car newsrc))) groups)))
13683                   (setq newsrc (cdr newsrc)))
13684                 (gnus-check-server method)
13685                 (setq list-type (gnus-retrieve-groups groups method))
13686                 (cond 
13687                  ((not list-type)
13688                   (gnus-message 
13689                    1 "Cannot read partial active file from %s server." 
13690                    (car method))
13691                   (ding)
13692                   (sit-for 2))
13693                  ((eq list-type 'active)
13694                   (gnus-active-to-gnus-format method gnus-active-hashtb))
13695                  (t
13696                   (gnus-groups-to-gnus-format method gnus-active-hashtb)))))
13697              (t
13698               (if (not (gnus-request-list method))
13699                   (progn
13700                     (gnus-message 1 "Cannot read active file from %s server." 
13701                                   (car method))
13702                     (ding))
13703                 (gnus-active-to-gnus-format method)
13704                 ;; We mark this active file as read.
13705                 (setq gnus-have-read-active-file
13706                       (cons method gnus-have-read-active-file))
13707                 (gnus-message 5 "%sdone" mesg))))))
13708         (setq methods (cdr methods))))))
13709
13710 ;; Read an active file and place the results in `gnus-active-hashtb'.
13711 (defun gnus-active-to-gnus-format (method &optional hashtb ignore-errors)
13712   (let ((cur (current-buffer))
13713         (hashtb (or hashtb 
13714                     (if (and gnus-active-hashtb 
13715                              (not (equal method gnus-select-method)))
13716                         gnus-active-hashtb
13717                       (setq gnus-active-hashtb
13718                             (if (equal method gnus-select-method)
13719                                 (gnus-make-hashtable 
13720                                  (count-lines (point-min) (point-max)))
13721                               (gnus-make-hashtable 4096))))))
13722         (flag-hashtb (gnus-make-hashtable 60)))
13723     ;; Delete unnecessary lines.
13724     (goto-char (point-min))
13725     (while (search-forward "\nto." nil t)
13726       (delete-region (1+ (match-beginning 0)) 
13727                      (progn (forward-line 1) (point))))
13728     (or (string= gnus-ignored-newsgroups "")
13729         (progn
13730           (goto-char (point-min))
13731           (delete-matching-lines gnus-ignored-newsgroups)))
13732     ;; Make the group names readable as a lisp expression even if they
13733     ;; contain special characters.
13734     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
13735     (goto-char (point-max))
13736     (while (re-search-backward "[][';?()#]" nil t)
13737       (insert ?\\))
13738     ;; If these are groups from a foreign select method, we insert the
13739     ;; group prefix in front of the group names. 
13740     (and method (not (gnus-server-equal
13741                       (gnus-server-get-method nil method)
13742                       (gnus-server-get-method nil gnus-select-method)))
13743          (let ((prefix (gnus-group-prefixed-name "" method)))
13744            (goto-char (point-min))
13745            (while (and (not (eobp))
13746                        (progn (insert prefix)
13747                               (zerop (forward-line 1)))))))
13748     ;; Store the active file in a hash table.
13749     (goto-char (point-min))
13750     (if (string-match "%[oO]" gnus-group-line-format)
13751         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
13752         ;; If we want information on moderated groups, we use this
13753         ;; loop...   
13754         (let* ((mod-hashtb (make-vector 7 0))
13755                (m (intern "m" mod-hashtb))
13756                group max min)
13757           (while (not (eobp))
13758             (condition-case nil
13759                 (progn
13760                   (narrow-to-region (point) (gnus-point-at-eol))
13761                   (setq group (let ((obarray hashtb)) (read cur)))
13762                   (if (and (numberp (setq max (read cur)))
13763                            (numberp (setq min (read cur)))
13764                            (progn 
13765                              (skip-chars-forward " \t")
13766                              (not
13767                               (or (= (following-char) ?=)
13768                                   (= (following-char) ?x)
13769                                   (= (following-char) ?j)))))
13770                       (set group (cons min max))
13771                     (set group nil))
13772                   ;; Enter moderated groups into a list.
13773                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
13774                       (setq gnus-moderated-list 
13775                             (cons (symbol-name group) gnus-moderated-list))))
13776               (error 
13777                (and group
13778                     (symbolp group)
13779                     (set group nil))))
13780             (widen)
13781             (forward-line 1)))
13782       ;; And if we do not care about moderation, we use this loop,
13783       ;; which is faster.
13784       (let (group max min)
13785         (while (not (eobp))
13786           (condition-case ()
13787               (progn
13788                 (narrow-to-region (point) (gnus-point-at-eol))
13789                 ;; group gets set to a symbol interned in the hash table
13790                 ;; (what a hack!!) - jwz
13791                 (setq group (let ((obarray hashtb)) (read cur)))
13792                 (if (and (numberp (setq max (read cur)))
13793                          (numberp (setq min (read cur)))
13794                          (progn 
13795                            (skip-chars-forward " \t")
13796                            (not
13797                             (or (= (following-char) ?=)
13798                                 (= (following-char) ?x)
13799                                 (= (following-char) ?j)))))
13800                     (set group (cons min max))
13801                   (set group nil)))
13802             (error 
13803              (progn 
13804                (and group
13805                     (symbolp group)
13806                     (set group nil))
13807                (or ignore-errors
13808                    (gnus-message 3 "Warning - illegal active: %s"
13809                                  (buffer-substring 
13810                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
13811           (widen)
13812           (forward-line 1))))))
13813
13814 (defun gnus-groups-to-gnus-format (method &optional hashtb)
13815   ;; Parse a "groups" active file.
13816   (let ((cur (current-buffer))
13817         (hashtb (or hashtb 
13818                     (if (and method gnus-active-hashtb)
13819                         gnus-active-hashtb
13820                       (setq gnus-active-hashtb
13821                             (gnus-make-hashtable 
13822                              (count-lines (point-min) (point-max)))))))
13823         (prefix (and method 
13824                      (not (gnus-server-equal
13825                            (gnus-server-get-method nil method)
13826                            (gnus-server-get-method nil gnus-select-method)))
13827                      (gnus-group-prefixed-name "" method))))
13828
13829     (goto-char (point-min))
13830     ;; We split this into to separate loops, one with the prefix
13831     ;; and one without to speed the reading up somewhat.
13832     (if prefix
13833         (let (min max opoint group)
13834           (while (not (eobp))
13835             (condition-case ()
13836                 (progn
13837                   (read cur) (read cur)
13838                   (setq min (read cur)
13839                         max (read cur)
13840                         opoint (point))
13841                   (skip-chars-forward " \t")
13842                   (insert prefix)
13843                   (goto-char opoint)
13844                   (set (let ((obarray hashtb)) (read cur)) 
13845                        (cons min max)))
13846               (error (and group (symbolp group) (set group nil))))
13847             (forward-line 1)))
13848       (let (min max group)
13849         (while (not (eobp))
13850           (condition-case ()
13851               (if (= (following-char) ?2)
13852                   (progn
13853                     (read cur) (read cur)
13854                     (setq min (read cur)
13855                           max (read cur))
13856                     (set (setq group (let ((obarray hashtb)) (read cur)))
13857                          (cons min max))))
13858             (error (and group (symbolp group) (set group nil))))
13859           (forward-line 1))))))
13860
13861 (defun gnus-read-newsrc-file (&optional force)
13862   "Read startup file.
13863 If FORCE is non-nil, the .newsrc file is read."
13864   ;; Reset variables that might be defined in the .newsrc.eld file.
13865   (let ((variables gnus-variable-list))
13866     (while variables
13867       (set (car variables) nil)
13868       (setq variables (cdr variables))))
13869   (let* ((newsrc-file gnus-current-startup-file)
13870          (quick-file (concat newsrc-file ".el")))
13871     (save-excursion
13872       ;; We always load the .newsrc.eld file. If always contains
13873       ;; much information that can not be gotten from the .newsrc
13874       ;; file (ticked articles, killed groups, foreign methods, etc.)
13875       (gnus-read-newsrc-el-file quick-file)
13876  
13877       (if (or force
13878               (and (file-newer-than-file-p newsrc-file quick-file)
13879                    (file-newer-than-file-p newsrc-file 
13880                                            (concat quick-file "d")))
13881               (not gnus-newsrc-alist))
13882           ;; We read the .newsrc file. Note that if there if a
13883           ;; .newsrc.eld file exists, it has already been read, and
13884           ;; the `gnus-newsrc-hashtb' has been created. While reading
13885           ;; the .newsrc file, Gnus will only use the information it
13886           ;; can find there for changing the data already read -
13887           ;; ie. reading the .newsrc file will not trash the data
13888           ;; already read (except for read articles).
13889           (save-excursion
13890             (gnus-message 5 "Reading %s..." newsrc-file)
13891             (set-buffer (find-file-noselect newsrc-file))
13892             (buffer-disable-undo (current-buffer))
13893             (gnus-newsrc-to-gnus-format)
13894             (kill-buffer (current-buffer))
13895             (gnus-message 5 "Reading %s...done" newsrc-file)))
13896
13897       ;; Read any slave files.
13898       (or gnus-slave
13899           (gnus-master-read-slave-newsrc)))))
13900
13901 (defun gnus-read-newsrc-el-file (file)
13902   (let ((ding-file (concat file "d")))
13903     ;; We always, always read the .eld file.
13904     (gnus-message 5 "Reading %s..." ding-file)
13905     (let (gnus-newsrc-assoc)
13906       (condition-case nil
13907           (load ding-file t t t)
13908         (error
13909          (gnus-message 1 "Error in %s" ding-file)
13910          (ding)))
13911       (and gnus-newsrc-assoc (setq gnus-newsrc-alist gnus-newsrc-assoc)))
13912     (let ((inhibit-quit t))
13913       (gnus-uncompress-newsrc-alist))
13914     (gnus-make-hashtable-from-newsrc-alist)
13915     (if (not (file-newer-than-file-p file ding-file))
13916         ()
13917       ;; Old format quick file
13918       (gnus-message 5 "Reading %s..." file)
13919       ;; The .el file is newer than the .eld file, so we read that one
13920       ;; as well. 
13921       (gnus-read-old-newsrc-el-file file))))
13922
13923 ;; Parse the old-style quick startup file
13924 (defun gnus-read-old-newsrc-el-file (file)
13925   (let (newsrc killed marked group m)
13926     (prog1
13927         (let ((gnus-killed-assoc nil)
13928               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
13929           (prog1
13930               (condition-case nil
13931                   (load file t t t)
13932                 (error nil))
13933             (setq newsrc gnus-newsrc-assoc
13934                   killed gnus-killed-assoc
13935                   marked gnus-marked-assoc)))
13936       (setq gnus-newsrc-alist nil)
13937       (while newsrc
13938         (setq group (car newsrc))
13939         (let ((info (nth 2 (gnus-gethash (car group) gnus-newsrc-hashtb))))
13940           (if info
13941               (progn
13942                 (setcar (nthcdr 2 info) (cdr (cdr group)))
13943                 (setcar (cdr info)
13944                         (if (nth 1 group) gnus-level-default-subscribed 
13945                           gnus-level-default-unsubscribed))
13946                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
13947             (setq gnus-newsrc-alist
13948                   (cons 
13949                    (setq info
13950                          (list (car group)
13951                                (if (nth 1 group) gnus-level-default-subscribed
13952                                  gnus-level-default-unsubscribed) 
13953                                (cdr (cdr group))))
13954                    gnus-newsrc-alist)))
13955           (if (setq m (assoc (car group) marked))
13956               (setcdr (cdr (cdr info))
13957                       (cons (list (cons 'tick (cdr m))) nil))))
13958         (setq newsrc (cdr newsrc)))
13959       (setq newsrc killed)
13960       (while newsrc
13961         (setcar newsrc (car (car newsrc)))
13962         (setq newsrc (cdr newsrc)))
13963       (setq gnus-killed-list killed))
13964     ;; The .el file version of this variable does not begin with
13965     ;; "options", while the .eld version does, so we just add it if it
13966     ;; isn't there.
13967     (and
13968      gnus-newsrc-options 
13969      (progn
13970        (and (not (string-match "^ *options" gnus-newsrc-options))
13971             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
13972        (and (not (string-match "\n$" gnus-newsrc-options))
13973             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
13974        ;; Finally, if we read some options lines, we parse them.
13975        (or (string= gnus-newsrc-options "")
13976            (gnus-newsrc-parse-options gnus-newsrc-options))))
13977
13978     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
13979     (gnus-make-hashtable-from-newsrc-alist)))
13980       
13981 (defun gnus-make-newsrc-file (file)
13982   "Make server dependent file name by catenating FILE and server host name."
13983   (let* ((file (expand-file-name file nil))
13984          (real-file (concat file "-" (nth 1 gnus-select-method))))
13985     (if (or (file-exists-p real-file)
13986             (file-exists-p (concat real-file ".el"))
13987             (file-exists-p (concat real-file ".eld")))
13988         real-file file)))
13989
13990 (defun gnus-uncompress-newsrc-alist ()
13991   ;; Uncompress all lists of marked articles in the newsrc assoc.
13992   (let ((newsrc gnus-newsrc-alist)
13993         marked)
13994     (while newsrc
13995       (if (not (setq marked (nth 3 (car newsrc))))
13996           ()
13997         (while marked
13998           (or (eq 'score (car (car marked)))
13999               (eq 'bookmark (car (car marked)))
14000               (eq 'killed (car (car marked)))
14001               (setcdr (car marked) (gnus-uncompress-range (cdr (car marked)))))
14002           (setq marked (cdr marked))))
14003       (setq newsrc (cdr newsrc)))))
14004
14005 (defun gnus-compress-newsrc-alist ()
14006   ;; Compress all lists of marked articles in the newsrc assoc.
14007   (let ((newsrc gnus-newsrc-alist)
14008         marked)
14009     (while newsrc
14010       (if (not (setq marked (nth 3 (car newsrc))))
14011           ()
14012         (while marked
14013           (or (eq 'score (car (car marked)))
14014               (eq 'bookmark (car (car marked)))
14015               (eq 'killed (car (car marked)))
14016               (setcdr (car marked) 
14017                       (condition-case ()
14018                           (gnus-compress-sequence 
14019                            (sort (cdr (car marked)) '<) t)
14020                         (error (cdr (car marked))))))
14021           (setq marked (cdr marked))))
14022       (setq newsrc (cdr newsrc)))))
14023
14024 (defun gnus-newsrc-to-gnus-format ()
14025   (setq gnus-newsrc-options "")
14026   (setq gnus-newsrc-options-n nil)
14027
14028   (or gnus-active-hashtb
14029       (setq gnus-active-hashtb (make-vector 4095 0)))
14030   (let ((buf (current-buffer))
14031         (already-read (> (length gnus-newsrc-alist) 1))
14032         group subscribed options-symbol newsrc Options-symbol
14033         symbol reads num1)
14034     (goto-char (point-min))
14035     ;; We intern the symbol `options' in the active hashtb so that we
14036     ;; can `eq' against it later.
14037     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
14038     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
14039   
14040     (while (not (eobp))
14041       ;; We first read the first word on the line by narrowing and
14042       ;; then reading into `gnus-active-hashtb'.  Most groups will
14043       ;; already exist in that hashtb, so this will save some string
14044       ;; space.
14045       (narrow-to-region
14046        (point)
14047        (progn (skip-chars-forward "^ \t!:\n") (point)))
14048       (goto-char (point-min))
14049       (setq symbol 
14050             (and (/= (point-min) (point-max))
14051                  (let ((obarray gnus-active-hashtb)) (read buf))))
14052       (widen)
14053       ;; Now, the symbol we have read is either `options' or a group
14054       ;; name.  If it is an options line, we just add it to a string. 
14055       (cond 
14056        ((or (eq symbol options-symbol)
14057             (eq symbol Options-symbol))
14058         (setq gnus-newsrc-options
14059               ;; This concatting is quite inefficient, but since our
14060               ;; thorough studies show that approx 99.37% of all
14061               ;; .newsrc files only contain a single options line, we
14062               ;; don't give a damn, frankly, my dear.
14063               (concat gnus-newsrc-options
14064                       (buffer-substring 
14065                        (gnus-point-at-bol)
14066                        ;; Options may continue on the next line.
14067                        (or (and (re-search-forward "^[^ \t]" nil 'move)
14068                                 (progn (beginning-of-line) (point)))
14069                            (point))))))
14070        (symbol
14071         (or (boundp symbol) (set symbol nil))
14072         ;; It was a group name.
14073         (setq subscribed (= (following-char) ?:)
14074               group (symbol-name symbol)
14075               reads nil)
14076         (if (eolp)
14077             ;; If the line ends here, this is clearly a buggy line, so
14078             ;; we put point a the beginning of line and let the cond
14079             ;; below do the error handling.
14080             (beginning-of-line)
14081           ;; We skip to the beginning of the ranges.
14082           (skip-chars-forward "!: \t"))
14083         ;; We are now at the beginning of the list of read articles.
14084         ;; We read them range by range.
14085         (while
14086             (cond 
14087              ((looking-at "[0-9]+")
14088               ;; We narrow and read a number instead of buffer-substring/
14089               ;; string-to-int because it's faster. narrow/widen is
14090               ;; faster than save-restriction/narrow, and save-restriction
14091               ;; produces a garbage object.
14092               (setq num1 (progn
14093                            (narrow-to-region (match-beginning 0) (match-end 0))
14094                            (read buf)))
14095               (widen)
14096               ;; If the next character is a dash, then this is a range.
14097               (if (= (following-char) ?-)
14098                   (progn
14099                     ;; We read the upper bound of the range.
14100                     (forward-char 1)
14101                     (if (not (looking-at "[0-9]+"))
14102                         ;; This is a buggy line, by we pretend that
14103                         ;; it's kinda OK. Perhaps the user should be
14104                         ;; dinged? 
14105                         (setq reads (cons num1 reads))
14106                       (setq reads 
14107                             (cons 
14108                              (cons num1
14109                                    (progn
14110                                      (narrow-to-region (match-beginning 0) 
14111                                                        (match-end 0))
14112                                      (read buf)))
14113                              reads))
14114                       (widen)))
14115                 ;; It was just a simple number, so we add it to the
14116                 ;; list of ranges.
14117                 (setq reads (cons num1 reads)))
14118               ;; If the next char in ?\n, then we have reached the end
14119               ;; of the line and return nil.
14120               (/= (following-char) ?\n))
14121              ((= (following-char) ?\n)
14122               ;; End of line, so we end.
14123               nil)
14124              (t
14125               ;; Not numbers and not eol, so this might be a buggy
14126               ;; line... 
14127               (or (eobp)                
14128                   ;; If it was eob instead of ?\n, we allow it.
14129                   (progn
14130                     ;; The line was buggy.
14131                     (setq group nil)
14132                     (gnus-message 3 "Mangled line: %s" 
14133                                   (buffer-substring (gnus-point-at-bol) 
14134                                                     (gnus-point-at-eol)))
14135                     (ding)
14136                     (sit-for 1)))
14137               nil))
14138           ;; Skip past ", ". Spaces are illegal in these ranges, but
14139           ;; we allow them, because it's a common mistake to put a
14140           ;; space after the comma.
14141           (skip-chars-forward ", "))
14142
14143         ;; We have already read .newsrc.eld, so we gently update the
14144         ;; data in the hash table with the information we have just
14145         ;; read. 
14146         (if (not group)
14147             ()
14148           (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
14149                 level)
14150             (if info
14151                 ;; There is an entry for this file in the alist.
14152                 (progn
14153                   (setcar (nthcdr 2 info) (nreverse reads))
14154                   ;; We update the level very gently.  In fact, we
14155                   ;; only change it if there's been a status change
14156                   ;; from subscribed to unsubscribed, or vice versa.
14157                   (setq level (nth 1 info))
14158                   (cond ((and (<= level gnus-level-subscribed)
14159                               (not subscribed))
14160                          (setq level (if reads
14161                                          gnus-level-default-unsubscribed 
14162                                        (1+ gnus-level-default-unsubscribed))))
14163                         ((and (> level gnus-level-subscribed) subscribed)
14164                          (setq level gnus-level-default-subscribed)))
14165                   (setcar (cdr info) level))
14166               ;; This is a new group.
14167               (setq info (list group 
14168                                (if subscribed
14169                                    gnus-level-default-subscribed 
14170                                  (if reads
14171                                      (1+ gnus-level-subscribed)
14172                                    gnus-level-default-unsubscribed))
14173                                (nreverse reads))))
14174             (setq newsrc (cons info newsrc))))))
14175       (forward-line 1))
14176     
14177     (setq newsrc (nreverse newsrc))
14178
14179     (if (not already-read)
14180         ()
14181       ;; We now have two newsrc lists - `newsrc', which is what we
14182       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
14183       ;; what we've read from .newsrc.eld. We have to merge these
14184       ;; lists. We do this by "attaching" any (foreign) groups in the
14185       ;; gnus-newsrc-alist to the (native) group that precedes them. 
14186       (let ((rc (cdr gnus-newsrc-alist))
14187             (prev gnus-newsrc-alist)
14188             entry mentry)
14189         (while rc
14190           (or (null (nth 4 (car rc)))   ; It's a native group.
14191               (assoc (car (car rc)) newsrc) ; It's already in the alist.
14192               (if (setq entry (assoc (car (car prev)) newsrc))
14193                   (setcdr (setq mentry (memq entry newsrc))
14194                           (cons (car rc) (cdr mentry)))
14195                 (setq newsrc (cons (car rc) newsrc))))
14196           (setq prev rc
14197                 rc (cdr rc)))))
14198
14199     (setq gnus-newsrc-alist newsrc)
14200     ;; We make the newsrc hashtb.
14201     (gnus-make-hashtable-from-newsrc-alist)
14202
14203     ;; Finally, if we read some options lines, we parse them.
14204     (or (string= gnus-newsrc-options "")
14205         (gnus-newsrc-parse-options gnus-newsrc-options))))
14206
14207 ;; Parse options lines to find "options -n !all rec.all" and stuff.
14208 ;; The return value will be a list on the form
14209 ;; ((regexp1 . ignore)
14210 ;;  (regexp2 . subscribe)...)
14211 ;; When handling new newsgroups, groups that match a `ignore' regexp
14212 ;; will be ignored, and groups that match a `subscribe' regexp will be
14213 ;; subscribed. A line like
14214 ;; options -n !all rec.all
14215 ;; will lead to a list that looks like
14216 ;; (("^rec\\..+" . subscribe) 
14217 ;;  ("^.+" . ignore))
14218 ;; So all "rec.*" groups will be subscribed, while all the other
14219 ;; groups will be ignored. Note that "options -n !all rec.all" is very
14220 ;; different from "options -n rec.all !all". 
14221 (defun gnus-newsrc-parse-options (options)
14222   (let (out eol)
14223     (save-excursion
14224       (gnus-set-work-buffer)
14225       (insert (regexp-quote options))
14226       ;; First we treat all continuation lines.
14227       (goto-char (point-min))
14228       (while (re-search-forward "\n[ \t]+" nil t)
14229         (replace-match " " t t))
14230       ;; Then we transform all "all"s into ".+"s.
14231       (goto-char (point-min))
14232       (while (re-search-forward "\\ball\\b" nil t)
14233         (replace-match ".+" t t))
14234       (goto-char (point-min))
14235       ;; We remove all other options than the "-n" ones.
14236       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
14237         (replace-match " ")
14238         (forward-char -1))
14239       (goto-char (point-min))
14240
14241       ;; We are only interested in "options -n" lines - we
14242       ;; ignore the other option lines.
14243       (while (re-search-forward "[ \t]-n" nil t)
14244         (setq eol 
14245               (or (save-excursion
14246                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
14247                          (- (point) 2)))
14248                   (gnus-point-at-eol)))
14249         ;; Search for all "words"...
14250         (while (re-search-forward "[^ \t,\n]+" eol t)
14251           (if (= (char-after (match-beginning 0)) ?!)
14252               ;; If the word begins with a bang (!), this is a "not"
14253               ;; spec. We put this spec (minus the bang) and the
14254               ;; symbol `ignore' into the list.
14255               (setq out (cons (cons (concat 
14256                                      "^" (buffer-substring 
14257                                           (1+ (match-beginning 0))
14258                                           (match-end 0)))
14259                                     'ignore) out))
14260             ;; There was no bang, so this is a "yes" spec.
14261             (setq out (cons (cons (concat 
14262                                    "^" (buffer-substring (match-beginning 0)
14263                                                          (match-end 0)))
14264                                   'subscribe) out)))))
14265     
14266       (setq gnus-newsrc-options-n out))))
14267                
14268
14269 (defun gnus-save-newsrc-file ()
14270   "Save .newsrc file."
14271   ;; Note: We cannot save .newsrc file if all newsgroups are removed
14272   ;; from the variable gnus-newsrc-alist.
14273   (and (or gnus-newsrc-alist gnus-killed-list)
14274        gnus-current-startup-file
14275        (progn
14276          (save-excursion
14277            (if (and (or gnus-use-dribble-file gnus-slave)
14278                     (or (not gnus-dribble-buffer)
14279                         (not (buffer-name gnus-dribble-buffer))
14280                         (zerop (save-excursion
14281                                  (set-buffer gnus-dribble-buffer)
14282                                  (buffer-size)))))
14283                (gnus-message 4 "(No changes need to be saved)")
14284              (run-hooks 'gnus-save-newsrc-hook)
14285              (if gnus-slave
14286                  (gnus-slave-save-newsrc)
14287                (if gnus-save-newsrc-file
14288                    (progn
14289                      (gnus-message 5 "Saving %s..." gnus-current-startup-file)
14290                      ;; Make backup file of master newsrc.
14291                      (gnus-gnus-to-newsrc-format)
14292                      (gnus-message 5 "Saving %s...done"
14293                                    gnus-current-startup-file)))
14294                ;; Quickly loadable .newsrc.
14295                (set-buffer (get-buffer-create " *Gnus-newsrc*"))
14296                (setq buffer-file-name 
14297                      (concat gnus-current-startup-file ".eld"))
14298                (gnus-add-current-to-buffer-list)
14299                (buffer-disable-undo (current-buffer))
14300                (erase-buffer)
14301                (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
14302                (gnus-gnus-to-quick-newsrc-format)
14303                (run-hooks 'gnus-save-quick-newsrc-hook)
14304                (save-buffer)
14305                (kill-buffer (current-buffer))
14306                (gnus-message 
14307                 5 "Saving %s.eld...done" gnus-current-startup-file))
14308              (gnus-dribble-delete-file))))))
14309
14310 (defun gnus-gnus-to-quick-newsrc-format ()
14311   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
14312   (insert ";; Gnus startup file.\n")
14313   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
14314   (insert ";; to read .newsrc.\n")
14315   (insert "(setq gnus-newsrc-file-version "
14316           (prin1-to-string gnus-version) ")\n")
14317   (let ((variables gnus-variable-list)
14318         (inhibit-quit t)
14319         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
14320         variable)
14321     ;; insert lisp expressions.
14322     (gnus-compress-newsrc-alist)
14323     (while variables
14324       (setq variable (car variables))
14325       (and (boundp variable)
14326            (symbol-value variable)
14327            (or gnus-save-killed-list (not (eq variable 'gnus-killed-list)))
14328            (insert "(setq " (symbol-name variable) " '"
14329                    (prin1-to-string (symbol-value variable))
14330                    ")\n"))
14331       (setq variables (cdr variables)))
14332     (gnus-uncompress-newsrc-alist)))
14333
14334 (defun gnus-gnus-to-newsrc-format ()
14335   ;; Generate and save the .newsrc file.
14336   (let ((newsrc (cdr gnus-newsrc-alist))
14337         info ranges range)
14338     (save-excursion
14339       (set-buffer (create-file-buffer gnus-current-startup-file))
14340       (setq buffer-file-name gnus-current-startup-file)
14341       (buffer-disable-undo (current-buffer))
14342       (erase-buffer)
14343       ;; Write options.
14344       (if gnus-newsrc-options (insert gnus-newsrc-options))
14345       ;; Write subscribed and unsubscribed.
14346       (while newsrc
14347         (setq info (car newsrc))
14348         (if (not (nth 4 info))          ;Don't write foreign groups to .newsrc.
14349             (progn
14350               (insert (car info) (if (> (nth 1 info) gnus-level-subscribed)
14351                                      "!" ":"))
14352               (if (setq ranges (nth 2 info))
14353                   (progn
14354                     (insert " ")
14355                     (if (not (listp (cdr ranges)))
14356                         (if (= (car ranges) (cdr ranges))
14357                             (insert (int-to-string (car ranges)))
14358                           (insert (int-to-string (car ranges)) "-" 
14359                                   (int-to-string (cdr ranges))))
14360                       (while ranges
14361                         (setq range (car ranges)
14362                               ranges (cdr ranges))
14363                         (if (or (atom range) (= (car range) (cdr range)))
14364                             (insert (int-to-string 
14365                                      (or (and (atom range) range) 
14366                                          (car range))))
14367                           (insert (int-to-string (car range)) "-"
14368                                   (int-to-string (cdr range))))
14369                         (if ranges (insert ","))))))
14370               (insert "\n")))
14371         (setq newsrc (cdr newsrc)))
14372       ;; It has been reported that sometime the modtime on the .newsrc
14373       ;; file seems to be off. We really do want to overwrite it, so
14374       ;; we clear the modtime here before saving. It's a bit odd,
14375       ;; though... 
14376       ;; sometimes the modtime clear isn't sufficient.  most brute force:
14377       ;; delete the silly thing entirely first.  but this fails to provide
14378       ;; such niceties as .newsrc~ creation.
14379       (if gnus-modtime-botch
14380           (delete-file gnus-startup-file)
14381         (clear-visited-file-modtime))
14382       (run-hooks 'gnus-save-standard-newsrc-hook)
14383       (save-buffer)
14384       (kill-buffer (current-buffer)))))
14385
14386
14387 ;;; Slave functions.
14388
14389 (defun gnus-slave-save-newsrc ()
14390   (save-excursion
14391     (set-buffer gnus-dribble-buffer)
14392     (let ((slave-name 
14393            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
14394       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
14395
14396 (defun gnus-master-read-slave-newsrc ()
14397   (let ((slave-files 
14398          (directory-files 
14399           (file-name-directory gnus-current-startup-file)
14400           t (concat 
14401              "^" (regexp-quote
14402                   (concat
14403                    (file-name-nondirectory gnus-current-startup-file)
14404                    "-slave-")))
14405           t))
14406         file)
14407     (if (not slave-files)
14408         ()                              ; There are no slave files to read.
14409       (gnus-message 7 "Reading slave newsrcs...")
14410       (save-excursion
14411         (set-buffer (get-buffer-create " *gnus slave*"))
14412         (buffer-disable-undo (current-buffer))
14413         (setq slave-files 
14414               (sort (mapcar (lambda (file) 
14415                               (list (nth 5 (file-attributes file)) file))
14416                             slave-files)
14417                     (lambda (f1 f2)
14418                       (or (< (car (car f1)) (car (car f2)))
14419                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
14420         (while slave-files
14421           (erase-buffer)
14422           (setq file (nth 1 (car slave-files)))
14423           (insert-file-contents file)
14424           (if (condition-case ()
14425                   (progn
14426                     (eval-buffer (current-buffer))
14427                     t)
14428                 (error 
14429                  (message "Possible error in %s" file)
14430                  (ding)
14431                  (sit-for 2)
14432                  nil))
14433               (or gnus-slave ; Slaves shouldn't delete these files.
14434                   (condition-case ()
14435                       (delete-file file)
14436                     (error nil))))
14437           (setq slave-files (cdr slave-files))))
14438       (gnus-message 7 "Reading slave newsrcs...done"))))
14439
14440
14441 ;;; Group description.
14442
14443 (defun gnus-read-all-descriptions-files ()
14444   (let ((methods (cons gnus-select-method gnus-secondary-select-methods)))
14445     (while methods
14446       (gnus-read-descriptions-file (car methods))
14447       (setq methods (cdr methods)))
14448     t))
14449
14450 (defun gnus-read-descriptions-file (&optional method)
14451   (let ((method (or method gnus-select-method)))
14452     ;; We create the hashtable whether we manage to read the desc file
14453     ;; to avoid trying to re-read after a failed read.
14454     (or gnus-description-hashtb
14455         (setq gnus-description-hashtb 
14456               (gnus-make-hashtable (length gnus-active-hashtb))))
14457     ;; Mark this method's desc file as read.
14458     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
14459                   gnus-description-hashtb)
14460
14461     (gnus-message 5 "Reading descriptions file via %s..." (car method))
14462     (cond 
14463      ((not (gnus-check-server method))
14464       (gnus-message 1 "Couldn't open server")
14465       nil)
14466      ((not (gnus-request-list-newsgroups method))
14467       (gnus-message 1 "Couldn't read newsgroups descriptions")
14468       nil)
14469      (t
14470       (let (group)
14471         (save-excursion
14472           (save-restriction
14473             (set-buffer nntp-server-buffer)
14474             (goto-char (point-min))
14475             (if (or (search-forward "\n.\n" nil t)
14476                     (goto-char (point-max)))
14477                 (progn
14478                   (beginning-of-line)
14479                   (narrow-to-region (point-min) (point))))
14480             (goto-char (point-min))
14481             (while (not (eobp))
14482               ;; If we get an error, we set group to 0, which is not a
14483               ;; symbol... 
14484               (setq group 
14485                     (condition-case ()
14486                         (let ((obarray gnus-description-hashtb))
14487                           ;; Group is set to a symbol interned in this
14488                           ;; hash table.
14489                           (read nntp-server-buffer))
14490                       (error 0)))
14491               (skip-chars-forward " \t")
14492               ;; ... which leads to this line being effectively ignored.
14493               (and (symbolp group)
14494                    (set group (buffer-substring 
14495                                (point) (progn (end-of-line) (point)))))
14496               (forward-line 1))))
14497         (gnus-message 5 "Reading descriptions file...done")
14498         t)))))
14499
14500 (defun gnus-group-get-description (group)
14501   ;; Get the description of a group by sending XGTITLE to the server.
14502   (and (gnus-request-group-description group)
14503        (save-excursion
14504          (set-buffer nntp-server-buffer)
14505          (goto-char (point-min))
14506          (and (looking-at "[^ \t]+[ \t]+\\(.*\\)")
14507               (buffer-substring (match-beginning 1) (match-end 1))))))
14508
14509 ;;;
14510 ;;; Server
14511 ;;;
14512
14513 (defvar gnus-server-mode-hook nil
14514   "Hook run in `gnus-server-mode' buffers.")
14515
14516 (defconst gnus-server-line-format "     {%(%h:%w%)} %s\n"
14517   "Format of server lines.
14518 It works along the same lines as a normal formatting string,
14519 with some simple extensions.")
14520
14521 (defvar gnus-server-mode-line-format "Gnus  List of servers"
14522   "The format specification for the server mode line.")
14523
14524 (defconst gnus-server-line-format-alist
14525   (` ((?h how ?s)
14526       (?n name ?s)
14527       (?w where ?s)
14528       (?s status ?s)))) 
14529
14530 (defconst gnus-server-mode-line-format-alist 
14531   (` ((?S news-server ?s)
14532       (?M news-method ?s)
14533       (?u user-defined ?s))))
14534
14535 (defvar gnus-server-line-format-spec nil)
14536 (defvar gnus-server-mode-line-format-spec nil)
14537 (defvar gnus-server-killed-servers nil)
14538
14539 (defvar gnus-server-mode-map nil)
14540 (put 'gnus-server-mode 'mode-class 'special)
14541
14542 (if gnus-server-mode-map
14543     nil
14544   (setq gnus-server-mode-map (make-sparse-keymap))
14545   (suppress-keymap gnus-server-mode-map)
14546   (define-key gnus-server-mode-map " " 'gnus-server-read-server)
14547   (define-key gnus-server-mode-map "\r" 'gnus-server-read-server)
14548   (define-key gnus-server-mode-map gnus-mouse-2 'gnus-server-pick-server)
14549   (define-key gnus-server-mode-map "q" 'gnus-server-exit)
14550   (define-key gnus-server-mode-map "l" 'gnus-server-list-servers)
14551   (define-key gnus-server-mode-map "k" 'gnus-server-kill-server)
14552   (define-key gnus-server-mode-map "y" 'gnus-server-yank-server)
14553   (define-key gnus-server-mode-map "c" 'gnus-server-copy-server)
14554   (define-key gnus-server-mode-map "a" 'gnus-server-add-server)
14555   (define-key gnus-server-mode-map "e" 'gnus-server-edit-server)
14556
14557   (define-key gnus-server-mode-map "O" 'gnus-server-open-server)
14558   (define-key gnus-server-mode-map "C" 'gnus-server-close-server)
14559   (define-key gnus-server-mode-map "D" 'gnus-server-deny-server)
14560   (define-key gnus-server-mode-map "R" 'gnus-server-remove-denials)
14561   )
14562
14563 (defun gnus-server-mode ()
14564   "Major mode for listing and editing servers.
14565
14566 All normal editing commands are switched off.
14567 \\<gnus-server-mode-map>
14568
14569 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
14570
14571 The following commands are available:
14572
14573 \\{gnus-server-mode-map}"
14574   (interactive)
14575   (if (gnus-visual-p 'server-menu 'menu) (gnus-server-make-menu-bar))
14576   (kill-all-local-variables)
14577   (setq mode-line-modified "-- ")
14578   (make-local-variable 'mode-line-format)
14579   (setq mode-line-format (copy-sequence mode-line-format))
14580   (and (equal (nth 3 mode-line-format) "   ")
14581        (setcar (nthcdr 3 mode-line-format) ""))
14582   (setq major-mode 'gnus-server-mode)
14583   (setq mode-name "Server")
14584                                         ;  (gnus-group-set-mode-line)
14585   (setq mode-line-process nil)
14586   (use-local-map gnus-server-mode-map)
14587   (buffer-disable-undo (current-buffer))
14588   (setq truncate-lines t)
14589   (setq buffer-read-only t)
14590   (run-hooks 'gnus-server-mode-hook))
14591
14592 (defun gnus-server-insert-server-line (sformat name method)
14593   (let* ((sformat (or sformat gnus-server-line-format-spec))
14594          (how (car method))
14595          (where (nth 1 method))
14596          (elem (assoc method gnus-opened-servers))
14597          (status (cond ((eq (nth 1 elem) 'denied)
14598                         "(denied)")
14599                        ((gnus-server-opened method)
14600                         "(open)")
14601                        (t
14602                         "(closed)")))
14603          b)
14604     (beginning-of-line)
14605     (setq b (point))
14606     ;; Insert the text.
14607     (eval sformat)
14608     (add-text-properties b (1+ b) (list 'gnus-server (intern name)))))
14609
14610 (defun gnus-server-setup-buffer ()
14611   (if (get-buffer gnus-server-buffer)
14612       ()
14613     (save-excursion
14614       (set-buffer (get-buffer-create gnus-server-buffer))
14615       (gnus-server-mode)
14616       (and gnus-carpal (gnus-carpal-setup-buffer 'server)))))
14617
14618 (defun gnus-server-prepare ()
14619   (setq gnus-server-mode-line-format-spec 
14620         (gnus-parse-format gnus-server-mode-line-format 
14621                            gnus-server-mode-line-format-alist))
14622   (setq gnus-server-line-format-spec 
14623         (gnus-parse-format gnus-server-line-format 
14624                            gnus-server-line-format-alist))
14625   (let ((alist gnus-server-alist)
14626         (buffer-read-only nil)
14627         (opened gnus-opened-servers)
14628         done)
14629     (erase-buffer)
14630     ;; First we do the real list of servers.
14631     (while alist
14632       (gnus-server-insert-server-line nil (car (car alist)) (cdr (car alist)))
14633       (and (assoc (cdr (car alist)) gnus-opened-servers)
14634            (setq done (cons (cdr (car alist)) done)))
14635       (setq alist (cdr alist)))
14636     ;; Then we insert the list of servers that have been opened in
14637     ;; this session.
14638     (while opened 
14639       (or (member (car (car opened)) done)
14640           (gnus-server-insert-server-line 
14641            nil (concat (car (car (car opened)))
14642                        (nth 1 (car (car opened)))) (car (car opened))))
14643       (setq opened (cdr opened))))
14644   (goto-char (point-min))
14645   (gnus-server-position-point))
14646
14647 (defun gnus-server-server-name ()
14648   (let ((server (get-text-property (gnus-point-at-bol) 'gnus-server)))
14649     (and server (symbol-name server))))
14650
14651 (defalias 'gnus-server-position-point 'gnus-goto-colon)
14652
14653 (defconst gnus-server-edit-buffer "*Gnus edit server*")
14654
14655 (defun gnus-server-update-server (server)
14656   (save-excursion
14657     (set-buffer gnus-server-buffer)
14658     (let ((buffer-read-only nil)
14659           (info (cdr (assoc server gnus-server-alist))))
14660       (gnus-dribble-enter 
14661        (concat "(gnus-server-set-info \"" server "\" '"
14662                (prin1-to-string info) ")"))
14663       ;; Buffer may be narrowed.
14664       (save-restriction
14665         (widen)
14666         (if (gnus-server-goto-server server)
14667             (delete-region (progn (beginning-of-line) (point))
14668                            (progn (forward-line 1) (point))))
14669         (let ((entry (assoc server gnus-server-alist)))
14670           (gnus-server-insert-server-line nil (car entry) (cdr entry))
14671           (gnus-server-position-point))))))
14672
14673 (defun gnus-server-set-info (server info)
14674   ;; Enter a select method into the virtual server alist.
14675   (gnus-dribble-enter 
14676    (concat "(gnus-server-set-info \"" server "\" '"
14677            (prin1-to-string info) ")"))
14678   (let* ((server (nth 1 info))
14679          (entry (assoc server gnus-server-alist)))
14680     (if entry (setcdr entry info)
14681       (setq gnus-server-alist
14682             (nconc gnus-server-alist (list (cons server info)))))))
14683
14684 (defun gnus-server-to-method (server)
14685   ;; Map virtual server names to select methods.
14686   (or (and (equal server "native") gnus-select-method)
14687       (cdr (assoc server gnus-server-alist))))
14688
14689 (defun gnus-server-extend-method (group method)
14690   ;; This function "extends" a virtual server.  If the server is
14691   ;; "hello", and the select method is ("hello" (my-var "something")) 
14692   ;; in the group "alt.alt", this will result in a new virtual server
14693   ;; called "helly+alt.alt".
14694   (let ((entry
14695          (gnus-copy-sequence 
14696           (if (equal (car method) "native") gnus-select-method
14697             (cdr (assoc (car method) gnus-server-alist))))))
14698     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
14699     (nconc entry (cdr method))))
14700
14701 (defun gnus-server-get-method (group method)
14702   ;; Input either a server name, and extended server name, or a
14703   ;; select method, and return a select method. 
14704   (cond ((stringp method)
14705          (gnus-server-to-method method))
14706         ((and (stringp (car method)) group)
14707          (gnus-server-extend-method group method))
14708         (t
14709          (gnus-server-add-address method))))
14710
14711 (defun gnus-server-add-address (method)
14712   (let ((method-name (symbol-name (car method))))
14713     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
14714              (not (assq (intern (concat method-name "-address")) method)))
14715         (append method (list (list (intern (concat method-name "-address"))
14716                                    (nth 1 method))))
14717       method)))
14718
14719 (defun gnus-server-equal (s1 s2)
14720   (or (equal s1 s2)
14721       (and (= (length s1) (length s2))
14722            (progn
14723              (while (and s1 (member (car s1) s2))
14724                (setq s1 (cdr s1)))
14725              (null s1)))))
14726
14727 ;;; Interactive server functions.
14728
14729 (defun gnus-server-kill-server (server)
14730   "Kill the server on the current line."
14731   (interactive (list (gnus-server-server-name)))
14732   (or (gnus-server-goto-server server)
14733       (if server (error "No such server: %s" server)
14734         (error "No server on the current line")))
14735   (gnus-dribble-enter "")
14736   (let ((buffer-read-only nil))
14737     (delete-region (progn (beginning-of-line) (point))
14738                    (progn (forward-line 1) (point))))
14739   (setq gnus-server-killed-servers 
14740         (cons (assoc server gnus-server-alist) gnus-server-killed-servers))
14741   (setq gnus-server-alist (delq (car gnus-server-killed-servers)
14742                                 gnus-server-alist))
14743   (gnus-server-position-point))
14744
14745 (defun gnus-server-yank-server ()
14746   "Yank the previously killed server."
14747   (interactive)
14748   (or gnus-server-killed-servers
14749       (error "No killed servers to be yanked"))
14750   (let ((alist gnus-server-alist)
14751         (server (gnus-server-server-name))
14752         (killed (car gnus-server-killed-servers)))
14753     (if (not server) 
14754         (setq gnus-server-alist (nconc gnus-server-alist (list killed)))
14755       (if (string= server (car (car gnus-server-alist)))
14756           (setq gnus-server-alist (cons killed gnus-server-alist))
14757         (while (and (cdr alist)
14758                     (not (string= server (car (car (cdr alist))))))
14759           (setq alist (cdr alist)))
14760         (setcdr alist (cons killed (cdr alist)))))
14761     (gnus-server-update-server (car killed))
14762     (setq gnus-server-killed-servers (cdr gnus-server-killed-servers))
14763     (gnus-server-position-point)))
14764
14765 (defun gnus-server-exit ()
14766   "Return to the group buffer."
14767   (interactive)
14768   (kill-buffer (current-buffer))
14769   (switch-to-buffer gnus-group-buffer))
14770
14771 (defun gnus-server-list-servers ()
14772   "List all available servers."
14773   (interactive)
14774   (let ((cur (gnus-server-server-name)))
14775     (gnus-server-prepare)
14776     (if cur (gnus-server-goto-server cur)
14777       (goto-char (point-max))
14778       (forward-line -1))
14779     (gnus-server-position-point)))
14780
14781 (defun gnus-opened-servers-remove (method)
14782   "Remove METHOD from the list of opened servers."
14783   (setq gnus-opened-servers (delq (assoc method gnus-opened-servers)
14784                                   gnus-opened-servers)))
14785
14786 (defun gnus-server-open-server (server)
14787   "Force an open of SERVER."
14788   (interactive (list (gnus-server-server-name)))
14789   (let ((method (gnus-server-to-method server)))
14790     (or method (error "No such server: %s" server))
14791     (gnus-opened-servers-remove method)
14792     (prog1
14793         (or (gnus-open-server method)
14794             (progn (message "Couldn't open %s" server) nil))
14795       (gnus-server-update-server server)
14796       (gnus-server-position-point))))
14797
14798 (defun gnus-server-close-server (server)
14799   "Close SERVER."
14800   (interactive (list (gnus-server-server-name)))
14801   (let ((method (gnus-server-to-method server)))
14802     (or method (error "No such server: %s" server))
14803     (gnus-opened-servers-remove method)
14804     (prog1
14805         (gnus-close-server method)
14806       (gnus-server-update-server server)
14807       (gnus-server-position-point))))
14808
14809 (defun gnus-server-deny-server (server)
14810   "Make sure SERVER will never be attempted opened."
14811   (interactive (list (gnus-server-server-name)))
14812   (let ((method (gnus-server-to-method server)))
14813     (or method (error "No such server: %s" server))
14814     (gnus-opened-servers-remove method)
14815     (setq gnus-opened-servers
14816           (cons (list method 'denied) gnus-opened-servers)))
14817   (gnus-server-update-server server)
14818   (gnus-server-position-point))
14819
14820 (defun gnus-server-remove-denials ()
14821   "Remove all marks as to whether Gnus could open servers or not."
14822   (interactive)
14823   (setq gnus-opened-servers nil)
14824   (gnus-server-list-servers))
14825
14826 (defun gnus-server-copy-server (from to)
14827   (interactive
14828    (list
14829     (or (gnus-server-server-name)
14830         (error "No server on the current line"))
14831     (read-string "Copy to: ")))
14832   (or from (error "No server on current line"))
14833   (or (and to (not (string= to ""))) (error "No name to copy to"))
14834   (and (assoc to gnus-server-alist) (error "%s already exists" to))
14835   (or (assoc from gnus-server-alist) 
14836       (error "%s: no such server" from))
14837   (let ((to-entry (gnus-copy-sequence (assoc from gnus-server-alist))))
14838     (setcar to-entry to)
14839     (setcar (nthcdr 2 to-entry) to)
14840     (setq gnus-server-killed-servers 
14841           (cons to-entry gnus-server-killed-servers))
14842     (gnus-server-yank-server)))
14843
14844 (defun gnus-server-add-server (how where)
14845   (interactive 
14846    (list (intern (completing-read "Server method: "
14847                                   gnus-valid-select-methods nil t))
14848          (read-string "Server name: ")))
14849   (setq gnus-server-killed-servers 
14850         (cons (list where how where) gnus-server-killed-servers))
14851   (gnus-server-yank-server))
14852
14853 (defun gnus-server-goto-server (server)
14854   "Jump to a server line."
14855   (interactive
14856    (list (completing-read "Goto server: " gnus-server-alist nil t)))
14857   (let ((to (text-property-any (point-min) (point-max) 
14858                                'gnus-server (intern server))))
14859     (and to
14860          (progn
14861            (goto-char to) 
14862            (gnus-server-position-point)))))
14863
14864 (defun gnus-server-edit-server (server)
14865   "Edit the server on the current line."
14866   (interactive (list (gnus-server-server-name)))
14867   (or server
14868       (error "No server on current line"))
14869   (let ((winconf (current-window-configuration)))
14870     (get-buffer-create gnus-server-edit-buffer)
14871     (gnus-configure-windows 'edit-server)
14872     (gnus-add-current-to-buffer-list)
14873     (emacs-lisp-mode)
14874     (make-local-variable 'gnus-prev-winconf)
14875     (setq gnus-prev-winconf winconf)
14876     (use-local-map (copy-keymap (current-local-map)))
14877     (let ((done-func '(lambda () 
14878                         "Exit editing mode and update the information."
14879                         (interactive)
14880                         (gnus-server-edit-server-done 'group))))
14881       (setcar (cdr (nth 4 done-func)) server)
14882       (local-set-key "\C-c\C-c" done-func))
14883     (erase-buffer)
14884     (insert ";; Type `C-c C-c' after you have edited the server.\n\n")
14885     (insert (pp-to-string (cdr (assoc server gnus-server-alist))))))
14886
14887 (defun gnus-server-edit-server-done (server)
14888   (interactive)
14889   (set-buffer (get-buffer-create gnus-server-edit-buffer))
14890   (goto-char (point-min))
14891   (let ((form (read (current-buffer)))
14892         (winconf gnus-prev-winconf))
14893     (gnus-server-set-info server form)
14894     (kill-buffer (current-buffer))
14895     (and winconf (set-window-configuration winconf))
14896     (set-buffer gnus-server-buffer)
14897     (gnus-server-update-server (gnus-server-server-name))
14898     (gnus-server-list-servers)
14899     (gnus-server-position-point)))
14900
14901 (defun gnus-server-read-server (server)
14902   "Browse a server."
14903   (interactive (list (gnus-server-server-name)))
14904   (gnus-browse-foreign-server (gnus-server-to-method server) (current-buffer)))
14905
14906 (defun gnus-mouse-pick-server (e)
14907   (interactive "e")
14908   (mouse-set-point e)
14909   (gnus-server-read-server (gnus-server-server-name)))
14910
14911 ;;;
14912 ;;; entry points into gnus-score.el
14913 ;;;
14914
14915 ;;; Finding score files. 
14916
14917 (defvar gnus-global-score-files nil
14918   "*List of global score files and directories.
14919 Set this variable if you want to use people's score files.  One entry
14920 for each score file or each score file directory.  Gnus will decide
14921 by itself what score files are applicable to which group.
14922
14923 Say you want to use the single score file
14924 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
14925 score files in the \"/ftp.some-where:/pub/score\" directory.
14926
14927  (setq gnus-global-score-files
14928        '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
14929          \"/ftp.some-where:/pub/score\"))")
14930
14931 (defun gnus-score-score-files (group)
14932   "Return a list of all possible score files."
14933   ;; Search and set any global score files.
14934   (and gnus-global-score-files 
14935        (or gnus-internal-global-score-files
14936            (gnus-score-search-global-directories gnus-global-score-files)))
14937   ;; Fix the kill-file dir variable.
14938   (setq gnus-kill-files-directory 
14939         (file-name-as-directory
14940          (or gnus-kill-files-directory "~/News/")))
14941   ;; If we can't read it, there are no score files.
14942   (if (not (file-exists-p (expand-file-name gnus-kill-files-directory)))
14943       (setq gnus-score-file-list nil)
14944     (if (gnus-use-long-file-name 'not-score)
14945         ;; We want long file names.
14946         (if (or (not gnus-score-file-list)
14947                 (not (car gnus-score-file-list))
14948                 (gnus-file-newer-than gnus-kill-files-directory
14949                                       (car gnus-score-file-list)))
14950             (setq gnus-score-file-list 
14951                   (cons (nth 5 (file-attributes gnus-kill-files-directory))
14952                         (nreverse 
14953                          (directory-files 
14954                           gnus-kill-files-directory t 
14955                           (gnus-score-file-regexp))))))
14956       ;; We do not use long file names, so we have to do some
14957       ;; directory traversing.  
14958       (let ((mdir (length (expand-file-name gnus-kill-files-directory)))
14959             (suffixes (list gnus-score-file-suffix gnus-adaptive-file-suffix))
14960             dir files suffix)
14961         (while suffixes
14962           (setq dir (expand-file-name
14963                      (concat gnus-kill-files-directory
14964                              (gnus-replace-chars-in-string group ?. ?/))))
14965           (setq dir (gnus-replace-chars-in-string dir ?: ?/))
14966           (setq suffix (car suffixes)
14967                 suffixes (cdr suffixes))
14968           (if (file-exists-p (concat dir "/" suffix))
14969               (setq files (cons (concat dir "/" suffix) files)))
14970           (while (>= (1+ (length dir)) mdir)
14971             (and (file-exists-p (concat dir "/all/" suffix))
14972                  (setq files (cons (concat dir "/all/" suffix) files)))
14973             (string-match "/[^/]*$" dir)
14974             (setq dir (substring dir 0 (match-beginning 0)))))
14975         (setq gnus-score-file-list 
14976               (cons nil (nreverse files)))))
14977     (cdr gnus-score-file-list)))
14978
14979 (defun gnus-score-file-regexp ()
14980   (concat "\\(" gnus-score-file-suffix 
14981           "\\|" gnus-adaptive-file-suffix "\\)$"))
14982         
14983 (defun gnus-score-find-bnews (group)
14984   "Return a list of score files for GROUP.
14985 The score files are those files in the ~/News directory which matches
14986 GROUP using BNews sys file syntax."
14987   (let* ((sfiles (append (gnus-score-score-files group)
14988                          gnus-internal-global-score-files))
14989          (kill-dir (file-name-as-directory 
14990                     (expand-file-name gnus-kill-files-directory)))
14991          (klen (length kill-dir))
14992          ofiles not-match regexp)
14993     (save-excursion
14994       (set-buffer (get-buffer-create "*gnus score files*"))
14995       (buffer-disable-undo (current-buffer))
14996       ;; Go through all score file names and create regexp with them
14997       ;; as the source.  
14998       (while sfiles
14999         (erase-buffer)
15000         (insert (car sfiles))
15001         (goto-char (point-min))
15002         ;; First remove the suffix itself.
15003         (re-search-forward (concat "." (gnus-score-file-regexp)))
15004         (replace-match "" t t) 
15005         (goto-char (point-min))
15006         (if (looking-at (regexp-quote kill-dir))
15007             ;; If the file name was just "SCORE", `klen' is one character
15008             ;; too much.
15009             (delete-char (min (1- (point-max)) klen))
15010           (goto-char (point-max))
15011           (search-backward "/")
15012           (delete-region (1+ (point)) (point-min)))
15013         ;; If short file names were used, we have to translate slashes.
15014         (goto-char (point-min))
15015         (while (re-search-forward "[/:]" nil t)
15016           (replace-match "." t t))
15017         ;; Cludge to get rid of "nntp+" problems.
15018         (goto-char (point-min))
15019         (and (looking-at "nn[a-z]+\\+")
15020              (progn
15021                (search-forward "+")
15022                (forward-char -1)
15023                (insert "\\")))
15024         ;; Translate "all" to ".*".
15025         (while (search-forward "all" nil t)
15026           (replace-match ".*" t t))
15027         (goto-char (point-min))
15028         ;; Deal with "not."s.
15029         (if (looking-at "not.")
15030             (progn
15031               (setq not-match t)
15032               (setq regexp (buffer-substring 5 (point-max))))
15033           (setq regexp (buffer-substring 1 (point-max)))
15034           (setq not-match nil))
15035         ;; Finally - if this resulting regexp matches the group name,
15036         ;; we add this score file to the list of score files
15037         ;; applicable to this group.
15038         (if (or (and not-match
15039                      (not (string-match regexp group)))
15040                 (and (not not-match)
15041                      (string-match regexp group)))
15042             (setq ofiles (cons (car sfiles) ofiles)))
15043         (setq sfiles (cdr sfiles)))
15044       (kill-buffer (current-buffer))
15045       ;; Slight kludge here - the last score file returned should be
15046       ;; the local score file, whether it exists or not. This is so
15047       ;; that any score commands the user enters will go to the right
15048       ;; file, and not end up in some global score file.
15049       (let ((localscore (gnus-score-file-name group)))
15050         (setq ofiles (cons localscore (delete localscore ofiles))))
15051       (nreverse ofiles))))
15052
15053 (defun gnus-score-find-single (group)
15054   "Return list containing the score file for GROUP."
15055   (list (gnus-score-file-name group gnus-adaptive-file-suffix)
15056         (gnus-score-file-name group)))
15057
15058 (defun gnus-score-find-hierarchical (group)
15059   "Return list of score files for GROUP.
15060 This includes the score file for the group and all its parents."
15061   (let ((all (copy-sequence '(nil)))
15062         (start 0))
15063     (while (string-match "\\." group (1+ start))
15064       (setq start (match-beginning 0))
15065       (setq all (cons (substring group 0 start) all)))
15066     (setq all (cons group all))
15067     (nconc
15068      (mapcar (lambda (newsgroup)
15069                (gnus-score-file-name newsgroup gnus-adaptive-file-suffix))
15070              (setq all (nreverse all)))
15071      (mapcar 'gnus-score-file-name all))))
15072
15073 (defvar gnus-score-file-alist-cache nil)
15074
15075 (defun gnus-score-find-alist (group)
15076   "Return list of score files for GROUP.
15077 The list is determined from the variable gnus-score-file-alist."
15078   (let ((alist gnus-score-file-multiple-match-alist)
15079         score-files)
15080     ;; if this group has been seen before, return the cached entry
15081     (if (setq score-files (assoc group gnus-score-file-alist-cache))
15082         (cdr score-files)               ;ensures caching groups with no matches
15083       ;; handle the multiple match alist
15084       (while alist
15085         (and (string-match (car (car alist)) group)
15086              (setq score-files
15087                    (nconc score-files (copy-sequence (cdr (car alist))))))
15088         (setq alist (cdr alist)))
15089       (setq alist gnus-score-file-single-match-alist)
15090       ;; handle the single match alist
15091       (while alist
15092         (and (string-match (car (car alist)) group)
15093              ;; progn used just in case ("regexp") has no files
15094              ;; and score-files is still nil. -sj
15095              ;; this can be construed as a "stop searching here" feature :>
15096              ;; and used to simplify regexps in the single-alist 
15097              (progn
15098                (setq score-files
15099                      (nconc score-files (copy-sequence (cdr (car alist)))))
15100                (setq alist nil)))
15101         (setq alist (cdr alist)))
15102       ;; cache the score files
15103       (setq gnus-score-file-alist-cache
15104             (cons (cons group score-files) gnus-score-file-alist-cache))
15105       score-files)))
15106
15107 (defun gnus-possibly-score-headers (&optional trace)
15108   (let ((func gnus-score-find-score-files-function)
15109         score-files)
15110     (and func 
15111          (not (listp func))
15112          (setq func (list func)))
15113     ;; Go through all the functions for finding score files (or actual
15114     ;; scores) and add them to a list.
15115     (and func (setq score-files (gnus-score-find-alist gnus-newsgroup-name)))
15116     (while func
15117       (and (symbolp (car func))
15118            (fboundp (car func))
15119            (setq score-files 
15120                  (nconc score-files (funcall (car func) gnus-newsgroup-name))))
15121       (setq func (cdr func)))
15122     (if score-files (gnus-score-headers score-files trace))))
15123
15124 (defun gnus-score-file-name (newsgroup &optional suffix)
15125   "Return the name of a score file for NEWSGROUP."
15126   (let ((suffix (or suffix gnus-score-file-suffix)))
15127     (cond 
15128      ((or (null newsgroup)
15129           (string-equal newsgroup ""))
15130       ;; The global score file is placed at top of the directory.
15131       (expand-file-name 
15132        suffix (or gnus-kill-files-directory "~/News")))
15133      ((gnus-use-long-file-name 'not-score)
15134       ;; Append ".SCORE" to newsgroup name.
15135       (expand-file-name (concat (gnus-newsgroup-saveable-name newsgroup)
15136                                 "." suffix)
15137                         (or gnus-kill-files-directory "~/News")))
15138      (t
15139       ;; Place "SCORE" under the hierarchical directory.
15140       (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
15141                                 "/" suffix)
15142                         (or gnus-kill-files-directory "~/News"))))))
15143
15144 (defun gnus-score-search-global-directories (files)
15145   "Scan all global score directories for score files."
15146   ;; Set the variable `gnus-internal-global-score-files' to all
15147   ;; available global score files.
15148   (interactive (list gnus-global-score-files))
15149   (let (out)
15150     (while files
15151       (if (string-match "/$" (car files))
15152           (setq out (nconc (directory-files 
15153                             (car files) t
15154                             (concat (gnus-score-file-regexp) "$"))))
15155         (setq out (cons (car files) out)))
15156       (setq files (cdr files)))
15157     (setq gnus-internal-global-score-files out)))
15158
15159 ;;;
15160 ;;; Buffering of read articles.
15161 ;;;
15162
15163 (defvar gnus-backlog-buffer " *Gnus Backlog*")
15164 (defvar gnus-backlog-articles nil)
15165
15166 (defun gnus-backlog-buffer ()
15167   (or (get-buffer gnus-backlog-buffer)
15168       (save-excursion
15169         (set-buffer (get-buffer-create gnus-backlog-buffer))
15170         (buffer-disable-undo (current-buffer))
15171         (setq buffer-read-only t)
15172         (gnus-add-current-to-buffer-list))))
15173
15174 (defun gnus-backlog-enter-article (group number buffer)
15175   (let ((ident (intern (concat group ":" (int-to-string number))))
15176         b)
15177     (if (memq ident gnus-backlog-articles)
15178         () ; It's already kept.
15179       ;; Remove the oldest article, if necessary.
15180       (and (numberp gnus-keep-backlog)
15181            (>= (length gnus-backlog-articles) gnus-keep-backlog)
15182            (gnus-backlog-remove-oldest-article))
15183       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
15184       ;; Insert the new article.
15185       (save-excursion
15186         (set-buffer (gnus-backlog-buffer))
15187         (let (buffer-read-only)
15188           (goto-char (point-max))
15189           (or (bolp) (insert "\n"))
15190           (setq b (point))
15191           (insert-buffer-substring buffer)
15192           ;; Tag the beginning of the article with the ident.
15193           (put-text-property b (1+ b) 'gnus-backlog ident))))))
15194
15195 (defun gnus-backlog-remove-oldest-article ()
15196   (save-excursion
15197     (set-buffer (gnus-backlog-buffer))
15198     (goto-char (point-min))
15199     (if (zerop (buffer-size))
15200         () ; The buffer is empty.
15201       (let ((ident (get-text-property (point) 'gnus-backlog))
15202             buffer-read-only)
15203         ;; Remove the ident from the list of articles.
15204         (and ident
15205              (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
15206         ;; Delete the article itself.
15207         (delete-region 
15208          (point) (next-single-property-change
15209                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
15210
15211 (defun gnus-backlog-request-article (group number buffer)
15212   (let ((ident (intern (concat group ":" (int-to-string number))))
15213         beg end)
15214     (if (not (memq ident gnus-backlog-articles))
15215         () ; It wasn't in the backlog.
15216       (save-excursion
15217         (set-buffer (gnus-backlog-buffer))
15218         (if (not (setq beg (text-property-any 
15219                             (point-min) (point-max) 'gnus-backlog
15220                             ident)))
15221             ;; It wasn't in the backlog after all.
15222             (progn
15223               (setq gnus-backlog-articles (delq ident gnus-backlog-articles))
15224               nil)
15225           ;; Find the end (i. e., the beginning of the next article).
15226           (setq end
15227                 (next-single-property-change 
15228                  (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
15229       (erase-buffer)
15230       (insert-buffer-substring gnus-backlog-buffer beg end))))
15231                                               
15232
15233 ;; Allow redefinition of Gnus functions.
15234
15235 (gnus-ems-redefine)
15236
15237 (provide 'gnus)
15238
15239 ;;; gnus.el ends here