*** empty log message ***
[gnus] / lisp / gnus.el
1 ;;; (ding) Gnus --- 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-assoc', `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 'rnews)
39 (require 'rmail)
40
41 (require 'nnheader)
42
43 ;; Customization variables
44
45 (defvar gnus-select-method 
46   (list 'nntp (or (getenv "NNTPSERVER") 
47                   (if (and gnus-default-nntp-server
48                            (not (string= gnus-default-nntp-server "")))
49                       gnus-default-nntp-server)
50                   (system-name))
51         "nntp")
52   "Default method for selecting a newsgroup.
53 This variable should be a list, where the first element is how the
54 news is to be fetched, the second is the address.  An optional third
55 element can be included to specify a port number if nntp is used.
56
57 For instance, if you want to get your news via NNTP from
58 \"flab.flab.edu\" on port 23, you could say:
59
60 (setq gnus-select-method '(nntp \"flab.flab.edu\" 23))
61
62 If you want to use your local spool, say:
63
64 (setq gnus-select-method (list 'nnspool (system-name)))
65
66 If you use this variable, you must set `gnus-nntp-server' to nil.")
67
68 ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
69 (defvar gnus-post-method nil
70   "Preferred method for posting USENET news.
71 If this variable is nil, Gnus will use the current method to decide
72 which method to use when posting.  If it is non-nil, it will override
73 the current method. This method will not be used in mail groups and
74 the like, only in \"real\" newsgroups.
75
76 The value must be a valid method as discussed in the documentation of
77 `gnus-select-method'.")
78
79 (defvar gnus-refer-article-method nil
80   "Preferred method for fetching an article by Message-ID.
81 If you are reading news from the local spool (with nnspool), fetching
82 articles by Message-ID is painfully slow. By setting this method to an
83 nntp method, you might get acceptable results.
84
85 The value of this variable must be a valid select method as discussed
86 in the documentation of `gnus-select-method'")
87
88 (defvar gnus-secondary-select-methods nil
89   "A list of secondary methods that will be used for reading news.
90 This is a list where each element is a complete select methdod (see
91 `gnus-select-method').  
92
93 If, for instance, you want to read your mail with the nnml backend,
94 you could set this variable:
95
96 (setq gnus-secondary-select-methods '((nnml ""))")
97
98 (defvar gnus-secondary-servers nil
99   "List of NNTP servers that the user can choose between interactively.
100 To make Gnus query you for a server, you have to give `gnus' a
101 non-numeric prefix - `C-u M-x gnus', in short.")
102
103 (defvar gnus-nntp-server nil
104   "The name of the host running the NNTP server.
105 This variable is semi-obsolete. Use the `gnus-select-method'
106 variable instead.")
107
108 (defvar gnus-nntp-service "nntp"
109   "NNTP service name (\"nntp\" or 119).
110 This is an obsolete variable, which is scarcely used. If you use an
111 nntp server for your newsgroup and want to change the port number
112 used to 899, you would say something along these lines:
113
114  (setq gnus-select-method '(nntp \"my.nntp.server\" 899))")
115
116 (defvar gnus-startup-file "~/.newsrc"
117   "Your `.newsrc' file.
118 `.newsrc-SERVER' will be used instead if that exists.")
119
120 (defvar gnus-signature-file "~/.signature"
121   "Your signature file.
122 If the variable is a string that doesn't correspond to a file, the
123 string itself is inserted.")
124
125 (defvar gnus-signature-function nil
126   "A function that should return a signature file name.
127 The function will be called with the name of the newsgroup being
128 posted to.
129 If the function returns a string that doesn't correspond to a file, the
130 string itself is inserted.
131 If the function returns nil, the `gnus-signature-file' variable will
132 be used instead.")
133
134 (defvar gnus-init-file "~/.gnus"
135   "Your Gnus elisp startup file.
136 If a file with the .el or .elc suffixes exist, it will be read
137 instead.") 
138
139 (defvar gnus-group-faq-directory "/ftp@rtfm.mit.edu:/pub/usenet-by-group/"
140   "Directory where the group FAQs are stored.
141 This will most commonly be on a remote machine, and the file will be
142 fetched by ange-ftp.")
143
144 (defvar gnus-default-subscribed-newsgroups nil
145   "This variable lists what newsgroups should be susbcribed the first time Gnus is used.
146 It should be a list of strings.
147 If it is `t', Gnus will not do anything special the first time it is
148 started; it'll just use the normal newsgroups subscription methods.")
149
150 (defvar gnus-post-prepare-function nil
151   "Function that is run after a post buffer has been prepared.
152 It is called with the name of the newsgroup that is posted to. It
153 might be used, for instance, for inserting signatures based on the
154 newsgroup name. (In that case, `gnus-signature-file' and
155 `mail-signature' should both be set to nil).")
156
157 (defvar gnus-use-cross-reference t
158   "Non-nil means that cross referenced articles will be marked as read.
159 If nil, ignore cross references.  If t, mark articles as read in
160 subscribed newsgroups. If neither t nor nil, mark as read in all
161 newsgroups.") 
162
163 (defvar gnus-use-dribble-file t
164   "Non-nil means that Gnus will use a dribble file to store user updates.
165 If Emacs should crash without saving the .newsrc files, complete
166 information can be restored from the dribble file.")
167
168 (defvar gnus-use-followup-to 'use
169   "Specifies what to do with Followup-To header.
170 If nil, ignore the header. If it is t, use its value, but ignore 
171 `poster'. If it is neither nil nor t, which is the default, always use
172 the value.") 
173
174 (defvar gnus-followup-to-function nil
175   "A variable that contains a function that returns a followup address.
176 The function will be called in the buffer of the article that is being
177 followed up. The buffer will be narrowed to the headers of the
178 article. To pick header headers, one might use `mail-fetch-field'.  The
179 function will be called with the name of the current newsgroup as the
180 argument.
181
182 Here's an example `gnus-followup-to-function':
183
184 (setq gnus-followup-to-function
185       (lambda (group)
186         (cond ((string= group \"mail.list\")
187                (or (mail-fetch-field \"sender\") 
188                    (mail-fetch-field \"from\")))
189               (t
190                (or (mail-fetch-field \"reply-to\") 
191                    (mail-fetch-field \"from\"))))))")
192
193 (defvar gnus-reply-to-function nil
194   "A variable that contains a function that returns a reply address.
195 See the `gnus-followup-to-function' variable for an explanation of how
196 this variable is used.")
197
198 (defvar gnus-large-newsgroup 200
199   "The number of articles which indicates a large newsgroup.
200 If the number of articles in a newsgroup is greater than this value,
201 confirmation is required for selecting the newsgroup.")
202
203 (defvar gnus-author-copy (getenv "AUTHORCOPY")
204   "Save outgoing articles in this file.
205 Initialized from the AUTHORCOPY environment variable.
206
207 If this variable begins with the character \"|\", outgoing articles
208 will be piped to the named program. It is possible to save an article
209 in an MH folder as follows:
210
211 \(setq gnus-author-copy \"|/usr/local/lib/mh/rcvstore +Article\")
212
213 If the first character is not a pipe, articles are saved using the
214 function specified by the `gnus-author-copy-saver' variable.")
215
216 (defvar gnus-mail-self-blind nil
217   "Non-nil means insert a BCC header in all outgoing articles.
218 This will result in having a copy of the article mailed to yourself.
219 The BCC header is inserted when the post buffer is initialized, so you
220 can remove or alter the BCC header to override the default.")
221
222 (defvar gnus-author-copy-saver (function rmail-output)
223   "A function called to save outgoing articles.
224 This function will be called with the same of the file to store the
225 article in. The default function is `rmail-output' which saves in Unix
226 mailbox format.")
227
228 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
229   "Non-nil means that the default name of a file to save articles in is the group name.
230 If it's nil, the directory form of the group name is used instead.")
231
232 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
233   "Name of the directory articles will be saved in (default \"~/News\").
234 Initialized from the SAVEDIR environment variable.")
235
236 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
237   "Name of the directory where kill files will be stored (default \"~/News\").
238 Initialized from the SAVEDIR environment variable.")
239
240 (defvar gnus-score-expiry-days 7
241   "*Number of days before unused score file entries are expired.")
242
243 (defvar gnus-default-article-saver (function gnus-summary-save-in-rmail)
244   "A function to save articles in your favorite format.
245 The function must be interactively callable (in other words, it must
246 be an Emacs command).
247
248 Gnus provides the following functions:
249
250 * gnus-summary-save-in-rmail (Rmail format)
251 * gnus-summary-save-in-mail (Unix mail format)
252 * gnus-summary-save-in-folder (MH folder)
253 * gnus-summary-save-in-file (article format).")
254
255 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
256   "A function generating a file name to save articles in Rmail format.
257 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
258
259 (defvar gnus-mail-save-name (function gnus-plain-save-name)
260   "A function generating a file name to save articles in Unix mail format.
261 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
262
263 (defvar gnus-folder-save-name (function gnus-folder-save-name)
264   "A function generating a file name to save articles in MH folder.
265 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
266
267 (defvar gnus-file-save-name (function gnus-numeric-save-name)
268   "A function generating a file name to save articles in article format.
269 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
270
271 (defvar gnus-score-file-suffix "SCORE"
272   "Suffix of the score files.")
273
274 (defvar gnus-fetch-old-headers nil
275   "Non-nil means that Gnus will try to build threads by grabbing old headers.
276 If an unread article in the group refers to an older, already read (or
277 just marked as read) article, the old article will not normally be
278 displayed in the Summary buffer.  If this variable is non-nil, Gnus
279 will attempt to grab the headers to the old articles, and thereby
280 build complete threads.  If it has the value `some', only enough
281 headers to connect otherwise loose threads will be displayed.
282
283 The server has to support XOVER for any of this to work.")
284
285 (defvar gnus-visual t
286   "*If non-nil, will do various highlighting.
287 If nil, no mouse highlights (or any other highlights) will be
288 performed.  This might speed up Gnus some when generating large group
289 and summary buffers.")
290
291 (defvar gnus-novice-user t
292   "*Non-nil means that you are a usenet novice.
293 If non-nil, verbose messages may be displayed and confirmations may be
294 required.")
295
296 (defvar gnus-expert-user nil
297   "*Non-nil means that you will never be asked for confirmation about anything.
298 And that means *anything*.")
299
300 (defvar gnus-keep-same-level nil
301   "Non-nil means that the next newsgroup after the current will be on the same level.
302 When you type, for instance, `n' after reading the last article in the
303 current newsgroup, you will go to the next newsgroup. If this variable
304 is nil, the next newsgroup will be the next from the group
305 buffer. 
306 If this variable is non-nil, Gnus will either put you in the
307 next newsgroup with the same level, or, if no such newsgroup is
308 available, the next newsgroup with the lowest possible level higher
309 than the current level.
310 If this variable is `best', Gnus will make the next newsgroup the one
311 with the best level.")
312
313 (defvar gnus-summary-make-false-root 'adopt
314   "nil means that Gnus won't gather loose threads.
315 If the root of a thread has expired or been read in a previous
316 session, the information necessary to build a complete thread has been
317 lost. Instead of having many small sub-threads from this original thread
318 scattered all over the summary buffer, Gnus can gather them. 
319
320 If non-nil, Gnus will try to gather all loose sub-threads from an
321 original thread into one large thread.
322
323 If this variable is non-nil, it should be one of `none', `adopt',
324 `dummy' or `empty'.
325
326 If this variable is `none', Gnus will not make a false root, but just
327 present the sub-threads after another.
328 If this variable is `dummy', Gnus will create a dummy root that will
329 have all the sub-threads as children.
330 If this variable is `adopt', Gnus will make one of the \"children\"
331 the parent and mark all the step-children as such.
332 If this variable is `empty', the \"children\" are printed with empty
333 subject fields.  (Or rather, they will be printed with a string
334 given by the `gnus-summary-same-subject' variable.)")
335
336 (defvar gnus-summary-gather-subject-limit nil
337   "*Maximum length of subject comparisons when gathering loose threads.
338 Use nil to compare full subjects.  Setting this variable to a low
339 number will help gather threads that have been corrupted by
340 newsreaders chopping off subject lines, but it might also mean that
341 unrelated articles that have subject that happen to begin with the
342 same few characters will be incorrectly gathered.")
343
344 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
345 (defvar gnus-summary-same-subject ""
346   "String indicating that the current article has the same subject as the previous.
347 This variable will only be used if the value of
348 `gnus-summary-make-false-root' is `empty'.")
349
350 (defvar gnus-summary-goto-unread nil
351   "If non-nil, marking commands will go to the next unread article.")
352
353 (defvar gnus-check-new-newsgroups t
354   "Non-nil means that Gnus will add new newsgroups at startup.
355 If this variable is `ask-server', Gnus will ask the server for new
356 groups since the last time it checked. This means that the killed list
357 is no longer necessary, so you could set `gnus-save-killed-list' to
358 nil. 
359
360 A variant is to have this variable be a list of select methods. Gnus
361 will then use the `ask-server' method on all these select methods to
362 query for new groups from all those servers.
363
364 Eg.
365   (setq gnus-check-new-newsgroups 
366         '((nntp \"some.server\") (nntp \"other.server\")))
367
368 If this variable is nil, then you have to tell Gnus explicitly to
369 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
370
371 (defvar gnus-check-bogus-newsgroups nil
372   "Non-nil means that Gnus will check and remove bogus newsgroup at startup.
373 If this variable is nil, then you have to tell Gnus explicitly to
374 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
375
376 (defvar gnus-read-active-file t
377   "Non-nil means that Gnus will read the entire active file at startup.
378 If this variable is nil, Gnus will only read parts of the active file.")
379
380 (defvar gnus-activate-foreign-newsgroups nil
381   "If nil, Gnus will not check foreign newsgroups at startup.
382 If it is non-nil, it should be a number between one and nine. Foreign
383 newsgroups that have a level lower or equal to this number will be
384 activated on startup. For instance, if you want to active all
385 subscribed newsgroups, but not the rest, you'd set this variable to 5.
386
387 If you subscribe to lots of newsgroups from different servers, startup
388 might take a while. By setting this variable to nil, you'll save time,
389 but you won't be told how many unread articles there are in the
390 groups.")
391
392 (defvar gnus-save-newsrc-file t
393   "Non-nil means that Gnus will save the `.newsrc' file.
394 Gnus always saves its own startup file, which is called
395 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
396 be readily understood by other newsreaders.  If you don't plan on
397 using other newsreaders, set this variable to nil to save some time on
398 exit.")
399
400 (defvar gnus-save-killed-list t
401   "If non-nil, save the list of killed groups to the startup file.
402 This will save both time (when starting and quitting) and space (both
403 memory and disk), but it will also mean that Gnus has no record of
404 which groups are new and which are old, so the automatic new
405 newsgroups subscription methods become meaningless. You should always
406 set `gnus-check-new-newsgroups' to `ask-server' or nil if you set this
407 variable to nil.")
408
409 (defvar gnus-interactive-catchup t
410   "If non-nil, require your confirmation when catching up a group.")
411
412 (defvar gnus-interactive-post t
413   "If non-nil, group name will be asked for when posting.")
414
415 (defvar gnus-interactive-exit t
416   "If non-nil, require your confirmation when exiting Gnus.")
417
418 (defvar gnus-kill-killed nil
419   "If non-nil, Gnus will apply kill files to already killed articles.
420 If it is nil, Gnus will never apply kill files to articles that have
421 already been through the scoring process, which might very well save lots
422 of time.")
423
424 (defvar gnus-extract-address-components 'gnus-extract-address-components
425   "Function for extracting address components from a From header.
426 Two pre-defined function exist: `gnus-extract-address-components',
427 which is the default, quite fast, and too simplistic solution, and
428 `mail-extract-address-components', which works much better, but is
429 slower.")
430
431 (defvar gnus-score-interactive-default-score 1000
432   "Scoring commands will raise/lower the score with this number as the default.")
433
434 (defvar gnus-save-score nil
435   "If non-nil, save group scoring info.")
436
437 (defvar gnus-global-score-files nil
438   "List of global score files and directories.
439 Set this variable if you want to use people's score files.  One entry
440 for each score file or each score file directory.  Gnus will decide
441 by itself what score files are applicable to which group.
442
443 Say you want to use the single score file
444 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
445 score files in the \"/ftp.some-where:/pub/score\" directory.
446
447  (setq gnus-global-score-files
448        '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
449          \"/ftp.some-where:/pub/score\"))")
450
451 (defvar gnus-summary-default-score 0
452   "Default article score level.
453 If this variable is nil, scoring will be disabled.")
454
455 (defvar gnus-user-login-name nil
456   "The login name of the user.
457 Got from the function `user-login-name' if undefined.")
458
459 (defvar gnus-user-full-name nil
460   "The full name of the user.
461 Got from the NAME environment variable if undefined.")
462
463 (defvar gnus-show-mime nil
464   "*If non-ni, do mime processing of articles.
465 The articles will simply be fed to the function given by
466 `gnus-show-mime-method'.")
467
468 (defvar gnus-show-mime-method (function metamail-buffer)
469   "Function to process a MIME message.
470 The function is called from the article buffer.")
471
472 (defvar gnus-show-threads t
473   "*If non-nil, display threads in summary mode.")
474
475 (defvar gnus-thread-hide-subtree nil
476   "If non-nil, hide all threads initially.
477 If threads are hidden, you have to run the command
478 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
479 to expose hidden threads.")
480
481 (defvar gnus-thread-hide-killed t
482   "If non-nil, hide killed threads automatically.")
483
484 (defvar gnus-thread-ignore-subject nil
485   "If non-nil, ignore subjects and do all threading based on the Reference header.
486 If nil, which is the default, articles that have different subjects
487 from their parents will start separate threads.")
488
489 (defvar gnus-thread-indent-level 4
490   "Number that says how much each sub-thread should be indented.")
491
492 ;; jwz: nuke newsgroups whose name is all digits - that means that
493 ;; some loser has let articles get into the root of the news spool,
494 ;; which is toxic. Lines beginning with whitespace also tend to be
495 ;; toxic.
496 (defvar gnus-ignored-newsgroups
497   (purecopy (mapconcat 'identity
498                        '("^to\\."       ; not "real" groups
499                          "^[0-9. \t]+ " ; all digits in name
500                          "[][\"#'()     ;\\]"   ; bogus characters
501                          )
502                        "\\|"))
503   "A regexp to match uninteresting newsgroups in the active file.
504 Any lines in the active file matching this regular expression are
505 removed from the newsgroup list before anything else is done to it,
506 thus making them effectively non-existant.")
507
508 (defvar gnus-ignored-headers
509   "^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:"
510   "All headers that match this regexp will be hidden.
511 Also see `gnus-visible-headers'.")
512
513 (defvar gnus-visible-headers "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:"
514   "All headers that do not match this regexp will be hidden.
515 Also see `gnus-ignored-headers'.")
516
517 (defvar gnus-sorted-header-list
518   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:" 
519     "^Cc:" "^Date:" "^Organization:")
520   "This variable is a list of regular expressions.
521 If it is non-nil, headers that match the regular expressions will
522 be placed first in the article buffer in the sequence specified by
523 this list.")
524
525 (defvar gnus-required-headers
526   '(From Date Newsgroups Subject Message-ID Organization Lines X-Newsreader)
527   "Headers to be generated or prompted for when posting an article.
528 RFC977 and RFC1036 require From, Date, Newsgroups, Subject, Message-ID
529 and Path headers.  Organization, Lines and X-Newsreader are optional.
530 If you want Gnus not to insert some header, remove it from this
531 list.") 
532
533 (defvar gnus-show-all-headers nil
534   "*If non-nil, don't hide any headers.")
535
536 (defvar gnus-save-all-headers t
537   "*If non-nil, don't remove any headers before saving.")
538
539 (defvar gnus-inhibit-startup-message nil
540   "If non-nil, the startup message will not be displayed.")
541
542 (defvar gnus-auto-extend-newsgroup t
543   "If non-nil, extend newsgroup forward and backward when requested.")
544
545 (defvar gnus-auto-select-first t
546   "If non-nil, select the first unread article when entering a group.
547 If you want to prevent automatic selection of the first unread article
548 in some newsgroups, set the variable to nil in
549 `gnus-select-group-hook'.") 
550
551 (defvar gnus-auto-select-next t
552   "If non-nil, offer to go to the next group from the end of the previous.
553 If the value is t and the next newsgroup is empty, Gnus will exit
554 summary mode and go back to group mode.  If the value is neither nil
555 nor t, Gnus will select the following unread newsgroup.  In
556 particular, if the value is the symbol `quietly', the next unread
557 newsgroup will be selected without any confirmations.")
558
559 (defvar gnus-auto-select-same nil
560   "If non-nil, select the next article with the same subject.")
561
562 (defvar gnus-auto-center-summary t
563   "*If non-nil, always center the current summary buffer.")
564
565 (defvar gnus-auto-mail-to-author nil
566   "*If non-nil, mail the authors of articles a copy of your follow-ups.
567 If this variable is `ask', the user will be prompted for whether to
568 mail a copy.  The string given by `gnus-mail-courtesy-message' will be
569 inserted at the beginning of the mail copy.
570
571 Mail is sent using the function specified by the
572 `gnus-mail-send-method' variable.")
573
574 ;; Added by Ethan Bradford <ethanb@ptolemy.astro.washington.edu>.
575 (defvar gnus-mail-courtesy-message
576   "The following message is a courtesy copy of an article\nthat has been posted as well.\n\n"
577   "This is inserted at the start of a mailed copy of a posted message.
578 If this variable is nil, no such courtesy message will be added.")
579
580 (defvar gnus-break-pages t
581   "*If non-nil, do page breaking on articles.
582 The page delimiter is specified by the `gnus-page-delimiter'
583 variable.")
584
585 (defvar gnus-page-delimiter "^\^L"
586   "Regexp describing what to use as article page delimiters.
587 The default value is \"^\^L\", which is a form linefeed at the
588 beginning of a line.")
589
590 (defvar gnus-use-full-window t
591   "*If non-nil, use the entire Emacs screen.")
592
593 (defvar gnus-window-configuration
594   '((summary (0 1 0))
595     (newsgroups (1 0 0))
596     (article (0 3 10)))
597   "Specify window configurations for each action.
598 The format of the variable is either a list of (ACTION (G S A)), where
599 G, S, and A are the relative height of group, summary, and article
600 windows, respectively, or a list of (ACTION FUNCTION), where FUNCTION
601 is a function that will be called with ACTION as an argument. ACTION
602 can be `summary', `newsgroups', or `article'.")
603
604 (defvar gnus-mail-reply-method (function gnus-mail-reply-using-mail)
605   "Function to compose a reply.
606 Two pre-made functions are `gnus-mail-reply-using-mail' (sendmail) and
607 `gnus-mail-reply-using-mhe' (MH-E).")
608
609 (defvar gnus-mail-forward-method (function gnus-mail-forward-using-mail)
610   "Function to forward the current message to another user.
611 Two pre-made functions are `gnus-mail-forward-using-mail' (sendmail)
612 and `gnus-mail-forward-using-mhe' (MH-E).") 
613
614 (defvar gnus-mail-other-window-method 'gnus-mail-other-window-using-mail
615   "Function to compose mail in the other window.
616 Two pre-made functions are `gnus-mail-other-window-using-mail'
617 (sendmail) and `gnus-mail-other-window-using-mhe' (MH-E).")
618
619 (defvar gnus-mail-send-method send-mail-function
620   "Function to mail a message which is also being posted as an article.
621 The message must have To or Cc header.  The default is copied from
622 the variable `send-mail-function'.")
623
624 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
625   "Function called with a group name when new group is detected.
626 A few pre-made functions are supplied: `gnus-subscribe-randomly'
627 inserts new groups at the beginning of the list of groups;
628 `gnus-subscribe-alphabetically' inserts new groups in strict
629 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
630 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
631 for your decision.")
632
633 ;; Suggested by a bug report by Hallvard B Furuseth
634 ;; <h.b.furuseth@usit.uio.no>. 
635 (defvar gnus-subscribe-options-newsgroup-method
636   (function gnus-subscribe-alphabetically)
637   "This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
638 If, for instance, you want to subscribe to all newsgroups in the
639 \"no\" and \"alt\" hierarchies, you'd put the following in your
640 .newsrc file:
641
642 options -n no.all alt.all
643
644 Gnus will the subscribe all new newsgroups in these hierarchies with
645 the subscription method in this variable.")
646
647 (defvar gnus-subscribe-hierarchical-interactive nil
648   "If non-nil, Gnus will offer to subscribe hierarchically.
649 When a new hierarchy appears, Gnus will ask the user:
650
651 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
652
653 If the user pressed `d', Gnus will descend the hierarchy, `y' will
654 subscribe to all newsgroups in the hierarchy and `s' will skip this
655 hierarchy in its entirety.")
656
657 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
658   "Function used for sorting the group buffer.
659 This function will be called with group info entries as the arguments
660 for the groups to be sorted.  Pre-made functions include
661 `gnus-sort-by-alphabet', `gnus-sort-by-unread' and
662 `gnus-sort-by-level'")
663
664 ;; Mark variables suggested by Thomas Michanek
665 ;; <Thomas.Michanek@telelogic.se>. 
666 (defvar gnus-unread-mark ? 
667   "Mark used for unread articles.")
668 (defvar gnus-ticked-mark ?!
669   "Mark used for ticked articles.")
670 (defvar gnus-dormant-mark ??
671   "Mark used for dormant articles.")
672 (defvar gnus-dread-mark ?D
673   "Mark used for read articles.")
674 (defvar gnus-read-mark ?d
675   "Mark used for read articles.")
676 (defvar gnus-expirable-mark ?E
677   "Mark used for expirable articles.")
678 (defvar gnus-killed-mark ?K
679   "Mark used for killed articles.")
680 (defvar gnus-kill-file-mark ?X
681   "Mark used for articles killed by kill files.")
682 (defvar gnus-low-score-mark ?Y
683   "Mark used for articles with a low score.")
684 (defvar gnus-catchup-mark ?C
685   "Mark used for articles that are caught up.")
686 (defvar gnus-replied-mark ?R
687   "Mark used for articles that have been replied to.")
688 (defvar gnus-process-mark ?# 
689   "Process mark.")
690 (defvar gnus-ancient-mark ?A
691   "Mark used for ancient articles.")
692 (defvar gnus-canceled-mark ?G
693   "Mark used for cancelled articles.")
694 (defvar gnus-score-over-mark ?+
695   "Score mark used for articles with high scores.")
696 (defvar gnus-score-below-mark ?-
697   "Score mark used for articles with low scores.")
698
699 (defvar gnus-view-pseudo-asynchronously nil
700   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
701
702 (defvar gnus-view-pseudos nil
703   "If `automatic', pseudo-articles will be viewed automatically.
704 If `not-confirm', pseudos will be viewed automatically, and the user
705 will not be asked to confirm the command.")
706
707 (defvar gnus-group-line-format "%M%S%5y: %(%g%)\n"
708   "Format of groups lines.
709 It works along the same lines as a normal formatting string,
710 with some simple extensions.
711
712 %M    Only marked articles (character, \"*\" or \" \")
713 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
714 %L    Level of subscribedness (integer, 1-9)
715 %N    Number of unread articles (integer)
716 %I    Number of dormant articles (integer)
717 %i    Number of ticked and dormant (integer)
718 %T    Number of ticked articles (integer)
719 %R    Number of read articles (integer)
720 %t    Total number of articles (integer)
721 %y    Number of unread, unticked articles (integer)
722 %G    Group name (string)
723 %g    Qualified group name (string)
724 %D    Group description (string)
725 %s    Select method (string)
726 %o    Moderated group (char, \"m\")
727 %O    Moderated group (string, \"(m)\" or \"\")
728 %n    Select from where (string)
729 %z    A string that look like `<%s:%n>' if a foreign select method is used
730 %u    User defined specifier. The next character in the format string should
731       be a letter.  Gnus will call the function gnus-user-format-function-X,
732       where X is the letter following %u. The function will be passed the
733       current header as argument. The function should return a string, which
734       will be inserted into the buffer just like information from any other
735       group specifier.
736
737 Text between %( and %) will be highlighted with `gnus-mouse-face' when
738 the mouse point move inside the area.  There can only be one such area.
739
740 Note that this format specification is not always respected. For
741 reasons of efficiency, when listing killed groups, this specification
742 is ignored altogether. If the spec is changed considerably, your
743 output may end up looking strange when listing both alive and killed
744 groups.
745
746 If you use %o or %O, reading the active file will be slower and quite
747 a bit of extra memory will be used. %D will also worsen performance.
748 Also note that if you change the format specification to include any
749 of these specs, you must probably re-start Gnus to see them go into
750 effect.") 
751
752 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
753   "The format specification of the lines in the summary buffer.
754
755 It works along the same lines as a normal formatting string,
756 with some simple extensions.
757
758 %N   Article number, left padded with spaces (string)
759 %S   Subject (string)
760 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
761 %n   Name of the poster (string)
762 %A   Address of the poster (string)
763 %L   Number of lines in the article (integer)
764 %c   Number of characters in the article (integer)
765 %D   Date of the article (string)
766 %I   Indentation based on thread level (a string of spaces)
767 %T   A string with two possible values: 80 spaces if the article
768      is on thread level two or larger and 0 spaces on level one
769 %U   Status of this article (character, \"D\", \"K\", \"-\" or \" \") 
770 %[   Opening bracket (character, \"[\" or \"<\")
771 %]   Closing bracket (character, \"]\" or \">\")
772 %>   Spaces of length thread-level (string)
773 %<   Spaces of length (- 20 thread-level) (string)
774 %i   Article score (number)
775 %z   Article zcore (character)
776 %u   User defined specifier. The next character in the format string should
777      be a letter.  Gnus will call the function gnus-user-format-function-X,
778      where X is the letter following %u. The function will be passed the
779      current header as argument. The function should return a string, which
780      will be inserted into the summary just like information from any other
781      summary specifier.
782
783 Text between %( and %) will be highlighted with `gnus-mouse-face'
784 when the mouse point is placed inside the area.  There can only be one
785 such area.
786
787 The %U (status), %R (replied) and %z (zcore) specs have to be handled
788 with care. For reasons of efficiency, Gnus will compute what column
789 these characters will end up in, and \"hard-code\" that. This means that
790 it is illegal to have these specs after a variable-length spec. Well,
791 you might not be arrested, but your summary buffer will look strange,
792 which is bad enough.
793
794 The smart choice is to have these specs as for to the left as
795 possible. 
796
797 This restriction may disappear in later versions of Gnus.")
798
799 (defvar gnus-summary-dummy-line-format "*   :                          : %S\n"
800   "The format specification for the dummy roots in the summary buffer.
801 It works along the same lines as a normal formatting string,
802 with some simple extensions.
803
804 %S  The subject")
805
806 (defvar gnus-summary-mode-line-format "(ding) %G/%A %Z"
807   "The format specification for the summary mode line.")
808
809 (defvar gnus-article-mode-line-format "(ding) %G/%A %S"
810   "The format specification for the article mode line.")
811
812 (defvar gnus-group-mode-line-format "(ding) List of groups   {%M:%S}  "
813   "The format specification for the group mode line.")
814
815 (defvar gnus-valid-select-methods
816   '(("nntp" post address prompt-address)
817     ("nnspool" post) ("nnvirtual" none virtual prompt-address) 
818     ("nnmbox" mail respool) ("nnml" mail respool)
819     ("nnmh" mail respool) ("nndir" none prompt-address) ("nndigest" none)
820     ("nndoc" none prompt-address) ("nnbabyl" mail respool)
821     ("nnkiboze" none virtual) ("nnfolder" mail respool))
822   "An alist of valid select methods.
823 The first element of each list lists should be a string with the name
824 of the select method. The other elements may be be the category of
825 this method (ie. `post', `mail', `none' or whatever) or other
826 properties that this method has (like being respoolable).
827 If you implement a new select method, all you should have to change is
828 this variable. I think.")
829
830 (defvar gnus-updated-mode-lines '(group article summary)
831   "List of buffers that should update their mode lines.
832 The list may contain the symbols `group', `article' and `summary'. If
833 the corresponding symbol is present, Gnus will keep that mode line
834 updated with information that may be pertinent. 
835 If this variable is nil, screen refresh may be quicker.")
836
837 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
838 (defvar gnus-mode-non-string-length 21
839   "Max length of mode-line non-string contents buffer contents.")
840
841 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
842 (defvar gnus-mouse-face 'highlight
843   "Face used for mouse highlighting in Gnus.
844 No mouse highlights will be done if `gnus-visual' is nil.")
845
846 (defvar gnus-summary-mark-below nil
847   "Mark all articles with a score below this variable as read.
848 This variable is local to each summary buffer and usually set by the
849 score file.")  
850
851 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
852   "List of functions used for sorting threads in the summary buffer.
853 By default, threads are sorted by article number.
854
855 Each function takes two threads and return non-nil if the first thread
856 should be sorted before the other.  If you use more than one function,
857 the primary sort function should be the last.
858
859 Ready-mady functions include `gnus-thread-sort-by-number',
860 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
861 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
862 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
863
864 The latter two only work on threads that have been scored prior to
865 entering the newsgroup.")
866
867 (defvar gnus-thread-score-function '+
868   "Function used for calculating the total score of a thread.
869
870 The function is called with the scores of the article and each
871 subthread and should then return the score of the thread.
872
873 Some functions you can use are `+', `max', or `min'.")
874
875 (defvar gnus-score-find-score-files-function 'gnus-score-find-bnews
876   "Function used to find SCORE files.
877 The function will be called with the group name as the argument, and
878 should return a list of score files to apply to that group.  The score
879 files do not actually have to exist.
880
881 Predefined values are:
882
883 gnus-score-find-single: Only apply the group's own SCORE file.
884 gnus-score-find-hierarchical: Also apply SCORE files from parent groups.
885 gnus-score-find-bnews: Apply SCORE files whose names matches.
886
887 See the documentation to these functions for more information.")
888
889 (defvar gnus-options-subscribe nil
890   "All new groups matching this regexp will be subscribed unconditionally.
891 Note that this variable deals only with new newsgroups.  This variable
892 does not affect old newsgroups.")
893
894 (defvar gnus-options-not-subscribe nil
895   "All new groups matching this regexp will be ignored.
896 Note that this variable deals only with new newsgroups.  This variable
897 does not affect old (already subscribed) newsgroups.")
898
899 (defvar gnus-auto-expirable-newsgroups nil
900   "Groups in which to automatically mark read articles as expirable.
901 If non-nil, this should be a regexp that should match all groups in
902 which to perform auto-expiry. This only makes sense for mail groups.")
903
904
905 ;; Hooks.
906
907 (defvar gnus-group-mode-hook nil
908   "A hook for Gnus group mode.")
909
910 (defvar gnus-summary-mode-hook nil
911   "A hook for Gnus summary mode.")
912
913 (defvar gnus-article-mode-hook nil
914   "A hook for Gnus article mode.")
915
916 (defvar gnus-open-server-hook nil
917   "A hook called just before opening connection to the news server.")
918
919 (defvar gnus-startup-hook nil
920   "A hook called at startup.
921 This hook is called after Gnus is connected to the NNTP server.")
922
923 (defvar gnus-get-new-news-hook nil
924   "A hook run just before Gnus checks for new news.")
925
926 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
927   "A function that is called to generate the group buffer.
928 The function is called with three arguments: The first is a number;
929 all group with a level less or equal to that number should be listed,
930 if the second is non-nil, empty groups should also be displayed. If
931 the third is non-nil, it is a number. No groups with a level lower
932 than this number should be displayed.
933
934 The only current function implemented is `gnus-group-prepare-flat'.")
935
936 (defvar gnus-group-prepare-hook nil
937   "A hook called after the group buffer has been generated.
938 If you want to modify the group buffer, you can use this hook.")
939
940 (defvar gnus-summary-prepare-hook nil
941   "A hook called after the summary buffer has been generated.
942 If you want to modify the summary buffer, you can use this hook.")
943
944 (defvar gnus-article-prepare-hook nil
945   "A hook called after an article has been prepared in the article buffer.
946 If you want to run a special decoding program like nkf, use this hook.")
947
948 (defvar gnus-article-display-hook nil
949   "A hook called after the article is displayed in the article buffer.
950 The hook is designed to change the contents of the article
951 buffer. Typical functions that this hook may contain are
952 `gnus-article-hide-headers' (hide selected headers),
953 `gnus-article-hide-signature' (hide signature) and
954 `gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
955 (add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
956 (add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
957
958 (defvar gnus-select-group-hook nil
959   "A hook called when a newsgroup is selected.
960
961 If you'd like to simplify subjects like the
962 `gnus-summary-next-same-subject' command does, you can use the
963 following hook:
964
965  (setq gnus-select-group-hook
966       (list
967         (lambda ()
968           (mapcar (lambda (header)
969                      (header-set-subject
970                       header
971                       (gnus-simplify-subject
972                        (header-subject header) 're-only)))
973                   gnus-newsgroup-headers))))")
974
975 (defvar gnus-select-article-hook
976   '(gnus-summary-show-thread)
977   "A hook called when an article is selected.
978 The default hook shows conversation thread subtrees of the selected
979 article automatically using `gnus-summary-show-thread'.")
980
981 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
982   "A hook called to apply kill files to a group.
983 This hook is intended to apply a kill file to the selected newsgroup.
984 The function `gnus-apply-kill-file' is called by default.
985
986 Since a general kill file is too heavy to use only for a few
987 newsgroups, I recommend you to use a lighter hook function. For
988 example, if you'd like to apply a kill file to articles which contains
989 a string `rmgroup' in subject in newsgroup `control', you can use the
990 following hook:
991
992 \(setq gnus-apply-kill-hook
993       (list
994         (lambda ()
995           (cond ((string-match \"control\" gnus-newsgroup-name)
996                  (gnus-kill \"Subject\" \"rmgroup\")
997                  (gnus-expunge \"X\"))))))")
998
999 (defvar gnus-visual-mark-article-hook 
1000   (list 'gnus-visual-highlight-selected-summary)
1001   "Hook run after selecting an article in the summary buffer.
1002 It is meant to be used for highlighting the article in some way.  It
1003 is not run if `gnus-visual' is nil.")
1004
1005 (defvar gnus-prepare-article-hook (list 'gnus-inews-insert-signature)
1006   "A hook called after preparing body, but before preparing header headers.
1007 The default hook (`gnus-inews-insert-signature') inserts a signature
1008 file specified by the variable `gnus-signature-file'.")
1009
1010 (defvar gnus-inews-article-hook (list 'gnus-inews-do-fcc)
1011   "A hook called before finally posting an article.
1012 The default hook (`gnus-inews-do-fcc') does FCC processing (ie. saves
1013 the article to a file).")
1014
1015 (defvar gnus-inews-article-header-hook nil
1016   "A hook called after inserting the headers in an article to be posted.
1017 The hook is called from the *post-news* buffer, narrowed to the
1018 headers.")
1019
1020 (defvar gnus-exit-group-hook nil
1021   "A hook called when exiting (not quitting) summary mode.")
1022
1023 (defvar gnus-suspend-gnus-hook nil
1024   "A hook called when suspending (not exiting) Gnus.")
1025
1026 (defvar gnus-exit-gnus-hook nil
1027   "A hook called when exiting Gnus.")
1028
1029 (defvar gnus-save-newsrc-hook nil
1030   "A hook called when saving the newsrc file.")
1031
1032 (defvar gnus-visual-summary-update-hook 
1033   (list 'gnus-visual-summary-highlight-line)
1034   "A hook called when a summary line is changed.
1035 The hook will not be called if `gnus-visual' is nil.
1036
1037 The default function `gnus-visual-summary-highlight-line' will
1038 highlight the line according to the `gnus-visual-summary-highlight'
1039 variable.")
1040
1041 (defvar gnus-mark-article-hook (list 'gnus-summary-mark-unread-as-read)
1042   "A hook called when an article is selected for the first time.
1043 The hook is intended to mark an article as read (or unread)
1044 automatically when it is selected.")
1045
1046 ;; Site dependent variables. These variables should be defined in
1047 ;; paths.el.
1048
1049 (defvar gnus-default-nntp-server nil
1050   "Specify a default NNTP server.
1051 This variable should be defined in paths.el, and should never be set
1052 by the user.
1053 If you want to change servers, you should use `gnus-select-method'.
1054 See the documentation to that variable.")
1055
1056 (defconst gnus-backup-default-subscribed-newsgroups 
1057   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
1058   "Default default new newsgroups the first time Gnus is run.
1059 Should be set in paths.el, and shouldn't be touched by the user.")
1060
1061 (defvar gnus-local-domain nil
1062   "Local domain name without a host name.
1063 The DOMAINNAME environment variable is used instead if it is defined.
1064 If the `system-name' function returns the full Internet name, there is
1065 no need to set this variable.")
1066
1067 (defvar gnus-local-organization nil
1068   "String with a description of what organization (if any) the user belongs to.
1069 The ORGANIZATION environment variable is used instead if it is defined.
1070 If this variable contains a function, this function will be called
1071 with the current newsgroup name as the argument. The function should
1072 return a string.
1073 In any case, if the string (either in the variable, in the environment
1074 variable, or returned by the function) is a file name, the contents of
1075 this file will be used as the organization.")
1076
1077 (defvar gnus-use-generic-from nil
1078   "If nil, the full host name will be the system name prepended to the domain name.
1079 If this is a string, the full host name will be this string.
1080 If this is non-nil, non-string, the domain name will be used as the
1081 full host name.")
1082
1083 (defvar gnus-use-generic-path nil
1084   "If nil, use the NNTP server name in the Path header.
1085 If stringp, use this; if non-nil, use no host name (user name only).")
1086
1087 \f
1088 ;; Internal variables
1089
1090 ;; Avoid highlighting in kill files.
1091 (defvar gnus-summary-inhibit-highlight nil)
1092 (defvar gnus-newsgroup-selected-overlay nil)
1093
1094 (defvar gnus-article-mode-map nil)
1095 (defvar caesar-translate-table nil)
1096 (defvar gnus-dribble-buffer nil)
1097 (defvar gnus-headers-retrieved-by nil)
1098 (defvar gnus-article-reply nil)
1099 (defvar gnus-override-method nil)
1100 (defvar gnus-article-check-size nil)
1101 (defvar gnus-score-file-list nil)
1102 (defvar gnus-internal-global-score-files nil)
1103 (defvar gnus-current-score-file nil)
1104
1105 (defvar gnus-score-alist nil
1106   "Alist containing score information.
1107 The keys can be symbols or strings.  The following symbols are defined. 
1108
1109 touched: If this alist has been modified.
1110 mark:    Automatically mark articles below this.
1111 expunge: Automatically expunge articles below this.
1112 files:   List of other SCORE files to load when loading this one.
1113 eval:    Sexp to be evaluated when the score file is loaded.
1114
1115 String entries have the form (HEADER (MATCH TYPE SCORE DATE) ...) 
1116 where HEADER is the header being scored, MATCH is the string we are
1117 looking for, TYPE is a flag indicating whether it should use regexp or
1118 substring matching, SCORE is the score to add and DATE is the date
1119 of the last succesful match.")
1120
1121 (defvar gnus-score-cache nil)
1122 (defvar gnus-scores-articles nil)
1123 (defvar gnus-header-index nil)
1124 (defvar gnus-score-index nil)
1125
1126 (defvar gnus-newsgroup-dependencies nil)
1127 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1128 (defvar gnus-default-subscribe-level 2)
1129 (defvar gnus-default-unsubscribe-level 6)
1130 (defvar gnus-default-kill-level 9)
1131
1132 (defconst gnus-group-line-format-alist
1133   (list (list ?M 'marked ?c)
1134         (list ?S 'subscribed ?c)
1135         (list ?L 'level ?d)
1136         (list ?N 'number ?s)
1137         (list ?I 'number-of-dormant ?d)
1138         (list ?T 'number-of-ticked ?d)
1139         (list ?R 'number-of-read ?s)
1140         (list ?t 'number-total ?d)
1141         (list ?y 'number-of-unread-unticked ?s)
1142         (list ?i 'number-of-ticked-and-dormant ?d)
1143         (list ?g 'group ?s)
1144         (list ?G 'qualified-group ?s)
1145         (list ?D 'newsgroup-description ?s)
1146         (list ?o 'moderated ?c)
1147         (list ?O 'moderated-string ?s)
1148         (list ?s 'news-server ?s)
1149         (list ?n 'news-method ?s)
1150         (list ?z 'news-method-string ?s)
1151         (list ?u 'user-defined ?s)))
1152
1153 (defconst gnus-summary-line-format-alist 
1154   (list (list ?N 'number ?s)
1155         (list ?S 'subject ?s)
1156         (list ?s 'subject-or-nil ?s)
1157         (list ?n 'name ?s)
1158         (list ?A 'address ?s)
1159         (list ?F 'from ?s)
1160         (list ?x (macroexpand '(header-xref header)) ?s)
1161         (list ?D (macroexpand '(header-date header)) ?s)
1162         (list ?M (macroexpand '(header-id header)) ?s)
1163         (list ?r (macroexpand '(header-references header)) ?s)
1164         (list ?c (macroexpand '(header-chars header)) ?d)
1165         (list ?L 'lines ?d)
1166         (list ?I 'indentation ?s)
1167         (list ?T '(if (< level 1) "" (make-string (frame-width) ? )) ?s)
1168         (list ?R 'replied ?c)
1169         (list ?\[ 'opening-bracket ?c)
1170         (list ?\] 'closing-bracket ?c)
1171         (list ?\> '(make-string level ? ) ?s)
1172         (list ?\< '(make-string (max 0 (- 20 level)) ? ) ?s)
1173         (list ?i 'score ?s)
1174         (list ?z 'score-char ?c)
1175         (list ?U 'unread ?c)
1176         (list ?u 'user-defined ?s))
1177   "An alist of format specifications that can appear in summary lines,
1178 and what variables they correspond with, along with the type of the
1179 variable (string, integer, character, etc).")
1180
1181 (defconst gnus-summary-dummy-line-format-alist
1182   (list (list ?S 'subject ?s)
1183         (list ?N 'number ?d)))
1184
1185 (defconst gnus-summary-mode-line-format-alist 
1186   (list (list ?G 'group-name ?s)
1187         (list ?A 'article-number ?d)
1188         (list ?Z 'unread-and-unselected ?s)
1189         (list ?V 'gnus-version ?s)
1190         (list ?U 'unread ?d)
1191         (list ?S 'subject ?s)
1192         (list ?u 'unselected ?d)))
1193
1194 (defconst gnus-group-mode-line-format-alist 
1195   (list (list ?S 'news-server ?s)
1196         (list ?M 'news-method ?s)))
1197
1198 (defvar gnus-have-read-active-file nil)
1199
1200 (defconst gnus-maintainer "Lars Magne Ingebrigtsen <larsi@ifi.uio.no>"
1201   "The mail address of the Gnus maintainer.")
1202
1203 (defconst gnus-version "(ding) Gnus v0.43"
1204   "Version number for this version of Gnus.")
1205
1206 (defvar gnus-info-nodes
1207   '((gnus-group-mode            "(gnus)The Group Buffer")
1208     (gnus-summary-mode          "(gnus)The Summary Buffer")
1209     (gnus-article-mode          "(gnus)The Article Buffer"))
1210   "Assoc list of major modes and related Info nodes.")
1211
1212 (defvar gnus-documentation-group-file "~/dgnus/lisp/doc.txt"
1213   "The location of the (ding) Gnus documentation group.")
1214
1215 (defvar gnus-group-buffer "*Group*")
1216 (defvar gnus-summary-buffer "*Summary*")
1217 (defvar gnus-article-buffer "*Article*")
1218
1219 (defvar gnus-buffer-list nil
1220   "Gnus buffers that should be killed on exit.")
1221
1222 (defvar gnus-variable-list
1223   '(gnus-newsrc-options 
1224     gnus-newsrc-options-n-yes gnus-newsrc-options-n-no
1225     gnus-newsrc-last-checked-date
1226     gnus-newsrc-assoc gnus-killed-list gnus-zombie-list)
1227   "Gnus variables saved in the quick startup file.")
1228
1229 (defvar gnus-overload-functions
1230   '((news-inews gnus-inews-news "rnewspost")
1231     (caesar-region gnus-caesar-region "rnews"))
1232   "Functions overloaded by gnus.
1233 It is a list of `(original overload &optional file)'.")
1234
1235 (defvar gnus-newsrc-options nil
1236   "Options line in the .newsrc file.")
1237
1238 (defvar gnus-newsrc-options-n-yes nil
1239   "Regexp representing groups to be subscribed to unconditionally.")
1240
1241 (defvar gnus-newsrc-options-n-no nil
1242   "Regexp representing group to be ignored unconditionally.")
1243
1244 (defvar gnus-newsrc-last-checked-date nil
1245   "Date Gnus last asked server for new newsgroups.")
1246
1247 (defvar gnus-newsrc-assoc nil
1248   "Assoc list of read articles.
1249 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1250
1251 (defvar gnus-newsrc-hashtb nil
1252   "Hashtable of gnus-newsrc-assoc.")
1253
1254 (defvar gnus-killed-list nil
1255   "List of killed newsgroups.")
1256
1257 (defvar gnus-killed-hashtb nil
1258   "Hash table equivalent of gnus-killed-list.")
1259
1260 (defvar gnus-zombie-list nil
1261   "List of almost dead newsgroups.")
1262
1263 (defvar gnus-description-hashtb nil
1264   "Descriptions of newsgroups.")
1265
1266 (defvar gnus-list-of-killed-groups nil
1267   "List of newsgroups that have recently been killed by the user.")
1268
1269 (defvar gnus-active-hashtb nil
1270   "Hashtable of active articles.")
1271
1272 (defvar gnus-moderated-list nil
1273   "List of moderated newsgroups.")
1274
1275 (defvar gnus-current-startup-file nil
1276   "Startup file for the current host.")
1277
1278 (defvar gnus-last-search-regexp nil
1279   "Default regexp for article search command.")
1280
1281 (defvar gnus-last-shell-command nil
1282   "Default shell command on article.")
1283
1284 (defvar gnus-current-select-method nil
1285   "The current method for selecting a newsgroup.")
1286
1287 (defvar gnus-have-all-newsgroups nil)
1288
1289 (defvar gnus-article-internal-prepare-hook nil)
1290
1291 (defvar gnus-newsgroup-name nil)
1292 (defvar gnus-newsgroup-begin nil)
1293 (defvar gnus-newsgroup-end nil)
1294 (defvar gnus-newsgroup-last-rmail nil)
1295 (defvar gnus-newsgroup-last-mail nil)
1296 (defvar gnus-newsgroup-last-folder nil)
1297 (defvar gnus-newsgroup-last-file nil)
1298 (defvar gnus-newsgroup-auto-expire nil)
1299
1300 (defvar gnus-newsgroup-unreads nil
1301   "List of unread articles in the current newsgroup.")
1302
1303 (defvar gnus-newsgroup-unselected nil
1304   "List of unselected unread articles in the current newsgroup.")
1305
1306 (defvar gnus-newsgroup-marked nil
1307   "List of ticked articles in the current newsgroup (a subset of unread art).")
1308
1309 (defvar gnus-newsgroup-killed nil
1310   "List of ranges of articles that have been through the scoring process.")
1311
1312 (defvar gnus-newsgroup-kill-headers nil)
1313
1314 (defvar gnus-newsgroup-replied nil
1315   "List of articles that have been replied to in the current newsgroup.")
1316
1317 (defvar gnus-newsgroup-expirable nil
1318   "List of articles in the current newsgroup that can be expired.")
1319
1320 (defvar gnus-newsgroup-processable nil
1321   "List of articles in the current newsgroup that can be processed.")
1322
1323 (defvar gnus-newsgroup-bookmarks nil
1324   "List of articles in the current newsgroup that have bookmarks.")
1325
1326 (defvar gnus-newsgroup-dormant nil
1327   "List of dormant articles in the current newsgroup.")
1328
1329 (defvar gnus-newsgroup-scored nil
1330   "List of scored articles in the current newsgroup.")
1331
1332 (defvar gnus-newsgroup-headers nil
1333   "List of article headers in the current newsgroup.")
1334 (defvar gnus-newsgroup-headers-hashtb-by-number nil)
1335
1336 (defvar gnus-newsgroup-ancient nil
1337   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1338
1339 (defvar gnus-current-article nil)
1340 (defvar gnus-article-current nil)
1341 (defvar gnus-current-headers nil)
1342 (defvar gnus-have-all-headers nil)
1343 (defvar gnus-last-article nil)
1344 (defvar gnus-newsgroup-history nil)
1345 (defvar gnus-current-kill-article nil)
1346
1347 ;; Save window configuration.
1348 (defvar gnus-winconf-edit-group nil)
1349 (defvar gnus-winconf-edit-score nil)
1350
1351 ;; Format specs
1352 (defvar gnus-summary-line-format-spec nil)
1353 (defvar gnus-summary-dummy-line-format-spec nil)
1354 (defvar gnus-group-line-format-spec nil)
1355 (defvar gnus-summary-mode-line-format-spec nil)
1356 (defvar gnus-article-mode-line-format-spec nil)
1357 (defvar gnus-group-mode-line-format-spec nil)
1358 (defvar gnus-summary-mark-positions nil)
1359
1360 (defvar gnus-summary-expunge-below nil)
1361 (defvar gnus-reffed-article-number nil)
1362
1363 (defvar rmail-default-file (expand-file-name "~/XMBOX"))
1364 (defvar rmail-default-rmail-file (expand-file-name "~/XNEWS"))
1365
1366 (defconst gnus-summary-local-variables 
1367   '(gnus-newsgroup-name 
1368     gnus-newsgroup-begin gnus-newsgroup-end 
1369     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail 
1370     gnus-newsgroup-last-folder gnus-newsgroup-last-file 
1371     gnus-newsgroup-auto-expire gnus-newsgroup-unreads 
1372     gnus-newsgroup-unselected gnus-newsgroup-marked
1373     gnus-newsgroup-replied gnus-newsgroup-expirable
1374     gnus-newsgroup-processable gnus-newsgroup-killed
1375     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1376     gnus-newsgroup-headers gnus-newsgroup-headers-hashtb-by-number
1377     gnus-current-article gnus-current-headers gnus-have-all-headers
1378     gnus-last-article gnus-article-internal-prepare-hook
1379     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1380     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1381     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below 
1382     gnus-summary-mark-below
1383     gnus-newsgroup-history gnus-newsgroup-ancient)
1384   "Variables that are buffer-local to the summary buffers.")
1385
1386 ;;; End of variables.
1387
1388 ;; Define some autoload functions Gnus might use.
1389 (eval-and-compile
1390   (autoload 'metamail-buffer "metamail")
1391   (autoload 'Info-goto-node "info")
1392   
1393   (autoload 'timezone-make-date-arpa-standard "timezone")
1394   (autoload 'timezone-fix-time "timezone")
1395   (autoload 'timezone-make-sortable-date "timezone")
1396   (autoload 'timezone-make-time-string "timezone")
1397   
1398   (autoload 'rmail-output "rmailout")
1399   (autoload 'mail-position-on-field "sendmail")
1400   (autoload 'mail-setup "sendmail")
1401   (autoload 'news-mail-other-window "rnewspost")
1402
1403   (autoload 'gnus-mail-reply-using-mhe "gnus-mh")
1404   (autoload 'gnus-mail-forward-using-mhe "gnus-mh")
1405   (autoload 'gnus-mail-other-window-using-mhe "gnus-mh")
1406   (autoload 'gnus-summary-save-in-folder "gnus-mh")
1407   (autoload 'gnus-Folder-save-name "gnus-mh")
1408   (autoload 'gnus-folder-save-name "gnus-mh")
1409   
1410   (autoload 'gnus-group-make-menu-bar "gnus-visual")
1411   (autoload 'gnus-summary-make-menu-bar "gnus-visual")
1412   (autoload 'gnus-article-make-menu-bar "gnus-visual")
1413   (autoload 'gnus-visual-highlight-selected-summary "gnus-visual")
1414   (autoload 'gnus-visual-summary-highlight-line "gnus-visual")
1415
1416   (autoload 'gnus-uu-mark-by-regexp "gnus-uu")
1417   (autoload 'gnus-uu-mark-region "gnus-uu")
1418   (autoload 'gnus-uu-mark-thread "gnus-uu")
1419   (autoload 'gnus-uu-mark-sparse "gnus-uu")
1420   (autoload 'gnus-uu-mark-series "gnus-uu")
1421   (autoload 'gnus-uu-mark-all "gnus-uu")
1422   (autoload 'gnus-uu-post-news "gnus-uu")
1423   (autoload 'gnus-uu-digest-and-forward "gnus-uu")
1424
1425   (autoload 'gnus-uu-decode-uu "gnus-uu")
1426   (autoload 'gnus-uu-decode-uu-and-save "gnus-uu")
1427   (autoload 'gnus-uu-decode-unshar "gnus-uu")
1428   (autoload 'gnus-uu-decode-unshar-and-save "gnus-uu")
1429   (autoload 'gnus-uu-decode-save "gnus-uu")
1430   (autoload 'gnus-uu-decode-binhex "gnus-uu")
1431   (autoload 'gnus-uu-decode-uu-view "gnus-uu")
1432   (autoload 'gnus-uu-decode-uu-and-save-view "gnus-uu")
1433   (autoload 'gnus-uu-decode-unshar-view "gnus-uu")
1434   (autoload 'gnus-uu-decode-unshar-and-save-view "gnus-uu")
1435   (autoload 'gnus-uu-decode-save-view "gnus-uu")
1436   (autoload 'gnus-uu-decode-binhex-view "gnus-uu")
1437
1438   (autoload 'gnus-kill "gnus-kill")
1439   (autoload 'gnus-apply-kill-file-internal "gnus-kill")
1440   (autoload 'gnus-kill-file-edit-file "gnus-kill")
1441   (autoload 'gnus-kill-file-raise-followups-to-author "gnus-kill")
1442   (autoload 'gnus-execute "gnus-kill")
1443
1444   (autoload 'pp "pp")
1445   (autoload 'pp-to-string "pp")
1446   (autoload 'mail-extract-address-components "mail-extr")
1447   )
1448
1449 (put 'gnus-group-mode 'mode-class 'special)
1450 (put 'gnus-summary-mode 'mode-class 'special)
1451 (put 'gnus-article-mode 'mode-class 'special)
1452
1453 \f
1454
1455 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1456 (defun gnus-summary-position-cursor () nil)
1457 (defun gnus-group-position-cursor () nil)
1458 (fset 'gnus-summary-position-cursor 'gnus-goto-colon)
1459 (fset 'gnus-group-position-cursor 'gnus-goto-colon)
1460
1461 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
1462   "Pop to BUFFER, evaluate FORMS, and then returns to original window."
1463   (` (let ((GnusStartBufferWindow (selected-window)))
1464        (unwind-protect
1465            (progn
1466              (pop-to-buffer (, buffer))
1467              (,@ forms))
1468          (select-window GnusStartBufferWindow)))))
1469
1470 (defun gnus-make-hashtable (&optional hashsize)
1471   "Make a hash table (default and minimum size is 255).
1472 Optional argument HASHSIZE specifies the table size."
1473   (make-vector (if hashsize 
1474                    (max (gnus-create-hash-size hashsize) 255)
1475                  255) 0))
1476
1477 (defmacro gnus-gethash (string hashtable)
1478   "Get hash value of STRING in HASHTABLE."
1479   ;;(` (symbol-value (abbrev-symbol (, string) (, hashtable))))
1480   ;;(` (abbrev-expansion (, string) (, hashtable)))
1481   (` (symbol-value (intern-soft (, string) (, hashtable)))))
1482
1483 (defmacro gnus-sethash (string value hashtable)
1484   "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
1485   ;; We cannot use define-abbrev since it only accepts string as value.
1486                                         ;  (set (intern string hashtable) value))
1487   (` (set (intern (, string) (, hashtable)) (, value))))
1488
1489 (defsubst gnus-buffer-substring (beg end)
1490   (buffer-substring (match-beginning beg) (match-end end)))
1491
1492 (defsubst gnus-simplify-subject-re (subject)
1493   "Remove \"Re:\" from subject lines."
1494   (let ((case-fold-search t))
1495     (if (string-match "^re: *" subject)
1496         (substring subject (match-end 0))
1497       subject)))
1498
1499 (defsubst gnus-goto-char (point)
1500   (and point (goto-char point)))
1501
1502 \f
1503 ;;;
1504 ;;; Gnus Utility Functions
1505 ;;;
1506
1507 (defun gnus-extract-address-components (from)
1508   (let (name address)
1509     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
1510         (setq address (substring from (match-beginning 0) (match-end 0))))
1511     (and address
1512          (string-match (concat "<" (regexp-quote address) ">") from)
1513          (setq name (substring from 0 (1- (match-beginning 0)))))
1514     (or name
1515         (and (string-match "(.+)" from)
1516              (setq name (substring from (1+ (match-beginning 0)) 
1517                                    (1- (match-end 0))))))
1518     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1519     (list (or name from) (or address from))))
1520
1521 (defun gnus-fetch-field (field)
1522   "Return the value of the header FIELD of current article."
1523   (save-excursion
1524     (save-restriction
1525       (gnus-narrow-to-headers)
1526       (mail-fetch-field field))))
1527
1528 (defun gnus-goto-colon ()
1529   (beginning-of-line)
1530   (search-forward ":" (save-excursion (end-of-line) (point)) t))
1531
1532 (defun gnus-narrow-to-headers ()
1533   (widen)
1534   (save-excursion
1535     (goto-char 1)
1536     (if (search-forward "\n\n")
1537         (narrow-to-region 1 (1- (point))))))
1538
1539 ;; Get a number that is suitable for hashing; bigger than MIN
1540 (defun gnus-create-hash-size (min)
1541   (let ((i 1))
1542     (while (< i min)
1543       (setq i (* 2 i)))
1544     (1- i)))
1545
1546 (defun gnus-update-format-specifications ()
1547   (setq gnus-summary-line-format-spec 
1548         (gnus-parse-format
1549          gnus-summary-line-format gnus-summary-line-format-alist))
1550   (gnus-update-summary-mark-positions)
1551   (setq gnus-summary-dummy-line-format-spec 
1552         (gnus-parse-format gnus-summary-dummy-line-format 
1553                            gnus-summary-dummy-line-format-alist))
1554   (setq gnus-group-line-format-spec
1555         (gnus-parse-format 
1556          gnus-group-line-format 
1557          gnus-group-line-format-alist))
1558   (if (and (string-match "%D" gnus-group-line-format)
1559            (not gnus-description-hashtb)
1560            gnus-read-active-file)
1561       (gnus-read-descriptions-file))
1562   (setq gnus-summary-mode-line-format-spec 
1563         (gnus-parse-format gnus-summary-mode-line-format 
1564                            gnus-summary-mode-line-format-alist))
1565   (setq gnus-article-mode-line-format-spec 
1566         (gnus-parse-format gnus-article-mode-line-format 
1567                            gnus-summary-mode-line-format-alist))
1568   (setq gnus-group-mode-line-format-spec 
1569         (gnus-parse-format gnus-group-mode-line-format 
1570                            gnus-group-mode-line-format-alist)))
1571
1572 (defun gnus-update-summary-mark-positions ()
1573   (save-excursion
1574     (let ((gnus-replied-mark 129)
1575           (gnus-score-below-mark 130)
1576           (gnus-score-over-mark 130)
1577           pos)
1578       (set-buffer (get-buffer-create " *gnus work*"))
1579       (buffer-disable-undo (current-buffer))
1580       (erase-buffer)
1581       (gnus-summary-insert-line 
1582        nil [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
1583       (goto-char (point-min))
1584       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
1585                                          (- (point) 2)))))
1586       (goto-char (point-min))
1587       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
1588                                           (- (point) 2))) pos))
1589       (goto-char (point-min))
1590       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
1591                                         (- (point) 2))) pos))
1592       (setq gnus-summary-mark-positions pos)
1593       (kill-buffer (current-buffer)))))
1594
1595 (defun gnus-format-max-width (var length)
1596   (let (result)
1597     (if (> (length (setq result (eval var))) length)
1598         (format "%s" (substring result 0 length))
1599       (format "%s" result))))
1600
1601 (defun gnus-set-mouse-face (string)
1602   ;; Set mouse face property on STRING.
1603   (put-text-property 0 (length string) 'mouse-face gnus-mouse-face string)
1604   string)
1605
1606 (defun gnus-parse-format (format spec-alist)
1607   ;; This function parses the FORMAT string with the help of the
1608   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1609   ;; string.  If the FORMAT string contains the specifiers %( and %)
1610   ;; the text between them will have the mouse-face text property.
1611   (if (string-match "\\`\\(.*\\)%(\\(.*\\)%)\\(.*\n?\\)\\'" format)
1612       (if (and gnus-visual gnus-mouse-face)
1613           (let ((pre (substring format (match-beginning 1) (match-end 1)))
1614                 (button (substring format (match-beginning 2) (match-end 2)))
1615                 (post (substring format (match-beginning 3) (match-end 3))))
1616             (list 'concat
1617                   (gnus-parse-simple-format pre spec-alist)
1618                   (list 'gnus-set-mouse-face
1619                         (gnus-parse-simple-format button spec-alist))
1620                   (gnus-parse-simple-format post spec-alist)))
1621         (gnus-parse-simple-format
1622          (concat (substring format (match-beginning 1) (match-end 1))
1623                  (substring format (match-beginning 2) (match-end 2))
1624                  (substring format (match-beginning 3) (match-end 3)))
1625          spec-alist))
1626     (gnus-parse-simple-format format spec-alist)))
1627
1628 (defun gnus-parse-simple-format (format spec-alist)
1629   ;; This function parses the FORMAT string with the help of the
1630   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1631   ;; string. The list will consist of the symbol `format', a format
1632   ;; specification string, and a list of forms depending on the
1633   ;; SPEC-ALIST.
1634   (let ((max-width 0)
1635         spec flist fstring b newspec max-width elem beg)
1636     (save-excursion
1637       (set-buffer (get-buffer-create " *gnus work*"))
1638       (buffer-disable-undo (current-buffer))
1639       (gnus-add-current-to-buffer-list)
1640       (erase-buffer)
1641       (insert format)
1642       (goto-char 1)
1643       (while (re-search-forward "%[-0-9]*\\(,[0-9]*\\)*\\(.\\)\\(.\\)?" nil t)
1644         (setq spec (string-to-char (buffer-substring (match-beginning 2)
1645                                                      (match-end 2))))
1646         ;; First check if there are any specs that look anything like
1647         ;; "%12,12A", ie. with a "max width specification". These have
1648         ;; to be treated specially.
1649         (if (setq beg (match-beginning 1))
1650             (setq max-width 
1651                   (string-to-int 
1652                    (buffer-substring (1+ (match-beginning 1)) (match-end 1))))
1653           (setq max-width 0)
1654           (setq beg (match-beginning 2)))
1655         ;; Find the specification from `spec-alist'.
1656         (if (not (setq elem (cdr (assq spec spec-alist))))
1657             (setq elem '("*" ?s)))
1658         ;; Treat user defined format specifiers specially
1659         (and (eq (car elem) 'user-defined)
1660              (setq elem
1661                    (list 
1662                     (list (intern (concat "gnus-user-format-function-"
1663                                           (buffer-substring
1664                                            (match-beginning 3)
1665                                            (match-end 3))))
1666                           'header)
1667                     ?s))
1668              (delete-region (match-beginning 3) (match-end 3)))
1669         (if (not (zerop max-width))
1670             (let ((el (car elem)))
1671               (cond ((= (car (cdr elem)) ?c) 
1672                      (setq el (list 'char-to-string el)))
1673                     ((= (car (cdr elem)) ?d)
1674                      (numberp el) (setq el (list 'int-to-string el))))
1675               (setq flist (cons (list 'gnus-format-max-width 
1676                                       el max-width) 
1677                                 flist))
1678               (setq newspec ?s))
1679           (setq flist (cons (car elem) flist))
1680           (setq newspec (car (cdr elem))))
1681         ;; Remove the old specification (and possibly a ",12" string).
1682         (delete-region beg (match-end 2))
1683         ;; Insert the new specification.
1684         (goto-char beg)
1685         (insert newspec))
1686       (setq fstring (buffer-substring 1 (point-max))))
1687     (cons 'format (cons fstring (nreverse flist)))))
1688
1689 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
1690 (defun gnus-read-init-file ()
1691   (and gnus-init-file
1692        (or (file-exists-p gnus-init-file)
1693            (file-exists-p (concat gnus-init-file ".el"))
1694            (file-exists-p (concat gnus-init-file ".elc")))
1695        (load gnus-init-file nil t)))
1696
1697 ;; Article file names when saving.
1698
1699 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
1700   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1701 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
1702 Otherwise, it is like ~/News/news/group/num."
1703   (let ((default
1704           (expand-file-name
1705            (concat (if gnus-use-long-file-name
1706                        (gnus-capitalize-newsgroup newsgroup)
1707                      (gnus-newsgroup-directory-form newsgroup))
1708                    "/" (int-to-string (header-number headers)))
1709            (or gnus-article-save-directory "~/News"))))
1710     (if (and last-file
1711              (string-equal (file-name-directory default)
1712                            (file-name-directory last-file))
1713              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1714         default
1715       (or last-file default))))
1716
1717 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
1718   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1719 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group/num.
1720 Otherwise, it is like ~/News/news/group/num."
1721   (let ((default
1722           (expand-file-name
1723            (concat (if gnus-use-long-file-name
1724                        newsgroup
1725                      (gnus-newsgroup-directory-form newsgroup))
1726                    "/" (int-to-string (header-number headers)))
1727            (or gnus-article-save-directory "~/News"))))
1728     (if (and last-file
1729              (string-equal (file-name-directory default)
1730                            (file-name-directory last-file))
1731              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1732         default
1733       (or last-file default))))
1734
1735 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
1736   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1737 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group.
1738 Otherwise, it is like ~/News/news/group/news."
1739   (or last-file
1740       (expand-file-name
1741        (if gnus-use-long-file-name
1742            (gnus-capitalize-newsgroup newsgroup)
1743          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1744        (or gnus-article-save-directory "~/News"))))
1745
1746 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
1747   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1748 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group.
1749 Otherwise, it is like ~/News/news/group/news."
1750   (or last-file
1751       (expand-file-name
1752        (if gnus-use-long-file-name
1753            newsgroup
1754          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1755        (or gnus-article-save-directory "~/News"))))
1756
1757 ;; For subscribing new newsgroup
1758
1759 (defun gnus-subscribe-hierarchical-interactive (groups)
1760   (let ((groups (sort groups 'string<))
1761         prefixes prefix start rest ans group starts)
1762     (while groups
1763       (setq prefixes (list "^"))
1764       (while (and groups prefixes)
1765         (while (not (string-match (car prefixes) (car groups)))
1766           (setq prefixes (cdr prefixes)))
1767         (setq prefix (car prefixes))
1768         (setq start (1- (length prefix)))
1769         (if (and (string-match "[^\\.]\\." (car groups) start)
1770                  (cdr groups)
1771                  (setq prefix 
1772                        (concat "^" (substring (car groups) 0 (match-end 0))))
1773                  (string-match prefix (car (cdr groups))))
1774             (progn
1775               (setq prefixes (cons prefix prefixes))
1776               (message "Descend hierarchy %s? ([y]nsq): " 
1777                        (substring prefix 1 (1- (length prefix))))
1778               (setq ans (read-char))
1779               (cond ((= ans ?n)
1780                      (while (and groups 
1781                                  (string-match prefix 
1782                                                (setq group (car groups))))
1783                        (setq gnus-killed-list 
1784                              (cons group gnus-killed-list))
1785                        (gnus-sethash group group gnus-killed-hashtb)
1786                        (setq groups (cdr groups)))
1787                      (setq starts (cdr starts)))
1788                     ((= ans ?s)
1789                      (while (and groups 
1790                                  (string-match prefix 
1791                                                (setq group (car groups))))
1792                        (gnus-sethash group group gnus-killed-hashtb)
1793                        (gnus-subscribe-alphabetically (car groups))
1794                        (setq groups (cdr groups)))
1795                      (setq starts (cdr starts)))
1796                     ((= ans ?q)
1797                      (while groups
1798                        (setq group (car groups))
1799                        (setq gnus-killed-list (cons group gnus-killed-list))
1800                        (gnus-sethash group group gnus-killed-hashtb)
1801                        (setq groups (cdr groups))))
1802                     (t nil)))
1803           (message "Subscribe %s? ([n]yq)" (car groups))
1804           (setq ans (read-char))
1805           (setq group (car groups))
1806           (cond ((= ans ?y)
1807                  (gnus-subscribe-alphabetically (car groups))
1808                  (gnus-sethash group group gnus-killed-hashtb))
1809                 ((= ans ?q)
1810                  (while groups
1811                    (setq group (car groups))
1812                    (setq gnus-killed-list (cons group gnus-killed-list))
1813                    (gnus-sethash group group gnus-killed-hashtb)
1814                    (setq groups (cdr groups))))
1815                 (t 
1816                  (setq gnus-killed-list (cons group gnus-killed-list))
1817                  (gnus-sethash group group gnus-killed-hashtb)))
1818           (setq groups (cdr groups)))))))
1819
1820 (defun gnus-subscribe-randomly (newsgroup)
1821   "Subscribe new NEWSGROUP by making it the first newsgroup."
1822   (gnus-subscribe-newsgroup newsgroup))
1823
1824 (defun gnus-subscribe-alphabetically (newgroup)
1825   "Subscribe new NEWSGROUP and insert it in alphabetical order."
1826   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
1827   (let ((groups (cdr gnus-newsrc-assoc))
1828         before)
1829     (while (and (not before) groups)
1830       (if (string< newgroup (car (car groups)))
1831           (setq before (car (car groups)))
1832         (setq groups (cdr groups))))
1833     (gnus-subscribe-newsgroup newgroup before)))
1834
1835 (defun gnus-subscribe-hierarchically (newgroup)
1836   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
1837   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
1838   (save-excursion
1839     (set-buffer (find-file-noselect gnus-current-startup-file))
1840     (let ((groupkey newgroup)
1841           before)
1842       (while (and (not before) groupkey)
1843         (goto-char (point-min))
1844         (let ((groupkey-re
1845                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
1846           (while (and (re-search-forward groupkey-re nil t)
1847                       (progn
1848                         (setq before (buffer-substring
1849                                       (match-beginning 1) (match-end 1)))
1850                         (string< before newgroup)))))
1851         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
1852         (setq groupkey
1853               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
1854                   (substring groupkey (match-beginning 1) (match-end 1)))))
1855       (gnus-subscribe-newsgroup newgroup before))))
1856
1857 (defun gnus-subscribe-interactively (newsgroup)
1858   "Subscribe new NEWSGROUP interactively.
1859 It is inserted in hierarchical newsgroup order if subscribed. If not,
1860 it is killed."
1861   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " newsgroup))
1862       (gnus-subscribe-hierarchically newsgroup)
1863     (setq gnus-killed-list (cons newsgroup gnus-killed-list))))
1864
1865 (defun gnus-subscribe-zombies (newsgroup)
1866   "Make new NEWSGROUP a zombie group."
1867   (setq gnus-zombie-list (cons newsgroup gnus-zombie-list)))
1868
1869 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
1870   "Subscribe new NEWSGROUP.
1871 If NEXT is non-nil, it is inserted before NEXT. Otherwise it is made
1872 the first newsgroup."
1873   ;; We subscribe the group by changing its level to 3.
1874   (gnus-group-change-level 
1875    newsgroup 3 9 (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
1876   (message "Subscribe newsgroup: %s" newsgroup))
1877
1878 ;; For directories
1879
1880 (defun gnus-newsgroup-directory-form (newsgroup)
1881   "Make hierarchical directory name from NEWSGROUP name."
1882   (let ((newsgroup (substring newsgroup 0)) ;Copy string.
1883         (len (length newsgroup))
1884         (idx 0))
1885     ;; Replace all occurrences of `.' with `/'.
1886     (while (< idx len)
1887       (if (= (aref newsgroup idx) ?.)
1888           (aset newsgroup idx ?/))
1889       (setq idx (1+ idx)))
1890     newsgroup
1891     ))
1892
1893 (defun gnus-make-directory (dir)
1894   "Make DIRECTORY recursively."
1895   (let* ((dir (expand-file-name dir default-directory))
1896          dirs)
1897     (if (string-match "/$" dir)
1898         (setq dir (substring dir 0 (match-beginning 0))))
1899     (while (not (file-exists-p dir))
1900       (setq dirs (cons dir dirs))
1901       (string-match "/[^/]+$" dir)
1902       (setq dir (substring dir 0 (match-beginning 0))))
1903     (while dirs
1904       (make-directory (car dirs))
1905       (setq dirs (cdr dirs)))))
1906
1907 (defun gnus-capitalize-newsgroup (newsgroup)
1908   "Capitalize NEWSGROUP name."
1909   (and (not (zerop (length newsgroup)))
1910        (concat (char-to-string (upcase (aref newsgroup 0)))
1911                (substring newsgroup 1))))
1912
1913 ;; Var
1914
1915 (defun gnus-simplify-subject (subject &optional re-only)
1916   "Remove `Re:' and words in parentheses.
1917 If optional argument RE-ONLY is non-nil, strip `Re:' only."
1918   (let ((case-fold-search t))           ;Ignore case.
1919     ;; Remove `Re:' and `Re^N:'.
1920     (if (string-match "^re:[ \t]*" subject)
1921         (setq subject (substring subject (match-end 0))))
1922     ;; Remove words in parentheses from end.
1923     (or re-only
1924         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1925           (setq subject (substring subject 0 (match-beginning 0)))))
1926     ;; Return subject string.
1927     subject
1928     ))
1929
1930 (defun gnus-add-current-to-buffer-list ()
1931   (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list)))
1932
1933 ;; Functions accessing headers.
1934 ;; Functions are more convenient than macros in some cases.
1935
1936 (defun gnus-header-number (header)
1937   "Return article number in HEADER."
1938   (header-number header))
1939
1940 (defun gnus-header-subject (header)
1941   "Return subject string in HEADER."
1942   (header-subject header))
1943
1944 (defun gnus-header-from (header)
1945   "Return author string in HEADER."
1946   (header-from header))
1947
1948 (defun gnus-header-xref (header)
1949   "Return xref string in HEADER."
1950   (header-xref header))
1951
1952 (defun gnus-header-lines (header)
1953   "Return lines in HEADER."
1954   (header-lines header))
1955
1956 (defun gnus-header-date (header)
1957   "Return date in HEADER."
1958   (header-date header))
1959
1960 (defun gnus-header-id (header)
1961   "Return Id in HEADER."
1962   (header-id header))
1963
1964 (defun gnus-header-references (header)
1965   "Return references in HEADER."
1966   (header-references header))
1967
1968 (defun gnus-clear-system ()
1969   "Clear all variables and buffers."
1970   ;; Clear Gnus variables.
1971   (let ((variables gnus-variable-list))
1972     (while variables
1973       (set (car variables) nil)
1974       (setq variables (cdr variables))))
1975   ;; Clear other internal variables.
1976   (setq gnus-list-of-killed-groups nil
1977         gnus-have-read-active-file nil
1978         gnus-newsrc-assoc nil
1979         gnus-newsrc-hashtb nil
1980         gnus-killed-list nil
1981         gnus-zombie-list nil
1982         gnus-killed-hashtb nil
1983         gnus-active-hashtb nil
1984         gnus-moderated-list nil
1985         gnus-description-hashtb nil
1986         gnus-newsgroup-headers nil
1987         gnus-score-cache nil
1988         gnus-newsgroup-headers-hashtb-by-number nil
1989         gnus-newsgroup-name nil
1990         gnus-internal-global-score-files nil
1991         gnus-current-select-method nil)
1992   ;; Kill the startup file.
1993   (and gnus-current-startup-file
1994        (get-file-buffer gnus-current-startup-file)
1995        (kill-buffer (get-file-buffer gnus-current-startup-file)))
1996   (gnus-dribble-clear)
1997   ;; Kill global KILL file buffer.
1998   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
1999       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
2000   ;; Kill Gnus buffers.
2001   (while gnus-buffer-list
2002     (if (and (get-buffer (car gnus-buffer-list))
2003              (buffer-name (get-buffer (car gnus-buffer-list))))
2004         (kill-buffer (car gnus-buffer-list)))
2005     (setq gnus-buffer-list (cdr gnus-buffer-list))))
2006
2007 (defun gnus-configure-windows (action &optional force)
2008   "Configure Gnus windows according to the next ACTION.
2009 The ACTION is either a symbol, such as `summary', or a
2010 configuration list such as `(1 1 2)'.  If ACTION is not a list,
2011 configuration list is got from the variable gnus-window-configuration.
2012 If FORCE is non-nil, the updating will be done whether it is necessary
2013 or not."
2014   (let* ((windows
2015           (if (listp action) action 
2016             (if (listp gnus-window-configuration)
2017                 (car (cdr (assq action gnus-window-configuration)))
2018               gnus-window-configuration)))
2019          (grpwin (get-buffer-window gnus-group-buffer))
2020          (subwin (get-buffer-window gnus-summary-buffer))
2021          (artwin (get-buffer-window gnus-article-buffer))
2022          (winsum nil)
2023          (height nil)
2024          (grpheight 0)
2025          (subheight 0)
2026          (artheight 0)
2027
2028          ;; Make split-window-vertically leave focus in upper window.
2029          (split-window-keep-point t))
2030     (if (and (symbolp windows) (fboundp windows))
2031         (funcall windows action)
2032       (if (and (not force)
2033                (or (null windows)               ;No configuration is specified.
2034                    (and (eq (null grpwin)
2035                             (zerop (nth 0 windows)))
2036                         (eq (null subwin)
2037                             (zerop (nth 1 windows)))
2038                         (eq (null artwin)
2039                             (zerop (nth 2 windows))))))
2040           ;; No need to change window configuration.
2041           nil
2042         (select-window (or grpwin subwin artwin (selected-window)))
2043         ;; First of all, compute the height of each window.
2044         (cond (gnus-use-full-window
2045                ;; Take up the entire screen.
2046                (delete-other-windows)
2047                (setq height (window-height (selected-window))))
2048               (t
2049                (setq height (+ (if grpwin (window-height grpwin) 0)
2050                                (if subwin (window-height subwin) 0)
2051                                (if artwin (window-height artwin) 0)))))
2052         ;; The group buffer exits always. So, use it to extend the
2053         ;; group window so as to get enough window space.
2054         (switch-to-buffer gnus-group-buffer 'norecord)
2055         (and (get-buffer gnus-summary-buffer)
2056              (delete-windows-on gnus-summary-buffer))
2057         (and (get-buffer gnus-article-buffer)
2058              (delete-windows-on gnus-article-buffer))
2059         ;; Compute expected window height.
2060         (setq winsum (apply (function +) windows))
2061         (if (not (zerop (nth 0 windows)))
2062             (setq grpheight (max window-min-height
2063                                  (/ (* height (nth 0 windows)) winsum))))
2064         (if (not (zerop (nth 1 windows)))
2065             (setq subheight (max window-min-height
2066                                  (/ (* height (nth 1 windows)) winsum))))
2067         (if (not (zerop (nth 2 windows)))
2068             (setq artheight (max window-min-height
2069                                  (/ (* height (nth 2 windows)) winsum))))
2070         (setq height (+ grpheight subheight artheight))
2071         (enlarge-window (max 0 (- height (window-height (selected-window)))))
2072         ;; Then split the window.
2073         (and (not (zerop artheight))
2074              (or (not (zerop grpheight))
2075                  (not (zerop subheight)))
2076              (split-window-vertically (+ grpheight subheight)))
2077         (and (not (zerop grpheight))
2078              (not (zerop subheight))
2079              (split-window-vertically grpheight))
2080         ;; Then select buffers in each window.
2081         (or (zerop grpheight)
2082             (progn
2083               (switch-to-buffer gnus-group-buffer 'norecord)
2084               (other-window 1)))
2085         (or (zerop subheight)
2086             (progn
2087               (switch-to-buffer gnus-summary-buffer 'norecord)
2088               (other-window 1)))
2089         (or (zerop artheight)
2090             (progn
2091               ;; If article buffer does not exist, it will be created
2092               ;; and initialized.
2093               (gnus-article-setup-buffer)
2094               (switch-to-buffer gnus-article-buffer 'norecord)
2095               (setq buffer-read-only t) ; !!! Why!?! 
2096               (bury-buffer gnus-summary-buffer)
2097               (bury-buffer gnus-group-buffer)))
2098         (or (zerop subheight)
2099             (progn
2100               (pop-to-buffer gnus-summary-buffer)
2101               ;; It seems that some code in this function will set
2102               ;; buffer-read-only to nil. I have absolutely no idea
2103               ;; why. 
2104               (setq buffer-read-only t))))))) ; !!! Why!?! 
2105
2106 (defun gnus-window-configuration-split (action)
2107   (switch-to-buffer gnus-group-buffer t)
2108   (delete-other-windows)
2109   (split-window-horizontally)
2110   (cond ((or (eq action 'newsgoups) (eq action 'summary))
2111          (if (and (get-buffer gnus-summary-buffer)
2112                   (buffer-name gnus-summary-buffer))
2113              (switch-to-buffer-other-window gnus-summary-buffer)))
2114         ((eq action 'article)
2115          (switch-to-buffer gnus-summary-buffer t)
2116          (other-window 1)
2117          (gnus-article-setup-buffer)
2118          (switch-to-buffer gnus-article-buffer t))))
2119
2120 (defun gnus-version ()
2121   "Version numbers of this version of Gnus."
2122   (interactive)
2123   (let ((methods gnus-valid-select-methods)
2124         (mess gnus-version)
2125         meth)
2126     ;; Go through all the legal select methods and add their version
2127     ;; numbers to the total version string. Only the backends that are
2128     ;; currently in use will have their message numbers taken into
2129     ;; consideration. 
2130     (while methods
2131       (setq meth (intern (concat (car (car methods)) "-version")))
2132       (and (boundp meth)
2133            (stringp (symbol-value meth))
2134            (setq mess (concat mess "; " (symbol-value meth))))
2135       (setq methods (cdr methods)))
2136     (message mess)))
2137
2138 (defun gnus-info-find-node ()
2139   "Find Info documentation of Gnus."
2140   (interactive)
2141   ;; Enlarge info window if needed.
2142   (cond ((eq major-mode 'gnus-group-mode)
2143          (gnus-configure-windows '(1 0 0)) ;Take all windows.
2144          (pop-to-buffer gnus-group-buffer))
2145         ((eq major-mode 'gnus-summary-mode)
2146          (gnus-configure-windows '(0 1 0)) ;Take all windows.
2147          (pop-to-buffer gnus-summary-buffer)))
2148   (Info-goto-node (car (cdr (assq major-mode gnus-info-nodes)))))
2149
2150 (defun gnus-bug ()
2151   "Send a bug report to the Gnus maintainers."
2152   (interactive)
2153   (pop-to-buffer "*Gnus Bug*")
2154   (erase-buffer)
2155   (mail-setup gnus-maintainer "[Gnus Bug Report] " nil nil nil nil)
2156   (goto-char (point-min))
2157   (search-forward mail-header-separator)
2158   (forward-line 1)
2159   (insert (format "%s\n%s\n\n" (gnus-version) (emacs-version)))
2160   (gnus-debug)
2161   (mail-mode)
2162   (message ""))
2163
2164 (defun gnus-debug ()
2165   "Attemps to go through the Gnus source file and report what variables have been changed.
2166 The source file has to be in the Emacs load path."
2167   (interactive)
2168   (let ((dirs load-path)
2169         file expr olist)
2170     (while dirs
2171       (if (file-exists-p (setq file (concat (car dirs) "/gnus.el")))
2172           (save-excursion
2173             (setq dirs nil)
2174             (set-buffer (get-buffer-create "*gnus bug info*"))
2175             (buffer-disable-undo (current-buffer))
2176             (erase-buffer)
2177             (insert-file-contents file)
2178             (goto-char (point-min))
2179             (or (search-forward "\n;; Internal variables" nil t)
2180                 (error "Malformed sources"))
2181             (narrow-to-region (point-min) (point))
2182             (goto-char (point-min))
2183             (while (setq expr (condition-case () 
2184                                   (read (current-buffer)) (error nil)))
2185               (and (eq (car expr) 'defvar)
2186                    (stringp (nth 3 expr))
2187                    (not (equal (eval (nth 2 expr))
2188                                (and (boundp (nth 1 expr))
2189                                     (symbol-value (nth 1 expr)))))
2190                    (setq olist (cons (nth 1 expr) olist))))
2191             (kill-buffer (current-buffer)))
2192         (setq dirs (cdr dirs))))
2193     (while olist
2194       (insert "(setq " (symbol-name (car olist)) " '" 
2195               (prin1-to-string (symbol-value (car olist))) ")\n")
2196       (setq olist (cdr olist)))
2197     (insert "\n\n")))
2198
2199 (defun gnus-overload-functions (&optional overloads)
2200   "Overload functions specified by optional argument OVERLOADS.
2201 If nothing is specified, use the variable gnus-overload-functions."
2202   (let ((defs nil)
2203         (overloads (or overloads gnus-overload-functions)))
2204     (while overloads
2205       (setq defs (car overloads))
2206       (setq overloads (cdr overloads))
2207       ;; Load file before overloading function if necessary.  Make
2208       ;; sure we cannot use `require' always.
2209       (and (not (fboundp (car defs)))
2210            (car (cdr (cdr defs)))
2211            (load (car (cdr (cdr defs))) nil 'nomessage))
2212       (fset (car defs) (car (cdr defs))))))
2213
2214 (defun gnus-replace-chars-in-string (string from to)
2215   "Replace characters in STRING from FROM to TO."
2216   (let ((string (substring string 0))   ;Copy string.
2217         (len (length string))
2218         (idx 0))
2219     ;; Replace all occurrences of FROM with TO.
2220     (while (< idx len)
2221       (if (= (aref string idx) from)
2222           (aset string idx to))
2223       (setq idx (1+ idx)))
2224     string))
2225
2226 (defun gnus-days-between (date1 date2)
2227   ;; Return the number of days between date1 and date2.
2228   (let ((d1 (mapcar (lambda (s) (and s (string-to-int s)) )
2229                     (timezone-parse-date date1)))
2230         (d2 (mapcar (lambda (s) (and s (string-to-int s)) )
2231                     (timezone-parse-date date2))))
2232     (- (timezone-absolute-from-gregorian 
2233         (nth 1 d1) (nth 2 d1) (car d1))
2234        (timezone-absolute-from-gregorian 
2235         (nth 1 d2) (nth 2 d2) (car d2)))))
2236
2237 (defun gnus-day-number (date)
2238   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
2239                      (timezone-parse-date date))))
2240     (timezone-absolute-from-gregorian 
2241      (nth 1 dat) (nth 2 dat) (car dat))))
2242
2243 (defun gnus-file-newer-than (file date)
2244   (let ((fdate (nth 5 (file-attributes file))))
2245     (or (> (car fdate) (car date))
2246         (and (= (car fdate) (car date))
2247              (> (nth 1 fdate) (nth 1 date))))))
2248
2249 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
2250 ;; the echo area.
2251 (defun gnus-y-or-n-p (prompt)
2252   (prog1
2253       (y-or-n-p prompt)
2254     (message "")))
2255
2256 (defun gnus-yes-or-no-p (prompt)
2257   (prog1
2258       (yes-or-no-p prompt)
2259     (message "")))
2260
2261 ;; Return a string of length POS+1 representing NUMber in BASE. The
2262 ;; resulting string will be left padded with zeds.
2263 (defun gnus-number-base-x (num pos base)
2264   (if (< pos 0)
2265       ""
2266     (concat 
2267      (char-to-string
2268       (aref "zyxwvutsrqponmlkjihgfedcba9876543210" (/ num (expt base pos))))
2269      (gnus-number-base-x 
2270       (% num (expt base pos)) (1- pos) base))))
2271
2272 ;; List and range functions
2273
2274 (defun gnus-last-element (list)
2275   "Return last element of LIST."
2276   (while (cdr list)
2277     (setq list (cdr list)))
2278   (car list))
2279
2280 (defun gnus-copy-sequence (list)
2281   "Do a complete, total copy of a list."
2282   (mapcar (lambda (elem) (if (consp elem) 
2283                              (if (consp (cdr elem))
2284                                  (gnus-copy-sequence elem)
2285                                (cons (car elem) (cdr elem)))
2286                            elem))
2287           list))
2288
2289 (defun gnus-set-difference (list1 list2)
2290   "Return a list of elements of LIST1 that do not appear in LIST2."
2291   (let ((list1 (copy-sequence list1)))
2292     (while list2
2293       (setq list1 (delq (car list2) list1))
2294       (setq list2 (cdr list2)))
2295     list1))
2296
2297 (defun gnus-sorted-complement (list1 list2)
2298   "Return a list of elements of LIST1 that do not appear in LIST2.
2299 Both lists have to be sorted over <."
2300   (let (out)
2301     (while (and list1 list2)
2302       (cond ((= (car list1) (car list2))
2303              (setq list1 (cdr list1)
2304                    list2 (cdr list2)))
2305             ((< (car list1) (car list2))
2306              (setq out (cons (car list1) out))
2307              (setq list1 (cdr list1)))
2308             (t
2309              (setq out (cons (car list2) out))
2310              (setq list2 (cdr list2)))))
2311     (append (or list1 list2) out)))
2312
2313 (defun gnus-intersection (list1 list2)      
2314   (let ((result nil))
2315     (while list2
2316       (if (memq (car list2) list1)
2317           (setq result (cons (car list2) result)))
2318       (setq list2 (cdr list2)))
2319     result))
2320
2321 (defun gnus-sorted-intersection (list1 list2)
2322   ;; LIST1 and LIST2 have to be sorted over <.
2323   (let (out)
2324     (while (and list1 list2)
2325       (cond ((= (car list1) (car list2))
2326              (setq out (cons (car list1) out)
2327                    list1 (cdr list1)
2328                    list2 (cdr list2)))
2329             ((< (car list1) (car list2))
2330              (setq list1 (cdr list1)))
2331             (t
2332              (setq list2 (cdr list2)))))
2333     out))
2334
2335 (defun gnus-set-sorted-intersection (list1 list2)
2336   ;; LIST1 and LIST2 have to be sorted over <.
2337   ;; This function modifies LIST1.
2338   (let* ((top (cons nil list1))
2339          (prev top))
2340   (while (and list1 list2)
2341     (cond ((= (car list1) (car list2))
2342            (setq prev list1
2343                  list1 (cdr list1)
2344                  list2 (cdr list2)))
2345           ((< (car list1) (car list2))
2346            (setcdr prev (cdr list1))
2347            (setq list1 (cdr list1)))
2348           (t
2349            (setq list2 (cdr list2)))))
2350   (setcdr prev nil)
2351   (cdr top)))
2352
2353 (defun gnus-compress-sequence (numbers &optional always-list)
2354   "Convert list of numbers to a list of ranges or a single range.
2355 If ALWAYS-LIST is non-nil, this function will always release a list of
2356 ranges."
2357   (let* ((first (car numbers))
2358          (last (car numbers))
2359          result)
2360     (if (null numbers)
2361         nil
2362       (if (not (listp (cdr numbers)))
2363           numbers
2364         (while numbers
2365           (cond ((= last (car numbers)) nil) ;Omit duplicated number
2366                 ((= (1+ last) (car numbers)) ;Still in sequence
2367                  (setq last (car numbers)))
2368                 (t                      ;End of one sequence
2369                  (setq result 
2370                        (cons (if (= first last) first (cons first last)) result))
2371                  (setq first (car numbers))
2372                  (setq last  (car numbers))))
2373           (setq numbers (cdr numbers)))
2374         (if (and (not always-list) (null result))
2375             (if (= first last) first (cons first last))
2376           (nreverse (cons (if (= first last) first (cons first last))
2377                           result)))))))
2378
2379 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
2380 (defun gnus-uncompress-range (ranges)
2381   "Expand a list of ranges into a list of numbers.
2382 RANGES is either a single range on the form `(num . num)' or a list of
2383 these ranges."
2384   (let (first last result)
2385     (cond 
2386      ((null ranges)
2387       nil)
2388      ((not (listp (cdr ranges)))
2389       (setq first (car ranges))
2390       (setq last (cdr ranges))
2391       (while (<= first last)
2392         (setq result (cons first result))
2393         (setq first (1+ first)))
2394       (nreverse result))
2395      (t
2396       (while ranges
2397         (if (atom (car ranges))
2398             (if (numberp (car ranges))
2399                 (setq result (cons (car ranges) result)))
2400           (setq first (car (car ranges)))
2401           (setq last  (cdr (car ranges)))
2402           (while (<= first last)
2403             (setq result (cons first result))
2404             (setq first (1+ first))))
2405         (setq ranges (cdr ranges)))
2406       (nreverse result)))))
2407
2408 (defun gnus-add-to-range (ranges list)
2409   "Return a list of ranges that has all articles from both RANGES and LIST.
2410 Note: LIST has to be sorted over `<'."
2411   (if (not ranges)
2412       (gnus-compress-sequence list t)
2413     (setq list (copy-sequence list))
2414     (or (listp (cdr ranges))
2415         (setq ranges (list ranges)))
2416     (let ((out ranges)
2417           ilist lowest highest temp)
2418       (while (and ranges list)
2419         (setq ilist list)
2420         (setq lowest (or (and (atom (car ranges)) (car ranges))
2421                          (car (car ranges))))
2422         (while (and list (cdr list) (< (car (cdr list)) lowest))
2423           (setq list (cdr list)))
2424         (if (< (car ilist) lowest)
2425             (progn
2426               (setq temp list)
2427               (setq list (cdr list))
2428               (setcdr temp nil)
2429               (setq out (nconc (gnus-compress-sequence ilist t) out))))
2430         (setq highest (or (and (atom (car ranges)) (car ranges))
2431                           (cdr (car ranges))))
2432         (while (and list (<= (car list) highest))
2433           (setq list (cdr list)))
2434         (setq ranges (cdr ranges)))
2435       (if list
2436           (setq out (nconc (gnus-compress-sequence list t) out)))
2437       (setq out (sort out (lambda (r1 r2) 
2438                             (< (or (and (atom r1) r1) (car r1))
2439                                (or (and (atom r2) r2) (car r2))))))
2440       (setq ranges out)
2441       (while ranges
2442         (if (atom (car ranges))
2443             (if (cdr ranges)
2444                 (if (atom (car (cdr ranges)))
2445                     (if (= (1+ (car ranges)) (car (cdr ranges)))
2446                         (progn
2447                           (setcar ranges (cons (car ranges) 
2448                                                (car (cdr ranges))))
2449                           (setcdr ranges (cdr (cdr ranges)))))
2450                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
2451                       (progn
2452                         (setcar (car (cdr ranges)) (car ranges))
2453                         (setcar ranges (car (cdr ranges)))
2454                         (setcdr ranges (cdr (cdr ranges)))))))
2455           (if (cdr ranges)
2456               (if (atom (car (cdr ranges)))
2457                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
2458                       (progn
2459                         (setcdr (car ranges) (car (cdr ranges)))
2460                         (setcdr ranges (cdr (cdr ranges)))))
2461                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
2462                     (progn
2463                       (setcdr (car ranges) (cdr (car (cdr ranges))))
2464                       (setcdr ranges (cdr (cdr ranges))))))))
2465         (setq ranges (cdr ranges)))
2466       out)))
2467
2468 (defun gnus-add-to-range-new (ranges list)
2469   (gnus-compress-sequence 
2470    (sort (nconc (gnus-uncompress-range ranges) list) '<)))     
2471
2472 (defun gnus-add-to-range-old (ranges list)
2473   "Return a list of ranges that has all articles from both RANGES and LIST.
2474 Note: LIST has to be sorted over `<'."
2475   (let* ((ranges (if (and ranges (atom (car ranges))) (list ranges) ranges))
2476          (inrange ranges)
2477          did-one
2478          range nranges first last)
2479     (if (not list)
2480         ranges
2481       (if (not ranges)
2482           (gnus-compress-sequence list t)
2483         (and ranges 
2484              (> (car (car ranges)) 1)
2485              (progn
2486                (setq did-one t)
2487                (setq inrange (setq ranges (cons (cons 1 1) ranges)))))
2488         (while (and ranges list)
2489           (setq range (car ranges))
2490           (while (and list (>= (car list) (car range))
2491                       (<= (car list) (cdr range)))
2492             (setq list (cdr list)))
2493           (while (and list (= (1- (car list)) (cdr range)))
2494             (setcdr range (car list))
2495             (setq list (cdr list)))
2496           (if (and list (and (> (car list) (cdr range)) 
2497                              (cdr ranges)
2498                              (< (car list) (car (car (cdr ranges))))))
2499               (setcdr ranges (cons (cons (car list) (car list)) (cdr ranges))))
2500           (setq ranges (cdr ranges)))
2501         (if (and list (not ranges))
2502             (setq inrange (nconc inrange (gnus-compress-sequence list t))))
2503         (if did-one
2504             (if (eq (cdr (car inrange)) 1)
2505                 (setq inrange (cdr inrange))
2506               (setcar (car inrange) 2)))
2507         (setq ranges inrange)
2508         (while ranges
2509           (if (and (cdr ranges) (>= (1+ (cdr (car ranges)))
2510                                     (car (car (cdr ranges)))))
2511               (progn
2512                 (setcdr (car ranges) (cdr (car (cdr ranges))))
2513                 (setcdr ranges (cdr (cdr ranges))))
2514             (setq ranges (cdr ranges))))
2515         (if (not (cdr inrange))
2516             (car inrange)
2517           inrange)))))
2518
2519 (defun gnus-remove-from-range (ranges list)
2520   "Return a list of ranges that has all articles from LIST removed from RANGES.
2521 Note: LIST has to be sorted over `<'."
2522   ;; !!! This function shouldn't look like this, but I've got a headache.
2523   (gnus-compress-sequence 
2524    (gnus-sorted-complement
2525     (gnus-uncompress-range ranges) list)))
2526
2527 (defun gnus-member-of-range (number ranges)
2528   (if (not (listp (cdr ranges)))
2529       (and (>= number (car ranges)) 
2530            (<= number (cdr ranges)))
2531     (let ((not-stop t))
2532       (while (and ranges 
2533                   (if (numberp (car ranges))
2534                       (>= number (car ranges))
2535                     (>= number (car (car ranges))))
2536                   not-stop)
2537         (if (if (numberp (car ranges))
2538                 (= number (car ranges))
2539               (and (>= number (car (car ranges)))
2540                    (<= number (cdr (car ranges)))))
2541             (setq not-stop nil))
2542         (setq ranges (cdr ranges)))
2543       (not not-stop))))
2544
2545 \f
2546 ;;;
2547 ;;; Gnus group mode
2548 ;;;
2549
2550 (defvar gnus-group-mode-map nil)
2551 (defvar gnus-group-make-map nil)
2552 (defvar gnus-group-list-map nil)
2553
2554 (if gnus-group-mode-map
2555     nil
2556   (setq gnus-group-mode-map (make-keymap))
2557   (suppress-keymap gnus-group-mode-map)
2558   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
2559   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
2560   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
2561   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
2562   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
2563   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
2564   (define-key gnus-group-mode-map "\177" 'gnus-group-prev-unread-group)
2565   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
2566   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
2567   (define-key gnus-group-mode-map "\M-n" 'gnus-group-next-unread-group-same-level)
2568   (define-key gnus-group-mode-map "\M-p" 'gnus-group-prev-unread-group-same-level)
2569   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
2570   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
2571   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
2572   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
2573   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
2574   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
2575   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
2576   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
2577   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
2578   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
2579   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
2580   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
2581   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
2582   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
2583   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
2584   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
2585   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
2586   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
2587   (define-key gnus-group-mode-map "\C-c\M-C-a" 'gnus-group-description-apropos)
2588   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
2589   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
2590   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
2591   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
2592   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
2593   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
2594   (define-key gnus-group-mode-map "\M-z" 'gnus-group-kill-all-zombies)
2595   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
2596   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
2597   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
2598   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
2599   (define-key gnus-group-mode-map "V" 'gnus-version)
2600   (define-key gnus-group-mode-map "S" 'gnus-group-set-current-level)
2601   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
2602   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
2603   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
2604   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
2605   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
2606   (define-key gnus-group-mode-map "\M-f" 'gnus-group-fetch-faq)
2607   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
2608   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
2609   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group)
2610   (define-key gnus-group-mode-map [mouse-2] 'gnus-mouse-pick-group)
2611
2612   (define-prefix-command 'gnus-group-make-map)
2613   (define-key gnus-group-mode-map "M" 'gnus-group-make-map)
2614   (define-key gnus-group-make-map "d" 'gnus-group-make-directory-group)
2615   (define-key gnus-group-make-map "h" 'gnus-group-make-help-group)
2616   (define-key gnus-group-make-map "k" 'gnus-group-make-kiboze-group)
2617   (define-key gnus-group-make-map "m" 'gnus-group-make-group)
2618   (define-key gnus-group-make-map "e" 'gnus-group-edit-group)
2619
2620   (define-prefix-command 'gnus-group-list-map)
2621   (define-key gnus-group-mode-map "G" 'gnus-group-list-map)
2622   (define-key gnus-group-list-map "k" 'gnus-group-list-killed)
2623   (define-key gnus-group-list-map "z" 'gnus-group-list-zombies)
2624   (define-key gnus-group-list-map "s" 'gnus-group-list-groups)
2625   (define-key gnus-group-list-map "u" 'gnus-group-list-all-groups)
2626   (define-key gnus-group-list-map "a" 'gnus-group-apropos)
2627   (define-key gnus-group-list-map "d" 'gnus-group-description-apropos)
2628   (define-key gnus-group-list-map "m" 'gnus-group-list-matching)
2629   (define-key gnus-group-list-map "M" 'gnus-group-list-all-matching)
2630   )
2631
2632 (defun gnus-group-mode ()
2633   "Major mode for reading news.
2634
2635 All normal editing commands are switched off.
2636 \\<gnus-group-mode-map>
2637 The group buffer lists (some of) the groups available.  For instance,
2638 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
2639 lists all zombie groups. 
2640
2641 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe 
2642 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'. 
2643
2644 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
2645
2646 The following commands are available:
2647
2648 \\{gnus-group-mode-map}"
2649   (interactive)
2650   (if gnus-visual (gnus-group-make-menu-bar))
2651   (kill-all-local-variables)
2652   (setq mode-line-modified "-- ")
2653   (make-local-variable 'mode-line-format)
2654   (setq mode-line-format (copy-sequence mode-line-format))
2655   (and (equal (nth 3 mode-line-format) "   ")
2656        (setcar (nthcdr 3 mode-line-format) ""))
2657   (setq major-mode 'gnus-group-mode)
2658   (setq mode-name "Group")
2659   (gnus-group-set-mode-line)
2660   (setq mode-line-process nil)
2661   (use-local-map gnus-group-mode-map)
2662   (buffer-disable-undo (current-buffer))
2663   (setq truncate-lines t)
2664   (setq buffer-read-only t)
2665   (run-hooks 'gnus-group-mode-hook))
2666
2667 (defun gnus-mouse-pick-group (e)
2668   (interactive "e")
2669   (mouse-set-point e)
2670   (gnus-group-read-group nil))
2671
2672 ;;;###autoload
2673 (defun gnus-no-server (&optional arg)
2674   "Read network news.
2675 If ARG is a positive number, Gnus will use that as the
2676 startup level. If ARG is nil, Gnus will be started at level 2. 
2677 If ARG is non-nil and not a positive number, Gnus will
2678 prompt the user for the name of an NNTP server to use.
2679 As opposed to `gnus', this command will not connect to the local server."
2680   (interactive "P")
2681   (gnus (or arg 2) t))
2682
2683 (defalias '\(ding\) 'gnus)
2684
2685 ;;;###autoload
2686 (defun gnus (&optional arg dont-connect)
2687   "Read network news.
2688 If ARG is non-nil and a positive number, Gnus will use that as the
2689 startup level. If ARG is non-nil and not a positive number, Gnus will
2690 prompt the user for the name of an NNTP server to use."
2691   (interactive "P")
2692   (if (get-buffer gnus-group-buffer)
2693       (progn
2694         (switch-to-buffer gnus-group-buffer)
2695         (gnus-group-get-new-news))
2696     (gnus-clear-system)
2697     (gnus-read-init-file)
2698     (let ((level (and arg (numberp arg) (> arg 0) arg)))
2699       (unwind-protect
2700           (progn
2701             (switch-to-buffer (get-buffer-create gnus-group-buffer))
2702             (gnus-add-current-to-buffer-list)
2703             (gnus-group-mode)
2704             (or dont-connect (gnus-start-news-server (and arg (not level)))))
2705         (if (and (not dont-connect) 
2706                  (not (gnus-server-opened gnus-select-method)))
2707             (gnus-group-quit)
2708           (run-hooks 'gnus-startup-hook)
2709           ;; NNTP server is successfully open. 
2710           (gnus-update-format-specifications)
2711           (let ((buffer-read-only nil))
2712             (erase-buffer)
2713             (if (not gnus-inhibit-startup-message)
2714                 (progn
2715                   (gnus-group-startup-message)
2716                   (sit-for 0))))
2717           (gnus-setup-news nil level)
2718           (and gnus-use-dribble-file (gnus-dribble-open))
2719           (or t (not gnus-novice-user)
2720               gnus-expert-user
2721               (gnus-group-describe-briefly)) ;Show brief help message.
2722           (gnus-group-list-groups (or level 5)))))))
2723
2724 (defun gnus-group-startup-message (&optional x y)
2725   "Insert startup message in current buffer."
2726   ;; Insert the message.
2727   (erase-buffer)
2728   (insert
2729    (format "
2730     %s
2731            A newsreader 
2732       for GNU Emacs
2733
2734         Based on GNUS 
2735              written by 
2736      Masanobu UMEDA
2737
2738     Lars Magne 
2739          Ingebrigtsen 
2740       larsi@ifi.uio.no
2741
2742            gnus-version))
2743   ;; And then hack it.
2744   ;; 18 is the longest line.
2745   (indent-rigidly (point-min) (point-max) 
2746                   (/ (max (- (window-width) (or x 28)) 0) 2))
2747   (goto-char (point-min))
2748   ;; +4 is fuzzy factor.
2749   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2)))
2750
2751 (defun gnus-group-setup-buffer ()
2752   (or (get-buffer gnus-group-buffer)
2753       (progn
2754         (switch-to-buffer (get-buffer-create gnus-group-buffer))
2755         (gnus-add-current-to-buffer-list)
2756         (gnus-group-mode))))
2757
2758 (defun gnus-group-list-groups (level &optional unread)
2759   "List newsgroups with level LEVEL or lower that have unread alticles.
2760 Default is 5, which lists all subscribed groups.
2761 If argument UNREAD is non-nil, groups with no unread articles are also listed."
2762   (interactive "P")
2763   (setq level (or level 5))
2764   (gnus-group-setup-buffer)     ;May call from out of group buffer
2765   (let ((case-fold-search nil)
2766         (group (gnus-group-group-name)))
2767     (funcall gnus-group-prepare-function level unread nil)
2768     (if (zerop (buffer-size))
2769         ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
2770         (message "No news is horrible news")
2771       (goto-char (point-min))
2772       (if (not group)
2773           ;; Go to the first group with unread articles.
2774           (gnus-group-search-forward nil nil nil t)
2775         ;; Find the right group to put point on. If the current group
2776         ;; has disapeared in the new listing, try to find the next
2777         ;; one. If no next one can be found, just leave point at the
2778         ;; first newsgroup in the buffer.
2779         (if (not (gnus-goto-char
2780                   (text-property-any (point-min) (point-max) 
2781                                      'gnus-group (intern group))))
2782             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
2783               (while (and newsrc
2784                           (not (gnus-goto-char 
2785                                 (text-property-any 
2786                                  (point-min) (point-max) 'gnus-group 
2787                                  (intern group)))))
2788                 (setq newsrc (cdr newsrc))))))
2789       ;; Adjust cursor point.
2790       (gnus-group-position-cursor))))
2791
2792 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
2793   "List all newsgroups with unread articles of level LEVEL or lower.
2794 If ALL is non-nil, list groups that have no unread articles.
2795 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
2796 If REGEXP, only list groups matching REGEXP."
2797   (set-buffer gnus-group-buffer)
2798   (let ((buffer-read-only nil)
2799         (newsrc (cdr gnus-newsrc-assoc))
2800         (lowest (or lowest 1))
2801         info clevel unread group)
2802     (erase-buffer)
2803     (if (< lowest 8)
2804         ;; List living groups.
2805         (while newsrc
2806           (setq info (car newsrc)
2807                 group (car info)
2808                 newsrc (cdr newsrc)
2809                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
2810           (and unread ; This group might be bogus
2811                (or (not regexp)
2812                    (string-match regexp group))
2813                (<= (setq clevel (car (cdr info))) level) 
2814                (>= clevel lowest)
2815                (or all            ; We list all groups?
2816                    (eq unread t)  ; We list unactivated groups
2817                    (> unread 0)   ; We list groups with unread articles
2818                    (cdr (assq 'tick (nth 3 info)))) ; And groups with tickeds
2819                (gnus-group-insert-group-line 
2820                 nil group (car (cdr info)) (nth 3 info) unread (nth 4 info)))))
2821
2822     ;; List dead groups.
2823     (and (>= level 8) (<= lowest 8)
2824          (gnus-group-prepare-flat-list-dead 
2825           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 8 ?Z
2826           regexp))
2827     (and (>= level 9) (<= lowest 9)
2828          (gnus-group-prepare-flat-list-dead 
2829           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 9 ?K
2830           regexp))
2831
2832     (gnus-group-set-mode-line)
2833     (setq gnus-have-all-newsgroups all)
2834     (run-hooks 'gnus-group-prepare-hook)))
2835
2836 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
2837   ;; List zombies and killed lists somehwat faster, which was
2838   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>. It does
2839   ;; this by ignoring the group format specification altogether.
2840   (let (group beg)
2841     (while groups
2842       (setq group (car groups)
2843             groups (cdr groups))
2844       (if (or (not regexp)
2845               (string-match regexp group))
2846           (progn
2847             (setq beg (point))
2848             (insert (format " %c    *: %s\n" mark group))
2849             (add-text-properties 
2850              beg (1+ beg) 
2851              (list 'gnus-group (intern group)
2852                    'gnus-unread t
2853                    'gnus-level level)))))))
2854
2855 (defun gnus-group-real-name (group)
2856   "Find the real name of a foreign newsgroup."
2857   (if (string-match "^[^:]+:" group)
2858       (substring group (match-end 0))
2859     group))
2860
2861 (defun gnus-group-prefixed-name (group method)
2862   "Return the whole name from GROUP and METHOD."
2863   (concat (format "%s" (car method))
2864           (if (assoc (format "%s" (car method)) (gnus-methods-using 'address))
2865               (concat "+" (nth 1 method)))
2866           ":" group))
2867
2868 (defun gnus-group-real-prefix (group)
2869   "Return the prefix of the current group name."
2870   (if (string-match "^[^:]+:" group)
2871       (substring group 0 (match-end 0))
2872     ""))
2873
2874 (defun gnus-group-method-name (group)
2875   "Return the method used for selecting GROUP."
2876   (let ((prefix (gnus-group-real-prefix group)))
2877     (if (equal prefix "")
2878         gnus-select-method
2879       (if (string-match "^[^\\+]+\\+" prefix)
2880           (list (intern (substring prefix 0 (1- (match-end 0))))
2881                 (substring prefix (match-end 0) (1- (length prefix))))
2882         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
2883
2884 (defun gnus-group-foreign-p (group)
2885   "Return nil if GROUP is native, non-nil if it is foreign."
2886   (string-match ":" group))
2887
2888 (defun gnus-group-set-info (info)
2889   (let ((entry (gnus-gethash (car info) gnus-newsrc-hashtb)))
2890     (let (marked)
2891       (if (not (setq marked (nth 3 info)))
2892           ()
2893         (while marked
2894           (or (eq 'score (car (car marked)))
2895               (eq 'bookmark (car (car marked)))
2896               (not (or (atom (cdr (cdr (car marked))))
2897                        (not (atom (car (cdr (car marked)))))))
2898               (setcdr (car marked) 
2899                       (gnus-uncompress-range (cdr (car marked)))))
2900           (setq marked (cdr marked)))))
2901     (if entry
2902         ()
2903       (save-excursion
2904         (set-buffer gnus-group-buffer)
2905         (if (nth 4 info)
2906             (gnus-group-make-group 
2907              (gnus-group-real-name (car info))
2908              (prin1-to-string (car (nth 4 info)))
2909              (nth 1 (nth 4 info)))
2910           (gnus-group-make-group
2911            (car info)
2912            (prin1-to-string (car gnus-select-method))
2913            (nth 1 gnus-select-method)))
2914         (message "Note: New group created")
2915         (setq entry 
2916               (gnus-gethash (gnus-group-prefixed-name 
2917                              (gnus-group-real-name (car info))
2918                              (or (nth 4 info) gnus-select-method))
2919                             gnus-newsrc-hashtb))))
2920     (if entry
2921         (progn
2922           (setcar (nthcdr 2 entry) info)
2923           (if (and (not (eq (car entry) t)) 
2924                    (gnus-gethash (car info) gnus-active-hashtb))
2925               (let ((marked (nth 3 info)))
2926                 (setcar entry 
2927                         (max 0 (- (length (gnus-list-of-unread-articles 
2928                                            (car info)))
2929                                   (length (cdr (assq 'tick marked)))
2930                                   (length (cdr (assq 'dormant marked)))))))))
2931       (error "No such group: %s" (car info)))))
2932
2933 (defun gnus-group-update-group-line ()
2934   "This function updates the current line in the newsgroup buffer and
2935 moves the point to the colon."
2936   (let* ((buffer-read-only nil)
2937          (group (gnus-group-group-name))
2938          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
2939     (if entry
2940         (gnus-dribble-enter 
2941          (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
2942                  ")")))
2943     (beginning-of-line)
2944     (delete-region (point) (save-excursion (forward-line 1) (point)))
2945     (gnus-group-insert-group-line-info group)
2946     (forward-line -1)
2947     (gnus-group-position-cursor)))
2948
2949 (defun gnus-group-insert-group-line-info (group)
2950   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
2951         active info)
2952     (if entry
2953         (progn
2954           (setq info (nth 2 entry))
2955           (gnus-group-insert-group-line 
2956            nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
2957       (setq active (gnus-gethash group gnus-active-hashtb))
2958       (gnus-group-insert-group-line 
2959        nil group (if (member group gnus-zombie-list) 8 9)
2960        nil (if active (- (1+ (cdr active)) (car active)) 0) nil))))
2961
2962 (defun gnus-group-insert-group-line (gformat group level marked number method)
2963   (let* ((gformat (or gformat gnus-group-line-format-spec))
2964          (active (gnus-gethash group gnus-active-hashtb))
2965          (number-total (if active (1+ (- (cdr active) (car active))) 0))
2966          (number-of-dormant (length (cdr (assq 'dormant marked))))
2967          (number-of-ticked (length (cdr (assq 'tick marked))))
2968          (number-of-ticked-and-dormant
2969           (+ number-of-ticked number-of-dormant))
2970          (number-of-unread-unticked 
2971           (if (numberp number) (int-to-string (max 0 number))
2972             "*"))
2973          (number-of-read
2974           (if (numberp number)
2975               (max 0 (- number-total number))
2976             "*"))
2977          (subscribed (cond ((< level 6) ? )
2978                            ((< level 8) ?U)
2979                            ((= level 8) ?Z)
2980                            (t ?K)))
2981          (qualified-group (gnus-group-real-name group))
2982          (newsgroup-description 
2983           (if gnus-description-hashtb
2984               (or (gnus-gethash group gnus-description-hashtb) "")
2985             ""))
2986          (moderated (if (member group gnus-moderated-list) ?m ? ))
2987          (moderated-string (if (eq moderated ?m) "(m)" ""))
2988          (news-server (or (car (cdr method)) ""))
2989          (news-method (or (car method) ""))
2990          (news-method-string 
2991           (if method (format "(%s:%s)" (car method) (car (cdr method))) ""))
2992          (marked (if (and 
2993                       (numberp number) 
2994                       (zerop number)
2995                       (> number-of-ticked 0))
2996                      ?* ? ))
2997          (number (if (eq number t) "*" (+ number number-of-dormant 
2998                                           number-of-ticked)))
2999          (buffer-read-only nil)
3000          b)
3001     (beginning-of-line)
3002     (setq b (point))
3003     ;; Insert the text.
3004     (insert (eval gformat))
3005
3006     (add-text-properties 
3007      b (1+ b) (list 'gnus-group (intern group)
3008                     'gnus-unread (if (numberp number)
3009                                      (string-to-int number-of-unread-unticked)
3010                                    t)
3011                     'gnus-marked marked
3012                     'gnus-level level))))
3013
3014 (defun gnus-group-update-group (group &optional visible-only)
3015   "Update newsgroup info of GROUP.
3016 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't already."
3017   (save-excursion
3018     (set-buffer gnus-group-buffer)
3019     (let ((buffer-read-only nil)
3020           visible)
3021       (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
3022         (if entry
3023             (gnus-dribble-enter 
3024              (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3025                      ")"))))
3026       ;; Buffer may be narrowed.
3027       (save-restriction
3028         (widen)
3029         ;; Search a line to modify.  If the buffer is large, the search
3030         ;; takes long time.  In most cases, current point is on the line
3031         ;; we are looking for.  So, first of all, check current line. 
3032         (if (or (progn
3033                   (beginning-of-line)
3034                   (eq (get-text-property (point) 'gnus-group)
3035                       (intern group)))
3036                 (progn
3037                   (gnus-goto-char 
3038                    (text-property-any 
3039                     (point-min) (point-max) 'gnus-group (intern group)))))
3040             ;; GROUP is listed in current buffer. So, delete old line.
3041             (progn
3042               (setq visible t)
3043               (beginning-of-line)
3044               (delete-region (point) (progn (forward-line 1) (point))))
3045           ;; No such line in the buffer, find out where it's supposed to
3046           ;; go, and insert it there (or at the end of the buffer).
3047           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
3048           (or visible-only
3049               (let ((entry (cdr (gnus-gethash group gnus-newsrc-hashtb))))
3050                 (while (and entry
3051                             (not
3052                              (gnus-goto-char
3053                               (text-property-any
3054                                (point-min) (point-max) 
3055                                'gnus-group (intern (car (car entry)))))))
3056                   (setq entry (cdr entry)))
3057                 (or entry (goto-char (point-max)))))))
3058       (if (or visible (not visible-only))
3059           (gnus-group-insert-group-line-info group))
3060       (gnus-group-set-mode-line))))
3061
3062 (defun gnus-group-set-mode-line ()
3063   (if (memq 'group gnus-updated-mode-lines)
3064       (let* ((gformat (or gnus-group-mode-line-format-spec
3065                           (setq gnus-group-mode-line-format-spec
3066                                 (gnus-parse-format 
3067                                  gnus-group-mode-line-format 
3068                                  gnus-group-mode-line-format-alist))))
3069              (news-server (car (cdr gnus-select-method)))
3070              (news-method (car gnus-select-method))
3071              (mode-string (eval gformat))
3072              (max-len 60))
3073         (if (> (length mode-string) max-len) 
3074             (setq mode-string (substring mode-string 0 (- max-len 4))))
3075         (setq mode-line-buffer-identification mode-string)
3076         (set-buffer-modified-p t))))
3077
3078 (defun gnus-group-group-name ()
3079   "Get the name of the newsgroup on the current line."
3080   (let ((group (get-text-property 
3081                 (save-excursion (beginning-of-line) (point)) 'gnus-group)))
3082     (and group (symbol-name group))))
3083
3084 (defun gnus-group-group-level ()
3085   "Get the level of the newsgroup on the current line."
3086   (get-text-property (save-excursion (beginning-of-line) (point)) 'gnus-level))
3087
3088 (defun gnus-group-search-forward (&optional backward all level first-too)
3089   "Find the next newsgroup with unread articles.
3090 If BACKWARD is non-nil, find the previous newsgroup instead.
3091 If ALL is non-nil, just find any newsgroup.
3092 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
3093 group exists.
3094 If FIRST-TOO, the current line is also eligeble as a target."
3095   (let ((way (if backward -1 1))
3096         (low 10)
3097         (beg (point))
3098         pos found)
3099     (or first-too (forward-line way))
3100     (while (and 
3101             (not (eobp))
3102             (not (setq 
3103                   found 
3104                   (and (or all
3105                            (and
3106                             (let ((unread 
3107                                    (get-text-property (point) 'gnus-unread)))
3108                               (or (eq unread t) (and unread (> unread 0))))
3109                             (< (get-text-property (point) 'gnus-level) 6)))
3110                        (or (not level)
3111                            (let ((lev (get-text-property (point) 'gnus-level)))
3112                              (if (<= lev level)
3113                                  t
3114                                (if (< lev low)
3115                                    (progn
3116                                      (setq low lev)
3117                                      (setq pos (point))))
3118                                nil))))))
3119             (zerop (forward-line way))))
3120     (if found 
3121         (progn (gnus-group-position-cursor) t)
3122       (if pos (goto-char pos) (goto-char beg))
3123       nil)))
3124
3125 ;; Gnus group mode commands
3126
3127 (defun gnus-group-read-group (all &optional no-article group)
3128   "Read news in this newsgroup.
3129 If argument ALL is non-nil, already read articles become readable.
3130 If optional argument NO-ARTICLE is non-nil, no article body is displayed."
3131   (interactive "P")
3132   (let ((group (or group (gnus-group-group-name)))
3133         number active marked entry)
3134     (or group (error "No group on current line"))
3135     (setq marked 
3136           (nth 3 (nth 2 (setq entry (gnus-gethash group gnus-newsrc-hashtb)))))
3137     ;; This group might be a dead group. In that case we have to get
3138     ;; the number of unread articles from `gnus-active-hashtb'.
3139     (if entry
3140         (setq number (car entry))
3141       (if (setq active (gnus-gethash group gnus-active-hashtb))
3142           (setq number (- (1+ (cdr active)) (car active)))))
3143     (gnus-summary-read-group 
3144      group (or all (and (numberp number) 
3145                         (zerop (+ number (length (cdr (assq 'tick marked)))
3146                                   (length (cdr (assq 'dormant marked)))))))
3147      no-article)))
3148
3149 (defun gnus-group-select-group (all)
3150   "Select this newsgroup.
3151 No article is selected automatically.
3152 If argument ALL is non-nil, already read articles become readable."
3153   (interactive "P")
3154   (gnus-group-read-group all t))
3155
3156 (defun gnus-group-jump-to-group (group)
3157   "Jump to newsgroup GROUP."
3158   (interactive
3159    (list 
3160     (completing-read "Group: " gnus-active-hashtb nil t)))
3161   (let (b)
3162     ;; Either go to the line in the group buffer...
3163     (or (and (setq b (text-property-any (point-min) (point-max) 
3164                                         'gnus-group (intern group)))
3165              (goto-char b))
3166         ;; ... or insert the line.
3167         (progn (gnus-group-update-group group)
3168                (goto-char (text-property-any (point-min) (point-max) 
3169                                              'gnus-group (intern group))))))
3170   ;; Adjust cursor point.
3171   (gnus-group-position-cursor))
3172
3173 (defun gnus-group-next-group (n)
3174   "Go to next N'th newsgroup.
3175 If N is negative, search backward instead.
3176 Returns the difference between N and the number of skips actually
3177 done."
3178   (interactive "p")
3179   (gnus-group-next-unread-group n t))
3180
3181 (defun gnus-group-next-unread-group (n &optional all level)
3182   "Go to next N'th unread newsgroup.
3183 If N is negative, search backward instead.
3184 If ALL is non-nil, choose any newsgroup, unread or not.
3185 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
3186 such group can be found, the next group with a level higher than
3187 LEVEL.
3188 Returns the difference between N and the number of skips actually
3189 made."
3190   (interactive "p")
3191   (let ((backward (< n 0))
3192         (n (abs n)))
3193     (while (and (> n 0)
3194                 (gnus-group-search-forward backward all level))
3195       (setq n (1- n)))
3196     (if (/= 0 n) (message "No more%s newsgroups%s" (if all "" " unread")
3197                           (if level " on this level or higher" "")))
3198     n))
3199
3200 (defun gnus-group-prev-group (n)
3201   "Go to previous N'th newsgroup.
3202 Returns the difference between N and the number of skips actually
3203 done."
3204   (interactive "p")
3205   (gnus-group-next-unread-group (- n) t))
3206
3207 (defun gnus-group-prev-unread-group (n)
3208   "Go to previous N'th unread newsgroup.
3209 Returns the difference between N and the number of skips actually
3210 done."  
3211   (interactive "p")
3212   (gnus-group-next-unread-group (- n)))
3213
3214 (defun gnus-group-next-unread-group-same-level (n)
3215   "Go to next N'th unread newsgroup on the same level.
3216 If N is negative, search backward instead.
3217 Returns the difference between N and the number of skips actually
3218 done."
3219   (interactive "p")
3220   (gnus-group-next-unread-group n t (gnus-group-group-level))
3221   (gnus-group-position-cursor))
3222
3223 (defun gnus-group-prev-unread-group-same-level (n)
3224   "Go to next N'th unread newsgroup on the same level.
3225 Returns the difference between N and the number of skips actually
3226 done."
3227   (interactive "p")
3228   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
3229   (gnus-group-position-cursor))
3230
3231 (defun gnus-group-best-unread-group ()
3232   "Go to the group with the highest level."
3233   (interactive)
3234   (goto-char (point-min))
3235   (let ((best 10)
3236         unread best-point)
3237     (while (setq unread (get-text-property (point) 'gnus-unread))
3238       (if (and (numberp unread) (> unread 0))
3239           (progn
3240             (or best-point (setq best-point (point)))
3241             (if (< (get-text-property (point) 'gnus-level) best)
3242                 (progn 
3243                   (setq best (get-text-property (point) 'gnus-level))
3244                   (setq best-point (point))))))
3245       (forward-line 1))
3246     (if best-point (goto-char best-point))
3247     (gnus-summary-position-cursor)
3248     (and best-point (gnus-group-group-name))))
3249
3250 (defun gnus-group-make-group (name method address)
3251   "Add a new newsgroup.
3252 The user will be prompted for a NAME, for a select METHOD, and an
3253 ADDRESS."
3254   (interactive
3255    (cons 
3256     (read-string "Group name: ")
3257     (let ((method
3258            (completing-read 
3259             "Method: " gnus-valid-select-methods nil t)))
3260       (list method
3261             (if (memq 'prompt-address
3262                       (assoc method gnus-valid-select-methods))
3263                 (read-string "Address: ")
3264               "")))))
3265   (let ((nname (gnus-group-prefixed-name name (list (intern method) address)))
3266         info)
3267     (gnus-group-change-level 
3268      (setq info (list t nname 3 nil nil (list (intern method) address)))
3269      3 9 (gnus-gethash (or (gnus-group-group-name) "dummy.group")
3270                        gnus-newsrc-hashtb) t)
3271     (gnus-sethash nname '(0 . 0) gnus-active-hashtb)
3272     (gnus-dribble-enter 
3273      (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")"))
3274     (gnus-group-insert-group-line-info nname)))
3275
3276 (defun gnus-group-edit-group (group)
3277   (interactive (list (gnus-group-group-name)))
3278   (let (info)
3279     (if group (setq info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
3280       (error "No group on current line"))
3281     (setq gnus-winconf-edit-group (current-window-configuration))
3282     (pop-to-buffer (get-buffer-create gnus-group-edit-buffer))
3283     (gnus-add-current-to-buffer-list)
3284     (emacs-lisp-mode)
3285     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3286     (use-local-map (copy-keymap emacs-lisp-mode-map))
3287     (local-set-key "\C-c\C-c" 'gnus-group-edit-group-done)
3288     (erase-buffer)
3289     (insert ";; Type `C-c C-c' after you have edited the newsgroup entry.\n\n")
3290     (let ((cinfo (gnus-copy-sequence info))
3291           marked)
3292       (if (not (setq marked (nth 3 cinfo)))
3293           ()
3294         (while marked
3295           (or (eq 'score (car (car marked)))
3296               (eq 'bookmark (car (car marked)))
3297               (setcdr (car marked) 
3298                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
3299           (setq marked (cdr marked))))
3300       (insert (pp-to-string (list 'gnus-group-set-info
3301                                   (list 'quote cinfo)))))))
3302
3303 (defun gnus-group-edit-group-done ()
3304   (interactive)
3305   (set-buffer (get-buffer-create gnus-group-edit-buffer))
3306   (eval-current-buffer)
3307   (kill-buffer (current-buffer))
3308   (and gnus-winconf-edit-group
3309        (set-window-configuration gnus-winconf-edit-group))
3310   (setq gnus-winconf-edit-group nil)
3311   (set-buffer gnus-group-buffer)
3312   (gnus-group-update-group (gnus-group-group-name))
3313   (gnus-group-position-cursor))
3314
3315 (defun gnus-group-make-help-group ()
3316   "Create the (ding) Gnus documentation group."
3317   (interactive)
3318   (and (gnus-gethash (gnus-group-prefixed-name "gnus-help" '(nndoc ""))
3319                      gnus-newsrc-hashtb)
3320        (error "Documentation group already exists"))
3321   (let ((path load-path))
3322     (while (and path
3323                 (not (file-exists-p (concat (file-name-as-directory (car path))
3324                                             "doc.txt"))))
3325       (setq path (cdr path)))
3326     (or path (error "Couldn't find doc group"))
3327     (gnus-group-make-group 
3328      "gnus-help" "nndoc" 
3329      (concat (file-name-as-directory (car path)) "doc.txt"))
3330     (gnus-group-position-cursor)))
3331
3332 (defun gnus-group-make-directory-group (dir)
3333   "Create an nndir group.
3334 The user will be prompted for a directory. The contents of this
3335 directory will be used as a newsgroup. The directory should contain
3336 mail messages or news articles in files that have numeric names."
3337   (interactive
3338    (list (read-file-name "Create group from directory: ")))
3339   (or (file-exists-p dir) (error "No such directory"))
3340   (or (file-directory-p dir) (error "Not a directory"))
3341   (gnus-group-make-group dir "nndir" dir)
3342   (gnus-group-position-cursor))
3343
3344 (defun gnus-group-make-kiboze-group (group address scores)
3345   "Create an nnkiboze group.
3346 The user will be prompted for a name, a regexp to match groups, and
3347 score file entries for articles to include in the group."
3348   (interactive
3349    (list
3350     (read-string "nnkiboze group name: ")
3351     (read-string "Source groups (regexp): ")
3352     (let ((headers (mapcar (lambda (group) (list group))
3353                            '("subject" "from" "number" "date" "message-id"
3354                              "references" "chars" "lines" "xref")))
3355           scores header regexp regexps)
3356       (while (not (equal "" (setq header (completing-read 
3357                                           "Match on header: " headers nil t))))
3358         (setq regexps nil)
3359         (while (not (equal "" (setq regexp (read-string 
3360                                             (format "Match on %s (string): "
3361                                                     header)))))
3362           (setq regexps (cons (list regexp nil 1000 nil) regexps)))
3363         (setq scores (cons (cons header regexps) scores)))
3364       (car scores))))
3365   (gnus-group-make-group group "nnkiboze" address)
3366   (save-excursion
3367     (set-buffer (get-buffer-create " *gnus work*"))
3368     (buffer-disable-undo (current-buffer))
3369     (let (emacs-lisp-mode-hook)
3370       (pp (list 'setq 'gnus-score-alist 
3371                 (list 'quote (list scores)))
3372           (current-buffer)))
3373     (write-region (point-min) (point-max) 
3374                   (concat (or gnus-kill-files-directory "~/News")
3375                           "nnkiboze:" group "." gnus-score-file-suffix))
3376     (kill-buffer (current-buffer)))
3377   (gnus-group-position-cursor))
3378
3379 ;; Group sorting commands
3380 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
3381
3382 (defun gnus-group-sort-groups ()
3383   "Sort the group buffer using `gnus-group-sort-function'."
3384   (interactive)
3385   (setq gnus-newsrc-assoc 
3386         (sort (cdr gnus-newsrc-assoc) gnus-group-sort-function))
3387   (gnus-make-hashtable-from-newsrc-alist)
3388   (gnus-get-unread-articles 6)
3389   (gnus-group-list-groups 5))
3390
3391 (defun gnus-group-sort-by-alphabet (info1 info2)
3392   (string< (car info1) (car info2)))
3393
3394 (defun gnus-group-sort-by-unread (info1 info2)
3395   (let ((n1 (car (gnus-gethash (car info1) gnus-newsrc-hashtb)))
3396         (n2 (car (gnus-gethash (car info2) gnus-newsrc-hashtb))))
3397     (< (or (and (numberp n1) n1) 0)
3398        (or (and (numberp n2) n2) 0))))
3399
3400 (defun gnus-group-sort-by-level (info1 info2)
3401   (< (nth 1 info1) (nth 1 info2)))
3402
3403 ;; Group catching up.
3404
3405 (defun gnus-group-catchup-current (n &optional all)
3406   "Mark all articles not marked as unread in current newsgroup as read.
3407 If prefix argument N is numeric, the ARG next newsgroups will be
3408 caught up. If ALL is non-nil, marked articles will also be marked as
3409 read. Cross references (Xref: header) of articles are ignored.
3410 The difference between N and actual number of newsgroups that were
3411 caught up is returned."
3412   (interactive "p")
3413   (if (or (not gnus-interactive-catchup) ;Without confirmation?
3414           gnus-expert-user
3415           (gnus-y-or-n-p
3416            (if all
3417                "Do you really want to mark all articles as read? "
3418              "Mark all unread articles as read? ")))
3419       (progn
3420         (while 
3421             (and (> n 0)
3422                  (progn
3423                    (setq n (1- n))
3424                    (gnus-group-catchup (gnus-group-group-name) all)
3425                    (gnus-group-update-group-line)
3426                    t)
3427                  (zerop (gnus-group-next-unread-group 1))))))
3428   n)
3429
3430 (defun gnus-group-catchup-current-all (n)
3431   "Mark all articles in current newsgroup as read.
3432 Cross references (Xref: header) of articles are ignored."
3433   (interactive "p")
3434   (gnus-group-catchup-current n 'all))
3435
3436 (defun gnus-group-catchup (group &optional all)
3437   "Mark all articles in GROUP as read.
3438 If ALL is non-nil, all articles are marked as read.
3439 The return value is the number of articles that were marked as read,
3440 or nil if no action could be taken."
3441   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
3442          (num (car entry))
3443          (marked (nth 3 (nth 2 entry)))
3444          ticked)
3445     (if (not (numberp (car entry)))
3446         (message "Can't catch up; non-active group")
3447       ;; Do the updating only if the newsgroup isn't killed
3448       (if entry
3449           (progn
3450             (setq ticked (if all nil (cdr (assq 'tick marked))))
3451             (gnus-update-read-articles group ticked nil ticked)
3452             (if (and all marked)
3453                 (setcar (nthcdr 3 (nth 2 entry)) 
3454                         (delq (assq 'dormant marked) marked))))))
3455     num))
3456
3457 (defun gnus-group-expire-articles (newsgroup)
3458   "Expire all expirable articles in the current newsgroup."
3459   (interactive (list (gnus-group-group-name)))
3460   (if (not newsgroup) (error "No current newsgroup"))
3461   (let ((expirable 
3462          (assq 'expire (nth 3 (nth 2 (gnus-gethash newsgroup 
3463                                                    gnus-newsrc-hashtb))))))
3464  (and expirable 
3465       (gnus-check-backend-function 'request-expire-articles newsgroup)
3466       (setcdr expirable
3467               (gnus-request-expire-articles (cdr expirable) newsgroup)))))
3468
3469 (defun gnus-group-expire-all-groups ()
3470   "Expire all expirable articles in all newsgroups."
3471   (interactive)
3472   (message "Expiring...")
3473   (let ((newsrc (cdr gnus-newsrc-assoc)))
3474     (while newsrc
3475       (gnus-group-expire-articles (car (car newsrc)))
3476       (setq newsrc (cdr newsrc))))
3477   (message "Expiring...done"))
3478
3479 (defun gnus-group-set-current-level (n)
3480   "Set the level of the current group to the numeric prefix."
3481   (interactive "P")
3482   (setq n (or n (string-to-int 
3483                  (completing-read 
3484                   "Level: " 
3485                   (mapcar (lambda (n) (list (char-to-string n))) "123456789")
3486                   nil t))))
3487   (let ((group (gnus-group-group-name)))
3488     (if (not group) (error "No newsgroup on current line.")
3489     (if (and (numberp n) (>= n 1) (<= n 9))
3490         (progn
3491           (message "Changed level of %s from %d to %d" 
3492                    group (gnus-group-group-level) n)
3493           (gnus-group-change-level group n (gnus-group-group-level))
3494           (gnus-group-update-group-line))
3495       (error "Illegal level: %s" n))))
3496   (forward-line 1)
3497   (gnus-group-position-cursor))
3498
3499 (defun gnus-group-unsubscribe-current-group (arg)
3500   "Toggle subscribe from/to unsubscribe current group."
3501   (interactive "P")
3502   (let ((group (gnus-group-group-name)))
3503     (or group (error "No newsgroup on current line"))
3504     (or arg (setq arg (if (<= (gnus-group-group-level) 5) 6 3)))
3505     (gnus-group-unsubscribe-group group arg)
3506     (gnus-group-next-group 1)))
3507
3508 (defun gnus-group-unsubscribe-group (group &optional level)
3509   "Toggle subscribe from/to unsubscribe GROUP.
3510 New newsgroup is added to .newsrc automatically."
3511   (interactive
3512    (list (completing-read "Group: " gnus-active-hashtb nil 
3513                           gnus-have-read-active-file)))
3514   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
3515     (cond (newsrc
3516            ;; Toggle subscription flag.
3517            (gnus-group-change-level 
3518             newsrc (if level level (if (< (nth 1 (nth 2 newsrc)) 6) 6 4)))
3519            (gnus-group-update-group group))
3520           ((and (stringp group)
3521                 (or (not gnus-have-read-active-file)
3522                     (gnus-gethash group gnus-active-hashtb)))
3523            ;; Add new newsgroup.
3524            (gnus-group-change-level 
3525             group 
3526             (if level level 3) 
3527             (or (and (member group gnus-zombie-list) 8) 9)
3528             (or (and (gnus-group-group-name)
3529                      (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb))
3530                 (gnus-gethash (car (car gnus-newsrc-assoc)) 
3531                               gnus-newsrc-hashtb)))
3532            (gnus-group-update-group group))
3533           (t (error "No such newsgroup: %s" group)))
3534     (gnus-group-position-cursor)))
3535
3536 (defun gnus-group-transpose-groups (arg)
3537   "Exchange current newsgroup and previous newsgroup.
3538 With argument ARG, takes previous newsgroup and moves it past ARG newsgroup."
3539   (interactive "p")
3540   ;; BUG: last newsgroup and the last but one cannot be transposed
3541   ;; since gnus-group-search-forward does not move forward beyond the
3542   ;; last.  If we instead use forward-line, no problem, but I don't
3543   ;; want to use it for later extension.
3544   (while (> arg 0)
3545     (gnus-group-search-forward t t)
3546     (gnus-group-kill-group 1)
3547     (gnus-group-search-forward nil t)
3548     (gnus-group-yank-group)
3549     (gnus-group-search-forward nil t)
3550     (setq arg (1- arg))))
3551
3552 (defun gnus-group-kill-all-zombies ()
3553   "Kill all zombie newsgroups."
3554   (interactive)
3555   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
3556   (setq gnus-zombie-list nil)
3557   (funcall gnus-group-prepare-function 5 nil nil)
3558   (goto-char (point-min))
3559   (gnus-group-position-cursor))
3560
3561 (defun gnus-group-kill-region (begin end)
3562   "Kill newsgroups in current region (excluding current point).
3563 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
3564   (interactive "r")
3565   (let ((lines
3566          ;; Exclude a line where current point is on.
3567          (1-
3568           ;; Count lines.
3569           (save-excursion
3570             (count-lines
3571              (progn
3572                (goto-char begin)
3573                (beginning-of-line)
3574                (point))
3575              (progn
3576                (goto-char end)
3577                (end-of-line)
3578                (point)))))))
3579     (goto-char begin)
3580     (beginning-of-line)                 ;Important when LINES < 1
3581     (gnus-group-kill-group lines)))
3582
3583 (defun gnus-group-kill-group (n)
3584   "The the next N groups.
3585 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
3586 However, only groups that were alive can be yanked; already killed 
3587 groups or zombie groups can't be yanked.
3588 The return value is the name of the (last) newsgroup that was killed."
3589   (interactive "p")
3590   (let ((buffer-read-only nil)
3591         group entry level)
3592     (while (>= (setq n  (1- n)) 0)
3593       (setq group (gnus-group-group-name))
3594       (or group
3595           (signal 'end-of-buffer nil))
3596       (setq level (gnus-group-group-level))
3597       (beginning-of-line)
3598       (delete-region (point) (progn (forward-line 1) (point)))
3599       (if (setq entry (gnus-gethash group gnus-newsrc-hashtb))
3600           (setq gnus-list-of-killed-groups 
3601                 (cons (cons (car entry) (nth 2 entry)) 
3602                       gnus-list-of-killed-groups)))
3603       (gnus-group-change-level (if entry entry group) 9 (if entry nil level)))
3604     (if (eobp)
3605         (forward-line -1))
3606     (gnus-group-position-cursor)
3607     group))
3608
3609 (defun gnus-group-yank-group (&optional arg)
3610   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
3611 inserting it before the current newsgroup.  The numeric ARG specifies
3612 how many newsgroups are to be yanked.  The name of the (last)
3613 newsgroup yanked is returned."
3614   (interactive "p")
3615   (if (not arg) (setq arg 1))
3616   (let (info group prev)
3617     (while (>= (setq arg (1- arg)) 0)
3618       (if (not (setq info (car gnus-list-of-killed-groups)))
3619           (error "No more newsgroups to yank"))
3620       (setq group (nth 2 info))
3621       ;; Find which newsgroup to insert this one before - search
3622       ;; backward until something suitable is found. If there are no
3623       ;; other newsgroups in this buffer, just make this newsgroup the
3624       ;; first newsgroup.
3625       (while (and (not (setq prev (gnus-group-group-name)))
3626                   (zerop (forward-line -1))))
3627       (if (not prev)
3628           (setq prev (car (car gnus-newsrc-assoc))))
3629       (gnus-group-change-level 
3630        info (nth 2 info) 9 
3631        (gnus-gethash prev gnus-newsrc-hashtb)
3632        t)
3633       (gnus-group-insert-group-line-info (nth 1 info))
3634       (setq gnus-list-of-killed-groups 
3635             (cdr gnus-list-of-killed-groups)))
3636     (forward-line -1)
3637     (gnus-group-position-cursor)
3638     group))
3639       
3640 (defun gnus-group-list-all-groups (arg)
3641   "List all newsgroups with level ARG or lower.
3642 Default is 7, which lists all subscribed and most unsubscribed groups."
3643   (interactive "P")
3644   (setq arg (or arg 7))
3645   (gnus-group-list-groups arg t))
3646
3647 (defun gnus-group-list-killed ()
3648   "List all killed newsgroups in the group buffer."
3649   (interactive)
3650   (if (not gnus-killed-list)
3651       (message "No killed groups")
3652     (funcall gnus-group-prepare-function 9 t 9)
3653     (goto-char (point-min)))
3654   (gnus-group-position-cursor))
3655
3656 (defun gnus-group-list-zombies ()
3657   "List all zombie newsgroups in the group buffer."
3658   (interactive)
3659   (if (not gnus-zombie-list)
3660       (message "No zombie groups")
3661     (funcall gnus-group-prepare-function 8 t 8)
3662     (goto-char (point-min)))
3663   (gnus-group-position-cursor))
3664
3665 (defun gnus-group-get-new-news (&optional arg)
3666   "Get newly arrived articles.
3667 If ARG is non-nil, it should be a number between one and nine to
3668 specify which levels you are interested in re-scanning."
3669   (interactive "P")
3670   (run-hooks 'gnus-get-new-news-hook)
3671   (if (and gnus-read-active-file (not arg))
3672       (progn
3673         (gnus-read-active-file)
3674         (gnus-get-unread-articles (or arg 6)))
3675     (let ((gnus-read-active-file nil))
3676       (gnus-get-unread-articles (or arg 6))))
3677   (gnus-group-list-groups 5 gnus-have-all-newsgroups))
3678
3679 (defun gnus-group-get-new-news-this-group (n)
3680   "Check for newly arrived news in the current group (and the N-1 next groups).
3681 The difference between N and the number of newsgroup checked is returned.
3682 If N is negative, this group and the N-1 previous groups will be checked."
3683   (interactive "p")
3684   (let ((way (if (< n 0) -1 1))
3685         (n (abs n))
3686         (w-p (window-start))
3687         group)
3688     (while (and (> n 0)
3689                 (progn
3690                   (or (gnus-get-new-news-in-group
3691                        (setq group (gnus-group-group-name)))
3692                       (progn 
3693                         (ding) 
3694                         (message "%s error: %s" 
3695                                  group (gnus-status-message group))))
3696                   t)
3697                 (zerop (gnus-group-next-group way)))
3698       (setq n (1- n)))
3699     (if (/= 0 n) (message "No more newsgroups"))
3700     ;; !!! I don't know why the buffer scrolls forward when updating
3701     ;; the first line in the group buffer, but it does. So we set the
3702     ;; window start forcibly.
3703     (set-window-start (get-buffer-window (current-buffer)) w-p)
3704     n))
3705
3706 (defun gnus-get-new-news-in-group (group)
3707   (and group 
3708        (gnus-activate-newsgroup group)
3709        (progn
3710          (gnus-get-unread-articles-in-group 
3711           (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
3712           (gnus-gethash group gnus-active-hashtb))
3713          (gnus-group-update-group-line)
3714          t)))
3715
3716 (defun gnus-group-fetch-faq (group)
3717   "Fetch the FAQ for the current group."
3718   (interactive (list (gnus-group-group-name)))
3719   (or group (error "No group name given"))
3720   (let ((file (concat gnus-group-faq-directory group))) 
3721     (if (not (file-exists-p file))
3722         (error "No such file: %s" file)
3723       (find-file file))))
3724   
3725 (defun gnus-group-describe-group (force &optional group)
3726   "Display a description of the current newsgroup."
3727   (interactive "P")
3728   (and force (setq gnus-description-hashtb nil))
3729   (let ((group (or group (gnus-group-group-name)))
3730         desc)
3731     (or group (message "No group name given"))
3732     (and (or gnus-description-hashtb
3733              (setq desc (gnus-group-get-description group))
3734              (gnus-read-descriptions-file))
3735          (message
3736           (or desc (gnus-gethash group gnus-description-hashtb)
3737               "No description available")))))
3738
3739 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
3740 (defun gnus-group-describe-all-groups (force)
3741   "Pop up a buffer with descriptons of all newsgroups."
3742   (interactive "P")
3743   (and force (setq gnus-description-hashtb nil))
3744   (if (not (or gnus-description-hashtb
3745                (gnus-read-descriptions-file)))
3746       (error "Couldn't request descriptions file"))
3747   (let ((buffer-read-only nil)
3748         b)
3749     (erase-buffer)
3750     (mapatoms
3751      (lambda (group)
3752        (setq b (point))
3753        (insert (format "      *: %-20s %s\n" (symbol-name group)
3754                        (symbol-value group)))
3755        (add-text-properties 
3756         b (1+ b) (list 'gnus-group group
3757                        'gnus-unread t 'gnus-marked nil 'gnus-level 6)))
3758      gnus-description-hashtb)
3759     (goto-char (point-min))
3760     (gnus-group-position-cursor)))
3761
3762 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
3763 (defun gnus-group-apropos (regexp &optional search-description)
3764   "List all newsgroups that have names that match a regexp."
3765   (interactive "sGnus apropos (regexp): ")
3766   (let ((prev "")
3767         (obuf (current-buffer))
3768         groups des prev)
3769     ;; Go through all newsgroups that are known to Gnus.
3770     (mapatoms 
3771      (lambda (group)
3772        (and (string-match regexp (symbol-name group))
3773             (setq groups (cons (symbol-name group) groups))))
3774      gnus-active-hashtb)
3775     ;; Go through all descriptions that are known to Gnus. 
3776     (if search-description
3777         (mapatoms 
3778          (lambda (group)
3779            (and (string-match regexp (symbol-value group))
3780                 (gnus-gethash (symbol-name group) gnus-active-hashtb)
3781                 (setq groups (cons (symbol-name group) groups))))
3782          gnus-description-hashtb))
3783     (if (not groups)
3784         (message "No groups matched \"%s\"." regexp)
3785       ;; Print out all the groups.
3786       (save-excursion
3787         (pop-to-buffer (get-buffer-create "*Gnus Help*"))
3788         (buffer-disable-undo (current-buffer))
3789         (erase-buffer)
3790         (setq groups (sort groups 'string<))
3791         (while groups
3792           ;; Groups may be entered twice into the list of groups.
3793           (if (not (string= (car groups) prev))
3794               (progn
3795                 (insert (setq prev (car groups)) "\n")
3796                 (if (and gnus-description-hashtb
3797                          (setq des (gnus-gethash (car groups) 
3798                                                  gnus-description-hashtb)))
3799                     (insert "  " des "\n"))))
3800           (setq groups (cdr groups)))
3801         (goto-char 1)))
3802     (pop-to-buffer obuf)))
3803
3804 (defun gnus-group-description-apropos (regexp)
3805   "List all newsgroups that have names or desccriptions that match a regexp."
3806   (interactive "sGnus description apropos (regexp): ")
3807   (if (not (or gnus-description-hashtb
3808                (gnus-read-descriptions-file)))
3809       (error "Couldn't request descriptions file"))
3810   (gnus-group-apropos regexp t))
3811
3812 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
3813 (defun gnus-group-list-matching (level regexp &optional all lowest) 
3814   "List all groups with unread articles that match REGEXP.
3815 If the prefix LEVEL is non-nil, it should be a number that says which
3816 level to cut off listing groups. 
3817 If ALL, also list groups with no unread articles.
3818 If LOWEST, don't list groups with level lower than LOWEST."
3819   (interactive "P\nsList newsgroups matching: ")
3820   (gnus-group-prepare-flat (or level 5) all (or lowest 1) regexp)
3821   (goto-char (point-min))
3822   (gnus-group-position-cursor))
3823
3824 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
3825   "List all groups that match REGEXP.
3826 If the prefix LEVEL is non-nil, it should be a number that says which
3827 level to cut off listing groups. 
3828 If LOWEST, don't list groups with level lower than LOWEST."
3829   (interactive "P\nsList newsgroups matching: ")
3830   (gnus-group-list-matching (or level 9) regexp t lowest))
3831
3832 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
3833 (defun gnus-group-save-newsrc ()
3834   "Save the Gnus startup files."
3835   (interactive)
3836   (gnus-save-newsrc-file))
3837
3838 (defun gnus-group-restart (&optional arg)
3839   "Force Gnus to read the .newsrc file."
3840   (interactive "P")
3841   (gnus-save-newsrc-file)
3842   (gnus-setup-news 'force)
3843   (gnus-group-list-groups (or arg 5) gnus-have-all-newsgroups))
3844
3845 (defun gnus-group-read-init-file ()
3846   "Read the Gnus elisp init file."
3847   (interactive)
3848   (gnus-read-init-file))
3849
3850 (defun gnus-group-check-bogus-groups ()
3851   "Check bogus newsgroups."
3852   (interactive)
3853   (gnus-check-bogus-newsgroups (not gnus-expert-user))  ;Require confirmation.
3854   (gnus-group-list-groups 5 gnus-have-all-newsgroups))
3855
3856 (defun gnus-group-mail ()
3857   "Start composing a mail."
3858   (interactive)
3859   (mail))
3860
3861 (defun gnus-group-edit-global-kill (article &optional group)
3862   "Edit the global kill file.
3863 If GROUP, edit that local kill file instead."
3864   (interactive "P")
3865   (setq gnus-current-kill-article article)
3866   (gnus-kill-file-edit-file group)
3867   (message
3868    (substitute-command-keys
3869     "Editing a global kill file (Type \\[gnus-kill-file-exit] to exit)")))
3870
3871 (defun gnus-group-edit-local-kill (article group)
3872   "Edit a local kill file."
3873   (interactive (list nil (gnus-group-group-name)))
3874   (gnus-group-edit-global-kill article group))
3875
3876 (defun gnus-group-force-update ()
3877   "Update `.newsrc' file."
3878   (interactive)
3879   (gnus-save-newsrc-file))
3880
3881 (defun gnus-group-suspend ()
3882   "Suspend the current Gnus session.
3883 In fact, cleanup buffers except for group mode buffer.
3884 The hook gnus-suspend-gnus-hook is called before actually suspending."
3885   (interactive)
3886   (run-hooks 'gnus-suspend-gnus-hook)
3887   ;; Kill Gnus buffers except for group mode buffer.
3888   (let ((group-buf (get-buffer gnus-group-buffer)))
3889     (while gnus-buffer-list
3890       (and (not (eq (get-buffer (car gnus-buffer-list)) group-buf))
3891            (not (eq (get-buffer (car gnus-buffer-list)) gnus-dribble-buffer))
3892            (get-buffer (car gnus-buffer-list))
3893            (buffer-name (get-buffer (car gnus-buffer-list)))
3894            (kill-buffer (car gnus-buffer-list)))
3895       (setq gnus-buffer-list (cdr gnus-buffer-list)))
3896     (setq gnus-buffer-list (list group-buf))
3897     (bury-buffer group-buf)
3898     (delete-windows-on group-buf t)))
3899
3900 (defun gnus-group-clear-dribble ()
3901   "Clear all information from the dribble buffer."
3902   (interactive)
3903   (gnus-dribble-clear))
3904
3905 (defun gnus-group-exit ()
3906   "Quit reading news after updating .newsrc.eld and .newsrc.
3907 The hook `gnus-exit-gnus-hook' is called before actually exiting."
3908   (interactive)
3909   (if (or noninteractive                ;For gnus-batch-kill
3910           (zerop (buffer-size))         ;No news is good news.
3911           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
3912           (not gnus-interactive-exit)   ;Without confirmation
3913           gnus-expert-user
3914           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
3915       (progn
3916         (run-hooks 'gnus-exit-gnus-hook)
3917         (gnus-save-newsrc-file)
3918         (gnus-close-backends)
3919         (gnus-clear-system))))
3920
3921 (defun gnus-close-backends ()
3922   (let ((methods gnus-valid-select-methods)
3923         func)
3924     (while methods
3925       (if (fboundp (setq func (intern (concat (car (car methods))
3926                                               "-request-close"))))
3927           (funcall func))
3928       (setq methods (cdr methods)))))
3929
3930 (defun gnus-group-quit ()
3931   "Quit reading news without updating .newsrc.eld or .newsrc.
3932 The hook `gnus-exit-gnus-hook' is called before actually exiting."
3933   (interactive)
3934   (if (or noninteractive                ;For gnus-batch-kill
3935           (zerop (buffer-size))
3936           (not (gnus-server-opened gnus-select-method))
3937           gnus-expert-user
3938           (not gnus-current-startup-file)
3939           (gnus-yes-or-no-p
3940            (format "Quit reading news without saving %s? "
3941                    (file-name-nondirectory gnus-current-startup-file))))
3942       (progn
3943         (run-hooks 'gnus-exit-gnus-hook)
3944         (gnus-dribble-save)
3945         (gnus-close-backends)
3946         (gnus-clear-system))))
3947
3948 (defun gnus-group-describe-briefly ()
3949   "Give a one line description of the group mode commands."
3950   (interactive)
3951   (message
3952    (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")))
3953
3954 (defun gnus-group-browse-foreign-server (method)
3955   "Browse a foreign news server.
3956 If called interactively, this function will ask for a select method
3957  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
3958 If not, METHOD should be a list where the first element is the method
3959 and the second element is the address."
3960   (interactive
3961    (list (list (intern (completing-read 
3962                         "Select method: "
3963                         gnus-valid-select-methods nil t "nntp"))
3964                ;; Suggested by mapjph@bath.ac.uk.
3965                (completing-read 
3966                 "Server name: " 
3967                 (mapcar (lambda (server) (list server))
3968                         gnus-secondary-servers)))))
3969   (gnus-browse-foreign-server method))
3970
3971 \f
3972 ;;;
3973 ;;; Browse Server Mode
3974 ;;;
3975
3976 (defvar gnus-browse-server-mode-hook nil)
3977 (defvar gnus-browse-server-mode-map nil)
3978
3979 (if gnus-browse-server-mode-map
3980     nil
3981   (setq gnus-browse-server-mode-map (make-keymap))
3982   (suppress-keymap gnus-browse-server-mode-map)
3983   (define-key gnus-browse-server-mode-map " " 'gnus-browse-read-group)
3984   (define-key gnus-browse-server-mode-map "=" 'gnus-browse-read-group)
3985   (define-key gnus-browse-server-mode-map "n" 'gnus-browse-next-group)
3986   (define-key gnus-browse-server-mode-map "p" 'gnus-browse-prev-group)
3987   (define-key gnus-browse-server-mode-map "\177" 'gnus-browse-prev-group)
3988   (define-key gnus-browse-server-mode-map "N" 'gnus-browse-next-group)
3989   (define-key gnus-browse-server-mode-map "P" 'gnus-browse-prev-group)
3990   (define-key gnus-browse-server-mode-map "\M-n" 'gnus-browse-next-group)
3991   (define-key gnus-browse-server-mode-map "\M-p" 'gnus-browse-prev-group)
3992   (define-key gnus-browse-server-mode-map "\r" 'gnus-browse-read-group)
3993   (define-key gnus-browse-server-mode-map "u" 'gnus-browse-unsubscribe-current-group)
3994   (define-key gnus-browse-server-mode-map "q" 'gnus-browse-exit)
3995   (define-key gnus-browse-server-mode-map "Q" 'gnus-browse-exit)
3996   (define-key gnus-browse-server-mode-map "\C-c\C-c" 'gnus-browse-exit)
3997   (define-key gnus-browse-server-mode-map "?" 'gnus-browse-describe-briefly)
3998   (define-key gnus-browse-server-mode-map "\C-c\C-i" 'gnus-info-find-node)
3999   )
4000
4001 (defvar gnus-browse-current-method nil)
4002
4003 (defun gnus-browse-foreign-server (method)
4004   (setq gnus-browse-current-method method)
4005   (let ((gnus-select-method method)
4006         groups group)
4007     (message "Connecting to %s..." (nth 1 method))
4008     (if (not (gnus-request-list method))
4009         (error "Unable to contact server: " (gnus-status-message method)))
4010     (set-buffer (get-buffer-create "*Gnus Browse Server*"))
4011     (gnus-add-current-to-buffer-list)
4012     (buffer-disable-undo (current-buffer))
4013     (let ((buffer-read-only nil))
4014       (erase-buffer))
4015     (gnus-browse-server-mode)
4016     (setq mode-line-buffer-identification
4017           (format
4018            "(ding) Browse Server {%s:%s}" (car method) (car (cdr method))))
4019     (save-excursion
4020       (set-buffer nntp-server-buffer)
4021       (let ((cur (current-buffer)))
4022         (goto-char 1)
4023         (delete-matching-lines gnus-ignored-newsgroups)
4024         (while (re-search-forward 
4025                 "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
4026           (goto-char (match-end 1))
4027           (setq groups (cons (cons (buffer-substring (match-beginning 1)
4028                                                      (match-end 1))
4029                                    (- (read cur) (read cur)))
4030                              groups)))))
4031     (setq groups (sort groups 
4032                        (lambda (l1 l2)
4033                          (string< (car l1) (car l2)))))
4034     (let ((buffer-read-only nil))
4035       (while groups
4036         (setq group (car groups))
4037         (insert 
4038          (format "K%7d: %s\n" (cdr group) (car group)))
4039         (setq groups (cdr groups))))
4040     (switch-to-buffer (current-buffer))
4041     (goto-char 1)
4042     (gnus-group-position-cursor)))
4043
4044 (defun gnus-browse-server-mode ()
4045   "Major mode for browsing a foreign server."
4046   (interactive)
4047   (kill-all-local-variables)
4048   (setq mode-line-modified "-- ")
4049   (make-local-variable 'mode-line-format)
4050   (setq mode-line-format (copy-sequence mode-line-format))
4051   (and (equal (nth 3 mode-line-format) "   ")
4052        (setcar (nthcdr 3 mode-line-format) ""))
4053   (setq major-mode 'gnus-browse-server-mode)
4054   (setq mode-name "Browse Server")
4055   (setq mode-line-process nil)
4056   (use-local-map gnus-browse-server-mode-map)
4057   (buffer-disable-undo (current-buffer))
4058   (setq truncate-lines t)
4059   (setq buffer-read-only t)
4060   (run-hooks 'gnus-browse-server-mode-hook))
4061
4062 (defun gnus-browse-read-group ()
4063   "Not implemented, and will probably never be."
4064   (interactive)
4065   (error "You can't read while browsing"))
4066
4067 (defun gnus-browse-next-group (n)
4068   "Go to the next group."
4069   (interactive "p")
4070   (prog1
4071       (forward-line n)
4072     (gnus-group-position-cursor)))
4073
4074 (defun gnus-browse-prev-group (n)
4075   "Go to the next group."
4076   (interactive "p")
4077   (gnus-browse-next-group (- n)))
4078
4079 (defun gnus-browse-unsubscribe-current-group (arg)
4080   "(Un)subscribe to the next ARG groups."
4081   (interactive "p")
4082   (and (eobp)
4083        (error "No group at current line."))
4084   (let ((ward (if (< arg 0) -1 1))
4085         (arg (abs arg)))
4086     (while (and (> arg 0)
4087                 (not (eobp))
4088                 (gnus-browse-unsubscribe-group)
4089                 (zerop (gnus-browse-next-group ward)))
4090       (setq arg (1- arg)))
4091     (gnus-group-position-cursor)
4092     (if (/= 0 arg) (message "No more newsgroups"))
4093     arg))
4094   
4095 (defun gnus-browse-unsubscribe-group ()
4096   (let ((sub nil)
4097         (buffer-read-only nil)
4098         group)
4099     (save-excursion
4100       (beginning-of-line)
4101       (if (= (following-char) ?K) (setq sub t))
4102       (re-search-forward ": \\(.*\\)$" nil t)
4103       (setq group (gnus-group-prefixed-name 
4104                    (buffer-substring (match-beginning 1) (match-end 1))
4105                    gnus-browse-current-method))
4106       (beginning-of-line)
4107       (delete-char 1)
4108       (if sub
4109           (progn
4110             (gnus-group-change-level 
4111              (list t group 3 nil nil gnus-browse-current-method) 3 9 
4112              (gnus-gethash (car (nth 1 gnus-newsrc-assoc)) gnus-newsrc-hashtb)
4113              t)
4114             (insert ? ))
4115         (gnus-group-change-level group 9 3)
4116         (insert ?K)))
4117     t))
4118
4119 (defun gnus-browse-exit ()
4120   "Quit browsing and return to the group buffer."
4121   (interactive)
4122   (if (eq major-mode 'gnus-browse-server-mode)
4123       (kill-buffer (current-buffer)))
4124   (switch-to-buffer gnus-group-buffer)
4125   (gnus-group-list-groups 5))
4126
4127 (defun gnus-browse-describe-briefly ()
4128   "Give a one line description of the group mode commands."
4129   (interactive)
4130   (message
4131    (substitute-command-keys "\\<gnus-browse-server-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")))
4132       
4133 \f
4134 ;;;
4135 ;;; Gnus summary mode
4136 ;;;
4137
4138 (defvar gnus-summary-mode-map nil)
4139 (defvar gnus-summary-mark-map nil)
4140 (defvar gnus-summary-mscore-map nil)
4141 (defvar gnus-summary-send-map nil)
4142 (defvar gnus-summary-extract-map nil)
4143 (defvar gnus-summary-extract-view-map nil)
4144 (defvar gnus-summary-article-map nil)
4145 (defvar gnus-summary-thread-map nil)
4146 (defvar gnus-summary-goto-map nil)
4147 (defvar gnus-summary-exit-map nil)
4148 (defvar gnus-summary-various-map nil)
4149 (defvar gnus-summary-interest-map nil)
4150 (defvar gnus-summary-process-map nil)
4151 (defvar gnus-summary-score-map nil)
4152 (defvar gnus-summary-sort-map nil)
4153 (defvar gnus-summary-mgroup-map nil)
4154 (defvar gnus-summary-vsave-map nil)
4155 (defvar gnus-summary-increase-map nil)
4156 (defvar gnus-summary-inc-subject-map nil)
4157 (defvar gnus-summary-inc-author-map nil)
4158 (defvar gnus-summary-inc-id-map nil)
4159 (defvar gnus-summary-inc-xref-map nil)
4160 (defvar gnus-summary-inc-thread-map nil)
4161 (defvar gnus-summary-inc-fol-map nil)
4162 (defvar gnus-summary-lower-map nil)
4163 (defvar gnus-summary-low-subject-map nil)
4164 (defvar gnus-summary-low-author-map nil)
4165 (defvar gnus-summary-low-id-map nil)
4166 (defvar gnus-summary-low-xref-map nil)
4167 (defvar gnus-summary-low-thread-map nil)
4168 (defvar gnus-summary-low-fol-map nil)
4169
4170 (if gnus-summary-mode-map
4171     nil
4172   (setq gnus-summary-mode-map (make-keymap))
4173   (suppress-keymap gnus-summary-mode-map)
4174
4175   ;; Non-orthogonal keys
4176
4177   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
4178   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
4179   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
4180   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
4181   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
4182   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
4183   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
4184   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
4185   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
4186   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
4187   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
4188   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
4189   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
4190   (define-key gnus-summary-mode-map "\M-s" 'gnus-summary-search-article-forward)
4191   (define-key gnus-summary-mode-map "\M-r" 'gnus-summary-search-article-backward)
4192   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
4193   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
4194   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-subject)
4195   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
4196   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
4197   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
4198   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
4199   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
4200   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
4201   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
4202   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
4203   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
4204   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
4205   (define-key gnus-summary-mode-map "k" 'gnus-summary-kill-same-subject-and-select)
4206   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
4207   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
4208   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
4209   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
4210   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
4211   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
4212   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
4213   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
4214   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
4215   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
4216   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
4217   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
4218   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
4219   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
4220   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
4221   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
4222   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
4223   (define-key gnus-summary-mode-map "\C-c\M-\C-s" 'gnus-summary-show-all-expunged)
4224   (define-key gnus-summary-mode-map "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
4225   (define-key gnus-summary-mode-map "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
4226   (define-key gnus-summary-mode-map "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
4227   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
4228   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
4229   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
4230   (define-key gnus-summary-mode-map "\C-x\C-s" 'gnus-summary-reselect-current-group)
4231   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
4232   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
4233   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
4234   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
4235   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
4236   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
4237   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
4238   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
4239   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
4240   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
4241   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
4242   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
4243   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
4244   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
4245   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
4246   (define-key gnus-summary-mode-map "V" 'gnus-version)
4247   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
4248   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
4249   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
4250   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
4251   (define-key gnus-summary-mode-map [mouse-2] 'gnus-mouse-pick-article)
4252   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
4253   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
4254   (define-key gnus-summary-mode-map "x" 'gnus-summary-delete-marked-as-read)
4255 ; (define-key gnus-summary-mode-map "X" 'gnus-summary-remove-lines-marked-with)
4256   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
4257   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
4258   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
4259 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
4260   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
4261   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu)
4262   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
4263
4264
4265   ;; Sort of orthogonal keymap
4266   (define-prefix-command 'gnus-summary-mark-map)
4267   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
4268   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
4269   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
4270   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
4271   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
4272   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
4273   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
4274   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
4275   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
4276   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
4277   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
4278   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
4279   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
4280   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
4281   (define-key gnus-summary-mark-map "\M-r" 'gnus-summary-remove-lines-marked-as-read)
4282   (define-key gnus-summary-mark-map "\M-\C-r" 'gnus-summary-remove-lines-marked-with)
4283   (define-key gnus-summary-mark-map "D" 'gnus-summary-show-all-dormant)
4284   (define-key gnus-summary-mark-map "\M-D" 'gnus-summary-hide-all-dormant)
4285   (define-key gnus-summary-mark-map "S" 'gnus-summary-show-all-expunged)
4286   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
4287   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
4288   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
4289   (define-key gnus-summary-mark-map "k" 'gnus-summary-kill-same-subject-and-select)
4290   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
4291
4292   (define-prefix-command 'gnus-summary-mscore-map)
4293   (define-key gnus-summary-mark-map "s" 'gnus-summary-mscore-map)
4294   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
4295   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
4296   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
4297   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
4298
4299   (define-prefix-command 'gnus-summary-process-map)
4300   (define-key gnus-summary-mark-map "p" 'gnus-summary-process-map)
4301   (define-key gnus-summary-process-map "p" 'gnus-summary-mark-as-processable)
4302   (define-key gnus-summary-process-map "u" 'gnus-summary-unmark-as-processable)
4303   (define-key gnus-summary-process-map "U" 'gnus-summary-unmark-all-processable)
4304   (define-key gnus-summary-process-map "s" 'gnus-uu-mark-series)
4305   (define-key gnus-summary-process-map "r" 'gnus-uu-mark-region)
4306   (define-key gnus-summary-process-map "R" 'gnus-uu-mark-by-regexp)
4307   (define-key gnus-summary-process-map "t" 'gnus-uu-mark-thread)
4308   (define-key gnus-summary-process-map "a" 'gnus-uu-mark-all)
4309   (define-key gnus-summary-process-map "S" 'gnus-uu-mark-sparse)
4310   
4311
4312   (define-prefix-command 'gnus-summary-send-map)
4313   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
4314   (define-key gnus-summary-send-map "p" 'gnus-summary-post-news)
4315   (define-key gnus-summary-send-map "f" 'gnus-summary-followup)
4316   (define-key gnus-summary-send-map "F" 'gnus-summary-followup-with-original)
4317   (define-key gnus-summary-send-map "b" 'gnus-summary-followup-and-reply)
4318   (define-key gnus-summary-send-map "B" 'gnus-summary-followup-and-reply-with-original)
4319   (define-key gnus-summary-send-map "c" 'gnus-summary-cancel-article)
4320   (define-key gnus-summary-send-map "s" 'gnus-summary-supersede-article)
4321   (define-key gnus-summary-send-map "r" 'gnus-summary-reply)
4322   (define-key gnus-summary-send-map "R" 'gnus-summary-reply-with-original)
4323   (define-key gnus-summary-send-map "\C-f" 'gnus-summary-mail-forward)
4324   (define-key gnus-summary-send-map "m" 'gnus-summary-mail-other-window)
4325   (define-key gnus-summary-send-map "u" 'gnus-uu-post-news)
4326   (define-key gnus-summary-send-map "\M-f" 'gnus-uu-digest-and-forward)
4327
4328   
4329   (define-prefix-command 'gnus-summary-goto-map)
4330   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
4331   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
4332   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
4333   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
4334   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
4335   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
4336   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
4337   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
4338   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
4339   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
4340   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
4341   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
4342   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
4343   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
4344
4345
4346   (define-prefix-command 'gnus-summary-thread-map)
4347   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
4348   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
4349   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
4350   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
4351   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
4352   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
4353   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
4354   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
4355   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
4356   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
4357   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
4358   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
4359   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
4360   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
4361
4362   
4363   (define-prefix-command 'gnus-summary-exit-map)
4364   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
4365   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
4366   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
4367   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
4368   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
4369   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
4370
4371
4372   (define-prefix-command 'gnus-summary-article-map)
4373   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
4374   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
4375   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
4376   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
4377   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
4378   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
4379   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
4380   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
4381   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
4382   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
4383   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
4384   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
4385   (define-key gnus-summary-article-map "w" 'gnus-summary-stop-page-breaking)
4386   (define-key gnus-summary-article-map "c" 'gnus-summary-caesar-message)
4387   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
4388   (define-key gnus-summary-article-map "t" 'gnus-summary-toggle-header)
4389   (define-key gnus-summary-article-map "hh" 'gnus-article-hide-headers)
4390   (define-key gnus-summary-article-map "hs" 'gnus-article-hide-signature)
4391   (define-key gnus-summary-article-map "hc" 'gnus-article-hide-citation)
4392   (define-key gnus-summary-article-map "ho" 'gnus-article-treat-overstrike)
4393   (define-key gnus-summary-article-map "hw" 'gnus-article-word-wrap)
4394   (define-key gnus-summary-article-map "hd" 'gnus-article-remove-cr)
4395   (define-key gnus-summary-article-map "hq" 'gnus-article-de-quoted-unreadable)
4396   (define-key gnus-summary-article-map "m" 'gnus-summary-toggle-mime)
4397   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
4398
4399
4400   (define-prefix-command 'gnus-summary-extract-map)
4401   (define-key gnus-summary-mode-map "X" 'gnus-summary-extract-map)
4402 ;  (define-key gnus-summary-extract-map "x" 'gnus-summary-extract-any)
4403 ;  (define-key gnus-summary-extract-map "m" 'gnus-summary-extract-mime)
4404   (define-key gnus-summary-extract-map "u" 'gnus-uu-decode-uu)
4405   (define-key gnus-summary-extract-map "U" 'gnus-uu-decode-uu-and-save)
4406   (define-key gnus-summary-extract-map "s" 'gnus-uu-decode-unshar)
4407   (define-key gnus-summary-extract-map "S" 'gnus-uu-decode-unshar-and-save)
4408   (define-key gnus-summary-extract-map "o" 'gnus-uu-decode-save)
4409   (define-key gnus-summary-extract-map "O" 'gnus-uu-decode-save)
4410   (define-key gnus-summary-extract-map "b" 'gnus-uu-decode-binhex)
4411   (define-key gnus-summary-extract-map "B" 'gnus-uu-decode-binhex)
4412
4413   (define-prefix-command 'gnus-summary-extract-view-map)
4414   (define-key gnus-summary-extract-map "v" 'gnus-summary-extract-view-map)
4415   (define-key gnus-summary-extract-view-map "u" 'gnus-uu-decode-uu-view)
4416   (define-key gnus-summary-extract-view-map "U" 'gnus-uu-decode-uu-and-save-view)
4417   (define-key gnus-summary-extract-view-map "s" 'gnus-uu-decode-unshar-view)
4418   (define-key gnus-summary-extract-view-map "S" 'gnus-uu-decode-unshar-and-save-view)
4419   (define-key gnus-summary-extract-view-map "o" 'gnus-uu-decode-save-view)
4420   (define-key gnus-summary-extract-view-map "O" 'gnus-uu-decode-save-view)
4421   (define-key gnus-summary-extract-view-map "b" 'gnus-uu-decode-binhex-view)
4422   (define-key gnus-summary-extract-view-map "B" 'gnus-uu-decode-binhex-view)
4423   
4424   
4425   (define-prefix-command 'gnus-summary-various-map)
4426   (define-key gnus-summary-mode-map "V" 'gnus-summary-various-map)
4427   (define-key gnus-summary-various-map "u" 'gnus-summary-universal-argument)
4428   (define-key gnus-summary-various-map "\C-s" 'gnus-summary-search-article-forward)
4429   (define-key gnus-summary-various-map "\C-r" 'gnus-summary-search-article-backward)
4430   (define-key gnus-summary-various-map "r" 'gnus-summary-refer-article)
4431   (define-key gnus-summary-various-map "&" 'gnus-summary-execute-command)
4432   (define-key gnus-summary-various-map "T" 'gnus-summary-toggle-truncation)
4433   (define-key gnus-summary-various-map "e" 'gnus-summary-expand-window)
4434   (define-key gnus-summary-various-map "S" 'gnus-summary-reselect-current-group)
4435   (define-key gnus-summary-various-map "g" 'gnus-summary-rescan-group)
4436   (define-key gnus-summary-various-map "V" 'gnus-version)
4437   (define-key gnus-summary-various-map "f" 'gnus-summary-fetch-faq)
4438   (define-key gnus-summary-various-map "d" 'gnus-summary-describe-group)
4439   (define-key gnus-summary-various-map "?" 'gnus-summary-describe-briefly)
4440   (define-key gnus-summary-various-map "i" 'gnus-info-find-node)
4441   (define-key gnus-summary-various-map "D" 'gnus-summary-enter-digest-group)
4442   (define-key gnus-summary-various-map "k" 'gnus-summary-edit-local-kill)
4443   (define-key gnus-summary-various-map "K" 'gnus-summary-edit-global-kill)
4444
4445   (define-prefix-command 'gnus-summary-vsave-map)
4446   (define-key gnus-summary-various-map "o" 'gnus-summary-vsave-map)
4447   (define-key gnus-summary-vsave-map "o" 'gnus-summary-save-article)
4448   (define-key gnus-summary-vsave-map "m" 'gnus-summary-save-article-mail)
4449   (define-key gnus-summary-vsave-map "r" 'gnus-summary-save-article-rmail)
4450   (define-key gnus-summary-vsave-map "f" 'gnus-summary-save-article-file)
4451   (define-key gnus-summary-vsave-map "h" 'gnus-summary-save-article-folder)
4452   (define-key gnus-summary-vsave-map "p" 'gnus-summary-pipe-output)
4453
4454   (define-prefix-command 'gnus-summary-score-map)
4455   (define-key gnus-summary-various-map "S" 'gnus-summary-score-map)
4456   (define-key gnus-summary-score-map "s" 'gnus-summary-set-score)
4457   (define-key gnus-summary-score-map "c" 'gnus-score-change-score-file)
4458   (define-key gnus-summary-score-map "m" 'gnus-score-set-mark-below)
4459   (define-key gnus-summary-score-map "x" 'gnus-score-set-expunge-below)
4460   (define-key gnus-summary-score-map "e" 'gnus-score-edit-alist)
4461   (define-key gnus-summary-score-map "f" 'gnus-score-edit-file)
4462
4463   (define-prefix-command 'gnus-summary-sort-map)
4464   (define-key gnus-summary-various-map "s" 'gnus-summary-sort-map)
4465   (define-key gnus-summary-sort-map "n" 'gnus-summary-sort-by-number)
4466   (define-key gnus-summary-sort-map "a" 'gnus-summary-sort-by-author)
4467   (define-key gnus-summary-sort-map "s" 'gnus-summary-sort-by-subject)
4468   (define-key gnus-summary-sort-map "d" 'gnus-summary-sort-by-date)
4469   (define-key gnus-summary-sort-map "i" 'gnus-summary-sort-by-score)
4470
4471   (define-prefix-command 'gnus-summary-mgroup-map)
4472   (define-key gnus-summary-various-map "m" 'gnus-summary-mgroup-map)
4473   (define-key gnus-summary-mgroup-map "e" 'gnus-summary-expire-articles)
4474   (define-key gnus-summary-mgroup-map "\177" 'gnus-summary-delete-article)
4475   (define-key gnus-summary-mgroup-map "m" 'gnus-summary-move-article)
4476   (define-key gnus-summary-mgroup-map "r" 'gnus-summary-respool-article)
4477   (define-key gnus-summary-mgroup-map "w" 'gnus-summary-edit-article)
4478   (define-key gnus-summary-mgroup-map "c" 'gnus-summary-copy-article)
4479
4480
4481   (define-prefix-command 'gnus-summary-increase-map)
4482   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-map)
4483   (define-key gnus-summary-increase-map "i" 'gnus-summary-raise-same-subject-and-select)
4484   (define-key gnus-summary-increase-map "I" 'gnus-summary-raise-same-subject)
4485   (define-key gnus-summary-increase-map "\C-i" 'gnus-summary-raise-score)
4486
4487   (define-prefix-command 'gnus-summary-inc-subject-map)
4488   (define-key gnus-summary-increase-map "s" 'gnus-summary-inc-subject-map)
4489   (define-key gnus-summary-increase-map "S" 'gnus-summary-temporarily-raise-by-subject)
4490   (define-key gnus-summary-inc-subject-map "s" 'gnus-summary-temporarily-raise-by-subject)
4491   (define-key gnus-summary-inc-subject-map "S" 'gnus-summary-raise-by-subject)
4492   (define-key gnus-summary-inc-subject-map "t" 'gnus-summary-temporarily-raise-by-subject)
4493   (define-key gnus-summary-inc-subject-map "p" 'gnus-summary-raise-by-subject)
4494
4495   (define-prefix-command 'gnus-summary-inc-author-map)
4496   (define-key gnus-summary-increase-map "a" 'gnus-summary-inc-author-map)
4497   (define-key gnus-summary-increase-map "A" 'gnus-summary-temporarily-raise-by-author)
4498   (define-key gnus-summary-inc-author-map "a" 'gnus-summary-temporarily-raise-by-author)
4499   (define-key gnus-summary-inc-author-map "A" 'gnus-summary-raise-by-author)
4500   (define-key gnus-summary-inc-author-map "t" 'gnus-summary-temporarily-raise-by-author)
4501   (define-key gnus-summary-inc-author-map "p" 'gnus-summary-raise-by-author)
4502
4503   (define-prefix-command 'gnus-summary-inc-id-map)
4504   (define-key gnus-summary-increase-map "i" 'gnus-summary-inc-id-map)
4505   (define-key gnus-summary-increase-map "I" 'gnus-summary-temporarily-raise-by-id)
4506   (define-key gnus-summary-inc-id-map "i" 'gnus-summary-temporarily-raise-by-id)
4507   (define-key gnus-summary-inc-id-map "I" 'gnus-summary-raise-by-id)
4508   (define-key gnus-summary-inc-id-map "t" 'gnus-summary-temporarily-raise-by-id)
4509   (define-key gnus-summary-inc-id-map "p" 'gnus-summary-raise-by-id)
4510
4511   (define-prefix-command 'gnus-summary-inc-thread-map)
4512   (define-key gnus-summary-increase-map "t" 'gnus-summary-inc-thread-map)
4513   (define-key gnus-summary-increase-map "T" 'gnus-summary-temporarily-raise-by-thread)
4514   (define-key gnus-summary-inc-thread-map "t" 'gnus-summary-temporarily-raise-by-thread)
4515   (define-key gnus-summary-inc-thread-map "T" 'gnus-summary-raise-by-thread)
4516   (define-key gnus-summary-inc-thread-map "t" 'gnus-summary-temporarily-raise-by-thread)
4517   (define-key gnus-summary-inc-thread-map "p" 'gnus-summary-raise-by-thread)
4518
4519   (define-prefix-command 'gnus-summary-inc-xref-map)
4520   (define-key gnus-summary-increase-map "x" 'gnus-summary-inc-xref-map)
4521   (define-key gnus-summary-increase-map "X" 'gnus-summary-temporarily-raise-by-xref)
4522   (define-key gnus-summary-inc-xref-map "x" 'gnus-summary-temporarily-raise-by-xref)
4523   (define-key gnus-summary-inc-xref-map "X" 'gnus-summary-raise-by-xref)
4524   (define-key gnus-summary-inc-xref-map "t" 'gnus-summary-temporarily-raise-by-xref)
4525   (define-key gnus-summary-inc-xref-map "p" 'gnus-summary-raise-by-xref)
4526
4527   (define-prefix-command 'gnus-summary-inc-fol-map)
4528   (define-key gnus-summary-increase-map "f" 'gnus-summary-inc-fol-map)
4529   (define-key gnus-summary-increase-map "F" 'gnus-summary-raise-followups-to-author)
4530   (define-key gnus-summary-inc-fol-map "f" 'gnus-summary-raise-followups-to-author)
4531   (define-key gnus-summary-inc-fol-map "F" 'gnus-summary-raise-followups-to-author)
4532   (define-key gnus-summary-inc-fol-map "t" 'gnus-summary-raise-followups-to-author)
4533   (define-key gnus-summary-inc-fol-map "p" 'gnus-summary-raise-followups-to-author)
4534
4535   (define-prefix-command 'gnus-summary-lower-map)
4536   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-map)
4537   (define-key gnus-summary-lower-map "l" 'gnus-summary-lower-same-subject-and-select)
4538   (define-key gnus-summary-lower-map "L" 'gnus-summary-lower-same-subject)
4539   (define-key gnus-summary-lower-map "\C-l" 'gnus-summary-lower-score)
4540
4541   (define-prefix-command 'gnus-summary-low-subject-map)
4542   (define-key gnus-summary-lower-map "s" 'gnus-summary-low-subject-map)
4543   (define-key gnus-summary-lower-map "S" 'gnus-summary-temporarily-lower-by-subject)
4544   (define-key gnus-summary-low-subject-map "s" 'gnus-summary-temporarily-lower-by-subject)
4545   (define-key gnus-summary-low-subject-map "S" 'gnus-summary-lower-by-subject)
4546   (define-key gnus-summary-low-subject-map "t" 'gnus-summary-temporarily-lower-by-subject)
4547   (define-key gnus-summary-low-subject-map "p" 'gnus-summary-lower-by-subject)
4548
4549   (define-prefix-command 'gnus-summary-low-author-map)
4550   (define-key gnus-summary-lower-map "a" 'gnus-summary-low-author-map)
4551   (define-key gnus-summary-lower-map "A" 'gnus-summary-temporarily-lower-by-author)
4552   (define-key gnus-summary-low-author-map "a" 'gnus-summary-temporarily-lower-by-author)
4553   (define-key gnus-summary-low-author-map "A" 'gnus-summary-lower-by-author)
4554   (define-key gnus-summary-low-author-map "t" 'gnus-summary-temporarily-lower-by-author)
4555   (define-key gnus-summary-low-author-map "p" 'gnus-summary-lower-by-author)
4556
4557   (define-prefix-command 'gnus-summary-low-id-map)
4558   (define-key gnus-summary-lower-map "i" 'gnus-summary-low-id-map)
4559   (define-key gnus-summary-lower-map "I" 'gnus-summary-temporarily-lower-by-id)
4560   (define-key gnus-summary-low-id-map "i" 'gnus-summary-temporarily-lower-by-id)
4561   (define-key gnus-summary-low-id-map "I" 'gnus-summary-lower-by-id)
4562   (define-key gnus-summary-low-id-map "t" 'gnus-summary-temporarily-lower-by-id)
4563   (define-key gnus-summary-low-id-map "p" 'gnus-summary-lower-by-id)
4564
4565   (define-prefix-command 'gnus-summary-low-thread-map)
4566   (define-key gnus-summary-lower-map "t" 'gnus-summary-low-thread-map)
4567   (define-key gnus-summary-lower-map "T" 'gnus-summary-temporarily-lower-by-thread)
4568   (define-key gnus-summary-low-thread-map "t" 'gnus-summary-temporarily-lower-by-thread)
4569   (define-key gnus-summary-low-thread-map "T" 'gnus-summary-lower-by-thread)
4570   (define-key gnus-summary-low-thread-map "t" 'gnus-summary-temporarily-lower-by-thread)
4571   (define-key gnus-summary-low-thread-map "p" 'gnus-summary-lower-by-thread)
4572
4573   (define-prefix-command 'gnus-summary-low-xref-map)
4574   (define-key gnus-summary-lower-map "x" 'gnus-summary-low-xref-map)
4575   (define-key gnus-summary-lower-map "X" 'gnus-summary-temporarily-lower-by-xref)
4576   (define-key gnus-summary-low-xref-map "x" 'gnus-summary-temporarily-lower-by-xref)
4577   (define-key gnus-summary-low-xref-map "X" 'gnus-summary-lower-by-xref)
4578   (define-key gnus-summary-low-xref-map "t" 'gnus-summary-temporarily-lower-by-xref)
4579   (define-key gnus-summary-low-xref-map "p" 'gnus-summary-lower-by-xref)
4580
4581   (define-prefix-command 'gnus-summary-low-fol-map)
4582   (define-key gnus-summary-lower-map "f" 'gnus-summary-low-fol-map)
4583   (define-key gnus-summary-lower-map "F" 'gnus-summary-lower-followups-to-author)
4584   (define-key gnus-summary-low-fol-map "f" 'gnus-summary-lower-followups-to-author)
4585   (define-key gnus-summary-low-fol-map "F" 'gnus-summary-lower-followups-to-author)
4586   (define-key gnus-summary-low-fol-map "t" 'gnus-summary-lower-followups-to-author)
4587   (define-key gnus-summary-low-fol-map "p" 'gnus-summary-lower-followups-to-author)
4588   )
4589
4590
4591 \f
4592
4593 (defun gnus-summary-mode ()
4594   "Major mode for reading articles.
4595
4596 All normal editing commands are switched off.
4597 \\<gnus-summary-mode-map>
4598 Each line in this buffer represents one article.  To read an
4599 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
4600 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
4601 respectively.
4602
4603 You can also post articles and send mail from this buffer.  To 
4604 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
4605 of an article, type `\\[gnus-summary-reply]'.
4606
4607 There are approx. one gazillion commands you can execute in this 
4608 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
4609
4610 The following commands are available:
4611
4612 \\{gnus-summary-mode-map}"
4613   (interactive)
4614   (if gnus-visual (gnus-summary-make-menu-bar))
4615   (kill-all-local-variables)
4616   (let ((locals gnus-summary-local-variables))
4617     (while locals
4618       (if (consp (car locals))
4619           (progn
4620             (make-local-variable (car (car locals)))
4621             (set (car (car locals)) (eval (cdr (car locals)))))
4622         (make-local-variable (car locals))
4623         (set (car locals) nil))
4624       (setq locals (cdr locals))))
4625   (gnus-update-format-specifications)
4626   (setq mode-line-modified "-- ")
4627   (make-local-variable 'mode-line-format)
4628   (setq mode-line-format (copy-sequence mode-line-format))
4629   (and (equal (nth 3 mode-line-format) "   ")
4630        (setcar (nthcdr 3 mode-line-format) ""))
4631   (setq major-mode 'gnus-summary-mode)
4632   (setq mode-name "Summary")
4633   (make-local-variable 'minor-mode-alist)
4634   (gnus-set-mode-line 'summary)
4635   (use-local-map gnus-summary-mode-map)
4636   (buffer-disable-undo (current-buffer))
4637   (setq buffer-read-only t)             ;Disable modification
4638   (setq truncate-lines t)
4639   (setq selective-display t)
4640   (setq selective-display-ellipses t)   ;Display `...'
4641   (run-hooks 'gnus-summary-mode-hook))
4642
4643 (defun gnus-summary-clear-local-variables ()
4644   (let ((locals gnus-summary-local-variables))
4645     (while locals
4646       (if (consp (car locals))
4647           (set (car (car locals)) nil)
4648         (set (car locals) nil))
4649       (setq locals (cdr locals)))))
4650
4651 (defun gnus-mouse-pick-article (e)
4652   (interactive "e")
4653   (mouse-set-point e)
4654   (gnus-summary-next-page nil t))
4655
4656 (defun gnus-summary-setup-buffer (group)
4657   "Initialize summary buffer."
4658   (let ((buffer (concat "*Summary " group "*")))
4659     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
4660     (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
4661     (gnus-add-current-to-buffer-list)
4662     (gnus-summary-mode)))
4663
4664 (defun gnus-set-global-variables ()
4665   ;; Set the global equivalents of the summary buffer-local variables
4666   ;; to the latest values they had. These reflect the summary buffer
4667   ;; that was in action when the last article was fetched.
4668   (if (eq major-mode 'gnus-summary-mode) 
4669       (progn
4670         (setq gnus-summary-buffer (current-buffer))
4671         (let ((name gnus-newsgroup-name)
4672               (marked gnus-newsgroup-marked)
4673               (unread gnus-newsgroup-unreads)
4674               (headers gnus-current-headers))
4675           (save-excursion
4676             (set-buffer gnus-group-buffer)
4677             (setq gnus-newsgroup-name name)
4678             (setq gnus-newsgroup-marked marked)
4679             (setq gnus-newsgroup-unreads unread)
4680             (setq gnus-current-headers headers))))))
4681
4682 (defun gnus-summary-insert-dummy-line (sformat subject number)
4683   (if (not sformat) 
4684       (setq sformat gnus-summary-dummy-line-format-spec))
4685   (let (b)
4686     (beginning-of-line)
4687     (setq b (point))
4688     (insert (eval sformat))
4689     (add-text-properties
4690      b (1+ b)
4691      (list 'gnus-subject (gnus-simplify-subject-re subject)
4692            'gnus-number number
4693            'gnus-mark ?Z
4694            'gnus-thread 0))))
4695
4696 (defun gnus-summary-insert-line 
4697   (sformat header level current unread replied expirable subject-or-nil
4698            &optional dummy score)
4699   (or sformat (setq sformat gnus-summary-line-format-spec))
4700   (let* ((indentation 
4701           (make-string (* level gnus-thread-indent-level) ? ))
4702          (lines (or (header-lines header) 0))
4703          (score (or score gnus-summary-default-score 0))
4704          (score-char (if (or (null gnus-summary-default-score)
4705                              (= score gnus-summary-default-score)) ? 
4706                        (if (< score gnus-summary-default-score) 
4707                            gnus-score-below-mark gnus-score-over-mark)))
4708          (replied (if replied gnus-replied-mark ? ))
4709          (from (header-from header))
4710          (name-address (funcall gnus-extract-address-components from))
4711          (address (car (cdr name-address)))
4712          (name (or (car name-address) (car (cdr name-address))))
4713          (number (header-number header))
4714          (subject (header-subject header))
4715          (buffer-read-only nil)
4716          (opening-bracket (if dummy ?\< ?\[))
4717          (closing-bracket (if dummy ?\> ?\]))
4718          b)
4719     ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
4720     (if (not (numberp lines)) (setq lines 0))
4721     (beginning-of-line)
4722     (setq b (point))
4723     (insert (eval sformat))
4724     (add-text-properties
4725      b (1+ b)
4726      (list 'gnus-subject (gnus-simplify-subject-re subject)
4727            'gnus-number number
4728            'gnus-mark (or unread gnus-unread-mark ? )
4729            'gnus-thread level))))
4730
4731 (defun gnus-summary-update-line (&optional dont-update)
4732   ;; Update summary line after change.
4733   (or (not gnus-summary-default-score)
4734       gnus-summary-inhibit-highlight
4735       (let ((gnus-summary-inhibit-highlight t))
4736         (progn
4737           (or dont-update
4738               (if (and gnus-summary-mark-below
4739                        (< (gnus-summary-article-score)
4740                           gnus-summary-mark-below))
4741                   (and (not (memq (gnus-summary-article-number)
4742                                   gnus-newsgroup-marked))
4743                        (gnus-summary-mark-article nil gnus-low-score-mark))
4744                 (and (eq (gnus-summary-article-mark) gnus-low-score-mark)
4745                      (gnus-summary-mark-article nil gnus-unread-mark))))
4746           (and gnus-visual
4747                (run-hooks 'gnus-visual-summary-update-hook))))))
4748
4749 (defun gnus-summary-update-lines ()
4750   ;; Rehighlight summary buffer according to `gnus-summary-highlight'.
4751   (and (save-excursion
4752          (set-buffer gnus-summary-buffer)
4753          (goto-char (point-min))
4754          (while (not (eobp))
4755            (gnus-summary-update-line)
4756            (forward-line 1)))))
4757
4758 (defun gnus-summary-read-group (group &optional show-all no-article kill-buffer)
4759   "Start reading news in newsgroup GROUP.
4760 If SHOW-ALL is non-nil, already read articles are also listed.
4761 If NO-ARTICLE is non-nil, no article is selected initially."
4762   (message "Retrieving newsgroup: %s..." group)
4763   (gnus-summary-setup-buffer group)
4764   (if (gnus-select-newsgroup group show-all)
4765       (progn
4766         ;; You can change the subjects in this hook.
4767         (run-hooks 'gnus-select-group-hook)
4768         ;; Do Score Processing.
4769         (gnus-score-headers)
4770         ;; Update the format specifiers.
4771         (gnus-update-format-specifications)
4772         (gnus-summary-prepare)
4773         (if (and (zerop (buffer-size))
4774                  gnus-newsgroup-dormant)
4775             (gnus-summary-show-all-dormant))
4776         (gnus-set-global-variables)
4777         ;; Function `gnus-apply-kill-file' must be called in this hook.
4778         (run-hooks 'gnus-apply-kill-hook)
4779         (if (zerop (buffer-size))
4780             (progn
4781               ;; This newsgroup is empty.
4782               (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
4783               (message "No unread news"))
4784           (save-excursion
4785             (if kill-buffer
4786                 (let ((gnus-summary-buffer kill-buffer))
4787                   (gnus-configure-windows 'newsgroups t))))
4788           ;; Hide conversation thread subtrees.  We cannot do this in
4789           ;; gnus-summary-prepare-hook since kill processing may not
4790           ;; work with hidden articles.
4791           (and gnus-show-threads
4792                gnus-thread-hide-subtree
4793                (gnus-summary-hide-all-threads))
4794           ;; Show first unread article if requested.
4795           (goto-char (point-min))
4796           (if (and (not no-article)
4797                    gnus-auto-select-first
4798                    (gnus-summary-first-unread-article))
4799               (gnus-configure-windows 'article)
4800             (gnus-configure-windows 'summary))
4801           (pop-to-buffer gnus-summary-buffer)
4802           (gnus-set-mode-line 'summary)
4803           (gnus-summary-position-cursor)
4804           (if (and kill-buffer
4805                    (get-buffer kill-buffer)
4806                    (buffer-name (get-buffer kill-buffer)))
4807               (kill-buffer kill-buffer))))
4808     ;; Cannot select newsgroup GROUP.
4809     (message "Couldn't select newsgroup")
4810     (and (eq major-mode 'gnus-summary-mode)
4811          (kill-buffer (current-buffer)))
4812     (switch-to-buffer gnus-group-buffer)))
4813
4814 (defun gnus-summary-prepare ()
4815   "Prepare summary list of current newsgroup in summary buffer."
4816   (let ((buffer-read-only nil))
4817     (erase-buffer)
4818     (gnus-summary-prepare-threads 
4819      (if gnus-show-threads
4820          (gnus-gather-threads 
4821           (gnus-sort-threads 
4822            (if gnus-summary-expunge-below
4823                (gnus-make-threads-and-expunge)
4824              (gnus-make-threads))))
4825        gnus-newsgroup-headers)
4826      0 nil nil t)
4827     ;; Erase header retrieval message.
4828     (gnus-summary-update-lines)
4829     (message "")
4830     ;; Call hooks for modifying summary buffer.
4831     ;; Suggested by sven@tde.LTH.Se (Sven Mattisson).
4832     (goto-char (point-min))
4833     (run-hooks 'gnus-summary-prepare-hook)))
4834
4835 (defun gnus-gather-threads (threads)
4836   "Gather threads that have lost their roots."
4837   (if (not gnus-summary-make-false-root)
4838       threads 
4839     (let ((hashtb (gnus-make-hashtable 1023))
4840           (prev threads)
4841           (result threads)
4842           thread subject hthread unre-subject whole-subject)
4843       (while threads
4844         (setq subject (header-subject (car (car threads)))
4845               whole-subject subject)
4846         (and gnus-summary-gather-subject-limit
4847              (> (length subject) gnus-summary-gather-subject-limit)
4848              (setq subject
4849                    (substring subject 0 gnus-summary-gather-subject-limit)))
4850         (if (setq hthread 
4851                   (gnus-gethash 
4852                    (setq unre-subject (gnus-simplify-subject-re subject))
4853                    hashtb))
4854             (progn
4855               (or (stringp (car (car hthread)))
4856                   (setcar hthread (list whole-subject (car hthread))))
4857               (setcdr (car hthread) (nconc (cdr (car hthread)) 
4858                                            (list (car threads))))
4859               (setcdr prev (cdr threads))
4860               (setq threads prev))
4861           (gnus-sethash unre-subject threads hashtb))
4862         (setq prev threads)
4863         (setq threads (cdr threads)))
4864       result)))
4865
4866 (defun gnus-make-threads ()
4867   ;; This function takes the dependencies already made by 
4868   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
4869   ;; through the dependecies in the hash table and finds all the
4870   ;; roots. Roots do not refer back to any valid articles.
4871   (let (roots)
4872     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
4873          (gnus-build-old-threads))
4874     (mapatoms
4875      (lambda (refs)
4876        (if (not (car (symbol-value refs)))
4877            (setq roots (append (cdr (symbol-value refs)) roots))
4878          ;; Ok, these refer back to valid articles, but if
4879          ;; `gnus-thread-ignore-subject' is nil, we have to check that
4880          ;; the root has the same subject as its children. The children
4881          ;; that do not are made into roots and removed from the list
4882          ;; of children. 
4883          (or gnus-thread-ignore-subject
4884              (let* ((prev (symbol-value refs))
4885                     (subject (gnus-simplify-subject-re 
4886                               (header-subject (car prev))))
4887                     (headers (cdr prev)))
4888                (while headers
4889                  (if (not (string= subject
4890                                    (gnus-simplify-subject-re 
4891                                     (header-subject (car headers)))))
4892                      (progn
4893                        (setq roots (cons (car headers) roots))
4894                        (setcdr prev (cdr headers)))
4895                    (setq prev headers))
4896                  (setq headers (cdr headers)))))))
4897      gnus-newsgroup-dependencies)
4898     
4899     (mapcar 'gnus-trim-thread
4900             (apply 'append
4901                    (mapcar 'gnus-cut-thread
4902                            (mapcar 'gnus-make-sub-thread roots))))))
4903   
4904 (defun gnus-make-threads-and-expunge ()
4905   ;; This function takes the dependencies already made by 
4906   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
4907   ;; through the dependecies in the hash table and finds all the
4908   ;; roots. Roots do not refer back to any valid articles.
4909   (let (roots)
4910     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
4911          (gnus-build-old-threads))
4912     (mapatoms
4913      (lambda (refs)
4914        (if (not (car (symbol-value refs)))
4915            (if (and gnus-summary-expunge-below
4916                     (not gnus-fetch-old-headers))
4917                (let ((headers (cdr (symbol-value refs))))
4918                  (while headers
4919                   (if (not (< (or (cdr (assq (header-number (car headers))
4920                                              gnus-newsgroup-scored))
4921                                   gnus-summary-default-score 0)
4922                               gnus-summary-expunge-below))
4923                       (setq roots (cons (car headers) roots))
4924                     (setq gnus-newsgroup-unreads
4925                           (delq (header-number (car headers))
4926                                 gnus-newsgroup-unreads)))
4927                   (setq headers (cdr headers))))
4928              (setq roots (append (cdr (symbol-value refs)) roots)))
4929          ;; Ok, these refer back to valid articles, but if
4930          ;; `gnus-thread-ignore-subject' is nil, we have to check that
4931          ;; the root has the same subject as its children. The children
4932          ;; that do not are made into roots and removed from the list
4933          ;; of children. 
4934          (or gnus-thread-ignore-subject
4935              (let* ((prev (symbol-value refs))
4936                     (subject (gnus-simplify-subject-re 
4937                               (header-subject (car prev))))
4938                     (headers (cdr prev)))
4939                (while headers
4940                  (if (not (string= subject
4941                                    (gnus-simplify-subject-re 
4942                                     (header-subject (car headers)))))
4943                      (progn
4944                        (setq roots (cons (car headers) roots))
4945                        (setcdr prev (cdr headers)))
4946                    (setq prev headers))
4947                  (setq headers (cdr headers)))))
4948          (and gnus-summary-expunge-below
4949               (not gnus-fetch-old-headers)
4950               (let* ((prev (symbol-value refs))
4951                      (headers (cdr prev))
4952                      id)
4953                 (while headers
4954                   (if (not (< (or (cdr (assq (header-number (car headers))
4955                                              gnus-newsgroup-scored))
4956                                   gnus-summary-default-score 0)
4957                               gnus-summary-expunge-below))
4958                       (setq prev (cdr prev))
4959                     (setq gnus-newsgroup-unreads 
4960                           (delq (header-number (car headers))
4961                                 gnus-newsgroup-unreads))
4962                     (setcdr prev (cdr headers))
4963                     (setq id (gnus-gethash (header-id (car headers))
4964                                            gnus-newsgroup-dependencies))
4965                     (let ((h (cdr id)))
4966                       (while h
4967                         (if (not (< (or (cdr (assq (header-number (car h))
4968                                                    gnus-newsgroup-scored))
4969                                         gnus-summary-default-score 0)
4970                                     gnus-summary-expunge-below))
4971                             (setq roots (cons (car h) roots)))
4972                         (setq h (cdr h)))))
4973                   (setq headers (cdr headers)))))))
4974      gnus-newsgroup-dependencies)
4975     
4976     (mapcar 'gnus-trim-thread
4977             (apply 'append
4978                    (mapcar 'gnus-cut-thread
4979                            (mapcar 'gnus-make-sub-thread roots))))))
4980   
4981 (defun gnus-cut-thread (thread)
4982   ;; Remove leaf dormant or ancient articles from THREAD.
4983   (let ((head (car thread))
4984         (tail (apply 'append (mapcar 'gnus-cut-thread (cdr thread)))))
4985     (if (and (null tail)
4986              (let ((number (header-number head)))
4987                (or (memq number gnus-newsgroup-ancient)
4988                    (memq number gnus-newsgroup-dormant)
4989                    (and gnus-summary-expunge-below
4990                         (eq gnus-fetch-old-headers 'some)
4991                         (< (or (cdr (assq number gnus-newsgroup-scored))
4992                                gnus-summary-default-score 0)
4993                            gnus-summary-expunge-below)
4994                         (progn
4995                           (setq gnus-newsgroup-unreads
4996                                 (delq number gnus-newsgroup-unreads))
4997                           t)))))
4998         nil
4999       (list (cons head tail)))))
5000
5001 (defun gnus-trim-thread (thread)
5002   ;; Remove root ancient articles with only one child from THREAD.
5003   (if (and (eq gnus-fetch-old-headers 'some)
5004            (memq (header-number (car thread)) gnus-newsgroup-ancient)
5005            (= (length thread) 2))
5006       (gnus-trim-thread (nth 1 thread))
5007     thread))
5008
5009 (defun gnus-make-sub-thread (root)
5010   ;; This function makes a sub-tree for a node in the tree.
5011   (let ((children (reverse (cdr (gnus-gethash (downcase (header-id root))
5012                                               gnus-newsgroup-dependencies)))))
5013     (cons root (mapcar 'gnus-make-sub-thread children))))
5014
5015 (defun gnus-build-old-threads ()
5016   ;; Look at all the articles that refer back to old articles, and
5017   ;; fetch the headers for the articles that aren't there. This will
5018   ;; build complete threads - if the roots haven't been expired by the
5019   ;; server, that is.
5020   (let (id heads)
5021     (mapatoms
5022      (lambda (refs)
5023        (if (not (car (symbol-value refs)))
5024            (progn
5025              (setq heads (cdr (symbol-value refs)))
5026              (while heads
5027                (if (not (memq (header-number (car heads))
5028                               gnus-newsgroup-dormant))
5029                    (progn
5030                      (setq id (symbol-name refs))
5031                      (while (and (setq id (gnus-build-get-header id))
5032                                  (not (car (gnus-gethash 
5033                                             id gnus-newsgroup-dependencies)))))
5034                      (setq heads nil))
5035                  (setq heads (cdr heads)))))))
5036      gnus-newsgroup-dependencies)))
5037
5038 (defun gnus-build-get-header (id)
5039   ;; Look through the buffer of NOV lines and find the header to
5040   ;; ID. Enter this line into the dependencies hash table, and return
5041   ;; the id of the parent article (if any).
5042   (let ((deps gnus-newsgroup-dependencies)
5043         found header)
5044     (prog1
5045         (save-excursion
5046           (set-buffer nntp-server-buffer)
5047           (goto-char (point-min))
5048           (while (and (not found) (search-forward id nil t))
5049             (beginning-of-line)
5050             (setq found (looking-at (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
5051                                             (regexp-quote id))))
5052             (or found (beginning-of-line 2)))
5053           (if found
5054               (let (ref)
5055                 (beginning-of-line)
5056                 (and
5057                  (setq header (gnus-nov-parse-line 
5058                                (read (current-buffer)) deps))
5059                  (setq ref (header-references header))
5060                  (string-match "\\(<[^>]+>\\) *$" ref)
5061                  (substring ref (match-beginning 1) (match-end 1))))))
5062       (and header
5063            (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers)
5064                  gnus-newsgroup-ancient (cons (header-number header)
5065                                               gnus-newsgroup-ancient))))))
5066
5067 (defun gnus-sort-threads (threads)
5068   ;; Sort threads as specified in `gnus-thread-sort-functions'.
5069   (let ((fun gnus-thread-sort-functions))
5070     (while fun
5071       (setq threads (sort threads (car fun))
5072             fun (cdr fun))))
5073   threads)
5074
5075 (defun gnus-thread-header (thread)
5076   ;; Return header of first article in THREAD.
5077   (if (consp thread)
5078       (if (stringp (car thread))
5079           (car (car (cdr thread)))
5080         (car thread))
5081     thread))
5082
5083 (defun gnus-thread-sort-by-number (h1 h2)
5084   "Sort threads by root article number."
5085   (let ((h1 (gnus-thread-header h1))
5086         (h2 (gnus-thread-header h2)))
5087     (< (header-number h1) (header-number h2))))
5088
5089 (defun gnus-thread-sort-by-author (h1 h2)
5090   "Sort threads by root author."
5091   (let ((h1 (gnus-thread-header h1))
5092         (h2 (gnus-thread-header h2)))
5093     (string-lessp
5094      (let ((extract (funcall 
5095                      gnus-extract-address-components (header-from h1))))
5096        (or (car extract) (cdr extract)))
5097      (let ((extract (funcall
5098                      gnus-extract-address-components (header-from h2))))
5099        (or (car extract) (cdr extract))))))
5100
5101 (defun gnus-thread-sort-by-subject (h1 h2)
5102   "Sort threads by root subject."
5103   (let ((h1 (gnus-thread-header h1))
5104         (h2 (gnus-thread-header h2)))
5105     (string-lessp
5106      (downcase (gnus-simplify-subject (header-subject h1)))
5107      (downcase (gnus-simplify-subject (header-subject h2))))))
5108
5109 (defun gnus-thread-sort-by-date (h1 h2)
5110   "Sort threads by root article date."
5111   (let ((h1 (gnus-thread-header h1))
5112         (h2 (gnus-thread-header h2)))
5113     (string-lessp
5114      (gnus-sortable-date (header-date h1))
5115      (gnus-sortable-date (header-date h2)))))
5116
5117 (defun gnus-thread-sort-by-score (h1 h2)
5118   "Sort threads by root article score.
5119 Unscored articles will be counted as havin a score of zero."
5120   (let ((h1 (gnus-thread-header h1))
5121         (h2 (gnus-thread-header h2)))
5122     (let ((s1 (assq (header-number h1) gnus-newsgroup-scored))
5123           (s2 (assq (header-number h2) gnus-newsgroup-scored)))
5124       (> (or (cdr s1) gnus-summary-default-score 0)
5125          (or (cdr s2) gnus-summary-default-score 0)))))
5126
5127 (defun gnus-thread-sort-by-total-score (h1 h2)
5128   "Sort threads by the sum of all scores in the thread.
5129 Unscored articles will be counted as havin a score of zero."
5130   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
5131
5132 (defun gnus-thread-total-score (thread)
5133   ;;  This function find the total score of  THREAD.
5134   (if (consp thread)
5135       (if (stringp (car thread))
5136           (apply gnus-thread-score-function 0
5137                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
5138         (gnus-thread-total-score-1 thread))
5139     (gnus-thread-total-score-1 (list thread))))
5140
5141 (defun gnus-thread-total-score-1 (root)
5142   ;; This function find the total score of the thread below ROOT.
5143   (setq root (car root))
5144   (apply gnus-thread-score-function
5145          (or (cdr (assq (header-number root) gnus-newsgroup-scored))
5146              gnus-summary-default-score 0)
5147          (mapcar 'gnus-thread-total-score
5148                  (cdr (gnus-gethash (downcase (header-id root))
5149                                     gnus-newsgroup-dependencies)))))
5150
5151 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5152 (defvar gnus-tmp-prev-subject "")
5153
5154 ;; Basic ideas by Paul Dworkin <paul@media-lab.media.mit.edu>.
5155 (defun gnus-summary-prepare-threads 
5156   (threads level &optional not-child no-subject cull)
5157   "Prepare summary buffer from THREADS and indentation LEVEL.  
5158 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
5159 or a straight list of headers."
5160   (let (thread header number subject clevel)
5161     (while threads
5162       (setq thread (car threads)
5163             threads (cdr threads))
5164       ;; If `thread' is a cons, hierarchical threads are used.  If not,
5165       ;; `thread' is the header.
5166       (if (consp thread)
5167           (setq header (car thread))
5168         (setq header thread)
5169         (and cull
5170              (or (memq (setq number (header-number header))
5171                        gnus-newsgroup-dormant)
5172                  (and gnus-summary-expunge-below
5173                       (< (or (cdr (assq number gnus-newsgroup-scored))
5174                              gnus-summary-default-score 0)
5175                          gnus-summary-expunge-below)))
5176              (progn
5177                (setq header nil)
5178                (setq gnus-newsgroup-unreads 
5179                      (delq number gnus-newsgroup-unreads)))))
5180       (cond 
5181        ((stringp header)
5182         ;; The header is a dummy root.
5183         (cond ((eq gnus-summary-make-false-root 'adopt)
5184                ;; We let the first article adopt the rest.
5185                (gnus-summary-prepare-threads (list (car (cdr thread))) 0)
5186                (setq thread (cdr (cdr thread)))
5187                (while thread
5188                  (gnus-summary-prepare-threads (list (car thread)) 1 t)
5189                  (setq thread (cdr thread))))
5190               ((eq gnus-summary-make-false-root 'dummy)
5191                ;; We output a dummy root.
5192                (gnus-summary-insert-dummy-line 
5193                 nil header (header-number (car (car (cdr thread)))))
5194                (setq clevel 1))
5195               ((eq gnus-summary-make-false-root 'empty)
5196                ;; We print the articles with empty subject fields. 
5197                (gnus-summary-prepare-threads (list (car (cdr thread))) 0)
5198                (setq thread (cdr (cdr thread)))
5199                (while thread
5200                  (gnus-summary-prepare-threads (list (car thread)) 0 nil t)
5201                  (setq thread (cdr thread))))
5202               (t
5203                ;; We do not make a root for the gathered
5204                ;; sub-threads at all.  
5205                (setq clevel 0)))
5206         ;; Print the sub-threads.
5207         (and (consp thread) (cdr thread)
5208              (gnus-summary-prepare-threads (cdr thread) clevel)))
5209        ;; The header is a real article.
5210        (header
5211         (setq number (header-number header)
5212               subject (header-subject header))
5213         (gnus-summary-insert-line
5214          nil header level nil 
5215          (cond ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
5216                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
5217                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
5218                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
5219                (t gnus-ancient-mark))
5220          (memq number gnus-newsgroup-replied)
5221          (memq number gnus-newsgroup-expirable)
5222          (if no-subject gnus-summary-same-subject
5223            (if (or (zerop level)
5224                    (and gnus-thread-ignore-subject
5225                         (not (string= 
5226                               (gnus-simplify-subject-re gnus-tmp-prev-subject)
5227                               (gnus-simplify-subject-re subject)))))
5228                subject
5229              gnus-summary-same-subject))
5230          not-child
5231          (cdr (assq number gnus-newsgroup-scored)))
5232         (setq gnus-tmp-prev-subject subject)
5233         ;; Recursively print subthreads.
5234         (and (consp thread) (cdr thread)
5235              (gnus-summary-prepare-threads (cdr thread) (1+ level))))))))
5236
5237 (defun gnus-select-newsgroup (group &optional read-all)
5238   "Select newsgroup GROUP.
5239 If READ-ALL is non-nil, all articles in the group are selected."
5240   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5241          (info (nth 2 entry))
5242          articles header-marks)
5243     (and (eq (car entry) t)
5244          (or (gnus-activate-newsgroup (car info))
5245              (progn
5246                (kill-buffer (current-buffer))
5247                (error "Couldn't request group %s: %s" 
5248                       group (gnus-status-message group)))))
5249     (setq gnus-current-select-method (or (nth 4 info) gnus-select-method))
5250     (gnus-check-news-server (nth 4 info))
5251     (or (gnus-request-group group t)
5252         (progn
5253           (kill-buffer (current-buffer))
5254           (error "Couldn't request group %s: %s" 
5255                  group (gnus-status-message group))))
5256
5257     (setq gnus-newsgroup-name group)
5258     (setq gnus-newsgroup-unselected nil)
5259     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5260
5261     (and info
5262          (let (marked)
5263            (gnus-adjust-marked-articles info)
5264            (setq gnus-newsgroup-marked 
5265                  (cdr (assq 'tick (setq marked (nth 3 info)))))
5266            (setq gnus-newsgroup-replied (cdr (assq 'reply marked)))
5267            (setq gnus-newsgroup-expirable (cdr (assq 'expire marked)))
5268            (setq gnus-newsgroup-killed (cdr (assq 'killed marked)))
5269            (setq gnus-newsgroup-bookmarks (cdr (assq 'bookmark marked)))
5270            (setq gnus-newsgroup-dormant (cdr (assq 'dormant marked)))
5271            (setq gnus-newsgroup-scored (cdr (assq 'score marked)))
5272            (setq gnus-newsgroup-processable nil)))
5273
5274     (if (not (setq articles (gnus-articles-to-read group read-all)))
5275         nil
5276       ;; Init the dependencies hash table.
5277       (setq gnus-newsgroup-dependencies 
5278             (gnus-make-hashtable (length articles)))
5279       ;; Retrieve the headers and read them in.
5280       (setq gnus-newsgroup-headers 
5281             (if (eq 'nov (setq gnus-headers-retrieved-by
5282                                (gnus-retrieve-headers 
5283                                 (if gnus-fetch-old-headers 
5284                                     (cons 1 articles) articles) 
5285                                 gnus-newsgroup-name)))
5286                 (progn
5287                   (gnus-get-newsgroup-headers-xover articles))
5288               (gnus-get-newsgroup-headers)))
5289       ;; If we were to fetch old headers, but the backend didn't
5290       ;; support XOVER, then it is possible we fetched one article
5291       ;; that we shouldn't have. If that's the case, we pop it off the
5292       ;; list of headers.
5293       (and (not (eq gnus-headers-retrieved-by 'nov))
5294            gnus-fetch-old-headers
5295            gnus-newsgroup-headers
5296            (/= (header-number (car gnus-newsgroup-headers)) (car articles))
5297            (setq gnus-newsgroup-headers (cdr gnus-newsgroup-headers)))
5298       ;; Remove cancelled articles from the list of unread articles.
5299       (setq gnus-newsgroup-unreads
5300             (gnus-set-sorted-intersection 
5301              gnus-newsgroup-unreads
5302              (mapcar (lambda (headers) (header-number headers))
5303                      gnus-newsgroup-headers)))
5304       ;; Check whether auto-expire is to be done in this group.
5305       (setq gnus-newsgroup-auto-expire
5306             (or (and (stringp gnus-auto-expirable-newsgroups)
5307                      (string-match gnus-auto-expirable-newsgroups group))
5308                 (memq 'auto-expire gnus-current-select-method)))
5309       ;; First and last article in this newsgroup.
5310       (and gnus-newsgroup-headers
5311            (setq gnus-newsgroup-begin 
5312                  (header-number (car gnus-newsgroup-headers)))
5313            (setq gnus-newsgroup-end
5314                  (header-number (gnus-last-element gnus-newsgroup-headers))))
5315       (setq gnus-reffed-article-number -1)
5316       ;; GROUP is successfully selected.
5317       (or gnus-newsgroup-headers t))))
5318
5319 (defun gnus-articles-to-read (group read-all)
5320   ;; Find out what articles the user wants to read.
5321   (let* ((articles
5322           ;; Select all articles if `read-all' is non-nil, or if all the
5323           ;; unread articles are dormant articles.
5324           (if (or read-all
5325                   (= (length gnus-newsgroup-unreads) 
5326                      (length gnus-newsgroup-dormant)))
5327               (gnus-uncompress-range 
5328                (gnus-gethash group gnus-active-hashtb))
5329             gnus-newsgroup-unreads))
5330          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5331          (scored (length scored-list))
5332          (number (length articles))
5333          (marked (+ (length gnus-newsgroup-marked)
5334                     (length gnus-newsgroup-dormant)))
5335          (select
5336           (condition-case ()
5337               (cond ((and (or (<= scored marked)
5338                               (= scored number))
5339                           (numberp gnus-large-newsgroup)
5340                           (> number gnus-large-newsgroup))
5341                      (let ((input
5342                             (read-string
5343                              (format
5344                               "How many articles from %s (default %d): "
5345                               gnus-newsgroup-name number))))
5346                        (if (string-equal input "")
5347                            number input)))
5348                     ((and (> scored marked) (< scored number))
5349                      (let ((input
5350                             (read-string
5351                              (format 
5352                               "%s %s (%d scored, %d total): "
5353                               "How many articles from"
5354                               group scored number))))
5355                        (if (string-equal input "")
5356                            number input)))
5357                     (t number))
5358             (quit 0)))
5359          total-articles)
5360     (setq select (if (numberp select) select (string-to-number select)))
5361     (if (zerop select)
5362         ()
5363       (if (and (not (zerop scored)) (<= (abs select) scored))
5364           (progn
5365             (setq articles (sort scored-list '<))
5366             (setq number (length articles)))
5367         (setq articles (copy-sequence articles)))
5368
5369       (setq total-articles articles)
5370       
5371       (if (< (abs select) number)
5372           (if (< select 0) 
5373               ;; Select the N oldest articles.
5374               (setcdr (nthcdr (1- (abs select)) articles) nil)
5375             ;; Select the N most recent articles.
5376             (setq articles (nthcdr (- number select) articles))))
5377       (setq gnus-newsgroup-unselected
5378             (gnus-sorted-intersection
5379              gnus-newsgroup-unselected 
5380              (gnus-sorted-complement articles total-articles)))
5381       articles)))
5382
5383 (defun gnus-killed-articles (killed articles)
5384   (let (out)
5385     (while articles
5386       (if (inline (gnus-member-of-range (car articles) killed))
5387           (setq out (cons (car articles) out)))
5388       (setq articles (cdr articles)))
5389     out))
5390
5391 (defun gnus-adjust-marked-articles (info &optional active)
5392   "Remove all marked articles that are no longer legal."
5393   (let ((marked-lists (nth 3 info))
5394         (active (or active (gnus-gethash (car info) gnus-active-hashtb)))
5395         marked m prev)
5396     ;; There are four types of marked articles - ticked, replied,
5397     ;; expirable and dormant.  
5398     (while marked-lists
5399       (setq m (cdr (setq prev (car marked-lists))))
5400       (cond ((or (eq 'tick (car prev)) (eq 'dormant (car prev)))
5401              ;; Make sure that all ticked articles are a subset of the
5402              ;; unread/unselected articles.
5403              (while m
5404                (if (or (memq (car m) gnus-newsgroup-unreads)
5405                        (memq (car m) gnus-newsgroup-unselected))
5406                    (setq prev m)
5407                  (setcdr prev (cdr m)))
5408                (setq m (cdr m))))
5409             ((eq 'score (car prev))
5410              ;; Scored articles should be a subset of
5411              ;; unread/unselected articles. 
5412              (while m
5413                (if (or (memq (car (car m)) gnus-newsgroup-unreads)
5414                        (memq (car (car m)) gnus-newsgroup-unreads))
5415                    (setq prev m)
5416                  (setcdr prev (cdr m)))
5417                (setq m (cdr m))))
5418             ((eq 'bookmark (car prev))
5419              ;; Bookmarks should be a subset of active articles.
5420              (while m
5421                (if (< (car (car m)) (car active))
5422                    (setcdr prev (cdr m))
5423                  (setq prev m))
5424                (setq m (cdr m))))
5425             ((eq 'killed (car prev))
5426              ;; Articles that have been through the kill process are
5427              ;; to be a subset of active articles.
5428              (while (and m (< (or (and (numberp (car m)) (car m))
5429                                   (cdr (car m)))
5430                               (car active)))
5431                (setcdr prev (cdr m))
5432                (setq m (cdr m)))
5433              (if (and m (< (or (and (numberp (car m)) (car m))
5434                                (car (car m)))
5435                            (car active))) 
5436                  (setcar (and (numberp (car m)) m (car m)) (car active))))
5437             ((or (eq 'reply (car marked)) (eq 'expire (car marked)))
5438              ;; The replied and expirable articles have to be articles
5439              ;; that are active. 
5440              (while m
5441                (if (< (car m) (car active))
5442                    (setcdr prev (cdr m))
5443                  (setq prev m))
5444                (setq m (cdr m)))))
5445       (setq marked-lists (cdr marked-lists)))
5446     ;; Remove all lists that are empty.
5447     (setq marked-lists (nth 3 info))
5448     (if marked-lists
5449         (progn
5450           (while (= 1 (length (car marked-lists)))
5451             (setq marked-lists (cdr marked-lists)))
5452           (setq m (cdr (setq prev marked-lists)))
5453           (while m
5454             (if (= 1 (length (car m)))
5455                 (setcdr prev (cdr m))
5456               (setq prev m))
5457             (setq m (cdr m)))
5458           (setcar (nthcdr 3 info) marked-lists)))
5459     ;; Finally, if there are no marked lists at all left, and if there
5460     ;; are no elements after the lists in the info list, we just chop
5461     ;; the info list off before the marked lists.
5462     (if (and (null marked-lists) (not (nthcdr 4 info)))
5463         (setcdr (nthcdr 2 info) nil)))
5464   info)
5465
5466 (defun gnus-set-marked-articles 
5467   (info ticked replied expirable killed dormant bookmark score) 
5468   "Enter the various lists of marked articles into the newsgroup info list."
5469   (let (newmarked)
5470     (and ticked (setq newmarked (cons (cons 'tick ticked) nil)))
5471     (and replied (setq newmarked (cons (cons 'reply replied) newmarked)))
5472     (and expirable (setq newmarked (cons (cons 'expire expirable) 
5473                                          newmarked)))
5474     (and killed (setq newmarked (cons (cons 'killed killed) newmarked)))
5475     (and dormant (setq newmarked (cons (cons 'dormant dormant) newmarked)))
5476     (and bookmark (setq newmarked (cons (cons 'bookmark bookmark) 
5477                                         newmarked)))
5478     (and score (setq newmarked (cons (cons 'score score) newmarked)))
5479     (if (nthcdr 3 info)
5480         (if newmarked
5481             (setcar (nthcdr 3 info) newmarked)
5482           (if (not (nthcdr 4 info))
5483               (setcdr (nthcdr 2 info) nil)
5484             (setcar (nthcdr 3 info) nil)))
5485       (if newmarked
5486           (setcdr (nthcdr 2 info) (cons newmarked nil))))))
5487
5488 (defun gnus-add-marked-articles (group type articles &optional info force)
5489   ;; Add ARTICLES of TYPE to the info of GROUP.
5490   ;; If INFO is non-nil, use that info. If FORCE is non-nil, don't
5491   ;; add, but replace marked articles of TYPE with ARTICLES.
5492   (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
5493         marked m)
5494     (or (not info)
5495         (and (not (setq marked (nthcdr 3 info)))
5496              (setcdr (nthcdr 2 info) (list (list (cons type articles)))))
5497         (and (not (setq m (assq type (car marked))))
5498              (setcar marked (cons (cons type articles) (car marked))))
5499         (if force
5500             (setcdr m articles)
5501           (nconc m articles)))))
5502          
5503 (defun gnus-set-mode-line (where)
5504   "This function sets the mode line of the article or summary buffers.
5505 If WHERE is `summary', the summary mode line format will be used."
5506   (if (memq where gnus-updated-mode-lines)
5507       (let (mode-string)
5508         (save-excursion
5509           (set-buffer gnus-summary-buffer)
5510           (let* ((mformat (if (eq where 'article) 
5511                               gnus-article-mode-line-format-spec
5512                             gnus-summary-mode-line-format-spec))
5513                  (group-name gnus-newsgroup-name)
5514                  (article-number (or gnus-current-article 0))
5515                  (unread (- (length gnus-newsgroup-unreads)
5516                             (length gnus-newsgroup-dormant)))
5517                  (unread-and-unticked 
5518                   (- unread (length gnus-newsgroup-marked)))
5519                  (unselected (length gnus-newsgroup-unselected))
5520                  (unread-and-unselected
5521                   (cond ((and (zerop unread-and-unticked)
5522                               (zerop unselected)) "")
5523                         ((zerop unselected) 
5524                          (format "{%d more}" unread-and-unticked))
5525                         (t (format "{%d(+%d) more}"
5526                                    unread-and-unticked unselected))))
5527                  (subject
5528                   (if gnus-current-headers
5529                       (header-subject gnus-current-headers) ""))
5530                  (max-len (- (frame-width) gnus-mode-non-string-length)))
5531             (setq mode-string (eval mformat))
5532             (if (> (length mode-string) max-len) 
5533                 (setq mode-string 
5534                       (concat (substring mode-string 0 (- max-len 3)) "...")))
5535             (setq mode-string (format (format "%%-%ds" max-len 5)
5536                                       mode-string))))
5537         (setq mode-line-buffer-identification mode-string)
5538         (set-buffer-modified-p t))))
5539
5540 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5541   "Go through the HEADERS list and add all Xrefs to a hash table.
5542 The resulting hash table is returned, or nil if no Xrefs were found."
5543   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
5544          (prefix (if (and 
5545                       (gnus-group-foreign-p from-newsgroup)
5546                       (not (memq 'virtual 
5547                                  (assoc (symbol-name (car from-method))
5548                                         gnus-valid-select-methods))))
5549                      (gnus-group-real-prefix from-newsgroup)))
5550          (xref-hashtb (make-vector 63 0))
5551          start group entry number xrefs header)
5552     (while headers
5553       (setq header (car headers))
5554       (if (and (setq xrefs (header-xref header))
5555                (not (memq (header-number header) unreads)))
5556           (progn
5557             (setq start 0)
5558             (while (string-match "\\([^ ]+\\):\\([0-9]+\\)" xrefs start)
5559               (setq start (match-end 0))
5560               (setq group (concat prefix (substring xrefs (match-beginning 1) 
5561                                                     (match-end 1))))
5562               (setq number 
5563                     (string-to-int (substring xrefs (match-beginning 2) 
5564                                               (match-end 2))))
5565               (if (setq entry (gnus-gethash group xref-hashtb))
5566                   (setcdr entry (cons number (cdr entry)))
5567                 (gnus-sethash group (cons number nil) xref-hashtb)))))
5568       (setq headers (cdr headers)))
5569     (if start xref-hashtb nil)))
5570
5571 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads expirable)
5572   "Look through all the headers and mark the Xrefs as read."
5573   (let ((virtual (memq 'virtual 
5574                        (assoc (symbol-name (car (gnus-find-method-for-group 
5575                                                  from-newsgroup)))
5576                               gnus-valid-select-methods)))
5577         name entry read info xref-hashtb idlist active num range exps method)
5578     (save-excursion
5579       (set-buffer gnus-group-buffer)
5580       (if (setq xref-hashtb 
5581                 (gnus-create-xref-hashtb from-newsgroup headers unreads))
5582           (mapatoms 
5583            (lambda (group)
5584              (if (string= from-newsgroup (setq name (symbol-name group)))
5585                  ()
5586                (setq idlist (symbol-value group))
5587                ;; Dead groups are not updated.
5588                (if (and (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5589                               info (nth 2 entry))
5590                         ;; Only do the xrefs if the group has the same
5591                         ;; select method as the group we have just read.
5592                         (or (gnus-methods-equal-p 
5593                              (nth 4 info)
5594                              (gnus-find-method-for-group from-newsgroup))
5595                             virtual
5596                             (equal (nth 4 info) 
5597                                    (setq method (gnus-find-method-for-group 
5598                                                  from-newsgroup)))
5599                             (and (equal (car (nth 4 info)) (car method))
5600                                  (equal (nth 1 (nth 4 info)) (nth 1 method))))
5601                         gnus-use-cross-reference
5602                         (or (not (eq gnus-use-cross-reference t))
5603                             virtual
5604                             ;; Only do cross-references on subscribed
5605                             ;; groups, if that is what is wanted.  
5606                             (<= (nth 1 info) 5)))
5607                    (progn
5608                      (setq num 0)
5609                      ;; Set the new list of read articles in this group.
5610                      (setq active (gnus-gethash name gnus-active-hashtb))
5611                      ;; First peel off all illegal article numbers.
5612                      (if active
5613                          (let ((ids idlist)
5614                                (ticked (cdr (assq 'tick (nth 3 info))))
5615                                (dormant (cdr (assq 'dormant (nth 3 info))))
5616                                id)
5617                            (setq exps nil)
5618                            (while ids
5619                              (setq id (car ids))
5620                              (if (or (> id (cdr active))
5621                                      (< id (car active))
5622                                      (memq id ticked)
5623                                      (memq id dormant))
5624                                  (setq idlist (delq id idlist)))
5625                              (and (memq id expirable)
5626                                   (setq exps (cons id exps)))
5627                              (setq ids (cdr ids)))))
5628                      ;; Update expirable articles.
5629                      (gnus-add-marked-articles nil 'expirable exps info)
5630                      (and (null (nth 2 info))
5631                           (> (car active) 1)
5632                           (setcar (nthcdr 2 info) (cons 1 (1- (car active)))))
5633                      (setcar (nthcdr 2 info)
5634                              (setq range
5635                                    (gnus-add-to-range 
5636                                     (nth 2 info) 
5637                                     (setq idlist (sort idlist '<)))))
5638                      ;; Then we have to re-compute how many unread
5639                      ;; articles there are in this group.
5640                      (if active
5641                          (progn
5642                            (cond 
5643                             ((not range)
5644                              (setq num (- (1+ (cdr active)) (car active))))
5645                             ((not (listp (cdr range)))
5646                              (setq num (- (cdr active) (- (1+ (cdr range)) 
5647                                                           (car range)))))
5648                             (t
5649                              (while range
5650                                (if (numberp (car range))
5651                                    (setq num (1+ num))
5652                                  (setq num (+ num (- (1+ (cdr (car range)))
5653                                                      (car (car range))))))
5654                                (setq range (cdr range)))
5655                              (setq num (- (cdr active) num))))
5656                            ;; Update the number of unread articles.
5657                            (setcar 
5658                             entry 
5659                             (max 0 (- num 
5660                                       (length (cdr (assq 'tick (nth 3 info))))
5661                                       (length 
5662                                        (cdr (assq 'dormant (nth 3 info)))))))
5663                            ;; Update the group buffer.
5664                            (gnus-group-update-group name t)))))))
5665            xref-hashtb)))))
5666
5667 (defun gnus-methods-equal-p (m1 m2)
5668   (let ((m1 (or m1 gnus-select-method))
5669         (m2 (or m2 gnus-select-method)))
5670     (or (equal m1 m2)
5671         (and (eq (car m1) (car m2))
5672              (or (not (memq 'address (assoc (symbol-name (car m1))
5673                                             gnus-valid-select-methods)))
5674                  (equal (nth 1 m1) (nth 1 m2)))))))
5675
5676 (defsubst gnus-header-value ()
5677   (buffer-substring (match-end 0) (save-excursion (end-of-line) (point))))
5678
5679 (defun gnus-get-newsgroup-headers ()
5680   (setq gnus-article-internal-prepare-hook nil)
5681   (let ((cur nntp-server-buffer)
5682         (dependencies gnus-newsgroup-dependencies)
5683         (none-id 0)
5684         headers char article id dep end)
5685     (save-excursion
5686       (set-buffer nntp-server-buffer)
5687       (goto-char 1)
5688       ;; Search to the beginning of the next header. Error messages
5689       ;; do not begin with 2 or 3.
5690       (while (re-search-forward "^[23][0-9]+ " nil t)
5691         (let ((header (make-vector 9 nil))
5692               (c (following-char))
5693               (case-fold-search t)
5694               (p (point))
5695               from subject in-reply-to references ref)
5696           (setq id nil
5697                 ref nil
5698                 references nil
5699                 subject nil
5700                 from nil)
5701           (header-set-number header (setq article (read cur)))
5702           ;; This implementation of this function, with nine
5703           ;; search-forwards instead of the one re-search-forward and
5704           ;; a case (which basically was the old function) is actually
5705           ;; about twice as fast, even though it looks messier. You
5706           ;; can't have everything, I guess. Speed and elegance
5707           ;; doesn't always come hand in hand.
5708           (save-restriction
5709             (narrow-to-region (point) (save-excursion 
5710                                         (search-forward "\n.\n" nil t)))
5711             (if (search-forward "\nfrom: " nil t)
5712                 (header-set-from header (gnus-header-value))
5713               (header-set-from header "(nobody)"))
5714             (goto-char p)
5715             (if (search-forward "\nsubject: " nil t)
5716                 (header-set-subject header (gnus-header-value))
5717               (header-set-subject header "(none)"))
5718             (goto-char p)
5719             (and (search-forward "\nxref: " nil t)
5720                  (header-set-xref header (gnus-header-value)))
5721             (goto-char p)
5722             (and (search-forward "\nlines: " nil t)
5723                  (header-set-lines header (read cur)))
5724             (goto-char p)
5725             (and (search-forward "\ndate: " nil t)
5726                  (header-set-date header (gnus-header-value)))
5727             (goto-char p)
5728             (if (search-forward "\nmessage-id: " nil t)
5729                 (header-set-id header (setq id (gnus-header-value)))
5730               ;; If there was no message-id, we just fake one to make
5731               ;; subsequent routines simpler.
5732               (header-set-id 
5733                header 
5734                (setq id (concat "none+" (int-to-string 
5735                                          (setq none-id (1+ none-id)))))))
5736             (goto-char p)
5737             (if (search-forward "\nreferences: " nil t)
5738                 (progn
5739                   (header-set-references header (gnus-header-value))
5740                   (setq end (match-end 0))
5741                   (save-excursion
5742                     (setq ref 
5743                           (downcase
5744                            (buffer-substring
5745                             (progn 
5746                               (end-of-line)
5747                               (search-backward ">" end t)
5748                               (1+ (point)))
5749                             (progn
5750                               (search-backward "<" end t)
5751                               (point)))))))
5752               ;; Get the references from the in-reply-to header if there
5753               ;; ware no references and the in-reply-to header looks
5754               ;; promising. 
5755               (if (and (search-forward "\nin-reply-to: " nil t)
5756                        (setq in-reply-to (gnus-header-value))
5757                        (string-match "<[^>]+>" in-reply-to))
5758                   (progn
5759                     (header-set-references 
5760                      header 
5761                      (setq ref (substring in-reply-to (match-beginning 0)
5762                                           (match-end 0))))
5763                     (setq ref (downcase ref)))
5764                 (setq ref "none")))
5765             ;; We do some threading while we read the headers. The
5766             ;; message-id and the last reference are both entered into
5767             ;; the same hash table. Some tippy-toeing around has to be
5768             ;; done in case an article has arrived before the article
5769             ;; which it refers to.
5770             (if (boundp (setq dep (intern (downcase id) dependencies)))
5771                 (if (car (symbol-value dep))
5772                     ;; An article with this Message-ID has already
5773                     ;; been seen, so we ignore this one, except we add
5774                     ;; any additional Xrefs (in case the two articles
5775                     ;; came from different servers.
5776                     (progn
5777                       (header-set-xref 
5778                        (car (symbol-value dep))
5779                        (concat (or (header-xref (car (symbol-value dep))) "")
5780                                (or (header-xref header) "")))
5781                       (setq header nil))
5782                   (setcar (symbol-value dep) header))
5783               (set dep (list header)))
5784             (if header
5785                 (progn
5786                   (if (boundp (setq dep (intern ref dependencies)))
5787                       (setcdr (symbol-value dep) 
5788                               (cons header (cdr (symbol-value dep))))
5789                     (set dep (list nil header)))
5790                   (setq headers (cons header headers))))
5791             (goto-char (point-max))))))
5792     (nreverse headers)))
5793
5794 ;; The following macros and functions were written by Felix Lee
5795 ;; <flee@cse.psu.edu>. 
5796
5797 ;; This is almost 4x faster than (string-to-int (buffer-substring ... ))
5798 ;; primarily because of garbage collection.  -jwz
5799 (defmacro gnus-read-integer (&optional point move-p)
5800   (` ((, (if move-p 'progn 'save-excursion))
5801       (,@ (if point (list (list 'goto-char point))))
5802       (if (and (<= (following-char) ?9)
5803                (>= (following-char) ?0))
5804           (read (current-buffer))
5805         0))))
5806
5807 (defmacro gnus-nov-skip-field ()
5808   '(search-forward "\t" eol 'end))
5809
5810 (defmacro gnus-nov-field ()
5811   '(buffer-substring
5812     (point)
5813     (progn (gnus-nov-skip-field) (1- (point)))))
5814
5815 ;; Goes through the xover lines and returns a list of vectors
5816 (defun gnus-get-newsgroup-headers-xover (sequence)
5817   "Parse the news overview data in the server buffer, and return a
5818 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
5819   ;; Get the Xref when the users reads the articles since most/some
5820   ;; NNTP servers do not include Xrefs when using XOVER.
5821   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5822   (let ((cur nntp-server-buffer)
5823         (dependencies gnus-newsgroup-dependencies)
5824         (none 0)
5825         number headers header)
5826     (save-excursion
5827       (set-buffer nntp-server-buffer)
5828       (goto-char (point-min))
5829       (while (and sequence (not (eobp)))
5830         (setq number (read cur))
5831         (while (and sequence (< (car sequence) number))
5832           (setq sequence (cdr sequence)))
5833         (and sequence 
5834              (eq number (car sequence))
5835              (progn
5836                (setq sequence (cdr sequence))
5837                (if (setq header 
5838                          (inline (gnus-nov-parse-line number dependencies)))
5839                    (setq headers (cons header headers)))))
5840         (forward-line 1))
5841       (setq headers (nreverse headers)))
5842     headers))
5843
5844 (defun gnus-nov-parse-line (number dependencies)
5845   "Point has to be after the number on the beginning of the line."
5846   (let ((none 0)
5847         header eol ref id dep)
5848     (save-excursion
5849       (end-of-line)
5850       (setq eol (point)))
5851     (forward-char)
5852     ;; overview: [num subject from date id refs chars lines misc]
5853     (setq header
5854           (vector 
5855            number                       ; number
5856            (gnus-nov-field)             ; subject
5857            (gnus-nov-field)             ; from
5858            (gnus-nov-field)             ; date
5859            (setq id (gnus-nov-field))   ; id
5860            (progn
5861              (save-excursion
5862                (let ((beg (point)))
5863                  (search-forward "\t" eol)
5864                  (if (search-backward ">" beg t)
5865                      (setq ref 
5866                            (downcase 
5867                             (buffer-substring 
5868                              (1+ (point))
5869                              (progn
5870                                (search-backward "<" beg t)
5871                                (point)))))
5872                    (setq ref nil))))
5873              (gnus-nov-field))          ; refs
5874            (read (current-buffer))      ; chars
5875            (read (current-buffer))      ; lines
5876            (if (/= (following-char) ?\t)
5877                nil
5878              (forward-char 1)
5879              (gnus-nov-field))          ; misc
5880            ))
5881     ;; We build the thread tree.
5882     (if (boundp 
5883          (setq dep 
5884                (intern 
5885                 (downcase 
5886                  (or id (concat "none+"
5887                                 (int-to-string 
5888                                  (setq none (1+ none))))))
5889                 dependencies)))
5890         (if (car (symbol-value dep))
5891             ;; An article with this Message-ID has already been seen,
5892             ;; so we ignore this one, except we add any additional
5893             ;; Xrefs (in case the two articles came from different
5894             ;; servers.
5895             (progn
5896               (header-set-xref 
5897                (car (symbol-value dep))
5898                (concat (or (header-xref (car (symbol-value dep))) "")
5899                        (or (header-xref header) "")))
5900               (setq header nil))
5901           (setcar (symbol-value dep) header))
5902       (set dep (list header)))
5903     (if header
5904         (progn
5905           (if (boundp (setq dep (intern (or ref "none") 
5906                                         dependencies)))
5907               (setcdr (symbol-value dep) 
5908                       (cons header (cdr (symbol-value dep))))
5909             (set dep (list nil header)))))
5910     header))
5911
5912 (defun gnus-article-get-xrefs ()
5913   "Fill in the Xref value in `gnus-current-headers', if necessary.
5914 This is meant to be called in `gnus-article-internal-prepare-hook'."
5915   (or (not gnus-use-cross-reference)
5916       (header-xref gnus-current-headers)
5917       (let ((case-fold-search t)
5918             xref)
5919         (save-restriction
5920           (gnus-narrow-to-headers)
5921           (goto-char (point-min))
5922           (if (or (and (eq (downcase (following-char)) ?x)
5923                        (looking-at "Xref:"))
5924                   (search-forward "\nXref:" nil t))
5925               (progn
5926                 (goto-char (1+ (match-end 0)))
5927                 (setq xref (buffer-substring (point) 
5928                                              (progn (end-of-line) (point))))
5929                 (save-excursion
5930                   (set-buffer gnus-summary-buffer)
5931                   (header-set-xref gnus-current-headers xref))))))))
5932
5933 (defalias 'gnus-find-header-by-number 'gnus-get-header-by-number)
5934 (make-obsolete 'gnus-find-header-by-number 'gnus-get-header-by-number)
5935
5936 ;; Return a header specified by a NUMBER.
5937 (defun gnus-get-header-by-number (number)
5938   (save-excursion
5939     (set-buffer gnus-summary-buffer)
5940     (or gnus-newsgroup-headers-hashtb-by-number
5941         (gnus-make-headers-hashtable-by-number))
5942     (gnus-gethash (int-to-string number)
5943                   gnus-newsgroup-headers-hashtb-by-number)))
5944
5945 (defun gnus-make-headers-hashtable-by-number ()
5946   "Make hashtable for the variable gnus-newsgroup-headers by number."
5947   (save-excursion
5948     (set-buffer gnus-summary-buffer)
5949     (let ((headers gnus-newsgroup-headers)
5950           header)
5951       (setq gnus-newsgroup-headers-hashtb-by-number
5952             (gnus-make-hashtable (length headers)))
5953       (while headers
5954         (setq header (car headers))
5955         (gnus-sethash (int-to-string (header-number header))
5956                       header gnus-newsgroup-headers-hashtb-by-number)
5957         (setq headers (cdr headers))))))
5958
5959 (defun gnus-more-header-backward ()
5960   "Find new header backward."
5961   (let ((first (car (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
5962         (artnum gnus-newsgroup-begin)
5963         (header nil))
5964     (while (and (not header)
5965                 (> artnum first))
5966       (setq artnum (1- artnum))
5967       (setq header (gnus-read-header artnum)))
5968     header))
5969
5970 (defun gnus-more-header-forward (&optional backward)
5971   "Find new header forward.
5972 If BACKWARD, find new header backward instead."
5973   (if backward
5974       (gnus-more-header-backward)
5975     (let ((last (cdr (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
5976           (artnum gnus-newsgroup-end)
5977           (header nil))
5978       (while (and (not header)
5979                   (< artnum last))
5980         (setq artnum (1+ artnum))
5981         (setq header (gnus-read-header artnum)))
5982       header)))
5983
5984 (defun gnus-extend-newsgroup (header &optional backward)
5985   "Extend newsgroup selection with HEADER.
5986 Optional argument BACKWARD means extend toward backward."
5987   (if header
5988       (let ((artnum (header-number header)))
5989         (setq gnus-newsgroup-headers
5990               (if backward
5991                   (cons header gnus-newsgroup-headers)
5992                 (nconc gnus-newsgroup-headers (list header))))
5993         (setq gnus-newsgroup-unselected
5994               (delq artnum gnus-newsgroup-unselected))
5995         (setq gnus-newsgroup-begin (min gnus-newsgroup-begin artnum))
5996         (setq gnus-newsgroup-end (max gnus-newsgroup-end artnum)))))
5997
5998 (defun gnus-summary-work-articles (n)
5999   "Return a list of articles to be worked upon. The prefix argument,
6000 the list of process marked articles, and the current article will be
6001 taken into consideration."
6002   (let (articles)
6003     (if (and n (numberp n))
6004         (let ((backward (< n 0))
6005               (n (abs n)))
6006           (save-excursion
6007             (while (and (> n 0)
6008                         (setq articles (cons (gnus-summary-article-number) 
6009                                              articles))
6010                         (gnus-summary-search-forward nil nil backward))
6011               (setq n (1- n))))
6012           (sort articles (function <)))
6013       (or (reverse gnus-newsgroup-processable)
6014           (list (gnus-summary-article-number))))))
6015
6016 (defun gnus-summary-search-group (&optional backward use-level)
6017   "Search for next unread newsgroup.
6018 If optional argument BACKWARD is non-nil, search backward instead."
6019   (save-excursion
6020     (set-buffer gnus-group-buffer)
6021     (save-excursion
6022       ;; We don't want to alter current point of group mode buffer.
6023       (if (gnus-group-search-forward 
6024            backward nil
6025            (if use-level (gnus-group-group-level) nil))
6026           (gnus-group-group-name)))))
6027
6028 (defun gnus-summary-best-group ()
6029   "Find the name of the best unread group."
6030   (save-excursion
6031     (set-buffer gnus-group-buffer)
6032     (save-excursion
6033       (gnus-group-best-unread-group))))
6034
6035 (defun gnus-summary-search-subject (&optional backward unread subject)
6036   "Search for article forward.
6037 If BACKWARD is non-nil, search backward.
6038 If UNREAD is non-nil, only unread articles are selected.
6039 If SUBJECT is non-nil, the article which has the same subject will be
6040 searched for." 
6041   (let ((func (if backward 'previous-single-property-change
6042                 'next-single-property-change))
6043         (beg (point))
6044         (did t)
6045         pos)
6046     (beginning-of-line)
6047     (forward-char (if backward (if (bobp) 0 -1) (if (eobp) 0 1)))
6048     (while (and (setq pos (funcall func (point) 'gnus-number))
6049                 (goto-char (if backward (1- pos) pos))
6050                 (setq did
6051                       (not (and (or (not unread)
6052                                     (eq (get-text-property (point) 'gnus-mark) 
6053                                         gnus-unread-mark))
6054                                 (or (not subject)
6055                                     (equal (gnus-simplify-subject-re 
6056                                             subject)
6057                                            (gnus-simplify-subject-re
6058                                             (get-text-property 
6059                                              (point) 
6060                                              'gnus-subject)))))))
6061                 (if backward (if (bobp) nil (forward-char -1) t)
6062                   (if (eobp) nil (forward-char 1) t))))
6063     (if did
6064         (progn (goto-char beg) nil)
6065       (prog1
6066           (get-text-property (point) 'gnus-number)
6067         (gnus-summary-position-cursor)))))
6068
6069 (defun gnus-summary-search-forward (&optional unread subject backward)
6070   "Search for article forward.
6071 If UNREAD is non-nil, only unread articles are selected.
6072 If SUBJECT is non-nil, the article which has the same subject will be
6073 searched for. 
6074 If BACKWARD is non-nil, the search will be performed backwards instead."
6075   (gnus-summary-search-subject backward unread subject))
6076
6077 (defun gnus-summary-search-backward (&optional unread subject)
6078   "Search for article backward.
6079 If 1st optional argument UNREAD is non-nil, only unread article is selected.
6080 If 2nd optional argument SUBJECT is non-nil, the article which has
6081 the same subject will be searched for."
6082   (gnus-summary-search-forward unread subject t))
6083
6084 (defun gnus-summary-article-number (&optional number-or-nil)
6085   "The article number of the article on the current line.
6086 If there isn's an article number here, then we return the current
6087 article number."
6088   (let ((number (get-text-property (save-excursion (beginning-of-line) (point))
6089                                    'gnus-number)))
6090     (if number-or-nil number (or number gnus-current-article))))
6091
6092 (defun gnus-summary-thread-level ()
6093   "The thread level of the article on the current line."
6094   (or (get-text-property (save-excursion (beginning-of-line) (point))
6095                          'gnus-thread)
6096       0))
6097
6098 (defun gnus-summary-pseudo-article ()
6099   "The thread level of the article on the current line."
6100   (get-text-property (save-excursion (beginning-of-line) (point)) 
6101                      'gnus-pseudo))
6102
6103 (defun gnus-summary-article-mark ()
6104   "The mark on the current line."
6105   (get-text-property (save-excursion (beginning-of-line) (point))
6106                      'gnus-mark))
6107
6108 (defun gnus-summary-subject-string ()
6109   "Return current subject string or nil if nothing."
6110   (get-text-property (save-excursion (beginning-of-line) (point))
6111                      'gnus-subject))
6112
6113 (defalias 'gnus-summary-score 'gnus-summary-article-score)
6114 (make-obsolete 'gnus-summary-score 'gnus-summary-article-score)
6115 (defun gnus-summary-article-score ()
6116   "Return current article score."
6117   (or (cdr (assq (gnus-summary-article-number) gnus-newsgroup-scored))
6118       gnus-summary-default-score 0))
6119
6120 (defun gnus-summary-recenter ()
6121   "Center point in the summary window.
6122 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6123 displayed, no centering will be performed." 
6124   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6125   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
6126   (let ((top (cond ((< (window-height) 4) 0)
6127                    ((< (window-height) 6) 1)
6128                    (t 2))))
6129     (and 
6130      ;; The user has to want it,
6131      gnus-auto-center-summary 
6132      ;; the article buffer must be displayed,
6133      (get-buffer-window gnus-article-buffer)
6134      ;; there must be lines left to scroll forward,
6135      (zerop (save-excursion (forward-line (- (window-height) 1 top))))
6136      ;; so we recenter.
6137      (set-window-start 
6138       (get-buffer-window (current-buffer)) 
6139       (save-excursion (forward-line (- top)) (point))))))
6140
6141 (defun gnus-summary-jump-to-group (newsgroup)
6142   "Move point to NEWSGROUP in group mode buffer."
6143   ;; Keep update point of group mode buffer if visible.
6144   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6145       (save-window-excursion
6146         ;; Take care of tree window mode.
6147         (if (get-buffer-window gnus-group-buffer)
6148             (pop-to-buffer gnus-group-buffer))
6149         (gnus-group-jump-to-group newsgroup))
6150     (save-excursion
6151       ;; Take care of tree window mode.
6152       (if (get-buffer-window gnus-group-buffer)
6153           (pop-to-buffer gnus-group-buffer)
6154         (set-buffer gnus-group-buffer))
6155       (gnus-group-jump-to-group newsgroup))))
6156
6157 ;; This function returns a list of article numbers based on the
6158 ;; difference between the ranges of read articles in this group and
6159 ;; the range of active articles.
6160 (defun gnus-list-of-unread-articles (group)
6161   (let* ((read (nth 2 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6162          (active (gnus-gethash group gnus-active-hashtb))
6163          (last (cdr active))
6164          unread first nlast unread)
6165     ;; If none are read, then all are unread. 
6166     (if (not read)
6167           (setq first (car active))
6168       ;; If the range of read articles is a single range, then the
6169       ;; first unread article is the article after the last read
6170       ;; article. Sounds logical, doesn't it?
6171       (if (atom (car read))
6172           (setq first (1+ (cdr read)))
6173         ;; `read' is a list of ranges.
6174         (while read
6175           (if first 
6176               (while (< first nlast)
6177                 (setq unread (cons first unread))
6178                 (setq first (1+ first))))
6179           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
6180           (setq nlast (if (atom (car (cdr read))) 
6181                           (car (cdr read))
6182                         (car (car (cdr read)))))
6183           (setq read (cdr read)))))
6184     ;; And add the last unread articles.
6185     (while (<= first last)
6186       (setq unread (cons first unread))
6187       (setq first (1+ first)))
6188     ;; Return the list of unread articles.
6189     (nreverse unread)))
6190
6191
6192 ;; Various summary commands
6193
6194 (defun gnus-summary-universal-argument ()
6195   "Perform any operation on all articles marked with the process mark."
6196   (interactive)
6197   (gnus-set-global-variables)
6198   (let ((articles (reverse gnus-newsgroup-processable))
6199         key func)
6200     (or articles (error "No articles marked"))
6201     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
6202         (error "Undefined key"))
6203     (while articles
6204       (gnus-summary-goto-subject (car articles))
6205       (command-execute func)
6206       (gnus-summary-remove-process-mark (car articles))
6207       (setq articles (cdr articles)))))
6208
6209 (defun gnus-summary-toggle-truncation (arg)
6210   "Toggle truncation of summary lines.
6211 With arg, turn line truncation on iff arg is positive."
6212   (interactive "P")
6213   (setq truncate-lines
6214         (if (null arg) (not truncate-lines)
6215           (> (prefix-numeric-value arg) 0)))
6216   (redraw-display))
6217
6218 (defun gnus-summary-reselect-current-group (show-all)
6219   "Once exit and then reselect the current newsgroup.
6220 Prefix argument SHOW-ALL means to select all articles."
6221   (interactive "P")
6222   (gnus-set-global-variables)
6223   (let ((current-subject (gnus-summary-article-number)))
6224     (gnus-summary-exit t)
6225     ;; We have to adjust the point of group mode buffer because the
6226     ;; current point was moved to the next unread newsgroup by
6227     ;; exiting.
6228     (gnus-summary-jump-to-group gnus-newsgroup-name)
6229     (gnus-group-read-group show-all t)
6230     (gnus-summary-goto-subject current-subject)))
6231
6232 (defun gnus-summary-rescan-group (all)
6233   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6234   (interactive "P")
6235   (gnus-set-global-variables)
6236   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
6237   (let ((group gnus-newsgroup-name))
6238     (gnus-summary-exit t)
6239     (gnus-summary-jump-to-group group)
6240     (save-excursion
6241       (set-buffer gnus-group-buffer)
6242       (gnus-group-get-new-news-this-group 1))
6243     (gnus-summary-jump-to-group group)
6244     (gnus-group-read-group all)))
6245
6246 (defun gnus-summary-exit (&optional temporary)
6247   "Exit reading current newsgroup, and then return to group selection mode.
6248 gnus-exit-group-hook is called with no arguments if that value is non-nil."
6249   (interactive)
6250   (gnus-set-global-variables)
6251   (gnus-kill-save-kill-buffer)
6252   (let* ((group gnus-newsgroup-name)
6253          (quit-buffer (cdr (assoc 'quit-buffer (gnus-find-method-for-group
6254                                                 gnus-newsgroup-name))))
6255          (mode major-mode)
6256          (method (car (gnus-find-method-for-group group)))
6257          (buf (current-buffer)))
6258     (if gnus-newsgroup-kill-headers
6259         (setq gnus-newsgroup-killed
6260               (gnus-compress-sequence
6261                (nconc
6262                 (gnus-set-sorted-intersection
6263                  (gnus-uncompress-range gnus-newsgroup-killed)
6264                  (setq gnus-newsgroup-unselected
6265                        (sort gnus-newsgroup-unselected '<)))
6266                 (setq gnus-newsgroup-unreads
6267                       (sort gnus-newsgroup-unreads '<))) t)))
6268     (or (listp (cdr gnus-newsgroup-killed))
6269         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6270     (let ((updated nil)
6271           (headers gnus-newsgroup-headers))
6272       (gnus-close-group group)
6273       (run-hooks 'gnus-exit-group-hook)
6274       (gnus-score-save)
6275       (gnus-update-read-articles 
6276        group gnus-newsgroup-unreads gnus-newsgroup-unselected 
6277        gnus-newsgroup-marked
6278        t gnus-newsgroup-replied gnus-newsgroup-expirable
6279        gnus-newsgroup-killed gnus-newsgroup-dormant
6280        gnus-newsgroup-bookmarks 
6281        (and gnus-save-score gnus-newsgroup-scored))
6282       (and gnus-use-cross-reference
6283            (gnus-mark-xrefs-as-read 
6284             group headers gnus-newsgroup-unreads gnus-newsgroup-expirable))
6285       ;; Do not switch windows but change the buffer to work.
6286       (set-buffer gnus-group-buffer)
6287       (or (eq 'nndigest method)
6288           (gnus-group-update-group group)))
6289     ;; Make sure where I was, and go to next newsgroup.
6290     (if (eq method 'nndigest)
6291         ()
6292       (gnus-group-jump-to-group group)
6293       (gnus-group-next-unread-group 1))
6294     (if temporary
6295         ;; If exiting temporary, caller should adjust group mode
6296         ;; buffer point by itself.
6297         nil                             ;Nothing to do.
6298       ;; We set all buffer-local variables to nil. It is unclear why
6299       ;; this is needed, but if we don't, buffer-local variables are
6300       ;; not garbage-collected, it seems. This would the lead to en
6301       ;; ever-growing Emacs.
6302       (set-buffer buf)
6303       (gnus-summary-clear-local-variables)
6304       ;; We clear the global counterparts of the buffer-local
6305       ;; variables as well, just to be on the safe side.
6306       (set-buffer gnus-group-buffer)
6307       (gnus-summary-clear-local-variables)
6308       (gnus-configure-windows 'newsgroups t)
6309       ;; Return to group mode buffer. 
6310       (and (get-buffer buf) 
6311            (eq mode 'gnus-summary-mode)
6312            (kill-buffer buf))
6313       (if (get-buffer gnus-article-buffer)
6314           (bury-buffer gnus-article-buffer))
6315       (setq gnus-current-select-method gnus-select-method)
6316       (pop-to-buffer gnus-group-buffer)
6317       (if (and quit-buffer (buffer-name quit-buffer))
6318           (progn
6319             (switch-to-buffer quit-buffer)
6320             (gnus-set-global-variables)
6321             (gnus-configure-windows 'summary))))))
6322
6323 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6324 (defun gnus-summary-exit-no-update (&optional no-questions)
6325   "Quit reading current newsgroup without updating read article info."
6326   (interactive)
6327   (let* ((group gnus-newsgroup-name)
6328          (quit-buffer (cdr (assoc 'quit-buffer 
6329                                   (gnus-find-method-for-group group)))))
6330     (if (or no-questions
6331             gnus-expert-user
6332             (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
6333         (progn
6334           (gnus-close-group group)
6335           (gnus-summary-clear-local-variables)
6336           (set-buffer gnus-group-buffer)
6337           (gnus-summary-clear-local-variables)
6338           ;; Return to group selection mode.
6339           (gnus-configure-windows 'newsgroups)
6340           (if (get-buffer gnus-summary-buffer)
6341               (kill-buffer gnus-summary-buffer))
6342           (if (get-buffer gnus-article-buffer)
6343               (bury-buffer gnus-article-buffer))
6344           (pop-to-buffer gnus-group-buffer)
6345           (gnus-group-jump-to-group group)
6346           (gnus-group-next-group 1)
6347           (if (and quit-buffer (buffer-name quit-buffer))
6348               (progn
6349                 (switch-to-buffer quit-buffer)
6350                 (gnus-configure-windows 'summary)))))))
6351
6352 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6353 (defun gnus-summary-fetch-faq (group)
6354   "Fetch the FAQ for the current group."
6355   (interactive (list gnus-newsgroup-name))
6356   (gnus-configure-windows 'article)
6357   (pop-to-buffer gnus-article-buffer)
6358   (find-file (concat gnus-group-faq-directory group)))
6359
6360 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6361 (defun gnus-summary-describe-group (force)
6362   "Describe the current newsgroup."
6363   (interactive "P")
6364   (gnus-group-describe-group force gnus-newsgroup-name))
6365
6366 (defun gnus-summary-describe-briefly ()
6367   "Describe summary mode commands briefly."
6368   (interactive)
6369   (message
6370     (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")))
6371
6372 ;; Walking around group mode buffer from summary mode.
6373
6374 (defun gnus-summary-next-group (&optional no-article group backward)
6375   "Exit current newsgroup and then select next unread newsgroup.
6376 If prefix argument NO-ARTICLE is non-nil, no article is selected initially.
6377 If BACKWARD, go to previous group instead."
6378   (interactive "P")
6379   (gnus-set-global-variables)
6380   (let ((ingroup gnus-newsgroup-name)
6381         (sumbuf (current-buffer))
6382         num)
6383     (gnus-summary-exit t)               ;Update all information.
6384     (if (and group
6385              (or (and (numberp (setq num (car (gnus-gethash
6386                                                group gnus-newsrc-hashtb))))
6387                       (< num 1))
6388                  (null num)))
6389         (progn
6390           (gnus-group-jump-to-group group)
6391           (setq group nil))
6392       (gnus-group-jump-to-group ingroup))
6393     (let ((group (or group (gnus-summary-search-group backward)))
6394           (buf gnus-summary-buffer))
6395       (if (null group)
6396           (gnus-summary-exit-no-update t)
6397         (message "Selecting %s..." group)
6398         ;; We are now in group mode buffer.
6399         ;; Make sure group mode buffer point is on GROUP.
6400         (gnus-group-jump-to-group group)
6401         (unwind-protect
6402             (gnus-summary-read-group group nil no-article buf)
6403           (and (string= gnus-newsgroup-name ingroup)
6404                (progn
6405                  (set-buffer sumbuf)
6406                  (gnus-summary-exit-no-update t))))))))
6407
6408 (defun gnus-summary-prev-group (no-article)
6409   "Exit current newsgroup and then select previous unread newsgroup.
6410 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6411   (interactive "P")
6412   (gnus-summary-next-group no-article nil t))
6413
6414 ;; Walking around summary lines.
6415
6416 (defun gnus-summary-first-subject (unread)
6417   "Go to the first unread subject.
6418 If UNREAD is non-nil, go to the first unread article.
6419 Returns nil if there are no unread articles."
6420   (interactive "P")
6421   (let ((begin (point)))
6422     (if unread
6423         (if (not (gnus-goto-char 
6424                   (text-property-any (point-min) (point-max)
6425                                      'gnus-mark gnus-unread-mark)))
6426             (progn
6427               ;; If there is no unread articles, stay where you are.
6428               (goto-char begin)
6429               (message "No more unread articles")
6430               nil)
6431           t)
6432       (goto-char (point-min)))))
6433
6434 (defun gnus-summary-next-subject (n &optional unread)
6435   "Go to next N'th summary line.
6436 If N is negative, go to the previous N'th subject line.
6437 If UNREAD is non-nil, only unread articles are selected.
6438 The difference between N and the actual number of steps taken is
6439 returned."
6440   (interactive "p")
6441   (let ((backward (< n 0))
6442         (n (abs n)))
6443   (while (and (> n 0)
6444               (gnus-summary-search-forward unread nil backward))
6445     (setq n (1- n)))
6446   (gnus-summary-recenter)
6447   (if (/= 0 n) (message "No more%s articles" (if unread " unread" "")))
6448   (gnus-summary-position-cursor)
6449   n))
6450
6451 (defun gnus-summary-next-unread-subject (n)
6452   "Go to next N'th unread summary line."
6453   (interactive "p")
6454   (gnus-summary-next-subject n t))
6455
6456 (defun gnus-summary-prev-subject (n &optional unread)
6457   "Go to previous N'th summary line.
6458 If optional argument UNREAD is non-nil, only unread article is selected."
6459   (interactive "p")
6460   (gnus-summary-next-subject (- n) unread))
6461
6462 (defun gnus-summary-prev-unread-subject (n)
6463   "Go to previous N'th unread summary line."
6464   (interactive "p")
6465   (gnus-summary-next-subject (- n) t))
6466
6467 (defun gnus-summary-goto-subject (article)
6468   "Go the subject line of ARTICLE."
6469   (interactive
6470    (list
6471     (string-to-int
6472      (completing-read "Article number: "
6473                       (mapcar
6474                        (lambda (headers)
6475                          (list
6476                           (int-to-string (header-number headers))))
6477                        gnus-newsgroup-headers)
6478                       nil 'require-match))))
6479   (or article (error "No article number"))
6480   (if (or (eq article (gnus-summary-article-number t))
6481           (gnus-goto-char
6482            (text-property-any
6483             (point-min) (point-max) 'gnus-number article)))
6484       article))
6485
6486 ;; Walking around summary lines with displaying articles.
6487
6488 (defun gnus-summary-expand-window ()
6489   "Expand summary window to show headers full window."
6490   (interactive)
6491   (gnus-set-global-variables)
6492   (gnus-configure-windows 'summary)
6493   (pop-to-buffer gnus-summary-buffer))
6494
6495 (defun gnus-summary-display-article (article &optional all-header)
6496   "Display ARTICLE in article buffer."
6497   (gnus-set-global-variables)
6498   (if (null article)
6499       nil
6500     (gnus-article-prepare article all-header)
6501     (if (= (gnus-summary-article-mark) ?Z) 
6502         (progn
6503           (forward-line 1)
6504           (gnus-summary-position-cursor)))
6505     (run-hooks 'gnus-select-article-hook)
6506     (gnus-summary-recenter)
6507 ;    (set-window-point (get-buffer-window (current-buffer)) (point-max))
6508 ;    (sit-for 0)
6509     (gnus-summary-goto-subject article)
6510     ;; Successfully display article.
6511     (gnus-summary-update-line)
6512     t))
6513
6514 (defun gnus-summary-select-article (&optional all-headers force pseudo)
6515   "Select the current article.
6516 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6517 non-nil, the article will be re-fetched even if it already present in
6518 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6519 be displayed."
6520   (and (not pseudo) (gnus-summary-pseudo-article)
6521        (error "This is a pseudo-article."))
6522   (let ((article (gnus-summary-article-number))
6523         (all-headers (not (not all-headers)))) ;Must be T or NIL.
6524     (if (or (null gnus-current-article)
6525             (null gnus-article-current)
6526             (/= article (cdr gnus-article-current))
6527             (not (equal (car gnus-article-current) gnus-newsgroup-name))
6528             force)
6529         ;; The requested article is different from the current article.
6530         (progn
6531           (gnus-summary-display-article article all-headers)
6532           article)
6533       (if all-headers (gnus-article-show-all-headers))
6534       (gnus-configure-windows 'article)
6535       (pop-to-buffer gnus-summary-buffer)
6536       nil)))
6537
6538 (defun gnus-summary-set-current-mark (&optional current-mark)
6539   "Obsolete function."
6540   nil)
6541
6542 (defun gnus-summary-next-article (unread &optional subject backward)
6543   "Select the next article.
6544 If UNREAD, only unread articles are selected.
6545 If SUBJECT, only articles with SUBJECT are selected.
6546 If BACKWARD, the previous article is selected instead of the next."
6547   (interactive "P")
6548   (let ((opoint (point))
6549         (method (car (gnus-find-method-for-group gnus-newsgroup-name)))
6550         header)
6551     (cond
6552      ;; Is there such an article?
6553      ((gnus-summary-display-article 
6554        (gnus-summary-search-forward unread subject backward))
6555       (gnus-summary-position-cursor))
6556      ;; If not, we try the first unread, if that is wanted.
6557      ((and subject
6558            gnus-auto-select-same
6559            (gnus-summary-first-unread-article))
6560       (message "Wrapped"))
6561      ;; Try to get next/previous article not displayed in this group.
6562      ((and gnus-auto-extend-newsgroup
6563            (not unread) (not subject)
6564            (setq header (gnus-more-header-forward backward)))
6565       (gnus-extend-newsgroup header backward)
6566       (let ((buffer-read-only nil))
6567         (goto-char (if backward (point-min) (point-max)))
6568         (gnus-summary-prepare-threads (list header) 0))
6569       (gnus-summary-goto-article (if backward gnus-newsgroup-begin
6570                                    gnus-newsgroup-end)))
6571      ;; Go to next/previous group.
6572      (t
6573       (gnus-summary-jump-to-group gnus-newsgroup-name)
6574       (let ((cmd (aref (this-command-keys) 0))
6575             (group 
6576              (if (eq gnus-keep-same-level 'best) (gnus-summary-best-group)
6577                (gnus-summary-search-group backward gnus-keep-same-level))))
6578         ;; Keep just the event type of CMD.
6579         (and (listp cmd) (setq cmd (car cmd)))
6580         ;; Select next unread newsgroup automagically.
6581         (cond 
6582          ((not gnus-auto-select-next)
6583           (message "No more%s articles" (if unread " unread" "")))
6584          ((eq gnus-auto-select-next 'quietly)
6585           ;; Select quietly.
6586           (if (eq method 'nndigest)
6587               (gnus-summary-exit)
6588             (message "No more%s articles (%s)..."
6589                      (if unread " unread" "") 
6590                      (if group (concat "selecting " group)
6591                        "exiting"))
6592             (gnus-summary-next-group nil group backward)))
6593          (t
6594           (let ((keystrokes '(?\C-n ?\C-p))
6595                 key)
6596             (while (or (null key) (memq key keystrokes))
6597               (message 
6598                "No more%s articles%s" (if unread " unread" "")
6599                (if (and group (not (eq method 'nndigest)))
6600                    (format " (Type %s for %s [%s])"
6601                            (single-key-description cmd) group
6602                            (car (gnus-gethash group gnus-newsrc-hashtb)))
6603                  (format " (Type %s to exit %s)"
6604                          (single-key-description cmd)
6605                          gnus-newsgroup-name)))
6606               ;; Confirm auto selection.
6607               (let* ((event (read-event)))
6608                 (setq key (if (listp event) (car event) event))
6609                 (if (member key keystrokes)
6610                     (let ((obuf (current-buffer)))
6611                       (switch-to-buffer gnus-group-buffer)
6612                       (gnus-group-jump-to-group group)
6613                       (execute-kbd-macro (char-to-string key))
6614                       (setq group (gnus-group-group-name))
6615                       (switch-to-buffer obuf)))))
6616             (if (eq key cmd)
6617                 (if (or (not group) (eq method 'nndigest))
6618                     (gnus-summary-exit)
6619                   (gnus-summary-next-group nil group backward))
6620               (setq unread-command-events (list key)))))))))))
6621
6622 (defun gnus-summary-next-unread-article ()
6623   "Select unread article after current one."
6624   (interactive)
6625   (gnus-summary-next-article t (and gnus-auto-select-same
6626                                     (gnus-summary-subject-string))))
6627
6628 (defun gnus-summary-prev-article (unread &optional subject)
6629   "Select the article after the current one.
6630 If UNREAD is non-nil, only unread articles are selected."
6631   (interactive "P")
6632   (gnus-summary-next-article unread subject t))
6633
6634 (defun gnus-summary-prev-unread-article ()
6635   "Select unred article before current one."
6636   (interactive)
6637   (gnus-summary-prev-article t (and gnus-auto-select-same
6638                                     (gnus-summary-subject-string))))
6639
6640 (defun gnus-summary-next-page (lines &optional circular)
6641   "Show next page of selected article.
6642 If end of article, select next article.
6643 Argument LINES specifies lines to be scrolled up.
6644 If CIRCULAR is non-nil, go to the start of the article instead of 
6645 instead of selecting the next article when reaching the end of the
6646 current article." 
6647   (interactive "P")
6648   (setq gnus-summary-buffer (current-buffer))
6649   (let ((article (gnus-summary-article-number))
6650         (endp nil))
6651     (if (or (null gnus-current-article)
6652             (null gnus-article-current)
6653             (/= article (cdr gnus-article-current))
6654             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6655         ;; Selected subject is different from current article's.
6656         (gnus-summary-display-article article)
6657       (gnus-configure-windows 'article)
6658       (pop-to-buffer gnus-summary-buffer)
6659       (gnus-eval-in-buffer-window
6660        gnus-article-buffer
6661        (setq endp (gnus-article-next-page lines)))
6662       (if endp
6663           (cond (circular
6664                  (gnus-summary-beginning-of-article))
6665                 (lines
6666                  (message "End of message"))
6667                 ((null lines)
6668                  (gnus-summary-next-unread-article))))))
6669   (gnus-summary-position-cursor))
6670
6671 (defun gnus-summary-prev-page (lines)
6672   "Show previous page of selected article.
6673 Argument LINES specifies lines to be scrolled down."
6674   (interactive "P")
6675   (let ((article (gnus-summary-article-number)))
6676     (if (or (null gnus-current-article)
6677             (null gnus-article-current)
6678             (/= article (cdr gnus-article-current))
6679             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6680         ;; Selected subject is different from current article's.
6681         (gnus-summary-display-article article)
6682       (gnus-configure-windows 'article)
6683       (pop-to-buffer gnus-summary-buffer)
6684       (gnus-eval-in-buffer-window gnus-article-buffer
6685         (gnus-article-prev-page lines))))
6686   (gnus-summary-position-cursor))
6687
6688 (defun gnus-summary-scroll-up (lines)
6689   "Scroll up (or down) one line current article.
6690 Argument LINES specifies lines to be scrolled up (or down if negative)."
6691   (interactive "p")
6692   (or (gnus-summary-select-article nil nil 'pseudo)
6693       (gnus-eval-in-buffer-window 
6694        gnus-article-buffer
6695        (cond ((> lines 0)
6696               (if (gnus-article-next-page lines)
6697                   (message "End of message")))
6698              ((< lines 0)
6699               (gnus-article-prev-page (- lines))))))
6700   (gnus-summary-position-cursor))
6701
6702 (defun gnus-summary-next-same-subject ()
6703   "Select next article which has the same subject as current one."
6704   (interactive)
6705   (gnus-summary-next-article nil (gnus-summary-subject-string)))
6706
6707 (defun gnus-summary-prev-same-subject ()
6708   "Select previous article which has the same subject as current one."
6709   (interactive)
6710   (gnus-summary-prev-article nil (gnus-summary-subject-string)))
6711
6712 (defun gnus-summary-next-unread-same-subject ()
6713   "Select next unread article which has the same subject as current one."
6714   (interactive)
6715   (gnus-summary-next-article t (gnus-summary-subject-string)))
6716
6717 (defun gnus-summary-prev-unread-same-subject ()
6718   "Select previous unread article which has the same subject as current one."
6719   (interactive)
6720   (gnus-summary-prev-article t (gnus-summary-subject-string)))
6721
6722 (defun gnus-summary-first-unread-article ()
6723   "Select the first unread article. 
6724 Return nil if there are no unread articles."
6725   (interactive)
6726   (prog1
6727       (if (gnus-summary-first-subject t)
6728           (gnus-summary-display-article (gnus-summary-article-number)))
6729     (gnus-summary-position-cursor)))
6730
6731 (defun gnus-summary-best-unread-article ()
6732   "Select the unread article with the highest score."
6733   (interactive)
6734   (gnus-set-global-variables)
6735   (let ((scored gnus-newsgroup-scored)
6736         (best -1000000)
6737         article art)
6738     (while scored
6739       (or (> best (cdr (car scored)))
6740           (and (memq (setq art (car (car scored))) gnus-newsgroup-unreads)
6741                (not (memq art gnus-newsgroup-marked))
6742                (not (memq art gnus-newsgroup-dormant))
6743                (if (= best (cdr (car scored)))
6744                    (setq article (min art article))
6745                  (setq article art)
6746                  (setq best (cdr (car scored))))))
6747       (setq scored (cdr scored)))
6748     (if article 
6749         (gnus-summary-goto-article article)
6750       (gnus-summary-first-unread-article))
6751     (gnus-summary-position-cursor)))
6752
6753 (defun gnus-summary-goto-article (article &optional all-headers)
6754   "Fetch ARTICLE and display it if it exists.
6755 If ALL-HEADERS is non-nil, no header lines are hidden."
6756   (interactive
6757    (list
6758     (string-to-int
6759      (completing-read 
6760       "Article number: "
6761       (mapcar (lambda (headers) (list (int-to-string (header-number headers))))
6762               gnus-newsgroup-headers) 
6763       nil 'require-match))))
6764   (if (gnus-summary-goto-subject article)
6765       (gnus-summary-display-article article all-headers))
6766   (gnus-summary-position-cursor))
6767
6768 (defun gnus-summary-goto-last-article ()
6769   "Go to the last article."
6770   (interactive)
6771   (if gnus-last-article
6772       (gnus-summary-goto-article gnus-last-article))
6773   (gnus-summary-position-cursor))
6774
6775 (defun gnus-summary-pop-article (number)
6776   "Pop one article off the history and go to the previous.
6777 NUMBER articles will be popped off."
6778   (interactive "p")
6779   (let (to)
6780     (setq gnus-newsgroup-history
6781           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6782     (if to
6783         (gnus-summary-goto-article (car to))
6784       (error "Article history empty")))
6785   (gnus-summary-position-cursor))
6786
6787 ;; Summary article oriented commands
6788
6789 (defun gnus-summary-refer-parent-article (n)
6790   "Refer parent article N times.
6791 The difference between N and the number of articles fetched is returned."
6792   (interactive "p")
6793   (gnus-set-global-variables)
6794   (while 
6795       (and 
6796        (> n 0)
6797        (let ((ref (header-references (gnus-get-header-by-number
6798                                       (gnus-summary-article-number)))))
6799          (if (and ref (not (equal ref ""))
6800                   (string-match "<[^<>]*>[ \t]*$" ref))
6801              (gnus-summary-refer-article 
6802               (substring ref (match-beginning 0) (match-end 0))))))
6803     (setq n (1- n)))
6804   (or (zerop n) (message "No references in article or expired article."))
6805   (gnus-summary-position-cursor)
6806   n)
6807     
6808 (defun gnus-summary-refer-article (message-id)
6809   "Refer article specified by MESSAGE-ID.
6810 NOTE: This command only works with newsgroup that use NNTP."
6811   (interactive "sMessage-ID: ")
6812   (if (or (not (stringp message-id))
6813           (zerop (length message-id)))
6814       ()
6815     ;; Construct the correct Message-ID if necessary.
6816     ;; Suggested by tale@pawl.rpi.edu.
6817     (or (string-match "^<" message-id)
6818         (setq message-id (concat "<" message-id)))
6819     (or (string-match ">$" message-id)
6820         (setq message-id (concat message-id ">")))
6821     (let ((header (car (gnus-gethash (downcase message-id)
6822                                      gnus-newsgroup-dependencies))))
6823       (if header
6824           (gnus-summary-goto-article (header-number header))
6825         (let ((gnus-override-method gnus-refer-article-method))
6826           (if (gnus-article-prepare 
6827                message-id nil (gnus-read-header message-id))
6828               (progn
6829                 (gnus-summary-insert-line 
6830                  nil gnus-current-headers 0 nil gnus-read-mark nil nil 
6831                  (header-subject gnus-current-headers))
6832                 (forward-line -1)
6833                 (gnus-summary-position-cursor)
6834                 (gnus-summary-update-line)
6835                 message-id)
6836             (message "No such references")
6837             nil))))))
6838
6839 (defun gnus-summary-enter-digest-group ()
6840   "Enter a digest group based on the current article."
6841   (interactive)
6842   (gnus-summary-select-article)
6843   (let ((name (format "%s/%d" 
6844                       (gnus-group-prefixed-name 
6845                        gnus-newsgroup-name (list 'nndigest "")) 
6846                       gnus-current-article))
6847         (buf (current-buffer)))
6848     (set-buffer gnus-group-buffer)
6849     (gnus-sethash 
6850      name 
6851      (list t nil (list name 3 nil nil 
6852                        (list 'nndigest gnus-article-buffer
6853                              (cons 'quit-buffer buf))))
6854      gnus-newsrc-hashtb)
6855     (gnus-group-read-group t nil name)))
6856   
6857 (defun gnus-summary-isearch-article ()
6858   "Do incremental search forward on current article."
6859   (interactive)
6860   (gnus-summary-select-article)
6861   (gnus-eval-in-buffer-window gnus-article-buffer
6862                               (isearch-forward)))
6863
6864 (defun gnus-summary-search-article-forward (regexp)
6865   "Search for an article containing REGEXP forward.
6866 gnus-select-article-hook is not called during the search."
6867   (interactive
6868    (list (read-string
6869           (concat "Search forward (regexp): "
6870                   (if gnus-last-search-regexp
6871                       (concat "(default " gnus-last-search-regexp ") "))))))
6872   (if (string-equal regexp "")
6873       (setq regexp (or gnus-last-search-regexp ""))
6874     (setq gnus-last-search-regexp regexp))
6875   (if (gnus-summary-search-article regexp nil)
6876       (gnus-eval-in-buffer-window 
6877        gnus-article-buffer
6878        (recenter 0))
6879     (error "Search failed: \"%s\"" regexp)))
6880
6881 (defun gnus-summary-search-article-backward (regexp)
6882   "Search for an article containing REGEXP backward.
6883 gnus-select-article-hook is not called during the search."
6884   (interactive
6885    (list (read-string
6886           (concat "Search backward (regexp): "
6887                   (if gnus-last-search-regexp
6888                       (concat "(default " gnus-last-search-regexp ") "))))))
6889   (if (string-equal regexp "")
6890       (setq regexp (or gnus-last-search-regexp ""))
6891     (setq gnus-last-search-regexp regexp))
6892   (if (gnus-summary-search-article regexp t)
6893       (gnus-eval-in-buffer-window
6894        gnus-article-buffer
6895        (recenter 0))
6896     (error "Search failed: \"%s\"" regexp)))
6897
6898 (defun gnus-summary-search-article (regexp &optional backward)
6899   "Search for an article containing REGEXP.
6900 Optional argument BACKWARD means do search for backward.
6901 gnus-select-article-hook is not called during the search."
6902   (let ((gnus-select-article-hook nil)  ;Disable hook.
6903         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6904         (re-search
6905          (if backward
6906              (function re-search-backward) (function re-search-forward)))
6907         (found nil)
6908         (last nil))
6909     ;; Hidden thread subtrees must be searched for ,too.
6910     (gnus-summary-show-all-threads)
6911     (if (eobp) (forward-line -1))
6912     ;; First of all, search current article.
6913     ;; We don't want to read article again from NNTP server nor reset
6914     ;; current point.
6915     (gnus-summary-select-article)
6916     (message "Searching article: %d..." gnus-current-article)
6917     (setq last gnus-current-article)
6918     (gnus-eval-in-buffer-window gnus-article-buffer
6919       (save-restriction
6920         (widen)
6921         ;; Begin search from current point.
6922         (setq found (funcall re-search regexp nil t))))
6923     ;; Then search next articles.
6924     (while (and (not found)
6925                 (gnus-summary-display-article 
6926                  (gnus-summary-search-subject backward nil nil)))
6927       (message "Searching article: %d..." gnus-current-article)
6928       (gnus-eval-in-buffer-window gnus-article-buffer
6929         (save-restriction
6930           (widen)
6931           (goto-char (if backward (point-max) (point-min)))
6932           (setq found (funcall re-search regexp nil t)))))
6933     (message "")
6934     ;; Adjust article pointer.
6935     (or (eq last gnus-current-article)
6936         (setq gnus-last-article last))
6937     ;; Return T if found such article.
6938     found))
6939
6940 (defun gnus-summary-execute-command (header regexp command &optional backward)
6941   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
6942 If HEADER is an empty string (or nil), the match is done on the entire
6943 article. If BACKWARD (the prefix) is non-nil, search backward instead."
6944   (interactive
6945    (list (let ((completion-ignore-case t))
6946            (completing-read 
6947             "Header name: "
6948             (mapcar (string) (list string)
6949                     '("Number" "Subject" "From" "Lines" "Date"
6950                       "Message-ID" "Xref" "References")
6951                     nil 'require-match)))
6952          (read-string "Regexp: ")
6953          (read-key-sequence "Command: ")
6954          current-prefix-arg))
6955   ;; Hidden thread subtrees must be searched as well.
6956   (gnus-summary-show-all-threads)
6957   ;; We don't want to change current point nor window configuration.
6958   (save-excursion
6959     (save-window-excursion
6960       (message "Executing %s..." (key-description command))
6961       ;; We'd like to execute COMMAND interactively so as to give arguments.
6962       (gnus-execute header regexp
6963                     (` (lambda ()
6964                          (call-interactively '(, (key-binding command)))))
6965                     backward)
6966       (message "Executing %s... done" (key-description command)))))
6967
6968 (defun gnus-summary-beginning-of-article ()
6969   "Scroll the article back to the beginning."
6970   (interactive)
6971   (gnus-summary-select-article)
6972   (gnus-eval-in-buffer-window
6973    gnus-article-buffer
6974    (widen)
6975    (goto-char (point-min))
6976    (and gnus-break-pages (gnus-narrow-to-page))))
6977
6978 (defun gnus-summary-end-of-article ()
6979   "Scroll to the end of the article."
6980   (interactive)
6981   (gnus-summary-select-article)
6982   (gnus-eval-in-buffer-window 
6983    gnus-article-buffer
6984    (widen)
6985    (goto-char (point-max))
6986    (and gnus-break-pages (gnus-narrow-to-page))))
6987
6988 (defun gnus-summary-show-article ()
6989   "Force re-fetching of the current article."
6990   (interactive)
6991   (gnus-summary-select-article gnus-have-all-headers t t))
6992
6993 (defun gnus-summary-toggle-header (arg)
6994   "Show the headers if they are hidden, or hide them if they are shown.
6995 If ARG is a positive number, show the entire header.
6996 If ARG is a negative number, hide the unwanted header lines."
6997   (interactive "P")
6998   (gnus-set-global-variables)
6999   (save-excursion
7000     (set-buffer gnus-article-buffer)
7001     (let ((buffer-read-only nil))
7002       (if (numberp arg) 
7003           (if (> arg 0) (remove-text-properties 1 (point-max) '(invisible t))
7004             (if (< arg 0) (run-hooks 'gnus-article-display-hook)))
7005         (if (text-property-any 1 (point-max) 'invisible t)
7006             (remove-text-properties 1 (point-max) '(invisible t))
7007           (let ((gnus-have-all-headers nil))
7008             (run-hooks 'gnus-article-display-hook)))))))
7009
7010 (defun gnus-summary-show-all-headers ()
7011   "Make all header lines visible."
7012   (interactive)
7013   (gnus-article-show-all-headers))
7014
7015 (defun gnus-summary-toggle-mime (arg)
7016   "Toggle MIME processing.
7017 If ARG is a positive number, turn MIME processing on."
7018   (interactive "P")
7019   (setq gnus-show-mime
7020         (if (null arg) (not gnus-show-mime)
7021           (> (prefix-numeric-value arg) 0)))
7022   (gnus-summary-select-article t 'force))
7023
7024 (defun gnus-summary-caesar-message (rotnum)
7025   "Caesar rotates all letters of current message by 13/47 places.
7026 With prefix arg, specifies the number of places to rotate each letter forward.
7027 Caesar rotates Japanese letters by 47 places in any case."
7028   (interactive "P")
7029   (gnus-summary-select-article)
7030   (let ((mail-header-separator "")) ; !!! Is this necessary?
7031     (gnus-overload-functions)
7032     (gnus-eval-in-buffer-window 
7033      gnus-article-buffer
7034      (save-restriction
7035        (widen)
7036        ;; We don't want to jump to the beginning of the message.
7037        ;; `save-excursion' does not do its job.
7038        (move-to-window-line 0)
7039        (let ((last (point)))
7040          (news-caesar-buffer-body rotnum)
7041          (goto-char last)
7042          (recenter 0))))))
7043
7044 (defun gnus-summary-stop-page-breaking ()
7045   "Stop page breaking in the current article."
7046   (interactive)
7047   (gnus-summary-select-article)
7048   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
7049
7050 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
7051
7052 (defun gnus-summary-move-article (n &optional to-newsgroup select-method)
7053   "Move the current article to a different newsgroup.
7054 If N is a positive number, move the N next articles.
7055 If N is a negative number, move the N previous articles.
7056 If N is nil and any articles have been marked with the process mark,
7057 move those articles instead.
7058 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
7059 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
7060 re-spool using this method.
7061 For this function to work, both the current newsgroup and the
7062 newsgroup that you want to move to have to support the `request-move'
7063 and `request-accept' functions. (Ie. mail newsgroups at present.)"
7064   (interactive "P")
7065   (gnus-set-global-variables)
7066   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
7067       (error "The current newsgroup does not support article moving"))
7068   (let ((articles (gnus-summary-work-articles n))
7069         art-group)
7070     (if (and (not to-newsgroup) (not select-method))
7071         (setq to-newsgroup
7072               (completing-read 
7073                (format "Where do you want to move %s? "
7074                        (if (> (length articles) 1)
7075                            (format "these %d articles" (length articles))
7076                          "this article"))
7077                gnus-active-hashtb nil t 
7078                (gnus-group-real-prefix gnus-newsgroup-name))))
7079     (or (gnus-check-backend-function 'request-accept-article 
7080                                      (or select-method to-newsgroup))
7081         (error "%s does not support article moving" to-newsgroup))
7082     (message "Moving to %s: %s..." (or select-method to-newsgroup) articles)
7083     (while articles
7084       (if (setq art-group
7085                 (gnus-request-move-article 
7086                  (car articles)                   ; Article to move
7087                  gnus-newsgroup-name              ; From newsgrouo
7088                  (nth 1 (gnus-find-method-for-group 
7089                          gnus-newsgroup-name))    ; Server
7090                  (list 'gnus-request-accept-article 
7091                        (if select-method
7092                            (quote select-method)
7093                          to-newsgroup)
7094                        (not (cdr articles)))     ; Accept form
7095                  (not (cdr articles))))          ; Only save nov last time
7096           (let* ((buffer-read-only nil)
7097                  (entry 
7098                   (or
7099                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
7100                    (gnus-gethash 
7101                     (gnus-group-prefixed-name 
7102                      (car art-group) 
7103                      (if select-method (list select-method "")
7104                        (gnus-find-method-for-group to-newsgroup)))
7105                     gnus-newsrc-hashtb)))
7106                  (info (nth 2 entry))
7107                  (article (car articles))
7108                  (marked (nth 3 info)))
7109             (gnus-summary-goto-subject article)
7110             (delete-region (progn (beginning-of-line) (point))
7111                            (progn (forward-line 1) (point)))
7112             (if (not (memq article gnus-newsgroup-unreads))
7113                 (setcar (cdr (cdr info))
7114                         (gnus-add-to-range (nth 2 info) 
7115                                            (list (cdr art-group)))))
7116             ;; Copy any marks over to the new group.
7117             (let ((marks '((tick . gnus-newsgroup-marked)
7118                            (dormant . gnus-newsgroup-dormant)
7119                            (expire . gnus-newsgroup-expirable)
7120                            (bookmark . gnus-newsgroup-bookmarks)
7121                         ;   (score . gnus-newsgroup-scored)
7122                            (reply . gnus-newsgroup-replied)))
7123                   (to-article (cdr art-group)))
7124               (while marks
7125                 (if (memq article (symbol-value (cdr (car marks))))
7126                     (gnus-add-marked-articles 
7127                      (car info) (car (car marks)) (list to-article) info))
7128                 (setq marks (cdr marks)))))
7129         (message "Couldn't move article %s" (car articles)))
7130       (gnus-summary-remove-process-mark (car articles))
7131       (setq articles (cdr articles)))))
7132
7133 (defun gnus-summary-respool-article (n &optional respool-method)
7134   "Respool the current article.
7135 The article will be squeezed through the mail spooling process again,
7136 which means that it will be put in some mail newsgroup or other
7137 depending on `nnmail-split-methods'.
7138 If N is a positive number, respool the N next articles.
7139 If N is a negative number, respool the N previous articles.
7140 If N is nil and any articles have been marked with the process mark,
7141 respool those articles instead.
7142 For this function to work, both the current newsgroup and the
7143 newsgroup that you want to move to have to support the `request-move'
7144 and `request-accept' functions. (Ie. mail newsgroups at present.)"
7145   (interactive "P")
7146   (gnus-set-global-variables)
7147   (or respool-method
7148       (setq respool-method
7149             (completing-read
7150              "What method do you want to use when respooling? "
7151              (gnus-methods-using 'respool) nil t)))
7152   (gnus-summary-move-article n nil (intern respool-method)))
7153
7154 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
7155 (defun gnus-summary-copy-article (n &optional to-newsgroup select-method)
7156   "Move the current article to a different newsgroup.
7157 If N is a positive number, move the N next articles.
7158 If N is a negative number, move the N previous articles.
7159 If N is nil and any articles have been marked with the process mark,
7160 move those articles instead.
7161 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
7162 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
7163 re-spool using this method.
7164 For this function to work, the newsgroup that you want to move to have
7165 to support the `request-move' and `request-accept'
7166 functions. (Ie. mail newsgroups at present.)"
7167   (interactive "P")
7168   (gnus-set-global-variables)
7169   (let ((articles (gnus-summary-work-articles n))
7170         (copy-buf (get-buffer-create "*copy work*"))
7171         art-group)
7172     (buffer-disable-undo copy-buf)
7173     (if (and (not to-newsgroup) (not select-method))
7174         (setq to-newsgroup
7175               (completing-read 
7176                (format "Where do you want to copy %s? "
7177                        (if (> (length articles) 1)
7178                            (format "these %d articles" (length articles))
7179                          "this article"))
7180                gnus-active-hashtb nil t 
7181                (gnus-group-real-prefix gnus-newsgroup-name))))
7182     (or (gnus-check-backend-function 'request-accept-article 
7183                                      (or select-method to-newsgroup))
7184         (error "%s does not support article copying" to-newsgroup))
7185     (message "Moving to %s: %s..." (or select-method to-newsgroup) articles)
7186     (while articles
7187       (if (setq art-group
7188                 (save-excursion
7189                   (set-buffer copy-buf)
7190                   (gnus-request-article-this-buffer
7191                    (car articles) gnus-newsgroup-name)
7192                   (gnus-request-accept-article
7193                    (if select-method (quote select-method) to-newsgroup)
7194                    (not (cdr articles)))))
7195           (let* ((entry 
7196                   (or
7197                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
7198                    (gnus-gethash 
7199                     (gnus-group-prefixed-name 
7200                      (car art-group) 
7201                      (if select-method (list select-method "")
7202                        (gnus-find-method-for-group to-newsgroup)))
7203                     gnus-newsrc-hashtb)))
7204                  (info (nth 2 entry))
7205                  (article (car articles))
7206                  (marked (nth 3 info)))
7207             (if (not (memq article gnus-newsgroup-unreads))
7208                 (setcar (cdr (cdr info))
7209                         (gnus-add-to-range (nth 2 info) 
7210                                            (list (cdr art-group)))))
7211             ;; Copy any marks over to the new group.
7212             (let ((marks '((tick . gnus-newsgroup-marked)
7213                            (dormant . gnus-newsgroup-dormant)
7214                            (expire . gnus-newsgroup-expirable)
7215                            (bookmark . gnus-newsgroup-bookmarks)
7216                         ;   (score . gnus-newsgroup-scored)
7217                            (reply . gnus-newsgroup-replied)))
7218                   (to-article (cdr art-group)))
7219               (while marks
7220                 (if (memq article (symbol-value (cdr (car marks))))
7221                     (gnus-add-marked-articles 
7222                      (car info) (car (car marks)) (list to-article) info))
7223                 (setq marks (cdr marks)))))
7224         (message "Couldn't copy article %s" (car articles)))
7225       (gnus-summary-remove-process-mark (car articles))
7226       (setq articles (cdr articles)))
7227     (kill-buffer copy-buf)))
7228
7229 (defun gnus-summary-expire-articles ()
7230   "Expire all articles that are marked as expirable in the current group."
7231   (interactive)
7232   (if (and gnus-newsgroup-expirable
7233            (gnus-check-backend-function 
7234             'request-expire-articles gnus-newsgroup-name))
7235       (let ((expirable gnus-newsgroup-expirable))
7236         ;; The list of articles that weren't expired is returned.
7237         (setq gnus-newsgroup-expirable 
7238               (gnus-request-expire-articles gnus-newsgroup-expirable
7239                                             gnus-newsgroup-name))
7240         ;; We go through the old list of expirable, and mark all
7241         ;; really expired articles as non-existant.
7242         (while expirable
7243           (or (memq (car expirable) gnus-newsgroup-expirable)
7244               (gnus-summary-mark-as-read (car expirable) "%"))
7245           (setq expirable (cdr expirable))))))
7246
7247 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7248 (defun gnus-summary-delete-article (n)
7249   "Delete the N next (mail) articles.
7250 This command actually deletes articles. This is not a marking
7251 command. The article will disappear forever from you life, never to
7252 return. 
7253 If N is negative, delete backwards.
7254 If N is nil and articles have been marked with the process mark,
7255 delete these instead."
7256   (interactive "P")
7257   (or (gnus-check-backend-function 'request-expire-articles 
7258                                    gnus-newsgroup-name)
7259       (error "The current newsgroup does not support article deletion."))
7260   ;; Compute the list of articles to delete.
7261   (let ((articles (gnus-summary-work-articles n)))
7262     (if (and gnus-novice-user
7263              (not (gnus-y-or-n-p 
7264                    (format "Do you really want to delete %s forever? "
7265                            (if (> (length articles) 1) "these articles"
7266                              "this article")))))
7267         ()
7268       ;; Delete the articles.
7269       (setq gnus-newsgroup-expirable 
7270             (gnus-request-expire-articles 
7271              articles gnus-newsgroup-name 'force))
7272       (while articles
7273         (gnus-summary-remove-process-mark (car articles))       
7274         ;; The backend might not have been able to delete the article
7275         ;; after all.  
7276         (or (memq (car articles) gnus-newsgroup-expirable)
7277             (gnus-summary-mark-as-read (car articles) gnus-canceled-mark))
7278         (setq articles (cdr articles))))))
7279
7280 (defun gnus-summary-edit-article ()
7281   "Enter into a buffer and edit the current article.
7282 This will have permanent effect only in mail groups."
7283   (interactive)
7284   (or (gnus-check-backend-function 
7285        'request-replace-article gnus-newsgroup-name)
7286       (error "The current newsgroup does not support article editing."))
7287   (gnus-summary-select-article t)
7288   (other-window 1)
7289   (message "C-c C-c to end edits")
7290   (setq buffer-read-only nil)
7291   (text-mode)
7292   (use-local-map (copy-keymap (current-local-map)))
7293   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
7294   (goto-char 1)
7295   (search-forward "\n\n" nil t))
7296
7297 (defun gnus-summary-edit-article-done ()
7298   "Make edits to the current article permanent."
7299   (interactive)
7300   (if (not (gnus-request-replace-article 
7301             (cdr gnus-article-current) (car gnus-article-current) 
7302             (current-buffer)))
7303       (error "Couldn't replace article.")
7304     (gnus-article-mode)
7305     (use-local-map gnus-article-mode-map)
7306     (setq buffer-read-only t)
7307     (pop-to-buffer gnus-summary-buffer)))      
7308
7309 ;; Summary score commands.
7310
7311 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
7312
7313 (defun gnus-summary-raise-score (n)
7314   "Raise the score of the current article by N."
7315   (interactive "p")
7316   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
7317
7318 (defun gnus-summary-lower-score (n)
7319   "Lower the score of the current article by N."
7320   (interactive "p")
7321   (gnus-summary-raise-score (- n)))
7322
7323 (defun gnus-summary-set-score (n)
7324   "Set the score of the current article to N."
7325   (interactive "p")
7326   ;; Skip dummy header line.
7327   (save-excursion
7328     (if (= (gnus-summary-article-mark) ?Z) (forward-line 1))
7329     (let ((buffer-read-only nil))
7330       ;; Set score.
7331       (gnus-summary-update-mark
7332        (if (= n (or gnus-summary-default-score 0)) ? 
7333          (if (< n (or gnus-summary-default-score 0)) 
7334              gnus-score-below-mark gnus-score-over-mark)) 'score))
7335     (let* ((article (gnus-summary-article-number))
7336            (score (assq article gnus-newsgroup-scored)))
7337       (if score (setcdr score n)
7338         (setq gnus-newsgroup-scored 
7339               (cons (cons article n) gnus-newsgroup-scored))))
7340     (gnus-summary-update-line)))
7341
7342 ;; Summary marking commands.
7343
7344 (defun gnus-summary-raise-same-subject-and-select (score)
7345   "Raise articles which has the same subject with SCORE and select the next."
7346   (interactive "p")
7347   (let ((subject (gnus-summary-subject-string)))
7348     (gnus-summary-raise-score score)
7349     (while (gnus-summary-search-subject nil nil subject)
7350       (gnus-summary-raise-score score))
7351     (gnus-summary-next-article t)))
7352
7353 (defun gnus-summary-raise-same-subject (score)
7354   "Raise articles which has the same subject with SCORE."
7355   (interactive "p")
7356   (let ((subject (gnus-summary-subject-string)))
7357     (gnus-summary-raise-score score)
7358     (while (gnus-summary-search-subject nil nil subject)
7359       (gnus-summary-raise-score score))
7360     (gnus-summary-next-subject 1 t)))
7361
7362 (defun gnus-summary-raise-thread (score)
7363   "Raise articles under current thread with SCORE."
7364   (interactive "p")
7365   (let (e)
7366     (save-excursion
7367       (let ((level (gnus-summary-thread-level)))
7368         (gnus-summary-raise-score score)
7369         (while (and (zerop (gnus-summary-next-subject 1))
7370                     (> (gnus-summary-thread-level) level))
7371           (gnus-summary-raise-score score))
7372         (setq e (point))))
7373     (or (zerop (gnus-summary-next-subject 1 t))
7374         (goto-char e)))
7375   (gnus-summary-position-cursor)
7376   (gnus-set-mode-line 'summary))
7377
7378 (defun gnus-summary-lower-same-subject-and-select (score)
7379   "Raise articles which has the same subject with SCORE and select the next."
7380   (interactive "p")
7381   (gnus-summary-raise-same-subject-and-select (- score)))
7382
7383 (defun gnus-summary-lower-same-subject (score)
7384   "Raise articles which has the same subject with SCORE."
7385   (interactive "p")
7386   (gnus-summary-raise-same-subject (- score)))
7387
7388 (defun gnus-summary-lower-thread (score)
7389   "Raise articles under current thread with SCORE."
7390   (interactive "p")
7391   (gnus-summary-raise-thread (- score)))
7392
7393 (defun gnus-summary-kill-same-subject-and-select (unmark)
7394   "Mark articles which has the same subject as read, and then select the next.
7395 If UNMARK is positive, remove any kind of mark.
7396 If UNMARK is negative, tick articles."
7397   (interactive "P")
7398   (if unmark
7399       (setq unmark (prefix-numeric-value unmark)))
7400   (let ((count
7401          (gnus-summary-mark-same-subject
7402           (gnus-summary-subject-string) unmark)))
7403     ;; Select next unread article. If auto-select-same mode, should
7404     ;; select the first unread article.
7405     (gnus-summary-next-article t (and gnus-auto-select-same
7406                                       (gnus-summary-subject-string)))
7407     (message "%d articles are marked as %s"
7408              count (if unmark "unread" "read"))))
7409
7410 (defun gnus-summary-kill-same-subject (unmark)
7411   "Mark articles which has the same subject as read. 
7412 If UNMARK is positive, remove any kind of mark.
7413 If UNMARK is negative, tick articles."
7414   (interactive "P")
7415   (if unmark
7416       (setq unmark (prefix-numeric-value unmark)))
7417   (let ((count
7418          (gnus-summary-mark-same-subject
7419           (gnus-summary-subject-string) unmark)))
7420     ;; If marked as read, go to next unread subject.
7421     (if (null unmark)
7422         ;; Go to next unread subject.
7423         (gnus-summary-next-subject 1 t))
7424     (message "%d articles are marked as %s"
7425              count (if unmark "unread" "read"))))
7426
7427 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7428   "Mark articles with same SUBJECT as read, and return marked number.
7429 If optional argument UNMARK is positive, remove any kinds of marks.
7430 If optional argument UNMARK is negative, mark articles as unread instead."
7431   (let ((count 1))
7432     (save-excursion
7433       (cond ((null unmark)
7434              (gnus-summary-mark-as-read nil gnus-killed-mark))
7435             ((> unmark 0)
7436              (gnus-summary-tick-article nil t))
7437             (t
7438              (gnus-summary-tick-article)))
7439       (while (and subject
7440                   (gnus-summary-search-forward nil subject))
7441         (cond ((null unmark)
7442                (gnus-summary-mark-as-read nil gnus-killed-mark))
7443               ((> unmark 0)
7444                (gnus-summary-tick-article nil t))
7445               (t
7446                (gnus-summary-tick-article)))
7447         (setq count (1+ count))))
7448     ;; Hide killed thread subtrees.  Does not work properly always.
7449     ;;(and (null unmark)
7450     ;;     gnus-thread-hide-killed
7451     ;;     (gnus-summary-hide-thread))
7452     ;; Return number of articles marked as read.
7453     count))
7454
7455 (defun gnus-summary-mark-as-processable (n &optional unmark)
7456   "Set the process mark on the next N articles.
7457 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7458 the process mark instead.  The difference between N and the actual
7459 number of articles marked is returned."
7460   (interactive "p")
7461   (let ((backward (< n 0))
7462         (n (abs n)))
7463   (while (and 
7464           (> n 0)
7465           (if unmark
7466               (gnus-summary-remove-process-mark (gnus-summary-article-number))
7467             (gnus-summary-set-process-mark (gnus-summary-article-number)))
7468           (zerop (gnus-summary-next-subject (if backward -1 1))))
7469     (setq n (1- n)))
7470   (if (/= 0 n) (message "No more articles"))
7471   n))
7472
7473 (defun gnus-summary-unmark-as-processable (n)
7474   "Remove the process mark from the next N articles.
7475 If N is negative, mark backward instead.  The difference between N and
7476 the actual number of articles marked is returned."
7477   (interactive "p")
7478   (gnus-summary-mark-as-processable n t))
7479
7480 (defun gnus-summary-unmark-all-processable ()
7481   "Remove the process mark from all articles."
7482   (interactive)
7483   (save-excursion
7484     (while gnus-newsgroup-processable
7485       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7486   (gnus-summary-position-cursor))
7487
7488 (defun gnus-summary-mark-as-expirable (n)
7489   "Mark N articles forward as expirable.
7490 If N is negative, mark backward instead. The difference between N and
7491 the actual number of articles marked is returned."
7492   (interactive "p")
7493   (gnus-summary-mark-forward n gnus-expirable-mark))
7494
7495 (defun gnus-summary-mark-article-as-replied (article)
7496   "Mark ARTICLE replied and update the summary line."
7497   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
7498   (let ((buffer-read-only nil))
7499     (if (gnus-summary-goto-subject article)
7500         (progn
7501           (gnus-summary-update-mark gnus-replied-mark 'replied)
7502           t))))
7503
7504 (defun gnus-summary-set-bookmark (article)
7505   "Set a bookmark in current article."
7506   (interactive (list (gnus-summary-article-number)))
7507   (if (or (not (get-buffer gnus-article-buffer))
7508           (not gnus-current-article)
7509           (not gnus-article-current)
7510           (not (equal gnus-newsgroup-name (car gnus-article-current))))
7511       (error "No current article selected"))
7512   ;; Remove old bookmark, if one exists.
7513   (let ((old (assq article gnus-newsgroup-bookmarks)))
7514     (if old (setq gnus-newsgroup-bookmarks 
7515                   (delq old gnus-newsgroup-bookmarks))))
7516   ;; Set the new bookmark, which is on the form 
7517   ;; (article-number . line-number-in-body).
7518   (setq gnus-newsgroup-bookmarks 
7519         (cons 
7520          (cons article 
7521                (save-excursion
7522                  (set-buffer gnus-article-buffer)
7523                  (count-lines
7524                   (min (point)
7525                        (save-excursion
7526                          (goto-char 1)
7527                          (search-forward "\n\n" nil t)
7528                          (point)))
7529                   (point))))
7530          gnus-newsgroup-bookmarks))
7531   (message "A bookmark has been added to the current article."))
7532
7533 (defun gnus-summary-remove-bookmark (article)
7534   "Remove the bookmark from the current article."
7535   (interactive (list (gnus-summary-article-number)))
7536   ;; Remove old bookmark, if one exists.
7537   (let ((old (assq article gnus-newsgroup-bookmarks)))
7538     (if old 
7539         (progn
7540           (setq gnus-newsgroup-bookmarks 
7541                 (delq old gnus-newsgroup-bookmarks))
7542           (message "Removed bookmark."))
7543       (message "No bookmark in current article."))))
7544
7545 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7546 (defun gnus-summary-mark-as-dormant (n)
7547   "Mark N articles forward as dormant.
7548 If N is negative, mark backward instead.  The difference between N and
7549 the actual number of articles marked is returned."
7550   (interactive "p")
7551   (gnus-summary-mark-forward n gnus-dormant-mark))
7552
7553 (defun gnus-summary-set-process-mark (article)
7554   "Set the process mark on ARTICLE and update the summary line."
7555   (setq gnus-newsgroup-processable (cons article gnus-newsgroup-processable))
7556   (let ((buffer-read-only nil))
7557     (if (gnus-summary-goto-subject article)
7558         (progn
7559           (if (= (gnus-summary-article-mark) ?Z) (forward-line 1))
7560           (gnus-summary-update-mark gnus-process-mark 'replied)
7561           t))))
7562
7563 (defun gnus-summary-remove-process-mark (article)
7564   "Remove the process mark from ARTICLE and update the summary line."
7565   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7566   (let ((buffer-read-only nil))
7567     (if (gnus-summary-goto-subject article)
7568         (progn
7569           (and (= (gnus-summary-article-mark) ?Z) (forward-line 1))
7570           (gnus-summary-update-mark ?  'replied)
7571           (if (memq article gnus-newsgroup-replied) 
7572               (gnus-summary-update-mark gnus-replied-mark 'replied))
7573           t))))
7574
7575 (defun gnus-summary-mark-forward (n &optional mark)
7576   "Mark N articles as read forwards.
7577 If N is negative, mark backwards instead.
7578 Mark with MARK. If MARK is ? , ?! or ??, articles will be
7579 marked as unread. 
7580 The difference between N and the actual number of articles marked is
7581 returned."
7582   (interactive "p")
7583   (gnus-set-global-variables)
7584   (let ((backward (< n 0))
7585         (n (abs n))
7586         (mark (or mark gnus-dread-mark)))
7587   (while (and (> n 0)
7588               (gnus-summary-mark-article nil mark)
7589               (zerop (gnus-summary-next-subject 
7590                       (if backward -1 1) gnus-summary-goto-unread)))
7591     (setq n (1- n)))
7592   (if (/= 0 n) (message "No more %sarticles" (if mark "" "unread ")))
7593   (gnus-set-mode-line 'summary)
7594   n))
7595
7596 (defun gnus-summary-mark-article (&optional article mark)
7597   "Mark ARTICLE with MARK.
7598 MARK can be any character.
7599 Five MARK strings are reserved: ?  (unread), 
7600 ?! (ticked), ?? (dormant), ?D (read), ?E (expirable).
7601 If MARK is nil, then the default character ?D is used.
7602 If ARTICLE is nil, then the article on the current line will be
7603 marked." 
7604   ;; If no mark is given, then we check auto-expiring.
7605   (and (or (not mark)
7606            (and (numberp mark) (or (= mark gnus-killed-mark)
7607                                    (= mark gnus-dread-mark)
7608                                    (= mark gnus-catchup-mark)
7609                                    (= mark gnus-low-score-mark)
7610                                    (= mark gnus-read-mark))))
7611        gnus-newsgroup-auto-expire 
7612        (setq mark gnus-expirable-mark))
7613   (let* ((buffer-read-only nil)
7614          (mark (or (and (stringp mark) (aref mark 0)) mark gnus-dread-mark))
7615          (article (or article (gnus-summary-article-number))))
7616     (if (or (= mark gnus-unread-mark) 
7617             (= mark gnus-ticked-mark) 
7618             (= mark gnus-dormant-mark))
7619         (gnus-mark-article-as-unread article mark)
7620       (gnus-mark-article-as-read article mark))
7621     (if (gnus-summary-goto-subject article)
7622         (progn
7623           (gnus-summary-show-thread)
7624           (beginning-of-line)
7625           (if (= (gnus-summary-article-mark) ?Z) (forward-line 1))
7626           ;; Fix the mark.
7627           (gnus-summary-update-mark mark 'unread)
7628           t))))
7629
7630 (defun gnus-summary-update-mark (mark type)
7631   (beginning-of-line)
7632   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
7633         plist)
7634     (if (not forward)
7635         ()
7636       (forward-char forward)
7637       (setq plist (text-properties-at (point)))
7638       (delete-char 1)
7639       (and (memq 'gnus-mark plist) (setcar (cdr (memq 'gnus-mark plist)) mark))
7640       (insert mark)
7641       (and plist (add-text-properties (1- (point)) (point) plist))
7642       (gnus-summary-update-line (eq mark gnus-unread-mark)))))
7643   
7644 (defun gnus-mark-article-as-read (article &optional mark)
7645   "Enter ARTICLE in the pertinent lists and remove it from others."
7646   ;; Make the article expirable.
7647   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-dread-mark)))
7648     (if (= mark gnus-expirable-mark)
7649         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
7650       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
7651     ;; Remove from unread and marked lists.
7652     (setq gnus-newsgroup-unreads
7653           (delq article gnus-newsgroup-unreads))
7654     (setq gnus-newsgroup-marked
7655           (delq article gnus-newsgroup-marked))
7656     (setq gnus-newsgroup-dormant
7657           (delq article gnus-newsgroup-dormant))))
7658
7659 (defun gnus-mark-article-as-unread (article &optional mark)
7660   "Enter ARTICLE in the pertinent lists and remove it from others."
7661   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-ticked-mark)))
7662     ;; Add to unread list.
7663     (or (memq article gnus-newsgroup-unreads)
7664         (setq gnus-newsgroup-unreads
7665               (cons article gnus-newsgroup-unreads)))
7666     ;; If CLEAR-MARK is non-nil, the article must be removed from marked
7667     ;; list.  Otherwise, it must be added to the list.
7668     (setq gnus-newsgroup-marked
7669           (delq article gnus-newsgroup-marked))
7670     (setq gnus-newsgroup-dormant
7671           (delq article gnus-newsgroup-dormant))
7672     (setq gnus-newsgroup-expirable 
7673           (delq article gnus-newsgroup-expirable))
7674     (if (= mark gnus-ticked-mark)
7675         (setq gnus-newsgroup-marked 
7676               (cons article gnus-newsgroup-marked)))
7677     (if (= mark gnus-dormant-mark)
7678         (setq gnus-newsgroup-dormant 
7679               (cons article gnus-newsgroup-dormant)))))
7680
7681 (defalias 'gnus-summary-mark-as-unread-forward 
7682   'gnus-summary-tick-article-forward)
7683 (make-obsolete 'gnus-summary-mark-as-unread-forward 
7684                'gnus-summary-tick-article-forward)
7685 (defun gnus-summary-tick-article-forward (n)
7686   "Tick N articles forwards.
7687 If N is negative, tick backwards instead.
7688 The difference between N and the number of articles ticked is returned."
7689   (interactive "p")
7690   (gnus-summary-mark-forward n gnus-ticked-mark))
7691
7692 (defalias 'gnus-summary-mark-as-unread-backward 
7693   'gnus-summary-tick-article-backward)
7694 (make-obsolete 'gnus-summary-mark-as-unread-backward 
7695                'gnus-summary-tick-article-backward)
7696 (defun gnus-summary-tick-article-backward (n)
7697   "Tick N articles backwards.
7698 The difference between N and the number of articles ticked is returned."
7699   (interactive "p")
7700   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
7701
7702 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
7703 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
7704 (defun gnus-summary-tick-article (&optional article clear-mark)
7705   "Mark current article as unread.
7706 Optional 1st argument ARTICLE specifies article number to be marked as unread.
7707 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
7708   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
7709                                        gnus-ticked-mark)))
7710
7711 (defun gnus-summary-mark-as-read-forward (n)
7712   "Mark N articles as read forwards.
7713 If N is negative, mark backwards instead.
7714 The difference between N and the actual number of articles marked is
7715 returned."
7716   (interactive "p")
7717   (gnus-summary-mark-forward n))
7718
7719 (defun gnus-summary-mark-as-read-backward (n)
7720   "Mark the N articles as read backwards.
7721 The difference between N and the actual number of articles marked is
7722 returned."
7723   (interactive "p")
7724   (gnus-summary-mark-forward (- n)))
7725
7726 (defun gnus-summary-mark-as-read (&optional article mark)
7727   "Mark current article as read.
7728 ARTICLE specifies the article to be marked as read.
7729 MARK specifies a string to be inserted at the beginning of the line.
7730 Any kind of string (length 1) except for a space and `-' is ok."
7731   (gnus-summary-mark-article article mark))
7732
7733 (defun gnus-summary-clear-mark-forward (n)
7734   "Clear marks from N articles forward.
7735 If N is negative, clear backward instead.
7736 The difference between N and the number of marks cleared is returned."
7737   (interactive "p")
7738   (gnus-summary-mark-forward n gnus-unread-mark))
7739
7740 (defun gnus-summary-clear-mark-backward (n)
7741   "Clear marks from N articles backward.
7742 The difference between N and the number of marks cleared is returned."
7743   (interactive "p")
7744   (gnus-summary-mark-forward (- n) gnus-unread-mark))
7745
7746 (defun gnus-summary-mark-unread-as-read ()
7747   "Intended to be used by `gnus-summary-mark-article-hook'."
7748   (or (memq gnus-current-article gnus-newsgroup-marked)
7749       (memq gnus-current-article gnus-newsgroup-dormant)
7750       (memq gnus-current-article gnus-newsgroup-expirable)
7751       (gnus-summary-mark-as-read gnus-current-article gnus-read-mark)))
7752
7753 ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
7754 (defalias 'gnus-summary-delete-marked-as-read 
7755   'gnus-summary-remove-lines-marked-as-read)
7756 (make-obsolete 'gnus-summary-delete-marked-as-read 
7757                'gnus-summary-remove-lines-marked-as-read)
7758 (defun gnus-summary-remove-lines-marked-as-read ()
7759   "Remove lines that are marked as read."
7760   (interactive)
7761   (gnus-summary-remove-lines-marked-with 
7762    (concat (mapconcat
7763             (lambda (char) (char-to-string (symbol-value char)))
7764             '(gnus-dread-mark gnus-read-mark
7765               gnus-killed-mark gnus-kill-file-mark
7766               gnus-low-score-mark gnus-expirable-mark)
7767             ""))))
7768
7769 (defalias 'gnus-summary-delete-marked-with 
7770   'gnus-summary-remove-lines-marked-with)
7771 (make-obsolete 'gnus-summary-delete-marked-with 
7772                'gnus-summary-remove-lines-marked-with)
7773 ;; Rewrite by Daniel Quinlan <quinlan@best.com>.
7774 (defun gnus-summary-remove-lines-marked-with (marks)
7775   "Remove lines that are marked with MARKS (e.g. \"DK\")."
7776   (interactive "sMarks: ")
7777   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
7778   (save-excursion
7779     (set-buffer gnus-summary-buffer)
7780     (let ((buffer-read-only nil)
7781           (marks (concat "^[" marks "]")))
7782       (goto-char (point-min))
7783       (while (search-forward-regexp marks (point-max) t)
7784         (delete-region (progn (beginning-of-line) (point))
7785                        (progn (forward-line 1) (point)))))
7786     (or (zerop (buffer-size))
7787         (if (eobp)
7788             (gnus-summary-prev-subject 1)
7789           (gnus-summary-position-cursor)))))
7790
7791 (defun gnus-summary-expunge-below (score)
7792   "Remove articles with score less than SCORE."
7793   (interactive "P")
7794   (setq score (if score
7795                   (prefix-numeric-value score)
7796                 (or gnus-summary-default-score 0)))
7797   (save-excursion
7798     (set-buffer gnus-summary-buffer)
7799     (goto-char (point-min))
7800     (let ((buffer-read-only nil)
7801           beg)
7802       (while (not (eobp))
7803         (if (< (gnus-summary-article-score) score)
7804             (progn
7805               (setq beg (point))
7806               (forward-line 1)
7807               (delete-region beg (point)))
7808           (forward-line 1)))
7809       ;; Adjust point.
7810       (or (zerop (buffer-size))
7811           (if (eobp)
7812               (gnus-summary-prev-subject 1)
7813             (gnus-summary-position-cursor))))))
7814
7815 (defun gnus-summary-mark-below (score mark)
7816   "Mark articles with score less than SCORE with MARK."
7817   (interactive "P\ncMark: ")
7818   (setq score (if score
7819                   (prefix-numeric-value score)
7820                 (or gnus-summary-default-score 0)))
7821   (save-excursion
7822     (set-buffer gnus-summary-buffer)
7823     (goto-char (point-min))
7824     (while (not (eobp))
7825       (and (< (gnus-summary-article-score) score)
7826            (gnus-summary-mark-article nil mark))
7827       (forward-line 1))))
7828
7829 (defun gnus-summary-kill-below (score)
7830   "Mark articles with score below SCORE as read."
7831   (interactive "P")
7832   (gnus-summary-mark-below score gnus-killed-mark))
7833
7834 (defun gnus-summary-clear-above (score)
7835   "Clear all marks from articles with score above SCORE."
7836   (interactive "P")
7837   (gnus-summary-mark-above score gnus-unread-mark))
7838
7839 (defun gnus-summary-tick-above (score)
7840   "Tick all articles with score above SCORE."
7841   (interactive "P")
7842   (gnus-summary-mark-above score gnus-ticked-mark))
7843
7844 (defun gnus-summary-mark-above (score mark)
7845   "Mark articles with score over SCORE with MARK."
7846   (interactive "P\ncMark: ")
7847   (setq score (if score
7848                   (prefix-numeric-value score)
7849                 (or gnus-summary-default-score 0)))
7850   (save-excursion
7851     (set-buffer gnus-summary-buffer)
7852     (goto-char (point-min))
7853     (while (not (eobp))
7854       (if (> (gnus-summary-article-score) score)
7855           (progn
7856             (gnus-summary-mark-article nil mark)
7857             (forward-line 1))
7858         (forward-line 1)))))
7859
7860 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
7861 (defun gnus-summary-show-all-expunged ()
7862   "Display all the hidden articles that were expunged for low scores."
7863   (interactive)
7864   (let ((buffer-read-only nil))
7865     (let ((scored gnus-newsgroup-scored)
7866           headers h)
7867       (while scored
7868         (or (gnus-summary-goto-subject (car (car scored)))
7869             (and (setq h (gnus-get-header-by-number (car (car scored))))
7870                  (< (cdr (car scored)) gnus-summary-expunge-below)
7871                  (setq headers (cons h headers))))
7872         (setq scored (cdr scored)))
7873       (or headers (error "No expunged articles hidden."))
7874       (goto-char (point-min))
7875       (save-excursion 
7876         (gnus-summary-prepare-threads (nreverse headers) 0)))
7877     (goto-char (point-min))
7878     (gnus-summary-position-cursor)))
7879
7880 (defun gnus-summary-show-all-dormant ()
7881   "Display all the hidden articles that are marked as dormant."
7882   (interactive)
7883   (let ((buffer-read-only nil))
7884     (goto-char (point-min))
7885     (let ((dormant gnus-newsgroup-dormant)
7886           headers h)
7887       (while dormant
7888         (or (gnus-summary-goto-subject (car dormant))
7889             (and (setq h (gnus-get-header-by-number (car dormant)))
7890                  (setq headers (cons h headers))))
7891         (setq dormant (cdr dormant)))
7892       (or headers (error "No dormant articles hidden."))
7893       (save-excursion (gnus-summary-prepare-threads (nreverse headers) 0)))
7894     (goto-char (point-min))
7895     (gnus-summary-position-cursor)))
7896
7897 (defun gnus-summary-hide-all-dormant ()
7898   "Hide all dormant articles."
7899   (interactive)
7900   (gnus-summary-remove-lines-marked-with (char-to-string gnus-dormant-mark))
7901   (gnus-summary-position-cursor))
7902
7903 (defun gnus-summary-catchup (all &optional quietly to-here)
7904   "Mark all articles not marked as unread in this newsgroup as read.
7905 If prefix argument ALL is non-nil, all articles are marked as read.
7906 If QUIETLY is non-nil, no questions will be asked.
7907 If TO-HERE is non-nil, it should be a point in the buffer. All
7908 articles before this point will be marked as read.
7909 The number of articles marked as read is returned."
7910   (interactive "P")
7911   (prog1
7912       (if (or quietly
7913               (not gnus-interactive-catchup) ;Without confirmation?
7914               gnus-expert-user
7915               (gnus-y-or-n-p
7916                (if all
7917                    "Mark absolutely all articles as read? "
7918                  "Mark all unread articles as read? ")))
7919           (let ((unreads (length gnus-newsgroup-unreads)))
7920             (if (gnus-summary-first-subject (not all))
7921                 (while (and (gnus-summary-mark-as-read nil gnus-catchup-mark)
7922                             (if to-here (< (point) to-here) t)
7923                             (gnus-summary-search-subject nil (not all)))))
7924             (- unreads (length gnus-newsgroup-unreads))))
7925     (setq gnus-newsgroup-unreads gnus-newsgroup-marked)
7926     (gnus-summary-position-cursor)))
7927
7928 (defun gnus-summary-catchup-to-here (&optional all)
7929   "Mark all unticked articles before the current one as read.
7930 If ALL is non-nil, also mark ticked and dormant articles as read."
7931   (interactive)
7932   (beginning-of-line)
7933   (gnus-summary-catchup all t (point))
7934   (gnus-summary-position-cursor))
7935
7936 (defun gnus-summary-catchup-all (&optional quietly)
7937   "Mark all articles in this newsgroup as read."
7938   (interactive)
7939   (gnus-summary-catchup t quietly))
7940
7941 (defun gnus-summary-catchup-and-exit (all &optional quietly)
7942   "Mark all articles not marked as unread in this newsgroup as read, then exit.
7943 If prefix argument ALL is non-nil, all articles are marked as read."
7944   (interactive "P")
7945   (gnus-summary-catchup all quietly)
7946   ;; Select next newsgroup or exit.
7947   (if (eq gnus-auto-select-next 'quietly)
7948       (gnus-summary-next-group nil)
7949     (gnus-summary-exit)))
7950
7951 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
7952   "Mark all articles in this newsgroup as read, and then exit."
7953   (interactive)
7954   (gnus-summary-catchup-and-exit t quietly))
7955
7956 ;; Thread-based commands.
7957
7958 (defun gnus-summary-toggle-threads (arg)
7959   "Toggle showing conversation threads.
7960 If ARG is positive number, turn showing conversation threads on."
7961   (interactive "P")
7962   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
7963     (setq gnus-show-threads
7964           (if (null arg) (not gnus-show-threads)
7965             (> (prefix-numeric-value arg) 0)))
7966     (gnus-summary-prepare)
7967     (gnus-summary-goto-subject current)
7968     (gnus-summary-position-cursor)))
7969
7970 (defun gnus-summary-show-all-threads ()
7971   "Show all threads."
7972   (interactive)
7973   (save-excursion
7974     (let ((buffer-read-only nil))
7975       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
7976   (gnus-summary-position-cursor))
7977
7978 (defun gnus-summary-show-thread ()
7979   "Show thread subtrees.
7980 Returns nil if no thread was there to be shown."
7981   (interactive)
7982   (prog1
7983       (save-excursion
7984         (let ((buffer-read-only nil)
7985               (beg (progn (beginning-of-line) (point)))
7986               (end (save-excursion (end-of-line) (point))))
7987           (prog1
7988               ;; Any hidden lines here?
7989               (search-forward "\r" end t)
7990             (subst-char-in-region beg end ?\^M ?\n t))))
7991     (gnus-summary-position-cursor)))
7992
7993 (defun gnus-summary-hide-all-threads ()
7994   "Hide all thread subtrees."
7995   (interactive)
7996   (save-excursion
7997     (goto-char (point-min))
7998     (gnus-summary-hide-thread)
7999     (while (and (not (eobp)) (zerop (forward-line 1)))
8000       (gnus-summary-hide-thread)))
8001   (gnus-summary-position-cursor))
8002
8003 (defun gnus-summary-hide-thread ()
8004   "Hide thread subtrees.
8005 Returns nil if no threads were there to be hidden."
8006   (interactive)
8007   (let ((buffer-read-only nil)
8008         (start (point))
8009         (level (gnus-summary-thread-level))
8010         (end (point)))
8011     ;; Go forward until either the buffer ends or the subthread
8012     ;; ends. 
8013     (if (eobp)
8014         ()
8015       (while (and (zerop (forward-line 1))
8016                   (> (gnus-summary-thread-level) level))
8017         (setq end (point)))
8018       (prog1
8019           (save-excursion
8020             (goto-char end)
8021             (search-backward "\n" start t))
8022         (subst-char-in-region start end ?\n ?\^M t)
8023         (forward-line -1)))))
8024
8025 (defun gnus-summary-go-to-next-thread (&optional previous)
8026   "Go to the same level (or less) next thread.
8027 If PREVIOUS is non-nil, go to previous thread instead.
8028 Return the article number moved to, or nil if moving was impossible."
8029   (let ((level (gnus-summary-thread-level))
8030         (article (gnus-summary-article-number)))
8031     (if previous 
8032         (while (and (zerop (gnus-summary-prev-subject 1))
8033                     (> (gnus-summary-thread-level) level)))
8034       (while (and (zerop (gnus-summary-next-subject 1))
8035                   (> (gnus-summary-thread-level) level))))
8036     (let ((oart (gnus-summary-article-number)))
8037       (and (/= oart article) oart))))
8038
8039 (defun gnus-summary-next-thread (n)
8040   "Go to the same level next N'th thread.
8041 If N is negative, search backward instead.
8042 Returns the difference between N and the number of skips actually
8043 done."
8044   (interactive "p")
8045   (let ((backward (< n 0))
8046         (n (abs n)))
8047   (while (and (> n 0)
8048               (gnus-summary-go-to-next-thread backward))
8049     (setq n (1- n)))
8050   (gnus-summary-position-cursor)
8051   (if (/= 0 n) (message "No more threads"))
8052   n))
8053
8054 (defun gnus-summary-prev-thread (n)
8055   "Go to the same level previous N'th thread.
8056 Returns the difference between N and the number of skips actually
8057 done."
8058   (interactive "p")
8059   (gnus-summary-next-thread (- n)))
8060
8061 (defun gnus-summary-go-down-thread (&optional same)
8062   "Go down one level in the current thread.
8063 If SAME is non-nil, also move to articles of the same level."
8064   (let ((level (gnus-summary-thread-level))
8065         (start (point)))
8066     (if (and (zerop (forward-line 1))
8067              (> (gnus-summary-thread-level) level))
8068         t
8069       (goto-char start)
8070       nil)))
8071
8072 (defun gnus-summary-go-up-thread ()
8073   "Go up one level in the current thread."
8074   (let ((level (gnus-summary-thread-level))
8075         (start (point)))
8076     (while (and (zerop (forward-line -1))
8077                 (>= (gnus-summary-thread-level) level)))
8078     (if (>= (gnus-summary-thread-level) level)
8079         (progn
8080           (goto-char start)
8081           nil)
8082       t)))
8083
8084 (defun gnus-summary-down-thread (n)
8085   "Go down thread N steps.
8086 If N is negative, go up instead.
8087 Returns the difference between N and how many steps down that were
8088 taken."
8089   (interactive "p")
8090   (let ((up (< n 0))
8091         (n (abs n)))
8092   (while (and (> n 0)
8093               (if up (gnus-summary-go-up-thread)
8094                 (gnus-summary-go-down-thread)))
8095     (setq n (1- n)))
8096   (gnus-summary-position-cursor)
8097   (if (/= 0 n) (message "Can't go further"))
8098   n))
8099
8100 (defun gnus-summary-up-thread (n)
8101   "Go up thread N steps.
8102 If N is negative, go up instead.
8103 Returns the difference between N and how many steps down that were
8104 taken."
8105   (interactive "p")
8106   (gnus-summary-down-thread (- n)))
8107
8108 (defun gnus-summary-kill-thread (unmark)
8109   "Mark articles under current thread as read.
8110 If the prefix argument is positive, remove any kinds of marks.
8111 If the prefix argument is negative, tick articles instead."
8112   (interactive "P")
8113   (if unmark
8114       (setq unmark (prefix-numeric-value unmark)))
8115   (let ((killing t)
8116         (level (gnus-summary-thread-level)))
8117     (save-excursion
8118       (while killing
8119         ;; Mark the article...
8120         (cond ((null unmark) (gnus-summary-mark-as-read nil gnus-killed-mark))
8121               ((> unmark 0) (gnus-summary-tick-article nil t))
8122               (t (gnus-summary-tick-article)))
8123         ;; ...and go forward until either the buffer ends or the subtree
8124         ;; ends. 
8125         (if (not (and (zerop (forward-line 1))
8126                       (> (gnus-summary-thread-level) level)))
8127             (setq killing nil))))
8128     ;; Hide killed subtrees.
8129     (and (null unmark)
8130          gnus-thread-hide-killed
8131          (gnus-summary-hide-thread))
8132     ;; If marked as read, go to next unread subject.
8133     (if (null unmark)
8134         ;; Go to next unread subject.
8135         (gnus-summary-next-subject 1 t)))
8136   (gnus-set-mode-line 'summary))
8137
8138 ;; Summary sorting commands
8139
8140 (defun gnus-summary-sort-by-number (reverse)
8141   "Sort summary buffer by article number.
8142 Argument REVERSE means reverse order."
8143   (interactive "P")
8144   (gnus-summary-sort 
8145    (cons 'gnus-summary-article-number 'gnus-thread-sort-by-number) reverse))
8146
8147 (defun gnus-summary-sort-by-author (reverse)
8148   "Sort summary buffer by author name alphabetically.
8149 If case-fold-search is non-nil, case of letters is ignored.
8150 Argument REVERSE means reverse order."
8151   (interactive "P")
8152   (gnus-summary-sort
8153    (cons
8154     (lambda ()
8155       (let ((extract (funcall
8156                       gnus-extract-address-components
8157                       (header-from (gnus-get-header-by-number
8158                                     (gnus-summary-article-number))))))
8159         (or (car extract) (cdr extract))))
8160     'gnus-thread-sort-by-author)
8161    reverse))
8162
8163 (defun gnus-summary-sort-by-subject (reverse)
8164   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
8165 If case-fold-search is non-nil, case of letters is ignored.
8166 Argument REVERSE means reverse order."
8167   (interactive "P")
8168   (gnus-summary-sort
8169    (cons
8170     (lambda ()
8171       (downcase (gnus-simplify-subject (gnus-summary-subject-string))))
8172     'gnus-thread-sort-by-subject)
8173    reverse))
8174
8175 (defun gnus-summary-sort-by-date (reverse)
8176   "Sort summary buffer by date.
8177 Argument REVERSE means reverse order."
8178   (interactive "P")
8179   (gnus-summary-sort
8180    (cons
8181     (lambda ()
8182       (gnus-sortable-date
8183        (header-date 
8184         (gnus-get-header-by-number (gnus-summary-article-number)))))
8185     'gnus-thread-sort-by-date)
8186    reverse))
8187
8188 (defun gnus-summary-sort-by-score (reverse)
8189   "Sort summary buffer by score.
8190 Argument REVERSE means reverse order."
8191   (interactive "P")
8192   (gnus-summary-sort 
8193    (cons 'gnus-summary-article-score 'gnus-thread-sort-by-score)
8194    (not reverse)))
8195
8196 (defun gnus-summary-sort (predicate reverse)
8197   ;; Sort summary buffer by PREDICATE.  REVERSE means reverse order. 
8198   (let (buffer-read-only)
8199     (if (not gnus-show-threads)
8200         (progn
8201           (goto-char (point-min))
8202           (sort-subr reverse 'forward-line 'end-of-line (car predicate)))
8203       (let ((gnus-thread-sort-functions (list (cdr predicate))))
8204         (gnus-summary-prepare)))))
8205
8206 (defun gnus-sortable-date (date)
8207   "Make sortable string by string-lessp from DATE.
8208 Timezone package is used."
8209   (let* ((date   (timezone-fix-time date nil nil)) ;[Y M D H M S]
8210          (year   (aref date 0))
8211          (month  (aref date 1))
8212          (day    (aref date 2)))
8213     (timezone-make-sortable-date 
8214      year month day 
8215      (timezone-make-time-string
8216       (aref date 3) (aref date 4) (aref date 5)))))
8217
8218
8219 ;; Summary saving commands.
8220
8221 (defun gnus-summary-save-article (n)
8222   "Save the current article using the default saver function.
8223 If N is a positive number, save the N next articles.
8224 If N is a negative number, save the N previous articles.
8225 If N is nil and any articles have been marked with the process mark,
8226 save those articles instead.
8227 The variable `gnus-default-article-saver' specifies the saver function."
8228   (interactive "P")
8229   (let (articles process)
8230     (if (and n (numberp n))
8231         (let ((backward (< n 0))
8232               (n (abs n)))
8233           (save-excursion
8234             (while (and (> n 0)
8235                         (setq articles (cons (gnus-summary-article-number) 
8236                                              articles))
8237                         (gnus-summary-search-forward nil nil backward))
8238               (setq n (1- n))))
8239           (setq articles (sort articles (function <))))
8240       (if gnus-newsgroup-processable
8241           (progn
8242             (setq articles (setq gnus-newsgroup-processable
8243                                  (nreverse gnus-newsgroup-processable)))
8244             (setq process t))
8245         (setq articles (list (gnus-summary-article-number)))))
8246     (while articles
8247       (let ((header (gnus-gethash (int-to-string (car articles))
8248                                   gnus-newsgroup-headers-hashtb-by-number)))
8249         (if (vectorp header)
8250             (progn
8251               (gnus-summary-display-article (car articles) t)
8252               (if (not gnus-save-all-headers)
8253                   (gnus-article-hide-headers t))
8254               (if gnus-default-article-saver
8255                   (funcall gnus-default-article-saver)
8256                 (error "No default saver is defined.")))
8257           (if (assq 'name header)
8258               (gnus-copy-file (cdr (assq 'name header)))
8259             (message "Article %d is unsaveable" (car articles)))))
8260       (if process
8261           (gnus-summary-remove-process-mark (car articles)))
8262       (setq articles (cdr articles)))
8263     (if process (setq gnus-newsgroup-processable 
8264                       (nreverse gnus-newsgroup-processable)))
8265     (gnus-summary-position-cursor)
8266     n))
8267
8268 (defun gnus-summary-pipe-output (arg)
8269   "Pipe the current article to a subprocess.
8270 If N is a positive number, pipe the N next articles.
8271 If N is a negative number, pipe the N previous articles.
8272 If N is nil and any articles have been marked with the process mark,
8273 pipe those articles instead."
8274   (interactive "P")
8275   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8276     (gnus-summary-save-article arg)))
8277
8278 (defun gnus-summary-save-article-mail (arg)
8279   "Append the current article to an mail file.
8280 If N is a positive number, save the N next articles.
8281 If N is a negative number, save the N previous articles.
8282 If N is nil and any articles have been marked with the process mark,
8283 save those articles instead."
8284   (interactive "P")
8285   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8286     (gnus-summary-save-article arg)))
8287
8288 (defun gnus-summary-save-article-rmail (arg)
8289   "Append the current article to an rmail file.
8290 If N is a positive number, save the N next articles.
8291 If N is a negative number, save the N previous articles.
8292 If N is nil and any articles have been marked with the process mark,
8293 save those articles instead."
8294   (interactive "P")
8295   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
8296     (gnus-summary-save-article arg)))
8297
8298 (defun gnus-summary-save-article-file (arg)
8299   "Append the current article to a file.
8300 If N is a positive number, save the N next articles.
8301 If N is a negative number, save the N previous articles.
8302 If N is nil and any articles have been marked with the process mark,
8303 save those articles instead."
8304   (interactive "P")
8305   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8306     (gnus-summary-save-article arg)))
8307
8308 (defun gnus-summary-save-article-folder (arg)
8309   "Append the current article to an mh folder.
8310 If N is a positive number, save the N next articles.
8311 If N is a negative number, save the N previous articles.
8312 If N is nil and any articles have been marked with the process mark,
8313 save those articles instead."
8314   (interactive "P")
8315   (let ((gnus-default-article-saver 'gnus-summary-save-in-folder))
8316     (gnus-summary-save-article arg)))
8317
8318 (defun gnus-summary-save-in-rmail (&optional filename)
8319   "Append this article to Rmail file.
8320 Optional argument FILENAME specifies file name.
8321 Directory to save to is default to `gnus-article-save-directory' which
8322 is initialized from the SAVEDIR environment variable."
8323   (interactive)
8324   (let ((default-name
8325           (funcall gnus-rmail-save-name gnus-newsgroup-name
8326                    gnus-current-headers gnus-newsgroup-last-rmail)))
8327     (or filename
8328         (setq filename
8329               (read-file-name
8330                (concat "Save article in rmail file: (default "
8331                        (file-name-nondirectory default-name) ") ")
8332                (file-name-directory default-name)
8333                default-name)))
8334     (gnus-make-directory (file-name-directory filename))
8335     (gnus-eval-in-buffer-window 
8336      gnus-article-buffer
8337      (save-excursion
8338        (save-restriction
8339          (widen)
8340          (gnus-output-to-rmail filename))))
8341     ;; Remember the directory name to save articles.
8342     (setq gnus-newsgroup-last-rmail filename)))
8343
8344 (defun gnus-summary-save-in-mail (&optional filename)
8345   "Append this article to Unix mail file.
8346 Optional argument FILENAME specifies file name.
8347 Directory to save to is default to `gnus-article-save-directory' which
8348 is initialized from the SAVEDIR environment variable."
8349   (interactive)
8350   (let ((default-name
8351           (funcall gnus-mail-save-name gnus-newsgroup-name
8352                    gnus-current-headers gnus-newsgroup-last-mail)))
8353     (or filename
8354         (setq filename
8355               (read-file-name
8356                (concat "Save article in Unix mail file: (default "
8357                        (file-name-nondirectory default-name) ") ")
8358                (file-name-directory default-name)
8359                default-name)))
8360     (setq filename
8361           (expand-file-name filename
8362                             (and default-name
8363                                  (file-name-directory default-name))))
8364     (gnus-make-directory (file-name-directory filename))
8365     (gnus-eval-in-buffer-window 
8366      gnus-article-buffer
8367      (save-excursion
8368        (save-restriction
8369          (widen)
8370          (if (and (file-readable-p filename) (rmail-file-p filename))
8371              (gnus-output-to-rmail filename)
8372            (rmail-output filename 1 t t)))))
8373     ;; Remember the directory name to save articles.
8374     (setq gnus-newsgroup-last-mail filename)))
8375
8376 (defun gnus-summary-save-in-file (&optional filename)
8377   "Append this article to file.
8378 Optional argument FILENAME specifies file name.
8379 Directory to save to is default to `gnus-article-save-directory' which
8380 is initialized from the SAVEDIR environment variable."
8381   (interactive)
8382   (let ((default-name
8383           (funcall gnus-file-save-name gnus-newsgroup-name
8384                    gnus-current-headers gnus-newsgroup-last-file)))
8385     (or filename
8386         (setq filename
8387               (read-file-name
8388                (concat "Save article in file: (default "
8389                        (file-name-nondirectory default-name) ") ")
8390                (file-name-directory default-name)
8391                default-name)))
8392     (gnus-make-directory (file-name-directory filename))
8393     (gnus-eval-in-buffer-window 
8394      gnus-article-buffer
8395      (save-excursion
8396        (save-restriction
8397          (widen)
8398          (gnus-output-to-file filename))))
8399     ;; Remember the directory name to save articles.
8400     (setq gnus-newsgroup-last-file filename)))
8401
8402 (defun gnus-summary-save-in-pipe (&optional command)
8403   "Pipe this article to subprocess."
8404   (interactive)
8405   (let ((command (read-string "Shell command on article: "
8406                               gnus-last-shell-command)))
8407     (if (string-equal command "")
8408         (setq command gnus-last-shell-command))
8409     (gnus-eval-in-buffer-window 
8410      gnus-article-buffer
8411      (save-restriction
8412        (widen)
8413        (shell-command-on-region (point-min) (point-max) command nil)))
8414     (setq gnus-last-shell-command command)))
8415
8416 ;; Summary extract commands
8417
8418 (defun gnus-summary-insert-pseudos (pslist)
8419   (let ((buffer-read-only nil)
8420         (article (gnus-summary-article-number))
8421         b)
8422     (or (gnus-summary-goto-subject article)
8423         (error (format "No such article: %d" article)))
8424     (gnus-summary-position-cursor)
8425     (if gnus-view-pseudos
8426         (while pslist
8427           (and (assq 'execute (car pslist))
8428                (gnus-execute-command (cdr (assq 'execute (car pslist)))
8429                                      (eq gnus-view-pseudos 'not-confirm)))
8430           (setq pslist (cdr pslist)))
8431       (save-excursion
8432         (while pslist
8433           (gnus-summary-goto-subject (or (cdr (assq 'article (car pslist)))
8434                                          (gnus-summary-article-number)))
8435           (forward-line 1)
8436           (setq b (point))
8437           (insert "          " (file-name-nondirectory 
8438                                 (cdr (assq 'name (car pslist))))
8439                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
8440           (add-text-properties 
8441            b (1+ b) (list 'gnus-subject (cdr (assq 'name (car pslist)))
8442                           'gnus-number gnus-reffed-article-number
8443                           'gnus-mark gnus-unread-mark
8444                           'gnus-pseudo (car pslist)
8445                           'gnus-thread 0))
8446           (gnus-sethash (int-to-string gnus-reffed-article-number)
8447                         (car pslist) gnus-newsgroup-headers-hashtb-by-number)
8448           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
8449           (setq pslist (cdr pslist)))))))
8450
8451 (defun gnus-request-pseudo-article (props)
8452   (cond ((assq 'execute props)
8453          (gnus-execute-command (cdr (assq 'execute props)))))
8454   (let ((gnus-current-article (gnus-summary-article-number)))
8455     (run-hooks 'gnus-mark-article-hook)))
8456
8457 (defun gnus-execute-command (command &optional automatic)
8458   (save-excursion
8459     (gnus-article-setup-buffer)
8460     (set-buffer gnus-article-buffer)
8461     (let ((command (if automatic command (read-string "Command: " command)))
8462           (buffer-read-only nil))
8463       (erase-buffer)
8464       (insert "$ " command "\n\n")
8465       (if gnus-view-pseudo-asynchronously
8466           (start-process "gnus-execute" nil "sh" "-c" command)
8467         (call-process "sh" nil t nil "-c" command)))))
8468
8469 (defun gnus-copy-file (file &optional to)
8470   "Copy FILE to TO."
8471   (interactive
8472    (list (read-file-name "Copy file: " default-directory)
8473          (read-file-name "Copy file to: " default-directory)))
8474   (or to (setq to (read-file-name "Copy file to: " default-directory)))
8475   (and (file-directory-p to) 
8476        (setq to (concat (file-name-as-directory to)
8477                         (file-name-nondirectory file))))
8478   (copy-file file to))
8479
8480 ;; Summary score file commands
8481
8482 ;; Much modification of the kill (ahem, score) code and lots of the
8483 ;; functions are written by Per Abrahamsen <amanda@iesd.auc.dk>.
8484
8485 (defun gnus-summary-header (header)
8486   ;; Return HEADER for current articles, or error.
8487   (let ((article (gnus-summary-article-number)))
8488     (if article
8489         (aref (gnus-get-header-by-number article)
8490               (nth 1 (assoc header gnus-header-index)))
8491       (error "No article on current line"))))
8492
8493 (defun gnus-summary-score-entry (header match type score date &optional prompt)
8494   "Enter score file entry.
8495 HEADER is the header being scored.
8496 MATCH is the string we are looking for.
8497 TYPE is a flag indicating if it is a regexp or substring.
8498 SCORE is the score to add.
8499 DATE is the expire date."
8500   (interactive
8501    (list (completing-read "Header: "
8502                           gnus-header-index
8503                           (lambda (x) (fboundp (nth 2 x)))
8504                           t)
8505          (read-string "Match: ")
8506          (y-or-n-p "Use regexp match? ")
8507          (prefix-numeric-value current-prefix-arg)
8508          (if (y-or-n-p "Expire kill? ")
8509              (current-time-string)
8510            nil)))
8511   (and prompt (setq match (read-string "Match: " match)))
8512   (let ((score (gnus-score-default score)))
8513     (gnus-summary-score-effect header match type score)
8514     (and (= score gnus-score-interactive-default-score)
8515          (setq score nil))
8516     (let ((new (cond (type
8517                   (list match score (and date (gnus-day-number date)) type))
8518                  (date
8519                   (list match score (gnus-day-number date)))
8520                  (score
8521                   (list match score))
8522                  (t
8523                   (list match))))
8524           (old (gnus-score-get header)))
8525       (gnus-score-set
8526        header
8527        (if old (cons new old) (list new))))
8528     (gnus-score-set 'touched '(t))))
8529
8530 (defun gnus-summary-score-effect (header match type score)
8531   "Simulate the effect of a score file entry.
8532 HEADER is the header being scored.
8533 MATCH is the string we are looking for.
8534 TYPE is a flag indicating if it is a regexp or substring.
8535 SCORE is the score to add."
8536   (interactive (list (completing-read "Header: "
8537                                       gnus-header-index
8538                                       (lambda (x) (fboundp (nth 2 x)))
8539                                       t)
8540                      (read-string "Match: ")
8541                      (y-or-n-p "Use regexp match? ")
8542                      (prefix-numeric-value current-prefix-arg)))
8543   (save-excursion
8544     (or (and (stringp match) (> (length match) 0))
8545       (error "No match"))
8546     (goto-char (point-min))
8547     (let ((regexp (if type
8548                       match
8549                     (concat "\\`.*" (regexp-quote match) ".*\\'"))))
8550       (while (not (eobp))
8551         (let ((content (gnus-summary-header header))
8552               (case-fold-search t))
8553           (and content
8554                (if (string-match regexp content)
8555                    (gnus-summary-raise-score score))))
8556         (beginning-of-line 2)))))
8557
8558 (defun gnus-summary-score-crossposting (score date)
8559    ;; Enter score file entry for current crossposting.
8560    ;; SCORE is the score to add.
8561    ;; DATE is the expire date.
8562    (let ((xref (gnus-summary-header "xref"))
8563          (start 0)
8564          group)
8565      (or xref (error "This article is not crossposted"))
8566      (while (string-match " \\([^ \t]+\\):" xref start)
8567        (setq start (match-end 0))
8568        (if (not (string= 
8569                  (setq group 
8570                        (substring xref (match-beginning 1) (match-end 1)))
8571                  gnus-newsgroup-name))
8572            (gnus-summary-score-entry
8573             "xref" (concat " " group ":") nil score date t)))))
8574
8575 (defun gnus-summary-temporarily-lower-by-subject (level)
8576   "Temporarily lower score by LEVEL for current subject.
8577 See `gnus-score-expiry-days'."
8578   (interactive "P")
8579   (gnus-summary-score-entry
8580    "subject" (gnus-simplify-subject-re (gnus-summary-header "subject"))
8581    nil (- (gnus-score-default level))
8582    (current-time-string) t))
8583
8584 (defun gnus-summary-temporarily-lower-by-author (level)
8585   "Temporarily lower score by LEVEL for current author.
8586 See `gnus-score-expiry-days'."
8587   (interactive "P")
8588   (gnus-summary-score-entry
8589    "from" (gnus-summary-header "from") nil (- (gnus-score-default level)) 
8590    (current-time-string) t))
8591
8592 (defun gnus-summary-temporarily-lower-by-id (level)
8593   "Temporarily lower score by LEVEL for current message-id.
8594 See `gnus-score-expiry-days'."
8595   (interactive "P")
8596   (gnus-summary-score-entry
8597    "message-id" (gnus-summary-header "message-id") 
8598    nil (- (gnus-score-default level)) 
8599    (current-time-string)))
8600
8601 (defun gnus-summary-temporarily-lower-by-xref (level)
8602   "Temporarily lower score by LEVEL for current xref.
8603 See `gnus-score-expiry-days'."
8604   (interactive "P")
8605   (gnus-summary-score-crossposting 
8606    (- (gnus-score-default level)) (current-time-string)))
8607
8608 (defun gnus-summary-temporarily-lower-by-thread (level)
8609   "Temporarily lower score by LEVEL for current thread.
8610 See `gnus-score-expiry-days'."
8611   (interactive "P")
8612   (gnus-summary-score-entry
8613    "references" (gnus-summary-header "message-id")
8614    nil (- (gnus-score-default level)) (current-time-string)))
8615
8616 (defun gnus-summary-lower-by-subject (level)
8617   "Lower score by LEVEL for current subject."
8618   (interactive "P")
8619   (gnus-summary-score-entry
8620    "subject" (gnus-simplify-subject-re (gnus-summary-header "subject"))
8621    nil (- (gnus-score-default level)) 
8622    nil t))
8623
8624 (defun gnus-summary-lower-by-author (level)
8625   "Lower score by LEVEL for current author."
8626   (interactive "P")
8627   (gnus-summary-score-entry
8628    "from" (gnus-summary-header "from") nil 
8629    (- (gnus-score-default level)) nil t))
8630
8631 (defun gnus-summary-lower-by-id (level)
8632   "Lower score by LEVEL for current message-id."
8633   (interactive "P")
8634   (gnus-summary-score-entry
8635    "message-id" (gnus-summary-header "message-id") nil 
8636    (- (gnus-score-default level)) nil))
8637
8638 (defun gnus-summary-lower-by-xref (level)
8639   "Lower score by LEVEL for current xref."
8640   (interactive "P")
8641   (gnus-summary-score-crossposting (- (gnus-score-default level)) nil))
8642
8643 (defun gnus-summary-lower-followups-to-author (level)
8644   "Lower score by LEVEL for all followups to the current author."
8645   (interactive "P")
8646   (gnus-summary-raise-followups-to-author
8647    (- (gnus-score-default level))))
8648
8649 (defun gnus-summary-temporarily-raise-by-subject (level)
8650   "Temporarily raise score by LEVEL for current subject.
8651 See `gnus-score-expiry-days'."
8652   (interactive "P")
8653   (gnus-summary-score-entry
8654    "subject" (gnus-simplify-subject-re (gnus-summary-header "subject"))
8655    nil level (current-time-string) t))
8656
8657 (defun gnus-summary-temporarily-raise-by-author (level)
8658   "Temporarily raise score by LEVEL for current author.
8659 See `gnus-score-expiry-days'."
8660   (interactive "P")
8661   (gnus-summary-score-entry
8662    "from" (gnus-summary-header "from") nil level (current-time-string) t))
8663
8664 (defun gnus-summary-temporarily-raise-by-id (level)
8665   "Temporarily raise score by LEVEL for current message-id.
8666 See `gnus-score-expiry-days'."
8667   (interactive "P")
8668   (gnus-summary-score-entry
8669    "message-id" (gnus-summary-header "message-id") 
8670    nil level (current-time-string)))
8671
8672 (defun gnus-summary-temporarily-raise-by-xref (level)
8673   "Temporarily raise score by LEVEL for current xref.
8674 See `gnus-score-expiry-days'."
8675   (interactive "P")
8676   (gnus-summary-score-crossposting level (current-time-string)))
8677
8678 (defun gnus-summary-temporarily-raise-by-thread (level)
8679   "Temporarily raise score by LEVEL for current thread.
8680 See `gnus-score-expiry-days'."
8681   (interactive "P")
8682   (gnus-summary-score-entry
8683    "references" (gnus-summary-header "message-id")
8684    nil level (current-time-string)))
8685
8686 (defun gnus-summary-raise-by-subject (level)
8687   "Raise score by LEVEL for current subject."
8688   (interactive "P")
8689   (gnus-summary-score-entry
8690    "subject" (gnus-simplify-subject-re (gnus-summary-header "subject"))
8691    nil level nil t))
8692
8693 (defun gnus-summary-raise-by-author (level)
8694   "Raise score by LEVEL for current author."
8695   (interactive "P")
8696   (gnus-summary-score-entry
8697    "from" (gnus-summary-header "from") nil level nil t))
8698
8699 (defun gnus-summary-raise-by-id (level)
8700   "Raise score by LEVEL for current message-id."
8701   (interactive "P")
8702   (gnus-summary-score-entry
8703    "message-id" (gnus-summary-header "message-id") nil level nil))
8704
8705 (defun gnus-summary-raise-by-xref (level)
8706   "Raise score by LEVEL for current xref."
8707   (interactive "P")
8708   (gnus-summary-score-crossposting level nil))
8709
8710 (defun gnus-summary-raise-followups-to-author (level)
8711   "Raise score by LEVEL for all followups to the current author."
8712   (interactive "P")
8713   (let ((article (gnus-summary-article-number)))
8714     (if article (setq gnus-current-headers (gnus-get-header-by-number article))
8715       (error "No article on current line")))
8716   (gnus-kill-file-raise-followups-to-author
8717    (gnus-score-default level)))
8718
8719 ;; Summary kill commands.
8720
8721 (defun gnus-summary-edit-global-kill (article)
8722   "Edit the global score file."
8723   (interactive (list (gnus-summary-article-number)))
8724   (gnus-group-edit-global-kill article))
8725
8726 (defun gnus-summary-edit-local-kill ()
8727   "Edit a local score file applied to the current newsgroup."
8728   (interactive)
8729   (setq gnus-current-headers 
8730         (gnus-gethash 
8731          (int-to-string (gnus-summary-article-number))
8732          gnus-newsgroup-headers-hashtb-by-number))
8733   (gnus-set-global-variables)
8734   (gnus-group-edit-local-kill 
8735    (gnus-summary-article-number) gnus-newsgroup-name))
8736
8737 \f
8738 ;;;
8739 ;;; Gnus article mode
8740 ;;;
8741
8742 (if gnus-article-mode-map
8743     nil
8744   (setq gnus-article-mode-map (make-keymap))
8745   (suppress-keymap gnus-article-mode-map)
8746   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
8747   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
8748   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
8749   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
8750   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
8751   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
8752   (define-key gnus-article-mode-map "\C-c\C-M" 'gnus-article-mail-with-original)
8753   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
8754   
8755   ;; Duplicate almost all summary keystrokes in the article mode map.
8756   (let ((commands 
8757          (list "#" "\M-#" "\C-c\M-#" "\r" "n" "p"
8758                "N" "P" "\M-\C-n" "\M-\C-p" "." "\M-s" "\M-r"
8759                "<" ">" "l" "j" "^" "\M-^" "-" "u" "U" "d" "D"
8760                "\M-u" "\M-U" "k" "\C-k" "\M-\C-k" "c" "x" "X" 
8761                "\M-\C-x" "\M-\177" "b" "B" "$" "w" "\C-c\C-r"
8762                "t" "\M-t" "a" "f" "F" "C" "S" "r" "R" "\C-c\C-f"
8763                "m" "o" "\C-o" "|" "\M-m" "\M-\C-m" "\M-k" "m" "M"
8764                "V" "\C-c\C-d" "q" "Q")))
8765     (while commands
8766       (define-key gnus-article-mode-map (car commands) 
8767         'gnus-article-summary-command)
8768       (setq commands (cdr commands)))))
8769
8770
8771 (defun gnus-article-mode ()
8772   "Major mode for displaying an article.
8773
8774 All normal editing commands are switched off.
8775
8776 The following commands are available:
8777
8778 \\<gnus-article-mode-map>
8779 \\[gnus-article-next-page]\t Scroll the article one page forwards
8780 \\[gnus-article-prev-page]\t Scroll the article one page backwards
8781 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
8782 \\[gnus-article-show-summary]\t Display the summary buffer
8783 \\[gnus-article-mail]\t Send a reply to the address near point
8784 \\[gnus-article-mail-with-original]\t Send a reply to the address near point; include the original article
8785 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
8786 \\[gnus-info-find-node]\t Go to the Gnus info node"
8787   (interactive)
8788   (if gnus-visual (gnus-article-make-menu-bar))
8789   (kill-all-local-variables)
8790   (setq mode-line-modified "-- ")
8791   (make-local-variable 'mode-line-format)
8792   (setq mode-line-format (copy-sequence mode-line-format))
8793   (and (equal (nth 3 mode-line-format) "   ")
8794        (setcar (nthcdr 3 mode-line-format) ""))
8795   (setq mode-name "Article")
8796   (setq major-mode 'gnus-article-mode)
8797   (make-local-variable 'minor-mode-alist)
8798   (or (assq 'gnus-show-mime minor-mode-alist)
8799       (setq minor-mode-alist
8800             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
8801   (use-local-map gnus-article-mode-map)
8802   (make-local-variable 'page-delimiter)
8803   (setq page-delimiter gnus-page-delimiter)
8804   (buffer-disable-undo (current-buffer))
8805   (setq buffer-read-only t)             ;Disable modification
8806   (run-hooks 'gnus-article-mode-hook))
8807
8808 (defun gnus-article-setup-buffer ()
8809   "Initialize article mode buffer."
8810   (or (get-buffer gnus-article-buffer)
8811       (save-excursion
8812         (set-buffer (get-buffer-create gnus-article-buffer))
8813         (gnus-add-current-to-buffer-list)
8814         (gnus-article-mode))))
8815
8816 (defun gnus-request-article-this-buffer (article &optional group)
8817   "Get an article and insert it into this buffer."
8818   (setq group (or group gnus-newsgroup-name))
8819   ;; Using `gnus-request-article' directly will insert the article into
8820   ;; `nntp-server-buffer' - so we'll save some time by not having to
8821   ;; copy it from the server buffer into the article buffer.
8822
8823   ;; We only request an article by message-id when we do not have the
8824   ;; headers for it, so we'll have to get those.
8825   (and (stringp article) 
8826        (let ((gnus-override-method gnus-refer-article-method))
8827          (gnus-read-header article)))
8828
8829   ;; If the article number is negative, that means that this article
8830   ;; doesn't belong in this newsgroup (possibly), so we find its
8831   ;; message-id and request it by id instead of number.
8832   (if (and (numberp article) (< article 0))
8833       (save-excursion
8834         (set-buffer gnus-summary-buffer)
8835         (let ((header (gnus-gethash (int-to-string article)
8836                                     gnus-newsgroup-headers-hashtb-by-number)))
8837           (if (vectorp header)
8838               ;; It's a real article.
8839               (setq article (header-id header))
8840             ;; It is an extracted pseudo-article.
8841             (setq article nil)
8842             (gnus-request-pseudo-article header)))))
8843   ;; Get the article and into the article buffer.
8844   (if article
8845       (progn
8846        (erase-buffer)
8847        (let ((gnus-override-method 
8848               (and (stringp article) gnus-refer-article-method)))
8849          (and (gnus-request-article article group (current-buffer))
8850               'article)))
8851     'pseudo))
8852
8853 (defun gnus-read-header (id)
8854   "Read the headers of article ID and enter them into the Gnus system."
8855   (or gnus-newsgroup-headers-hashtb-by-number
8856       (gnus-make-headers-hashtable-by-number))
8857   (let (header)
8858     (if (not (setq header 
8859                    (car (if (let ((gnus-nov-is-evil t))
8860                               (gnus-retrieve-headers 
8861                                (list id) gnus-newsgroup-name))
8862                             (gnus-get-newsgroup-headers)))))
8863         nil
8864       (if (stringp id)
8865           (header-set-number header gnus-reffed-article-number))
8866       (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers))
8867       (gnus-sethash (int-to-string (header-number header)) header
8868                     gnus-newsgroup-headers-hashtb-by-number)
8869       (if (stringp id)
8870           (setq gnus-reffed-article-number (1- gnus-reffed-article-number)))
8871       (setq gnus-current-headers header)
8872       header)))
8873
8874 (defun gnus-article-prepare (article &optional all-headers header)
8875   "Prepare ARTICLE in article mode buffer.
8876 ARTICLE should either be an article number or a Message-ID.
8877 If ARTICLE is an id, HEADER should be the article headers.
8878 If ALL-HEADERS is non-nil, no headers are hidden."
8879   (save-excursion
8880     ;; Make sure we start in a summary buffer.
8881     (or (eq major-mode 'gnus-summary-mode)
8882         (set-buffer gnus-summary-buffer))
8883     (setq gnus-summary-buffer (current-buffer))
8884     ;; Make sure the connection to the server is alive.
8885     (or (gnus-server-opened (gnus-find-method-for-group gnus-newsgroup-name))
8886         (progn
8887           (gnus-check-news-server 
8888            (gnus-find-method-for-group gnus-newsgroup-name))
8889           (gnus-request-group gnus-newsgroup-name t)))
8890     (or gnus-newsgroup-headers-hashtb-by-number
8891         (gnus-make-headers-hashtable-by-number))
8892     (let* ((article (if header (header-number header) article))
8893            (summary-buffer (current-buffer))
8894            (internal-hook gnus-article-internal-prepare-hook)
8895            (bookmark (cdr (assq article gnus-newsgroup-bookmarks)))
8896            (group gnus-newsgroup-name)
8897            result)
8898       (save-excursion
8899         (gnus-article-setup-buffer)
8900         (set-buffer gnus-article-buffer)
8901         (let ((buffer-read-only nil))
8902           (if (not (setq result (gnus-request-article-this-buffer 
8903                                  article group)))
8904               ;; There is no such article.
8905               (progn
8906                 (save-excursion
8907                   (set-buffer gnus-summary-buffer)
8908                   (setq gnus-current-article 0)
8909                   (and (numberp article) 
8910                        (gnus-summary-mark-as-read article gnus-canceled-mark))
8911                   (message "No such article (may be canceled)")
8912                   (ding))
8913                 (setq gnus-article-current nil)
8914                 nil)
8915             (if (not (eq result 'article))
8916                 (progn
8917                   (save-excursion
8918                     (set-buffer summary-buffer)
8919                     (setq gnus-last-article gnus-current-article
8920                           gnus-newsgroup-history (cons gnus-current-article
8921                                                        gnus-newsgroup-history)
8922                           gnus-current-article 0
8923                           gnus-current-headers nil
8924                           gnus-article-current nil)
8925                     (gnus-configure-windows 'article)
8926                     (gnus-set-global-variables))
8927                   (gnus-set-mode-line 'article))
8928               ;; The result from the `request' was an actual article -
8929               ;; or at least some text that is now displayed in the
8930               ;; article buffer.
8931               (if (and (numberp article)
8932                        (not (eq article gnus-current-article)))
8933                   ;; Seems like a new article has been selected.
8934                   ;; `gnus-current-article' must be an article number.
8935                   (save-excursion
8936                     (set-buffer summary-buffer)
8937                     (setq gnus-last-article gnus-current-article
8938                           gnus-newsgroup-history (cons gnus-current-article
8939                                                        gnus-newsgroup-history)
8940                           gnus-current-article article
8941                           gnus-current-headers 
8942                           (gnus-get-header-by-number gnus-current-article)
8943                           gnus-article-current 
8944                           (cons gnus-newsgroup-name gnus-current-article))
8945                     (run-hooks 'gnus-mark-article-hook)
8946                     (gnus-set-mode-line 'summary)
8947                     (and gnus-visual 
8948                          (run-hooks 'gnus-visual-mark-article-hook))
8949                     ;; Set the global newsgroup variables here.
8950                     ;; Suggested by Jim Sisolak
8951                     ;; <sisolak@trans4.neep.wisc.edu>.
8952                     (gnus-set-global-variables)))
8953               ;; gnus-have-all-headers must be either T or NIL.
8954               (setq gnus-have-all-headers
8955                     (not (not (or all-headers gnus-show-all-headers))))
8956               ;; Hooks for getting information from the article.
8957               ;; This hook must be called before being narrowed.
8958               (run-hooks 'internal-hook)
8959               (run-hooks 'gnus-article-prepare-hook)
8960               ;; Decode MIME message.
8961               (if (and gnus-show-mime
8962                        (gnus-fetch-field "Mime-Version"))
8963                   (funcall gnus-show-mime-method))
8964               ;; Perform the article display hooks.
8965               (let ((buffer-read-only nil))
8966                 (run-hooks 'gnus-article-display-hook))
8967               ;; Do page break.
8968               (goto-char (point-min))
8969               (and gnus-break-pages (gnus-narrow-to-page))
8970               (gnus-set-mode-line 'article)
8971               (gnus-configure-windows 'article)
8972               (goto-char 1)
8973               (set-window-start 
8974                (get-buffer-window gnus-article-buffer) (point-min))
8975               (if bookmark
8976                   (progn
8977                     (message "Moved to bookmark")
8978                     (search-forward "\n\n" nil t)
8979                     (forward-line bookmark)))
8980               t)))))))
8981
8982 (defun gnus-article-show-all-headers ()
8983   "Show all article headers in article mode buffer."
8984   (save-excursion 
8985     (setq gnus-have-all-headers t)
8986     (gnus-article-setup-buffer)
8987     (set-buffer gnus-article-buffer)
8988     (let ((buffer-read-only nil))
8989       (remove-text-properties 1 (point-max) '(invisible t)))))
8990
8991 (defun gnus-article-hide-headers-if-wanted ()
8992   "Hide unwanted headers if `gnus-have-all-headers' is nil.
8993 Provided for backwards compatability."
8994   (or gnus-have-all-headers
8995       (gnus-article-hide-headers)))
8996
8997 (defun gnus-article-hide-headers (&optional delete)
8998   "Hide unwanted headers and possibly sort them as well."
8999   (interactive "P")
9000   (save-excursion
9001     (set-buffer gnus-article-buffer)
9002     (save-restriction
9003       (let ((sorted gnus-sorted-header-list)
9004             (buffer-read-only nil)
9005             want want-list beg want-l)
9006         ;; First we narrow to just the headers.
9007         (widen)
9008         (goto-char 1)
9009         ;; Hide any "From " lines at the beginning of (mail) articles. 
9010         (while (looking-at rmail-unix-mail-delimiter)
9011           (forward-line 1))
9012         (if (/= (point) 1) 
9013             (add-text-properties 1 (point) '(invisible t)))
9014         ;; Then treat the rest of the header lines.
9015         (narrow-to-region 
9016          (point) 
9017          (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
9018         ;; Then we use the two regular expressions
9019         ;; `gnus-ignored-headers' and `gnus-visible-headers' to
9020         ;; select which header lines is to remain visible in the
9021         ;; article buffer.
9022         (goto-char 1)
9023         (while (re-search-forward "^[^ \t]*:" nil t)
9024           (beginning-of-line)
9025           ;; We add the headers we want to keep to a list and delete
9026           ;; them from the buffer.
9027           (if (or (and (stringp gnus-visible-headers)
9028                        (looking-at gnus-visible-headers))
9029                   (and (not (stringp gnus-visible-headers))
9030                        (stringp gnus-ignored-headers)
9031                        (not (looking-at gnus-ignored-headers))))
9032               (progn
9033                 (setq beg (point))
9034                 (forward-line 1)
9035                 ;; Be sure to get multi-line headers...
9036                 (re-search-forward "^[^ \t]*:" nil t)
9037                 (beginning-of-line)
9038                 (setq want-list 
9039                       (cons (buffer-substring beg (point)) want-list))
9040                 (delete-region beg (point))
9041                 (goto-char beg))
9042             (forward-line 1)))
9043         ;; Next we perform the sorting by looking at
9044         ;; `gnus-sorted-header-list'. 
9045         (goto-char 1)
9046         (while (and sorted want-list)
9047           (setq want-l want-list)
9048           (while (and want-l
9049                       (not (string-match (car sorted) (car want-l))))
9050             (setq want-l (cdr want-l)))
9051           (if want-l 
9052               (progn
9053                 (insert (car want-l))
9054                 (setq want-list (delq (car want-l) want-list))))
9055           (setq sorted (cdr sorted)))
9056         ;; Any headers that were not matched by the sorted list we
9057         ;; just tack on the end of the visible header list.
9058         (while want-list
9059           (insert (car want-list))
9060           (setq want-list (cdr want-list)))
9061         ;; And finally we make the unwanted headers invisible.
9062         (if delete
9063             (delete-region (point) (point-max))
9064           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
9065           (add-text-properties (point) (point-max) '(invisible t)))))))
9066
9067 (defun gnus-article-hide-signature ()
9068   "Hides the signature in an article.
9069 It does this by hiding everyting after \"^-- *$\", which is what all
9070 signatures should be preceded by. Note that this may mean that parts
9071 of an article may disappear if the article has such a line in the
9072 middle of the text."
9073   (interactive)
9074   (save-excursion
9075     (set-buffer gnus-article-buffer)
9076     (let ((buffer-read-only nil))
9077       (goto-char (point-max))
9078       (if (re-search-backward "^-- *$" nil t)
9079           (progn
9080             (add-text-properties (point) (point-max) '(invisible t)))))))
9081
9082 (defun gnus-article-hide-citation ()
9083   "Hide all cited text.
9084 This function uses the famous, extremely intelligent \"shoot in foot\"
9085 algorithm - which is simply deleting all lines that start with
9086 \">\". Your mileage may vary. If you come up with anything better,
9087 please do mail it to me."
9088   (interactive)
9089   (save-excursion
9090     (set-buffer gnus-article-buffer)
9091     (let ((buffer-read-only nil))
9092       (goto-char 1)
9093       (search-forward "\n\n" nil t)
9094       (while (not (eobp))
9095         (if (looking-at ">")
9096             (add-text-properties 
9097              (point) (save-excursion (forward-line 1) (point))
9098              '(invisible t)))
9099         (forward-line 1)))))
9100
9101 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
9102 (defun gnus-article-treat-overstrike ()
9103   ;; Prepare article for overstrike commands.
9104   (interactive)
9105   (save-excursion
9106     (set-buffer gnus-article-buffer)
9107     (let ((buffer-read-only nil))
9108       (while (search-forward "\b" nil t)
9109         (let ((next (following-char))
9110               (previous (char-after (- (point) 2))))
9111           (cond ((eq next previous)
9112                  (delete-region (- (point) 2) (point))
9113                  (put-text-property (point) (1+ (point))
9114                                     'face 'bold))
9115                 ((eq next ?_)
9116                  (delete-region (1- (point)) (1+ (point)))
9117                  (put-text-property (1- (point)) (point)
9118                                     'face 'underline))
9119                 ((eq previous ?_)
9120                  (delete-region (- (point) 2) (point))
9121                  (put-text-property (point) (1+ (point))
9122                                     'face 'underline))))))))
9123
9124 (defun gnus-article-word-wrap ()
9125   "Format too long lines."
9126   (interactive)
9127   (save-excursion
9128     (set-buffer gnus-article-buffer)
9129     (let ((buffer-read-only nil))
9130       (goto-char 1)
9131       (search-forward "\n\n" nil t)
9132       (end-of-line 1)
9133       (let ((paragraph-start "^\\W"))
9134         (while (not (eobp))
9135           (and (>= (current-column) (window-width))
9136                (/= (preceding-char) ?:)
9137                (fill-paragraph nil))
9138           (end-of-line 2))))))
9139
9140 (defun gnus-article-remove-cr ()
9141   (interactive)
9142   (save-excursion
9143     (set-buffer gnus-article-buffer)
9144     (let ((buffer-read-only nil))
9145       (goto-char (point-min))
9146       (while (search-forward "\r" nil t)
9147         (replace-match "")))))
9148
9149 (defun gnus-article-de-quoted-unreadable ()
9150   (interactive)
9151   (save-excursion
9152     (save-restriction
9153       (set-buffer gnus-article-buffer)
9154       (let ((buffer-read-only nil))
9155         (widen)
9156         (goto-char (point-min))
9157         (while (re-search-forward "=[0-9A-F][0-9A-F]" nil t)
9158           (replace-match 
9159            (char-to-string 
9160             (+
9161              (* 16 (gnus-hex-char-to-integer 
9162                     (char-after (1+ (match-beginning 0)))))
9163              (gnus-hex-char-to-integer
9164               (char-after (1- (match-end 0))))))))))))
9165
9166 ;; Taken from hexl.el.
9167 (defun gnus-hex-char-to-integer (character)
9168   "Take a char and return its value as if it was a hex digit."
9169   (if (and (>= character ?0) (<= character ?9))
9170       (- character ?0)
9171     (let ((ch (logior character 32)))
9172       (if (and (>= ch ?a) (<= ch ?f))
9173           (- ch (- ?a 10))
9174         (error (format "Invalid hex digit `%c'." ch))))))
9175
9176 ;; Article savers.
9177
9178 (defun gnus-output-to-rmail (file-name)
9179   "Append the current article to an Rmail file named FILE-NAME."
9180   (require 'rmail)
9181   ;; Most of these codes are borrowed from rmailout.el.
9182   (setq file-name (expand-file-name file-name))
9183   (setq rmail-default-rmail-file file-name)
9184   (let ((artbuf (current-buffer))
9185         (tmpbuf (get-buffer-create " *Gnus-output*")))
9186     (save-excursion
9187       (or (get-file-buffer file-name)
9188           (file-exists-p file-name)
9189           (if (gnus-yes-or-no-p
9190                (concat "\"" file-name "\" does not exist, create it? "))
9191               (let ((file-buffer (create-file-buffer file-name)))
9192                 (save-excursion
9193                   (set-buffer file-buffer)
9194                   (rmail-insert-rmail-file-header)
9195                   (let ((require-final-newline nil))
9196                     (write-region (point-min) (point-max) file-name t 1)))
9197                 (kill-buffer file-buffer))
9198             (error "Output file does not exist")))
9199       (set-buffer tmpbuf)
9200       (buffer-disable-undo (current-buffer))
9201       (erase-buffer)
9202       (insert-buffer-substring artbuf)
9203       (gnus-convert-article-to-rmail)
9204       ;; Decide whether to append to a file or to an Emacs buffer.
9205       (let ((outbuf (get-file-buffer file-name)))
9206         (if (not outbuf)
9207             (append-to-file (point-min) (point-max) file-name)
9208           ;; File has been visited, in buffer OUTBUF.
9209           (set-buffer outbuf)
9210           (let ((buffer-read-only nil)
9211                 (msg (and (boundp 'rmail-current-message)
9212                           rmail-current-message)))
9213             ;; If MSG is non-nil, buffer is in RMAIL mode.
9214             (if msg
9215                 (progn (widen)
9216                        (narrow-to-region (point-max) (point-max))))
9217             (insert-buffer-substring tmpbuf)
9218             (if msg
9219                 (progn
9220                   (goto-char (point-min))
9221                   (widen)
9222                   (search-backward "\^_")
9223                   (narrow-to-region (point) (point-max))
9224                   (goto-char (1+ (point-min)))
9225                   (rmail-count-new-messages t)
9226                   (rmail-show-message msg)))))))
9227     (kill-buffer tmpbuf)))
9228
9229 (defun gnus-output-to-file (file-name)
9230   "Append the current article to a file named FILE-NAME."
9231   (setq file-name (expand-file-name file-name))
9232   (let ((artbuf (current-buffer))
9233         (tmpbuf (get-buffer-create " *Gnus-output*")))
9234     (save-excursion
9235       (set-buffer tmpbuf)
9236       (buffer-disable-undo (current-buffer))
9237       (erase-buffer)
9238       (insert-buffer-substring artbuf)
9239       ;; Append newline at end of the buffer as separator, and then
9240       ;; save it to file.
9241       (goto-char (point-max))
9242       (insert "\n")
9243       (append-to-file (point-min) (point-max) file-name))
9244     (kill-buffer tmpbuf)))
9245
9246 (defun gnus-convert-article-to-rmail ()
9247   "Convert article in current buffer to Rmail message format."
9248   (let ((buffer-read-only nil))
9249     ;; Convert article directly into Babyl format.
9250     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
9251     (goto-char (point-min))
9252     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
9253     (while (search-forward "\n\^_" nil t) ;single char
9254       (replace-match "\n^_"))           ;2 chars: "^" and "_"
9255     (goto-char (point-max))
9256     (insert "\^_")))
9257
9258 (defun gnus-narrow-to-page (&optional arg)
9259   "Make text outside current page invisible except for page delimiter.
9260 A numeric arg specifies to move forward or backward by that many pages,
9261 thus showing a page other than the one point was originally in."
9262   (interactive "P")
9263   (setq arg (if arg (prefix-numeric-value arg) 0))
9264   (save-excursion
9265     (forward-page -1)                   ;Beginning of current page.
9266     (widen)
9267     (if (> arg 0)
9268         (forward-page arg)
9269       (if (< arg 0)
9270           (forward-page (1- arg))))
9271     ;; Find the end of the page.
9272     (forward-page)
9273     ;; If we stopped due to end of buffer, stay there.
9274     ;; If we stopped after a page delimiter, put end of restriction
9275     ;; at the beginning of that line.
9276     ;; These are commented out.
9277     ;;    (if (save-excursion (beginning-of-line)
9278     ;;                  (looking-at page-delimiter))
9279     ;;  (beginning-of-line))
9280     (narrow-to-region (point)
9281                       (progn
9282                         ;; Find the top of the page.
9283                         (forward-page -1)
9284                         ;; If we found beginning of buffer, stay there.
9285                         ;; If extra text follows page delimiter on same line,
9286                         ;; include it.
9287                         ;; Otherwise, show text starting with following line.
9288                         (if (and (eolp) (not (bobp)))
9289                             (forward-line 1))
9290                         (point)))))
9291
9292 (defun gnus-gmt-to-local ()
9293   "Rewrite Date header described in GMT to local in current buffer.
9294 Intended to be used with gnus-article-prepare-hook."
9295   (save-excursion
9296     (save-restriction
9297       (widen)
9298       (goto-char (point-min))
9299       (narrow-to-region (point-min)
9300                         (progn (search-forward "\n\n" nil 'move) (point)))
9301       (goto-char (point-min))
9302       (if (re-search-forward "^Date:[ \t]\\(.*\\)$" nil t)
9303           (let ((buffer-read-only nil)
9304                 (date (buffer-substring (match-beginning 1) (match-end 1))))
9305             (delete-region (match-beginning 1) (match-end 1))
9306             (insert
9307              (timezone-make-date-arpa-standard 
9308               date nil (current-time-zone))))))))
9309
9310
9311 ;; Article mode commands
9312
9313 (defun gnus-article-next-page (lines)
9314   "Show next page of current article.
9315 If end of article, return non-nil. Otherwise return nil.
9316 Argument LINES specifies lines to be scrolled up."
9317   (interactive "P")
9318   (move-to-window-line -1)
9319   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
9320   (if (save-excursion
9321         (end-of-line)
9322         (and (pos-visible-in-window-p)  ;Not continuation line.
9323              (eobp)))
9324       ;; Nothing in this page.
9325       (if (or (not gnus-break-pages)
9326               (save-excursion
9327                 (save-restriction
9328                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
9329           t                             ;Nothing more.
9330         (gnus-narrow-to-page 1)         ;Go to next page.
9331         nil)
9332     ;; More in this page.
9333     (condition-case ()
9334         (scroll-up lines)
9335       (end-of-buffer
9336        ;; Long lines may cause an end-of-buffer error.
9337        (goto-char (point-max))))
9338     nil))
9339
9340 (defun gnus-article-prev-page (lines)
9341   "Show previous page of current article.
9342 Argument LINES specifies lines to be scrolled down."
9343   (interactive "P")
9344   (move-to-window-line 0)
9345   (if (and gnus-break-pages
9346            (bobp)
9347            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
9348       (progn
9349         (gnus-narrow-to-page -1) ;Go to previous page.
9350         (goto-char (point-max))
9351         (recenter -1))
9352     (scroll-down lines)))
9353
9354 (defun gnus-article-refer-article ()
9355   "Read article specified by message-id around point."
9356   (interactive)
9357   (search-forward ">" nil t)    ;Move point to end of "<....>".
9358   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
9359       (let ((message-id
9360              (buffer-substring (match-beginning 1) (match-end 1))))
9361         (set-buffer gnus-summary-buffer)
9362         (gnus-summary-refer-article message-id))
9363     (error "No references around point")))
9364
9365 (defun gnus-article-mail (yank)
9366   "Send a reply to the address near point.
9367 If YANK is non-nil, include the original article."
9368   (interactive "P")
9369   (let ((address 
9370          (buffer-substring
9371           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
9372           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
9373     (and address
9374          (progn
9375            (switch-to-buffer gnus-summary-buffer)
9376            (funcall gnus-mail-reply-method yank address)))))
9377
9378 (defun gnus-article-mail-with-original ()
9379   "Send a reply to the address near point and include the original article."
9380   (interactive)
9381   (gnus-article-mail 'yank))
9382
9383 (defun gnus-article-show-summary ()
9384   "Reconfigure windows to show summary buffer."
9385   (interactive)
9386   (gnus-configure-windows 'article)
9387   (pop-to-buffer gnus-summary-buffer)
9388   (gnus-summary-goto-subject gnus-current-article))
9389
9390 (defun gnus-article-describe-briefly ()
9391   "Describe article mode commands briefly."
9392   (interactive)
9393   (message
9394    (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")))
9395
9396 (defun gnus-article-summary-command ()
9397   "Execute the last keystroke in the summary buffer."
9398   (interactive)
9399   (message "                                                                              ")
9400   (let ((obuf (current-buffer))
9401         (owin (current-window-configuration)))
9402     (switch-to-buffer gnus-summary-buffer 'norecord)
9403     (execute-kbd-macro (this-command-keys))
9404     (set-buffer obuf)
9405     (let ((npoint (point)))
9406       (set-window-configuration owin)
9407       (set-window-start (get-buffer-window (current-buffer)) (point)))))
9408
9409 ;; caesar-region written by phr@prep.ai.mit.edu  Nov 86
9410 ;; Modified by tower@prep Nov 86
9411 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
9412
9413 (defun gnus-caesar-region (&optional n)
9414   "Caesar rotation of region by N, default 13, for decrypting netnews.
9415 ROT47 will be performed for Japanese text in any case."
9416   (interactive (if current-prefix-arg   ; Was there a prefix arg?
9417                    (list (prefix-numeric-value current-prefix-arg))
9418                  (list nil)))
9419   (cond ((not (numberp n)) (setq n 13))
9420         (t (setq n (mod n 26))))        ;canonicalize N
9421   (if (not (zerop n))           ; no action needed for a rot of 0
9422       (progn
9423         (if (or (not (boundp 'caesar-translate-table))
9424                 (not caesar-translate-table)
9425                 (/= (aref caesar-translate-table ?a) (+ ?a n)))
9426             (let ((i 0) (lower "abcdefghijklmnopqrstuvwxyz") upper)
9427               (message "Building caesar-translate-table...")
9428               (setq caesar-translate-table (make-vector 256 0))
9429               (while (< i 256)
9430                 (aset caesar-translate-table i i)
9431                 (setq i (1+ i)))
9432               (setq lower (concat lower lower) upper (upcase lower) i 0)
9433               (while (< i 26)
9434                 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
9435                 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
9436                 (setq i (1+ i)))
9437               ;; ROT47 for Japanese text.
9438               ;; Thanks to ichikawa@flab.fujitsu.junet.
9439               (setq i 161)
9440               (let ((t1 (logior ?O 128))
9441                     (t2 (logior ?! 128))
9442                     (t3 (logior ?~ 128)))
9443                 (while (< i 256)
9444                   (aset caesar-translate-table i
9445                         (let ((v (aref caesar-translate-table i)))
9446                           (if (<= v t1) (if (< v t2) v (+ v 47))
9447                             (if (<= v t3) (- v 47) v))))
9448                   (setq i (1+ i))))
9449               (message "Building caesar-translate-table... done")))
9450         (let ((from (region-beginning))
9451               (to (region-end))
9452               (i 0) str len)
9453           (setq str (buffer-substring from to))
9454           (setq len (length str))
9455           (while (< i len)
9456             (aset str i (aref caesar-translate-table (aref str i)))
9457             (setq i (1+ i)))
9458           (goto-char from)
9459           (delete-region from to)
9460           (insert str)))))
9461
9462 \f
9463 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
9464
9465 ;;;###autoload
9466 (defalias 'gnus-batch-kill 'gnus-batch-score)
9467 ;;;###autoload
9468 (defun gnus-batch-score ()
9469   "Run batched scoring.
9470 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
9471 Newsgroups is a list of strings in Bnews format.  If you want to score
9472 the comp hierarchy, you'd say \"comp.all\". If you would not like to
9473 score the alt hierarchy, you'd say \"!alt.all\"."
9474   (interactive)
9475   (let* ((yes-and-no
9476           (gnus-parse-n-options
9477            (apply (function concat)
9478                   (mapcar (lambda (g) (concat g " "))
9479                           command-line-args-left))))
9480          (gnus-expert-user t)
9481          (nnmail-spool-file nil)
9482          (gnus-use-dribble-file nil)
9483          (yes (car yes-and-no))
9484          (no (cdr yes-and-no))
9485          group subscribed newsrc entry
9486          ;; Disable verbose message.
9487          gnus-novice-user gnus-large-newsgroup)
9488     ;; Eat all arguments.
9489     (setq command-line-args-left nil)
9490     ;; Start Gnus.
9491     (gnus)
9492     ;; Apply kills to specified newsgroups in command line arguments.
9493     (setq newsrc (cdr gnus-newsrc-assoc))
9494     (while newsrc
9495       (setq group (car (car newsrc)))
9496       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
9497       (if (and (<= (nth 1 (car newsrc)) 5)
9498                (and (car entry)
9499                     (or (eq (car entry) t)
9500                         (not (zerop (car entry)))))
9501                (if yes (string-match yes group) t)
9502                (or (null no) (not (string-match no group))))
9503           (progn
9504             (gnus-summary-read-group group nil t)
9505             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
9506                  (gnus-summary-exit))))
9507       (setq newsrc (cdr newsrc)))
9508     ;; Exit Emacs.
9509     (switch-to-buffer gnus-group-buffer)
9510     (gnus-group-save-newsrc)))
9511
9512 (defun gnus-apply-kill-file ()
9513   "Apply a kill file to the current newsgroup.
9514 Returns the number of articles marked as read."
9515   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
9516           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
9517       (gnus-apply-kill-file-internal)
9518     0))
9519
9520 (defun gnus-kill-save-kill-buffer ()
9521   (save-excursion
9522     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
9523       (if (get-file-buffer file)
9524           (progn
9525             (set-buffer (get-file-buffer file))
9526             (and (buffer-modified-p) (save-buffer))
9527             (kill-buffer (current-buffer)))))))
9528
9529 (defvar gnus-kill-file-name "KILL"
9530   "Suffix of the kill files.")
9531
9532 (defun gnus-newsgroup-kill-file (newsgroup)
9533   "Return the name of a kill file name for NEWSGROUP.
9534 If NEWSGROUP is nil, return the global kill file name instead."
9535   (cond ((or (null newsgroup)
9536              (string-equal newsgroup ""))
9537          ;; The global KILL file is placed at top of the directory.
9538          (expand-file-name gnus-kill-file-name
9539                            (or gnus-kill-files-directory "~/News")))
9540         (gnus-use-long-file-name
9541          ;; Append ".KILL" to newsgroup name.
9542          (expand-file-name (concat newsgroup "." gnus-kill-file-name)
9543                            (or gnus-kill-files-directory "~/News")))
9544         (t
9545          ;; Place "KILL" under the hierarchical directory.
9546          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
9547                                    "/" gnus-kill-file-name)
9548                            (or gnus-kill-files-directory "~/News")))))
9549
9550 \f
9551 ;;;
9552 ;;; Gnus Score Files
9553 ;;;
9554
9555 ;; All score code written by Per Abrahamsen <abraham@iesd.auc.dk>.
9556
9557 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9558 (defun gnus-score-set-mark-below (score)
9559   "Automatically mark articles with score below SCORE as read."
9560   (interactive 
9561    (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
9562              (string-to-int (read-string "Mark below: ")))))
9563   (setq score (or score gnus-summary-default-score 0))
9564   (gnus-score-set 'mark (list score))
9565   (gnus-score-set 'touched '(t))
9566   (setq gnus-summary-mark-below score)
9567   (gnus-summary-update-lines))
9568
9569 (defun gnus-score-set-expunge-below (score)
9570   "Automatically expunge articles with score below SCORE."
9571   (interactive 
9572    (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
9573              (string-to-int (read-string "Expunge below: ")))))
9574   (setq score (or score gnus-summary-default-score 0))
9575   (gnus-score-set 'expunge (list score))
9576   (gnus-score-set 'touched '(t)))
9577
9578 (defun gnus-score-default (level)
9579   (if level (prefix-numeric-value level) 
9580     gnus-score-interactive-default-score))
9581
9582 (defun gnus-score-set (symbol value &optional alist)
9583   ;; Set SYMBOL to VALUE in ALIST.
9584   (let* ((alist 
9585           (or alist 
9586               gnus-score-alist
9587               (progn
9588                 (gnus-score-load (gnus-score-file-name gnus-newsgroup-name))
9589                 gnus-score-alist)))
9590          (entry (assoc symbol alist)))
9591     (cond ((gnus-score-get 'read-only alist)
9592            ;; This is a read-only score file, so we do nothing.
9593            )
9594           (entry
9595            (setcdr entry value))
9596           ((null alist)
9597            (error "Empty alist"))
9598           (t
9599            (setcdr alist
9600                    (cons (cons symbol value) (cdr alist)))))))
9601
9602 (defun gnus-score-get (symbol &optional alist)
9603   ;; Get SYMBOL's definition in ALIST.
9604   (cdr (assoc symbol 
9605               (or alist 
9606                   gnus-score-alist
9607                   (progn
9608                     (gnus-score-load 
9609                      (gnus-score-file-name gnus-newsgroup-name))
9610                     gnus-score-alist)))))
9611
9612 (defun gnus-score-change-score-file (file)
9613   "Change current score alist."
9614   (interactive
9615    (list (completing-read "Score file: " gnus-score-cache)))
9616   (setq gnus-current-score-file file)
9617   (gnus-score-load-file file))
9618
9619 (defun gnus-score-edit-alist (file)
9620   "Edit the current score alist."
9621   (interactive (list gnus-current-score-file))
9622   (and (buffer-name gnus-summary-buffer) (gnus-score-save))
9623   (setq gnus-winconf-edit-score (current-window-configuration))
9624   (gnus-configure-windows 'article)
9625   (pop-to-buffer (find-file-noselect file))
9626   (message (substitute-command-keys 
9627             "\\<gnus-score-mode-map>\\[gnus-score-edit-done] to save edits"))
9628   (gnus-score-mode))
9629   
9630 (defun gnus-score-edit-file (file)
9631   "Edit a score file."
9632   (interactive 
9633    (list (read-file-name "Edit score file: " gnus-kill-files-directory)))
9634   (and (buffer-name gnus-summary-buffer) (gnus-score-save))
9635   (setq gnus-winconf-edit-score (current-window-configuration))
9636   (gnus-configure-windows 'article)
9637   (pop-to-buffer (find-file-noselect file))
9638   (message (substitute-command-keys 
9639             "\\<gnus-score-mode-map>\\[gnus-score-edit-done] to save edits"))
9640   (gnus-score-mode))
9641   
9642 (defun gnus-score-load-file (file)
9643   ;; Load score file FILE.  Returns a list a retrieved score-alists.
9644   (let* ((file (expand-file-name (or (and (string-match "^/" file) file)
9645                                      (concat gnus-kill-files-directory file))))
9646          (cached (assoc file gnus-score-cache))
9647          (global (member file gnus-internal-global-score-files))
9648          lists alist)
9649     (if cached
9650         ;; The score file was already loaded.
9651         (setq gnus-score-alist (cdr cached))
9652       ;; We load the score file.
9653       (setq gnus-score-alist nil)
9654       (setq alist (gnus-score-load-score-alist file))
9655       ;; We add '(touched) to the alist to signify that it hasn't been
9656       ;; touched (yet). 
9657       (if (not (assq 'touched gnus-score-alist))
9658           (setq gnus-score-alist 
9659                 (cons (list 'touched nil) gnus-score-alist)))
9660       ;; If it is a global score file, we make it read-only.
9661       (and global
9662            (not (assq 'read-only gnus-score-alist))
9663            (setq gnus-score-alist 
9664                  (cons (list 'read-only t) gnus-score-alist)))
9665       ;; Update cache.
9666       (setq gnus-score-cache
9667             (cons (cons file gnus-score-alist) gnus-score-cache)))
9668     ;; If there are actual scores in the alist, we add it to the
9669     ;; return value of this function.
9670     (if (memq t (mapcar (lambda (e) (stringp (car e))) gnus-score-alist))
9671         (setq lists (list gnus-score-alist)))
9672     ;; Treat the other possible atoms in the score alist.
9673     (let ((mark (car (gnus-score-get 'mark gnus-score-alist)))
9674           (expunge (car (gnus-score-get 'expunge gnus-score-alist)))
9675           (mark-and-expunge 
9676            (car (gnus-score-get 'mark-and-expunge gnus-score-alist)))
9677           (read-only (gnus-score-get 'read-only gnus-score-alist))
9678           (files (gnus-score-get 'files gnus-score-alist))
9679           (eval (gnus-score-get 'eval gnus-score-alist)))
9680       ;; We do not respect eval and files atoms from global score
9681       ;; files. 
9682       (and files (not global)
9683            (setq lists (apply 'append lists
9684                               (mapcar (lambda (file)
9685                                         (gnus-score-load-file file)) 
9686                                       files))))
9687       (and eval (not global) (eval eval))
9688       (setq gnus-summary-mark-below (or mark mark-and-expunge))
9689       (setq gnus-summary-expunge-below (or expunge mark-and-expunge)))
9690     (setq gnus-current-score-file file)
9691     (setq gnus-score-alist alist)
9692     lists))
9693
9694 (defun gnus-score-load (file)
9695   ;; Load score FILE.
9696   (let ((cache (assoc file gnus-score-cache)))
9697     (if cache
9698         (setq gnus-score-alist (cdr cache))
9699       (setq gnus-score-alist nil)
9700       (gnus-score-load-score-alist file)
9701       (or gnus-score-alist
9702           (setq gnus-score-alist (copy-alist '((touched . nil)))))
9703       (setq gnus-score-cache
9704             (cons (cons file gnus-score-alist) gnus-score-cache)))))
9705
9706 (defun gnus-score-remove-from-cache (file)
9707   (setq gnus-score-cache (delq (assoc file gnus-score-cache)
9708                                gnus-score-cache)))
9709
9710 (defun gnus-score-load-score-alist (file)
9711   (let (alist)
9712     (if (file-readable-p file)
9713         (progn
9714           (save-excursion
9715             (set-buffer (get-buffer-create " *gnus work*"))
9716             (buffer-disable-undo (current-buffer))
9717             (erase-buffer)
9718             (insert-file-contents file)
9719             (goto-char (point-min))
9720             (setq alist
9721                   (condition-case ()
9722                       (read (current-buffer))
9723                     (error 
9724                      (progn
9725                        (message "Problem with score file %s" file)
9726                        (ding) 
9727                        nil)))))
9728           (if (eq (car alist) 'setq)
9729               (setq gnus-score-alist
9730                     (gnus-score-transform-old-to-new alist))
9731             (setq gnus-score-alist alist))))))
9732
9733 (defun gnus-score-transform-old-to-new (alist)
9734   (let* ((alist (nth 2 alist))
9735          out entry)
9736     (if (eq (car alist) 'quote)
9737         (setq alist (nth 1 alist)))
9738     (while alist
9739       (setq entry (car alist))
9740       (if (stringp (car entry))
9741           (let ((scor (cdr entry)))
9742             (setq out (cons entry out))
9743             (while scor
9744               (setcar scor
9745                       (list (car (car scor)) (nth 2 (car scor))
9746                             (and (nth 3 (car scor))
9747                                  (gnus-day-number (nth 3 (car scor))))
9748                             (if (nth 1 (car scor)) 'r 's)))
9749               (setq scor (cdr scor))))
9750         (setq out (cons (list (car entry) (cdr entry)) out)))
9751       (setq alist (cdr alist)))
9752     (cons (list 'touched t) (nreverse out))))
9753   
9754 (defun gnus-score-save ()
9755   ;; Save all SCORE information.
9756   (let (cache)
9757     (save-excursion
9758       (set-buffer gnus-summary-buffer)
9759       (setq cache gnus-score-cache
9760             gnus-score-cache nil))
9761     (save-excursion
9762       (setq gnus-score-alist nil)
9763       (set-buffer (get-buffer-create "*Score*"))
9764       (buffer-disable-undo (current-buffer))
9765       (let (entry score file)
9766         (while cache
9767           (setq entry (car cache)
9768                 cache (cdr cache)
9769                 file (car entry)
9770                 score (cdr entry))
9771           (if (or (not (equal (gnus-score-get 'touched score) '(t)))
9772                   (gnus-score-get 'read-only score)
9773                   (not (file-writable-p file)))
9774               ()
9775             (setq score (delq (assq 'touched score) score))
9776             (erase-buffer)
9777             (let (emacs-lisp-mode-hook)
9778               (pp score (current-buffer)))
9779             (make-directory (file-name-directory file) t)
9780             (write-region (point-min) (point-max) file nil 'silent))))
9781       (kill-buffer (current-buffer)))))
9782   
9783 (defun gnus-score-headers ()
9784   ;; Score `gnus-newsgroup-headers'.
9785   (let ((score-files (and (symbolp gnus-score-find-score-files-function)
9786                           (fboundp gnus-score-find-score-files-function)
9787                           (funcall gnus-score-find-score-files-function
9788                                    gnus-newsgroup-name)))
9789         scores)
9790     ;; Load the SCORE files.
9791     (while score-files
9792       (setq scores (nconc (gnus-score-load-file (car score-files)) scores))
9793       (setq score-files (cdr score-files)))
9794     (if (not (and gnus-summary-default-score
9795                   scores
9796                   (> (length gnus-newsgroup-headers)
9797                      (length gnus-newsgroup-scored))))
9798         ()
9799       (let* ((entries gnus-header-index)
9800              (now (gnus-day-number (current-time-string)))
9801              (expire (- now gnus-score-expiry-days))
9802              (headers gnus-newsgroup-headers)
9803              entry header)
9804         (message "Scoring...")
9805         ;; Create articles, an alist of the form `(HEADER . SCORE)'.
9806         (while headers
9807           (setq header (car headers)
9808                 headers (cdr headers))
9809           ;; WARNING: The assq makes the function O(N*S) while it could
9810           ;; be written as O(N+S), where N is (length gnus-newsgroup-headers)
9811           ;; and S is (length gnus-newsgroup-scored).
9812           (or (assq (header-number header) gnus-newsgroup-scored)
9813               (setq gnus-scores-articles       ;Total of 2 * N cons-cells used.
9814                     (cons (cons header (or gnus-summary-default-score 0))
9815                           gnus-scores-articles))))
9816   
9817         (save-excursion
9818           (set-buffer (get-buffer-create "*Headers*"))
9819           (buffer-disable-undo (current-buffer))
9820           ;; Run each header through the score process.
9821           (while entries
9822             (setq entry (car entries)
9823                   header (nth 0 entry)
9824                   entries (cdr entries))
9825             (setq gnus-score-index (nth 1 (assoc header gnus-header-index)))
9826             (if (< 0 (apply 'max (mapcar
9827                                   (lambda (score)
9828                                     (length (gnus-score-get header score)))
9829                                   scores)))
9830                 (funcall (nth 2 entry) scores header now expire)))
9831           ;; Remove the buffer.
9832           (kill-buffer (current-buffer)))
9833
9834         ;; Add articles to `gnus-newsgroup-scored'.
9835         (while gnus-scores-articles
9836           (or (= gnus-summary-default-score (cdr (car gnus-scores-articles)))
9837               (setq gnus-newsgroup-scored
9838                     (cons (cons (header-number 
9839                                  (car (car gnus-scores-articles)))
9840                                 (cdr (car gnus-scores-articles)))
9841                           gnus-newsgroup-scored)))
9842           (setq gnus-scores-articles (cdr gnus-scores-articles)))
9843
9844         (message "Scoring...done")))))
9845
9846 ;;(defun gnus-score-integer (scores header now expire)
9847 ;;  )
9848
9849 ;;(defun gnus-score-date (scores header now expire)
9850 ;;  )
9851
9852 (defun gnus-score-string (scores header now expire)
9853   ;; Score ARTICLES according to HEADER in SCORES.
9854   ;; Update matches entries to NOW and remove unmatched entried older
9855   ;; than EXPIRE.
9856   
9857   ;; Insert the unique article headers in the buffer.
9858   (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
9859         ;; gnus-score-index is used as a free variable.
9860         alike last this art entries alist articles)
9861
9862     ;; Sorting the articles costs os O(N*log N) but will allow us to
9863     ;; only match with each unique header.  Thus the actual matching
9864     ;; will be O(M*U) where M is the number of strings to match with,
9865     ;; and U is the number of unique headers.  It is assumed (but
9866     ;; untested) this will be a net win because of the large constant
9867     ;; factor involved with string matching.
9868     (setq gnus-scores-articles (sort gnus-scores-articles 'gnus-score-string<)
9869           articles gnus-scores-articles)
9870
9871     (erase-buffer)
9872     (while articles
9873       (setq art (car articles)
9874             this (aref (car art) gnus-score-index)
9875             articles (cdr articles))
9876       (if (equal last this)
9877           ;; O(N*H) cons-cells used here, where H is the number of
9878           ;; headers.
9879           (setq alike (cons art alike))
9880         (if last
9881             (progn
9882               ;; Insert the line, with a text property on the
9883               ;; terminating newline refering to the articles with
9884               ;; this line.
9885               (insert last ?\n)
9886               (put-text-property (1- (point)) (point) 'articles alike)))
9887         (setq alike (list art)
9888               last this)))
9889     (and last                           ; Bwadr, duplicate code.
9890          (progn
9891            (insert last ?\n)                    
9892            (put-text-property (1- (point)) (point) 'articles alike)))
9893   
9894     ;; Find matches.
9895     (while scores
9896       (setq alist (car scores)
9897             scores (cdr scores)
9898             entries (assoc header alist))
9899       (while (cdr entries)              ;First entry is the header index.
9900         (let* ((rest (cdr entries))             
9901                (kill (car rest))
9902                (match (nth 0 kill))
9903                (type (or (nth 3 kill) 's))
9904                (score (or (nth 1 kill) gnus-score-interactive-default-score))
9905                (date (nth 2 kill))
9906                (found nil)
9907                (case-fold-search t)
9908                arts art)
9909           (goto-char (point-min))
9910           (while (cond ((eq type 'r)
9911                         (re-search-forward match nil t))
9912                        ((eq type 's)
9913                         (search-forward match nil t)))
9914             (end-of-line 1)
9915             (setq found t
9916                   arts (get-text-property (point) 'articles))
9917             ;; Found a match, update scores.
9918             (while arts
9919               (setq art (car arts)
9920                     arts (cdr arts))
9921               (setcdr art (+ score (cdr art)))))
9922           ;; Update expire date
9923           (cond ((null date))           ;Permanent entry.
9924                 (found                  ;Match, update date.
9925                  (gnus-score-set 'touched '(t) alist)
9926                  (setcar (nthcdr 2 kill) now))
9927                 ((< date expire) ;Old entry, remove.
9928                  (gnus-score-set 'touched '(t) alist)
9929                  (setcdr entries (cdr rest))
9930                  (setq rest entries)))
9931           (setq entries rest))))))
9932
9933 (defun gnus-score-string< (a1 a2)
9934   ;; Compare headers in articles A2 and A2.
9935   ;; The header index used is the free variable `gnus-score-index'.
9936   (string-lessp (aref (car a1) gnus-score-index)
9937                 (aref (car a2) gnus-score-index)))
9938
9939 (defun gnus-score-build-cons (article)
9940   ;; Build a `gnus-newsgroup-scored' type cons from ARTICLE.
9941   (cons (header-number (car article)) (cdr article)))
9942
9943 (defconst gnus-header-index
9944   ;; Name to index alist.
9945   '(("number" 0 gnus-score-integer)
9946     ("subject" 1 gnus-score-string)
9947     ("from" 2 gnus-score-string)
9948     ("date" 3 gnus-score-date)
9949     ("message-id" 4 gnus-score-string) 
9950     ("references" 5 gnus-score-string) 
9951     ("chars" 6 gnus-score-integer) 
9952     ("lines" 7 gnus-score-integer) 
9953     ("xref" 8 gnus-score-string)))
9954
9955 (defun gnus-score-file-name (newsgroup)
9956   "Return the name of a score file for NEWSGROUP."
9957   (cond  ((or (null newsgroup)
9958               (string-equal newsgroup ""))
9959           ;; The global score file is placed at top of the directory.
9960           (expand-file-name gnus-score-file-suffix
9961                             (or gnus-kill-files-directory "~/News")))
9962          (gnus-use-long-file-name
9963           ;; Append ".SCORE" to newsgroup name.
9964           (expand-file-name (concat newsgroup "." gnus-score-file-suffix)
9965                             (or gnus-kill-files-directory "~/News")))
9966          (t
9967           ;; Place "SCORE" under the hierarchical directory.
9968           (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
9969                                     "/" gnus-score-file-suffix)
9970                             (or gnus-kill-files-directory "~/News")))))
9971
9972 (defun gnus-score-score-files (group)
9973   "Return a list of all possible score files."
9974   (and gnus-global-score-files 
9975        (or gnus-internal-global-score-files
9976            (gnus-score-search-global-directories gnus-global-score-files)))
9977   (setq gnus-kill-files-directory 
9978         (file-name-as-directory
9979          (or gnus-kill-files-directory "~/News/")))
9980   (if (not (file-readable-p gnus-kill-files-directory))
9981       (setq gnus-score-file-list nil)
9982     (if gnus-use-long-file-name
9983         (if (or (not gnus-score-file-list)
9984                 (gnus-file-newer-than gnus-kill-files-directory
9985                                       (car gnus-score-file-list)))
9986               (setq gnus-score-file-list 
9987                     (cons (nth 5 (file-attributes gnus-kill-files-directory))
9988                           (nreverse 
9989                            (directory-files 
9990                             gnus-kill-files-directory t
9991                             (concat gnus-score-file-suffix "$"))))))
9992       (let ((dir (expand-file-name
9993                   (concat gnus-kill-files-directory
9994                           (gnus-replace-chars-in-string group ?. ?/))))
9995             (mdir (length (expand-file-name gnus-kill-files-directory)))
9996             files)
9997         (if (file-exists-p (concat dir "/" gnus-score-file-suffix))
9998             (setq files (list (concat dir "/" gnus-score-file-suffix))))
9999         (while (>= (1+ (length dir)) mdir)
10000           (and (file-exists-p (concat dir "/all/" gnus-score-file-suffix))
10001                (setq files (cons (concat dir "/all/" gnus-score-file-suffix)
10002                                  files)))
10003           (string-match "/[^/]*$" dir)
10004           (setq dir (substring dir 0 (match-beginning 0))))
10005         (setq gnus-score-file-list 
10006               (cons nil (nreverse files)))))
10007     (cdr gnus-score-file-list)))
10008         
10009 (defun gnus-score-find-single (group)
10010   "Return list containing the score file for GROUP."
10011   (list (gnus-score-file-name group)))
10012
10013 (defun gnus-score-find-hierarchical (group)
10014   "Return list of score files for GROUP.
10015 This includes the score file for the group and all its parents."
10016   (let ((all (copy-sequence '(nil)))
10017         (start 0))
10018     (while (string-match "\\." group (1+ start))
10019       (setq start (match-beginning 0))
10020       (setq all (cons (substring group 0 start) all)))
10021     (setq all (cons group all))
10022     (mapcar 'gnus-score-file-name (nreverse all))))
10023
10024 (defun gnus-score-find-bnews (group)
10025   "Return a list of score files for GROUP.
10026 The score files are those files in the ~/News directory which matches
10027 GROUP using BNews sys file syntax."
10028   (let* ((sfiles (append (gnus-score-score-files group)
10029                          gnus-internal-global-score-files))
10030          (kill-dir (file-name-as-directory 
10031                     (expand-file-name gnus-kill-files-directory)))
10032          (klen (length kill-dir))
10033          ofiles not-match regexp)
10034     (save-excursion
10035       (set-buffer (get-buffer-create "*gnus score files*"))
10036       (buffer-disable-undo (current-buffer))
10037       ;; Go through all score file names and create regexp with them
10038       ;; as the source.  
10039       (while sfiles
10040         (erase-buffer)
10041         (insert (car sfiles))
10042         (goto-char 1)
10043         ;; First remove the suffix itself.
10044         (re-search-forward (concat "." gnus-score-file-suffix "$"))
10045         (replace-match "") 
10046         (goto-char 1)
10047         (if (looking-at (regexp-quote kill-dir))
10048             ;; If the file name was just "SCORE", `klen' is one character
10049             ;; too much.
10050             (delete-char (min (1- (point-max)) klen))
10051           (goto-char (point-max))
10052           (search-backward "/")
10053           (delete-region (1+ (point)) (point-min)))
10054         ;; Translate "all" to ".*".
10055         (while (search-forward "all" nil t)
10056           (replace-match ".*"))
10057         (goto-char 1)
10058         ;; Deal with "not."s.
10059         (if (looking-at "not.")
10060             (progn
10061               (setq not-match t)
10062               (setq regexp (buffer-substring 5 (point-max))))
10063           (setq regexp (buffer-substring 1 (point-max)))
10064           (setq not-match nil))
10065         ;; Finally - if this resulting regexp matches the group name,
10066         ;; we add this score file to the list of score files
10067         ;; applicable to this group.
10068         (if (or (and not-match
10069                      (not (string-match regexp group)))
10070                 (and (not not-match)
10071                      (string-match regexp group)))
10072             (setq ofiles (cons (car sfiles) ofiles)))
10073         (setq sfiles (cdr sfiles)))
10074       (kill-buffer (current-buffer))
10075       ;; Slight kludge here - the last score file returned should be
10076       ;; the local score file, whether it exists or not. This is so
10077       ;; that any score commands the user enters will go to the right
10078       ;; file, and not end up in some global score file.
10079       (let ((localscore
10080              (expand-file-name
10081               (if gnus-use-long-file-name
10082                   (concat gnus-kill-files-directory group "." 
10083                           gnus-score-file-suffix)
10084                 (concat gnus-kill-files-directory
10085                         (gnus-replace-chars-in-string group ?. ?/)
10086                         "/" gnus-score-file-suffix)))))
10087         (and (member localscore ofiles)
10088              (delete localscore ofiles))
10089         (setq ofiles (cons localscore ofiles)))
10090       (nreverse ofiles))))
10091
10092 (defun gnus-score-search-global-directories (files)
10093   "Scan all global score directories for score files."
10094   ;; Set the variable `gnus-internal-global-score-files' to all
10095   ;; available global score files.
10096   (interactive (list gnus-global-score-files))
10097   (let (out)
10098     (while files
10099       (if (string-match "/$" (car files))
10100           (setq out (nconc (directory-files 
10101                             (car files) t
10102                             (concat gnus-score-file-suffix "$"))))
10103         (setq out (cons (car files) out)))
10104       (setq files (cdr files)))
10105     (setq gnus-internal-global-score-files out)))
10106
10107 ;;;
10108 ;;; Score mode.
10109 ;;;
10110
10111 (defvar gnus-score-mode-map nil)
10112 (defvar gnus-score-mode-hook nil)
10113
10114 (if gnus-score-mode-map
10115     ()
10116   (setq gnus-score-mode-map (copy-keymap emacs-lisp-mode-map))
10117   (define-key gnus-score-mode-map "\C-c\C-c" 'gnus-score-edit-done)
10118   (define-key gnus-score-mode-map "\C-c\C-d" 'gnus-score-edit-insert-date))
10119
10120 (defun gnus-score-mode ()
10121   "Mode for editing score files.
10122 This mode is an extended emacs-lisp mode.
10123
10124 \\{gnus-score-mode-map}"
10125   (interactive)
10126   (kill-all-local-variables)
10127   (use-local-map gnus-score-mode-map)
10128   (set-syntax-table emacs-lisp-mode-syntax-table)
10129   (setq major-mode 'gnus-score-mode)
10130   (setq mode-name "Score")
10131   (lisp-mode-variables nil)
10132   (run-hooks 'emacs-lisp-mode-hook 'gnus-score-mode-hook))
10133
10134 (defun gnus-score-edit-insert-date ()
10135   "Insert date in numerical format."
10136   (interactive)
10137   (insert (int-to-string (gnus-day-number (current-time-string)))))
10138
10139 (defun gnus-score-edit-done ()
10140   "Save the score file and return to the summary buffer."
10141   (interactive)
10142   (let ((bufnam (buffer-file-name (current-buffer))))
10143     (save-buffer)
10144     (kill-buffer (current-buffer))
10145     (and gnus-winconf-edit-score
10146          (set-window-configuration gnus-winconf-edit-score))
10147     (gnus-score-remove-from-cache bufnam)
10148     (gnus-score-load-file bufnam)))
10149
10150 \f
10151 ;;;
10152 ;;; Gnus Posting Functions
10153 ;;;
10154
10155 (defvar gnus-organization-file "/usr/lib/news/organization"
10156   "*Local news organization file.")
10157
10158 (defvar gnus-post-news-buffer "*post-news*")
10159 (defvar gnus-winconf-post-news nil)
10160
10161 ;;; Post news commands of Gnus group mode and summary mode
10162
10163 (defun gnus-group-post-news ()
10164   "Post an article."
10165   (interactive)
10166   (gnus-set-global-variables)
10167   ;; Save window configuration.
10168   (setq gnus-winconf-post-news (current-window-configuration))
10169   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
10170   (or gnus-newsgroup-name (setq gnus-newsgroup-name (gnus-group-group-name)))
10171   (unwind-protect
10172       (gnus-post-news 'post)
10173     (or (and (eq (current-buffer) (get-buffer gnus-post-news-buffer))
10174              (not (zerop (buffer-size))))
10175         ;; Restore last window configuration.
10176         (and gnus-winconf-post-news
10177              (set-window-configuration gnus-winconf-post-news))))
10178   ;; We don't want to return to summary buffer nor article buffer later.
10179   (setq gnus-winconf-post-news nil)
10180   (if (get-buffer gnus-summary-buffer)
10181       (bury-buffer gnus-summary-buffer))
10182   (if (get-buffer gnus-article-buffer)
10183       (bury-buffer gnus-article-buffer)))
10184
10185 (defun gnus-summary-post-news ()
10186   "Post an article."
10187   (interactive)
10188   (gnus-set-global-variables)
10189   ;; Save window configuration.
10190   (setq gnus-winconf-post-news (current-window-configuration))
10191   (unwind-protect
10192       (gnus-post-news 'post gnus-newsgroup-name)
10193     (or (and (eq (current-buffer) (get-buffer gnus-post-news-buffer))
10194              (not (zerop (buffer-size))))
10195         ;; Restore last window configuration.
10196         (and gnus-winconf-post-news
10197              (set-window-configuration gnus-winconf-post-news))))
10198   ;; We don't want to return to article buffer later.
10199   (if (get-buffer gnus-article-buffer)
10200       (bury-buffer gnus-article-buffer)))
10201
10202 (defun gnus-summary-followup (yank)
10203   "Compose a followup to an article.
10204 If prefix argument YANK is non-nil, original article is yanked automatically."
10205   (interactive "P")
10206   (gnus-set-global-variables)
10207   (save-window-excursion
10208     (gnus-summary-select-article t))
10209   (let ((headers gnus-current-headers)
10210         (gnus-newsgroup-name gnus-newsgroup-name))
10211     ;; Check Followup-To: poster.
10212     (set-buffer gnus-article-buffer)
10213     (if (and gnus-use-followup-to
10214              (string-equal "poster" (gnus-fetch-field "followup-to"))
10215              (or (not (eq gnus-use-followup-to t))
10216                  (not (gnus-y-or-n-p 
10217                        "Do you want to ignore `Followup-To: poster'? "))))
10218         ;; Mail to the poster.  Gnus is now RFC1036 compliant.
10219         (gnus-summary-reply yank)
10220       ;; Save window configuration.
10221       (setq gnus-winconf-post-news (current-window-configuration))
10222       (unwind-protect
10223           (gnus-post-news nil gnus-newsgroup-name
10224                           headers gnus-article-buffer yank)
10225         (or (and (eq (current-buffer) (get-buffer gnus-post-news-buffer))
10226                  (not (zerop (buffer-size))))
10227             ;; Restore last window configuration.
10228             (and gnus-winconf-post-news
10229                  (set-window-configuration gnus-winconf-post-news))))
10230       ;; We don't want to return to article buffer later.
10231       (bury-buffer gnus-article-buffer))))
10232
10233 (defun gnus-summary-followup-with-original ()
10234   "Compose a followup to an article and include the original article."
10235   (interactive)
10236   (gnus-summary-followup t))
10237
10238 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10239 (defun gnus-summary-followup-and-reply (yank)
10240   "Compose a followup and do an auto mail to author."
10241   (interactive "P")
10242   (let ((gnus-auto-mail-to-author t))
10243     (gnus-summary-followup yank)))
10244
10245 (defun gnus-summary-followup-and-reply-with-original ()
10246   "Compose a followup, include the original, and do an auto mail to author."
10247   (interactive)
10248   (gnus-summary-followup-and-reply t))
10249
10250 (defun gnus-summary-cancel-article ()
10251   "Cancel an article you posted."
10252   (interactive)
10253   (gnus-set-global-variables)
10254   (gnus-summary-select-article t)
10255   (gnus-eval-in-buffer-window gnus-article-buffer
10256                               (gnus-cancel-news)))
10257
10258 (defun gnus-summary-supersede-article ()
10259   "Compose an article that will supersede a previous article.
10260 This is done simply by taking the old article and adding a Supersedes
10261 header line with the old Message-ID."
10262   (interactive)
10263   (gnus-set-global-variables)
10264   (if (not
10265        (string-equal
10266         (downcase (mail-strip-quoted-names 
10267                    (header-from gnus-current-headers)))
10268         (downcase (mail-strip-quoted-names (gnus-inews-user-name)))))
10269       (error "This article is not yours."))
10270   (gnus-summary-select-article t)
10271   (save-excursion
10272     (set-buffer gnus-article-buffer)
10273     (let ((buffer-read-only nil))
10274       (goto-char (point-min))
10275       (search-forward "\n\n" nil t)
10276       (if (not (re-search-backward "^Message-ID: " nil t))
10277           (error "No Message-ID in this article"))))
10278   (if (gnus-post-news 'post gnus-newsgroup-name)
10279       (progn
10280         (erase-buffer)
10281         (insert-buffer gnus-article-buffer)
10282         (goto-char (point-min))
10283         (search-forward "\n\n" nil t)
10284         (if (not (re-search-backward "^Message-ID: " nil t))
10285             (error "No Message-ID in this article")
10286           (replace-match "Supersedes: "))
10287         (search-forward "\n\n")
10288         (forward-line -1)
10289         (insert mail-header-separator))))
10290
10291 \f
10292 ;;;###autoload
10293 (fset 'sendnews 'gnus-post-news)
10294
10295 ;;;###autoload
10296 (fset 'postnews 'gnus-post-news)
10297
10298 (defun gnus-post-news (post &optional group header article-buffer yank)
10299   "Begin editing a new USENET news article to be posted.
10300 Type \\[describe-mode] in the buffer to get a list of commands."
10301   (interactive (list t))
10302   (if (or (not gnus-novice-user)
10303           gnus-expert-user
10304           (not (eq 'post 
10305                    (nth 1 (assoc 
10306                            (format "%s" (car (gnus-find-method-for-group 
10307                                               gnus-newsgroup-name)))
10308                            gnus-valid-select-methods))))
10309           (assq 'to-address (gnus-find-method-for-group gnus-newsgroup-name))
10310           (gnus-y-or-n-p "Are you sure you want to post to all of USENET? "))
10311       (let ((sumart (if (not post)
10312                         (save-excursion
10313                           (set-buffer gnus-summary-buffer)
10314                           (cons (current-buffer) gnus-current-article))))
10315             (from (and header (header-from header)))
10316             subject follow-to real-group)
10317         (and gnus-interactive-post
10318              (not gnus-expert-user)
10319              post (not group)
10320              (progn
10321                (setq group 
10322                      (completing-read "Group: " gnus-active-hashtb nil t))
10323                (setq subject (read-string "Subject: "))))
10324         (setq mail-reply-buffer article-buffer)
10325
10326         (setq real-group (gnus-group-real-name group))
10327         (setq gnus-post-news-buffer 
10328               (gnus-request-post-buffer 
10329                post real-group subject header article-buffer
10330                (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
10331                (if (and (boundp 'gnus-followup-to-function)
10332                         gnus-followup-to-function)
10333                    (setq follow-to
10334                          (save-excursion
10335                            (set-buffer article-buffer)
10336                            (funcall gnus-followup-to-function group))))
10337                (eq gnus-use-followup-to t)))
10338         (if post
10339             (progn
10340               (gnus-configure-windows '(1 0 0))
10341               (switch-to-buffer gnus-post-news-buffer))
10342           (gnus-configure-windows '(0 1 0))
10343           (if (not yank)
10344               (progn
10345                 (switch-to-buffer article-buffer)
10346                 (pop-to-buffer gnus-post-news-buffer))
10347             (switch-to-buffer gnus-post-news-buffer)))
10348         (gnus-overload-functions)
10349         (make-local-variable 'gnus-article-reply)
10350         (make-local-variable 'gnus-article-check-size)
10351         (setq gnus-article-reply sumart)
10352         ;; Handle `gnus-auto-mail-to-author'.
10353         ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10354         (let ((to (if (eq gnus-auto-mail-to-author 'ask)
10355                       (and (y-or-n-p "Also send mail to author? ") from)
10356                     (and gnus-auto-mail-to-author from))))
10357           (if to
10358               (progn
10359                 (if (mail-fetch-field "To")
10360                     (progn
10361                       (beginning-of-line)
10362                       (insert "Cc: " to "\n"))
10363                   (mail-position-on-field "To")
10364                   (insert to)))))
10365         ;; Handle author copy using BCC field.
10366         (if (and gnus-mail-self-blind
10367                  (not (mail-fetch-field "bcc")))
10368             (progn
10369               (mail-position-on-field "Bcc")
10370               (insert (if (stringp gnus-mail-self-blind)
10371                           gnus-mail-self-blind
10372                         (user-login-name)))))
10373         ;; Handle author copy using FCC field.
10374         (if gnus-author-copy
10375             (progn
10376               (mail-position-on-field "Fcc")
10377               (insert gnus-author-copy)))
10378         (goto-char (point-min))
10379         (if post 
10380             (cond ((not group)
10381                    (re-search-forward "^Newsgroup:" nil t)
10382                    (end-of-line))
10383                   ((not subject)
10384                    (re-search-forward "^Subject:" nil t)
10385                    (end-of-line))
10386                   (t
10387                    (search-forward (concat "\n" mail-header-separator "\n"))))
10388           (search-forward (concat "\n" mail-header-separator "\n"))
10389           (if yank 
10390               (save-excursion
10391                 (run-hooks 'news-reply-header-hook)
10392                 (mail-yank-original nil)))
10393           (if gnus-post-prepare-function
10394               (funcall gnus-post-prepare-function group)))))
10395   (setq gnus-article-check-size (cons (buffer-size) (gnus-article-checksum)))
10396   (message "")
10397   t)
10398
10399 (defun gnus-inews-news (&optional use-group-method)
10400   "Send a news message.
10401 If given a prefix, and the group is a foreign group, this function
10402 will attempt to use the foreign server to post the article."
10403   (interactive "P")
10404   ;; Check whether the article is a good Net Citizen.
10405   (if (and gnus-article-check-size (not (gnus-inews-check-post)))
10406       ;; Aber nein!
10407       ()
10408     ;; Looks ok, so we do the nasty.
10409     (let* ((case-fold-search nil)
10410            (server-running (gnus-server-opened gnus-select-method))
10411            (reply gnus-article-reply))
10412       (save-excursion
10413         ;; Connect to default NNTP server if necessary.
10414         ;; Suggested by yuki@flab.fujitsu.junet.
10415         (gnus-start-news-server)        ;Use default server.
10416         ;; NNTP server must be opened before current buffer is modified.
10417         (widen)
10418         (goto-char (point-min))
10419         (run-hooks 'news-inews-hook)
10420         (save-restriction
10421           (narrow-to-region
10422            (point-min)
10423            (progn
10424              (goto-char (point-min))
10425              (search-forward (concat "\n" mail-header-separator "\n"))
10426              (point)))
10427
10428           ;; Correct newsgroups field: change sequence of spaces to comma and 
10429           ;; eliminate spaces around commas.  Eliminate imbedded line breaks.
10430           (goto-char (point-min))
10431           (if (search-forward-regexp "^Newsgroups: +" nil t)
10432               (save-restriction
10433                 (narrow-to-region
10434                  (point)
10435                  (if (re-search-forward "^[^ \t]" nil 'end)
10436                      (match-beginning 0)
10437                    (point-max)))
10438                 (goto-char (point-min))
10439                 (replace-regexp "\n[ \t]+" " ") ;No line breaks (too confusing)
10440                 (goto-char (point-min))
10441                 (replace-regexp "[ \t\n]*,[ \t\n]*\\|[ \t]+" ",")))
10442
10443           ;; Added by Per Abrahamsen <abraham@iesd.auc.dk>.
10444           ;; Help save the the world!
10445           (or 
10446            gnus-expert-user
10447            (let ((newsgroups (mail-fetch-field "newsgroups"))
10448                  (followup-to (mail-fetch-field "followup-to"))
10449                  groups to)
10450              (if (and (string-match "," newsgroups) (not followup-to))
10451                  (progn
10452                    (while (string-match "," newsgroups)
10453                      (setq groups
10454                            (cons (list (substring newsgroups
10455                                                   0 (match-beginning 0)))
10456                                  groups))
10457                      (setq newsgroups (substring newsgroups (match-end 0))))
10458                    (setq groups (nreverse (cons (list newsgroups) groups)))
10459
10460                    (setq to
10461                          (completing-read "Followups to: (default all groups) "
10462                                           groups))
10463                    (if (> (length to) 0)
10464                        (progn
10465                          (goto-char (point-min))
10466                          (insert "Followup-To: " to "\n")))))))
10467
10468           ;; Cleanup Followup-To.
10469           (goto-char (point-min))
10470           (if (search-forward-regexp "^Followup-To: +" nil t)
10471               (save-restriction
10472                 (narrow-to-region
10473                  (point)
10474                  (if (re-search-forward "^[^ \t]" nil 'end)
10475                      (match-beginning 0)
10476                    (point-max)))
10477                 (goto-char (point-min))
10478                 (replace-regexp "\n[ \t]+" " ") ;No line breaks (too confusing)
10479                 (goto-char (point-min))
10480                 (replace-regexp "[ \t\n]*,[ \t\n]*\\|[ \t]+" ",")))
10481
10482           ;; Mail the message too if To:, Bcc:. or Cc: exists.
10483           (if (or (mail-fetch-field "to" nil t)
10484                   (mail-fetch-field "bcc" nil t)
10485                   (mail-fetch-field "cc" nil t))
10486               (if gnus-mail-send-method
10487                   (save-excursion
10488                     (save-restriction
10489                       (widen)
10490                       (message "Sending via mail...")
10491                       
10492                       (if gnus-mail-courtesy-message
10493                           (progn
10494                             ;; Insert "courtesy" mail message.
10495                             (goto-char 1)
10496                             (re-search-forward mail-header-separator)
10497                             (forward-line 1)
10498                             (insert gnus-mail-courtesy-message)
10499                             (funcall gnus-mail-send-method)
10500                             (goto-char 1)
10501                             (search-forward gnus-mail-courtesy-message)
10502                             (replace-match ""))
10503                         (funcall gnus-mail-send-method))
10504
10505                       (message "Sending via mail... done")
10506                       
10507                       (goto-char 1)
10508                       (narrow-to-region
10509                        1 (re-search-forward mail-header-separator))
10510                       (goto-char 1)
10511                       (delete-matching-lines "BCC:.*")))
10512                 (ding)
10513                 (message "No mailer defined.  To: and/or Cc: fields ignored.")
10514                 (sit-for 1))))
10515
10516         ;; Send to NNTP server. 
10517         (message "Posting to USENET...")
10518         (if (gnus-inews-article use-group-method)
10519             (progn
10520               (message "Posting to USENET... done")
10521               (if (and reply
10522                        (get-buffer (car reply))
10523                        (buffer-name (car reply)))
10524                   (progn
10525                     (save-excursion
10526                       (set-buffer gnus-summary-buffer)
10527                       (gnus-summary-mark-article-as-replied 
10528                        (cdr reply))))))
10529           ;; We cannot signal an error.
10530           (ding) (message "Article rejected: %s" 
10531                           (gnus-status-message gnus-select-method)))
10532         (set-buffer-modified-p nil))
10533       ;; If NNTP server is opened by gnus-inews-news, close it by myself.
10534       (or server-running
10535           (gnus-close-server (gnus-find-method-for-group gnus-newsgroup-name)))
10536       (and (fboundp 'bury-buffer) (bury-buffer))
10537       ;; Restore last window configuration.
10538       (and gnus-winconf-post-news
10539            (set-window-configuration gnus-winconf-post-news))
10540       (setq gnus-winconf-post-news nil))))
10541
10542 (defun gnus-inews-check-post ()
10543   "Check whether the post looks ok."
10544   (and 
10545    ;; Check excessive size.
10546    (if (> (buffer-size) 60000)
10547        (gnus-y-or-n-p (format "The article is %d octets long. Really post? "
10548                               (buffer-size)))
10549      t)
10550    ;; Check for commands in Subject.
10551    (save-excursion
10552      (save-restriction
10553        (goto-char (point-min))
10554        (narrow-to-region (point) (search-forward mail-header-separator))
10555        (if (string-match "^cmsg " (mail-fetch-field "subject"))
10556            (gnus-y-or-n-p
10557             "The control code \"cmsg \" is in the subject. Really post? ")
10558          t)))
10559    ;; Check for control characters.
10560    (save-excursion
10561      (if (re-search-forward "[\000-\007\013\015-\037\200-\237]" nil t)
10562          (gnus-y-or-n-p 
10563           "The article contains control characters. Really post? ")
10564        t))
10565    ;; Check for multiple identical headers.
10566    (let (found)
10567      (save-excursion
10568        (save-restriction
10569          (goto-char (point-min))
10570          (narrow-to-region (point) (search-forward mail-header-separator))
10571          (goto-char (point-min))
10572          (while (and (not found) (re-search-forward "^[^ \t:]+: " nil t))
10573            (save-excursion
10574              (or (re-search-forward 
10575                   (concat "^" (setq found
10576                                     (buffer-substring (match-beginning 0) 
10577                                                       (match-end 0))))
10578                   nil t)
10579                  (setq found nil))))
10580          (if found
10581              (gnus-y-or-n-p 
10582               (format "Multiple %s headers. Really post? " found))
10583            t))))
10584    ;; Check for version and sendsys.
10585    (save-excursion
10586      (save-restriction
10587        (goto-char (point-min))
10588        (narrow-to-region (point) (search-forward mail-header-separator))
10589        (if (re-search-backward "^Sendsys:\\|^Version:" nil t)
10590            (gnus-yes-or-no-p
10591             (format "The article contains a %s command. Really post? "
10592                     (buffer-substring (match-beginning 0) (match-end 0))))
10593          t)))
10594    (save-excursion
10595      (save-restriction
10596        (goto-char (point-min))
10597        (narrow-to-region (point) (search-forward mail-header-separator))
10598        (let* ((case-fold-search t)
10599               (from (mail-fetch-field "from")))
10600          (if (and from
10601                   (string-match "@" from)
10602                   (not (string-match "@[^\\.]*\\." from)))
10603              (gnus-yes-or-no-p
10604               (format "The domain looks strange: \"%s\". Really post? "
10605                       from))
10606            t))))
10607    ;; Use the (size . checksum) variable to see whether the
10608    ;; article is empty or has only quoted text.
10609    (if (and (= (buffer-size) (car gnus-article-check-size))
10610             (= (gnus-article-checksum) (cdr gnus-article-check-size)))
10611        (gnus-yes-or-no-p
10612         "It looks like there's no new text in your article. Really post? ")
10613      t)))
10614
10615 (defun gnus-article-checksum ()
10616   (let ((sum 0))
10617     (save-excursion
10618       (while (not (eobp))
10619         (setq sum (logxor sum (following-char)))
10620         (forward-char 1)))
10621     sum))
10622
10623 (defun gnus-cancel-news ()
10624   "Cancel an article you posted."
10625   (interactive)
10626   (if (gnus-yes-or-no-p "Do you really want to cancel this article? ")
10627       (let ((from nil)
10628             (newsgroups nil)
10629             (message-id nil)
10630             (distribution nil))
10631         (save-excursion
10632           ;; Get header info. from original article.
10633           (save-restriction
10634             (gnus-article-show-all-headers)
10635             (goto-char (point-min))
10636             (search-forward "\n\n" nil 'move)
10637             (narrow-to-region (point-min) (point))
10638             (setq from (mail-fetch-field "from"))
10639             (setq newsgroups (mail-fetch-field "newsgroups"))
10640             (setq message-id (mail-fetch-field "message-id"))
10641             (setq distribution (mail-fetch-field "distribution")))
10642           ;; Verify if the article is absolutely user's by comparing
10643           ;; user id with value of its From: field.
10644           (if (not
10645                (string-equal
10646                 (downcase (mail-strip-quoted-names from))
10647                 (downcase (mail-strip-quoted-names (gnus-inews-user-name)))))
10648               (progn
10649                 (ding) (message "This article is not yours."))
10650             ;; Make control article.
10651             (set-buffer (get-buffer-create " *Gnus-canceling*"))
10652             (buffer-disable-undo (current-buffer))
10653             (erase-buffer)
10654             (insert "Newsgroups: " newsgroups "\n"
10655                     "Subject: cancel " message-id "\n"
10656                     "Control: cancel " message-id "\n"
10657                     mail-header-separator "\n"
10658                     "This is a cancel message from " from ".\n")
10659             ;; Send the control article to NNTP server.
10660             (message "Canceling your article...")
10661             (if (gnus-inews-article)
10662                 (message "Canceling your article... done")
10663               (ding) (message "Failed to cancel your article"))
10664             ;; Kill the article buffer.
10665             (kill-buffer (current-buffer)))))))
10666
10667 \f
10668 ;;; Lowlevel inews interface
10669
10670 (defun gnus-inews-article (&optional use-group-method)
10671   "Post an article in current buffer using NNTP protocol."
10672   (let ((artbuf (current-buffer))
10673         (tmpbuf (get-buffer-create " *Gnus-posting*")))
10674     (widen)
10675     (goto-char (point-max))
10676     ;; require a newline at the end for inews to append .signature to
10677     (or (= (preceding-char) ?\n)
10678         (insert ?\n))
10679     ;; Prepare article headers.  All message body such as signature
10680     ;; must be inserted before Lines: field is prepared.
10681     (save-restriction
10682       (goto-char (point-min))
10683       (narrow-to-region 
10684        (point-min) 
10685        (save-excursion
10686          (search-forward (concat "\n" mail-header-separator "\n")) 
10687          (forward-line -1) 
10688          (point)))
10689       (gnus-inews-insert-headers)
10690       (run-hooks gnus-inews-article-header-hook)
10691       (widen))
10692     (save-excursion
10693       (set-buffer tmpbuf)
10694       (buffer-disable-undo (current-buffer))
10695       (erase-buffer)
10696       (insert-buffer-substring artbuf)
10697       ;; Remove the header separator.
10698       (goto-char (point-min))
10699       (search-forward (concat "\n" mail-header-separator "\n"))
10700       (replace-match "\n\n")
10701       ;; This hook may insert a signature.
10702       (run-hooks 'gnus-prepare-article-hook)
10703       ;; Run final inews hooks.  This hook may do FCC.
10704       ;; The article must be saved before being posted because
10705       ;; `gnus-request-post' modifies the buffer.
10706       (run-hooks 'gnus-inews-article-hook)
10707       ;; Post an article to NNTP server.
10708       ;; Return NIL if post failed.
10709       (prog1
10710           (gnus-request-post 
10711            (if use-group-method
10712                (gnus-find-method-for-group gnus-newsgroup-name)
10713              gnus-select-method) use-group-method)
10714         (kill-buffer (current-buffer))))))
10715
10716 (defun gnus-inews-insert-headers ()
10717   "Prepare article headers.
10718 Headers already prepared in the buffer are not modified.
10719 Headers in `gnus-required-headers' will be generated."
10720   (let ((Date (gnus-inews-date))
10721         (Message-ID (gnus-inews-message-id))
10722         (Organization (gnus-inews-organization))
10723         (From (gnus-inews-user-name))
10724         (Path (gnus-inews-path))
10725         (Subject nil)
10726         (Newsgroups nil)
10727         (Distribution nil)
10728         (Lines (gnus-inews-lines))
10729         (X-Newsreader gnus-version)
10730         (headers gnus-required-headers)
10731         (case-fold-search t)
10732         header value)
10733     ;; First we remove any old Message-IDs. This might be slightly
10734     ;; fascist, but if the user really wants to generate Message-IDs
10735     ;; by herself, she should remove it from the `gnus-required-list'. 
10736     (goto-char (point-min))
10737     (and (memq 'Message-ID headers)
10738          (re-search-forward "^Message-ID:" nil t)
10739          (delete-region (progn (beginning-of-line) (point))
10740                         (progn (forward-line 1) (point))))
10741     ;; Remove NNTP-posting-host.
10742     (goto-char (point-min))
10743     (and (re-search-forward "nntp-posting-host^:" nil t)
10744          (delete-region (progn (beginning-of-line) (point))
10745                         (progn (forward-line 1) (point))))
10746     ;; Insert new Sender if the From is strange. 
10747     (let ((from (mail-fetch-field "from")))
10748       (if (and from (not (string= (downcase from) (downcase From))))
10749           (progn
10750             (goto-char (point-min))    
10751             (and (re-search-forward "^Sender:" nil t)
10752                  (delete-region (progn (beginning-of-line) (point))
10753                                 (progn (forward-line 1) (point))))
10754             (insert "Sender: " From "\n"))))
10755     ;; If there are References, and no "Re: ", then the thread has
10756     ;; changed name. See Son-of-1036.
10757     (if (and (mail-fetch-field "references")
10758              (get-buffer gnus-article-buffer))
10759         (let ((psubject (gnus-simplify-subject-re
10760                          (mail-fetch-field "subject")))
10761               subject)
10762           (save-excursion
10763             (set-buffer (get-buffer gnus-article-buffer))
10764             (save-restriction
10765               (gnus-narrow-to-headers)
10766               (if (setq subject (mail-fetch-field "subject"))
10767                   (progn
10768                     (and gnus-summary-gather-subject-limit
10769                          (> (length subject) gnus-summary-gather-subject-limit)
10770                          (setq subject
10771                                (substring subject 0
10772                                           gnus-summary-gather-subject-limit)))
10773                     (setq subject (gnus-simplify-subject-re subject))))))
10774           (or (and psubject subject (string= subject psubject))
10775               (progn
10776                 (string-match "@" Message-ID)
10777                 (setq Message-ID
10778                       (concat (substring Message-ID 0 (match-beginning 0))
10779                               "_-_" 
10780                               (substring Message-ID (match-beginning 0))))))))
10781     ;; Go through all the required headers and see if they are in the
10782     ;; articles already. If they are not, or are empty, they are
10783     ;; inserted automatically - except for Subject, Newsgroups and
10784     ;; Distribution. 
10785     (while headers
10786       (goto-char (point-min))
10787       (setq header (car headers))
10788       (if (or (not (re-search-forward 
10789                     (concat "^" (downcase (symbol-name header)) ":") nil t))
10790               (progn
10791                 (if (= (following-char) ? ) (forward-char 1) (insert " "))
10792                 (looking-at "[ \t]*$")))
10793           (progn
10794             (setq value (or (and (boundp header) (symbol-value header))
10795                             (read-from-minibuffer
10796                              (format "Empty header for %s; enter value: " 
10797                                      header))))
10798             (if (bolp)
10799                 (save-excursion
10800                   (goto-char (point-max))
10801                   (insert (symbol-name header) ": " value "\n"))
10802               (replace-match value))))
10803       (setq headers (cdr headers)))))
10804
10805 (defun gnus-inews-insert-signature ()
10806   "Insert a signature file.
10807 If `gnus-signature-function' is bound and returns a string, this
10808 string is used instead of the variable `gnus-signature-file'.
10809 In either case, if the string is a file name, this file is
10810 inserted. If the string is not a file name, the string itself is
10811 inserted. 
10812 If you never want any signature inserted, set both those variables to
10813 nil."
10814   (save-excursion
10815     (let ((signature 
10816            (or (and gnus-signature-function
10817                     (fboundp gnus-signature-function)
10818                     (funcall gnus-signature-function gnus-newsgroup-name))
10819                gnus-signature-file))
10820           b)
10821       (if (and signature
10822                (or (file-exists-p signature)
10823                    (string-match " " signature)
10824                    (not (string-match 
10825                          "^/[^/]+/" (expand-file-name signature)))))
10826           (progn
10827             (goto-char (point-max))
10828             ;; Delete any previous signatures.
10829             (if (and mail-signature (search-backward "\n-- \n" nil t))
10830                 (delete-region (1+ (point)) (point-max)))
10831             (insert "\n-- \n")
10832             (and (< 4 (setq b (count-lines 
10833                                (point)
10834                                (progn
10835                                  (if (file-exists-p signature)
10836                                      (insert-file-contents signature)
10837                                    (insert signature))
10838                                  (goto-char (point-max))
10839                                  (or (bolp) (insert "\n"))
10840                                  (point)))))
10841                  (not gnus-expert-user)
10842                  (not
10843                   (gnus-y-or-n-p
10844                    (format
10845                     "Your .sig is %d lines; it should be max 4. Really post? "
10846                     b)))
10847                  (if (file-exists-p signature)
10848                      (error (format "Edit %s." signature))
10849                    (error "Trim your signature."))))))))
10850
10851 (defun gnus-inews-do-fcc ()
10852   "Process FCC: fields in current article buffer.
10853 Unless the first character of the field is `|', the article is saved
10854 to the specified file using the function specified by the variable
10855 gnus-author-copy-saver.  The default function rmail-output saves in
10856 Unix mailbox format.
10857 If the first character is `|', the contents of the article is send to
10858 a program specified by the rest of the value."
10859   (let ((fcc-list nil)
10860         (fcc-file nil)
10861         (case-fold-search t))           ;Should ignore case.
10862     (save-excursion
10863       (save-restriction
10864         (goto-char (point-min))
10865         (search-forward "\n\n")
10866         (narrow-to-region (point-min) (point))
10867         (goto-char (point-min))
10868         (while (re-search-forward "^FCC:[ \t]*" nil t)
10869           (setq fcc-list
10870                 (cons (buffer-substring
10871                        (point)
10872                        (progn
10873                          (end-of-line)
10874                          (skip-chars-backward " \t")
10875                          (point)))
10876                       fcc-list))
10877           (delete-region (match-beginning 0)
10878                          (progn (forward-line 1) (point))))
10879         ;; Process FCC operations.
10880         (widen)
10881         (while fcc-list
10882           (setq fcc-file (car fcc-list))
10883           (setq fcc-list (cdr fcc-list))
10884           (cond ((string-match "^[ \t]*|[ \t]*\\(.*\\)[ \t]*$" fcc-file)
10885                  (let ((program (substring fcc-file
10886                                            (match-beginning 1) (match-end 1))))
10887                    ;; Suggested by yuki@flab.fujitsu.junet.
10888                    ;; Send article to named program.
10889                    (call-process-region (point-min) (point-max) shell-file-name
10890                                         nil nil nil "-c" program)))
10891                 (t
10892                  ;; Suggested by hyoko@flab.fujitsu.junet.
10893                  ;; Save article in Unix mail format by default.
10894                  (if (and gnus-author-copy-saver
10895                           (not (eq gnus-author-copy-saver 'rmail-output)))
10896                      (funcall gnus-author-copy-saver fcc-file)
10897                    (if (and (file-readable-p fcc-file) (rmail-file-p fcc-file))
10898                        (gnus-output-to-rmail fcc-file)
10899                      (rmail-output fcc-file 1 t t))))))))))
10900
10901 (defun gnus-inews-path ()
10902   "Return uucp path."
10903   (let ((login-name (gnus-inews-login-name)))
10904     (cond ((null gnus-use-generic-path)
10905            (concat (nth 1 gnus-select-method) "!" login-name))
10906           ((stringp gnus-use-generic-path)
10907            ;; Support GENERICPATH.  Suggested by vixie@decwrl.dec.com.
10908            (concat gnus-use-generic-path "!" login-name))
10909           (t login-name))))
10910
10911 (defun gnus-inews-user-name ()
10912   "Return user's network address as \"NAME@DOMAIN (FULL-NAME)\"."
10913   (let ((full-name (gnus-inews-full-name)))
10914     (concat (if (or gnus-user-login-name gnus-use-generic-from
10915                     gnus-local-domain (getenv "DOMAINNAME"))
10916                 (concat (gnus-inews-login-name) "@"
10917                         (gnus-inews-domain-name gnus-use-generic-from))
10918               user-mail-address)
10919             ;; User's full name.
10920             (cond ((string-equal full-name "") "")
10921                   ((string-equal full-name "&") ;Unix hack.
10922                    (concat " (" (user-login-name) ")"))
10923                   (t
10924                    (concat " (" full-name ")"))))))
10925
10926 (defun gnus-inews-login-name ()
10927   "Return login name."
10928   (or gnus-user-login-name (getenv "LOGNAME") (user-login-name)))
10929
10930 (defun gnus-inews-full-name ()
10931   "Return full user name."
10932   (or gnus-user-full-name (getenv "NAME") (user-full-name)))
10933
10934 (defun gnus-inews-domain-name (&optional genericfrom)
10935   "Return user's domain name.
10936 If optional argument GENERICFROM is a string, use it as the domain
10937 name; if it is non-nil, strip off local host name from the domain name.
10938 If the function `system-name' returns full internet name and the
10939 domain is undefined, the domain name is got from it."
10940   (if (or genericfrom gnus-local-domain (getenv "DOMAINNAME"))
10941       (let* ((system-name (system-name))
10942              (domain 
10943               (or (if (stringp genericfrom) genericfrom)
10944                   (getenv "DOMAINNAME")
10945                   gnus-local-domain
10946                   ;; Function `system-name' may return full internet name.
10947                   ;; Suggested by Mike DeCorte <mrd@sun.soe.clarkson.edu>.
10948                   (if (string-match "\\." system-name)
10949                       (substring system-name (match-end 0)))
10950                   (read-string "Domain name (no host): ")))
10951              (host (or (if (string-match "\\." system-name)
10952                            (substring system-name 0 (match-beginning 0)))
10953                        system-name)))
10954         (if (string-equal "." (substring domain 0 1))
10955             (setq domain (substring domain 1)))
10956         ;; Support GENERICFROM as same as standard Bnews system.
10957         ;; Suggested by ohm@kaba.junet and vixie@decwrl.dec.com.
10958         (cond ((null genericfrom)
10959                (concat host "." domain))
10960               ;;((stringp genericfrom) genericfrom)
10961               (t domain)))
10962     (if (string-match "\\." (system-name))
10963         (system-name)
10964       (substring user-mail-address 
10965                  (1+ (string-match "@" user-mail-address))))))
10966
10967 (defun gnus-inews-full-address ()
10968   (let ((domain (gnus-inews-domain-name))
10969         (system (system-name))
10970         (case-fold-search t))
10971     (if (string-match "\\." system) system
10972       (if (string-match (concat "^" (regexp-quote system)) domain) domain
10973         (concat system "." domain)))))
10974
10975 (defun gnus-inews-message-id ()
10976   "Generate unique Message-ID for user."
10977   ;; Message-ID should not contain a slash and should be terminated by
10978   ;; a number.  I don't know the reason why it is so.
10979   (concat "<" (gnus-inews-unique-id) "@" (gnus-inews-full-address) ">"))
10980
10981 (defun gnus-inews-unique-id ()
10982   "Generate unique ID from user name and current time."
10983   (concat (downcase (gnus-inews-login-name))
10984           (mapconcat 
10985            (lambda (num) (gnus-number-base-x num 3 31))
10986            (current-time) "")))
10987
10988 (defun gnus-inews-date ()
10989   "Current time string."
10990   (timezone-make-date-arpa-standard 
10991    (current-time-string) (current-time-zone)))
10992
10993 (defun gnus-inews-organization ()
10994   "Return user's organization.
10995 The ORGANIZATION environment variable is used if defined.
10996 If not, the variable `gnus-local-organization' is used instead.
10997 If it is a function, the function will be called with the current
10998 newsgroup name as the argument.
10999 If this is a file name, the contents of this file will be used as the
11000 organization."
11001   (let* ((organization 
11002           (or (getenv "ORGANIZATION")
11003               (if gnus-local-organization
11004                   (if (and (symbolp gnus-local-organization)
11005                            (fboundp gnus-local-organization))
11006                       (funcall gnus-local-organization gnus-newsgroup-name)
11007                     gnus-local-organization))
11008               gnus-organization-file
11009               "~/.organization")))
11010     (and (stringp organization)
11011          (> (length organization) 0)
11012          (or (file-exists-p organization)
11013              (string-match " " organization)
11014              (not (string-match  "^/[^/]+/" (expand-file-name organization))))
11015          (save-excursion
11016            (set-buffer (get-buffer-create " *Gnus organization*"))
11017            (buffer-disable-undo (current-buffer))
11018            (erase-buffer)
11019            (if (file-exists-p organization)
11020                (insert-file-contents organization)
11021              (insert organization))
11022            (goto-char (point-min))
11023            (while (re-search-forward " *\n *" nil t)
11024              (replace-match " "))
11025            (buffer-substring (point-min) (point-max))))))
11026
11027 (defun gnus-inews-lines ()
11028   "Count the number of lines and return numeric string."
11029   (save-excursion
11030     (save-restriction
11031       (widen)
11032       (goto-char (point-min))
11033       (search-forward "\n\n" nil 'move)
11034       (int-to-string (count-lines (point) (point-max))))))
11035
11036 \f
11037 ;;;
11038 ;;; Gnus Mail Functions 
11039 ;;;
11040
11041 ;;; Mail reply commands of Gnus summary mode
11042
11043 (defun gnus-summary-reply (yank)
11044   "Reply mail to news author.
11045 If prefix argument YANK is non-nil, original article is yanked automatically.
11046 Customize the variable gnus-mail-reply-method to use another mailer."
11047   (interactive "P")
11048   ;; Bug fix by jbw@bigbird.bu.edu (Joe Wells)
11049   ;; Stripping headers should be specified with mail-yank-ignored-headers.
11050   (gnus-summary-select-article t)
11051   (setq gnus-winconf-post-news (current-window-configuration))
11052   (let ((gnus-newsgroup-name gnus-newsgroup-name))
11053     (bury-buffer gnus-article-buffer)
11054     (funcall gnus-mail-reply-method yank)))
11055
11056 (defun gnus-summary-reply-with-original ()
11057   "Reply mail to news author with original article.
11058 Customize the variable gnus-mail-reply-method to use another mailer."
11059   (interactive)
11060   (gnus-summary-reply t))
11061
11062 (defun gnus-summary-mail-forward ()
11063   "Forward the current message to another user.
11064 Customize the variable gnus-mail-forward-method to use another mailer."
11065   (interactive)
11066   (gnus-summary-select-article t)
11067   (setq gnus-winconf-post-news (current-window-configuration))
11068   (set-buffer gnus-article-buffer)
11069   (let ((gnus-newsgroup-name gnus-newsgroup-name))
11070     (funcall gnus-mail-forward-method)))
11071
11072 (defun gnus-summary-mail-other-window ()
11073   "Compose mail in other window.
11074 Customize the variable `gnus-mail-other-window-method' to use another
11075 mailer."
11076   (interactive)
11077   (setq gnus-winconf-post-news (current-window-configuration))
11078   (let ((gnus-newsgroup-name gnus-newsgroup-name))
11079     (funcall gnus-mail-other-window-method)))
11080
11081 (defun gnus-mail-reply-using-mail (&optional yank to-address)
11082   (save-excursion
11083     (set-buffer gnus-summary-buffer)
11084     (let ((info (nth 2 (gnus-gethash gnus-newsgroup-name gnus-newsrc-hashtb)))
11085           (group (gnus-group-real-name gnus-newsgroup-name))
11086           (cur (cons (current-buffer) (cdr gnus-article-current)))
11087           from subject date to reply-to message-of
11088           references message-id sender follow-to cc)
11089       (set-buffer (get-buffer-create "*mail*"))
11090       (mail-mode)
11091       (make-local-variable 'gnus-article-reply)
11092       (setq gnus-article-reply cur)
11093       (use-local-map (copy-keymap mail-mode-map))
11094       (local-set-key "\C-c\C-c" 'gnus-mail-send-and-exit)
11095       (if (and (buffer-modified-p)
11096                (> (buffer-size) 0)
11097                (not (gnus-y-or-n-p 
11098                      "Unsent article being composed; erase it? ")))
11099           ()
11100         (erase-buffer)
11101         (save-excursion
11102           (set-buffer gnus-article-buffer)
11103           (let ((buffer-read-only nil))
11104             (goto-char (point-min))
11105             (narrow-to-region (point-min)
11106                               (progn (search-forward "\n\n") (point)))
11107             (add-text-properties (point-min) (point-max) '(invisible nil)))
11108           (if (and (boundp 'gnus-reply-to-function)
11109                    gnus-reply-to-function)
11110               (save-excursion
11111                 (save-restriction
11112                   (gnus-narrow-to-headers)
11113                   (setq follow-to (funcall gnus-reply-to-function group)))))
11114           (setq from (mail-fetch-field "from"))
11115           (setq date (mail-fetch-field "date"))
11116           (and from
11117                (let ((stop-pos 
11118                       (string-match "  *at \\|  *@ \\| *(\\| *<" from)))
11119                  (setq message-of
11120                        (concat (if stop-pos (substring from 0 stop-pos) from)
11121                                "'s message of " date))))
11122           (setq sender (mail-fetch-field "sender"))
11123           (setq subject (or (mail-fetch-field "subject")
11124                             "Re: none"))
11125           (or (string-match "^[Rr][Ee]:" subject)
11126               (setq subject (concat "Re: " subject)))
11127           (setq cc (mail-fetch-field "cc"))
11128           (setq reply-to (mail-fetch-field "reply-to"))
11129           (setq references (mail-fetch-field "references"))
11130           (setq message-id (mail-fetch-field "message-id"))
11131           (widen))
11132         (setq news-reply-yank-from from)
11133         (setq news-reply-yank-message-id message-id)
11134         (mail-setup (or to-address follow-to reply-to from sender "") 
11135                     subject message-of nil gnus-article-buffer nil)
11136         ;; Fold long references line to follow RFC1036.
11137         (mail-position-on-field "References")
11138         (let ((begin (- (point) (length "References: ")))
11139               (fill-column 78)
11140               (fill-prefix "\t"))
11141           (if references (insert references))
11142           (if (and references message-id) (insert " "))
11143           (if message-id (insert message-id))
11144           ;; The region must end with a newline to fill the region
11145           ;; without inserting extra newline.
11146           (fill-region-as-paragraph begin (1+ (point))))
11147         (goto-char (point-min))
11148         (search-forward (concat "\n" mail-header-separator "\n"))
11149         (if yank
11150             (let ((last (point)))
11151               (run-hooks 'news-reply-header-hook)
11152               (mail-yank-original nil)
11153               (goto-char last))))
11154       (if (not yank)
11155           (let ((mail (current-buffer)))
11156             (gnus-configure-windows '(0 0 1))
11157             (switch-to-buffer-other-window mail))
11158         (gnus-configure-windows '(0 1 0))
11159         (switch-to-buffer (current-buffer))))))
11160
11161 (defun gnus-mail-yank-original ()
11162   (interactive)
11163   (run-hooks 'news-reply-header-hook)
11164   (mail-yank-original nil))
11165
11166 (defun gnus-mail-send-and-exit ()
11167   (interactive)
11168   (let ((cbuf (current-buffer)))
11169     (mail-send-and-exit nil)
11170     (if (get-buffer gnus-group-buffer)
11171         (progn
11172           (save-excursion
11173             (set-buffer cbuf)
11174             (let ((reply gnus-article-reply))
11175               (if (and reply
11176                        (get-buffer (car reply))
11177                        (buffer-name (car reply)))
11178                   (progn
11179                     (set-buffer (car reply))
11180                     (and (cdr reply)
11181                          (gnus-summary-mark-article-as-replied 
11182                           (cdr reply)))))))
11183           (and gnus-winconf-post-news
11184                (set-window-configuration gnus-winconf-post-news))
11185           (setq gnus-winconf-post-news nil)))))
11186
11187 (defun gnus-mail-forward-using-mail ()
11188   "Forward the current message to another user using mail."
11189   ;; This is almost a carbon copy of rmail-forward in rmail.el.
11190   (let ((forward-buffer (current-buffer))
11191         (subject
11192          (concat "[" gnus-newsgroup-name "] "
11193                  (or (gnus-fetch-field "Subject") "")))
11194         beg)
11195     ;; If only one window, use it for the mail buffer.
11196     ;; Otherwise, use another window for the mail buffer
11197     ;; so that the Rmail buffer remains visible
11198     ;; and sending the mail will get back to it.
11199     (if (if (one-window-p t)
11200             (mail nil nil subject)
11201           (mail-other-window nil nil subject))
11202         (save-excursion
11203           (use-local-map (copy-keymap emacs-lisp-mode-map))
11204           (local-set-key "\C-c\C-c" 'gnus-mail-send-and-exit)
11205           (setq beg (goto-char (point-max)))
11206           (insert "------- Start of forwarded message -------\n")
11207           (insert-buffer forward-buffer)
11208           (goto-char (point-max))
11209           (insert "------- End of forwarded message -------\n")
11210           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>. 
11211           (goto-char beg)
11212           (while (setq beg (next-single-property-change (point) 'invisible))
11213             (goto-char beg)
11214             (delete-region beg (or (next-single-property-change 
11215                                     (point) 'invisible)
11216                                    (point-max))))
11217           ;; You have a chance to arrange the message.
11218           (run-hooks 'gnus-mail-forward-hook)))))
11219
11220 (defun gnus-mail-other-window-using-mail ()
11221   "Compose mail other window using mail."
11222   (mail-other-window nil nil nil nil nil nil (get-buffer gnus-article-buffer))
11223   (use-local-map (copy-keymap emacs-lisp-mode-map))
11224   (local-set-key "\C-c\C-c" 'gnus-mail-send-and-exit))
11225
11226 \f
11227 ;;;
11228 ;;; Dribble file
11229 ;;;
11230
11231 (defvar gnus-dribble-ignore nil)
11232
11233 (defun gnus-dribble-file-name ()
11234   (concat gnus-startup-file "-dribble"))
11235
11236 (defun gnus-dribble-open ()
11237   (save-excursion 
11238     (set-buffer 
11239      (setq gnus-dribble-buffer (find-file-noselect (gnus-dribble-file-name))))
11240     (buffer-disable-undo (current-buffer))
11241     (bury-buffer gnus-dribble-buffer)
11242     (auto-save-mode t)
11243     (goto-char (point-max))))
11244
11245 (defun gnus-dribble-enter (string)
11246   (if (and (not gnus-dribble-ignore)
11247            gnus-dribble-buffer
11248            (buffer-name gnus-dribble-buffer))
11249       (let ((obuf (current-buffer)))
11250         (set-buffer gnus-dribble-buffer)
11251         (insert string "\n")
11252         (set-window-point (get-buffer-window (current-buffer)) (point-max))
11253         (set-buffer obuf))))
11254
11255 (defun gnus-dribble-read-file ()
11256   (let ((dribble-file (gnus-dribble-file-name)))
11257     (save-excursion 
11258       (set-buffer (setq gnus-dribble-buffer 
11259                         (get-buffer-create 
11260                          (file-name-nondirectory dribble-file))))
11261       (gnus-add-current-to-buffer-list)
11262       (erase-buffer)
11263       (set-visited-file-name dribble-file)
11264       (buffer-disable-undo (current-buffer))
11265       (bury-buffer (current-buffer))
11266       (set-buffer-modified-p nil)
11267       (let ((auto (make-auto-save-file-name))
11268             (gnus-dribble-ignore t))
11269         (if (or (file-exists-p auto) (file-exists-p dribble-file))
11270             (progn
11271               (if (file-newer-than-file-p auto dribble-file)
11272                   (setq dribble-file auto))
11273               (insert-file-contents dribble-file)
11274               (if (not (zerop (buffer-size)))
11275                   (set-buffer-modified-p t))
11276               (if (gnus-y-or-n-p 
11277                    "Auto-save file exists. Do you want to read it? ")
11278                   (progn
11279                     (message "Reading %s..." dribble-file) 
11280                     (eval-current-buffer)
11281                     (message "Reading %s...done" dribble-file)))))))))
11282
11283 (defun gnus-dribble-delete-file ()
11284   (if (file-exists-p (gnus-dribble-file-name))
11285       (delete-file (gnus-dribble-file-name)))
11286   (if gnus-dribble-buffer
11287       (save-excursion
11288         (set-buffer gnus-dribble-buffer)
11289         (let ((auto (make-auto-save-file-name)))
11290           (if (file-exists-p auto)
11291               (delete-file auto))
11292           (erase-buffer)
11293           (set-buffer-modified-p nil)))))
11294
11295 (defun gnus-dribble-save ()
11296   (if (and gnus-dribble-buffer
11297            (buffer-name gnus-dribble-buffer))
11298       (save-excursion
11299         (set-buffer gnus-dribble-buffer)
11300         (save-buffer))))
11301
11302 (defun gnus-dribble-clear ()
11303   (save-excursion
11304     (if (and gnus-dribble-buffer
11305              (buffer-name (get-buffer gnus-dribble-buffer)))
11306         (progn
11307           (set-buffer gnus-dribble-buffer)
11308           (erase-buffer)
11309           (set-buffer-modified-p nil)
11310           (setq buffer-saved-size (buffer-size))))))
11311
11312 ;;;
11313 ;;; Server Communication
11314 ;;;
11315
11316 ;; All the Gnus backends have the same interface, and should return
11317 ;; data in a similar format. Below is an overview of what functions
11318 ;; these packages must supply and what results they should return.
11319 ;;
11320 ;; Variables:
11321 ;;
11322 ;; `nntp-server-buffer' - All data should be returned to Gnus in this
11323 ;; buffer. 
11324 ;;
11325 ;; Functions for the imaginary backend `choke':
11326 ;;
11327 ;; `choke-retrieve-headers ARTICLES &optional GROUP SERVER'
11328 ;; Should return all headers for all ARTICLES, or return NOV lines for
11329 ;; the same.
11330 ;;
11331 ;; `choke-request-group GROUP &optional SERVER DISCARD'
11332 ;; Switch to GROUP. If DISCARD is nil, active information on the group
11333 ;; must be returned.
11334 ;;
11335 ;; `choke-close-group GROUP &optional SERVER'
11336 ;; Close group. Most backends won't have to do anything with this
11337 ;; call, but it is an opportunity to clean up, if that is needed. It
11338 ;; is called when Gnus exits a group.
11339 ;;
11340 ;; `choke-request-article ARTICLE &optional GROUP SERVER'
11341 ;; Return ARTICLE, which is either an article number or
11342 ;; message-id. Note that not all backends can return articles based on
11343 ;; message-id. 
11344 ;;
11345 ;; `choke-request-list SERVER'
11346 ;; Return a list of all newsgroups on SERVER.
11347 ;;
11348 ;; `choke-request-list-newsgroups SERVER'
11349 ;; Return a list of descriptions of all newsgroups on SERVER.
11350 ;;
11351 ;; `choke-request-newgroups DATE &optional SERVER'
11352 ;; Return a list of all groups that have arrived after DATE on
11353 ;; SERVER. Note that the date doesn't have to be respected - Gnus will
11354 ;; always check whether the groups are old or not. Backends that do
11355 ;; not store date information may just return the entire list of
11356 ;; groups, although this might not be a good idea in general.
11357 ;;
11358 ;; `choke-request-post-buffer METHOD HEADER ARTICLE-BUFFER GROUP INFO'
11359 ;; Should return a buffer that is suitable for "posting". nnspool and
11360 ;; nntp return a `*post-buffer*', and nnmail return a `*mail*'
11361 ;; buffer. This function should fill out the appropriate headers. 
11362 ;;
11363 ;; `choke-request-post &optional SERVER'
11364 ;; Function that will be called from a buffer to be posted. 
11365 ;;
11366 ;; `choke-open-server SERVER &optional ARGUMENT'
11367 ;; Open a connection to SERVER.
11368 ;;
11369 ;; `choke-close-server &optional SERVER'
11370 ;; Close the connection to SERVER.
11371 ;;
11372 ;; `choke-server-opened &optional SERVER'
11373 ;; Whether the conenction to SERVER is opened or not.
11374 ;;
11375 ;; `choke-server-status &optional SERVER'
11376 ;; Should return a status string (not in the nntp buffer, but as the
11377 ;; result of the function).
11378 ;;
11379 ;; The following functions are optional and apply only to backends
11380 ;; that are able to control the contents of their groups totally
11381 ;; (ie. mail backends.)  Backends that aren't able to do that
11382 ;; shouldn't define these functions at all. Gnus will check for their
11383 ;; presence before attempting to call them.
11384 ;;
11385 ;; `choke-request-expire-articles ARTICLES &optional NEWSGROUP SERVER'
11386 ;; Should expire (according to some aging scheme) all ARTICLES. Most
11387 ;; backends will not be able to expire articles. Should return a list
11388 ;; of all articles that were not expired.
11389 ;;
11390 ;; `choke-request-move-article ARTICLE GROUP SERVER ACCEPT-FORM &optional LAST'
11391 ;; Should move ARTICLE from GROUP on SERVER by using ACCEPT-FORM.
11392 ;; Removes any information it has added to the article (extra headers,
11393 ;; whatever - make it as clean as possible), and then passes the
11394 ;; article on by evaling ACCEPT-FORM, which is normally a call to the
11395 ;; function described below. If the ACCEPT-FORM returns a non-nil
11396 ;; value, the article should then be deleted. If LAST is nil, that
11397 ;; means that there will be further calls to this function. This might
11398 ;; be taken as an advice not to save buffers/internal variables just
11399 ;; yet, but wait until the last call to speed things up.
11400 ;;
11401 ;; `choke-request-accept-article GROUP &optional LAST' 
11402 ;; The contents of the current buffer will be put into GROUP.  There
11403 ;; should, of course, be an article in the current buffer.  This
11404 ;; function is normally only called by the function described above,
11405 ;; and LAST works the same way as in that function.
11406 ;;
11407 ;; `choke-request-replace-article ARTICLE GROUP BUFFER'
11408 ;; Replace ARTICLE in GROUP with the contents of BUFFER.
11409 ;; This provides an easy interface for allowing editing of
11410 ;; articles. Note that even headers may be edited, so the backend has
11411 ;; to update any tables (nov buffers, etc) that it maintains after
11412 ;; replacing the article.
11413 ;;
11414 ;; All these functions must return nil if they couldn't service the
11415 ;; request. If the optional arguments are not supplied, some "current"
11416 ;; or "default" values should be used. In short, one should emulate an
11417 ;; NNTP server, in a way.
11418 ;;
11419 ;; If you want to write a new backend, you just have to supply the
11420 ;; functions listed above. In addition, you must enter the new backend
11421 ;; into the list of valid select methods:
11422 ;; (setq gnus-valid-select-methods 
11423 ;;       (cons '("choke" mail) gnus-valid-select-methods))
11424 ;; The first element in this list is the name of the backend. Other
11425 ;; elemnets may be `mail' (for mail groups),  `post' (for news
11426 ;; groups), `none' (neither), `respool' (for groups that can control
11427 ;; their contents). 
11428
11429 (defun gnus-start-news-server (&optional confirm)
11430   "Open a method for getting news.
11431 If CONFIRM is non-nil, the user will be asked for an NNTP server."
11432   (let (how where)
11433     (if gnus-current-select-method
11434         ;; Stream is already opened.
11435         nil
11436       ;; Open NNTP server.
11437       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
11438       (if confirm
11439           (progn
11440             ;; Read server name with completion.
11441             (setq gnus-nntp-server
11442                   (completing-read "NNTP server: "
11443                                    (mapcar (lambda (server) (list server))
11444                                            (cons (list gnus-nntp-server)
11445                                                  gnus-secondary-servers))
11446                                    nil nil gnus-nntp-server))))
11447
11448       (if (and gnus-nntp-server 
11449                (stringp gnus-nntp-server)
11450                (not (string= gnus-nntp-server "")))
11451           (setq gnus-select-method
11452                 (cond ((or (string= gnus-nntp-server "")
11453                            (string= gnus-nntp-server "::"))
11454                        (list 'nnspool (system-name)))
11455                       ((string-match ":" gnus-nntp-server)
11456                        (list 'nnmh gnus-nntp-server))
11457                       (t
11458                        (list 'nntp gnus-nntp-server)))))
11459
11460       (setq how (car gnus-select-method))
11461       (setq where (car (cdr gnus-select-method)))
11462       (cond ((eq how 'nnspool)
11463              (require 'nnspool)
11464              (message "Looking up local news spool..."))
11465             ((eq how 'nnmh)
11466              (require 'nnmh)
11467              (message "Looking up mh spool..."))
11468             (t
11469              (require 'nntp)))
11470       (setq gnus-current-select-method gnus-select-method)
11471       (run-hooks 'gnus-open-server-hook)
11472       (or 
11473        ;; gnus-open-server-hook might have opened it
11474        (gnus-server-opened gnus-select-method)  
11475        (gnus-open-server gnus-select-method)
11476        (error "%s" (gnus-nntp-message 
11477                     (format "Cannot open NNTP server on %s" 
11478                             where))))
11479       gnus-select-method)))
11480
11481 (defun gnus-check-news-server (method)
11482   "If the news server is down, start it up again."
11483   (let ((method (if method method gnus-select-method)))
11484     (if (gnus-server-opened method)
11485         ;; Stream is already opened.
11486         t
11487       ;; Open server.
11488       (message "Opening server %s on %s..." (car method) (nth 1 method))
11489       (run-hooks 'gnus-open-server-hook)
11490       (or (gnus-server-opened method)
11491           (gnus-open-server method))
11492       (message ""))))
11493
11494 (defun gnus-nntp-message (&optional message)
11495   "Check the status of the NNTP server.
11496 If the status of the server is clear and MESSAGE is non-nil, MESSAGE
11497 is returned insted of the status string."
11498   (let ((status (gnus-status-message (gnus-find-method-for-group 
11499                                       gnus-newsgroup-name)))
11500         (message (or message "")))
11501     (if (and (stringp status) (> (length status) 0))
11502         status message)))
11503
11504 (defun gnus-get-function (method function)
11505   (let ((func (intern (format "%s-%s" (car method) function))))
11506     (if (not (fboundp func)) 
11507         (progn
11508           (require (car method))
11509           (if (not (fboundp func)) 
11510               (error "No such function: %s" func))))
11511     func))
11512
11513 ;; Specifying port number suggested by Stephane Laveau <laveau@corse.inria.fr>.
11514 (defun gnus-open-server (method)
11515   (apply (gnus-get-function method 'open-server) (cdr method)))
11516
11517 (defun gnus-close-server (method)
11518   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
11519
11520 (defun gnus-request-list (method)
11521   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
11522
11523 (defun gnus-request-list-newsgroups (method)
11524   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
11525
11526 (defun gnus-request-newgroups (date method)
11527   (funcall (gnus-get-function method 'request-newgroups) 
11528            date (nth 1 method)))
11529
11530 (defun gnus-server-opened (method)
11531   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
11532
11533 (defun gnus-status-message (method)
11534   (let ((method (if (stringp method) (gnus-find-method-for-group method)
11535                   method)))
11536     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
11537
11538 (defun gnus-request-group (group &optional dont-check)
11539   (let ((method (gnus-find-method-for-group group)))
11540     (funcall (gnus-get-function method 'request-group) 
11541              (gnus-group-real-name group) (nth 1 method) dont-check)))
11542
11543 (defun gnus-request-group-description (group)
11544   (let ((method (gnus-find-method-for-group group))
11545         (func 'request-group-description))
11546     (and (gnus-check-backend-function func group)
11547          (funcall (gnus-get-function method func) 
11548                   (gnus-group-real-name group) (nth 1 method)))))
11549
11550 (defun gnus-close-group (group)
11551   (let ((method (gnus-find-method-for-group group)))
11552     (funcall (gnus-get-function method 'close-group) 
11553              (gnus-group-real-name group) (nth 1 method))))
11554
11555 (defun gnus-retrieve-headers (articles group)
11556   (let ((method (gnus-find-method-for-group group)))
11557     (funcall (gnus-get-function method 'retrieve-headers) 
11558              articles (gnus-group-real-name group) (nth 1 method))))
11559
11560 (defun gnus-request-article (article group buffer)
11561   (let ((method (gnus-find-method-for-group group)))
11562     (funcall (gnus-get-function method 'request-article) 
11563              article (gnus-group-real-name group) (nth 1 method) buffer)))
11564
11565 (defun gnus-request-head (article group)
11566   (let ((method (gnus-find-method-for-group group)))
11567     (funcall (gnus-get-function method 'request-head) 
11568              article (gnus-group-real-name group) (nth 1 method))))
11569
11570 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11571 (defun gnus-request-post-buffer (post group subject header artbuf
11572                                       info follow-to respect-poster)
11573    (let* ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
11574           (method
11575            (if (and gnus-post-method
11576                     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11577                     (memq 'post (assoc
11578                                  (format "%s" (car (gnus-find-method-for-group
11579                                                     gnus-newsgroup-name)))
11580                                         gnus-valid-select-methods)))
11581                gnus-post-method
11582              (gnus-find-method-for-group gnus-newsgroup-name))))
11583      (let ((mail-self-blind nil)
11584            (mail-archive-file-name nil))
11585        (funcall (gnus-get-function method 'request-post-buffer) 
11586                 post group subject header artbuf info follow-to
11587                 respect-poster))))
11588
11589 (defun gnus-request-post (method &optional force)
11590   (and (not force) gnus-post-method
11591        (memq 'post (assoc (format "%s" (car method))
11592                           gnus-valid-select-methods))
11593        (setq method gnus-post-method))
11594   (funcall (gnus-get-function method 'request-post) 
11595            (nth 1 method)))
11596
11597 (defun gnus-request-expire-articles (articles group &optional force)
11598   (let ((method (gnus-find-method-for-group group)))
11599     (funcall (gnus-get-function method 'request-expire-articles) 
11600              articles (gnus-group-real-name group) (nth 1 method)
11601              force)))
11602
11603 (defun gnus-request-move-article 
11604   (article group server accept-function &optional last)
11605   (let ((method (gnus-find-method-for-group group)))
11606     (funcall (gnus-get-function method 'request-move-article) 
11607              article (gnus-group-real-name group) 
11608              (nth 1 method) accept-function last)))
11609
11610 (defun gnus-request-accept-article (group &optional last)
11611   (let ((func (if (symbolp group) group
11612                 (car (gnus-find-method-for-group group)))))
11613     (funcall (intern (format "%s-request-accept-article" func))
11614              (if (stringp group) (gnus-group-real-name group) group)
11615              last)))
11616
11617 (defun gnus-request-replace-article (article group buffer)
11618   (let ((func (car (gnus-find-method-for-group group))))
11619     (funcall (intern (format "%s-request-replace-article" func))
11620              article (gnus-group-real-name group) buffer)))
11621
11622 (defun gnus-find-method-for-group (group)
11623   (or gnus-override-method
11624       (and (not group)
11625            gnus-select-method)
11626       (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
11627         (if (or (not info)
11628                 (not (nth 4 info)))
11629             gnus-select-method
11630           (nth 4 info)))))
11631
11632 (defun gnus-check-backend-function (func group)
11633   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
11634                  group)))
11635     (fboundp (intern (format "%s-%s" method func)))))
11636
11637 (defun gnus-methods-using (method)
11638   (let ((valids gnus-valid-select-methods)
11639         outs)
11640     (while valids
11641       (if (memq method (car valids)) 
11642           (setq outs (cons (car valids) outs)))
11643       (setq valids (cdr valids)))
11644     outs))
11645
11646 ;;; 
11647 ;;; Active & Newsrc File Handling
11648 ;;;
11649
11650 ;; Newsrc related functions.
11651 ;; Gnus internal format of gnus-newsrc-assoc:
11652 ;; (("alt.general" 3 (1 . 1))
11653 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
11654 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
11655 ;; The first item is the group name; the second is the subscription
11656 ;; level; the third is either a range of a list of ranges of read
11657 ;; articles, the optional fourth element is a list of marked articles,
11658 ;; the optional fifth element is the select method.
11659 ;;
11660 ;; Gnus internal format of gnus-newsrc-hashtb:
11661 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
11662 ;; This is the entry for "alt.misc". The first element is the number
11663 ;; of unread articles in "alt.misc". The cdr of this entry is the
11664 ;; element *before* "alt.misc" in gnus-newsrc-assoc, which makes is
11665 ;; trivial to remove or add new elements into gnus-newsrc-assoc
11666 ;; without scanning the entire list. So, to get the actual information
11667 ;; of "alt.misc", you'd say something like 
11668 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
11669 ;;
11670 ;; Gnus internal format of gnus-active-hashtb:
11671 ;; ((1 . 1))
11672 ;;  (5 . 10))
11673 ;;  (67 . 99)) ...)
11674 ;; The only element in each entry in this hash table is a range of
11675 ;; (possibly) available articles. (Articles in this range may have
11676 ;; been expired or cancelled.)
11677 ;;
11678 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
11679 ;; ("alt.misc" "alt.test" "alt.general" ...)
11680
11681 (defun gnus-setup-news (&optional rawfile level)
11682   "Setup news information.
11683 If RAWFILE is non-nil, the .newsrc file will also be read.
11684 If LEVEL is non-nil, the news will be set up at level LEVEL."
11685   (let ((init (not (and gnus-newsrc-assoc gnus-active-hashtb (not rawfile)))))
11686     ;; Clear some variables to re-initialize news information.
11687     (if init (setq gnus-newsrc-assoc nil gnus-active-hashtb nil))
11688     ;; Read the active file and create `gnus-active-hashtb'.
11689     ;; If `gnus-read-active-file' is nil, then we just create an empty
11690     ;; hash table. The partial filling out of the hash table will be
11691     ;; done in `gnus-get-unread-articles'.
11692     (if (and gnus-read-active-file (not level))
11693         (gnus-read-active-file)
11694       (setq gnus-active-hashtb (make-vector 4095 0)))
11695
11696     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
11697     (if init (gnus-read-newsrc-file rawfile))
11698     ;; Find the number of unread articles in each non-dead group.
11699     (gnus-get-unread-articles (or level 6))
11700     ;; Find new newsgroups and treat them.
11701     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level))
11702         (gnus-find-new-newsgroups))
11703     (if (and init gnus-check-bogus-newsgroups 
11704              gnus-read-active-file (not level))
11705         (gnus-check-bogus-newsgroups))))
11706
11707 (defun gnus-find-new-newsgroups ()
11708   "Search for new newsgroups and add them.
11709 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
11710 The `-n' option line from .newsrc is respected."
11711   (interactive)
11712   (or (gnus-check-first-time-used)
11713       (if (or (consp gnus-check-new-newsgroups)
11714               (eq gnus-check-new-newsgroups 'ask-server))
11715           (gnus-ask-server-for-new-groups)
11716         (let ((groups 0)
11717               group new-newsgroups)
11718           (or gnus-have-read-active-file (gnus-read-active-file))
11719           (setq gnus-newsrc-last-checked-date (current-time-string))
11720           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
11721           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
11722           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
11723           (mapatoms
11724            (lambda (sym)
11725              (setq group (symbol-name sym))
11726              (if (or (gnus-gethash group gnus-killed-hashtb)
11727                      (gnus-gethash group gnus-newsrc-hashtb))
11728                  ()
11729                (if (and gnus-newsrc-options-n-yes
11730                         (string-match gnus-newsrc-options-n-yes group))
11731                    (progn
11732                      (setq groups (1+ groups))
11733                      (gnus-sethash group group gnus-killed-hashtb)
11734                      (funcall gnus-subscribe-options-newsgroup-method group))
11735                  (if (or (null gnus-newsrc-options-n-no)
11736                          (not (string-match gnus-newsrc-options-n-no group)))
11737                      ;; Add this group.
11738                      (progn
11739                        (setq groups (1+ groups))
11740                        (gnus-sethash group group gnus-killed-hashtb)
11741                        (if gnus-subscribe-hierarchical-interactive
11742                            (setq new-newsgroups (cons group new-newsgroups))
11743                          (funcall gnus-subscribe-newsgroup-method group)))))))
11744            gnus-active-hashtb)
11745           (if new-newsgroups 
11746               (gnus-subscribe-hierarchical-interactive new-newsgroups))
11747           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11748           (if (> groups 0)
11749               (message "%d new newsgroup%s arrived." 
11750                        groups (if (> groups 1) "s have" " has")))))))
11751
11752 (defun gnus-ask-server-for-new-groups ()
11753   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
11754          (methods (cons gnus-select-method 
11755                         (append
11756                          (and (consp gnus-check-new-newsgroups)
11757                               gnus-check-new-newsgroups)
11758                          gnus-secondary-select-methods)))
11759          (groups 0)
11760          (new-date (current-time-string))
11761          hashtb group new-newsgroups got-new)
11762     ;; Go thorugh both primary and secondary select methods and
11763     ;; request new newsgroups.  
11764     (while methods
11765       (if (gnus-request-newgroups date (car methods))
11766           (save-excursion
11767             (setq got-new t)
11768             (or hashtb (setq hashtb (gnus-make-hashtable 
11769                                      (count-lines (point-min) (point-max)))))
11770             (set-buffer nntp-server-buffer)
11771             ;; Enter all the new groups in a hashtable.
11772             (gnus-active-to-gnus-format (car methods) hashtb)))
11773       (setq methods (cdr methods)))
11774     (and got-new (setq gnus-newsrc-last-checked-date new-date))
11775     ;; Now all new groups from all select methods are in `hashtb'.
11776     (mapatoms
11777      (lambda (group-sym)
11778        (setq group (symbol-name group-sym))
11779        (if (or (gnus-gethash group gnus-newsrc-hashtb)
11780                (member group gnus-zombie-list)
11781                (member group gnus-killed-list))
11782            ;; The group is already known.
11783            ()
11784          (gnus-sethash group (symbol-value group-sym) gnus-active-hashtb)
11785          (if (and gnus-newsrc-options-n-yes
11786                   (string-match gnus-newsrc-options-n-yes group))
11787              (progn
11788                (setq groups (1+ groups))
11789                (funcall gnus-subscribe-options-newsgroup-method group))
11790            (if (or (null gnus-newsrc-options-n-no)
11791                    (not (string-match gnus-newsrc-options-n-no group)))
11792                ;; Add this group.
11793                (progn
11794                  (setq groups (1+ groups))
11795                  (if gnus-subscribe-hierarchical-interactive
11796                      (setq new-newsgroups (cons group new-newsgroups))
11797                    (funcall gnus-subscribe-newsgroup-method group)))))))
11798      hashtb)
11799     (if new-newsgroups 
11800         (gnus-subscribe-hierarchical-interactive new-newsgroups))
11801     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11802     (if (> groups 0)
11803         (message "%d new newsgroup%s arrived." 
11804                  groups (if (> groups 1) "s have" " has")))
11805     got-new))
11806
11807 (defun gnus-check-first-time-used ()
11808   (if (or (> (length gnus-newsrc-assoc) 1)
11809           (file-exists-p gnus-startup-file)
11810           (file-exists-p (concat gnus-startup-file ".el"))
11811           (file-exists-p (concat gnus-startup-file ".eld")))
11812       nil
11813     (message "First time user; subscribing you to default groups")
11814     (or gnus-have-read-active-file (gnus-read-active-file))
11815     (setq gnus-newsrc-last-checked-date (current-time-string))
11816     (let ((groups gnus-default-subscribed-newsgroups)
11817           group)
11818       (if (eq groups t)
11819           nil
11820         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
11821         (mapatoms
11822          (lambda (sym)
11823            (setq group (symbol-name sym))
11824            (if (and gnus-newsrc-options-n-yes
11825                     (string-match gnus-newsrc-options-n-yes group))
11826                (funcall gnus-subscribe-options-newsgroup-method group)
11827              (and (or (null gnus-newsrc-options-n-no)
11828                       (not (string-match gnus-newsrc-options-n-no group)))
11829                   (setq gnus-killed-list (cons group gnus-killed-list)))))
11830          gnus-active-hashtb)
11831         (while groups
11832           (if (gnus-gethash (car groups) gnus-active-hashtb)
11833               (gnus-group-change-level (car groups) 3 9))
11834           (setq groups (cdr groups)))
11835         (gnus-group-make-help-group)
11836         (and gnus-novice-user
11837              (message (substitute-command-keys "\\<gnus-group-mode-map>\\[gnus-group-list-killed] to list killed groups")))))))
11838
11839 ;; `gnus-group-change-level' is the fundamental function for changing
11840 ;; subscription levels of newsgroups. This might mean just changing
11841 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
11842 ;; again, which subscribes/unsubscribes a group, which is equally
11843 ;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
11844 ;; from 8-9 to 1-7 means that you remove the group from the list of
11845 ;; killed (or zombie) groups and add them to the (kinda) subscribed
11846 ;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
11847 ;; which is trivial.
11848 ;; ENTRY can either be a string (newsgroup name) or a list (if
11849 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
11850 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
11851 ;; entries. 
11852 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
11853 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
11854 ;; after. 
11855 (defun gnus-group-change-level (entry level &optional oldlevel
11856                                       previous fromkilled)
11857   (let ((pinfo entry)
11858         group info active num)
11859     ;; Glean what info we can from the arguments
11860     (if (consp entry)
11861         (if fromkilled (setq group (nth 1 entry))
11862           (setq group (car (nth 2 entry))))
11863       (setq group entry))
11864     (if (and (stringp entry)
11865              oldlevel 
11866              (< oldlevel 8))
11867         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
11868     (if (and (not oldlevel)
11869              (listp entry))
11870         (setq oldlevel (car (cdr (nth 2 entry)))))
11871     (if (stringp previous)
11872         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
11873
11874     (gnus-dribble-enter
11875      (format "(gnus-group-change-level %S %S %S %S %S)" 
11876              group level oldlevel (car (nth 2 previous)) fromkilled))
11877     
11878     ;; Then we remove the newgroup from any old structures, if needed.
11879     ;; If the group was killed, we remove it from the killed or zombie
11880     ;; list. If not, and it is in fact going to be killed, we remove
11881     ;; it from the newsrc hash table and assoc.
11882     (cond ((>= oldlevel 8)
11883            (if (= oldlevel 8)
11884                (setq gnus-zombie-list (delete group gnus-zombie-list))
11885              (setq gnus-killed-list (delete group gnus-killed-list))))
11886           (t
11887            (if (>= level 8)
11888                (progn
11889                  (gnus-sethash (car (nth 2 entry))
11890                                nil gnus-newsrc-hashtb)
11891                  (if (nth 3 entry)
11892                      (setcdr (gnus-gethash (car (nth 3 entry))
11893                                            gnus-newsrc-hashtb)
11894                              (cdr entry)))
11895                  (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
11896
11897     ;; Finally we enter (if needed) the list where it is supposed to
11898     ;; go, and change the subscription level. If it is to be killed,
11899     ;; we enter it into the killed or zombie list.
11900     (cond ((>= level 8)
11901            (if (= level 8)
11902                (setq gnus-zombie-list (cons group gnus-zombie-list))
11903              (setq gnus-killed-list (cons group gnus-killed-list))))
11904           (t
11905            ;; If the list is to be entered into the newsrc assoc, and
11906            ;; it was killed, we have to create an entry in the newsrc
11907            ;; hashtb format and fix the pointers in the newsrc assoc.
11908            (if (>= oldlevel 8)
11909                (progn
11910                  (if (listp entry)
11911                      (progn
11912                        (setq info (cdr entry))
11913                        (setq num (car entry)))
11914                    (setq active (gnus-gethash group gnus-active-hashtb))
11915                    (setq num (if active (- (1+ (cdr active)) (car active)) t))
11916                    ;; Check whether the group is foreign. If so, the
11917                    ;; foreign select method has to be entered into the
11918                    ;; info. 
11919                    (let ((method (gnus-group-method-name group)))
11920                      (if (eq method gnus-select-method)
11921                          (setq info (list group level nil))
11922                        (setq info (list group level nil nil method)))))
11923                  (setq entry (cons info (if previous (cdr (cdr previous))
11924                                           (cdr gnus-newsrc-assoc))))
11925                  (setcdr (if previous (cdr previous) gnus-newsrc-assoc)
11926                          entry)
11927                  (gnus-sethash group (cons num (if previous (cdr previous)
11928                                                  gnus-newsrc-assoc))
11929                                gnus-newsrc-hashtb)
11930                  (if (cdr entry)
11931                      (setcdr (gnus-gethash (car (car (cdr entry)))
11932                                            gnus-newsrc-hashtb)
11933                              entry)))
11934              ;; It was alive, and it is going to stay alive, so we
11935              ;; just change the level and don't change any pointers or
11936              ;; hash table entries.
11937              (setcar (cdr (car (cdr (cdr entry)))) level))))))
11938
11939 (defun gnus-kill-newsgroup (newsgroup)
11940   "Obsolete function. Kills a newsgroup."
11941   (gnus-group-change-level (gnus-gethash newsgroup gnus-newsrc-hashtb) 9))
11942
11943 (defun gnus-check-bogus-newsgroups (&optional confirm)
11944   "Remove bogus newsgroups.
11945 If CONFIRM is non-nil, the user has to confirm the deletion of every
11946 newsgroup." 
11947   (let ((newsrc (cdr gnus-newsrc-assoc))
11948         bogus group)
11949     (message "Checking bogus newsgroups...")
11950     (or gnus-have-read-active-file (gnus-read-active-file))
11951     ;; Find all bogus newsgroup that are subscribed.
11952     (while newsrc
11953       (setq group (car (car newsrc)))
11954       (if (or (gnus-gethash group gnus-active-hashtb)
11955               (nth 4 (car newsrc))
11956               (and confirm
11957                    (not (gnus-y-or-n-p
11958                          (format "Remove bogus newsgroup: %s " group)))))
11959           ;; Active newsgroup.
11960           ()
11961         ;; Found a bogus newsgroup.
11962         (setq bogus (cons group bogus)))
11963       (setq newsrc (cdr newsrc)))
11964     ;; Remove all bogus subscribed groups by first killing them, and
11965     ;; then removing them from the list of killed groups.
11966     (while bogus
11967       (gnus-group-change-level 
11968        (gnus-gethash (car bogus) gnus-newsrc-hashtb) 9)
11969       (setq gnus-killed-list (delete (car bogus) gnus-killed-list))
11970       (setq bogus (cdr bogus)))
11971     ;; Then we remove all bogus groups from the list of killed and
11972     ;; zombie groups. They are are removed without confirmation.
11973     (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
11974           killed)
11975       (while dead-lists
11976         (setq killed (symbol-value (car dead-lists)))
11977         (while killed
11978           (setq group (car killed))
11979           (or (gnus-gethash group gnus-active-hashtb)
11980               ;; The group is bogus.
11981               (set (car dead-lists)
11982                    (delete group (symbol-value (car dead-lists)))))
11983           (setq killed (cdr killed)))
11984         (setq dead-lists (cdr dead-lists))))
11985     (message "Checking bogus newsgroups... done")))
11986
11987 (defun gnus-check-duplicate-killed-groups ()
11988   "Remove duplicates from the list of killed groups."
11989   (interactive)
11990   (let ((killed gnus-killed-list))
11991     (while killed
11992       (message "%d" (length killed))
11993       (setcdr killed (delete (car killed) (cdr killed)))
11994       (setq killed (cdr killed)))))
11995
11996 ;; Go though `gnus-newsrc-assoc' and compare with `gnus-active-hashtb'
11997 ;; and compute how many unread articles there are in each group.
11998 (defun gnus-get-unread-articles (&optional level)
11999   (let ((newsrc (cdr gnus-newsrc-assoc))
12000         (level (or level 6))
12001         info group active virtuals)
12002     (message "Checking new news...")
12003     (while newsrc
12004       (setq info (car newsrc))
12005       (setq group (car info))
12006       (setq active (gnus-gethash group gnus-active-hashtb))
12007
12008       ;; Check newsgroups. If the user doesn't want to check them, or
12009       ;; they can't be checked (for instance, if the news server can't
12010       ;; be reached) we just set the number of unread articles in this
12011       ;; newsgroup to t. This means that Gnus thinks that there are
12012       ;; unread articles, but it has no idea how many.
12013       (if (nth 4 info)
12014           (if (or (and gnus-activate-foreign-newsgroups 
12015                        (not (numberp gnus-activate-foreign-newsgroups)))
12016                   (and (numberp gnus-activate-foreign-newsgroups)
12017                        (<= (nth 1 info) gnus-activate-foreign-newsgroups)
12018                        (<= (nth 1 info) level)))
12019               (if (eq (car (nth 4 info)) 'nnvirtual)
12020                   (setq virtuals (cons info virtuals))
12021                 (setq active (gnus-activate-newsgroup (car info)))))
12022         (if (and (not gnus-read-active-file)
12023                  (<= (nth 1 info) level))
12024             (progn
12025               (setq active (gnus-activate-newsgroup (car info))))))
12026       
12027       (or active (progn (gnus-sethash group nil gnus-active-hashtb)
12028                         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
12029       (and active (gnus-get-unread-articles-in-group info active))
12030       (setq newsrc (cdr newsrc)))
12031
12032     ;; Activate the virtual groups. This has to be done after all the
12033     ;; other groups. 
12034     ;; !!! If one virtual group contains another virtual group, even
12035     ;; doing it this way might cause problems.
12036     (while virtuals
12037       (gnus-activate-newsgroup (car (car virtuals)))
12038       (setq virtuals (cdr virtuals)))
12039
12040     (message "Checking new news... done")))
12041
12042 ;; Create a hash table out of the newsrc alist. The `car's of the
12043 ;; alist elements are used as keys.
12044 (defun gnus-make-hashtable-from-newsrc-alist ()
12045   (let ((alist gnus-newsrc-assoc)
12046          prev)
12047     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
12048     (setq alist 
12049           (setq prev (setq gnus-newsrc-assoc 
12050                            (cons (list "dummy.group" 0 nil) alist))))
12051     (while alist
12052       (gnus-sethash (car (car alist)) (cons nil prev) gnus-newsrc-hashtb)
12053       (setq prev alist)
12054       (setq alist (cdr alist)))))
12055
12056 (defun gnus-make-hashtable-from-killed ()
12057   "Create a hash table from the killed and zombie lists."
12058   (let ((lists '(gnus-killed-list gnus-zombie-list))
12059         list)
12060     (setq gnus-killed-hashtb 
12061           (gnus-make-hashtable 
12062            (+ (length gnus-killed-list) (length gnus-zombie-list))))
12063     (while lists
12064       (setq list (symbol-value (car lists)))
12065       (setq lists (cdr lists))
12066       (while list
12067         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
12068         (setq list (cdr list))))))
12069
12070 (defun gnus-get-unread-articles-in-group (info active)
12071   (let* ((range (nth 2 info))
12072          (num 0)
12073          (marked (nth 3 info))
12074          srange lowest group highest)
12075     ;; Modify the list of read articles according to what articles 
12076     ;; are available; then tally the unread articles and add the
12077     ;; number to the group hash table entry.
12078     (cond ((zerop (cdr active))
12079            (setq num 0))
12080           ((not range)
12081            (setq num (- (1+ (cdr active)) (car active))))
12082           ((atom (car range))
12083            ;; Fix a single (num . num) range according to the
12084            ;; active hash table.
12085            (and (< (cdr range) (car active)) (setcdr range (car active)))
12086            (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
12087            ;; Compute number of unread articles.
12088            (setq num (max 0 (- (cdr active) 
12089                                (- (1+ (cdr range)) (car range))))))
12090           (t
12091            ;; The read list is a list of ranges. Fix them according to
12092            ;; the active hash table.
12093            ;; First peel off any elements that are below the lower
12094            ;; active limit. 
12095            (while (and (cdr range) 
12096                        (>= (car active) 
12097                            (or (and (atom (car (cdr range))) (car (cdr range)))
12098                                (car (car (cdr range))))))
12099              (setcdr (car range) (cdr (car (cdr range))))
12100              (setcdr range (cdr (cdr range))))
12101            ;; Adjust the first element to be the same as the lower limit. 
12102            (if (and (not (atom (car range))) 
12103                     (< (cdr (car range)) (car active)))
12104                (setcdr (car range) (car active)))
12105            ;; Then we want to peel off any elements that are higher
12106            ;; than the upper active limit.  
12107            (let ((srange range))
12108              ;; Go past all legal elements.
12109              (while (and (cdr srange) 
12110                          (<= (or (and (atom (car (cdr srange)))
12111                                       (car (cdr srange)))
12112                                  (car (car (cdr srange)))) (cdr active)))
12113                (setq srange (cdr srange)))
12114              (if (cdr srange)
12115                  ;; Nuke all remaining illegal elements.
12116                  (setcdr srange nil))
12117
12118              ;; Adjust the final element.
12119              (if (and (not (atom (car srange)))
12120                       (> (cdr (car srange)) (cdr active)))
12121                  (setcdr (car srange) (cdr active))))
12122            ;; Compute the number of unread articles.
12123            (while range
12124              (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
12125                                          (cdr (car range))))
12126                                  (or (and (atom (car range)) (car range))
12127                                      (car (car range))))))
12128              (setq range (cdr range)))
12129            (setq num (max 0 (- (cdr active) num)))))
12130     (and info
12131          (progn
12132            (and (assq 'tick marked)
12133                 (inline (gnus-remove-illegal-marked-articles
12134                          (assq 'tick marked) (nth 2 info))))
12135            (and (assq 'dormant marked)
12136                 (inline (gnus-remove-illegal-marked-articles
12137                          (assq 'dormant marked) (nth 2 info))))
12138            (setcar
12139             (gnus-gethash (car info) gnus-newsrc-hashtb) 
12140             (setq num (max 0 (- num (length (cdr (assq 'tick marked)))
12141                                 (length (cdr (assq 'dormant marked)))))))))
12142     num))
12143
12144 (defun gnus-remove-illegal-marked-articles (marked ranges)
12145   (let ((m (cdr marked)))
12146     ;; Make sure that all ticked articles are a subset of the unread
12147     ;; articles. 
12148     (while m
12149       (if (gnus-member-of-range (car m) ranges)
12150           (setcdr marked (cdr m))
12151         (setq marked m))
12152       (setq m (cdr m)))))
12153
12154 (defun gnus-activate-newsgroup (group)
12155   (let ((method (gnus-find-method-for-group group))
12156         active)
12157     (and (or (gnus-server-opened method) (gnus-open-server method))
12158          (gnus-request-group group)
12159          (save-excursion
12160            (set-buffer nntp-server-buffer)
12161            (goto-char 1)
12162            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
12163                 (progn
12164                   (goto-char (match-beginning 1))
12165                   (gnus-sethash 
12166                    group (setq active (cons (read (current-buffer))
12167                                             (read (current-buffer))))
12168                    gnus-active-hashtb))
12169                 active)))))
12170
12171 (defun gnus-update-read-articles 
12172   (group unread unselected ticked &optional domarks replied expirable killed
12173          dormant bookmark score)
12174   "Update the list of read and ticked articles in GROUP using the
12175 UNREAD and TICKED lists.
12176 Note: UNSELECTED has to be sorted over `<'.
12177 Returns whether the updating was successful."
12178   (let* ((active (gnus-gethash group gnus-active-hashtb))
12179          (entry (gnus-gethash group gnus-newsrc-hashtb))
12180          (number (car entry))
12181          (info (nth 2 entry))
12182          (marked (nth 3 info))
12183          (prev 1)
12184          (unread (sort (copy-sequence unread) (function <)))
12185          last read)
12186     (if (or (not info) (not active))
12187         ;; There is no info on this group if it was, in fact,
12188         ;; killed. Gnus stores no information on killed groups, so
12189         ;; there's nothing to be done. 
12190         ;; One could store the information somewhere temporarily,
12191         ;; perhaps... Hmmm... 
12192         ()
12193       ;; Remove any negative articles numbers.
12194       (while (and unread (< (car unread) 0))
12195         (setq unread (cdr unread)))
12196       (setq unread (sort (append unselected unread) '<))
12197       ;; Set the number of unread articles in gnus-newsrc-hashtb.
12198       (setcar entry (max 0 (- (length unread) (length ticked) 
12199                               (length dormant))))
12200       ;; Compute the ranges of read articles by looking at the list of
12201       ;; unread articles.  
12202       (while unread
12203         (if (/= (car unread) prev)
12204             (setq read (cons (if (= prev (1- (car unread))) prev
12205                                (cons prev (1- (car unread)))) read)))
12206         (setq prev (1+ (car unread)))
12207         (setq unread (cdr unread)))
12208       (if (<= prev (cdr active))
12209           (setq read (cons (cons prev (cdr active)) read)))
12210       ;; Enter this list into the group info.
12211       (setcar (cdr (cdr info)) 
12212               (if (> (length read) 1) (nreverse read) (car read)))
12213       ;; Enter the list of ticked articles.
12214       (gnus-set-marked-articles 
12215        info ticked
12216        (if domarks replied (cdr (assq 'reply marked)))
12217        (if domarks expirable (cdr (assq 'expire marked)))
12218        (if domarks killed (cdr (assq 'killed marked)))
12219        (if domarks dormant (cdr (assq 'dormant marked)))
12220        (if domarks bookmark (cdr (assq 'bookmark marked)))
12221        (if domarks score (cdr (assq 'score marked))))
12222       t)))
12223
12224 (defun gnus-make-articles-unread (group articles)
12225   "Mark ARTICLES in GROUP as unread."
12226   (let ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
12227                          (gnus-gethash (gnus-group-real-name group)
12228                                        gnus-newsrc-hashtb)))))
12229     (setcar (nthcdr 2 info)
12230             (gnus-remove-from-range (nth 2 info) articles))
12231     (gnus-group-update-group group t)))
12232
12233 (defun gnus-read-active-file ()
12234   "Get active file from NNTP server."
12235   (gnus-group-set-mode-line)
12236   (let ((methods (cons gnus-select-method gnus-secondary-select-methods)))
12237     (setq gnus-have-read-active-file nil)
12238     (while methods
12239       (let* ((where (nth 1 (car methods)))
12240              (mesg (format "Reading active file%s via %s..."
12241                            (if (and where (not (zerop (length where))))
12242                                (concat " from " where) "")
12243                            (car (car methods)))))
12244         (message mesg)
12245         (if (gnus-request-list (car methods)) ; Get active 
12246             (save-excursion
12247               (set-buffer nntp-server-buffer)
12248               (gnus-active-to-gnus-format 
12249                (and gnus-have-read-active-file (car methods)))
12250               (setq gnus-have-read-active-file t)
12251               (message "%s...done" mesg))
12252           (message "Cannot read active file from %s server." 
12253                    (car (car methods)))
12254           (ding)))
12255       (setq methods (cdr methods)))))
12256
12257 ;; rewritten by jwz based on ideas from Rick Sladkey <jrs@world.std.com>
12258 ;; Further rewrites by lmi.
12259 (defun gnus-active-to-gnus-format (method &optional hashtb)
12260   "Convert active file format to internal format.
12261 Lines matching `gnus-ignored-newsgroups' are ignored."
12262   (let ((cur (current-buffer))
12263         (hashtb (or hashtb 
12264                     (if method
12265                         gnus-active-hashtb
12266                       (setq gnus-active-hashtb
12267                             (gnus-make-hashtable 
12268                              (count-lines (point-min) (point-max))))))))
12269     ;; Delete unnecessary lines.
12270     (goto-char (point-min))
12271     (delete-matching-lines gnus-ignored-newsgroups)
12272     (and method (not (eq method gnus-select-method))
12273          (let ((prefix (gnus-group-prefixed-name "" method)))
12274            (goto-char (point-min))
12275            (while (and (not (eobp))
12276                        (null (insert prefix))
12277                        (zerop (forward-line 1))))))
12278     (goto-char (point-min))
12279     ;; Store active file in hashtable.
12280     (save-restriction
12281       (if (or (re-search-forward "\n.\r?$" nil t)
12282               (goto-char (point-max)))
12283           (progn
12284             (beginning-of-line)
12285             (narrow-to-region (point-min) (point))))
12286       (goto-char (point-min))
12287       (if (string-match "%[oO]" gnus-group-line-format)
12288           ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
12289           ;; If we want information on moderated groups, we use this
12290           ;; loop...   
12291           (condition-case ()
12292               (let ((mod-hashtb (make-vector 7 0))
12293                     group max mod)
12294                 (while (not (eobp))
12295                   (setq group (let ((obarray hashtb))
12296                                 (read cur)))
12297                   (setq max (read cur))
12298                   (set group (cons (read cur) max))
12299                   ;; Enter moderated groups into a list.
12300                   (if (string= 
12301                        (symbol-name  (let ((obarray mod-hashtb)) (read cur)))
12302                        "m")
12303                       (setq gnus-moderated-list 
12304                             (cons (symbol-name group) gnus-moderated-list)))
12305                   (forward-line 1)))
12306             (error 
12307              (progn (ding) (message "Possible error in active file."))))
12308         ;; And if we do not care about moderation, we use this loop,
12309         ;; which is faster.
12310         (condition-case ()
12311             (let (group max)
12312               (while (not (eobp))
12313                 ;; group gets set to a symbol interned in the hash table
12314                 ;; (what a hack!!)
12315                 (setq group (let ((obarray hashtb)) (read cur)))
12316                 (setq max (read cur))
12317                 (set group (cons (read cur) max))
12318                 (forward-line 1)))
12319           (error 
12320            (progn (ding) (message "Possible error in active file."))))))))
12321
12322 (defun gnus-read-newsrc-file (&optional force)
12323   "Read startup file.
12324 If FORCE is non-nil, the .newsrc file is read."
12325   (setq gnus-current-startup-file (gnus-make-newsrc-file gnus-startup-file))
12326   ;; Reset variables that might be defined in the .newsrc.eld file.
12327   (let ((variables gnus-variable-list))
12328     (while variables
12329       (set (car variables) nil)
12330       (setq variables (cdr variables))))
12331   (let* ((newsrc-file gnus-current-startup-file)
12332          (quick-file (concat newsrc-file ".el")))
12333     (save-excursion
12334       ;; We always load the .newsrc.eld file. If always contains
12335       ;; much information that can not be gotten from the .newsrc
12336       ;; file (ticked articles, killed groups, foreign methods, etc.)
12337       (gnus-read-newsrc-el-file quick-file)
12338  
12339       (if (or force
12340               (and (file-newer-than-file-p newsrc-file quick-file)
12341                    (file-newer-than-file-p newsrc-file 
12342                                            (concat quick-file "d")))
12343               (not gnus-newsrc-assoc))
12344           ;; We read the .newsrc file. Note that if there if a
12345           ;; .newsrc.eld file exists, it has already been read, and
12346           ;; the `gnus-newsrc-hashtb' has been created. While reading
12347           ;; the .newsrc file, Gnus will only use the information it
12348           ;; can find there for changing the data already read -
12349           ;; ie. reading the .newsrc file will not trash the data
12350           ;; already read (except for read articles).
12351           (save-excursion
12352             (message "Reading %s..." newsrc-file)
12353             (set-buffer (find-file-noselect newsrc-file))
12354             (buffer-disable-undo (current-buffer))
12355             (gnus-newsrc-to-gnus-format)
12356             (kill-buffer (current-buffer))
12357             (message "Reading %s... done" newsrc-file)))
12358       (and gnus-use-dribble-file (gnus-dribble-read-file)))))
12359
12360 (defun gnus-read-newsrc-el-file (file)
12361   (let ((ding-file (concat file "d")))
12362     ;; We always, always read the .eld file.
12363     (message "Reading %s..." ding-file)
12364     (condition-case nil
12365         (load ding-file t t t)
12366       (error nil))
12367     (gnus-uncompress-newsrc-assoc)
12368     (gnus-make-hashtable-from-newsrc-alist)
12369     (if (not (file-newer-than-file-p file ding-file))
12370         ()
12371       ;; Old format quick file
12372       (message "Reading %s..." file)
12373       ;; The .el file is newer than the .eld file, so we read that one
12374       ;; as well. 
12375       (gnus-read-old-newsrc-el-file file))))
12376
12377 ;; Parse the old-style quick startup file
12378 (defun gnus-read-old-newsrc-el-file (file)
12379   (let (newsrc killed marked group g m len info)
12380     (prog1
12381         (let (gnus-killed-assoc gnus-marked-assoc gnus-newsrc-assoc)
12382           (prog1
12383               (condition-case nil
12384                   (load file t t t)
12385                 (error nil))
12386             (setq newsrc gnus-newsrc-assoc
12387                   killed gnus-killed-assoc
12388                   marked gnus-marked-assoc)))
12389       (setq gnus-newsrc-assoc nil)
12390       (while newsrc
12391         (setq group (car newsrc))
12392         (let ((info (nth 2 (gnus-gethash (car group) gnus-newsrc-hashtb))))
12393           (if info
12394               (progn
12395                 (setcar (nthcdr 2 info) (cdr (cdr group)))
12396                 (setcar (cdr info) (if (nth 1 group) 3 6))
12397                 (setq gnus-newsrc-assoc (cons info gnus-newsrc-assoc)))
12398             (setq gnus-newsrc-assoc
12399                   (cons 
12400                    (setq info
12401                          (list (car group)
12402                                (if (nth 1 group) 3 6) (cdr (cdr group))))
12403                    gnus-newsrc-assoc)))
12404           (if (setq m (assoc (car group) marked))
12405             (setcdr (cdr (cdr info)) (cons (list (cons 'tick (cdr m))) nil))))
12406         (setq newsrc (cdr newsrc)))
12407       (setq newsrc killed)
12408       (while newsrc
12409         (setcar newsrc (car (car newsrc)))
12410         (setq newsrc (cdr newsrc)))
12411       (setq gnus-killed-list killed))
12412     (setq gnus-newsrc-assoc (nreverse gnus-newsrc-assoc))
12413     (gnus-make-hashtable-from-newsrc-alist)))
12414       
12415 (defun gnus-make-newsrc-file (file)
12416   "Make server dependent file name by catenating FILE and server host name."
12417   (let* ((file (expand-file-name file nil))
12418          (real-file (concat file "-" (nth 1 gnus-select-method))))
12419     (if (file-exists-p real-file)
12420         real-file file)))
12421
12422 (defun gnus-uncompress-newsrc-assoc ()
12423   ;; Uncompress all lists of marked articles in the newsrc assoc.
12424   (let ((newsrc gnus-newsrc-assoc)
12425         marked)
12426     (while newsrc
12427       (if (not (setq marked (nth 3 (car newsrc))))
12428           ()
12429         (while marked
12430           (or (eq 'score (car (car marked)))
12431               (eq 'bookmark (car (car marked)))
12432               (setcdr (car marked) (gnus-uncompress-range (cdr (car marked)))))
12433           (setq marked (cdr marked))))
12434       (setq newsrc (cdr newsrc)))))
12435
12436 (defun gnus-compress-newsrc-assoc ()
12437   ;; Compress all lists of marked articles in the newsrc assoc.
12438   (let ((newsrc gnus-newsrc-assoc)
12439         marked)
12440     (while newsrc
12441       (if (not (setq marked (nth 3 (car newsrc))))
12442           ()
12443         (while marked
12444           (or (eq 'score (car (car marked)))
12445               (eq 'bookmark (car (car marked)))
12446               (setcdr (car marked) 
12447                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
12448           (setq marked (cdr marked))))
12449       (setq newsrc (cdr newsrc)))))
12450
12451 ;; jwz: rewrote this function to be much more efficient, and not be
12452 ;; subject to regexp overflow errors when it encounters very long
12453 ;; lines -- the old behavior was to blow off the rest of the *file*
12454 ;; when a line was encountered that was too long to match!!  Now it
12455 ;; uses only simple looking-at calls, and doesn't create as many
12456 ;; temporary strings.  It also now handles multiple consecutive
12457 ;; options lines (before it only handled the first.)
12458 ;; Tiny rewrite by lmi. 
12459 (defun gnus-newsrc-to-gnus-format ()
12460   "Parse current buffer as .newsrc file."
12461   ;; We have to re-initialize these variables (except for
12462   ;; gnus-killed-list) because the quick startup file may contain bogus
12463   ;; values.
12464   (setq gnus-newsrc-options nil)
12465   (setq gnus-newsrc-options-n-yes nil)
12466   (setq gnus-newsrc-options-n-no nil)
12467   (gnus-parse-options-lines)
12468   (gnus-parse-newsrc-body))
12469
12470 (defun gnus-parse-options-lines ()
12471   ;; newsrc.5 seems to indicate that the options line can come anywhere
12472   ;; in the file, and that there can be any number of them:
12473   ;;
12474   ;;       An  options  line  starts  with  the  word  options (left-
12475   ;;       justified).  Then there are the list of  options  just  as
12476   ;;       they would be on the readnews command line.  For instance:
12477   ;;
12478   ;;       options -n all !net.sf-lovers !mod.human-nets -r
12479   ;;       options -c -r
12480   ;;
12481   ;;       A string of lines beginning with a space or tab after  the
12482   ;;       initial  options  line  will  be  considered  continuation
12483   ;;       lines.
12484   ;;
12485   ;; For now, we only accept it at the beginning of the file.
12486
12487   (goto-char (point-min))
12488   (skip-chars-forward " \t\n")
12489   (setq gnus-newsrc-options nil)
12490   (while (looking-at "^options[ \t]*\\(.*\\)\n")
12491     ;; handle consecutive options lines
12492     (setq gnus-newsrc-options (concat gnus-newsrc-options
12493                                       (if gnus-newsrc-options "\n\t")
12494                                       (buffer-substring (match-beginning 1)
12495                                                         (match-end 1))))
12496     (forward-line 1)
12497     (while (looking-at "[ \t]+\\(.*\\)\n")
12498       ;; handle subsequent continuation lines of this options line
12499       (setq gnus-newsrc-options (concat gnus-newsrc-options "\n\t"
12500                                         (buffer-substring (match-beginning 1)
12501                                                           (match-end 1))))
12502       (forward-line 1)))
12503   ;; Gather all "-n" options lines.
12504   (let ((start 0)
12505         (result nil))
12506     (if gnus-newsrc-options
12507         (while (and (string-match "^[ \t]*-n\\([^\n]*\\)$"
12508                                   gnus-newsrc-options
12509                                   start)
12510                     (setq start (match-end 0)))
12511           (setq result (concat result
12512                                (and result " ")
12513                                (substring gnus-newsrc-options
12514                                           (match-beginning 1)
12515                                           (match-end 1))))))
12516     (let ((yes-and-no (and result (gnus-parse-n-options result))))
12517       (and (or gnus-options-subscribe (car yes-and-no))
12518            (setq gnus-newsrc-options-n-yes 
12519                  (concat (or gnus-options-subscribe "") 
12520                          (or (car yes-and-no) ""))))
12521       (and (or gnus-options-not-subscribe (cdr yes-and-no))
12522            (setq gnus-newsrc-options-n-no 
12523                  (concat (or gnus-options-not-subscribe "") 
12524                          (or (cdr yes-and-no) "")))))
12525     nil))
12526
12527 (defun gnus-parse-newsrc-body ()
12528   ;; Point has been positioned after the options lines.  We shouldn't
12529   ;; see any more in here.
12530
12531   (let ((subscribe nil)
12532         (read-list nil)
12533         (line (1+ (count-lines (point-min) (point))))
12534         newsgroup
12535         p p2)
12536     (save-restriction
12537       (skip-chars-forward " \t")
12538       (while (not (eobp))
12539         (cond
12540          ((= (following-char) ?\n)
12541           ;; skip blank lines
12542           nil)
12543          (t
12544           (setq p (point))
12545           (skip-chars-forward "^:!\n")
12546           (if (= (following-char) ?\n)
12547               (error "line %d is unparsable in %s" line (buffer-name)))
12548           (setq p2 (point))
12549           (skip-chars-backward " \t")
12550
12551           ;; #### note: we could avoid consing a string here by binding obarray
12552           ;; and reading the newsgroup directly into the gnus-newsrc-hashtb,
12553           ;; then setq'ing newsgroup to symbol-name of that, like we do in
12554           ;; gnus-active-to-gnus-format.
12555           (setq newsgroup (buffer-substring p (point)))
12556           (goto-char p2)
12557
12558           (setq subscribe (= (following-char) ?:))
12559           (setq read-list nil)
12560
12561           (forward-char 1)              ; after : or !
12562           (skip-chars-forward " \t")
12563           (while (not (= (following-char) ?\n))
12564             (skip-chars-forward " \t")
12565             (or
12566              (and (cond
12567                    ((looking-at "\\([0-9]+\\)-\\([0-9]+\\)") ; a range
12568                     (setq read-list
12569                           (cons
12570                            (cons
12571                             (progn
12572                               ;; faster that buffer-substring/string-to-int
12573                               (narrow-to-region (point-min) (match-end 1))
12574                               (read (current-buffer)))
12575                             (progn
12576                               (narrow-to-region (point-min) (match-end 2))
12577                               (forward-char) ; skip over "-"
12578                               (prog1
12579                                   (read (current-buffer))
12580                                 (widen))))
12581                            read-list))
12582                     t)
12583                    ((looking-at "[0-9]+")
12584                     ;; faster that buffer-substring/string-to-int
12585                     (narrow-to-region (point-min) (match-end 0))
12586                     (setq p (read (current-buffer)))
12587                     (widen)
12588                     (setq read-list (cons (cons p p) read-list))
12589                     t)
12590                    (t
12591                     ;; bogus chars in ranges
12592                     nil))
12593                   (progn
12594                     (goto-char (match-end 0))
12595                     (skip-chars-forward " \t")
12596                     (cond ((= (following-char) ?,)
12597                            (forward-char 1)
12598                            t)
12599                           ((= (following-char) ?\n)
12600                            t)
12601                           (t
12602                            ;; bogus char after range
12603                            nil))))
12604              ;; if we get here, the parse failed
12605              (progn
12606                (end-of-line)            ; give up on this line
12607                (ding)
12608                (message "Ignoring bogus line %d for %s in %s"
12609                         line newsgroup (buffer-name))
12610                (sleep-for 1))))
12611           ;; We have already read .newsrc.eld, so we gently update the
12612           ;; data in the hash table with the information we have just
12613           ;; read. 
12614           (let ((info (nth 2 (gnus-gethash newsgroup gnus-newsrc-hashtb))))
12615             (if info
12616                 (progn
12617                   (setcar (nthcdr 2 info) (nreverse read-list))
12618                   (setcar (cdr info) (if subscribe 3 (if read-list 6 7))))
12619               (setq gnus-newsrc-assoc
12620                     (cons (list newsgroup (if subscribe 3 (if read-list 6 7))
12621                                 (nreverse read-list))
12622                           gnus-newsrc-assoc))))))
12623         (setq line (1+ line))
12624         (forward-line 1))))
12625   (setq gnus-newsrc-assoc (cdr gnus-newsrc-assoc))
12626   (gnus-make-hashtable-from-newsrc-alist)
12627   nil)
12628
12629 (defun gnus-parse-n-options (options)
12630   "Parse -n NEWSGROUPS options and return a cons of YES and NO regexps."
12631   (let ((yes nil)
12632         (no nil)
12633         (yes-or-no nil)                 ;`!' or not.
12634         (newsgroup nil))
12635     ;; Parse each newsgroup description such as "comp.all".  Commas
12636     ;; and white spaces can be a newsgroup separator.
12637     (while
12638         (string-match "^[ \t\n,]*\\(!?\\)\\([^- \t\n,][^ \t\n,]*\\)" options)
12639       (setq yes-or-no
12640             (substring options (match-beginning 1) (match-end 1)))
12641       (setq newsgroup
12642             (regexp-quote
12643              (substring options
12644                         (match-beginning 2) (match-end 2))))
12645       (setq options (substring options (match-end 2)))
12646       ;; Rewrite "all" to ".+" not ".*".  ".+" requires at least one
12647       ;; character.
12648       (while (string-match "\\(^\\|\\\\[.]\\)all\\(\\\\[.]\\|$\\)" newsgroup)
12649         (setq newsgroup
12650               (concat (substring newsgroup 0 (match-end 1))
12651                       ".+"
12652                       (substring newsgroup (match-beginning 2)))))
12653       ;; It is yes or no.
12654       (cond ((string-equal yes-or-no "!")
12655              (setq no (cons newsgroup no)))
12656             ((string-equal newsgroup ".+")) ;Ignore `all'.
12657             (t
12658              (setq yes (cons newsgroup yes)))))
12659     ;; Make a cons of regexps from parsing result.
12660     ;; We have to append \(\.\|$\) to prevent matching substring of
12661     ;; newsgroup.  For example, "jp.net" should not match with
12662     ;; "jp.network".
12663     ;; Fixes for large regexp problems are from yonezu@nak.math.keio.ac.jp.
12664     (cons (if yes
12665               (concat "^\\("
12666                       (apply (function concat)
12667                              (mapcar
12668                               (lambda (newsgroup)
12669                                 (concat newsgroup "\\|"))
12670                               (cdr yes)))
12671                       (car yes) "\\)\\(\\.\\|$\\)"))
12672           (if no
12673               (concat "^\\("
12674                       (apply (function concat)
12675                              (mapcar
12676                               (lambda (newsgroup)
12677                                 (concat newsgroup "\\|"))
12678                               (cdr no)))
12679                       (car no) "\\)\\(\\.\\|$\\)")))))
12680
12681 (defun gnus-save-newsrc-file ()
12682   "Save .newsrc file."
12683   ;; Note: We cannot save .newsrc file if all newsgroups are removed
12684   ;; from the variable gnus-newsrc-assoc.
12685   (and (or gnus-newsrc-assoc gnus-killed-list)
12686        gnus-current-startup-file
12687        (save-excursion
12688          (if (or (not gnus-dribble-buffer)
12689                  (not (buffer-name gnus-dribble-buffer))
12690                  (zerop (save-excursion
12691                           (set-buffer gnus-dribble-buffer)
12692                           (buffer-size))))
12693              (message "(No changes need to be saved)")
12694            (if gnus-save-newsrc-file
12695                (let ((make-backup-files t)
12696                      (version-control nil)
12697                      (require-final-newline t)) ;Don't ask even if requested.
12698                  (message "Saving %s..." gnus-current-startup-file)
12699                  ;; Make backup file of master newsrc.
12700                  ;; You can stop or change version control of backup file.
12701                  ;; Suggested by jason@violet.berkeley.edu.
12702                  (run-hooks 'gnus-save-newsrc-hook)
12703                  (gnus-gnus-to-newsrc-format)
12704                  (message "Saving %s... done" gnus-current-startup-file)))
12705            ;; Quickly loadable .newsrc.
12706            (set-buffer (get-buffer-create " *Gnus-newsrc*"))
12707            (gnus-add-current-to-buffer-list)
12708            (buffer-disable-undo (current-buffer))
12709            (erase-buffer)
12710            (message "Saving %s.eld..." gnus-current-startup-file)
12711            (gnus-gnus-to-quick-newsrc-format)
12712            (let ((make-backup-files nil)
12713                  (version-control nil)
12714                  (require-final-newline t)) ;Don't ask even if requested.
12715              (write-region 1 (point-max) 
12716                            (concat gnus-current-startup-file ".eld") 
12717                            nil 'nomesg))
12718            (kill-buffer (current-buffer))
12719            (message "Saving %s.eld... done" gnus-current-startup-file)
12720            (gnus-dribble-delete-file)))))
12721
12722 (defun gnus-gnus-to-quick-newsrc-format ()
12723   "Insert Gnus variables such as gnus-newsrc-assoc in lisp format."
12724   (insert ";; (ding) Gnus startup file.\n")
12725   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
12726   (insert ";; to read .newsrc.\n")
12727   (let ((variables gnus-variable-list)
12728         (gnus-newsrc-assoc (cdr gnus-newsrc-assoc))
12729         variable)
12730     ;; insert lisp expressions.
12731     (gnus-compress-newsrc-assoc)
12732     (while variables
12733       (setq variable (car variables))
12734       (and (boundp variable)
12735            (symbol-value variable)
12736            (or gnus-save-killed-list (not (eq variable 'gnus-killed-list)))
12737            (insert "(setq " (symbol-name variable) " '"
12738                    (prin1-to-string (symbol-value variable))
12739                    ")\n"))
12740       (setq variables (cdr variables)))
12741     (gnus-uncompress-newsrc-assoc)))
12742
12743
12744 (defun gnus-gnus-to-newsrc-format ()
12745   ;; Generate and save the .newsrc file.
12746   (let ((newsrc (cdr gnus-newsrc-assoc))
12747         info ranges range)
12748     (save-excursion
12749       (set-buffer (create-file-buffer gnus-startup-file))
12750       (buffer-disable-undo (current-buffer))
12751       (erase-buffer)
12752       ;; Write options.
12753       (if gnus-newsrc-options (insert "options " gnus-newsrc-options "\n"))
12754       ;; Write subscribed and unsubscribed.
12755       (while newsrc
12756         (setq info (car newsrc))
12757         (if (not (nth 4 info))          ;Don't write foreign groups to .newsrc.
12758             (progn
12759               (insert (car info) (if (>= (nth 1 info) 6) "!" ":"))
12760               (if (setq ranges (nth 2 info))
12761                   (progn
12762                     (insert " ")
12763                     (if (not (listp (cdr ranges)))
12764                         (if (= (car ranges) (cdr ranges))
12765                             (insert (int-to-string (car ranges)))
12766                           (insert (int-to-string (car ranges)) "-" 
12767                                   (int-to-string (cdr ranges))))
12768                       (while ranges
12769                         (setq range (car ranges)
12770                               ranges (cdr ranges))
12771                         (if (or (atom range) (= (car range) (cdr range)))
12772                             (insert (int-to-string 
12773                                      (or (and (atom range) range) 
12774                                          (car range))))
12775                           (insert (int-to-string (car range)) "-"
12776                                   (int-to-string (cdr range))))
12777                         (if ranges (insert ","))))))
12778               (insert "\n")))
12779         (setq newsrc (cdr newsrc)))
12780       (write-region 1 (point-max) gnus-current-startup-file nil 'nomesg)
12781       (kill-buffer (current-buffer)))))
12782
12783 (defun gnus-read-descriptions-file ()
12784   (message "Reading descriptions file...")
12785   (if (not (gnus-request-list-newsgroups gnus-select-method))
12786       (progn
12787         (message "Couldn't read newsgroups descriptions")
12788         nil)
12789     (let (group)
12790       (setq gnus-description-hashtb 
12791             (gnus-make-hashtable (length gnus-active-hashtb)))
12792       (save-excursion
12793         (save-restriction
12794           (set-buffer nntp-server-buffer)
12795           (goto-char (point-min))
12796           (delete-non-matching-lines "^[-\\._+A-Za-z0-9]+[ \t]")
12797           (goto-char (point-min))
12798           (if (or (search-forward "\n.\n" nil t)
12799                   (goto-char (point-max)))
12800               (progn
12801                 (beginning-of-line)
12802                 (narrow-to-region (point-min) (point))))
12803           (goto-char (point-min))
12804           (while (not (eobp))
12805             (setq group (let ((obarray gnus-description-hashtb))
12806                           (read (current-buffer))))
12807             (skip-chars-forward " \t")
12808             (set group (buffer-substring 
12809                         (point) (save-excursion (end-of-line) (point))))
12810             (forward-line 1))))
12811       (message "Reading descriptions file...done")
12812       t)))
12813
12814 (defun gnus-group-get-description (group)
12815   ;; Get the description of a group by sending XGTITLE to the server.
12816   (and (gnus-request-group-description group)
12817        (save-excursion
12818          (set-buffer nntp-server-buffer)
12819          (goto-char (point-min))
12820          (and (looking-at "[^ \t]+[ \t]+\\(.*\\)")
12821               (buffer-substring (match-beginning 1) (match-end 1))))))
12822
12823 (provide 'gnus)
12824
12825 ;;; gnus.el ends here