*** 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 (ding) Gnus looks suspiciously like GNUS, it isn't quite
27 ;; the same beast. Most internal structures have been changed. If you
28 ;; have 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 (require 'mail-utils)
37 (require 'timezone)
38 (require 'nnheader)
39
40 ;; Site dependent variables. These variables should be defined in
41 ;; paths.el.
42
43 (defvar gnus-default-nntp-server nil
44   "Specify a default NNTP server.
45 This variable should be defined in paths.el, and should never be set
46 by the user.
47 If you want to change servers, you should use `gnus-select-method'.
48 See the documentation to that variable.")
49
50 (defconst gnus-backup-default-subscribed-newsgroups 
51   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
52   "Default default new newsgroups the first time Gnus is run.
53 Should be set in paths.el, and shouldn't be touched by the user.")
54
55 (defvar gnus-local-domain nil
56   "Local domain name without a host name.
57 The DOMAINNAME environment variable is used instead if it is defined.
58 If the `system-name' function returns the full Internet name, there is
59 no need to set this variable.")
60
61 (defvar gnus-local-organization nil
62   "String with a description of what organization (if any) the user belongs to.
63 The ORGANIZATION environment variable is used instead if it is defined.
64 If this variable contains a function, this function will be called
65 with the current newsgroup name as the argument. The function should
66 return a string.
67
68 In any case, if the string (either in the variable, in the environment
69 variable, or returned by the function) is a file name, the contents of
70 this file will be used as the organization.")
71
72 (defvar gnus-use-generic-from nil
73   "If nil, the full host name will be the system name prepended to the domain name.
74 If this is a string, the full host name will be this string.
75 If this is non-nil, non-string, the domain name will be used as the
76 full host name.")
77
78 (defvar gnus-use-generic-path nil
79   "If nil, use the NNTP server name in the Path header.
80 If stringp, use this; if non-nil, use no host name (user name only).")
81
82
83 ;; Customization variables
84
85 (defvar gnus-select-method 
86   (list 'nntp (or (getenv "NNTPSERVER") 
87                   (if (and gnus-default-nntp-server
88                            (not (string= gnus-default-nntp-server "")))
89                       gnus-default-nntp-server)
90                   (system-name)))
91   "*Default method for selecting a newsgroup.
92 This variable should be a list, where the first element is how the
93 news is to be fetched, the second is the address. 
94
95 For instance, if you want to get your news via NNTP from
96 \"flab.flab.edu\", you could say:
97
98 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
99
100 If you want to use your local spool, say:
101
102 (setq gnus-select-method (list 'nnspool (system-name)))
103
104 If you use this variable, you must set `gnus-nntp-server' to nil.
105
106 There is a lot more to know about select methods and virtual servers -
107 see the manual for details.")
108
109 ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
110 (defvar gnus-post-method nil
111   "*Preferred method for posting USENET news.
112 If this variable is nil, Gnus will use the current method to decide
113 which method to use when posting.  If it is non-nil, it will override
114 the current method.  This method will not be used in mail groups and
115 the like, only in \"real\" newsgroups.
116
117 The value must be a valid method as discussed in the documentation of
118 `gnus-select-method'.")
119
120 (defvar gnus-refer-article-method nil
121   "*Preferred method for fetching an article by Message-ID.
122 If you are reading news from the local spool (with nnspool), fetching
123 articles by Message-ID is painfully slow. By setting this method to an
124 nntp method, you might get acceptable results.
125
126 The value of this variable must be a valid select method as discussed
127 in the documentation of `gnus-select-method'")
128
129 (defvar gnus-secondary-select-methods nil
130   "*A list of secondary methods that will be used for reading news.
131 This is a list where each element is a complete select methdod (see
132 `gnus-select-method').  
133
134 If, for instance, you want to read your mail with the nnml backend,
135 you could set this variable:
136
137 (setq gnus-secondary-select-methods '((nnml \"\"))")
138
139 (defvar gnus-secondary-servers nil
140   "*List of NNTP servers that the user can choose between interactively.
141 To make Gnus query you for a server, you have to give `gnus' a
142 non-numeric prefix - `C-u M-x gnus', in short.")
143
144 (defvar gnus-nntp-server nil
145   "*The name of the host running the NNTP server.
146 This variable is semi-obsolete. Use the `gnus-select-method'
147 variable instead.")
148
149 (defvar gnus-nntp-service "nntp"
150   "*NNTP service name (\"nntp\" or 119).
151 This is an obsolete variable, which is scarcely used. If you use an
152 nntp server for your newsgroup and want to change the port number
153 used to 899, you would say something along these lines:
154
155  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
156
157 (defvar gnus-startup-file "~/.newsrc"
158   "*Your `.newsrc' file.
159 `.newsrc-SERVER' will be used instead if that exists.")
160
161 (defvar gnus-init-file "~/.gnus"
162   "*Your Gnus elisp startup file.
163 If a file with the .el or .elc suffixes exist, it will be read
164 instead.") 
165
166 (defvar gnus-group-faq-directory
167   "/anonymous@rtfm.mit.edu:/pub/usenet-by-group/"
168   "*Directory where the group FAQs are stored.
169 This will most commonly be on a remote machine, and the file will be
170 fetched by ange-ftp.")
171
172 (defvar gnus-group-archive-directory
173   "/ftp@sina.tcamc.uh.edu:/pub/emacs/ding-list/" 
174   "*The address of the (ding) archives.")
175
176 (defvar gnus-default-subscribed-newsgroups nil
177   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
178 It should be a list of strings.
179 If it is `t', Gnus will not do anything special the first time it is
180 started; it'll just use the normal newsgroups subscription methods.")
181
182 (defvar gnus-use-cross-reference t
183   "*Non-nil means that cross referenced articles will be marked as read.
184 If nil, ignore cross references.  If t, mark articles as read in
185 subscribed newsgroups. If neither t nor nil, mark as read in all
186 newsgroups.") 
187
188 (defvar gnus-use-dribble-file t
189   "*Non-nil means that Gnus will use a dribble file to store user updates.
190 If Emacs should crash without saving the .newsrc files, complete
191 information can be restored from the dribble file.")
192
193 (defvar gnus-asynchronous nil
194   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
195
196 (defvar gnus-asynchronous-article-function nil
197   "*Function for picking articles to pre-fetch, possibly.")
198
199 (defvar gnus-score-file-single-match-alist nil
200   "*Alist mapping regexps to lists of score files.
201 Each element of this alist should be of the form
202         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
203
204 If the name of a group is matched by REGEXP, the corresponding scorefiles
205 will be used for that group.
206 The first match found is used, subsequent matching entries are ignored (to
207 use mutliple matches, see gnus-score-file-multiple-match-alist).
208
209 These score files are loaded in addition to any files returned by
210 gnus-score-find-score-files-function (which see).")
211
212 (defvar gnus-score-file-multiple-match-alist nil
213   "*Alist mapping regexps to lists of score files.
214 Each element of this alist should be of the form
215         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
216
217 If the name of a group is matched by REGEXP, the corresponding scorefiles
218 will be used for that group.
219 If multiple REGEXPs match a group, the score files corresponding to each
220 match will be used (for only one match to be used, see
221 gnus-score-file-single-match-alist).
222
223 These score files are loaded in addition to any files returned by
224 gnus-score-find-score-files-function (which see).")
225
226
227 (defvar gnus-score-file-suffix "SCORE"
228   "*Suffix of the score files.")
229
230 (defvar gnus-adaptive-file-suffix "ADAPT"
231   "*Suffix of the adaptive score files.")
232
233 (defvar gnus-score-find-score-files-function 'gnus-score-find-bnews
234   "*Function used to find SCORE files.
235 The function will be called with the group name as the argument, and
236 should return a list of score files to apply to that group.  The score
237 files do not actually have to exist.
238
239 Predefined values are:
240
241 gnus-score-find-single: Only apply the group's own SCORE file.
242 gnus-score-find-hierarchical: Also apply SCORE files from parent groups.
243 gnus-score-find-bnews: Apply SCORE files whose names matches.
244
245 See the documentation to these functions for more information.
246
247 This variable can also be a list of functions to be called.  Each
248 function should either return a list of score files, or a list of
249 score alists.")
250
251 (defvar gnus-score-interactive-default-score 1000
252   "*Scoring commands will raise/lower the score with this number as the default.")
253
254 (defvar gnus-large-newsgroup 200
255   "*The number of articles which indicates a large newsgroup.
256 If the number of articles in a newsgroup is greater than this value,
257 confirmation is required for selecting the newsgroup.")
258
259 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
260 (defvar gnus-no-groups-message "No news is horrible news"
261   "*Message displayed by Gnus when no groups are available.")
262
263 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
264   "*Non-nil means that the default name of a file to save articles in is the group name.
265 If it's nil, the directory form of the group name is used instead.
266
267 If this variable is a list, and the list contains the element
268 `not-score', long file names will not be used for score files; if it
269 contains the element `not-save', long file names will not be used for
270 saving; and if it contains the element `not-kill', long file names
271 will not be used for kill files.")
272
273 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
274   "*Name of the directory articles will be saved in (default \"~/News\").
275 Initialized from the SAVEDIR environment variable.")
276
277 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
278   "*Name of the directory where kill files will be stored (default \"~/News\").
279 Initialized from the SAVEDIR environment variable.")
280
281 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
282   "*A function to save articles in your favorite format.
283 The function must be interactively callable (in other words, it must
284 be an Emacs command).
285
286 Gnus provides the following functions:
287
288 * gnus-summary-save-in-rmail (Rmail format)
289 * gnus-summary-save-in-mail (Unix mail format)
290 * gnus-summary-save-in-folder (MH folder)
291 * gnus-summary-save-in-file (article format).
292 * gnus-summary-save-in-vm (use VM's folder format).")
293
294 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
295   "*A function generating a file name to save articles in Rmail format.
296 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
297
298 (defvar gnus-mail-save-name (function gnus-plain-save-name)
299   "*A function generating a file name to save articles in Unix mail format.
300 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
301
302 (defvar gnus-folder-save-name (function gnus-folder-save-name)
303   "*A function generating a file name to save articles in MH folder.
304 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
305
306 (defvar gnus-file-save-name (function gnus-numeric-save-name)
307   "*A function generating a file name to save articles in article format.
308 The function is called with NEWSGROUP, HEADERS, and optional
309 LAST-FILE.")
310
311 (defvar gnus-split-methods nil
312   "*Variable used to suggest where articles are to be saved.
313 The syntax of this variable is the same as `nnmail-split-methods'.  
314
315 For instance, if you would like to save articles related to Gnus in
316 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
317 you could set this variable to something like:
318
319  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
320    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))")
321
322 (defvar gnus-save-score nil
323   "*If non-nil, save group scoring info.")
324
325 (defvar gnus-use-adaptive-scoring nil
326   "*If non-nil, use some adaptive scoring scheme.")
327
328 (defvar gnus-use-cache nil
329   "*If non-nil, Gnus will cache (some) articles locally.")
330
331 (defvar gnus-use-scoring t
332   "*If non-nil, enable scoring.")
333
334 (defvar gnus-fetch-old-headers nil
335   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
336 If an unread article in the group refers to an older, already read (or
337 just marked as read) article, the old article will not normally be
338 displayed in the Summary buffer.  If this variable is non-nil, Gnus
339 will attempt to grab the headers to the old articles, and thereby
340 build complete threads.  If it has the value `some', only enough
341 headers to connect otherwise loose threads will be displayed.
342
343 The server has to support XOVER for any of this to work.")
344
345 (defvar gnus-visual t
346   "*If non-nil, will do various highlighting.
347 If nil, no mouse highlights (or any other highlights) will be
348 performed.  This might speed up Gnus some when generating large group
349 and summary buffers.")
350
351 (defvar gnus-novice-user t
352   "*Non-nil means that you are a usenet novice.
353 If non-nil, verbose messages may be displayed and confirmations may be
354 required.")
355
356 (defvar gnus-expert-user nil
357   "*Non-nil means that you will never be asked for confirmation about anything.
358 And that means *anything*.")
359
360 (defvar gnus-verbose 7
361   "*Integer that says how verbose Gnus should be.
362 The higher the number, the more messages Gnus will flash to say what
363 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
364 display most important messages; and at ten, Gnus will keep on
365 jabbering all the time.")
366
367 (defvar gnus-keep-same-level nil
368   "*Non-nil means that the next newsgroup after the current will be on the same level.
369 When you type, for instance, `n' after reading the last article in the
370 current newsgroup, you will go to the next newsgroup. If this variable
371 is nil, the next newsgroup will be the next from the group
372 buffer. 
373 If this variable is non-nil, Gnus will either put you in the
374 next newsgroup with the same level, or, if no such newsgroup is
375 available, the next newsgroup with the lowest possible level higher
376 than the current level.
377 If this variable is `best', Gnus will make the next newsgroup the one
378 with the best level.")
379
380 (defvar gnus-summary-make-false-root 'adopt
381   "*nil means that Gnus won't gather loose threads.
382 If the root of a thread has expired or been read in a previous
383 session, the information necessary to build a complete thread has been
384 lost. Instead of having many small sub-threads from this original thread
385 scattered all over the summary buffer, Gnus can gather them. 
386
387 If non-nil, Gnus will try to gather all loose sub-threads from an
388 original thread into one large thread.
389
390 If this variable is non-nil, it should be one of `none', `adopt',
391 `dummy' or `empty'.
392
393 If this variable is `none', Gnus will not make a false root, but just
394 present the sub-threads after another.
395 If this variable is `dummy', Gnus will create a dummy root that will
396 have all the sub-threads as children.
397 If this variable is `adopt', Gnus will make one of the \"children\"
398 the parent and mark all the step-children as such.
399 If this variable is `empty', the \"children\" are printed with empty
400 subject fields.  (Or rather, they will be printed with a string
401 given by the `gnus-summary-same-subject' variable.)")
402
403 (defvar gnus-summary-gather-subject-limit nil
404   "*Maximum length of subject comparisons when gathering loose threads.
405 Use nil to compare full subjects.  Setting this variable to a low
406 number will help gather threads that have been corrupted by
407 newsreaders chopping off subject lines, but it might also mean that
408 unrelated articles that have subject that happen to begin with the
409 same few characters will be incorrectly gathered.
410
411 If this variable is `fuzzy', Gnus will use a fuzzy algortihm when
412 comparing subjects.")
413
414 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
415 (defvar gnus-summary-same-subject ""
416   "*String indicating that the current article has the same subject as the previous.
417 This variable will only be used if the value of
418 `gnus-summary-make-false-root' is `empty'.")
419
420 (defvar gnus-summary-goto-unread nil
421   "*If non-nil, marking commands will go to the next unread article.")
422
423 (defvar gnus-group-goto-unread t
424   "*If non-nil, movement commands will go to the next unread and subscribed group.")
425
426 (defvar gnus-check-new-newsgroups t
427   "*Non-nil means that Gnus will add new newsgroups at startup.
428 If this variable is `ask-server', Gnus will ask the server for new
429 groups since the last time it checked. This means that the killed list
430 is no longer necessary, so you could set `gnus-save-killed-list' to
431 nil. 
432
433 A variant is to have this variable be a list of select methods. Gnus
434 will then use the `ask-server' method on all these select methods to
435 query for new groups from all those servers.
436
437 Eg.
438   (setq gnus-check-new-newsgroups 
439         '((nntp \"some.server\") (nntp \"other.server\")))
440
441 If this variable is nil, then you have to tell Gnus explicitly to
442 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
443
444 (defvar gnus-check-bogus-newsgroups nil
445   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
446 If this variable is nil, then you have to tell Gnus explicitly to
447 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
448
449 (defvar gnus-read-active-file t
450   "*Non-nil means that Gnus will read the entire active file at startup.
451 If this variable is nil, Gnus will only know about the groups in your
452 `.newsrc' file.
453
454 If this variable is `some', Gnus will try to only read the relevant
455 parts of the active file from the server.  Not all servers support
456 this, and it might be quite slow with other servers, but this should
457 generally be faster than both the t and nil value.
458
459 If you set this variable to nil or `some', you probably still want to
460 be told about new newsgroups that arrive.  To do that, set
461 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
462 properly with all servers.")
463
464 (defvar gnus-level-subscribed 5
465   "*Groups with levels less than or equal to this variable are subscribed.")
466
467 (defvar gnus-level-unsubscribed 7
468   "*Groups with levels less than or equal to this variable are unsubscribed.
469 Groups with levels less than `gnus-level-subscribed', which should be
470 less than this variable, are subscribed.")
471
472 (defvar gnus-level-zombie 8
473   "*Groups with this level are zombie groups.")
474
475 (defvar gnus-level-killed 9
476   "*Groups with this level are killed.")
477
478 (defvar gnus-level-default-subscribed 3
479   "*New subscribed groups will be subscribed at this level.")
480
481 (defvar gnus-level-default-unsubscribed 6
482   "*New unsubscribed groups will be unsubscribed at this level.")
483
484 (defvar gnus-activate-foreign-newsgroups nil
485   "*If nil, Gnus will not check foreign newsgroups at startup.
486 If it is non-nil, it should be a number between one and nine. Foreign
487 newsgroups that have a level lower or equal to this number will be
488 activated on startup. For instance, if you want to active all
489 subscribed newsgroups, but not the rest, you'd set this variable to 
490 `gnus-level-subscribed'.
491
492 If you subscribe to lots of newsgroups from different servers, startup
493 might take a while. By setting this variable to nil, you'll save time,
494 but you won't be told how many unread articles there are in the
495 groups.")
496
497 (defvar gnus-save-newsrc-file t
498   "*Non-nil means that Gnus will save the `.newsrc' file.
499 Gnus always saves its own startup file, which is called
500 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
501 be readily understood by other newsreaders.  If you don't plan on
502 using other newsreaders, set this variable to nil to save some time on
503 exit.")
504
505 (defvar gnus-save-killed-list t
506   "*If non-nil, save the list of killed groups to the startup file.
507 This will save both time (when starting and quitting) and space (both
508 memory and disk), but it will also mean that Gnus has no record of
509 which groups are new and which are old, so the automatic new
510 newsgroups subscription methods become meaningless. You should always
511 set `gnus-check-new-newsgroups' to `ask-server' or nil if you set this
512 variable to nil.")
513
514 (defvar gnus-interactive-catchup t
515   "*If non-nil, require your confirmation when catching up a group.")
516
517 (defvar gnus-interactive-post t
518   "*If non-nil, group name will be asked for when posting.")
519
520 (defvar gnus-interactive-exit t
521   "*If non-nil, require your confirmation when exiting Gnus.")
522
523 (defvar gnus-kill-killed nil
524   "*If non-nil, Gnus will apply kill files to already killed articles.
525 If it is nil, Gnus will never apply kill files to articles that have
526 already been through the scoring process, which might very well save lots
527 of time.")
528
529 (defvar gnus-extract-address-components 'gnus-extract-address-components
530   "*Function for extracting address components from a From header.
531 Two pre-defined function exist: `gnus-extract-address-components',
532 which is the default, quite fast, and too simplistic solution, and
533 `mail-extract-address-components', which works much better, but is
534 slower.")
535
536 (defvar gnus-summary-default-score 0
537   "*Default article score level.
538 If this variable is nil, scoring will be disabled.")
539
540 (defvar gnus-group-default-list-level gnus-level-subscribed
541   "*Default listing level. 
542 Ignored if `gnus-group-use-permanent-levels' is nil.")
543
544 (defvar gnus-group-use-permanent-levels nil
545   "*If non-nil, once you set a level, Gnus will use this level.")
546
547 (defvar gnus-show-mime nil
548   "*If non-ni, do mime processing of articles.
549 The articles will simply be fed to the function given by
550 `gnus-show-mime-method'.")
551
552 (defvar gnus-show-mime-method (function metamail-buffer)
553   "*Function to process a MIME message.
554 The function is called from the article buffer.")
555
556 (defvar gnus-show-threads t
557   "*If non-nil, display threads in summary mode.")
558
559 (defvar gnus-thread-hide-subtree nil
560   "*If non-nil, hide all threads initially.
561 If threads are hidden, you have to run the command
562 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
563 to expose hidden threads.")
564
565 (defvar gnus-thread-hide-killed t
566   "*If non-nil, hide killed threads automatically.")
567
568 (defvar gnus-thread-ignore-subject nil
569   "*If non-nil, ignore subjects and do all threading based on the Reference header.
570 If nil, which is the default, articles that have different subjects
571 from their parents will start separate threads.")
572
573 (defvar gnus-thread-indent-level 4
574   "*Number that says how much each sub-thread should be indented.")
575
576 (defvar gnus-ignored-newsgroups ""
577   "*A regexp to match uninteresting newsgroups in the active file.
578 Any lines in the active file matching this regular expression are
579 removed from the newsgroup list before anything else is done to it,
580 thus making them effectively non-existant.")
581
582 (defvar gnus-ignored-headers
583   "^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:"
584   "*All headers that match this regexp will be hidden.
585 Also see `gnus-visible-headers'.")
586
587 (defvar gnus-visible-headers "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:"
588   "*All headers that do not match this regexp will be hidden.
589 Also see `gnus-ignored-headers'.")
590
591 (defvar gnus-sorted-header-list
592   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:" 
593     "^Cc:" "^Date:" "^Organization:")
594   "*This variable is a list of regular expressions.
595 If it is non-nil, headers that match the regular expressions will
596 be placed first in the article buffer in the sequence specified by
597 this list.")
598
599 (defvar gnus-show-all-headers nil
600   "*If non-nil, don't hide any headers.")
601
602 (defvar gnus-save-all-headers t
603   "*If non-nil, don't remove any headers before saving.")
604
605 (defvar gnus-inhibit-startup-message nil
606   "*If non-nil, the startup message will not be displayed.")
607
608 (defvar gnus-signature-separator "^-- *$"
609   "Regexp matching signature separator.")
610
611 (defvar gnus-auto-extend-newsgroup t
612   "*If non-nil, extend newsgroup forward and backward when requested.")
613
614 (defvar gnus-auto-select-first t
615   "*If non-nil, select the first unread article when entering a group.
616 If you want to prevent automatic selection of the first unread article
617 in some newsgroups, set the variable to nil in
618 `gnus-select-group-hook'.") 
619
620 (defvar gnus-auto-select-next t
621   "*If non-nil, offer to go to the next group from the end of the previous.
622 If the value is t and the next newsgroup is empty, Gnus will exit
623 summary mode and go back to group mode.  If the value is neither nil
624 nor t, Gnus will select the following unread newsgroup.  In
625 particular, if the value is the symbol `quietly', the next unread
626 newsgroup will be selected without any confirmations.")
627
628 (defvar gnus-auto-select-same nil
629   "*If non-nil, select the next article with the same subject.")
630
631 (defvar gnus-summary-check-current nil
632   "*If non-nil, consider the current article when moving.
633 The \"unread\" movement commands will stay on the same line if the
634 current article is unread.")
635
636 (defvar gnus-auto-center-summary t
637   "*If non-nil, always center the current summary buffer.")
638
639 (defvar gnus-break-pages t
640   "*If non-nil, do page breaking on articles.
641 The page delimiter is specified by the `gnus-page-delimiter'
642 variable.")
643
644 (defvar gnus-page-delimiter "^\^L"
645   "*Regexp describing what to use as article page delimiters.
646 The default value is \"^\^L\", which is a form linefeed at the
647 beginning of a line.")
648
649 (defvar gnus-use-full-window t
650   "*If non-nil, use the entire Emacs screen.")
651
652 (defvar gnus-window-configuration nil
653   "Obsolete variable.  See `gnus-buffer-configuration'.")
654
655 (defvar gnus-buffer-configuration
656   '((group ([group 1.0 point] 
657             (if gnus-carpal [group-carpal 4])))
658     (summary ([summary 1.0 point]
659               (if gnus-carpal [summary-carpal 4])))
660     (article ([summary 0.25 point] 
661               (if gnus-carpal [summary-carpal 4]) 
662               [article 1.0]))
663     (server ([server 1.0 point]
664              (if gnus-carpal [server-carpal 2])))
665     (browse ([browse 1.0 point]
666              (if gnus-carpal [browse-carpal 2])))
667     (group-mail ([mail 1.0 point]))
668     (summary-mail ([mail 1.0 point]))
669     (summary-reply ([article 0.5]
670                     [mail 1.0 point]))
671     (info ([nil 1.0 point]))
672     (summary-faq ([summary 0.25]
673                   [article 1.0 point]))
674     (edit-group ([group 0.5]
675                  [edit-group 1.0 point]))
676     (edit-server ([server 0.5]
677                   [edit-server 1.0 point]))
678     (post ([post 1.0 point]))
679     (reply ([article 0.5]
680             [mail 1.0 point]))
681     (mail-forward ([mail 1.0 point]))
682     (post-forward ([post 1.0 point]))
683     (reply-yank ([mail 1.0 point]))
684     (followup ([article 0.5]
685                [post 1.0 point]))
686     (followup-yank ([post 1.0 point])))
687   "Window configuration for all possible Gnus buffers.
688 This variable is a list of lists.  Each of these lists has a NAME and
689 a RULE.  The NAMEs are commonsense names like `group', which names a
690 rule used when displaying the group buffer; `summary', which names a
691 rule for what happens when you enter a group and do not display an
692 article buffer; and so on.  See the value of this variable for a
693 complete list of NAMEs.
694
695 Each RULE is a list of vectors.  The first element in this vector is
696 the name of the buffer to be displayed; the second element is the
697 percentage of the screen this buffer is to occupy (a number in the
698 0.0-0.99 range); the optional third element is `point', which should
699 be present to denote which buffer point is to go to after making this
700 buffer configuration.")
701
702 (defvar gnus-window-to-buffer
703   '((group . gnus-group-buffer)
704     (summary . gnus-summary-buffer)
705     (article . gnus-article-buffer)
706     (server . gnus-server-buffer)
707     (browse . "*Gnus Browse Server*")
708     (edit-group . gnus-group-edit-buffer)
709     (edit-server . gnus-server-edit-buffer)
710     (group-carpal . gnus-carpal-group-buffer)
711     (summary-carpal . gnus-carpal-summary-buffer)
712     (server-carpal . gnus-carpal-server-buffer)
713     (browse-carpal . gnus-carpal-browse-buffer)
714     (mail . "*mail*")
715     (post . gnus-post-news-buffer))
716   "Mapping from short symbols to buffer names or buffer variables.")
717
718 (defvar gnus-carpal nil
719   "*If non-nil, display clickable icons.")
720
721 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
722   "*Function called with a group name when new group is detected.
723 A few pre-made functions are supplied: `gnus-subscribe-randomly'
724 inserts new groups at the beginning of the list of groups;
725 `gnus-subscribe-alphabetically' inserts new groups in strict
726 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
727 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
728 for your decision.")
729
730 ;; Suggested by a bug report by Hallvard B Furuseth.
731 ;; <h.b.furuseth@usit.uio.no>. 
732 (defvar gnus-subscribe-options-newsgroup-method
733   (function gnus-subscribe-alphabetically)
734   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
735 If, for instance, you want to subscribe to all newsgroups in the
736 \"no\" and \"alt\" hierarchies, you'd put the following in your
737 .newsrc file:
738
739 options -n no.all alt.all
740
741 Gnus will the subscribe all new newsgroups in these hierarchies with
742 the subscription method in this variable.")
743
744 (defvar gnus-subscribe-hierarchical-interactive nil
745   "*If non-nil, Gnus will offer to subscribe hierarchically.
746 When a new hierarchy appears, Gnus will ask the user:
747
748 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
749
750 If the user pressed `d', Gnus will descend the hierarchy, `y' will
751 subscribe to all newsgroups in the hierarchy and `s' will skip this
752 hierarchy in its entirety.")
753
754 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
755   "*Function used for sorting the group buffer.
756 This function will be called with group info entries as the arguments
757 for the groups to be sorted.  Pre-made functions include
758 `gnus-sort-by-alphabet', `gnus-sort-by-unread' and
759 `gnus-sort-by-level'")
760
761 ;; Mark variables suggested by Thomas Michanek
762 ;; <Thomas.Michanek@telelogic.se>. 
763 (defvar gnus-unread-mark ? 
764   "*Mark used for unread articles.")
765 (defvar gnus-ticked-mark ?!
766   "*Mark used for ticked articles.")
767 (defvar gnus-dormant-mark ??
768   "*Mark used for dormant articles.")
769 (defvar gnus-del-mark ?D
770   "*Mark used for del'd articles.")
771 (defvar gnus-read-mark ?d
772   "*Mark used for read articles.")
773 (defvar gnus-expirable-mark ?E
774   "*Mark used for expirable articles.")
775 (defvar gnus-killed-mark ?K
776   "*Mark used for killed articles.")
777 (defvar gnus-kill-file-mark ?X
778   "*Mark used for articles killed by kill files.")
779 (defvar gnus-low-score-mark ?Y
780   "*Mark used for articles with a low score.")
781 (defvar gnus-catchup-mark ?C
782   "*Mark used for articles that are caught up.")
783 (defvar gnus-replied-mark ?R
784   "*Mark used for articles that have been replied to.")
785 (defvar gnus-process-mark ?# 
786   "*Process mark.")
787 (defvar gnus-ancient-mark ?A
788   "*Mark used for ancient articles.")
789 (defvar gnus-canceled-mark ?G
790   "*Mark used for cancelled articles.")
791 (defvar gnus-score-over-mark ?+
792   "*Score mark used for articles with high scores.")
793 (defvar gnus-score-below-mark ?-
794   "*Score mark used for articles with low scores.")
795 (defvar gnus-empty-thread-mark ? 
796   "*There is no thread under the article.")
797 (defvar gnus-not-empty-thread-mark ?=
798   "*There is a thread under the article.")
799 (defvar gnus-dummy-mark ?Z
800   "*This is a dummy article.")
801
802 (defvar gnus-view-pseudo-asynchronously nil
803   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
804
805 (defvar gnus-view-pseudos nil
806   "*If `automatic', pseudo-articles will be viewed automatically.
807 If `not-confirm', pseudos will be viewed automatically, and the user
808 will not be asked to confirm the command.")
809
810 (defvar gnus-view-pseudos-separately t
811   "*If non-nil, one pseudo-article will be created for each file to be viewed.
812 If nil, all files that use the same viewing command will be given as a
813 list of parameters to that command.")
814
815 (defvar gnus-group-line-format "%M%S%p%5y: %(%g%)\n"
816   "*Format of group lines.
817 It works along the same lines as a normal formatting string,
818 with some simple extensions.
819
820 %M    Only marked articles (character, \"*\" or \" \")
821 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
822 %L    Level of subscribedness (integer)
823 %N    Number of unread articles (integer)
824 %I    Number of dormant articles (integer)
825 %i    Number of ticked and dormant (integer)
826 %T    Number of ticked articles (integer)
827 %R    Number of read articles (integer)
828 %t    Total number of articles (integer)
829 %y    Number of unread, unticked articles (integer)
830 %G    Group name (string)
831 %g    Qualified group name (string)
832 %D    Group description (string)
833 %s    Select method (string)
834 %o    Moderated group (char, \"m\")
835 %p    Process mark (char)
836 %O    Moderated group (string, \"(m)\" or \"\")
837 %n    Select from where (string)
838 %z    A string that look like `<%s:%n>' if a foreign select method is used
839 %u    User defined specifier. The next character in the format string should
840       be a letter.  Gnus will call the function gnus-user-format-function-X,
841       where X is the letter following %u. The function will be passed the
842       current header as argument. The function should return a string, which
843       will be inserted into the buffer just like information from any other
844       group specifier.
845
846 Text between %( and %) will be highlighted with `gnus-mouse-face' when
847 the mouse point move inside the area.  There can only be one such area.
848
849 Note that this format specification is not always respected. For
850 reasons of efficiency, when listing killed groups, this specification
851 is ignored altogether. If the spec is changed considerably, your
852 output may end up looking strange when listing both alive and killed
853 groups.
854
855 If you use %o or %O, reading the active file will be slower and quite
856 a bit of extra memory will be used. %D will also worsen performance.
857 Also note that if you change the format specification to include any
858 of these specs, you must probably re-start Gnus to see them go into
859 effect.") 
860
861 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
862   "*The format specification of the lines in the summary buffer.
863
864 It works along the same lines as a normal formatting string,
865 with some simple extensions.
866
867 %N   Article number, left padded with spaces (string)
868 %S   Subject (string)
869 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
870 %n   Name of the poster (string)
871 %A   Address of the poster (string)
872 %F   Contents of the From: header (string)
873 %x   Contents of the Xref: header (string)
874 %D   Date of the article (string)
875 %d   Date of the article (string) in DD-MMM format
876 %M   Message-id of the article (string)
877 %r   References of the article (string)
878 %c   Number of characters in the article (integer)
879 %L   Number of lines in the article (integer)
880 %I   Indentation based on thread level (a string of spaces)
881 %T   A string with two possible values: 80 spaces if the article
882      is on thread level two or larger and 0 spaces on level one
883 %R   \"R\" if this article has been replied to, \" \" otherwise (character)
884 %U   Status of this article (character, \"D\", \"K\", \"-\" or \" \")
885 %[   Opening bracket (character, \"[\" or \"<\")
886 %]   Closing bracket (character, \"]\" or \">\")
887 %>   Spaces of length thread-level (string)
888 %<   Spaces of length (- 20 thread-level) (string)
889 %i   Article score (number)
890 %z   Article zcore (character)
891 %t   Number of articles under the current thread (number).
892 %e   Whether the thread is empty or not (character).
893 %u   User defined specifier. The next character in the format string should
894      be a letter.  Gnus will call the function gnus-user-format-function-X,
895      where X is the letter following %u. The function will be passed the
896      current header as argument. The function should return a string, which
897      will be inserted into the summary just like information from any other
898      summary specifier.
899
900 Text between %( and %) will be highlighted with `gnus-mouse-face'
901 when the mouse point is placed inside the area.  There can only be one
902 such area.
903
904 The %U (status), %R (replied) and %z (zcore) specs have to be handled
905 with care. For reasons of efficiency, Gnus will compute what column
906 these characters will end up in, and \"hard-code\" that. This means that
907 it is illegal to have these specs after a variable-length spec. Well,
908 you might not be arrested, but your summary buffer will look strange,
909 which is bad enough.
910
911 The smart choice is to have these specs as for to the left as
912 possible. 
913
914 This restriction may disappear in later versions of Gnus.")
915
916 (defvar gnus-summary-dummy-line-format "*  :                          : %S\n"
917   "*The format specification for the dummy roots in the summary buffer.
918 It works along the same lines as a normal formatting string,
919 with some simple extensions.
920
921 %S  The subject")
922
923 (defvar gnus-summary-mode-line-format "(ding) %G/%A %Z"
924   "*The format specification for the summary mode line.")
925
926 (defvar gnus-article-mode-line-format "(ding) %G/%A %S"
927   "*The format specification for the article mode line.")
928
929 (defvar gnus-group-mode-line-format "(ding) List of groups   {%M:%S}  "
930   "*The format specification for the group mode line.")
931
932 (defvar gnus-valid-select-methods
933   '(("nntp" post address prompt-address)
934     ("nnspool" post)
935     ("nnvirtual" none virtual prompt-address) 
936     ("nnmbox" mail respool) 
937     ("nnml" mail respool)
938     ("nnmh" mail respool) 
939     ("nndir" none prompt-address address)
940     ("nneething" none prompt-address)
941     ("nndigest" none) 
942     ("nndoc" none prompt-address) 
943     ("nnbabyl" mail respool) 
944     ("nnkiboze" none virtual) 
945     ("nnsoup" post)
946     ("nnfolder" mail respool))
947   "An alist of valid select methods.
948 The first element of each list lists should be a string with the name
949 of the select method. The other elements may be be the category of
950 this method (ie. `post', `mail', `none' or whatever) or other
951 properties that this method has (like being respoolable).
952 If you implement a new select method, all you should have to change is
953 this variable. I think.")
954
955 (defvar gnus-updated-mode-lines '(group article summary)
956   "*List of buffers that should update their mode lines.
957 The list may contain the symbols `group', `article' and `summary'. If
958 the corresponding symbol is present, Gnus will keep that mode line
959 updated with information that may be pertinent. 
960 If this variable is nil, screen refresh may be quicker.")
961
962 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
963 (defvar gnus-mode-non-string-length 21
964   "*Max length of mode-line non-string contents.
965 If this is nil, Gnus will take space as is needed, leaving the rest
966 of the modeline intact.")
967
968 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
969 (defvar gnus-mouse-face 'highlight
970   "*Face used for mouse highlighting in Gnus.
971 No mouse highlights will be done if `gnus-visual' is nil.")
972
973 (defvar gnus-summary-mark-below nil
974   "*Mark all articles with a score below this variable as read.
975 This variable is local to each summary buffer and usually set by the
976 score file.")  
977
978 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
979   "*List of functions used for sorting threads in the summary buffer.
980 By default, threads are sorted by article number.
981
982 Each function takes two threads and return non-nil if the first thread
983 should be sorted before the other.  If you use more than one function,
984 the primary sort function should be the last.
985
986 Ready-mady functions include `gnus-thread-sort-by-number',
987 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
988 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
989 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
990
991 (defvar gnus-thread-score-function '+
992   "*Function used for calculating the total score of a thread.
993
994 The function is called with the scores of the article and each
995 subthread and should then return the score of the thread.
996
997 Some functions you can use are `+', `max', or `min'.")
998
999 (defvar gnus-options-subscribe nil
1000   "*All new groups matching this regexp will be subscribed unconditionally.
1001 Note that this variable deals only with new newsgroups.  This variable
1002 does not affect old newsgroups.")
1003
1004 (defvar gnus-options-not-subscribe nil
1005   "*All new groups matching this regexp will be ignored.
1006 Note that this variable deals only with new newsgroups.  This variable
1007 does not affect old (already subscribed) newsgroups.")
1008
1009 (defvar gnus-auto-expirable-newsgroups nil
1010   "*Groups in which to automatically mark read articles as expirable.
1011 If non-nil, this should be a regexp that should match all groups in
1012 which to perform auto-expiry.  This only makes sense for mail groups.")
1013
1014 (defvar gnus-hidden-properties '(invisible t intangible t)
1015   "Property list to use for hiding text.")
1016
1017 ;; Hooks.
1018
1019 (defvar gnus-group-mode-hook nil
1020   "*A hook for Gnus group mode.")
1021
1022 (defvar gnus-summary-mode-hook nil
1023   "*A hook for Gnus summary mode.")
1024
1025 (defvar gnus-article-mode-hook nil
1026   "*A hook for Gnus article mode.")
1027
1028 (defvar gnus-open-server-hook nil
1029   "*A hook called just before opening connection to the news server.")
1030
1031 (defvar gnus-startup-hook nil
1032   "*A hook called at startup.
1033 This hook is called after Gnus is connected to the NNTP server.")
1034
1035 (defvar gnus-get-new-news-hook nil
1036   "*A hook run just before Gnus checks for new news.")
1037
1038 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1039   "*A function that is called to generate the group buffer.
1040 The function is called with three arguments: The first is a number;
1041 all group with a level less or equal to that number should be listed,
1042 if the second is non-nil, empty groups should also be displayed. If
1043 the third is non-nil, it is a number. No groups with a level lower
1044 than this number should be displayed.
1045
1046 The only current function implemented is `gnus-group-prepare-flat'.")
1047
1048 (defvar gnus-group-prepare-hook nil
1049   "*A hook called after the group buffer has been generated.
1050 If you want to modify the group buffer, you can use this hook.")
1051
1052 (defvar gnus-summary-prepare-hook nil
1053   "*A hook called after the summary buffer has been generated.
1054 If you want to modify the summary buffer, you can use this hook.")
1055
1056 (defvar gnus-article-prepare-hook nil
1057   "*A hook called after an article has been prepared in the article buffer.
1058 If you want to run a special decoding program like nkf, use this hook.")
1059
1060 (defvar gnus-article-display-hook nil
1061   "*A hook called after the article is displayed in the article buffer.
1062 The hook is designed to change the contents of the article
1063 buffer. Typical functions that this hook may contain are
1064 `gnus-article-hide-headers' (hide selected headers),
1065 `gnus-article-maybe-highlight' (perform fancy article highlighting), 
1066 `gnus-article-hide-signature' (hide signature) and
1067 `gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1068 (add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1069 (add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1070
1071 (defvar gnus-article-x-face-command
1072   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1073   "String or function to be executed to display an X-Face header.
1074 If it is a string, the command will be executed in a sub-shell
1075 asynchronously. The compressed face will be piped to this command.") 
1076
1077 (defvar gnus-article-x-face-too-ugly nil
1078   "Regexp matching posters whose face shouldn't be shown automatically.")
1079
1080 (defvar gnus-select-group-hook nil
1081   "*A hook called when a newsgroup is selected.
1082
1083 If you'd like to simplify subjects like the
1084 `gnus-summary-next-same-subject' command does, you can use the
1085 following hook:
1086
1087  (setq gnus-select-group-hook
1088       (list
1089         (lambda ()
1090           (mapcar (lambda (header)
1091                      (header-set-subject
1092                       header
1093                       (gnus-simplify-subject
1094                        (header-subject header) 're-only)))
1095                   gnus-newsgroup-headers))))")
1096
1097 (defvar gnus-select-article-hook
1098   '(gnus-summary-show-thread)
1099   "*A hook called when an article is selected.
1100 The default hook shows conversation thread subtrees of the selected
1101 article automatically using `gnus-summary-show-thread'.")
1102
1103 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1104   "*A hook called to apply kill files to a group.
1105 This hook is intended to apply a kill file to the selected newsgroup.
1106 The function `gnus-apply-kill-file' is called by default.
1107
1108 Since a general kill file is too heavy to use only for a few
1109 newsgroups, I recommend you to use a lighter hook function. For
1110 example, if you'd like to apply a kill file to articles which contains
1111 a string `rmgroup' in subject in newsgroup `control', you can use the
1112 following hook:
1113
1114 \(setq gnus-apply-kill-hook
1115       (list
1116         (lambda ()
1117           (cond ((string-match \"control\" gnus-newsgroup-name)
1118                  (gnus-kill \"Subject\" \"rmgroup\")
1119                  (gnus-expunge \"X\"))))))")
1120
1121 (defvar gnus-visual-mark-article-hook 
1122   (list 'gnus-highlight-selected-summary)
1123   "*Hook run after selecting an article in the summary buffer.
1124 It is meant to be used for highlighting the article in some way.  It
1125 is not run if `gnus-visual' is nil.")
1126
1127 (defvar gnus-prepare-article-hook (list 'gnus-inews-insert-signature)
1128   "*A hook called after preparing body, but before preparing header headers.
1129 The default hook (`gnus-inews-insert-signature') inserts a signature
1130 file specified by the variable `gnus-signature-file'.")
1131
1132 (defvar gnus-exit-group-hook nil
1133   "*A hook called when exiting (not quitting) summary mode.")
1134
1135 (defvar gnus-suspend-gnus-hook nil
1136   "*A hook called when suspending (not exiting) Gnus.")
1137
1138 (defvar gnus-exit-gnus-hook nil
1139   "*A hook called when exiting Gnus.")
1140
1141 (defvar gnus-save-newsrc-hook nil
1142   "*A hook called when saving the newsrc file.")
1143
1144 (defvar gnus-summary-update-hook 
1145   (list 'gnus-summary-highlight-line)
1146   "*A hook called when a summary line is changed.
1147 The hook will not be called if `gnus-visual' is nil.
1148
1149 The default function `gnus-summary-highlight-line' will
1150 highlight the line according to the `gnus-summary-highlight'
1151 variable.")
1152
1153 (defvar gnus-mark-article-hook (list 'gnus-summary-mark-unread-as-read)
1154   "*A hook called when an article is selected for the first time.
1155 The hook is intended to mark an article as read (or unread)
1156 automatically when it is selected.")
1157
1158 \f
1159 ;; Internal variables
1160
1161 ;; Avoid highlighting in kill files.
1162 (defvar gnus-summary-inhibit-highlight nil)
1163 (defvar gnus-newsgroup-selected-overlay nil)
1164
1165 (defvar gnus-article-mode-map nil)
1166 (defvar caesar-translate-table nil)
1167 (defvar gnus-dribble-buffer nil)
1168 (defvar gnus-headers-retrieved-by nil)
1169 (defvar gnus-article-reply nil)
1170 (defvar gnus-override-method nil)
1171 (defvar gnus-article-check-size nil)
1172
1173 (defvar gnus-current-score-file nil)
1174 (defvar gnus-internal-global-score-files nil)
1175 (defvar gnus-score-file-list nil)
1176
1177
1178 (defvar gnus-current-move-group nil)
1179
1180 (defvar gnus-newsgroup-dependencies nil)
1181 (defvar gnus-newsgroup-threads nil)
1182 (defvar gnus-newsgroup-async nil)
1183 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1184
1185 (defvar gnus-newsgroup-adaptive nil)
1186
1187 (defvar gnus-summary-display-table nil)
1188
1189 (defconst gnus-group-line-format-alist
1190   (list (list ?M 'marked ?c)
1191         (list ?S 'subscribed ?c)
1192         (list ?L 'level ?d)
1193         (list ?N 'number ?s)
1194         (list ?I 'number-of-dormant ?d)
1195         (list ?T 'number-of-ticked ?d)
1196         (list ?R 'number-of-read ?s)
1197         (list ?t 'number-total ?d)
1198         (list ?y 'number-of-unread-unticked ?s)
1199         (list ?i 'number-of-ticked-and-dormant ?d)
1200         (list ?g 'group ?s)
1201         (list ?G 'qualified-group ?s)
1202         (list ?D 'newsgroup-description ?s)
1203         (list ?o 'moderated ?c)
1204         (list ?O 'moderated-string ?s)
1205         (list ?p 'process-marked ?c)
1206         (list ?s 'news-server ?s)
1207         (list ?n 'news-method ?s)
1208         (list ?z 'news-method-string ?s)
1209         (list ?u 'user-defined ?s)))
1210
1211 (defconst gnus-summary-line-format-alist 
1212   (list (list ?N 'number ?d)
1213         (list ?S 'subject ?s)
1214         (list ?s 'subject-or-nil ?s)
1215         (list ?n 'name ?s)
1216         (list ?A 'address ?s)
1217         (list ?F 'from ?s)
1218         (list ?x (macroexpand '(header-xref header)) ?s)
1219         (list ?D (macroexpand '(header-date header)) ?s)
1220         (list ?d '(gnus-dd-mmm (header-date header)) ?s)
1221         (list ?M (macroexpand '(header-id header)) ?s)
1222         (list ?r (macroexpand '(header-references header)) ?s)
1223         (list ?c '(or (header-chars header) 0) ?d)
1224         (list ?L 'lines ?d)
1225         (list ?I 'indentation ?s)
1226         (list ?T '(if (= level 0) "" (make-string (frame-width) ? )) ?s)
1227         (list ?R 'replied ?c)
1228         (list ?\[ 'opening-bracket ?c)
1229         (list ?\] 'closing-bracket ?c)
1230         (list ?\> '(make-string level ? ) ?s)
1231         (list ?\< '(make-string (max 0 (- 20 level)) ? ) ?s)
1232         (list ?i 'score ?d)
1233         (list ?z 'score-char ?c)
1234         (list ?U 'unread ?c)
1235         (list ?t '(gnus-summary-number-of-articles-in-thread 
1236                    (or (prog1 gnus-tmp-adopt-thread 
1237                          (setq gnus-tmp-adopt-thread nil))
1238                        (if (boundp 'thread) (symbol-value 'thread)
1239                          thread nil)))
1240                    ?d)
1241         (list ?e '(gnus-summary-number-of-articles-in-thread 
1242                    (or gnus-tmp-adopt-thread 
1243                        (if (boundp 'thread) (symbol-value 'thread)
1244                          thread nil)) t)
1245                    ?d)
1246         (list ?u 'user-defined ?s))
1247   "An alist of format specifications that can appear in summary lines,
1248 and what variables they correspond with, along with the type of the
1249 variable (string, integer, character, etc).")
1250
1251 (defconst gnus-summary-dummy-line-format-alist
1252   (list (list ?S 'subject ?s)
1253         (list ?N 'number ?d)
1254         (list ?u 'user-defined ?s)))
1255
1256 (defconst gnus-summary-mode-line-format-alist 
1257   (list (list ?G 'group-name ?s)
1258         (list ?g '(gnus-short-group-name group-name) ?s)
1259         (list ?A 'article-number ?d)
1260         (list ?Z 'unread-and-unselected ?s)
1261         (list ?V 'gnus-version ?s)
1262         (list ?U 'unread ?d)
1263         (list ?S 'subject ?s)
1264         (list ?e 'unselected ?d)
1265         (list ?u 'user-defined ?s)
1266         (list ?s '(gnus-current-score-file-nondirectory) ?s)))
1267
1268 (defconst gnus-group-mode-line-format-alist 
1269   (list (list ?S 'news-server ?s)
1270         (list ?M 'news-method ?s)
1271         (list ?u 'user-defined ?s)))
1272
1273 (defvar gnus-have-read-active-file nil)
1274
1275 (defconst gnus-maintainer "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls & Boys)"
1276   "The mail address of the Gnus maintainer.")
1277
1278 (defconst gnus-version "(ding) Gnus v0.81"
1279   "Version number for this version of Gnus.")
1280
1281 (defvar gnus-info-nodes
1282   '((gnus-group-mode            "(gnus)The Group Buffer")
1283     (gnus-summary-mode          "(gnus)The Summary Buffer")
1284     (gnus-article-mode          "(gnus)The Article Buffer"))
1285   "Assoc list of major modes and related Info nodes.")
1286
1287 (defvar gnus-documentation-group-file "~/dgnus/lisp/doc.txt"
1288   "The location of the (ding) Gnus documentation group.")
1289
1290 (defvar gnus-group-buffer "*Group*")
1291 (defvar gnus-summary-buffer "*Summary*")
1292 (defvar gnus-article-buffer "*Article*")
1293 (defvar gnus-server-buffer "*Server*")
1294
1295 (defvar gnus-work-buffer " *gnus work*")
1296
1297 (defvar gnus-buffer-list nil
1298   "Gnus buffers that should be killed on exit.")
1299
1300 (defvar gnus-server-alist nil
1301   "List of available servers.")
1302
1303 (defvar gnus-variable-list
1304   '(gnus-newsrc-options gnus-newsrc-options-n
1305     gnus-newsrc-last-checked-date 
1306     gnus-newsrc-alist gnus-server-alist
1307     gnus-killed-list gnus-zombie-list)
1308   "Gnus variables saved in the quick startup file.")
1309
1310 (defvar gnus-overload-functions
1311   '((news-inews gnus-inews-news "rnewspost")
1312     (caesar-region gnus-caesar-region "rnews"))
1313   "Functions overloaded by gnus.
1314 It is a list of `(original overload &optional file)'.")
1315
1316 (defvar gnus-newsrc-options nil
1317   "Options line in the .newsrc file.")
1318
1319 (defvar gnus-newsrc-options-n nil
1320   "List of regexps representing groups to be subscribed/ignored unconditionally.") 
1321
1322 (defvar gnus-newsrc-last-checked-date nil
1323   "Date Gnus last asked server for new newsgroups.")
1324
1325 (defvar gnus-newsrc-alist nil
1326   "Assoc list of read articles.
1327 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1328
1329 (defvar gnus-newsrc-hashtb nil
1330   "Hashtable of gnus-newsrc-alist.")
1331
1332 (defvar gnus-killed-list nil
1333   "List of killed newsgroups.")
1334
1335 (defvar gnus-killed-hashtb nil
1336   "Hash table equivalent of gnus-killed-list.")
1337
1338 (defvar gnus-zombie-list nil
1339   "List of almost dead newsgroups.")
1340
1341 (defvar gnus-description-hashtb nil
1342   "Descriptions of newsgroups.")
1343
1344 (defvar gnus-list-of-killed-groups nil
1345   "List of newsgroups that have recently been killed by the user.")
1346
1347 (defvar gnus-active-hashtb nil
1348   "Hashtable of active articles.")
1349
1350 (defvar gnus-moderated-list nil
1351   "List of moderated newsgroups.")
1352
1353 (defvar gnus-group-marked nil)
1354
1355 (defvar gnus-current-startup-file nil
1356   "Startup file for the current host.")
1357
1358 (defvar gnus-last-search-regexp nil
1359   "Default regexp for article search command.")
1360
1361 (defvar gnus-last-shell-command nil
1362   "Default shell command on article.")
1363
1364 (defvar gnus-current-select-method nil
1365   "The current method for selecting a newsgroup.")
1366
1367 (defvar gnus-have-all-newsgroups nil)
1368
1369 (defvar gnus-article-internal-prepare-hook nil)
1370
1371 (defvar gnus-newsgroup-name nil)
1372 (defvar gnus-newsgroup-begin nil)
1373 (defvar gnus-newsgroup-end nil)
1374 (defvar gnus-newsgroup-last-rmail nil)
1375 (defvar gnus-newsgroup-last-mail nil)
1376 (defvar gnus-newsgroup-last-folder nil)
1377 (defvar gnus-newsgroup-last-file nil)
1378 (defvar gnus-newsgroup-auto-expire nil)
1379 (defvar gnus-newsgroup-active nil)
1380
1381 (defvar gnus-newsgroup-unreads nil
1382   "List of unread articles in the current newsgroup.")
1383
1384 (defvar gnus-newsgroup-unselected nil
1385   "List of unselected unread articles in the current newsgroup.")
1386
1387 (defvar gnus-newsgroup-marked nil
1388   "List of ticked articles in the current newsgroup (a subset of unread art).")
1389
1390 (defvar gnus-newsgroup-killed nil
1391   "List of ranges of articles that have been through the scoring process.")
1392
1393 (defvar gnus-newsgroup-kill-headers nil)
1394
1395 (defvar gnus-newsgroup-replied nil
1396   "List of articles that have been replied to in the current newsgroup.")
1397
1398 (defvar gnus-newsgroup-expirable nil
1399   "List of articles in the current newsgroup that can be expired.")
1400
1401 (defvar gnus-newsgroup-processable nil
1402   "List of articles in the current newsgroup that can be processed.")
1403
1404 (defvar gnus-newsgroup-bookmarks nil
1405   "List of articles in the current newsgroup that have bookmarks.")
1406
1407 (defvar gnus-newsgroup-dormant nil
1408   "List of dormant articles in the current newsgroup.")
1409
1410 (defvar gnus-newsgroup-scored nil
1411   "List of scored articles in the current newsgroup.")
1412
1413 (defvar gnus-newsgroup-headers nil
1414   "List of article headers in the current newsgroup.")
1415 (defvar gnus-newsgroup-headers-hashtb-by-number nil)
1416
1417 (defvar gnus-newsgroup-ancient nil
1418   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1419
1420 (defvar gnus-current-article nil)
1421 (defvar gnus-article-current nil)
1422 (defvar gnus-current-headers nil)
1423 (defvar gnus-have-all-headers nil)
1424 (defvar gnus-last-article nil)
1425 (defvar gnus-newsgroup-history nil)
1426 (defvar gnus-current-kill-article nil)
1427
1428 ;; Save window configuration.
1429 (defvar gnus-prev-winconf nil)
1430
1431 ;; Format specs
1432 (defvar gnus-summary-line-format-spec nil)
1433 (defvar gnus-summary-dummy-line-format-spec nil)
1434 (defvar gnus-group-line-format-spec nil)
1435 (defvar gnus-summary-mode-line-format-spec nil)
1436 (defvar gnus-article-mode-line-format-spec nil)
1437 (defvar gnus-group-mode-line-format-spec nil)
1438 (defvar gnus-summary-mark-positions nil)
1439
1440 (defvar gnus-summary-expunge-below nil)
1441 (defvar gnus-reffed-article-number nil)
1442
1443 (defvar rmail-default-file (expand-file-name "~/XMBOX"))
1444 (defvar rmail-default-rmail-file (expand-file-name "~/XNEWS"))
1445
1446 (defconst gnus-summary-local-variables 
1447   '(gnus-newsgroup-name 
1448     gnus-newsgroup-begin gnus-newsgroup-end 
1449     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail 
1450     gnus-newsgroup-last-folder gnus-newsgroup-last-file 
1451     gnus-newsgroup-auto-expire gnus-newsgroup-unreads 
1452     gnus-newsgroup-unselected gnus-newsgroup-marked
1453     gnus-newsgroup-replied gnus-newsgroup-expirable
1454     gnus-newsgroup-processable gnus-newsgroup-killed
1455     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1456     gnus-newsgroup-headers gnus-newsgroup-headers-hashtb-by-number
1457     gnus-current-article gnus-current-headers gnus-have-all-headers
1458     gnus-last-article gnus-article-internal-prepare-hook
1459     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1460     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1461     gnus-newsgroup-threads gnus-newsgroup-async
1462     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below 
1463     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1464     gnus-newsgroup-history gnus-newsgroup-ancient
1465     gnus-newsgroup-adaptive)
1466   "Variables that are buffer-local to the summary buffers.")
1467
1468 (defconst gnus-bug-message
1469   "Sending a bug report to the Gnus Towers.
1470 ========================================
1471
1472 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1473 be sent to the Gnus Bug Exterminators. 
1474
1475 At the bottom of the buffer you'll see lots of variable settings.
1476 Please do not delete those.  They will tell the Bug People what your
1477 environment is, so that it will be easier to locate the bugs.
1478
1479 If you have found a bug that makes Emacs go \"beep\", set
1480 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET') 
1481 and include the backtrace in your bug report.
1482
1483 Please describe the bug in annoying, painstaking detail.
1484
1485 Thank you for your help in stamping out bugs.
1486 ")
1487
1488 ;;; End of variables.
1489
1490 ;; Define some autoload functions Gnus might use.
1491 (eval-and-compile
1492
1493   ;; Various 
1494   (autoload 'metamail-buffer "metamail")
1495   (autoload 'Info-goto-node "info")
1496   (autoload 'hexl-hex-string-to-integer "hexl")
1497   (autoload 'pp "pp")
1498   (autoload 'pp-to-string "pp")
1499   (autoload 'pp-eval-expression "pp")
1500   (autoload 'mail-extract-address-components "mail-extr")
1501
1502   (autoload 'nnmail-split-fancy "nnmail")
1503   (autoload 'nnvirtual-catchup-group "nnvirtual")
1504
1505   ;; timezone
1506   (autoload 'timezone-make-date-arpa-standard "timezone")
1507   (autoload 'timezone-fix-time "timezone")
1508   (autoload 'timezone-make-sortable-date "timezone")
1509   (autoload 'timezone-make-time-string "timezone")
1510
1511   ;; rmail & friends
1512   (autoload 'mail-position-on-field "sendmail")
1513   (autoload 'mail-setup "sendmail")
1514   (autoload 'rmail-output "rmailout")
1515   (autoload 'news-mail-other-window "rnewspost")
1516   (autoload 'news-reply-yank-original "rnewspost")
1517   (autoload 'news-caesar-buffer-body "rnewspost")
1518   (autoload 'rmail-insert-rmail-file-header "rmail")
1519   (autoload 'rmail-count-new-messages "rmail")
1520   (autoload 'rmail-show-message "rmail")
1521
1522   ;; gnus-soup
1523   (autoload 'gnus-group-brew-soup "gnus-soup" nil t)
1524   (autoload 'gnus-brew-soup "gnus-soup" nil t)
1525   (autoload 'gnus-soup-add-article "gnus-soup" nil t)
1526   (autoload 'gnus-soup-send-replies "gnus-soup" nil t)
1527   (autoload 'gnus-soup-save-areas "gnus-soup" nil t)
1528   (autoload 'gnus-soup-pack-packet "gnus-soup" nil t)
1529   (autoload 'nnsoup-pack-replies "nnsoup" nil t)
1530
1531   ;; gnus-mh
1532   (autoload 'gnus-mail-reply-using-mhe "gnus-mh")
1533   (autoload 'gnus-mail-forward-using-mhe "gnus-mh")
1534   (autoload 'gnus-mail-other-window-using-mhe "gnus-mh")
1535   (autoload 'gnus-summary-save-in-folder "gnus-mh")
1536   (autoload 'gnus-summary-save-article-folder "gnus-mh")
1537   (autoload 'gnus-Folder-save-name "gnus-mh")
1538   (autoload 'gnus-folder-save-name "gnus-mh")
1539
1540   ;; gnus-vis misc
1541   (autoload 'gnus-group-make-menu-bar "gnus-vis")
1542   (autoload 'gnus-summary-make-menu-bar "gnus-vis")
1543   (autoload 'gnus-server-make-menu-bar "gnus-vis")
1544   (autoload 'gnus-article-make-menu-bar "gnus-vis")
1545   (autoload 'gnus-browse-make-menu-bar "gnus-vis")
1546   (autoload 'gnus-highlight-selected-summary "gnus-vis")
1547   (autoload 'gnus-summary-highlight-line "gnus-vis")
1548   (autoload 'gnus-carpal-setup-buffer "gnus-vis")
1549
1550   ;; gnus-vis article
1551   (autoload 'gnus-article-push-button "gnus-vis" nil t)
1552   (autoload 'gnus-article-press-button "gnus-vis" nil t)
1553   (autoload 'gnus-article-highlight "gnus-vis" nil t)
1554   (autoload 'gnus-article-hide "gnus-vis" nil t)
1555   (autoload 'gnus-article-hide-signature "gnus-vis" nil t)
1556   (autoload 'gnus-article-highlight-headers "gnus-vis" nil t)
1557   (autoload 'gnus-article-highlight-signature "gnus-vis" nil t)
1558   (autoload 'gnus-article-add-buttons "gnus-vis" nil t)
1559   (autoload 'gnus-article-next-button "gnus-vis" nil t)
1560   (autoload 'gnus-article-add-button "gnus-vis")
1561
1562   ;; gnus-cite
1563   (autoload 'gnus-article-highlight-citation "gnus-cite" nil t)
1564   (autoload 'gnus-article-hide-citation-maybe "gnus-cite" nil t)
1565   (autoload 'gnus-article-hide-citation "gnus-cite" nil t)
1566
1567   ;; gnus-kill
1568   (autoload 'gnus-kill "gnus-kill")
1569   (autoload 'gnus-apply-kill-file-internal "gnus-kill")
1570   (autoload 'gnus-kill-file-edit-file "gnus-kill")
1571   (autoload 'gnus-kill-file-raise-followups-to-author "gnus-kill")
1572   (autoload 'gnus-execute "gnus-kill")
1573   (autoload 'gnus-expunge "gnus-kill")
1574
1575   ;; gnus-cache
1576   (autoload 'gnus-cache-possibly-enter-article "gnus-cache")
1577   (autoload 'gnus-cache-save-buffers "gnus-cache")
1578   (autoload 'gnus-cache-possibly-remove-article "gnus-cache")
1579   (autoload 'gnus-cache-request-article "gnus-cache")
1580   (autoload 'gnus-cache-retrieve-headers "gnus-cache")
1581   (autoload 'gnus-cache-possibly-alter-active "gnus-cache")
1582   (autoload 'gnus-jog-cache "gnus-cache" nil t)
1583
1584   ;; gnus-score
1585   (autoload 'gnus-summary-increase-score "gnus-score" nil t)
1586   (autoload 'gnus-summary-lower-score "gnus-score" nil t)
1587   (autoload 'gnus-summary-score-map "gnus-score" nil nil 'keymap)
1588   (autoload 'gnus-score-save "gnus-score")
1589   (autoload 'gnus-score-headers "gnus-score")
1590   (autoload 'gnus-current-score-file-nondirectory "gnus-score")
1591   (autoload 'gnus-score-adaptive "gnus-score")
1592   (autoload 'gnus-score-remove-lines-adaptive "gnus-score")
1593   (autoload 'gnus-possibly-score-headers "gnus-score")
1594   (autoload 'gnus-score-find-trace "gnus-score")
1595
1596   ;; gnus-edit
1597   (autoload 'gnus-score-customize "gnus-edit" nil t)
1598
1599   ;; gnus-uu
1600   (autoload 'gnus-uu-extract-map "gnus-uu" nil nil 'keymap)
1601   (autoload 'gnus-uu-mark-map "gnus-uu" nil nil 'keymap)
1602   (autoload 'gnus-uu-digest-mail-forward "gnus-uu" nil t)
1603   (autoload 'gnus-uu-digest-post-forward "gnus-uu" nil t)
1604   (autoload 'gnus-uu-mark-series "gnus-uu" nil t)
1605   (autoload 'gnus-uu-mark-region "gnus-uu" nil t)
1606   (autoload 'gnus-uu-mark-by-regexp "gnus-uu" nil t)
1607   (autoload 'gnus-uu-mark-all "gnus-uu" nil t)
1608   (autoload 'gnus-uu-mark-sparse "gnus-uu" nil t)
1609   (autoload 'gnus-uu-mark-thread "gnus-uu" nil t)
1610   (autoload 'gnus-uu-decode-uu "gnus-uu" nil t)
1611   (autoload 'gnus-uu-decode-uu-and-save "gnus-uu" nil t)
1612   (autoload 'gnus-uu-decode-unshar "gnus-uu" nil t)
1613   (autoload 'gnus-uu-decode-unshar-and-save "gnus-uu" nil t)
1614   (autoload 'gnus-uu-decode-save "gnus-uu" nil t)
1615   (autoload 'gnus-uu-decode-binhex "gnus-uu" nil t)
1616   (autoload 'gnus-uu-decode-uu-view "gnus-uu" nil t)
1617   (autoload 'gnus-uu-decode-uu-and-save-view "gnus-uu" nil t)
1618   (autoload 'gnus-uu-decode-unshar-view "gnus-uu" nil t)
1619   (autoload 'gnus-uu-decode-unshar-and-save-view "gnus-uu" nil t)
1620   (autoload 'gnus-uu-decode-save-view "gnus-uu" nil t)
1621   (autoload 'gnus-uu-decode-binhex-view "gnus-uu" nil t)
1622
1623   ;; gnus-msg
1624   (autoload 'gnus-summary-send-map "gnus-msg" nil nil 'keymap)
1625   (autoload 'gnus-group-post-news "gnus-msg" nil t)
1626   (autoload 'gnus-group-mail "gnus-msg" nil t)
1627   (autoload 'gnus-summary-post-news "gnus-msg" nil t)
1628   (autoload 'gnus-summary-followup "gnus-msg" nil t)
1629   (autoload 'gnus-summary-followup-with-original "gnus-msg" nil t)
1630   (autoload 'gnus-summary-followup-and-reply "gnus-msg" nil t)
1631   (autoload 'gnus-summary-followup-and-reply-with-original "gnus-msg" nil t)
1632   (autoload 'gnus-summary-cancel-article "gnus-msg" nil t)
1633   (autoload 'gnus-summary-supersede-article "gnus-msg" nil t)
1634   (autoload 'gnus-post-news "gnus-msg" nil t)
1635   (autoload 'gnus-inews-news "gnus-msg" nil t)
1636   (autoload 'gnus-cancel-news "gnus-msg" nil t)
1637   (autoload 'gnus-summary-reply "gnus-msg" nil t)
1638   (autoload 'gnus-summary-reply-with-original "gnus-msg" nil t)
1639   (autoload 'gnus-summary-mail-forward "gnus-msg" nil t)
1640   (autoload 'gnus-summary-mail-other-window "gnus-msg" nil t)
1641   (autoload 'gnus-mail-reply-using-mail "gnus-msg")
1642   (autoload 'gnus-mail-yank-original "gnus-msg")
1643   (autoload 'gnus-mail-send-and-exit "gnus-msg")
1644   (autoload 'gnus-mail-forward-using-mail "gnus-msg")
1645   (autoload 'gnus-mail-other-window-using-mail "gnus-msg")
1646   (autoload 'gnus-article-mail-with-original "gnus-msg")
1647   (autoload 'gnus-article-mail "gnus-msg")
1648
1649   ;; gnus-vm
1650   (autoload 'gnus-summary-save-in-vm "gnus-vm" nil t)
1651   (autoload 'gnus-summary-save-article-vm "gnus-vm" nil t)
1652   (autoload 'gnus-mail-forward-using-vm "gnus-vm")
1653   (autoload 'gnus-mail-reply-using-vm "gnus-vm")
1654   (autoload 'gnus-mail-other-window-using-vm "gnus-vm" nil t)
1655   (autoload 'gnus-yank-article "gnus-vm" nil t)
1656
1657   )
1658
1659 \f
1660
1661 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1662 ;; If you want the cursor to go somewhere else, set these two
1663 ;; functions in some startup hook to whatever you want.
1664 (defalias 'gnus-summary-position-cursor 'gnus-goto-colon)
1665 (defalias 'gnus-group-position-cursor 'gnus-goto-colon)
1666
1667 ;;; Various macros and substs.
1668
1669 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
1670   "Pop to BUFFER, evaluate FORMS, and then returns to original window."
1671   (` (let ((GnusStartBufferWindow (selected-window)))
1672        (unwind-protect
1673            (progn
1674              (pop-to-buffer (, buffer))
1675              (,@ forms))
1676          (select-window GnusStartBufferWindow)))))
1677
1678 (defmacro gnus-gethash (string hashtable)
1679   "Get hash value of STRING in HASHTABLE."
1680   ;;(` (symbol-value (abbrev-symbol (, string) (, hashtable))))
1681   ;;(` (abbrev-expansion (, string) (, hashtable)))
1682   (` (symbol-value (intern-soft (, string) (, hashtable)))))
1683
1684 (defmacro gnus-sethash (string value hashtable)
1685   "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
1686   ;; We cannot use define-abbrev since it only accepts string as value.
1687   ;; (set (intern string hashtable) value))
1688   (` (set (intern (, string) (, hashtable)) (, value))))
1689
1690 (defsubst gnus-buffer-substring (beg end)
1691   (buffer-substring (match-beginning beg) (match-end end)))
1692
1693 (defsubst gnus-simplify-subject-re (subject)
1694   "Remove \"Re:\" from subject lines."
1695   (let ((case-fold-search t))
1696     (if (string-match "^re: *" subject)
1697         (substring subject (match-end 0))
1698       subject)))
1699
1700 (defsubst gnus-goto-char (point)
1701   (and point (goto-char point)))
1702
1703 (defmacro gnus-buffer-exists-p (buffer)
1704   (` (and (, buffer)
1705           (funcall (if (stringp (, buffer)) 'get-buffer 'buffer-name)
1706                    (, buffer)))))
1707
1708 (defmacro gnus-kill-buffer (buffer)
1709   (` (if (gnus-buffer-exists-p (, buffer))
1710          (kill-buffer (, buffer)))))
1711
1712 (defsubst gnus-point-at-bol ()
1713   "Return point at the beginning of line."
1714   (let ((p (point)))
1715     (beginning-of-line)
1716     (prog1
1717         (point)
1718       (goto-char p))))
1719
1720 (defsubst gnus-point-at-eol ()
1721   "Return point at the beginning of line."
1722   (let ((p (point)))
1723     (end-of-line)
1724     (prog1
1725         (point)
1726       (goto-char p))))
1727
1728 ;; Delete the current line (and the next N lines.);
1729 (defmacro gnus-delete-line (&optional n)
1730   (` (delete-region (progn (beginning-of-line) (point))
1731                     (progn (forward-line (, (or n 1))) (point)))))
1732
1733 ;;; Load the compatability functions. 
1734
1735 (require 'gnus-ems)
1736
1737 \f
1738 ;;;
1739 ;;; Gnus Utility Functions
1740 ;;;
1741
1742 (defun gnus-extract-address-components (from)
1743   (let (name address)
1744     ;; First find the address - the thing with the @ in it.  This may
1745     ;; not be accurate in mail addresses, but does the trick most of
1746     ;; the time in news messages.
1747     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
1748         (setq address (substring from (match-beginning 0) (match-end 0))))
1749     ;; Then we check whether the "name <address>" format is used.
1750     (and address
1751          (string-match (concat "<" (regexp-quote address) ">") from)
1752          (and (setq name (substring from 0 (1- (match-beginning 0))))
1753               ;; Strip any quotes from the name.
1754               (string-match "\".*\"" name)
1755               (setq name (substring name 1 (1- (match-end 0))))))
1756     ;; If not, then "address (name)" is used.
1757     (or name
1758         (and (string-match "(.+)" from)
1759              (setq name (substring from (1+ (match-beginning 0)) 
1760                                    (1- (match-end 0))))))
1761     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1762     (list (or name from) (or address from))))
1763
1764 (defun gnus-fetch-field (field)
1765   "Return the value of the header FIELD of current article."
1766   (save-excursion
1767     (save-restriction
1768       (let ((case-fold-search t))
1769         (gnus-narrow-to-headers)
1770         (mail-fetch-field field)))))
1771
1772 (defun gnus-goto-colon ()
1773   (beginning-of-line)
1774   (search-forward ":" (gnus-point-at-eol) t))
1775
1776 (defun gnus-narrow-to-headers ()
1777   (widen)
1778   (save-excursion
1779     (narrow-to-region
1780      (goto-char (point-min))
1781      (if (search-forward "\n\n" nil t)
1782          (1- (point))
1783        (point-max)))))
1784
1785 (defun gnus-update-format-specifications ()
1786   (gnus-make-thread-indent-array)
1787   (setq gnus-summary-line-format-spec 
1788         (gnus-parse-format
1789          gnus-summary-line-format gnus-summary-line-format-alist))
1790   (gnus-update-summary-mark-positions)
1791   (setq gnus-summary-dummy-line-format-spec 
1792         (gnus-parse-format gnus-summary-dummy-line-format 
1793                            gnus-summary-dummy-line-format-alist))
1794   (setq gnus-group-line-format-spec
1795         (gnus-parse-format 
1796          gnus-group-line-format 
1797          gnus-group-line-format-alist))
1798   (if (and (string-match "%D" gnus-group-line-format)
1799            (not gnus-description-hashtb)
1800            gnus-read-active-file)
1801       (gnus-read-all-descriptions-files))
1802   (setq gnus-summary-mode-line-format-spec 
1803         (gnus-parse-format gnus-summary-mode-line-format 
1804                            gnus-summary-mode-line-format-alist))
1805   (setq gnus-article-mode-line-format-spec 
1806         (gnus-parse-format gnus-article-mode-line-format 
1807                            gnus-summary-mode-line-format-alist))
1808   (setq gnus-group-mode-line-format-spec 
1809         (gnus-parse-format gnus-group-mode-line-format 
1810                            gnus-group-mode-line-format-alist)))
1811
1812 (defun gnus-update-summary-mark-positions ()
1813   (save-excursion
1814     (let ((gnus-replied-mark 129)
1815           (gnus-score-below-mark 130)
1816           (gnus-score-over-mark 130)
1817           (thread nil)
1818           pos)
1819       (gnus-set-work-buffer)
1820       (gnus-summary-insert-line 
1821        nil [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
1822       (goto-char (point-min))
1823       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
1824                                          (- (point) 2)))))
1825       (goto-char (point-min))
1826       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
1827                                           (- (point) 2))) pos))
1828       (goto-char (point-min))
1829       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
1830                                         (- (point) 2))) pos))
1831       (setq gnus-summary-mark-positions pos))))
1832
1833 (defun gnus-format-max-width (form length)
1834   (let* ((val (eval form))
1835          (valstr (if (numberp val) (int-to-string val) val)))
1836     (if (> (length valstr) length)
1837         (substring valstr 0 length)
1838       valstr)))
1839
1840 (defun gnus-set-mouse-face (string)
1841   ;; Set mouse face property on STRING.
1842   (put-text-property 0 (length string) 'mouse-face gnus-mouse-face string)
1843   string)
1844
1845 (defun gnus-parse-format (format spec-alist)
1846   ;; This function parses the FORMAT string with the help of the
1847   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1848   ;; string.  If the FORMAT string contains the specifiers %( and %)
1849   ;; the text between them will have the mouse-face text property.
1850   (if (string-match "\\`\\(.*\\)%(\\(.*\\)%)\\(.*\n?\\)\\'" format)
1851       (if (and gnus-visual gnus-mouse-face)
1852           (let ((pre (substring format (match-beginning 1) (match-end 1)))
1853                 (button (substring format (match-beginning 2) (match-end 2)))
1854                 (post (substring format (match-beginning 3) (match-end 3))))
1855             (list 'concat
1856                   (gnus-parse-simple-format pre spec-alist)
1857                   (list 'gnus-set-mouse-face
1858                         (gnus-parse-simple-format button spec-alist))
1859                   (gnus-parse-simple-format post spec-alist)))
1860         (gnus-parse-simple-format
1861          (concat (substring format (match-beginning 1) (match-end 1))
1862                  (substring format (match-beginning 2) (match-end 2))
1863                  (substring format (match-beginning 3) (match-end 3)))
1864          spec-alist))
1865     (gnus-parse-simple-format format spec-alist)))
1866
1867 (defun gnus-parse-simple-format (format spec-alist)
1868   ;; This function parses the FORMAT string with the help of the
1869   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1870   ;; string. The list will consist of the symbol `format', a format
1871   ;; specification string, and a list of forms depending on the
1872   ;; SPEC-ALIST.
1873   (let ((max-width 0)
1874         spec flist fstring b newspec max-width elem beg)
1875     (save-excursion
1876       (gnus-set-work-buffer)
1877       (insert format)
1878       (goto-char (point-min))
1879       (while (re-search-forward "%[-0-9]*\\(,[0-9]*\\)*\\(.\\)\\(.\\)?" nil t)
1880         (setq spec (string-to-char (buffer-substring (match-beginning 2)
1881                                                      (match-end 2))))
1882         ;; First check if there are any specs that look anything like
1883         ;; "%12,12A", ie. with a "max width specification". These have
1884         ;; to be treated specially.
1885         (if (setq beg (match-beginning 1))
1886             (setq max-width 
1887                   (string-to-int 
1888                    (buffer-substring (1+ (match-beginning 1)) (match-end 1))))
1889           (setq max-width 0)
1890           (setq beg (match-beginning 2)))
1891         ;; Find the specification from `spec-alist'.
1892         (if (not (setq elem (cdr (assq spec spec-alist))))
1893             (setq elem '("*" ?s)))
1894         ;; Treat user defined format specifiers specially
1895         (and (eq (car elem) 'user-defined)
1896              (setq elem
1897                    (list 
1898                     (list (intern (concat "gnus-user-format-function-"
1899                                           (buffer-substring
1900                                            (match-beginning 3)
1901                                            (match-end 3))))
1902                           'header)
1903                     ?s))
1904              (delete-region (match-beginning 3) (match-end 3)))
1905         (if (not (zerop max-width))
1906             (let ((el (car elem)))
1907               (cond ((= (car (cdr elem)) ?c) 
1908                      (setq el (list 'char-to-string el)))
1909                     ((= (car (cdr elem)) ?d)
1910                      (numberp el) (setq el (list 'int-to-string el))))
1911               (setq flist (cons (list 'gnus-format-max-width el max-width) 
1912                                 flist))
1913               (setq newspec ?s))
1914           (setq flist (cons (car elem) flist))
1915           (setq newspec (car (cdr elem))))
1916         ;; Remove the old specification (and possibly a ",12" string).
1917         (delete-region beg (match-end 2))
1918         ;; Insert the new specification.
1919         (goto-char beg)
1920         (insert newspec))
1921       (setq fstring (buffer-substring 1 (point-max))))
1922     (cons 'format (cons fstring (nreverse flist)))))
1923
1924 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
1925 (defun gnus-read-init-file ()
1926   (and gnus-init-file
1927        (or (and (file-exists-p gnus-init-file) 
1928                 ;; Don't try to load a directory.
1929                 (not (file-directory-p gnus-init-file)))
1930            (file-exists-p (concat gnus-init-file ".el"))
1931            (file-exists-p (concat gnus-init-file ".elc")))
1932        (load gnus-init-file nil t)))
1933
1934 (defun gnus-set-work-buffer ()
1935   (if (get-buffer gnus-work-buffer)
1936       (progn
1937         (set-buffer gnus-work-buffer)
1938         (erase-buffer))
1939     (set-buffer (get-buffer-create gnus-work-buffer))
1940     (kill-all-local-variables)
1941     (buffer-disable-undo (current-buffer))
1942     (gnus-add-current-to-buffer-list)))
1943
1944 ;; Article file names when saving.
1945
1946 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
1947   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1948 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
1949 Otherwise, it is like ~/News/news/group/num."
1950   (let ((default
1951           (expand-file-name
1952            (concat (if (gnus-use-long-file-name 'not-save)
1953                        (gnus-capitalize-newsgroup newsgroup)
1954                      (gnus-newsgroup-directory-form newsgroup))
1955                    "/" (int-to-string (header-number headers)))
1956            (or gnus-article-save-directory "~/News"))))
1957     (if (and last-file
1958              (string-equal (file-name-directory default)
1959                            (file-name-directory last-file))
1960              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1961         default
1962       (or last-file default))))
1963
1964 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
1965   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1966 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group/num.
1967 Otherwise, it is like ~/News/news/group/num."
1968   (let ((default
1969           (expand-file-name
1970            (concat (if (gnus-use-long-file-name 'not-save)
1971                        newsgroup
1972                      (gnus-newsgroup-directory-form newsgroup))
1973                    "/" (int-to-string (header-number headers)))
1974            (or gnus-article-save-directory "~/News"))))
1975     (if (and last-file
1976              (string-equal (file-name-directory default)
1977                            (file-name-directory last-file))
1978              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1979         default
1980       (or last-file default))))
1981
1982 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
1983   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1984 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group.
1985 Otherwise, it is like ~/News/news/group/news."
1986   (or last-file
1987       (expand-file-name
1988        (if (gnus-use-long-file-name 'not-save)
1989            (gnus-capitalize-newsgroup newsgroup)
1990          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1991        (or gnus-article-save-directory "~/News"))))
1992
1993 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
1994   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1995 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group.
1996 Otherwise, it is like ~/News/news/group/news."
1997   (or last-file
1998       (expand-file-name
1999        (if (gnus-use-long-file-name 'not-save)
2000            newsgroup
2001          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2002        (or gnus-article-save-directory "~/News"))))
2003
2004 ;; For subscribing new newsgroup
2005
2006 (defun gnus-subscribe-hierarchical-interactive (groups)
2007   (let ((groups (sort groups 'string<))
2008         prefixes prefix start rest ans group starts)
2009     (while groups
2010       (setq prefixes (list "^"))
2011       (while (and groups prefixes)
2012         (while (not (string-match (car prefixes) (car groups)))
2013           (setq prefixes (cdr prefixes)))
2014         (setq prefix (car prefixes))
2015         (setq start (1- (length prefix)))
2016         (if (and (string-match "[^\\.]\\." (car groups) start)
2017                  (cdr groups)
2018                  (setq prefix 
2019                        (concat "^" (substring (car groups) 0 (match-end 0))))
2020                  (string-match prefix (car (cdr groups))))
2021             (progn
2022               (setq prefixes (cons prefix prefixes))
2023               (message "Descend hierarchy %s? ([y]nsq): " 
2024                        (substring prefix 1 (1- (length prefix))))
2025               (setq ans (read-char))
2026               (cond ((= ans ?n)
2027                      (while (and groups 
2028                                  (string-match prefix 
2029                                                (setq group (car groups))))
2030                        (setq gnus-killed-list 
2031                              (cons group gnus-killed-list))
2032                        (gnus-sethash group group gnus-killed-hashtb)
2033                        (setq groups (cdr groups)))
2034                      (setq starts (cdr starts)))
2035                     ((= ans ?s)
2036                      (while (and groups 
2037                                  (string-match prefix 
2038                                                (setq group (car groups))))
2039                        (gnus-sethash group group gnus-killed-hashtb)
2040                        (gnus-subscribe-alphabetically (car groups))
2041                        (setq groups (cdr groups)))
2042                      (setq starts (cdr starts)))
2043                     ((= ans ?q)
2044                      (while groups
2045                        (setq group (car groups))
2046                        (setq gnus-killed-list (cons group gnus-killed-list))
2047                        (gnus-sethash group group gnus-killed-hashtb)
2048                        (setq groups (cdr groups))))
2049                     (t nil)))
2050           (message "Subscribe %s? ([n]yq)" (car groups))
2051           (setq ans (read-char))
2052           (setq group (car groups))
2053           (cond ((= ans ?y)
2054                  (gnus-subscribe-alphabetically (car groups))
2055                  (gnus-sethash group group gnus-killed-hashtb))
2056                 ((= ans ?q)
2057                  (while groups
2058                    (setq group (car groups))
2059                    (setq gnus-killed-list (cons group gnus-killed-list))
2060                    (gnus-sethash group group gnus-killed-hashtb)
2061                    (setq groups (cdr groups))))
2062                 (t 
2063                  (setq gnus-killed-list (cons group gnus-killed-list))
2064                  (gnus-sethash group group gnus-killed-hashtb)))
2065           (setq groups (cdr groups)))))))
2066
2067 (defun gnus-subscribe-randomly (newsgroup)
2068   "Subscribe new NEWSGROUP by making it the first newsgroup."
2069   (gnus-subscribe-newsgroup newsgroup))
2070
2071 (defun gnus-subscribe-alphabetically (newgroup)
2072   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2073   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2074   (let ((groups (cdr gnus-newsrc-alist))
2075         before)
2076     (while (and (not before) groups)
2077       (if (string< newgroup (car (car groups)))
2078           (setq before (car (car groups)))
2079         (setq groups (cdr groups))))
2080     (gnus-subscribe-newsgroup newgroup before)))
2081
2082 (defun gnus-subscribe-hierarchically (newgroup)
2083   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2084   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2085   (save-excursion
2086     (set-buffer (find-file-noselect gnus-current-startup-file))
2087     (let ((groupkey newgroup)
2088           before)
2089       (while (and (not before) groupkey)
2090         (goto-char (point-min))
2091         (let ((groupkey-re
2092                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2093           (while (and (re-search-forward groupkey-re nil t)
2094                       (progn
2095                         (setq before (buffer-substring
2096                                       (match-beginning 1) (match-end 1)))
2097                         (string< before newgroup)))))
2098         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2099         (setq groupkey
2100               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2101                   (substring groupkey (match-beginning 1) (match-end 1)))))
2102       (gnus-subscribe-newsgroup newgroup before))))
2103
2104 (defun gnus-subscribe-interactively (newsgroup)
2105   "Subscribe new NEWSGROUP interactively.
2106 It is inserted in hierarchical newsgroup order if subscribed. If not,
2107 it is killed."
2108   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " newsgroup))
2109       (gnus-subscribe-hierarchically newsgroup)
2110     (setq gnus-killed-list (cons newsgroup gnus-killed-list))))
2111
2112 (defun gnus-subscribe-zombies (newsgroup)
2113   "Make new NEWSGROUP a zombie group."
2114   (setq gnus-zombie-list (cons newsgroup gnus-zombie-list)))
2115
2116 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2117   "Subscribe new NEWSGROUP.
2118 If NEXT is non-nil, it is inserted before NEXT. Otherwise it is made
2119 the first newsgroup."
2120   ;; We subscribe the group by changing its level to `subscribed'.
2121   (gnus-group-change-level 
2122    newsgroup gnus-level-default-subscribed
2123    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2124   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2125
2126 ;; For directories
2127
2128 (defun gnus-newsgroup-directory-form (newsgroup)
2129   "Make hierarchical directory name from NEWSGROUP name."
2130   (let ((newsgroup (substring newsgroup 0)) ;Copy string.
2131         (len (length newsgroup))
2132         idx)
2133     ;; If this is a foreign group, we don't want to translate the
2134     ;; entire name.  
2135     (if (setq idx (string-match ":" newsgroup))
2136         (aset newsgroup idx ?/)
2137       (setq idx 0))
2138     ;; Replace all occurrences of `.' with `/'.
2139     (while (< idx len)
2140       (if (= (aref newsgroup idx) ?.)
2141           (aset newsgroup idx ?/))
2142       (setq idx (1+ idx)))
2143     newsgroup))
2144
2145 (defun gnus-make-directory (dir)
2146   "Make DIRECTORY recursively."
2147   (let* ((dir (expand-file-name dir default-directory))
2148          dirs)
2149     (if (string-match "/$" dir)
2150         (setq dir (substring dir 0 (match-beginning 0))))
2151     (while (not (file-exists-p dir))
2152       (setq dirs (cons dir dirs))
2153       (string-match "/[^/]+$" dir)
2154       (setq dir (substring dir 0 (match-beginning 0))))
2155     (while dirs
2156       (make-directory (car dirs))
2157       (setq dirs (cdr dirs)))))
2158
2159 (defun gnus-capitalize-newsgroup (newsgroup)
2160   "Capitalize NEWSGROUP name."
2161   (and (not (zerop (length newsgroup)))
2162        (concat (char-to-string (upcase (aref newsgroup 0)))
2163                (substring newsgroup 1))))
2164
2165 ;; Var
2166
2167 (defun gnus-simplify-subject (subject &optional re-only)
2168   "Remove `Re:' and words in parentheses.
2169 If optional argument RE-ONLY is non-nil, strip `Re:' only."
2170   (let ((case-fold-search t))           ;Ignore case.
2171     ;; Remove `Re:' and `Re^N:'.
2172     (if (string-match "^re:[ \t]*" subject)
2173         (setq subject (substring subject (match-end 0))))
2174     ;; Remove words in parentheses from end.
2175     (or re-only
2176         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2177           (setq subject (substring subject 0 (match-beginning 0)))))
2178     ;; Return subject string.
2179     subject))
2180
2181 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
2182 ;; all whitespace.
2183 (defun gnus-simplify-subject-fuzzy (subject)
2184   (let ((case-fold-search t))
2185     (save-excursion
2186       (gnus-set-work-buffer)
2187       (insert subject)
2188       (gnus-simplify-buffer-fuzzy)
2189       (buffer-string))))
2190
2191 (defun gnus-simplify-buffer-fuzzy ()
2192   (goto-char (point-min))
2193   (while (re-search-forward "^[ \t]*re:[ \t]*" nil t)
2194     (replace-match "" t t))
2195   (goto-char (point-min))
2196   (while (re-search-forward "[ \t\n]*([^()]*)[ \t\n]*$" nil t)
2197     (replace-match "" t t))
2198   (goto-char (point-min))
2199   (while (re-search-forward "[ \t]+" nil t)
2200     (replace-match " " t t))
2201   (goto-char (point-min))
2202   (while (re-search-forward "[ \t]+$" nil t)
2203     (replace-match "" t t))
2204   (goto-char (point-min))
2205   (while (re-search-forward "^[ \t]+" nil t)
2206     (replace-match "" t t)))
2207
2208 ;; Add the current buffer to the list of buffers to be killed on exit. 
2209 (defun gnus-add-current-to-buffer-list ()
2210   (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list)))
2211
2212 (defun gnus-string> (s1 s2)
2213   (not (or (string< s1 s2)
2214            (string= s1 s2))))
2215
2216 ;; Functions accessing headers.
2217 ;; Functions are more convenient than macros in some cases.
2218
2219 (defun gnus-header-number (header)
2220   (header-number header))
2221
2222 (defun gnus-header-subject (header)
2223   (header-subject header))
2224
2225 (defun gnus-header-from (header)
2226   (header-from header))
2227
2228 (defun gnus-header-xref (header)
2229   (header-xref header))
2230
2231 (defun gnus-header-lines (header)
2232   (header-lines header))
2233
2234 (defun gnus-header-date (header)
2235   (header-date header))
2236
2237 (defun gnus-header-id (header)
2238   (header-id header))
2239
2240 (defun gnus-header-references (header)
2241   (header-references header))
2242
2243 ;;; General various misc type functions.
2244
2245 (defun gnus-clear-system ()
2246   "Clear all variables and buffers."
2247   ;; Clear Gnus variables.
2248   (let ((variables gnus-variable-list))
2249     (while variables
2250       (set (car variables) nil)
2251       (setq variables (cdr variables))))
2252   ;; Clear other internal variables.
2253   (setq gnus-list-of-killed-groups nil
2254         gnus-have-read-active-file nil
2255         gnus-newsrc-alist nil
2256         gnus-newsrc-hashtb nil
2257         gnus-killed-list nil
2258         gnus-zombie-list nil
2259         gnus-killed-hashtb nil
2260         gnus-active-hashtb nil
2261         gnus-moderated-list nil
2262         gnus-description-hashtb nil
2263         gnus-newsgroup-headers nil
2264         gnus-newsgroup-headers-hashtb-by-number nil
2265         gnus-newsgroup-name nil
2266         gnus-server-alist nil
2267         gnus-current-select-method nil)
2268   ;; Reset any score variables.
2269   (and (boundp 'gnus-score-cache)
2270        (set 'gnus-score-cache nil))
2271   (and (boundp 'gnus-internal-global-score-files)
2272        (set 'gnus-internal-global-score-files nil))
2273   ;; Kill the startup file.
2274   (and gnus-current-startup-file
2275        (get-file-buffer gnus-current-startup-file)
2276        (kill-buffer (get-file-buffer gnus-current-startup-file)))
2277   ;; Save any cache buffers.
2278   (and gnus-use-cache (gnus-cache-save-buffers))
2279   ;; Clear the dribble buffer.
2280   (gnus-dribble-clear)
2281   ;; Kill global KILL file buffer.
2282   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
2283       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
2284   (gnus-kill-buffer nntp-server-buffer)
2285   ;; Kill Gnus buffers.
2286   (while gnus-buffer-list
2287     (gnus-kill-buffer (car gnus-buffer-list))
2288     (setq gnus-buffer-list (cdr gnus-buffer-list))))
2289
2290 (defun gnus-windows-old-to-new (setting)
2291   (if (symbolp setting)
2292       (setq setting 
2293             (cond ((eq setting 'SelectArticle)
2294                    'article)
2295                   ((eq setting 'SelectSubject)
2296                    'summary)
2297                   ((eq setting 'SelectNewsgroup)
2298                    'group)
2299                   (t setting))))
2300   (if (or (listp setting)
2301           (not (and gnus-window-configuration
2302                     (memq setting '(group summary article)))))
2303       setting
2304     (let* ((setting (if (eq setting 'group) 
2305                         (if (assq 'newsgroup gnus-window-configuration)
2306                             'newsgroup
2307                           'newsgroups) setting))
2308            (elem (car (cdr (assq setting gnus-window-configuration))))
2309            (total (apply '+ elem))
2310            (types '(group summary article))
2311            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
2312            (i 0)
2313            perc
2314            out)
2315       (while (< i 3)
2316         (or (zerop (nth i elem))
2317             (progn
2318               (setq perc  (/ (* 1.0 (nth 0 elem)) total))
2319               (setq out (cons (if (eq pbuf (nth i types))
2320                                   (vector (nth i types) perc 'point)
2321                                 (vector (nth i types) perc))
2322                               out))))
2323         (setq i (1+ i)))
2324       (list (nreverse out)))))
2325            
2326 (defun gnus-configure-windows (setting)
2327   (setq setting (gnus-windows-old-to-new setting))
2328   (let ((r (if (symbolp setting)
2329                   (cdr (assq setting gnus-buffer-configuration))
2330                 setting))
2331         (in-buf (current-buffer))
2332         rule val window w height hor ohor heights sub jump-buffer
2333         rel total to-buf)
2334     (or r (error "No such setting: %s" setting))
2335
2336     ;; Either remove all windows or just remove all Gnus windows.
2337     (if gnus-use-full-window
2338         (delete-other-windows)
2339       (gnus-remove-some-windows)
2340       (switch-to-buffer nntp-server-buffer))
2341
2342     (while r
2343       (setq hor (car r)
2344             ohor nil)
2345
2346       ;; We have to do the (possible) horizontal splitting before the
2347       ;; vertical. 
2348       (if (and (listp (car hor)) 
2349                (eq (car (car hor)) 'horizontal))
2350           (progn
2351             (split-window nil (- (frame-width) 
2352                                  (floor (* (frame-width) (nth 1 (car hor)))))
2353                           t)
2354             (setq hor (cdr hor))))
2355
2356       ;; Go through the rules and eval the elements that are to be
2357       ;; evaled.  
2358       (while hor
2359         (if (setq val (if (vectorp (car hor)) (car hor) (eval (car hor))))
2360             (progn
2361               ;; Expand short buffer name.
2362               (setq w (aref val 0))
2363               (and (setq w (cdr (assq w gnus-window-to-buffer)))
2364                    (progn
2365                      (setq val (apply 'vector (mapcar (lambda (v) v) val)))
2366                      (aset val 0 w)))
2367               (setq ohor (cons val ohor))))
2368         (setq hor (cdr hor)))
2369       (setq rule (cons (nreverse ohor) rule))
2370       (setq r (cdr r)))
2371     (setq rule (nreverse rule))
2372
2373     ;; We tally the window sizes.
2374     (setq total (window-height))
2375     (while rule
2376       (setq hor (car rule))
2377       (if (and (listp (car hor)) (eq (car (car hor)) 'horizontal))
2378           (setq hor (cdr hor)))
2379       (setq sub 0)
2380       (while hor
2381         (setq rel (aref (car hor) 1)
2382               heights (cons
2383                        (cond ((and (floatp rel) (= 1.0 rel))
2384                               'x)
2385                              ((integerp rel)
2386                               rel)
2387                              (t
2388                               (max (floor (* total rel)) 4)))
2389                        heights)
2390               sub (+ sub (if (numberp (car heights)) (car heights) 0))
2391               hor (cdr hor)))
2392       (setq heights (nreverse heights)
2393             hor (car rule))
2394
2395       ;; We then go through these heighs and create windows for them.
2396       (while heights
2397         (setq height (car heights)
2398               heights (cdr heights))
2399         (and (eq height 'x)
2400              (setq height (- total sub)))
2401         (and heights
2402              (split-window nil height))
2403         (setq to-buf (aref (car hor) 0))
2404         (switch-to-buffer 
2405          (cond ((not to-buf)
2406                 in-buf)
2407                ((symbolp to-buf)
2408                 (symbol-value (aref (car hor) 0)))
2409                (t
2410                 (aref (car hor) 0))))
2411         (and (> (length (car hor)) 2)
2412              (eq (aref (car hor) 2) 'point)
2413              (setq jump-buffer (current-buffer)))
2414         (other-window 1)
2415         (setq hor (cdr hor)))
2416       
2417       (setq rule (cdr rule)))
2418
2419     ;; Finally, we pop to the buffer that's supposed to have point. 
2420     (or jump-buffer (error "Missing `point' in spec for %s" setting))
2421
2422     (select-window (get-buffer-window jump-buffer))
2423     jump-buffer))
2424       
2425 (defun gnus-remove-some-windows ()
2426   (let ((buffers gnus-window-to-buffer)
2427         (first t)
2428         buf)
2429     (while buffers
2430       (setq buf (cdr (car buffers)))
2431       (if (symbolp buf)
2432           (setq buf (and (boundp buf) (symbol-value buf))))
2433       (and buf 
2434            (get-buffer-window buf)
2435            (progn
2436              (set-buffer buf)
2437              (if first
2438                  (progn
2439                    (switch-to-buffer nntp-server-buffer)
2440                    (setq first nil))
2441                (delete-window (get-buffer-window buf)))))
2442       (setq buffers (cdr buffers)))
2443     (set-buffer nntp-server-buffer)))
2444
2445 (defun gnus-version ()
2446   "Version numbers of this version of Gnus."
2447   (interactive)
2448   (let ((methods gnus-valid-select-methods)
2449         (mess gnus-version)
2450         meth)
2451     ;; Go through all the legal select methods and add their version
2452     ;; numbers to the total version string. Only the backends that are
2453     ;; currently in use will have their message numbers taken into
2454     ;; consideration. 
2455     (while methods
2456       (setq meth (intern (concat (car (car methods)) "-version")))
2457       (and (boundp meth)
2458            (stringp (symbol-value meth))
2459            (setq mess (concat mess "; " (symbol-value meth))))
2460       (setq methods (cdr methods)))
2461     (gnus-message 2 mess)))
2462
2463 (defun gnus-info-find-node ()
2464   "Find Info documentation of Gnus."
2465   (interactive)
2466   ;; Enlarge info window if needed.
2467   (let ((mode major-mode))
2468     (gnus-configure-windows 'info)
2469     (Info-goto-node (car (cdr (assq mode gnus-info-nodes))))))
2470
2471 (defun gnus-bug ()
2472   "Send a bug report to the Gnus maintainers."
2473   (interactive)
2474   (let ((winconf (current-window-configuration)))
2475     (delete-other-windows)
2476     (switch-to-buffer "*Gnus Bug Help*")
2477     (erase-buffer)
2478     (insert gnus-bug-message)
2479     (goto-char (point-min))
2480     (pop-to-buffer "*Gnus Bug*")
2481     (erase-buffer)
2482     (mail-mode)
2483     (mail-setup gnus-maintainer nil nil nil nil nil)
2484     (make-local-variable 'gnus-prev-winconf)
2485     (setq gnus-prev-winconf winconf)
2486     (goto-char (point-min))
2487     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
2488     (forward-line 1)
2489     (insert (format "%s\n%s\n\n\n\n\n" (gnus-version) (emacs-version)))
2490     (let ((b (point)))
2491       (gnus-debug)
2492       (goto-char (- b 3)))
2493     (message "")))
2494
2495 (defun gnus-debug ()
2496   "Attemps to go through the Gnus source file and report what variables have been changed.
2497 The source file has to be in the Emacs load path."
2498   (interactive)
2499   (let ((files '("gnus.el" "gnus-msg.el" "gnus-score.el"))
2500         file dirs expr olist)
2501     (save-excursion
2502       (set-buffer (get-buffer-create " *gnus bug info*"))
2503       (buffer-disable-undo (current-buffer))
2504       (message "Please wait while we snoop your variables...")
2505       (sit-for 0)
2506       (while files
2507         (erase-buffer)
2508         (setq dirs load-path)
2509         (while dirs
2510           (if (or (not (car dirs))
2511                   (not (stringp (car dirs)))
2512                   (not (file-exists-p 
2513                         (setq file (concat (file-name-as-directory 
2514                                             (car dirs)) (car files))))))
2515               (setq dirs (cdr dirs))
2516             (setq dirs nil)
2517             (insert-file-contents file)
2518             (goto-char (point-min))
2519             (or (re-search-forward "^;;* Internal variables" nil t)
2520                 (error "Malformed sources in file %s" file))
2521             (narrow-to-region (point-min) (point))
2522             (goto-char (point-min))
2523             (while (setq expr (condition-case () 
2524                                   (read (current-buffer)) (error nil)))
2525               (and (eq (car expr) 'defvar)
2526                    (stringp (nth 3 expr))
2527                    (or (not (boundp (nth 1 expr)))
2528                        (not (equal (eval (nth 2 expr))
2529                                    (symbol-value (nth 1 expr)))))
2530                    (setq olist (cons (nth 1 expr) olist))))))
2531         (setq files (cdr files)))
2532       (kill-buffer (current-buffer)))
2533     (insert "------------------- Environment follows -------------------\n\n")
2534     (while olist
2535       (if (boundp (car olist))
2536           (insert "(setq " (symbol-name (car olist)) " '" 
2537                   (prin1-to-string (symbol-value (car olist))) ")\n")
2538         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
2539       (setq olist (cdr olist)))
2540     (insert "\n\n")))
2541
2542 (defun gnus-overload-functions (&optional overloads)
2543   "Overload functions specified by optional argument OVERLOADS.
2544 If nothing is specified, use the variable gnus-overload-functions."
2545   (let ((defs nil)
2546         (overloads (or overloads gnus-overload-functions)))
2547     (while overloads
2548       (setq defs (car overloads))
2549       (setq overloads (cdr overloads))
2550       ;; Load file before overloading function if necessary.  Make
2551       ;; sure we cannot use `require' always.
2552       (and (not (fboundp (car defs)))
2553            (car (cdr (cdr defs)))
2554            (load (car (cdr (cdr defs))) nil 'nomessage))
2555       (fset (car defs) (car (cdr defs))))))
2556
2557 (defun gnus-replace-chars-in-string (string from to)
2558   "Replace characters in STRING from FROM to TO."
2559   (let ((string (substring string 0))   ;Copy string.
2560         (len (length string))
2561         (idx 0))
2562     ;; Replace all occurrences of FROM with TO.
2563     (while (< idx len)
2564       (if (= (aref string idx) from)
2565           (aset string idx to))
2566       (setq idx (1+ idx)))
2567     string))
2568
2569 (defun gnus-days-between (date1 date2)
2570   ;; Return the number of days between date1 and date2.
2571   (- (gnus-day-number date1) (gnus-day-number date2)))
2572
2573 (defun gnus-day-number (date)
2574   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
2575                      (timezone-parse-date date))))
2576     (timezone-absolute-from-gregorian 
2577      (nth 1 dat) (nth 2 dat) (car dat))))
2578
2579 ;; Returns a floating point number that says how many seconds have
2580 ;; lapsed between Jan 1 12:00:00 1970 and DATE.
2581 (defun gnus-seconds-since-epoch (date)
2582   (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2583                         (timezone-parse-date date)))
2584          (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
2585                         (timezone-parse-time
2586                          (aref (timezone-parse-date date) 3))))
2587          (edate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2588                         (timezone-parse-date "Jan 1 12:00:00 1970")))
2589          (tday (- (timezone-absolute-from-gregorian 
2590                    (nth 1 tdate) (nth 2 tdate) (nth 0 tdate))
2591                   (timezone-absolute-from-gregorian 
2592                    (nth 1 edate) (nth 2 edate) (nth 0 edate)))))
2593     (+ (nth 2 ttime)
2594        (* (nth 1 ttime) 60)
2595        (* 1.0 (nth 0 ttime) 60 60)
2596        (* 1.0 tday 60 60 24))))
2597
2598 (defun gnus-file-newer-than (file date)
2599   (let ((fdate (nth 5 (file-attributes file))))
2600     (or (> (car fdate) (car date))
2601         (and (= (car fdate) (car date))
2602              (> (nth 1 fdate) (nth 1 date))))))
2603
2604 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
2605 ;; the echo area.
2606 (defun gnus-y-or-n-p (prompt)
2607   (prog1
2608       (y-or-n-p prompt)
2609     (message "")))
2610
2611 (defun gnus-yes-or-no-p (prompt)
2612   (prog1
2613       (yes-or-no-p prompt)
2614     (message "")))
2615
2616 ;; Check whether to use long file names.
2617 (defun gnus-use-long-file-name (symbol)
2618   ;; The variable has to be set...
2619   (and gnus-use-long-file-name
2620        ;; If it isn't a list, then we return t.
2621        (or (not (listp gnus-use-long-file-name))
2622            ;; If it is a list, and the list contains `symbol', we
2623            ;; return nil.  
2624            (not (memq symbol gnus-use-long-file-name)))))
2625
2626 ;; I suspect there's a better way, but I haven't taken the time to do
2627 ;; it yet. -erik selberg@cs.washington.edu
2628 (defun gnus-dd-mmm (messy-date)
2629   "Return a string like DD-MMM from a big messy string"
2630   (let ((datevec (timezone-parse-date messy-date)))
2631     (format "%2s-%s"
2632             (or (aref datevec 2) "??")
2633             (capitalize
2634              (or (car 
2635                   (nth (1- (string-to-number (aref datevec 1)))
2636                        timezone-months-assoc))
2637                  "???")))))
2638
2639 ;; Make a hash table (default and minimum size is 255).
2640 ;; Optional argument HASHSIZE specifies the table size.
2641 (defun gnus-make-hashtable (&optional hashsize)
2642   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
2643
2644 ;; Make a number that is suitable for hashing; bigger than MIN and one
2645 ;; less than 2^x.
2646 (defun gnus-create-hash-size (min)
2647   (let ((i 1))
2648     (while (< i min)
2649       (setq i (* 2 i)))
2650     (1- i)))
2651
2652 ;; Show message if message has a lower level than `gnus-verbose'. 
2653 ;; Guide-line for numbers:
2654 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
2655 ;; for things that take a long time, 7 - not very important messages
2656 ;; on stuff, 9 - messages inside loops.
2657 (defun gnus-message (level &rest args)
2658   (if (<= level gnus-verbose)
2659       (apply 'message args)
2660     ;; We have to do this format thingie here even if the result isn't
2661     ;; shown - the return value has to be the same as the return value
2662     ;; from `message'.
2663     (apply 'format args)))
2664
2665 ;; Generate a unique new group name.
2666 (defun gnus-generate-new-group-name (leaf)
2667   (let ((name leaf)
2668         (num 0))
2669     (while (gnus-gethash name gnus-newsrc-hashtb)
2670       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
2671     name))
2672
2673 (defun gnus-find-file-noselect (file &optional force)
2674   "Does vaguely the same as find-file-noselect. No hooks are run."
2675   (let (buf insert)
2676     (if (setq buf (get-file-buffer file))
2677         (setq insert force)
2678       (setq buf (create-file-buffer file))
2679       (setq insert t))
2680     (if (not insert)
2681         buf
2682       (save-excursion
2683         (set-buffer buf)
2684         (erase-buffer)
2685         (and (file-readable-p file)
2686              (insert-file-contents file))
2687         (set-visited-file-name file)
2688         (set-buffer-modified-p nil)
2689         (current-buffer)))))
2690
2691 ;;; List and range functions
2692
2693 (defun gnus-last-element (list)
2694   "Return last element of LIST."
2695   (while (cdr list)
2696     (setq list (cdr list)))
2697   (car list))
2698
2699 (defun gnus-copy-sequence (list)
2700   "Do a complete, total copy of a list."
2701   (if (and (consp list) (not (consp (cdr list))))
2702       (cons (car list) (cdr list))
2703     (mapcar (lambda (elem) (if (consp elem) 
2704                                (if (consp (cdr elem))
2705                                    (gnus-copy-sequence elem)
2706                                  (cons (car elem) (cdr elem)))
2707                              elem))
2708             list)))
2709
2710 (defun gnus-set-difference (list1 list2)
2711   "Return a list of elements of LIST1 that do not appear in LIST2."
2712   (let ((list1 (copy-sequence list1)))
2713     (while list2
2714       (setq list1 (delq (car list2) list1))
2715       (setq list2 (cdr list2)))
2716     list1))
2717
2718 (defun gnus-sorted-complement (list1 list2)
2719   "Return a list of elements of LIST1 that do not appear in LIST2.
2720 Both lists have to be sorted over <."
2721   (let (out)
2722     (if (or (null list1) (null list2))
2723         (or list1 list2)
2724       (while (and list1 list2)
2725         (cond ((= (car list1) (car list2))
2726                (setq list1 (cdr list1)
2727                      list2 (cdr list2)))
2728               ((< (car list1) (car list2))
2729                (setq out (cons (car list1) out))
2730                (setq list1 (cdr list1)))
2731               (t
2732                (setq out (cons (car list2) out))
2733                (setq list2 (cdr list2)))))
2734       (nconc (nreverse out) (or list1 list2)))))
2735
2736 (defun gnus-intersection (list1 list2)      
2737   (let ((result nil))
2738     (while list2
2739       (if (memq (car list2) list1)
2740           (setq result (cons (car list2) result)))
2741       (setq list2 (cdr list2)))
2742     result))
2743
2744 (defun gnus-sorted-intersection (list1 list2)
2745   ;; LIST1 and LIST2 have to be sorted over <.
2746   (let (out)
2747     (while (and list1 list2)
2748       (cond ((= (car list1) (car list2))
2749              (setq out (cons (car list1) out)
2750                    list1 (cdr list1)
2751                    list2 (cdr list2)))
2752             ((< (car list1) (car list2))
2753              (setq list1 (cdr list1)))
2754             (t
2755              (setq list2 (cdr list2)))))
2756     (nreverse out)))
2757
2758 (defun gnus-set-sorted-intersection (list1 list2)
2759   ;; LIST1 and LIST2 have to be sorted over <.
2760   ;; This function modifies LIST1.
2761   (let* ((top (cons nil list1))
2762          (prev top))
2763   (while (and list1 list2)
2764     (cond ((= (car list1) (car list2))
2765            (setq prev list1
2766                  list1 (cdr list1)
2767                  list2 (cdr list2)))
2768           ((< (car list1) (car list2))
2769            (setcdr prev (cdr list1))
2770            (setq list1 (cdr list1)))
2771           (t
2772            (setq list2 (cdr list2)))))
2773   (setcdr prev nil)
2774   (cdr top)))
2775
2776 (defun gnus-compress-sequence (numbers &optional always-list)
2777   "Convert list of numbers to a list of ranges or a single range.
2778 If ALWAYS-LIST is non-nil, this function will always release a list of
2779 ranges."
2780   (let* ((first (car numbers))
2781          (last (car numbers))
2782          result)
2783     (if (null numbers)
2784         nil
2785       (if (not (listp (cdr numbers)))
2786           numbers
2787         (while numbers
2788           (cond ((= last (car numbers)) nil) ;Omit duplicated number
2789                 ((= (1+ last) (car numbers)) ;Still in sequence
2790                  (setq last (car numbers)))
2791                 (t                      ;End of one sequence
2792                  (setq result 
2793                        (cons (if (= first last) first
2794                                (cons first last)) result))
2795                  (setq first (car numbers))
2796                  (setq last  (car numbers))))
2797           (setq numbers (cdr numbers)))
2798         (if (and (not always-list) (null result))
2799             (if (= first last) (list first) (cons first last))
2800           (nreverse (cons (if (= first last) first (cons first last))
2801                           result)))))))
2802
2803 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
2804 (defun gnus-uncompress-range (ranges)
2805   "Expand a list of ranges into a list of numbers.
2806 RANGES is either a single range on the form `(num . num)' or a list of
2807 these ranges."
2808   (let (first last result)
2809     (cond 
2810      ((null ranges)
2811       nil)
2812      ((not (listp (cdr ranges)))
2813       (setq first (car ranges))
2814       (setq last (cdr ranges))
2815       (while (<= first last)
2816         (setq result (cons first result))
2817         (setq first (1+ first)))
2818       (nreverse result))
2819      (t
2820       (while ranges
2821         (if (atom (car ranges))
2822             (if (numberp (car ranges))
2823                 (setq result (cons (car ranges) result)))
2824           (setq first (car (car ranges)))
2825           (setq last  (cdr (car ranges)))
2826           (while (<= first last)
2827             (setq result (cons first result))
2828             (setq first (1+ first))))
2829         (setq ranges (cdr ranges)))
2830       (nreverse result)))))
2831
2832 (defun gnus-add-to-range (ranges list)
2833   "Return a list of ranges that has all articles from both RANGES and LIST.
2834 Note: LIST has to be sorted over `<'."
2835   (if (not ranges)
2836       (gnus-compress-sequence list t)
2837     (setq list (copy-sequence list))
2838     (or (listp (cdr ranges))
2839         (setq ranges (list ranges)))
2840     (let ((out ranges)
2841           ilist lowest highest temp)
2842       (while (and ranges list)
2843         (setq ilist list)
2844         (setq lowest (or (and (atom (car ranges)) (car ranges))
2845                          (car (car ranges))))
2846         (while (and list (cdr list) (< (car (cdr list)) lowest))
2847           (setq list (cdr list)))
2848         (if (< (car ilist) lowest)
2849             (progn
2850               (setq temp list)
2851               (setq list (cdr list))
2852               (setcdr temp nil)
2853               (setq out (nconc (gnus-compress-sequence ilist t) out))))
2854         (setq highest (or (and (atom (car ranges)) (car ranges))
2855                           (cdr (car ranges))))
2856         (while (and list (<= (car list) highest))
2857           (setq list (cdr list)))
2858         (setq ranges (cdr ranges)))
2859       (if list
2860           (setq out (nconc (gnus-compress-sequence list t) out)))
2861       (setq out (sort out (lambda (r1 r2) 
2862                             (< (or (and (atom r1) r1) (car r1))
2863                                (or (and (atom r2) r2) (car r2))))))
2864       (setq ranges out)
2865       (while ranges
2866         (if (atom (car ranges))
2867             (if (cdr ranges)
2868                 (if (atom (car (cdr ranges)))
2869                     (if (= (1+ (car ranges)) (car (cdr ranges)))
2870                         (progn
2871                           (setcar ranges (cons (car ranges) 
2872                                                (car (cdr ranges))))
2873                           (setcdr ranges (cdr (cdr ranges)))))
2874                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
2875                       (progn
2876                         (setcar (car (cdr ranges)) (car ranges))
2877                         (setcar ranges (car (cdr ranges)))
2878                         (setcdr ranges (cdr (cdr ranges)))))))
2879           (if (cdr ranges)
2880               (if (atom (car (cdr ranges)))
2881                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
2882                       (progn
2883                         (setcdr (car ranges) (car (cdr ranges)))
2884                         (setcdr ranges (cdr (cdr ranges)))))
2885                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
2886                     (progn
2887                       (setcdr (car ranges) (cdr (car (cdr ranges))))
2888                       (setcdr ranges (cdr (cdr ranges))))))))
2889         (setq ranges (cdr ranges)))
2890       out)))
2891
2892 (defun gnus-remove-from-range (ranges list)
2893   "Return a list of ranges that has all articles from LIST removed from RANGES.
2894 Note: LIST has to be sorted over `<'."
2895   ;; !!! This function shouldn't look like this, but I've got a headache.
2896   (gnus-compress-sequence 
2897    (gnus-sorted-complement
2898     (gnus-uncompress-range ranges) list)))
2899
2900 (defun gnus-member-of-range (number ranges)
2901   (if (not (listp (cdr ranges)))
2902       (and (>= number (car ranges)) 
2903            (<= number (cdr ranges)))
2904     (let ((not-stop t))
2905       (while (and ranges 
2906                   (if (numberp (car ranges))
2907                       (>= number (car ranges))
2908                     (>= number (car (car ranges))))
2909                   not-stop)
2910         (if (if (numberp (car ranges))
2911                 (= number (car ranges))
2912               (and (>= number (car (car ranges)))
2913                    (<= number (cdr (car ranges)))))
2914             (setq not-stop nil))
2915         (setq ranges (cdr ranges)))
2916       (not not-stop))))
2917
2918 \f
2919 ;;;
2920 ;;; Gnus group mode
2921 ;;;
2922
2923 (defvar gnus-group-mode-map nil)
2924 (defvar gnus-group-group-map nil)
2925 (defvar gnus-group-mark-map nil)
2926 (defvar gnus-group-list-map nil)
2927 (defvar gnus-group-sub-map nil)
2928 (put 'gnus-group-mode 'mode-class 'special)
2929
2930 (if gnus-group-mode-map
2931     nil
2932   (setq gnus-group-mode-map (make-keymap))
2933   (suppress-keymap gnus-group-mode-map)
2934   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
2935   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
2936   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
2937   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
2938   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
2939   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
2940   (define-key gnus-group-mode-map "\177" 'gnus-group-prev-unread-group)
2941   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
2942   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
2943   (define-key gnus-group-mode-map "\M-n" 'gnus-group-next-unread-group-same-level)
2944   (define-key gnus-group-mode-map "\M-p" 'gnus-group-prev-unread-group-same-level)
2945   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
2946   (define-key gnus-group-mode-map "." 'gnus-group-first-unread-group)
2947   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
2948   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
2949   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
2950   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
2951   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
2952   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
2953   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
2954   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
2955   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
2956   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
2957   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
2958   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
2959   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
2960   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
2961   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
2962   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
2963   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
2964   (define-key gnus-group-mode-map "\C-c\M-C-a" 'gnus-group-description-apropos)
2965   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
2966   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
2967   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
2968   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
2969   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
2970   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
2971   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
2972   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
2973   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
2974   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
2975   (define-key gnus-group-mode-map "V" 'gnus-version)
2976   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
2977   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
2978   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
2979   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
2980   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
2981   (define-key gnus-group-mode-map "\M-f" 'gnus-group-fetch-faq)
2982   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
2983   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
2984   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group-method)
2985   (define-key gnus-group-mode-map "^" 'gnus-group-enter-server-mode)
2986   (define-key gnus-group-mode-map gnus-mouse-2 'gnus-mouse-pick-group)
2987   (define-key gnus-group-mode-map "<" 'beginning-of-buffer)
2988   (define-key gnus-group-mode-map ">" 'end-of-buffer)
2989   (define-key gnus-group-mode-map "\C-c\C-b" 'gnus-bug)
2990   (define-key gnus-group-mode-map "\C-c\C-s" 'gnus-group-sort-groups)
2991
2992   (define-key gnus-group-mode-map "#" 'gnus-group-mark-group)
2993   (define-key gnus-group-mode-map "\M-#" 'gnus-group-unmark-group)
2994   (define-prefix-command 'gnus-group-mark-map)
2995   (define-key gnus-group-mode-map "M" 'gnus-group-mark-map)
2996   (define-key gnus-group-mark-map "m" 'gnus-group-mark-group)
2997   (define-key gnus-group-mark-map "u" 'gnus-group-unmark-group)
2998   (define-key gnus-group-mark-map "w" 'gnus-group-mark-region)
2999
3000   (define-prefix-command 'gnus-group-group-map)
3001   (define-key gnus-group-mode-map "G" 'gnus-group-group-map)
3002   (define-key gnus-group-group-map "d" 'gnus-group-make-directory-group)
3003   (define-key gnus-group-group-map "h" 'gnus-group-make-help-group)
3004   (define-key gnus-group-group-map "a" 'gnus-group-make-archive-group)
3005   (define-key gnus-group-group-map "k" 'gnus-group-make-kiboze-group)
3006   (define-key gnus-group-group-map "m" 'gnus-group-make-group)
3007   (define-key gnus-group-group-map "E" 'gnus-group-edit-group)
3008   (define-key gnus-group-group-map "e" 'gnus-group-edit-group-method)
3009   (define-key gnus-group-group-map "p" 'gnus-group-edit-group-parameters)
3010   (define-key gnus-group-group-map "v" 'gnus-group-add-to-virtual)
3011   (define-key gnus-group-group-map "V" 'gnus-group-make-empty-virtual)
3012   (define-key gnus-group-group-map "D" 'gnus-group-enter-directory)
3013   (define-key gnus-group-group-map "f" 'gnus-group-make-doc-group)
3014   (define-key gnus-group-group-map "sb" 'gnus-group-brew-soup)
3015   (define-key gnus-group-group-map "sw" 'gnus-soup-save-areas)
3016   (define-key gnus-group-group-map "ss" 'gnus-soup-send-replies)
3017   (define-key gnus-group-group-map "sp" 'gnus-soup-pack-packet)
3018   (define-key gnus-group-group-map "sr" 'nnsoup-pack-replies)
3019
3020   (define-prefix-command 'gnus-group-list-map)
3021   (define-key gnus-group-mode-map "A" 'gnus-group-list-map)
3022   (define-key gnus-group-list-map "k" 'gnus-group-list-killed)
3023   (define-key gnus-group-list-map "z" 'gnus-group-list-zombies)
3024   (define-key gnus-group-list-map "s" 'gnus-group-list-groups)
3025   (define-key gnus-group-list-map "u" 'gnus-group-list-all-groups)
3026   (define-key gnus-group-list-map "a" 'gnus-group-apropos)
3027   (define-key gnus-group-list-map "d" 'gnus-group-description-apropos)
3028   (define-key gnus-group-list-map "m" 'gnus-group-list-matching)
3029   (define-key gnus-group-list-map "M" 'gnus-group-list-all-matching)
3030
3031   (define-prefix-command 'gnus-group-sub-map)
3032   (define-key gnus-group-mode-map "S" 'gnus-group-sub-map)
3033   (define-key gnus-group-sub-map "l" 'gnus-group-set-current-level)
3034   (define-key gnus-group-sub-map "t" 'gnus-group-unsubscribe-current-group)
3035   (define-key gnus-group-sub-map "s" 'gnus-group-unsubscribe-group)
3036   (define-key gnus-group-sub-map "k" 'gnus-group-kill-group)
3037   (define-key gnus-group-sub-map "y" 'gnus-group-yank-group)
3038   (define-key gnus-group-sub-map "w" 'gnus-group-kill-region)
3039   (define-key gnus-group-sub-map "z" 'gnus-group-kill-all-zombies))
3040
3041 (defun gnus-group-mode ()
3042   "Major mode for reading news.
3043
3044 All normal editing commands are switched off.
3045 \\<gnus-group-mode-map>
3046 The group buffer lists (some of) the groups available.  For instance,
3047 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
3048 lists all zombie groups. 
3049
3050 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe 
3051 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'. 
3052
3053 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
3054
3055 The following commands are available:
3056
3057 \\{gnus-group-mode-map}"
3058   (interactive)
3059   (if gnus-visual (gnus-group-make-menu-bar))
3060   (kill-all-local-variables)
3061   (setq mode-line-modified "-- ")
3062   (make-local-variable 'mode-line-format)
3063   (setq mode-line-format (copy-sequence mode-line-format))
3064   (and (equal (nth 3 mode-line-format) "   ")
3065        (setcar (nthcdr 3 mode-line-format) ""))
3066   (setq major-mode 'gnus-group-mode)
3067   (setq mode-name "Group")
3068   (gnus-group-set-mode-line)
3069   (setq mode-line-process nil)
3070   (use-local-map gnus-group-mode-map)
3071   (buffer-disable-undo (current-buffer))
3072   (setq truncate-lines t)
3073   (setq buffer-read-only t)
3074   (run-hooks 'gnus-group-mode-hook))
3075
3076 (defun gnus-mouse-pick-group (e)
3077   (interactive "e")
3078   (mouse-set-point e)
3079   (gnus-group-read-group nil))
3080
3081 ;;;###autoload
3082 (defun gnus-no-server (&optional arg)
3083   "Read network news.
3084 If ARG is a positive number, Gnus will use that as the
3085 startup level. If ARG is nil, Gnus will be started at level 2. 
3086 If ARG is non-nil and not a positive number, Gnus will
3087 prompt the user for the name of an NNTP server to use.
3088 As opposed to `gnus', this command will not connect to the local server."
3089   (interactive "P")
3090   (gnus (or arg (1- gnus-level-default-subscribed)) t))
3091
3092 (defalias '\(ding\) 'gnus)
3093
3094 ;;;###autoload
3095 (defun gnus (&optional arg dont-connect)
3096   "Read network news.
3097 If ARG is non-nil and a positive number, Gnus will use that as the
3098 startup level. If ARG is non-nil and not a positive number, Gnus will
3099 prompt the user for the name of an NNTP server to use."
3100   (interactive "P")
3101   (if (get-buffer gnus-group-buffer)
3102       (progn
3103         (switch-to-buffer gnus-group-buffer)
3104         (gnus-group-get-new-news))
3105     (gnus-clear-system)
3106     (nnheader-init-server-buffer)
3107     (gnus-read-init-file)
3108     (let ((level (and arg (numberp arg) (> arg 0) arg))
3109           did-connect)
3110       (unwind-protect
3111           (progn
3112             (gnus-group-setup-buffer)
3113             (or dont-connect 
3114                 (setq did-connect
3115                       (gnus-start-news-server (and arg (not level))))))
3116         (if (and (not dont-connect) 
3117                  (not did-connect))
3118             (gnus-group-quit)
3119           (run-hooks 'gnus-startup-hook)
3120           ;; NNTP server is successfully open. 
3121           (gnus-update-format-specifications)
3122           (gnus-summary-make-display-table)
3123           (let ((buffer-read-only nil))
3124             (erase-buffer)
3125             (if (not gnus-inhibit-startup-message)
3126                 (progn
3127                   (gnus-group-startup-message)
3128                   (sit-for 0))))
3129           (gnus-setup-news nil level)
3130           (and gnus-use-dribble-file (gnus-dribble-open))
3131           (gnus-group-list-groups level)
3132           (gnus-configure-windows 'group))))))
3133
3134 (defun gnus-group-startup-message (&optional x y)
3135   "Insert startup message in current buffer."
3136   ;; Insert the message.
3137   (erase-buffer)
3138   (insert
3139    (format "
3140     %s
3141            A newsreader 
3142       for GNU Emacs
3143
3144         Based on GNUS 
3145              written by 
3146      Masanobu UMEDA
3147
3148     Lars Magne 
3149          Ingebrigtsen 
3150       larsi@ifi.uio.no
3151
3152            gnus-version))
3153   ;; And then hack it.
3154   ;; 18 is the longest line.
3155   (indent-rigidly (point-min) (point-max) 
3156                   (/ (max (- (window-width) (or x 28)) 0) 2))
3157   (goto-char (point-min))
3158   ;; +4 is fuzzy factor.
3159   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2)))
3160
3161 (defun gnus-group-setup-buffer ()
3162   (or (get-buffer gnus-group-buffer)
3163       (progn
3164         (switch-to-buffer gnus-group-buffer)
3165         (gnus-add-current-to-buffer-list)
3166         (gnus-group-mode)
3167         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
3168
3169 (defun gnus-group-list-groups (level &optional unread)
3170   "List newsgroups with level LEVEL or lower that have unread alticles.
3171 Default is all subscribed groups.
3172 If argument UNREAD is non-nil, groups with no unread articles are also listed."
3173   (interactive (list (and current-prefix-arg
3174                           (prefix-numeric-value current-prefix-arg))))
3175   (if gnus-group-use-permanent-levels
3176       (progn
3177         (setq gnus-group-default-list-level 
3178               (or level gnus-group-default-list-level))
3179         (setq level (or gnus-group-default-list-level gnus-level-subscribed)))
3180     (setq level (or level gnus-group-default-list-level 
3181                     gnus-level-subscribed)))
3182   (gnus-group-setup-buffer)     ;May call from out of group buffer
3183   (let ((case-fold-search nil)
3184         (group (gnus-group-group-name)))
3185     (funcall gnus-group-prepare-function level unread nil)
3186     (if (zerop (buffer-size))
3187         (gnus-message 5 gnus-no-groups-message)
3188       (goto-char (point-min))
3189       (if (not group)
3190           ;; Go to the first group with unread articles.
3191           (gnus-group-search-forward nil nil nil t)
3192         ;; Find the right group to put point on. If the current group
3193         ;; has disapeared in the new listing, try to find the next
3194         ;; one. If no next one can be found, just leave point at the
3195         ;; first newsgroup in the buffer.
3196         (if (not (gnus-goto-char
3197                   (text-property-any (point-min) (point-max) 
3198                                      'gnus-group (intern group))))
3199             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
3200               (while (and newsrc
3201                           (not (gnus-goto-char 
3202                                 (text-property-any 
3203                                  (point-min) (point-max) 'gnus-group 
3204                                  (intern (car (car newsrc)))))))
3205                 (setq newsrc (cdr newsrc)))
3206               (or newsrc (progn (goto-char (point-max))
3207                                 (forward-line -1))))))
3208       ;; Adjust cursor point.
3209       (gnus-group-position-cursor))))
3210
3211 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
3212   "List all newsgroups with unread articles of level LEVEL or lower.
3213 If ALL is non-nil, list groups that have no unread articles.
3214 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
3215 If REGEXP, only list groups matching REGEXP."
3216   (set-buffer gnus-group-buffer)
3217   (let ((buffer-read-only nil)
3218         (newsrc (cdr gnus-newsrc-alist))
3219         (lowest (or lowest 1))
3220         info clevel unread group)
3221     (erase-buffer)
3222     (if (< lowest gnus-level-zombie)
3223         ;; List living groups.
3224         (while newsrc
3225           (setq info (car newsrc)
3226                 group (car info)
3227                 newsrc (cdr newsrc)
3228                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
3229           (and unread ; This group might be bogus
3230                (or (not regexp)
3231                    (string-match regexp group))
3232                (<= (setq clevel (car (cdr info))) level) 
3233                (>= clevel lowest)
3234                (or all            ; We list all groups?
3235                    (eq unread t)  ; We list unactivated groups
3236                    (> unread 0)   ; We list groups with unread articles
3237                    (cdr (assq 'tick (nth 3 info)))) ; And groups with tickeds
3238                (gnus-group-insert-group-line 
3239                 nil group (car (cdr info)) (nth 3 info) unread (nth 4 info)))))
3240
3241     ;; List dead groups.
3242     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
3243          (gnus-group-prepare-flat-list-dead 
3244           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 
3245           gnus-level-zombie ?Z
3246           regexp))
3247     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
3248          (gnus-group-prepare-flat-list-dead 
3249           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 
3250           gnus-level-killed ?K regexp))
3251
3252     (gnus-group-set-mode-line)
3253     (setq gnus-have-all-newsgroups all)
3254     (run-hooks 'gnus-group-prepare-hook)))
3255
3256 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
3257   ;; List zombies and killed lists somehwat faster, which was
3258   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>. It does
3259   ;; this by ignoring the group format specification altogether.
3260   (let (group beg)
3261     (while groups
3262       (setq group (car groups)
3263             groups (cdr groups))
3264       (if (or (not regexp)
3265               (string-match regexp group))
3266           (progn
3267             (setq beg (point))
3268             (insert (format " %c     *: %s\n" mark group))
3269             (add-text-properties 
3270              beg (1+ beg) 
3271              (list 'gnus-group (intern group)
3272                    'gnus-unread t
3273                    'gnus-level level)))))))
3274
3275 (defun gnus-group-real-name (group)
3276   "Find the real name of a foreign newsgroup."
3277   (if (string-match ":[^:]+$" group)
3278       (substring group (1+ (match-beginning 0)))
3279     group))
3280
3281 (defun gnus-group-prefixed-name (group method)
3282   "Return the whole name from GROUP and METHOD."
3283   (and (stringp method) (setq method (gnus-server-to-method method)))
3284   (concat (format "%s" (car method))
3285           (if (and 
3286                (assoc (format "%s" (car method)) (gnus-methods-using 'address))
3287                (not (string= (nth 1 method) "")))
3288               (concat "+" (nth 1 method)))
3289           ":" group))
3290
3291 (defun gnus-group-real-prefix (group)
3292   "Return the prefix of the current group name."
3293   (if (string-match "^[^:]+:" group)
3294       (substring group 0 (match-end 0))
3295     ""))
3296
3297 (defun gnus-group-method-name (group)
3298   "Return the method used for selecting GROUP."
3299   (let ((prefix (gnus-group-real-prefix group)))
3300     (if (equal prefix "")
3301         gnus-select-method
3302       (if (string-match "^[^\\+]+\\+" prefix)
3303           (list (intern (substring prefix 0 (1- (match-end 0))))
3304                 (substring prefix (match-end 0) (1- (length prefix))))
3305         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
3306
3307 (defun gnus-group-foreign-p (group)
3308   "Return nil if GROUP is native, non-nil if it is foreign."
3309   (string-match ":" group))
3310
3311 (defun gnus-group-set-info (info &optional method-only-group part)
3312   (let* ((entry (gnus-gethash
3313                  (or method-only-group (car info)) gnus-newsrc-hashtb))
3314          (part-info info)
3315          (info (if method-only-group (nth 2 entry) info)))
3316     (if (not method-only-group)
3317         ()
3318       (or entry
3319           (error "Trying to change non-existant group %s" method-only-group))
3320       ;; We have recevied parts of the actual group info - either the
3321       ;; select method or the group parameters.  We first check
3322       ;; whether we have to extend the info, and if so, do that.
3323       (let ((len (length info))
3324             (total (if (eq part 'method) 5 6)))
3325         (and (< len total)
3326              (setcdr (nthcdr (1- len) info)
3327                      (make-list (- total len) nil)))
3328         ;; Then we enter the new info.
3329         (setcar (nthcdr (1- total) info) part-info)))
3330     ;; We uncompress some lists of marked articles.
3331     (let (marked)
3332       (if (not (setq marked (nth 3 info)))
3333           ()
3334         (while marked
3335           (or (eq 'score (car (car marked)))
3336               (eq 'bookmark (car (car marked)))
3337               (eq 'killed (car (car marked)))
3338               (setcdr (car marked) 
3339                       (gnus-uncompress-range (cdr (car marked)))))
3340           (setq marked (cdr marked)))))
3341     (if entry
3342         ()
3343       ;; This is a new group, so we just create it.
3344       (save-excursion
3345         (set-buffer gnus-group-buffer)
3346         (if (nth 4 info)
3347             ;; It's a foreign group...
3348             (gnus-group-make-group 
3349              (gnus-group-real-name (car info))
3350              (prin1-to-string (car (nth 4 info)))
3351              (nth 1 (nth 4 info)))
3352           ;; It's a native group.
3353           (gnus-group-make-group
3354            (car info)
3355            (prin1-to-string (car gnus-select-method))
3356            (nth 1 gnus-select-method)))
3357         (gnus-message 6 "Note: New group created")
3358         (setq entry 
3359               (gnus-gethash (gnus-group-prefixed-name 
3360                              (gnus-group-real-name (car info))
3361                              (or (nth 4 info) gnus-select-method))
3362                             gnus-newsrc-hashtb))))
3363     ;; Whether it was a new group or not, we now have the entry, so we
3364     ;; can do the update.
3365     (if entry
3366         (progn
3367           (setcar (nthcdr 2 entry) info)
3368           (if (and (not (eq (car entry) t)) 
3369                    (gnus-gethash (car info) gnus-active-hashtb))
3370               (let ((marked (nth 3 info)))
3371                 (setcar entry 
3372                         (max 0 (- (length (gnus-list-of-unread-articles 
3373                                            (car info)))
3374                                   (length (cdr (assq 'tick marked)))
3375                                   (length (cdr (assq 'dormant marked)))))))))
3376       (error "No such group: %s" (car info)))))
3377
3378 (defun gnus-group-set-method-info (group select-method)
3379   (gnus-group-set-info select-method group 'method))
3380
3381 (defun gnus-group-set-params-info (group params)
3382   (gnus-group-set-info params group 'params))
3383
3384 (defun gnus-group-update-group-line ()
3385   "This function updates the current line in the newsgroup buffer and
3386 moves the point to the colon."
3387   (let* ((buffer-read-only nil)
3388          (group (gnus-group-group-name))
3389          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
3390     (if entry
3391         (gnus-dribble-enter 
3392          (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3393                  ")")))
3394     (beginning-of-line)
3395     (delete-region (point) (progn (forward-line 1) (point)))
3396     (gnus-group-insert-group-line-info group)
3397     (forward-line -1)
3398     (gnus-group-position-cursor)))
3399
3400 (defun gnus-group-insert-group-line-info (group)
3401   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
3402         active info)
3403     (if entry
3404         (progn
3405           (setq info (nth 2 entry))
3406           (gnus-group-insert-group-line 
3407            nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
3408       (setq active (gnus-gethash group gnus-active-hashtb))
3409       (gnus-group-insert-group-line 
3410        nil group (if (member group gnus-zombie-list) gnus-level-zombie
3411                    gnus-level-killed)
3412        nil (if active (- (1+ (cdr active)) (car active)) 0) nil))))
3413
3414 (defun gnus-group-insert-group-line (gformat group level marked number method)
3415   (let* ((gformat (or gformat gnus-group-line-format-spec))
3416          (active (gnus-gethash group gnus-active-hashtb))
3417          (number-total (if active (1+ (- (cdr active) (car active))) 0))
3418          (number-of-dormant (length (cdr (assq 'dormant marked))))
3419          (number-of-ticked (length (cdr (assq 'tick marked))))
3420          (number-of-ticked-and-dormant
3421           (+ number-of-ticked number-of-dormant))
3422          (number-of-unread-unticked 
3423           (if (numberp number) (int-to-string (max 0 number))
3424             "*"))
3425          (number-of-read
3426           (if (numberp number)
3427               (max 0 (- number-total number))
3428             "*"))
3429          (subscribed (cond ((<= level gnus-level-subscribed) ? )
3430                            ((<= level gnus-level-unsubscribed) ?U)
3431                            ((= level gnus-level-zombie) ?Z)
3432                            (t ?K)))
3433          (qualified-group (gnus-group-real-name group))
3434          (newsgroup-description 
3435           (if gnus-description-hashtb
3436               (or (gnus-gethash group gnus-description-hashtb) "")
3437             ""))
3438          (moderated (if (member group gnus-moderated-list) ?m ? ))
3439          (moderated-string (if (eq moderated ?m) "(m)" ""))
3440          (method (gnus-server-get-method group method))
3441          (news-server (or (car (cdr method)) ""))
3442          (news-method (or (car method) ""))
3443          (news-method-string 
3444           (if method (format "(%s:%s)" (car method) (car (cdr method))) ""))
3445          (marked (if (and 
3446                       (numberp number) 
3447                       (zerop number)
3448                       (> number-of-ticked 0))
3449                      ?* ? ))
3450          (number (if (eq number t) "*" (+ number number-of-dormant 
3451                                           number-of-ticked)))
3452          (process-marked (if (member qualified-group gnus-group-marked)
3453                              gnus-process-mark ? ))
3454          (buffer-read-only nil)
3455          b)
3456     (beginning-of-line)
3457     (setq b (point))
3458     ;; Insert the text.
3459     (insert (eval gformat))
3460
3461     (add-text-properties 
3462      b (1+ b) (list 'gnus-group (intern group)
3463                     'gnus-unread (if (numberp number)
3464                                      (string-to-int number-of-unread-unticked)
3465                                    t)
3466                     'gnus-marked marked
3467                     'gnus-level level))))
3468
3469 (defun gnus-group-update-group (group &optional visible-only)
3470   "Update newsgroup info of GROUP.
3471 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't already."
3472   (save-excursion
3473     (set-buffer gnus-group-buffer)
3474     (let ((buffer-read-only nil)
3475           visible)
3476       (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
3477         (if entry
3478             (gnus-dribble-enter 
3479              (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3480                      ")"))))
3481       ;; Buffer may be narrowed.
3482       (save-restriction
3483         (widen)
3484         ;; Search a line to modify.  If the buffer is large, the search
3485         ;; takes long time.  In most cases, current point is on the line
3486         ;; we are looking for.  So, first of all, check current line. 
3487         (if (or (progn
3488                   (beginning-of-line)
3489                   (eq (get-text-property (point) 'gnus-group)
3490                       (intern group)))
3491                 (progn
3492                   (gnus-goto-char 
3493                    (text-property-any 
3494                     (point-min) (point-max) 'gnus-group (intern group)))))
3495             ;; GROUP is listed in current buffer. So, delete old line.
3496             (progn
3497               (setq visible t)
3498               (beginning-of-line)
3499               (delete-region (point) (progn (forward-line 1) (point))))
3500           ;; No such line in the buffer, find out where it's supposed to
3501           ;; go, and insert it there (or at the end of the buffer).
3502           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
3503           (or visible-only
3504               (let ((entry 
3505                      (cdr (cdr (gnus-gethash group gnus-newsrc-hashtb)))))
3506                 (while (and entry
3507                             (car entry)
3508                             (not
3509                              (gnus-goto-char
3510                               (text-property-any
3511                                (point-min) (point-max) 
3512                                'gnus-group (intern (car (car entry)))))))
3513                   (setq entry (cdr entry)))
3514                 (or entry (goto-char (point-max)))))))
3515       (if (or visible (not visible-only))
3516           (gnus-group-insert-group-line-info group))
3517       (gnus-group-set-mode-line))))
3518
3519 (defun gnus-group-set-mode-line ()
3520   (if (memq 'group gnus-updated-mode-lines)
3521       (let* ((gformat (or gnus-group-mode-line-format-spec
3522                           (setq gnus-group-mode-line-format-spec
3523                                 (gnus-parse-format 
3524                                  gnus-group-mode-line-format 
3525                                  gnus-group-mode-line-format-alist))))
3526              (news-server (car (cdr gnus-select-method)))
3527              (news-method (car gnus-select-method))
3528              (max-len 60)
3529              (mode-string (eval gformat)))
3530         (setq mode-string (eval gformat))
3531         (if (> (length mode-string) max-len) 
3532             (setq mode-string (substring mode-string 0 (- max-len 4))))
3533         (setq mode-line-buffer-identification mode-string)
3534         (set-buffer-modified-p t))))
3535
3536 (defun gnus-group-group-name ()
3537   "Get the name of the newsgroup on the current line."
3538   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
3539     (and group (symbol-name group))))
3540
3541 (defun gnus-group-group-level ()
3542   "Get the level of the newsgroup on the current line."
3543   (get-text-property (gnus-point-at-bol) 'gnus-level))
3544
3545 (defun gnus-group-search-forward (&optional backward all level first-too)
3546   "Find the next newsgroup with unread articles.
3547 If BACKWARD is non-nil, find the previous newsgroup instead.
3548 If ALL is non-nil, just find any newsgroup.
3549 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
3550 group exists.
3551 If FIRST-TOO, the current line is also eligeble as a target."
3552   (let ((way (if backward -1 1))
3553         (low 10)
3554         (beg (point))
3555         pos found)
3556     (if (and backward (progn (beginning-of-line)) (bobp))
3557         nil
3558       (or first-too (forward-line way))
3559       (while (and 
3560               (not (eobp))
3561               (not (setq 
3562                     found 
3563                     (and (or all
3564                              (and
3565                               (let ((unread 
3566                                      (get-text-property (point) 'gnus-unread)))
3567                                 (or (eq unread t) (and unread (> unread 0))))
3568                               (let ((lev (get-text-property
3569                                           (point) 'gnus-level)))
3570                                 (and lev (<= (get-text-property 
3571                                               (point) 'gnus-level)
3572                                              gnus-level-subscribed)))))
3573                          (or (not level)
3574                              (let ((lev (get-text-property (point) 'gnus-level)))
3575                                (if (and lev (<= lev level))
3576                                    t
3577                                  (if (< lev low)
3578                                      (progn
3579                                        (setq low lev)
3580                                        (setq pos (point))))
3581                                  nil))))))
3582               (zerop (forward-line way)))))
3583     (if found 
3584         (progn (gnus-group-position-cursor) t)
3585       (if pos (goto-char pos) (goto-char beg))
3586       nil)))
3587
3588 ;;; Gnus group mode commands
3589
3590 ;; Group marking.
3591
3592 (defun gnus-group-mark-group (n &optional unmark)
3593   "Mark the current group."
3594   (interactive "p")
3595   (let ((buffer-read-only nil)
3596         group)
3597     (while 
3598         (and (> n 0) 
3599              (setq group (gnus-group-group-name))
3600              (progn
3601                (beginning-of-line)
3602                (forward-char 2)
3603                (delete-char 1)
3604                (if unmark
3605                    (progn
3606                      (insert " ")
3607                      (setq gnus-group-marked (delete group gnus-group-marked)))
3608                  (insert "#")
3609                  (setq gnus-group-marked
3610                        (cons group (delete group gnus-group-marked))))
3611                t)
3612              (zerop (gnus-group-next-group 1)))
3613       (setq n (1- n)))
3614     (gnus-summary-position-cursor)
3615     n))
3616
3617 (defun gnus-group-unmark-group (n)
3618   "Remove the mark from the current group."
3619   (interactive "p")
3620   (gnus-group-mark-group n 'unmark))
3621
3622 (defun gnus-group-mark-region (unmark beg end)
3623   "Mark all groups between point and mark.
3624 If UNMARK, remove the mark instead."
3625   (interactive "P\nr")
3626   (let ((num (count-lines beg end)))
3627     (save-excursion
3628       (goto-char beg)
3629       (- num (gnus-group-mark-group num unmark)))))
3630
3631 (defun gnus-group-remove-mark (group)
3632   (and (gnus-group-goto-group group)
3633        (save-excursion
3634          (gnus-group-mark-group 1 'unmark))))
3635
3636 ;; Return a list of groups to work on.  Take into consideration N (the
3637 ;; prefix) and the list of marked groups.
3638 (defun gnus-group-process-prefix (n)
3639   (cond (n
3640          (setq n (prefix-numeric-value n))
3641          ;; There is a prefix, so we return a list of the N next
3642          ;; groups. 
3643          (let ((way (if (< n 0) -1 1))
3644                (n (abs n))
3645                group groups)
3646            (save-excursion
3647              (while (and (> n 0)
3648                          (setq group (gnus-group-group-name)))
3649                (setq groups (cons group groups))
3650                (setq n (1- n))
3651                (forward-line way)))
3652            (nreverse groups)))
3653         (gnus-group-marked
3654          ;; No prefix, but a list of marked articles.
3655          (reverse gnus-group-marked))
3656         (t
3657          ;; Neither marked articles or a prefix, so we return the
3658          ;; current group.
3659          (let ((group (gnus-group-group-name)))
3660            (and group (list group))))))
3661
3662 ;; Selecting groups.
3663
3664 (defun gnus-group-read-group (all &optional no-article group)
3665   "Read news in this newsgroup.
3666 If argument ALL is non-nil, already read articles become readable.
3667 If optional argument NO-ARTICLE is non-nil, no article body is displayed."
3668   (interactive "P")
3669   (let ((group (or group (gnus-group-group-name)))
3670         number active marked entry)
3671     (or group (error "No group on current line"))
3672     (setq marked 
3673           (nth 3 (nth 2 (setq entry (gnus-gethash group gnus-newsrc-hashtb)))))
3674     ;; This group might be a dead group. In that case we have to get
3675     ;; the number of unread articles from `gnus-active-hashtb'.
3676     (if entry
3677         (setq number (car entry))
3678       (if (setq active (gnus-gethash group gnus-active-hashtb))
3679           (setq number (- (1+ (cdr active)) (car active)))))
3680     (gnus-summary-read-group 
3681      group (or all (and (numberp number) 
3682                         (zerop (+ number (length (cdr (assq 'tick marked)))
3683                                   (length (cdr (assq 'dormant marked)))))))
3684      no-article)))
3685
3686 (defun gnus-group-select-group (all)
3687   "Select this newsgroup.
3688 No article is selected automatically.
3689 If argument ALL is non-nil, already read articles become readable."
3690   (interactive "P")
3691   (gnus-group-read-group all t))
3692
3693 ;; Enter a group that is not in the group buffer. Non-nil is returned
3694 ;; if selection was successful.
3695 (defun gnus-group-read-ephemeral-group 
3696   (group method &optional activate quit-config)
3697   (let ((group (if (gnus-group-foreign-p group) group
3698                  (gnus-group-prefixed-name group method))))
3699     (gnus-sethash 
3700      group
3701      (list t nil (list group gnus-level-default-subscribed nil nil 
3702                        (append method
3703                                (list
3704                                 (list 'quit-config 
3705                                       (if quit-config quit-config
3706                                         (cons (current-buffer) 'summary)))))))
3707      gnus-newsrc-hashtb)
3708     (set-buffer gnus-group-buffer)
3709     (or (gnus-server-opened method)
3710         (gnus-open-server method)
3711         (error "Unable to contact server: %s" (gnus-status-message method)))
3712     (if activate (gnus-request-group group))
3713     (condition-case ()
3714         (gnus-group-read-group t t group)
3715       (error nil)
3716       (quit nil))
3717     (not (equal major-mode 'gnus-group-mode))))
3718   
3719 (defun gnus-group-jump-to-group (group)
3720   "Jump to newsgroup GROUP."
3721   (interactive 
3722    (list (completing-read 
3723           "Group: " gnus-active-hashtb nil (not (not gnus-read-active-file)))))
3724
3725   (if (equal group "")
3726       (error "Empty group name"))
3727
3728   (let ((b (text-property-any 
3729             (point-min) (point-max) 'gnus-group (intern group))))
3730     (if b
3731         ;; Either go to the line in the group buffer...
3732         (goto-char b)
3733       ;; ... or insert the line.
3734       (or
3735        (gnus-gethash group gnus-active-hashtb)
3736        (gnus-activate-newsgroup group)
3737        (error "%s error: %s" group (gnus-status-message group)))
3738
3739       (gnus-group-update-group group)
3740       (goto-char (text-property-any 
3741                   (point-min) (point-max) 'gnus-group (intern group)))))
3742   ;; Adjust cursor point.
3743   (gnus-group-position-cursor))
3744
3745 (defun gnus-group-goto-group (group)
3746   "Goto to newsgroup GROUP."
3747   (let ((b (text-property-any (point-min) (point-max) 
3748                               'gnus-group (intern group))))
3749     (and b (goto-char b))))
3750
3751 (defun gnus-group-next-group (n)
3752   "Go to next N'th newsgroup.
3753 If N is negative, search backward instead.
3754 Returns the difference between N and the number of skips actually
3755 done."
3756   (interactive "p")
3757   (gnus-group-next-unread-group n t))
3758
3759 (defun gnus-group-next-unread-group (n &optional all level)
3760   "Go to next N'th unread newsgroup.
3761 If N is negative, search backward instead.
3762 If ALL is non-nil, choose any newsgroup, unread or not.
3763 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
3764 such group can be found, the next group with a level higher than
3765 LEVEL.
3766 Returns the difference between N and the number of skips actually
3767 made."
3768   (interactive "p")
3769   (let ((backward (< n 0))
3770         (n (abs n)))
3771     (while (and (> n 0)
3772                 (gnus-group-search-forward 
3773                  backward (or (not gnus-group-goto-unread) all) level))
3774       (setq n (1- n)))
3775     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
3776                                (if level " on this level or higher" "")))
3777     n))
3778
3779 (defun gnus-group-prev-group (n)
3780   "Go to previous N'th newsgroup.
3781 Returns the difference between N and the number of skips actually
3782 done."
3783   (interactive "p")
3784   (gnus-group-next-unread-group (- n) t))
3785
3786 (defun gnus-group-prev-unread-group (n)
3787   "Go to previous N'th unread newsgroup.
3788 Returns the difference between N and the number of skips actually
3789 done."  
3790   (interactive "p")
3791   (gnus-group-next-unread-group (- n)))
3792
3793 (defun gnus-group-next-unread-group-same-level (n)
3794   "Go to next N'th unread newsgroup on the same level.
3795 If N is negative, search backward instead.
3796 Returns the difference between N and the number of skips actually
3797 done."
3798   (interactive "p")
3799   (gnus-group-next-unread-group n t (gnus-group-group-level))
3800   (gnus-group-position-cursor))
3801
3802 (defun gnus-group-prev-unread-group-same-level (n)
3803   "Go to next N'th unread newsgroup on the same level.
3804 Returns the difference between N and the number of skips actually
3805 done."
3806   (interactive "p")
3807   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
3808   (gnus-group-position-cursor))
3809
3810 (defun gnus-group-best-unread-group (&optional exclude-group)
3811   "Go to the group with the highest level.
3812 If EXCLUDE-GROUP, do not go to that group."
3813   (interactive)
3814   (goto-char (point-min))
3815   (let ((best 100000)
3816         unread best-point)
3817     (while (setq unread (get-text-property (point) 'gnus-unread))
3818       (if (and (numberp unread) (> unread 0))
3819           (progn
3820             (or best-point (setq best-point (point)))
3821             (if (and (< (get-text-property (point) 'gnus-level) best)
3822                      (or (not exclude-group)
3823                          (not (equal exclude-group (gnus-group-group-name)))))
3824                 (progn 
3825                   (setq best (get-text-property (point) 'gnus-level))
3826                   (setq best-point (point))))))
3827       (forward-line 1))
3828     (if best-point (goto-char best-point))
3829     (gnus-summary-position-cursor)
3830     (and best-point (gnus-group-group-name))))
3831
3832 (defun gnus-group-first-unread-group ()
3833   "Go to the first group with unread articles."
3834   (interactive)
3835   (goto-char (point-min))
3836   (or (not (zerop (or (get-text-property (point) 'gnus-unread) 0)))
3837       (gnus-group-next-unread-group 1))
3838   (gnus-group-position-cursor))
3839
3840 (defun gnus-group-enter-server-mode ()
3841   "Jump to the server buffer."
3842   (interactive)
3843   (gnus-server-setup-buffer)
3844   (gnus-configure-windows 'server)
3845   (gnus-server-prepare))
3846
3847 (defun gnus-group-make-group (name method &optional address)
3848   "Add a new newsgroup.
3849 The user will be prompted for a NAME, for a select METHOD, and an
3850 ADDRESS."
3851   (interactive
3852    (cons 
3853     (read-string "Group name: ")
3854     (let ((method
3855            (completing-read 
3856             "Method: " (append gnus-valid-select-methods gnus-server-alist)
3857             nil t)))
3858       (if (assoc method gnus-valid-select-methods)
3859           (list method
3860                 (if (memq 'prompt-address
3861                           (assoc method gnus-valid-select-methods))
3862                     (read-string "Address: ")
3863                   ""))
3864         (list method nil)))))
3865   
3866   (let* ((meth (if address (list (intern method) address) method))
3867          (nname (gnus-group-prefixed-name name meth))
3868          info)
3869     (and (gnus-gethash nname gnus-newsrc-hashtb)
3870          (error "Group %s already exists" nname))
3871     (gnus-group-change-level 
3872      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
3873      gnus-level-default-subscribed gnus-level-killed 
3874      (gnus-gethash (or (gnus-group-group-name) "dummy.group")
3875                        gnus-newsrc-hashtb) t)
3876     (gnus-sethash nname '(0 . 0) gnus-active-hashtb)
3877     (gnus-dribble-enter 
3878      (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")"))
3879     (gnus-group-insert-group-line-info nname)
3880
3881     (and (gnus-check-backend-function 'request-create-group nname)
3882          (gnus-request-create-group nname))))
3883
3884 (defun gnus-group-edit-group (group &optional part)
3885   "Edit the group on the current line."
3886   (interactive (list (gnus-group-group-name)))
3887   (let ((done-func '(lambda () 
3888                       "Exit editing mode and update the information."
3889                       (interactive)
3890                       (gnus-group-edit-group-done 'part 'group)))
3891         (part (or part 'info))
3892         (winconf (current-window-configuration))
3893         info)
3894     (if group (setq info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
3895       (error "No group on current line"))
3896     (set-buffer (get-buffer-create gnus-group-edit-buffer))
3897     (gnus-configure-windows 'edit-group)
3898     (gnus-add-current-to-buffer-list)
3899     (emacs-lisp-mode)
3900     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3901     (use-local-map (copy-keymap emacs-lisp-mode-map))
3902     (local-set-key "\C-c\C-c" done-func)
3903     (make-local-variable 'gnus-prev-winconf)
3904     (setq gnus-prev-winconf winconf)
3905     ;; We modify the func to let it know what part it is editing.
3906     (setcar (cdr (nth 4 done-func)) (list 'quote part))
3907     (setcar (cdr (cdr (nth 4 done-func))) group)
3908     (erase-buffer)
3909     (insert
3910      (cond 
3911       ((eq part 'method)
3912        ";; Type `C-c C-c' after editing the select method.\n\n")
3913       ((eq part 'params)
3914        ";; Type `C-c C-c' after editing the group parameters.\n\n")
3915       ((eq part 'info)
3916        ";; Type `C-c C-c' after editing the group info.\n\n")))
3917     (let ((cinfo (gnus-copy-sequence info))
3918           marked)
3919       (if (not (setq marked (nth 3 cinfo)))
3920           ()
3921         (while marked
3922           (or (eq 'score (car (car marked)))
3923               (eq 'bookmark (car (car marked)))
3924               (eq 'killed (car (car marked)))
3925               (setcdr (car marked) 
3926                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
3927           (setq marked (cdr marked))))
3928       (insert 
3929        (pp-to-string
3930         (cond ((eq part 'method)
3931                (or (nth 4 info) "native"))
3932               ((eq part 'params)
3933                (nth 5 info))
3934               (t
3935                cinfo)))
3936        "\n"))))
3937
3938 (defun gnus-group-edit-group-method (group)
3939   "Edit the select method of GROUP."
3940   (interactive (list (gnus-group-group-name)))
3941   (gnus-group-edit-group group 'method))
3942
3943 (defun gnus-group-edit-group-parameters (group)
3944   "Edit the group parameters of GROUP."
3945   (interactive (list (gnus-group-group-name)))
3946   (gnus-group-edit-group group 'params))
3947
3948 (defun gnus-group-edit-group-done (part group)
3949   "Get info from buffer, update variables and jump to the group buffer."
3950   (set-buffer (get-buffer-create gnus-group-edit-buffer))
3951   (goto-char (point-min))
3952   (let ((form (read (current-buffer)))
3953         (winconf gnus-prev-winconf))
3954     (if (eq part 'info) 
3955         (gnus-group-set-info form)
3956       (gnus-group-set-info form group part))
3957     (kill-buffer (current-buffer))
3958     (and winconf (set-window-configuration winconf))
3959     (set-buffer gnus-group-buffer)
3960     (gnus-group-update-group (gnus-group-group-name))
3961     (gnus-group-position-cursor)))
3962
3963 (defun gnus-group-make-help-group ()
3964   "Create the (ding) Gnus documentation group."
3965   (interactive)
3966   (let ((path load-path))
3967     (and (gnus-gethash (setq name (gnus-group-prefixed-name
3968                                    "gnus-help" '(nndoc "gnus-help")))
3969                        gnus-newsrc-hashtb)
3970          (error "Documentation group already exists"))
3971     (while (and path
3972                 (not (file-exists-p (concat (file-name-as-directory (car path))
3973                                             "doc.txt"))))
3974       (setq path (cdr path)))
3975     (or path (error "Couldn't find doc group"))
3976     (gnus-group-make-group 
3977      (gnus-group-real-name name)
3978      (list 'nndoc name
3979            (list 'nndoc-address (concat (file-name-as-directory (car path)) "doc.txt"))
3980            (list 'nndoc-article-type 'mbox))))
3981   (gnus-group-position-cursor))
3982
3983 (defun gnus-group-make-doc-group (file type)
3984   "Create a group that uses a single file as the source."
3985   (interactive 
3986    (list (read-file-name "File name: ") 
3987          (let ((err "")
3988                found char)
3989            (while (not found)
3990              (message "%sFile type (mbox, babyl, digest) [mbd]: " err)
3991              (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
3992                                ((= char ?b) 'babyl)
3993                                ((= char ?d) 'digest)
3994                                (t (setq mess "%c unknown. " char)
3995                                   nil))))
3996            found)))
3997   (let* ((file (expand-file-name file))
3998          (name (gnus-generate-new-group-name
3999                 (gnus-group-prefixed-name
4000                  (file-name-nondirectory file) '(nndoc "")))))
4001     (gnus-group-make-group 
4002      (gnus-group-real-name name)
4003      (list 'nndoc name
4004            (list 'nndoc-address file)
4005            (list 'nndoc-article-type type)))))
4006
4007 (defun gnus-group-make-archive-group ()
4008   "Create the (ding) Gnus archive group."
4009   (interactive)
4010   (and (gnus-gethash (gnus-group-prefixed-name "ding.archives" '(nndir ""))
4011                      gnus-newsrc-hashtb)
4012        (error "Archive group already exists"))
4013   (gnus-group-make-group "ding.archives" "nndir" gnus-group-archive-directory)
4014   (gnus-group-position-cursor))
4015
4016 (defun gnus-group-make-directory-group (dir)
4017   "Create an nndir group.
4018 The user will be prompted for a directory. The contents of this
4019 directory will be used as a newsgroup. The directory should contain
4020 mail messages or news articles in files that have numeric names."
4021   (interactive
4022    (list (read-file-name "Create group from directory: ")))
4023   (or (file-exists-p dir) (error "No such directory"))
4024   (or (file-directory-p dir) (error "Not a directory"))
4025   (gnus-group-make-group dir "nndir" dir)
4026   (gnus-group-position-cursor))
4027
4028 (defun gnus-group-make-kiboze-group (group address scores)
4029   "Create an nnkiboze group.
4030 The user will be prompted for a name, a regexp to match groups, and
4031 score file entries for articles to include in the group."
4032   (interactive
4033    (list
4034     (read-string "nnkiboze group name: ")
4035     (read-string "Source groups (regexp): ")
4036     (let ((headers (mapcar (lambda (group) (list group))
4037                            '("subject" "from" "number" "date" "message-id"
4038                              "references" "chars" "lines" "xref")))
4039           scores header regexp regexps)
4040       (while (not (equal "" (setq header (completing-read 
4041                                           "Match on header: " headers nil t))))
4042         (setq regexps nil)
4043         (while (not (equal "" (setq regexp (read-string 
4044                                             (format "Match on %s (string): "
4045                                                     header)))))
4046           (setq regexps (cons (list regexp nil nil 'r) regexps)))
4047         (setq scores (cons (cons header regexps) scores)))
4048       scores)))
4049   (gnus-group-make-group group "nnkiboze" address)
4050   (save-excursion
4051     (gnus-set-work-buffer)
4052     (let (emacs-lisp-mode-hook)
4053       (pp scores (current-buffer)))
4054     (write-region (point-min) (point-max) 
4055                   (concat (or gnus-kill-files-directory "~/News")
4056                           "nnkiboze:" group "." gnus-score-file-suffix)))
4057   (gnus-group-position-cursor))
4058
4059 (defun gnus-group-add-to-virtual (n vgroup)
4060   "Add the current group to a virtual group."
4061   (interactive
4062    (list current-prefix-arg
4063          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
4064                           "nnvirtual:")))
4065   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
4066       (error "%s is not an nnvirtual group" vgroup))
4067   (let* ((groups (gnus-group-process-prefix n))
4068          (method (nth 4 (nth 2 (gnus-gethash vgroup gnus-newsrc-hashtb)))))
4069     (setcar (cdr method)
4070             (concat 
4071              (nth 1 method) "\\|"
4072              (mapconcat 
4073               (lambda (s) 
4074                 (gnus-group-remove-mark s)
4075                 (concat "\\(^" (regexp-quote s) "$\\)"))
4076               groups "\\|"))))
4077   (gnus-group-position-cursor))
4078
4079 (defun gnus-group-make-empty-virtual (group)
4080   "Create a new, fresh, empty virtual group."
4081   (interactive "sCreate new, empty virtual group: ")
4082   (let* ((method (list 'nnvirtual "^$"))
4083          (pgroup (gnus-group-prefixed-name group method)))
4084     ;; Check whether it exists already.
4085     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
4086          (error "Group %s already exists." pgroup))
4087     ;; Subscribe the new group after the group on the current line.
4088     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
4089     (gnus-group-update-group pgroup)
4090     (forward-line -1)
4091     (gnus-group-position-cursor)))
4092
4093 (defun gnus-group-enter-directory (dir)
4094   "Enter an ephemeral nneething group."
4095   (interactive "DDirectory to read: ")
4096   (let* ((method (list 'nneething dir))
4097          (leaf (gnus-group-prefixed-name
4098                 (file-name-nondirectory (directory-file-name dir))
4099                 method))
4100          (name (gnus-generate-new-group-name leaf))
4101          (num 0))
4102     (let ((nneething-read-only t))
4103       (or (gnus-group-read-ephemeral-group 
4104            name method t
4105            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
4106                                       'summary 'group)))
4107           (error "Couldn't enter %s" dir)))))
4108
4109 ;; Group sorting commands
4110 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
4111
4112 (defun gnus-group-sort-groups ()
4113   "Sort the group buffer using `gnus-group-sort-function'."
4114   (interactive)
4115   (setq gnus-newsrc-alist 
4116         (sort (cdr gnus-newsrc-alist) gnus-group-sort-function))
4117   (gnus-make-hashtable-from-newsrc-alist)
4118   (gnus-group-list-groups nil gnus-have-all-newsgroups))
4119
4120 (defun gnus-group-sort-by-alphabet (info1 info2)
4121   (string< (car info1) (car info2)))
4122
4123 (defun gnus-group-sort-by-unread (info1 info2)
4124   (let ((n1 (car (gnus-gethash (car info1) gnus-newsrc-hashtb)))
4125         (n2 (car (gnus-gethash (car info2) gnus-newsrc-hashtb))))
4126     (< (or (and (numberp n1) n1) 0)
4127        (or (and (numberp n2) n2) 0))))
4128
4129 (defun gnus-group-sort-by-level (info1 info2)
4130   (< (nth 1 info1) (nth 1 info2)))
4131
4132 ;; Group catching up.
4133
4134 (defun gnus-group-catchup-current (n &optional all)
4135   "Mark all articles not marked as unread in current newsgroup as read.
4136 If prefix argument N is numeric, the ARG next newsgroups will be
4137 caught up. If ALL is non-nil, marked articles will also be marked as
4138 read. Cross references (Xref: header) of articles are ignored.
4139 The difference between N and actual number of newsgroups that were
4140 caught up is returned."
4141   (interactive "P")
4142   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
4143                gnus-expert-user
4144                (gnus-y-or-n-p
4145                 (if all
4146                     "Do you really want to mark all articles as read? "
4147                   "Mark all unread articles as read? "))))
4148       n
4149     (let ((groups (gnus-group-process-prefix n))
4150           (ret 0))
4151       (while groups
4152         ;; Virtual groups have to be given special treatment. 
4153         (let ((method (gnus-find-method-for-group (car groups))))
4154           (if (eq 'nnvirtual (car method))
4155               (nnvirtual-catchup-group
4156                (gnus-group-real-name (car groups)) (nth 1 method) all)))
4157         (gnus-group-remove-mark (car groups))
4158         (if (prog1
4159                 (gnus-group-goto-group (car groups))
4160               (gnus-group-catchup (car groups) all))
4161             (gnus-group-update-group-line)
4162           (setq ret (1+ ret)))
4163         (setq groups (cdr groups)))
4164       (gnus-group-next-unread-group 1)
4165       ret)))
4166
4167 (defun gnus-group-catchup-current-all (n)
4168   "Mark all articles in current newsgroup as read.
4169 Cross references (Xref: header) of articles are ignored."
4170   (interactive "P")
4171   (gnus-group-catchup-current n 'all))
4172
4173 (defun gnus-group-catchup (group &optional all)
4174   "Mark all articles in GROUP as read.
4175 If ALL is non-nil, all articles are marked as read.
4176 The return value is the number of articles that were marked as read,
4177 or nil if no action could be taken."
4178   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4179          (num (car entry))
4180          (marked (nth 3 (nth 2 entry)))
4181          ticked)
4182     (if (not (numberp (car entry)))
4183         (gnus-message 1 "Can't catch up; non-active group")
4184       ;; Do the updating only if the newsgroup isn't killed.
4185       (if entry
4186           (progn
4187             (setq ticked (if all nil (cdr (assq 'tick marked))))
4188             (gnus-update-read-articles group ticked nil ticked)
4189             (if (and all marked)
4190                 (setcar (nthcdr 3 (nth 2 entry)) 
4191                         (delq (assq 'dormant marked) marked))))))
4192     num))
4193
4194 (defun gnus-group-expire-articles (n)
4195   "Expire all expirable articles in the current newsgroup."
4196   (interactive "P")
4197   (let ((groups (gnus-group-process-prefix n))
4198         group)
4199     (or groups (error "No groups to expire"))
4200     (while groups
4201       (setq group (car groups)
4202             groups (cdr groups))
4203       (gnus-group-remove-mark group)
4204       (if (not (gnus-check-backend-function 'request-expire-articles group))
4205           ()
4206         (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
4207                (expirable (if (memq 'total-expire (nth 5 info))
4208                               (cons nil (gnus-list-of-read-articles group))
4209                             (assq 'expire (nth 3 info)))))
4210           (and expirable 
4211                (setcdr expirable
4212                        (gnus-request-expire-articles 
4213                         (cdr expirable) group))))))))
4214
4215 (defun gnus-group-expire-all-groups ()
4216   "Expire all expirable articles in all newsgroups."
4217   (interactive)
4218   (gnus-message 5 "Expiring...")
4219   (let ((gnus-group-marked (mapcar (lambda (info) (car info))
4220                                    (cdr gnus-newsrc-alist))))
4221     (gnus-group-expire-articles nil))
4222   (gnus-message 5 "Expiring...done"))
4223
4224 (defun gnus-group-set-current-level (n level)
4225   "Set the level of the next N groups to LEVEL."
4226   (interactive "P\nnLevel: ")
4227   (or (and (>= level 1) (<= level gnus-level-killed))
4228       (error "Illegal level: %d" level))
4229   (let ((groups (gnus-group-process-prefix n))
4230         group)
4231     (while groups
4232       (setq group (car groups)
4233             groups (cdr groups))
4234       (gnus-group-remove-mark group)
4235       (gnus-message 6 "Changed level of %s from %d to %d" 
4236                     group (gnus-group-group-level) level)
4237       (gnus-group-change-level group level
4238                                (gnus-group-group-level))
4239       (gnus-group-update-group-line)))
4240   (gnus-group-position-cursor))
4241
4242 (defun gnus-group-unsubscribe-current-group (arg)
4243   "Toggle subscribe from/to unsubscribe current group."
4244   (interactive "P")
4245   (let ((group (gnus-group-group-name)))
4246     (or group (error "No newsgroup on current line"))
4247     (or arg (setq arg (if (<= (gnus-group-group-level) gnus-level-subscribed)
4248                           gnus-level-default-unsubscribed
4249                         gnus-level-default-subscribed)))
4250     (gnus-group-unsubscribe-group group arg)
4251     (gnus-group-next-group 1)))
4252
4253 (defun gnus-group-unsubscribe-group (group &optional level)
4254   "Toggle subscribe from/to unsubscribe GROUP.
4255 New newsgroup is added to .newsrc automatically."
4256   (interactive
4257    (list (completing-read "Group: " gnus-active-hashtb nil 
4258                           gnus-have-read-active-file)))
4259   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
4260     (cond (newsrc
4261            ;; Toggle subscription flag.
4262            (gnus-group-change-level 
4263             newsrc (if level level (if (<= (nth 1 (nth 2 newsrc)) 
4264                                            gnus-level-subscribed) 
4265                                        (1+ gnus-level-subscribed)
4266                                      gnus-level-default-subscribed)))
4267            (gnus-group-update-group group))
4268           ((and (stringp group)
4269                 (or (not gnus-have-read-active-file)
4270                     (gnus-gethash group gnus-active-hashtb)))
4271            ;; Add new newsgroup.
4272            (gnus-group-change-level 
4273             group 
4274             (if level level gnus-level-default-subscribed) 
4275             (or (and (member group gnus-zombie-list) 
4276                      gnus-level-zombie) 
4277                 gnus-level-killed)
4278             (and (gnus-group-group-name)
4279                  (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
4280            (gnus-group-update-group group))
4281           (t (error "No such newsgroup: %s" group)))
4282     (gnus-group-position-cursor)))
4283
4284 (defun gnus-group-transpose-groups (n)
4285   "Move the current newsgroup up N places.
4286 If given a negative prefix, move down instead. The difference between
4287 N and the number of steps taken is retured." 
4288   (interactive "p")
4289   (or (gnus-group-group-name)
4290       (error "No group on current line"))
4291   (gnus-group-kill-group 1)
4292   (prog1
4293       (forward-line (- n))
4294     (gnus-group-yank-group)
4295     (gnus-group-position-cursor)))
4296
4297 (defun gnus-group-kill-all-zombies ()
4298   "Kill all zombie newsgroups."
4299   (interactive)
4300   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
4301   (setq gnus-zombie-list nil)
4302   (funcall gnus-group-prepare-function gnus-level-subscribed nil nil)
4303   (goto-char (point-min))
4304   (gnus-group-position-cursor))
4305
4306 (defun gnus-group-kill-region (begin end)
4307   "Kill newsgroups in current region (excluding current point).
4308 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
4309   (interactive "r")
4310   (let ((lines
4311          ;; Exclude a line where current point is on.
4312          (1-
4313           ;; Count lines.
4314           (save-excursion
4315             (count-lines
4316              (progn
4317                (goto-char begin)
4318                (beginning-of-line)
4319                (point))
4320              (progn
4321                (goto-char end)
4322                (end-of-line)
4323                (point)))))))
4324     (goto-char begin)
4325     (beginning-of-line)                 ;Important when LINES < 1
4326     (gnus-group-kill-group lines)))
4327
4328 (defun gnus-group-kill-group (n)
4329   "The the next N groups.
4330 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
4331 However, only groups that were alive can be yanked; already killed 
4332 groups or zombie groups can't be yanked.
4333 The return value is the name of the (last) group that was killed."
4334   (interactive "P")
4335   (let ((buffer-read-only nil)
4336         (groups (gnus-group-process-prefix n))
4337         group entry level)
4338     (while groups
4339       (setq group (car groups)
4340             groups (cdr groups))
4341       (gnus-group-remove-mark group)
4342       (setq level (gnus-group-group-level))
4343       (gnus-delete-line)
4344       (if (setq entry (gnus-gethash group gnus-newsrc-hashtb))
4345           (setq gnus-list-of-killed-groups 
4346                 (cons (cons (car entry) (nth 2 entry)) 
4347                       gnus-list-of-killed-groups)))
4348       (gnus-group-change-level 
4349        (if entry entry group) gnus-level-killed (if entry nil level)))
4350     (gnus-group-position-cursor)
4351     group))
4352
4353 (defun gnus-group-yank-group (&optional arg)
4354   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
4355 inserting it before the current newsgroup.  The numeric ARG specifies
4356 how many newsgroups are to be yanked.  The name of the (last)
4357 newsgroup yanked is returned."
4358   (interactive "p")
4359   (if (not arg) (setq arg 1))
4360   (let (info group prev)
4361     (while (>= (setq arg (1- arg)) 0)
4362       (if (not (setq info (car gnus-list-of-killed-groups)))
4363           (error "No more newsgroups to yank"))
4364       (setq group (nth 2 info))
4365       ;; Find which newsgroup to insert this one before - search
4366       ;; backward until something suitable is found. If there are no
4367       ;; other newsgroups in this buffer, just make this newsgroup the
4368       ;; first newsgroup.
4369       (setq prev (gnus-group-group-name))
4370       (gnus-group-change-level 
4371        info (nth 2 info) gnus-level-killed 
4372        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
4373        t)
4374       (gnus-group-insert-group-line-info (nth 1 info))
4375       (setq gnus-list-of-killed-groups 
4376             (cdr gnus-list-of-killed-groups)))
4377     (forward-line -1)
4378     (gnus-group-position-cursor)
4379     group))
4380       
4381 (defun gnus-group-list-all-groups (arg)
4382   "List all newsgroups with level ARG or lower.
4383 Default is gnus-level-unsubscribed, which lists all subscribed and most
4384 unsubscribed groups."
4385   (interactive "P")
4386   (setq arg (or arg gnus-level-unsubscribed))
4387   (gnus-group-list-groups arg t))
4388
4389 (defun gnus-group-list-killed ()
4390   "List all killed newsgroups in the group buffer."
4391   (interactive)
4392   (if (not gnus-killed-list)
4393       (gnus-message 6 "No killed groups")
4394     (funcall gnus-group-prepare-function gnus-level-killed t gnus-level-killed)
4395     (goto-char (point-min)))
4396   (gnus-group-position-cursor))
4397
4398 (defun gnus-group-list-zombies ()
4399   "List all zombie newsgroups in the group buffer."
4400   (interactive)
4401   (if (not gnus-zombie-list)
4402       (gnus-message 6 "No zombie groups")
4403     (funcall gnus-group-prepare-function gnus-level-zombie t gnus-level-zombie)
4404     (goto-char (point-min)))
4405   (gnus-group-position-cursor))
4406
4407 (defun gnus-group-get-new-news (&optional arg)
4408   "Get newly arrived articles.
4409 If ARG is non-nil, it should be a number between one and nine to
4410 specify which levels you are interested in re-scanning."
4411   (interactive "P")
4412   (run-hooks 'gnus-get-new-news-hook)
4413   (let ((level arg))
4414     (if gnus-group-use-permanent-levels
4415         (if level
4416             (setq gnus-group-default-list-level level)
4417           (setq level (or gnus-group-default-list-level 
4418                           gnus-level-subscribed))))
4419     (if (and gnus-read-active-file (not level))
4420         (progn
4421           (gnus-read-active-file)
4422           (gnus-get-unread-articles (or level (1+ gnus-level-subscribed))))
4423       (let ((gnus-read-active-file nil))
4424         (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))))
4425     (gnus-group-list-groups (or (and gnus-group-use-permanent-levels level)
4426                                 gnus-group-default-list-level
4427                                 gnus-level-subscribed)
4428                             gnus-have-all-newsgroups)))
4429
4430 (defun gnus-group-get-new-news-this-group (n)
4431   "Check for newly arrived news in the current group (and the N-1 next groups).
4432 The difference between N and the number of newsgroup checked is returned.
4433 If N is negative, this group and the N-1 previous groups will be checked."
4434   (interactive "P")
4435   (let* ((groups (gnus-group-process-prefix n))
4436          (ret (if (numberp n) (- n (length groups)) 0))
4437          (w-p (window-start))
4438          group)
4439     (while groups
4440       (setq group (car groups)
4441             groups (cdr groups))
4442       (gnus-group-remove-mark group)
4443       (or (gnus-get-new-news-in-group group)
4444           (progn 
4445             (ding) 
4446             (message "%s error: %s" group (gnus-status-message group))
4447             (sit-for 2))))
4448     ;; !!! I don't know why the buffer scrolls forward when updating
4449     ;; the first line in the group buffer, but it does. So we set the
4450     ;; window start forcibly.
4451 ;    (set-window-start (get-buffer-window (current-buffer)) w-p)
4452     (gnus-group-next-unread-group 1 t)
4453     (gnus-summary-position-cursor)
4454     ret))
4455
4456 (defun gnus-get-new-news-in-group (group)
4457   (and group 
4458        (gnus-activate-newsgroup group)
4459        (progn
4460          (gnus-get-unread-articles-in-group 
4461           (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
4462           (gnus-gethash group gnus-active-hashtb))
4463          (gnus-group-update-group-line)
4464          t)))
4465
4466 (defun gnus-group-fetch-faq (group)
4467   "Fetch the FAQ for the current group."
4468   (interactive (list (gnus-group-real-name (gnus-group-group-name))))
4469   (or group (error "No group name given"))
4470   (let ((file (concat gnus-group-faq-directory group))) 
4471     (if (not (file-exists-p file))
4472         (error "No such file: %s" file)
4473       (find-file file))))
4474   
4475 (defun gnus-group-describe-group (force &optional group)
4476   "Display a description of the current newsgroup."
4477   (interactive (list current-prefix-arg (gnus-group-group-name)))
4478   (and force (setq gnus-description-hashtb nil))
4479   (let ((method (gnus-find-method-for-group group))
4480         desc)
4481     (or group (error "No group name given"))
4482     (and (or (and gnus-description-hashtb
4483                   ;; We check whether this group's method has been
4484                   ;; queried for a description file.  
4485                   (gnus-gethash 
4486                    (gnus-group-prefixed-name "" method) 
4487                    gnus-description-hashtb))
4488              (setq desc (gnus-group-get-description group))
4489              (gnus-read-descriptions-file method))
4490          (message
4491           (or desc (gnus-gethash group gnus-description-hashtb)
4492               "No description available")))))
4493
4494 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4495 (defun gnus-group-describe-all-groups (force)
4496   "Pop up a buffer with descriptons of all newsgroups."
4497   (interactive "P")
4498   (and force (setq gnus-description-hashtb nil))
4499   (if (not (or gnus-description-hashtb
4500                (gnus-read-all-descriptions-files)))
4501       (error "Couldn't request descriptions file"))
4502   (let ((buffer-read-only nil)
4503         b)
4504     (erase-buffer)
4505     (mapatoms
4506      (lambda (group)
4507        (setq b (point))
4508        (insert (format "      *: %-20s %s\n" (symbol-name group)
4509                        (symbol-value group)))
4510        (add-text-properties 
4511         b (1+ b) (list 'gnus-group group
4512                        'gnus-unread t 'gnus-marked nil
4513                        'gnus-level (1+ gnus-level-subscribed))))
4514      gnus-description-hashtb)
4515     (goto-char (point-min))
4516     (gnus-group-position-cursor)))
4517
4518 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
4519 (defun gnus-group-apropos (regexp &optional search-description)
4520   "List all newsgroups that have names that match a regexp."
4521   (interactive "sGnus apropos (regexp): ")
4522   (let ((prev "")
4523         (obuf (current-buffer))
4524         groups des prev)
4525     ;; Go through all newsgroups that are known to Gnus.
4526     (mapatoms 
4527      (lambda (group)
4528        (and (string-match regexp (symbol-name group))
4529             (setq groups (cons (symbol-name group) groups))))
4530      gnus-active-hashtb)
4531     ;; Go through all descriptions that are known to Gnus. 
4532     (if search-description
4533         (mapatoms 
4534          (lambda (group)
4535            (and (string-match regexp (symbol-value group))
4536                 (gnus-gethash (symbol-name group) gnus-active-hashtb)
4537                 (setq groups (cons (symbol-name group) groups))))
4538          gnus-description-hashtb))
4539     (if (not groups)
4540         (gnus-message 3 "No groups matched \"%s\"." regexp)
4541       ;; Print out all the groups.
4542       (save-excursion
4543         (pop-to-buffer "*Gnus Help*")
4544         (buffer-disable-undo (current-buffer))
4545         (erase-buffer)
4546         (setq groups (sort groups 'string<))
4547         (while groups
4548           ;; Groups may be entered twice into the list of groups.
4549           (if (not (string= (car groups) prev))
4550               (progn
4551                 (insert (setq prev (car groups)) "\n")
4552                 (if (and gnus-description-hashtb
4553                          (setq des (gnus-gethash (car groups) 
4554                                                  gnus-description-hashtb)))
4555                     (insert "  " des "\n"))))
4556           (setq groups (cdr groups)))
4557         (goto-char (point-min))))
4558     (pop-to-buffer obuf)))
4559
4560 (defun gnus-group-description-apropos (regexp)
4561   "List all newsgroups that have names or desccriptions that match a regexp."
4562   (interactive "sGnus description apropos (regexp): ")
4563   (if (not (or gnus-description-hashtb
4564                (gnus-read-all-descriptions-files)))
4565       (error "Couldn't request descriptions file"))
4566   (gnus-group-apropos regexp t))
4567
4568 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4569 (defun gnus-group-list-matching (level regexp &optional all lowest) 
4570   "List all groups with unread articles that match REGEXP.
4571 If the prefix LEVEL is non-nil, it should be a number that says which
4572 level to cut off listing groups. 
4573 If ALL, also list groups with no unread articles.
4574 If LOWEST, don't list groups with level lower than LOWEST."
4575   (interactive "P\nsList newsgroups matching: ")
4576   (gnus-group-prepare-flat (or level gnus-level-subscribed)
4577                            all (or lowest 1) regexp)
4578   (goto-char (point-min))
4579   (gnus-group-position-cursor))
4580
4581 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
4582   "List all groups that match REGEXP.
4583 If the prefix LEVEL is non-nil, it should be a number that says which
4584 level to cut off listing groups. 
4585 If LOWEST, don't list groups with level lower than LOWEST."
4586   (interactive "P\nsList newsgroups matching: ")
4587   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
4588
4589 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
4590 (defun gnus-group-save-newsrc ()
4591   "Save the Gnus startup files."
4592   (interactive)
4593   (gnus-save-newsrc-file))
4594
4595 (defun gnus-group-restart (&optional arg)
4596   "Force Gnus to read the .newsrc file."
4597   (interactive "P")
4598   (gnus-save-newsrc-file)
4599   (gnus-setup-news 'force)
4600   (gnus-group-list-groups arg gnus-have-all-newsgroups))
4601
4602 (defun gnus-group-read-init-file ()
4603   "Read the Gnus elisp init file."
4604   (interactive)
4605   (gnus-read-init-file))
4606
4607 (defun gnus-group-check-bogus-groups (silent)
4608   "Check bogus newsgroups.
4609 If given a prefix, don't ask for confirmation before removing a bogus
4610 group."
4611   (interactive "P")
4612   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
4613   (gnus-group-list-groups nil gnus-have-all-newsgroups))
4614
4615 (defun gnus-group-edit-global-kill (article &optional group)
4616   "Edit the global kill file.
4617 If GROUP, edit that local kill file instead."
4618   (interactive "P")
4619   (setq gnus-current-kill-article article)
4620   (gnus-kill-file-edit-file group)
4621   (gnus-message 6
4622    (substitute-command-keys
4623     "Editing a global kill file (Type \\[gnus-kill-file-exit] to exit)")))
4624
4625 (defun gnus-group-edit-local-kill (article group)
4626   "Edit a local kill file."
4627   (interactive (list nil (gnus-group-group-name)))
4628   (gnus-group-edit-global-kill article group))
4629
4630 (defun gnus-group-force-update ()
4631   "Update `.newsrc' file."
4632   (interactive)
4633   (gnus-save-newsrc-file))
4634
4635 (defun gnus-group-suspend ()
4636   "Suspend the current Gnus session.
4637 In fact, cleanup buffers except for group mode buffer.
4638 The hook gnus-suspend-gnus-hook is called before actually suspending."
4639   (interactive)
4640   (run-hooks 'gnus-suspend-gnus-hook)
4641   ;; Kill Gnus buffers except for group mode buffer.
4642   (let ((group-buf (get-buffer gnus-group-buffer)))
4643     ;; Do this on a separate list in case the user does a ^G before we finish
4644     (let ((gnus-buffer-list
4645            (delq group-buf (delq gnus-dribble-buffer
4646                                  (append gnus-buffer-list nil)))))
4647       (while gnus-buffer-list
4648         (gnus-kill-buffer (car gnus-buffer-list))
4649         (setq gnus-buffer-list (cdr gnus-buffer-list))))
4650     (if group-buf
4651         (progn
4652           (setq gnus-buffer-list (list group-buf))
4653           (bury-buffer group-buf)
4654           (delete-windows-on group-buf t)))))
4655
4656 (defun gnus-group-clear-dribble ()
4657   "Clear all information from the dribble buffer."
4658   (interactive)
4659   (gnus-dribble-clear))
4660
4661 (defun gnus-group-exit ()
4662   "Quit reading news after updating .newsrc.eld and .newsrc.
4663 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4664   (interactive)
4665   (if (or noninteractive                ;For gnus-batch-kill
4666           (zerop (buffer-size))         ;No news is good news.
4667           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
4668           (not gnus-interactive-exit)   ;Without confirmation
4669           gnus-expert-user
4670           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
4671       (progn
4672         (run-hooks 'gnus-exit-gnus-hook)
4673         (gnus-offer-save-summaries)
4674         (gnus-save-newsrc-file)
4675         (gnus-close-backends)
4676         (gnus-clear-system))))
4677
4678 (defun gnus-close-backends ()
4679   ;; Send a close request to all backends that support such a request. 
4680   (let ((methods gnus-valid-select-methods)
4681         func)
4682     (while methods
4683       (if (fboundp (setq func (intern (concat (car (car methods))
4684                                               "-request-close"))))
4685           (funcall func))
4686       (setq methods (cdr methods)))))
4687
4688 (defun gnus-group-quit ()
4689   "Quit reading news without updating .newsrc.eld or .newsrc.
4690 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4691   (interactive)
4692   (if (or noninteractive                ;For gnus-batch-kill
4693           (zerop (buffer-size))
4694           (not (gnus-server-opened gnus-select-method))
4695           gnus-expert-user
4696           (not gnus-current-startup-file)
4697           (gnus-yes-or-no-p
4698            (format "Quit reading news without saving %s? "
4699                    (file-name-nondirectory gnus-current-startup-file))))
4700       (progn
4701         (run-hooks 'gnus-exit-gnus-hook)
4702         (gnus-dribble-save)
4703         (gnus-close-backends)
4704         (gnus-clear-system))))
4705
4706 (defun gnus-offer-save-summaries ()
4707   (let ((buffers (buffer-list)))
4708     (save-excursion
4709       (while buffers
4710         (and 
4711          ;; We look for buffers with "Summary" in the name.
4712          (string-match "Summary" (or (buffer-name (car buffers)) ""))
4713          (progn
4714            (set-buffer (car buffers))
4715            ;; We check that this is, indeed, a summary buffer.
4716            (eq major-mode 'gnus-summary-mode)) 
4717          ;; We ask the user whether she wants to save the info.
4718          (gnus-y-or-n-p
4719                (format "Update summary buffer %s? " (buffer-name)))
4720          ;; We do it by simply exiting.
4721          (gnus-summary-exit))
4722         (setq buffers (cdr buffers))))))
4723
4724 (defun gnus-group-describe-briefly ()
4725   "Give a one line description of the group mode commands."
4726   (interactive)
4727   (gnus-message 6
4728    (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")))
4729
4730 (defun gnus-group-browse-foreign-server (method)
4731   "Browse a foreign news server.
4732 If called interactively, this function will ask for a select method
4733  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
4734 If not, METHOD should be a list where the first element is the method
4735 and the second element is the address."
4736   (interactive
4737    (list (let ((how (completing-read 
4738                      "Which backend: "
4739                      (append gnus-valid-select-methods gnus-server-alist)
4740                      nil t "nntp")))
4741            ;; We either got a backend name or a virtual server name.
4742            ;; If the first, we also need an address.
4743            (if (assoc how gnus-valid-select-methods)
4744                (list (intern how)
4745                      ;; Suggested by mapjph@bath.ac.uk.
4746                      (completing-read 
4747                       "Address: " 
4748                       (mapcar (lambda (server) (list server))
4749                               gnus-secondary-servers)))
4750              ;; We got a server name, so we find the method.
4751              (gnus-server-to-method how)))))
4752   (gnus-browse-foreign-server method))
4753
4754 \f
4755 ;;;
4756 ;;; Browse Server Mode
4757 ;;;
4758
4759 (defvar gnus-browse-mode-hook nil)
4760 (defvar gnus-browse-mode-map nil)
4761 (put 'gnus-browse-mode 'mode-class 'special)
4762
4763 (if gnus-browse-mode-map
4764     nil
4765   (setq gnus-browse-mode-map (make-keymap))
4766   (suppress-keymap gnus-browse-mode-map)
4767   (define-key gnus-browse-mode-map " " 'gnus-browse-read-group)
4768   (define-key gnus-browse-mode-map "=" 'gnus-browse-select-group)
4769   (define-key gnus-browse-mode-map "n" 'gnus-browse-next-group)
4770   (define-key gnus-browse-mode-map "p" 'gnus-browse-prev-group)
4771   (define-key gnus-browse-mode-map "\177" 'gnus-browse-prev-group)
4772   (define-key gnus-browse-mode-map "N" 'gnus-browse-next-group)
4773   (define-key gnus-browse-mode-map "P" 'gnus-browse-prev-group)
4774   (define-key gnus-browse-mode-map "\M-n" 'gnus-browse-next-group)
4775   (define-key gnus-browse-mode-map "\M-p" 'gnus-browse-prev-group)
4776   (define-key gnus-browse-mode-map "\r" 'gnus-browse-select-group)
4777   (define-key gnus-browse-mode-map "u" 'gnus-browse-unsubscribe-current-group)
4778   (define-key gnus-browse-mode-map "l" 'gnus-browse-exit)
4779   (define-key gnus-browse-mode-map "L" 'gnus-browse-exit)
4780   (define-key gnus-browse-mode-map "q" 'gnus-browse-exit)
4781   (define-key gnus-browse-mode-map "Q" 'gnus-browse-exit)
4782   (define-key gnus-browse-mode-map "\C-c\C-c" 'gnus-browse-exit)
4783   (define-key gnus-browse-mode-map "?" 'gnus-browse-describe-briefly)
4784   (define-key gnus-browse-mode-map "\C-c\C-i" 'gnus-info-find-node)
4785   )
4786
4787 (defvar gnus-browse-current-method nil)
4788 (defvar gnus-browse-return-buffer nil)
4789
4790 (defvar gnus-browse-buffer "*Gnus Browse Server*")
4791
4792 (defun gnus-browse-foreign-server (method &optional return-buffer)
4793   (setq gnus-browse-current-method method)
4794   (setq gnus-browse-return-buffer return-buffer)
4795   (let ((gnus-select-method method)
4796         groups group)
4797     (gnus-message 5 "Connecting to %s..." (nth 1 method))
4798     (or (gnus-server-opened method)
4799         (gnus-open-server method)
4800         (error "Unable to contact server: %s" (gnus-status-message method)))
4801     (or (gnus-request-list method)
4802         (error "Couldn't request list: %s" (gnus-status-message method)))
4803     (get-buffer-create gnus-browse-buffer)
4804     (gnus-add-current-to-buffer-list)
4805     (and gnus-carpal (gnus-carpal-setup-buffer 'browse))
4806     (gnus-configure-windows 'browse)
4807     (buffer-disable-undo (current-buffer))
4808     (let ((buffer-read-only nil))
4809       (erase-buffer))
4810     (gnus-browse-mode)
4811     (setq mode-line-buffer-identification
4812           (format
4813            "(ding) Browse Server {%s:%s}" (car method) (car (cdr method))))
4814     (save-excursion
4815       (set-buffer nntp-server-buffer)
4816       (let ((cur (current-buffer)))
4817         (goto-char (point-min))
4818         (or (string= gnus-ignored-newsgroups "")
4819             (delete-matching-lines gnus-ignored-newsgroups))
4820         (while (re-search-forward 
4821                 "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
4822           (goto-char (match-end 1))
4823           (setq groups (cons (cons (buffer-substring (match-beginning 1)
4824                                                      (match-end 1))
4825                                    (max 0 (- (1+ (read cur)) (read cur))))
4826                              groups)))))
4827     (setq groups (sort groups 
4828                        (lambda (l1 l2)
4829                          (string< (car l1) (car l2)))))
4830     (let ((buffer-read-only nil))
4831       (while groups
4832         (setq group (car groups))
4833         (insert 
4834          (format "K%7d: %s\n" (cdr group) (car group)))
4835         (setq groups (cdr groups))))
4836     (switch-to-buffer (current-buffer))
4837     (goto-char (point-min))
4838     (gnus-group-position-cursor)))
4839
4840 (defun gnus-browse-mode ()
4841   "Major mode for browsing a foreign server.
4842
4843 All normal editing commands are switched off.
4844
4845 \\<gnus-browse-mode-map>
4846 The only things you can do in this buffer is
4847
4848 1) `\\[gnus-browse-unsubscribe-current-group]' to subscribe to a group.
4849 The group will be inserted into the group buffer upon exit from this
4850 buffer.  
4851
4852 2) `\\[gnus-browse-read-group]' to read a group ephemerally.
4853
4854 3) `\\[gnus-browse-exit]' to return to the group buffer."
4855   (interactive)
4856   (kill-all-local-variables)
4857   (if gnus-visual (gnus-browse-make-menu-bar))
4858   (setq mode-line-modified "-- ")
4859   (make-local-variable 'mode-line-format)
4860   (setq mode-line-format (copy-sequence mode-line-format))
4861   (and (equal (nth 3 mode-line-format) "   ")
4862        (setcar (nthcdr 3 mode-line-format) ""))
4863   (setq major-mode 'gnus-browse-mode)
4864   (setq mode-name "Browse Server")
4865   (setq mode-line-process nil)
4866   (use-local-map gnus-browse-mode-map)
4867   (buffer-disable-undo (current-buffer))
4868   (setq truncate-lines t)
4869   (setq buffer-read-only t)
4870   (run-hooks 'gnus-browse-mode-hook))
4871
4872 (defun gnus-browse-read-group (&optional no-article)
4873   "Enter the group at the current line."
4874   (interactive)
4875   (let ((group (gnus-browse-group-name)))
4876     (or (gnus-group-read-ephemeral-group 
4877          group gnus-browse-current-method nil
4878          (cons (current-buffer) 'browse))
4879         (error "Couldn't enter %s" group))))
4880
4881 (defun gnus-browse-select-group ()
4882   "Select the current group."
4883   (interactive)
4884   (gnus-browse-read-group 'no))
4885
4886 (defun gnus-browse-next-group (n)
4887   "Go to the next group."
4888   (interactive "p")
4889   (prog1
4890       (forward-line n)
4891     (gnus-group-position-cursor)))
4892
4893 (defun gnus-browse-prev-group (n)
4894   "Go to the next group."
4895   (interactive "p")
4896   (gnus-browse-next-group (- n)))
4897
4898 (defun gnus-browse-unsubscribe-current-group (arg)
4899   "(Un)subscribe to the next ARG groups."
4900   (interactive "p")
4901   (and (eobp)
4902        (error "No group at current line."))
4903   (let ((ward (if (< arg 0) -1 1))
4904         (arg (abs arg)))
4905     (while (and (> arg 0)
4906                 (not (eobp))
4907                 (gnus-browse-unsubscribe-group)
4908                 (zerop (gnus-browse-next-group ward)))
4909       (setq arg (1- arg)))
4910     (gnus-group-position-cursor)
4911     (if (/= 0 arg) (gnus-message 7 "No more newsgroups"))
4912     arg))
4913
4914 (defun gnus-browse-group-name ()
4915   (save-excursion
4916     (beginning-of-line)
4917     (if (not (re-search-forward ": \\(.*\\)$" (gnus-point-at-eol) t))
4918         ()
4919       (gnus-group-prefixed-name 
4920        (buffer-substring (match-beginning 1) (match-end 1))
4921        gnus-browse-current-method))))
4922   
4923 (defun gnus-browse-unsubscribe-group ()
4924   (let ((sub nil)
4925         (buffer-read-only nil)
4926         group)
4927     (save-excursion
4928       (beginning-of-line)
4929       (if (= (following-char) ?K) (setq sub t))
4930       (setq group (gnus-browse-group-name))
4931       (beginning-of-line)
4932       (delete-char 1)
4933       (if sub
4934           (progn
4935             (gnus-group-change-level 
4936              (list t group gnus-level-default-subscribed
4937                    nil nil gnus-browse-current-method) 
4938              gnus-level-default-subscribed gnus-level-killed
4939              (gnus-gethash (car (nth 1 gnus-newsrc-alist)) gnus-newsrc-hashtb)
4940              t)
4941             (insert ? ))
4942         (gnus-group-change-level 
4943          group gnus-level-killed gnus-level-default-subscribed)
4944         (insert ?K)))
4945     t))
4946
4947 (defun gnus-browse-exit ()
4948   "Quit browsing and return to the group buffer."
4949   (interactive)
4950   (if (eq major-mode 'gnus-browse-mode)
4951       (kill-buffer (current-buffer)))
4952   (if gnus-browse-return-buffer
4953       (gnus-configure-windows 'server)
4954     (gnus-configure-windows 'group)))
4955
4956 (defun gnus-browse-describe-briefly ()
4957   "Give a one line description of the group mode commands."
4958   (interactive)
4959   (gnus-message 6
4960    (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")))
4961       
4962 \f
4963 ;;;
4964 ;;; Gnus summary mode
4965 ;;;
4966
4967 (defvar gnus-summary-mode-map nil)
4968 (defvar gnus-summary-mark-map nil)
4969 (defvar gnus-summary-mscore-map nil)
4970 (defvar gnus-summary-article-map nil)
4971 (defvar gnus-summary-thread-map nil)
4972 (defvar gnus-summary-goto-map nil)
4973 (defvar gnus-summary-exit-map nil)
4974 (defvar gnus-summary-various-map nil)
4975 (defvar gnus-summary-interest-map nil)
4976 (defvar gnus-summary-sort-map nil)
4977 (defvar gnus-summary-backend-map nil)
4978 (defvar gnus-summary-save-map nil)
4979 (defvar gnus-summary-wash-map nil)
4980 (defvar gnus-summary-help-map nil)
4981
4982 (put 'gnus-summary-mode 'mode-class 'special)
4983
4984 (if gnus-summary-mode-map
4985     nil
4986   (setq gnus-summary-mode-map (make-keymap))
4987   (suppress-keymap gnus-summary-mode-map)
4988
4989   ;; Non-orthogonal keys
4990
4991   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
4992   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
4993   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
4994   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
4995   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
4996   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
4997   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
4998   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
4999   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
5000   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
5001   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
5002   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
5003   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
5004   (define-key gnus-summary-mode-map "\M-s" 'gnus-summary-search-article-forward)
5005   (define-key gnus-summary-mode-map "\M-r" 'gnus-summary-search-article-backward)
5006   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
5007   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
5008   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-subject)
5009   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
5010   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
5011   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
5012   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
5013   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
5014   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
5015   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
5016   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
5017   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
5018   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
5019   (define-key gnus-summary-mode-map "k" 'gnus-summary-kill-same-subject-and-select)
5020   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
5021   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
5022   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
5023   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
5024   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
5025   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
5026   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
5027   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
5028   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
5029   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
5030   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
5031   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
5032   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
5033   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
5034   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
5035   (define-key gnus-summary-mode-map "\C-w" 'gnus-summary-mark-region-as-read)
5036   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
5037   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
5038   (define-key gnus-summary-mode-map "\C-c\M-\C-s" 'gnus-summary-show-all-expunged)
5039   (define-key gnus-summary-mode-map "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
5040   (define-key gnus-summary-mode-map "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
5041   (define-key gnus-summary-mode-map "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
5042   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
5043   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
5044   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
5045   (define-key gnus-summary-mode-map "\C-x\C-s" 'gnus-summary-reselect-current-group)
5046   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
5047   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
5048   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
5049   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
5050   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
5051   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
5052   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
5053   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
5054   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
5055   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
5056   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
5057   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
5058   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
5059   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
5060   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
5061   (define-key gnus-summary-mode-map "V" 'gnus-version)
5062   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
5063   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
5064   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
5065   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
5066   (define-key gnus-summary-mode-map gnus-mouse-2 'gnus-mouse-pick-article)
5067   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
5068   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
5069   (define-key gnus-summary-mode-map "x" 'gnus-summary-remove-lines-marked-as-read)
5070 ; (define-key gnus-summary-mode-map "X" 'gnus-summary-remove-lines-marked-with)
5071   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
5072   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
5073   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
5074 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
5075   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
5076   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu-view)
5077   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
5078
5079
5080   ;; Sort of orthogonal keymap
5081   (define-prefix-command 'gnus-summary-mark-map)
5082   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
5083   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
5084   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
5085   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
5086   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
5087   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
5088   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
5089   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
5090   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
5091   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
5092   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
5093   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
5094   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
5095   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
5096   (define-key gnus-summary-mark-map "\M-r" 'gnus-summary-remove-lines-marked-as-read)
5097   (define-key gnus-summary-mark-map "\M-\C-r" 'gnus-summary-remove-lines-marked-with)
5098   (define-key gnus-summary-mark-map "D" 'gnus-summary-show-all-dormant)
5099   (define-key gnus-summary-mark-map "\M-D" 'gnus-summary-hide-all-dormant)
5100   (define-key gnus-summary-mark-map "S" 'gnus-summary-show-all-expunged)
5101   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
5102   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
5103   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
5104   (define-key gnus-summary-mark-map "k" 'gnus-summary-kill-same-subject-and-select)
5105   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
5106
5107   (define-prefix-command 'gnus-summary-mscore-map)
5108   (define-key gnus-summary-mark-map "s" 'gnus-summary-mscore-map)
5109   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
5110   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
5111   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
5112   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
5113
5114   (define-key gnus-summary-mark-map "p" 'gnus-uu-mark-map)
5115   
5116   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
5117   
5118   (define-prefix-command 'gnus-summary-goto-map)
5119   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
5120   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
5121   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
5122   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
5123   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
5124   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
5125   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
5126   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
5127   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
5128   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
5129   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
5130   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
5131   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
5132   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
5133
5134
5135   (define-prefix-command 'gnus-summary-thread-map)
5136   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
5137   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
5138   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
5139   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
5140   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
5141   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
5142   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
5143   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
5144   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
5145   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
5146   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
5147   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
5148   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
5149   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
5150
5151   
5152   (define-prefix-command 'gnus-summary-exit-map)
5153   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
5154   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
5155   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
5156   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
5157   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
5158   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
5159   (define-key gnus-summary-exit-map "n" 'gnus-summary-catchup-and-goto-next-group)
5160   (define-key gnus-summary-exit-map "R" 'gnus-summary-reselect-current-group)
5161   (define-key gnus-summary-exit-map "G" 'gnus-summary-rescan-group)
5162   (define-key gnus-summary-exit-map "N" 'gnus-summary-next-group)
5163   (define-key gnus-summary-exit-map "P" 'gnus-summary-prev-group)
5164
5165
5166   (define-prefix-command 'gnus-summary-article-map)
5167   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
5168   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
5169   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
5170   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
5171   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
5172   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
5173   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
5174   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
5175   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
5176   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
5177   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
5178   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
5179   (define-key gnus-summary-article-map "w" 'gnus-summary-stop-page-breaking)
5180   (define-key gnus-summary-article-map "c" 'gnus-summary-caesar-message)
5181   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
5182   (define-key gnus-summary-article-map "t" 'gnus-summary-toggle-header)
5183   (define-key gnus-summary-article-map "m" 'gnus-summary-toggle-mime)
5184   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
5185
5186
5187   (define-prefix-command 'gnus-summary-wash-map)
5188   (define-key gnus-summary-mode-map "W" 'gnus-summary-wash-map)
5189   (define-key gnus-summary-wash-map "h" 'gnus-article-hide-headers)
5190   (define-key gnus-summary-wash-map "s" 'gnus-article-hide-signature)
5191   (define-key gnus-summary-wash-map "c" 'gnus-article-hide-citation)
5192   (define-key gnus-summary-wash-map "o" 'gnus-article-treat-overstrike)
5193   (define-key gnus-summary-wash-map "w" 'gnus-article-word-wrap)
5194   (define-key gnus-summary-wash-map "d" 'gnus-article-remove-cr)
5195   (define-key gnus-summary-wash-map "q" 'gnus-article-de-quoted-unreadable)
5196   (define-key gnus-summary-wash-map "f" 'gnus-article-display-x-face)
5197   (define-key gnus-summary-wash-map "t" 'gnus-article-date-ut)
5198   (define-key gnus-summary-wash-map "\C-t" 'gnus-article-date-local)
5199   (define-key gnus-summary-wash-map "T" 'gnus-article-date-lapsed)
5200
5201   (define-key gnus-summary-wash-map "A" 'gnus-article-highlight)
5202   (define-key gnus-summary-wash-map "a" 'gnus-article-hide)
5203   (define-key gnus-summary-wash-map "H" 'gnus-article-highlight-headers)
5204   (define-key gnus-summary-wash-map "C" 'gnus-article-highlight-citation)
5205   (define-key gnus-summary-wash-map "S" 'gnus-article-highlight-signature)
5206   (define-key gnus-summary-wash-map "b" 'gnus-article-add-buttons)
5207
5208
5209   (define-prefix-command 'gnus-summary-help-map)
5210   (define-key gnus-summary-mode-map "H" 'gnus-summary-help-map)
5211   (define-key gnus-summary-help-map "v" 'gnus-version)
5212   (define-key gnus-summary-help-map "f" 'gnus-summary-fetch-faq)
5213   (define-key gnus-summary-help-map "d" 'gnus-summary-describe-group)
5214   (define-key gnus-summary-help-map "h" 'gnus-summary-describe-briefly)
5215   (define-key gnus-summary-help-map "i" 'gnus-info-find-node)
5216
5217
5218   (define-prefix-command 'gnus-summary-backend-map)
5219   (define-key gnus-summary-mode-map "B" 'gnus-summary-backend-map)
5220   (define-key gnus-summary-backend-map "e" 'gnus-summary-expire-articles)
5221   (define-key gnus-summary-backend-map "\177" 'gnus-summary-delete-article)
5222   (define-key gnus-summary-backend-map "m" 'gnus-summary-move-article)
5223   (define-key gnus-summary-backend-map "r" 'gnus-summary-respool-article)
5224   (define-key gnus-summary-backend-map "w" 'gnus-summary-edit-article)
5225   (define-key gnus-summary-backend-map "c" 'gnus-summary-copy-article)
5226   (define-key gnus-summary-backend-map "q" 'gnus-summary-fancy-query)
5227   (define-key gnus-summary-backend-map "i" 'gnus-summary-import-article)
5228
5229
5230   (define-prefix-command 'gnus-summary-save-map)
5231   (define-key gnus-summary-mode-map "O" 'gnus-summary-save-map)
5232   (define-key gnus-summary-save-map "o" 'gnus-summary-save-article)
5233   (define-key gnus-summary-save-map "m" 'gnus-summary-save-article-mail)
5234   (define-key gnus-summary-save-map "r" 'gnus-summary-save-article-rmail)
5235   (define-key gnus-summary-save-map "f" 'gnus-summary-save-article-file)
5236   (define-key gnus-summary-save-map "h" 'gnus-summary-save-article-folder)
5237   (define-key gnus-summary-save-map "v" 'gnus-summary-save-article-vm)
5238   (define-key gnus-summary-save-map "p" 'gnus-summary-pipe-output)
5239 ;  (define-key gnus-summary-save-map "s" 'gnus-soup-add-article)
5240
5241   (define-key gnus-summary-mode-map "X" 'gnus-uu-extract-map)
5242   
5243   (define-prefix-command 'gnus-summary-various-map)
5244   (define-key gnus-summary-mode-map "V" 'gnus-summary-various-map)
5245   (define-key gnus-summary-various-map "u" 'gnus-summary-universal-argument)
5246   (define-key gnus-summary-various-map "\C-s" 'gnus-summary-search-article-forward)
5247   (define-key gnus-summary-various-map "\C-r" 'gnus-summary-search-article-backward)
5248   (define-key gnus-summary-various-map "r" 'gnus-summary-refer-article)
5249   (define-key gnus-summary-various-map "&" 'gnus-summary-execute-command)
5250   (define-key gnus-summary-various-map "T" 'gnus-summary-toggle-truncation)
5251   (define-key gnus-summary-various-map "e" 'gnus-summary-expand-window)
5252   (define-key gnus-summary-various-map "D" 'gnus-summary-enter-digest-group)
5253   (define-key gnus-summary-various-map "k" 'gnus-summary-edit-local-kill)
5254   (define-key gnus-summary-various-map "K" 'gnus-summary-edit-global-kill)
5255
5256   (define-key gnus-summary-various-map "S" 'gnus-summary-score-map)
5257
5258   (define-prefix-command 'gnus-summary-sort-map)
5259   (define-key gnus-summary-various-map "s" 'gnus-summary-sort-map)
5260   (define-key gnus-summary-sort-map "n" 'gnus-summary-sort-by-number)
5261   (define-key gnus-summary-sort-map "a" 'gnus-summary-sort-by-author)
5262   (define-key gnus-summary-sort-map "s" 'gnus-summary-sort-by-subject)
5263   (define-key gnus-summary-sort-map "d" 'gnus-summary-sort-by-date)
5264   (define-key gnus-summary-sort-map "i" 'gnus-summary-sort-by-score)
5265
5266   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-score)
5267   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-score)
5268   )
5269
5270
5271 \f
5272
5273 (defun gnus-summary-mode ()
5274   "Major mode for reading articles.
5275
5276 All normal editing commands are switched off.
5277 \\<gnus-summary-mode-map>
5278 Each line in this buffer represents one article.  To read an
5279 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
5280 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
5281 respectively.
5282
5283 You can also post articles and send mail from this buffer.  To 
5284 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
5285 of an article, type `\\[gnus-summary-reply]'.
5286
5287 There are approx. one gazillion commands you can execute in this 
5288 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
5289
5290 The following commands are available:
5291
5292 \\{gnus-summary-mode-map}"
5293   (interactive)
5294   (if gnus-visual (gnus-summary-make-menu-bar))
5295   (kill-all-local-variables)
5296   (let ((locals gnus-summary-local-variables))
5297     (while locals
5298       (if (consp (car locals))
5299           (progn
5300             (make-local-variable (car (car locals)))
5301             (set (car (car locals)) (eval (cdr (car locals)))))
5302         (make-local-variable (car locals))
5303         (set (car locals) nil))
5304       (setq locals (cdr locals))))
5305   (gnus-make-thread-indent-array)
5306   (gnus-update-format-specifications)
5307   (setq mode-line-modified "-- ")
5308   (make-local-variable 'mode-line-format)
5309   (setq mode-line-format (copy-sequence mode-line-format))
5310   (and (equal (nth 3 mode-line-format) "   ")
5311        (setcar (nthcdr 3 mode-line-format) ""))
5312   (setq major-mode 'gnus-summary-mode)
5313   (setq mode-name "Summary")
5314   (make-local-variable 'minor-mode-alist)
5315   (use-local-map gnus-summary-mode-map)
5316   (buffer-disable-undo (current-buffer))
5317   (setq buffer-read-only t)             ;Disable modification
5318   (setq truncate-lines t)
5319   (setq selective-display t)
5320   (setq selective-display-ellipses t)   ;Display `...'
5321   (setq buffer-display-table gnus-summary-display-table)
5322   (run-hooks 'gnus-summary-mode-hook))
5323
5324 (defun gnus-summary-make-display-table ()
5325   ;; Change the display table.  Odd characters have a tendency to mess
5326   ;; up nicely formatted displays - we make all possible glyphs
5327   ;; display only a single character.
5328
5329   ;; We start from the standard display table, if any.
5330   (setq gnus-summary-display-table 
5331         (or (copy-sequence standard-display-table)
5332             (make-display-table)))
5333   ;; Nix out all the control chars...
5334   (let ((i 32))
5335     (while (>= (setq i (1- i)) 0)
5336       (aset gnus-summary-display-table i [??])))
5337   ;; ... but not newline, of course.
5338   (aset gnus-summary-display-table ?\n nil)
5339   ;; We nix out any glyphs over 126 that are not set already.  
5340   (let ((i 256))
5341     (while (>= (setq i (1- i)) 127)
5342       ;; Only modify if the entry is nil.
5343       (or (aref gnus-summary-display-table i) 
5344           (aset gnus-summary-display-table i [??])))))
5345
5346 (defun gnus-summary-clear-local-variables ()
5347   (let ((locals gnus-summary-local-variables))
5348     (while locals
5349       (if (consp (car locals))
5350           (and (vectorp (car (car locals)))
5351                (set (car (car locals)) nil))
5352         (and (vectorp (car locals))
5353              (set (car locals) nil)))
5354       (setq locals (cdr locals)))))
5355
5356 (defun gnus-mouse-pick-article (e)
5357   (interactive "e")
5358   (mouse-set-point e)
5359   (gnus-summary-next-page nil t))
5360
5361 (defun gnus-summary-setup-buffer (group)
5362   "Initialize summary buffer."
5363   (let ((buffer (concat "*Summary " group "*")))
5364     (if (get-buffer buffer)
5365         (progn
5366           (set-buffer buffer)
5367           (not gnus-newsgroup-threads))
5368       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
5369       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
5370       (gnus-add-current-to-buffer-list)
5371       (gnus-summary-mode)
5372       (and gnus-carpal (gnus-carpal-setup-buffer 'summary))
5373       (setq gnus-newsgroup-name group)
5374       t)))
5375
5376 (defun gnus-set-global-variables ()
5377   ;; Set the global equivalents of the summary buffer-local variables
5378   ;; to the latest values they had. These reflect the summary buffer
5379   ;; that was in action when the last article was fetched.
5380   (if (eq major-mode 'gnus-summary-mode) 
5381       (progn
5382         (setq gnus-summary-buffer (current-buffer))
5383         (let ((name gnus-newsgroup-name)
5384               (marked gnus-newsgroup-marked)
5385               (unread gnus-newsgroup-unreads)
5386               (headers gnus-current-headers)
5387               (score-file gnus-current-score-file))
5388           (save-excursion
5389             (set-buffer gnus-group-buffer)
5390             (setq gnus-newsgroup-name name)
5391             (setq gnus-newsgroup-marked marked)
5392             (setq gnus-newsgroup-unreads unread)
5393             (setq gnus-current-headers headers)
5394             (setq gnus-current-score-file score-file))))))
5395
5396 (defun gnus-summary-insert-dummy-line (sformat subject number)
5397   (if (not sformat) 
5398       (setq sformat gnus-summary-dummy-line-format-spec))
5399   (let (b)
5400     (beginning-of-line)
5401     (setq b (point))
5402     (insert (eval sformat))
5403     (add-text-properties
5404      b (1+ b)
5405      (list 'gnus-number number 
5406            'gnus-mark gnus-dummy-mark
5407            'gnus-level 0))))
5408
5409 (defvar gnus-thread-indent-array nil)
5410 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
5411 (defun gnus-make-thread-indent-array ()
5412   (let ((n 200))
5413     (if (and gnus-thread-indent-array
5414              (= gnus-thread-indent-level gnus-thread-indent-array-level))
5415         nil
5416       (setq gnus-thread-indent-array (make-vector 201 "")
5417             gnus-thread-indent-array-level gnus-thread-indent-level)
5418       (while (>= n 0)
5419         (aset gnus-thread-indent-array n
5420               (make-string (* n gnus-thread-indent-level) ? ))
5421         (setq n (1- n))))))
5422
5423 (defun gnus-summary-insert-line 
5424   (sformat header level current unread replied expirable subject-or-nil
5425            &optional dummy score)
5426   (or sformat (setq sformat gnus-summary-line-format-spec))
5427   (let* ((indentation (aref gnus-thread-indent-array level))
5428          (lines (header-lines header))
5429          (score (or score gnus-summary-default-score 0))
5430          (score-char (if (or (null gnus-summary-default-score)
5431                              (= score gnus-summary-default-score)) ? 
5432                        (if (< score gnus-summary-default-score) 
5433                            gnus-score-below-mark gnus-score-over-mark)))
5434          (replied (if replied gnus-replied-mark ? ))
5435          (from (header-from header))
5436          (name-address (funcall gnus-extract-address-components from))
5437          (address (car (cdr name-address)))
5438          (name (or (car name-address) (car (cdr name-address))))
5439          (subject (header-subject header))
5440          (number (header-number header))
5441          (opening-bracket (if dummy ?\< ?\[))
5442          (closing-bracket (if dummy ?\> ?\]))
5443          (buffer-read-only nil)
5444          (b (progn (beginning-of-line) (point))))
5445     (or (numberp lines) (setq lines 0))
5446     (insert (eval sformat))
5447     (add-text-properties
5448      b (1+ b) (list 'gnus-number number 
5449                     'gnus-mark (or unread gnus-unread-mark)
5450                     'gnus-level level))))
5451
5452 (defun gnus-summary-update-line (&optional dont-update)
5453   ;; Update summary line after change.
5454   (or (not gnus-summary-default-score)
5455       gnus-summary-inhibit-highlight
5456       (let ((gnus-summary-inhibit-highlight t)
5457             (article (gnus-summary-article-number)))
5458         (progn
5459           (or dont-update
5460               (if (and gnus-summary-mark-below
5461                        (< (gnus-summary-article-score)
5462                           gnus-summary-mark-below))
5463                   (and (not (memq article gnus-newsgroup-marked))
5464                        (not (memq article gnus-newsgroup-dormant))
5465                        (memq article gnus-newsgroup-unreads)
5466                        (gnus-summary-mark-article nil gnus-low-score-mark))
5467                 (and (eq (gnus-summary-article-mark) gnus-low-score-mark)
5468                      (gnus-summary-mark-article nil gnus-unread-mark))))
5469           (and gnus-visual
5470                (run-hooks 'gnus-summary-update-hook))))))
5471
5472 (defun gnus-summary-update-lines (&optional beg end)
5473   ;; Rehighlight summary buffer according to `gnus-summary-highlight'.
5474   (let ((beg (or beg (point-min)))
5475         (end (or end (point-max))))
5476     (save-excursion
5477       (set-buffer gnus-summary-buffer)
5478       (goto-char beg)
5479       (while (and (not (eobp)) (< (point) end))
5480         (gnus-summary-update-line)
5481         (forward-line 1)))))
5482
5483 (defun gnus-summary-number-of-articles-in-thread (thread &optional char)
5484   ;; Sum up all elements (and sub-elements) in a list.
5485   (let ((number 
5486          (if (listp thread) 
5487              (apply 
5488               '+ (mapcar 'gnus-summary-number-of-articles-in-thread thread))
5489            1)))
5490     (if char 
5491         (if (> number 1) gnus-not-empty-thread-mark
5492           gnus-empty-thread-mark)
5493       number)))
5494
5495 (defun gnus-summary-read-group (group &optional show-all no-article kill-buffer)
5496   "Start reading news in newsgroup GROUP.
5497 If SHOW-ALL is non-nil, already read articles are also listed.
5498 If NO-ARTICLE is non-nil, no article is selected initially."
5499   (gnus-message 5 "Retrieving newsgroup: %s..." group)
5500   (let* ((new-group (gnus-summary-setup-buffer group))
5501          (did-select (and new-group (gnus-select-newsgroup group show-all)))
5502          (method (car (gnus-find-method-for-group group))))
5503     (cond 
5504      ((not new-group)
5505       (gnus-set-global-variables)
5506       (gnus-kill-buffer kill-buffer)
5507       (gnus-configure-windows 'summary)
5508       (gnus-set-mode-line 'summary)
5509       (gnus-summary-position-cursor)
5510       (message "")
5511       t)
5512      ((null did-select) 
5513       (and (eq major-mode 'gnus-summary-mode)
5514            (not (equal (current-buffer) kill-buffer))
5515            (progn
5516              (kill-buffer (current-buffer))
5517              (set-buffer gnus-group-buffer)
5518              (gnus-group-next-unread-group 1)))
5519       (message "Can't select group")
5520       nil)
5521      ((eq did-select 'quit)
5522       (and (eq major-mode 'gnus-summary-mode)
5523            (not (equal (current-buffer) kill-buffer))
5524            (kill-buffer (current-buffer)))
5525       (gnus-kill-buffer kill-buffer)
5526       (gnus-configure-windows 'group)
5527       (gnus-group-next-unread-group 1)
5528       (signal 'quit nil))
5529      (t
5530       (gnus-set-global-variables)
5531       ;; Save the active value in effect when the group was entered.
5532       (setq gnus-newsgroup-active 
5533             (gnus-copy-sequence
5534              (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
5535       ;; You can change the subjects in this hook.
5536       (run-hooks 'gnus-select-group-hook)
5537       ;; Do score processing.
5538       (and gnus-use-scoring (gnus-possibly-score-headers))
5539       ;; Update the format specifiers.
5540       (gnus-update-format-specifications)
5541       ;; Generate the summary buffer.
5542       (gnus-summary-prepare)
5543       (if (zerop (buffer-size))
5544           (cond (gnus-newsgroup-dormant
5545                  (gnus-summary-show-all-dormant))
5546                 ((and gnus-newsgroup-scored show-all)
5547                  (gnus-summary-show-all-expunged))))
5548       ;; Function `gnus-apply-kill-file' must be called in this hook.
5549       (run-hooks 'gnus-apply-kill-hook)
5550       (if (zerop (buffer-size))
5551           (progn
5552             ;; This newsgroup is empty.
5553             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
5554             (gnus-message 6 "No unread news")
5555             (gnus-kill-buffer kill-buffer)
5556             nil)
5557         ;;(save-excursion
5558         ;;  (if kill-buffer
5559         ;;      (let ((gnus-summary-buffer kill-buffer))
5560         ;;      (gnus-configure-windows 'group))))
5561         ;; Hide conversation thread subtrees.  We cannot do this in
5562         ;; gnus-summary-prepare-hook since kill processing may not
5563         ;; work with hidden articles.
5564         (and gnus-show-threads
5565              gnus-thread-hide-subtree
5566              (gnus-summary-hide-all-threads))
5567         ;; Show first unread article if requested.
5568         (goto-char (point-min))
5569         (if (and (not no-article)
5570                  gnus-auto-select-first
5571                  (gnus-summary-first-unread-article))
5572             ()
5573           (gnus-configure-windows 'summary))
5574         (gnus-set-mode-line 'summary)
5575         (gnus-summary-position-cursor)
5576         ;; If in async mode, we send some info to the backend.
5577         (and gnus-newsgroup-async
5578              (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
5579              (gnus-request-asynchronous 
5580               gnus-newsgroup-name
5581               (if (and gnus-asynchronous-article-function
5582                        (fboundp gnus-asynchronous-article-function))
5583                   (funcall gnus-asynchronous-article-function
5584                            gnus-newsgroup-threads)
5585                 gnus-newsgroup-threads)))
5586         (gnus-kill-buffer kill-buffer)
5587         (if (not (get-buffer-window gnus-group-buffer))
5588             ()
5589           ;; gotta use windows, because recenter does wierd stuff if
5590           ;; the current buffer ain't the displayed window.
5591           (let ((owin (selected-window))) 
5592             (select-window (get-buffer-window gnus-group-buffer))
5593             (and (gnus-group-goto-group group)
5594                  (recenter))
5595             (select-window owin))))
5596       t))))
5597
5598 (defun gnus-summary-prepare ()
5599   ;; Generate the summary buffer.
5600   (let ((buffer-read-only nil))
5601     (erase-buffer)
5602     (gnus-summary-prepare-threads 
5603      (if gnus-show-threads
5604          (gnus-gather-threads 
5605           (gnus-sort-threads 
5606            (if (and gnus-summary-expunge-below
5607                     (not gnus-fetch-old-headers))
5608                (gnus-make-threads-and-expunge)
5609              (gnus-make-threads))))
5610        gnus-newsgroup-headers)
5611      0 nil nil t)
5612     ;; Erase header retrieval message.
5613     (gnus-summary-update-lines)
5614     (message "")
5615     ;; Remove the final newline.
5616     ;;(goto-char (point-max))
5617     ;;(delete-char -1)
5618     ;; Call hooks for modifying summary buffer.
5619     ;; Suggested by sven@tde.LTH.Se (Sven Mattisson).
5620     (goto-char (point-min))
5621     (run-hooks 'gnus-summary-prepare-hook)))
5622
5623 (defun gnus-subject-equal (s1 s2)
5624   (cond 
5625    ((numberp gnus-summary-gather-subject-limit)
5626     (string= (if (> (length s1) gnus-summary-gather-subject-limit)
5627                  (substring s1 0 gnus-summary-gather-subject-limit)
5628                s1)
5629              (if (> (length s2) gnus-summary-gather-subject-limit)
5630                  (substring s2 0 gnus-summary-gather-subject-limit)
5631                s2)))
5632    ((eq 'fuzzy gnus-summary-gather-subject-limit)
5633     (string= (gnus-simplify-subject-fuzzy s1)
5634              (gnus-simplify-subject-fuzzy s2)))
5635    (t
5636     (string= s1 s2))))
5637
5638 (defun gnus-gather-threads (threads)
5639   "Gather threads that have lost their roots."
5640   (if (not gnus-summary-make-false-root)
5641       threads 
5642     (let ((hashtb (gnus-make-hashtable 1023))
5643           (prev threads)
5644           (result threads)
5645           thread subject hthread whole-subject)
5646       (while threads
5647         (setq whole-subject 
5648               (setq subject (header-subject (car (car threads)))))
5649         (if gnus-summary-gather-subject-limit
5650             (or (and (numberp gnus-summary-gather-subject-limit)
5651                      (> (length subject) gnus-summary-gather-subject-limit)
5652                      (setq subject
5653                            (substring subject 0 
5654                                       gnus-summary-gather-subject-limit)))
5655                 (and (eq 'fuzzy gnus-summary-gather-subject-limit)
5656                      (setq subject (gnus-simplify-subject-fuzzy subject))))
5657           (setq subject (gnus-simplify-subject-re subject)))
5658         (if (setq hthread 
5659                   (gnus-gethash subject hashtb))
5660             (progn
5661               (or (stringp (car (car hthread)))
5662                   (setcar hthread (list whole-subject (car hthread))))
5663               (setcdr (car hthread) (nconc (cdr (car hthread)) 
5664                                            (list (car threads))))
5665               (setcdr prev (cdr threads))
5666               (setq threads prev))
5667           (gnus-sethash subject threads hashtb))
5668         (setq prev threads)
5669         (setq threads (cdr threads)))
5670       result)))
5671
5672 (defun gnus-make-threads ()
5673   ;; This function takes the dependencies already made by 
5674   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5675   ;; through the dependecies in the hash table and finds all the
5676   ;; roots. Roots do not refer back to any valid articles.
5677   (let (roots)
5678     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5679          (gnus-build-old-threads))
5680     (mapatoms
5681      (lambda (refs)
5682        (if (not (car (symbol-value refs)))
5683            (setq roots (append (cdr (symbol-value refs)) roots))
5684          ;; Ok, these refer back to valid articles, but if
5685          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5686          ;; the root has the same subject as its children. The children
5687          ;; that do not are made into roots and removed from the list
5688          ;; of children. 
5689          (or gnus-thread-ignore-subject
5690              (let* ((prev (symbol-value refs))
5691                     (subject (gnus-simplify-subject-re 
5692                               (header-subject (car prev))))
5693                     (headers (cdr prev)))
5694                (while headers
5695                  (if (not (string= subject
5696                                    (gnus-simplify-subject-re 
5697                                     (header-subject (car headers)))))
5698                      (progn
5699                        (setq roots (cons (car headers) roots))
5700                        (setcdr prev (cdr headers)))
5701                    (setq prev headers))
5702                  (setq headers (cdr headers)))))))
5703      gnus-newsgroup-dependencies)
5704     
5705     (mapcar 'gnus-trim-thread
5706             (apply 'append
5707                    (mapcar 'gnus-cut-thread
5708                            (mapcar 'gnus-make-sub-thread roots))))))
5709   
5710 (defun gnus-make-threads-and-expunge ()
5711   ;; This function takes the dependencies already made by 
5712   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5713   ;; through the dependecies in the hash table and finds all the
5714   ;; roots. Roots do not refer back to any valid articles.
5715   (let ((default (or gnus-summary-default-score 0))
5716         (below gnus-summary-expunge-below)
5717         roots article)
5718     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5719          (gnus-build-old-threads))
5720     (mapatoms
5721      (lambda (refs)
5722        (if (not (car (symbol-value refs)))
5723            ;; These articles do not refer back to any other articles -
5724            ;; they are roots.
5725            (let ((headers (cdr (symbol-value refs))))
5726              ;; We weed out the low-scored articles.
5727              (while headers
5728                (if (not (< (or (cdr (assq (header-number (car headers))
5729                                           gnus-newsgroup-scored)) default)
5730                            below))
5731                    ;; It is over.
5732                    (setq roots (cons (car headers) roots))
5733                  ;; It is below, so we mark it as read.
5734                  (setq gnus-newsgroup-unreads
5735                        (delq (header-number (car headers))
5736                              gnus-newsgroup-unreads)))
5737                (setq headers (cdr headers))))
5738          ;; Ok, these refer back to valid articles, but if
5739          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5740          ;; the root has the same subject as its children. The children
5741          ;; that do not are made into roots and removed from the list
5742          ;; of children. 
5743          (or gnus-thread-ignore-subject
5744              (let* ((prev (symbol-value refs))
5745                     (subject (gnus-simplify-subject-re 
5746                               (header-subject (car prev))))
5747                     (headers (cdr prev)))
5748                (while headers
5749                  (if (not (string= subject
5750                                    (gnus-simplify-subject-re 
5751                                     (header-subject (car headers)))))
5752                      (progn
5753                        (if (not (< (or (cdr (assq (header-number (car headers))
5754                                                   gnus-newsgroup-scored))
5755                                        default) below))
5756                            (setq roots (cons (car headers) roots))
5757                          (setq gnus-newsgroup-unreads
5758                                (delq (header-number (car headers))
5759                                      gnus-newsgroup-unreads)))
5760                        (setcdr prev (cdr headers)))
5761                    (setq prev headers))
5762                  (setq headers (cdr headers)))))
5763          ;; If this article is expunged, some of the children might be
5764          ;; roots.  
5765          (if (< (or (cdr (assq (header-number (car (symbol-value refs)))
5766                                gnus-newsgroup-scored)) default)
5767                 below)
5768              (let* ((prev (symbol-value refs))
5769                     (headers (cdr prev)))
5770                (while headers
5771                  (setq article (header-number (car headers)))
5772                  (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
5773                                  default) below))
5774                      (progn (setq roots (cons (car headers) roots))
5775                             (setq prev headers))
5776                    (setq gnus-newsgroup-unreads 
5777                          (delq article gnus-newsgroup-unreads))
5778                    (setcdr prev (cdr headers)))
5779                  (setq headers (cdr headers))))
5780            ;; It was not expunged, but we look at expunged children.
5781            (let* ((prev (symbol-value refs))
5782                   (headers (cdr prev))
5783                   article id)
5784              (while headers
5785                (setq article (header-number (car headers)))
5786                (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
5787                                default) below))
5788                    (setq prev headers)
5789                  (setq gnus-newsgroup-unreads 
5790                        (delq article gnus-newsgroup-unreads))
5791                  (setcdr prev (cdr headers)))
5792                (setq headers (cdr headers)))))))
5793      gnus-newsgroup-dependencies)
5794
5795     (mapcar 'gnus-trim-thread
5796             (apply 'append
5797                    (mapcar 'gnus-cut-thread
5798                            (mapcar 'gnus-make-sub-thread roots))))))
5799   
5800 (defun gnus-cut-thread (thread)
5801   ;; Remove leaf dormant or ancient articles from THREAD.
5802   (let ((head (car thread))
5803         (tail (apply 'append (mapcar 'gnus-cut-thread (cdr thread)))))
5804     (if (and (null tail)
5805              (let ((number (header-number head)))
5806                (or (memq number gnus-newsgroup-ancient)
5807                    (memq number gnus-newsgroup-dormant)
5808                    (and gnus-summary-expunge-below
5809                         (eq gnus-fetch-old-headers 'some)
5810                         (< (or (cdr (assq number gnus-newsgroup-scored))
5811                                gnus-summary-default-score 0)
5812                            gnus-summary-expunge-below)
5813                         (progn
5814                           (setq gnus-newsgroup-unreads
5815                                 (delq number gnus-newsgroup-unreads))
5816                           t)))))
5817         nil
5818       (list (cons head tail)))))
5819
5820 (defun gnus-trim-thread (thread)
5821   ;; Remove root ancient articles with only one child from THREAD.
5822   (if (and (eq gnus-fetch-old-headers 'some)
5823            (memq (header-number (car thread)) gnus-newsgroup-ancient)
5824            (= (length thread) 2))
5825       (gnus-trim-thread (nth 1 thread))
5826     thread))
5827
5828 (defun gnus-make-sub-thread (root)
5829   ;; This function makes a sub-tree for a node in the tree.
5830   (let ((children (reverse (cdr (gnus-gethash (downcase (header-id root))
5831                                               gnus-newsgroup-dependencies)))))
5832     (cons root (mapcar 'gnus-make-sub-thread children))))
5833
5834 (defun gnus-build-old-threads ()
5835   ;; Look at all the articles that refer back to old articles, and
5836   ;; fetch the headers for the articles that aren't there. This will
5837   ;; build complete threads - if the roots haven't been expired by the
5838   ;; server, that is.
5839   (let (id heads)
5840     (mapatoms
5841      (lambda (refs)
5842        (if (not (car (symbol-value refs)))
5843            (progn
5844              (setq heads (cdr (symbol-value refs)))
5845              (while heads
5846                (if (not (memq (header-number (car heads))
5847                               gnus-newsgroup-dormant))
5848                    (progn
5849                      (setq id (symbol-name refs))
5850                      (while (and (setq id (gnus-build-get-header id))
5851                                  (not (car (gnus-gethash 
5852                                             id gnus-newsgroup-dependencies)))))
5853                      (setq heads nil))
5854                  (setq heads (cdr heads)))))))
5855      gnus-newsgroup-dependencies)))
5856
5857 (defun gnus-build-get-header (id)
5858   ;; Look through the buffer of NOV lines and find the header to
5859   ;; ID. Enter this line into the dependencies hash table, and return
5860   ;; the id of the parent article (if any).
5861   (let ((deps gnus-newsgroup-dependencies)
5862         found header)
5863     (prog1
5864         (save-excursion
5865           (set-buffer nntp-server-buffer)
5866           (goto-char (point-min))
5867           (while (and (not found) (search-forward id nil t))
5868             (beginning-of-line)
5869             (setq found (looking-at 
5870                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
5871                                  (regexp-quote id))))
5872             (or found (beginning-of-line 2)))
5873           (if found
5874               (let (ref)
5875                 (beginning-of-line)
5876                 (and
5877                  (setq header (gnus-nov-parse-line 
5878                                (read (current-buffer)) deps))
5879                  (setq ref (header-references header))
5880                  (string-match "\\(<[^>]+>\\) *$" ref)
5881                  (substring ref (match-beginning 1) (match-end 1))))))
5882       (and header
5883            (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers)
5884                  gnus-newsgroup-ancient (cons (header-number header)
5885                                               gnus-newsgroup-ancient))))))
5886
5887 ;; Re-build the thread containing ID.
5888 (defun gnus-rebuild-thread (id)
5889   (let ((dep gnus-newsgroup-dependencies)
5890         (buffer-read-only nil)
5891         parent headers refs thread art)
5892     (while (and id (setq headers
5893                          (car (setq art (gnus-gethash (downcase id) dep)))))
5894       (setq parent art)
5895       (setq id (and (setq refs (header-references headers))
5896                     (string-match "\\(<[^>]+>\\) *$" refs)
5897                     (substring refs (match-beginning 1) (match-end 1)))))
5898     (setq thread (gnus-make-sub-thread (car parent)))
5899     (gnus-rebuild-remove-articles thread)
5900     (let ((beg (point)))
5901       (gnus-summary-prepare-threads (list thread) 0)
5902       (gnus-summary-update-lines beg (point)))))
5903
5904 ;; Delete all lines in the summary buffer that correspond to articles
5905 ;; in this thread.
5906 (defun gnus-rebuild-remove-articles (thread)
5907   (and (gnus-summary-goto-subject (header-number (car thread)))
5908        (gnus-delete-line))
5909   (mapcar (lambda (th) (gnus-rebuild-remove-articles th)) (cdr thread)))
5910
5911 (defun gnus-sort-threads (threads)
5912   ;; Sort threads as specified in `gnus-thread-sort-functions'.
5913   (let ((fun gnus-thread-sort-functions))
5914     (while fun
5915       (setq threads (sort threads (car fun))
5916             fun (cdr fun))))
5917   threads)
5918
5919 (defun gnus-thread-header (thread)
5920   ;; Return header of first article in THREAD.
5921   (if (consp thread)
5922       (if (stringp (car thread))
5923           (car (car (cdr thread)))
5924         (car thread))
5925     thread))
5926
5927 (defun gnus-thread-sort-by-number (h1 h2)
5928   "Sort threads by root article number."
5929   (let ((h1 (gnus-thread-header h1))
5930         (h2 (gnus-thread-header h2)))
5931     (< (header-number h1) (header-number h2))))
5932
5933 (defun gnus-thread-sort-by-author (h1 h2)
5934   "Sort threads by root author."
5935   (let ((h1 (gnus-thread-header h1))
5936         (h2 (gnus-thread-header h2)))
5937     (string-lessp
5938      (let ((extract (funcall 
5939                      gnus-extract-address-components (header-from h1))))
5940        (or (car extract) (cdr extract)))
5941      (let ((extract (funcall
5942                      gnus-extract-address-components (header-from h2))))
5943        (or (car extract) (cdr extract))))))
5944
5945 (defun gnus-thread-sort-by-subject (h1 h2)
5946   "Sort threads by root subject."
5947   (let ((h1 (gnus-thread-header h1))
5948         (h2 (gnus-thread-header h2)))
5949     (string-lessp
5950      (downcase (gnus-simplify-subject (header-subject h1)))
5951      (downcase (gnus-simplify-subject (header-subject h2))))))
5952
5953 (defun gnus-thread-sort-by-date (h1 h2)
5954   "Sort threads by root article date."
5955   (let ((h1 (gnus-thread-header h1))
5956         (h2 (gnus-thread-header h2)))
5957     (string-lessp
5958      (gnus-sortable-date (header-date h1))
5959      (gnus-sortable-date (header-date h2)))))
5960
5961 (defun gnus-thread-sort-by-score (h1 h2)
5962   "Sort threads by root article score.
5963 Unscored articles will be counted as havin a score of zero."
5964   (let ((h1 (gnus-thread-header h1))
5965         (h2 (gnus-thread-header h2)))
5966     (let ((s1 (assq (header-number h1) gnus-newsgroup-scored))
5967           (s2 (assq (header-number h2) gnus-newsgroup-scored)))
5968       (> (or (cdr s1) gnus-summary-default-score 0)
5969          (or (cdr s2) gnus-summary-default-score 0)))))
5970
5971 (defun gnus-thread-sort-by-total-score (h1 h2)
5972   "Sort threads by the sum of all scores in the thread.
5973 Unscored articles will be counted as havin a score of zero."
5974   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
5975
5976 (defun gnus-thread-total-score (thread)
5977   ;;  This function find the total score of THREAD.
5978   (if (consp thread)
5979       (if (stringp (car thread))
5980           (apply gnus-thread-score-function 0
5981                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
5982         (gnus-thread-total-score-1 thread))
5983     (gnus-thread-total-score-1 (list thread))))
5984
5985 (defun gnus-thread-total-score-1 (root)
5986   ;; This function find the total score of the thread below ROOT.
5987   (setq root (car root))
5988   (apply gnus-thread-score-function
5989          (or (cdr (assq (header-number root) gnus-newsgroup-scored))
5990              gnus-summary-default-score 0)
5991          (mapcar 'gnus-thread-total-score
5992                  (cdr (gnus-gethash (downcase (header-id root))
5993                                     gnus-newsgroup-dependencies)))))
5994
5995 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5996 (defvar gnus-tmp-prev-subject "")
5997 (defvar gnus-tmp-adopt-thread nil)
5998
5999 ;; Basic ideas by Paul Dworkin <paul@media-lab.media.mit.edu>.
6000 (defun gnus-summary-prepare-threads 
6001   (threads level &optional not-child no-subject cull)
6002   "Prepare summary buffer from THREADS and indentation LEVEL.  
6003 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
6004 or a straight list of headers."
6005   (let (thread header number subject clevel)
6006     (while threads
6007       (setq thread (car threads)
6008             threads (cdr threads))
6009       ;; If `thread' is a cons, hierarchical threads are used.  If not,
6010       ;; `thread' is the header.
6011       (if (consp thread)
6012           (setq header (car thread))
6013         (setq header thread)
6014         (and cull
6015              (or (memq (setq number (header-number header))
6016                        gnus-newsgroup-dormant)
6017                  (and gnus-summary-expunge-below
6018                       (< (or (cdr (assq number gnus-newsgroup-scored))
6019                              gnus-summary-default-score 0)
6020                          gnus-summary-expunge-below)))
6021              (progn
6022                (setq header nil)
6023                (setq gnus-newsgroup-unreads 
6024                      (delq number gnus-newsgroup-unreads)))))
6025       (cond 
6026        ((stringp header)
6027         ;; The header is a dummy root.
6028         (cond ((eq gnus-summary-make-false-root 'adopt)
6029                ;; We let the first article adopt the rest.
6030                (let ((gnus-tmp-adopt-thread (list (cdr thread))))
6031                  (gnus-summary-prepare-threads (list (car (cdr thread))) 0))
6032                (setq thread (cdr (cdr thread)))
6033                (while thread
6034                  (gnus-summary-prepare-threads (list (car thread)) 1 t)
6035                  (setq thread (cdr thread))))
6036               ((eq gnus-summary-make-false-root 'dummy)
6037                ;; We output a dummy root.
6038                (gnus-summary-insert-dummy-line 
6039                 nil header (header-number (car (car (cdr thread)))))
6040                (setq clevel 1))
6041               ((eq gnus-summary-make-false-root 'empty)
6042                ;; We print the articles with empty subject fields. 
6043                (let ((gnus-tmp-adopt-thread (list (cdr thread))))
6044                  (gnus-summary-prepare-threads (list (car (cdr thread))) 0))
6045                (setq thread (cdr (cdr thread)))
6046                (while thread
6047                  (gnus-summary-prepare-threads 
6048                   (list (car thread)) 0 nil
6049                   (not (and (eq gnus-summary-gather-subject-limit 'fuzzy)
6050                             (not (string=  
6051                                   (gnus-simplify-subject-re 
6052                                    (header-subject (car (car thread))))
6053                                   (gnus-simplify-subject-re header))))))
6054                  (setq thread (cdr thread))))
6055               (t
6056                ;; We do not make a root for the gathered
6057                ;; sub-threads at all.  
6058                (setq clevel 0)))
6059         ;; Print the sub-threads.
6060         (and (consp thread) (cdr thread)
6061              (gnus-summary-prepare-threads (cdr thread) clevel)))
6062        ;; The header is a real article.
6063        (header
6064         (setq number (header-number header)
6065               subject (header-subject header))
6066         (and gnus-newsgroup-async
6067              (setq gnus-newsgroup-threads
6068                    (cons (cons (header-number header)
6069                                (header-lines header)) gnus-newsgroup-threads)))
6070         (gnus-summary-insert-line
6071          nil header level nil 
6072          (cond ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6073                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6074                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6075                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6076                (t gnus-ancient-mark))
6077          (memq number gnus-newsgroup-replied)
6078          (memq number gnus-newsgroup-expirable)
6079          (if no-subject 
6080              gnus-summary-same-subject
6081            (if (or (zerop level)
6082                    (and gnus-thread-ignore-subject
6083                         (not (string= 
6084                               (gnus-simplify-subject-re gnus-tmp-prev-subject)
6085                               (gnus-simplify-subject-re subject)))))
6086                subject
6087              gnus-summary-same-subject))
6088          not-child
6089          (cdr (assq number gnus-newsgroup-scored)))
6090         (setq gnus-tmp-prev-subject subject)
6091         ;; Recursively print subthreads.
6092         (and (consp thread) (cdr thread)
6093              (gnus-summary-prepare-threads (cdr thread) (1+ level))))))))
6094
6095 (defun gnus-select-newsgroup (group &optional read-all)
6096   "Select newsgroup GROUP.
6097 If READ-ALL is non-nil, all articles in the group are selected."
6098   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6099          (info (nth 2 entry))
6100          articles header-marks)
6101     (gnus-check-news-server
6102      (setq gnus-current-select-method (gnus-find-method-for-group group)))
6103
6104     (or (gnus-server-opened gnus-current-select-method)
6105         (gnus-open-server gnus-current-select-method)
6106         (error "Couldn't open server"))
6107     
6108     (or (and (eq (car entry) t)
6109              (gnus-activate-newsgroup (car info)))
6110         (gnus-request-group group t)
6111         (progn
6112           (kill-buffer (current-buffer))
6113           (error "Couldn't request group %s: %s" 
6114                  group (gnus-status-message group))))
6115
6116     (setq gnus-newsgroup-name group)
6117     (setq gnus-newsgroup-unselected nil)
6118     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
6119
6120     (and gnus-asynchronous
6121          (gnus-check-backend-function 
6122           'request-asynchronous gnus-newsgroup-name)
6123          (setq gnus-newsgroup-async
6124                (gnus-request-asynchronous gnus-newsgroup-name)))
6125
6126     (setq articles (gnus-articles-to-read group read-all))
6127
6128     (cond 
6129      ((null articles) 
6130       (gnus-message 3 "Couldn't select newsgroup")
6131       'quit)
6132      ((eq articles 0) nil)
6133      (t
6134       ;; Init the dependencies hash table.
6135       (setq gnus-newsgroup-dependencies 
6136             (gnus-make-hashtable (length articles)))
6137       ;; Retrieve the headers and read them in.
6138       (setq gnus-newsgroup-headers 
6139             (if (eq 'nov (setq gnus-headers-retrieved-by
6140                                (gnus-retrieve-headers 
6141                                 (if gnus-fetch-old-headers 
6142                                     (cons 1 articles) articles) 
6143                                 gnus-newsgroup-name)))
6144                 (progn
6145                   (gnus-get-newsgroup-headers-xover articles))
6146               (gnus-get-newsgroup-headers)))
6147       ;; If we were to fetch old headers, but the backend didn't
6148       ;; support XOVER, then it is possible we fetched one article
6149       ;; that we shouldn't have. If that's the case, we pop it off the
6150       ;; list of headers.
6151       (and (not (eq gnus-headers-retrieved-by 'nov))
6152            gnus-fetch-old-headers
6153            gnus-newsgroup-headers
6154            (/= (header-number (car gnus-newsgroup-headers)) (car articles))
6155            (setq gnus-newsgroup-headers (cdr gnus-newsgroup-headers)))
6156       ;; Remove cancelled articles from the list of unread articles.
6157       (setq gnus-newsgroup-unreads
6158             (gnus-set-sorted-intersection 
6159              gnus-newsgroup-unreads
6160              (mapcar (lambda (headers) (header-number headers))
6161                      gnus-newsgroup-headers)))
6162       ;; Adjust and set lists of article marks.
6163       (and info
6164            (let (marked)
6165              (gnus-adjust-marked-articles info)
6166              (setq gnus-newsgroup-marked 
6167                    (cdr (assq 'tick (setq marked (nth 3 info)))))
6168              (setq gnus-newsgroup-replied (cdr (assq 'reply marked)))
6169              (setq gnus-newsgroup-expirable (cdr (assq 'expire marked)))
6170              (setq gnus-newsgroup-killed (cdr (assq 'killed marked)))
6171              (setq gnus-newsgroup-bookmarks (cdr (assq 'bookmark marked)))
6172              (setq gnus-newsgroup-dormant (cdr (assq 'dormant marked)))
6173              (setq gnus-newsgroup-scored (cdr (assq 'score marked)))
6174              (setq gnus-newsgroup-processable nil)))
6175       ;; Check whether auto-expire is to be done in this group.
6176       (setq gnus-newsgroup-auto-expire
6177             (or (and (stringp gnus-auto-expirable-newsgroups)
6178                      (string-match gnus-auto-expirable-newsgroups group))
6179                 (memq 'auto-expire (nth 5 info))))
6180       ;; First and last article in this newsgroup.
6181       (and gnus-newsgroup-headers
6182            (setq gnus-newsgroup-begin 
6183                  (header-number (car gnus-newsgroup-headers)))
6184            (setq gnus-newsgroup-end
6185                  (header-number (gnus-last-element gnus-newsgroup-headers))))
6186       (setq gnus-reffed-article-number -1)
6187       ;; GROUP is successfully selected.
6188       (or gnus-newsgroup-headers t)))))
6189
6190 (defun gnus-articles-to-read (group read-all)
6191   ;; Find out what articles the user wants to read.
6192   (let* ((articles
6193           ;; Select all articles if `read-all' is non-nil, or if all the
6194           ;; unread articles are dormant articles.
6195           (if (or (and read-all (not (numberp read-all)))
6196                   (= (length gnus-newsgroup-unreads) 
6197                      (length gnus-newsgroup-dormant)))
6198               (gnus-uncompress-range 
6199                (gnus-gethash group gnus-active-hashtb))
6200             gnus-newsgroup-unreads))
6201          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
6202          (scored (length scored-list))
6203          (number (length articles))
6204          (marked (+ (length gnus-newsgroup-marked)
6205                     (length gnus-newsgroup-dormant)))
6206          (select
6207           (cond 
6208            ((numberp read-all)
6209             read-all)
6210            (t
6211             (condition-case ()
6212                 (cond ((and (or (<= scored marked)
6213                                 (= scored number))
6214                             (numberp gnus-large-newsgroup)
6215                             (> number gnus-large-newsgroup))
6216                        (let ((input
6217                               (read-string
6218                                (format
6219                                 "How many articles from %s (default %d): "
6220                                 gnus-newsgroup-name number))))
6221                          (if (string-equal input "")
6222                              number input)))
6223                       ((and (> scored marked) (< scored number))
6224                        (let ((input
6225                               (read-string
6226                                (format 
6227                                 "%s %s (%d scored, %d total): "
6228                                 "How many articles from"
6229                                 group scored number))))
6230                          (if (string-equal input "")
6231                              number input)))
6232                       (t number))
6233               (quit nil)))))
6234          total-articles)
6235     (setq select (if (stringp select) (string-to-number select) select))
6236     (if (or (null select) (zerop select))
6237         select
6238       (if (and (not (zerop scored)) (<= (abs select) scored))
6239           (progn
6240             (setq articles (sort scored-list '<))
6241             (setq number (length articles)))
6242         (setq articles (copy-sequence articles)))
6243
6244       (setq total-articles articles)
6245       
6246       (if (< (abs select) number)
6247           (if (< select 0) 
6248               ;; Select the N oldest articles.
6249               (setcdr (nthcdr (1- (abs select)) articles) nil)
6250             ;; Select the N most recent articles.
6251             (setq articles (nthcdr (- number select) articles))))
6252       (setq gnus-newsgroup-unselected
6253             (gnus-sorted-intersection
6254              gnus-newsgroup-unreads
6255              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
6256       articles)))
6257
6258 (defun gnus-killed-articles (killed articles)
6259   (let (out)
6260     (while articles
6261       (if (inline (gnus-member-of-range (car articles) killed))
6262           (setq out (cons (car articles) out)))
6263       (setq articles (cdr articles)))
6264     out))
6265
6266 (defun gnus-adjust-marked-articles (info &optional active)
6267   "Remove all marked articles that are no longer legal."
6268   (let ((marked-lists (nth 3 info))
6269         (active (or active (gnus-gethash (car info) gnus-active-hashtb)))
6270         marked m prev)
6271     ;; There are many types of marked articles.
6272     (while marked-lists
6273       (setq m (cdr (setq prev (car marked-lists))))
6274       (cond ((or (eq 'tick (car prev)) (eq 'dormant (car prev)))
6275              ;; Make sure that all ticked articles are a subset of the
6276              ;; unread/unselected articles.
6277              (while m
6278                (if (or (memq (car m) gnus-newsgroup-unreads)
6279                        (memq (car m) gnus-newsgroup-unselected))
6280                    (setq prev m)
6281                  (setcdr prev (cdr m)))
6282                (setq m (cdr m))))
6283             ((eq 'score (car prev))
6284              ;; Scored articles should be a subset of
6285              ;; unread/unselected articles. 
6286              (while m
6287                (if (or (memq (car (car m)) gnus-newsgroup-unreads)
6288                        (memq (car (car m)) gnus-newsgroup-unreads))
6289                    (setq prev m)
6290                  (setcdr prev (cdr m)))
6291                (setq m (cdr m))))
6292             ((eq 'bookmark (car prev))
6293              ;; Bookmarks should be a subset of active articles.
6294              (while m
6295                (if (< (car (car m)) (car active))
6296                    (setcdr prev (cdr m))
6297                  (setq prev m))
6298                (setq m (cdr m))))
6299             ((eq 'killed (car prev))
6300              ;; Articles that have been through the kill process are
6301              ;; to be a subset of active articles.
6302              (while (and m (< (or (and (numberp (car m)) (car m))
6303                                   (cdr (car m)))
6304                               (car active)))
6305                (setcdr prev (cdr m))
6306                (setq m (cdr m)))
6307              (if (and m (< (or (and (numberp (car m)) (car m))
6308                                (car (car m)))
6309                            (car active))) 
6310                  (setcar (if (numberp (car m)) m (car m)) (car active))))
6311             ((or (eq 'reply (car prev)) (eq 'expire (car prev)))
6312              ;; The replied and expirable articles have to be articles
6313              ;; that are active. 
6314              (while m
6315                (if (< (car m) (car active))
6316                    (setcdr prev (cdr m))
6317                  (setq prev m))
6318                (setq m (cdr m)))))
6319       (setq marked-lists (cdr marked-lists)))
6320     ;; Remove all lists that are empty.
6321     (setq marked-lists (nth 3 info))
6322     (if marked-lists
6323         (progn
6324           (while (= 1 (length (car marked-lists)))
6325             (setq marked-lists (cdr marked-lists)))
6326           (setq m (cdr (setq prev marked-lists)))
6327           (while m
6328             (if (= 1 (length (car m)))
6329                 (setcdr prev (cdr m))
6330               (setq prev m))
6331             (setq m (cdr m)))
6332           (setcar (nthcdr 3 info) marked-lists)))
6333     ;; Finally, if there are no marked lists at all left, and if there
6334     ;; are no elements after the lists in the info list, we just chop
6335     ;; the info list off before the marked lists.
6336     (and (null marked-lists) 
6337          (not (nthcdr 4 info))
6338          (setcdr (nthcdr 2 info) nil)))
6339   info)
6340
6341 (defun gnus-set-marked-articles 
6342   (info ticked replied expirable killed dormant bookmark score) 
6343   "Enter the various lists of marked articles into the newsgroup info list."
6344   (let (newmarked)
6345     (and ticked (setq newmarked (cons (cons 'tick ticked) nil)))
6346     (and replied (setq newmarked (cons (cons 'reply replied) newmarked)))
6347     (and expirable (setq newmarked (cons (cons 'expire expirable) 
6348                                          newmarked)))
6349     (and killed (setq newmarked (cons (cons 'killed killed) newmarked)))
6350     (and dormant (setq newmarked (cons (cons 'dormant dormant) newmarked)))
6351     (and bookmark (setq newmarked (cons (cons 'bookmark bookmark) 
6352                                         newmarked)))
6353     (and score (setq newmarked (cons (cons 'score score) newmarked)))
6354     (if (nthcdr 3 info)
6355         (if newmarked
6356             (setcar (nthcdr 3 info) newmarked)
6357           (if (not (nthcdr 4 info))
6358               (setcdr (nthcdr 2 info) nil)
6359             (setcar (nthcdr 3 info) nil)))
6360       (if newmarked
6361           (setcdr (nthcdr 2 info) (cons newmarked nil))))))
6362
6363 (defun gnus-add-marked-articles (group type articles &optional info force)
6364   ;; Add ARTICLES of TYPE to the info of GROUP.
6365   ;; If INFO is non-nil, use that info. If FORCE is non-nil, don't
6366   ;; add, but replace marked articles of TYPE with ARTICLES.
6367   (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6368         marked m)
6369     (or (not info)
6370         (and (not (setq marked (nthcdr 3 info)))
6371              (setcdr (nthcdr 2 info) (list (list (cons type articles)))))
6372         (and (not (setq m (assq type (car marked))))
6373              (setcar marked (cons (cons type articles) (car marked))))
6374         (if force
6375             (setcdr m articles)
6376           (nconc m articles)))))
6377          
6378 (defun gnus-set-mode-line (where)
6379   "This function sets the mode line of the article or summary buffers.
6380 If WHERE is `summary', the summary mode line format will be used."
6381   (if (memq where gnus-updated-mode-lines)
6382       (let (mode-string)
6383         (save-excursion
6384           (set-buffer gnus-summary-buffer)
6385           (let* ((mformat (if (eq where 'article) 
6386                               gnus-article-mode-line-format-spec
6387                             gnus-summary-mode-line-format-spec))
6388                  (group-name gnus-newsgroup-name)
6389                  (article-number (or gnus-current-article 0))
6390                  (unread (- (length gnus-newsgroup-unreads)
6391                             (length gnus-newsgroup-dormant)))
6392                  (unread-and-unticked 
6393                   (- unread (length gnus-newsgroup-marked)))
6394                  (unselected (length gnus-newsgroup-unselected))
6395                  (unread-and-unselected
6396                   (cond ((and (zerop unread-and-unticked)
6397                               (zerop unselected)) "")
6398                         ((zerop unselected) 
6399                          (format "{%d more}" unread-and-unticked))
6400                         (t (format "{%d(+%d) more}"
6401                                    unread-and-unticked unselected))))
6402                  (subject
6403                   (if gnus-current-headers
6404                       (header-subject gnus-current-headers) ""))
6405                  (max-len (and gnus-mode-non-string-length
6406                                (- (frame-width) gnus-mode-non-string-length)))
6407                  header) ;; passed as argument to any user-format-funcs
6408             (setq mode-string (eval mformat))
6409             (or (numberp max-len)
6410                 (setq max-len (length mode-string)))
6411             (if (< max-len 4) (setq max-len 4))
6412             (if (> (length mode-string) max-len)
6413                 (setq mode-string 
6414                       (concat (substring mode-string 0 (- max-len 3))
6415                               "...")))
6416             (setq mode-string (format (format "%%-%ds" max-len)
6417                                       mode-string))))
6418         (setq mode-line-buffer-identification mode-string)
6419         (set-buffer-modified-p t))))
6420
6421 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6422   "Go through the HEADERS list and add all Xrefs to a hash table.
6423 The resulting hash table is returned, or nil if no Xrefs were found."
6424   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
6425          (prefix (if (and 
6426                       (gnus-group-foreign-p from-newsgroup)
6427                       (not (memq 'virtual 
6428                                  (assoc (symbol-name (car from-method))
6429                                         gnus-valid-select-methods))))
6430                      (gnus-group-real-prefix from-newsgroup)))
6431          (xref-hashtb (make-vector 63 0))
6432          start group entry number xrefs header)
6433     (while headers
6434       (setq header (car headers))
6435       (if (and (setq xrefs (header-xref header))
6436                (not (memq (header-number header) unreads)))
6437           (progn
6438             (setq start 0)
6439             (while (string-match "\\([^ ]+\\):\\([0-9]+\\)" xrefs start)
6440               (setq start (match-end 0))
6441               (setq group (concat prefix (substring xrefs (match-beginning 1) 
6442                                                     (match-end 1))))
6443               (setq number 
6444                     (string-to-int (substring xrefs (match-beginning 2) 
6445                                               (match-end 2))))
6446               (if (setq entry (gnus-gethash group xref-hashtb))
6447                   (setcdr entry (cons number (cdr entry)))
6448                 (gnus-sethash group (cons number nil) xref-hashtb)))))
6449       (setq headers (cdr headers)))
6450     (if start xref-hashtb nil)))
6451
6452 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads expirable)
6453   "Look through all the headers and mark the Xrefs as read."
6454   (let ((virtual (memq 'virtual 
6455                        (assoc (symbol-name (car (gnus-find-method-for-group 
6456                                                  from-newsgroup)))
6457                               gnus-valid-select-methods)))
6458         name entry read info xref-hashtb idlist active num range exps method
6459         nth4)
6460     (save-excursion
6461       (set-buffer gnus-group-buffer)
6462       (if (setq xref-hashtb 
6463                 (gnus-create-xref-hashtb from-newsgroup headers unreads))
6464           (mapatoms 
6465            (lambda (group)
6466              (if (string= from-newsgroup (setq name (symbol-name group)))
6467                  ()
6468                (setq idlist (symbol-value group))
6469                ;; Dead groups are not updated.
6470                (if (and (prog1 
6471                             (setq entry (gnus-gethash name gnus-newsrc-hashtb)
6472                                   info (nth 2 entry))
6473                           (if (stringp (setq nth4 (nth 4 info)))
6474                               (setq nth4 (gnus-server-to-method nth4))))
6475                         ;; Only do the xrefs if the group has the same
6476                         ;; select method as the group we have just read.
6477                         (or (gnus-methods-equal-p 
6478                              nth4 (gnus-find-method-for-group from-newsgroup))
6479                             virtual
6480                             (equal nth4 
6481                                    (setq method (gnus-find-method-for-group 
6482                                                  from-newsgroup)))
6483                             (and (equal (car nth4) (car method))
6484                                  (equal (nth 1 nth4) (nth 1 method))))
6485                         gnus-use-cross-reference
6486                         (or (not (eq gnus-use-cross-reference t))
6487                             virtual
6488                             ;; Only do cross-references on subscribed
6489                             ;; groups, if that is what is wanted.  
6490                             (<= (nth 1 info) gnus-level-subscribed)))
6491                    (progn
6492                      (setq num 0)
6493                      ;; Set the new list of read articles in this group.
6494                      (setq active (gnus-gethash name gnus-active-hashtb))
6495                      ;; First peel off all illegal article numbers.
6496                      (if active
6497                          (let ((ids idlist)
6498                                (ticked (cdr (assq 'tick (nth 3 info))))
6499                                (dormant (cdr (assq 'dormant (nth 3 info))))
6500                                id)
6501                            (setq exps nil)
6502                            (while ids
6503                              (setq id (car ids))
6504                              (if (or (> id (cdr active))
6505                                      (< id (car active))
6506                                      (memq id ticked)
6507                                      (memq id dormant))
6508                                  (setq idlist (delq id idlist)))
6509                              (and (memq id expirable)
6510                                   (setq exps (cons id exps)))
6511                              (setq ids (cdr ids)))))
6512                      ;; Update expirable articles.
6513                      (gnus-add-marked-articles nil 'expirable exps info)
6514                      (and active
6515                           (null (nth 2 info))
6516                           (> (car active) 1)
6517                           (setcar (nthcdr 2 info) (cons 1 (1- (car active)))))
6518                      (setcar (nthcdr 2 info)
6519                              (setq range
6520                                    (gnus-add-to-range 
6521                                     (nth 2 info) 
6522                                     (setq idlist (sort idlist '<)))))
6523                      ;; Then we have to re-compute how many unread
6524                      ;; articles there are in this group.
6525                      (if active
6526                          (progn
6527                            (cond 
6528                             ((not range)
6529                              (setq num (- (1+ (cdr active)) (car active))))
6530                             ((not (listp (cdr range)))
6531                              (setq num (- (cdr active) (- (1+ (cdr range)) 
6532                                                           (car range)))))
6533                             (t
6534                              (while range
6535                                (if (numberp (car range))
6536                                    (setq num (1+ num))
6537                                  (setq num (+ num (- (1+ (cdr (car range)))
6538                                                      (car (car range))))))
6539                                (setq range (cdr range)))
6540                              (setq num (- (cdr active) num))))
6541                            ;; Update the number of unread articles.
6542                            (setcar 
6543                             entry 
6544                             (max 0 (- num 
6545                                       (length (cdr (assq 'tick (nth 3 info))))
6546                                       (length 
6547                                        (cdr (assq 'dormant (nth 3 info)))))))
6548                            ;; Update the group buffer.
6549                            (gnus-group-update-group name t)))))))
6550            xref-hashtb)))))
6551
6552 (defun gnus-methods-equal-p (m1 m2)
6553   (let ((m1 (or m1 gnus-select-method))
6554         (m2 (or m2 gnus-select-method)))
6555     (or (equal m1 m2)
6556         (and (eq (car m1) (car m2))
6557              (or (not (memq 'address (assoc (symbol-name (car m1))
6558                                             gnus-valid-select-methods)))
6559                  (equal (nth 1 m1) (nth 1 m2)))))))
6560
6561 (defsubst gnus-header-value ()
6562   (buffer-substring (match-end 0) (gnus-point-at-eol)))
6563
6564 (defvar gnus-newsgroup-none-id 0)
6565
6566 (defun gnus-get-newsgroup-headers ()
6567   (setq gnus-article-internal-prepare-hook nil)
6568   (let ((cur nntp-server-buffer)
6569         (dependencies gnus-newsgroup-dependencies)
6570         headers char article id dep end)
6571     (save-excursion
6572       (set-buffer nntp-server-buffer)
6573       (goto-char (point-min))
6574       ;; Search to the beginning of the next header. Error messages
6575       ;; do not begin with 2 or 3.
6576       (while (re-search-forward "^[23][0-9]+ " nil t)
6577         (let ((header (make-vector 9 nil))
6578               (c (following-char))
6579               (case-fold-search t)
6580               (p (point))
6581               from subject in-reply-to references ref)
6582           (setq id nil
6583                 ref nil
6584                 references nil
6585                 subject nil
6586                 from nil)
6587           (header-set-number header (setq article (read cur)))
6588           ;; This implementation of this function, with nine
6589           ;; search-forwards instead of the one re-search-forward and
6590           ;; a case (which basically was the old function) is actually
6591           ;; about twice as fast, even though it looks messier. You
6592           ;; can't have everything, I guess. Speed and elegance
6593           ;; doesn't always come hand in hand.
6594           (save-restriction
6595             (narrow-to-region (point) (or (save-excursion 
6596                                             (search-forward "\n.\n" nil t))
6597                                           (point)))
6598             (if (search-forward "\nfrom: " nil t)
6599                 (header-set-from header (gnus-header-value))
6600               (header-set-from header "(nobody)"))
6601             (goto-char p)
6602             (if (search-forward "\nsubject: " nil t)
6603                 (header-set-subject header (gnus-header-value))
6604               (header-set-subject header "(none)"))
6605             (goto-char p)
6606             (and (search-forward "\nxref: " nil t)
6607                  (header-set-xref header (gnus-header-value)))
6608             (goto-char p)
6609             (or (numberp (and (search-forward "\nlines: " nil t)
6610                               (header-set-lines header (read cur))))
6611                 (header-set-lines header 0))
6612             (goto-char p)
6613             (and (search-forward "\ndate: " nil t)
6614                  (header-set-date header (gnus-header-value)))
6615             (goto-char p)
6616             (if (search-forward "\nmessage-id: " nil t)
6617                 (header-set-id header (setq id (gnus-header-value)))
6618               ;; If there was no message-id, we just fake one to make
6619               ;; subsequent routines simpler.
6620               (header-set-id 
6621                header 
6622                (setq id (concat "none+" 
6623                                 (int-to-string 
6624                                  (setq gnus-newsgroup-none-id 
6625                                        (1+ gnus-newsgroup-none-id)))))))
6626             (goto-char p)
6627             (if (search-forward "\nreferences: " nil t)
6628                 (progn
6629                   (header-set-references header (gnus-header-value))
6630                   (setq end (match-end 0))
6631                   (save-excursion
6632                     (setq ref 
6633                           (downcase
6634                            (buffer-substring
6635                             (progn 
6636                               (end-of-line)
6637                               (search-backward ">" end t)
6638                               (1+ (point)))
6639                             (progn
6640                               (search-backward "<" end t)
6641                               (point)))))))
6642               ;; Get the references from the in-reply-to header if there
6643               ;; ware no references and the in-reply-to header looks
6644               ;; promising. 
6645               (if (and (search-forward "\nin-reply-to: " nil t)
6646                        (setq in-reply-to (gnus-header-value))
6647                        (string-match "<[^>]+>" in-reply-to))
6648                   (progn
6649                     (header-set-references 
6650                      header 
6651                      (setq ref (substring in-reply-to (match-beginning 0)
6652                                           (match-end 0))))
6653                     (setq ref (downcase ref)))
6654                 (setq ref "none")))
6655             ;; We do some threading while we read the headers. The
6656             ;; message-id and the last reference are both entered into
6657             ;; the same hash table. Some tippy-toeing around has to be
6658             ;; done in case an article has arrived before the article
6659             ;; which it refers to.
6660             (if (boundp (setq dep (intern (downcase id) dependencies)))
6661                 (if (car (symbol-value dep))
6662                     ;; An article with this Message-ID has already
6663                     ;; been seen, so we ignore this one, except we add
6664                     ;; any additional Xrefs (in case the two articles
6665                     ;; came from different servers.
6666                     (progn
6667                       (header-set-xref 
6668                        (car (symbol-value dep))
6669                        (concat (or (header-xref (car (symbol-value dep))) "")
6670                                (or (header-xref header) "")))
6671                       (setq header nil))
6672                   (setcar (symbol-value dep) header))
6673               (set dep (list header)))
6674             (if header
6675                 (progn
6676                   (if (boundp (setq dep (intern ref dependencies)))
6677                       (setcdr (symbol-value dep) 
6678                               (cons header (cdr (symbol-value dep))))
6679                     (set dep (list nil header)))
6680                   (setq headers (cons header headers))))
6681             (goto-char (point-max))))))
6682     (nreverse headers)))
6683
6684 ;; The following macros and functions were written by Felix Lee
6685 ;; <flee@cse.psu.edu>. 
6686
6687 (defmacro gnus-nov-read-integer ()
6688   '(prog1
6689        (if (= (following-char) ?\t)
6690            0
6691          (let ((num (condition-case nil (read buffer) (error nil))))
6692            (if (numberp num) num 0)))
6693      (or (eobp) (forward-char 1))))
6694
6695 (defmacro gnus-nov-skip-field ()
6696   '(search-forward "\t" eol 'move))
6697
6698 (defmacro gnus-nov-field ()
6699   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
6700
6701 ;; Goes through the xover lines and returns a list of vectors
6702 (defun gnus-get-newsgroup-headers-xover (sequence)
6703   "Parse the news overview data in the server buffer, and return a
6704 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
6705   ;; Get the Xref when the users reads the articles since most/some
6706   ;; NNTP servers do not include Xrefs when using XOVER.
6707   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6708   (let ((cur nntp-server-buffer)
6709         (dependencies gnus-newsgroup-dependencies)
6710         (none 0)
6711         number headers header)
6712     (save-excursion
6713       (set-buffer nntp-server-buffer)
6714       (goto-char (point-min))
6715       (while (and sequence (not (eobp)))
6716         (setq number (read cur))
6717         (while (and sequence (< (car sequence) number))
6718           (setq sequence (cdr sequence)))
6719         (and sequence 
6720              (eq number (car sequence))
6721              (progn
6722                (setq sequence (cdr sequence))
6723                (if (setq header 
6724                          (inline (gnus-nov-parse-line number dependencies)))
6725                    (setq headers (cons header headers)))))
6726         (forward-line 1))
6727       (setq headers (nreverse headers)))
6728     headers))
6729
6730 ;; This function has to be called with point after the article number
6731 ;; on the beginning of the line.
6732 (defun gnus-nov-parse-line (number dependencies)
6733   (let ((none 0)
6734         (eol (gnus-point-at-eol)) 
6735         (buffer (current-buffer))
6736         header ref id dep)
6737
6738     ;; overview: [num subject from date id refs chars lines misc]
6739     (narrow-to-region (point) eol)
6740     (forward-char)
6741
6742     (condition-case nil
6743         (setq header
6744               (vector 
6745                number                   ; number
6746                (gnus-nov-field)         ; subject
6747                (gnus-nov-field)         ; from
6748                (gnus-nov-field)         ; date
6749                (setq id (or (gnus-nov-field)
6750                             (concat "none+"
6751                                     (int-to-string 
6752                                      (setq none (1+ none)))))) ; id
6753                (progn
6754                  (save-excursion
6755                    (let ((beg (point)))
6756                      (search-forward "\t" eol)
6757                      (if (search-backward ">" beg t)
6758                          (setq ref 
6759                                (downcase 
6760                                 (buffer-substring 
6761                                  (1+ (point))
6762                                  (progn
6763                                    (search-backward "<" beg t)
6764                                    (point)))))
6765                        (setq ref nil))))
6766                  (gnus-nov-field))      ; refs
6767                (gnus-nov-read-integer)  ; chars
6768                (gnus-nov-read-integer)  ; lines
6769                (if (= (following-char) ?\n)
6770                    nil
6771                  (gnus-nov-field))      ; misc
6772                ))
6773       (error (progn 
6774                (ding)
6775                (message "Strange nov line.")
6776                (setq header nil)
6777                (goto-char eol))))
6778
6779     (widen)
6780
6781     ;; We build the thread tree.
6782     (and header
6783          (if (boundp (setq dep (intern (downcase id) dependencies)))
6784              (if (car (symbol-value dep))
6785                  ;; An article with this Message-ID has already been seen,
6786                  ;; so we ignore this one, except we add any additional
6787                  ;; Xrefs (in case the two articles came from different
6788                  ;; servers.
6789                  (progn
6790                    (header-set-xref 
6791                     (car (symbol-value dep))
6792                     (concat (or (header-xref (car (symbol-value dep))) "")
6793                             (or (header-xref header) "")))
6794                    (setq header nil))
6795                (setcar (symbol-value dep) header))
6796            (set dep (list header))))
6797     (if header
6798         (progn
6799           (if (boundp (setq dep (intern (or ref "none") 
6800                                         dependencies)))
6801               (setcdr (symbol-value dep) 
6802                       (cons header (cdr (symbol-value dep))))
6803             (set dep (list nil header)))))
6804     header))
6805
6806 (defun gnus-article-get-xrefs ()
6807   "Fill in the Xref value in `gnus-current-headers', if necessary.
6808 This is meant to be called in `gnus-article-internal-prepare-hook'."
6809   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
6810                                  gnus-current-headers)))
6811     (or (not gnus-use-cross-reference)
6812         (not headers)
6813         (and (header-xref headers)
6814              (not (string= (header-xref headers) "")))
6815         (let ((case-fold-search t)
6816               xref)
6817           (save-restriction
6818             (gnus-narrow-to-headers)
6819             (goto-char (point-min))
6820             (if (or (and (eq (downcase (following-char)) ?x)
6821                          (looking-at "Xref:"))
6822                     (search-forward "\nXref:" nil t))
6823                 (progn
6824                   (goto-char (1+ (match-end 0)))
6825                   (setq xref (buffer-substring (point) 
6826                                                (progn (end-of-line) (point))))
6827                   (header-set-xref headers xref))))))))
6828
6829 (defalias 'gnus-find-header-by-number 'gnus-get-header-by-number)
6830 (make-obsolete 'gnus-find-header-by-number 'gnus-get-header-by-number)
6831
6832 ;; Return a header specified by a NUMBER.
6833 (defun gnus-get-header-by-number (number)
6834   (save-excursion
6835     (set-buffer gnus-summary-buffer)
6836     (or gnus-newsgroup-headers-hashtb-by-number
6837         (gnus-make-headers-hashtable-by-number))
6838     (gnus-gethash (int-to-string number)
6839                   gnus-newsgroup-headers-hashtb-by-number)))
6840
6841 (defun gnus-make-headers-hashtable-by-number ()
6842   "Make hashtable for the variable gnus-newsgroup-headers by number."
6843   (save-excursion
6844     (set-buffer gnus-summary-buffer)
6845     (let ((headers gnus-newsgroup-headers)
6846           header)
6847       (setq gnus-newsgroup-headers-hashtb-by-number
6848             (gnus-make-hashtable (length headers)))
6849       (while headers
6850         (setq header (car headers))
6851         (gnus-sethash (int-to-string (header-number header))
6852                       header gnus-newsgroup-headers-hashtb-by-number)
6853         (setq headers (cdr headers))))))
6854
6855 (defun gnus-more-header-backward ()
6856   "Find new header backward."
6857   (let ((first (car (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6858         (artnum gnus-newsgroup-begin)
6859         (header nil))
6860     (while (and (not header)
6861                 (> artnum first))
6862       (setq artnum (1- artnum))
6863       (setq header (gnus-read-header artnum)))
6864     header))
6865
6866 (defun gnus-more-header-forward (&optional backward)
6867   "Find new header forward.
6868 If BACKWARD, find new header backward instead."
6869   (if backward
6870       (gnus-more-header-backward)
6871     (let ((last (cdr (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6872           (artnum gnus-newsgroup-end)
6873           (header nil))
6874       (while (and (not header)
6875                   (< artnum last))
6876         (setq artnum (1+ artnum))
6877         (setq header (gnus-read-header artnum)))
6878       header)))
6879
6880 (defun gnus-extend-newsgroup (header &optional backward)
6881   "Extend newsgroup selection with HEADER.
6882 Optional argument BACKWARD means extend toward backward."
6883   (if header
6884       (let ((artnum (header-number header)))
6885         (setq gnus-newsgroup-headers
6886               (if backward
6887                   (cons header gnus-newsgroup-headers)
6888                 (nconc gnus-newsgroup-headers (list header))))
6889         (setq gnus-newsgroup-unselected
6890               (delq artnum gnus-newsgroup-unselected))
6891         (setq gnus-newsgroup-begin (min gnus-newsgroup-begin artnum))
6892         (setq gnus-newsgroup-end (max gnus-newsgroup-end artnum)))))
6893
6894 (defun gnus-summary-work-articles (n)
6895   "Return a list of articles to be worked upon. The prefix argument,
6896 the list of process marked articles, and the current article will be
6897 taken into consideration."
6898   (let (articles)
6899     (if (and n (numberp n))
6900         (let ((backward (< n 0))
6901               (n (abs n)))
6902           (save-excursion
6903             (while (and (> n 0)
6904                         (setq articles (cons (gnus-summary-article-number) 
6905                                              articles))
6906                         (gnus-summary-search-forward nil nil backward))
6907               (setq n (1- n))))
6908           (sort articles (function <)))
6909       (or (reverse gnus-newsgroup-processable)
6910           (list (gnus-summary-article-number))))))
6911
6912 (defun gnus-summary-search-group (&optional backward use-level)
6913   "Search for next unread newsgroup.
6914 If optional argument BACKWARD is non-nil, search backward instead."
6915   (save-excursion
6916     (set-buffer gnus-group-buffer)
6917     (save-excursion
6918       ;; We don't want to alter current point of group mode buffer.
6919       (if (gnus-group-search-forward 
6920            backward nil
6921            (if use-level (gnus-group-group-level) nil))
6922           (gnus-group-group-name)))))
6923
6924 (defun gnus-summary-best-group (&optional exclude-group)
6925   "Find the name of the best unread group.
6926 If EXCLUDE-GROUP, do not go to this group."
6927   (save-excursion
6928     (set-buffer gnus-group-buffer)
6929     (save-excursion
6930       (gnus-group-best-unread-group exclude-group))))
6931
6932 (defun gnus-summary-search-subject (&optional backward unread subject)
6933   "Search for article forward.
6934 If BACKWARD is non-nil, search backward.
6935 If UNREAD is non-nil, only unread articles are selected.
6936 If SUBJECT is non-nil, the article which has the same subject will be
6937 searched for." 
6938   (let ((func (if backward 'previous-single-property-change
6939                 'next-single-property-change))
6940         (beg (point))
6941         (did t)
6942         pos)
6943     (beginning-of-line)
6944     (and gnus-summary-check-current unread
6945          (eq (get-text-property (point) 'gnus-mark) gnus-unread-mark)
6946          (setq did nil))
6947     (if (not did)
6948         ()
6949       (forward-char (if backward (if (bobp) 0 -1) (if (eobp) 0 1)))
6950       (while
6951           (and 
6952            (setq pos (funcall func (point) 'gnus-number))
6953            (goto-char (if backward (1- pos) pos))
6954            (setq did
6955                  (not (and
6956                        (or (not unread)
6957                            (eq (get-text-property (point) 'gnus-mark)
6958                                gnus-unread-mark))
6959                        (or (not subject)
6960                            (equal (gnus-simplify-subject-re subject)
6961                                   (gnus-simplify-subject-re
6962                                    (gnus-summary-subject-string)))))))
6963            (if backward (if (bobp) nil (forward-char -1) t)
6964              (if (eobp) nil (forward-char 1) t)))))
6965     (if did
6966         (progn (goto-char beg) nil)
6967       (prog1
6968           (get-text-property (point) 'gnus-number)
6969         (gnus-summary-position-cursor)))))
6970
6971 (defun gnus-summary-search-forward (&optional unread subject backward)
6972   "Search for article forward.
6973 If UNREAD is non-nil, only unread articles are selected.
6974 If SUBJECT is non-nil, the article which has the same subject will be
6975 searched for. 
6976 If BACKWARD is non-nil, the search will be performed backwards instead."
6977   (gnus-summary-search-subject backward unread subject))
6978
6979 (defun gnus-summary-search-backward (&optional unread subject)
6980   "Search for article backward.
6981 If 1st optional argument UNREAD is non-nil, only unread article is selected.
6982 If 2nd optional argument SUBJECT is non-nil, the article which has
6983 the same subject will be searched for."
6984   (gnus-summary-search-forward unread subject t))
6985
6986 (defun gnus-summary-article-number (&optional number-or-nil)
6987   "The article number of the article on the current line.
6988 If there isn's an article number here, then we return the current
6989 article number."
6990   (let* ((number (get-text-property (gnus-point-at-bol) 'gnus-number)))
6991     (if number-or-nil number (or number gnus-current-article))))
6992
6993 (defun gnus-summary-thread-level ()
6994   "The thread level of the article on the current line."
6995   (or (get-text-property (gnus-point-at-bol) 'gnus-level)
6996       0))
6997
6998 (defun gnus-summary-pseudo-article ()
6999   "The thread level of the article on the current line."
7000   (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
7001
7002 (defun gnus-summary-article-mark ()
7003   "The mark on the current line."
7004   (get-text-property (gnus-point-at-bol) 'gnus-mark))
7005
7006 (defun gnus-summary-subject-string ()
7007   "Return current subject string or nil if nothing."
7008   (let ((article (gnus-summary-article-number))
7009         header)
7010     (and article 
7011          (setq header (gnus-get-header-by-number article))
7012          (vectorp header)
7013          (header-subject header))))
7014
7015 (defalias 'gnus-summary-score 'gnus-summary-article-score)
7016 (make-obsolete 'gnus-summary-score 'gnus-summary-article-score)
7017 (defun gnus-summary-article-score ()
7018   "Return current article score."
7019   (or (cdr (assq (gnus-summary-article-number) gnus-newsgroup-scored))
7020       gnus-summary-default-score 0))
7021
7022 ;; Written by Sudish Joseph <joseph@cis.ohio-state.edu>.
7023
7024 (defun gnus-summary-recenter ()
7025   "Center point in the summary window.
7026 If `gnus-auto-center-summary' is nil, or the article buffer isn't
7027 displayed, no centering will be performed." 
7028   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
7029   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
7030   (let* ((top (cond ((< (window-height) 4) 0)
7031                     ((< (window-height) 6) 1)
7032                     (t 2)))
7033          (height (1- (window-height)))
7034          (bottom (save-excursion (goto-char (point-max))
7035                                  (forward-line (- height))
7036                                  (point)))
7037          (window (get-buffer-window (current-buffer))))
7038     (and 
7039      ;; The user has to want it,
7040      gnus-auto-center-summary 
7041      ;; the article buffer must be displayed,
7042      (get-buffer-window gnus-article-buffer)
7043      ;; Set the window start to either `bottom', which is the biggest
7044      ;; possible valid number, or the second line from the top,
7045      ;; whichever is the least.
7046      (set-window-start
7047       window (min bottom (save-excursion (forward-line (- top)) (point)))))))
7048
7049 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
7050 (defun gnus-short-group-name (group &optional levels)
7051   "Collapse GROUP name LEVELS."
7052   (let* ((name "") (foreign "") (depth -1) (skip 1)
7053          (levels (or levels
7054                      (progn
7055                        (while (string-match "\\." group skip)
7056                          (setq skip (match-end 0)
7057                                depth (+ depth 1)))
7058                        depth))))
7059     (if (string-match ":" group)
7060         (setq foreign (substring group 0 (match-end 0))
7061               group (substring group (match-end 0))))
7062     (while group
7063       (if (and (string-match "\\." group) (> levels 0))
7064           (setq name (concat name (substring group 0 1))
7065                 group (substring group (match-end 0))
7066                 levels (- levels 1)
7067                 name (concat name "."))
7068         (setq name (concat foreign name group)
7069               group nil)))
7070     name))
7071
7072 (defun gnus-summary-jump-to-group (newsgroup)
7073   "Move point to NEWSGROUP in group mode buffer."
7074   ;; Keep update point of group mode buffer if visible.
7075   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
7076       (save-window-excursion
7077         ;; Take care of tree window mode.
7078         (if (get-buffer-window gnus-group-buffer)
7079             (pop-to-buffer gnus-group-buffer))
7080         (gnus-group-jump-to-group newsgroup))
7081     (save-excursion
7082       ;; Take care of tree window mode.
7083       (if (get-buffer-window gnus-group-buffer)
7084           (pop-to-buffer gnus-group-buffer)
7085         (set-buffer gnus-group-buffer))
7086       (gnus-group-jump-to-group newsgroup))))
7087
7088 ;; This function returns a list of article numbers based on the
7089 ;; difference between the ranges of read articles in this group and
7090 ;; the range of active articles.
7091 (defun gnus-list-of-unread-articles (group)
7092   (let* ((read (nth 2 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
7093          (active (gnus-gethash group gnus-active-hashtb))
7094          (last (cdr active))
7095          unread first nlast unread)
7096     ;; If none are read, then all are unread. 
7097     (if (not read)
7098         (setq first (car active))
7099       ;; If the range of read articles is a single range, then the
7100       ;; first unread article is the article after the last read
7101       ;; article. Sounds logical, doesn't it?
7102       (if (not (listp (cdr read)))
7103           (setq first (1+ (cdr read)))
7104         ;; `read' is a list of ranges.
7105         (if (/= (setq nlast (or (and (numberp (car read)) (car read)) 
7106                                 (car (car read)))) 1)
7107             (setq first 1))
7108         (while read
7109           (if first 
7110               (while (< first nlast)
7111                 (setq unread (cons first unread))
7112                 (setq first (1+ first))))
7113           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
7114           (setq nlast (if (atom (car (cdr read))) 
7115                           (car (cdr read))
7116                         (car (car (cdr read)))))
7117           (setq read (cdr read)))))
7118     ;; And add the last unread articles.
7119     (while (<= first last)
7120       (setq unread (cons first unread))
7121       (setq first (1+ first)))
7122     ;; Return the list of unread articles.
7123     (nreverse unread)))
7124
7125 (defun gnus-list-of-read-articles (group)
7126   (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
7127         (active (gnus-gethash group gnus-active-hashtb)))
7128     (and info active
7129          (gnus-sorted-complement 
7130           (gnus-uncompress-range active) 
7131           (gnus-list-of-unread-articles group)))))
7132
7133 ;; Various summary commands
7134
7135 (defun gnus-summary-universal-argument ()
7136   "Perform any operation on all articles marked with the process mark."
7137   (interactive)
7138   (gnus-set-global-variables)
7139   (let ((articles (reverse gnus-newsgroup-processable))
7140         key func)
7141     (or articles (error "No articles marked"))
7142     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
7143         (error "Undefined key"))
7144     (while articles
7145       (gnus-summary-goto-subject (car articles))
7146       (command-execute func)
7147       (gnus-summary-remove-process-mark (car articles))
7148       (setq articles (cdr articles)))))
7149
7150 (defun gnus-summary-toggle-truncation (arg)
7151   "Toggle truncation of summary lines.
7152 With arg, turn line truncation on iff arg is positive."
7153   (interactive "P")
7154   (setq truncate-lines
7155         (if (null arg) (not truncate-lines)
7156           (> (prefix-numeric-value arg) 0)))
7157   (redraw-display))
7158
7159 (defun gnus-summary-reselect-current-group (all)
7160   "Once exit and then reselect the current newsgroup.
7161 The prefix argument ALL means to select all articles."
7162   (interactive "P")
7163   (gnus-set-global-variables)
7164   (let ((current-subject (gnus-summary-article-number))
7165         (group gnus-newsgroup-name))
7166     (setq gnus-newsgroup-threads nil)
7167     (gnus-summary-exit t)
7168     ;; We have to adjust the point of group mode buffer because the
7169     ;; current point was moved to the next unread newsgroup by
7170     ;; exiting.
7171     (gnus-summary-jump-to-group group)
7172     (gnus-group-read-group all t)
7173     (gnus-summary-goto-subject current-subject)))
7174
7175 (defun gnus-summary-rescan-group (all)
7176   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7177   (interactive "P")
7178   (gnus-set-global-variables)
7179   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
7180   (let ((group gnus-newsgroup-name))
7181     (gnus-summary-exit)
7182     (gnus-summary-jump-to-group group)
7183     (save-excursion
7184       (set-buffer gnus-group-buffer)
7185       (gnus-group-get-new-news-this-group 1))
7186     (gnus-summary-jump-to-group group)
7187     (gnus-group-read-group all)))
7188
7189 (defun gnus-summary-update-info ()
7190   (let* ((group gnus-newsgroup-name)
7191          (method (car (gnus-find-method-for-group group))))
7192     (if gnus-newsgroup-kill-headers
7193         (setq gnus-newsgroup-killed
7194               (gnus-compress-sequence
7195                (nconc
7196                 (gnus-set-sorted-intersection
7197                  (gnus-uncompress-range gnus-newsgroup-killed)
7198                  (setq gnus-newsgroup-unselected
7199                        (sort gnus-newsgroup-unselected '<)))
7200                 (setq gnus-newsgroup-unreads
7201                       (sort gnus-newsgroup-unreads '<))) t)))
7202     (or (listp (cdr gnus-newsgroup-killed))
7203         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7204     (let ((updated nil)
7205           (headers gnus-newsgroup-headers))
7206       (gnus-close-group group)
7207       (run-hooks 'gnus-exit-group-hook)
7208       (gnus-update-read-articles 
7209        group gnus-newsgroup-unreads gnus-newsgroup-unselected 
7210        gnus-newsgroup-marked
7211        t gnus-newsgroup-replied gnus-newsgroup-expirable
7212        gnus-newsgroup-killed gnus-newsgroup-dormant
7213        gnus-newsgroup-bookmarks 
7214        (and gnus-save-score gnus-newsgroup-scored))
7215       (and gnus-use-cross-reference
7216            (gnus-mark-xrefs-as-read 
7217             group headers gnus-newsgroup-unreads gnus-newsgroup-expirable))
7218       ;; Do adaptive scoring, and possibly save score files.
7219       (and gnus-newsgroup-adaptive
7220            (gnus-score-adaptive))
7221       (and gnus-use-scoring 
7222            (fboundp 'gnus-score-save)
7223            (funcall 'gnus-score-save))
7224       ;; Do not switch windows but change the buffer to work.
7225       (set-buffer gnus-group-buffer)
7226       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7227           (gnus-group-update-group group)))))
7228   
7229 (defun gnus-summary-exit (&optional temporary)
7230   "Exit reading current newsgroup, and then return to group selection mode.
7231 gnus-exit-group-hook is called with no arguments if that value is non-nil."
7232   (interactive)
7233   (gnus-set-global-variables)
7234   (gnus-kill-save-kill-buffer)
7235   (let* ((group gnus-newsgroup-name)
7236          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
7237                                                   gnus-newsgroup-name))))
7238          (mode major-mode)
7239          (method (car (gnus-find-method-for-group group)))
7240          (buf (current-buffer)))
7241     (gnus-summary-update-info) ; Make all changes in this group permanent.
7242     ;; Make sure where I was, and go to next newsgroup.
7243     (or quit-config
7244         (progn
7245           (gnus-group-jump-to-group group)
7246           (gnus-group-next-unread-group 1)))
7247     (if temporary
7248         nil                             ;Nothing to do.
7249       ;; We set all buffer-local variables to nil. It is unclear why
7250       ;; this is needed, but if we don't, buffer-local variables are
7251       ;; not garbage-collected, it seems. This would the lead to en
7252       ;; ever-growing Emacs.
7253       (set-buffer buf)
7254       (gnus-summary-clear-local-variables)
7255       ;; We clear the global counterparts of the buffer-local
7256       ;; variables as well, just to be on the safe side.
7257       (gnus-configure-windows 'group)
7258       (gnus-summary-clear-local-variables)
7259       ;; Return to group mode buffer. 
7260       (if (eq mode 'gnus-summary-mode)
7261           (gnus-kill-buffer buf))
7262       (if (get-buffer gnus-article-buffer)
7263           (bury-buffer gnus-article-buffer))
7264       (setq gnus-current-select-method gnus-select-method)
7265       (pop-to-buffer gnus-group-buffer)
7266       (if (not quit-config)
7267           (progn
7268             (gnus-group-jump-to-group group)
7269             (gnus-group-next-unread-group 1))
7270         (if (not (buffer-name (car quit-config)))
7271             (gnus-configure-windows 'group)
7272           (set-buffer (car quit-config))
7273           (and (eq major-mode 'gnus-summary-mode)
7274                (gnus-set-global-variables))
7275           (gnus-configure-windows (cdr quit-config)))))))
7276
7277 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7278 (defun gnus-summary-exit-no-update (&optional no-questions)
7279   "Quit reading current newsgroup without updating read article info."
7280   (interactive)
7281   (let* ((group gnus-newsgroup-name)
7282          (quit-config (nth 1 (assoc 'quit-config 
7283                                     (gnus-find-method-for-group group)))))
7284     (if (or no-questions
7285             gnus-expert-user
7286             (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
7287         (progn
7288           (gnus-close-group group)
7289           (gnus-summary-clear-local-variables)
7290           (set-buffer gnus-group-buffer)
7291           (gnus-summary-clear-local-variables)
7292           ;; Return to group selection mode.
7293           (gnus-configure-windows 'group)
7294           (if (get-buffer gnus-summary-buffer)
7295               (kill-buffer gnus-summary-buffer))
7296           (if (get-buffer gnus-article-buffer)
7297               (bury-buffer gnus-article-buffer))
7298           (if (equal (gnus-group-group-name) group)
7299               (gnus-group-next-unread-group 1))
7300           (if quit-config
7301               (progn
7302                 (if (not (buffer-name (car quit-config)))
7303                     (gnus-configure-windows 'group)
7304                   (set-buffer (car quit-config))
7305                   (and (eq major-mode 'gnus-summary-mode)
7306                        (gnus-set-global-variables))
7307                   (gnus-configure-windows (cdr quit-config)))))))))
7308
7309 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7310 (defun gnus-summary-fetch-faq (group)
7311   "Fetch the FAQ for the current group."
7312   (interactive (list gnus-newsgroup-name))
7313   (gnus-configure-windows 'summary-faq)
7314   (find-file (concat gnus-group-faq-directory group)))
7315
7316 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7317 (defun gnus-summary-describe-group (force)
7318   "Describe the current newsgroup."
7319   (interactive "P")
7320   (gnus-group-describe-group force gnus-newsgroup-name))
7321
7322 (defun gnus-summary-describe-briefly ()
7323   "Describe summary mode commands briefly."
7324   (interactive)
7325   (gnus-message 6
7326     (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")))
7327
7328 ;; Walking around group mode buffer from summary mode.
7329
7330 (defun gnus-summary-next-group (&optional no-article group backward)
7331   "Exit current newsgroup and then select next unread newsgroup.
7332 If prefix argument NO-ARTICLE is non-nil, no article is selected initially.
7333 If BACKWARD, go to previous group instead."
7334   (interactive "P")
7335   (gnus-set-global-variables)
7336   (let ((ingroup gnus-newsgroup-name)
7337         (sumbuf (current-buffer))
7338         num)
7339     (gnus-summary-exit t)               ;Update all information.
7340     (if (and group
7341              (or (and (numberp (setq num (car (gnus-gethash
7342                                                group gnus-newsrc-hashtb))))
7343                       (< num 1))
7344                  (null num)))
7345         (progn
7346           (gnus-group-jump-to-group group)
7347           (setq group nil))
7348       (gnus-group-jump-to-group ingroup))
7349     (gnus-summary-search-group backward)
7350     (let ((group (or group (gnus-summary-search-group backward)))
7351           (buf gnus-summary-buffer))
7352       (if (null group)
7353           (gnus-summary-exit-no-update t)
7354         (gnus-message 5 "Selecting %s..." group)
7355         ;; We are now in group mode buffer.
7356         ;; Make sure group mode buffer point is on GROUP.
7357         (gnus-group-jump-to-group group)
7358         (if (not (eq gnus-auto-select-next 'quietly))
7359             (progn
7360               (gnus-summary-read-group group nil no-article buf)
7361               (and (string= gnus-newsgroup-name ingroup)
7362                    (bufferp sumbuf) (buffer-name sumbuf)
7363                    (progn
7364                      (set-buffer (setq gnus-summary-buffer sumbuf))
7365                      (gnus-summary-exit-no-update t))))
7366           (let ((prevgroup group))
7367             (gnus-summary-read-group group nil no-article buf)
7368             (while (and (string= gnus-newsgroup-name ingroup)
7369                         (bufferp sumbuf) 
7370                         (buffer-name sumbuf)
7371                         (not (string= prevgroup (gnus-group-group-name))))
7372               (set-buffer gnus-group-buffer)
7373               (gnus-summary-read-group 
7374                (setq prevgroup (gnus-group-group-name)) 
7375                nil no-article buf))
7376             (and (string= prevgroup (gnus-group-group-name))
7377                  ;; We have reached the final group in the group
7378                  ;; buffer.
7379                  (progn
7380                    (set-buffer sumbuf)
7381                    (gnus-summary-exit)))))))))
7382
7383 (defun gnus-summary-prev-group (no-article)
7384   "Exit current newsgroup and then select previous unread newsgroup.
7385 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7386   (interactive "P")
7387   (gnus-summary-next-group no-article nil t))
7388
7389 ;; Walking around summary lines.
7390
7391 (defun gnus-summary-first-subject (unread)
7392   "Go to the first unread subject.
7393 If UNREAD is non-nil, go to the first unread article.
7394 Returns nil if there are no unread articles."
7395   (interactive "P")
7396   (prog1
7397       (if (or (not unread)
7398               (gnus-goto-char 
7399                (text-property-any 
7400                 (point-min) (point-max) 'gnus-mark gnus-unread-mark)))
7401           t 
7402         ;; If there are no unread articles.
7403         (gnus-message 3 "No more unread articles")
7404         nil)
7405     (gnus-summary-position-cursor)))
7406
7407 (defun gnus-summary-next-subject (n &optional unread dont-display)
7408   "Go to next N'th summary line.
7409 If N is negative, go to the previous N'th subject line.
7410 If UNREAD is non-nil, only unread articles are selected.
7411 The difference between N and the actual number of steps taken is
7412 returned."
7413   (interactive "p")
7414   (let ((backward (< n 0))
7415         (n (abs n)))
7416     (while (and (> n 0)
7417                 (gnus-summary-search-forward unread nil backward))
7418       (setq n (1- n)))
7419     (if (/= 0 n) (gnus-message 7 "No more%s articles"
7420                                (if unread " unread" "")))
7421     (or dont-display
7422         (progn
7423           (gnus-summary-recenter)
7424           (gnus-summary-position-cursor)))
7425   n))
7426
7427 (defun gnus-summary-next-unread-subject (n)
7428   "Go to next N'th unread summary line."
7429   (interactive "p")
7430   (gnus-summary-next-subject n t))
7431
7432 (defun gnus-summary-prev-subject (n &optional unread)
7433   "Go to previous N'th summary line.
7434 If optional argument UNREAD is non-nil, only unread article is selected."
7435   (interactive "p")
7436   (gnus-summary-next-subject (- n) unread))
7437
7438 (defun gnus-summary-prev-unread-subject (n)
7439   "Go to previous N'th unread summary line."
7440   (interactive "p")
7441   (gnus-summary-next-subject (- n) t))
7442
7443 (defun gnus-summary-goto-subject (article)
7444   "Go the subject line of ARTICLE."
7445   (interactive
7446    (list
7447     (string-to-int
7448      (completing-read "Article number: "
7449                       (mapcar
7450                        (lambda (headers)
7451                          (list
7452                           (int-to-string (header-number headers))))
7453                        gnus-newsgroup-headers)
7454                       nil 'require-match))))
7455   (or article (error "No article number"))
7456   (let ((b (point)))
7457     (if (not (gnus-goto-char (text-property-any (point-min) (point-max)
7458                                                 'gnus-number article)))
7459         ()
7460       (gnus-summary-show-thread)
7461       ;; Skip dummy articles. 
7462       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7463           (forward-line 1))
7464       (prog1
7465           (if (not (eobp))
7466               article
7467             (goto-char b)
7468             nil)
7469         (gnus-summary-position-cursor)))))
7470
7471 ;; Walking around summary lines with displaying articles.
7472
7473 (defun gnus-summary-expand-window ()
7474   "Expand summary window to show headers full window."
7475   (interactive)
7476   (gnus-set-global-variables)
7477   (gnus-configure-windows 'summary))
7478
7479 (defun gnus-summary-display-article (article &optional all-header)
7480   "Display ARTICLE in article buffer."
7481   (gnus-set-global-variables)
7482   (if (null article)
7483       nil
7484     (gnus-article-prepare article all-header)
7485     (gnus-summary-show-thread)
7486     (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7487         (progn
7488           (forward-line 1)
7489           (gnus-summary-position-cursor)))
7490     (run-hooks 'gnus-select-article-hook)
7491     (gnus-summary-recenter)
7492 ;    (set-window-point (get-buffer-window (current-buffer)) (point-max))
7493 ;    (sit-for 0)
7494     (gnus-summary-goto-subject article)
7495     ;; Successfully display article.
7496     (gnus-summary-update-line)
7497     (gnus-article-set-window-start 
7498      (cdr (assq article gnus-newsgroup-bookmarks)))
7499     t))
7500
7501 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7502   "Select the current article.
7503 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7504 non-nil, the article will be re-fetched even if it already present in
7505 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7506 be displayed."
7507   (and (not pseudo) (gnus-summary-pseudo-article)
7508        (error "This is a pseudo-article."))
7509   (let ((article (or article (gnus-summary-article-number)))
7510         (all-headers (not (not all-headers))) ;Must be T or NIL.
7511         did) 
7512     (prog1
7513         (save-excursion
7514           (set-buffer gnus-summary-buffer)
7515           (if (or (null gnus-current-article)
7516                   (null gnus-article-current)
7517                   (not (eq article (cdr gnus-article-current)))
7518                   (not (equal (car gnus-article-current) gnus-newsgroup-name))
7519                   force)
7520               ;; The requested article is different from the current article.
7521               (progn
7522                 (gnus-summary-display-article article all-headers)
7523                 (setq did article))
7524             (if all-headers (gnus-article-show-all-headers))
7525             nil))
7526       (if did 
7527           (gnus-article-set-window-start 
7528            (cdr (assq article gnus-newsgroup-bookmarks)))))))
7529
7530 (defun gnus-summary-set-current-mark (&optional current-mark)
7531   "Obsolete function."
7532   nil)
7533
7534 (defun gnus-summary-next-article (unread &optional subject backward)
7535   "Select the next article.
7536 If UNREAD, only unread articles are selected.
7537 If SUBJECT, only articles with SUBJECT are selected.
7538 If BACKWARD, the previous article is selected instead of the next."
7539   (interactive "P")
7540   (gnus-set-global-variables)
7541   (let ((opoint (point))
7542         (method (car (gnus-find-method-for-group gnus-newsgroup-name)))
7543         header)
7544     (cond
7545      ;; Is there such an article?
7546      ((gnus-summary-display-article 
7547        (gnus-summary-search-forward unread subject backward))
7548       (gnus-summary-position-cursor))
7549      ;; If not, we try the first unread, if that is wanted.
7550      ((and subject
7551            gnus-auto-select-same
7552            (gnus-summary-first-unread-article))
7553       (gnus-message 6 "Wrapped"))
7554      ;; Try to get next/previous article not displayed in this group.
7555      ((and gnus-auto-extend-newsgroup
7556            (not unread) (not subject)
7557            (setq header (gnus-more-header-forward backward)))
7558       (gnus-extend-newsgroup header backward)
7559       (let ((buffer-read-only nil))
7560         (goto-char (if backward (point-min) (point-max)))
7561         (gnus-summary-prepare-threads (list header) 0))
7562       (gnus-summary-goto-article (if backward gnus-newsgroup-begin
7563                                    gnus-newsgroup-end)))
7564      ;; Go to next/previous group.
7565      (t
7566       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7567           (gnus-summary-jump-to-group gnus-newsgroup-name))
7568       (let ((cmd (aref (this-command-keys) 0))
7569             (group 
7570              (if (eq gnus-keep-same-level 'best) 
7571                  (gnus-summary-best-group gnus-newsgroup-name)
7572                (gnus-summary-search-group backward gnus-keep-same-level))))
7573         ;; Keep just the event type of CMD.
7574         (and (listp cmd) (setq cmd (car cmd)))
7575         ;; Select next unread newsgroup automagically.
7576         (cond 
7577          ((not gnus-auto-select-next)
7578           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7579          ((eq gnus-auto-select-next 'quietly)
7580           ;; Select quietly.
7581           (if (assoc 'quit-config (gnus-find-method-for-group 
7582                                    gnus-newsgroup-name))
7583               (gnus-summary-exit)
7584             (gnus-message 7 "No more%s articles (%s)..."
7585                           (if unread " unread" "") 
7586                           (if group (concat "selecting " group)
7587                             "exiting"))
7588             (gnus-summary-next-group nil group backward)))
7589          (t
7590           (let ((keystrokes '(?\C-n ?\C-p))
7591                 key)
7592             (while (or (null key) (memq key keystrokes))
7593               (gnus-message 
7594                7 "No more%s articles%s" (if unread " unread" "")
7595                (if (and group (not (assoc 'quit-config
7596                                           (gnus-find-method-for-group 
7597                                            gnus-newsgroup-name))))
7598                    (format " (Type %s for %s [%s])"
7599                            (single-key-description cmd) group
7600                            (car (gnus-gethash group gnus-newsrc-hashtb)))
7601                  (format " (Type %s to exit %s)"
7602                          (single-key-description cmd)
7603                          gnus-newsgroup-name)))
7604               ;; Confirm auto selection.
7605               (let* ((event (read-event)))
7606                 (setq key (if (listp event) (car event) event))
7607                 (if (member key keystrokes)
7608                     (let ((obuf (current-buffer)))
7609                       (switch-to-buffer gnus-group-buffer)
7610                       (gnus-group-jump-to-group group)
7611                       (setq group (gnus-group-group-name))
7612                       (switch-to-buffer obuf)
7613                       (execute-kbd-macro (char-to-string key))))))
7614             (if (eq key cmd)
7615                 (if (or (not group) (assoc 'quit-config
7616                                            (gnus-find-method-for-group
7617                                             gnus-newsgroup-name)))
7618                     (gnus-summary-exit)
7619                   (gnus-summary-next-group nil group backward))
7620               (setq unread-command-events (list key)))))))))))
7621
7622 (defun gnus-summary-next-unread-article ()
7623   "Select unread article after current one."
7624   (interactive)
7625   (gnus-summary-next-article t (and gnus-auto-select-same
7626                                     (gnus-summary-subject-string))))
7627
7628 (defun gnus-summary-prev-article (unread &optional subject)
7629   "Select the article after the current one.
7630 If UNREAD is non-nil, only unread articles are selected."
7631   (interactive "P")
7632   (gnus-summary-next-article unread subject t))
7633
7634 (defun gnus-summary-prev-unread-article ()
7635   "Select unred article before current one."
7636   (interactive)
7637   (gnus-summary-prev-article t (and gnus-auto-select-same
7638                                     (gnus-summary-subject-string))))
7639
7640 (defun gnus-summary-next-page (lines &optional circular)
7641   "Show next page of selected article.
7642 If end of article, select next article.
7643 Argument LINES specifies lines to be scrolled up.
7644 If CIRCULAR is non-nil, go to the start of the article instead of 
7645 instead of selecting the next article when reaching the end of the
7646 current article." 
7647   (interactive "P")
7648   (setq gnus-summary-buffer (current-buffer))
7649   (gnus-set-global-variables)
7650   (let ((article (gnus-summary-article-number))
7651         (endp nil))
7652     (gnus-configure-windows 'article)
7653     (if (or (null gnus-current-article)
7654             (null gnus-article-current)
7655             (/= article (cdr gnus-article-current))
7656             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7657         ;; Selected subject is different from current article's.
7658         (gnus-summary-display-article article)
7659       (gnus-eval-in-buffer-window
7660        gnus-article-buffer
7661        (setq endp (gnus-article-next-page lines)))
7662       (if endp
7663           (cond (circular
7664                  (gnus-summary-beginning-of-article))
7665                 (lines
7666                  (gnus-message 3 "End of message"))
7667                 ((null lines)
7668                  (gnus-summary-next-unread-article)))))
7669     (gnus-summary-recenter)
7670     (gnus-summary-position-cursor)))
7671
7672 (defun gnus-summary-prev-page (lines)
7673   "Show previous page of selected article.
7674 Argument LINES specifies lines to be scrolled down."
7675   (interactive "P")
7676   (gnus-set-global-variables)
7677   (let ((article (gnus-summary-article-number)))
7678     (gnus-configure-windows 'article)
7679     (if (or (null gnus-current-article)
7680             (null gnus-article-current)
7681             (/= article (cdr gnus-article-current))
7682             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7683         ;; Selected subject is different from current article's.
7684         (gnus-summary-display-article article)
7685       (gnus-summary-recenter)
7686       (gnus-eval-in-buffer-window gnus-article-buffer
7687         (gnus-article-prev-page lines))))
7688   (gnus-summary-position-cursor))
7689
7690 (defun gnus-summary-scroll-up (lines)
7691   "Scroll up (or down) one line current article.
7692 Argument LINES specifies lines to be scrolled up (or down if negative)."
7693   (interactive "p")
7694   (gnus-set-global-variables)
7695   (gnus-configure-windows 'article)
7696   (or (gnus-summary-select-article nil nil 'pseudo)
7697       (gnus-eval-in-buffer-window 
7698        gnus-article-buffer
7699        (cond ((> lines 0)
7700               (if (gnus-article-next-page lines)
7701                   (gnus-message 3 "End of message")))
7702              ((< lines 0)
7703               (gnus-article-prev-page (- lines))))))
7704   (gnus-summary-recenter)
7705   (gnus-summary-position-cursor))
7706
7707 (defun gnus-summary-next-same-subject ()
7708   "Select next article which has the same subject as current one."
7709   (interactive)
7710   (gnus-set-global-variables)
7711   (gnus-summary-next-article nil (gnus-summary-subject-string)))
7712
7713 (defun gnus-summary-prev-same-subject ()
7714   "Select previous article which has the same subject as current one."
7715   (interactive)
7716   (gnus-set-global-variables)
7717   (gnus-summary-prev-article nil (gnus-summary-subject-string)))
7718
7719 (defun gnus-summary-next-unread-same-subject ()
7720   "Select next unread article which has the same subject as current one."
7721   (interactive)
7722   (gnus-set-global-variables)
7723   (gnus-summary-next-article t (gnus-summary-subject-string)))
7724
7725 (defun gnus-summary-prev-unread-same-subject ()
7726   "Select previous unread article which has the same subject as current one."
7727   (interactive)
7728   (gnus-set-global-variables)
7729   (gnus-summary-prev-article t (gnus-summary-subject-string)))
7730
7731 (defun gnus-summary-first-unread-article ()
7732   "Select the first unread article. 
7733 Return nil if there are no unread articles."
7734   (interactive)
7735   (gnus-set-global-variables)
7736   (prog1
7737       (if (gnus-summary-first-subject t)
7738           (gnus-summary-display-article (gnus-summary-article-number)))
7739     (gnus-summary-position-cursor)))
7740
7741 (defun gnus-summary-best-unread-article ()
7742   "Select the unread article with the highest score."
7743   (interactive)
7744   (gnus-set-global-variables)
7745   (let ((scored gnus-newsgroup-scored)
7746         (best -1000000)
7747         article art)
7748     (while scored
7749       (or (> best (cdr (car scored)))
7750           (and (memq (setq art (car (car scored))) gnus-newsgroup-unreads)
7751                (not (memq art gnus-newsgroup-marked))
7752                (not (memq art gnus-newsgroup-dormant))
7753                (if (= best (cdr (car scored)))
7754                    (setq article (min art article))
7755                  (setq article art)
7756                  (setq best (cdr (car scored))))))
7757       (setq scored (cdr scored)))
7758     (if article 
7759         (gnus-summary-goto-article article)
7760       (gnus-summary-first-unread-article))
7761     (gnus-summary-position-cursor)))
7762
7763 (defun gnus-summary-goto-article (article &optional all-headers)
7764   "Fetch ARTICLE and display it if it exists.
7765 If ALL-HEADERS is non-nil, no header lines are hidden."
7766   (interactive
7767    (list
7768     (string-to-int
7769      (completing-read 
7770       "Article number: "
7771       (mapcar (lambda (headers) (list (int-to-string (header-number headers))))
7772               gnus-newsgroup-headers) 
7773       nil 'require-match))))
7774   (prog1
7775       (and (gnus-summary-goto-subject article)
7776            (gnus-summary-display-article article all-headers))
7777     (gnus-summary-position-cursor)))
7778
7779 (defun gnus-summary-goto-last-article ()
7780   "Go to the last article."
7781   (interactive)
7782   (prog1
7783       (and gnus-last-article
7784            (gnus-summary-goto-article gnus-last-article))
7785     (gnus-summary-position-cursor)))
7786
7787 (defun gnus-summary-pop-article (number)
7788   "Pop one article off the history and go to the previous.
7789 NUMBER articles will be popped off."
7790   (interactive "p")
7791   (let (to)
7792     (setq gnus-newsgroup-history
7793           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7794     (if to
7795         (gnus-summary-goto-article (car to))
7796       (error "Article history empty")))
7797   (gnus-summary-position-cursor))
7798
7799 ;; Summary article oriented commands
7800
7801 (defun gnus-summary-refer-parent-article (n)
7802   "Refer parent article N times.
7803 The difference between N and the number of articles fetched is returned."
7804   (interactive "p")
7805   (gnus-set-global-variables)
7806   (while 
7807       (and 
7808        (> n 0)
7809        (let ((ref (header-references (gnus-get-header-by-number
7810                                       (gnus-summary-article-number)))))
7811          (if (and ref (not (equal ref ""))
7812                   (string-match "<[^<>]*>[ \t]*$" ref))
7813              (gnus-summary-refer-article 
7814               (substring ref (match-beginning 0) (match-end 0))))))
7815     (setq n (1- n)))
7816   (or (zerop n) 
7817       (gnus-message 1 "No references in article or expired article."))
7818   (gnus-summary-position-cursor)
7819   n)
7820     
7821 (defun gnus-summary-refer-article (message-id)
7822   "Refer article specified by MESSAGE-ID.
7823 NOTE: This command only works with newsgroups that use real or simulated NNTP."
7824   (interactive "sMessage-ID: ")
7825   (if (or (not (stringp message-id))
7826           (zerop (length message-id)))
7827       ()
7828     ;; Construct the correct Message-ID if necessary.
7829     ;; Suggested by tale@pawl.rpi.edu.
7830     (or (string-match "^<" message-id)
7831         (setq message-id (concat "<" message-id)))
7832     (or (string-match ">$" message-id)
7833         (setq message-id (concat message-id ">")))
7834     (let ((header (car (gnus-gethash (downcase message-id)
7835                                      gnus-newsgroup-dependencies))))
7836       (if header
7837           (or (gnus-summary-goto-article (header-number header))
7838               ;; The header has been read, but the article had been
7839               ;; expunged, so we insert it again.
7840               (progn
7841                 (gnus-summary-insert-line
7842                  nil header 0 nil gnus-read-mark nil nil
7843                  (header-subject header))
7844                 (forward-line -1)
7845                 (header-number header)))
7846         (let ((gnus-override-method gnus-refer-article-method)
7847               (gnus-ancient-mark gnus-read-mark)
7848               number)
7849           (and gnus-refer-article-method
7850                (or (gnus-server-opened gnus-refer-article-method)
7851                    (gnus-open-server gnus-refer-article-method)))
7852           (if (gnus-article-prepare 
7853                message-id nil (gnus-read-header message-id))
7854               (progn
7855                 (setq number (header-number gnus-current-headers))
7856                 (gnus-rebuild-thread message-id)
7857                 (gnus-summary-goto-subject number)
7858                 (gnus-article-set-window-start 
7859                  (cdr (assq number gnus-newsgroup-bookmarks)))
7860                 message-id)
7861             (gnus-message 1 "No such references")
7862             nil))))))
7863
7864 (defun gnus-summary-enter-digest-group ()
7865   "Enter a digest group based on the current article."
7866   (interactive)
7867   (gnus-set-global-variables)
7868   (gnus-summary-select-article)
7869   ;; We do not want a narrowed article.
7870   (gnus-summary-stop-page-breaking)
7871   (let ((name (format "%s-%d" 
7872                       (gnus-group-prefixed-name 
7873                        gnus-newsgroup-name (list 'nndoc "")) 
7874                       gnus-current-article))
7875         (buf (current-buffer)))
7876     (if (gnus-group-read-ephemeral-group 
7877          name (list 'nndoc name
7878                     (list 'nndoc-address (get-buffer gnus-article-buffer))
7879                     '(nndoc-article-type digest))
7880          t)
7881         ()
7882       (switch-to-buffer buf)
7883       (gnus-set-global-variables)
7884       (gnus-configure-windows 'summary)
7885       (gnus-message 3 "Article not a digest?"))))
7886
7887 (defun gnus-summary-isearch-article ()
7888   "Do incremental search forward on current article."
7889   (interactive)
7890   (gnus-set-global-variables)
7891   (gnus-summary-select-article)
7892   (gnus-eval-in-buffer-window 
7893    gnus-article-buffer (isearch-forward)))
7894
7895 (defun gnus-summary-search-article-forward (regexp)
7896   "Search for an article containing REGEXP forward.
7897 gnus-select-article-hook is not called during the search."
7898   (interactive
7899    (list (read-string
7900           (concat "Search forward (regexp): "
7901                   (if gnus-last-search-regexp
7902                       (concat "(default " gnus-last-search-regexp ") "))))))
7903   (gnus-set-global-variables)
7904   (if (string-equal regexp "")
7905       (setq regexp (or gnus-last-search-regexp ""))
7906     (setq gnus-last-search-regexp regexp))
7907   (if (gnus-summary-search-article regexp nil)
7908       (gnus-eval-in-buffer-window 
7909        gnus-article-buffer
7910        (recenter 0))
7911     (error "Search failed: \"%s\"" regexp)))
7912
7913 (defun gnus-summary-search-article-backward (regexp)
7914   "Search for an article containing REGEXP backward.
7915 gnus-select-article-hook is not called during the search."
7916   (interactive
7917    (list (read-string
7918           (concat "Search backward (regexp): "
7919                   (if gnus-last-search-regexp
7920                       (concat "(default " gnus-last-search-regexp ") "))))))
7921   (gnus-set-global-variables)
7922   (if (string-equal regexp "")
7923       (setq regexp (or gnus-last-search-regexp ""))
7924     (setq gnus-last-search-regexp regexp))
7925   (if (gnus-summary-search-article regexp t)
7926       (gnus-eval-in-buffer-window
7927        gnus-article-buffer
7928        (recenter 0))
7929     (error "Search failed: \"%s\"" regexp)))
7930
7931 (defun gnus-summary-search-article (regexp &optional backward)
7932   "Search for an article containing REGEXP.
7933 Optional argument BACKWARD means do search for backward.
7934 gnus-select-article-hook is not called during the search."
7935   (let ((gnus-select-article-hook nil)  ;Disable hook.
7936         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7937         (re-search
7938          (if backward
7939              (function re-search-backward) (function re-search-forward)))
7940         (found nil)
7941         (last nil))
7942     ;; Hidden thread subtrees must be searched for ,too.
7943     (gnus-summary-show-all-threads)
7944     (if (eobp) (forward-line -1))
7945     ;; First of all, search current article.
7946     ;; We don't want to read article again from NNTP server nor reset
7947     ;; current point.
7948     (gnus-summary-select-article)
7949     (gnus-message 9 "Searching article: %d..." gnus-current-article)
7950     (setq last gnus-current-article)
7951     (gnus-eval-in-buffer-window gnus-article-buffer
7952       (save-restriction
7953         (widen)
7954         ;; Begin search from current point.
7955         (setq found (funcall re-search regexp nil t))))
7956     ;; Then search next articles.
7957     (while (and (not found)
7958                 (gnus-summary-display-article 
7959                  (gnus-summary-search-subject backward nil nil)))
7960       (gnus-message 9 "Searching article: %d..." gnus-current-article)
7961       (gnus-eval-in-buffer-window gnus-article-buffer
7962         (save-restriction
7963           (widen)
7964           (goto-char (if backward (point-max) (point-min)))
7965           (setq found (funcall re-search regexp nil t)))))
7966     (message "")
7967     ;; Adjust article pointer.
7968     (or (eq last gnus-current-article)
7969         (setq gnus-last-article last))
7970     ;; Return T if found such article.
7971     found))
7972
7973 (defun gnus-summary-execute-command (header regexp command &optional backward)
7974   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7975 If HEADER is an empty string (or nil), the match is done on the entire
7976 article. If BACKWARD (the prefix) is non-nil, search backward instead."
7977   (interactive
7978    (list (let ((completion-ignore-case t))
7979            (completing-read 
7980             "Header name: "
7981             (mapcar (lambda (string) (list string))
7982                     '("Number" "Subject" "From" "Lines" "Date"
7983                       "Message-ID" "Xref" "References"))
7984             nil 'require-match))
7985          (read-string "Regexp: ")
7986          (read-key-sequence "Command: ")
7987          current-prefix-arg))
7988   (gnus-set-global-variables)
7989   ;; Hidden thread subtrees must be searched as well.
7990   (gnus-summary-show-all-threads)
7991   ;; We don't want to change current point nor window configuration.
7992   (save-excursion
7993     (save-window-excursion
7994       (gnus-message 6 "Executing %s..." (key-description command))
7995       ;; We'd like to execute COMMAND interactively so as to give arguments.
7996       (gnus-execute header regexp
7997                     (` (lambda ()
7998                          (call-interactively '(, (key-binding command)))))
7999                     backward)
8000       (gnus-message 6 "Executing %s...done" (key-description command)))))
8001
8002 (defun gnus-summary-beginning-of-article ()
8003   "Scroll the article back to the beginning."
8004   (interactive)
8005   (gnus-set-global-variables)
8006   (gnus-summary-select-article)
8007   (gnus-eval-in-buffer-window
8008    gnus-article-buffer
8009    (widen)
8010    (goto-char (point-min))
8011    (and gnus-break-pages (gnus-narrow-to-page))))
8012
8013 (defun gnus-summary-end-of-article ()
8014   "Scroll to the end of the article."
8015   (interactive)
8016   (gnus-set-global-variables)
8017   (gnus-summary-select-article)
8018   (gnus-eval-in-buffer-window 
8019    gnus-article-buffer
8020    (widen)
8021    (goto-char (point-max))
8022    (and gnus-break-pages (gnus-narrow-to-page))))
8023
8024 (defun gnus-summary-show-article ()
8025   "Force re-fetching of the current article."
8026   (interactive)
8027   (gnus-set-global-variables)
8028   (gnus-summary-select-article gnus-have-all-headers t))
8029
8030 (defun gnus-summary-toggle-header (arg)
8031   "Show the headers if they are hidden, or hide them if they are shown.
8032 If ARG is a positive number, show the entire header.
8033 If ARG is a negative number, hide the unwanted header lines."
8034   (interactive "P")
8035   (gnus-set-global-variables)
8036   (save-excursion
8037     (set-buffer gnus-article-buffer)
8038     (let ((buffer-read-only nil))
8039       (if (numberp arg) 
8040           (if (> arg 0) (remove-text-properties (point-min) (point-max) 
8041                                                 gnus-hidden-properties)
8042             (if (< arg 0) (run-hooks 'gnus-article-display-hook)))
8043         (if (text-property-any (point-min) (point-max) 'invisible t)
8044             (remove-text-properties (point-min) (point-max)
8045                                     gnus-hidden-properties)
8046           (let ((gnus-have-all-headers nil))
8047             (run-hooks 'gnus-article-display-hook))))
8048       (set-window-point (get-buffer-window (current-buffer)) (point-min)))))
8049
8050 (defun gnus-summary-show-all-headers ()
8051   "Make all header lines visible."
8052   (interactive)
8053   (gnus-set-global-variables)
8054   (gnus-article-show-all-headers))
8055
8056 (defun gnus-summary-toggle-mime (arg)
8057   "Toggle MIME processing.
8058 If ARG is a positive number, turn MIME processing on."
8059   (interactive "P")
8060   (gnus-set-global-variables)
8061   (setq gnus-show-mime
8062         (if (null arg) (not gnus-show-mime)
8063           (> (prefix-numeric-value arg) 0)))
8064   (gnus-summary-select-article t 'force))
8065
8066 (defun gnus-summary-caesar-message (rotnum)
8067   "Caesar rotates all letters of current message by 13/47 places.
8068 With prefix arg, specifies the number of places to rotate each letter forward.
8069 Caesar rotates Japanese letters by 47 places in any case."
8070   (interactive "P")
8071   (gnus-set-global-variables)
8072   (gnus-summary-select-article)
8073   (let ((mail-header-separator "")) ; !!! Is this necessary?
8074     (gnus-overload-functions)
8075     (gnus-eval-in-buffer-window 
8076      gnus-article-buffer
8077      (save-restriction
8078        (widen)
8079        ;; We don't want to jump to the beginning of the message.
8080        ;; `save-excursion' does not do its job.
8081        (move-to-window-line 0)
8082        (let ((last (point)))
8083          (news-caesar-buffer-body rotnum)
8084          (goto-char last)
8085          (recenter 0))))))
8086
8087 (defun gnus-summary-stop-page-breaking ()
8088   "Stop page breaking in the current article."
8089   (interactive)
8090   (gnus-set-global-variables)
8091   (gnus-summary-select-article)
8092   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
8093
8094 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
8095
8096 (defun gnus-summary-move-article (n &optional to-newsgroup select-method)
8097   "Move the current article to a different newsgroup.
8098 If N is a positive number, move the N next articles.
8099 If N is a negative number, move the N previous articles.
8100 If N is nil and any articles have been marked with the process mark,
8101 move those articles instead.
8102 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8103 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8104 re-spool using this method.
8105 For this function to work, both the current newsgroup and the
8106 newsgroup that you want to move to have to support the `request-move'
8107 and `request-accept' functions. (Ie. mail newsgroups at present.)"
8108   (interactive "P")
8109   (gnus-set-global-variables)
8110   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
8111       (error "The current newsgroup does not support article moving"))
8112   (let ((articles (gnus-summary-work-articles n))
8113         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8114         art-group)
8115     (if (and (not to-newsgroup) (not select-method))
8116         (setq to-newsgroup
8117               (completing-read 
8118                (format "Where do you want to move %s? %s"
8119                        (if (> (length articles) 1)
8120                            (format "these %d articles" (length articles))
8121                          "this article")
8122                        (if gnus-current-move-group
8123                            (format "(%s default) " gnus-current-move-group)
8124                          ""))
8125                gnus-active-hashtb nil nil prefix)))
8126     (if to-newsgroup
8127         (progn
8128           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8129               (setq to-newsgroup (or gnus-current-move-group "")))
8130           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8131               (gnus-activate-newsgroup to-newsgroup)
8132               (error "No such group: %s" to-newsgroup))
8133           (setq gnus-current-move-group to-newsgroup)))
8134     (or (gnus-check-backend-function 'request-accept-article 
8135                                      (or select-method to-newsgroup))
8136         (error "%s does not support article moving" to-newsgroup))
8137     (gnus-message 6 "Moving to %s: %s..." 
8138                   (or select-method to-newsgroup) articles)
8139     (while articles
8140       (if (setq art-group
8141                 (gnus-request-move-article 
8142                  (car articles)                   ; Article to move
8143                  gnus-newsgroup-name              ; From newsgrouo
8144                  (nth 1 (gnus-find-method-for-group 
8145                          gnus-newsgroup-name))    ; Server
8146                  (list 'gnus-request-accept-article 
8147                        (if select-method
8148                            (list 'quote select-method)
8149                          to-newsgroup)
8150                        (not (cdr articles)))     ; Accept form
8151                  (not (cdr articles))))          ; Only save nov last time
8152           (let* ((buffer-read-only nil)
8153                  (entry 
8154                   (or
8155                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8156                    (gnus-gethash 
8157                     (gnus-group-prefixed-name 
8158                      (car art-group) 
8159                      (if select-method (list select-method "")
8160                        (gnus-find-method-for-group to-newsgroup)))
8161                     gnus-newsrc-hashtb)))
8162                  (info (nth 2 entry))
8163                  (article (car articles))
8164                  (marked (nth 3 info)))
8165             (gnus-summary-goto-subject article)
8166             (beginning-of-line)
8167             (delete-region (point)
8168                            (progn (forward-line 1) (point)))
8169             (if (not (memq article gnus-newsgroup-unreads))
8170                 (setcar (cdr (cdr info))
8171                         (gnus-add-to-range (nth 2 info) 
8172                                            (list (cdr art-group)))))
8173             ;; Copy any marks over to the new group.
8174             (let ((marks '((tick . gnus-newsgroup-marked)
8175                            (dormant . gnus-newsgroup-dormant)
8176                            (expire . gnus-newsgroup-expirable)
8177                            (bookmark . gnus-newsgroup-bookmarks)
8178                         ;   (score . gnus-newsgroup-scored)
8179                            (reply . gnus-newsgroup-replied)))
8180                   (to-article (cdr art-group)))
8181               (while marks
8182                 (if (memq article (symbol-value (cdr (car marks))))
8183                     (gnus-add-marked-articles 
8184                      (car info) (car (car marks)) (list to-article) info))
8185                 (setq marks (cdr marks))))
8186             (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8187             (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8188             (setq gnus-newsgroup-dormant
8189                   (delq article gnus-newsgroup-dormant)))
8190         (gnus-message 1 "Couldn't move article %s" (car articles)))
8191       (gnus-summary-remove-process-mark (car articles))
8192       (setq articles (cdr articles)))))
8193
8194 (defun gnus-summary-respool-article (n &optional respool-method)
8195   "Respool the current article.
8196 The article will be squeezed through the mail spooling process again,
8197 which means that it will be put in some mail newsgroup or other
8198 depending on `nnmail-split-methods'.
8199 If N is a positive number, respool the N next articles.
8200 If N is a negative number, respool the N previous articles.
8201 If N is nil and any articles have been marked with the process mark,
8202 respool those articles instead.
8203
8204 Respooling can be done both from mail groups and \"real\" newsgroups.
8205 In the former case, the articles in question will be moved from the
8206 current group into whatever groups they are destined to.  In the
8207 latter case, they will be copied into the relevant groups."
8208   (interactive "P")
8209   (gnus-set-global-variables)
8210   (let ((respool-methods (gnus-methods-using 'respool))
8211         (methname 
8212          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
8213     (or respool-method
8214         (setq respool-method
8215               (completing-read
8216                "What method do you want to use when respooling? "
8217                respool-methods nil t methname)))
8218     (or (string= respool-method "")
8219         (if (assoc (symbol-name
8220                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
8221                    respool-methods)
8222             (gnus-summary-move-article n nil (intern respool-method))
8223           (gnus-summary-copy-article n nil (intern respool-method))))))
8224
8225 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
8226 (defun gnus-summary-copy-article (n &optional to-newsgroup select-method)
8227   "Move the current article to a different newsgroup.
8228 If N is a positive number, move the N next articles.
8229 If N is a negative number, move the N previous articles.
8230 If N is nil and any articles have been marked with the process mark,
8231 move those articles instead.
8232 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8233 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8234 re-spool using this method.
8235 For this function to work, the newsgroup that you want to move to have
8236 to support the `request-move' and `request-accept'
8237 functions. (Ie. mail newsgroups at present.)"
8238   (interactive "P")
8239   (gnus-set-global-variables)
8240   (let ((articles (gnus-summary-work-articles n))
8241         (copy-buf (get-buffer-create "*copy work*"))
8242         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8243         art-group)
8244     (buffer-disable-undo copy-buf)
8245     (if (and (not to-newsgroup) (not select-method))
8246         (setq to-newsgroup
8247               (completing-read 
8248                (format "Where do you want to copy %s? %s"
8249                        (if (> (length articles) 1)
8250                            (format "these %d articles" (length articles))
8251                          "this article")
8252                        (if gnus-current-move-group
8253                            (format "(%s default) " gnus-current-move-group)
8254                          ""))
8255                gnus-active-hashtb nil nil prefix)))
8256     (if to-newsgroup
8257         (progn
8258           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8259               (setq to-newsgroup (or gnus-current-move-group "")))
8260           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8261               (gnus-activate-newsgroup to-newsgroup)
8262               (error "No such group: %s" to-newsgroup))
8263           (setq gnus-current-move-group to-newsgroup)))
8264     (or (gnus-check-backend-function 'request-accept-article 
8265                                      (or select-method to-newsgroup))
8266         (error "%s does not support article copying" to-newsgroup))
8267     (gnus-message 6 "Copying to %s: %s..." 
8268                   (or select-method to-newsgroup) articles)
8269     (while articles
8270       (if (setq art-group
8271                 (save-excursion
8272                   (set-buffer copy-buf)
8273                   (gnus-request-article-this-buffer
8274                    (car articles) gnus-newsgroup-name)
8275                   (gnus-request-accept-article
8276                    (if select-method (quote select-method) to-newsgroup)
8277                    (not (cdr articles)))))
8278           (let* ((entry 
8279                   (or
8280                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8281                    (gnus-gethash 
8282                     (gnus-group-prefixed-name 
8283                      (car art-group) 
8284                      (if select-method (list select-method "")
8285                        (gnus-find-method-for-group to-newsgroup)))
8286                     gnus-newsrc-hashtb)))
8287                  (info (nth 2 entry))
8288                  (article (car articles))
8289                  (marked (nth 3 info)))
8290             (if (not (memq article gnus-newsgroup-unreads))
8291                 (setcar (cdr (cdr info))
8292                         (gnus-add-to-range (nth 2 info) 
8293                                            (list (cdr art-group)))))
8294             ;; Copy any marks over to the new group.
8295             (let ((marks '((tick . gnus-newsgroup-marked)
8296                            (dormant . gnus-newsgroup-dormant)
8297                            (expire . gnus-newsgroup-expirable)
8298                            (bookmark . gnus-newsgroup-bookmarks)
8299                         ;   (score . gnus-newsgroup-scored)
8300                            (reply . gnus-newsgroup-replied)))
8301                   (to-article (cdr art-group)))
8302               (while marks
8303                 (if (memq article (symbol-value (cdr (car marks))))
8304                     (gnus-add-marked-articles 
8305                      (car info) (car (car marks)) (list to-article) info))
8306                 (setq marks (cdr marks)))))
8307         (gnus-message 1 "Couldn't copy article %s" (car articles)))
8308       (gnus-summary-remove-process-mark (car articles))
8309       (setq articles (cdr articles)))
8310     (kill-buffer copy-buf)))
8311
8312 (defun gnus-summary-import-article (file)
8313   "Import a random file into a mail newsgroup."
8314   (interactive "fImport file: ")
8315   (let ((group gnus-newsgroup-name)
8316         atts)
8317     (or (gnus-check-backend-function 'request-accept-article group)
8318         (error "%s does not support article importing" group))
8319     (or (file-readable-p file)
8320         (not (file-regular-p file))
8321         (error "Can't read %s" file))
8322     (save-excursion
8323       (set-buffer (get-buffer-create " *import file*"))
8324       (buffer-disable-undo (current-buffer))
8325       (erase-buffer)
8326       (insert-file-contents file)
8327       (goto-char (point-min))
8328       (if (nnheader-article-p)
8329           ()
8330         (setq atts (file-attributes file))
8331         (insert "From: " (read-string "From: ") "\n"
8332                 "Subject: " (read-string "Subject: ") "\n"
8333                 "Date: " (current-time-string (nth 5 atts)) "\n"
8334                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8335       (gnus-request-accept-article group t)
8336       (kill-buffer (current-buffer)))))
8337
8338 (defun gnus-summary-expire-articles ()
8339   "Expire all articles that are marked as expirable in the current group."
8340   (interactive)
8341   (if (and gnus-newsgroup-expirable
8342            (gnus-check-backend-function 
8343             'request-expire-articles gnus-newsgroup-name))
8344       (let ((expirable gnus-newsgroup-expirable))
8345         ;; The list of articles that weren't expired is returned.
8346         (setq gnus-newsgroup-expirable 
8347               (gnus-request-expire-articles gnus-newsgroup-expirable
8348                                             gnus-newsgroup-name))
8349         ;; We go through the old list of expirable, and mark all
8350         ;; really expired articles as non-existant.
8351         (while expirable
8352           (or (memq (car expirable) gnus-newsgroup-expirable)
8353               (gnus-summary-mark-as-read (car expirable) gnus-canceled-mark))
8354           (setq expirable (cdr expirable))))))
8355
8356 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8357 (defun gnus-summary-delete-article (n)
8358   "Delete the N next (mail) articles.
8359 This command actually deletes articles. This is not a marking
8360 command. The article will disappear forever from you life, never to
8361 return. 
8362 If N is negative, delete backwards.
8363 If N is nil and articles have been marked with the process mark,
8364 delete these instead."
8365   (interactive "P")
8366   (or (gnus-check-backend-function 'request-expire-articles 
8367                                    gnus-newsgroup-name)
8368       (error "The current newsgroup does not support article deletion."))
8369   ;; Compute the list of articles to delete.
8370   (let ((articles (gnus-summary-work-articles n))
8371         not-deleted)
8372     (if (and gnus-novice-user
8373              (not (gnus-y-or-n-p 
8374                    (format "Do you really want to delete %s forever? "
8375                            (if (> (length articles) 1) "these articles"
8376                              "this article")))))
8377         ()
8378       ;; Delete the articles.
8379       (setq not-deleted (gnus-request-expire-articles 
8380                          articles gnus-newsgroup-name 'force))
8381       (while articles
8382         (gnus-summary-remove-process-mark (car articles))       
8383         ;; The backend might not have been able to delete the article
8384         ;; after all.  
8385         (or (memq (car articles) not-deleted)
8386             (gnus-summary-mark-as-read (car articles) gnus-canceled-mark))
8387         (setq articles (cdr articles))))
8388     (gnus-summary-position-cursor)
8389     not-deleted))
8390
8391 (defun gnus-summary-edit-article ()
8392   "Enter into a buffer and edit the current article.
8393 This will have permanent effect only in mail groups."
8394   (interactive)
8395   (or (gnus-check-backend-function 
8396        'request-replace-article gnus-newsgroup-name)
8397       (error "The current newsgroup does not support article editing."))
8398   (gnus-summary-select-article t)
8399   (other-window 1)
8400   (gnus-message 6 "C-c C-c to end edits")
8401   (setq buffer-read-only nil)
8402   (text-mode)
8403   (use-local-map (copy-keymap (current-local-map)))
8404   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
8405   (goto-char (point-min))
8406   (search-forward "\n\n" nil t))
8407
8408 (defun gnus-summary-edit-article-done ()
8409   "Make edits to the current article permanent."
8410   (interactive)
8411   (if (not (gnus-request-replace-article 
8412             (cdr gnus-article-current) (car gnus-article-current) 
8413             (current-buffer)))
8414       (error "Couldn't replace article.")
8415     (gnus-article-mode)
8416     (use-local-map gnus-article-mode-map)
8417     (setq buffer-read-only t)
8418     (pop-to-buffer gnus-summary-buffer)))      
8419
8420 (defun gnus-summary-fancy-query ()
8421   "Query where the fancy respool algorithm would put this article."
8422   (interactive)
8423   (gnus-summary-select-article)
8424   (save-excursion
8425     (set-buffer gnus-article-buffer)
8426     (save-restriction
8427       (goto-char (point-min))
8428       (search-forward "\n\n")
8429       (narrow-to-region (point-min) (point))
8430       (pp-eval-expression (list 'quote (nnmail-split-fancy))))))
8431
8432 ;; Summary score commands.
8433
8434 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
8435
8436 (defun gnus-summary-raise-score (n)
8437   "Raise the score of the current article by N."
8438   (interactive "p")
8439   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
8440
8441 (defun gnus-summary-lower-score (n)
8442   "Lower the score of the current article by N."
8443   (interactive "p")
8444   (gnus-summary-raise-score (- n)))
8445
8446 (defun gnus-summary-set-score (n)
8447   "Set the score of the current article to N."
8448   (interactive "p")
8449   ;; Skip dummy header line.
8450   (save-excursion
8451     (gnus-summary-show-thread)
8452     (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
8453         (forward-line 1))
8454     (let ((buffer-read-only nil))
8455       ;; Set score.
8456       (gnus-summary-update-mark
8457        (if (= n (or gnus-summary-default-score 0)) ? 
8458          (if (< n (or gnus-summary-default-score 0)) 
8459              gnus-score-below-mark gnus-score-over-mark)) 'score))
8460     (let* ((article (gnus-summary-article-number))
8461            (score (assq article gnus-newsgroup-scored)))
8462       (if score (setcdr score n)
8463         (setq gnus-newsgroup-scored 
8464               (cons (cons article n) gnus-newsgroup-scored))))
8465     (gnus-summary-update-line)))
8466
8467 (defun gnus-summary-current-score ()
8468   "Return the score of the current article."
8469   (interactive)
8470   (message "%s" (gnus-summary-article-score)))
8471
8472 ;; Summary marking commands.
8473
8474 (defun gnus-summary-raise-same-subject-and-select (score)
8475   "Raise articles which has the same subject with SCORE and select the next."
8476   (interactive "p")
8477   (let ((subject (gnus-summary-subject-string)))
8478     (gnus-summary-raise-score score)
8479     (while (gnus-summary-search-subject nil nil subject)
8480       (gnus-summary-raise-score score))
8481     (gnus-summary-next-article t)))
8482
8483 (defun gnus-summary-raise-same-subject (score)
8484   "Raise articles which has the same subject with SCORE."
8485   (interactive "p")
8486   (let ((subject (gnus-summary-subject-string)))
8487     (gnus-summary-raise-score score)
8488     (while (gnus-summary-search-subject nil nil subject)
8489       (gnus-summary-raise-score score))
8490     (gnus-summary-next-subject 1 t)))
8491
8492 (defun gnus-score-default (level)
8493   (if level (prefix-numeric-value level) 
8494     gnus-score-interactive-default-score))
8495
8496 (defun gnus-summary-raise-thread (score)
8497   "Raise articles under current thread with SCORE."
8498   (interactive "P")
8499   (setq score (1- (gnus-score-default score)))
8500   (let (e)
8501     (save-excursion
8502       (let ((level (gnus-summary-thread-level)))
8503         (gnus-summary-raise-score score)
8504         (while (and (zerop (gnus-summary-next-subject 1 nil t))
8505                     (> (gnus-summary-thread-level) level))
8506           (gnus-summary-raise-score score))
8507         (setq e (point))))
8508     (let ((gnus-summary-check-current t))
8509       (or (zerop (gnus-summary-next-subject 1 t))
8510           (goto-char e))))
8511   (gnus-summary-recenter)
8512   (gnus-summary-position-cursor)
8513   (gnus-set-mode-line 'summary))
8514
8515 (defun gnus-summary-lower-same-subject-and-select (score)
8516   "Raise articles which has the same subject with SCORE and select the next."
8517   (interactive "p")
8518   (gnus-summary-raise-same-subject-and-select (- score)))
8519
8520 (defun gnus-summary-lower-same-subject (score)
8521   "Raise articles which has the same subject with SCORE."
8522   (interactive "p")
8523   (gnus-summary-raise-same-subject (- score)))
8524
8525 (defun gnus-summary-lower-thread (score)
8526   "Raise articles under current thread with SCORE."
8527   (interactive "P")
8528   (gnus-summary-raise-thread (- (1- (gnus-score-default score)))))
8529
8530 (defun gnus-summary-kill-same-subject-and-select (unmark)
8531   "Mark articles which has the same subject as read, and then select the next.
8532 If UNMARK is positive, remove any kind of mark.
8533 If UNMARK is negative, tick articles."
8534   (interactive "P")
8535   (if unmark
8536       (setq unmark (prefix-numeric-value unmark)))
8537   (let ((count
8538          (gnus-summary-mark-same-subject
8539           (gnus-summary-subject-string) unmark)))
8540     ;; Select next unread article. If auto-select-same mode, should
8541     ;; select the first unread article.
8542     (gnus-summary-next-article t (and gnus-auto-select-same
8543                                       (gnus-summary-subject-string)))
8544     (gnus-message 7 "%d articles are marked as %s"
8545                   count (if unmark "unread" "read"))))
8546
8547 (defun gnus-summary-kill-same-subject (unmark)
8548   "Mark articles which has the same subject as read. 
8549 If UNMARK is positive, remove any kind of mark.
8550 If UNMARK is negative, tick articles."
8551   (interactive "P")
8552   (if unmark
8553       (setq unmark (prefix-numeric-value unmark)))
8554   (let ((count
8555          (gnus-summary-mark-same-subject
8556           (gnus-summary-subject-string) unmark)))
8557     ;; If marked as read, go to next unread subject.
8558     (if (null unmark)
8559         ;; Go to next unread subject.
8560         (gnus-summary-next-subject 1 t))
8561     (gnus-message 7 "%d articles are marked as %s"
8562                   count (if unmark "unread" "read"))))
8563
8564 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8565   "Mark articles with same SUBJECT as read, and return marked number.
8566 If optional argument UNMARK is positive, remove any kinds of marks.
8567 If optional argument UNMARK is negative, mark articles as unread instead."
8568   (let ((count 1))
8569     (save-excursion
8570       (cond ((null unmark)
8571              (gnus-summary-mark-as-read nil gnus-killed-mark))
8572             ((> unmark 0)
8573              (gnus-summary-tick-article nil t))
8574             (t
8575              (gnus-summary-tick-article)))
8576       (while (and subject
8577                   (gnus-summary-search-forward nil subject))
8578         (cond ((null unmark)
8579                (gnus-summary-mark-as-read nil gnus-killed-mark))
8580               ((> unmark 0)
8581                (gnus-summary-tick-article nil t))
8582               (t
8583                (gnus-summary-tick-article)))
8584         (setq count (1+ count))))
8585     ;; Hide killed thread subtrees.  Does not work properly always.
8586     ;;(and (null unmark)
8587     ;;     gnus-thread-hide-killed
8588     ;;     (gnus-summary-hide-thread))
8589     ;; Return number of articles marked as read.
8590     count))
8591
8592 (defun gnus-summary-mark-as-processable (n &optional unmark)
8593   "Set the process mark on the next N articles.
8594 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8595 the process mark instead.  The difference between N and the actual
8596 number of articles marked is returned."
8597   (interactive "p")
8598   (let ((backward (< n 0))
8599         (n (abs n)))
8600   (while (and 
8601           (> n 0)
8602           (if unmark
8603               (gnus-summary-remove-process-mark (gnus-summary-article-number))
8604             (gnus-summary-set-process-mark (gnus-summary-article-number)))
8605           (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8606     (setq n (1- n)))
8607   (if (/= 0 n) (gnus-message 7 "No more articles"))
8608   (gnus-summary-recenter)
8609   (gnus-summary-position-cursor)
8610   n))
8611
8612 (defun gnus-summary-unmark-as-processable (n)
8613   "Remove the process mark from the next N articles.
8614 If N is negative, mark backward instead.  The difference between N and
8615 the actual number of articles marked is returned."
8616   (interactive "p")
8617   (gnus-summary-mark-as-processable n t))
8618
8619 (defun gnus-summary-unmark-all-processable ()
8620   "Remove the process mark from all articles."
8621   (interactive)
8622   (save-excursion
8623     (while gnus-newsgroup-processable
8624       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8625   (gnus-summary-position-cursor))
8626
8627 (defun gnus-summary-mark-as-expirable (n)
8628   "Mark N articles forward as expirable.
8629 If N is negative, mark backward instead. The difference between N and
8630 the actual number of articles marked is returned."
8631   (interactive "p")
8632   (gnus-summary-mark-forward n gnus-expirable-mark))
8633
8634 (defun gnus-summary-mark-article-as-replied (article)
8635   "Mark ARTICLE replied and update the summary line."
8636   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
8637   (let ((buffer-read-only nil))
8638     (if (gnus-summary-goto-subject article)
8639         (progn
8640           (gnus-summary-update-mark gnus-replied-mark 'replied)
8641           t))))
8642
8643 (defun gnus-summary-set-bookmark (article)
8644   "Set a bookmark in current article."
8645   (interactive (list (gnus-summary-article-number)))
8646   (if (or (not (get-buffer gnus-article-buffer))
8647           (not gnus-current-article)
8648           (not gnus-article-current)
8649           (not (equal gnus-newsgroup-name (car gnus-article-current))))
8650       (error "No current article selected"))
8651   ;; Remove old bookmark, if one exists.
8652   (let ((old (assq article gnus-newsgroup-bookmarks)))
8653     (if old (setq gnus-newsgroup-bookmarks 
8654                   (delq old gnus-newsgroup-bookmarks))))
8655   ;; Set the new bookmark, which is on the form 
8656   ;; (article-number . line-number-in-body).
8657   (setq gnus-newsgroup-bookmarks 
8658         (cons 
8659          (cons article 
8660                (save-excursion
8661                  (set-buffer gnus-article-buffer)
8662                  (count-lines
8663                   (min (point)
8664                        (save-excursion
8665                          (goto-char (point-min))
8666                          (search-forward "\n\n" nil t)
8667                          (point)))
8668                   (point))))
8669          gnus-newsgroup-bookmarks))
8670   (gnus-message 6 "A bookmark has been added to the current article."))
8671
8672 (defun gnus-summary-remove-bookmark (article)
8673   "Remove the bookmark from the current article."
8674   (interactive (list (gnus-summary-article-number)))
8675   ;; Remove old bookmark, if one exists.
8676   (let ((old (assq article gnus-newsgroup-bookmarks)))
8677     (if old 
8678         (progn
8679           (setq gnus-newsgroup-bookmarks 
8680                 (delq old gnus-newsgroup-bookmarks))
8681           (gnus-message 6 "Removed bookmark."))
8682       (gnus-message 6 "No bookmark in current article."))))
8683
8684 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8685 (defun gnus-summary-mark-as-dormant (n)
8686   "Mark N articles forward as dormant.
8687 If N is negative, mark backward instead.  The difference between N and
8688 the actual number of articles marked is returned."
8689   (interactive "p")
8690   (gnus-summary-mark-forward n gnus-dormant-mark))
8691
8692 (defun gnus-summary-set-process-mark (article)
8693   "Set the process mark on ARTICLE and update the summary line."
8694   (setq gnus-newsgroup-processable (cons article gnus-newsgroup-processable))
8695   (let ((buffer-read-only nil))
8696     (if (gnus-summary-goto-subject article)
8697         (progn
8698           (gnus-summary-show-thread)
8699           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
8700                (forward-line 1))
8701           (gnus-summary-update-mark gnus-process-mark 'replied)
8702           t))))
8703
8704 (defun gnus-summary-remove-process-mark (article)
8705   "Remove the process mark from ARTICLE and update the summary line."
8706   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8707   (let ((buffer-read-only nil))
8708     (if (gnus-summary-goto-subject article)
8709         (progn
8710           (gnus-summary-show-thread)
8711           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
8712                (forward-line 1))
8713           (gnus-summary-update-mark ?  'replied)
8714           (if (memq article gnus-newsgroup-replied) 
8715               (gnus-summary-update-mark gnus-replied-mark 'replied))
8716           t))))
8717
8718 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8719   "Mark N articles as read forwards.
8720 If N is negative, mark backwards instead.
8721 Mark with MARK. If MARK is ? , ?! or ??, articles will be
8722 marked as unread. 
8723 The difference between N and the actual number of articles marked is
8724 returned."
8725   (interactive "p")
8726   (gnus-set-global-variables)
8727   (let ((backward (< n 0))
8728         (n (abs n))
8729         (mark (or mark gnus-del-mark)))
8730   (while (and (> n 0)
8731               (gnus-summary-mark-article nil mark no-expire)
8732               (zerop (gnus-summary-next-subject 
8733                       (if backward -1 1) gnus-summary-goto-unread t)))
8734     (setq n (1- n)))
8735   (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8736   (gnus-summary-recenter)
8737   (gnus-summary-position-cursor)
8738   (gnus-set-mode-line 'summary)
8739   n))
8740
8741 (defun gnus-summary-mark-article (&optional article mark no-expire)
8742   "Mark ARTICLE with MARK.
8743 MARK can be any character.
8744 Five MARK strings are reserved: ?  (unread), 
8745 ?! (ticked), ?? (dormant), ?D (read), ?E (expirable).
8746 If MARK is nil, then the default character ?D is used.
8747 If ARTICLE is nil, then the article on the current line will be
8748 marked." 
8749   ;; If no mark is given, then we check auto-expiring.
8750   (and (not no-expire)
8751        gnus-newsgroup-auto-expire 
8752        (or (not mark)
8753            (and (numberp mark) (or (= mark gnus-killed-mark)
8754                                    (= mark gnus-del-mark)
8755                                    (= mark gnus-catchup-mark)
8756                                    (= mark gnus-low-score-mark)
8757                                    (= mark gnus-read-mark))))
8758        (setq mark gnus-expirable-mark))
8759   (let* ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark))
8760          (article (or article (gnus-summary-article-number))))
8761     (if (or (= mark gnus-unread-mark) 
8762             (= mark gnus-ticked-mark) 
8763             (= mark gnus-dormant-mark))
8764         (gnus-mark-article-as-unread article mark)
8765       (gnus-mark-article-as-read article mark))
8766
8767     ;; See whether the article is to be put in the cache.
8768     (and gnus-use-cache
8769          (save-excursion
8770            (gnus-cache-possibly-enter-article 
8771             gnus-newsgroup-name article 
8772             (gnus-get-header-by-number article)
8773             (= mark gnus-ticked-mark)
8774             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8775
8776     (if (gnus-summary-goto-subject article)
8777         (let ((buffer-read-only nil))
8778           (gnus-summary-show-thread)
8779           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
8780                (forward-line 1))
8781           ;; Fix the mark.
8782           (gnus-summary-update-mark mark 'unread)
8783           t))))
8784
8785 (defun gnus-summary-update-mark (mark type)
8786   (beginning-of-line)
8787   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8788         plist)
8789     (if (not forward)
8790         ()
8791       (forward-char forward)
8792       (setq plist (text-properties-at (point)))
8793       (delete-char 1)
8794       (insert mark)
8795       (and plist (add-text-properties (1- (point)) (point) plist))
8796       (and (eq type 'unread)
8797            (add-text-properties (1- (point)) (point) (list 'gnus-mark mark)))
8798       (gnus-summary-update-line (eq mark gnus-unread-mark)))))
8799   
8800 (defun gnus-mark-article-as-read (article &optional mark)
8801   "Enter ARTICLE in the pertinent lists and remove it from others."
8802   ;; Make the article expirable.
8803   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark)))
8804     (if (= mark gnus-expirable-mark)
8805         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
8806       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8807     ;; Remove from unread and marked lists.
8808     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8809     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8810     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))))
8811
8812 (defun gnus-mark-article-as-unread (article &optional mark)
8813   "Enter ARTICLE in the pertinent lists and remove it from others."
8814   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-ticked-mark)))
8815     ;; Add to unread list.
8816     (or (memq article gnus-newsgroup-unreads)
8817         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
8818     ;; If CLEAR-MARK is non-nil, the article must be removed from mark
8819     ;; lists.  Otherwise, it must be added to the list.
8820     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8821     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8822     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8823     (if (= mark gnus-ticked-mark)
8824         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
8825     (if (= mark gnus-dormant-mark)
8826         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))))
8827
8828 (defalias 'gnus-summary-mark-as-unread-forward 
8829   'gnus-summary-tick-article-forward)
8830 (make-obsolete 'gnus-summary-mark-as-unread-forward 
8831                'gnus-summary-tick-article-forward)
8832 (defun gnus-summary-tick-article-forward (n)
8833   "Tick N articles forwards.
8834 If N is negative, tick backwards instead.
8835 The difference between N and the number of articles ticked is returned."
8836   (interactive "p")
8837   (gnus-summary-mark-forward n gnus-ticked-mark))
8838
8839 (defalias 'gnus-summary-mark-as-unread-backward 
8840   'gnus-summary-tick-article-backward)
8841 (make-obsolete 'gnus-summary-mark-as-unread-backward 
8842                'gnus-summary-tick-article-backward)
8843 (defun gnus-summary-tick-article-backward (n)
8844   "Tick N articles backwards.
8845 The difference between N and the number of articles ticked is returned."
8846   (interactive "p")
8847   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8848
8849 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8850 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8851 (defun gnus-summary-tick-article (&optional article clear-mark)
8852   "Mark current article as unread.
8853 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8854 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8855   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8856                                        gnus-ticked-mark)))
8857
8858 (defun gnus-summary-mark-as-read-forward (n)
8859   "Mark N articles as read forwards.
8860 If N is negative, mark backwards instead.
8861 The difference between N and the actual number of articles marked is
8862 returned."
8863   (interactive "p")
8864   (gnus-summary-mark-forward n gnus-del-mark t))
8865
8866 (defun gnus-summary-mark-as-read-backward (n)
8867   "Mark the N articles as read backwards.
8868 The difference between N and the actual number of articles marked is
8869 returned."
8870   (interactive "p")
8871   (gnus-summary-mark-forward (- n) gnus-del-mark t))
8872
8873 (defun gnus-summary-mark-as-read (&optional article mark)
8874   "Mark current article as read.
8875 ARTICLE specifies the article to be marked as read.
8876 MARK specifies a string to be inserted at the beginning of the line."
8877   (gnus-summary-mark-article article mark))
8878
8879 (defun gnus-summary-clear-mark-forward (n)
8880   "Clear marks from N articles forward.
8881 If N is negative, clear backward instead.
8882 The difference between N and the number of marks cleared is returned."
8883   (interactive "p")
8884   (gnus-summary-mark-forward n gnus-unread-mark))
8885
8886 (defun gnus-summary-clear-mark-backward (n)
8887   "Clear marks from N articles backward.
8888 The difference between N and the number of marks cleared is returned."
8889   (interactive "p")
8890   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8891
8892 (defun gnus-summary-mark-unread-as-read ()
8893   "Intended to be used by `gnus-summary-mark-article-hook'."
8894   (or (memq gnus-current-article gnus-newsgroup-marked)
8895       (memq gnus-current-article gnus-newsgroup-dormant)
8896       (memq gnus-current-article gnus-newsgroup-expirable)
8897       (gnus-summary-mark-as-read gnus-current-article gnus-read-mark)))
8898
8899 (defun gnus-summary-mark-region-as-read (point mark all)
8900   "Mark all unread articles between point and mark as read.
8901 If given a prefix, mark all articles between point and mark as read,
8902 even ticked and dormant ones."
8903   (interactive "r\nP")
8904   (save-excursion
8905     (goto-char point)
8906     (beginning-of-line)
8907     (while (and 
8908             (< (point) mark)
8909             (progn
8910               (and
8911                (or all
8912                    (and
8913                     (not (memq (gnus-summary-article-number)
8914                                gnus-newsgroup-marked))
8915                     (not (memq (gnus-summary-article-number)
8916                                gnus-newsgroup-dormant))))
8917                (gnus-summary-mark-article
8918                 (gnus-summary-article-number) gnus-del-mark))
8919               t)
8920             (zerop (forward-line 1))))))
8921
8922 ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
8923 (defalias 'gnus-summary-delete-marked-as-read 
8924   'gnus-summary-remove-lines-marked-as-read)
8925 (make-obsolete 'gnus-summary-delete-marked-as-read 
8926                'gnus-summary-remove-lines-marked-as-read)
8927 (defun gnus-summary-remove-lines-marked-as-read ()
8928   "Remove lines that are marked as read."
8929   (interactive)
8930   (gnus-summary-remove-lines-marked-with 
8931    (concat (mapconcat
8932             (lambda (char) (char-to-string (symbol-value char)))
8933             '(gnus-del-mark gnus-read-mark gnus-ancient-mark
8934               gnus-killed-mark gnus-kill-file-mark
8935               gnus-low-score-mark gnus-expirable-mark)
8936             ""))))
8937
8938 (defalias 'gnus-summary-delete-marked-with 
8939   'gnus-summary-remove-lines-marked-with)
8940 (make-obsolete 'gnus-summary-delete-marked-with 
8941                'gnus-summary-remove-lines-marked-with)
8942 ;; Rewrite by Daniel Quinlan <quinlan@best.com>.
8943 (defun gnus-summary-remove-lines-marked-with (marks)
8944   "Remove lines that are marked with MARKS (e.g. \"DK\")."
8945   (interactive "sMarks: ")
8946   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
8947   (gnus-set-global-variables)
8948   (let ((buffer-read-only nil)
8949         (marks (concat "^[" marks "]")))
8950     (goto-char (point-min))
8951     (if gnus-newsgroup-adaptive
8952         (gnus-score-remove-lines-adaptive marks)
8953       (while (re-search-forward marks nil t)
8954         (gnus-delete-line)))
8955     ;; If we use dummy roots, we have to do an additional sweep over
8956     ;; the buffer.
8957     (if (not (eq gnus-summary-make-false-root 'dummy))
8958         ()
8959       (goto-char (point-min))
8960       (setq marks (concat "^[" (char-to-string gnus-dummy-mark) "]"))
8961       (while (re-search-forward marks nil t)
8962         (if (gnus-subject-equal
8963              (gnus-summary-subject-string)
8964              (progn
8965                (forward-line 1)
8966                (gnus-summary-subject-string)))
8967             ()
8968           (forward-line -1)
8969           (gnus-delete-line))))))
8970   (or (zerop (buffer-size))
8971       (if (eobp)
8972           (gnus-summary-prev-subject 1)
8973         (gnus-summary-position-cursor)))
8974
8975 (defun gnus-summary-expunge-below (score)
8976   "Remove articles with score less than SCORE."
8977   (interactive "P")
8978   (gnus-set-global-variables)
8979   (setq score (if score
8980                   (prefix-numeric-value score)
8981                 (or gnus-summary-default-score 0)))
8982   (save-excursion
8983     (set-buffer gnus-summary-buffer)
8984     (goto-char (point-min))
8985     (let ((buffer-read-only nil)
8986           beg)
8987       (while (not (eobp))
8988         (if (< (gnus-summary-article-score) score)
8989             (progn
8990               (setq beg (point))
8991               (forward-line 1)
8992               (delete-region beg (point)))
8993           (forward-line 1)))
8994       ;; Adjust point.
8995       (or (zerop (buffer-size))
8996           (if (eobp)
8997               (gnus-summary-prev-subject 1)
8998             (gnus-summary-position-cursor))))))
8999
9000 (defun gnus-summary-mark-below (score mark)
9001   "Mark articles with score less than SCORE with MARK."
9002   (interactive "P\ncMark: ")
9003   (gnus-set-global-variables)
9004   (setq score (if score
9005                   (prefix-numeric-value score)
9006                 (or gnus-summary-default-score 0)))
9007   (save-excursion
9008     (set-buffer gnus-summary-buffer)
9009     (goto-char (point-min))
9010     (while (not (eobp))
9011       (and (< (gnus-summary-article-score) score)
9012            (gnus-summary-mark-article nil mark))
9013       (forward-line 1))))
9014
9015 (defun gnus-summary-kill-below (score)
9016   "Mark articles with score below SCORE as read."
9017   (interactive "P")
9018   (gnus-summary-mark-below score gnus-killed-mark))
9019
9020 (defun gnus-summary-clear-above (score)
9021   "Clear all marks from articles with score above SCORE."
9022   (interactive "P")
9023   (gnus-summary-mark-above score gnus-unread-mark))
9024
9025 (defun gnus-summary-tick-above (score)
9026   "Tick all articles with score above SCORE."
9027   (interactive "P")
9028   (gnus-summary-mark-above score gnus-ticked-mark))
9029
9030 (defun gnus-summary-mark-above (score mark)
9031   "Mark articles with score over SCORE with MARK."
9032   (interactive "P\ncMark: ")
9033   (setq score (if score
9034                   (prefix-numeric-value score)
9035                 (or gnus-summary-default-score 0)))
9036   (save-excursion
9037     (set-buffer gnus-summary-buffer)
9038     (goto-char (point-min))
9039     (while (not (eobp))
9040       (if (> (gnus-summary-article-score) score)
9041           (progn
9042             (gnus-summary-mark-article nil mark)
9043             (forward-line 1))
9044         (forward-line 1)))))
9045
9046 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
9047 (defun gnus-summary-show-all-expunged ()
9048   "Display all the hidden articles that were expunged for low scores."
9049   (interactive)
9050   (let ((buffer-read-only nil))
9051     (let ((scored gnus-newsgroup-scored)
9052           headers h)
9053       (while scored
9054         (or (gnus-summary-goto-subject (car (car scored)))
9055             (and (setq h (gnus-get-header-by-number (car (car scored))))
9056                  (< (cdr (car scored)) gnus-summary-expunge-below)
9057                  (setq headers (cons h headers))))
9058         (setq scored (cdr scored)))
9059       (or headers (error "No expunged articles hidden."))
9060       (goto-char (point-min))
9061       (save-excursion 
9062         (gnus-summary-update-lines 
9063          (point)
9064          (progn
9065            (gnus-summary-prepare-threads (nreverse headers) 0)
9066            (point)))))
9067     (goto-char (point-min))
9068     (gnus-summary-position-cursor)))
9069
9070 (defun gnus-summary-show-all-dormant ()
9071   "Display all the hidden articles that are marked as dormant."
9072   (interactive)
9073   (let ((buffer-read-only nil))
9074     (goto-char (point-min))
9075     (let ((dormant gnus-newsgroup-dormant)
9076           headers h)
9077       (while dormant
9078         (or (gnus-summary-goto-subject (car dormant))
9079             (and (setq h (gnus-get-header-by-number (car dormant)))
9080                  (setq headers (cons h headers))))
9081         (setq dormant (cdr dormant)))
9082       (or headers (error "No dormant articles hidden."))
9083       (save-excursion 
9084         (gnus-summary-update-lines 
9085          (point)
9086          (progn
9087            (gnus-summary-prepare-threads (nreverse headers) 0)
9088            (point)))))
9089     (goto-char (point-min))
9090     (gnus-summary-position-cursor)))
9091
9092 (defun gnus-summary-hide-all-dormant ()
9093   "Hide all dormant articles."
9094   (interactive)
9095   (gnus-summary-remove-lines-marked-with (char-to-string gnus-dormant-mark))
9096   (gnus-summary-position-cursor))
9097
9098 (defun gnus-summary-catchup (all &optional quietly to-here not-mark)
9099   "Mark all articles not marked as unread in this newsgroup as read.
9100 If prefix argument ALL is non-nil, all articles are marked as read.
9101 If QUIETLY is non-nil, no questions will be asked.
9102 If TO-HERE is non-nil, it should be a point in the buffer. All
9103 articles before this point will be marked as read.
9104 The number of articles marked as read is returned."
9105   (interactive "P")
9106   (prog1
9107       (if (or quietly
9108               (not gnus-interactive-catchup) ;Without confirmation?
9109               gnus-expert-user
9110               (gnus-y-or-n-p
9111                (if all
9112                    "Mark absolutely all articles as read? "
9113                  "Mark all unread articles as read? ")))
9114           (if (and not-mark 
9115                    (not gnus-newsgroup-adaptive)
9116                    (not gnus-newsgroup-auto-expire))
9117               (progn
9118                 (and all (setq gnus-newsgroup-marked nil
9119                                gnus-newsgroup-dormant nil))
9120                 (setq gnus-newsgroup-unreads gnus-newsgroup-marked))
9121             (let ((unreads (length gnus-newsgroup-unreads)))
9122               (if (gnus-summary-first-subject (not all))
9123                   (while (and (gnus-summary-mark-as-read nil gnus-catchup-mark)
9124                               (if to-here (< (point) to-here) t)
9125                               (gnus-summary-search-subject nil (not all)))))
9126               (- unreads (length gnus-newsgroup-unreads))
9127               (or to-here
9128                   (setq gnus-newsgroup-unreads gnus-newsgroup-marked)))))
9129     (gnus-summary-position-cursor)))
9130
9131 (defun gnus-summary-catchup-to-here (&optional all)
9132   "Mark all unticked articles before the current one as read.
9133 If ALL is non-nil, also mark ticked and dormant articles as read."
9134   (interactive)
9135   (beginning-of-line)
9136   (gnus-summary-catchup all t (point))
9137   (gnus-set-mode-line 'summary)
9138   (gnus-summary-position-cursor))
9139
9140 (defun gnus-summary-catchup-all (&optional quietly)
9141   "Mark all articles in this newsgroup as read."
9142   (interactive)
9143   (gnus-summary-catchup t quietly))
9144
9145 (defun gnus-summary-catchup-and-exit (all &optional quietly)
9146   "Mark all articles not marked as unread in this newsgroup as read, then exit.
9147 If prefix argument ALL is non-nil, all articles are marked as read."
9148   (interactive "P")
9149   (gnus-summary-catchup all quietly nil 'fast)
9150   ;; Select next newsgroup or exit.
9151   (if (eq gnus-auto-select-next 'quietly)
9152       (gnus-summary-next-group nil)
9153     (gnus-summary-exit)))
9154
9155 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9156   "Mark all articles in this newsgroup as read, and then exit."
9157   (interactive)
9158   (gnus-summary-catchup-and-exit t quietly))
9159
9160 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
9161 (defun gnus-summary-catchup-and-goto-next-group (all)
9162   "Mark all articles in this group as read and select the next group.
9163 If given a prefix, mark all articles, unread as well as ticked, as
9164 read." 
9165   (interactive "P")
9166   (gnus-summary-catchup all)
9167   (gnus-summary-next-group))
9168
9169 ;; Thread-based commands.
9170
9171 (defun gnus-summary-toggle-threads (arg)
9172   "Toggle showing conversation threads.
9173 If ARG is positive number, turn showing conversation threads on."
9174   (interactive "P")
9175   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9176     (setq gnus-show-threads
9177           (if (null arg) (not gnus-show-threads)
9178             (> (prefix-numeric-value arg) 0)))
9179     (gnus-summary-prepare)
9180     (gnus-summary-goto-subject current)
9181     (gnus-summary-position-cursor)))
9182
9183 (defun gnus-summary-show-all-threads ()
9184   "Show all threads."
9185   (interactive)
9186   (save-excursion
9187     (let ((buffer-read-only nil))
9188       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9189   (gnus-summary-position-cursor))
9190
9191 (defun gnus-summary-show-thread ()
9192   "Show thread subtrees.
9193 Returns nil if no thread was there to be shown."
9194   (interactive)
9195   (prog1
9196       (save-excursion
9197         (let ((buffer-read-only nil)
9198               ;; first goto end then to beg, to have point at beg after let
9199               (end (progn (end-of-line) (point)))
9200               (beg (progn (beginning-of-line) (point))))
9201           (prog1
9202               ;; Any hidden lines here?
9203               (search-forward "\r" end t)
9204             (subst-char-in-region beg end ?\^M ?\n t))))
9205     (gnus-summary-position-cursor)))
9206
9207 (defun gnus-summary-hide-all-threads ()
9208   "Hide all thread subtrees."
9209   (interactive)
9210   (save-excursion
9211     (goto-char (point-min))
9212     (gnus-summary-hide-thread)
9213     (while (and (not (eobp)) (zerop (forward-line 1)))
9214       (gnus-summary-hide-thread)))
9215   (gnus-summary-position-cursor))
9216
9217 (defun gnus-summary-hide-thread ()
9218   "Hide thread subtrees.
9219 Returns nil if no threads were there to be hidden."
9220   (interactive)
9221   (let ((buffer-read-only nil)
9222         (start (point))
9223         (level (gnus-summary-thread-level))
9224         (end (point)))
9225     ;; Go forward until either the buffer ends or the subthread
9226     ;; ends. 
9227     (if (eobp)
9228         ()
9229       (while (and (zerop (forward-line 1))
9230                   (> (gnus-summary-thread-level) level))
9231         (setq end (point)))
9232       (prog1
9233           (save-excursion
9234             (goto-char end)
9235             (search-backward "\n" start t))
9236         (subst-char-in-region start end ?\n ?\^M t)
9237         (forward-line -1)))))
9238
9239 (defun gnus-summary-go-to-next-thread (&optional previous)
9240   "Go to the same level (or less) next thread.
9241 If PREVIOUS is non-nil, go to previous thread instead.
9242 Return the article number moved to, or nil if moving was impossible."
9243   (let ((level (gnus-summary-thread-level))
9244         (article (gnus-summary-article-number)))
9245     (if previous 
9246         (while (and (zerop (gnus-summary-prev-subject 1))
9247                     (> (gnus-summary-thread-level) level)))
9248       (while (and (zerop (gnus-summary-next-subject 1))
9249                   (> (gnus-summary-thread-level) level))))
9250     (gnus-summary-recenter)
9251     (gnus-summary-position-cursor)
9252     (let ((oart (gnus-summary-article-number)))
9253       (and (/= oart article) oart))))
9254
9255 (defun gnus-summary-next-thread (n)
9256   "Go to the same level next N'th thread.
9257 If N is negative, search backward instead.
9258 Returns the difference between N and the number of skips actually
9259 done."
9260   (interactive "p")
9261   (let ((backward (< n 0))
9262         (n (abs n)))
9263   (while (and (> n 0)
9264               (gnus-summary-go-to-next-thread backward))
9265     (setq n (1- n)))
9266   (gnus-summary-position-cursor)
9267   (if (/= 0 n) (gnus-message 7 "No more threads"))
9268   n))
9269
9270 (defun gnus-summary-prev-thread (n)
9271   "Go to the same level previous N'th thread.
9272 Returns the difference between N and the number of skips actually
9273 done."
9274   (interactive "p")
9275   (gnus-summary-next-thread (- n)))
9276
9277 (defun gnus-summary-go-down-thread (&optional same)
9278   "Go down one level in the current thread.
9279 If SAME is non-nil, also move to articles of the same level."
9280   (let ((level (gnus-summary-thread-level))
9281         (start (point)))
9282     (if (and (zerop (forward-line 1))
9283              (> (gnus-summary-thread-level) level))
9284         t
9285       (goto-char start)
9286       nil)))
9287
9288 (defun gnus-summary-go-up-thread ()
9289   "Go up one level in the current thread."
9290   (let ((level (gnus-summary-thread-level))
9291         (start (point)))
9292     (while (and (zerop (forward-line -1))
9293                 (>= (gnus-summary-thread-level) level)))
9294     (if (>= (gnus-summary-thread-level) level)
9295         (progn
9296           (goto-char start)
9297           nil)
9298       t)))
9299
9300 (defun gnus-summary-down-thread (n)
9301   "Go down thread N steps.
9302 If N is negative, go up instead.
9303 Returns the difference between N and how many steps down that were
9304 taken."
9305   (interactive "p")
9306   (let ((up (< n 0))
9307         (n (abs n)))
9308   (while (and (> n 0)
9309               (if up (gnus-summary-go-up-thread)
9310                 (gnus-summary-go-down-thread)))
9311     (setq n (1- n)))
9312   (gnus-summary-position-cursor)
9313   (if (/= 0 n) (gnus-message 7 "Can't go further"))
9314   n))
9315
9316 (defun gnus-summary-up-thread (n)
9317   "Go up thread N steps.
9318 If N is negative, go up instead.
9319 Returns the difference between N and how many steps down that were
9320 taken."
9321   (interactive "p")
9322   (gnus-summary-down-thread (- n)))
9323
9324 (defun gnus-summary-kill-thread (unmark)
9325   "Mark articles under current thread as read.
9326 If the prefix argument is positive, remove any kinds of marks.
9327 If the prefix argument is negative, tick articles instead."
9328   (interactive "P")
9329   (if unmark
9330       (setq unmark (prefix-numeric-value unmark)))
9331   (let ((killing t)
9332         (level (gnus-summary-thread-level)))
9333     (save-excursion
9334       (while killing
9335         ;; Mark the article...
9336         (cond ((null unmark) (gnus-summary-mark-as-read nil gnus-killed-mark))
9337               ((> unmark 0) (gnus-summary-tick-article nil t))
9338               (t (gnus-summary-tick-article)))
9339         ;; ...and go forward until either the buffer ends or the subtree
9340         ;; ends. 
9341         (if (not (and (zerop (forward-line 1))
9342                       (> (gnus-summary-thread-level) level)))
9343             (setq killing nil))))
9344     ;; Hide killed subtrees.
9345     (and (null unmark)
9346          gnus-thread-hide-killed
9347          (gnus-summary-hide-thread))
9348     ;; If marked as read, go to next unread subject.
9349     (if (null unmark)
9350         ;; Go to next unread subject.
9351         (gnus-summary-next-subject 1 t)))
9352   (gnus-set-mode-line 'summary))
9353
9354 ;; Summary sorting commands
9355
9356 (defun gnus-summary-sort-by-number (&optional reverse)
9357   "Sort summary buffer by article number.
9358 Argument REVERSE means reverse order."
9359   (interactive "P")
9360   (gnus-summary-sort 
9361    (cons 'gnus-summary-article-number 'gnus-thread-sort-by-number) reverse))
9362
9363 (defun gnus-summary-sort-by-author (&optional reverse)
9364   "Sort summary buffer by author name alphabetically.
9365 If case-fold-search is non-nil, case of letters is ignored.
9366 Argument REVERSE means reverse order."
9367   (interactive "P")
9368   (gnus-summary-sort
9369    (cons
9370     (lambda ()
9371       (let ((extract (funcall
9372                       gnus-extract-address-components
9373                       (header-from (gnus-get-header-by-number
9374                                     (gnus-summary-article-number))))))
9375         (or (car extract) (cdr extract))))
9376     'gnus-thread-sort-by-author)
9377    reverse))
9378
9379 (defun gnus-summary-sort-by-subject (&optional reverse)
9380   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
9381 If case-fold-search is non-nil, case of letters is ignored.
9382 Argument REVERSE means reverse order."
9383   (interactive "P")
9384   (gnus-summary-sort
9385    (cons
9386     (lambda ()
9387       (downcase (gnus-simplify-subject (gnus-summary-subject-string))))
9388     'gnus-thread-sort-by-subject)
9389    reverse))
9390
9391 (defun gnus-summary-sort-by-date (&optional reverse)
9392   "Sort summary buffer by date.
9393 Argument REVERSE means reverse order."
9394   (interactive "P")
9395   (gnus-summary-sort
9396    (cons
9397     (lambda ()
9398       (gnus-sortable-date
9399        (header-date 
9400         (gnus-get-header-by-number (gnus-summary-article-number)))))
9401     'gnus-thread-sort-by-date)
9402    reverse))
9403
9404 (defun gnus-summary-sort-by-score (&optional reverse)
9405   "Sort summary buffer by score.
9406 Argument REVERSE means reverse order."
9407   (interactive "P")
9408   (gnus-summary-sort 
9409    (cons 'gnus-summary-article-score 'gnus-thread-sort-by-score)
9410    (not reverse)))
9411
9412 (defvar gnus-summary-already-sorted nil)
9413 (defun gnus-summary-sort (predicate reverse)
9414   ;; Sort summary buffer by PREDICATE.  REVERSE means reverse order. 
9415   (if gnus-summary-already-sorted
9416       ()
9417     (let (buffer-read-only)
9418       (if (not gnus-show-threads)
9419           (progn
9420             (goto-char (point-min))
9421             (sort-subr reverse 'forward-line 'end-of-line (car predicate)))
9422         (let ((gnus-thread-sort-functions (list (cdr predicate)))
9423               (gnus-summary-prepare-hook nil)
9424               (gnus-summary-already-sorted nil))
9425           (gnus-summary-prepare)
9426           (and gnus-show-threads
9427                gnus-thread-hide-subtree
9428                (gnus-summary-hide-all-threads)))))))
9429   
9430 (defun gnus-sortable-date (date)
9431   "Make sortable string by string-lessp from DATE.
9432 Timezone package is used."
9433   (let* ((date   (timezone-fix-time date nil nil)) ;[Y M D H M S]
9434          (year   (aref date 0))
9435          (month  (aref date 1))
9436          (day    (aref date 2)))
9437     (timezone-make-sortable-date 
9438      year month day 
9439      (timezone-make-time-string
9440       (aref date 3) (aref date 4) (aref date 5)))))
9441
9442
9443 ;; Summary saving commands.
9444
9445 (defun gnus-summary-save-article (n)
9446   "Save the current article using the default saver function.
9447 If N is a positive number, save the N next articles.
9448 If N is a negative number, save the N previous articles.
9449 If N is nil and any articles have been marked with the process mark,
9450 save those articles instead.
9451 The variable `gnus-default-article-saver' specifies the saver function."
9452   (interactive "P")
9453   (let ((articles (gnus-summary-work-articles n)))
9454     (while articles
9455       (let ((header (gnus-get-header-by-number (car articles))))
9456         (if (vectorp header)
9457             (progn
9458               (gnus-summary-select-article t nil nil (car articles))
9459               (or gnus-save-all-headers
9460                   (gnus-article-hide-headers t))
9461               ;; Remove any X-Gnus lines.
9462               (save-excursion
9463                 (save-restriction
9464                   (set-buffer gnus-article-buffer)
9465                   (let ((buffer-read-only nil))
9466                     (goto-char (point-min))
9467                     (narrow-to-region (point) (or (search-forward "\n\n" nil t)
9468                                                   (point-max)))
9469                     (while (re-search-forward "^X-Gnus" nil t)
9470                       (beginning-of-line)
9471                       (delete-region (point)
9472                                      (progn (forward-line 1) (point))))
9473                     (widen))))
9474               (save-excursion
9475                 (if gnus-default-article-saver
9476                     (funcall gnus-default-article-saver)
9477                   (error "No default saver is defined."))))
9478           (if (assq 'name header)
9479               (gnus-copy-file (cdr (assq 'name header)))
9480             (gnus-message 1 "Article %d is unsaveable" (car articles)))))
9481       (gnus-summary-remove-process-mark (car articles))
9482       (setq articles (cdr articles)))
9483     (gnus-summary-position-cursor)
9484     n))
9485
9486 (defun gnus-summary-pipe-output (arg)
9487   "Pipe the current article to a subprocess.
9488 If N is a positive number, pipe the N next articles.
9489 If N is a negative number, pipe the N previous articles.
9490 If N is nil and any articles have been marked with the process mark,
9491 pipe those articles instead."
9492   (interactive "P")
9493   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9494     (gnus-summary-save-article arg)))
9495
9496 (defun gnus-summary-save-article-mail (arg)
9497   "Append the current article to an mail file.
9498 If N is a positive number, save the N next articles.
9499 If N is a negative number, save the N previous articles.
9500 If N is nil and any articles have been marked with the process mark,
9501 save those articles instead."
9502   (interactive "P")
9503   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9504     (gnus-summary-save-article arg)))
9505
9506 (defun gnus-summary-save-article-rmail (arg)
9507   "Append the current article to an rmail file.
9508 If N is a positive number, save the N next articles.
9509 If N is a negative number, save the N previous articles.
9510 If N is nil and any articles have been marked with the process mark,
9511 save those articles instead."
9512   (interactive "P")
9513   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9514     (gnus-summary-save-article arg)))
9515
9516 (defun gnus-summary-save-article-file (arg)
9517   "Append the current article to a file.
9518 If N is a positive number, save the N next articles.
9519 If N is a negative number, save the N previous articles.
9520 If N is nil and any articles have been marked with the process mark,
9521 save those articles instead."
9522   (interactive "P")
9523   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9524     (gnus-summary-save-article arg)))
9525
9526 (defun gnus-read-save-file-name (prompt default-name)
9527   (let ((methods gnus-split-methods)
9528         split-name)
9529     (if (not gnus-split-methods)
9530         ()
9531       (save-excursion
9532         (set-buffer gnus-article-buffer)
9533         (gnus-narrow-to-headers)
9534         (while methods
9535           (goto-char (point-min))
9536           (and (condition-case () 
9537                    (re-search-forward (car (car methods)) nil t)
9538                  (error nil))
9539                (setq split-name (cons (nth 1 (car methods)) split-name)))
9540           (setq methods (cdr methods)))
9541         (widen)))
9542     (cond ((null split-name)
9543            (read-file-name
9544             (concat prompt " (default "
9545                     (file-name-nondirectory default-name) ") ")
9546             (file-name-directory default-name)
9547             default-name))
9548           ((= 1 (length split-name))
9549            (read-file-name
9550             (concat prompt " (default " (car split-name) ") ")
9551             gnus-article-save-directory
9552             (concat gnus-article-save-directory (car split-name))))
9553           (t
9554            (setq split-name (mapcar (lambda (el) (list el))
9555                                     (nreverse split-name)))
9556            (let ((result (completing-read 
9557                           (concat prompt " ")
9558                           split-name nil nil)))
9559              (concat gnus-article-save-directory
9560                      (if (string= result "")
9561                          (car (car split-name))
9562                        result)))))))
9563
9564 (defun gnus-summary-save-in-rmail (&optional filename)
9565   "Append this article to Rmail file.
9566 Optional argument FILENAME specifies file name.
9567 Directory to save to is default to `gnus-article-save-directory' which
9568 is initialized from the SAVEDIR environment variable."
9569   (interactive)
9570   (let ((default-name
9571           (funcall gnus-rmail-save-name gnus-newsgroup-name
9572                    gnus-current-headers gnus-newsgroup-last-rmail)))
9573     (or filename
9574         (setq filename (gnus-read-save-file-name 
9575                         "Save in rmail file:" default-name)))
9576     (gnus-make-directory (file-name-directory filename))
9577     (gnus-eval-in-buffer-window 
9578      gnus-article-buffer
9579      (save-excursion
9580        (save-restriction
9581          (widen)
9582          (gnus-output-to-rmail filename))))
9583     ;; Remember the directory name to save articles.
9584     (setq gnus-newsgroup-last-rmail filename)))
9585
9586 (defun gnus-summary-save-in-mail (&optional filename)
9587   "Append this article to Unix mail file.
9588 Optional argument FILENAME specifies file name.
9589 Directory to save to is default to `gnus-article-save-directory' which
9590 is initialized from the SAVEDIR environment variable."
9591   (interactive)
9592   (let ((default-name
9593           (funcall gnus-mail-save-name gnus-newsgroup-name
9594                    gnus-current-headers gnus-newsgroup-last-mail)))
9595     (or filename
9596         (setq filename (gnus-read-save-file-name 
9597                         "Save in Unix mail file:" default-name)))
9598     (setq filename
9599           (expand-file-name filename
9600                             (and default-name
9601                                  (file-name-directory default-name))))
9602     (gnus-make-directory (file-name-directory filename))
9603     (gnus-eval-in-buffer-window 
9604      gnus-article-buffer
9605      (save-excursion
9606        (save-restriction
9607          (widen)
9608          (if (and (file-readable-p filename) (rmail-file-p filename))
9609              (gnus-output-to-rmail filename)
9610            (rmail-output filename 1 t t)))))
9611     ;; Remember the directory name to save articles.
9612     (setq gnus-newsgroup-last-mail filename)))
9613
9614 (defun gnus-summary-save-in-file (&optional filename)
9615   "Append this article to file.
9616 Optional argument FILENAME specifies file name.
9617 Directory to save to is default to `gnus-article-save-directory' which
9618 is initialized from the SAVEDIR environment variable."
9619   (interactive)
9620   (let ((default-name
9621           (funcall gnus-file-save-name gnus-newsgroup-name
9622                    gnus-current-headers gnus-newsgroup-last-file)))
9623     (or filename
9624         (setq filename (gnus-read-save-file-name 
9625                         "Save in file:" default-name)))
9626     (gnus-make-directory (file-name-directory filename))
9627     (gnus-eval-in-buffer-window 
9628      gnus-article-buffer
9629      (save-excursion
9630        (save-restriction
9631          (widen)
9632          (gnus-output-to-file filename))))
9633     ;; Remember the directory name to save articles.
9634     (setq gnus-newsgroup-last-file filename)))
9635
9636 (defun gnus-summary-save-in-pipe (&optional command)
9637   "Pipe this article to subprocess."
9638   (interactive)
9639   (let ((command (read-string "Shell command on article: "
9640                               gnus-last-shell-command)))
9641     (if (string-equal command "")
9642         (setq command gnus-last-shell-command))
9643     (gnus-eval-in-buffer-window 
9644      gnus-article-buffer
9645      (save-restriction
9646        (widen)
9647        (shell-command-on-region (point-min) (point-max) command nil)))
9648     (setq gnus-last-shell-command command)))
9649
9650 ;; Summary extract commands
9651
9652 (defun gnus-summary-insert-pseudos (pslist)
9653   (let ((buffer-read-only nil)
9654         (article (gnus-summary-article-number))
9655         b)
9656     (or (gnus-summary-goto-subject article)
9657         (error (format "No such article: %d" article)))
9658     (or gnus-newsgroup-headers-hashtb-by-number
9659         (gnus-make-headers-hashtable-by-number))
9660     (gnus-summary-position-cursor)
9661     ;; If all commands are to be bunched up on one line, we collect
9662     ;; them here.  
9663     (if gnus-view-pseudos-separately
9664         ()
9665       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9666             files action)
9667         (while ps
9668           (setq action (cdr (assq 'action (car ps))))
9669           (setq files (list (cdr (assq 'name (car ps)))))
9670           (while (and ps (cdr ps)
9671                       (string= (or action "1")
9672                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
9673             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
9674             (setcdr ps (cdr (cdr ps))))
9675           (if (not files)
9676               ()
9677             (if (not (string-match "%s" action))
9678                 (setq files (cons " " files)))
9679             (setq files (cons " " files))
9680             (and (assq 'execute (car ps))
9681                  (setcdr (assq 'execute (car ps))
9682                          (funcall (if (string-match "%s" action)
9683                                       'format 'concat)
9684                                   action 
9685                                   (mapconcat (lambda (f) f) files " ")))))
9686           (setq ps (cdr ps)))))
9687     (if gnus-view-pseudos
9688         (while pslist
9689           (and (assq 'execute (car pslist))
9690                (gnus-execute-command (cdr (assq 'execute (car pslist)))
9691                                      (eq gnus-view-pseudos 'not-confirm)))
9692           (setq pslist (cdr pslist)))
9693       (save-excursion
9694         (while pslist
9695           (gnus-summary-goto-subject (or (cdr (assq 'article (car pslist)))
9696                                          (gnus-summary-article-number)))
9697           (forward-line 1)
9698           (setq b (point))
9699           (insert "          " (file-name-nondirectory 
9700                                 (cdr (assq 'name (car pslist))))
9701                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9702           (add-text-properties 
9703            b (1+ b) (list 'gnus-number gnus-reffed-article-number
9704                           'gnus-mark gnus-unread-mark 
9705                           'gnus-level 0
9706                           'gnus-pseudo (car pslist)))
9707           (forward-line -1)
9708           (gnus-sethash (int-to-string gnus-reffed-article-number)
9709                         (car pslist) gnus-newsgroup-headers-hashtb-by-number)
9710           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9711           (setq pslist (cdr pslist)))))))
9712
9713 (defun gnus-pseudos< (p1 p2)
9714   (let ((c1 (cdr (assq 'action p1)))
9715         (c2 (cdr (assq 'action p2))))
9716     (and c1 c2 (string< c1 c2))))
9717
9718 (defun gnus-request-pseudo-article (props)
9719   (cond ((assq 'execute props)
9720          (gnus-execute-command (cdr (assq 'execute props)))))
9721   (let ((gnus-current-article (gnus-summary-article-number)))
9722     (run-hooks 'gnus-mark-article-hook)))
9723
9724 (defun gnus-execute-command (command &optional automatic)
9725   (save-excursion
9726     (gnus-article-setup-buffer)
9727     (set-buffer gnus-article-buffer)
9728     (let ((command (if automatic command (read-string "Command: " command)))
9729           (buffer-read-only nil))
9730       (erase-buffer)
9731       (insert "$ " command "\n\n")
9732       (if gnus-view-pseudo-asynchronously
9733           (start-process "gnus-execute" nil "sh" "-c" command)
9734         (call-process "sh" nil t nil "-c" command)))))
9735
9736 (defun gnus-copy-file (file &optional to)
9737   "Copy FILE to TO."
9738   (interactive
9739    (list (read-file-name "Copy file: " default-directory)
9740          (read-file-name "Copy file to: " default-directory)))
9741   (or to (setq to (read-file-name "Copy file to: " default-directory)))
9742   (and (file-directory-p to) 
9743        (setq to (concat (file-name-as-directory to)
9744                         (file-name-nondirectory file))))
9745   (copy-file file to))
9746
9747 ;; Summary kill commands.
9748
9749 (defun gnus-summary-edit-global-kill (article)
9750   "Edit the \"global\" kill file."
9751   (interactive (list (gnus-summary-article-number)))
9752   (gnus-group-edit-global-kill article))
9753
9754 (defun gnus-summary-edit-local-kill ()
9755   "Edit a local kill file applied to the current newsgroup."
9756   (interactive)
9757   (setq gnus-current-headers 
9758         (gnus-gethash 
9759          (int-to-string (gnus-summary-article-number))
9760          gnus-newsgroup-headers-hashtb-by-number))
9761   (gnus-set-global-variables)
9762   (gnus-group-edit-local-kill 
9763    (gnus-summary-article-number) gnus-newsgroup-name))
9764
9765 \f
9766 ;;;
9767 ;;; Gnus article mode
9768 ;;;
9769
9770 (put 'gnus-article-mode 'mode-class 'special)
9771
9772 (if gnus-article-mode-map
9773     nil
9774   (setq gnus-article-mode-map (make-keymap))
9775   (suppress-keymap gnus-article-mode-map)
9776   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
9777   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
9778   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
9779   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
9780   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
9781   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
9782   (define-key gnus-article-mode-map "\C-c\C-M" 'gnus-article-mail-with-original)
9783   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
9784   (define-key gnus-article-mode-map gnus-mouse-2 'gnus-article-push-button)
9785   (define-key gnus-article-mode-map "\r" 'gnus-article-press-button)
9786   (define-key gnus-article-mode-map "\t" 'gnus-article-next-button)
9787   
9788   ;; Duplicate almost all summary keystrokes in the article mode map.
9789   (let ((commands 
9790          (list "#" "\M-#" "\C-c\M-#" "n" "p"
9791                "N" "P" "\M-\C-n" "\M-\C-p" "." "\M-s" "\M-r"
9792                "<" ">" "l" "j" "^" "\M-^" "-" "u" "U" "d" "D"
9793                "\M-u" "\M-U" "k" "\C-k" "\M-\C-k""x" "X" 
9794                "\M-\C-x" "\M-\177" "b" "B" "$" "w" "\C-c\C-r"
9795                "t" "\M-t" "C" "S"
9796                "m" "o" "\C-o" "|" "\M-m" "\M-\C-m" "\M-k" "M"
9797                "V" "\C-c\C-d")))
9798     (while commands
9799       (define-key gnus-article-mode-map (car commands) 
9800         'gnus-article-summary-command)
9801       (setq commands (cdr commands))))
9802
9803   (let ((commands (list "q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F")))
9804     (while commands
9805       (define-key gnus-article-mode-map (car commands) 
9806         'gnus-article-summary-command-nosave)
9807       (setq commands (cdr commands)))))
9808
9809
9810 (defun gnus-article-mode ()
9811   "Major mode for displaying an article.
9812
9813 All normal editing commands are switched off.
9814
9815 The following commands are available:
9816
9817 \\<gnus-article-mode-map>
9818 \\[gnus-article-next-page]\t Scroll the article one page forwards
9819 \\[gnus-article-prev-page]\t Scroll the article one page backwards
9820 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
9821 \\[gnus-article-show-summary]\t Display the summary buffer
9822 \\[gnus-article-mail]\t Send a reply to the address near point
9823 \\[gnus-article-mail-with-original]\t Send a reply to the address near point; include the original article
9824 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
9825 \\[gnus-info-find-node]\t Go to the Gnus info node"
9826   (interactive)
9827   (if gnus-visual (gnus-article-make-menu-bar))
9828   (kill-all-local-variables)
9829   (setq mode-line-modified "-- ")
9830   (make-local-variable 'mode-line-format)
9831   (setq mode-line-format (copy-sequence mode-line-format))
9832   (and (equal (nth 3 mode-line-format) "   ")
9833        (setcar (nthcdr 3 mode-line-format) ""))
9834   (setq mode-name "Article")
9835   (setq major-mode 'gnus-article-mode)
9836   (make-local-variable 'minor-mode-alist)
9837   (or (assq 'gnus-show-mime minor-mode-alist)
9838       (setq minor-mode-alist
9839             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
9840   (use-local-map gnus-article-mode-map)
9841   (make-local-variable 'page-delimiter)
9842   (setq page-delimiter gnus-page-delimiter)
9843   (buffer-disable-undo (current-buffer))
9844   (setq buffer-read-only t)             ;Disable modification
9845   (run-hooks 'gnus-article-mode-hook))
9846
9847 (defun gnus-article-setup-buffer ()
9848   "Initialize article mode buffer."
9849   (if (get-buffer gnus-article-buffer)
9850       (save-excursion
9851         (set-buffer gnus-article-buffer)
9852         (or (eq major-mode 'gnus-article-mode)
9853             (gnus-article-mode)))
9854     (save-excursion
9855       (set-buffer (get-buffer-create gnus-article-buffer))
9856       (gnus-add-current-to-buffer-list)
9857       (gnus-article-mode))))
9858
9859 ;; Set article window start at LINE, where LINE is the number of lines
9860 ;; from the head of the article.
9861 (defun gnus-article-set-window-start (&optional line)
9862   (set-window-start 
9863    (get-buffer-window gnus-article-buffer)
9864    (save-excursion
9865      (set-buffer gnus-article-buffer)
9866      (goto-char (point-min))
9867      (if (not line)
9868          (point-min)
9869        (gnus-message 6 "Moved to bookmark")
9870        (search-forward "\n\n" nil t)
9871        (forward-line line)
9872        (point)))))
9873
9874 (defun gnus-request-article-this-buffer (article group)
9875   "Get an article and insert it into this buffer."
9876   (setq group (or group gnus-newsgroup-name))
9877
9878   ;; Open server if it has closed.
9879   (gnus-check-news-server (gnus-find-method-for-group group))
9880
9881   ;; Using `gnus-request-article' directly will insert the article into
9882   ;; `nntp-server-buffer' - so we'll save some time by not having to
9883   ;; copy it from the server buffer into the article buffer.
9884
9885   ;; We only request an article by message-id when we do not have the
9886   ;; headers for it, so we'll have to get those.
9887   (and (stringp article) 
9888        (let ((gnus-override-method gnus-refer-article-method))
9889          (gnus-read-header article)))
9890
9891   ;; If the article number is negative, that means that this article
9892   ;; doesn't belong in this newsgroup (possibly), so we find its
9893   ;; message-id and request it by id instead of number.
9894   (if (not (numberp article))
9895       ()
9896     (save-excursion
9897       (set-buffer gnus-summary-buffer)
9898       (let ((header (gnus-get-header-by-number article)))
9899         (if (< article 0)
9900             (if (vectorp header)
9901                 ;; It's a real article.
9902                 (setq article (header-id header))
9903               ;; It is an extracted pseudo-article.
9904               (setq article 'pseudo)
9905               (gnus-request-pseudo-article header)))
9906
9907         (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
9908           (if (not (eq (car method) 'nneething))
9909               ()
9910             (let ((dir (concat (file-name-as-directory (nth 1 method))
9911                                (header-subject header))))
9912               (if (file-directory-p dir)
9913                   (progn
9914                     (setq article 'nneething)
9915                     (gnus-group-enter-directory dir)))))))))
9916
9917   ;; Check the cache.
9918   (if (and gnus-use-cache
9919            (numberp article)
9920            (gnus-cache-request-article article group))
9921       'article
9922     ;; Get the article and into the article buffer.
9923     (if (or (stringp article) (numberp article))
9924         (progn
9925           (erase-buffer)
9926           (let ((gnus-override-method 
9927                  (and (stringp article) gnus-refer-article-method)))
9928             (and (gnus-request-article article group (current-buffer))
9929                  'article)))
9930       article)))
9931
9932 (defun gnus-read-header (id)
9933   "Read the headers of article ID and enter them into the Gnus system."
9934   (or gnus-newsgroup-headers-hashtb-by-number
9935       (gnus-make-headers-hashtable-by-number))
9936   (let (header)
9937     (if (not (setq header 
9938                    (car (if (let ((gnus-nov-is-evil t))
9939                               (gnus-retrieve-headers 
9940                                (list id) gnus-newsgroup-name))
9941                             (gnus-get-newsgroup-headers)))))
9942         nil
9943       (if (stringp id)
9944           (header-set-number header gnus-reffed-article-number))
9945       (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers))
9946       (gnus-sethash (int-to-string (header-number header)) header
9947                     gnus-newsgroup-headers-hashtb-by-number)
9948       (if (stringp id)
9949           (setq gnus-reffed-article-number (1- gnus-reffed-article-number)))
9950       (setq gnus-current-headers header)
9951       header)))
9952
9953 (defun gnus-article-prepare (article &optional all-headers header)
9954   "Prepare ARTICLE in article mode buffer.
9955 ARTICLE should either be an article number or a Message-ID.
9956 If ARTICLE is an id, HEADER should be the article headers.
9957 If ALL-HEADERS is non-nil, no headers are hidden."
9958   (save-excursion
9959     ;; Make sure we start in a summary buffer.
9960     (or (eq major-mode 'gnus-summary-mode)
9961         (set-buffer gnus-summary-buffer))
9962     (setq gnus-summary-buffer (current-buffer))
9963     ;; Make sure the connection to the server is alive.
9964     (or (gnus-server-opened (gnus-find-method-for-group gnus-newsgroup-name))
9965         (progn
9966           (gnus-check-news-server 
9967            (gnus-find-method-for-group gnus-newsgroup-name))
9968           (gnus-request-group gnus-newsgroup-name t)))
9969     (or gnus-newsgroup-headers-hashtb-by-number
9970         (gnus-make-headers-hashtable-by-number))
9971     (let* ((article (if header (header-number header) article))
9972            (summary-buffer (current-buffer))
9973            (internal-hook gnus-article-internal-prepare-hook)
9974            (group gnus-newsgroup-name)
9975            result)
9976       (save-excursion
9977         (gnus-article-setup-buffer)
9978         (set-buffer gnus-article-buffer)
9979         (if (not (setq result (let ((buffer-read-only nil))
9980                                 (gnus-request-article-this-buffer 
9981                                  article group))))
9982             ;; There is no such article.
9983             (progn
9984               (save-excursion
9985                 (set-buffer gnus-summary-buffer)
9986                 (setq gnus-current-article 0)
9987                 (and (numberp article) 
9988                      (gnus-summary-mark-as-read article gnus-canceled-mark))
9989                 (gnus-message 1 "No such article (may be canceled)")
9990                 (and (numberp article)
9991                      (setq gnus-current-article article))
9992                 (ding))
9993               (and (numberp article)
9994                    (setq gnus-article-current 
9995                          (cons gnus-newsgroup-name article)))
9996               nil)
9997           (if (or (eq result 'pseudo) (eq result 'nneething))
9998               (progn
9999                 (save-excursion
10000                   (set-buffer summary-buffer)
10001                   (setq gnus-last-article gnus-current-article
10002                         gnus-newsgroup-history (cons gnus-current-article
10003                                                      gnus-newsgroup-history)
10004                         gnus-current-article 0
10005                         gnus-current-headers nil
10006                         gnus-article-current nil)
10007                   (if (eq result 'nneething)
10008                       (gnus-configure-windows 'summary)
10009                     (gnus-configure-windows 'article))
10010                   (gnus-set-global-variables))
10011                 (gnus-set-mode-line 'article))
10012             ;; The result from the `request' was an actual article -
10013             ;; or at least some text that is now displayed in the
10014             ;; article buffer.
10015             (if (and (numberp article)
10016                      (not (eq article gnus-current-article)))
10017                 ;; Seems like a new article has been selected.
10018                 ;; `gnus-current-article' must be an article number.
10019                 (save-excursion
10020                   (set-buffer summary-buffer)
10021                   (setq gnus-last-article gnus-current-article
10022                         gnus-newsgroup-history (cons gnus-current-article
10023                                                      gnus-newsgroup-history)
10024                         gnus-current-article article
10025                         gnus-current-headers 
10026                         (gnus-get-header-by-number gnus-current-article)
10027                         gnus-article-current 
10028                         (cons gnus-newsgroup-name gnus-current-article))
10029                   (gnus-summary-show-thread)
10030                   (run-hooks 'gnus-mark-article-hook)
10031                   (gnus-set-mode-line 'summary)
10032                   (and gnus-visual 
10033                        (run-hooks 'gnus-visual-mark-article-hook))
10034                   ;; Set the global newsgroup variables here.
10035                   ;; Suggested by Jim Sisolak
10036                   ;; <sisolak@trans4.neep.wisc.edu>.
10037                   (gnus-set-global-variables)
10038                   (and gnus-use-cache 
10039                        (gnus-cache-possibly-enter-article
10040                         group article
10041                         (gnus-get-header-by-number article)
10042                         (memq article gnus-newsgroup-marked)
10043                         (memq article gnus-newsgroup-dormant)
10044                         (memq article gnus-newsgroup-unreads)))))
10045             ;; gnus-have-all-headers must be either T or NIL.
10046             (setq gnus-have-all-headers
10047                   (not (not (or all-headers gnus-show-all-headers))))
10048             ;; Hooks for getting information from the article.
10049             ;; This hook must be called before being narrowed.
10050             (let (buffer-read-only)
10051               (run-hooks 'internal-hook)
10052               (run-hooks 'gnus-article-prepare-hook)
10053               ;; Decode MIME message.
10054               (if (and gnus-show-mime
10055                        (gnus-fetch-field "Mime-Version"))
10056                   (funcall gnus-show-mime-method))
10057               ;; Perform the article display hooks.
10058               (run-hooks 'gnus-article-display-hook))
10059             ;; Do page break.
10060             (goto-char (point-min))
10061             (and gnus-break-pages (gnus-narrow-to-page))
10062             (gnus-set-mode-line 'article)
10063             (gnus-configure-windows 'article)
10064             (goto-char (point-min))
10065             t))))))
10066
10067 (defun gnus-article-show-all-headers ()
10068   "Show all article headers in article mode buffer."
10069   (save-excursion 
10070     (gnus-article-setup-buffer)
10071     (set-buffer gnus-article-buffer)
10072     (let ((buffer-read-only nil))
10073       (remove-text-properties (point-min) (point-max) 
10074                               gnus-hidden-properties))))
10075
10076 (defun gnus-article-hide-headers-if-wanted ()
10077   "Hide unwanted headers if `gnus-have-all-headers' is nil.
10078 Provided for backwards compatability."
10079   (or gnus-have-all-headers
10080       (gnus-article-hide-headers)))
10081
10082 (defun gnus-article-hide-headers (&optional delete)
10083   "Hide unwanted headers and possibly sort them as well."
10084   (interactive "P")
10085   (save-excursion
10086     (set-buffer gnus-article-buffer)
10087     (save-restriction
10088       (let ((sorted gnus-sorted-header-list)
10089             (buffer-read-only nil)
10090             want want-list beg want-l)
10091         ;; First we narrow to just the headers.
10092         (widen)
10093         (goto-char (point-min))
10094         ;; Hide any "From " lines at the beginning of (mail) articles. 
10095         (while (looking-at "From ")
10096           (forward-line 1))
10097         (if (bobp) 
10098             (add-text-properties (point-min) (point) gnus-hidden-properties))
10099         ;; Then treat the rest of the header lines.
10100         (narrow-to-region 
10101          (point) 
10102          (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
10103         ;; Then we use the two regular expressions
10104         ;; `gnus-ignored-headers' and `gnus-visible-headers' to
10105         ;; select which header lines is to remain visible in the
10106         ;; article buffer.
10107         (goto-char (point-min))
10108         (while (re-search-forward "^[^ \t]*:" nil t)
10109           (beginning-of-line)
10110           ;; We add the headers we want to keep to a list and delete
10111           ;; them from the buffer.
10112           (if (or (and (stringp gnus-visible-headers)
10113                        (looking-at gnus-visible-headers))
10114                   (and (not (stringp gnus-visible-headers))
10115                        (stringp gnus-ignored-headers)
10116                        (not (looking-at gnus-ignored-headers))))
10117               (progn
10118                 (setq beg (point))
10119                 (forward-line 1)
10120                 ;; Be sure to get multi-line headers...
10121                 (re-search-forward "^[^ \t]*:" nil t)
10122                 (beginning-of-line)
10123                 (setq want-list 
10124                       (cons (buffer-substring beg (point)) want-list))
10125                 (delete-region beg (point))
10126                 (goto-char beg))
10127             (forward-line 1)))
10128         ;; Next we perform the sorting by looking at
10129         ;; `gnus-sorted-header-list'. 
10130         (goto-char (point-min))
10131         (while (and sorted want-list)
10132           (setq want-l want-list)
10133           (while (and want-l
10134                       (not (string-match (car sorted) (car want-l))))
10135             (setq want-l (cdr want-l)))
10136           (if want-l 
10137               (progn
10138                 (insert (car want-l))
10139                 (setq want-list (delq (car want-l) want-list))))
10140           (setq sorted (cdr sorted)))
10141         ;; Any headers that were not matched by the sorted list we
10142         ;; just tack on the end of the visible header list.
10143         (while want-list
10144           (insert (car want-list))
10145           (setq want-list (cdr want-list)))
10146         ;; And finally we make the unwanted headers invisible.
10147         (if delete
10148             (delete-region (point) (point-max))
10149           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
10150           (add-text-properties (point) (point-max) gnus-hidden-properties))))))
10151
10152 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
10153 (defun gnus-article-treat-overstrike ()
10154   "Translate overstrikes into bold text."
10155   (interactive)
10156   (save-excursion
10157     (set-buffer gnus-article-buffer)
10158     (let ((buffer-read-only nil))
10159       (while (search-forward "\b" nil t)
10160         (let ((next (following-char))
10161               (previous (char-after (- (point) 2))))
10162           (cond ((eq next previous)
10163                  (delete-region (- (point) 2) (point))
10164                  (put-text-property (point) (1+ (point))
10165                                     'face 'bold))
10166                 ((eq next ?_)
10167                  (delete-region (1- (point)) (1+ (point)))
10168                  (put-text-property (1- (point)) (point)
10169                                     'face 'underline))
10170                 ((eq previous ?_)
10171                  (delete-region (- (point) 2) (point))
10172                  (put-text-property (point) (1+ (point))
10173                                     'face 'underline))))))))
10174
10175 (defun gnus-article-word-wrap ()
10176   "Format too long lines."
10177   (interactive)
10178   (save-excursion
10179     (set-buffer gnus-article-buffer)
10180     (let ((buffer-read-only nil))
10181       (goto-char (point-min))
10182       (search-forward "\n\n" nil t)
10183       (end-of-line 1)
10184       (let ((paragraph-start "^\\W"))
10185         (while (not (eobp))
10186           (and (>= (current-column) (min fill-column (window-width)))
10187                (/= (preceding-char) ?:)
10188                (fill-paragraph nil))
10189           (end-of-line 2))))))
10190
10191 (defun gnus-article-remove-cr ()
10192   "Remove carriage returns from an article."
10193   (interactive)
10194   (save-excursion
10195     (set-buffer gnus-article-buffer)
10196     (let ((buffer-read-only nil))
10197       (goto-char (point-min))
10198       (while (search-forward "\r" nil t)
10199         (replace-match "" t t)))))
10200
10201 (defun gnus-article-display-x-face (&optional force)
10202   "Look for an X-Face header and display it if present."
10203   (interactive (list 'force))
10204   (save-excursion
10205     (set-buffer gnus-article-buffer)
10206     (let ((inhibit-point-motion-hooks t)
10207           (case-fold-search nil))
10208       (save-restriction
10209         (goto-char (point-min))
10210         (search-forward "\n\n")
10211         (narrow-to-region (point-min) (point))
10212         (goto-char (point-min))
10213         (if (or (not gnus-article-x-face-command)
10214                 (and (not force)
10215                      (or (not gnus-article-x-face-too-ugly)
10216                          (string-match gnus-article-x-face-too-ugly
10217                                        (mail-fetch-field "from"))))
10218                 (progn
10219                   (goto-char (point-min))
10220                   (not (re-search-forward "^X-Face: " nil t))))
10221             nil
10222           (let ((beg (point))
10223                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
10224             (if (symbolp gnus-article-x-face-command)
10225                 (and (or (fboundp gnus-article-x-face-command)
10226                          (error "%s is not a function"
10227                                 gnus-article-x-face-command))
10228                      (funcall gnus-article-x-face-command beg end))
10229               (call-process-region beg end "sh" nil 0 nil
10230                                    "-c" gnus-article-x-face-command))))))))
10231
10232 (defun gnus-article-de-quoted-unreadable (&optional force)
10233   "Do a naïve translation of a quoted-printable-encoded article.
10234 This is in no way, shape or form meant as a replacement for real MIME
10235 processing, but is simply a stop-gap measure until MIME support is
10236 written.
10237 If FORCE, decode the article whether it is marked as quoted-printable
10238 or not." 
10239   (interactive (list 'force))
10240   (save-excursion
10241     (set-buffer gnus-article-buffer)
10242     (let ((case-fold-search t)
10243           (buffer-read-only nil)
10244           (type (gnus-fetch-field "content-transfer-encoding")))
10245       (if (or force (and type (string-match "quoted-printable" type)))
10246           (progn
10247             (goto-char (point-min))
10248             (search-forward "\n\n" nil 'move)
10249             (gnus-mime-decode-quoted-printable (point) (point-max)))))))
10250
10251 (defun gnus-mime-decode-quoted-printable (from to)
10252   ;; Decode quoted-printable from region between FROM and TO.
10253   (save-excursion
10254     (goto-char from)
10255     (while (search-forward "=" to t)
10256       (cond ((eq (following-char) ?\n)
10257              (delete-char -1)
10258              (delete-char 1))
10259             ((looking-at "[0-9A-F][0-9A-F]")
10260              (delete-char -1)
10261              (insert (hexl-hex-string-to-integer
10262                       (buffer-substring (point) (+ 2 (point)))))
10263              (delete-char 2))
10264             ((gnus-message 3 "Malformed MIME quoted-printable message"))))))
10265
10266 (defvar gnus-article-time-units
10267   (list (cons 'year (* 365.25 24 60 60))
10268         (cons 'week (* 7 24 60 60))
10269         (cons 'day (* 24 60 60))
10270         (cons 'hour (* 60 60))
10271         (cons 'minute 60)
10272         (cons 'second 1)))
10273
10274 (defun gnus-article-date-ut (&optional type)
10275   "Convert DATE date to universal time in the current article.
10276 If TYPE is `local', convert to local time; if it is `lapsed', output
10277 how much time has lapsed since DATE."
10278   (interactive (list 'ut))
10279   (let ((date (header-date (or gnus-current-headers 
10280                                (gnus-get-header-by-number
10281                                 (gnus-summary-article-number))"")))
10282         (date-regexp "^Date: \\|^X-Sent: "))
10283     (if (or (not date)
10284             (string= date ""))
10285         ()
10286       (save-excursion
10287         (set-buffer gnus-article-buffer)
10288         (let ((buffer-read-only nil))
10289           (goto-char (point-min))
10290           (if (and (re-search-forward date-regexp nil t)
10291                    (progn 
10292                      (beginning-of-line)
10293                      (looking-at date-regexp)))
10294               (delete-region (gnus-point-at-bol)
10295                              (progn (end-of-line) (1+ (point))))
10296             (goto-char (point-min))
10297             (goto-char (- (search-forward "\n\n") 2)))
10298           (insert
10299            (cond 
10300             ((eq type 'local)
10301              (concat "Date: " (timezone-make-date-arpa-standard date) "\n"))
10302             ((eq type 'ut)
10303              (concat "Date: " (timezone-make-date-arpa-standard date nil "UT")
10304                      "\n"))
10305             ((eq type 'lapsed)
10306              (let* ((sec (max (- (gnus-seconds-since-epoch 
10307                                   (timezone-make-date-arpa-standard
10308                                    (current-time-string) 
10309                                    (current-time-zone) "UT"))
10310                                  (gnus-seconds-since-epoch 
10311                                   (timezone-make-date-arpa-standard 
10312                                    date nil "UT")))
10313                               0))
10314                     num prev)
10315                (concat
10316                 "X-Sent: "
10317                 (mapconcat 
10318                  (lambda (unit)
10319                    (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
10320                        ""
10321                      (setq sec (- sec (* num (cdr unit))))
10322                      (prog1
10323                          (concat (if prev ", " "") (int-to-string (floor num))
10324                                  " " (symbol-name (car unit))
10325                                  (if (> num 1) "s" ""))
10326                        (setq prev t))))
10327                  gnus-article-time-units "")
10328                 " ago\n")))
10329             (t
10330              (error "Unknown conversion type: %s" type)))))))))
10331
10332 (defun gnus-article-date-local ()
10333   "Convert the current article date to the local timezone."
10334   (interactive)
10335   (gnus-article-date-ut 'local))
10336
10337 (defun gnus-article-date-lapsed ()
10338   "Convert the current article date to time lapsed since it was sent."
10339   (interactive)
10340   (gnus-article-date-ut 'lapsed))
10341
10342 (defun gnus-article-maybe-highlight ()
10343   (if gnus-visual (gnus-article-highlight)))
10344
10345 ;; Article savers.
10346
10347 (defun gnus-output-to-rmail (file-name)
10348   "Append the current article to an Rmail file named FILE-NAME."
10349   (require 'rmail)
10350   ;; Most of these codes are borrowed from rmailout.el.
10351   (setq file-name (expand-file-name file-name))
10352   (setq rmail-default-rmail-file file-name)
10353   (let ((artbuf (current-buffer))
10354         (tmpbuf (get-buffer-create " *Gnus-output*")))
10355     (save-excursion
10356       (or (get-file-buffer file-name)
10357           (file-exists-p file-name)
10358           (if (gnus-yes-or-no-p
10359                (concat "\"" file-name "\" does not exist, create it? "))
10360               (let ((file-buffer (create-file-buffer file-name)))
10361                 (save-excursion
10362                   (set-buffer file-buffer)
10363                   (rmail-insert-rmail-file-header)
10364                   (let ((require-final-newline nil))
10365                     (write-region (point-min) (point-max) file-name t 1)))
10366                 (kill-buffer file-buffer))
10367             (error "Output file does not exist")))
10368       (set-buffer tmpbuf)
10369       (buffer-disable-undo (current-buffer))
10370       (erase-buffer)
10371       (insert-buffer-substring artbuf)
10372       (gnus-convert-article-to-rmail)
10373       ;; Decide whether to append to a file or to an Emacs buffer.
10374       (let ((outbuf (get-file-buffer file-name)))
10375         (if (not outbuf)
10376             (append-to-file (point-min) (point-max) file-name)
10377           ;; File has been visited, in buffer OUTBUF.
10378           (set-buffer outbuf)
10379           (let ((buffer-read-only nil)
10380                 (msg (and (boundp 'rmail-current-message)
10381                           (symbol-value 'rmail-current-message))))
10382             ;; If MSG is non-nil, buffer is in RMAIL mode.
10383             (if msg
10384                 (progn (widen)
10385                        (narrow-to-region (point-max) (point-max))))
10386             (insert-buffer-substring tmpbuf)
10387             (if msg
10388                 (progn
10389                   (goto-char (point-min))
10390                   (widen)
10391                   (search-backward "\^_")
10392                   (narrow-to-region (point) (point-max))
10393                   (goto-char (1+ (point-min)))
10394                   (rmail-count-new-messages t)
10395                   (rmail-show-message msg)))))))
10396     (kill-buffer tmpbuf)))
10397
10398 (defun gnus-output-to-file (file-name)
10399   "Append the current article to a file named FILE-NAME."
10400   (setq file-name (expand-file-name file-name))
10401   (let ((artbuf (current-buffer))
10402         (tmpbuf (get-buffer-create " *Gnus-output*")))
10403     (save-excursion
10404       (set-buffer tmpbuf)
10405       (buffer-disable-undo (current-buffer))
10406       (erase-buffer)
10407       (insert-buffer-substring artbuf)
10408       ;; Append newline at end of the buffer as separator, and then
10409       ;; save it to file.
10410       (goto-char (point-max))
10411       (insert "\n")
10412       (append-to-file (point-min) (point-max) file-name))
10413     (kill-buffer tmpbuf)))
10414
10415 (defun gnus-convert-article-to-rmail ()
10416   "Convert article in current buffer to Rmail message format."
10417   (let ((buffer-read-only nil))
10418     ;; Convert article directly into Babyl format.
10419     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
10420     (goto-char (point-min))
10421     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
10422     (while (search-forward "\n\^_" nil t) ;single char
10423       (replace-match "\n^_" t t))               ;2 chars: "^" and "_"
10424     (goto-char (point-max))
10425     (insert "\^_")))
10426
10427 (defun gnus-narrow-to-page (&optional arg)
10428   "Make text outside current page invisible except for page delimiter.
10429 A numeric arg specifies to move forward or backward by that many pages,
10430 thus showing a page other than the one point was originally in."
10431   (interactive "P")
10432   (setq arg (if arg (prefix-numeric-value arg) 0))
10433   (save-excursion
10434     (forward-page -1)                   ;Beginning of current page.
10435     (widen)
10436     (if (> arg 0)
10437         (forward-page arg)
10438       (if (< arg 0)
10439           (forward-page (1- arg))))
10440     ;; Find the end of the page.
10441     (forward-page)
10442     ;; If we stopped due to end of buffer, stay there.
10443     ;; If we stopped after a page delimiter, put end of restriction
10444     ;; at the beginning of that line.
10445     ;; These are commented out.
10446     ;;    (if (save-excursion (beginning-of-line)
10447     ;;                  (looking-at page-delimiter))
10448     ;;  (beginning-of-line))
10449     (narrow-to-region (point)
10450                       (progn
10451                         ;; Find the top of the page.
10452                         (forward-page -1)
10453                         ;; If we found beginning of buffer, stay there.
10454                         ;; If extra text follows page delimiter on same line,
10455                         ;; include it.
10456                         ;; Otherwise, show text starting with following line.
10457                         (if (and (eolp) (not (bobp)))
10458                             (forward-line 1))
10459                         (point)))))
10460
10461 (defun gnus-gmt-to-local ()
10462   "Rewrite Date header described in GMT to local in current buffer.
10463 Intended to be used with gnus-article-prepare-hook."
10464   (save-excursion
10465     (save-restriction
10466       (widen)
10467       (goto-char (point-min))
10468       (narrow-to-region (point-min)
10469                         (progn (search-forward "\n\n" nil 'move) (point)))
10470       (goto-char (point-min))
10471       (if (re-search-forward "^Date:[ \t]\\(.*\\)$" nil t)
10472           (let ((buffer-read-only nil)
10473                 (date (buffer-substring (match-beginning 1) (match-end 1))))
10474             (delete-region (match-beginning 1) (match-end 1))
10475             (insert
10476              (timezone-make-date-arpa-standard 
10477               date nil (current-time-zone))))))))
10478
10479
10480 ;; Article mode commands
10481
10482 (defun gnus-article-next-page (lines)
10483   "Show next page of current article.
10484 If end of article, return non-nil. Otherwise return nil.
10485 Argument LINES specifies lines to be scrolled up."
10486   (interactive "P")
10487   (move-to-window-line -1)
10488   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
10489   (if (save-excursion
10490         (end-of-line)
10491         (and (pos-visible-in-window-p)  ;Not continuation line.
10492              (eobp)))
10493       ;; Nothing in this page.
10494       (if (or (not gnus-break-pages)
10495               (save-excursion
10496                 (save-restriction
10497                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
10498           t                             ;Nothing more.
10499         (gnus-narrow-to-page 1)         ;Go to next page.
10500         nil)
10501     ;; More in this page.
10502     (condition-case ()
10503         (scroll-up lines)
10504       (end-of-buffer
10505        ;; Long lines may cause an end-of-buffer error.
10506        (goto-char (point-max))))
10507     nil))
10508
10509 (defun gnus-article-prev-page (lines)
10510   "Show previous page of current article.
10511 Argument LINES specifies lines to be scrolled down."
10512   (interactive "P")
10513   (move-to-window-line 0)
10514   (if (and gnus-break-pages
10515            (bobp)
10516            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
10517       (progn
10518         (gnus-narrow-to-page -1) ;Go to previous page.
10519         (goto-char (point-max))
10520         (recenter -1))
10521     (scroll-down lines)))
10522
10523 (defun gnus-article-refer-article ()
10524   "Read article specified by message-id around point."
10525   (interactive)
10526   (search-forward ">" nil t)    ;Move point to end of "<....>".
10527   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
10528       (let ((message-id
10529              (buffer-substring (match-beginning 1) (match-end 1))))
10530         (set-buffer gnus-summary-buffer)
10531         (gnus-summary-refer-article message-id))
10532     (error "No references around point")))
10533
10534 (defun gnus-article-show-summary ()
10535   "Reconfigure windows to show summary buffer."
10536   (interactive)
10537   (gnus-configure-windows 'article)
10538   (gnus-summary-goto-subject gnus-current-article))
10539
10540 (defun gnus-article-describe-briefly ()
10541   "Describe article mode commands briefly."
10542   (interactive)
10543   (gnus-message 6
10544    (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")))
10545
10546 (defun gnus-article-summary-command ()
10547   "Execute the last keystroke in the summary buffer."
10548   (interactive)
10549   (let ((obuf (current-buffer))
10550         (owin (current-window-configuration))
10551         func)
10552     (switch-to-buffer gnus-summary-buffer 'norecord)
10553     (setq func (lookup-key (current-local-map) (this-command-keys)))
10554     (call-interactively func)
10555     (set-buffer obuf)
10556     (let ((npoint (point)))
10557       (set-window-configuration owin)
10558       (set-window-start (get-buffer-window (current-buffer)) (point)))))
10559
10560 (defun gnus-article-summary-command-nosave ()
10561   "Execute the last keystroke in the summary buffer."
10562   (interactive)
10563   (let ((obuf (current-buffer))
10564         (owin (current-window-configuration))
10565         func)
10566     (switch-to-buffer gnus-summary-buffer 'norecord)
10567     (setq func (lookup-key (current-local-map) (this-command-keys)))
10568     (call-interactively func)))
10569
10570 ;; caesar-region written by phr@prep.ai.mit.edu  Nov 86
10571 ;; Modified by tower@prep Nov 86
10572 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
10573
10574 (defun gnus-caesar-region (&optional n)
10575   "Caesar rotation of region by N, default 13, for decrypting netnews.
10576 ROT47 will be performed for Japanese text in any case."
10577   (interactive (if current-prefix-arg   ; Was there a prefix arg?
10578                    (list (prefix-numeric-value current-prefix-arg))
10579                  (list nil)))
10580   (cond ((not (numberp n)) (setq n 13))
10581         (t (setq n (mod n 26))))        ;canonicalize N
10582   (if (not (zerop n))           ; no action needed for a rot of 0
10583       (progn
10584         (if (or (not (boundp 'caesar-translate-table))
10585                 (not caesar-translate-table)
10586                 (/= (aref caesar-translate-table ?a) (+ ?a n)))
10587             (let ((i 0) 
10588                   (lower "abcdefghijklmnopqrstuvwxyz")
10589                   upper)
10590               (gnus-message 9 "Building caesar-translate-table...")
10591               (setq caesar-translate-table (make-vector 256 0))
10592               (while (< i 256)
10593                 (aset caesar-translate-table i i)
10594                 (setq i (1+ i)))
10595               (setq lower (concat lower lower)
10596                     upper (upcase lower)
10597                     i 0)
10598               (while (< i 26)
10599                 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
10600                 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
10601                 (setq i (1+ i)))
10602               ;; ROT47 for Japanese text.
10603               ;; Thanks to ichikawa@flab.fujitsu.junet.
10604               (setq i 161)
10605               (let ((t1 (logior ?O 128))
10606                     (t2 (logior ?! 128))
10607                     (t3 (logior ?~ 128)))
10608                 (while (< i 256)
10609                   (aset caesar-translate-table i
10610                         (let ((v (aref caesar-translate-table i)))
10611                           (if (<= v t1) (if (< v t2) v (+ v 47))
10612                             (if (<= v t3) (- v 47) v))))
10613                   (setq i (1+ i))))
10614               (gnus-message 9 "Building caesar-translate-table...done")))
10615         (let ((from (region-beginning))
10616               (to (region-end))
10617               (i 0) str len)
10618           (setq str (buffer-substring from to))
10619           (setq len (length str))
10620           (while (< i len)
10621             (aset str i (aref caesar-translate-table (aref str i)))
10622             (setq i (1+ i)))
10623           (goto-char from)
10624           (delete-region from to)
10625           (insert str)))))
10626
10627 \f
10628 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
10629
10630 ;;;###autoload
10631 (defalias 'gnus-batch-kill 'gnus-batch-score)
10632 ;;;###autoload
10633 (defun gnus-batch-score ()
10634   "Run batched scoring.
10635 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
10636 Newsgroups is a list of strings in Bnews format.  If you want to score
10637 the comp hierarchy, you'd say \"comp.all\". If you would not like to
10638 score the alt hierarchy, you'd say \"!alt.all\"."
10639   (interactive)
10640   (let* ((yes-and-no
10641           (gnus-newsrc-parse-options
10642            (apply (function concat)
10643                   (mapcar (lambda (g) (concat g " "))
10644                           command-line-args-left))))
10645          (gnus-expert-user t)
10646          (nnmail-spool-file nil)
10647          (gnus-use-dribble-file nil)
10648          (yes (car yes-and-no))
10649          (no (cdr yes-and-no))
10650          group subscribed newsrc entry
10651          ;; Disable verbose message.
10652          gnus-novice-user gnus-large-newsgroup)
10653     ;; Eat all arguments.
10654     (setq command-line-args-left nil)
10655     ;; Start Gnus.
10656     (gnus)
10657     ;; Apply kills to specified newsgroups in command line arguments.
10658     (setq newsrc (cdr gnus-newsrc-alist))
10659     (while newsrc
10660       (setq group (car (car newsrc)))
10661       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
10662       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
10663                (and (car entry)
10664                     (or (eq (car entry) t)
10665                         (not (zerop (car entry)))))
10666                (if yes (string-match yes group) t)
10667                (or (null no) (not (string-match no group))))
10668           (progn
10669             (gnus-summary-read-group group nil t)
10670             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
10671                  (gnus-summary-exit))))
10672       (setq newsrc (cdr newsrc)))
10673     ;; Exit Emacs.
10674     (switch-to-buffer gnus-group-buffer)
10675     (gnus-group-save-newsrc)))
10676
10677 (defun gnus-apply-kill-file ()
10678   "Apply a kill file to the current newsgroup.
10679 Returns the number of articles marked as read."
10680   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
10681           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
10682       (gnus-apply-kill-file-internal)
10683     0))
10684
10685 (defun gnus-kill-save-kill-buffer ()
10686   (save-excursion
10687     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
10688       (if (get-file-buffer file)
10689           (progn
10690             (set-buffer (get-file-buffer file))
10691             (and (buffer-modified-p) (save-buffer))
10692             (kill-buffer (current-buffer)))))))
10693
10694 (defvar gnus-kill-file-name "KILL"
10695   "Suffix of the kill files.")
10696
10697 (defun gnus-newsgroup-kill-file (newsgroup)
10698   "Return the name of a kill file name for NEWSGROUP.
10699 If NEWSGROUP is nil, return the global kill file name instead."
10700   (cond ((or (null newsgroup)
10701              (string-equal newsgroup ""))
10702          ;; The global KILL file is placed at top of the directory.
10703          (expand-file-name gnus-kill-file-name
10704                            (or gnus-kill-files-directory "~/News")))
10705         ((gnus-use-long-file-name 'not-kill)
10706          ;; Append ".KILL" to newsgroup name.
10707          (expand-file-name (concat newsgroup "." gnus-kill-file-name)
10708                            (or gnus-kill-files-directory "~/News")))
10709         (t
10710          ;; Place "KILL" under the hierarchical directory.
10711          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
10712                                    "/" gnus-kill-file-name)
10713                            (or gnus-kill-files-directory "~/News")))))
10714
10715 \f
10716 ;;;
10717 ;;; Dribble file
10718 ;;;
10719
10720 (defvar gnus-dribble-ignore nil)
10721
10722 (defun gnus-dribble-file-name ()
10723   (concat gnus-startup-file "-dribble"))
10724
10725 (defun gnus-dribble-open ()
10726   (save-excursion 
10727     (set-buffer 
10728      (setq gnus-dribble-buffer (find-file-noselect (gnus-dribble-file-name))))
10729     (buffer-disable-undo (current-buffer))
10730     (bury-buffer gnus-dribble-buffer)
10731     (auto-save-mode t)
10732     (goto-char (point-max))))
10733
10734 (defun gnus-dribble-enter (string)
10735   (if (and (not gnus-dribble-ignore)
10736            gnus-dribble-buffer
10737            (buffer-name gnus-dribble-buffer))
10738       (let ((obuf (current-buffer)))
10739         (set-buffer gnus-dribble-buffer)
10740         (insert string "\n")
10741         (set-window-point (get-buffer-window (current-buffer)) (point-max))
10742         (set-buffer obuf))))
10743
10744 (defun gnus-dribble-read-file ()
10745   (let ((dribble-file (gnus-dribble-file-name)))
10746     (save-excursion 
10747       (set-buffer (setq gnus-dribble-buffer 
10748                         (get-buffer-create 
10749                          (file-name-nondirectory dribble-file))))
10750       (gnus-add-current-to-buffer-list)
10751       (erase-buffer)
10752       (set-visited-file-name dribble-file)
10753       (buffer-disable-undo (current-buffer))
10754       (bury-buffer (current-buffer))
10755       (set-buffer-modified-p nil)
10756       (let ((auto (make-auto-save-file-name))
10757             (gnus-dribble-ignore t))
10758         (if (or (file-exists-p auto) (file-exists-p dribble-file))
10759             (progn
10760               (if (file-newer-than-file-p auto dribble-file)
10761                   (setq dribble-file auto))
10762               (insert-file-contents dribble-file)
10763               (if (not (zerop (buffer-size)))
10764                   (set-buffer-modified-p t))
10765               (if (gnus-y-or-n-p 
10766                    "Auto-save file exists. Do you want to read it? ")
10767                   (progn
10768                     (gnus-message 5 "Reading %s..." dribble-file) 
10769                     (eval-current-buffer)
10770                     (gnus-message 5 "Reading %s...done" dribble-file)))))))))
10771
10772 (defun gnus-dribble-delete-file ()
10773   (if (file-exists-p (gnus-dribble-file-name))
10774       (delete-file (gnus-dribble-file-name)))
10775   (if gnus-dribble-buffer
10776       (save-excursion
10777         (set-buffer gnus-dribble-buffer)
10778         (let ((auto (make-auto-save-file-name)))
10779           (if (file-exists-p auto)
10780               (delete-file auto))
10781           (erase-buffer)
10782           (set-buffer-modified-p nil)))))
10783
10784 (defun gnus-dribble-save ()
10785   (if (and gnus-dribble-buffer
10786            (buffer-name gnus-dribble-buffer))
10787       (save-excursion
10788         (set-buffer gnus-dribble-buffer)
10789         (save-buffer))))
10790
10791 (defun gnus-dribble-clear ()
10792   (save-excursion
10793     (if (gnus-buffer-exists-p gnus-dribble-buffer)
10794         (progn
10795           (set-buffer gnus-dribble-buffer)
10796           (erase-buffer)
10797           (set-buffer-modified-p nil)
10798           (setq buffer-saved-size (buffer-size))))))
10799
10800 ;;;
10801 ;;; Server Communication
10802 ;;;
10803
10804 ;; All the Gnus backends have the same interface, and should return
10805 ;; data in a similar format. Below is an overview of what functions
10806 ;; these packages must supply and what results they should return.
10807 ;;
10808 ;; Variables:
10809 ;;
10810 ;; `nntp-server-buffer' - All data should be returned to Gnus in this
10811 ;; buffer. 
10812 ;;
10813 ;; Functions for the imaginary backend `choke':
10814 ;;
10815 ;; `choke-retrieve-headers ARTICLES &optional GROUP SERVER'
10816 ;; Should return all headers for all ARTICLES, or return NOV lines for
10817 ;; the same.
10818 ;;
10819 ;; `choke-request-group GROUP &optional SERVER DISCARD'
10820 ;; Switch to GROUP. If DISCARD is nil, active information on the group
10821 ;; must be returned.
10822 ;;
10823 ;; `choke-close-group GROUP &optional SERVER'
10824 ;; Close group. Most backends won't have to do anything with this
10825 ;; call, but it is an opportunity to clean up, if that is needed. It
10826 ;; is called when Gnus exits a group.
10827 ;;
10828 ;; `choke-request-article ARTICLE &optional GROUP SERVER'
10829 ;; Return ARTICLE, which is either an article number or
10830 ;; message-id. Note that not all backends can return articles based on
10831 ;; message-id. 
10832 ;;
10833 ;; `choke-request-list SERVER'
10834 ;; Return a list of all newsgroups on SERVER.
10835 ;;
10836 ;; `choke-request-list-newsgroups SERVER'
10837 ;; Return a list of descriptions of all newsgroups on SERVER.
10838 ;;
10839 ;; `choke-request-newgroups DATE &optional SERVER'
10840 ;; Return a list of all groups that have arrived after DATE on
10841 ;; SERVER. Note that the date doesn't have to be respected - Gnus will
10842 ;; always check whether the groups are old or not. Backends that do
10843 ;; not store date information may just return the entire list of
10844 ;; groups, although this might not be a good idea in general.
10845 ;;
10846 ;; `choke-request-post-buffer METHOD HEADER ARTICLE-BUFFER GROUP INFO'
10847 ;; Should return a buffer that is suitable for "posting". nnspool and
10848 ;; nntp return a `*post-buffer*', and nnmail return a `*mail*'
10849 ;; buffer. This function should fill out the appropriate headers. 
10850 ;;
10851 ;; `choke-request-post &optional SERVER'
10852 ;; Function that will be called from a buffer to be posted. 
10853 ;;
10854 ;; `choke-open-server SERVER &optional ARGUMENT'
10855 ;; Open a connection to SERVER.
10856 ;;
10857 ;; `choke-close-server &optional SERVER'
10858 ;; Close the connection to SERVER.
10859 ;;
10860 ;; `choke-server-opened &optional SERVER'
10861 ;; Whether the conenction to SERVER is opened or not.
10862 ;;
10863 ;; `choke-server-status &optional SERVER'
10864 ;; Should return a status string (not in the nntp buffer, but as the
10865 ;; result of the function).
10866 ;;
10867 ;; `choke-retrieve-groups GROUPS &optional SERVER'
10868 ;; Optional function for retrieving active file info on all groups in
10869 ;; GROUPS.  Two return formats are supported: The normal active file
10870 ;; format, and a list of GROUP lines.  This function should return (as
10871 ;; a function value) either `active' or `group', depending on what
10872 ;; format it returns.
10873 ;;
10874 ;; The following functions are optional and apply only to backends
10875 ;; that are able to control the contents of their groups totally
10876 ;; (ie. mail backends.)  Backends that aren't able to do that
10877 ;; shouldn't define these functions at all. Gnus will check for their
10878 ;; presence before attempting to call them.
10879 ;;
10880 ;; `choke-request-expire-articles ARTICLES &optional NEWSGROUP SERVER'
10881 ;; Should expire (according to some aging scheme) all ARTICLES. Most
10882 ;; backends will not be able to expire articles. Should return a list
10883 ;; of all articles that were not expired.
10884 ;;
10885 ;; `choke-request-move-article ARTICLE GROUP SERVER ACCEPT-FORM &optional LAST'
10886 ;; Should move ARTICLE from GROUP on SERVER by using ACCEPT-FORM.
10887 ;; Removes any information it has added to the article (extra headers,
10888 ;; whatever - make it as clean as possible), and then passes the
10889 ;; article on by evaling ACCEPT-FORM, which is normally a call to the
10890 ;; function described below. If the ACCEPT-FORM returns a non-nil
10891 ;; value, the article should then be deleted. If LAST is nil, that
10892 ;; means that there will be further calls to this function. This might
10893 ;; be taken as an advice not to save buffers/internal variables just
10894 ;; yet, but wait until the last call to speed things up.
10895 ;;
10896 ;; `choke-request-accept-article GROUP &optional LAST' 
10897 ;; The contents of the current buffer will be put into GROUP.  There
10898 ;; should, of course, be an article in the current buffer.  This
10899 ;; function is normally only called by the function described above,
10900 ;; and LAST works the same way as in that function.
10901 ;;
10902 ;; `choke-request-replace-article ARTICLE GROUP BUFFER'
10903 ;; Replace ARTICLE in GROUP with the contents of BUFFER.
10904 ;; This provides an easy interface for allowing editing of
10905 ;; articles. Note that even headers may be edited, so the backend has
10906 ;; to update any tables (nov buffers, etc) that it maintains after
10907 ;; replacing the article.
10908 ;;
10909 ;; `choke-request-create-group GROUP &optional SERVER'
10910 ;; Create GROUP on SERVER.  This might be a new, empty group, or it
10911 ;; might be a group that already exists, but hasn't been registered
10912 ;; yet. 
10913 ;;
10914 ;; All these functions must return nil if they couldn't service the
10915 ;; request. If the optional arguments are not supplied, some "current"
10916 ;; or "default" values should be used. In short, one should emulate an
10917 ;; NNTP server, in a way.
10918 ;;
10919 ;; If you want to write a new backend, you just have to supply the
10920 ;; functions listed above. In addition, you must enter the new backend
10921 ;; into the list of valid select methods:
10922 ;; (setq gnus-valid-select-methods 
10923 ;;       (cons '("choke" mail) gnus-valid-select-methods))
10924 ;; The first element in this list is the name of the backend. Other
10925 ;; elemnets may be `mail' (for mail groups),  `post' (for news
10926 ;; groups), `none' (neither), `respool' (for groups that can control
10927 ;; their contents). 
10928
10929 (defun gnus-start-news-server (&optional confirm)
10930   "Open a method for getting news.
10931 If CONFIRM is non-nil, the user will be asked for an NNTP server."
10932   (let (how where)
10933     (if gnus-current-select-method
10934         ;; Stream is already opened.
10935         nil
10936       ;; Open NNTP server.
10937       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
10938       (if confirm
10939           (progn
10940             ;; Read server name with completion.
10941             (setq gnus-nntp-server
10942                   (completing-read "NNTP server: "
10943                                    (mapcar (lambda (server) (list server))
10944                                            (cons (list gnus-nntp-server)
10945                                                  gnus-secondary-servers))
10946                                    nil nil gnus-nntp-server))))
10947
10948       (if (and gnus-nntp-server 
10949                (stringp gnus-nntp-server)
10950                (not (string= gnus-nntp-server "")))
10951           (setq gnus-select-method
10952                 (cond ((or (string= gnus-nntp-server "")
10953                            (string= gnus-nntp-server "::"))
10954                        (list 'nnspool (system-name)))
10955                       ((string-match "^:" gnus-nntp-server)
10956                        (list 'nnmh gnus-nntp-server 
10957                              (list 'nnmh-directory 
10958                                    (file-name-as-directory
10959                                     (expand-file-name
10960                                      (concat "~/" (substring
10961                                                    gnus-nntp-server 1)))))
10962                              (list 'nnmh-get-new-mail nil)))
10963                       (t
10964                        (list 'nntp gnus-nntp-server)))))
10965
10966       (setq how (car gnus-select-method))
10967       (setq where (car (cdr gnus-select-method)))
10968       (cond ((eq how 'nnspool)
10969              (require 'nnspool)
10970              (gnus-message 5 "Looking up local news spool..."))
10971             ((eq how 'nnmh)
10972              (require 'nnmh)
10973              (gnus-message 5 "Looking up mh spool..."))
10974             (t
10975              (require 'nntp)))
10976       (setq gnus-current-select-method gnus-select-method)
10977       (run-hooks 'gnus-open-server-hook)
10978       (or 
10979        ;; gnus-open-server-hook might have opened it
10980        (gnus-server-opened gnus-select-method)  
10981        (gnus-open-server gnus-select-method)
10982        (gnus-y-or-n-p
10983         (format
10984          "%s server on %s can't be opened. Continue? "
10985          (car gnus-select-method) (nth 1 gnus-select-method)))
10986        (progn
10987          (gnus-message 1 "Couldn't open server on %s" 
10988                        (nth 1 gnus-select-method))
10989          (ding)
10990          nil)))))
10991
10992 (defun gnus-check-news-server (&optional method)
10993   "If the news server is down, start it up again."
10994   (let ((method (if method method gnus-select-method)))
10995     (and (stringp method)
10996          (setq method (gnus-server-to-method method)))
10997     (if (gnus-server-opened method)
10998         ;; Stream is already opened.
10999         t
11000       ;; Open server.
11001       (gnus-message 5 "Opening server %s on %s..." (car method) (nth 1 method))
11002       (run-hooks 'gnus-open-server-hook)
11003       (or (gnus-server-opened method)
11004           (gnus-open-server method))
11005       (message ""))))
11006
11007 (defun gnus-nntp-message (&optional message)
11008   "Check the status of the NNTP server.
11009 If the status of the server is clear and MESSAGE is non-nil, MESSAGE
11010 is returned insted of the status string."
11011   (let ((status (gnus-status-message (gnus-find-method-for-group 
11012                                       gnus-newsgroup-name)))
11013         (message (or message "")))
11014     (if (and (stringp status) (> (length status) 0))
11015         status message)))
11016
11017 (defun gnus-get-function (method function)
11018   (and (stringp method)
11019        (setq method (gnus-server-to-method method)))
11020   (let ((func (intern (format "%s-%s" (car method) function))))
11021     (if (not (fboundp func)) 
11022         (progn
11023           (require (car method))
11024           (if (not (fboundp func)) 
11025               (error "No such function: %s" func))))
11026     func))
11027
11028 ;;; Interface functions to the backends.
11029
11030 (defun gnus-open-server (method)
11031   (funcall (gnus-get-function method 'open-server)
11032            (nth 1 method) (nthcdr 2 method)))
11033
11034 (defun gnus-close-server (method)
11035   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
11036
11037 (defun gnus-request-list (method)
11038   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
11039
11040 (defun gnus-request-list-newsgroups (method)
11041   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
11042
11043 (defun gnus-request-newgroups (date method)
11044   (funcall (gnus-get-function method 'request-newgroups) 
11045            date (nth 1 method)))
11046
11047 (defun gnus-server-opened (method)
11048   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
11049
11050 (defun gnus-status-message (method)
11051   (let ((method (if (stringp method) (gnus-find-method-for-group method)
11052                   method)))
11053     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
11054
11055 (defun gnus-request-group (group &optional dont-check)
11056   (let ((method (gnus-find-method-for-group group)))
11057 ;    (and t (message "%s GROUP %s" (car method) group))
11058     (funcall (gnus-get-function method 'request-group) 
11059              (gnus-group-real-name group) (nth 1 method) dont-check)))
11060
11061 (defun gnus-request-asynchronous (group &optional articles)
11062   (let ((method (gnus-find-method-for-group group)))
11063     (funcall (gnus-get-function method 'request-asynchronous) 
11064              (gnus-group-real-name group) (nth 1 method) articles)))
11065
11066 (defun gnus-list-active-group (group)
11067   (let ((method (gnus-find-method-for-group group))
11068         (func 'list-active-group))
11069     (and (gnus-check-backend-function func group)
11070          (funcall (gnus-get-function method func) 
11071                   (gnus-group-real-name group) (nth 1 method)))))
11072
11073 (defun gnus-request-group-description (group)
11074   (let ((method (gnus-find-method-for-group group))
11075         (func 'request-group-description))
11076     (and (gnus-check-backend-function func group)
11077          (funcall (gnus-get-function method func) 
11078                   (gnus-group-real-name group) (nth 1 method)))))
11079
11080 (defun gnus-close-group (group)
11081   (let ((method (gnus-find-method-for-group group)))
11082     (funcall (gnus-get-function method 'close-group) 
11083              (gnus-group-real-name group) (nth 1 method))))
11084
11085 (defun gnus-retrieve-headers (articles group)
11086   (let ((method (gnus-find-method-for-group group)))
11087     (if gnus-use-cache
11088         (gnus-cache-retrieve-headers articles group)
11089       (funcall (gnus-get-function method 'retrieve-headers) 
11090                articles (gnus-group-real-name group) (nth 1 method)))))
11091
11092 (defun gnus-retrieve-groups (groups method)
11093   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
11094
11095 (defun gnus-request-article (article group &optional buffer)
11096   (let ((method (gnus-find-method-for-group group)))
11097     (funcall (gnus-get-function method 'request-article) 
11098              article (gnus-group-real-name group) (nth 1 method) buffer)))
11099
11100 (defun gnus-request-head (article group)
11101   (let ((method (gnus-find-method-for-group group)))
11102     (funcall (gnus-get-function method 'request-head) 
11103              article (gnus-group-real-name group) (nth 1 method))))
11104
11105 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11106 (defun gnus-request-post-buffer (post group subject header artbuf
11107                                       info follow-to respect-poster)
11108    (let* ((info (or info (and group (nth 2 (gnus-gethash 
11109                                             group gnus-newsrc-hashtb)))))
11110           (method
11111            (if (and gnus-post-method
11112                     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11113                     (memq 'post (assoc
11114                                  (format "%s" (car (gnus-find-method-for-group
11115                                                     gnus-newsgroup-name)))
11116                                         gnus-valid-select-methods)))
11117                gnus-post-method
11118              (gnus-find-method-for-group gnus-newsgroup-name))))
11119      (or (gnus-server-opened method)
11120          (gnus-open-server method)
11121          (error "Can't open server %s:%s" (car method) (nth 1 method)))
11122      (let ((mail-self-blind nil)
11123            (mail-archive-file-name nil))
11124        (funcall (gnus-get-function method 'request-post-buffer) 
11125                 post group subject header artbuf info follow-to
11126                 respect-poster))))
11127
11128 (defun gnus-request-post (method &optional force)
11129   (and (stringp method)
11130        (setq method (gnus-server-to-method method)))
11131   (and (not force) gnus-post-method
11132        (memq 'post (assoc (format "%s" (car method))
11133                           gnus-valid-select-methods))
11134        (setq method gnus-post-method))
11135   (funcall (gnus-get-function method 'request-post) 
11136            (nth 1 method)))
11137
11138 (defun gnus-request-expire-articles (articles group &optional force)
11139   (let ((method (gnus-find-method-for-group group)))
11140     (funcall (gnus-get-function method 'request-expire-articles) 
11141              articles (gnus-group-real-name group) (nth 1 method)
11142              force)))
11143
11144 (defun gnus-request-move-article 
11145   (article group server accept-function &optional last)
11146   (let ((method (gnus-find-method-for-group group)))
11147     (funcall (gnus-get-function method 'request-move-article) 
11148              article (gnus-group-real-name group) 
11149              (nth 1 method) accept-function last)))
11150
11151 (defun gnus-request-accept-article (group &optional last)
11152   (let ((func (if (symbolp group) group
11153                 (car (gnus-find-method-for-group group)))))
11154     (funcall (intern (format "%s-request-accept-article" func))
11155              (if (stringp group) (gnus-group-real-name group) group)
11156              last)))
11157
11158 (defun gnus-request-replace-article (article group buffer)
11159   (let ((func (car (gnus-find-method-for-group group))))
11160     (funcall (intern (format "%s-request-replace-article" func))
11161              article (gnus-group-real-name group) buffer)))
11162
11163 (defun gnus-request-create-group (group)
11164   (let ((method (gnus-find-method-for-group group)))
11165     (funcall (gnus-get-function method 'request-create-group) 
11166              (gnus-group-real-name group) (nth 1 method))))
11167
11168 (defun gnus-member-of-valid (symbol group)
11169   (memq symbol (assoc
11170                 (format "%s" (car (gnus-find-method-for-group group)))
11171                 gnus-valid-select-methods)))
11172
11173 (defun gnus-find-method-for-group (group &optional info)
11174   (or gnus-override-method
11175       (and (not group)
11176            gnus-select-method)
11177       (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
11178             method)
11179         (if (or (not info)
11180                 (not (setq method (nth 4 info))))
11181             (setq method gnus-select-method)
11182           (setq method
11183                 (cond ((stringp method)
11184                        (gnus-server-to-method method))
11185                       ((stringp (car method))
11186                        (gnus-server-extend-method group method))
11187                       (t
11188                        method))))
11189         (gnus-server-add-address method))))
11190
11191 (defun gnus-check-backend-function (func group)
11192   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
11193                  group)))
11194     (fboundp (intern (format "%s-%s" method func)))))
11195
11196 (defun gnus-methods-using (method)
11197   (let ((valids gnus-valid-select-methods)
11198         outs)
11199     (while valids
11200       (if (memq method (car valids)) 
11201           (setq outs (cons (car valids) outs)))
11202       (setq valids (cdr valids)))
11203     outs))
11204
11205 ;;; 
11206 ;;; Active & Newsrc File Handling
11207 ;;;
11208
11209 ;; Newsrc related functions.
11210 ;; Gnus internal format of gnus-newsrc-alist:
11211 ;; (("alt.general" 3 (1 . 1))
11212 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
11213 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
11214 ;; The first item is the group name; the second is the subscription
11215 ;; level; the third is either a range of a list of ranges of read
11216 ;; articles, the optional fourth element is a list of marked articles,
11217 ;; the optional fifth element is the select method.
11218 ;;
11219 ;; Gnus internal format of gnus-newsrc-hashtb:
11220 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
11221 ;; This is the entry for "alt.misc". The first element is the number
11222 ;; of unread articles in "alt.misc". The cdr of this entry is the
11223 ;; element *before* "alt.misc" in gnus-newsrc-alist, which makes is
11224 ;; trivial to remove or add new elements into gnus-newsrc-alist
11225 ;; without scanning the entire list. So, to get the actual information
11226 ;; of "alt.misc", you'd say something like 
11227 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
11228 ;;
11229 ;; Gnus internal format of gnus-active-hashtb:
11230 ;; ((1 . 1))
11231 ;;  (5 . 10))
11232 ;;  (67 . 99)) ...)
11233 ;; The only element in each entry in this hash table is a range of
11234 ;; (possibly) available articles. (Articles in this range may have
11235 ;; been expired or cancelled.)
11236 ;;
11237 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
11238 ;; ("alt.misc" "alt.test" "alt.general" ...)
11239
11240 (defun gnus-setup-news (&optional rawfile level)
11241   "Setup news information.
11242 If RAWFILE is non-nil, the .newsrc file will also be read.
11243 If LEVEL is non-nil, the news will be set up at level LEVEL."
11244   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
11245     ;; Clear some variables to re-initialize news information.
11246     (if init (setq gnus-newsrc-alist nil gnus-active-hashtb nil))
11247
11248     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
11249     (if init (gnus-read-newsrc-file rawfile))
11250
11251     ;; Read the active file and create `gnus-active-hashtb'.
11252     ;; If `gnus-read-active-file' is nil, then we just create an empty
11253     ;; hash table. The partial filling out of the hash table will be
11254     ;; done in `gnus-get-unread-articles'.
11255     (if (and gnus-read-active-file 
11256              (not level)
11257              (gnus-server-opened gnus-select-method))
11258         (gnus-read-active-file)
11259       (setq gnus-active-hashtb (make-vector 4095 0)))
11260
11261     (and init gnus-use-dribble-file (gnus-dribble-read-file))
11262
11263     ;; Find the number of unread articles in each non-dead group.
11264     (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))
11265     ;; Find new newsgroups and treat them.
11266     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level)
11267              (gnus-server-opened gnus-select-method))
11268         (gnus-find-new-newsgroups))
11269     (if (and init gnus-check-bogus-newsgroups 
11270              gnus-read-active-file (not level)
11271              (gnus-server-opened gnus-select-method))
11272         (gnus-check-bogus-newsgroups))))
11273
11274 (defun gnus-find-new-newsgroups ()
11275   "Search for new newsgroups and add them.
11276 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
11277 The `-n' option line from .newsrc is respected."
11278   (interactive)
11279   (or (gnus-check-first-time-used)
11280       (if (or (consp gnus-check-new-newsgroups)
11281               (eq gnus-check-new-newsgroups 'ask-server))
11282           (gnus-ask-server-for-new-groups)
11283         (let ((groups 0)
11284               group new-newsgroups)
11285           (or gnus-have-read-active-file (gnus-read-active-file))
11286           (setq gnus-newsrc-last-checked-date (current-time-string))
11287           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
11288           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
11289           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
11290           (mapatoms
11291            (lambda (sym)
11292              (setq group (symbol-name sym))
11293              (if (or (gnus-gethash group gnus-killed-hashtb)
11294                      (gnus-gethash group gnus-newsrc-hashtb))
11295                  ()
11296                (let ((do-sub (gnus-matches-options-n group)))
11297                  (cond ((eq do-sub 'subscribe)
11298                         (setq groups (1+ groups))
11299                         (gnus-sethash group group gnus-killed-hashtb)
11300                         (funcall 
11301                          gnus-subscribe-options-newsgroup-method group))
11302                        ((eq do-sub 'ignore)
11303                         nil)
11304                        (t
11305                         (setq groups (1+ groups))
11306                         (gnus-sethash group group gnus-killed-hashtb)
11307                         (if gnus-subscribe-hierarchical-interactive
11308                             (setq new-newsgroups (cons group new-newsgroups))
11309                           (funcall gnus-subscribe-newsgroup-method group)))))))
11310            gnus-active-hashtb)
11311           (if new-newsgroups 
11312               (gnus-subscribe-hierarchical-interactive new-newsgroups))
11313           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11314           (if (> groups 0)
11315               (gnus-message 6 "%d new newsgroup%s arrived." 
11316                             groups (if (> groups 1) "s have" " has")))))))
11317
11318 (defun gnus-matches-options-n (group)
11319   ;; Returns `subscribe' if the group is to be uncoditionally
11320   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
11321   ;; no match for the group.
11322
11323   ;; First we check the two user variables.
11324   (cond
11325    ((and gnus-options-subscribe
11326          (string-match gnus-options-not-subscribe group))
11327     'subscribe)
11328    ((and gnus-options-not-subscribe
11329          (string-match gnus-options-subscribe group))
11330     'ignore)
11331    ;; Then we go through the list that was retrieved from the .newsrc
11332    ;; file.  This list has elements on the form 
11333    ;; `(REGEXP . {ignore,subscribe})'. The first match found (the list
11334    ;; is in the reverse order of the options line) is returned.
11335    (t
11336     (let ((regs gnus-newsrc-options-n))
11337       (while (and regs
11338                   (not (string-match (car (car regs)) group)))
11339         (setq regs (cdr regs)))
11340       (and regs (cdr (car regs)))))))
11341
11342 (defun gnus-ask-server-for-new-groups ()
11343   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
11344          (methods (cons gnus-select-method 
11345                         (append
11346                          (and (consp gnus-check-new-newsgroups)
11347                               gnus-check-new-newsgroups)
11348                          gnus-secondary-select-methods)))
11349          (groups 0)
11350          (new-date (current-time-string))
11351          hashtb group new-newsgroups got-new)
11352     ;; Go thorugh both primary and secondary select methods and
11353     ;; request new newsgroups.  
11354     (while methods
11355       (and (or (gnus-server-opened (car methods))
11356                (gnus-open-server (car methods)))
11357            (gnus-request-newgroups date (car methods))
11358            (save-excursion
11359              (setq got-new t)
11360              (set-buffer nntp-server-buffer)
11361              (or hashtb (setq hashtb (gnus-make-hashtable 
11362                                       (count-lines (point-min) (point-max)))))
11363              ;; Enter all the new groups in a hashtable.
11364              (gnus-active-to-gnus-format (car methods) hashtb)))
11365       (setq methods (cdr methods)))
11366     (and got-new (setq gnus-newsrc-last-checked-date new-date))
11367     ;; Now all new groups from all select methods are in `hashtb'.
11368     (mapatoms
11369      (lambda (group-sym)
11370        (setq group (symbol-name group-sym))
11371        (if (or (gnus-gethash group gnus-newsrc-hashtb)
11372                (member group gnus-zombie-list)
11373                (member group gnus-killed-list))
11374            ;; The group is already known.
11375            ()
11376          (gnus-sethash group (symbol-value group-sym) gnus-active-hashtb)
11377          (let ((do-sub (gnus-matches-options-n group)))
11378            (cond ((eq do-sub 'subscribe)
11379                   (setq groups (1+ groups))
11380                   (gnus-sethash group group gnus-killed-hashtb)
11381                   (funcall 
11382                    gnus-subscribe-options-newsgroup-method group))
11383                  ((eq do-sub 'ignore)
11384                   nil)
11385                  (t
11386                   (setq groups (1+ groups))
11387                   (gnus-sethash group group gnus-killed-hashtb)
11388                   (if gnus-subscribe-hierarchical-interactive
11389                       (setq new-newsgroups (cons group new-newsgroups))
11390                     (funcall gnus-subscribe-newsgroup-method group)))))))
11391      hashtb)
11392     (if new-newsgroups 
11393         (gnus-subscribe-hierarchical-interactive new-newsgroups))
11394     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11395     (if (> groups 0)
11396         (gnus-message 6 "%d new newsgroup%s arrived." 
11397                       groups (if (> groups 1) "s have" " has")))
11398     got-new))
11399
11400 (defun gnus-check-first-time-used ()
11401   (if (or (> (length gnus-newsrc-alist) 1)
11402           (file-exists-p gnus-startup-file)
11403           (file-exists-p (concat gnus-startup-file ".el"))
11404           (file-exists-p (concat gnus-startup-file ".eld")))
11405       nil
11406     (gnus-message 6 "First time user; subscribing you to default groups")
11407     (or gnus-have-read-active-file (gnus-read-active-file))
11408     (setq gnus-newsrc-last-checked-date (current-time-string))
11409     (let ((groups gnus-default-subscribed-newsgroups)
11410           group)
11411       (if (eq groups t)
11412           nil
11413         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
11414         (mapatoms
11415          (lambda (sym)
11416            (setq group (symbol-name sym))
11417            (let ((do-sub (gnus-matches-options-n group)))
11418              (cond ((eq do-sub 'subscribe)
11419                     (gnus-sethash group group gnus-killed-hashtb)
11420                     (funcall 
11421                      gnus-subscribe-options-newsgroup-method group))
11422                    ((eq do-sub 'ignore)
11423                     nil)
11424                    (t
11425                     (setq gnus-killed-list (cons group gnus-killed-list))))))
11426          gnus-active-hashtb)
11427         (while groups
11428           (if (gnus-gethash (car groups) gnus-active-hashtb)
11429               (gnus-group-change-level 
11430                (car groups) gnus-level-default-subscribed gnus-level-killed))
11431           (setq groups (cdr groups)))
11432         (gnus-group-make-help-group)
11433         (and gnus-novice-user
11434              (gnus-message 7 "`A k' to list killed groups"))))))
11435
11436 (defun gnus-subscribe-group (group previous &optional method)
11437   (gnus-group-change-level 
11438    (if method
11439        (list t group gnus-level-default-subscribed nil nil method)
11440      group) 
11441    gnus-level-default-subscribed gnus-level-killed previous t))
11442
11443 ;; `gnus-group-change-level' is the fundamental function for changing
11444 ;; subscription levels of newsgroups. This might mean just changing
11445 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
11446 ;; again, which subscribes/unsubscribes a group, which is equally
11447 ;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
11448 ;; from 8-9 to 1-7 means that you remove the group from the list of
11449 ;; killed (or zombie) groups and add them to the (kinda) subscribed
11450 ;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
11451 ;; which is trivial.
11452 ;; ENTRY can either be a string (newsgroup name) or a list (if
11453 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
11454 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
11455 ;; entries. 
11456 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
11457 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
11458 ;; after. 
11459 (defun gnus-group-change-level (entry level &optional oldlevel
11460                                       previous fromkilled)
11461   (let ((pinfo entry)
11462         group info active num)
11463     ;; Glean what info we can from the arguments
11464     (if (consp entry)
11465         (if fromkilled (setq group (nth 1 entry))
11466           (setq group (car (nth 2 entry))))
11467       (setq group entry))
11468     (if (and (stringp entry)
11469              oldlevel 
11470              (< oldlevel gnus-level-zombie))
11471         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
11472     (if (and (not oldlevel)
11473              (consp entry))
11474         (setq oldlevel (car (cdr (nth 2 entry)))))
11475     (if (stringp previous)
11476         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
11477
11478     (gnus-dribble-enter
11479      (format "(gnus-group-change-level %S %S %S %S %S)" 
11480              group level oldlevel (car (nth 2 previous)) fromkilled))
11481     
11482     ;; Then we remove the newgroup from any old structures, if needed.
11483     ;; If the group was killed, we remove it from the killed or zombie
11484     ;; list. If not, and it is in fact going to be killed, we remove
11485     ;; it from the newsrc hash table and assoc.
11486     (cond ((>= oldlevel gnus-level-zombie)
11487            (if (= oldlevel gnus-level-zombie)
11488                (setq gnus-zombie-list (delete group gnus-zombie-list))
11489              (setq gnus-killed-list (delete group gnus-killed-list))))
11490           (t
11491            (if (>= level gnus-level-zombie)
11492                (progn
11493                  (gnus-sethash (car (nth 2 entry))
11494                                nil gnus-newsrc-hashtb)
11495                  (if (nth 3 entry)
11496                      (setcdr (gnus-gethash (car (nth 3 entry))
11497                                            gnus-newsrc-hashtb)
11498                              (cdr entry)))
11499                  (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
11500
11501     ;; Finally we enter (if needed) the list where it is supposed to
11502     ;; go, and change the subscription level. If it is to be killed,
11503     ;; we enter it into the killed or zombie list.
11504     (cond ((>= level gnus-level-zombie)
11505            ;; Remove from the hash table.
11506            (gnus-sethash group nil gnus-newsrc-hashtb)
11507            (or (gnus-group-foreign-p group)
11508                ;; We do not enter foreign groups into the list of dead
11509                ;; groups.  
11510                (if (= level gnus-level-zombie)
11511                    (setq gnus-zombie-list (cons group gnus-zombie-list))
11512                  (setq gnus-killed-list (cons group gnus-killed-list)))))
11513           (t
11514            ;; If the list is to be entered into the newsrc assoc, and
11515            ;; it was killed, we have to create an entry in the newsrc
11516            ;; hashtb format and fix the pointers in the newsrc assoc.
11517            (if (>= oldlevel gnus-level-zombie)
11518                (progn
11519                  (if (listp entry)
11520                      (progn
11521                        (setq info (cdr entry))
11522                        (setq num (car entry)))
11523                    (setq active (gnus-gethash group gnus-active-hashtb))
11524                    (setq num (if active (- (1+ (cdr active)) (car active)) t))
11525                    ;; Check whether the group is foreign. If so, the
11526                    ;; foreign select method has to be entered into the
11527                    ;; info. 
11528                    (let ((method (gnus-group-method-name group)))
11529                      (if (eq method gnus-select-method)
11530                          (setq info (list group level nil))
11531                        (setq info (list group level nil nil method)))))
11532                  (or previous 
11533                      (setq previous 
11534                            (let ((p gnus-newsrc-alist))
11535                              (while (cdr (cdr p))
11536                                (setq p (cdr p)))
11537                              p)))
11538                  (setq entry (cons info (cdr (cdr previous))))
11539                  (if (cdr previous)
11540                      (progn
11541                        (setcdr (cdr previous) entry)
11542                        (gnus-sethash group (cons num (cdr previous)) 
11543                                      gnus-newsrc-hashtb))
11544                    (setcdr previous entry)
11545                    (gnus-sethash group (cons num previous)
11546                                  gnus-newsrc-hashtb))
11547                  (if (cdr entry)
11548                      (setcdr (gnus-gethash (car (car (cdr entry)))
11549                                            gnus-newsrc-hashtb)
11550                              entry)))
11551              ;; It was alive, and it is going to stay alive, so we
11552              ;; just change the level and don't change any pointers or
11553              ;; hash table entries.
11554              (setcar (cdr (car (cdr (cdr entry)))) level))))))
11555
11556 (defun gnus-kill-newsgroup (newsgroup)
11557   "Obsolete function. Kills a newsgroup."
11558   (gnus-group-change-level
11559    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
11560
11561 (defun gnus-check-bogus-newsgroups (&optional confirm)
11562   "Remove bogus newsgroups.
11563 If CONFIRM is non-nil, the user has to confirm the deletion of every
11564 newsgroup." 
11565   (let ((newsrc (cdr gnus-newsrc-alist))
11566         bogus group entry)
11567     (gnus-message 5 "Checking bogus newsgroups...")
11568     (or gnus-have-read-active-file (gnus-read-active-file))
11569     ;; Find all bogus newsgroup that are subscribed.
11570     (while newsrc
11571       (setq group (car (car newsrc)))
11572       (if (or (gnus-gethash group gnus-active-hashtb) ; Active
11573               (nth 4 (car newsrc))      ; Foreign
11574               (and confirm
11575                    (not (gnus-y-or-n-p
11576                          (format "Remove bogus newsgroup: %s " group)))))
11577           ;; Don't remove.
11578           ()
11579         ;; Found a bogus newsgroup.
11580         (setq bogus (cons group bogus)))
11581       (setq newsrc (cdr newsrc)))
11582     ;; Remove all bogus subscribed groups by first killing them, and
11583     ;; then removing them from the list of killed groups.
11584     (while bogus
11585       (and (setq entry (gnus-gethash (car bogus) gnus-newsrc-hashtb))
11586            (progn
11587              (gnus-group-change-level entry gnus-level-killed)
11588              (setq gnus-killed-list (delete (car bogus) gnus-killed-list))))
11589       (setq bogus (cdr bogus)))
11590     ;; Then we remove all bogus groups from the list of killed and
11591     ;; zombie groups. They are are removed without confirmation.
11592     (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
11593           killed)
11594       (while dead-lists
11595         (setq killed (symbol-value (car dead-lists)))
11596         (while killed
11597           (setq group (car killed))
11598           (or (gnus-gethash group gnus-active-hashtb)
11599               ;; The group is bogus.
11600               (set (car dead-lists)
11601                    (delete group (symbol-value (car dead-lists)))))
11602           (setq killed (cdr killed)))
11603         (setq dead-lists (cdr dead-lists))))
11604     (gnus-message 5 "Checking bogus newsgroups...done")))
11605
11606 (defun gnus-check-duplicate-killed-groups ()
11607   "Remove duplicates from the list of killed groups."
11608   (interactive)
11609   (let ((killed gnus-killed-list))
11610     (while killed
11611       (gnus-message 9 "%d" (length killed))
11612       (setcdr killed (delete (car killed) (cdr killed)))
11613       (setq killed (cdr killed)))))
11614
11615 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
11616 ;; and compute how many unread articles there are in each group.
11617 (defun gnus-get-unread-articles (&optional level)
11618   (let* ((newsrc (cdr gnus-newsrc-alist))
11619          (conditional level)
11620          (level (or level (1+ gnus-level-subscribed)))
11621          info group active virtuals method)
11622     (gnus-message 5 "Checking new news...")
11623     (while newsrc
11624       (setq info (car newsrc))
11625       (setq group (car info))
11626       (setq active (gnus-gethash group gnus-active-hashtb))
11627
11628       ;; Check newsgroups. If the user doesn't want to check them, or
11629       ;; they can't be checked (for instance, if the news server can't
11630       ;; be reached) we just set the number of unread articles in this
11631       ;; newsgroup to t. This means that Gnus thinks that there are
11632       ;; unread articles, but it has no idea how many.
11633       (if (and (setq method (nth 4 info))
11634                (not (gnus-server-equal gnus-select-method
11635                                        (gnus-server-get-method nil method))))
11636           (if (or (and gnus-activate-foreign-newsgroups 
11637                        (not (numberp gnus-activate-foreign-newsgroups)))
11638                   (and (numberp gnus-activate-foreign-newsgroups)
11639                        (<= (nth 1 info) gnus-activate-foreign-newsgroups)
11640                        (<= (nth 1 info) level)))
11641               (if (eq (car (if (stringp method) 
11642                                (gnus-server-to-method method)
11643                              (nth 4 info))) 'nnvirtual)
11644                   (setq virtuals (cons info virtuals))
11645                 (setq active (gnus-activate-newsgroup (car info)))))
11646         (if (and (not gnus-read-active-file)
11647                  (<= (nth 1 info) level))
11648             (progn
11649               (setq active (gnus-activate-newsgroup (car info))))))
11650       
11651       (or active (progn (gnus-sethash group nil gnus-active-hashtb)
11652                         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
11653       (and active 
11654            (gnus-get-unread-articles-in-group info active)
11655            ;; Close the groups as we look at them!
11656            (gnus-close-group group))
11657       (setq newsrc (cdr newsrc)))
11658
11659     ;; Activate the virtual groups. This has to be done after all the
11660     ;; other groups. 
11661     ;; !!! If one virtual group contains another virtual group, even
11662     ;; doing it this way might cause problems.
11663    (while virtuals
11664       (and (setq active (gnus-activate-newsgroup (car (car virtuals))))
11665            (gnus-get-unread-articles-in-group (car virtuals) active))
11666       (setq virtuals (cdr virtuals)))
11667
11668     (gnus-message 5 "Checking new news...done")))
11669
11670 ;; Create a hash table out of the newsrc alist. The `car's of the
11671 ;; alist elements are used as keys.
11672 (defun gnus-make-hashtable-from-newsrc-alist ()
11673   (let ((alist gnus-newsrc-alist)
11674         (ohashtb gnus-newsrc-hashtb)
11675         prev)
11676     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
11677     (setq alist 
11678           (setq prev (setq gnus-newsrc-alist 
11679                            (if (equal (car (car gnus-newsrc-alist))
11680                                       "dummy.group")
11681                                gnus-newsrc-alist
11682                              (cons (list "dummy.group" 0 nil) alist)))))
11683     (while alist
11684       (gnus-sethash (car (car alist)) 
11685                     (cons (and ohashtb (car (gnus-gethash 
11686                                              (car (car alist)) ohashtb))) 
11687                           prev) gnus-newsrc-hashtb)
11688       (setq prev alist
11689             alist (cdr alist)))))
11690
11691 (defun gnus-make-hashtable-from-killed ()
11692   "Create a hash table from the killed and zombie lists."
11693   (let ((lists '(gnus-killed-list gnus-zombie-list))
11694         list)
11695     (setq gnus-killed-hashtb 
11696           (gnus-make-hashtable 
11697            (+ (length gnus-killed-list) (length gnus-zombie-list))))
11698     (while lists
11699       (setq list (symbol-value (car lists)))
11700       (setq lists (cdr lists))
11701       (while list
11702         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
11703         (setq list (cdr list))))))
11704
11705 (defun gnus-get-unread-articles-in-group (info active)
11706   (let* ((range (nth 2 info))
11707          (num 0)
11708          (marked (nth 3 info))
11709          srange lowest group highest)
11710     ;; If a cache is present, we may have to alter the active info.
11711     (and gnus-use-cache
11712          (gnus-cache-possibly-alter-active (car info) active))
11713     ;; Modify the list of read articles according to what articles 
11714     ;; are available; then tally the unread articles and add the
11715     ;; number to the group hash table entry.
11716     (cond ((zerop (cdr active))
11717            (setq num 0))
11718           ((not range)
11719            (setq num (- (1+ (cdr active)) (car active))))
11720           ((not (listp (cdr range)))
11721            ;; Fix a single (num . num) range according to the
11722            ;; active hash table.
11723            ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
11724            (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
11725            (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
11726            ;; Compute number of unread articles.
11727            (setq num (max 0 (- (cdr active) 
11728                                (- (1+ (cdr range)) (car range))))))
11729           (t
11730            ;; The read list is a list of ranges. Fix them according to
11731            ;; the active hash table.
11732            ;; First peel off any elements that are below the lower
11733            ;; active limit. 
11734            (while (and (cdr range) 
11735                        (>= (car active) 
11736                            (or (and (atom (car (cdr range))) (car (cdr range)))
11737                                (car (car (cdr range))))))
11738              (if (numberp (car range))
11739                  (setcar range 
11740                          (cons (car range) 
11741                                (or (and (numberp (car (cdr range)))
11742                                         (car (cdr range))) 
11743                                    (cdr (car (cdr range))))))
11744                (setcdr (car range) 
11745                        (or (and (numberp (nth 1 range)) (nth 1 range))
11746                            (cdr (car (cdr range))))))
11747              (setcdr range (cdr (cdr range))))
11748            ;; Adjust the first element to be the same as the lower limit. 
11749            (if (and (not (atom (car range))) 
11750                     (< (cdr (car range)) (car active)))
11751                (setcdr (car range) (1- (car active))))
11752            ;; Then we want to peel off any elements that are higher
11753            ;; than the upper active limit.  
11754            (let ((srange range))
11755              ;; Go past all legal elements.
11756              (while (and (cdr srange) 
11757                          (<= (or (and (atom (car (cdr srange)))
11758                                       (car (cdr srange)))
11759                                  (car (car (cdr srange)))) (cdr active)))
11760                (setq srange (cdr srange)))
11761              (if (cdr srange)
11762                  ;; Nuke all remaining illegal elements.
11763                  (setcdr srange nil))
11764
11765              ;; Adjust the final element.
11766              (if (and (not (atom (car srange)))
11767                       (> (cdr (car srange)) (cdr active)))
11768                  (setcdr (car srange) (cdr active))))
11769            ;; Compute the number of unread articles.
11770            (while range
11771              (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
11772                                          (cdr (car range))))
11773                                  (or (and (atom (car range)) (car range))
11774                                      (car (car range))))))
11775              (setq range (cdr range)))
11776            (setq num (max 0 (- (cdr active) num)))))
11777     (and info
11778          (progn
11779            (and (assq 'tick marked)
11780                 (inline (gnus-remove-illegal-marked-articles
11781                          (assq 'tick marked) (nth 2 info))))
11782            (and (assq 'dormant marked)
11783                 (inline (gnus-remove-illegal-marked-articles
11784                          (assq 'dormant marked) (nth 2 info))))
11785            (setcar
11786             (gnus-gethash (car info) gnus-newsrc-hashtb) 
11787             (setq num (max 0 (- num (length (cdr (assq 'tick marked)))
11788                                 (length (cdr (assq 'dormant marked)))))))))
11789     num))
11790
11791 (defun gnus-remove-illegal-marked-articles (marked ranges)
11792   (let ((m (cdr marked)))
11793     ;; Make sure that all ticked articles are a subset of the unread
11794     ;; articles. 
11795     (while m
11796       (if (gnus-member-of-range (car m) ranges)
11797           (setcdr marked (cdr m))
11798         (setq marked m))
11799       (setq m (cdr m)))))
11800
11801 (defun gnus-activate-newsgroup (group)
11802   (let ((method (gnus-find-method-for-group group))
11803         active)
11804     (and (or (gnus-server-opened method) (gnus-open-server method))
11805          (gnus-request-group group)
11806          (save-excursion
11807            (set-buffer nntp-server-buffer)
11808            (goto-char (point-min))
11809            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
11810                 (progn
11811                   (goto-char (match-beginning 1))
11812                   (gnus-sethash 
11813                    group (setq active (cons (read (current-buffer))
11814                                             (read (current-buffer))))
11815                    gnus-active-hashtb))
11816                 active)))))
11817
11818 (defun gnus-update-read-articles 
11819   (group unread unselected ticked &optional domarks replied expirable killed
11820          dormant bookmark score)
11821   "Update the list of read and ticked articles in GROUP using the
11822 UNREAD and TICKED lists.
11823 Note: UNSELECTED has to be sorted over `<'.
11824 Returns whether the updating was successful."
11825   (let* ((active (or gnus-newsgroup-active 
11826                      (gnus-gethash group gnus-active-hashtb)))
11827          (entry (gnus-gethash group gnus-newsrc-hashtb))
11828          (number (car entry))
11829          (info (nth 2 entry))
11830          (marked (nth 3 info))
11831          (prev 1)
11832          (unread (sort (copy-sequence unread) (function <)))
11833          last read)
11834     (if (or (not info) (not active))
11835         ;; There is no info on this group if it was, in fact,
11836         ;; killed. Gnus stores no information on killed groups, so
11837         ;; there's nothing to be done. 
11838         ;; One could store the information somewhere temporarily,
11839         ;; perhaps... Hmmm... 
11840         ()
11841       ;; Remove any negative articles numbers.
11842       (while (and unread (< (car unread) 0))
11843         (setq unread (cdr unread)))
11844       ;; Remove any expired article numbers
11845       (while (and unread (< (car unread) (car active)))
11846         (setq unread (cdr unread)))
11847       (while (and ticked (< (car ticked) (car active)))
11848         (setq ticked (cdr ticked)))
11849       (while (and dormant (< (car dormant) (car active)))
11850         (setq dormant (cdr dormant)))
11851       (setq unread (sort (append unselected unread) '<))
11852       ;; Set the number of unread articles in gnus-newsrc-hashtb.
11853       (setcar entry (max 0 (- (length unread) (length ticked) 
11854                               (length dormant))))
11855       ;; Compute the ranges of read articles by looking at the list of
11856       ;; unread articles.  
11857       (while unread
11858         (if (/= (car unread) prev)
11859             (setq read (cons (if (= prev (1- (car unread))) prev
11860                                (cons prev (1- (car unread)))) read)))
11861         (setq prev (1+ (car unread)))
11862         (setq unread (cdr unread)))
11863       (if (<= prev (cdr active))
11864           (setq read (cons (cons prev (cdr active)) read)))
11865       ;; Enter this list into the group info.
11866       (setcar (cdr (cdr info)) 
11867               (if (> (length read) 1) (nreverse read) read))
11868       ;; Enter the list of ticked articles.
11869       (gnus-set-marked-articles 
11870        info ticked
11871        (if domarks replied (cdr (assq 'reply marked)))
11872        (if domarks expirable (cdr (assq 'expire marked)))
11873        (if domarks killed (cdr (assq 'killed marked)))
11874        (if domarks dormant (cdr (assq 'dormant marked)))
11875        (if domarks bookmark (cdr (assq 'bookmark marked)))
11876        (if domarks score (cdr (assq 'score marked))))
11877       t)))
11878
11879 (defun gnus-make-articles-unread (group articles)
11880   "Mark ARTICLES in GROUP as unread."
11881   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
11882                           (gnus-gethash (gnus-group-real-name group)
11883                                         gnus-newsrc-hashtb))))
11884          (ranges (nth 2 info))
11885          news)
11886     (while articles
11887       (and (gnus-member-of-range (car articles) ranges)
11888            (setq news (cons (car articles) news)))
11889       (setq articles (cdr articles)))
11890     (if (not news)
11891         ()
11892       (setcar (nthcdr 2 info)
11893               (gnus-remove-from-range (nth 2 info) (nreverse news)))
11894       (gnus-group-update-group group t))))
11895
11896 (defun gnus-read-active-file ()
11897   "Get active file from NNTP server."
11898   (gnus-group-set-mode-line)
11899   (let ((methods (cons gnus-select-method gnus-secondary-select-methods))
11900         (not-first nil)
11901         list-type)
11902     (setq gnus-have-read-active-file nil)
11903     (save-excursion
11904       (set-buffer nntp-server-buffer)
11905       (while methods
11906         (let* ((where (nth 1 (car methods)))
11907                (mesg (format "Reading active file%s via %s..."
11908                              (if (and where (not (zerop (length where))))
11909                                  (concat " from " where) "")
11910                              (car (car methods)))))
11911           (gnus-message 5 mesg)
11912           (gnus-check-news-server (car methods))
11913           (cond 
11914            ((and (eq gnus-read-active-file 'some)
11915                  (gnus-check-backend-function
11916                   'retrieve-groups (car (car methods))))
11917             (let ((newsrc (cdr gnus-newsrc-alist))
11918                   groups)
11919               (while newsrc
11920                 (and (gnus-server-equal 
11921                       (gnus-find-method-for-group
11922                        (car (car newsrc)) (car newsrc))
11923                       (gnus-server-get-method nil (car methods)))
11924                      (setq groups (cons (car (car newsrc)) groups)))
11925                 (setq newsrc (cdr newsrc)))
11926               (setq list-type (gnus-retrieve-groups groups (car methods)))
11927               (cond ((not list-type)
11928                      (gnus-message 
11929                       1 "Cannot read partial active file from %s server." 
11930                       (car (car methods)))
11931                      (ding)
11932                      (sit-for 2))
11933                     ((eq list-type 'active)
11934                      (gnus-active-to-gnus-format (and not-first (car methods)))
11935                      (setq not-first t))
11936                     (t
11937                      (gnus-groups-to-gnus-format (and not-first (car methods)))
11938                      (setq not-first t)))))
11939            (t
11940             (if (not (gnus-request-list (car methods)))
11941                 (progn
11942                   (gnus-message 1 "Cannot read active file from %s server." 
11943                                 (car (car methods)))
11944                   (ding))
11945               (gnus-active-to-gnus-format 
11946                (and gnus-have-read-active-file (car methods)))
11947               (setq gnus-have-read-active-file t)
11948               (gnus-message 5 "%sdone" mesg)))))
11949         (setq methods (cdr methods))))))
11950
11951 ;; Read an active file and place the results in `gnus-active-hashtb'.
11952 (defun gnus-active-to-gnus-format (method &optional hashtb)
11953   (let ((cur (current-buffer))
11954         (hashtb (or hashtb 
11955                     (if method
11956                         gnus-active-hashtb
11957                       (setq gnus-active-hashtb
11958                             (gnus-make-hashtable 
11959                              (count-lines (point-min) (point-max))))))))
11960     ;; Delete unnecessary lines.
11961     (goto-char (point-min))
11962     (while (search-forward "\nto." nil t)
11963       (delete-region (match-beginning 0) (progn (forward-line 1) (point))))
11964     (or (string= gnus-ignored-newsgroups "")
11965         (progn
11966           (goto-char (point-min))
11967           (delete-matching-lines gnus-ignored-newsgroups)))
11968     ;; If these are groups from a foreign select method, we insert the
11969     ;; group prefix in front of the group names. 
11970     (and method (not (eq method gnus-select-method))
11971          (let ((prefix (gnus-group-prefixed-name "" method)))
11972            (goto-char (point-min))
11973            (while (and (not (eobp))
11974                        (progn (insert prefix)
11975                               (zerop (forward-line 1)))))))
11976     (goto-char (point-min))
11977     ;; Store active file in hashtable.
11978     (goto-char (point-min))
11979     (if (string-match "%[oO]" gnus-group-line-format)
11980         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
11981         ;; If we want information on moderated groups, we use this
11982         ;; loop...   
11983         (let* ((mod-hashtb (make-vector 7 0))
11984                (m (intern "m" mod-hashtb))
11985                group max mod min)
11986           (while (not (eobp))
11987             (condition-case nil
11988                 (progn
11989                   (narrow-to-region (point) (gnus-point-at-eol))
11990                   (setq group (let ((obarray hashtb)) (read cur)))
11991                   (and (numberp (setq max (read cur)))
11992                        (numberp (setq min (read cur)))
11993                        (set group (cons min max)))
11994                   ;; Enter moderated groups into a list.
11995                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
11996                       (setq gnus-moderated-list 
11997                             (cons (symbol-name group) gnus-moderated-list))))
11998               (error nil))
11999             (widen)
12000             (forward-line 1)))
12001       ;; And if we do not care about moderation, we use this loop,
12002       ;; which is faster.
12003       (let (group max min)
12004         (while (not (eobp))
12005           (condition-case ()
12006               (progn
12007                 (narrow-to-region (point) (gnus-point-at-eol))
12008                 ;; group gets set to a symbol interned in the hash table
12009                 ;; (what a hack!!)
12010                 (setq group (let ((obarray hashtb)) (read cur)))
12011                 (and (numberp (setq max (read cur)))
12012                      (numberp (setq min (read cur)))
12013                      (set group (cons min max))))
12014             (error 
12015              (progn (ding) 
12016                     (gnus-message 3 "Warning - illegal active: %s"
12017                                   (buffer-substring 
12018                                    (gnus-point-at-bol) (gnus-point-at-eol)))
12019                     nil)))
12020           (widen)
12021           (forward-line 1))))))
12022
12023 (defun gnus-groups-to-gnus-format (method &optional hashtb)
12024   ;; Parse a "groups" active file.
12025   (let ((cur (current-buffer))
12026         (hashtb (or hashtb 
12027                     (if method
12028                         gnus-active-hashtb
12029                       (setq gnus-active-hashtb
12030                             (gnus-make-hashtable 
12031                              (count-lines (point-min) (point-max)))))))
12032         (prefix (and method (not (eq method gnus-select-method))
12033                      (gnus-group-prefixed-name "" method))))
12034
12035     (goto-char (point-min))
12036     (condition-case ()
12037         ;; We split this into to separate loops, one with the prefix
12038         ;; and one without to speed the reading up somewhat.
12039         (if prefix
12040             (let (min max opoint)
12041               (while (not (eobp))
12042                 (read cur) (read cur)
12043                 (setq min (read cur)
12044                       max (read cur)
12045                       opoint (point))
12046                 (skip-chars-forward " \t")
12047                 (insert prefix)
12048                 (goto-char opoint)
12049                 (set (let ((obarray hashtb)) (read cur)) 
12050                      (cons min max))
12051                 (forward-line 1)))
12052           (let (min max opoint)
12053             (while (not (eobp))
12054               (if (= (following-char) ?2)
12055                   (progn
12056                     (read cur) (read cur)
12057                     (setq min (read cur)
12058                           max (read cur))
12059                     (set (let ((obarray hashtb)) (read cur)) 
12060                          (cons min max))))
12061               (forward-line 1))))
12062       (error 
12063        (progn (ding) (gnus-message 3 "Possible error in active file."))))))
12064
12065 (defun gnus-read-newsrc-file (&optional force)
12066   "Read startup file.
12067 If FORCE is non-nil, the .newsrc file is read."
12068   (setq gnus-current-startup-file (gnus-make-newsrc-file gnus-startup-file))
12069   ;; Reset variables that might be defined in the .newsrc.eld file.
12070   (let ((variables gnus-variable-list))
12071     (while variables
12072       (set (car variables) nil)
12073       (setq variables (cdr variables))))
12074   (let* ((newsrc-file gnus-current-startup-file)
12075          (quick-file (concat newsrc-file ".el")))
12076     (save-excursion
12077       ;; We always load the .newsrc.eld file. If always contains
12078       ;; much information that can not be gotten from the .newsrc
12079       ;; file (ticked articles, killed groups, foreign methods, etc.)
12080       (gnus-read-newsrc-el-file quick-file)
12081  
12082       (if (or force
12083               (and (file-newer-than-file-p newsrc-file quick-file)
12084                    (file-newer-than-file-p newsrc-file 
12085                                            (concat quick-file "d")))
12086               (not gnus-newsrc-alist))
12087           ;; We read the .newsrc file. Note that if there if a
12088           ;; .newsrc.eld file exists, it has already been read, and
12089           ;; the `gnus-newsrc-hashtb' has been created. While reading
12090           ;; the .newsrc file, Gnus will only use the information it
12091           ;; can find there for changing the data already read -
12092           ;; ie. reading the .newsrc file will not trash the data
12093           ;; already read (except for read articles).
12094           (save-excursion
12095             (gnus-message 5 "Reading %s..." newsrc-file)
12096             (set-buffer (find-file-noselect newsrc-file))
12097             (buffer-disable-undo (current-buffer))
12098             (gnus-newsrc-to-gnus-format)
12099             (kill-buffer (current-buffer))
12100             (gnus-message 5 "Reading %s...done" newsrc-file))))))
12101
12102 (defun gnus-read-newsrc-el-file (file)
12103   (let ((ding-file (concat file "d")))
12104     ;; We always, always read the .eld file.
12105     (gnus-message 5 "Reading %s..." ding-file)
12106     (let (gnus-newsrc-assoc)
12107       (condition-case nil
12108           (load ding-file t t t)
12109         (error nil))
12110       (and gnus-newsrc-assoc (setq gnus-newsrc-alist gnus-newsrc-assoc)))
12111     (let ((inhibit-quit t))
12112       (gnus-uncompress-newsrc-assoc))
12113     (gnus-make-hashtable-from-newsrc-alist)
12114     (if (not (file-newer-than-file-p file ding-file))
12115         ()
12116       ;; Old format quick file
12117       (gnus-message 5 "Reading %s..." file)
12118       ;; The .el file is newer than the .eld file, so we read that one
12119       ;; as well. 
12120       (gnus-read-old-newsrc-el-file file))))
12121
12122 ;; Parse the old-style quick startup file
12123 (defun gnus-read-old-newsrc-el-file (file)
12124   (let (newsrc killed marked group g m len info)
12125     (prog1
12126         (let ((gnus-killed-assoc nil)
12127               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
12128           (prog1
12129               (condition-case nil
12130                   (load file t t t)
12131                 (error nil))
12132             (setq newsrc gnus-newsrc-assoc
12133                   killed gnus-killed-assoc
12134                   marked gnus-marked-assoc)))
12135       (setq gnus-newsrc-alist nil)
12136       (while newsrc
12137         (setq group (car newsrc))
12138         (let ((info (nth 2 (gnus-gethash (car group) gnus-newsrc-hashtb))))
12139           (if info
12140               (progn
12141                 (setcar (nthcdr 2 info) (cdr (cdr group)))
12142                 (setcar (cdr info)
12143                         (if (nth 1 group) gnus-level-default-subscribed 
12144                           gnus-level-default-unsubscribed))
12145                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
12146             (setq gnus-newsrc-alist
12147                   (cons 
12148                    (setq info
12149                          (list (car group)
12150                                (if (nth 1 group) gnus-level-default-subscribed
12151                                  gnus-level-default-unsubscribed) 
12152                                (cdr (cdr group))))
12153                    gnus-newsrc-alist)))
12154           (if (setq m (assoc (car group) marked))
12155             (setcdr (cdr (cdr info)) (cons (list (cons 'tick (cdr m))) nil))))
12156         (setq newsrc (cdr newsrc)))
12157       (setq newsrc killed)
12158       (while newsrc
12159         (setcar newsrc (car (car newsrc)))
12160         (setq newsrc (cdr newsrc)))
12161       (setq gnus-killed-list killed))
12162     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
12163     (gnus-make-hashtable-from-newsrc-alist)))
12164       
12165 (defun gnus-make-newsrc-file (file)
12166   "Make server dependent file name by catenating FILE and server host name."
12167   (let* ((file (expand-file-name file nil))
12168          (real-file (concat file "-" (nth 1 gnus-select-method))))
12169     (if (file-exists-p real-file)
12170         real-file file)))
12171
12172 (defun gnus-uncompress-newsrc-assoc ()
12173   ;; Uncompress all lists of marked articles in the newsrc assoc.
12174   (let ((newsrc gnus-newsrc-alist)
12175         marked)
12176     (while newsrc
12177       (if (not (setq marked (nth 3 (car newsrc))))
12178           ()
12179         (while marked
12180           (or (eq 'score (car (car marked)))
12181               (eq 'bookmark (car (car marked)))
12182               (eq 'killed (car (car marked)))
12183               (setcdr (car marked) (gnus-uncompress-range (cdr (car marked)))))
12184           (setq marked (cdr marked))))
12185       (setq newsrc (cdr newsrc)))))
12186
12187 (defun gnus-compress-newsrc-assoc ()
12188   ;; Compress all lists of marked articles in the newsrc assoc.
12189   (let ((newsrc gnus-newsrc-alist)
12190         marked)
12191     (while newsrc
12192       (if (not (setq marked (nth 3 (car newsrc))))
12193           ()
12194         (while marked
12195           (or (eq 'score (car (car marked)))
12196               (eq 'bookmark (car (car marked)))
12197               (eq 'killed (car (car marked)))
12198               (setcdr (car marked) 
12199                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
12200           (setq marked (cdr marked))))
12201       (setq newsrc (cdr newsrc)))))
12202
12203 (defun gnus-newsrc-to-gnus-format ()
12204   (setq gnus-newsrc-options "")
12205   (or gnus-active-hashtb
12206       (setq gnus-active-hashtb (make-vector 4095 0)))
12207   (let ((buf (current-buffer))
12208         (already-read (> (length gnus-newsrc-alist) 1))
12209         group level subscribed info options-symbol newsrc
12210         symbol reads num1)
12211     (goto-char (point-min))
12212     ;; We intern the symbol `options' in the active hashtb so that we
12213     ;; can `eq' against it later.
12214     (setq options-symbol (intern "options" gnus-active-hashtb))
12215   
12216     (while (not (eobp))
12217       ;; We first read the first word on the line by narrowing and
12218       ;; then reading into `gnus-active-hashtb'.  Most groups will
12219       ;; already exist in that hashtb, so this will save some string
12220       ;; space.
12221       (narrow-to-region
12222        (point)
12223        (progn (skip-chars-forward "^ \t!:\n") (point)))
12224       (goto-char (point-min))
12225       (setq symbol 
12226             (and (/= (point-min) (point-max))
12227                  (let ((obarray gnus-active-hashtb)) (read buf))))
12228       (widen)
12229       ;; Now, the symbol we have read is either `options' or a group
12230       ;; name.  If it is an options line, we just add it to a string. 
12231       (cond 
12232        ((eq symbol options-symbol)
12233         (setq gnus-newsrc-options
12234               ;; This concatting is quite inefficient, but since our
12235               ;; thorough studies show that approx 99.37% of all
12236               ;; .newsrc files only contain a single options line, we
12237               ;; don't give a damn, frankly, my dear.
12238               (concat gnus-newsrc-options
12239                       (buffer-substring 
12240                        (gnus-point-at-bol)
12241                        ;; Options may continue on the next line.
12242                        (or (and (re-search-forward "^[^ \t]" nil 'move)
12243                                 (progn (beginning-of-line) (point)))
12244                            (point))))))
12245        (symbol
12246         ;; It was a group name.
12247         (setq subscribed (= (following-char) ?:)
12248               group (symbol-name symbol)
12249               reads nil)
12250         (if (eolp)
12251             ;; If the line ends here, this is clearly a buggy line, so
12252             ;; we put point a the beginning of line and let the cond
12253             ;; below do the error handling.
12254             (beginning-of-line)
12255           ;; We skip to the beginning of the ranges.
12256           (skip-chars-forward "!: \t"))
12257         ;; We are now at the beginning of the list of read articles.
12258         ;; We read them range by range.
12259         (while
12260             (cond 
12261              ((looking-at "[0-9]+")
12262               ;; We narrow and read a number instead of buffer-substring/
12263               ;; string-to-int because it's faster. narrow/widen is
12264               ;; faster than save-restriction/narrow, and save-restriction
12265               ;; produces a garbage object.
12266               (setq num1 (progn
12267                            (narrow-to-region (match-beginning 0) (match-end 0))
12268                            (read buf)))
12269               (widen)
12270               ;; If the next character is a dash, then this is a range.
12271               (if (= (following-char) ?-)
12272                   (progn
12273                     ;; We read the upper bound of the range.
12274                     (forward-char 1)
12275                     (if (not (looking-at "[0-9]+"))
12276                         ;; This is a buggy line, by we pretend that
12277                         ;; it's kinda OK. Perhaps the user should be
12278                         ;; dinged? 
12279                         (setq reads (cons num1 reads))
12280                       (setq reads 
12281                             (cons 
12282                              (cons num1 (progn
12283                                           (narrow-to-region (match-beginning 0) 
12284                                                             (match-end 0))
12285                                           (read buf)))
12286                              reads))
12287                       (widen)))
12288                 ;; It was just a simple number, so we add it to the
12289                 ;; list of ranges.
12290                 (setq reads (cons num1 reads)))
12291               ;; If the next char in ?\n, then we have reached the end
12292               ;; of the line and return nil.
12293               (/= (following-char) ?\n))
12294              ((= (following-char) ?\n)
12295               ;; End of line, so we end.
12296               nil)
12297              (t
12298               ;; Not numbers and not eol, so this might be a buggy
12299               ;; line... 
12300               (or (eobp) ; If it was eob instead of ?\n, we allow it.
12301                   (progn
12302                     ;; The line was buggy.
12303                     (setq group nil)
12304                     (gnus-message 3 "Mangled line: %s" 
12305                                   (buffer-substring (gnus-point-at-bol) 
12306                                                     (gnus-point-at-eol)))
12307                     (ding)
12308                     (sit-for 1)))
12309               nil))
12310           ;; Skip past ", ". Spaces are illegal in these ranges, but
12311           ;; we allow them, because it's a common mistake to put a
12312           ;; space after the comma.
12313           (skip-chars-forward ", "))
12314
12315         ;; We have already read .newsrc.eld, so we gently update the
12316         ;; data in the hash table with the information we have just
12317         ;; read. 
12318         (if (not group)
12319             ()
12320           (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
12321                 level)
12322             (if info
12323                 ;; There is an entry for this file in the alist.
12324                 (progn
12325                   (setcar (nthcdr 2 info) (nreverse reads))
12326                   ;; We update the level very gently.  In fact, we
12327                   ;; only change it if there's been a status change
12328                   ;; from subscribed to unsubscribed, or vice versa.
12329                   (setq level (nth 1 info))
12330                   (cond ((and (<= level gnus-level-subscribed)
12331                               (not subscribed))
12332                          (setq level (if reads
12333                                          gnus-level-default-unsubscribed 
12334                                        (1+ gnus-level-default-unsubscribed))))
12335                         ((and (> level gnus-level-subscribed) subscribed)
12336                          (setq level gnus-level-default-subscribed)))
12337                   (setcar (cdr info) level))
12338               ;; This is a new group.
12339               (setq info (list group 
12340                                (if subscribed
12341                                    gnus-level-default-subscribed 
12342                                  (if reads
12343                                      (1+ gnus-level-subscribed)
12344                                    gnus-level-default-unsubscribed))
12345                                (nreverse reads))))
12346             (setq newsrc (cons info newsrc))))
12347         (forward-line 1))))
12348     
12349     (setq newsrc (nreverse newsrc))
12350
12351     (if (not already-read)
12352         ()
12353       ;; We now have two newsrc lists - `newsrc', which is what we
12354       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
12355       ;; what we've read from .newsrc.eld. We have to merge these
12356       ;; lists. We do this by "attaching" any (foreign) groups in the
12357       ;; gnus-newsrc-alist to the (native) group that precedes them. 
12358       (let ((rc (cdr gnus-newsrc-alist))
12359             (prev gnus-newsrc-alist)
12360             entry mentry)
12361         (while rc
12362           (or (assoc (car (car rc)) newsrc)
12363               (if (setq entry (assoc (car (car prev)) newsrc))
12364                   (setcdr (setq mentry (memq entry newsrc))
12365                           (cons (car rc) (cdr mentry)))
12366                 (setq newsrc (cons (car rc) newsrc))))
12367           (setq prev rc
12368                 rc (cdr rc)))))
12369
12370     (setq gnus-newsrc-alist newsrc)
12371     ;; We make the newsrc hashtb.
12372     (gnus-make-hashtable-from-newsrc-alist)
12373
12374     ;; Finally, if we read some options lines, we parse them.
12375     (or (string= gnus-newsrc-options "")
12376         (gnus-newsrc-parse-options gnus-newsrc-options))
12377     ))
12378
12379 ;; Parse options lines to find "options -n !all rec.all" and stuff.
12380 ;; The return value will be a list on the form
12381 ;; ((regexp1 . ignore)
12382 ;;  (regexp2 . subscribe)...)
12383 ;; When handling new newsgroups, groups that match a `ignore' regexp
12384 ;; will be ignored, and groups that match a `subscribe' regexp will be
12385 ;; subscribed. A line like
12386 ;; options -n !all rec.all
12387 ;; will lead to a list that looks like
12388 ;; (("^rec\\..+" . subscribe) 
12389 ;;  ("^.+" . ignore))
12390 ;; So all "rec.*" groups will be subscribed, while all the other
12391 ;; groups will be ignored. Note that "options -n !all rec.all" is very
12392 ;; different from "options -n rec.all !all". 
12393 (defun gnus-newsrc-parse-options (options)
12394   (let (out eol)
12395     (save-excursion
12396       (gnus-set-work-buffer)
12397       (insert (regexp-quote options))
12398       ;; First we treat all continuation lines.
12399       (goto-char (point-min))
12400       (while (re-search-forward "\n[ \t]+" nil t)
12401         (replace-match " " t t))
12402       ;; Then we transform all "all"s into ".+"s.
12403       (goto-char (point-min))
12404       (while (re-search-forward "\\ball\\b" nil t)
12405         (replace-match ".+" t t))
12406       (goto-char (point-min))
12407       ;; We remove all other options than the "-n" ones.
12408       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
12409         (replace-match " ")
12410         (forward-char -1))
12411       (goto-char (point-min))
12412
12413       ;; We are only interested in "options -n" lines - we
12414       ;; ignore the other option lines.
12415       (while (re-search-forward "[ \t]-n" nil t)
12416         (setq eol 
12417               (or (save-excursion
12418                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
12419                          (- (point) 2)))
12420                   (gnus-point-at-eol)))
12421         ;; Search for all "words"...
12422         (while (re-search-forward "[^ \t,\n-]+" eol t)
12423           (if (= (char-after (match-beginning 0)) ?!)
12424               ;; If the word begins with a bang (!), this is a "not"
12425               ;; spec. We put this spec (minus the bang) and the
12426               ;; symbol `ignore' into the list.
12427               (setq out (cons (cons (concat 
12428                                      "^" (buffer-substring 
12429                                           (1+ (match-beginning 0))
12430                                           (match-end 0)))
12431                                     'ignore) out))
12432             ;; There was no bang, so this is a "yes" spec.
12433             (setq out (cons (cons (concat 
12434                                    "^" (buffer-substring (match-beginning 0)
12435                                                          (match-end 0)))
12436                                   'subscribe) out)))))
12437     
12438       (setq gnus-newsrc-options-n out))))
12439                
12440
12441 (defun gnus-save-newsrc-file ()
12442   "Save .newsrc file."
12443   ;; Note: We cannot save .newsrc file if all newsgroups are removed
12444   ;; from the variable gnus-newsrc-alist.
12445   (and (or gnus-newsrc-alist gnus-killed-list)
12446        gnus-current-startup-file
12447        (let ((make-backup-files t)
12448              (version-control nil)
12449              (require-final-newline t)) ;Don't ask even if requested.
12450          ;; You can stop or change version control of backup file.
12451          ;; Suggested by jason@violet.berkeley.edu.
12452          (run-hooks 'gnus-save-newsrc-hook)
12453          (save-excursion
12454            (if (or (not gnus-dribble-buffer)
12455                    (not (buffer-name gnus-dribble-buffer))
12456                    (zerop (save-excursion
12457                             (set-buffer gnus-dribble-buffer)
12458                             (buffer-size))))
12459                (gnus-message 4 "(No changes need to be saved)")
12460              (if gnus-save-newsrc-file
12461                  (progn
12462                    (gnus-message 5 "Saving %s..." gnus-current-startup-file)
12463                    ;; Make backup file of master newsrc.
12464                    (gnus-gnus-to-newsrc-format)
12465                    (gnus-message 5 "Saving %s...done"
12466                                  gnus-current-startup-file)))
12467              ;; Quickly loadable .newsrc.
12468              (set-buffer (get-buffer-create " *Gnus-newsrc*"))
12469              (gnus-add-current-to-buffer-list)
12470              (buffer-disable-undo (current-buffer))
12471              (erase-buffer)
12472              (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
12473              (gnus-gnus-to-quick-newsrc-format)
12474              (write-region 1 (point-max) 
12475                            (concat gnus-current-startup-file ".eld") 
12476                            nil 'nomesg)
12477              (kill-buffer (current-buffer))
12478              (gnus-message 5 "Saving %s.eld...done" gnus-current-startup-file)
12479              (gnus-dribble-delete-file))))))
12480
12481 (defun gnus-gnus-to-quick-newsrc-format ()
12482   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
12483   (insert ";; (ding) Gnus startup file.\n")
12484   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
12485   (insert ";; to read .newsrc.\n")
12486   (let ((variables gnus-variable-list)
12487         (inhibit-quit t)
12488         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
12489         variable)
12490     ;; insert lisp expressions.
12491     (gnus-compress-newsrc-assoc)
12492     (while variables
12493       (setq variable (car variables))
12494       (and (boundp variable)
12495            (symbol-value variable)
12496            (or gnus-save-killed-list (not (eq variable 'gnus-killed-list)))
12497            (insert "(setq " (symbol-name variable) " '"
12498                    (prin1-to-string (symbol-value variable))
12499                    ")\n"))
12500       (setq variables (cdr variables)))
12501     (gnus-uncompress-newsrc-assoc)))
12502
12503
12504 (defun gnus-gnus-to-newsrc-format ()
12505   ;; Generate and save the .newsrc file.
12506   (let ((newsrc (cdr gnus-newsrc-alist))
12507         info ranges range)
12508     (save-excursion
12509       (set-buffer (create-file-buffer gnus-startup-file))
12510       (buffer-disable-undo (current-buffer))
12511       (erase-buffer)
12512       ;; Write options.
12513       (if gnus-newsrc-options (insert gnus-newsrc-options))
12514       ;; Write subscribed and unsubscribed.
12515       (while newsrc
12516         (setq info (car newsrc))
12517         (if (not (nth 4 info))          ;Don't write foreign groups to .newsrc.
12518             (progn
12519               (insert (car info) (if (> (nth 1 info) gnus-level-subscribed)
12520                                      "!" ":"))
12521               (if (setq ranges (nth 2 info))
12522                   (progn
12523                     (insert " ")
12524                     (if (not (listp (cdr ranges)))
12525                         (if (= (car ranges) (cdr ranges))
12526                             (insert (int-to-string (car ranges)))
12527                           (insert (int-to-string (car ranges)) "-" 
12528                                   (int-to-string (cdr ranges))))
12529                       (while ranges
12530                         (setq range (car ranges)
12531                               ranges (cdr ranges))
12532                         (if (or (atom range) (= (car range) (cdr range)))
12533                             (insert (int-to-string 
12534                                      (or (and (atom range) range) 
12535                                          (car range))))
12536                           (insert (int-to-string (car range)) "-"
12537                                   (int-to-string (cdr range))))
12538                         (if ranges (insert ","))))))
12539               (insert "\n")))
12540         (setq newsrc (cdr newsrc)))
12541       (write-region 1 (point-max) gnus-current-startup-file nil 'nomesg)
12542       (kill-buffer (current-buffer)))))
12543
12544 (defun gnus-read-all-descriptions-files ()
12545   (let ((methods (nconc (list gnus-select-method) 
12546                         gnus-secondary-select-methods)))
12547     (while methods
12548       (gnus-read-descriptions-file (car methods))
12549       (setq methods (cdr methods)))))
12550
12551 (defun gnus-read-descriptions-file (&optional method)
12552   (let ((method (or method gnus-select-method)))
12553     (gnus-message 5 "Reading descriptions file via %s..." (car method))
12554     (cond 
12555      ((not (or (gnus-server-opened method)
12556                (gnus-open-server method)))
12557       (gnus-message 1 "Couldn't open server")
12558       nil)
12559      ((not (gnus-request-list-newsgroups method))
12560       (gnus-message 1 "Couldn't read newsgroups descriptions")
12561       nil)
12562      (t
12563       (let (group)
12564         (or gnus-description-hashtb
12565             (setq gnus-description-hashtb 
12566                   (gnus-make-hashtable (length gnus-active-hashtb))))
12567         ;; Mark this method's desc file as read.
12568         (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
12569                       gnus-description-hashtb)
12570         (save-excursion
12571           (save-restriction
12572             (set-buffer nntp-server-buffer)
12573             (goto-char (point-min))
12574             (if (or (search-forward "\n.\n" nil t)
12575                     (goto-char (point-max)))
12576                 (progn
12577                   (beginning-of-line)
12578                   (narrow-to-region (point-min) (point))))
12579             (goto-char (point-min))
12580             (while (not (eobp))
12581               ;; If we get an error, we set group to 0, which is not a
12582               ;; symbol... 
12583               (setq group 
12584                     (condition-case ()
12585                         (let ((obarray gnus-description-hashtb))
12586                           ;; Group is set to a symbol interned in this
12587                           ;; hash table.
12588                           (read nntp-server-buffer))
12589                       (error 0)))
12590               (skip-chars-forward " \t")
12591               ;; ... which leads to this line being effectively ignored.
12592               (and (symbolp group)
12593                    (set group (buffer-substring 
12594                                (point) (progn (end-of-line) (point)))))
12595               (forward-line 1))))
12596         (gnus-message 5 "Reading descriptions file...done")
12597         t)))))
12598
12599 (defun gnus-group-get-description (group)
12600   ;; Get the description of a group by sending XGTITLE to the server.
12601   (and (gnus-request-group-description group)
12602        (save-excursion
12603          (set-buffer nntp-server-buffer)
12604          (goto-char (point-min))
12605          (and (looking-at "[^ \t]+[ \t]+\\(.*\\)")
12606               (buffer-substring (match-beginning 1) (match-end 1))))))
12607
12608 ;;;
12609 ;;; Server
12610 ;;;
12611
12612 (defvar gnus-server-mode-hook nil
12613   "Hook run in `gnus-server-mode' buffers.")
12614
12615 (defconst gnus-server-line-format "     {%(%h:%w%)}\n"
12616   "Format of server lines.
12617 It works along the same lines as a normal formatting string,
12618 with some simple extensions.")
12619
12620 (defvar gnus-server-mode-line-format "(ding) List of servers"
12621   "The format specification for the server mode line.")
12622
12623 (defconst gnus-server-line-format-alist
12624   (list (list ?h 'how ?s)
12625         (list ?n 'name ?s)
12626         (list ?w 'where ?s)
12627         ))
12628
12629 (defconst gnus-server-mode-line-format-alist 
12630   (list (list ?S 'news-server ?s)
12631         (list ?M 'news-method ?s)
12632         (list ?u 'user-defined ?s)))
12633
12634 (defvar gnus-server-line-format-spec nil)
12635 (defvar gnus-server-mode-line-format-spec nil)
12636 (defvar gnus-server-killed-servers nil)
12637
12638 (defvar gnus-server-mode-map nil)
12639 (put 'gnus-server-mode 'mode-class 'special)
12640
12641 (if gnus-server-mode-map
12642     nil
12643   (setq gnus-server-mode-map (make-sparse-keymap))
12644   (suppress-keymap gnus-server-mode-map)
12645   (define-key gnus-server-mode-map " " 'gnus-server-read-server)
12646   (define-key gnus-server-mode-map "\r" 'gnus-server-read-server)
12647   (define-key gnus-server-mode-map gnus-mouse-2 'gnus-server-pick-server)
12648   (define-key gnus-server-mode-map "q" 'gnus-server-exit)
12649   (define-key gnus-server-mode-map "l" 'gnus-server-list-servers)
12650   (define-key gnus-server-mode-map "k" 'gnus-server-kill-server)
12651   (define-key gnus-server-mode-map "y" 'gnus-server-yank-server)
12652   (define-key gnus-server-mode-map "c" 'gnus-server-copy-server)
12653   (define-key gnus-server-mode-map "a" 'gnus-server-add-server)
12654   (define-key gnus-server-mode-map "e" 'gnus-server-edit-server))
12655
12656 (defun gnus-server-mode ()
12657   "Major mode for listing and editing servers.
12658
12659 All normal editing commands are switched off.
12660 \\<gnus-server-mode-map>
12661
12662 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
12663
12664 The following commands are available:
12665
12666 \\{gnus-server-mode-map}"
12667   (interactive)
12668   (if gnus-visual (gnus-server-make-menu-bar))
12669   (kill-all-local-variables)
12670   (setq mode-line-modified "-- ")
12671   (make-local-variable 'mode-line-format)
12672   (setq mode-line-format (copy-sequence mode-line-format))
12673   (and (equal (nth 3 mode-line-format) "   ")
12674        (setcar (nthcdr 3 mode-line-format) ""))
12675   (setq major-mode 'gnus-server-mode)
12676   (setq mode-name "Server")
12677 ;  (gnus-group-set-mode-line)
12678   (setq mode-line-process nil)
12679   (use-local-map gnus-server-mode-map)
12680   (buffer-disable-undo (current-buffer))
12681   (setq truncate-lines t)
12682   (setq buffer-read-only t)
12683   (run-hooks 'gnus-server-mode-hook))
12684
12685 (defun gnus-server-insert-server-line (sformat name method)
12686   (let* ((sformat (or sformat gnus-server-line-format-spec))
12687          (how (car method))
12688          (where (nth 1 method))
12689          b)
12690     (beginning-of-line)
12691     (setq b (point))
12692     ;; Insert the text.
12693     (insert (eval sformat))
12694     (add-text-properties 
12695      b (1+ b) (list 'gnus-server (intern name)))))
12696
12697 (defun gnus-server-setup-buffer ()
12698   (if (get-buffer gnus-server-buffer)
12699       ()
12700     (save-excursion
12701       (set-buffer (get-buffer-create gnus-server-buffer))
12702       (gnus-server-mode)
12703       (and gnus-carpal (gnus-carpal-setup-buffer 'server)))))
12704
12705 (defun gnus-server-prepare ()
12706   (setq gnus-server-mode-line-format-spec 
12707         (gnus-parse-format gnus-server-mode-line-format 
12708                            gnus-server-mode-line-format-alist))
12709   (setq gnus-server-line-format-spec 
12710         (gnus-parse-format gnus-server-line-format 
12711                            gnus-server-line-format-alist))
12712   (let ((alist gnus-server-alist)
12713         (buffer-read-only nil))
12714     (erase-buffer)
12715     (while alist
12716       (gnus-server-insert-server-line nil (car (car alist)) (cdr (car alist)))
12717       (setq alist (cdr alist))))
12718   (goto-char (point-min))
12719   (gnus-server-position-cursor))
12720
12721 (defun gnus-server-server-name ()
12722   (let ((server (get-text-property (gnus-point-at-bol) 'gnus-server)))
12723     (and server (symbol-name server))))
12724
12725 (defalias 'gnus-server-position-cursor 'gnus-goto-colon)
12726
12727 (defconst gnus-server-edit-buffer "*Gnus edit server*")
12728
12729 (defun gnus-server-update-server (server)
12730   (save-excursion
12731     (set-buffer gnus-server-buffer)
12732     (let ((buffer-read-only nil)
12733           (info (cdr (assoc server gnus-server-alist))))
12734       (gnus-dribble-enter 
12735        (concat "(gnus-server-set-info \"" server "\" '"
12736                (prin1-to-string info) ")"))
12737       ;; Buffer may be narrowed.
12738       (save-restriction
12739         (widen)
12740         (if (gnus-server-goto-server server)
12741             (delete-region (progn (beginning-of-line) (point))
12742                            (progn (forward-line 1) (point))))
12743         (let ((entry (assoc server gnus-server-alist)))
12744           (gnus-server-insert-server-line nil (car entry) (cdr entry))
12745           (gnus-server-position-cursor))))))
12746
12747 (defun gnus-server-set-info (server info)
12748   ;; Enter a select method into the virtual server alist.
12749   (gnus-dribble-enter 
12750    (concat "(gnus-server-set-info \"" server "\" '"
12751            (prin1-to-string info) ")"))
12752   (let* ((server (nth 1 info))
12753          (entry (assoc server gnus-server-alist)))
12754     (if entry (setcdr entry info)
12755       (setq gnus-server-alist
12756             (nconc gnus-server-alist (list (cons server info)))))))
12757
12758 (defun gnus-server-to-method (server)
12759   ;; Map virtual server names to select methods.
12760   (or (and (equal server "native") gnus-select-method)
12761       (cdr (assoc server gnus-server-alist))))
12762
12763 (defun gnus-server-extend-method (group method)
12764   ;; This function "extends" a virtual server.  If the server is
12765   ;; "hello", and the select method is ("hello" (my-var "something")) 
12766   ;; in the group "alt.alt", this will result in a new virtual server
12767   ;; called "helly+alt.alt".
12768   (let ((entry
12769          (gnus-copy-sequence 
12770           (if (equal (car method) "native") gnus-select-method
12771               (cdr (assoc (car method) gnus-server-alist))))))
12772     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
12773     (nconc entry (cdr method))))
12774
12775 (defun gnus-server-get-method (group method)
12776   ;; Input either a server name, and extended server name, or a
12777   ;; select method, and return a select method. 
12778   (cond ((stringp method)
12779          (gnus-server-to-method method))
12780         ((stringp (car method))
12781          (gnus-server-extend-method group method))
12782         (t
12783          (gnus-server-add-address method))))
12784
12785 (defun gnus-server-add-address (method)
12786   (let ((method-name (symbol-name (car method))))
12787     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
12788              (not (assq (intern (concat method-name "-address")) method)))
12789         (append method (list (list (intern (concat method-name "-address"))
12790                                    (nth 1 method))))
12791       method)))
12792
12793 (defun gnus-server-equal (s1 s2)
12794   (or (equal s1 s2)
12795       (and (= (length s1) (length s2))
12796            (progn
12797              (while (and s1 (member (car s1) s2))
12798                (setq s1 (cdr s1)))
12799              (null s1)))))
12800
12801 ;;; Interactive server functions.
12802
12803 (defun gnus-server-kill-server (server)
12804   "Kill the server on the current line."
12805   (interactive (list (gnus-server-server-name)))
12806   (or (gnus-server-goto-server server)
12807       (if server (error "No such server: %s" server)
12808         (error "No server on the current line")))
12809   (let ((buffer-read-only nil))
12810     (delete-region (progn (beginning-of-line) (point))
12811                    (progn (forward-line 1) (point))))
12812   (setq gnus-server-killed-servers 
12813         (cons (assoc server gnus-server-alist) gnus-server-killed-servers))
12814   (setq gnus-server-alist (delq (car gnus-server-killed-servers)
12815                                 gnus-server-alist))
12816   (gnus-server-position-cursor))
12817
12818 (defun gnus-server-yank-server ()
12819   "Yank the previously killed server."
12820   (interactive)
12821   (or gnus-server-killed-servers
12822       (error "No killed servers to be yanked"))
12823   (let ((alist gnus-server-alist)
12824         (server (gnus-server-server-name))
12825         (killed (car gnus-server-killed-servers)))
12826     (if (not server) 
12827         (setq gnus-server-alist (nconc gnus-server-alist (list killed)))
12828       (if (string= server (car (car gnus-server-alist)))
12829           (setq gnus-server-alist (cons killed gnus-server-alist))
12830         (while (and (cdr alist)
12831                     (not (string= server (car (car (cdr alist))))))
12832           (setq alist (cdr alist)))
12833         (setcdr alist (cons killed (cdr alist)))))
12834     (gnus-server-update-server (car killed))
12835     (setq gnus-server-killed-servers (cdr gnus-server-killed-servers))
12836     (gnus-server-position-cursor)))
12837
12838 (defun gnus-server-exit ()
12839   "Return to the group buffer."
12840   (interactive)
12841   (kill-buffer (current-buffer))
12842   (switch-to-buffer gnus-group-buffer))
12843
12844 (defun gnus-server-list-servers ()
12845   "List all available servers."
12846   (interactive)
12847   (let ((cur (gnus-server-server-name)))
12848     (gnus-server-prepare)
12849     (if cur (gnus-server-goto-server cur)
12850       (goto-char (point-max))
12851       (forward-line -1))
12852     (gnus-server-position-cursor)))
12853
12854 (defun gnus-server-copy-server (from to)
12855   (interactive
12856    (list
12857     (or (gnus-server-server-name)
12858         (error "No server on the current line"))
12859     (read-string "Copy to: ")))
12860   (or from (error "No server on current line"))
12861   (or (and to (not (string= to ""))) (error "No name to copy to"))
12862   (and (assoc to gnus-server-alist) (error "%s already exists" to))
12863   (or (assoc from gnus-server-alist) 
12864       (error "%s: no such server" from))
12865   (let ((to-entry (gnus-copy-sequence (assoc from gnus-server-alist))))
12866     (setcar to-entry to)
12867     (setcar (nthcdr 2 to-entry) to)
12868     (setq gnus-server-killed-servers 
12869           (cons to-entry gnus-server-killed-servers))
12870     (gnus-server-yank-server)))
12871
12872 (defun gnus-server-add-server (how where)
12873   (interactive 
12874    (list (intern (completing-read "Server method: "
12875                                   gnus-valid-select-methods nil t))
12876          (read-string "Server name: ")))
12877   (setq gnus-server-killed-servers 
12878         (cons (list where how where) gnus-server-killed-servers))
12879   (gnus-server-yank-server))
12880
12881 (defun gnus-server-goto-server (server)
12882   "Jump to a server line."
12883   (interactive
12884    (list (completing-read "Goto server: " gnus-server-alist nil t)))
12885   (let ((to (text-property-any (point-min) (point-max) 
12886                                'gnus-server (intern server))))
12887     (and to
12888          (progn
12889            (goto-char to) 
12890            (gnus-server-position-cursor)))))
12891
12892 (defun gnus-server-edit-server (server)
12893   "Edit the server on the current line."
12894   (interactive (list (gnus-server-server-name)))
12895   (or server
12896       (error "No server on current line"))
12897   (let ((winconf (current-window-configuration)))
12898     (get-buffer-create gnus-server-edit-buffer)
12899     (gnus-configure-windows 'edit-server)
12900     (gnus-add-current-to-buffer-list)
12901     (emacs-lisp-mode)
12902     (make-local-variable 'gnus-prev-winconf)
12903     (setq gnus-prev-winconf winconf)
12904     (use-local-map (copy-keymap (current-local-map)))
12905     (let ((done-func '(lambda () 
12906                         "Exit editing mode and update the information."
12907                         (interactive)
12908                         (gnus-server-edit-server-done 'group))))
12909       (setcar (cdr (nth 4 done-func)) server)
12910       (local-set-key "\C-c\C-c" done-func))
12911     (erase-buffer)
12912     (insert ";; Type `C-c C-c' after you have edited the server.\n\n")
12913     (insert (pp-to-string (cdr (assoc server gnus-server-alist))))))
12914
12915 (defun gnus-server-edit-server-done (server)
12916   (interactive)
12917   (set-buffer (get-buffer-create gnus-server-edit-buffer))
12918   (goto-char (point-min))
12919   (let ((form (read (current-buffer)))
12920         (winconf gnus-prev-winconf))
12921     (gnus-server-set-info server form)
12922     (kill-buffer (current-buffer))
12923     (and winconf (set-window-configuration winconf))
12924     (set-buffer gnus-server-buffer)
12925     (gnus-server-update-server (gnus-server-server-name))
12926     (gnus-server-position-cursor)))
12927
12928 (defun gnus-server-read-server (server)
12929   "Browse a server."
12930   (interactive (list (gnus-server-server-name)))
12931   (gnus-browse-foreign-server (gnus-server-to-method server) (current-buffer)))
12932
12933 (defun gnus-mouse-pick-server (e)
12934   (interactive "e")
12935   (mouse-set-point e)
12936   (gnus-server-read-server (gnus-server-server-name)))
12937
12938 ;;;
12939 ;;; entry points into gnus-score.el
12940 ;;;
12941
12942 ;;; Finding score files. 
12943
12944 (defvar gnus-global-score-files nil
12945   "*List of global score files and directories.
12946 Set this variable if you want to use people's score files.  One entry
12947 for each score file or each score file directory.  Gnus will decide
12948 by itself what score files are applicable to which group.
12949
12950 Say you want to use the single score file
12951 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
12952 score files in the \"/ftp.some-where:/pub/score\" directory.
12953
12954  (setq gnus-global-score-files
12955        '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
12956          \"/ftp.some-where:/pub/score\"))")
12957
12958 (defun gnus-score-score-files (group)
12959   "Return a list of all possible score files."
12960   ;; Search and set any global score files.
12961   (and gnus-global-score-files 
12962        (or gnus-internal-global-score-files
12963            (gnus-score-search-global-directories gnus-global-score-files)))
12964   ;; Fix the kill-file dir variable.
12965   (setq gnus-kill-files-directory 
12966         (file-name-as-directory
12967          (or gnus-kill-files-directory "~/News/")))
12968   ;; If er can't read it, there's no score files.
12969   (if (not (file-readable-p (expand-file-name gnus-kill-files-directory)))
12970       (setq gnus-score-file-list nil)
12971     (if (gnus-use-long-file-name 'not-score)
12972         ;; We want long file names.
12973         (if (or (not gnus-score-file-list)
12974                 (not (car gnus-score-file-list))
12975                 (gnus-file-newer-than gnus-kill-files-directory
12976                                       (car gnus-score-file-list)))
12977               (setq gnus-score-file-list 
12978                     (cons (nth 5 (file-attributes gnus-kill-files-directory))
12979                           (nreverse 
12980                            (directory-files 
12981                             gnus-kill-files-directory t 
12982                             (gnus-score-file-regexp))))))
12983       ;; We do not use long file names, so we have to do some
12984       ;; directory traversing.  
12985       (let ((mdir (length (expand-file-name gnus-kill-files-directory)))
12986             (suffixes (list gnus-score-file-suffix gnus-adaptive-file-suffix))
12987             dir files suffix)
12988         (while suffixes
12989           (setq dir (expand-file-name
12990                      (concat gnus-kill-files-directory
12991                              (gnus-replace-chars-in-string group ?. ?/))))
12992           (setq suffix (car suffixes)
12993                 suffixes (cdr suffixes))
12994           (if (file-exists-p (concat dir "/" suffix))
12995               (setq files (cons (concat dir "/" suffix) files)))
12996           (while (>= (1+ (length dir)) mdir)
12997             (and (file-exists-p (concat dir "/all/" suffix))
12998                  (setq files (cons (concat dir "/all/" suffix) files)))
12999             (string-match "/[^/]*$" dir)
13000             (setq dir (substring dir 0 (match-beginning 0)))))
13001         (setq gnus-score-file-list 
13002               (cons nil (nreverse files)))))
13003     (cdr gnus-score-file-list)))
13004
13005 (defun gnus-score-file-regexp ()
13006   (concat "\\(" gnus-score-file-suffix 
13007           "\\|" gnus-adaptive-file-suffix "\\)$"))
13008         
13009 (defun gnus-score-find-bnews (group)
13010   "Return a list of score files for GROUP.
13011 The score files are those files in the ~/News directory which matches
13012 GROUP using BNews sys file syntax."
13013   (let* ((sfiles (append (gnus-score-score-files group)
13014                          gnus-internal-global-score-files))
13015          (kill-dir (file-name-as-directory 
13016                     (expand-file-name gnus-kill-files-directory)))
13017          (klen (length kill-dir))
13018          ofiles not-match regexp)
13019     (save-excursion
13020       (set-buffer (get-buffer-create "*gnus score files*"))
13021       (buffer-disable-undo (current-buffer))
13022       ;; Go through all score file names and create regexp with them
13023       ;; as the source.  
13024       (while sfiles
13025         (erase-buffer)
13026         (insert (car sfiles))
13027         (goto-char (point-min))
13028         ;; First remove the suffix itself.
13029         (re-search-forward (concat "." (gnus-score-file-regexp)))
13030         (replace-match "" t t) 
13031         (goto-char (point-min))
13032         (if (looking-at (regexp-quote kill-dir))
13033             ;; If the file name was just "SCORE", `klen' is one character
13034             ;; too much.
13035             (delete-char (min (1- (point-max)) klen))
13036           (goto-char (point-max))
13037           (search-backward "/")
13038           (delete-region (1+ (point)) (point-min)))
13039         ;; If short file names were used, we have to translate slashes.
13040         (goto-char (point-min))
13041         (while (search-forward "/" nil t)
13042           (replace-match "." t t))
13043         ;; Translate "all" to ".*".
13044         (while (search-forward "all" nil t)
13045           (replace-match ".*" t t))
13046         (goto-char (point-min))
13047         ;; Deal with "not."s.
13048         (if (looking-at "not.")
13049             (progn
13050               (setq not-match t)
13051               (setq regexp (buffer-substring 5 (point-max))))
13052           (setq regexp (buffer-substring 1 (point-max)))
13053           (setq not-match nil))
13054         ;; Finally - if this resulting regexp matches the group name,
13055         ;; we add this score file to the list of score files
13056         ;; applicable to this group.
13057         (if (or (and not-match
13058                      (not (string-match regexp group)))
13059                 (and (not not-match)
13060                      (string-match regexp group)))
13061             (setq ofiles (cons (car sfiles) ofiles)))
13062         (setq sfiles (cdr sfiles)))
13063       (kill-buffer (current-buffer))
13064       ;; Slight kludge here - the last score file returned should be
13065       ;; the local score file, whether it exists or not. This is so
13066       ;; that any score commands the user enters will go to the right
13067       ;; file, and not end up in some global score file.
13068       (let ((localscore
13069              (expand-file-name
13070               (if (gnus-use-long-file-name 'not-score)
13071                   (concat gnus-kill-files-directory group "." 
13072                           gnus-score-file-suffix)
13073                 (concat gnus-kill-files-directory
13074                         (gnus-replace-chars-in-string group ?. ?/)
13075                         "/" gnus-score-file-suffix)))))
13076         (and (member localscore ofiles)
13077              (delete localscore ofiles))
13078         (setq ofiles (cons localscore ofiles)))
13079       (nreverse ofiles))))
13080
13081 (defun gnus-score-find-single (group)
13082   "Return list containing the score file for GROUP."
13083   (list (gnus-score-file-name group)))
13084
13085 (defun gnus-score-find-hierarchical (group)
13086   "Return list of score files for GROUP.
13087 This includes the score file for the group and all its parents."
13088   (let ((all (copy-sequence '(nil)))
13089         (start 0))
13090     (while (string-match "\\." group (1+ start))
13091       (setq start (match-beginning 0))
13092       (setq all (cons (substring group 0 start) all)))
13093     (setq all (cons group all))
13094     (mapcar 'gnus-score-file-name (nreverse all))))
13095
13096 (defvar gnus-score-file-alist-cache nil)
13097
13098 (defun gnus-score-find-alist (group)
13099   "Return list of score files for GROUP.
13100 The list is determined from the variable gnus-score-file-alist."
13101   (let ((alist gnus-score-file-multiple-match-alist)
13102         score-files)
13103     ;; if this group has been seen before, return the cached entry
13104     (if (setq score-files (assoc group gnus-score-file-alist-cache))
13105         (cdr score-files)       ; ensures caching of groups with no matches
13106       ;; handle the multiple match alist
13107       (while alist
13108         (and (string-match (car (car alist)) group)
13109              (setq score-files
13110                    (nconc score-files (cdr (car alist)))))
13111         (setq alist (cdr alist)))
13112       (setq alist gnus-score-file-single-match-alist)
13113       ;; handle the single match alist
13114       (catch 'done
13115         (while alist
13116           (and (string-match (car (car alist)) group)
13117                ;; progn used just in case ("regexp") has no files
13118                ;; and score-files is still nil. -sj
13119                ;; this can be construed as a "stop searching here" feature :>
13120                ;; and used to simplify regexps in the single-alist 
13121                (progn
13122                  (setq score-files
13123                        (nconc score-files (cdr (car alist))))
13124                  (throw 'done nil)))
13125           (setq alist (cdr alist))))
13126       ;; cache the score files
13127       (setq gnus-score-file-alist-cache
13128             (cons (cons group score-files) gnus-score-file-alist-cache))
13129       score-files)))
13130
13131
13132 (defun gnus-possibly-score-headers (&optional trace)
13133   (let ((func gnus-score-find-score-files-function)
13134         score-files scores)
13135     (and func (not (listp func))
13136          (setq func (list func)))
13137     ;; Go through all the functions for finding score files (or actual
13138     ;; scores) and add them to a list.
13139     (setq score-files (gnus-score-find-alist gnus-newsgroup-name))
13140     (while func
13141       (and (symbolp (car func))
13142            (fboundp (car func))
13143            (setq score-files 
13144                  (nconc score-files (funcall (car func) gnus-newsgroup-name))))
13145       (setq func (cdr func)))
13146     (if score-files (gnus-score-headers score-files trace))))
13147
13148 (defun gnus-score-file-name (newsgroup &optional suffix)
13149   "Return the name of a score file for NEWSGROUP."
13150   (let ((suffix (or suffix gnus-score-file-suffix)))
13151     (cond  ((or (null newsgroup)
13152                 (string-equal newsgroup ""))
13153             ;; The global score file is placed at top of the directory.
13154             (expand-file-name 
13155              suffix (or gnus-kill-files-directory "~/News")))
13156            ((gnus-use-long-file-name 'not-score)
13157             ;; Append ".SCORE" to newsgroup name.
13158             (expand-file-name (concat newsgroup "." suffix)
13159                               (or gnus-kill-files-directory "~/News")))
13160            (t
13161             ;; Place "SCORE" under the hierarchical directory.
13162             (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
13163                                       "/" suffix)
13164                               (or gnus-kill-files-directory "~/News"))))))
13165
13166 (defun gnus-score-search-global-directories (files)
13167   "Scan all global score directories for score files."
13168   ;; Set the variable `gnus-internal-global-score-files' to all
13169   ;; available global score files.
13170   (interactive (list gnus-global-score-files))
13171   (let (out)
13172     (while files
13173       (if (string-match "/$" (car files))
13174           (setq out (nconc (directory-files 
13175                             (car files) t
13176                             (concat (gnus-score-file-regexp) "$"))))
13177         (setq out (cons (car files) out)))
13178       (setq files (cdr files)))
13179     (setq gnus-internal-global-score-files out)))
13180
13181 ;; Allow redefinition of Gnus functions.
13182
13183 (gnus-ems-redefine)
13184
13185 (provide 'gnus)
13186
13187 ;;; gnus.el ends here