*** 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   "*Default method for selecting a newsgroup.
52 This variable should be a list, where the first element is how the
53 news is to be fetched, the second is the address.  An optional third
54 element can be included to specify a port number if nntp is used.
55
56 For instance, if you want to get your news via NNTP from
57 \"flab.flab.edu\" on port 23, you could say:
58
59 (setq gnus-select-method '(nntp \"flab.flab.edu\" 23))
60
61 If you want to use your local spool, say:
62
63 (setq gnus-select-method (list 'nnspool (system-name)))
64
65 If you use this variable, you must set `gnus-nntp-server' to nil.")
66
67 ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
68 (defvar gnus-post-method nil
69   "*Preferred method for posting USENET news.
70 If this variable is nil, Gnus will use the current method to decide
71 which method to use when posting.  If it is non-nil, it will override
72 the current method. This method will not be used in mail groups and
73 the like, only in \"real\" newsgroups.
74
75 The value must be a valid method as discussed in the documentation of
76 `gnus-select-method'.")
77
78 (defvar gnus-refer-article-method nil
79   "*Preferred method for fetching an article by Message-ID.
80 If you are reading news from the local spool (with nnspool), fetching
81 articles by Message-ID is painfully slow. By setting this method to an
82 nntp method, you might get acceptable results.
83
84 The value of this variable must be a valid select method as discussed
85 in the documentation of `gnus-select-method'")
86
87 (defvar gnus-secondary-select-methods nil
88   "*A list of secondary methods that will be used for reading news.
89 This is a list where each element is a complete select methdod (see
90 `gnus-select-method').  
91
92 If, for instance, you want to read your mail with the nnml backend,
93 you could set this variable:
94
95 (setq gnus-secondary-select-methods '((nnml \"\"))")
96
97 (defvar gnus-secondary-servers nil
98   "*List of NNTP servers that the user can choose between interactively.
99 To make Gnus query you for a server, you have to give `gnus' a
100 non-numeric prefix - `C-u M-x gnus', in short.")
101
102 (defvar gnus-nntp-server nil
103   "*The name of the host running the NNTP server.
104 This variable is semi-obsolete. Use the `gnus-select-method'
105 variable instead.")
106
107 (defvar gnus-nntp-service "nntp"
108   "*NNTP service name (\"nntp\" or 119).
109 This is an obsolete variable, which is scarcely used. If you use an
110 nntp server for your newsgroup and want to change the port number
111 used to 899, you would say something along these lines:
112
113  (setq gnus-select-method '(nntp \"my.nntp.server\" 899))")
114
115 (defvar gnus-startup-file "~/.newsrc"
116   "*Your `.newsrc' file.
117 `.newsrc-SERVER' will be used instead if that exists.")
118
119 (defvar gnus-signature-file "~/.signature"
120   "*Your signature file.
121 If the variable is a string that doesn't correspond to a file, the
122 string itself is inserted.")
123
124 (defvar gnus-signature-function nil
125   "*A function that should return a signature file name.
126 The function will be called with the name of the newsgroup being
127 posted to.
128 If the function returns a string that doesn't correspond to a file, the
129 string itself is inserted.
130 If the function returns nil, the `gnus-signature-file' variable will
131 be used instead.")
132
133 (defvar gnus-init-file "~/.gnus"
134   "*Your Gnus elisp startup file.
135 If a file with the .el or .elc suffixes exist, it will be read
136 instead.") 
137
138 (defvar gnus-group-faq-directory
139   "/anonymous@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-group-archive-directory
145   "/ftp@sina.tcamc.uh.edu:/pub/emacs/ding-list/" 
146   "*The address of the (ding) archives.")
147
148 (defvar gnus-default-subscribed-newsgroups nil
149   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
150 It should be a list of strings.
151 If it is `t', Gnus will not do anything special the first time it is
152 started; it'll just use the normal newsgroups subscription methods.")
153
154 (defvar gnus-post-prepare-function nil
155   "*Function that is run after a post buffer has been prepared.
156 It is called with the name of the newsgroup that is posted to. It
157 might be used, for instance, for inserting signatures based on the
158 newsgroup name. (In that case, `gnus-signature-file' and
159 `mail-signature' should both be set to nil).")
160
161 (defvar gnus-use-cross-reference t
162   "*Non-nil means that cross referenced articles will be marked as read.
163 If nil, ignore cross references.  If t, mark articles as read in
164 subscribed newsgroups. If neither t nor nil, mark as read in all
165 newsgroups.") 
166
167 (defvar gnus-use-dribble-file t
168   "*Non-nil means that Gnus will use a dribble file to store user updates.
169 If Emacs should crash without saving the .newsrc files, complete
170 information can be restored from the dribble file.")
171
172 (defvar gnus-use-followup-to 'use
173   "*Specifies what to do with Followup-To header.
174 If nil, ignore the header. If it is t, use its value, but ignore 
175 `poster'. If it is neither nil nor t, which is the default, always use
176 the value.") 
177
178 (defvar gnus-followup-to-function nil
179   "*A variable that contains a function that returns a followup address.
180 The function will be called in the buffer of the article that is being
181 followed up. The buffer will be narrowed to the headers of the
182 article. To pick header headers, one might use `mail-fetch-field'.  The
183 function will be called with the name of the current newsgroup as the
184 argument.
185
186 Here's an example `gnus-followup-to-function':
187
188 (setq gnus-followup-to-function
189       (lambda (group)
190         (cond ((string= group \"mail.list\")
191                (or (mail-fetch-field \"sender\") 
192                    (mail-fetch-field \"from\")))
193               (t
194                (or (mail-fetch-field \"reply-to\") 
195                    (mail-fetch-field \"from\"))))))")
196
197 (defvar gnus-reply-to-function nil
198   "*A variable that contains a function that returns a reply address.
199 See the `gnus-followup-to-function' variable for an explanation of how
200 this variable is used.
201
202 This function should return a string that will be used to fill in the
203 header.  This function may also return a list.  In that case, every
204 list element should be a cons where the first car should be a string
205 with the header name, and the cdr should be a string with the header
206 value.")
207
208 (defvar gnus-large-newsgroup 200
209   "*The number of articles which indicates a large newsgroup.
210 If the number of articles in a newsgroup is greater than this value,
211 confirmation is required for selecting the newsgroup.")
212
213 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
214 (defvar gnus-no-groups-message "No news is horrible news"
215   "*Message displayed by Gnus when no groups are available.")
216
217 (defvar gnus-author-copy (getenv "AUTHORCOPY")
218   "*Save outgoing articles in this file.
219 Initialized from the AUTHORCOPY environment variable.
220
221 If this variable begins with the character \"|\", outgoing articles
222 will be piped to the named program. It is possible to save an article
223 in an MH folder as follows:
224
225 \(setq gnus-author-copy \"|/usr/local/lib/mh/rcvstore +Article\")
226
227 If the first character is not a pipe, articles are saved using the
228 function specified by the `gnus-author-copy-saver' variable.")
229
230 (defvar gnus-mail-self-blind nil
231   "*Non-nil means insert a BCC header in all outgoing articles.
232 This will result in having a copy of the article mailed to yourself.
233 The BCC header is inserted when the post buffer is initialized, so you
234 can remove or alter the BCC header to override the default.")
235
236 (defvar gnus-author-copy-saver (function rmail-output)
237   "*A function called to save outgoing articles.
238 This function will be called with the same of the file to store the
239 article in. The default function is `rmail-output' which saves in Unix
240 mailbox format.")
241
242 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
243   "*Non-nil means that the default name of a file to save articles in is the group name.
244 If it's nil, the directory form of the group name is used instead.
245
246 If this variable is a list, and the list contains the element
247 `not-score', long file names will not be used for score files; if it
248 contains the element `not-save', long file names will not be used for
249 saving; and if it contains the element `not-kill', long file names
250 will not be used for kill files.")
251
252 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
253   "*Name of the directory articles will be saved in (default \"~/News\").
254 Initialized from the SAVEDIR environment variable.")
255
256 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
257   "*Name of the directory where kill files will be stored (default \"~/News\").
258 Initialized from the SAVEDIR environment variable.")
259
260 (defvar gnus-score-expiry-days 7
261   "*Number of days before unused score file entries are expired.")
262
263 (defvar gnus-default-article-saver (function gnus-summary-save-in-rmail)
264   "*A function to save articles in your favorite format.
265 The function must be interactively callable (in other words, it must
266 be an Emacs command).
267
268 Gnus provides the following functions:
269
270 * gnus-summary-save-in-rmail (Rmail format)
271 * gnus-summary-save-in-mail (Unix mail format)
272 * gnus-summary-save-in-folder (MH folder)
273 * gnus-summary-save-in-file (article format).")
274
275 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
276   "*A function generating a file name to save articles in Rmail format.
277 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
278
279 (defvar gnus-mail-save-name (function gnus-plain-save-name)
280   "*A function generating a file name to save articles in Unix mail format.
281 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
282
283 (defvar gnus-folder-save-name (function gnus-folder-save-name)
284   "*A function generating a file name to save articles in MH folder.
285 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
286
287 (defvar gnus-file-save-name (function gnus-numeric-save-name)
288   "*A function generating a file name to save articles in article format.
289 The function is called with NEWSGROUP, HEADERS, and optional
290 LAST-FILE.")
291
292 (defvar gnus-split-methods nil
293   "*Variable used to suggest where articles are to be saved.
294 The syntax of this variable is the same as `nnmail-split-methods'.  
295
296 For instance, if you would like to save articles related to Gnus in
297 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
298 you could set this variable to something like:
299
300  '(("^Subject:.*gnus\\|^Newsgroups:.*gnus" "gnus-stuff")
301    ("^Subject:.*vm\\|^Xref:.*vm" "vm-stuff"))")
302
303 (defvar gnus-score-file-suffix "SCORE"
304   "*Suffix of the score files.")
305
306 (defvar gnus-fetch-old-headers nil
307   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
308 If an unread article in the group refers to an older, already read (or
309 just marked as read) article, the old article will not normally be
310 displayed in the Summary buffer.  If this variable is non-nil, Gnus
311 will attempt to grab the headers to the old articles, and thereby
312 build complete threads.  If it has the value `some', only enough
313 headers to connect otherwise loose threads will be displayed.
314
315 The server has to support XOVER for any of this to work.")
316
317 (defvar gnus-visual t
318   "*If non-nil, will do various highlighting.
319 If nil, no mouse highlights (or any other highlights) will be
320 performed.  This might speed up Gnus some when generating large group
321 and summary buffers.")
322
323 (defvar gnus-novice-user t
324   "*Non-nil means that you are a usenet novice.
325 If non-nil, verbose messages may be displayed and confirmations may be
326 required.")
327
328 (defvar gnus-expert-user nil
329   "*Non-nil means that you will never be asked for confirmation about anything.
330 And that means *anything*.")
331
332 (defvar gnus-keep-same-level nil
333   "*Non-nil means that the next newsgroup after the current will be on the same level.
334 When you type, for instance, `n' after reading the last article in the
335 current newsgroup, you will go to the next newsgroup. If this variable
336 is nil, the next newsgroup will be the next from the group
337 buffer. 
338 If this variable is non-nil, Gnus will either put you in the
339 next newsgroup with the same level, or, if no such newsgroup is
340 available, the next newsgroup with the lowest possible level higher
341 than the current level.
342 If this variable is `best', Gnus will make the next newsgroup the one
343 with the best level.")
344
345 (defvar gnus-summary-make-false-root 'adopt
346   "*nil means that Gnus won't gather loose threads.
347 If the root of a thread has expired or been read in a previous
348 session, the information necessary to build a complete thread has been
349 lost. Instead of having many small sub-threads from this original thread
350 scattered all over the summary buffer, Gnus can gather them. 
351
352 If non-nil, Gnus will try to gather all loose sub-threads from an
353 original thread into one large thread.
354
355 If this variable is non-nil, it should be one of `none', `adopt',
356 `dummy' or `empty'.
357
358 If this variable is `none', Gnus will not make a false root, but just
359 present the sub-threads after another.
360 If this variable is `dummy', Gnus will create a dummy root that will
361 have all the sub-threads as children.
362 If this variable is `adopt', Gnus will make one of the \"children\"
363 the parent and mark all the step-children as such.
364 If this variable is `empty', the \"children\" are printed with empty
365 subject fields.  (Or rather, they will be printed with a string
366 given by the `gnus-summary-same-subject' variable.)")
367
368 (defvar gnus-summary-gather-subject-limit nil
369   "*Maximum length of subject comparisons when gathering loose threads.
370 Use nil to compare full subjects.  Setting this variable to a low
371 number will help gather threads that have been corrupted by
372 newsreaders chopping off subject lines, but it might also mean that
373 unrelated articles that have subject that happen to begin with the
374 same few characters will be incorrectly gathered.
375
376 If this variable is `fuzzy', Gnus will use a fuzzy algortihm when
377 comparing subjects.")
378
379 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
380 (defvar gnus-summary-same-subject ""
381   "*String indicating that the current article has the same subject as the previous.
382 This variable will only be used if the value of
383 `gnus-summary-make-false-root' is `empty'.")
384
385 (defvar gnus-summary-goto-unread nil
386   "*If non-nil, marking commands will go to the next unread article.")
387
388 (defvar gnus-check-new-newsgroups t
389   "*Non-nil means that Gnus will add new newsgroups at startup.
390 If this variable is `ask-server', Gnus will ask the server for new
391 groups since the last time it checked. This means that the killed list
392 is no longer necessary, so you could set `gnus-save-killed-list' to
393 nil. 
394
395 A variant is to have this variable be a list of select methods. Gnus
396 will then use the `ask-server' method on all these select methods to
397 query for new groups from all those servers.
398
399 Eg.
400   (setq gnus-check-new-newsgroups 
401         '((nntp \"some.server\") (nntp \"other.server\")))
402
403 If this variable is nil, then you have to tell Gnus explicitly to
404 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
405
406 (defvar gnus-check-bogus-newsgroups nil
407   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
408 If this variable is nil, then you have to tell Gnus explicitly to
409 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
410
411 (defvar gnus-read-active-file t
412   "*Non-nil means that Gnus will read the entire active file at startup.
413 If this variable is nil, Gnus will only know about the groups in your
414 `.newsrc' file.
415
416 If you set this variable to nil, you probably still want to be told
417 about new newsgroups that arrive.  To do that, set
418 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
419 properly with all servers.") 
420
421 (defvar gnus-activate-foreign-newsgroups nil
422   "*If nil, Gnus will not check foreign newsgroups at startup.
423 If it is non-nil, it should be a number between one and nine. Foreign
424 newsgroups that have a level lower or equal to this number will be
425 activated on startup. For instance, if you want to active all
426 subscribed newsgroups, but not the rest, you'd set this variable to 5.
427
428 If you subscribe to lots of newsgroups from different servers, startup
429 might take a while. By setting this variable to nil, you'll save time,
430 but you won't be told how many unread articles there are in the
431 groups.")
432
433 (defvar gnus-save-newsrc-file t
434   "*Non-nil means that Gnus will save the `.newsrc' file.
435 Gnus always saves its own startup file, which is called
436 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
437 be readily understood by other newsreaders.  If you don't plan on
438 using other newsreaders, set this variable to nil to save some time on
439 exit.")
440
441 (defvar gnus-save-killed-list t
442   "*If non-nil, save the list of killed groups to the startup file.
443 This will save both time (when starting and quitting) and space (both
444 memory and disk), but it will also mean that Gnus has no record of
445 which groups are new and which are old, so the automatic new
446 newsgroups subscription methods become meaningless. You should always
447 set `gnus-check-new-newsgroups' to `ask-server' or nil if you set this
448 variable to nil.")
449
450 (defvar gnus-interactive-catchup t
451   "*If non-nil, require your confirmation when catching up a group.")
452
453 (defvar gnus-interactive-post t
454   "*If non-nil, group name will be asked for when posting.")
455
456 (defvar gnus-interactive-exit t
457   "*If non-nil, require your confirmation when exiting Gnus.")
458
459 (defvar gnus-kill-killed nil
460   "*If non-nil, Gnus will apply kill files to already killed articles.
461 If it is nil, Gnus will never apply kill files to articles that have
462 already been through the scoring process, which might very well save lots
463 of time.")
464
465 (defvar gnus-extract-address-components 'gnus-extract-address-components
466   "*Function for extracting address components from a From header.
467 Two pre-defined function exist: `gnus-extract-address-components',
468 which is the default, quite fast, and too simplistic solution, and
469 `mail-extract-address-components', which works much better, but is
470 slower.")
471
472 (defvar gnus-score-interactive-default-score 1000
473   "*Scoring commands will raise/lower the score with this number as the default.")
474
475 (defvar gnus-save-score nil
476   "*If non-nil, save group scoring info.")
477
478 (defvar gnus-global-score-files nil
479   "*List of global score files and directories.
480 Set this variable if you want to use people's score files.  One entry
481 for each score file or each score file directory.  Gnus will decide
482 by itself what score files are applicable to which group.
483
484 Say you want to use the single score file
485 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
486 score files in the \"/ftp.some-where:/pub/score\" directory.
487
488  (setq gnus-global-score-files
489        '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
490          \"/ftp.some-where:/pub/score\"))")
491
492 (defvar gnus-summary-default-score 0
493   "*Default article score level.
494 If this variable is nil, scoring will be disabled.")
495
496 (defvar gnus-group-default-list-level 5
497   "*Default listing level.")
498
499 (defvar gnus-group-always-list-unread 5
500   "*Always list groups less than this variable with unread articles. 
501 If nil, use parameter to specify.")
502
503 (defvar gnus-user-login-name nil
504   "*The login name of the user.
505 Got from the function `user-login-name' if undefined.")
506
507 (defvar gnus-user-full-name nil
508   "*The full name of the user.
509 Got from the NAME environment variable if undefined.")
510
511 (defvar gnus-user-from-line nil
512   "*Your full, complete e-mail address.  
513 Overrides the other Gnus variables if it is non-nil.
514
515 Here are two example values of this variable:
516
517  \"Lars Magne Ingebrigtsen <larsi@ifi.uio.no>\"
518
519 and
520
521  \"larsi@ifi.uio.no (Lars Magne Ingebrigtsen)\"
522
523 The first version is recommended, but the name has to be quoted if it
524 contains non-alphanumerical characters.")
525
526 (defvar gnus-show-mime nil
527   "*If non-ni, do mime processing of articles.
528 The articles will simply be fed to the function given by
529 `gnus-show-mime-method'.")
530
531 (defvar gnus-show-mime-method (function metamail-buffer)
532   "*Function to process a MIME message.
533 The function is called from the article buffer.")
534
535 (defvar gnus-show-threads t
536   "*If non-nil, display threads in summary mode.")
537
538 (defvar gnus-thread-hide-subtree nil
539   "*If non-nil, hide all threads initially.
540 If threads are hidden, you have to run the command
541 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
542 to expose hidden threads.")
543
544 (defvar gnus-thread-hide-killed t
545   "*If non-nil, hide killed threads automatically.")
546
547 (defvar gnus-thread-ignore-subject nil
548   "*If non-nil, ignore subjects and do all threading based on the Reference header.
549 If nil, which is the default, articles that have different subjects
550 from their parents will start separate threads.")
551
552 (defvar gnus-thread-indent-level 4
553   "*Number that says how much each sub-thread should be indented.")
554
555 ;; jwz: nuke newsgroups whose name is all digits - that means that
556 ;; some loser has let articles get into the root of the news spool,
557 ;; which is toxic. Lines beginning with whitespace also tend to be
558 ;; toxic.
559 (defvar gnus-ignored-newsgroups
560   (purecopy (mapconcat 'identity
561                        '("^to\\."       ; not "real" groups
562                          "^[0-9. \t]+ " ; all digits in name
563                          "[][\"#'()     ;\\]"   ; bogus characters
564                          )
565                        "\\|"))
566   "*A regexp to match uninteresting newsgroups in the active file.
567 Any lines in the active file matching this regular expression are
568 removed from the newsgroup list before anything else is done to it,
569 thus making them effectively non-existant.")
570
571 (defvar gnus-ignored-headers
572   "^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:"
573   "*All headers that match this regexp will be hidden.
574 Also see `gnus-visible-headers'.")
575
576 (defvar gnus-visible-headers "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:"
577   "*All headers that do not match this regexp will be hidden.
578 Also see `gnus-ignored-headers'.")
579
580 (defvar gnus-sorted-header-list
581   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:" 
582     "^Cc:" "^Date:" "^Organization:")
583   "*This variable is a list of regular expressions.
584 If it is non-nil, headers that match the regular expressions will
585 be placed first in the article buffer in the sequence specified by
586 this list.")
587
588 (defvar gnus-required-headers
589   '(From Date Newsgroups Subject Message-ID Organization Lines X-Newsreader)
590   "*Headers to be generated or prompted for when posting an article.
591 RFC977 and RFC1036 require From, Date, Newsgroups, Subject,
592 Message-ID.  Organization, Lines and X-Newsreader are optional.  If
593 you want Gnus not to insert some header, remove it from this list.")
594
595 (defvar gnus-show-all-headers nil
596   "*If non-nil, don't hide any headers.")
597
598 (defvar gnus-save-all-headers t
599   "*If non-nil, don't remove any headers before saving.")
600
601 (defvar gnus-inhibit-startup-message nil
602   "*If non-nil, the startup message will not be displayed.")
603
604 (defvar gnus-auto-extend-newsgroup t
605   "*If non-nil, extend newsgroup forward and backward when requested.")
606
607 (defvar gnus-auto-select-first t
608   "*If non-nil, select the first unread article when entering a group.
609 If you want to prevent automatic selection of the first unread article
610 in some newsgroups, set the variable to nil in
611 `gnus-select-group-hook'.") 
612
613 (defvar gnus-auto-select-next t
614   "*If non-nil, offer to go to the next group from the end of the previous.
615 If the value is t and the next newsgroup is empty, Gnus will exit
616 summary mode and go back to group mode.  If the value is neither nil
617 nor t, Gnus will select the following unread newsgroup.  In
618 particular, if the value is the symbol `quietly', the next unread
619 newsgroup will be selected without any confirmations.")
620
621 (defvar gnus-auto-select-same nil
622   "*If non-nil, select the next article with the same subject.")
623
624 (defvar gnus-auto-center-summary t
625   "*If non-nil, always center the current summary buffer.")
626
627 (defvar gnus-auto-mail-to-author nil
628   "*If non-nil, mail the authors of articles a copy of your follow-ups.
629 If this variable is `ask', the user will be prompted for whether to
630 mail a copy.  The string given by `gnus-mail-courtesy-message' will be
631 inserted at the beginning of the mail copy.
632
633 Mail is sent using the function specified by the
634 `gnus-mail-send-method' variable.")
635
636 ;; Added by Ethan Bradford <ethanb@ptolemy.astro.washington.edu>.
637 (defvar gnus-mail-courtesy-message
638   "*The following message is a courtesy copy of an article\nthat has been posted as well.\n\n"
639   "This is inserted at the start of a mailed copy of a posted message.
640 If this variable is nil, no such courtesy message will be added.")
641
642 (defvar gnus-break-pages t
643   "*If non-nil, do page breaking on articles.
644 The page delimiter is specified by the `gnus-page-delimiter'
645 variable.")
646
647 (defvar gnus-page-delimiter "^\^L"
648   "*Regexp describing what to use as article page delimiters.
649 The default value is \"^\^L\", which is a form linefeed at the
650 beginning of a line.")
651
652 (defvar gnus-use-full-window t
653   "*If non-nil, use the entire Emacs screen.")
654
655 ; for split windows. maybe a better way?
656 (defvar gnus-split-window nil
657   "*If non-nil, put the article buffer in left-hand side of the window .")
658
659 (defvar gnus-window-configuration
660   '((summary (0 1 0))
661     (newsgroups (1 0 0))
662     (article (0 3 10)))
663   "*Specify window configurations for each action.
664 The format of the variable is either a list of (ACTION (G S A)), where
665 G, S, and A are the relative height of group, summary, and article
666 windows, respectively, or a list of (ACTION FUNCTION), where FUNCTION
667 is a function that will be called with ACTION as an argument. ACTION
668 can be `summary', `newsgroups', or `article'.")
669
670 (defvar gnus-mail-reply-method (function gnus-mail-reply-using-mail)
671   "*Function to compose a reply.
672 Two pre-made functions are `gnus-mail-reply-using-mail' (sendmail) and
673 `gnus-mail-reply-using-mhe' (MH-E).")
674
675 (defvar gnus-mail-forward-method (function gnus-mail-forward-using-mail)
676   "*Function to forward the current message to another user.
677 Two pre-made functions are `gnus-mail-forward-using-mail' (sendmail)
678 and `gnus-mail-forward-using-mhe' (MH-E).") 
679
680 (defvar gnus-mail-other-window-method 'gnus-mail-other-window-using-mail
681   "*Function to compose mail in the other window.
682 Two pre-made functions are `gnus-mail-other-window-using-mail'
683 (sendmail) and `gnus-mail-other-window-using-mhe' (MH-E).")
684
685 (defvar gnus-mail-send-method send-mail-function
686   "*Function to mail a message which is also being posted as an article.
687 The message must have To or Cc header.  The default is copied from
688 the variable `send-mail-function'.")
689
690 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
691   "*Function called with a group name when new group is detected.
692 A few pre-made functions are supplied: `gnus-subscribe-randomly'
693 inserts new groups at the beginning of the list of groups;
694 `gnus-subscribe-alphabetically' inserts new groups in strict
695 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
696 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
697 for your decision.")
698
699 ;; Suggested by a bug report by Hallvard B Furuseth.
700 ;; <h.b.furuseth@usit.uio.no>. 
701 (defvar gnus-subscribe-options-newsgroup-method
702   (function gnus-subscribe-alphabetically)
703   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
704 If, for instance, you want to subscribe to all newsgroups in the
705 \"no\" and \"alt\" hierarchies, you'd put the following in your
706 .newsrc file:
707
708 options -n no.all alt.all
709
710 Gnus will the subscribe all new newsgroups in these hierarchies with
711 the subscription method in this variable.")
712
713 (defvar gnus-subscribe-hierarchical-interactive nil
714   "*If non-nil, Gnus will offer to subscribe hierarchically.
715 When a new hierarchy appears, Gnus will ask the user:
716
717 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
718
719 If the user pressed `d', Gnus will descend the hierarchy, `y' will
720 subscribe to all newsgroups in the hierarchy and `s' will skip this
721 hierarchy in its entirety.")
722
723 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
724   "*Function used for sorting the group buffer.
725 This function will be called with group info entries as the arguments
726 for the groups to be sorted.  Pre-made functions include
727 `gnus-sort-by-alphabet', `gnus-sort-by-unread' and
728 `gnus-sort-by-level'")
729
730 ;; Mark variables suggested by Thomas Michanek
731 ;; <Thomas.Michanek@telelogic.se>. 
732 (defvar gnus-unread-mark ? 
733   "*Mark used for unread articles.")
734 (defvar gnus-ticked-mark ?!
735   "*Mark used for ticked articles.")
736 (defvar gnus-dormant-mark ??
737   "*Mark used for dormant articles.")
738 (defvar gnus-dread-mark ?D
739   "*Mark used for read articles.")
740 (defvar gnus-read-mark ?d
741   "*Mark used for read articles.")
742 (defvar gnus-expirable-mark ?E
743   "*Mark used for expirable articles.")
744 (defvar gnus-killed-mark ?K
745   "*Mark used for killed articles.")
746 (defvar gnus-kill-file-mark ?X
747   "*Mark used for articles killed by kill files.")
748 (defvar gnus-low-score-mark ?Y
749   "*Mark used for articles with a low score.")
750 (defvar gnus-catchup-mark ?C
751   "*Mark used for articles that are caught up.")
752 (defvar gnus-replied-mark ?R
753   "*Mark used for articles that have been replied to.")
754 (defvar gnus-process-mark ?# 
755   "*Process mark.")
756 (defvar gnus-ancient-mark ?A
757   "*Mark used for ancient articles.")
758 (defvar gnus-canceled-mark ?G
759   "*Mark used for cancelled articles.")
760 (defvar gnus-score-over-mark ?+
761   "*Score mark used for articles with high scores.")
762 (defvar gnus-score-below-mark ?-
763   "*Score mark used for articles with low scores.")
764
765 (defvar gnus-view-pseudo-asynchronously nil
766   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
767
768 (defvar gnus-view-pseudos nil
769   "*If `automatic', pseudo-articles will be viewed automatically.
770 If `not-confirm', pseudos will be viewed automatically, and the user
771 will not be asked to confirm the command.")
772
773 (defvar gnus-group-line-format "%M%S%5y: %(%g%)\n"
774   "*Format of group lines.
775 It works along the same lines as a normal formatting string,
776 with some simple extensions.
777
778 %M    Only marked articles (character, \"*\" or \" \")
779 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
780 %L    Level of subscribedness (integer, 1-9)
781 %N    Number of unread articles (integer)
782 %I    Number of dormant articles (integer)
783 %i    Number of ticked and dormant (integer)
784 %T    Number of ticked articles (integer)
785 %R    Number of read articles (integer)
786 %t    Total number of articles (integer)
787 %y    Number of unread, unticked articles (integer)
788 %G    Group name (string)
789 %g    Qualified group name (string)
790 %D    Group description (string)
791 %s    Select method (string)
792 %o    Moderated group (char, \"m\")
793 %O    Moderated group (string, \"(m)\" or \"\")
794 %n    Select from where (string)
795 %z    A string that look like `<%s:%n>' if a foreign select method is used
796 %u    User defined specifier. The next character in the format string should
797       be a letter.  Gnus will call the function gnus-user-format-function-X,
798       where X is the letter following %u. The function will be passed the
799       current header as argument. The function should return a string, which
800       will be inserted into the buffer just like information from any other
801       group specifier.
802
803 Text between %( and %) will be highlighted with `gnus-mouse-face' when
804 the mouse point move inside the area.  There can only be one such area.
805
806 Note that this format specification is not always respected. For
807 reasons of efficiency, when listing killed groups, this specification
808 is ignored altogether. If the spec is changed considerably, your
809 output may end up looking strange when listing both alive and killed
810 groups.
811
812 If you use %o or %O, reading the active file will be slower and quite
813 a bit of extra memory will be used. %D will also worsen performance.
814 Also note that if you change the format specification to include any
815 of these specs, you must probably re-start Gnus to see them go into
816 effect.") 
817
818 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
819   "*The format specification of the lines in the summary buffer.
820
821 It works along the same lines as a normal formatting string,
822 with some simple extensions.
823
824 %N   Article number, left padded with spaces (string)
825 %S   Subject (string)
826 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
827 %n   Name of the poster (string)
828 %A   Address of the poster (string)
829 %F   Contents of the From: header (string)
830 %x   Contents of the Xref: header (string)
831 %D   Date of the article (string)
832 %d   Date of the article (string) in DD-MMM format
833 %M   Message-id of the article (string)
834 %r   References of the article (string)
835 %c   Number of characters in the article (integer)
836 %L   Number of lines in the article (integer)
837 %I   Indentation based on thread level (a string of spaces)
838 %T   A string with two possible values: 80 spaces if the article
839      is on thread level two or larger and 0 spaces on level one
840 %R   \"R\" if this article has been replied to, \" \" otherwise (character)
841 %U   Status of this article (character, \"D\", \"K\", \"-\" or \" \")
842 %[   Opening bracket (character, \"[\" or \"<\")
843 %]   Closing bracket (character, \"]\" or \">\")
844 %>   Spaces of length thread-level (string)
845 %<   Spaces of length (- 20 thread-level) (string)
846 %i   Article score (number)
847 %z   Article zcore (character)
848 %t   Number of articles under the current thread.
849 %u   User defined specifier. The next character in the format string should
850      be a letter.  Gnus will call the function gnus-user-format-function-X,
851      where X is the letter following %u. The function will be passed the
852      current header as argument. The function should return a string, which
853      will be inserted into the summary just like information from any other
854      summary specifier.
855
856 Text between %( and %) will be highlighted with `gnus-mouse-face'
857 when the mouse point is placed inside the area.  There can only be one
858 such area.
859
860 The %U (status), %R (replied) and %z (zcore) specs have to be handled
861 with care. For reasons of efficiency, Gnus will compute what column
862 these characters will end up in, and \"hard-code\" that. This means that
863 it is illegal to have these specs after a variable-length spec. Well,
864 you might not be arrested, but your summary buffer will look strange,
865 which is bad enough.
866
867 The smart choice is to have these specs as for to the left as
868 possible. 
869
870 This restriction may disappear in later versions of Gnus.")
871
872 (defvar gnus-summary-dummy-line-format "*   :                          : %S\n"
873   "*The format specification for the dummy roots in the summary buffer.
874 It works along the same lines as a normal formatting string,
875 with some simple extensions.
876
877 %S  The subject")
878
879 (defvar gnus-summary-mode-line-format "(ding) %G/%A %Z"
880   "*The format specification for the summary mode line.")
881
882 (defvar gnus-article-mode-line-format "(ding) %G/%A %S"
883   "*The format specification for the article mode line.")
884
885 (defvar gnus-group-mode-line-format "(ding) List of groups   {%M:%S}  "
886   "*The format specification for the group mode line.")
887
888 (defvar gnus-valid-select-methods
889   '(("nntp" post address prompt-address)
890     ("nnspool" post)
891     ("nnvirtual" none virtual prompt-address address) 
892     ("nnmbox" mail respool) 
893     ("nnml" mail respool)
894     ("nnmh" mail respool) 
895     ("nndir" none prompt-address address)
896     ("nndigest" none) 
897     ("nndoc" none prompt-address) 
898     ("nnbabyl" mail respool) 
899     ("nnkiboze" none virtual) 
900     ("nnfolder" mail respool))
901   "An alist of valid select methods.
902 The first element of each list lists should be a string with the name
903 of the select method. The other elements may be be the category of
904 this method (ie. `post', `mail', `none' or whatever) or other
905 properties that this method has (like being respoolable).
906 If you implement a new select method, all you should have to change is
907 this variable. I think.")
908
909 (defvar gnus-updated-mode-lines '(group article summary)
910   "*List of buffers that should update their mode lines.
911 The list may contain the symbols `group', `article' and `summary'. If
912 the corresponding symbol is present, Gnus will keep that mode line
913 updated with information that may be pertinent. 
914 If this variable is nil, screen refresh may be quicker.")
915
916 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
917 (defvar gnus-mode-non-string-length 21
918   "*Max length of mode-line non-string contents.
919 If this is nil, Gnus will take space as is needed, leaving the rest
920 of the modeline intact.")
921
922 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
923 (defvar gnus-mouse-face 'highlight
924   "*Face used for mouse highlighting in Gnus.
925 No mouse highlights will be done if `gnus-visual' is nil.")
926
927 (defvar gnus-summary-mark-below nil
928   "*Mark all articles with a score below this variable as read.
929 This variable is local to each summary buffer and usually set by the
930 score file.")  
931
932 (defvar gnus-orphan-score nil
933   "*All orphans get this score added. Set in the score file.")
934
935 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
936   "*List of functions used for sorting threads in the summary buffer.
937 By default, threads are sorted by article number.
938
939 Each function takes two threads and return non-nil if the first thread
940 should be sorted before the other.  If you use more than one function,
941 the primary sort function should be the last.
942
943 Ready-mady functions include `gnus-thread-sort-by-number',
944 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
945 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
946 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
947
948 The latter two only work on threads that have been scored prior to
949 entering the newsgroup.")
950
951 (defvar gnus-thread-score-function '+
952   "*Function used for calculating the total score of a thread.
953
954 The function is called with the scores of the article and each
955 subthread and should then return the score of the thread.
956
957 Some functions you can use are `+', `max', or `min'.")
958
959 (defvar gnus-score-find-score-files-function 'gnus-score-find-bnews
960   "*Function used to find SCORE files.
961 The function will be called with the group name as the argument, and
962 should return a list of score files to apply to that group.  The score
963 files do not actually have to exist.
964
965 Predefined values are:
966
967 gnus-score-find-single: Only apply the group's own SCORE file.
968 gnus-score-find-hierarchical: Also apply SCORE files from parent groups.
969 gnus-score-find-bnews: Apply SCORE files whose names matches.
970
971 See the documentation to these functions for more information.
972
973 This variable can also be a list of functions to be called.  Each
974 function should either return a list of score files, or a list of
975 score alists.")
976
977 (defvar gnus-options-subscribe nil
978   "*All new groups matching this regexp will be subscribed unconditionally.
979 Note that this variable deals only with new newsgroups.  This variable
980 does not affect old newsgroups.")
981
982 (defvar gnus-options-not-subscribe nil
983   "*All new groups matching this regexp will be ignored.
984 Note that this variable deals only with new newsgroups.  This variable
985 does not affect old (already subscribed) newsgroups.")
986
987 (defvar gnus-auto-expirable-newsgroups nil
988   "*Groups in which to automatically mark read articles as expirable.
989 If non-nil, this should be a regexp that should match all groups in
990 which to perform auto-expiry.  This only makes sense for mail groups.")
991
992
993 ;; Hooks.
994
995 (defvar gnus-group-mode-hook nil
996   "*A hook for Gnus group mode.")
997
998 (defvar gnus-summary-mode-hook nil
999   "*A hook for Gnus summary mode.")
1000
1001 (defvar gnus-article-mode-hook nil
1002   "*A hook for Gnus article mode.")
1003
1004 (defvar gnus-open-server-hook nil
1005   "*A hook called just before opening connection to the news server.")
1006
1007 (defvar gnus-startup-hook nil
1008   "*A hook called at startup.
1009 This hook is called after Gnus is connected to the NNTP server.")
1010
1011 (defvar gnus-get-new-news-hook nil
1012   "*A hook run just before Gnus checks for new news.")
1013
1014 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1015   "*A function that is called to generate the group buffer.
1016 The function is called with three arguments: The first is a number;
1017 all group with a level less or equal to that number should be listed,
1018 if the second is non-nil, empty groups should also be displayed. If
1019 the third is non-nil, it is a number. No groups with a level lower
1020 than this number should be displayed.
1021
1022 The only current function implemented is `gnus-group-prepare-flat'.")
1023
1024 (defvar gnus-group-prepare-hook nil
1025   "*A hook called after the group buffer has been generated.
1026 If you want to modify the group buffer, you can use this hook.")
1027
1028 (defvar gnus-summary-prepare-hook nil
1029   "*A hook called after the summary buffer has been generated.
1030 If you want to modify the summary buffer, you can use this hook.")
1031
1032 (defvar gnus-article-prepare-hook nil
1033   "*A hook called after an article has been prepared in the article buffer.
1034 If you want to run a special decoding program like nkf, use this hook.")
1035
1036 (defvar gnus-article-display-hook nil
1037   "*A hook called after the article is displayed in the article buffer.
1038 The hook is designed to change the contents of the article
1039 buffer. Typical functions that this hook may contain are
1040 `gnus-article-hide-headers' (hide selected headers),
1041 `gnus-article-hide-signature' (hide signature) and
1042 `gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1043 (add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1044 (add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1045
1046 (defconst gnus-article-x-face-command "uncompface | ikon2xbm | xv -"
1047   "String or function to be executed to display an X-Face header.
1048 If it is a string, the command will be executed in a sub-shell
1049 asynchronously. The compressed face will be piped to this command.") 
1050
1051 (defvar gnus-select-group-hook nil
1052   "*A hook called when a newsgroup is selected.
1053
1054 If you'd like to simplify subjects like the
1055 `gnus-summary-next-same-subject' command does, you can use the
1056 following hook:
1057
1058  (setq gnus-select-group-hook
1059       (list
1060         (lambda ()
1061           (mapcar (lambda (header)
1062                      (header-set-subject
1063                       header
1064                       (gnus-simplify-subject
1065                        (header-subject header) 're-only)))
1066                   gnus-newsgroup-headers))))")
1067
1068 (defvar gnus-select-article-hook
1069   '(gnus-summary-show-thread)
1070   "*A hook called when an article is selected.
1071 The default hook shows conversation thread subtrees of the selected
1072 article automatically using `gnus-summary-show-thread'.")
1073
1074 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1075   "*A hook called to apply kill files to a group.
1076 This hook is intended to apply a kill file to the selected newsgroup.
1077 The function `gnus-apply-kill-file' is called by default.
1078
1079 Since a general kill file is too heavy to use only for a few
1080 newsgroups, I recommend you to use a lighter hook function. For
1081 example, if you'd like to apply a kill file to articles which contains
1082 a string `rmgroup' in subject in newsgroup `control', you can use the
1083 following hook:
1084
1085 \(setq gnus-apply-kill-hook
1086       (list
1087         (lambda ()
1088           (cond ((string-match \"control\" gnus-newsgroup-name)
1089                  (gnus-kill \"Subject\" \"rmgroup\")
1090                  (gnus-expunge \"X\"))))))")
1091
1092 (defvar gnus-visual-mark-article-hook 
1093   (list 'gnus-visual-highlight-selected-summary)
1094   "*Hook run after selecting an article in the summary buffer.
1095 It is meant to be used for highlighting the article in some way.  It
1096 is not run if `gnus-visual' is nil.")
1097
1098 (defvar gnus-prepare-article-hook (list 'gnus-inews-insert-signature)
1099   "*A hook called after preparing body, but before preparing header headers.
1100 The default hook (`gnus-inews-insert-signature') inserts a signature
1101 file specified by the variable `gnus-signature-file'.")
1102
1103 (defvar gnus-inews-article-hook (list 'gnus-inews-do-fcc)
1104   "*A hook called before finally posting an article.
1105 The default hook (`gnus-inews-do-fcc') does FCC processing (ie. saves
1106 the article to a file).")
1107
1108 (defvar gnus-inews-article-header-hook nil
1109   "*A hook called after inserting the headers in an article to be posted.
1110 The hook is called from the *post-news* buffer, narrowed to the
1111 headers.")
1112
1113 (defvar gnus-exit-group-hook nil
1114   "*A hook called when exiting (not quitting) summary mode.")
1115
1116 (defvar gnus-suspend-gnus-hook nil
1117   "*A hook called when suspending (not exiting) Gnus.")
1118
1119 (defvar gnus-exit-gnus-hook nil
1120   "*A hook called when exiting Gnus.")
1121
1122 (defvar gnus-save-newsrc-hook nil
1123   "*A hook called when saving the newsrc file.")
1124
1125 (defvar gnus-visual-summary-update-hook 
1126   (list 'gnus-visual-summary-highlight-line)
1127   "*A hook called when a summary line is changed.
1128 The hook will not be called if `gnus-visual' is nil.
1129
1130 The default function `gnus-visual-summary-highlight-line' will
1131 highlight the line according to the `gnus-visual-summary-highlight'
1132 variable.")
1133
1134 (defvar gnus-mark-article-hook (list 'gnus-summary-mark-unread-as-read)
1135   "*A hook called when an article is selected for the first time.
1136 The hook is intended to mark an article as read (or unread)
1137 automatically when it is selected.")
1138
1139 ;; Site dependent variables. These variables should be defined in
1140 ;; paths.el.
1141
1142 (defvar gnus-default-nntp-server nil
1143   "*Specify a default NNTP server.
1144 This variable should be defined in paths.el, and should never be set
1145 by the user.
1146 If you want to change servers, you should use `gnus-select-method'.
1147 See the documentation to that variable.")
1148
1149 (defconst gnus-backup-default-subscribed-newsgroups 
1150   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
1151   "Default default new newsgroups the first time Gnus is run.
1152 Should be set in paths.el, and shouldn't be touched by the user.")
1153
1154 (defvar gnus-local-domain nil
1155   "*Local domain name without a host name.
1156 The DOMAINNAME environment variable is used instead if it is defined.
1157 If the `system-name' function returns the full Internet name, there is
1158 no need to set this variable.")
1159
1160 (defvar gnus-local-organization nil
1161   "*String with a description of what organization (if any) the user belongs to.
1162 The ORGANIZATION environment variable is used instead if it is defined.
1163 If this variable contains a function, this function will be called
1164 with the current newsgroup name as the argument. The function should
1165 return a string.
1166 In any case, if the string (either in the variable, in the environment
1167 variable, or returned by the function) is a file name, the contents of
1168 this file will be used as the organization.")
1169
1170 (defvar gnus-use-generic-from nil
1171   "*If nil, the full host name will be the system name prepended to the domain name.
1172 If this is a string, the full host name will be this string.
1173 If this is non-nil, non-string, the domain name will be used as the
1174 full host name.")
1175
1176 (defvar gnus-use-generic-path nil
1177   "*If nil, use the NNTP server name in the Path header.
1178 If stringp, use this; if non-nil, use no host name (user name only).")
1179
1180 \f
1181 ;; Internal variables
1182
1183 ;; Avoid highlighting in kill files.
1184 (defvar gnus-summary-inhibit-highlight nil)
1185 (defvar gnus-newsgroup-selected-overlay nil)
1186
1187 (defvar gnus-article-mode-map nil)
1188 (defvar caesar-translate-table nil)
1189 (defvar gnus-dribble-buffer nil)
1190 (defvar gnus-headers-retrieved-by nil)
1191 (defvar gnus-article-reply nil)
1192 (defvar gnus-override-method nil)
1193 (defvar gnus-article-check-size nil)
1194 (defvar gnus-score-file-list nil)
1195 (defvar gnus-internal-global-score-files nil)
1196 (defvar gnus-current-score-file nil)
1197
1198 (defvar gnus-current-move-group nil)
1199
1200 (defvar gnus-score-alist nil
1201   "Alist containing score information.
1202 The keys can be symbols or strings.  The following symbols are defined. 
1203
1204 touched: If this alist has been modified.
1205 mark:    Automatically mark articles below this.
1206 expunge: Automatically expunge articles below this.
1207 files:   List of other SCORE files to load when loading this one.
1208 eval:    Sexp to be evaluated when the score file is loaded.
1209
1210 String entries have the form (HEADER (MATCH TYPE SCORE DATE) ...) 
1211 where HEADER is the header being scored, MATCH is the string we are
1212 looking for, TYPE is a flag indicating whether it should use regexp or
1213 substring matching, SCORE is the score to add and DATE is the date
1214 of the last succesful match.")
1215
1216 (defvar gnus-score-cache nil)
1217 (defvar gnus-scores-articles nil)
1218 (defvar gnus-scores-exclude-files nil)
1219 (defvar gnus-header-index nil)
1220 (defvar gnus-score-index nil)
1221
1222 (defvar gnus-newsgroup-dependencies nil)
1223 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1224 (defvar gnus-default-subscribe-level 2)
1225 (defvar gnus-default-unsubscribe-level 6)
1226 (defvar gnus-default-kill-level 9)
1227
1228 (defconst gnus-group-line-format-alist
1229   (list (list ?M 'marked ?c)
1230         (list ?S 'subscribed ?c)
1231         (list ?L 'level ?d)
1232         (list ?N 'number ?s)
1233         (list ?I 'number-of-dormant ?d)
1234         (list ?T 'number-of-ticked ?d)
1235         (list ?R 'number-of-read ?s)
1236         (list ?t 'number-total ?d)
1237         (list ?y 'number-of-unread-unticked ?s)
1238         (list ?i 'number-of-ticked-and-dormant ?d)
1239         (list ?g 'group ?s)
1240         (list ?G 'qualified-group ?s)
1241         (list ?D 'newsgroup-description ?s)
1242         (list ?o 'moderated ?c)
1243         (list ?O 'moderated-string ?s)
1244         (list ?s 'news-server ?s)
1245         (list ?n 'news-method ?s)
1246         (list ?z 'news-method-string ?s)
1247         (list ?u 'user-defined ?s)))
1248
1249 (defconst gnus-summary-line-format-alist 
1250   (list (list ?N 'number ?s)
1251         (list ?S 'subject ?s)
1252         (list ?s 'subject-or-nil ?s)
1253         (list ?n 'name ?s)
1254         (list ?A 'address ?s)
1255         (list ?F 'from ?s)
1256         (list ?x (macroexpand '(header-xref header)) ?s)
1257         (list ?D (macroexpand '(header-date header)) ?s)
1258         (list ?d '(gnus-dd-mmm (header-date header)) ?s)
1259         (list ?M (macroexpand '(header-id header)) ?s)
1260         (list ?r (macroexpand '(header-references header)) ?s)
1261         (list ?c (macroexpand '(header-chars header)) ?d)
1262         (list ?L 'lines ?d)
1263         (list ?I 'indentation ?s)
1264         (list ?T '(if (< level 1) "" (make-string (frame-width) ? )) ?s)
1265         (list ?R 'replied ?c)
1266         (list ?\[ 'opening-bracket ?c)
1267         (list ?\] 'closing-bracket ?c)
1268         (list ?\> '(make-string level ? ) ?s)
1269         (list ?\< '(make-string (max 0 (- 20 level)) ? ) ?s)
1270         (list ?i 'score ?s)
1271         (list ?z 'score-char ?c)
1272         (list ?U 'unread ?c)
1273         (list ?t '(gnus-summary-number-of-articles-in-thread thread) ?d)
1274         (list ?u 'user-defined ?s))
1275   "An alist of format specifications that can appear in summary lines,
1276 and what variables they correspond with, along with the type of the
1277 variable (string, integer, character, etc).")
1278
1279 (defconst gnus-summary-dummy-line-format-alist
1280   (list (list ?S 'subject ?s)
1281         (list ?N 'number ?d)
1282         (list ?u 'user-defined ?s)))
1283
1284 (defconst gnus-summary-mode-line-format-alist 
1285   (list (list ?G 'group-name ?s)
1286         (list ?g '(gnus-short-group-name group-name) ?s)
1287         (list ?A 'article-number ?d)
1288         (list ?Z 'unread-and-unselected ?s)
1289         (list ?V 'gnus-version ?s)
1290         (list ?U 'unread ?d)
1291         (list ?S 'subject ?s)
1292         (list ?e 'unselected ?d)
1293         (list ?u 'user-defined ?s)
1294         (list ?s '(gnus-current-score-file-nondirectory)?s)))
1295
1296 (defconst gnus-group-mode-line-format-alist 
1297   (list (list ?S 'news-server ?s)
1298         (list ?M 'news-method ?s)
1299         (list ?u 'user-defined ?s)))
1300
1301 (defvar gnus-have-read-active-file nil)
1302
1303 (defconst gnus-maintainer "Lars Magne Ingebrigtsen <larsi@ifi.uio.no>"
1304   "The mail address of the Gnus maintainer.")
1305
1306 (defconst gnus-version "(ding) Gnus v0.53"
1307   "Version number for this version of Gnus.")
1308
1309 (defvar gnus-info-nodes
1310   '((gnus-group-mode            "(gnus)The Group Buffer")
1311     (gnus-summary-mode          "(gnus)The Summary Buffer")
1312     (gnus-article-mode          "(gnus)The Article Buffer"))
1313   "Assoc list of major modes and related Info nodes.")
1314
1315 (defvar gnus-documentation-group-file "~/dgnus/lisp/doc.txt"
1316   "The location of the (ding) Gnus documentation group.")
1317
1318 (defvar gnus-group-buffer "*Group*")
1319 (defvar gnus-summary-buffer "*Summary*")
1320 (defvar gnus-article-buffer "*Article*")
1321 (defvar gnus-server-buffer "*Server*")
1322
1323 (defvar gnus-buffer-list nil
1324   "Gnus buffers that should be killed on exit.")
1325
1326 (defvar gnus-server-alist nil
1327   "List of available servers.")
1328
1329 (defvar gnus-variable-list
1330   '(gnus-newsrc-options gnus-newsrc-options-n
1331     gnus-newsrc-last-checked-date 
1332     gnus-newsrc-assoc gnus-server-alist
1333     gnus-killed-list gnus-zombie-list)
1334   "Gnus variables saved in the quick startup file.")
1335
1336 (defvar gnus-overload-functions
1337   '((news-inews gnus-inews-news "rnewspost")
1338     (caesar-region gnus-caesar-region "rnews"))
1339   "Functions overloaded by gnus.
1340 It is a list of `(original overload &optional file)'.")
1341
1342 (defvar gnus-newsrc-options nil
1343   "Options line in the .newsrc file.")
1344
1345 (defvar gnus-newsrc-options-n nil
1346   "List of regexps representing groups to be subscribed/ignored unconditionally.") 
1347
1348 (defvar gnus-newsrc-last-checked-date nil
1349   "Date Gnus last asked server for new newsgroups.")
1350
1351 (defvar gnus-newsrc-assoc nil
1352   "Assoc list of read articles.
1353 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1354
1355 (defvar gnus-newsrc-hashtb nil
1356   "Hashtable of gnus-newsrc-assoc.")
1357
1358 (defvar gnus-killed-list nil
1359   "List of killed newsgroups.")
1360
1361 (defvar gnus-killed-hashtb nil
1362   "Hash table equivalent of gnus-killed-list.")
1363
1364 (defvar gnus-zombie-list nil
1365   "List of almost dead newsgroups.")
1366
1367 (defvar gnus-description-hashtb nil
1368   "Descriptions of newsgroups.")
1369
1370 (defvar gnus-list-of-killed-groups nil
1371   "List of newsgroups that have recently been killed by the user.")
1372
1373 (defvar gnus-active-hashtb nil
1374   "Hashtable of active articles.")
1375
1376 (defvar gnus-moderated-list nil
1377   "List of moderated newsgroups.")
1378
1379 (defvar gnus-current-startup-file nil
1380   "Startup file for the current host.")
1381
1382 (defvar gnus-last-search-regexp nil
1383   "Default regexp for article search command.")
1384
1385 (defvar gnus-last-shell-command nil
1386   "Default shell command on article.")
1387
1388 (defvar gnus-current-select-method nil
1389   "The current method for selecting a newsgroup.")
1390
1391 (defvar gnus-have-all-newsgroups nil)
1392
1393 (defvar gnus-article-internal-prepare-hook nil)
1394
1395 (defvar gnus-newsgroup-name nil)
1396 (defvar gnus-newsgroup-begin nil)
1397 (defvar gnus-newsgroup-end nil)
1398 (defvar gnus-newsgroup-last-rmail nil)
1399 (defvar gnus-newsgroup-last-mail nil)
1400 (defvar gnus-newsgroup-last-folder nil)
1401 (defvar gnus-newsgroup-last-file nil)
1402 (defvar gnus-newsgroup-auto-expire nil)
1403 (defvar gnus-newsgroup-active nil)
1404
1405 (defvar gnus-newsgroup-unreads nil
1406   "List of unread articles in the current newsgroup.")
1407
1408 (defvar gnus-newsgroup-unselected nil
1409   "List of unselected unread articles in the current newsgroup.")
1410
1411 (defvar gnus-newsgroup-marked nil
1412   "List of ticked articles in the current newsgroup (a subset of unread art).")
1413
1414 (defvar gnus-newsgroup-killed nil
1415   "List of ranges of articles that have been through the scoring process.")
1416
1417 (defvar gnus-newsgroup-kill-headers nil)
1418
1419 (defvar gnus-newsgroup-replied nil
1420   "List of articles that have been replied to in the current newsgroup.")
1421
1422 (defvar gnus-newsgroup-expirable nil
1423   "List of articles in the current newsgroup that can be expired.")
1424
1425 (defvar gnus-newsgroup-processable nil
1426   "List of articles in the current newsgroup that can be processed.")
1427
1428 (defvar gnus-newsgroup-bookmarks nil
1429   "List of articles in the current newsgroup that have bookmarks.")
1430
1431 (defvar gnus-newsgroup-dormant nil
1432   "List of dormant articles in the current newsgroup.")
1433
1434 (defvar gnus-newsgroup-scored nil
1435   "List of scored articles in the current newsgroup.")
1436
1437 (defvar gnus-newsgroup-headers nil
1438   "List of article headers in the current newsgroup.")
1439 (defvar gnus-newsgroup-headers-hashtb-by-number nil)
1440
1441 (defvar gnus-newsgroup-ancient nil
1442   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1443
1444 (defvar gnus-current-article nil)
1445 (defvar gnus-article-current nil)
1446 (defvar gnus-current-headers nil)
1447 (defvar gnus-have-all-headers nil)
1448 (defvar gnus-last-article nil)
1449 (defvar gnus-newsgroup-history nil)
1450 (defvar gnus-current-kill-article nil)
1451
1452 ;; Save window configuration.
1453 (defvar gnus-winconf-edit-group nil)
1454 (defvar gnus-winconf-edit-score nil)
1455
1456 ;; Format specs
1457 (defvar gnus-summary-line-format-spec nil)
1458 (defvar gnus-summary-dummy-line-format-spec nil)
1459 (defvar gnus-group-line-format-spec nil)
1460 (defvar gnus-summary-mode-line-format-spec nil)
1461 (defvar gnus-article-mode-line-format-spec nil)
1462 (defvar gnus-group-mode-line-format-spec nil)
1463 (defvar gnus-summary-mark-positions nil)
1464
1465 (defvar gnus-summary-expunge-below nil)
1466 (defvar gnus-reffed-article-number nil)
1467
1468 (defvar rmail-default-file (expand-file-name "~/XMBOX"))
1469 (defvar rmail-default-rmail-file (expand-file-name "~/XNEWS"))
1470
1471 (defconst gnus-summary-local-variables 
1472   '(gnus-newsgroup-name 
1473     gnus-newsgroup-begin gnus-newsgroup-end 
1474     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail 
1475     gnus-newsgroup-last-folder gnus-newsgroup-last-file 
1476     gnus-newsgroup-auto-expire gnus-newsgroup-unreads 
1477     gnus-newsgroup-unselected gnus-newsgroup-marked
1478     gnus-newsgroup-replied gnus-newsgroup-expirable
1479     gnus-newsgroup-processable gnus-newsgroup-killed
1480     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1481     gnus-newsgroup-headers gnus-newsgroup-headers-hashtb-by-number
1482     gnus-current-article gnus-current-headers gnus-have-all-headers
1483     gnus-last-article gnus-article-internal-prepare-hook
1484     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1485     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1486     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below 
1487     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1488     gnus-newsgroup-history gnus-newsgroup-ancient)
1489   "Variables that are buffer-local to the summary buffers.")
1490
1491 ;;; End of variables.
1492
1493 ;; Define some autoload functions Gnus might use.
1494 (eval-and-compile
1495   (autoload 'metamail-buffer "metamail")
1496   (autoload 'Info-goto-node "info")
1497   
1498   (autoload 'timezone-make-date-arpa-standard "timezone")
1499   (autoload 'timezone-fix-time "timezone")
1500   (autoload 'timezone-make-sortable-date "timezone")
1501   (autoload 'timezone-make-time-string "timezone")
1502   
1503   (autoload 'rmail-output "rmailout")
1504   (autoload 'mail-position-on-field "sendmail")
1505   (autoload 'mail-setup "sendmail")
1506   (autoload 'news-mail-other-window "rnewspost")
1507   (autoload 'news-reply-yank-original "rnewspost")
1508
1509   (autoload 'gnus-mail-reply-using-mhe "gnus-mh")
1510   (autoload 'gnus-mail-forward-using-mhe "gnus-mh")
1511   (autoload 'gnus-mail-other-window-using-mhe "gnus-mh")
1512   (autoload 'gnus-summary-save-in-folder "gnus-mh")
1513   (autoload 'gnus-Folder-save-name "gnus-mh")
1514   (autoload 'gnus-folder-save-name "gnus-mh")
1515   
1516   (autoload 'gnus-group-make-menu-bar "gnus-visual")
1517   (autoload 'gnus-summary-make-menu-bar "gnus-visual")
1518   (autoload 'gnus-article-make-menu-bar "gnus-visual")
1519   (autoload 'gnus-visual-highlight-selected-summary "gnus-visual")
1520   (autoload 'gnus-visual-summary-highlight-line "gnus-visual")
1521
1522   (autoload 'gnus-uu-mark-by-regexp "gnus-uu")
1523   (autoload 'gnus-uu-mark-region "gnus-uu")
1524   (autoload 'gnus-uu-mark-thread "gnus-uu")
1525   (autoload 'gnus-uu-mark-sparse "gnus-uu")
1526   (autoload 'gnus-uu-mark-series "gnus-uu")
1527   (autoload 'gnus-uu-mark-all "gnus-uu")
1528   (autoload 'gnus-uu-post-news "gnus-uu")
1529   (autoload 'gnus-uu-digest-and-forward "gnus-uu")
1530
1531   (autoload 'gnus-uu-decode-uu "gnus-uu")
1532   (autoload 'gnus-uu-decode-uu-and-save "gnus-uu")
1533   (autoload 'gnus-uu-decode-unshar "gnus-uu")
1534   (autoload 'gnus-uu-decode-unshar-and-save "gnus-uu")
1535   (autoload 'gnus-uu-decode-save "gnus-uu")
1536   (autoload 'gnus-uu-decode-binhex "gnus-uu")
1537   (autoload 'gnus-uu-decode-uu-view "gnus-uu")
1538   (autoload 'gnus-uu-decode-uu-and-save-view "gnus-uu")
1539   (autoload 'gnus-uu-decode-unshar-view "gnus-uu")
1540   (autoload 'gnus-uu-decode-unshar-and-save-view "gnus-uu")
1541   (autoload 'gnus-uu-decode-save-view "gnus-uu")
1542   (autoload 'gnus-uu-decode-binhex-view "gnus-uu")
1543   (autoload 'gnus-uu-decode-postscript "gnus-uu")
1544   (autoload 'gnus-uu-decode-postscript-and-save "gnus-uu")
1545   (autoload 'gnus-uu-decode-postscript-view "gnus-uu")
1546   (autoload 'gnus-uu-decode-postscript-and-save-view "gnus-uu")
1547
1548   (autoload 'gnus-kill "gnus-kill")
1549   (autoload 'gnus-apply-kill-file-internal "gnus-kill")
1550   (autoload 'gnus-kill-file-edit-file "gnus-kill")
1551   (autoload 'gnus-kill-file-raise-followups-to-author "gnus-kill")
1552   (autoload 'gnus-execute "gnus-kill")
1553
1554   (autoload 'pp "pp")
1555   (autoload 'pp-to-string "pp")
1556   (autoload 'mail-extract-address-components "mail-extr")
1557   )
1558
1559 \f
1560
1561 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1562 ;; If you want the cursor to go somewhere else, set these two
1563 ;; functions in some startup hook to whatever you want.
1564 (defalias 'gnus-summary-position-cursor 'gnus-goto-colon)
1565 (defalias 'gnus-group-position-cursor 'gnus-goto-colon)
1566
1567 ;; Cruft to make Gnus work under GNU XEmacs.
1568 (defvar gnus-xemacs nil
1569   "Non-nil if Gnus is running under GNU XEmacs.")
1570
1571 (if (not (string-match "XEmacs\\|Lucid" emacs-version))
1572     ()
1573   (setq gnus-xemacs t)
1574   (eval
1575    '((or (memq 'underline (list-faces))
1576          (make-face 'underline))
1577      (or (face-differs-from-default-p 'underline)
1578          (set-face-underline-p 'underline t))
1579      
1580      (defun set-text-properties (start end props &optional buffer)
1581        (if props
1582            (put-text-property start end (car props) (cadr props) buffer)
1583          (remove-text-properties start end ()))))))
1584
1585
1586 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
1587   "Pop to BUFFER, evaluate FORMS, and then returns to original window."
1588   (` (let ((GnusStartBufferWindow (selected-window)))
1589        (unwind-protect
1590            (progn
1591              (pop-to-buffer (, buffer))
1592              (,@ forms))
1593          (select-window GnusStartBufferWindow)))))
1594
1595 (defun gnus-make-hashtable (&optional hashsize)
1596   "Make a hash table (default and minimum size is 255).
1597 Optional argument HASHSIZE specifies the table size."
1598   (make-vector (if hashsize 
1599                    (max (gnus-create-hash-size hashsize) 255)
1600                  255) 0))
1601
1602 (defmacro gnus-gethash (string hashtable)
1603   "Get hash value of STRING in HASHTABLE."
1604   ;;(` (symbol-value (abbrev-symbol (, string) (, hashtable))))
1605   ;;(` (abbrev-expansion (, string) (, hashtable)))
1606   (` (symbol-value (intern-soft (, string) (, hashtable)))))
1607
1608 (defmacro gnus-sethash (string value hashtable)
1609   "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
1610   ;; We cannot use define-abbrev since it only accepts string as value.
1611                                         ;  (set (intern string hashtable) value))
1612   (` (set (intern (, string) (, hashtable)) (, value))))
1613
1614 (defsubst gnus-buffer-substring (beg end)
1615   (buffer-substring (match-beginning beg) (match-end end)))
1616
1617 (defsubst gnus-simplify-subject-re (subject)
1618   "Remove \"Re:\" from subject lines."
1619   (let ((case-fold-search t))
1620     (if (string-match "^re: *" subject)
1621         (substring subject (match-end 0))
1622       subject)))
1623
1624 (defsubst gnus-goto-char (point)
1625   (and point (goto-char point)))
1626
1627 (defsubst gnus-point-at-bol ()
1628   "Return point at the beginning of line."
1629   (let ((p (point)))
1630     (beginning-of-line)
1631     (prog1
1632         (point)
1633       (goto-char p))))
1634
1635 (defsubst gnus-point-at-eol ()
1636   "Return point at the beginning of line."
1637   (let ((p (point)))
1638     (end-of-line)
1639     (prog1
1640         (point)
1641       (goto-char p))))
1642
1643 \f
1644 ;;;
1645 ;;; Gnus Utility Functions
1646 ;;;
1647
1648 (defun gnus-extract-address-components (from)
1649   (let (name address)
1650     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
1651         (setq address (substring from (match-beginning 0) (match-end 0))))
1652     (and address
1653          (string-match (concat "<" (regexp-quote address) ">") from)
1654          (setq name (substring from 0 (1- (match-beginning 0)))))
1655     (or name
1656         (and (string-match "(.+)" from)
1657              (setq name (substring from (1+ (match-beginning 0)) 
1658                                    (1- (match-end 0))))))
1659     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1660     (list (or name from) (or address from))))
1661
1662 (defun gnus-fetch-field (field)
1663   "Return the value of the header FIELD of current article."
1664   (save-excursion
1665     (save-restriction
1666       (gnus-narrow-to-headers)
1667       (mail-fetch-field field))))
1668
1669 (defun gnus-goto-colon ()
1670   (beginning-of-line)
1671   (search-forward ":" (gnus-point-at-eol) t))
1672
1673 (defun gnus-narrow-to-headers ()
1674   (widen)
1675   (save-excursion
1676     (goto-char 1)
1677     (if (search-forward "\n\n")
1678         (narrow-to-region 1 (1- (point))))))
1679
1680 ;; Get a number that is suitable for hashing; bigger than MIN
1681 (defun gnus-create-hash-size (min)
1682   (let ((i 1))
1683     (while (< i min)
1684       (setq i (* 2 i)))
1685     (1- i)))
1686
1687 (defun gnus-update-format-specifications ()
1688   (setq gnus-summary-line-format-spec 
1689         (gnus-parse-format
1690          gnus-summary-line-format gnus-summary-line-format-alist))
1691   (gnus-update-summary-mark-positions)
1692   (setq gnus-summary-dummy-line-format-spec 
1693         (gnus-parse-format gnus-summary-dummy-line-format 
1694                            gnus-summary-dummy-line-format-alist))
1695   (setq gnus-group-line-format-spec
1696         (gnus-parse-format 
1697          gnus-group-line-format 
1698          gnus-group-line-format-alist))
1699   (if (and (string-match "%D" gnus-group-line-format)
1700            (not gnus-description-hashtb)
1701            gnus-read-active-file)
1702       (gnus-read-descriptions-file))
1703   (setq gnus-summary-mode-line-format-spec 
1704         (gnus-parse-format gnus-summary-mode-line-format 
1705                            gnus-summary-mode-line-format-alist))
1706   (setq gnus-article-mode-line-format-spec 
1707         (gnus-parse-format gnus-article-mode-line-format 
1708                            gnus-summary-mode-line-format-alist))
1709   (setq gnus-group-mode-line-format-spec 
1710         (gnus-parse-format gnus-group-mode-line-format 
1711                            gnus-group-mode-line-format-alist)))
1712
1713 (defun gnus-update-summary-mark-positions ()
1714   (save-excursion
1715     (let ((gnus-replied-mark 129)
1716           (gnus-score-below-mark 130)
1717           (gnus-score-over-mark 130)
1718           (thread nil)
1719           pos)
1720       (set-buffer (get-buffer-create " *gnus work*"))
1721       (buffer-disable-undo (current-buffer))
1722       (erase-buffer)
1723       (gnus-summary-insert-line 
1724        nil [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
1725       (goto-char (point-min))
1726       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
1727                                          (- (point) 2)))))
1728       (goto-char (point-min))
1729       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
1730                                           (- (point) 2))) pos))
1731       (goto-char (point-min))
1732       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
1733                                         (- (point) 2))) pos))
1734       (setq gnus-summary-mark-positions pos)
1735       (kill-buffer (current-buffer)))))
1736
1737 (defun gnus-format-max-width (var length)
1738   (let (result)
1739     (if (> (length (setq result (eval var))) length)
1740         (format "%s" (substring result 0 length))
1741       (format "%s" result))))
1742
1743 (defun gnus-set-mouse-face (string)
1744   ;; Set mouse face property on STRING.
1745   (or gnus-xemacs
1746       (put-text-property 0 (length string) 'mouse-face gnus-mouse-face string))
1747   string)
1748
1749 (defun gnus-parse-format (format spec-alist)
1750   ;; This function parses the FORMAT string with the help of the
1751   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1752   ;; string.  If the FORMAT string contains the specifiers %( and %)
1753   ;; the text between them will have the mouse-face text property.
1754   (if (string-match "\\`\\(.*\\)%(\\(.*\\)%)\\(.*\n?\\)\\'" format)
1755       (if (and gnus-visual gnus-mouse-face)
1756           (let ((pre (substring format (match-beginning 1) (match-end 1)))
1757                 (button (substring format (match-beginning 2) (match-end 2)))
1758                 (post (substring format (match-beginning 3) (match-end 3))))
1759             (list 'concat
1760                   (gnus-parse-simple-format pre spec-alist)
1761                   (list 'gnus-set-mouse-face
1762                         (gnus-parse-simple-format button spec-alist))
1763                   (gnus-parse-simple-format post spec-alist)))
1764         (gnus-parse-simple-format
1765          (concat (substring format (match-beginning 1) (match-end 1))
1766                  (substring format (match-beginning 2) (match-end 2))
1767                  (substring format (match-beginning 3) (match-end 3)))
1768          spec-alist))
1769     (gnus-parse-simple-format format spec-alist)))
1770
1771 (defun gnus-parse-simple-format (format spec-alist)
1772   ;; This function parses the FORMAT string with the help of the
1773   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1774   ;; string. The list will consist of the symbol `format', a format
1775   ;; specification string, and a list of forms depending on the
1776   ;; SPEC-ALIST.
1777   (let ((max-width 0)
1778         spec flist fstring b newspec max-width elem beg)
1779     (save-excursion
1780       (set-buffer (get-buffer-create " *gnus work*"))
1781       (buffer-disable-undo (current-buffer))
1782       (gnus-add-current-to-buffer-list)
1783       (erase-buffer)
1784       (insert format)
1785       (goto-char 1)
1786       (while (re-search-forward "%[-0-9]*\\(,[0-9]*\\)*\\(.\\)\\(.\\)?" nil t)
1787         (setq spec (string-to-char (buffer-substring (match-beginning 2)
1788                                                      (match-end 2))))
1789         ;; First check if there are any specs that look anything like
1790         ;; "%12,12A", ie. with a "max width specification". These have
1791         ;; to be treated specially.
1792         (if (setq beg (match-beginning 1))
1793             (setq max-width 
1794                   (string-to-int 
1795                    (buffer-substring (1+ (match-beginning 1)) (match-end 1))))
1796           (setq max-width 0)
1797           (setq beg (match-beginning 2)))
1798         ;; Find the specification from `spec-alist'.
1799         (if (not (setq elem (cdr (assq spec spec-alist))))
1800             (setq elem '("*" ?s)))
1801         ;; Treat user defined format specifiers specially
1802         (and (eq (car elem) 'user-defined)
1803              (setq elem
1804                    (list 
1805                     (list (intern (concat "gnus-user-format-function-"
1806                                           (buffer-substring
1807                                            (match-beginning 3)
1808                                            (match-end 3))))
1809                           'header)
1810                     ?s))
1811              (delete-region (match-beginning 3) (match-end 3)))
1812         (if (not (zerop max-width))
1813             (let ((el (car elem)))
1814               (cond ((= (car (cdr elem)) ?c) 
1815                      (setq el (list 'char-to-string el)))
1816                     ((= (car (cdr elem)) ?d)
1817                      (numberp el) (setq el (list 'int-to-string el))))
1818               (setq flist (cons (list 'gnus-format-max-width 
1819                                       el max-width) 
1820                                 flist))
1821               (setq newspec ?s))
1822           (setq flist (cons (car elem) flist))
1823           (setq newspec (car (cdr elem))))
1824         ;; Remove the old specification (and possibly a ",12" string).
1825         (delete-region beg (match-end 2))
1826         ;; Insert the new specification.
1827         (goto-char beg)
1828         (insert newspec))
1829       (setq fstring (buffer-substring 1 (point-max))))
1830     (cons 'format (cons fstring (nreverse flist)))))
1831
1832 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
1833 (defun gnus-read-init-file ()
1834   (and gnus-init-file
1835        (or (and (file-exists-p gnus-init-file) 
1836                 ;; Don't try to load a directory.
1837                 (not (file-directory-p gnus-init-file)))
1838            (file-exists-p (concat gnus-init-file ".el"))
1839            (file-exists-p (concat gnus-init-file ".elc")))
1840        (load gnus-init-file nil t)))
1841
1842 ;; Article file names when saving.
1843
1844 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
1845   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1846 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
1847 Otherwise, it is like ~/News/news/group/num."
1848   (let ((default
1849           (expand-file-name
1850            (concat (if (gnus-use-long-file-name 'not-save)
1851                        (gnus-capitalize-newsgroup newsgroup)
1852                      (gnus-newsgroup-directory-form newsgroup))
1853                    "/" (int-to-string (header-number headers)))
1854            (or gnus-article-save-directory "~/News"))))
1855     (if (and last-file
1856              (string-equal (file-name-directory default)
1857                            (file-name-directory last-file))
1858              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1859         default
1860       (or last-file default))))
1861
1862 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
1863   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1864 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group/num.
1865 Otherwise, it is like ~/News/news/group/num."
1866   (let ((default
1867           (expand-file-name
1868            (concat (if (gnus-use-long-file-name 'not-save)
1869                        newsgroup
1870                      (gnus-newsgroup-directory-form newsgroup))
1871                    "/" (int-to-string (header-number headers)))
1872            (or gnus-article-save-directory "~/News"))))
1873     (if (and last-file
1874              (string-equal (file-name-directory default)
1875                            (file-name-directory last-file))
1876              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1877         default
1878       (or last-file default))))
1879
1880 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
1881   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1882 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group.
1883 Otherwise, it is like ~/News/news/group/news."
1884   (or last-file
1885       (expand-file-name
1886        (if (gnus-use-long-file-name 'not-save)
1887            (gnus-capitalize-newsgroup newsgroup)
1888          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1889        (or gnus-article-save-directory "~/News"))))
1890
1891 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
1892   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1893 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group.
1894 Otherwise, it is like ~/News/news/group/news."
1895   (or last-file
1896       (expand-file-name
1897        (if (gnus-use-long-file-name 'not-save)
1898            newsgroup
1899          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1900        (or gnus-article-save-directory "~/News"))))
1901
1902 ;; For subscribing new newsgroup
1903
1904 (defun gnus-subscribe-hierarchical-interactive (groups)
1905   (let ((groups (sort groups 'string<))
1906         prefixes prefix start rest ans group starts)
1907     (while groups
1908       (setq prefixes (list "^"))
1909       (while (and groups prefixes)
1910         (while (not (string-match (car prefixes) (car groups)))
1911           (setq prefixes (cdr prefixes)))
1912         (setq prefix (car prefixes))
1913         (setq start (1- (length prefix)))
1914         (if (and (string-match "[^\\.]\\." (car groups) start)
1915                  (cdr groups)
1916                  (setq prefix 
1917                        (concat "^" (substring (car groups) 0 (match-end 0))))
1918                  (string-match prefix (car (cdr groups))))
1919             (progn
1920               (setq prefixes (cons prefix prefixes))
1921               (message "Descend hierarchy %s? ([y]nsq): " 
1922                        (substring prefix 1 (1- (length prefix))))
1923               (setq ans (read-char))
1924               (cond ((= ans ?n)
1925                      (while (and groups 
1926                                  (string-match prefix 
1927                                                (setq group (car groups))))
1928                        (setq gnus-killed-list 
1929                              (cons group gnus-killed-list))
1930                        (gnus-sethash group group gnus-killed-hashtb)
1931                        (setq groups (cdr groups)))
1932                      (setq starts (cdr starts)))
1933                     ((= ans ?s)
1934                      (while (and groups 
1935                                  (string-match prefix 
1936                                                (setq group (car groups))))
1937                        (gnus-sethash group group gnus-killed-hashtb)
1938                        (gnus-subscribe-alphabetically (car groups))
1939                        (setq groups (cdr groups)))
1940                      (setq starts (cdr starts)))
1941                     ((= ans ?q)
1942                      (while groups
1943                        (setq group (car groups))
1944                        (setq gnus-killed-list (cons group gnus-killed-list))
1945                        (gnus-sethash group group gnus-killed-hashtb)
1946                        (setq groups (cdr groups))))
1947                     (t nil)))
1948           (message "Subscribe %s? ([n]yq)" (car groups))
1949           (setq ans (read-char))
1950           (setq group (car groups))
1951           (cond ((= ans ?y)
1952                  (gnus-subscribe-alphabetically (car groups))
1953                  (gnus-sethash group group gnus-killed-hashtb))
1954                 ((= ans ?q)
1955                  (while groups
1956                    (setq group (car groups))
1957                    (setq gnus-killed-list (cons group gnus-killed-list))
1958                    (gnus-sethash group group gnus-killed-hashtb)
1959                    (setq groups (cdr groups))))
1960                 (t 
1961                  (setq gnus-killed-list (cons group gnus-killed-list))
1962                  (gnus-sethash group group gnus-killed-hashtb)))
1963           (setq groups (cdr groups)))))))
1964
1965 (defun gnus-subscribe-randomly (newsgroup)
1966   "Subscribe new NEWSGROUP by making it the first newsgroup."
1967   (gnus-subscribe-newsgroup newsgroup))
1968
1969 (defun gnus-subscribe-alphabetically (newgroup)
1970   "Subscribe new NEWSGROUP and insert it in alphabetical order."
1971   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
1972   (let ((groups (cdr gnus-newsrc-assoc))
1973         before)
1974     (while (and (not before) groups)
1975       (if (string< newgroup (car (car groups)))
1976           (setq before (car (car groups)))
1977         (setq groups (cdr groups))))
1978     (gnus-subscribe-newsgroup newgroup before)))
1979
1980 (defun gnus-subscribe-hierarchically (newgroup)
1981   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
1982   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
1983   (save-excursion
1984     (set-buffer (find-file-noselect gnus-current-startup-file))
1985     (let ((groupkey newgroup)
1986           before)
1987       (while (and (not before) groupkey)
1988         (goto-char (point-min))
1989         (let ((groupkey-re
1990                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
1991           (while (and (re-search-forward groupkey-re nil t)
1992                       (progn
1993                         (setq before (buffer-substring
1994                                       (match-beginning 1) (match-end 1)))
1995                         (string< before newgroup)))))
1996         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
1997         (setq groupkey
1998               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
1999                   (substring groupkey (match-beginning 1) (match-end 1)))))
2000       (gnus-subscribe-newsgroup newgroup before))))
2001
2002 (defun gnus-subscribe-interactively (newsgroup)
2003   "Subscribe new NEWSGROUP interactively.
2004 It is inserted in hierarchical newsgroup order if subscribed. If not,
2005 it is killed."
2006   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " newsgroup))
2007       (gnus-subscribe-hierarchically newsgroup)
2008     (setq gnus-killed-list (cons newsgroup gnus-killed-list))))
2009
2010 (defun gnus-subscribe-zombies (newsgroup)
2011   "Make new NEWSGROUP a zombie group."
2012   (setq gnus-zombie-list (cons newsgroup gnus-zombie-list)))
2013
2014 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2015   "Subscribe new NEWSGROUP.
2016 If NEXT is non-nil, it is inserted before NEXT. Otherwise it is made
2017 the first newsgroup."
2018   ;; We subscribe the group by changing its level to 3.
2019   (gnus-group-change-level 
2020    newsgroup 3 9 (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2021   (message "Subscribe newsgroup: %s" newsgroup))
2022
2023 ;; For directories
2024
2025 (defun gnus-newsgroup-directory-form (newsgroup)
2026   "Make hierarchical directory name from NEWSGROUP name."
2027   (let ((newsgroup (substring newsgroup 0)) ;Copy string.
2028         (len (length newsgroup))
2029         (idx 0))
2030     ;; Replace all occurrences of `.' with `/'.
2031     (while (< idx len)
2032       (if (= (aref newsgroup idx) ?.)
2033           (aset newsgroup idx ?/))
2034       (setq idx (1+ idx)))
2035     newsgroup
2036     ))
2037
2038 (defun gnus-make-directory (dir)
2039   "Make DIRECTORY recursively."
2040   (let* ((dir (expand-file-name dir default-directory))
2041          dirs)
2042     (if (string-match "/$" dir)
2043         (setq dir (substring dir 0 (match-beginning 0))))
2044     (while (not (file-exists-p dir))
2045       (setq dirs (cons dir dirs))
2046       (string-match "/[^/]+$" dir)
2047       (setq dir (substring dir 0 (match-beginning 0))))
2048     (while dirs
2049       (make-directory (car dirs))
2050       (setq dirs (cdr dirs)))))
2051
2052 (defun gnus-capitalize-newsgroup (newsgroup)
2053   "Capitalize NEWSGROUP name."
2054   (and (not (zerop (length newsgroup)))
2055        (concat (char-to-string (upcase (aref newsgroup 0)))
2056                (substring newsgroup 1))))
2057
2058 ;; Var
2059
2060 (defun gnus-simplify-subject (subject &optional re-only)
2061   "Remove `Re:' and words in parentheses.
2062 If optional argument RE-ONLY is non-nil, strip `Re:' only."
2063   (let ((case-fold-search t))           ;Ignore case.
2064     ;; Remove `Re:' and `Re^N:'.
2065     (if (string-match "^re:[ \t]*" subject)
2066         (setq subject (substring subject (match-end 0))))
2067     ;; Remove words in parentheses from end.
2068     (or re-only
2069         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2070           (setq subject (substring subject 0 (match-beginning 0)))))
2071     ;; Return subject string.
2072     subject))
2073
2074 (defun gnus-simplify-subject-fuzzy (subject)
2075   (let ((case-fold-search t))
2076     (and (string-match "^re:[ \t]*" subject)
2077          (setq subject (substring subject (match-end 0))))
2078     (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2079       (setq subject (substring subject 0 (match-beginning 0))))
2080     (let ((beg 0)
2081           (osubject ""))
2082       (while (string-match "[ \t]+" subject beg)
2083         (setq osubject
2084               (concat osubject (substring 
2085                                 subject beg (match-beginning 0)) " ")
2086               beg (match-end 0)))
2087       (setq osubject (concat osubject (substring subject beg)))
2088       (and (string-match " \\'" osubject) 
2089            (setq osubject (substring osubject 0 (match-beginning 0))))
2090       osubject)))
2091
2092 (defun gnus-add-current-to-buffer-list ()
2093   (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list)))
2094
2095 (defun gnus-string> (s1 s2)
2096   (not (or (string< s1 s2)
2097            (string= s1 s2))))
2098
2099 ;; Functions accessing headers.
2100 ;; Functions are more convenient than macros in some cases.
2101
2102 (defun gnus-header-number (header)
2103   "Return article number in HEADER."
2104   (header-number header))
2105
2106 (defun gnus-header-subject (header)
2107   "Return subject string in HEADER."
2108   (header-subject header))
2109
2110 (defun gnus-header-from (header)
2111   "Return author string in HEADER."
2112   (header-from header))
2113
2114 (defun gnus-header-xref (header)
2115   "Return xref string in HEADER."
2116   (header-xref header))
2117
2118 (defun gnus-header-lines (header)
2119   "Return lines in HEADER."
2120   (header-lines header))
2121
2122 (defun gnus-header-date (header)
2123   "Return date in HEADER."
2124   (header-date header))
2125
2126 (defun gnus-header-id (header)
2127   "Return Id in HEADER."
2128   (header-id header))
2129
2130 (defun gnus-header-references (header)
2131   "Return references in HEADER."
2132   (header-references header))
2133
2134 (defun gnus-clear-system ()
2135   "Clear all variables and buffers."
2136   ;; Clear Gnus variables.
2137   (let ((variables gnus-variable-list))
2138     (while variables
2139       (set (car variables) nil)
2140       (setq variables (cdr variables))))
2141   ;; Clear other internal variables.
2142   (setq gnus-list-of-killed-groups nil
2143         gnus-have-read-active-file nil
2144         gnus-newsrc-assoc nil
2145         gnus-newsrc-hashtb nil
2146         gnus-killed-list nil
2147         gnus-zombie-list nil
2148         gnus-killed-hashtb nil
2149         gnus-active-hashtb nil
2150         gnus-moderated-list nil
2151         gnus-description-hashtb nil
2152         gnus-newsgroup-headers nil
2153         gnus-score-cache nil
2154         gnus-newsgroup-headers-hashtb-by-number nil
2155         gnus-newsgroup-name nil
2156         gnus-internal-global-score-files nil
2157         gnus-server-alist nil
2158         gnus-current-select-method nil)
2159   ;; Kill the startup file.
2160   (and gnus-current-startup-file
2161        (get-file-buffer gnus-current-startup-file)
2162        (kill-buffer (get-file-buffer gnus-current-startup-file)))
2163   (gnus-dribble-clear)
2164   ;; Kill global KILL file buffer.
2165   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
2166       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
2167   (and (buffer-name nntp-server-buffer)
2168        (kill-buffer nntp-server-buffer))
2169   ;; Kill Gnus buffers.
2170   (while gnus-buffer-list
2171     (if (and (get-buffer (car gnus-buffer-list))
2172              (buffer-name (get-buffer (car gnus-buffer-list))))
2173         (kill-buffer (car gnus-buffer-list)))
2174     (setq gnus-buffer-list (cdr gnus-buffer-list))))
2175
2176 (defun gnus-configure-windows (action &optional force)
2177   "Configure Gnus windows according to the next ACTION.
2178 The ACTION is either a symbol, such as `summary', or a
2179 configuration list such as `(1 1 2)'.  If ACTION is not a list,
2180 configuration list is got from the variable gnus-window-configuration.
2181 If FORCE is non-nil, the updating will be done whether it is necessary
2182 or not."
2183   (let* ((windows
2184           (if (listp action) action 
2185             (if (listp gnus-window-configuration)
2186                 (car (cdr (assq action gnus-window-configuration)))
2187               gnus-window-configuration)))
2188          (grpwin (get-buffer-window gnus-group-buffer))
2189          (subwin (get-buffer-window gnus-summary-buffer))
2190          (artwin (get-buffer-window gnus-article-buffer))
2191          (winsum nil)
2192          (height nil)
2193          (grpheight 0)
2194          (subheight 0)
2195          (artheight 0)
2196          ;; Make split-window-vertically leave focus in upper window.
2197          (split-window-keep-point t))
2198     (if (and (symbolp windows) (fboundp windows))
2199         (funcall windows action)
2200       (if (and (not force)
2201                (or (null windows)               ;No configuration is specified.
2202                    (and (eq (null grpwin)
2203                             (zerop (nth 0 windows)))
2204                         (eq (null subwin)
2205                             (zerop (nth 1 windows)))
2206                         (eq (null artwin)
2207                             (zerop (nth 2 windows))))))
2208           ;; No need to change window configuration.
2209           nil
2210         (select-window (or grpwin subwin artwin (selected-window)))
2211         ;; First of all, compute the height of each window.
2212         (cond (gnus-use-full-window
2213                ;; Take up the entire screen.
2214                (delete-other-windows)
2215                (setq height (window-height (selected-window))))
2216               (t
2217                (setq height (+ (if grpwin (window-height grpwin) 0)
2218                                (if subwin (window-height subwin) 0)
2219                                (if artwin (window-height artwin) 0)))))
2220         ;; The group buffer exits always. So, use it to extend the
2221         ;; group window so as to get enough window space.
2222         (switch-to-buffer gnus-group-buffer 'norecord)
2223         (and (get-buffer gnus-summary-buffer)
2224              (delete-windows-on gnus-summary-buffer))
2225         (and (get-buffer gnus-article-buffer)
2226              (delete-windows-on gnus-article-buffer))
2227         ;; Compute expected window height.
2228         (setq winsum (apply (function +) windows))
2229         (if (not (zerop (nth 0 windows)))
2230             (setq grpheight (max window-min-height
2231                                  (/ (* height (nth 0 windows)) winsum))))
2232         (if (not (zerop (nth 1 windows)))
2233             (setq subheight (max window-min-height
2234                                  (/ (* height (nth 1 windows)) winsum))))
2235         (if (not (zerop (nth 2 windows)))
2236             (if gnus-split-window ;hack by erik
2237                 (setq artheight height)
2238               (setq artheight (max window-min-height
2239                                    (/ (* height (nth 2 windows)) winsum)))))
2240         (setq height (+ grpheight subheight artheight))
2241         (enlarge-window (max 0 (- height (window-height (selected-window)))))
2242         ;; Then split the window.
2243         (if (and (not (zerop artheight))
2244                  (or (not (zerop grpheight))
2245                      (not (zerop subheight))))
2246             (if gnus-split-window
2247                 (split-window-horizontally)
2248               (split-window-vertically (+ grpheight subheight))))
2249         (and (not (zerop grpheight))
2250              (not (zerop subheight))
2251              (split-window-vertically grpheight))
2252         ;; Then select buffers in each window.
2253         (or (zerop grpheight)
2254             (progn
2255               (switch-to-buffer gnus-group-buffer 'norecord)
2256               (other-window 1)))
2257         (or (zerop subheight)
2258             (progn
2259               (switch-to-buffer gnus-summary-buffer 'norecord)
2260               (other-window 1)))
2261         (or (zerop artheight)
2262             (progn
2263               ;; If article buffer does not exist, it will be created
2264               ;; and initialized.
2265               (gnus-article-setup-buffer)
2266               (switch-to-buffer gnus-article-buffer 'norecord)
2267               (setq buffer-read-only t) ; !!! Why!?! 
2268               (bury-buffer gnus-summary-buffer)
2269               (bury-buffer gnus-group-buffer)))
2270         (or (zerop subheight)
2271             (progn
2272               (pop-to-buffer gnus-summary-buffer)
2273               ;; It seems that some code in this function will set
2274               ;; buffer-read-only to nil. I have absolutely no idea
2275               ;; why. 
2276               (setq buffer-read-only t))))))) ; !!! Why!?! 
2277
2278 (defun gnus-window-configuration-split (action)
2279   (switch-to-buffer gnus-group-buffer t)
2280   (delete-other-windows)
2281   (split-window-horizontally)
2282   (cond ((or (eq action 'newsgoups) (eq action 'summary))
2283          (if (and (get-buffer gnus-summary-buffer)
2284                   (buffer-name gnus-summary-buffer))
2285              (switch-to-buffer-other-window gnus-summary-buffer)))
2286         ((eq action 'article)
2287          (switch-to-buffer gnus-summary-buffer t)
2288          (other-window 1)
2289          (gnus-article-setup-buffer)
2290          (switch-to-buffer gnus-article-buffer t))))
2291
2292 (defun gnus-version ()
2293   "Version numbers of this version of Gnus."
2294   (interactive)
2295   (let ((methods gnus-valid-select-methods)
2296         (mess gnus-version)
2297         meth)
2298     ;; Go through all the legal select methods and add their version
2299     ;; numbers to the total version string. Only the backends that are
2300     ;; currently in use will have their message numbers taken into
2301     ;; consideration. 
2302     (while methods
2303       (setq meth (intern (concat (car (car methods)) "-version")))
2304       (and (boundp meth)
2305            (stringp (symbol-value meth))
2306            (setq mess (concat mess "; " (symbol-value meth))))
2307       (setq methods (cdr methods)))
2308     (message mess)))
2309
2310 (defun gnus-info-find-node ()
2311   "Find Info documentation of Gnus."
2312   (interactive)
2313   ;; Enlarge info window if needed.
2314   (cond ((eq major-mode 'gnus-group-mode)
2315          (gnus-configure-windows '(1 0 0)) ;Take all windows.
2316          (pop-to-buffer gnus-group-buffer))
2317         ((eq major-mode 'gnus-summary-mode)
2318          (gnus-configure-windows '(0 1 0)) ;Take all windows.
2319          (pop-to-buffer gnus-summary-buffer)))
2320   (Info-goto-node (car (cdr (assq major-mode gnus-info-nodes)))))
2321
2322 (defun gnus-bug ()
2323   "Send a bug report to the Gnus maintainers."
2324   (interactive)
2325   (pop-to-buffer "*Gnus Bug*")
2326   (erase-buffer)
2327   (mail-setup gnus-maintainer "[Gnus Bug Report] " nil nil nil nil)
2328   (goto-char (point-min))
2329   (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
2330   (forward-line 1)
2331   (insert (format "%s\n%s\n\n" (gnus-version) (emacs-version)))
2332   (gnus-debug)
2333   (mail-mode)
2334   (message ""))
2335
2336 (defun gnus-debug ()
2337   "Attemps to go through the Gnus source file and report what variables have been changed.
2338 The source file has to be in the Emacs load path."
2339   (interactive)
2340   (let ((dirs load-path)
2341         file expr olist)
2342     (while dirs
2343       (if (file-exists-p (setq file (concat (car dirs) "/gnus.el")))
2344           (save-excursion
2345             (setq dirs nil)
2346             (set-buffer (get-buffer-create "*gnus bug info*"))
2347             (buffer-disable-undo (current-buffer))
2348             (erase-buffer)
2349             (insert-file-contents file)
2350             (goto-char (point-min))
2351             (or (search-forward "\n;; Internal variables" nil t)
2352                 (error "Malformed sources"))
2353             (narrow-to-region (point-min) (point))
2354             (goto-char (point-min))
2355             (while (setq expr (condition-case () 
2356                                   (read (current-buffer)) (error nil)))
2357               (and (eq (car expr) 'defvar)
2358                    (stringp (nth 3 expr))
2359                    (not (equal (eval (nth 2 expr))
2360                                (and (boundp (nth 1 expr))
2361                                     (symbol-value (nth 1 expr)))))
2362                    (setq olist (cons (nth 1 expr) olist))))
2363             (kill-buffer (current-buffer)))
2364         (setq dirs (cdr dirs))))
2365     (while olist
2366       (insert "(setq " (symbol-name (car olist)) " '" 
2367               (prin1-to-string (symbol-value (car olist))) ")\n")
2368       (setq olist (cdr olist)))
2369     (insert "\n\n")))
2370
2371 (defun gnus-overload-functions (&optional overloads)
2372   "Overload functions specified by optional argument OVERLOADS.
2373 If nothing is specified, use the variable gnus-overload-functions."
2374   (let ((defs nil)
2375         (overloads (or overloads gnus-overload-functions)))
2376     (while overloads
2377       (setq defs (car overloads))
2378       (setq overloads (cdr overloads))
2379       ;; Load file before overloading function if necessary.  Make
2380       ;; sure we cannot use `require' always.
2381       (and (not (fboundp (car defs)))
2382            (car (cdr (cdr defs)))
2383            (load (car (cdr (cdr defs))) nil 'nomessage))
2384       (fset (car defs) (car (cdr defs))))))
2385
2386 (defun gnus-replace-chars-in-string (string from to)
2387   "Replace characters in STRING from FROM to TO."
2388   (let ((string (substring string 0))   ;Copy string.
2389         (len (length string))
2390         (idx 0))
2391     ;; Replace all occurrences of FROM with TO.
2392     (while (< idx len)
2393       (if (= (aref string idx) from)
2394           (aset string idx to))
2395       (setq idx (1+ idx)))
2396     string))
2397
2398 (defun gnus-days-between (date1 date2)
2399   ;; Return the number of days between date1 and date2.
2400   (let ((d1 (mapcar (lambda (s) (and s (string-to-int s)) )
2401                     (timezone-parse-date date1)))
2402         (d2 (mapcar (lambda (s) (and s (string-to-int s)) )
2403                     (timezone-parse-date date2))))
2404     (- (timezone-absolute-from-gregorian 
2405         (nth 1 d1) (nth 2 d1) (car d1))
2406        (timezone-absolute-from-gregorian 
2407         (nth 1 d2) (nth 2 d2) (car d2)))))
2408
2409 (defun gnus-day-number (date)
2410   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
2411                      (timezone-parse-date date))))
2412     (timezone-absolute-from-gregorian 
2413      (nth 1 dat) (nth 2 dat) (car dat))))
2414
2415 (defun gnus-file-newer-than (file date)
2416   (let ((fdate (nth 5 (file-attributes file))))
2417     (or (> (car fdate) (car date))
2418         (and (= (car fdate) (car date))
2419              (> (nth 1 fdate) (nth 1 date))))))
2420
2421 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
2422 ;; the echo area.
2423 (defun gnus-y-or-n-p (prompt)
2424   (prog1
2425       (y-or-n-p prompt)
2426     (message "")))
2427
2428 (defun gnus-yes-or-no-p (prompt)
2429   (prog1
2430       (yes-or-no-p prompt)
2431     (message "")))
2432
2433 ;; Return a string of length POS+1 representing NUMber in BASE. The
2434 ;; resulting string will be left padded with zeds.
2435 (defun gnus-number-base-x (num pos base)
2436   (if (< pos 0)
2437       ""
2438     (concat 
2439      (char-to-string
2440       (aref "zyxwvutsrqponmlkjihgfedcba9876543210" (/ num (expt base pos))))
2441      (gnus-number-base-x 
2442       (% num (expt base pos)) (1- pos) base))))
2443
2444 ;; Check whether to use long file names.
2445 (defun gnus-use-long-file-name (symbol)
2446   ;; The variable has to be set...
2447   (and gnus-use-long-file-name
2448        ;; If it isn't a list, then we return t.
2449        (or (not (listp gnus-use-long-file-name))
2450            ;; If it is a list, and the list contains `symbol', we
2451            ;; return nil.  
2452            (not (memq symbol gnus-use-long-file-name)))))
2453
2454 ;; I suspect there's a better way, but I haven't taken the time to do
2455 ;; it yet. -erik selberg@cs.washington.edu
2456 (defun gnus-dd-mmm (messy-date)
2457   "Return a string like DD-MMM from a big messy string"
2458   (let ((datevec (timezone-parse-date messy-date)))
2459     (format "%2s-%s"
2460             (or (aref datevec 2) "??")
2461             (capitalize
2462              (or (car 
2463                   (nth (1- (string-to-number (aref datevec 1)))
2464                        timezone-months-assoc))
2465                  "???")))))
2466  
2467 ;; List and range functions
2468
2469 (defun gnus-last-element (list)
2470   "Return last element of LIST."
2471   (while (cdr list)
2472     (setq list (cdr list)))
2473   (car list))
2474
2475 (defun gnus-copy-sequence (list)
2476   "Do a complete, total copy of a list."
2477   (if (and (consp list) (not (consp (cdr list))))
2478       (cons (car list) (cdr list))
2479     (mapcar (lambda (elem) (if (consp elem) 
2480                                (if (consp (cdr elem))
2481                                    (gnus-copy-sequence elem)
2482                                  (cons (car elem) (cdr elem)))
2483                              elem))
2484             list)))
2485
2486 (defun gnus-set-difference (list1 list2)
2487   "Return a list of elements of LIST1 that do not appear in LIST2."
2488   (let ((list1 (copy-sequence list1)))
2489     (while list2
2490       (setq list1 (delq (car list2) list1))
2491       (setq list2 (cdr list2)))
2492     list1))
2493
2494 (defun gnus-sorted-complement (list1 list2)
2495   "Return a list of elements of LIST1 that do not appear in LIST2.
2496 Both lists have to be sorted over <."
2497   (let (out)
2498     (while (and list1 list2)
2499       (cond ((= (car list1) (car list2))
2500              (setq list1 (cdr list1)
2501                    list2 (cdr list2)))
2502             ((< (car list1) (car list2))
2503              (setq out (cons (car list1) out))
2504              (setq list1 (cdr list1)))
2505             (t
2506              (setq out (cons (car list2) out))
2507              (setq list2 (cdr list2)))))
2508     (nreverse (append (or list1 list2) out))))
2509
2510 (defun gnus-intersection (list1 list2)      
2511   (let ((result nil))
2512     (while list2
2513       (if (memq (car list2) list1)
2514           (setq result (cons (car list2) result)))
2515       (setq list2 (cdr list2)))
2516     result))
2517
2518 (defun gnus-sorted-intersection (list1 list2)
2519   ;; LIST1 and LIST2 have to be sorted over <.
2520   (let (out)
2521     (while (and list1 list2)
2522       (cond ((= (car list1) (car list2))
2523              (setq out (cons (car list1) out)
2524                    list1 (cdr list1)
2525                    list2 (cdr list2)))
2526             ((< (car list1) (car list2))
2527              (setq list1 (cdr list1)))
2528             (t
2529              (setq list2 (cdr list2)))))
2530     (nreverse out)))
2531
2532 (defun gnus-set-sorted-intersection (list1 list2)
2533   ;; LIST1 and LIST2 have to be sorted over <.
2534   ;; This function modifies LIST1.
2535   (let* ((top (cons nil list1))
2536          (prev top))
2537   (while (and list1 list2)
2538     (cond ((= (car list1) (car list2))
2539            (setq prev list1
2540                  list1 (cdr list1)
2541                  list2 (cdr list2)))
2542           ((< (car list1) (car list2))
2543            (setcdr prev (cdr list1))
2544            (setq list1 (cdr list1)))
2545           (t
2546            (setq list2 (cdr list2)))))
2547   (setcdr prev nil)
2548   (cdr top)))
2549
2550 (defun gnus-compress-sequence (numbers &optional always-list)
2551   "Convert list of numbers to a list of ranges or a single range.
2552 If ALWAYS-LIST is non-nil, this function will always release a list of
2553 ranges."
2554   (let* ((first (car numbers))
2555          (last (car numbers))
2556          result)
2557     (if (null numbers)
2558         nil
2559       (if (not (listp (cdr numbers)))
2560           numbers
2561         (while numbers
2562           (cond ((= last (car numbers)) nil) ;Omit duplicated number
2563                 ((= (1+ last) (car numbers)) ;Still in sequence
2564                  (setq last (car numbers)))
2565                 (t                      ;End of one sequence
2566                  (setq result 
2567                        (cons (if (= first last) first (cons first last)) result))
2568                  (setq first (car numbers))
2569                  (setq last  (car numbers))))
2570           (setq numbers (cdr numbers)))
2571         (if (and (not always-list) (null result))
2572             (if (= first last) first (cons first last))
2573           (nreverse (cons (if (= first last) first (cons first last))
2574                           result)))))))
2575
2576 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
2577 (defun gnus-uncompress-range (ranges)
2578   "Expand a list of ranges into a list of numbers.
2579 RANGES is either a single range on the form `(num . num)' or a list of
2580 these ranges."
2581   (let (first last result)
2582     (cond 
2583      ((null ranges)
2584       nil)
2585      ((not (listp (cdr ranges)))
2586       (setq first (car ranges))
2587       (setq last (cdr ranges))
2588       (while (<= first last)
2589         (setq result (cons first result))
2590         (setq first (1+ first)))
2591       (nreverse result))
2592      (t
2593       (while ranges
2594         (if (atom (car ranges))
2595             (if (numberp (car ranges))
2596                 (setq result (cons (car ranges) result)))
2597           (setq first (car (car ranges)))
2598           (setq last  (cdr (car ranges)))
2599           (while (<= first last)
2600             (setq result (cons first result))
2601             (setq first (1+ first))))
2602         (setq ranges (cdr ranges)))
2603       (nreverse result)))))
2604
2605 (defun gnus-add-to-range (ranges list)
2606   "Return a list of ranges that has all articles from both RANGES and LIST.
2607 Note: LIST has to be sorted over `<'."
2608   (if (not ranges)
2609       (gnus-compress-sequence list t)
2610     (setq list (copy-sequence list))
2611     (or (listp (cdr ranges))
2612         (setq ranges (list ranges)))
2613     (let ((out ranges)
2614           ilist lowest highest temp)
2615       (while (and ranges list)
2616         (setq ilist list)
2617         (setq lowest (or (and (atom (car ranges)) (car ranges))
2618                          (car (car ranges))))
2619         (while (and list (cdr list) (< (car (cdr list)) lowest))
2620           (setq list (cdr list)))
2621         (if (< (car ilist) lowest)
2622             (progn
2623               (setq temp list)
2624               (setq list (cdr list))
2625               (setcdr temp nil)
2626               (setq out (nconc (gnus-compress-sequence ilist t) out))))
2627         (setq highest (or (and (atom (car ranges)) (car ranges))
2628                           (cdr (car ranges))))
2629         (while (and list (<= (car list) highest))
2630           (setq list (cdr list)))
2631         (setq ranges (cdr ranges)))
2632       (if list
2633           (setq out (nconc (gnus-compress-sequence list t) out)))
2634       (setq out (sort out (lambda (r1 r2) 
2635                             (< (or (and (atom r1) r1) (car r1))
2636                                (or (and (atom r2) r2) (car r2))))))
2637       (setq ranges out)
2638       (while ranges
2639         (if (atom (car ranges))
2640             (if (cdr ranges)
2641                 (if (atom (car (cdr ranges)))
2642                     (if (= (1+ (car ranges)) (car (cdr ranges)))
2643                         (progn
2644                           (setcar ranges (cons (car ranges) 
2645                                                (car (cdr ranges))))
2646                           (setcdr ranges (cdr (cdr ranges)))))
2647                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
2648                       (progn
2649                         (setcar (car (cdr ranges)) (car ranges))
2650                         (setcar ranges (car (cdr ranges)))
2651                         (setcdr ranges (cdr (cdr ranges)))))))
2652           (if (cdr ranges)
2653               (if (atom (car (cdr ranges)))
2654                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
2655                       (progn
2656                         (setcdr (car ranges) (car (cdr ranges)))
2657                         (setcdr ranges (cdr (cdr ranges)))))
2658                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
2659                     (progn
2660                       (setcdr (car ranges) (cdr (car (cdr ranges))))
2661                       (setcdr ranges (cdr (cdr ranges))))))))
2662         (setq ranges (cdr ranges)))
2663       out)))
2664
2665 (defun gnus-add-to-range-new (ranges list)
2666   (gnus-compress-sequence 
2667    (sort (nconc (gnus-uncompress-range ranges) list) '<)))     
2668
2669 (defun gnus-add-to-range-old (ranges list)
2670   "Return a list of ranges that has all articles from both RANGES and LIST.
2671 Note: LIST has to be sorted over `<'."
2672   (let* ((ranges (if (and ranges (not (listp (cdr ranges))))
2673                      (list ranges) ranges))
2674          (inrange ranges)
2675          did-one
2676          range nranges first last)
2677     (if (not list)
2678         ranges
2679       (if (not ranges)
2680           (gnus-compress-sequence list t)
2681         (and ranges 
2682              (> (car (car ranges)) 1)
2683              (progn
2684                (setq did-one t)
2685                (setq inrange (setq ranges (cons (cons 1 1) ranges)))))
2686         (while (and ranges list)
2687           (setq range (car ranges))
2688           (while (and list (>= (car list) (car range))
2689                       (<= (car list) (cdr range)))
2690             (setq list (cdr list)))
2691           (while (and list (= (1- (car list)) (cdr range)))
2692             (setcdr range (car list))
2693             (setq list (cdr list)))
2694           (if (and list (and (> (car list) (cdr range)) 
2695                              (cdr ranges)
2696                              (< (car list) (car (car (cdr ranges))))))
2697               (setcdr ranges (cons (cons (car list) (car list)) (cdr ranges))))
2698           (setq ranges (cdr ranges)))
2699         (if (and list (not ranges))
2700             (setq inrange (nconc inrange (gnus-compress-sequence list t))))
2701         (if did-one
2702             (if (eq (cdr (car inrange)) 1)
2703                 (setq inrange (cdr inrange))
2704               (setcar (car inrange) 2)))
2705         (setq ranges inrange)
2706         (while ranges
2707           (if (and (cdr ranges) (>= (1+ (cdr (car ranges)))
2708                                     (car (car (cdr ranges)))))
2709               (progn
2710                 (setcdr (car ranges) (cdr (car (cdr ranges))))
2711                 (setcdr ranges (cdr (cdr ranges))))
2712             (setq ranges (cdr ranges))))
2713         (if (not (cdr inrange))
2714             (car inrange)
2715           inrange)))))
2716
2717 (defun gnus-remove-from-range (ranges list)
2718   "Return a list of ranges that has all articles from LIST removed from RANGES.
2719 Note: LIST has to be sorted over `<'."
2720   ;; !!! This function shouldn't look like this, but I've got a headache.
2721   (gnus-compress-sequence 
2722    (gnus-sorted-complement
2723     (gnus-uncompress-range ranges) list)))
2724
2725 (defun gnus-member-of-range (number ranges)
2726   (if (not (listp (cdr ranges)))
2727       (and (>= number (car ranges)) 
2728            (<= number (cdr ranges)))
2729     (let ((not-stop t))
2730       (while (and ranges 
2731                   (if (numberp (car ranges))
2732                       (>= number (car ranges))
2733                     (>= number (car (car ranges))))
2734                   not-stop)
2735         (if (if (numberp (car ranges))
2736                 (= number (car ranges))
2737               (and (>= number (car (car ranges)))
2738                    (<= number (cdr (car ranges)))))
2739             (setq not-stop nil))
2740         (setq ranges (cdr ranges)))
2741       (not not-stop))))
2742
2743 \f
2744 ;;;
2745 ;;; Gnus group mode
2746 ;;;
2747
2748 (defvar gnus-group-mode-map nil)
2749 (defvar gnus-group-make-map nil)
2750 (defvar gnus-group-list-map nil)
2751 (defvar gnus-group-sub-map nil)
2752 (put 'gnus-group-mode 'mode-class 'special)
2753
2754 (if gnus-group-mode-map
2755     nil
2756   (setq gnus-group-mode-map (make-keymap))
2757   (suppress-keymap gnus-group-mode-map)
2758   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
2759   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
2760   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
2761   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
2762   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
2763   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
2764   (define-key gnus-group-mode-map "\177" 'gnus-group-prev-unread-group)
2765   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
2766   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
2767   (define-key gnus-group-mode-map "\M-n" 'gnus-group-next-unread-group-same-level)
2768   (define-key gnus-group-mode-map "\M-p" 'gnus-group-prev-unread-group-same-level)
2769   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
2770   (define-key gnus-group-mode-map "." 'gnus-group-first-unread-group)
2771   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
2772   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
2773   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
2774   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
2775   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
2776   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
2777   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
2778   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
2779   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
2780   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
2781   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
2782   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
2783   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
2784   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
2785   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
2786   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
2787   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
2788   (define-key gnus-group-mode-map "\C-c\M-C-a" 'gnus-group-description-apropos)
2789   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
2790   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
2791   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
2792   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
2793   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
2794   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
2795   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
2796   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
2797   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
2798   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
2799   (define-key gnus-group-mode-map "V" 'gnus-version)
2800   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
2801   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
2802   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
2803   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
2804   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
2805   (define-key gnus-group-mode-map "\M-f" 'gnus-group-fetch-faq)
2806   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
2807   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
2808   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group)
2809   (define-key gnus-group-mode-map "^" 'gnus-group-enter-server-mode)
2810   (define-key gnus-group-mode-map
2811     (if gnus-xemacs [button2] [mouse-2]) 'gnus-mouse-pick-group)
2812   (define-key gnus-group-mode-map "<" 'beginning-of-buffer)
2813   (define-key gnus-group-mode-map ">" 'end-of-buffer)
2814
2815   (define-prefix-command 'gnus-group-make-map)
2816   (define-key gnus-group-mode-map "M" 'gnus-group-make-map)
2817   (define-key gnus-group-make-map "d" 'gnus-group-make-directory-group)
2818   (define-key gnus-group-make-map "h" 'gnus-group-make-help-group)
2819   (define-key gnus-group-make-map "a" 'gnus-group-make-archive-group)
2820   (define-key gnus-group-make-map "k" 'gnus-group-make-kiboze-group)
2821   (define-key gnus-group-make-map "m" 'gnus-group-make-group)
2822   (define-key gnus-group-make-map "e" 'gnus-group-edit-group)
2823
2824   (define-prefix-command 'gnus-group-list-map)
2825   (define-key gnus-group-mode-map "G" 'gnus-group-list-map)
2826   (define-key gnus-group-list-map "k" 'gnus-group-list-killed)
2827   (define-key gnus-group-list-map "z" 'gnus-group-list-zombies)
2828   (define-key gnus-group-list-map "s" 'gnus-group-list-groups)
2829   (define-key gnus-group-list-map "u" 'gnus-group-list-all-groups)
2830   (define-key gnus-group-list-map "a" 'gnus-group-apropos)
2831   (define-key gnus-group-list-map "d" 'gnus-group-description-apropos)
2832   (define-key gnus-group-list-map "m" 'gnus-group-list-matching)
2833   (define-key gnus-group-list-map "M" 'gnus-group-list-all-matching)
2834
2835   (define-prefix-command 'gnus-group-sub-map)
2836   (define-key gnus-group-mode-map "S" 'gnus-group-sub-map)
2837   (define-key gnus-group-sub-map "l" 'gnus-group-set-current-level)
2838   (define-key gnus-group-sub-map "t" 'gnus-group-unsubscribe-current-group)
2839   (define-key gnus-group-sub-map "s" 'gnus-group-unsubscribe-group)
2840   (define-key gnus-group-sub-map "k" 'gnus-group-kill-group)
2841   (define-key gnus-group-sub-map "y" 'gnus-group-yank-group)
2842   (define-key gnus-group-sub-map "w" 'gnus-group-kill-region)
2843   (define-key gnus-group-sub-map "z" 'gnus-group-kill-all-zombies))
2844
2845 (defun gnus-group-mode ()
2846   "Major mode for reading news.
2847
2848 All normal editing commands are switched off.
2849 \\<gnus-group-mode-map>
2850 The group buffer lists (some of) the groups available.  For instance,
2851 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
2852 lists all zombie groups. 
2853
2854 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe 
2855 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'. 
2856
2857 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
2858
2859 The following commands are available:
2860
2861 \\{gnus-group-mode-map}"
2862   (interactive)
2863   (if gnus-visual (gnus-group-make-menu-bar))
2864   (kill-all-local-variables)
2865   (setq mode-line-modified "-- ")
2866   (make-local-variable 'mode-line-format)
2867   (setq mode-line-format (copy-sequence mode-line-format))
2868   (and (equal (nth 3 mode-line-format) "   ")
2869        (setcar (nthcdr 3 mode-line-format) ""))
2870   (setq major-mode 'gnus-group-mode)
2871   (setq mode-name "Group")
2872   (gnus-group-set-mode-line)
2873   (setq mode-line-process nil)
2874   (use-local-map gnus-group-mode-map)
2875   (buffer-disable-undo (current-buffer))
2876   (setq truncate-lines t)
2877   (setq buffer-read-only t)
2878   (run-hooks 'gnus-group-mode-hook))
2879
2880 (defun gnus-mouse-pick-group (e)
2881   (interactive "e")
2882   (mouse-set-point e)
2883   (gnus-group-read-group nil))
2884
2885 ;;;###autoload
2886 (defun gnus-no-server (&optional arg)
2887   "Read network news.
2888 If ARG is a positive number, Gnus will use that as the
2889 startup level. If ARG is nil, Gnus will be started at level 2. 
2890 If ARG is non-nil and not a positive number, Gnus will
2891 prompt the user for the name of an NNTP server to use.
2892 As opposed to `gnus', this command will not connect to the local server."
2893   (interactive "P")
2894   (gnus (or arg 2) t))
2895
2896 (defalias '\(ding\) 'gnus)
2897
2898 ;;;###autoload
2899 (defun gnus (&optional arg dont-connect)
2900   "Read network news.
2901 If ARG is non-nil and a positive number, Gnus will use that as the
2902 startup level. If ARG is non-nil and not a positive number, Gnus will
2903 prompt the user for the name of an NNTP server to use."
2904   (interactive "P")
2905   (if (get-buffer gnus-group-buffer)
2906       (progn
2907         (switch-to-buffer gnus-group-buffer)
2908         (gnus-group-get-new-news))
2909     (gnus-clear-system)
2910     (gnus-read-init-file)
2911     (let ((level (and arg (numberp arg) (> arg 0) arg))
2912           did-connect)
2913       (unwind-protect
2914           (progn
2915             (switch-to-buffer (get-buffer-create gnus-group-buffer))
2916             (gnus-add-current-to-buffer-list)
2917             (gnus-group-mode)
2918             (or dont-connect 
2919                 (setq did-connect
2920                       (gnus-start-news-server (and arg (not level))))))
2921         (if (and (not dont-connect) 
2922                  (not did-connect))
2923             (gnus-group-quit)
2924           (run-hooks 'gnus-startup-hook)
2925           ;; NNTP server is successfully open. 
2926           (gnus-update-format-specifications)
2927           (let ((buffer-read-only nil))
2928             (erase-buffer)
2929             (if (not gnus-inhibit-startup-message)
2930                 (progn
2931                   (gnus-group-startup-message)
2932                   (sit-for 0))))
2933           (gnus-setup-news nil level)
2934           (and gnus-use-dribble-file (gnus-dribble-open))
2935           (or t (not gnus-novice-user)
2936               gnus-expert-user
2937               (gnus-group-describe-briefly)) ;Show brief help message.
2938           (gnus-group-list-groups level))))))
2939
2940 (defun gnus-group-startup-message (&optional x y)
2941   "Insert startup message in current buffer."
2942   ;; Insert the message.
2943   (erase-buffer)
2944   (insert
2945    (format "
2946     %s
2947            A newsreader 
2948       for GNU Emacs
2949
2950         Based on GNUS 
2951              written by 
2952      Masanobu UMEDA
2953
2954     Lars Magne 
2955          Ingebrigtsen 
2956       larsi@ifi.uio.no
2957
2958            gnus-version))
2959   ;; And then hack it.
2960   ;; 18 is the longest line.
2961   (indent-rigidly (point-min) (point-max) 
2962                   (/ (max (- (window-width) (or x 28)) 0) 2))
2963   (goto-char (point-min))
2964   ;; +4 is fuzzy factor.
2965   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2)))
2966
2967 (defun gnus-group-setup-buffer ()
2968   (or (get-buffer gnus-group-buffer)
2969       (progn
2970         (switch-to-buffer (get-buffer-create gnus-group-buffer))
2971         (gnus-add-current-to-buffer-list)
2972         (gnus-group-mode))))
2973
2974 (defun gnus-group-list-groups (level &optional unread)
2975   "List newsgroups with level LEVEL or lower that have unread alticles.
2976 Default is 5, which lists all subscribed groups.
2977 If argument UNREAD is non-nil, groups with no unread articles are also listed."
2978   (interactive "P")
2979   (setq level (or level gnus-group-default-list-level 5))
2980   (gnus-group-setup-buffer)     ;May call from out of group buffer
2981   (let ((case-fold-search nil)
2982         (group (gnus-group-group-name)))
2983     (funcall gnus-group-prepare-function level unread nil)
2984     (if (zerop (buffer-size))
2985         (message gnus-no-groups-message)
2986       (goto-char (point-min))
2987       (if (not group)
2988           ;; Go to the first group with unread articles.
2989           (gnus-group-search-forward nil nil nil t)
2990         ;; Find the right group to put point on. If the current group
2991         ;; has disapeared in the new listing, try to find the next
2992         ;; one. If no next one can be found, just leave point at the
2993         ;; first newsgroup in the buffer.
2994         (if (not (gnus-goto-char
2995                   (text-property-any (point-min) (point-max) 
2996                                      'gnus-group (intern group))))
2997             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
2998               (while (and newsrc
2999                           (not (gnus-goto-char 
3000                                 (text-property-any 
3001                                  (point-min) (point-max) 'gnus-group 
3002                                  (intern (car (car newsrc)))))))
3003                 (setq newsrc (cdr newsrc)))
3004               (or newsrc (progn (goto-char (point-max))
3005                                 (forward-line -1))))))
3006       ;; Adjust cursor point.
3007       (gnus-group-position-cursor))))
3008
3009 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
3010   "List all newsgroups with unread articles of level LEVEL or lower.
3011 If ALL is non-nil, list groups that have no unread articles.
3012 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
3013 If REGEXP, only list groups matching REGEXP."
3014   (set-buffer gnus-group-buffer)
3015   (let ((buffer-read-only nil)
3016         (newsrc (cdr gnus-newsrc-assoc))
3017         (lowest (or lowest 1))
3018         info clevel unread group)
3019     (erase-buffer)
3020     (if (< lowest 8)
3021         ;; List living groups.
3022         (while newsrc
3023           (setq info (car newsrc)
3024                 group (car info)
3025                 newsrc (cdr newsrc)
3026                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
3027           (and unread ; This group might be bogus
3028                (or (not regexp)
3029                    (string-match regexp group))
3030                (<= (setq clevel (car (cdr info))) level) 
3031                (>= clevel lowest)
3032                (or all            ; We list all groups?
3033                    (eq unread t)  ; We list unactivated groups
3034                    (> unread 0)   ; We list groups with unread articles
3035                    (cdr (assq 'tick (nth 3 info)))) ; And groups with tickeds
3036                (gnus-group-insert-group-line 
3037                 nil group (car (cdr info)) (nth 3 info) unread (nth 4 info)))))
3038
3039     ;; List dead groups.
3040     (and (>= level 8) (<= lowest 8)
3041          (gnus-group-prepare-flat-list-dead 
3042           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 8 ?Z
3043           regexp))
3044     (and (>= level 9) (<= lowest 9)
3045          (gnus-group-prepare-flat-list-dead 
3046           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 9 ?K
3047           regexp))
3048
3049     (gnus-group-set-mode-line)
3050     (setq gnus-have-all-newsgroups all)
3051     (run-hooks 'gnus-group-prepare-hook)))
3052
3053 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
3054   ;; List zombies and killed lists somehwat faster, which was
3055   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>. It does
3056   ;; this by ignoring the group format specification altogether.
3057   (let (group beg)
3058     (while groups
3059       (setq group (car groups)
3060             groups (cdr groups))
3061       (if (or (not regexp)
3062               (string-match regexp group))
3063           (progn
3064             (setq beg (point))
3065             (insert (format " %c    *: %s\n" mark group))
3066             (add-text-properties 
3067              beg (1+ beg) 
3068              (list 'gnus-group (intern group)
3069                    'gnus-unread t
3070                    'gnus-level level)))))))
3071
3072 (defun gnus-group-real-name (group)
3073   "Find the real name of a foreign newsgroup."
3074   (if (string-match "^[^:]+:" group)
3075       (substring group (match-end 0))
3076     group))
3077
3078 (defun gnus-group-prefixed-name (group method)
3079   "Return the whole name from GROUP and METHOD."
3080   (and (stringp method) (setq method (gnus-server-to-method method)))
3081   (concat (format "%s" (car method))
3082           (if (and 
3083                (assoc (format "%s" (car method)) (gnus-methods-using 'address))
3084                (not (string= (nth 1 method) "")))
3085               (concat "+" (nth 1 method)))
3086           ":" group))
3087
3088 (defun gnus-group-real-prefix (group)
3089   "Return the prefix of the current group name."
3090   (if (string-match "^[^:]+:" group)
3091       (substring group 0 (match-end 0))
3092     ""))
3093
3094 (defun gnus-group-method-name (group)
3095   "Return the method used for selecting GROUP."
3096   (let ((prefix (gnus-group-real-prefix group)))
3097     (if (equal prefix "")
3098         gnus-select-method
3099       (if (string-match "^[^\\+]+\\+" prefix)
3100           (list (intern (substring prefix 0 (1- (match-end 0))))
3101                 (substring prefix (match-end 0) (1- (length prefix))))
3102         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
3103
3104 (defun gnus-group-foreign-p (group)
3105   "Return nil if GROUP is native, non-nil if it is foreign."
3106   (string-match ":" group))
3107
3108 (defun gnus-group-set-info (info)
3109   (let ((entry (gnus-gethash (car info) gnus-newsrc-hashtb)))
3110     (let (marked)
3111       (if (not (setq marked (nth 3 info)))
3112           ()
3113         (while marked
3114           (or (eq 'score (car (car marked)))
3115               (eq 'bookmark (car (car marked)))
3116               (eq 'killed (car (car marked)))
3117               (setcdr (car marked) 
3118                       (gnus-uncompress-range (cdr (car marked)))))
3119           (setq marked (cdr marked)))))
3120     (if entry
3121         ()
3122       (save-excursion
3123         (set-buffer gnus-group-buffer)
3124         (if (nth 4 info)
3125             (gnus-group-make-group 
3126              (gnus-group-real-name (car info))
3127              (prin1-to-string (car (nth 4 info)))
3128              (nth 1 (nth 4 info)))
3129           (gnus-group-make-group
3130            (car info)
3131            (prin1-to-string (car gnus-select-method))
3132            (nth 1 gnus-select-method)))
3133         (message "Note: New group created")
3134         (setq entry 
3135               (gnus-gethash (gnus-group-prefixed-name 
3136                              (gnus-group-real-name (car info))
3137                              (or (nth 4 info) gnus-select-method))
3138                             gnus-newsrc-hashtb))))
3139     (if entry
3140         (progn
3141           (setcar (nthcdr 2 entry) info)
3142           (if (and (not (eq (car entry) t)) 
3143                    (gnus-gethash (car info) gnus-active-hashtb))
3144               (let ((marked (nth 3 info)))
3145                 (setcar entry 
3146                         (max 0 (- (length (gnus-list-of-unread-articles 
3147                                            (car info)))
3148                                   (length (cdr (assq 'tick marked)))
3149                                   (length (cdr (assq 'dormant marked)))))))))
3150       (error "No such group: %s" (car info)))))
3151
3152 (defun gnus-group-update-group-line ()
3153   "This function updates the current line in the newsgroup buffer and
3154 moves the point to the colon."
3155   (let* ((buffer-read-only nil)
3156          (group (gnus-group-group-name))
3157          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
3158     (if entry
3159         (gnus-dribble-enter 
3160          (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3161                  ")")))
3162     (beginning-of-line)
3163     (delete-region (point) (progn (forward-line 1) (point)))
3164     (gnus-group-insert-group-line-info group)
3165     (forward-line -1)
3166     (gnus-group-position-cursor)))
3167
3168 (defun gnus-group-insert-group-line-info (group)
3169   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
3170         active info)
3171     (if entry
3172         (progn
3173           (setq info (nth 2 entry))
3174           (gnus-group-insert-group-line 
3175            nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
3176       (setq active (gnus-gethash group gnus-active-hashtb))
3177       (gnus-group-insert-group-line 
3178        nil group (if (member group gnus-zombie-list) 8 9)
3179        nil (if active (- (1+ (cdr active)) (car active)) 0) nil))))
3180
3181 (defun gnus-group-insert-group-line (gformat group level marked number method)
3182   (let* ((gformat (or gformat gnus-group-line-format-spec))
3183          (active (gnus-gethash group gnus-active-hashtb))
3184          (number-total (if active (1+ (- (cdr active) (car active))) 0))
3185          (number-of-dormant (length (cdr (assq 'dormant marked))))
3186          (number-of-ticked (length (cdr (assq 'tick marked))))
3187          (number-of-ticked-and-dormant
3188           (+ number-of-ticked number-of-dormant))
3189          (number-of-unread-unticked 
3190           (if (numberp number) (int-to-string (max 0 number))
3191             "*"))
3192          (number-of-read
3193           (if (numberp number)
3194               (max 0 (- number-total number))
3195             "*"))
3196          (subscribed (cond ((< level 6) ? )
3197                            ((< level 8) ?U)
3198                            ((= level 8) ?Z)
3199                            (t ?K)))
3200          (qualified-group (gnus-group-real-name group))
3201          (newsgroup-description 
3202           (if gnus-description-hashtb
3203               (or (gnus-gethash group gnus-description-hashtb) "")
3204             ""))
3205          (moderated (if (member group gnus-moderated-list) ?m ? ))
3206          (moderated-string (if (eq moderated ?m) "(m)" ""))
3207          (method (gnus-server-get-method group method))
3208          (news-server (or (car (cdr method)) ""))
3209          (news-method (or (car method) ""))
3210          (news-method-string 
3211           (if method (format "(%s:%s)" (car method) (car (cdr method))) ""))
3212          (marked (if (and 
3213                       (numberp number) 
3214                       (zerop number)
3215                       (> number-of-ticked 0))
3216                      ?* ? ))
3217          (number (if (eq number t) "*" (+ number number-of-dormant 
3218                                           number-of-ticked)))
3219          (buffer-read-only nil)
3220          b)
3221     (beginning-of-line)
3222     (setq b (point))
3223     ;; Insert the text.
3224     (insert (eval gformat))
3225
3226     (add-text-properties 
3227      b (1+ b) (list 'gnus-group (intern group)
3228                     'gnus-unread (if (numberp number)
3229                                      (string-to-int number-of-unread-unticked)
3230                                    t)
3231                     'gnus-marked marked
3232                     'gnus-level level))))
3233
3234 (defun gnus-group-update-group (group &optional visible-only)
3235   "Update newsgroup info of GROUP.
3236 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't already."
3237   (save-excursion
3238     (set-buffer gnus-group-buffer)
3239     (let ((buffer-read-only nil)
3240           visible)
3241       (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
3242         (if entry
3243             (gnus-dribble-enter 
3244              (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3245                      ")"))))
3246       ;; Buffer may be narrowed.
3247       (save-restriction
3248         (widen)
3249         ;; Search a line to modify.  If the buffer is large, the search
3250         ;; takes long time.  In most cases, current point is on the line
3251         ;; we are looking for.  So, first of all, check current line. 
3252         (if (or (progn
3253                   (beginning-of-line)
3254                   (eq (get-text-property (point) 'gnus-group)
3255                       (intern group)))
3256                 (progn
3257                   (gnus-goto-char 
3258                    (text-property-any 
3259                     (point-min) (point-max) 'gnus-group (intern group)))))
3260             ;; GROUP is listed in current buffer. So, delete old line.
3261             (progn
3262               (setq visible t)
3263               (beginning-of-line)
3264               (delete-region (point) (progn (forward-line 1) (point))))
3265           ;; No such line in the buffer, find out where it's supposed to
3266           ;; go, and insert it there (or at the end of the buffer).
3267           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
3268           (or visible-only
3269               (let ((entry (cdr (gnus-gethash group gnus-newsrc-hashtb))))
3270                 (while (and entry
3271                             (not
3272                              (gnus-goto-char
3273                               (text-property-any
3274                                (point-min) (point-max) 
3275                                'gnus-group (intern (car (car entry)))))))
3276                   (setq entry (cdr entry)))
3277                 (or entry (goto-char (point-max)))))))
3278       (if (or visible (not visible-only))
3279           (gnus-group-insert-group-line-info group))
3280       (gnus-group-set-mode-line))))
3281
3282 (defun gnus-group-set-mode-line ()
3283   (if (memq 'group gnus-updated-mode-lines)
3284       (let* ((gformat (or gnus-group-mode-line-format-spec
3285                           (setq gnus-group-mode-line-format-spec
3286                                 (gnus-parse-format 
3287                                  gnus-group-mode-line-format 
3288                                  gnus-group-mode-line-format-alist))))
3289              (news-server (car (cdr gnus-select-method)))
3290              (news-method (car gnus-select-method))
3291              (mode-string (eval gformat))
3292              (max-len 60))
3293         (if (> (length mode-string) max-len) 
3294             (setq mode-string (substring mode-string 0 (- max-len 4))))
3295         (setq mode-line-buffer-identification mode-string)
3296         (set-buffer-modified-p t))))
3297
3298 (defun gnus-group-group-name ()
3299   "Get the name of the newsgroup on the current line."
3300   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
3301     (and group (symbol-name group))))
3302
3303 (defun gnus-group-group-level ()
3304   "Get the level of the newsgroup on the current line."
3305   (get-text-property (gnus-point-at-bol) 'gnus-level))
3306
3307 (defun gnus-group-search-forward (&optional backward all level first-too)
3308   "Find the next newsgroup with unread articles.
3309 If BACKWARD is non-nil, find the previous newsgroup instead.
3310 If ALL is non-nil, just find any newsgroup.
3311 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
3312 group exists.
3313 If FIRST-TOO, the current line is also eligeble as a target."
3314   (let ((way (if backward -1 1))
3315         (low 10)
3316         (beg (point))
3317         pos found)
3318     (or first-too (forward-line way))
3319     (while (and 
3320             (not (eobp))
3321             (not (setq 
3322                   found 
3323                   (and (or all
3324                            (and
3325                             (let ((unread 
3326                                    (get-text-property (point) 'gnus-unread)))
3327                               (or (eq unread t) (and unread (> unread 0))))
3328                             (let ((lev (get-text-property (point) 'gnus-level)))
3329                               (and lev (< (get-text-property (point) 'gnus-level) 6)))))
3330                        (or (not level)
3331                            (let ((lev (get-text-property (point) 'gnus-level)))
3332                              (if (and lev (<= lev level))
3333                                  t
3334                                (if (< lev low)
3335                                    (progn
3336                                      (setq low lev)
3337                                      (setq pos (point))))
3338                                nil))))))
3339             (zerop (forward-line way))))
3340     (if found 
3341         (progn (gnus-group-position-cursor) t)
3342       (if pos (goto-char pos) (goto-char beg))
3343       nil)))
3344
3345 ;; Gnus group mode commands
3346
3347 (defun gnus-group-read-group (all &optional no-article group)
3348   "Read news in this newsgroup.
3349 If argument ALL is non-nil, already read articles become readable.
3350 If optional argument NO-ARTICLE is non-nil, no article body is displayed."
3351   (interactive "P")
3352   (let ((group (or group (gnus-group-group-name)))
3353         number active marked entry)
3354     (or group (error "No group on current line"))
3355     (setq marked 
3356           (nth 3 (nth 2 (setq entry (gnus-gethash group gnus-newsrc-hashtb)))))
3357     ;; This group might be a dead group. In that case we have to get
3358     ;; the number of unread articles from `gnus-active-hashtb'.
3359     (if entry
3360         (setq number (car entry))
3361       (if (setq active (gnus-gethash group gnus-active-hashtb))
3362           (setq number (- (1+ (cdr active)) (car active)))))
3363     (gnus-summary-read-group 
3364      group (or all (and (numberp number) 
3365                         (zerop (+ number (length (cdr (assq 'tick marked)))
3366                                   (length (cdr (assq 'dormant marked)))))))
3367      no-article)))
3368
3369 (defun gnus-group-select-group (all)
3370   "Select this newsgroup.
3371 No article is selected automatically.
3372 If argument ALL is non-nil, already read articles become readable."
3373   (interactive "P")
3374   (gnus-group-read-group all t))
3375
3376 (defun gnus-group-jump-to-group (group)
3377   "Jump to newsgroup GROUP."
3378   (interactive (list (completing-read "Group: " gnus-active-hashtb nil
3379                                       (not (not gnus-read-active-file)))))
3380
3381   (if (equal group "")
3382       (error "empty group name"))
3383
3384   (let ((b (text-property-any (point-min) (point-max) 
3385                               'gnus-group (intern group))))
3386     (if b
3387         ;; Either go to the line in the group buffer...
3388         (goto-char b)
3389       ;; ... or insert the line.
3390       (or
3391        (gnus-gethash group gnus-active-hashtb)
3392        (gnus-activate-newsgroup group)
3393        (error "%s error: %s" group (gnus-status-message group)))
3394
3395       (gnus-group-update-group group)
3396       (goto-char (text-property-any (point-min) (point-max) 
3397                                     'gnus-group (intern group)))))
3398   ;; Adjust cursor point.
3399   (gnus-group-position-cursor))
3400
3401 (defun gnus-group-next-group (n)
3402   "Go to next N'th newsgroup.
3403 If N is negative, search backward instead.
3404 Returns the difference between N and the number of skips actually
3405 done."
3406   (interactive "p")
3407   (gnus-group-next-unread-group n t))
3408
3409 (defun gnus-group-next-unread-group (n &optional all level)
3410   "Go to next N'th unread newsgroup.
3411 If N is negative, search backward instead.
3412 If ALL is non-nil, choose any newsgroup, unread or not.
3413 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
3414 such group can be found, the next group with a level higher than
3415 LEVEL.
3416 Returns the difference between N and the number of skips actually
3417 made."
3418   (interactive "p")
3419   (let ((backward (< n 0))
3420         (n (abs n)))
3421     (while (and (> n 0)
3422                 (gnus-group-search-forward backward all level))
3423       (setq n (1- n)))
3424     (if (/= 0 n) (message "No more%s newsgroups%s" (if all "" " unread")
3425                           (if level " on this level or higher" "")))
3426     n))
3427
3428 (defun gnus-group-prev-group (n)
3429   "Go to previous N'th newsgroup.
3430 Returns the difference between N and the number of skips actually
3431 done."
3432   (interactive "p")
3433   (gnus-group-next-unread-group (- n) t))
3434
3435 (defun gnus-group-prev-unread-group (n)
3436   "Go to previous N'th unread newsgroup.
3437 Returns the difference between N and the number of skips actually
3438 done."  
3439   (interactive "p")
3440   (gnus-group-next-unread-group (- n)))
3441
3442 (defun gnus-group-next-unread-group-same-level (n)
3443   "Go to next N'th unread newsgroup on the same level.
3444 If N is negative, search backward instead.
3445 Returns the difference between N and the number of skips actually
3446 done."
3447   (interactive "p")
3448   (gnus-group-next-unread-group n t (gnus-group-group-level))
3449   (gnus-group-position-cursor))
3450
3451 (defun gnus-group-prev-unread-group-same-level (n)
3452   "Go to next N'th unread newsgroup on the same level.
3453 Returns the difference between N and the number of skips actually
3454 done."
3455   (interactive "p")
3456   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
3457   (gnus-group-position-cursor))
3458
3459 (defun gnus-group-best-unread-group (&optional exclude-group)
3460   "Go to the group with the highest level.
3461 If EXCLUDE-GROUP, do not go to that group."
3462   (interactive)
3463   (goto-char (point-min))
3464   (let ((best 10)
3465         unread best-point)
3466     (while (setq unread (get-text-property (point) 'gnus-unread))
3467       (if (and (numberp unread) (> unread 0))
3468           (progn
3469             (or best-point (setq best-point (point)))
3470             (if (and (< (get-text-property (point) 'gnus-level) best)
3471                      (or (not exclude-group)
3472                          (not (equal exclude-group (gnus-group-group-name)))))
3473                 (progn 
3474                   (setq best (get-text-property (point) 'gnus-level))
3475                   (setq best-point (point))))))
3476       (forward-line 1))
3477     (if best-point (goto-char best-point))
3478     (gnus-summary-position-cursor)
3479     (and best-point (gnus-group-group-name))))
3480
3481 (defun gnus-group-first-unread-group ()
3482   "Go to the first group with unread articles."
3483   (interactive)
3484   (goto-char (point-min))
3485   (gnus-group-next-unread-group 1))
3486
3487 (defun gnus-group-enter-server-mode ()
3488   "Jump to the server buffer."
3489   (interactive)
3490   (gnus-server-setup-buffer)
3491   (switch-to-buffer gnus-server-buffer)
3492   (gnus-server-prepare))
3493
3494 (defun gnus-group-make-group (name method address)
3495   "Add a new newsgroup.
3496 The user will be prompted for a NAME, for a select METHOD, and an
3497 ADDRESS."
3498   (interactive
3499    (cons 
3500     (read-string "Group name: ")
3501     (let ((method
3502            (completing-read 
3503             "Method: " (append gnus-valid-select-methods gnus-server-alist)
3504             nil t)))
3505       (if (assoc method gnus-valid-select-methods)
3506           (list method
3507                 (if (memq 'prompt-address
3508                           (assoc method gnus-valid-select-methods))
3509                     (read-string "Address: ")
3510                   ""))
3511         (list method nil)))))
3512   
3513   (let* ((meth (if address (list (intern method) address) method))
3514          (nname (gnus-group-prefixed-name name meth))
3515          info)
3516     (and (gnus-gethash nname gnus-active-hashtb)
3517          (error "Group %s already exists" nname))
3518     (gnus-group-change-level 
3519      (setq info (list t nname 3 nil nil meth))
3520      3 9 (gnus-gethash (or (gnus-group-group-name) "dummy.group")
3521                        gnus-newsrc-hashtb) t)
3522     (gnus-sethash nname '(0 . 0) gnus-active-hashtb)
3523     (gnus-dribble-enter 
3524      (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")"))
3525     (gnus-group-insert-group-line-info nname)
3526
3527     (and (gnus-check-backend-function 'request-create-group nname)
3528          (gnus-request-create-group nname))))
3529
3530 (defun gnus-group-edit-group (group)
3531   "Edit the group on the current line."
3532   (interactive (list (gnus-group-group-name)))
3533   (let (info)
3534     (if group (setq info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
3535       (error "No group on current line"))
3536     (setq gnus-winconf-edit-group (current-window-configuration))
3537     (pop-to-buffer (get-buffer-create gnus-group-edit-buffer))
3538     (gnus-add-current-to-buffer-list)
3539     (emacs-lisp-mode)
3540     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3541     (use-local-map (copy-keymap emacs-lisp-mode-map))
3542     (local-set-key "\C-c\C-c" 'gnus-group-edit-group-done)
3543     (erase-buffer)
3544     (insert ";; Type `C-c C-c' after you have edited the newsgroup entry.\n\n")
3545     (let ((cinfo (gnus-copy-sequence info))
3546           marked)
3547       (if (not (setq marked (nth 3 cinfo)))
3548           ()
3549         (while marked
3550           (or (eq 'score (car (car marked)))
3551               (eq 'bookmark (car (car marked)))
3552               (eq 'killed (car (car marked)))
3553               (setcdr (car marked) 
3554                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
3555           (setq marked (cdr marked))))
3556       (insert (pp-to-string (list 'gnus-group-set-info
3557                                   (list 'quote cinfo)))))))
3558
3559 (defun gnus-group-edit-group-done ()
3560   (interactive)
3561   (set-buffer (get-buffer-create gnus-group-edit-buffer))
3562   (eval-current-buffer)
3563   (kill-buffer (current-buffer))
3564   (and gnus-winconf-edit-group
3565        (set-window-configuration gnus-winconf-edit-group))
3566   (setq gnus-winconf-edit-group nil)
3567   (set-buffer gnus-group-buffer)
3568   (gnus-group-update-group (gnus-group-group-name))
3569   (gnus-group-position-cursor))
3570
3571 (defun gnus-group-make-help-group ()
3572   "Create the (ding) Gnus documentation group."
3573   (interactive)
3574   (and (gnus-gethash (gnus-group-prefixed-name "gnus-help" '(nndoc ""))
3575                      gnus-newsrc-hashtb)
3576        (error "Documentation group already exists"))
3577   (let ((path load-path))
3578     (while (and path
3579                 (not (file-exists-p (concat (file-name-as-directory (car path))
3580                                             "doc.txt"))))
3581       (setq path (cdr path)))
3582     (or path (error "Couldn't find doc group"))
3583     (gnus-group-make-group 
3584      "gnus-help" "nndoc" 
3585      (concat (file-name-as-directory (car path)) "doc.txt"))
3586     (gnus-group-position-cursor)))
3587
3588 (defun gnus-group-make-archive-group ()
3589   "Create the (ding) Gnus archive group."
3590   (interactive)
3591   (and (gnus-gethash (gnus-group-prefixed-name "ding.archives" '(nndir ""))
3592                      gnus-newsrc-hashtb)
3593        (error "Archive group already exists"))
3594   (gnus-group-make-group "ding.archives" "nndir" gnus-group-archive-directory)
3595   (gnus-group-position-cursor))
3596
3597 (defun gnus-group-make-directory-group (dir)
3598   "Create an nndir group.
3599 The user will be prompted for a directory. The contents of this
3600 directory will be used as a newsgroup. The directory should contain
3601 mail messages or news articles in files that have numeric names."
3602   (interactive
3603    (list (read-file-name "Create group from directory: ")))
3604   (or (file-exists-p dir) (error "No such directory"))
3605   (or (file-directory-p dir) (error "Not a directory"))
3606   (gnus-group-make-group dir "nndir" dir)
3607   (gnus-group-position-cursor))
3608
3609 (defun gnus-group-make-kiboze-group (group address scores)
3610   "Create an nnkiboze group.
3611 The user will be prompted for a name, a regexp to match groups, and
3612 score file entries for articles to include in the group."
3613   (interactive
3614    (list
3615     (read-string "nnkiboze group name: ")
3616     (read-string "Source groups (regexp): ")
3617     (let ((headers (mapcar (lambda (group) (list group))
3618                            '("subject" "from" "number" "date" "message-id"
3619                              "references" "chars" "lines" "xref")))
3620           scores header regexp regexps)
3621       (while (not (equal "" (setq header (completing-read 
3622                                           "Match on header: " headers nil t))))
3623         (setq regexps nil)
3624         (while (not (equal "" (setq regexp (read-string 
3625                                             (format "Match on %s (string): "
3626                                                     header)))))
3627           (setq regexps (cons (list regexp nil 1000 nil) regexps)))
3628         (setq scores (cons (cons header regexps) scores)))
3629       (car scores))))
3630   (gnus-group-make-group group "nnkiboze" address)
3631   (save-excursion
3632     (set-buffer (get-buffer-create " *gnus work*"))
3633     (buffer-disable-undo (current-buffer))
3634     (let (emacs-lisp-mode-hook)
3635       (pp (list 'setq 'gnus-score-alist 
3636                 (list 'quote (list scores)))
3637           (current-buffer)))
3638     (write-region (point-min) (point-max) 
3639                   (concat (or gnus-kill-files-directory "~/News")
3640                           "nnkiboze:" group "." gnus-score-file-suffix))
3641     (kill-buffer (current-buffer)))
3642   (gnus-group-position-cursor))
3643
3644 ;; Group sorting commands
3645 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
3646
3647 (defun gnus-group-sort-groups ()
3648   "Sort the group buffer using `gnus-group-sort-function'."
3649   (interactive)
3650   (setq gnus-newsrc-assoc 
3651         (sort (cdr gnus-newsrc-assoc) gnus-group-sort-function))
3652   (gnus-make-hashtable-from-newsrc-alist)
3653   (gnus-get-unread-articles 6)
3654   (gnus-group-list-groups nil))
3655
3656 (defun gnus-group-sort-by-alphabet (info1 info2)
3657   (string< (car info1) (car info2)))
3658
3659 (defun gnus-group-sort-by-unread (info1 info2)
3660   (let ((n1 (car (gnus-gethash (car info1) gnus-newsrc-hashtb)))
3661         (n2 (car (gnus-gethash (car info2) gnus-newsrc-hashtb))))
3662     (< (or (and (numberp n1) n1) 0)
3663        (or (and (numberp n2) n2) 0))))
3664
3665 (defun gnus-group-sort-by-level (info1 info2)
3666   (< (nth 1 info1) (nth 1 info2)))
3667
3668 ;; Group catching up.
3669
3670 (defun gnus-group-catchup-current (n &optional all)
3671   "Mark all articles not marked as unread in current newsgroup as read.
3672 If prefix argument N is numeric, the ARG next newsgroups will be
3673 caught up. If ALL is non-nil, marked articles will also be marked as
3674 read. Cross references (Xref: header) of articles are ignored.
3675 The difference between N and actual number of newsgroups that were
3676 caught up is returned."
3677   (interactive "p")
3678   (if (or (not gnus-interactive-catchup) ;Without confirmation?
3679           gnus-expert-user
3680           (gnus-y-or-n-p
3681            (if all
3682                "Do you really want to mark all articles as read? "
3683              "Mark all unread articles as read? ")))
3684       (progn
3685         (while 
3686             (and (> n 0)
3687                  (progn
3688                    (setq n (1- n))
3689                    (gnus-group-catchup (gnus-group-group-name) all)
3690                    (gnus-group-update-group-line)
3691                    t)
3692                  (zerop (gnus-group-next-unread-group 1))))))
3693   n)
3694
3695 (defun gnus-group-catchup-current-all (n)
3696   "Mark all articles in current newsgroup as read.
3697 Cross references (Xref: header) of articles are ignored."
3698   (interactive "p")
3699   (gnus-group-catchup-current n 'all))
3700
3701 (defun gnus-group-catchup (group &optional all)
3702   "Mark all articles in GROUP as read.
3703 If ALL is non-nil, all articles are marked as read.
3704 The return value is the number of articles that were marked as read,
3705 or nil if no action could be taken."
3706   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
3707          (num (car entry))
3708          (marked (nth 3 (nth 2 entry)))
3709          ticked)
3710     (if (not (numberp (car entry)))
3711         (message "Can't catch up; non-active group")
3712       ;; Do the updating only if the newsgroup isn't killed
3713       (if entry
3714           (progn
3715             (setq ticked (if all nil (cdr (assq 'tick marked))))
3716             (gnus-update-read-articles group ticked nil ticked)
3717             (if (and all marked)
3718                 (setcar (nthcdr 3 (nth 2 entry)) 
3719                         (delq (assq 'dormant marked) marked))))))
3720     num))
3721
3722 (defun gnus-group-expire-articles (newsgroup)
3723   "Expire all expirable articles in the current newsgroup."
3724   (interactive (list (gnus-group-group-name)))
3725   (if (not newsgroup) (error "No current newsgroup"))
3726   (let ((expirable 
3727          (assq 'expire (nth 3 (nth 2 (gnus-gethash newsgroup 
3728                                                    gnus-newsrc-hashtb))))))
3729  (and expirable 
3730       (gnus-check-backend-function 'request-expire-articles newsgroup)
3731       (setcdr expirable
3732               (gnus-request-expire-articles (cdr expirable) newsgroup)))))
3733
3734 (defun gnus-group-expire-all-groups ()
3735   "Expire all expirable articles in all newsgroups."
3736   (interactive)
3737   (message "Expiring...")
3738   (let ((newsrc (cdr gnus-newsrc-assoc)))
3739     (while newsrc
3740       (gnus-group-expire-articles (car (car newsrc)))
3741       (setq newsrc (cdr newsrc))))
3742   (message "Expiring...done"))
3743
3744 (defun gnus-group-set-current-level (n)
3745   "Set the level of the current group to the numeric prefix."
3746   (interactive "P")
3747   (setq n (or n (string-to-int 
3748                  (completing-read 
3749                   "Level: " 
3750                   (mapcar (lambda (n) (list (char-to-string n))) "123456789")
3751                   nil t))))
3752   (let ((group (gnus-group-group-name)))
3753     (if (not group) (error "No newsgroup on current line.")
3754     (if (and (numberp n) (>= n 1) (<= n 9))
3755         (progn
3756           (message "Changed level of %s from %d to %d" 
3757                    group (gnus-group-group-level) n)
3758           (gnus-group-change-level group n (gnus-group-group-level))
3759           (gnus-group-update-group-line))
3760       (error "Illegal level: %s" n))))
3761   (forward-line 1)
3762   (gnus-group-position-cursor))
3763
3764 (defun gnus-group-unsubscribe-current-group (arg)
3765   "Toggle subscribe from/to unsubscribe current group."
3766   (interactive "P")
3767   (let ((group (gnus-group-group-name)))
3768     (or group (error "No newsgroup on current line"))
3769     (or arg (setq arg (if (<= (gnus-group-group-level) 5) 6 3)))
3770     (gnus-group-unsubscribe-group group arg)
3771     (gnus-group-next-group 1)))
3772
3773 (defun gnus-group-unsubscribe-group (group &optional level)
3774   "Toggle subscribe from/to unsubscribe GROUP.
3775 New newsgroup is added to .newsrc automatically."
3776   (interactive
3777    (list (completing-read "Group: " gnus-active-hashtb nil 
3778                           gnus-have-read-active-file)))
3779   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
3780     (cond (newsrc
3781            ;; Toggle subscription flag.
3782            (gnus-group-change-level 
3783             newsrc (if level level (if (< (nth 1 (nth 2 newsrc)) 6) 6 4)))
3784            (gnus-group-update-group group))
3785           ((and (stringp group)
3786                 (or (not gnus-have-read-active-file)
3787                     (gnus-gethash group gnus-active-hashtb)))
3788            ;; Add new newsgroup.
3789            (gnus-group-change-level 
3790             group 
3791             (if level level 3) 
3792             (or (and (member group gnus-zombie-list) 8) 9)
3793             (or (and (gnus-group-group-name)
3794                      (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb))
3795                 (gnus-gethash (car (car gnus-newsrc-assoc)) 
3796                               gnus-newsrc-hashtb)))
3797            (gnus-group-update-group group))
3798           (t (error "No such newsgroup: %s" group)))
3799     (gnus-group-position-cursor)))
3800
3801 (defun gnus-group-transpose-groups (arg)
3802   "Exchange current newsgroup and previous newsgroup.
3803 With argument ARG, takes previous newsgroup and moves it past ARG newsgroup."
3804   (interactive "p")
3805   ;; BUG: last newsgroup and the last but one cannot be transposed
3806   ;; since gnus-group-search-forward does not move forward beyond the
3807   ;; last.  If we instead use forward-line, no problem, but I don't
3808   ;; want to use it for later extension.
3809   (while (> arg 0)
3810     (gnus-group-search-forward t t)
3811     (gnus-group-kill-group 1)
3812     (gnus-group-search-forward nil t)
3813     (gnus-group-yank-group)
3814     (gnus-group-search-forward nil t)
3815     (setq arg (1- arg))))
3816
3817 (defun gnus-group-kill-all-zombies ()
3818   "Kill all zombie newsgroups."
3819   (interactive)
3820   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
3821   (setq gnus-zombie-list nil)
3822   (funcall gnus-group-prepare-function 5 nil nil)
3823   (goto-char (point-min))
3824   (gnus-group-position-cursor))
3825
3826 (defun gnus-group-kill-region (begin end)
3827   "Kill newsgroups in current region (excluding current point).
3828 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
3829   (interactive "r")
3830   (let ((lines
3831          ;; Exclude a line where current point is on.
3832          (1-
3833           ;; Count lines.
3834           (save-excursion
3835             (count-lines
3836              (progn
3837                (goto-char begin)
3838                (beginning-of-line)
3839                (point))
3840              (progn
3841                (goto-char end)
3842                (end-of-line)
3843                (point)))))))
3844     (goto-char begin)
3845     (beginning-of-line)                 ;Important when LINES < 1
3846     (gnus-group-kill-group lines)))
3847
3848 (defun gnus-group-kill-group (n)
3849   "The the next N groups.
3850 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
3851 However, only groups that were alive can be yanked; already killed 
3852 groups or zombie groups can't be yanked.
3853 The return value is the name of the (last) newsgroup that was killed."
3854   (interactive "p")
3855   (let ((buffer-read-only nil)
3856         group entry level)
3857     (while (>= (setq n  (1- n)) 0)
3858       (setq group (gnus-group-group-name))
3859       (or group
3860           (signal 'end-of-buffer nil))
3861       (setq level (gnus-group-group-level))
3862       (beginning-of-line)
3863       (delete-region (point) (progn (forward-line 1) (point)))
3864       (if (setq entry (gnus-gethash group gnus-newsrc-hashtb))
3865           (setq gnus-list-of-killed-groups 
3866                 (cons (cons (car entry) (nth 2 entry)) 
3867                       gnus-list-of-killed-groups)))
3868       (gnus-group-change-level (if entry entry group) 9 (if entry nil level)))
3869     (if (eobp)
3870         (forward-line -1))
3871     (gnus-group-position-cursor)
3872     group))
3873
3874 (defun gnus-group-yank-group (&optional arg)
3875   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
3876 inserting it before the current newsgroup.  The numeric ARG specifies
3877 how many newsgroups are to be yanked.  The name of the (last)
3878 newsgroup yanked is returned."
3879   (interactive "p")
3880   (if (not arg) (setq arg 1))
3881   (let (info group prev)
3882     (while (>= (setq arg (1- arg)) 0)
3883       (if (not (setq info (car gnus-list-of-killed-groups)))
3884           (error "No more newsgroups to yank"))
3885       (setq group (nth 2 info))
3886       ;; Find which newsgroup to insert this one before - search
3887       ;; backward until something suitable is found. If there are no
3888       ;; other newsgroups in this buffer, just make this newsgroup the
3889       ;; first newsgroup.
3890       (while (and (not (setq prev (gnus-group-group-name)))
3891                   (zerop (forward-line -1))))
3892       (if (not prev)
3893           (setq prev (car (car gnus-newsrc-assoc))))
3894       (gnus-group-change-level 
3895        info (nth 2 info) 9 
3896        (gnus-gethash prev gnus-newsrc-hashtb)
3897        t)
3898       (gnus-group-insert-group-line-info (nth 1 info))
3899       (setq gnus-list-of-killed-groups 
3900             (cdr gnus-list-of-killed-groups)))
3901     (forward-line -1)
3902     (gnus-group-position-cursor)
3903     group))
3904       
3905 (defun gnus-group-list-all-groups (arg)
3906   "List all newsgroups with level ARG or lower.
3907 Default is 7, which lists all subscribed and most unsubscribed groups."
3908   (interactive "P")
3909   (setq arg (or arg 7))
3910   (gnus-group-list-groups arg t))
3911
3912 (defun gnus-group-list-killed ()
3913   "List all killed newsgroups in the group buffer."
3914   (interactive)
3915   (if (not gnus-killed-list)
3916       (message "No killed groups")
3917     (funcall gnus-group-prepare-function 9 t 9)
3918     (goto-char (point-min)))
3919   (gnus-group-position-cursor))
3920
3921 (defun gnus-group-list-zombies ()
3922   "List all zombie newsgroups in the group buffer."
3923   (interactive)
3924   (if (not gnus-zombie-list)
3925       (message "No zombie groups")
3926     (funcall gnus-group-prepare-function 8 t 8)
3927     (goto-char (point-min)))
3928   (gnus-group-position-cursor))
3929
3930 (defun gnus-group-get-new-news (&optional arg)
3931   "Get newly arrived articles.
3932 If ARG is non-nil, it should be a number between one and nine to
3933 specify which levels you are interested in re-scanning."
3934   (interactive "P")
3935   (run-hooks 'gnus-get-new-news-hook)
3936   (if (and gnus-read-active-file (not arg))
3937       (progn
3938         (gnus-read-active-file)
3939         (gnus-get-unread-articles (or arg 6)))
3940     (let ((gnus-read-active-file nil))
3941       (gnus-get-unread-articles (or arg 6))))
3942   (gnus-group-list-groups 
3943    (or gnus-group-always-list-unread arg 5) gnus-have-all-newsgroups))
3944
3945 (defun gnus-group-get-new-news-this-group (n)
3946   "Check for newly arrived news in the current group (and the N-1 next groups).
3947 The difference between N and the number of newsgroup checked is returned.
3948 If N is negative, this group and the N-1 previous groups will be checked."
3949   (interactive "p")
3950   (let ((way (if (< n 0) -1 1))
3951         (n (abs n))
3952         (w-p (window-start))
3953         group)
3954     (while (and (> n 0)
3955                 (progn
3956                   (or (gnus-get-new-news-in-group
3957                        (setq group (gnus-group-group-name)))
3958                       (progn 
3959                         (ding) 
3960                         (message "%s error: %s" 
3961                                  group (gnus-status-message group))))
3962                   t)
3963                 (zerop (gnus-group-next-group way)))
3964       (setq n (1- n)))
3965     (if (/= 0 n) (message "No more newsgroups"))
3966     ;; !!! I don't know why the buffer scrolls forward when updating
3967     ;; the first line in the group buffer, but it does. So we set the
3968     ;; window start forcibly.
3969     (set-window-start (get-buffer-window (current-buffer)) w-p)
3970     n))
3971
3972 (defun gnus-get-new-news-in-group (group)
3973   (and group 
3974        (gnus-activate-newsgroup group)
3975        (progn
3976          (gnus-get-unread-articles-in-group 
3977           (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
3978           (gnus-gethash group gnus-active-hashtb))
3979          (gnus-group-update-group-line)
3980          t)))
3981
3982 (defun gnus-group-fetch-faq (group)
3983   "Fetch the FAQ for the current group."
3984   (interactive (list (gnus-group-group-name)))
3985   (or group (error "No group name given"))
3986   (let ((file (concat gnus-group-faq-directory group))) 
3987     (if (not (file-exists-p file))
3988         (error "No such file: %s" file)
3989       (find-file file))))
3990   
3991 (defun gnus-group-describe-group (force &optional group)
3992   "Display a description of the current newsgroup."
3993   (interactive "P")
3994   (and force (setq gnus-description-hashtb nil))
3995   (let ((group (or group (gnus-group-group-name)))
3996         desc)
3997     (or group (message "No group name given"))
3998     (and (or gnus-description-hashtb
3999              (setq desc (gnus-group-get-description group))
4000              (gnus-read-descriptions-file))
4001          (message
4002           (or desc (gnus-gethash group gnus-description-hashtb)
4003               "No description available")))))
4004
4005 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4006 (defun gnus-group-describe-all-groups (force)
4007   "Pop up a buffer with descriptons of all newsgroups."
4008   (interactive "P")
4009   (and force (setq gnus-description-hashtb nil))
4010   (if (not (or gnus-description-hashtb
4011                (gnus-read-descriptions-file)))
4012       (error "Couldn't request descriptions file"))
4013   (let ((buffer-read-only nil)
4014         b)
4015     (erase-buffer)
4016     (mapatoms
4017      (lambda (group)
4018        (setq b (point))
4019        (insert (format "      *: %-20s %s\n" (symbol-name group)
4020                        (symbol-value group)))
4021        (add-text-properties 
4022         b (1+ b) (list 'gnus-group group
4023                        'gnus-unread t 'gnus-marked nil 'gnus-level 6)))
4024      gnus-description-hashtb)
4025     (goto-char (point-min))
4026     (gnus-group-position-cursor)))
4027
4028 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
4029 (defun gnus-group-apropos (regexp &optional search-description)
4030   "List all newsgroups that have names that match a regexp."
4031   (interactive "sGnus apropos (regexp): ")
4032   (let ((prev "")
4033         (obuf (current-buffer))
4034         groups des prev)
4035     ;; Go through all newsgroups that are known to Gnus.
4036     (mapatoms 
4037      (lambda (group)
4038        (and (string-match regexp (symbol-name group))
4039             (setq groups (cons (symbol-name group) groups))))
4040      gnus-active-hashtb)
4041     ;; Go through all descriptions that are known to Gnus. 
4042     (if search-description
4043         (mapatoms 
4044          (lambda (group)
4045            (and (string-match regexp (symbol-value group))
4046                 (gnus-gethash (symbol-name group) gnus-active-hashtb)
4047                 (setq groups (cons (symbol-name group) groups))))
4048          gnus-description-hashtb))
4049     (if (not groups)
4050         (message "No groups matched \"%s\"." regexp)
4051       ;; Print out all the groups.
4052       (save-excursion
4053         (pop-to-buffer (get-buffer-create "*Gnus Help*"))
4054         (buffer-disable-undo (current-buffer))
4055         (erase-buffer)
4056         (setq groups (sort groups 'string<))
4057         (while groups
4058           ;; Groups may be entered twice into the list of groups.
4059           (if (not (string= (car groups) prev))
4060               (progn
4061                 (insert (setq prev (car groups)) "\n")
4062                 (if (and gnus-description-hashtb
4063                          (setq des (gnus-gethash (car groups) 
4064                                                  gnus-description-hashtb)))
4065                     (insert "  " des "\n"))))
4066           (setq groups (cdr groups)))
4067         (goto-char 1)))
4068     (pop-to-buffer obuf)))
4069
4070 (defun gnus-group-description-apropos (regexp)
4071   "List all newsgroups that have names or desccriptions that match a regexp."
4072   (interactive "sGnus description apropos (regexp): ")
4073   (if (not (or gnus-description-hashtb
4074                (gnus-read-descriptions-file)))
4075       (error "Couldn't request descriptions file"))
4076   (gnus-group-apropos regexp t))
4077
4078 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4079 (defun gnus-group-list-matching (level regexp &optional all lowest) 
4080   "List all groups with unread articles that match REGEXP.
4081 If the prefix LEVEL is non-nil, it should be a number that says which
4082 level to cut off listing groups. 
4083 If ALL, also list groups with no unread articles.
4084 If LOWEST, don't list groups with level lower than LOWEST."
4085   (interactive "P\nsList newsgroups matching: ")
4086   (gnus-group-prepare-flat (or level 5) all (or lowest 1) regexp)
4087   (goto-char (point-min))
4088   (gnus-group-position-cursor))
4089
4090 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
4091   "List all groups that match REGEXP.
4092 If the prefix LEVEL is non-nil, it should be a number that says which
4093 level to cut off listing groups. 
4094 If LOWEST, don't list groups with level lower than LOWEST."
4095   (interactive "P\nsList newsgroups matching: ")
4096   (gnus-group-list-matching (or level 9) regexp t lowest))
4097
4098 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
4099 (defun gnus-group-save-newsrc ()
4100   "Save the Gnus startup files."
4101   (interactive)
4102   (gnus-save-newsrc-file))
4103
4104 (defun gnus-group-restart (&optional arg)
4105   "Force Gnus to read the .newsrc file."
4106   (interactive "P")
4107   (gnus-save-newsrc-file)
4108   (gnus-setup-news 'force)
4109   (gnus-group-list-groups arg gnus-have-all-newsgroups))
4110
4111 (defun gnus-group-read-init-file ()
4112   "Read the Gnus elisp init file."
4113   (interactive)
4114   (gnus-read-init-file))
4115
4116 (defun gnus-group-check-bogus-groups ()
4117   "Check bogus newsgroups."
4118   (interactive)
4119   (gnus-check-bogus-newsgroups (not gnus-expert-user))  ;Require confirmation.
4120   (gnus-group-list-groups nil gnus-have-all-newsgroups))
4121
4122 (defun gnus-group-mail ()
4123   "Start composing a mail."
4124   (interactive)
4125   (mail))
4126
4127 (defun gnus-group-edit-global-kill (article &optional group)
4128   "Edit the global kill file.
4129 If GROUP, edit that local kill file instead."
4130   (interactive "P")
4131   (setq gnus-current-kill-article article)
4132   (gnus-kill-file-edit-file group)
4133   (message
4134    (substitute-command-keys
4135     "Editing a global kill file (Type \\[gnus-kill-file-exit] to exit)")))
4136
4137 (defun gnus-group-edit-local-kill (article group)
4138   "Edit a local kill file."
4139   (interactive (list nil (gnus-group-group-name)))
4140   (gnus-group-edit-global-kill article group))
4141
4142 (defun gnus-group-force-update ()
4143   "Update `.newsrc' file."
4144   (interactive)
4145   (gnus-save-newsrc-file))
4146
4147 (defun gnus-group-suspend ()
4148   "Suspend the current Gnus session.
4149 In fact, cleanup buffers except for group mode buffer.
4150 The hook gnus-suspend-gnus-hook is called before actually suspending."
4151   (interactive)
4152   (run-hooks 'gnus-suspend-gnus-hook)
4153   ;; Kill Gnus buffers except for group mode buffer.
4154   (let ((group-buf (get-buffer gnus-group-buffer)))
4155     (while gnus-buffer-list
4156       (and (not (eq (get-buffer (car gnus-buffer-list)) group-buf))
4157            (not (eq (get-buffer (car gnus-buffer-list)) gnus-dribble-buffer))
4158            (get-buffer (car gnus-buffer-list))
4159            (buffer-name (get-buffer (car gnus-buffer-list)))
4160            (kill-buffer (car gnus-buffer-list)))
4161       (setq gnus-buffer-list (cdr gnus-buffer-list)))
4162     (setq gnus-buffer-list (list group-buf))
4163     (bury-buffer group-buf)
4164     (delete-windows-on group-buf t)))
4165
4166 (defun gnus-group-clear-dribble ()
4167   "Clear all information from the dribble buffer."
4168   (interactive)
4169   (gnus-dribble-clear))
4170
4171 (defun gnus-group-exit ()
4172   "Quit reading news after updating .newsrc.eld and .newsrc.
4173 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4174   (interactive)
4175   (if (or noninteractive                ;For gnus-batch-kill
4176           (zerop (buffer-size))         ;No news is good news.
4177           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
4178           (not gnus-interactive-exit)   ;Without confirmation
4179           gnus-expert-user
4180           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
4181       (progn
4182         (run-hooks 'gnus-exit-gnus-hook)
4183         (gnus-save-newsrc-file)
4184         (gnus-close-backends)
4185         (gnus-clear-system))))
4186
4187 (defun gnus-close-backends ()
4188   ;; Send a close request to all backends that support such a request. 
4189   (let ((methods gnus-valid-select-methods)
4190         func)
4191     (while methods
4192       (if (fboundp (setq func (intern (concat (car (car methods))
4193                                               "-request-close"))))
4194           (funcall func))
4195       (setq methods (cdr methods)))))
4196
4197 (defun gnus-group-quit ()
4198   "Quit reading news without updating .newsrc.eld or .newsrc.
4199 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4200   (interactive)
4201   (if (or noninteractive                ;For gnus-batch-kill
4202           (zerop (buffer-size))
4203           (not (gnus-server-opened gnus-select-method))
4204           gnus-expert-user
4205           (not gnus-current-startup-file)
4206           (gnus-yes-or-no-p
4207            (format "Quit reading news without saving %s? "
4208                    (file-name-nondirectory gnus-current-startup-file))))
4209       (progn
4210         (run-hooks 'gnus-exit-gnus-hook)
4211         (gnus-dribble-save)
4212         (gnus-close-backends)
4213         (gnus-clear-system))))
4214
4215 (defun gnus-group-describe-briefly ()
4216   "Give a one line description of the group mode commands."
4217   (interactive)
4218   (message
4219    (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")))
4220
4221 (defun gnus-group-browse-foreign-server (method)
4222   "Browse a foreign news server.
4223 If called interactively, this function will ask for a select method
4224  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
4225 If not, METHOD should be a list where the first element is the method
4226 and the second element is the address."
4227   (interactive
4228    (list (list (intern (completing-read 
4229                         "Select method: "
4230                         gnus-valid-select-methods nil t "nntp"))
4231                ;; Suggested by mapjph@bath.ac.uk.
4232                (completing-read 
4233                 "Server name: " 
4234                 (mapcar (lambda (server) (list server))
4235                         gnus-secondary-servers)))))
4236   (gnus-browse-foreign-server method))
4237
4238 \f
4239 ;;;
4240 ;;; Browse Server Mode
4241 ;;;
4242
4243 (defvar gnus-browse-server-mode-hook nil)
4244 (defvar gnus-browse-server-mode-map nil)
4245 (put 'gnus-browse-server-mode 'mode-class 'special)
4246
4247 (if gnus-browse-server-mode-map
4248     nil
4249   (setq gnus-browse-server-mode-map (make-keymap))
4250   (suppress-keymap gnus-browse-server-mode-map)
4251   (define-key gnus-browse-server-mode-map " " 'gnus-browse-read-group)
4252   (define-key gnus-browse-server-mode-map "=" 'gnus-browse-read-group)
4253   (define-key gnus-browse-server-mode-map "n" 'gnus-browse-next-group)
4254   (define-key gnus-browse-server-mode-map "p" 'gnus-browse-prev-group)
4255   (define-key gnus-browse-server-mode-map "\177" 'gnus-browse-prev-group)
4256   (define-key gnus-browse-server-mode-map "N" 'gnus-browse-next-group)
4257   (define-key gnus-browse-server-mode-map "P" 'gnus-browse-prev-group)
4258   (define-key gnus-browse-server-mode-map "\M-n" 'gnus-browse-next-group)
4259   (define-key gnus-browse-server-mode-map "\M-p" 'gnus-browse-prev-group)
4260   (define-key gnus-browse-server-mode-map "\r" 'gnus-browse-read-group)
4261   (define-key gnus-browse-server-mode-map "u" 'gnus-browse-unsubscribe-current-group)
4262   (define-key gnus-browse-server-mode-map "l" 'gnus-browse-exit)
4263   (define-key gnus-browse-server-mode-map "L" 'gnus-browse-exit)
4264   (define-key gnus-browse-server-mode-map "q" 'gnus-browse-exit)
4265   (define-key gnus-browse-server-mode-map "Q" 'gnus-browse-exit)
4266   (define-key gnus-browse-server-mode-map "\C-c\C-c" 'gnus-browse-exit)
4267   (define-key gnus-browse-server-mode-map "?" 'gnus-browse-describe-briefly)
4268   (define-key gnus-browse-server-mode-map "\C-c\C-i" 'gnus-info-find-node)
4269   )
4270
4271 (defvar gnus-browse-current-method nil)
4272
4273 (defun gnus-browse-foreign-server (method)
4274   (setq gnus-browse-current-method method)
4275   (let ((gnus-select-method method)
4276         groups group)
4277     (message "Connecting to %s..." (nth 1 method))
4278     (or (gnus-server-opened method)
4279         (gnus-open-server method)
4280         (error "Unable to contact server: %s" (gnus-status-message method)))
4281     (or (gnus-request-list method)
4282         (error "Couldn't request list: %s" (gnus-status-message method)))
4283     (set-buffer (get-buffer-create "*Gnus Browse Server*"))
4284     (gnus-add-current-to-buffer-list)
4285     (buffer-disable-undo (current-buffer))
4286     (let ((buffer-read-only nil))
4287       (erase-buffer))
4288     (gnus-browse-server-mode)
4289     (setq mode-line-buffer-identification
4290           (format
4291            "(ding) Browse Server {%s:%s}" (car method) (car (cdr method))))
4292     (save-excursion
4293       (set-buffer nntp-server-buffer)
4294       (let ((cur (current-buffer)))
4295         (goto-char 1)
4296         (delete-matching-lines gnus-ignored-newsgroups)
4297         (while (re-search-forward 
4298                 "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
4299           (goto-char (match-end 1))
4300           (setq groups (cons (cons (buffer-substring (match-beginning 1)
4301                                                      (match-end 1))
4302                                    (- (read cur) (read cur)))
4303                              groups)))))
4304     (setq groups (sort groups 
4305                        (lambda (l1 l2)
4306                          (string< (car l1) (car l2)))))
4307     (let ((buffer-read-only nil))
4308       (while groups
4309         (setq group (car groups))
4310         (insert 
4311          (format "K%7d: %s\n" (cdr group) (car group)))
4312         (setq groups (cdr groups))))
4313     (switch-to-buffer (current-buffer))
4314     (goto-char 1)
4315     (gnus-group-position-cursor)))
4316
4317 (defun gnus-browse-server-mode ()
4318   "Major mode for browsing a foreign server."
4319   (interactive)
4320   (kill-all-local-variables)
4321   (setq mode-line-modified "-- ")
4322   (make-local-variable 'mode-line-format)
4323   (setq mode-line-format (copy-sequence mode-line-format))
4324   (and (equal (nth 3 mode-line-format) "   ")
4325        (setcar (nthcdr 3 mode-line-format) ""))
4326   (setq major-mode 'gnus-browse-server-mode)
4327   (setq mode-name "Browse Server")
4328   (setq mode-line-process nil)
4329   (use-local-map gnus-browse-server-mode-map)
4330   (buffer-disable-undo (current-buffer))
4331   (setq truncate-lines t)
4332   (setq buffer-read-only t)
4333   (run-hooks 'gnus-browse-server-mode-hook))
4334
4335 (defun gnus-browse-read-group ()
4336   "Not implemented, and will probably never be."
4337   (interactive)
4338   (error "You can't read while browsing"))
4339
4340 (defun gnus-browse-next-group (n)
4341   "Go to the next group."
4342   (interactive "p")
4343   (prog1
4344       (forward-line n)
4345     (gnus-group-position-cursor)))
4346
4347 (defun gnus-browse-prev-group (n)
4348   "Go to the next group."
4349   (interactive "p")
4350   (gnus-browse-next-group (- n)))
4351
4352 (defun gnus-browse-unsubscribe-current-group (arg)
4353   "(Un)subscribe to the next ARG groups."
4354   (interactive "p")
4355   (and (eobp)
4356        (error "No group at current line."))
4357   (let ((ward (if (< arg 0) -1 1))
4358         (arg (abs arg)))
4359     (while (and (> arg 0)
4360                 (not (eobp))
4361                 (gnus-browse-unsubscribe-group)
4362                 (zerop (gnus-browse-next-group ward)))
4363       (setq arg (1- arg)))
4364     (gnus-group-position-cursor)
4365     (if (/= 0 arg) (message "No more newsgroups"))
4366     arg))
4367   
4368 (defun gnus-browse-unsubscribe-group ()
4369   (let ((sub nil)
4370         (buffer-read-only nil)
4371         group)
4372     (save-excursion
4373       (beginning-of-line)
4374       (if (= (following-char) ?K) (setq sub t))
4375       (re-search-forward ": \\(.*\\)$" nil t)
4376       (setq group (gnus-group-prefixed-name 
4377                    (buffer-substring (match-beginning 1) (match-end 1))
4378                    gnus-browse-current-method))
4379       (beginning-of-line)
4380       (delete-char 1)
4381       (if sub
4382           (progn
4383             (gnus-group-change-level 
4384              (list t group 3 nil nil gnus-browse-current-method) 3 9 
4385              (gnus-gethash (car (nth 1 gnus-newsrc-assoc)) gnus-newsrc-hashtb)
4386              t)
4387             (insert ? ))
4388         (gnus-group-change-level group 9 3)
4389         (insert ?K)))
4390     t))
4391
4392 (defun gnus-browse-exit ()
4393   "Quit browsing and return to the group buffer."
4394   (interactive)
4395   (if (eq major-mode 'gnus-browse-server-mode)
4396       (kill-buffer (current-buffer)))
4397   (switch-to-buffer gnus-group-buffer)
4398   (gnus-group-list-groups nil))
4399
4400 (defun gnus-browse-describe-briefly ()
4401   "Give a one line description of the group mode commands."
4402   (interactive)
4403   (message
4404    (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")))
4405       
4406 \f
4407 ;;;
4408 ;;; Gnus summary mode
4409 ;;;
4410
4411 (defvar gnus-summary-mode-map nil)
4412 (defvar gnus-summary-mark-map nil)
4413 (defvar gnus-summary-mscore-map nil)
4414 (defvar gnus-summary-send-map nil)
4415 (defvar gnus-summary-extract-map nil)
4416 (defvar gnus-summary-extract-view-map nil)
4417 (defvar gnus-summary-article-map nil)
4418 (defvar gnus-summary-thread-map nil)
4419 (defvar gnus-summary-goto-map nil)
4420 (defvar gnus-summary-exit-map nil)
4421 (defvar gnus-summary-various-map nil)
4422 (defvar gnus-summary-interest-map nil)
4423 (defvar gnus-summary-process-map nil)
4424 (defvar gnus-summary-score-map nil)
4425 (defvar gnus-summary-sort-map nil)
4426 (defvar gnus-summary-mgroup-map nil)
4427 (defvar gnus-summary-vsave-map nil)
4428 (defvar gnus-summary-increase-map nil)
4429 (defvar gnus-summary-inc-subject-map nil)
4430 (defvar gnus-summary-inc-author-map nil)
4431 (defvar gnus-summary-inc-body-map nil)
4432 (defvar gnus-summary-inc-id-map nil)
4433 (defvar gnus-summary-inc-xref-map nil)
4434 (defvar gnus-summary-inc-thread-map nil)
4435 (defvar gnus-summary-inc-fol-map nil)
4436 (defvar gnus-summary-lower-map nil)
4437 (defvar gnus-summary-low-subject-map nil)
4438 (defvar gnus-summary-low-author-map nil)
4439 (defvar gnus-summary-low-body-map nil)
4440 (defvar gnus-summary-low-id-map nil)
4441 (defvar gnus-summary-low-xref-map nil)
4442 (defvar gnus-summary-low-thread-map nil)
4443 (defvar gnus-summary-low-fol-map nil)
4444 (put 'gnus-summary-mode 'mode-class 'special)
4445
4446 (if gnus-summary-mode-map
4447     nil
4448   (setq gnus-summary-mode-map (make-keymap))
4449   (suppress-keymap gnus-summary-mode-map)
4450
4451   ;; Non-orthogonal keys
4452
4453   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
4454   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
4455   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
4456   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
4457   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
4458   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
4459   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
4460   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
4461   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
4462   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
4463   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
4464   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
4465   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
4466   (define-key gnus-summary-mode-map "\M-s" 'gnus-summary-search-article-forward)
4467   (define-key gnus-summary-mode-map "\M-r" 'gnus-summary-search-article-backward)
4468   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
4469   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
4470   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-subject)
4471   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
4472   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
4473   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
4474   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
4475   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
4476   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
4477   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
4478   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
4479   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
4480   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
4481   (define-key gnus-summary-mode-map "k" 'gnus-summary-kill-same-subject-and-select)
4482   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
4483   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
4484   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
4485   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
4486   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
4487   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
4488   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
4489   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
4490   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
4491   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
4492   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
4493   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
4494   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
4495   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
4496   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
4497   (define-key gnus-summary-mode-map "\C-w" 'gnus-summary-mark-region-as-read)
4498   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
4499   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
4500   (define-key gnus-summary-mode-map "\C-c\M-\C-s" 'gnus-summary-show-all-expunged)
4501   (define-key gnus-summary-mode-map "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
4502   (define-key gnus-summary-mode-map "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
4503   (define-key gnus-summary-mode-map "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
4504   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
4505   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
4506   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
4507   (define-key gnus-summary-mode-map "\C-x\C-s" 'gnus-summary-reselect-current-group)
4508   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
4509   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
4510   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
4511   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
4512   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
4513   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
4514   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
4515   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
4516   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
4517   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
4518   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
4519   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
4520   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
4521   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
4522   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
4523   (define-key gnus-summary-mode-map "V" 'gnus-version)
4524   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
4525   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
4526   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
4527   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
4528   (define-key gnus-summary-mode-map
4529     (if gnus-xemacs [button2] [mouse-2]) 'gnus-mouse-pick-article)
4530   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
4531   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
4532   (define-key gnus-summary-mode-map "x" 'gnus-summary-remove-lines-marked-as-read)
4533 ; (define-key gnus-summary-mode-map "X" 'gnus-summary-remove-lines-marked-with)
4534   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
4535   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
4536   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
4537 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
4538   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
4539   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu-view)
4540   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
4541
4542
4543   ;; Sort of orthogonal keymap
4544   (define-prefix-command 'gnus-summary-mark-map)
4545   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
4546   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
4547   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
4548   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
4549   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
4550   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
4551   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
4552   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
4553   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
4554   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
4555   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
4556   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
4557   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
4558   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
4559   (define-key gnus-summary-mark-map "\M-r" 'gnus-summary-remove-lines-marked-as-read)
4560   (define-key gnus-summary-mark-map "\M-\C-r" 'gnus-summary-remove-lines-marked-with)
4561   (define-key gnus-summary-mark-map "D" 'gnus-summary-show-all-dormant)
4562   (define-key gnus-summary-mark-map "\M-D" 'gnus-summary-hide-all-dormant)
4563   (define-key gnus-summary-mark-map "S" 'gnus-summary-show-all-expunged)
4564   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
4565   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
4566   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
4567   (define-key gnus-summary-mark-map "k" 'gnus-summary-kill-same-subject-and-select)
4568   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
4569
4570   (define-prefix-command 'gnus-summary-mscore-map)
4571   (define-key gnus-summary-mark-map "s" 'gnus-summary-mscore-map)
4572   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
4573   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
4574   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
4575   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
4576
4577   (define-prefix-command 'gnus-summary-process-map)
4578   (define-key gnus-summary-mark-map "p" 'gnus-summary-process-map)
4579   (define-key gnus-summary-process-map "p" 'gnus-summary-mark-as-processable)
4580   (define-key gnus-summary-process-map "u" 'gnus-summary-unmark-as-processable)
4581   (define-key gnus-summary-process-map "U" 'gnus-summary-unmark-all-processable)
4582   (define-key gnus-summary-process-map "s" 'gnus-uu-mark-series)
4583   (define-key gnus-summary-process-map "r" 'gnus-uu-mark-region)
4584   (define-key gnus-summary-process-map "R" 'gnus-uu-mark-by-regexp)
4585   (define-key gnus-summary-process-map "t" 'gnus-uu-mark-thread)
4586   (define-key gnus-summary-process-map "a" 'gnus-uu-mark-all)
4587   (define-key gnus-summary-process-map "S" 'gnus-uu-mark-sparse)
4588   
4589
4590   (define-prefix-command 'gnus-summary-send-map)
4591   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
4592   (define-key gnus-summary-send-map "p" 'gnus-summary-post-news)
4593   (define-key gnus-summary-send-map "f" 'gnus-summary-followup)
4594   (define-key gnus-summary-send-map "F" 'gnus-summary-followup-with-original)
4595   (define-key gnus-summary-send-map "b" 'gnus-summary-followup-and-reply)
4596   (define-key gnus-summary-send-map "B" 'gnus-summary-followup-and-reply-with-original)
4597   (define-key gnus-summary-send-map "c" 'gnus-summary-cancel-article)
4598   (define-key gnus-summary-send-map "s" 'gnus-summary-supersede-article)
4599   (define-key gnus-summary-send-map "r" 'gnus-summary-reply)
4600   (define-key gnus-summary-send-map "R" 'gnus-summary-reply-with-original)
4601   (define-key gnus-summary-send-map "\C-f" 'gnus-summary-mail-forward)
4602   (define-key gnus-summary-send-map "m" 'gnus-summary-mail-other-window)
4603   (define-key gnus-summary-send-map "u" 'gnus-uu-post-news)
4604   (define-key gnus-summary-send-map "\M-f" 'gnus-uu-digest-and-forward)
4605
4606   
4607   (define-prefix-command 'gnus-summary-goto-map)
4608   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
4609   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
4610   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
4611   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
4612   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
4613   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
4614   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
4615   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
4616   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
4617   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
4618   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
4619   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
4620   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
4621   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
4622
4623
4624   (define-prefix-command 'gnus-summary-thread-map)
4625   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
4626   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
4627   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
4628   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
4629   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
4630   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
4631   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
4632   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
4633   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
4634   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
4635   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
4636   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
4637   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
4638   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
4639
4640   
4641   (define-prefix-command 'gnus-summary-exit-map)
4642   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
4643   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
4644   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
4645   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
4646   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
4647   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
4648   (define-key gnus-summary-exit-map "n" 'gnus-summary-catchup-and-goto-next-group)
4649
4650
4651   (define-prefix-command 'gnus-summary-article-map)
4652   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
4653   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
4654   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
4655   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
4656   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
4657   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
4658   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
4659   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
4660   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
4661   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
4662   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
4663   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
4664   (define-key gnus-summary-article-map "w" 'gnus-summary-stop-page-breaking)
4665   (define-key gnus-summary-article-map "c" 'gnus-summary-caesar-message)
4666   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
4667   (define-key gnus-summary-article-map "t" 'gnus-summary-toggle-header)
4668   (define-key gnus-summary-article-map "hh" 'gnus-article-hide-headers)
4669   (define-key gnus-summary-article-map "hs" 'gnus-article-hide-signature)
4670   (define-key gnus-summary-article-map "hc" 'gnus-article-hide-citation)
4671   (define-key gnus-summary-article-map "ho" 'gnus-article-treat-overstrike)
4672   (define-key gnus-summary-article-map "hw" 'gnus-article-word-wrap)
4673   (define-key gnus-summary-article-map "hd" 'gnus-article-remove-cr)
4674   (define-key gnus-summary-article-map "hq" 'gnus-article-de-quoted-unreadable)
4675   (define-key gnus-summary-article-map "hf" 'gnus-article-display-x-face)
4676   (define-key gnus-summary-article-map "m" 'gnus-summary-toggle-mime)
4677   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
4678
4679
4680   (define-prefix-command 'gnus-summary-extract-map)
4681   (define-key gnus-summary-mode-map "X" 'gnus-summary-extract-map)
4682 ;  (define-key gnus-summary-extract-map "x" 'gnus-summary-extract-any)
4683 ;  (define-key gnus-summary-extract-map "m" 'gnus-summary-extract-mime)
4684   (define-key gnus-summary-extract-map "u" 'gnus-uu-decode-uu)
4685   (define-key gnus-summary-extract-map "U" 'gnus-uu-decode-uu-and-save)
4686   (define-key gnus-summary-extract-map "s" 'gnus-uu-decode-unshar)
4687   (define-key gnus-summary-extract-map "S" 'gnus-uu-decode-unshar-and-save)
4688   (define-key gnus-summary-extract-map "o" 'gnus-uu-decode-save)
4689   (define-key gnus-summary-extract-map "O" 'gnus-uu-decode-save)
4690   (define-key gnus-summary-extract-map "b" 'gnus-uu-decode-binhex)
4691   (define-key gnus-summary-extract-map "B" 'gnus-uu-decode-binhex)
4692   (define-key gnus-summary-extract-map "p" 'gnus-uu-decode-postscript)
4693   (define-key gnus-summary-extract-map "P" 'gnus-uu-decode-postscript-and-save)
4694
4695
4696   (define-prefix-command 'gnus-summary-extract-view-map)
4697   (define-key gnus-summary-extract-map "v" 'gnus-summary-extract-view-map)
4698   (define-key gnus-summary-extract-view-map "u" 'gnus-uu-decode-uu-view)
4699   (define-key gnus-summary-extract-view-map "U" 'gnus-uu-decode-uu-and-save-view)
4700   (define-key gnus-summary-extract-view-map "s" 'gnus-uu-decode-unshar-view)
4701   (define-key gnus-summary-extract-view-map "S" 'gnus-uu-decode-unshar-and-save-view)
4702   (define-key gnus-summary-extract-view-map "o" 'gnus-uu-decode-save-view)
4703   (define-key gnus-summary-extract-view-map "O" 'gnus-uu-decode-save-view)
4704   (define-key gnus-summary-extract-view-map "b" 'gnus-uu-decode-binhex-view)
4705   (define-key gnus-summary-extract-view-map "B" 'gnus-uu-decode-binhex-view)
4706   (define-key gnus-summary-extract-view-map "p" 'gnus-uu-decode-postscript-view)
4707   (define-key gnus-summary-extract-view-map "P" 'gnus-uu-decode-postscript-and-save-view)
4708   
4709   
4710   (define-prefix-command 'gnus-summary-various-map)
4711   (define-key gnus-summary-mode-map "V" 'gnus-summary-various-map)
4712   (define-key gnus-summary-various-map "u" 'gnus-summary-universal-argument)
4713   (define-key gnus-summary-various-map "\C-s" 'gnus-summary-search-article-forward)
4714   (define-key gnus-summary-various-map "\C-r" 'gnus-summary-search-article-backward)
4715   (define-key gnus-summary-various-map "r" 'gnus-summary-refer-article)
4716   (define-key gnus-summary-various-map "&" 'gnus-summary-execute-command)
4717   (define-key gnus-summary-various-map "T" 'gnus-summary-toggle-truncation)
4718   (define-key gnus-summary-various-map "e" 'gnus-summary-expand-window)
4719   (define-key gnus-summary-various-map "S" 'gnus-summary-reselect-current-group)
4720   (define-key gnus-summary-various-map "g" 'gnus-summary-rescan-group)
4721   (define-key gnus-summary-various-map "V" 'gnus-version)
4722   (define-key gnus-summary-various-map "f" 'gnus-summary-fetch-faq)
4723   (define-key gnus-summary-various-map "d" 'gnus-summary-describe-group)
4724   (define-key gnus-summary-various-map "?" 'gnus-summary-describe-briefly)
4725   (define-key gnus-summary-various-map "i" 'gnus-info-find-node)
4726   (define-key gnus-summary-various-map "D" 'gnus-summary-enter-digest-group)
4727   (define-key gnus-summary-various-map "k" 'gnus-summary-edit-local-kill)
4728   (define-key gnus-summary-various-map "K" 'gnus-summary-edit-global-kill)
4729
4730   (define-prefix-command 'gnus-summary-vsave-map)
4731   (define-key gnus-summary-various-map "o" 'gnus-summary-vsave-map)
4732   (define-key gnus-summary-vsave-map "o" 'gnus-summary-save-article)
4733   (define-key gnus-summary-vsave-map "m" 'gnus-summary-save-article-mail)
4734   (define-key gnus-summary-vsave-map "r" 'gnus-summary-save-article-rmail)
4735   (define-key gnus-summary-vsave-map "f" 'gnus-summary-save-article-file)
4736   (define-key gnus-summary-vsave-map "h" 'gnus-summary-save-article-folder)
4737   (define-key gnus-summary-vsave-map "p" 'gnus-summary-pipe-output)
4738
4739   (define-prefix-command 'gnus-summary-score-map)
4740   (define-key gnus-summary-various-map "S" 'gnus-summary-score-map)
4741   (define-key gnus-summary-score-map "s" 'gnus-summary-set-score)
4742   (define-key gnus-summary-score-map "c" 'gnus-score-change-score-file)
4743   (define-key gnus-summary-score-map "m" 'gnus-score-set-mark-below)
4744   (define-key gnus-summary-score-map "x" 'gnus-score-set-expunge-below)
4745   (define-key gnus-summary-score-map "e" 'gnus-score-edit-alist)
4746   (define-key gnus-summary-score-map "f" 'gnus-score-edit-file)
4747
4748   (define-prefix-command 'gnus-summary-sort-map)
4749   (define-key gnus-summary-various-map "s" 'gnus-summary-sort-map)
4750   (define-key gnus-summary-sort-map "n" 'gnus-summary-sort-by-number)
4751   (define-key gnus-summary-sort-map "a" 'gnus-summary-sort-by-author)
4752   (define-key gnus-summary-sort-map "s" 'gnus-summary-sort-by-subject)
4753   (define-key gnus-summary-sort-map "d" 'gnus-summary-sort-by-date)
4754   (define-key gnus-summary-sort-map "i" 'gnus-summary-sort-by-score)
4755
4756   (define-prefix-command 'gnus-summary-mgroup-map)
4757   (define-key gnus-summary-various-map "m" 'gnus-summary-mgroup-map)
4758   (define-key gnus-summary-mgroup-map "e" 'gnus-summary-expire-articles)
4759   (define-key gnus-summary-mgroup-map "\177" 'gnus-summary-delete-article)
4760   (define-key gnus-summary-mgroup-map "m" 'gnus-summary-move-article)
4761   (define-key gnus-summary-mgroup-map "r" 'gnus-summary-respool-article)
4762   (define-key gnus-summary-mgroup-map "w" 'gnus-summary-edit-article)
4763   (define-key gnus-summary-mgroup-map "c" 'gnus-summary-copy-article)
4764
4765
4766   (define-prefix-command 'gnus-summary-increase-map)
4767   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-map)
4768   (define-key gnus-summary-increase-map "i" 'gnus-summary-raise-same-subject-and-select)
4769   (define-key gnus-summary-increase-map "I" 'gnus-summary-raise-same-subject)
4770   (define-key gnus-summary-increase-map "\C-i" 'gnus-summary-raise-score)
4771
4772   (define-prefix-command 'gnus-summary-inc-subject-map)
4773   (define-key gnus-summary-increase-map "s" 'gnus-summary-inc-subject-map)
4774   (define-key gnus-summary-increase-map "S" 'gnus-summary-temporarily-raise-by-subject)
4775   (define-key gnus-summary-inc-subject-map "s" 'gnus-summary-temporarily-raise-by-subject)
4776   (define-key gnus-summary-inc-subject-map "S" 'gnus-summary-raise-by-subject)
4777   (define-key gnus-summary-inc-subject-map "t" 'gnus-summary-temporarily-raise-by-subject)
4778   (define-key gnus-summary-inc-subject-map "p" 'gnus-summary-raise-by-subject)
4779
4780   (define-prefix-command 'gnus-summary-inc-author-map)
4781   (define-key gnus-summary-increase-map "a" 'gnus-summary-inc-author-map)
4782   (define-key gnus-summary-increase-map "A" 'gnus-summary-temporarily-raise-by-author)
4783   (define-key gnus-summary-inc-author-map "a" 'gnus-summary-temporarily-raise-by-author)
4784   (define-key gnus-summary-inc-author-map "A" 'gnus-summary-raise-by-author)
4785   (define-key gnus-summary-inc-author-map "t" 'gnus-summary-temporarily-raise-by-author)
4786   (define-key gnus-summary-inc-author-map "p" 'gnus-summary-raise-by-author)
4787
4788   (define-prefix-command 'gnus-summary-inc-body-map)
4789   (define-key gnus-summary-increase-map "b" 'gnus-summary-inc-body-map)
4790   (define-key gnus-summary-increase-map "B" 'gnus-summary-temporarily-raise-by-body)
4791   (define-key gnus-summary-inc-body-map "b" 'gnus-summary-temporarily-raise-by-body)
4792   (define-key gnus-summary-inc-body-map "B" 'gnus-summary-raise-by-body)
4793   (define-key gnus-summary-inc-body-map "t" 'gnus-summary-temporarily-raise-by-body)
4794   (define-key gnus-summary-inc-body-map "p" 'gnus-summary-raise-by-body)
4795
4796   (define-prefix-command 'gnus-summary-inc-id-map)
4797   (define-key gnus-summary-increase-map "i" 'gnus-summary-inc-id-map)
4798   (define-key gnus-summary-increase-map "I" 'gnus-summary-temporarily-raise-by-id)
4799   (define-key gnus-summary-inc-id-map "i" 'gnus-summary-temporarily-raise-by-id)
4800   (define-key gnus-summary-inc-id-map "I" 'gnus-summary-raise-by-id)
4801   (define-key gnus-summary-inc-id-map "t" 'gnus-summary-temporarily-raise-by-id)
4802   (define-key gnus-summary-inc-id-map "p" 'gnus-summary-raise-by-id)
4803
4804   (define-prefix-command 'gnus-summary-inc-thread-map)
4805   (define-key gnus-summary-increase-map "t" 'gnus-summary-inc-thread-map)
4806   (define-key gnus-summary-increase-map "T" 'gnus-summary-temporarily-raise-by-thread)
4807   (define-key gnus-summary-inc-thread-map "t" 'gnus-summary-temporarily-raise-by-thread)
4808   (define-key gnus-summary-inc-thread-map "T" 'gnus-summary-raise-by-thread)
4809   (define-key gnus-summary-inc-thread-map "t" 'gnus-summary-temporarily-raise-by-thread)
4810   (define-key gnus-summary-inc-thread-map "p" 'gnus-summary-raise-by-thread)
4811
4812   (define-prefix-command 'gnus-summary-inc-xref-map)
4813   (define-key gnus-summary-increase-map "x" 'gnus-summary-inc-xref-map)
4814   (define-key gnus-summary-increase-map "X" 'gnus-summary-temporarily-raise-by-xref)
4815   (define-key gnus-summary-inc-xref-map "x" 'gnus-summary-temporarily-raise-by-xref)
4816   (define-key gnus-summary-inc-xref-map "X" 'gnus-summary-raise-by-xref)
4817   (define-key gnus-summary-inc-xref-map "t" 'gnus-summary-temporarily-raise-by-xref)
4818   (define-key gnus-summary-inc-xref-map "p" 'gnus-summary-raise-by-xref)
4819
4820   (define-prefix-command 'gnus-summary-inc-fol-map)
4821   (define-key gnus-summary-increase-map "f" 'gnus-summary-inc-fol-map)
4822   (define-key gnus-summary-increase-map "F" 'gnus-summary-raise-followups-to-author)
4823   (define-key gnus-summary-inc-fol-map "f" 'gnus-summary-raise-followups-to-author)
4824   (define-key gnus-summary-inc-fol-map "F" 'gnus-summary-raise-followups-to-author)
4825   (define-key gnus-summary-inc-fol-map "t" 'gnus-summary-raise-followups-to-author)
4826   (define-key gnus-summary-inc-fol-map "p" 'gnus-summary-raise-followups-to-author)
4827
4828   (define-prefix-command 'gnus-summary-lower-map)
4829   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-map)
4830   (define-key gnus-summary-lower-map "l" 'gnus-summary-lower-same-subject-and-select)
4831   (define-key gnus-summary-lower-map "L" 'gnus-summary-lower-same-subject)
4832   (define-key gnus-summary-lower-map "\C-l" 'gnus-summary-lower-score)
4833
4834   (define-prefix-command 'gnus-summary-low-subject-map)
4835   (define-key gnus-summary-lower-map "s" 'gnus-summary-low-subject-map)
4836   (define-key gnus-summary-lower-map "S" 'gnus-summary-temporarily-lower-by-subject)
4837   (define-key gnus-summary-low-subject-map "s" 'gnus-summary-temporarily-lower-by-subject)
4838   (define-key gnus-summary-low-subject-map "S" 'gnus-summary-lower-by-subject)
4839   (define-key gnus-summary-low-subject-map "t" 'gnus-summary-temporarily-lower-by-subject)
4840   (define-key gnus-summary-low-subject-map "p" 'gnus-summary-lower-by-subject)
4841
4842   (define-prefix-command 'gnus-summary-low-body-map)
4843   (define-key gnus-summary-lower-map "b" 'gnus-summary-low-body-map)
4844   (define-key gnus-summary-lower-map "B" 'gnus-summary-temporarily-lower-by-body)
4845   (define-key gnus-summary-low-body-map "b" 'gnus-summary-temporarily-lower-by-body)
4846   (define-key gnus-summary-low-body-map "B" 'gnus-summary-lower-by-body)
4847   (define-key gnus-summary-low-body-map "t" 'gnus-summary-temporarily-lower-by-body)
4848   (define-key gnus-summary-low-body-map "p" 'gnus-summary-lower-by-body)
4849
4850   (define-prefix-command 'gnus-summary-low-author-map)
4851   (define-key gnus-summary-lower-map "a" 'gnus-summary-low-author-map)
4852   (define-key gnus-summary-lower-map "A" 'gnus-summary-temporarily-lower-by-author)
4853   (define-key gnus-summary-low-author-map "a" 'gnus-summary-temporarily-lower-by-author)
4854   (define-key gnus-summary-low-author-map "A" 'gnus-summary-lower-by-author)
4855   (define-key gnus-summary-low-author-map "t" 'gnus-summary-temporarily-lower-by-author)
4856   (define-key gnus-summary-low-author-map "p" 'gnus-summary-lower-by-author)
4857
4858   (define-prefix-command 'gnus-summary-low-id-map)
4859   (define-key gnus-summary-lower-map "i" 'gnus-summary-low-id-map)
4860   (define-key gnus-summary-lower-map "I" 'gnus-summary-temporarily-lower-by-id)
4861   (define-key gnus-summary-low-id-map "i" 'gnus-summary-temporarily-lower-by-id)
4862   (define-key gnus-summary-low-id-map "I" 'gnus-summary-lower-by-id)
4863   (define-key gnus-summary-low-id-map "t" 'gnus-summary-temporarily-lower-by-id)
4864   (define-key gnus-summary-low-id-map "p" 'gnus-summary-lower-by-id)
4865
4866   (define-prefix-command 'gnus-summary-low-thread-map)
4867   (define-key gnus-summary-lower-map "t" 'gnus-summary-low-thread-map)
4868   (define-key gnus-summary-lower-map "T" 'gnus-summary-temporarily-lower-by-thread)
4869   (define-key gnus-summary-low-thread-map "t" 'gnus-summary-temporarily-lower-by-thread)
4870   (define-key gnus-summary-low-thread-map "T" 'gnus-summary-lower-by-thread)
4871   (define-key gnus-summary-low-thread-map "t" 'gnus-summary-temporarily-lower-by-thread)
4872   (define-key gnus-summary-low-thread-map "p" 'gnus-summary-lower-by-thread)
4873
4874   (define-prefix-command 'gnus-summary-low-xref-map)
4875   (define-key gnus-summary-lower-map "x" 'gnus-summary-low-xref-map)
4876   (define-key gnus-summary-lower-map "X" 'gnus-summary-temporarily-lower-by-xref)
4877   (define-key gnus-summary-low-xref-map "x" 'gnus-summary-temporarily-lower-by-xref)
4878   (define-key gnus-summary-low-xref-map "X" 'gnus-summary-lower-by-xref)
4879   (define-key gnus-summary-low-xref-map "t" 'gnus-summary-temporarily-lower-by-xref)
4880   (define-key gnus-summary-low-xref-map "p" 'gnus-summary-lower-by-xref)
4881
4882   (define-prefix-command 'gnus-summary-low-fol-map)
4883   (define-key gnus-summary-lower-map "f" 'gnus-summary-low-fol-map)
4884   (define-key gnus-summary-lower-map "F" 'gnus-summary-lower-followups-to-author)
4885   (define-key gnus-summary-low-fol-map "f" 'gnus-summary-lower-followups-to-author)
4886   (define-key gnus-summary-low-fol-map "F" 'gnus-summary-lower-followups-to-author)
4887   (define-key gnus-summary-low-fol-map "t" 'gnus-summary-lower-followups-to-author)
4888   (define-key gnus-summary-low-fol-map "p" 'gnus-summary-lower-followups-to-author)
4889   )
4890
4891
4892 \f
4893
4894 (defun gnus-summary-mode ()
4895   "Major mode for reading articles.
4896
4897 All normal editing commands are switched off.
4898 \\<gnus-summary-mode-map>
4899 Each line in this buffer represents one article.  To read an
4900 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
4901 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
4902 respectively.
4903
4904 You can also post articles and send mail from this buffer.  To 
4905 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
4906 of an article, type `\\[gnus-summary-reply]'.
4907
4908 There are approx. one gazillion commands you can execute in this 
4909 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
4910
4911 The following commands are available:
4912
4913 \\{gnus-summary-mode-map}"
4914   (interactive)
4915   (if gnus-visual (gnus-summary-make-menu-bar))
4916   (kill-all-local-variables)
4917   (let ((locals gnus-summary-local-variables))
4918     (while locals
4919       (if (consp (car locals))
4920           (progn
4921             (make-local-variable (car (car locals)))
4922             (set (car (car locals)) (eval (cdr (car locals)))))
4923         (make-local-variable (car locals))
4924         (set (car locals) nil))
4925       (setq locals (cdr locals))))
4926   (gnus-update-format-specifications)
4927   (setq mode-line-modified "-- ")
4928   (make-local-variable 'mode-line-format)
4929   (setq mode-line-format (copy-sequence mode-line-format))
4930   (and (equal (nth 3 mode-line-format) "   ")
4931        (setcar (nthcdr 3 mode-line-format) ""))
4932   (setq major-mode 'gnus-summary-mode)
4933   (setq mode-name "Summary")
4934   (make-local-variable 'minor-mode-alist)
4935   (gnus-set-mode-line 'summary)
4936   (use-local-map gnus-summary-mode-map)
4937   (buffer-disable-undo (current-buffer))
4938   (setq buffer-read-only t)             ;Disable modification
4939   (setq truncate-lines t)
4940   (setq selective-display t)
4941   (setq selective-display-ellipses t)   ;Display `...'
4942   (run-hooks 'gnus-summary-mode-hook))
4943
4944 (defun gnus-summary-clear-local-variables ()
4945   (let ((locals gnus-summary-local-variables))
4946     (while locals
4947       (if (consp (car locals))
4948           (set (car (car locals)) nil)
4949         (set (car locals) nil))
4950       (setq locals (cdr locals)))))
4951
4952 (defun gnus-mouse-pick-article (e)
4953   (interactive "e")
4954   (mouse-set-point e)
4955   (gnus-summary-next-page nil t))
4956
4957 (defun gnus-summary-setup-buffer (group)
4958   "Initialize summary buffer."
4959   (let ((buffer (concat "*Summary " group "*")))
4960     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
4961     (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
4962     (gnus-add-current-to-buffer-list)
4963     (gnus-summary-mode)))
4964
4965 (defun gnus-set-global-variables ()
4966   ;; Set the global equivalents of the summary buffer-local variables
4967   ;; to the latest values they had. These reflect the summary buffer
4968   ;; that was in action when the last article was fetched.
4969   (if (eq major-mode 'gnus-summary-mode) 
4970       (progn
4971         (setq gnus-summary-buffer (current-buffer))
4972         (let ((name gnus-newsgroup-name)
4973               (marked gnus-newsgroup-marked)
4974               (unread gnus-newsgroup-unreads)
4975               (headers gnus-current-headers))
4976           (save-excursion
4977             (set-buffer gnus-group-buffer)
4978             (setq gnus-newsgroup-name name)
4979             (setq gnus-newsgroup-marked marked)
4980             (setq gnus-newsgroup-unreads unread)
4981             (setq gnus-current-headers headers))))))
4982
4983 (defun gnus-summary-insert-dummy-line (sformat subject number)
4984   (if (not sformat) 
4985       (setq sformat gnus-summary-dummy-line-format-spec))
4986   (let (b)
4987     (beginning-of-line)
4988     (setq b (point))
4989     (insert (eval sformat))
4990     (add-text-properties
4991      b (1+ b)
4992      (list 'gnus-subject (gnus-simplify-subject-re subject)
4993            'gnus-number number
4994            'gnus-mark ?Z
4995            'gnus-thread 0))))
4996
4997 (defun gnus-summary-insert-line 
4998   (sformat header level current unread replied expirable subject-or-nil
4999            &optional dummy score)
5000   (or sformat (setq sformat gnus-summary-line-format-spec))
5001   (let* ((indentation 
5002           (make-string (* level gnus-thread-indent-level) ? ))
5003          (lines (or (header-lines header) 0))
5004          (score (or score gnus-summary-default-score 0))
5005          (score-char (if (or (null gnus-summary-default-score)
5006                              (= score gnus-summary-default-score)) ? 
5007                        (if (< score gnus-summary-default-score) 
5008                            gnus-score-below-mark gnus-score-over-mark)))
5009          (replied (if replied gnus-replied-mark ? ))
5010          (from (header-from header))
5011          (name-address (funcall gnus-extract-address-components from))
5012          (address (car (cdr name-address)))
5013          (name (or (car name-address) (car (cdr name-address))))
5014          (number (header-number header))
5015          (subject (header-subject header))
5016          (buffer-read-only nil)
5017          (opening-bracket (if dummy ?\< ?\[))
5018          (closing-bracket (if dummy ?\> ?\]))
5019          b)
5020     ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
5021     (if (not (numberp lines)) (setq lines 0))
5022     (beginning-of-line)
5023     (setq b (point))
5024     (insert (eval sformat))
5025     (add-text-properties
5026      b (1+ b)
5027      (list 'gnus-subject (gnus-simplify-subject-re subject)
5028            'gnus-number number
5029            'gnus-mark (or unread gnus-unread-mark ? )
5030            'gnus-thread level))))
5031
5032 (defun gnus-summary-update-line (&optional dont-update)
5033   ;; Update summary line after change.
5034   (or (not gnus-summary-default-score)
5035       gnus-summary-inhibit-highlight
5036       (let ((gnus-summary-inhibit-highlight t)
5037             (article (gnus-summary-article-number)))
5038         (progn
5039           (or dont-update
5040               (if (and gnus-summary-mark-below
5041                        (< (gnus-summary-article-score)
5042                           gnus-summary-mark-below))
5043                   (and (not (memq article gnus-newsgroup-marked))
5044                        (not (memq article gnus-newsgroup-dormant))
5045                        (memq article gnus-newsgroup-unreads)
5046                        (gnus-summary-mark-article nil gnus-low-score-mark))
5047                 (and (eq (gnus-summary-article-mark) gnus-low-score-mark)
5048                      (gnus-summary-mark-article nil gnus-unread-mark))))
5049           (and gnus-visual
5050                (run-hooks 'gnus-visual-summary-update-hook))))))
5051
5052 (defun gnus-summary-update-lines ()
5053   ;; Rehighlight summary buffer according to `gnus-summary-highlight'.
5054   (and (save-excursion
5055          (set-buffer gnus-summary-buffer)
5056          (goto-char (point-min))
5057          (while (not (eobp))
5058            (gnus-summary-update-line)
5059            (forward-line 1)))))
5060
5061 (defun gnus-summary-number-of-articles-in-thread (thread)
5062   ;; Sum up all elements (and sub-elements) in a list.
5063   (if (listp thread) 
5064       (apply '+ (mapcar 'gnus-summary-number-of-articles-in-thread thread))
5065     1))
5066
5067 (defun gnus-summary-read-group (group &optional show-all no-article kill-buffer)
5068   "Start reading news in newsgroup GROUP.
5069 If SHOW-ALL is non-nil, already read articles are also listed.
5070 If NO-ARTICLE is non-nil, no article is selected initially."
5071   (message "Retrieving newsgroup: %s..." group)
5072   (gnus-summary-setup-buffer group)
5073   (if (gnus-select-newsgroup group show-all)
5074       (progn
5075         (gnus-set-global-variables)
5076         ;; Save the active value in effect when the group was entered.
5077         (setq gnus-newsgroup-active 
5078               (gnus-copy-sequence
5079                (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
5080         ;; You can change the subjects in this hook.
5081         (run-hooks 'gnus-select-group-hook)
5082         ;; Do score processing.
5083         (gnus-score-headers)
5084         ;; Update the format specifiers.
5085         (gnus-update-format-specifications)
5086         ;; Generate the summary buffer.
5087         (gnus-summary-prepare)
5088         (if (zerop (buffer-size))
5089             (cond (gnus-newsgroup-dormant
5090                    (gnus-summary-show-all-dormant))
5091                   ((and gnus-newsgroup-scored show-all)
5092                    (gnus-summary-show-all-expunged))))
5093         ;; Function `gnus-apply-kill-file' must be called in this hook.
5094         (run-hooks 'gnus-apply-kill-hook)
5095         (if (zerop (buffer-size))
5096             (progn
5097               ;; This newsgroup is empty.
5098               (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
5099               (message "No unread news")
5100               (and kill-buffer
5101                    (get-buffer kill-buffer)
5102                    (buffer-name (get-buffer kill-buffer))
5103                    (kill-buffer kill-buffer)))
5104           (save-excursion
5105             (if kill-buffer
5106                 (let ((gnus-summary-buffer kill-buffer))
5107                   (gnus-configure-windows 'newsgroups t))))
5108           ;; Hide conversation thread subtrees.  We cannot do this in
5109           ;; gnus-summary-prepare-hook since kill processing may not
5110           ;; work with hidden articles.
5111           (and gnus-show-threads
5112                gnus-thread-hide-subtree
5113                (gnus-summary-hide-all-threads))
5114           ;; Show first unread article if requested.
5115           (goto-char (point-min))
5116           (if (and (not no-article)
5117                    gnus-auto-select-first
5118                    (gnus-summary-first-unread-article))
5119               (gnus-configure-windows 'article)
5120             (gnus-configure-windows 'summary))
5121           (pop-to-buffer gnus-summary-buffer)
5122           (gnus-set-mode-line 'summary)
5123           (gnus-summary-position-cursor)
5124           (if (and kill-buffer
5125                    (get-buffer kill-buffer)
5126                    (buffer-name (get-buffer kill-buffer)))
5127               (kill-buffer kill-buffer))))
5128     ;; Cannot select newsgroup GROUP.
5129     (message "Couldn't select newsgroup")
5130     (and (eq major-mode 'gnus-summary-mode)
5131          (kill-buffer (current-buffer)))
5132     (switch-to-buffer gnus-group-buffer)
5133     (gnus-group-next-unread-group 1)))
5134
5135 (defun gnus-summary-prepare ()
5136   "Prepare summary list of current newsgroup in summary buffer."
5137   (let ((buffer-read-only nil))
5138     (erase-buffer)
5139     (gnus-summary-prepare-threads 
5140      (if gnus-show-threads
5141          (gnus-gather-threads 
5142           (gnus-sort-threads 
5143            (if gnus-summary-expunge-below
5144                (gnus-make-threads-and-expunge)
5145              (gnus-make-threads))))
5146        gnus-newsgroup-headers)
5147      0 nil nil t)
5148     ;; Erase header retrieval message.
5149     (gnus-summary-update-lines)
5150     (message "")
5151     ;; Call hooks for modifying summary buffer.
5152     ;; Suggested by sven@tde.LTH.Se (Sven Mattisson).
5153     (goto-char (point-min))
5154     (run-hooks 'gnus-summary-prepare-hook)))
5155
5156 (defun gnus-gather-threads (threads)
5157   "Gather threads that have lost their roots."
5158   (if (not gnus-summary-make-false-root)
5159       threads 
5160     (let ((hashtb (gnus-make-hashtable 1023))
5161           (prev threads)
5162           (result threads)
5163           thread subject hthread unre-subject whole-subject)
5164       (while threads
5165         (setq subject (header-subject (car (car threads)))
5166               whole-subject subject)
5167         (and gnus-summary-gather-subject-limit
5168              (or (and (numberp gnus-summary-gather-subject-limit)
5169                       (> (length subject) gnus-summary-gather-subject-limit)
5170                       (setq subject
5171                             (substring subject 0 
5172                                        gnus-summary-gather-subject-limit)))
5173                  (and (eq 'fuzzy gnus-summary-gather-subject-limit)
5174                       (setq subject (gnus-simplify-subject-fuzzy subject)))))
5175         (if (setq hthread 
5176                   (gnus-gethash 
5177                    (setq unre-subject (gnus-simplify-subject-re subject))
5178                    hashtb))
5179             (progn
5180               (or (stringp (car (car hthread)))
5181                   (setcar hthread (list whole-subject (car hthread))))
5182               (setcdr (car hthread) (nconc (cdr (car hthread)) 
5183                                            (list (car threads))))
5184               (setcdr prev (cdr threads))
5185               (setq threads prev))
5186           (gnus-sethash unre-subject threads hashtb))
5187         (setq prev threads)
5188         (setq threads (cdr threads)))
5189       result)))
5190
5191 (defun gnus-make-threads ()
5192   ;; This function takes the dependencies already made by 
5193   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5194   ;; through the dependecies in the hash table and finds all the
5195   ;; roots. Roots do not refer back to any valid articles.
5196   (let (roots)
5197     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5198          (gnus-build-old-threads))
5199     (mapatoms
5200      (lambda (refs)
5201        (if (not (car (symbol-value refs)))
5202            (setq roots (append (cdr (symbol-value refs)) roots))
5203          ;; Ok, these refer back to valid articles, but if
5204          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5205          ;; the root has the same subject as its children. The children
5206          ;; that do not are made into roots and removed from the list
5207          ;; of children. 
5208          (or gnus-thread-ignore-subject
5209              (let* ((prev (symbol-value refs))
5210                     (subject (gnus-simplify-subject-re 
5211                               (header-subject (car prev))))
5212                     (headers (cdr prev)))
5213                (while headers
5214                  (if (not (string= subject
5215                                    (gnus-simplify-subject-re 
5216                                     (header-subject (car headers)))))
5217                      (progn
5218                        (setq roots (cons (car headers) roots))
5219                        (setcdr prev (cdr headers)))
5220                    (setq prev headers))
5221                  (setq headers (cdr headers)))))))
5222      gnus-newsgroup-dependencies)
5223     
5224     (mapcar 'gnus-trim-thread
5225             (apply 'append
5226                    (mapcar 'gnus-cut-thread
5227                            (mapcar 'gnus-make-sub-thread roots))))))
5228   
5229 (defun gnus-make-threads-and-expunge ()
5230   ;; This function takes the dependencies already made by 
5231   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5232   ;; through the dependecies in the hash table and finds all the
5233   ;; roots. Roots do not refer back to any valid articles.
5234   (let (roots)
5235     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5236          (gnus-build-old-threads))
5237     (mapatoms
5238      (lambda (refs)
5239        (if (not (car (symbol-value refs)))
5240            (if (and gnus-summary-expunge-below
5241                     (not gnus-fetch-old-headers))
5242                (let ((headers (cdr (symbol-value refs))))
5243                  (while headers
5244                   (if (not (< (or (cdr (assq (header-number (car headers))
5245                                              gnus-newsgroup-scored))
5246                                   gnus-summary-default-score 0)
5247                               gnus-summary-expunge-below))
5248                       (setq roots (cons (car headers) roots))
5249                     (setq gnus-newsgroup-unreads
5250                           (delq (header-number (car headers))
5251                                 gnus-newsgroup-unreads)))
5252                   (setq headers (cdr headers))))
5253              (setq roots (append (cdr (symbol-value refs)) roots)))
5254          ;; Ok, these refer back to valid articles, but if
5255          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5256          ;; the root has the same subject as its children. The children
5257          ;; that do not are made into roots and removed from the list
5258          ;; of children. 
5259          (or gnus-thread-ignore-subject
5260              (let* ((prev (symbol-value refs))
5261                     (subject (gnus-simplify-subject-re 
5262                               (header-subject (car prev))))
5263                     (headers (cdr prev)))
5264                (while headers
5265                  (if (not (string= subject
5266                                    (gnus-simplify-subject-re 
5267                                     (header-subject (car headers)))))
5268                      (progn
5269                        (setq roots (cons (car headers) roots))
5270                        (setcdr prev (cdr headers)))
5271                    (setq prev headers))
5272                  (setq headers (cdr headers)))))
5273          (and gnus-summary-expunge-below
5274               (not gnus-fetch-old-headers)
5275               (let* ((prev (symbol-value refs))
5276                      (headers (cdr prev))
5277                      id)
5278                 (while headers
5279                   (if (not (< (or (cdr (assq (header-number (car headers))
5280                                              gnus-newsgroup-scored))
5281                                   gnus-summary-default-score 0)
5282                               gnus-summary-expunge-below))
5283                       (setq prev (cdr prev))
5284                     (setq gnus-newsgroup-unreads 
5285                           (delq (header-number (car headers))
5286                                 gnus-newsgroup-unreads))
5287                     (setcdr prev (cdr headers))
5288                     (setq id (gnus-gethash (header-id (car headers))
5289                                            gnus-newsgroup-dependencies))
5290                     (let ((h (cdr id)))
5291                       (while h
5292                         (if (not (< (or (cdr (assq (header-number (car h))
5293                                                    gnus-newsgroup-scored))
5294                                         gnus-summary-default-score 0)
5295                                     gnus-summary-expunge-below))
5296                             (setq roots (cons (car h) roots)))
5297                         (setq h (cdr h)))))
5298                   (setq headers (cdr headers)))))))
5299      gnus-newsgroup-dependencies)
5300     
5301     (mapcar 'gnus-trim-thread
5302             (apply 'append
5303                    (mapcar 'gnus-cut-thread
5304                            (mapcar 'gnus-make-sub-thread roots))))))
5305   
5306 (defun gnus-cut-thread (thread)
5307   ;; Remove leaf dormant or ancient articles from THREAD.
5308   (let ((head (car thread))
5309         (tail (apply 'append (mapcar 'gnus-cut-thread (cdr thread)))))
5310     (if (and (null tail)
5311              (let ((number (header-number head)))
5312                (or (memq number gnus-newsgroup-ancient)
5313                    (memq number gnus-newsgroup-dormant)
5314                    (and gnus-summary-expunge-below
5315                         (eq gnus-fetch-old-headers 'some)
5316                         (< (or (cdr (assq number gnus-newsgroup-scored))
5317                                gnus-summary-default-score 0)
5318                            gnus-summary-expunge-below)
5319                         (progn
5320                           (setq gnus-newsgroup-unreads
5321                                 (delq number gnus-newsgroup-unreads))
5322                           t)))))
5323         nil
5324       (list (cons head tail)))))
5325
5326 (defun gnus-trim-thread (thread)
5327   ;; Remove root ancient articles with only one child from THREAD.
5328   (if (and (eq gnus-fetch-old-headers 'some)
5329            (memq (header-number (car thread)) gnus-newsgroup-ancient)
5330            (= (length thread) 2))
5331       (gnus-trim-thread (nth 1 thread))
5332     thread))
5333
5334 (defun gnus-make-sub-thread (root)
5335   ;; This function makes a sub-tree for a node in the tree.
5336   (let ((children (reverse (cdr (gnus-gethash (downcase (header-id root))
5337                                               gnus-newsgroup-dependencies)))))
5338     (cons root (mapcar 'gnus-make-sub-thread children))))
5339
5340 (defun gnus-build-old-threads ()
5341   ;; Look at all the articles that refer back to old articles, and
5342   ;; fetch the headers for the articles that aren't there. This will
5343   ;; build complete threads - if the roots haven't been expired by the
5344   ;; server, that is.
5345   (let (id heads)
5346     (mapatoms
5347      (lambda (refs)
5348        (if (not (car (symbol-value refs)))
5349            (progn
5350              (setq heads (cdr (symbol-value refs)))
5351              (while heads
5352                (if (not (memq (header-number (car heads))
5353                               gnus-newsgroup-dormant))
5354                    (progn
5355                      (setq id (symbol-name refs))
5356                      (while (and (setq id (gnus-build-get-header id))
5357                                  (not (car (gnus-gethash 
5358                                             id gnus-newsgroup-dependencies)))))
5359                      (setq heads nil))
5360                  (setq heads (cdr heads)))))))
5361      gnus-newsgroup-dependencies)))
5362
5363 (defun gnus-build-get-header (id)
5364   ;; Look through the buffer of NOV lines and find the header to
5365   ;; ID. Enter this line into the dependencies hash table, and return
5366   ;; the id of the parent article (if any).
5367   (let ((deps gnus-newsgroup-dependencies)
5368         found header)
5369     (prog1
5370         (save-excursion
5371           (set-buffer nntp-server-buffer)
5372           (goto-char (point-min))
5373           (while (and (not found) (search-forward id nil t))
5374             (beginning-of-line)
5375             (setq found (looking-at (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
5376                                             (regexp-quote id))))
5377             (or found (beginning-of-line 2)))
5378           (if found
5379               (let (ref)
5380                 (beginning-of-line)
5381                 (and
5382                  (setq header (gnus-nov-parse-line 
5383                                (read (current-buffer)) deps))
5384                  (setq ref (header-references header))
5385                  (string-match "\\(<[^>]+>\\) *$" ref)
5386                  (substring ref (match-beginning 1) (match-end 1))))))
5387       (and header
5388            (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers)
5389                  gnus-newsgroup-ancient (cons (header-number header)
5390                                               gnus-newsgroup-ancient))))))
5391
5392 (defun gnus-sort-threads (threads)
5393   ;; Sort threads as specified in `gnus-thread-sort-functions'.
5394   (let ((fun gnus-thread-sort-functions))
5395     (while fun
5396       (setq threads (sort threads (car fun))
5397             fun (cdr fun))))
5398   threads)
5399
5400 (defun gnus-thread-header (thread)
5401   ;; Return header of first article in THREAD.
5402   (if (consp thread)
5403       (if (stringp (car thread))
5404           (car (car (cdr thread)))
5405         (car thread))
5406     thread))
5407
5408 (defun gnus-thread-sort-by-number (h1 h2)
5409   "Sort threads by root article number."
5410   (let ((h1 (gnus-thread-header h1))
5411         (h2 (gnus-thread-header h2)))
5412     (< (header-number h1) (header-number h2))))
5413
5414 (defun gnus-thread-sort-by-author (h1 h2)
5415   "Sort threads by root author."
5416   (let ((h1 (gnus-thread-header h1))
5417         (h2 (gnus-thread-header h2)))
5418     (string-lessp
5419      (let ((extract (funcall 
5420                      gnus-extract-address-components (header-from h1))))
5421        (or (car extract) (cdr extract)))
5422      (let ((extract (funcall
5423                      gnus-extract-address-components (header-from h2))))
5424        (or (car extract) (cdr extract))))))
5425
5426 (defun gnus-thread-sort-by-subject (h1 h2)
5427   "Sort threads by root subject."
5428   (let ((h1 (gnus-thread-header h1))
5429         (h2 (gnus-thread-header h2)))
5430     (string-lessp
5431      (downcase (gnus-simplify-subject (header-subject h1)))
5432      (downcase (gnus-simplify-subject (header-subject h2))))))
5433
5434 (defun gnus-thread-sort-by-date (h1 h2)
5435   "Sort threads by root article date."
5436   (let ((h1 (gnus-thread-header h1))
5437         (h2 (gnus-thread-header h2)))
5438     (string-lessp
5439      (gnus-sortable-date (header-date h1))
5440      (gnus-sortable-date (header-date h2)))))
5441
5442 (defun gnus-thread-sort-by-score (h1 h2)
5443   "Sort threads by root article score.
5444 Unscored articles will be counted as havin a score of zero."
5445   (let ((h1 (gnus-thread-header h1))
5446         (h2 (gnus-thread-header h2)))
5447     (let ((s1 (assq (header-number h1) gnus-newsgroup-scored))
5448           (s2 (assq (header-number h2) gnus-newsgroup-scored)))
5449       (> (or (cdr s1) gnus-summary-default-score 0)
5450          (or (cdr s2) gnus-summary-default-score 0)))))
5451
5452 (defun gnus-thread-sort-by-total-score (h1 h2)
5453   "Sort threads by the sum of all scores in the thread.
5454 Unscored articles will be counted as havin a score of zero."
5455   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
5456
5457 (defun gnus-thread-total-score (thread)
5458   ;;  This function find the total score of  THREAD.
5459   (if (consp thread)
5460       (if (stringp (car thread))
5461           (apply gnus-thread-score-function 0
5462                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
5463         (gnus-thread-total-score-1 thread))
5464     (gnus-thread-total-score-1 (list thread))))
5465
5466 (defun gnus-thread-total-score-1 (root)
5467   ;; This function find the total score of the thread below ROOT.
5468   (setq root (car root))
5469   (apply gnus-thread-score-function
5470          (or (cdr (assq (header-number root) gnus-newsgroup-scored))
5471              gnus-summary-default-score 0)
5472          (mapcar 'gnus-thread-total-score
5473                  (cdr (gnus-gethash (downcase (header-id root))
5474                                     gnus-newsgroup-dependencies)))))
5475
5476 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5477 (defvar gnus-tmp-prev-subject "")
5478
5479 ;; Basic ideas by Paul Dworkin <paul@media-lab.media.mit.edu>.
5480 (defun gnus-summary-prepare-threads 
5481   (threads level &optional not-child no-subject cull)
5482   "Prepare summary buffer from THREADS and indentation LEVEL.  
5483 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
5484 or a straight list of headers."
5485   (let (thread header number subject clevel)
5486     (while threads
5487       (setq thread (car threads)
5488             threads (cdr threads))
5489       ;; If `thread' is a cons, hierarchical threads are used.  If not,
5490       ;; `thread' is the header.
5491       (if (consp thread)
5492           (setq header (car thread))
5493         (setq header thread)
5494         (and cull
5495              (or (memq (setq number (header-number header))
5496                        gnus-newsgroup-dormant)
5497                  (and gnus-summary-expunge-below
5498                       (< (or (cdr (assq number gnus-newsgroup-scored))
5499                              gnus-summary-default-score 0)
5500                          gnus-summary-expunge-below)))
5501              (progn
5502                (setq header nil)
5503                (setq gnus-newsgroup-unreads 
5504                      (delq number gnus-newsgroup-unreads)))))
5505       (cond 
5506        ((stringp header)
5507         ;; The header is a dummy root.
5508         (cond ((eq gnus-summary-make-false-root 'adopt)
5509                ;; We let the first article adopt the rest.
5510                (gnus-summary-prepare-threads (list (car (cdr thread))) 0)
5511                (setq thread (cdr (cdr thread)))
5512                (while thread
5513                  (gnus-summary-prepare-threads (list (car thread)) 1 t)
5514                  (setq thread (cdr thread))))
5515               ((eq gnus-summary-make-false-root 'dummy)
5516                ;; We output a dummy root.
5517                (gnus-summary-insert-dummy-line 
5518                 nil header (header-number (car (car (cdr thread)))))
5519                (setq clevel 1))
5520               ((eq gnus-summary-make-false-root 'empty)
5521                ;; We print the articles with empty subject fields. 
5522                (gnus-summary-prepare-threads (list (car (cdr thread))) 0)
5523                (setq thread (cdr (cdr thread)))
5524                (while thread
5525                  (gnus-summary-prepare-threads 
5526                   (list (car thread)) 0 nil
5527                   (not (and (eq gnus-summary-gather-subject-limit 'fuzzy)
5528                             (not (string=  
5529                                   (gnus-simplify-subject-re 
5530                                    (header-subject (car (car thread))))
5531                                   (gnus-simplify-subject-re header))))))
5532                  (setq thread (cdr thread))))
5533               (t
5534                ;; We do not make a root for the gathered
5535                ;; sub-threads at all.  
5536                (setq clevel 0)))
5537         ;; Print the sub-threads.
5538         (and (consp thread) (cdr thread)
5539              (gnus-summary-prepare-threads (cdr thread) clevel)))
5540        ;; The header is a real article.
5541        (header
5542         (setq number (header-number header)
5543               subject (header-subject header))
5544         (gnus-summary-insert-line
5545          nil header level nil 
5546          (cond ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
5547                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
5548                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
5549                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
5550                (t gnus-ancient-mark))
5551          (memq number gnus-newsgroup-replied)
5552          (memq number gnus-newsgroup-expirable)
5553          (if no-subject 
5554              gnus-summary-same-subject
5555            (if (or (zerop level)
5556                    (and gnus-thread-ignore-subject
5557                         (not (string= 
5558                               (gnus-simplify-subject-re gnus-tmp-prev-subject)
5559                               (gnus-simplify-subject-re subject)))))
5560                subject
5561              gnus-summary-same-subject))
5562          not-child
5563          (cdr (assq number gnus-newsgroup-scored)))
5564         (setq gnus-tmp-prev-subject subject)
5565         ;; Recursively print subthreads.
5566         (and (consp thread) (cdr thread)
5567              (gnus-summary-prepare-threads (cdr thread) (1+ level))))))))
5568
5569 (defun gnus-select-newsgroup (group &optional read-all)
5570   "Select newsgroup GROUP.
5571 If READ-ALL is non-nil, all articles in the group are selected."
5572   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5573          (info (nth 2 entry))
5574          articles header-marks)
5575     (gnus-check-news-server
5576      (setq gnus-current-select-method (gnus-find-method-for-group group)))
5577
5578     (or (gnus-server-opened gnus-current-select-method)
5579         (gnus-open-server gnus-current-select-method)
5580         (error "Couldn't open server"))
5581     
5582     (or (and (eq (car entry) t)
5583              (gnus-activate-newsgroup (car info)))
5584         (gnus-request-group group t)
5585         (progn
5586           (kill-buffer (current-buffer))
5587           (error "Couldn't request group %s: %s" 
5588                  group (gnus-status-message group))))
5589
5590     (setq gnus-newsgroup-name group)
5591     (setq gnus-newsgroup-unselected nil)
5592     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5593
5594     (if (not (setq articles (gnus-articles-to-read group read-all)))
5595         nil
5596       ;; Init the dependencies hash table.
5597       (setq gnus-newsgroup-dependencies 
5598             (gnus-make-hashtable (length articles)))
5599       ;; Retrieve the headers and read them in.
5600       (setq gnus-newsgroup-headers 
5601             (if (eq 'nov (setq gnus-headers-retrieved-by
5602                                (gnus-retrieve-headers 
5603                                 (if gnus-fetch-old-headers 
5604                                     (cons 1 articles) articles) 
5605                                 gnus-newsgroup-name)))
5606                 (progn
5607                   (gnus-get-newsgroup-headers-xover articles))
5608               (gnus-get-newsgroup-headers)))
5609       ;; If we were to fetch old headers, but the backend didn't
5610       ;; support XOVER, then it is possible we fetched one article
5611       ;; that we shouldn't have. If that's the case, we pop it off the
5612       ;; list of headers.
5613       (and (not (eq gnus-headers-retrieved-by 'nov))
5614            gnus-fetch-old-headers
5615            gnus-newsgroup-headers
5616            (/= (header-number (car gnus-newsgroup-headers)) (car articles))
5617            (setq gnus-newsgroup-headers (cdr gnus-newsgroup-headers)))
5618       ;; Remove cancelled articles from the list of unread articles.
5619       (setq gnus-newsgroup-unreads
5620             (gnus-set-sorted-intersection 
5621              gnus-newsgroup-unreads
5622              (mapcar (lambda (headers) (header-number headers))
5623                      gnus-newsgroup-headers)))
5624       ;; Adjust and set lists of article marks.
5625       (and info
5626            (let (marked)
5627              (gnus-adjust-marked-articles info)
5628              (setq gnus-newsgroup-marked 
5629                    (cdr (assq 'tick (setq marked (nth 3 info)))))
5630              (setq gnus-newsgroup-replied (cdr (assq 'reply marked)))
5631              (setq gnus-newsgroup-expirable (cdr (assq 'expire marked)))
5632              (setq gnus-newsgroup-killed (cdr (assq 'killed marked)))
5633              (setq gnus-newsgroup-bookmarks (cdr (assq 'bookmark marked)))
5634              (setq gnus-newsgroup-dormant (cdr (assq 'dormant marked)))
5635              (setq gnus-newsgroup-scored (cdr (assq 'score marked)))
5636              (setq gnus-newsgroup-processable nil)))
5637       ;; Check whether auto-expire is to be done in this group.
5638       (setq gnus-newsgroup-auto-expire
5639             (or (and (stringp gnus-auto-expirable-newsgroups)
5640                      (string-match gnus-auto-expirable-newsgroups group))
5641                 (memq 'auto-expire gnus-current-select-method)))
5642       ;; First and last article in this newsgroup.
5643       (and gnus-newsgroup-headers
5644            (setq gnus-newsgroup-begin 
5645                  (header-number (car gnus-newsgroup-headers)))
5646            (setq gnus-newsgroup-end
5647                  (header-number (gnus-last-element gnus-newsgroup-headers))))
5648       (setq gnus-reffed-article-number -1)
5649       ;; GROUP is successfully selected.
5650       (or gnus-newsgroup-headers t))))
5651
5652 (defun gnus-articles-to-read (group read-all)
5653   ;; Find out what articles the user wants to read.
5654   (let* ((articles
5655           ;; Select all articles if `read-all' is non-nil, or if all the
5656           ;; unread articles are dormant articles.
5657           (if (or read-all
5658                   (= (length gnus-newsgroup-unreads) 
5659                      (length gnus-newsgroup-dormant)))
5660               (gnus-uncompress-range 
5661                (gnus-gethash group gnus-active-hashtb))
5662             gnus-newsgroup-unreads))
5663          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5664          (scored (length scored-list))
5665          (number (length articles))
5666          (marked (+ (length gnus-newsgroup-marked)
5667                     (length gnus-newsgroup-dormant)))
5668          (select
5669           (condition-case ()
5670               (cond ((and (or (<= scored marked)
5671                               (= scored number))
5672                           (numberp gnus-large-newsgroup)
5673                           (> number gnus-large-newsgroup))
5674                      (let ((input
5675                             (read-string
5676                              (format
5677                               "How many articles from %s (default %d): "
5678                               gnus-newsgroup-name number))))
5679                        (if (string-equal input "")
5680                            number input)))
5681                     ((and (> scored marked) (< scored number))
5682                      (let ((input
5683                             (read-string
5684                              (format 
5685                               "%s %s (%d scored, %d total): "
5686                               "How many articles from"
5687                               group scored number))))
5688                        (if (string-equal input "")
5689                            number input)))
5690                     (t number))
5691             (quit 0)))
5692          total-articles)
5693     (setq select (if (numberp select) select (string-to-number select)))
5694     (if (zerop select)
5695         ()
5696       (if (and (not (zerop scored)) (<= (abs select) scored))
5697           (progn
5698             (setq articles (sort scored-list '<))
5699             (setq number (length articles)))
5700         (setq articles (copy-sequence articles)))
5701
5702       (setq total-articles articles)
5703       
5704       (if (< (abs select) number)
5705           (if (< select 0) 
5706               ;; Select the N oldest articles.
5707               (setcdr (nthcdr (1- (abs select)) articles) nil)
5708             ;; Select the N most recent articles.
5709             (setq articles (nthcdr (- number select) articles))))
5710       (setq gnus-newsgroup-unselected
5711             (gnus-sorted-intersection
5712              gnus-newsgroup-unreads
5713              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
5714       articles)))
5715
5716 (defun gnus-killed-articles (killed articles)
5717   (let (out)
5718     (while articles
5719       (if (inline (gnus-member-of-range (car articles) killed))
5720           (setq out (cons (car articles) out)))
5721       (setq articles (cdr articles)))
5722     out))
5723
5724 (defun gnus-adjust-marked-articles (info &optional active)
5725   "Remove all marked articles that are no longer legal."
5726   (let ((marked-lists (nth 3 info))
5727         (active (or active (gnus-gethash (car info) gnus-active-hashtb)))
5728         marked m prev)
5729     ;; There are many types of marked articles.
5730     (while marked-lists
5731       (setq m (cdr (setq prev (car marked-lists))))
5732       (cond ((or (eq 'tick (car prev)) (eq 'dormant (car prev)))
5733              ;; Make sure that all ticked articles are a subset of the
5734              ;; unread/unselected articles.
5735              (while m
5736                (if (or (memq (car m) gnus-newsgroup-unreads)
5737                        (memq (car m) gnus-newsgroup-unselected))
5738                    (setq prev m)
5739                  (setcdr prev (cdr m)))
5740                (setq m (cdr m))))
5741             ((eq 'score (car prev))
5742              ;; Scored articles should be a subset of
5743              ;; unread/unselected articles. 
5744              (while m
5745                (if (or (memq (car (car m)) gnus-newsgroup-unreads)
5746                        (memq (car (car m)) gnus-newsgroup-unreads))
5747                    (setq prev m)
5748                  (setcdr prev (cdr m)))
5749                (setq m (cdr m))))
5750             ((eq 'bookmark (car prev))
5751              ;; Bookmarks should be a subset of active articles.
5752              (while m
5753                (if (< (car (car m)) (car active))
5754                    (setcdr prev (cdr m))
5755                  (setq prev m))
5756                (setq m (cdr m))))
5757             ((eq 'killed (car prev))
5758              ;; Articles that have been through the kill process are
5759              ;; to be a subset of active articles.
5760              (while (and m (< (or (and (numberp (car m)) (car m))
5761                                   (cdr (car m)))
5762                               (car active)))
5763                (setcdr prev (cdr m))
5764                (setq m (cdr m)))
5765              (if (and m (< (or (and (numberp (car m)) (car m))
5766                                (car (car m)))
5767                            (car active))) 
5768                  (setcar (if (numberp (car m)) m (car m)) (car active))))
5769             ((or (eq 'reply (car prev)) (eq 'expire (car prev)))
5770              ;; The replied and expirable articles have to be articles
5771              ;; that are active. 
5772              (while m
5773                (if (< (car m) (car active))
5774                    (setcdr prev (cdr m))
5775                  (setq prev m))
5776                (setq m (cdr m)))))
5777       (setq marked-lists (cdr marked-lists)))
5778     ;; Remove all lists that are empty.
5779     (setq marked-lists (nth 3 info))
5780     (if marked-lists
5781         (progn
5782           (while (= 1 (length (car marked-lists)))
5783             (setq marked-lists (cdr marked-lists)))
5784           (setq m (cdr (setq prev marked-lists)))
5785           (while m
5786             (if (= 1 (length (car m)))
5787                 (setcdr prev (cdr m))
5788               (setq prev m))
5789             (setq m (cdr m)))
5790           (setcar (nthcdr 3 info) marked-lists)))
5791     ;; Finally, if there are no marked lists at all left, and if there
5792     ;; are no elements after the lists in the info list, we just chop
5793     ;; the info list off before the marked lists.
5794     (and (null marked-lists) 
5795          (not (nthcdr 4 info))
5796          (setcdr (nthcdr 2 info) nil)))
5797   info)
5798
5799 (defun gnus-set-marked-articles 
5800   (info ticked replied expirable killed dormant bookmark score) 
5801   "Enter the various lists of marked articles into the newsgroup info list."
5802   (let (newmarked)
5803     (and ticked (setq newmarked (cons (cons 'tick ticked) nil)))
5804     (and replied (setq newmarked (cons (cons 'reply replied) newmarked)))
5805     (and expirable (setq newmarked (cons (cons 'expire expirable) 
5806                                          newmarked)))
5807     (and killed (setq newmarked (cons (cons 'killed killed) newmarked)))
5808     (and dormant (setq newmarked (cons (cons 'dormant dormant) newmarked)))
5809     (and bookmark (setq newmarked (cons (cons 'bookmark bookmark) 
5810                                         newmarked)))
5811     (and score (setq newmarked (cons (cons 'score score) newmarked)))
5812     (if (nthcdr 3 info)
5813         (if newmarked
5814             (setcar (nthcdr 3 info) newmarked)
5815           (if (not (nthcdr 4 info))
5816               (setcdr (nthcdr 2 info) nil)
5817             (setcar (nthcdr 3 info) nil)))
5818       (if newmarked
5819           (setcdr (nthcdr 2 info) (cons newmarked nil))))))
5820
5821 (defun gnus-add-marked-articles (group type articles &optional info force)
5822   ;; Add ARTICLES of TYPE to the info of GROUP.
5823   ;; If INFO is non-nil, use that info. If FORCE is non-nil, don't
5824   ;; add, but replace marked articles of TYPE with ARTICLES.
5825   (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
5826         marked m)
5827     (or (not info)
5828         (and (not (setq marked (nthcdr 3 info)))
5829              (setcdr (nthcdr 2 info) (list (list (cons type articles)))))
5830         (and (not (setq m (assq type (car marked))))
5831              (setcar marked (cons (cons type articles) (car marked))))
5832         (if force
5833             (setcdr m articles)
5834           (nconc m articles)))))
5835          
5836 (defun gnus-set-mode-line (where)
5837   "This function sets the mode line of the article or summary buffers.
5838 If WHERE is `summary', the summary mode line format will be used."
5839   (if (memq where gnus-updated-mode-lines)
5840       (let (mode-string)
5841         (save-excursion
5842           (set-buffer gnus-summary-buffer)
5843           (let* ((mformat (if (eq where 'article) 
5844                               gnus-article-mode-line-format-spec
5845                             gnus-summary-mode-line-format-spec))
5846                  (group-name gnus-newsgroup-name)
5847                  (article-number (or gnus-current-article 0))
5848                  (unread (- (length gnus-newsgroup-unreads)
5849                             (length gnus-newsgroup-dormant)))
5850                  (unread-and-unticked 
5851                   (- unread (length gnus-newsgroup-marked)))
5852                  (unselected (length gnus-newsgroup-unselected))
5853                  (unread-and-unselected
5854                   (cond ((and (zerop unread-and-unticked)
5855                               (zerop unselected)) "")
5856                         ((zerop unselected) 
5857                          (format "{%d more}" unread-and-unticked))
5858                         (t (format "{%d(+%d) more}"
5859                                    unread-and-unticked unselected))))
5860                  (subject
5861                   (if gnus-current-headers
5862                       (header-subject gnus-current-headers) ""))
5863                  (max-len (and gnus-mode-non-string-length
5864                                (- (frame-width) gnus-mode-non-string-length)))
5865                  header) ;; passed as argument to any user-format-funcs
5866             (setq mode-string (eval mformat))
5867             (and (numberp max-len)
5868                  (progn
5869                    (if (> (length mode-string) max-len)
5870                        (setq mode-string 
5871                              (concat (substring mode-string 0 (- max-len 3))
5872                                      "...")))
5873                    (setq mode-string (format (format "%%-%ds" max-len)
5874                                              mode-string))))))
5875         (setq mode-line-buffer-identification mode-string)
5876         (set-buffer-modified-p t))))
5877
5878 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5879   "Go through the HEADERS list and add all Xrefs to a hash table.
5880 The resulting hash table is returned, or nil if no Xrefs were found."
5881   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
5882          (prefix (if (and 
5883                       (gnus-group-foreign-p from-newsgroup)
5884                       (not (memq 'virtual 
5885                                  (assoc (symbol-name (car from-method))
5886                                         gnus-valid-select-methods))))
5887                      (gnus-group-real-prefix from-newsgroup)))
5888          (xref-hashtb (make-vector 63 0))
5889          start group entry number xrefs header)
5890     (while headers
5891       (setq header (car headers))
5892       (if (and (setq xrefs (header-xref header))
5893                (not (memq (header-number header) unreads)))
5894           (progn
5895             (setq start 0)
5896             (while (string-match "\\([^ ]+\\):\\([0-9]+\\)" xrefs start)
5897               (setq start (match-end 0))
5898               (setq group (concat prefix (substring xrefs (match-beginning 1) 
5899                                                     (match-end 1))))
5900               (setq number 
5901                     (string-to-int (substring xrefs (match-beginning 2) 
5902                                               (match-end 2))))
5903               (if (setq entry (gnus-gethash group xref-hashtb))
5904                   (setcdr entry (cons number (cdr entry)))
5905                 (gnus-sethash group (cons number nil) xref-hashtb)))))
5906       (setq headers (cdr headers)))
5907     (if start xref-hashtb nil)))
5908
5909 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads expirable)
5910   "Look through all the headers and mark the Xrefs as read."
5911   (let ((virtual (memq 'virtual 
5912                        (assoc (symbol-name (car (gnus-find-method-for-group 
5913                                                  from-newsgroup)))
5914                               gnus-valid-select-methods)))
5915         name entry read info xref-hashtb idlist active num range exps method
5916         nth4)
5917     (save-excursion
5918       (set-buffer gnus-group-buffer)
5919       (if (setq xref-hashtb 
5920                 (gnus-create-xref-hashtb from-newsgroup headers unreads))
5921           (mapatoms 
5922            (lambda (group)
5923              (if (string= from-newsgroup (setq name (symbol-name group)))
5924                  ()
5925                (setq idlist (symbol-value group))
5926                ;; Dead groups are not updated.
5927                (if (and (prog1 
5928                             (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5929                                   info (nth 2 entry))
5930                           (if (stringp (setq nth4 (nth 4 info)))
5931                               (setq nth4 (gnus-server-to-method nth4))))
5932                         ;; Only do the xrefs if the group has the same
5933                         ;; select method as the group we have just read.
5934                         (or (gnus-methods-equal-p 
5935                              nth4 (gnus-find-method-for-group from-newsgroup))
5936                             virtual
5937                             (equal nth4 
5938                                    (setq method (gnus-find-method-for-group 
5939                                                  from-newsgroup)))
5940                             (and (equal (car nth4) (car method))
5941                                  (equal (nth 1 nth4) (nth 1 method))))
5942                         gnus-use-cross-reference
5943                         (or (not (eq gnus-use-cross-reference t))
5944                             virtual
5945                             ;; Only do cross-references on subscribed
5946                             ;; groups, if that is what is wanted.  
5947                             (<= (nth 1 info) 5)))
5948                    (progn
5949                      (setq num 0)
5950                      ;; Set the new list of read articles in this group.
5951                      (setq active (gnus-gethash name gnus-active-hashtb))
5952                      ;; First peel off all illegal article numbers.
5953                      (if active
5954                          (let ((ids idlist)
5955                                (ticked (cdr (assq 'tick (nth 3 info))))
5956                                (dormant (cdr (assq 'dormant (nth 3 info))))
5957                                id)
5958                            (setq exps nil)
5959                            (while ids
5960                              (setq id (car ids))
5961                              (if (or (> id (cdr active))
5962                                      (< id (car active))
5963                                      (memq id ticked)
5964                                      (memq id dormant))
5965                                  (setq idlist (delq id idlist)))
5966                              (and (memq id expirable)
5967                                   (setq exps (cons id exps)))
5968                              (setq ids (cdr ids)))))
5969                      ;; Update expirable articles.
5970                      (gnus-add-marked-articles nil 'expirable exps info)
5971                      (and (null (nth 2 info))
5972                           (> (car active) 1)
5973                           (setcar (nthcdr 2 info) (cons 1 (1- (car active)))))
5974                      (setcar (nthcdr 2 info)
5975                              (setq range
5976                                    (gnus-add-to-range 
5977                                     (nth 2 info) 
5978                                     (setq idlist (sort idlist '<)))))
5979                      ;; Then we have to re-compute how many unread
5980                      ;; articles there are in this group.
5981                      (if active
5982                          (progn
5983                            (cond 
5984                             ((not range)
5985                              (setq num (- (1+ (cdr active)) (car active))))
5986                             ((not (listp (cdr range)))
5987                              (setq num (- (cdr active) (- (1+ (cdr range)) 
5988                                                           (car range)))))
5989                             (t
5990                              (while range
5991                                (if (numberp (car range))
5992                                    (setq num (1+ num))
5993                                  (setq num (+ num (- (1+ (cdr (car range)))
5994                                                      (car (car range))))))
5995                                (setq range (cdr range)))
5996                              (setq num (- (cdr active) num))))
5997                            ;; Update the number of unread articles.
5998                            (setcar 
5999                             entry 
6000                             (max 0 (- num 
6001                                       (length (cdr (assq 'tick (nth 3 info))))
6002                                       (length 
6003                                        (cdr (assq 'dormant (nth 3 info)))))))
6004                            ;; Update the group buffer.
6005                            (gnus-group-update-group name t)))))))
6006            xref-hashtb)))))
6007
6008 (defun gnus-methods-equal-p (m1 m2)
6009   (let ((m1 (or m1 gnus-select-method))
6010         (m2 (or m2 gnus-select-method)))
6011     (or (equal m1 m2)
6012         (and (eq (car m1) (car m2))
6013              (or (not (memq 'address (assoc (symbol-name (car m1))
6014                                             gnus-valid-select-methods)))
6015                  (equal (nth 1 m1) (nth 1 m2)))))))
6016
6017 (defsubst gnus-header-value ()
6018   (buffer-substring (match-end 0) (gnus-point-at-eol)))
6019
6020 (defun gnus-get-newsgroup-headers ()
6021   (setq gnus-article-internal-prepare-hook nil)
6022   (let ((cur nntp-server-buffer)
6023         (dependencies gnus-newsgroup-dependencies)
6024         (none-id 0)
6025         headers char article id dep end)
6026     (save-excursion
6027       (set-buffer nntp-server-buffer)
6028       (goto-char 1)
6029       ;; Search to the beginning of the next header. Error messages
6030       ;; do not begin with 2 or 3.
6031       (while (re-search-forward "^[23][0-9]+ " nil t)
6032         (let ((header (make-vector 9 nil))
6033               (c (following-char))
6034               (case-fold-search t)
6035               (p (point))
6036               from subject in-reply-to references ref)
6037           (setq id nil
6038                 ref nil
6039                 references nil
6040                 subject nil
6041                 from nil)
6042           (header-set-number header (setq article (read cur)))
6043           ;; This implementation of this function, with nine
6044           ;; search-forwards instead of the one re-search-forward and
6045           ;; a case (which basically was the old function) is actually
6046           ;; about twice as fast, even though it looks messier. You
6047           ;; can't have everything, I guess. Speed and elegance
6048           ;; doesn't always come hand in hand.
6049           (save-restriction
6050             (narrow-to-region (point) (save-excursion 
6051                                         (search-forward "\n.\n" nil t)))
6052             (if (search-forward "\nfrom: " nil t)
6053                 (header-set-from header (gnus-header-value))
6054               (header-set-from header "(nobody)"))
6055             (goto-char p)
6056             (if (search-forward "\nsubject: " nil t)
6057                 (header-set-subject header (gnus-header-value))
6058               (header-set-subject header "(none)"))
6059             (goto-char p)
6060             (and (search-forward "\nxref: " nil t)
6061                  (header-set-xref header (gnus-header-value)))
6062             (goto-char p)
6063             (and (search-forward "\nlines: " nil t)
6064                  (header-set-lines header (read cur)))
6065             (goto-char p)
6066             (and (search-forward "\ndate: " nil t)
6067                  (header-set-date header (gnus-header-value)))
6068             (goto-char p)
6069             (if (search-forward "\nmessage-id: " nil t)
6070                 (header-set-id header (setq id (gnus-header-value)))
6071               ;; If there was no message-id, we just fake one to make
6072               ;; subsequent routines simpler.
6073               (header-set-id 
6074                header 
6075                (setq id (concat "none+" (int-to-string 
6076                                          (setq none-id (1+ none-id)))))))
6077             (goto-char p)
6078             (if (search-forward "\nreferences: " nil t)
6079                 (progn
6080                   (header-set-references header (gnus-header-value))
6081                   (setq end (match-end 0))
6082                   (save-excursion
6083                     (setq ref 
6084                           (downcase
6085                            (buffer-substring
6086                             (progn 
6087                               (end-of-line)
6088                               (search-backward ">" end t)
6089                               (1+ (point)))
6090                             (progn
6091                               (search-backward "<" end t)
6092                               (point)))))))
6093               ;; Get the references from the in-reply-to header if there
6094               ;; ware no references and the in-reply-to header looks
6095               ;; promising. 
6096               (if (and (search-forward "\nin-reply-to: " nil t)
6097                        (setq in-reply-to (gnus-header-value))
6098                        (string-match "<[^>]+>" in-reply-to))
6099                   (progn
6100                     (header-set-references 
6101                      header 
6102                      (setq ref (substring in-reply-to (match-beginning 0)
6103                                           (match-end 0))))
6104                     (setq ref (downcase ref)))
6105                 (setq ref "none")))
6106             ;; We do some threading while we read the headers. The
6107             ;; message-id and the last reference are both entered into
6108             ;; the same hash table. Some tippy-toeing around has to be
6109             ;; done in case an article has arrived before the article
6110             ;; which it refers to.
6111             (if (boundp (setq dep (intern (downcase id) dependencies)))
6112                 (if (car (symbol-value dep))
6113                     ;; An article with this Message-ID has already
6114                     ;; been seen, so we ignore this one, except we add
6115                     ;; any additional Xrefs (in case the two articles
6116                     ;; came from different servers.
6117                     (progn
6118                       (header-set-xref 
6119                        (car (symbol-value dep))
6120                        (concat (or (header-xref (car (symbol-value dep))) "")
6121                                (or (header-xref header) "")))
6122                       (setq header nil))
6123                   (setcar (symbol-value dep) header))
6124               (set dep (list header)))
6125             (if header
6126                 (progn
6127                   (if (boundp (setq dep (intern ref dependencies)))
6128                       (setcdr (symbol-value dep) 
6129                               (cons header (cdr (symbol-value dep))))
6130                     (set dep (list nil header)))
6131                   (setq headers (cons header headers))))
6132             (goto-char (point-max))))))
6133     (nreverse headers)))
6134
6135 ;; The following macros and functions were written by Felix Lee
6136 ;; <flee@cse.psu.edu>. 
6137
6138 ;; This is almost 4x faster than (string-to-int (buffer-substring ... ))
6139 ;; primarily because of garbage collection.  -jwz
6140 (defmacro gnus-read-integer (&optional point move-p)
6141   (` ((, (if move-p 'progn 'save-excursion))
6142       (,@ (if point (list (list 'goto-char point))))
6143       (if (and (<= (following-char) ?9)
6144                (>= (following-char) ?0))
6145           (read (current-buffer))
6146         0))))
6147
6148 (defmacro gnus-nov-skip-field ()
6149   '(search-forward "\t" eol 'end))
6150
6151 (defmacro gnus-nov-field ()
6152   '(buffer-substring
6153     (point)
6154     (progn (gnus-nov-skip-field) (1- (point)))))
6155
6156 ;; Goes through the xover lines and returns a list of vectors
6157 (defun gnus-get-newsgroup-headers-xover (sequence)
6158   "Parse the news overview data in the server buffer, and return a
6159 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
6160   ;; Get the Xref when the users reads the articles since most/some
6161   ;; NNTP servers do not include Xrefs when using XOVER.
6162   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6163   (let ((cur nntp-server-buffer)
6164         (dependencies gnus-newsgroup-dependencies)
6165         (none 0)
6166         number headers header)
6167     (save-excursion
6168       (set-buffer nntp-server-buffer)
6169       (goto-char (point-min))
6170       (while (and sequence (not (eobp)))
6171         (setq number (read cur))
6172         (while (and sequence (< (car sequence) number))
6173           (setq sequence (cdr sequence)))
6174         (and sequence 
6175              (eq number (car sequence))
6176              (progn
6177                (setq sequence (cdr sequence))
6178                (if (setq header 
6179                          (inline (gnus-nov-parse-line number dependencies)))
6180                    (setq headers (cons header headers)))))
6181         (forward-line 1))
6182       (setq headers (nreverse headers)))
6183     headers))
6184
6185 (defun gnus-nov-parse-line (number dependencies)
6186   "Point has to be after the number on the beginning of the line."
6187   (let ((none 0)
6188         header (eol (gnus-point-at-eol)) ref id dep)
6189     (forward-char)
6190     ;; overview: [num subject from date id refs chars lines misc]
6191     (save-restriction
6192       (narrow-to-region (point) eol)
6193       (condition-case nil
6194           (setq header
6195                 (vector 
6196                  number                 ; number
6197                  (gnus-nov-field)       ; subject
6198                  (gnus-nov-field)       ; from
6199                  (gnus-nov-field)       ; date
6200                  (setq id (or (gnus-nov-field)
6201                               (concat "none+"
6202                                      (int-to-string 
6203                                       (setq none (1+ none)))))) ; id
6204                  (progn
6205                    (save-excursion
6206                      (let ((beg (point)))
6207                        (search-forward "\t" eol)
6208                        (if (search-backward ">" beg t)
6209                            (setq ref 
6210                                  (downcase 
6211                                   (buffer-substring 
6212                                    (1+ (point))
6213                                    (progn
6214                                      (search-backward "<" beg t)
6215                                      (point)))))
6216                          (setq ref nil))))
6217                    (gnus-nov-field))    ; refs
6218                  (read (current-buffer)) ; chars
6219                  (read (current-buffer)) ; lines
6220                  (if (/= (following-char) ?\t)
6221                      nil
6222                    (forward-char 1)
6223                    (gnus-nov-field))    ; misc
6224                  ))
6225         (error (progn 
6226                  (setq header nil)
6227                  (goto-char eol)))))
6228     ;; We build the thread tree.
6229     (and header
6230          (if (boundp (setq dep (intern (downcase id) dependencies)))
6231              (if (car (symbol-value dep))
6232                  ;; An article with this Message-ID has already been seen,
6233                  ;; so we ignore this one, except we add any additional
6234                  ;; Xrefs (in case the two articles came from different
6235                  ;; servers.
6236                  (progn
6237                    (header-set-xref 
6238                     (car (symbol-value dep))
6239                     (concat (or (header-xref (car (symbol-value dep))) "")
6240                             (or (header-xref header) "")))
6241                    (setq header nil))
6242                (setcar (symbol-value dep) header))
6243            (set dep (list header))))
6244     (if header
6245         (progn
6246           (if (boundp (setq dep (intern (or ref "none") 
6247                                         dependencies)))
6248               (setcdr (symbol-value dep) 
6249                       (cons header (cdr (symbol-value dep))))
6250             (set dep (list nil header)))))
6251     header))
6252
6253 (defun gnus-article-get-xrefs ()
6254   "Fill in the Xref value in `gnus-current-headers', if necessary.
6255 This is meant to be called in `gnus-article-internal-prepare-hook'."
6256   (or (not gnus-use-cross-reference)
6257       (header-xref gnus-current-headers)
6258       (let ((case-fold-search t)
6259             xref)
6260         (save-restriction
6261           (gnus-narrow-to-headers)
6262           (goto-char (point-min))
6263           (if (or (and (eq (downcase (following-char)) ?x)
6264                        (looking-at "Xref:"))
6265                   (search-forward "\nXref:" nil t))
6266               (progn
6267                 (goto-char (1+ (match-end 0)))
6268                 (setq xref (buffer-substring (point) 
6269                                              (progn (end-of-line) (point))))
6270                 (save-excursion
6271                   (set-buffer gnus-summary-buffer)
6272                   (header-set-xref gnus-current-headers xref))))))))
6273
6274 (defalias 'gnus-find-header-by-number 'gnus-get-header-by-number)
6275 (make-obsolete 'gnus-find-header-by-number 'gnus-get-header-by-number)
6276
6277 ;; Return a header specified by a NUMBER.
6278 (defun gnus-get-header-by-number (number)
6279   (save-excursion
6280     (set-buffer gnus-summary-buffer)
6281     (or gnus-newsgroup-headers-hashtb-by-number
6282         (gnus-make-headers-hashtable-by-number))
6283     (gnus-gethash (int-to-string number)
6284                   gnus-newsgroup-headers-hashtb-by-number)))
6285
6286 (defun gnus-make-headers-hashtable-by-number ()
6287   "Make hashtable for the variable gnus-newsgroup-headers by number."
6288   (save-excursion
6289     (set-buffer gnus-summary-buffer)
6290     (let ((headers gnus-newsgroup-headers)
6291           header)
6292       (setq gnus-newsgroup-headers-hashtb-by-number
6293             (gnus-make-hashtable (length headers)))
6294       (while headers
6295         (setq header (car headers))
6296         (gnus-sethash (int-to-string (header-number header))
6297                       header gnus-newsgroup-headers-hashtb-by-number)
6298         (setq headers (cdr headers))))))
6299
6300 (defun gnus-more-header-backward ()
6301   "Find new header backward."
6302   (let ((first (car (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6303         (artnum gnus-newsgroup-begin)
6304         (header nil))
6305     (while (and (not header)
6306                 (> artnum first))
6307       (setq artnum (1- artnum))
6308       (setq header (gnus-read-header artnum)))
6309     header))
6310
6311 (defun gnus-more-header-forward (&optional backward)
6312   "Find new header forward.
6313 If BACKWARD, find new header backward instead."
6314   (if backward
6315       (gnus-more-header-backward)
6316     (let ((last (cdr (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6317           (artnum gnus-newsgroup-end)
6318           (header nil))
6319       (while (and (not header)
6320                   (< artnum last))
6321         (setq artnum (1+ artnum))
6322         (setq header (gnus-read-header artnum)))
6323       header)))
6324
6325 (defun gnus-extend-newsgroup (header &optional backward)
6326   "Extend newsgroup selection with HEADER.
6327 Optional argument BACKWARD means extend toward backward."
6328   (if header
6329       (let ((artnum (header-number header)))
6330         (setq gnus-newsgroup-headers
6331               (if backward
6332                   (cons header gnus-newsgroup-headers)
6333                 (nconc gnus-newsgroup-headers (list header))))
6334         (setq gnus-newsgroup-unselected
6335               (delq artnum gnus-newsgroup-unselected))
6336         (setq gnus-newsgroup-begin (min gnus-newsgroup-begin artnum))
6337         (setq gnus-newsgroup-end (max gnus-newsgroup-end artnum)))))
6338
6339 (defun gnus-summary-work-articles (n)
6340   "Return a list of articles to be worked upon. The prefix argument,
6341 the list of process marked articles, and the current article will be
6342 taken into consideration."
6343   (let (articles)
6344     (if (and n (numberp n))
6345         (let ((backward (< n 0))
6346               (n (abs n)))
6347           (save-excursion
6348             (while (and (> n 0)
6349                         (setq articles (cons (gnus-summary-article-number) 
6350                                              articles))
6351                         (gnus-summary-search-forward nil nil backward))
6352               (setq n (1- n))))
6353           (sort articles (function <)))
6354       (or (reverse gnus-newsgroup-processable)
6355           (list (gnus-summary-article-number))))))
6356
6357 (defun gnus-summary-search-group (&optional backward use-level)
6358   "Search for next unread newsgroup.
6359 If optional argument BACKWARD is non-nil, search backward instead."
6360   (save-excursion
6361     (set-buffer gnus-group-buffer)
6362     (save-excursion
6363       ;; We don't want to alter current point of group mode buffer.
6364       (if (gnus-group-search-forward 
6365            backward nil
6366            (if use-level (gnus-group-group-level) nil))
6367           (gnus-group-group-name)))))
6368
6369 (defun gnus-summary-best-group (&optional exclude-group)
6370   "Find the name of the best unread group.
6371 If EXCLUDE-GROUP, do not go to this group."
6372   (save-excursion
6373     (set-buffer gnus-group-buffer)
6374     (save-excursion
6375       (gnus-group-best-unread-group exclude-group))))
6376
6377 (defun gnus-summary-search-subject (&optional backward unread subject)
6378   "Search for article forward.
6379 If BACKWARD is non-nil, search backward.
6380 If UNREAD is non-nil, only unread articles are selected.
6381 If SUBJECT is non-nil, the article which has the same subject will be
6382 searched for." 
6383   (let ((func (if backward 'previous-single-property-change
6384                 'next-single-property-change))
6385         (beg (point))
6386         (did t)
6387         pos)
6388     (beginning-of-line)
6389     (forward-char (if backward (if (bobp) 0 -1) (if (eobp) 0 1)))
6390     (while (and (setq pos (funcall func (point) 'gnus-number))
6391                 (goto-char (if backward (1- pos) pos))
6392                 (setq did
6393                       (not (and (or (not unread)
6394                                     (eq (get-text-property (point) 'gnus-mark) 
6395                                         gnus-unread-mark))
6396                                 (or (not subject)
6397                                     (equal (gnus-simplify-subject-re 
6398                                             subject)
6399                                            (gnus-simplify-subject-re
6400                                             (get-text-property 
6401                                              (point) 
6402                                              'gnus-subject)))))))
6403                 (if backward (if (bobp) nil (forward-char -1) t)
6404                   (if (eobp) nil (forward-char 1) t))))
6405     (if did
6406         (progn (goto-char beg) nil)
6407       (prog1
6408           (get-text-property (point) 'gnus-number)
6409         (gnus-summary-position-cursor)))))
6410
6411 (defun gnus-summary-search-forward (&optional unread subject backward)
6412   "Search for article forward.
6413 If UNREAD is non-nil, only unread articles are selected.
6414 If SUBJECT is non-nil, the article which has the same subject will be
6415 searched for. 
6416 If BACKWARD is non-nil, the search will be performed backwards instead."
6417   (gnus-summary-search-subject backward unread subject))
6418
6419 (defun gnus-summary-search-backward (&optional unread subject)
6420   "Search for article backward.
6421 If 1st optional argument UNREAD is non-nil, only unread article is selected.
6422 If 2nd optional argument SUBJECT is non-nil, the article which has
6423 the same subject will be searched for."
6424   (gnus-summary-search-forward unread subject t))
6425
6426 (defun gnus-summary-article-number (&optional number-or-nil)
6427   "The article number of the article on the current line.
6428 If there isn's an article number here, then we return the current
6429 article number."
6430   (let* ((p (point))
6431          (number (get-text-property 
6432                   (progn (beginning-of-line) (prog1 (point) (goto-char p)))
6433                   'gnus-number)))
6434     (if number-or-nil number (or number gnus-current-article))))
6435
6436 (defun gnus-summary-thread-level ()
6437   "The thread level of the article on the current line."
6438   (or (get-text-property (gnus-point-at-bol) 'gnus-thread)
6439       0))
6440
6441 (defun gnus-summary-pseudo-article ()
6442   "The thread level of the article on the current line."
6443   (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
6444
6445 (defun gnus-summary-article-mark ()
6446   "The mark on the current line."
6447   (get-text-property (gnus-point-at-bol) 'gnus-mark))
6448
6449 (defun gnus-summary-subject-string ()
6450   "Return current subject string or nil if nothing."
6451   (get-text-property (gnus-point-at-bol) 'gnus-subject))
6452
6453 (defalias 'gnus-summary-score 'gnus-summary-article-score)
6454 (make-obsolete 'gnus-summary-score 'gnus-summary-article-score)
6455 (defun gnus-summary-article-score ()
6456   "Return current article score."
6457   (or (cdr (assq (gnus-summary-article-number) gnus-newsgroup-scored))
6458       gnus-summary-default-score 0))
6459
6460 (defun gnus-summary-recenter ()
6461   "Center point in the summary window.
6462 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6463 displayed, no centering will be performed." 
6464   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6465   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
6466   (let ((top (cond ((< (window-height) 4) 0)
6467                    ((< (window-height) 6) 1)
6468                    (t 2))))
6469     (and 
6470      ;; The user has to want it,
6471      gnus-auto-center-summary 
6472      ;; the article buffer must be displayed,
6473      (get-buffer-window gnus-article-buffer)
6474      ;; there must be lines left to scroll forward,
6475      (zerop (save-excursion (forward-line (- (window-height) 1 top))))
6476      ;; so we recenter.
6477      (set-window-start 
6478       (get-buffer-window (current-buffer)) 
6479       (save-excursion (forward-line (- top)) (point))))))
6480
6481 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
6482 (defun gnus-short-group-name (newsgroup)
6483   "Convert a long group name to an initialized form.
6484 The last part of the name is left intact: \"rec.arts.anime\" becomes
6485 \"r.a.anime\"."
6486   (let ((name ""))
6487     (while newsgroup
6488       (if (string-match "\\." newsgroup)
6489           (progn
6490             (setq name (concat name (substring newsgroup 0 1))
6491                   newsgroup (substring newsgroup (match-end 0))
6492                   name (concat name ".")))
6493           (setq name (concat name newsgroup)
6494                 newsgroup nil)))
6495     name))
6496
6497
6498 (defun gnus-summary-jump-to-group (newsgroup)
6499   "Move point to NEWSGROUP in group mode buffer."
6500   ;; Keep update point of group mode buffer if visible.
6501   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6502       (save-window-excursion
6503         ;; Take care of tree window mode.
6504         (if (get-buffer-window gnus-group-buffer)
6505             (pop-to-buffer gnus-group-buffer))
6506         (gnus-group-jump-to-group newsgroup))
6507     (save-excursion
6508       ;; Take care of tree window mode.
6509       (if (get-buffer-window gnus-group-buffer)
6510           (pop-to-buffer gnus-group-buffer)
6511         (set-buffer gnus-group-buffer))
6512       (gnus-group-jump-to-group newsgroup))))
6513
6514 ;; This function returns a list of article numbers based on the
6515 ;; difference between the ranges of read articles in this group and
6516 ;; the range of active articles.
6517 (defun gnus-list-of-unread-articles (group)
6518   (let* ((read (nth 2 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6519          (active (gnus-gethash group gnus-active-hashtb))
6520          (last (cdr active))
6521          unread first nlast unread)
6522     ;; If none are read, then all are unread. 
6523     (if (not read)
6524         (setq first (car active))
6525       ;; If the range of read articles is a single range, then the
6526       ;; first unread article is the article after the last read
6527       ;; article. Sounds logical, doesn't it?
6528       (if (not (listp (cdr read)))
6529           (setq first (1+ (cdr read)))
6530         ;; `read' is a list of ranges.
6531         (if (/= (setq nlast (or (and (numberp (car read)) (car read)) 
6532                                 (car (car read)))) 1)
6533             (setq first 1))
6534         (while read
6535           (if first 
6536               (while (< first nlast)
6537                 (setq unread (cons first unread))
6538                 (setq first (1+ first))))
6539           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
6540           (setq nlast (if (atom (car (cdr read))) 
6541                           (car (cdr read))
6542                         (car (car (cdr read)))))
6543           (setq read (cdr read)))))
6544     ;; And add the last unread articles.
6545     (while (<= first last)
6546       (setq unread (cons first unread))
6547       (setq first (1+ first)))
6548     ;; Return the list of unread articles.
6549     (nreverse unread)))
6550
6551
6552 ;; Various summary commands
6553
6554 (defun gnus-summary-universal-argument ()
6555   "Perform any operation on all articles marked with the process mark."
6556   (interactive)
6557   (gnus-set-global-variables)
6558   (let ((articles (reverse gnus-newsgroup-processable))
6559         key func)
6560     (or articles (error "No articles marked"))
6561     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
6562         (error "Undefined key"))
6563     (while articles
6564       (gnus-summary-goto-subject (car articles))
6565       (command-execute func)
6566       (gnus-summary-remove-process-mark (car articles))
6567       (setq articles (cdr articles)))))
6568
6569 (defun gnus-summary-toggle-truncation (arg)
6570   "Toggle truncation of summary lines.
6571 With arg, turn line truncation on iff arg is positive."
6572   (interactive "P")
6573   (setq truncate-lines
6574         (if (null arg) (not truncate-lines)
6575           (> (prefix-numeric-value arg) 0)))
6576   (redraw-display))
6577
6578 (defun gnus-summary-reselect-current-group (all)
6579   "Once exit and then reselect the current newsgroup.
6580 The prefix argument ALL means to select all articles."
6581   (interactive "P")
6582   (gnus-set-global-variables)
6583   (let ((current-subject (gnus-summary-article-number)))
6584     (gnus-summary-exit t)
6585     ;; We have to adjust the point of group mode buffer because the
6586     ;; current point was moved to the next unread newsgroup by
6587     ;; exiting.
6588     (gnus-summary-jump-to-group gnus-newsgroup-name)
6589     (gnus-group-read-group all t)
6590     (gnus-summary-goto-subject current-subject)))
6591
6592 (defun gnus-summary-rescan-group (all)
6593   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6594   (interactive "P")
6595   (gnus-set-global-variables)
6596   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
6597   (let ((group gnus-newsgroup-name))
6598     (gnus-summary-exit t)
6599     (gnus-summary-jump-to-group group)
6600     (save-excursion
6601       (set-buffer gnus-group-buffer)
6602       (gnus-group-get-new-news-this-group 1))
6603     (gnus-summary-jump-to-group group)
6604     (gnus-group-read-group all)))
6605
6606 (defun gnus-summary-exit (&optional temporary)
6607   "Exit reading current newsgroup, and then return to group selection mode.
6608 gnus-exit-group-hook is called with no arguments if that value is non-nil."
6609   (interactive)
6610   (gnus-set-global-variables)
6611   (gnus-kill-save-kill-buffer)
6612   (let* ((group gnus-newsgroup-name)
6613          (quit-buffer (cdr (assoc 'quit-buffer (gnus-find-method-for-group
6614                                                 gnus-newsgroup-name))))
6615          (mode major-mode)
6616          (method (car (gnus-find-method-for-group group)))
6617          (buf (current-buffer)))
6618     (if gnus-newsgroup-kill-headers
6619         (setq gnus-newsgroup-killed
6620               (gnus-compress-sequence
6621                (nconc
6622                 (gnus-set-sorted-intersection
6623                  (gnus-uncompress-range gnus-newsgroup-killed)
6624                  (setq gnus-newsgroup-unselected
6625                        (sort gnus-newsgroup-unselected '<)))
6626                 (setq gnus-newsgroup-unreads
6627                       (sort gnus-newsgroup-unreads '<))) t)))
6628     (or (listp (cdr gnus-newsgroup-killed))
6629         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6630     (let ((updated nil)
6631           (headers gnus-newsgroup-headers))
6632       (gnus-close-group group)
6633       (run-hooks 'gnus-exit-group-hook)
6634       (gnus-score-save)
6635       (gnus-update-read-articles 
6636        group gnus-newsgroup-unreads gnus-newsgroup-unselected 
6637        gnus-newsgroup-marked
6638        t gnus-newsgroup-replied gnus-newsgroup-expirable
6639        gnus-newsgroup-killed gnus-newsgroup-dormant
6640        gnus-newsgroup-bookmarks 
6641        (and gnus-save-score gnus-newsgroup-scored))
6642       (and gnus-use-cross-reference
6643            (gnus-mark-xrefs-as-read 
6644             group headers gnus-newsgroup-unreads gnus-newsgroup-expirable))
6645       ;; Do not switch windows but change the buffer to work.
6646       (set-buffer gnus-group-buffer)
6647       (or (eq 'nndigest method)
6648           (gnus-group-update-group group)))
6649     ;; Make sure where I was, and go to next newsgroup.
6650     (if (eq method 'nndigest)
6651         ()
6652       (gnus-group-jump-to-group group)
6653       (gnus-group-next-unread-group 1))
6654     (if temporary
6655         ;; If exiting temporary, caller should adjust group mode
6656         ;; buffer point by itself.
6657         nil                             ;Nothing to do.
6658       ;; We set all buffer-local variables to nil. It is unclear why
6659       ;; this is needed, but if we don't, buffer-local variables are
6660       ;; not garbage-collected, it seems. This would the lead to en
6661       ;; ever-growing Emacs.
6662       (set-buffer buf)
6663       (gnus-summary-clear-local-variables)
6664       ;; We clear the global counterparts of the buffer-local
6665       ;; variables as well, just to be on the safe side.
6666       (set-buffer gnus-group-buffer)
6667       (gnus-summary-clear-local-variables)
6668       (gnus-configure-windows 'newsgroups t)
6669       ;; Return to group mode buffer. 
6670       (and (get-buffer buf) 
6671            (eq mode 'gnus-summary-mode)
6672            (kill-buffer buf))
6673       (if (get-buffer gnus-article-buffer)
6674           (bury-buffer gnus-article-buffer))
6675       (setq gnus-current-select-method gnus-select-method)
6676       (pop-to-buffer gnus-group-buffer)
6677       (if (and quit-buffer (buffer-name quit-buffer))
6678           (progn
6679             (switch-to-buffer quit-buffer)
6680             (gnus-set-global-variables)
6681             (gnus-configure-windows 'summary))))))
6682
6683 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6684 (defun gnus-summary-exit-no-update (&optional no-questions)
6685   "Quit reading current newsgroup without updating read article info."
6686   (interactive)
6687   (let* ((group gnus-newsgroup-name)
6688          (quit-buffer (cdr (assoc 'quit-buffer 
6689                                   (gnus-find-method-for-group group)))))
6690     (if (or no-questions
6691             gnus-expert-user
6692             (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
6693         (progn
6694           (gnus-close-group group)
6695           (gnus-summary-clear-local-variables)
6696           (set-buffer gnus-group-buffer)
6697           (gnus-summary-clear-local-variables)
6698           ;; Return to group selection mode.
6699           (gnus-configure-windows 'newsgroups)
6700           (if (get-buffer gnus-summary-buffer)
6701               (kill-buffer gnus-summary-buffer))
6702           (if (get-buffer gnus-article-buffer)
6703               (bury-buffer gnus-article-buffer))
6704           (pop-to-buffer gnus-group-buffer)
6705           (gnus-group-jump-to-group group)
6706           (gnus-group-next-group 1)
6707           (if (and quit-buffer (buffer-name quit-buffer))
6708               (progn
6709                 (switch-to-buffer quit-buffer)
6710                 (gnus-configure-windows 'summary)))))))
6711
6712 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6713 (defun gnus-summary-fetch-faq (group)
6714   "Fetch the FAQ for the current group."
6715   (interactive (list gnus-newsgroup-name))
6716   (gnus-configure-windows 'article)
6717   (pop-to-buffer gnus-article-buffer)
6718   (find-file (concat gnus-group-faq-directory group)))
6719
6720 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6721 (defun gnus-summary-describe-group (force)
6722   "Describe the current newsgroup."
6723   (interactive "P")
6724   (gnus-group-describe-group force gnus-newsgroup-name))
6725
6726 (defun gnus-summary-describe-briefly ()
6727   "Describe summary mode commands briefly."
6728   (interactive)
6729   (message
6730     (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")))
6731
6732 ;; Walking around group mode buffer from summary mode.
6733
6734 (defun gnus-summary-next-group (&optional no-article group backward)
6735   "Exit current newsgroup and then select next unread newsgroup.
6736 If prefix argument NO-ARTICLE is non-nil, no article is selected initially.
6737 If BACKWARD, go to previous group instead."
6738   (interactive "P")
6739   (gnus-set-global-variables)
6740   (let ((ingroup gnus-newsgroup-name)
6741         (sumbuf (current-buffer))
6742         num)
6743     (gnus-summary-exit t)               ;Update all information.
6744     (if (and group
6745              (or (and (numberp (setq num (car (gnus-gethash
6746                                                group gnus-newsrc-hashtb))))
6747                       (< num 1))
6748                  (null num)))
6749         (progn
6750           (gnus-group-jump-to-group group)
6751           (setq group nil))
6752       (gnus-group-jump-to-group ingroup))
6753     (let ((group (or group (gnus-summary-search-group backward)))
6754           (buf gnus-summary-buffer))
6755       (if (null group)
6756           (gnus-summary-exit-no-update t)
6757         (message "Selecting %s..." group)
6758         ;; We are now in group mode buffer.
6759         ;; Make sure group mode buffer point is on GROUP.
6760         (gnus-group-jump-to-group group)
6761         (unwind-protect
6762             (gnus-summary-read-group group nil no-article buf)
6763           (and (string= gnus-newsgroup-name ingroup)
6764                (bufferp sumbuf) (buffer-name sumbuf)
6765                (progn
6766                  (set-buffer (setq gnus-summary-buffer sumbuf))
6767                  (gnus-summary-exit-no-update t))))))))
6768
6769 (defun gnus-summary-prev-group (no-article)
6770   "Exit current newsgroup and then select previous unread newsgroup.
6771 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6772   (interactive "P")
6773   (gnus-summary-next-group no-article nil t))
6774
6775 ;; Walking around summary lines.
6776
6777 (defun gnus-summary-first-subject (unread)
6778   "Go to the first unread subject.
6779 If UNREAD is non-nil, go to the first unread article.
6780 Returns nil if there are no unread articles."
6781   (interactive "P")
6782   (let ((begin (point)))
6783     (if unread
6784         (if (not (gnus-goto-char 
6785                   (text-property-any (point-min) (point-max)
6786                                      'gnus-mark gnus-unread-mark)))
6787             (progn
6788               ;; If there is no unread articles, stay where you are.
6789               (goto-char begin)
6790               (message "No more unread articles")
6791               nil)
6792           t)
6793       (goto-char (point-min)))))
6794
6795 (defun gnus-summary-next-subject (n &optional unread)
6796   "Go to next N'th summary line.
6797 If N is negative, go to the previous N'th subject line.
6798 If UNREAD is non-nil, only unread articles are selected.
6799 The difference between N and the actual number of steps taken is
6800 returned."
6801   (interactive "p")
6802   (let ((backward (< n 0))
6803         (n (abs n)))
6804   (while (and (> n 0)
6805               (gnus-summary-search-forward unread nil backward))
6806     (setq n (1- n)))
6807   (gnus-summary-recenter)
6808   (if (/= 0 n) (message "No more%s articles" (if unread " unread" "")))
6809   (gnus-summary-position-cursor)
6810   n))
6811
6812 (defun gnus-summary-next-unread-subject (n)
6813   "Go to next N'th unread summary line."
6814   (interactive "p")
6815   (gnus-summary-next-subject n t))
6816
6817 (defun gnus-summary-prev-subject (n &optional unread)
6818   "Go to previous N'th summary line.
6819 If optional argument UNREAD is non-nil, only unread article is selected."
6820   (interactive "p")
6821   (gnus-summary-next-subject (- n) unread))
6822
6823 (defun gnus-summary-prev-unread-subject (n)
6824   "Go to previous N'th unread summary line."
6825   (interactive "p")
6826   (gnus-summary-next-subject (- n) t))
6827
6828 (defun gnus-summary-goto-subject (article)
6829   "Go the subject line of ARTICLE."
6830   (interactive
6831    (list
6832     (string-to-int
6833      (completing-read "Article number: "
6834                       (mapcar
6835                        (lambda (headers)
6836                          (list
6837                           (int-to-string (header-number headers))))
6838                        gnus-newsgroup-headers)
6839                       nil 'require-match))))
6840   (or article (error "No article number"))
6841   (if (or (eq article (gnus-summary-article-number t))
6842           (gnus-goto-char
6843            (text-property-any
6844             (point-min) (point-max) 'gnus-number article)))
6845       article))
6846
6847 ;; Walking around summary lines with displaying articles.
6848
6849 (defun gnus-summary-expand-window ()
6850   "Expand summary window to show headers full window."
6851   (interactive)
6852   (gnus-set-global-variables)
6853   (gnus-configure-windows 'summary)
6854   (pop-to-buffer gnus-summary-buffer))
6855
6856 (defun gnus-summary-display-article (article &optional all-header)
6857   "Display ARTICLE in article buffer."
6858   (gnus-set-global-variables)
6859   (if (null article)
6860       nil
6861     (gnus-article-prepare article all-header)
6862     (if (eq (gnus-summary-article-mark) ?Z)
6863         (progn
6864           (forward-line 1)
6865           (gnus-summary-position-cursor)))
6866     (run-hooks 'gnus-select-article-hook)
6867     (gnus-summary-recenter)
6868 ;    (set-window-point (get-buffer-window (current-buffer)) (point-max))
6869 ;    (sit-for 0)
6870     (gnus-summary-goto-subject article)
6871     ;; Successfully display article.
6872     (gnus-summary-update-line)
6873     t))
6874
6875 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6876   "Select the current article.
6877 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6878 non-nil, the article will be re-fetched even if it already present in
6879 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6880 be displayed."
6881   (and (not pseudo) (gnus-summary-pseudo-article)
6882        (error "This is a pseudo-article."))
6883   (let ((article (or article (gnus-summary-article-number)))
6884         (all-headers (not (not all-headers)))) ;Must be T or NIL.
6885     (if (or (null gnus-current-article)
6886             (null gnus-article-current)
6887             (/= article (cdr gnus-article-current))
6888             (not (equal (car gnus-article-current) gnus-newsgroup-name))
6889             force)
6890         ;; The requested article is different from the current article.
6891         (progn
6892           (gnus-summary-display-article article all-headers)
6893           article)
6894       (if all-headers (gnus-article-show-all-headers))
6895       (gnus-configure-windows 'article)
6896       (pop-to-buffer gnus-summary-buffer)
6897       nil)))
6898
6899 (defun gnus-summary-set-current-mark (&optional current-mark)
6900   "Obsolete function."
6901   nil)
6902
6903 (defun gnus-summary-next-article (unread &optional subject backward)
6904   "Select the next article.
6905 If UNREAD, only unread articles are selected.
6906 If SUBJECT, only articles with SUBJECT are selected.
6907 If BACKWARD, the previous article is selected instead of the next."
6908   (interactive "P")
6909   (let ((opoint (point))
6910         (method (car (gnus-find-method-for-group gnus-newsgroup-name)))
6911         header)
6912     (cond
6913      ;; Is there such an article?
6914      ((gnus-summary-display-article 
6915        (gnus-summary-search-forward unread subject backward))
6916       (gnus-summary-position-cursor))
6917      ;; If not, we try the first unread, if that is wanted.
6918      ((and subject
6919            gnus-auto-select-same
6920            (gnus-summary-first-unread-article))
6921       (message "Wrapped"))
6922      ;; Try to get next/previous article not displayed in this group.
6923      ((and gnus-auto-extend-newsgroup
6924            (not unread) (not subject)
6925            (setq header (gnus-more-header-forward backward)))
6926       (gnus-extend-newsgroup header backward)
6927       (let ((buffer-read-only nil))
6928         (goto-char (if backward (point-min) (point-max)))
6929         (gnus-summary-prepare-threads (list header) 0))
6930       (gnus-summary-goto-article (if backward gnus-newsgroup-begin
6931                                    gnus-newsgroup-end)))
6932      ;; Go to next/previous group.
6933      (t
6934       (gnus-summary-jump-to-group gnus-newsgroup-name)
6935       (let ((cmd (aref (this-command-keys) 0))
6936             (group 
6937              (if (eq gnus-keep-same-level 'best) 
6938                  (gnus-summary-best-group gnus-newsgroup-name)
6939                (gnus-summary-search-group backward gnus-keep-same-level))))
6940         ;; Keep just the event type of CMD.
6941         (and (listp cmd) (setq cmd (car cmd)))
6942         ;; Select next unread newsgroup automagically.
6943         (cond 
6944          ((not gnus-auto-select-next)
6945           (message "No more%s articles" (if unread " unread" "")))
6946          ((eq gnus-auto-select-next 'quietly)
6947           ;; Select quietly.
6948           (if (eq method 'nndigest)
6949               (gnus-summary-exit)
6950             (message "No more%s articles (%s)..."
6951                      (if unread " unread" "") 
6952                      (if group (concat "selecting " group)
6953                        "exiting"))
6954             (gnus-summary-next-group nil group backward)))
6955          (t
6956           (let ((keystrokes '(?\C-n ?\C-p))
6957                 key)
6958             (while (or (null key) (memq key keystrokes))
6959               (message 
6960                "No more%s articles%s" (if unread " unread" "")
6961                (if (and group (not (eq method 'nndigest)))
6962                    (format " (Type %s for %s [%s])"
6963                            (single-key-description cmd) group
6964                            (car (gnus-gethash group gnus-newsrc-hashtb)))
6965                  (format " (Type %s to exit %s)"
6966                          (single-key-description cmd)
6967                          gnus-newsgroup-name)))
6968               ;; Confirm auto selection.
6969               (let* ((event (read-event)))
6970                 (setq key (if (listp event) (car event) event))
6971                 (if (member key keystrokes)
6972                     (let ((obuf (current-buffer)))
6973                       (switch-to-buffer gnus-group-buffer)
6974                       (gnus-group-jump-to-group group)
6975                       (execute-kbd-macro (char-to-string key))
6976                       (setq group (gnus-group-group-name))
6977                       (switch-to-buffer obuf)))))
6978             (if (eq key cmd)
6979                 (if (or (not group) (eq method 'nndigest))
6980                     (gnus-summary-exit)
6981                   (gnus-summary-next-group nil group backward))
6982               (setq unread-command-events (list key)))))))))))
6983
6984 (defun gnus-summary-next-unread-article ()
6985   "Select unread article after current one."
6986   (interactive)
6987   (gnus-summary-next-article t (and gnus-auto-select-same
6988                                     (gnus-summary-subject-string))))
6989
6990 (defun gnus-summary-prev-article (unread &optional subject)
6991   "Select the article after the current one.
6992 If UNREAD is non-nil, only unread articles are selected."
6993   (interactive "P")
6994   (gnus-summary-next-article unread subject t))
6995
6996 (defun gnus-summary-prev-unread-article ()
6997   "Select unred article before current one."
6998   (interactive)
6999   (gnus-summary-prev-article t (and gnus-auto-select-same
7000                                     (gnus-summary-subject-string))))
7001
7002 (defun gnus-summary-next-page (lines &optional circular)
7003   "Show next page of selected article.
7004 If end of article, select next article.
7005 Argument LINES specifies lines to be scrolled up.
7006 If CIRCULAR is non-nil, go to the start of the article instead of 
7007 instead of selecting the next article when reaching the end of the
7008 current article." 
7009   (interactive "P")
7010   (setq gnus-summary-buffer (current-buffer))
7011   (gnus-set-global-variables)
7012   (let ((article (gnus-summary-article-number))
7013         (endp nil))
7014     (if (or (null gnus-current-article)
7015             (null gnus-article-current)
7016             (/= article (cdr gnus-article-current))
7017             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7018         ;; Selected subject is different from current article's.
7019         (gnus-summary-display-article article)
7020       (gnus-configure-windows 'article)
7021       (gnus-eval-in-buffer-window
7022        gnus-article-buffer
7023        (setq endp (gnus-article-next-page lines)))
7024       (pop-to-buffer gnus-summary-buffer)
7025       (if endp
7026           (cond (circular
7027                  (gnus-summary-beginning-of-article))
7028                 (lines
7029                  (message "End of message"))
7030                 ((null lines)
7031                  (gnus-summary-next-unread-article))))
7032       (pop-to-buffer gnus-summary-buffer)))
7033   (gnus-summary-position-cursor))
7034
7035 (defun gnus-summary-prev-page (lines)
7036   "Show previous page of selected article.
7037 Argument LINES specifies lines to be scrolled down."
7038   (interactive "P")
7039   (let ((article (gnus-summary-article-number)))
7040     (if (or (null gnus-current-article)
7041             (null gnus-article-current)
7042             (/= article (cdr gnus-article-current))
7043             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7044         ;; Selected subject is different from current article's.
7045         (gnus-summary-display-article article)
7046       (gnus-configure-windows 'article)
7047       (pop-to-buffer gnus-summary-buffer)
7048       (gnus-eval-in-buffer-window gnus-article-buffer
7049         (gnus-article-prev-page lines))))
7050   (gnus-summary-position-cursor))
7051
7052 (defun gnus-summary-scroll-up (lines)
7053   "Scroll up (or down) one line current article.
7054 Argument LINES specifies lines to be scrolled up (or down if negative)."
7055   (interactive "p")
7056   (or (gnus-summary-select-article nil nil 'pseudo)
7057       (gnus-eval-in-buffer-window 
7058        gnus-article-buffer
7059        (cond ((> lines 0)
7060               (if (gnus-article-next-page lines)
7061                   (message "End of message")))
7062              ((< lines 0)
7063               (gnus-article-prev-page (- lines))))))
7064   (gnus-summary-position-cursor))
7065
7066 (defun gnus-summary-next-same-subject ()
7067   "Select next article which has the same subject as current one."
7068   (interactive)
7069   (gnus-summary-next-article nil (gnus-summary-subject-string)))
7070
7071 (defun gnus-summary-prev-same-subject ()
7072   "Select previous article which has the same subject as current one."
7073   (interactive)
7074   (gnus-summary-prev-article nil (gnus-summary-subject-string)))
7075
7076 (defun gnus-summary-next-unread-same-subject ()
7077   "Select next unread article which has the same subject as current one."
7078   (interactive)
7079   (gnus-summary-next-article t (gnus-summary-subject-string)))
7080
7081 (defun gnus-summary-prev-unread-same-subject ()
7082   "Select previous unread article which has the same subject as current one."
7083   (interactive)
7084   (gnus-summary-prev-article t (gnus-summary-subject-string)))
7085
7086 (defun gnus-summary-first-unread-article ()
7087   "Select the first unread article. 
7088 Return nil if there are no unread articles."
7089   (interactive)
7090   (prog1
7091       (if (gnus-summary-first-subject t)
7092           (gnus-summary-display-article (gnus-summary-article-number)))
7093     (gnus-summary-position-cursor)))
7094
7095 (defun gnus-summary-best-unread-article ()
7096   "Select the unread article with the highest score."
7097   (interactive)
7098   (gnus-set-global-variables)
7099   (let ((scored gnus-newsgroup-scored)
7100         (best -1000000)
7101         article art)
7102     (while scored
7103       (or (> best (cdr (car scored)))
7104           (and (memq (setq art (car (car scored))) gnus-newsgroup-unreads)
7105                (not (memq art gnus-newsgroup-marked))
7106                (not (memq art gnus-newsgroup-dormant))
7107                (if (= best (cdr (car scored)))
7108                    (setq article (min art article))
7109                  (setq article art)
7110                  (setq best (cdr (car scored))))))
7111       (setq scored (cdr scored)))
7112     (if article 
7113         (gnus-summary-goto-article article)
7114       (gnus-summary-first-unread-article))
7115     (gnus-summary-position-cursor)))
7116
7117 (defun gnus-summary-goto-article (article &optional all-headers)
7118   "Fetch ARTICLE and display it if it exists.
7119 If ALL-HEADERS is non-nil, no header lines are hidden."
7120   (interactive
7121    (list
7122     (string-to-int
7123      (completing-read 
7124       "Article number: "
7125       (mapcar (lambda (headers) (list (int-to-string (header-number headers))))
7126               gnus-newsgroup-headers) 
7127       nil 'require-match))))
7128   (if (gnus-summary-goto-subject article)
7129       (gnus-summary-display-article article all-headers))
7130   (gnus-summary-position-cursor))
7131
7132 (defun gnus-summary-goto-last-article ()
7133   "Go to the last article."
7134   (interactive)
7135   (if gnus-last-article
7136       (gnus-summary-goto-article gnus-last-article))
7137   (gnus-summary-position-cursor))
7138
7139 (defun gnus-summary-pop-article (number)
7140   "Pop one article off the history and go to the previous.
7141 NUMBER articles will be popped off."
7142   (interactive "p")
7143   (let (to)
7144     (setq gnus-newsgroup-history
7145           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7146     (if to
7147         (gnus-summary-goto-article (car to))
7148       (error "Article history empty")))
7149   (gnus-summary-position-cursor))
7150
7151 ;; Summary article oriented commands
7152
7153 (defun gnus-summary-refer-parent-article (n)
7154   "Refer parent article N times.
7155 The difference between N and the number of articles fetched is returned."
7156   (interactive "p")
7157   (gnus-set-global-variables)
7158   (while 
7159       (and 
7160        (> n 0)
7161        (let ((ref (header-references (gnus-get-header-by-number
7162                                       (gnus-summary-article-number)))))
7163          (if (and ref (not (equal ref ""))
7164                   (string-match "<[^<>]*>[ \t]*$" ref))
7165              (gnus-summary-refer-article 
7166               (substring ref (match-beginning 0) (match-end 0))))))
7167     (setq n (1- n)))
7168   (or (zerop n) (message "No references in article or expired article."))
7169   (gnus-summary-position-cursor)
7170   n)
7171     
7172 (defun gnus-summary-refer-article (message-id)
7173   "Refer article specified by MESSAGE-ID.
7174 NOTE: This command only works with newsgroup that use NNTP."
7175   (interactive "sMessage-ID: ")
7176   (if (or (not (stringp message-id))
7177           (zerop (length message-id)))
7178       ()
7179     ;; Construct the correct Message-ID if necessary.
7180     ;; Suggested by tale@pawl.rpi.edu.
7181     (or (string-match "^<" message-id)
7182         (setq message-id (concat "<" message-id)))
7183     (or (string-match ">$" message-id)
7184         (setq message-id (concat message-id ">")))
7185     (let ((header (car (gnus-gethash (downcase message-id)
7186                                      gnus-newsgroup-dependencies))))
7187       (if header
7188           (gnus-summary-goto-article (header-number header))
7189         (let ((gnus-override-method gnus-refer-article-method))
7190           (if (gnus-article-prepare 
7191                message-id nil (gnus-read-header message-id))
7192               (progn
7193                 (gnus-summary-insert-line 
7194                  nil gnus-current-headers 0 nil gnus-read-mark nil nil 
7195                  (header-subject gnus-current-headers))
7196                 (forward-line -1)
7197                 (gnus-summary-position-cursor)
7198                 (gnus-summary-update-line)
7199                 message-id)
7200             (message "No such references")
7201             nil))))))
7202
7203 (defun gnus-summary-enter-digest-group ()
7204   "Enter a digest group based on the current article."
7205   (interactive)
7206   (gnus-summary-select-article)
7207   (let ((name (format "%s/%d" 
7208                       (gnus-group-prefixed-name 
7209                        gnus-newsgroup-name (list 'nndigest "")) 
7210                       gnus-current-article))
7211         (buf (current-buffer)))
7212     (set-buffer gnus-group-buffer)
7213     (gnus-sethash 
7214      name 
7215      (list t nil (list name 3 nil nil 
7216                        (list 'nndigest gnus-article-buffer
7217                              (cons 'quit-buffer buf))))
7218      gnus-newsrc-hashtb)
7219     (gnus-group-read-group t nil name)))
7220   
7221 (defun gnus-summary-isearch-article ()
7222   "Do incremental search forward on current article."
7223   (interactive)
7224   (gnus-summary-select-article)
7225   (gnus-eval-in-buffer-window gnus-article-buffer
7226                               (isearch-forward)))
7227
7228 (defun gnus-summary-search-article-forward (regexp)
7229   "Search for an article containing REGEXP forward.
7230 gnus-select-article-hook is not called during the search."
7231   (interactive
7232    (list (read-string
7233           (concat "Search forward (regexp): "
7234                   (if gnus-last-search-regexp
7235                       (concat "(default " gnus-last-search-regexp ") "))))))
7236   (if (string-equal regexp "")
7237       (setq regexp (or gnus-last-search-regexp ""))
7238     (setq gnus-last-search-regexp regexp))
7239   (if (gnus-summary-search-article regexp nil)
7240       (gnus-eval-in-buffer-window 
7241        gnus-article-buffer
7242        (recenter 0))
7243     (error "Search failed: \"%s\"" regexp)))
7244
7245 (defun gnus-summary-search-article-backward (regexp)
7246   "Search for an article containing REGEXP backward.
7247 gnus-select-article-hook is not called during the search."
7248   (interactive
7249    (list (read-string
7250           (concat "Search backward (regexp): "
7251                   (if gnus-last-search-regexp
7252                       (concat "(default " gnus-last-search-regexp ") "))))))
7253   (if (string-equal regexp "")
7254       (setq regexp (or gnus-last-search-regexp ""))
7255     (setq gnus-last-search-regexp regexp))
7256   (if (gnus-summary-search-article regexp t)
7257       (gnus-eval-in-buffer-window
7258        gnus-article-buffer
7259        (recenter 0))
7260     (error "Search failed: \"%s\"" regexp)))
7261
7262 (defun gnus-summary-search-article (regexp &optional backward)
7263   "Search for an article containing REGEXP.
7264 Optional argument BACKWARD means do search for backward.
7265 gnus-select-article-hook is not called during the search."
7266   (let ((gnus-select-article-hook nil)  ;Disable hook.
7267         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7268         (re-search
7269          (if backward
7270              (function re-search-backward) (function re-search-forward)))
7271         (found nil)
7272         (last nil))
7273     ;; Hidden thread subtrees must be searched for ,too.
7274     (gnus-summary-show-all-threads)
7275     (if (eobp) (forward-line -1))
7276     ;; First of all, search current article.
7277     ;; We don't want to read article again from NNTP server nor reset
7278     ;; current point.
7279     (gnus-summary-select-article)
7280     (message "Searching article: %d..." gnus-current-article)
7281     (setq last gnus-current-article)
7282     (gnus-eval-in-buffer-window gnus-article-buffer
7283       (save-restriction
7284         (widen)
7285         ;; Begin search from current point.
7286         (setq found (funcall re-search regexp nil t))))
7287     ;; Then search next articles.
7288     (while (and (not found)
7289                 (gnus-summary-display-article 
7290                  (gnus-summary-search-subject backward nil nil)))
7291       (message "Searching article: %d..." gnus-current-article)
7292       (gnus-eval-in-buffer-window gnus-article-buffer
7293         (save-restriction
7294           (widen)
7295           (goto-char (if backward (point-max) (point-min)))
7296           (setq found (funcall re-search regexp nil t)))))
7297     (message "")
7298     ;; Adjust article pointer.
7299     (or (eq last gnus-current-article)
7300         (setq gnus-last-article last))
7301     ;; Return T if found such article.
7302     found))
7303
7304 (defun gnus-summary-execute-command (header regexp command &optional backward)
7305   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7306 If HEADER is an empty string (or nil), the match is done on the entire
7307 article. If BACKWARD (the prefix) is non-nil, search backward instead."
7308   (interactive
7309    (list (let ((completion-ignore-case t))
7310            (completing-read 
7311             "Header name: "
7312             (mapcar (string) (list string)
7313                     '("Number" "Subject" "From" "Lines" "Date"
7314                       "Message-ID" "Xref" "References")
7315                     nil 'require-match)))
7316          (read-string "Regexp: ")
7317          (read-key-sequence "Command: ")
7318          current-prefix-arg))
7319   ;; Hidden thread subtrees must be searched as well.
7320   (gnus-summary-show-all-threads)
7321   ;; We don't want to change current point nor window configuration.
7322   (save-excursion
7323     (save-window-excursion
7324       (message "Executing %s..." (key-description command))
7325       ;; We'd like to execute COMMAND interactively so as to give arguments.
7326       (gnus-execute header regexp
7327                     (` (lambda ()
7328                          (call-interactively '(, (key-binding command)))))
7329                     backward)
7330       (message "Executing %s... done" (key-description command)))))
7331
7332 (defun gnus-summary-beginning-of-article ()
7333   "Scroll the article back to the beginning."
7334   (interactive)
7335   (gnus-summary-select-article)
7336   (gnus-eval-in-buffer-window
7337    gnus-article-buffer
7338    (widen)
7339    (goto-char (point-min))
7340    (and gnus-break-pages (gnus-narrow-to-page))))
7341
7342 (defun gnus-summary-end-of-article ()
7343   "Scroll to the end of the article."
7344   (interactive)
7345   (gnus-summary-select-article)
7346   (gnus-eval-in-buffer-window 
7347    gnus-article-buffer
7348    (widen)
7349    (goto-char (point-max))
7350    (and gnus-break-pages (gnus-narrow-to-page))))
7351
7352 (defun gnus-summary-show-article ()
7353   "Force re-fetching of the current article."
7354   (interactive)
7355   (gnus-summary-select-article gnus-have-all-headers t t))
7356
7357 (defun gnus-summary-toggle-header (arg)
7358   "Show the headers if they are hidden, or hide them if they are shown.
7359 If ARG is a positive number, show the entire header.
7360 If ARG is a negative number, hide the unwanted header lines."
7361   (interactive "P")
7362   (gnus-set-global-variables)
7363   (save-excursion
7364     (set-buffer gnus-article-buffer)
7365     (let ((buffer-read-only nil))
7366       (if (numberp arg) 
7367           (if (> arg 0) (remove-text-properties 1 (point-max) '(invisible t))
7368             (if (< arg 0) (run-hooks 'gnus-article-display-hook)))
7369         (if (text-property-any 1 (point-max) 'invisible t)
7370             (remove-text-properties 1 (point-max) '(invisible t))
7371           (let ((gnus-have-all-headers nil))
7372             (run-hooks 'gnus-article-display-hook))))
7373       (set-window-point (get-buffer-window (current-buffer)) 1))))
7374
7375 (defun gnus-summary-show-all-headers ()
7376   "Make all header lines visible."
7377   (interactive)
7378   (gnus-article-show-all-headers))
7379
7380 (defun gnus-summary-toggle-mime (arg)
7381   "Toggle MIME processing.
7382 If ARG is a positive number, turn MIME processing on."
7383   (interactive "P")
7384   (setq gnus-show-mime
7385         (if (null arg) (not gnus-show-mime)
7386           (> (prefix-numeric-value arg) 0)))
7387   (gnus-summary-select-article t 'force))
7388
7389 (defun gnus-summary-caesar-message (rotnum)
7390   "Caesar rotates all letters of current message by 13/47 places.
7391 With prefix arg, specifies the number of places to rotate each letter forward.
7392 Caesar rotates Japanese letters by 47 places in any case."
7393   (interactive "P")
7394   (gnus-summary-select-article)
7395   (let ((mail-header-separator "")) ; !!! Is this necessary?
7396     (gnus-overload-functions)
7397     (gnus-eval-in-buffer-window 
7398      gnus-article-buffer
7399      (save-restriction
7400        (widen)
7401        ;; We don't want to jump to the beginning of the message.
7402        ;; `save-excursion' does not do its job.
7403        (move-to-window-line 0)
7404        (let ((last (point)))
7405          (news-caesar-buffer-body rotnum)
7406          (goto-char last)
7407          (recenter 0))))))
7408
7409 (defun gnus-summary-stop-page-breaking ()
7410   "Stop page breaking in the current article."
7411   (interactive)
7412   (gnus-summary-select-article)
7413   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
7414
7415 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
7416
7417 (defun gnus-summary-move-article (n &optional to-newsgroup select-method)
7418   "Move the current article to a different newsgroup.
7419 If N is a positive number, move the N next articles.
7420 If N is a negative number, move the N previous articles.
7421 If N is nil and any articles have been marked with the process mark,
7422 move those articles instead.
7423 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
7424 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
7425 re-spool using this method.
7426 For this function to work, both the current newsgroup and the
7427 newsgroup that you want to move to have to support the `request-move'
7428 and `request-accept' functions. (Ie. mail newsgroups at present.)"
7429   (interactive "P")
7430   (gnus-set-global-variables)
7431   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
7432       (error "The current newsgroup does not support article moving"))
7433   (let ((articles (gnus-summary-work-articles n))
7434         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7435         art-group)
7436     (if (and (not to-newsgroup) (not select-method))
7437         (setq to-newsgroup
7438               (completing-read 
7439                (format "Where do you want to move %s? %s"
7440                        (if (> (length articles) 1)
7441                            (format "these %d articles" (length articles))
7442                          "this article")
7443                        (if gnus-current-move-group
7444                            (format "(%s default) " gnus-current-move-group)
7445                          ""))
7446                gnus-active-hashtb nil nil prefix)))
7447     (if to-newsgroup
7448         (progn
7449           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
7450               (setq to-newsgroup (or gnus-current-move-group "")))
7451           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
7452               (error "No such group: %s" to-newsgroup))
7453           (setq gnus-current-move-group to-newsgroup)))
7454     (or (gnus-check-backend-function 'request-accept-article 
7455                                      (or select-method to-newsgroup))
7456         (error "%s does not support article moving" to-newsgroup))
7457     (message "Moving to %s: %s..." (or select-method to-newsgroup) articles)
7458     (while articles
7459       (if (setq art-group
7460                 (gnus-request-move-article 
7461                  (car articles)                   ; Article to move
7462                  gnus-newsgroup-name              ; From newsgrouo
7463                  (nth 1 (gnus-find-method-for-group 
7464                          gnus-newsgroup-name))    ; Server
7465                  (list 'gnus-request-accept-article 
7466                        (if select-method
7467                            (quote select-method)
7468                          to-newsgroup)
7469                        (not (cdr articles)))     ; Accept form
7470                  (not (cdr articles))))          ; Only save nov last time
7471           (let* ((buffer-read-only nil)
7472                  (entry 
7473                   (or
7474                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
7475                    (gnus-gethash 
7476                     (gnus-group-prefixed-name 
7477                      (car art-group) 
7478                      (if select-method (list select-method "")
7479                        (gnus-find-method-for-group to-newsgroup)))
7480                     gnus-newsrc-hashtb)))
7481                  (info (nth 2 entry))
7482                  (article (car articles))
7483                  (marked (nth 3 info)))
7484             (gnus-summary-goto-subject article)
7485             (beginning-of-line)
7486             (delete-region (point)
7487                            (progn (forward-line 1) (point)))
7488             (if (not (memq article gnus-newsgroup-unreads))
7489                 (setcar (cdr (cdr info))
7490                         (gnus-add-to-range (nth 2 info) 
7491                                            (list (cdr art-group)))))
7492             ;; Copy any marks over to the new group.
7493             (let ((marks '((tick . gnus-newsgroup-marked)
7494                            (dormant . gnus-newsgroup-dormant)
7495                            (expire . gnus-newsgroup-expirable)
7496                            (bookmark . gnus-newsgroup-bookmarks)
7497                         ;   (score . gnus-newsgroup-scored)
7498                            (reply . gnus-newsgroup-replied)))
7499                   (to-article (cdr art-group)))
7500               (while marks
7501                 (if (memq article (symbol-value (cdr (car marks))))
7502                     (gnus-add-marked-articles 
7503                      (car info) (car (car marks)) (list to-article) info))
7504                 (setq marks (cdr marks))))
7505             (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7506             (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7507             (setq gnus-newsgroup-dormant
7508                   (delq article gnus-newsgroup-dormant)))
7509         (message "Couldn't move article %s" (car articles)))
7510       (gnus-summary-remove-process-mark (car articles))
7511       (setq articles (cdr articles)))))
7512
7513 (defun gnus-summary-respool-article (n &optional respool-method)
7514   "Respool the current article.
7515 The article will be squeezed through the mail spooling process again,
7516 which means that it will be put in some mail newsgroup or other
7517 depending on `nnmail-split-methods'.
7518 If N is a positive number, respool the N next articles.
7519 If N is a negative number, respool the N previous articles.
7520 If N is nil and any articles have been marked with the process mark,
7521 respool those articles instead.
7522
7523 Respooling can be done both from mail groups and \"real\" newsgroups.
7524 In the former case, the articles in question will be moved from the
7525 current group into whatever groups they are destined to.  In the
7526 latter case, they will be copied into the relevant groups."
7527   (interactive "P")
7528   (gnus-set-global-variables)
7529   (let ((respool-methods (gnus-methods-using 'respool)))
7530     (or respool-method
7531         (setq respool-method
7532               (completing-read
7533                "What method do you want to use when respooling? "
7534                respool-methods nil t)))
7535     (if (assoc (symbol-name
7536                 (car (gnus-find-method-for-group gnus-newsgroup-name)))
7537                respool-methods)
7538         (gnus-summary-move-article n nil (intern respool-method)))
7539     (gnus-summary-copy-article n nil (intern respool-method))))
7540
7541 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
7542 (defun gnus-summary-copy-article (n &optional to-newsgroup select-method)
7543   "Move the current article to a different newsgroup.
7544 If N is a positive number, move the N next articles.
7545 If N is a negative number, move the N previous articles.
7546 If N is nil and any articles have been marked with the process mark,
7547 move those articles instead.
7548 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
7549 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
7550 re-spool using this method.
7551 For this function to work, the newsgroup that you want to move to have
7552 to support the `request-move' and `request-accept'
7553 functions. (Ie. mail newsgroups at present.)"
7554   (interactive "P")
7555   (gnus-set-global-variables)
7556   (let ((articles (gnus-summary-work-articles n))
7557         (copy-buf (get-buffer-create "*copy work*"))
7558         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7559         art-group)
7560     (buffer-disable-undo copy-buf)
7561     (if (and (not to-newsgroup) (not select-method))
7562         (setq to-newsgroup
7563               (completing-read 
7564                (format "Where do you want to copy %s? %s"
7565                        (if (> (length articles) 1)
7566                            (format "these %d articles" (length articles))
7567                          "this article")
7568                        (if gnus-current-move-group
7569                            (format "(%s default) " gnus-current-move-group)
7570                          ""))
7571                gnus-active-hashtb nil nil prefix)))
7572     (if to-newsgroup
7573         (progn
7574           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
7575               (setq to-newsgroup (or gnus-current-move-group "")))
7576           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
7577               (error "No such group: %s" to-newsgroup))
7578           (setq gnus-current-move-group to-newsgroup)))
7579     (or (gnus-check-backend-function 'request-accept-article 
7580                                      (or select-method to-newsgroup))
7581         (error "%s does not support article copying" to-newsgroup))
7582     (message "Copying to %s: %s..." (or select-method to-newsgroup) articles)
7583     (while articles
7584       (if (setq art-group
7585                 (save-excursion
7586                   (set-buffer copy-buf)
7587                   (gnus-request-article-this-buffer
7588                    (car articles) gnus-newsgroup-name)
7589                   (gnus-request-accept-article
7590                    (if select-method (quote select-method) to-newsgroup)
7591                    (not (cdr articles)))))
7592           (let* ((entry 
7593                   (or
7594                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
7595                    (gnus-gethash 
7596                     (gnus-group-prefixed-name 
7597                      (car art-group) 
7598                      (if select-method (list select-method "")
7599                        (gnus-find-method-for-group to-newsgroup)))
7600                     gnus-newsrc-hashtb)))
7601                  (info (nth 2 entry))
7602                  (article (car articles))
7603                  (marked (nth 3 info)))
7604             (if (not (memq article gnus-newsgroup-unreads))
7605                 (setcar (cdr (cdr info))
7606                         (gnus-add-to-range (nth 2 info) 
7607                                            (list (cdr art-group)))))
7608             ;; Copy any marks over to the new group.
7609             (let ((marks '((tick . gnus-newsgroup-marked)
7610                            (dormant . gnus-newsgroup-dormant)
7611                            (expire . gnus-newsgroup-expirable)
7612                            (bookmark . gnus-newsgroup-bookmarks)
7613                         ;   (score . gnus-newsgroup-scored)
7614                            (reply . gnus-newsgroup-replied)))
7615                   (to-article (cdr art-group)))
7616               (while marks
7617                 (if (memq article (symbol-value (cdr (car marks))))
7618                     (gnus-add-marked-articles 
7619                      (car info) (car (car marks)) (list to-article) info))
7620                 (setq marks (cdr marks)))))
7621         (message "Couldn't copy article %s" (car articles)))
7622       (gnus-summary-remove-process-mark (car articles))
7623       (setq articles (cdr articles)))
7624     (kill-buffer copy-buf)))
7625
7626 (defun gnus-summary-expire-articles ()
7627   "Expire all articles that are marked as expirable in the current group."
7628   (interactive)
7629   (if (and gnus-newsgroup-expirable
7630            (gnus-check-backend-function 
7631             'request-expire-articles gnus-newsgroup-name))
7632       (let ((expirable gnus-newsgroup-expirable))
7633         ;; The list of articles that weren't expired is returned.
7634         (setq gnus-newsgroup-expirable 
7635               (gnus-request-expire-articles gnus-newsgroup-expirable
7636                                             gnus-newsgroup-name))
7637         ;; We go through the old list of expirable, and mark all
7638         ;; really expired articles as non-existant.
7639         (while expirable
7640           (or (memq (car expirable) gnus-newsgroup-expirable)
7641               (gnus-summary-mark-as-read (car expirable) "%"))
7642           (setq expirable (cdr expirable))))))
7643
7644 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7645 (defun gnus-summary-delete-article (n)
7646   "Delete the N next (mail) articles.
7647 This command actually deletes articles. This is not a marking
7648 command. The article will disappear forever from you life, never to
7649 return. 
7650 If N is negative, delete backwards.
7651 If N is nil and articles have been marked with the process mark,
7652 delete these instead."
7653   (interactive "P")
7654   (or (gnus-check-backend-function 'request-expire-articles 
7655                                    gnus-newsgroup-name)
7656       (error "The current newsgroup does not support article deletion."))
7657   ;; Compute the list of articles to delete.
7658   (let ((articles (gnus-summary-work-articles n)))
7659     (if (and gnus-novice-user
7660              (not (gnus-y-or-n-p 
7661                    (format "Do you really want to delete %s forever? "
7662                            (if (> (length articles) 1) "these articles"
7663                              "this article")))))
7664         ()
7665       ;; Delete the articles.
7666       (setq gnus-newsgroup-expirable 
7667             (gnus-request-expire-articles 
7668              articles gnus-newsgroup-name 'force))
7669       (while articles
7670         (gnus-summary-remove-process-mark (car articles))       
7671         ;; The backend might not have been able to delete the article
7672         ;; after all.  
7673         (or (memq (car articles) gnus-newsgroup-expirable)
7674             (gnus-summary-mark-as-read (car articles) gnus-canceled-mark))
7675         (setq articles (cdr articles))))))
7676
7677 (defun gnus-summary-edit-article ()
7678   "Enter into a buffer and edit the current article.
7679 This will have permanent effect only in mail groups."
7680   (interactive)
7681   (or (gnus-check-backend-function 
7682        'request-replace-article gnus-newsgroup-name)
7683       (error "The current newsgroup does not support article editing."))
7684   (gnus-summary-select-article t)
7685   (other-window 1)
7686   (message "C-c C-c to end edits")
7687   (setq buffer-read-only nil)
7688   (text-mode)
7689   (use-local-map (copy-keymap (current-local-map)))
7690   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
7691   (goto-char 1)
7692   (search-forward "\n\n" nil t))
7693
7694 (defun gnus-summary-edit-article-done ()
7695   "Make edits to the current article permanent."
7696   (interactive)
7697   (if (not (gnus-request-replace-article 
7698             (cdr gnus-article-current) (car gnus-article-current) 
7699             (current-buffer)))
7700       (error "Couldn't replace article.")
7701     (gnus-article-mode)
7702     (use-local-map gnus-article-mode-map)
7703     (setq buffer-read-only t)
7704     (pop-to-buffer gnus-summary-buffer)))      
7705
7706 ;; Summary score commands.
7707
7708 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
7709
7710 (defun gnus-summary-raise-score (n)
7711   "Raise the score of the current article by N."
7712   (interactive "p")
7713   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
7714
7715 (defun gnus-summary-lower-score (n)
7716   "Lower the score of the current article by N."
7717   (interactive "p")
7718   (gnus-summary-raise-score (- n)))
7719
7720 (defun gnus-summary-set-score (n)
7721   "Set the score of the current article to N."
7722   (interactive "p")
7723   ;; Skip dummy header line.
7724   (save-excursion
7725     (if (eq (gnus-summary-article-mark) ?Z)
7726         (forward-line 1))
7727     (let ((buffer-read-only nil))
7728       ;; Set score.
7729       (gnus-summary-update-mark
7730        (if (= n (or gnus-summary-default-score 0)) ? 
7731          (if (< n (or gnus-summary-default-score 0)) 
7732              gnus-score-below-mark gnus-score-over-mark)) 'score))
7733     (let* ((article (gnus-summary-article-number))
7734            (score (assq article gnus-newsgroup-scored)))
7735       (if score (setcdr score n)
7736         (setq gnus-newsgroup-scored 
7737               (cons (cons article n) gnus-newsgroup-scored))))
7738     (gnus-summary-update-line)))
7739
7740 ;; Summary marking commands.
7741
7742 (defun gnus-summary-raise-same-subject-and-select (score)
7743   "Raise articles which has the same subject with SCORE and select the next."
7744   (interactive "p")
7745   (let ((subject (gnus-summary-subject-string)))
7746     (gnus-summary-raise-score score)
7747     (while (gnus-summary-search-subject nil nil subject)
7748       (gnus-summary-raise-score score))
7749     (gnus-summary-next-article t)))
7750
7751 (defun gnus-summary-raise-same-subject (score)
7752   "Raise articles which has the same subject with SCORE."
7753   (interactive "p")
7754   (let ((subject (gnus-summary-subject-string)))
7755     (gnus-summary-raise-score score)
7756     (while (gnus-summary-search-subject nil nil subject)
7757       (gnus-summary-raise-score score))
7758     (gnus-summary-next-subject 1 t)))
7759
7760 (defun gnus-summary-raise-thread (score)
7761   "Raise articles under current thread with SCORE."
7762   (interactive "p")
7763   (let (e)
7764     (save-excursion
7765       (let ((level (gnus-summary-thread-level)))
7766         (gnus-summary-raise-score score)
7767         (while (and (zerop (gnus-summary-next-subject 1))
7768                     (> (gnus-summary-thread-level) level))
7769           (gnus-summary-raise-score score))
7770         (setq e (point))))
7771     (or (zerop (gnus-summary-next-subject 1 t))
7772         (goto-char e)))
7773   (gnus-summary-position-cursor)
7774   (gnus-set-mode-line 'summary))
7775
7776 (defun gnus-summary-lower-same-subject-and-select (score)
7777   "Raise articles which has the same subject with SCORE and select the next."
7778   (interactive "p")
7779   (gnus-summary-raise-same-subject-and-select (- score)))
7780
7781 (defun gnus-summary-lower-same-subject (score)
7782   "Raise articles which has the same subject with SCORE."
7783   (interactive "p")
7784   (gnus-summary-raise-same-subject (- score)))
7785
7786 (defun gnus-summary-lower-thread (score)
7787   "Raise articles under current thread with SCORE."
7788   (interactive "p")
7789   (gnus-summary-raise-thread (- score)))
7790
7791 (defun gnus-summary-kill-same-subject-and-select (unmark)
7792   "Mark articles which has the same subject as read, and then select the next.
7793 If UNMARK is positive, remove any kind of mark.
7794 If UNMARK is negative, tick articles."
7795   (interactive "P")
7796   (if unmark
7797       (setq unmark (prefix-numeric-value unmark)))
7798   (let ((count
7799          (gnus-summary-mark-same-subject
7800           (gnus-summary-subject-string) unmark)))
7801     ;; Select next unread article. If auto-select-same mode, should
7802     ;; select the first unread article.
7803     (gnus-summary-next-article t (and gnus-auto-select-same
7804                                       (gnus-summary-subject-string)))
7805     (message "%d articles are marked as %s"
7806              count (if unmark "unread" "read"))))
7807
7808 (defun gnus-summary-kill-same-subject (unmark)
7809   "Mark articles which has the same subject as read. 
7810 If UNMARK is positive, remove any kind of mark.
7811 If UNMARK is negative, tick articles."
7812   (interactive "P")
7813   (if unmark
7814       (setq unmark (prefix-numeric-value unmark)))
7815   (let ((count
7816          (gnus-summary-mark-same-subject
7817           (gnus-summary-subject-string) unmark)))
7818     ;; If marked as read, go to next unread subject.
7819     (if (null unmark)
7820         ;; Go to next unread subject.
7821         (gnus-summary-next-subject 1 t))
7822     (message "%d articles are marked as %s"
7823              count (if unmark "unread" "read"))))
7824
7825 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7826   "Mark articles with same SUBJECT as read, and return marked number.
7827 If optional argument UNMARK is positive, remove any kinds of marks.
7828 If optional argument UNMARK is negative, mark articles as unread instead."
7829   (let ((count 1))
7830     (save-excursion
7831       (cond ((null unmark)
7832              (gnus-summary-mark-as-read nil gnus-killed-mark))
7833             ((> unmark 0)
7834              (gnus-summary-tick-article nil t))
7835             (t
7836              (gnus-summary-tick-article)))
7837       (while (and subject
7838                   (gnus-summary-search-forward nil subject))
7839         (cond ((null unmark)
7840                (gnus-summary-mark-as-read nil gnus-killed-mark))
7841               ((> unmark 0)
7842                (gnus-summary-tick-article nil t))
7843               (t
7844                (gnus-summary-tick-article)))
7845         (setq count (1+ count))))
7846     ;; Hide killed thread subtrees.  Does not work properly always.
7847     ;;(and (null unmark)
7848     ;;     gnus-thread-hide-killed
7849     ;;     (gnus-summary-hide-thread))
7850     ;; Return number of articles marked as read.
7851     count))
7852
7853 (defun gnus-summary-mark-as-processable (n &optional unmark)
7854   "Set the process mark on the next N articles.
7855 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7856 the process mark instead.  The difference between N and the actual
7857 number of articles marked is returned."
7858   (interactive "p")
7859   (let ((backward (< n 0))
7860         (n (abs n)))
7861   (while (and 
7862           (> n 0)
7863           (if unmark
7864               (gnus-summary-remove-process-mark (gnus-summary-article-number))
7865             (gnus-summary-set-process-mark (gnus-summary-article-number)))
7866           (zerop (gnus-summary-next-subject (if backward -1 1))))
7867     (setq n (1- n)))
7868   (if (/= 0 n) (message "No more articles"))
7869   n))
7870
7871 (defun gnus-summary-unmark-as-processable (n)
7872   "Remove the process mark from the next N articles.
7873 If N is negative, mark backward instead.  The difference between N and
7874 the actual number of articles marked is returned."
7875   (interactive "p")
7876   (gnus-summary-mark-as-processable n t))
7877
7878 (defun gnus-summary-unmark-all-processable ()
7879   "Remove the process mark from all articles."
7880   (interactive)
7881   (save-excursion
7882     (while gnus-newsgroup-processable
7883       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7884   (gnus-summary-position-cursor))
7885
7886 (defun gnus-summary-mark-as-expirable (n)
7887   "Mark N articles forward as expirable.
7888 If N is negative, mark backward instead. The difference between N and
7889 the actual number of articles marked is returned."
7890   (interactive "p")
7891   (gnus-summary-mark-forward n gnus-expirable-mark))
7892
7893 (defun gnus-summary-mark-article-as-replied (article)
7894   "Mark ARTICLE replied and update the summary line."
7895   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
7896   (let ((buffer-read-only nil))
7897     (if (gnus-summary-goto-subject article)
7898         (progn
7899           (gnus-summary-update-mark gnus-replied-mark 'replied)
7900           t))))
7901
7902 (defun gnus-summary-set-bookmark (article)
7903   "Set a bookmark in current article."
7904   (interactive (list (gnus-summary-article-number)))
7905   (if (or (not (get-buffer gnus-article-buffer))
7906           (not gnus-current-article)
7907           (not gnus-article-current)
7908           (not (equal gnus-newsgroup-name (car gnus-article-current))))
7909       (error "No current article selected"))
7910   ;; Remove old bookmark, if one exists.
7911   (let ((old (assq article gnus-newsgroup-bookmarks)))
7912     (if old (setq gnus-newsgroup-bookmarks 
7913                   (delq old gnus-newsgroup-bookmarks))))
7914   ;; Set the new bookmark, which is on the form 
7915   ;; (article-number . line-number-in-body).
7916   (setq gnus-newsgroup-bookmarks 
7917         (cons 
7918          (cons article 
7919                (save-excursion
7920                  (set-buffer gnus-article-buffer)
7921                  (count-lines
7922                   (min (point)
7923                        (save-excursion
7924                          (goto-char 1)
7925                          (search-forward "\n\n" nil t)
7926                          (point)))
7927                   (point))))
7928          gnus-newsgroup-bookmarks))
7929   (message "A bookmark has been added to the current article."))
7930
7931 (defun gnus-summary-remove-bookmark (article)
7932   "Remove the bookmark from the current article."
7933   (interactive (list (gnus-summary-article-number)))
7934   ;; Remove old bookmark, if one exists.
7935   (let ((old (assq article gnus-newsgroup-bookmarks)))
7936     (if old 
7937         (progn
7938           (setq gnus-newsgroup-bookmarks 
7939                 (delq old gnus-newsgroup-bookmarks))
7940           (message "Removed bookmark."))
7941       (message "No bookmark in current article."))))
7942
7943 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7944 (defun gnus-summary-mark-as-dormant (n)
7945   "Mark N articles forward as dormant.
7946 If N is negative, mark backward instead.  The difference between N and
7947 the actual number of articles marked is returned."
7948   (interactive "p")
7949   (gnus-summary-mark-forward n gnus-dormant-mark))
7950
7951 (defun gnus-summary-set-process-mark (article)
7952   "Set the process mark on ARTICLE and update the summary line."
7953   (setq gnus-newsgroup-processable (cons article gnus-newsgroup-processable))
7954   (let ((buffer-read-only nil))
7955     (if (gnus-summary-goto-subject article)
7956         (progn
7957           (and (eq (gnus-summary-article-mark) ?Z)
7958                (forward-line 1))
7959           (gnus-summary-update-mark gnus-process-mark 'replied)
7960           t))))
7961
7962 (defun gnus-summary-remove-process-mark (article)
7963   "Remove the process mark from ARTICLE and update the summary line."
7964   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7965   (let ((buffer-read-only nil))
7966     (if (gnus-summary-goto-subject article)
7967         (progn
7968           (and (eq (gnus-summary-article-mark) ?Z)
7969                (forward-line 1))
7970           (gnus-summary-update-mark ?  'replied)
7971           (if (memq article gnus-newsgroup-replied) 
7972               (gnus-summary-update-mark gnus-replied-mark 'replied))
7973           t))))
7974
7975 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7976   "Mark N articles as read forwards.
7977 If N is negative, mark backwards instead.
7978 Mark with MARK. If MARK is ? , ?! or ??, articles will be
7979 marked as unread. 
7980 The difference between N and the actual number of articles marked is
7981 returned."
7982   (interactive "p")
7983   (gnus-set-global-variables)
7984   (let ((backward (< n 0))
7985         (n (abs n))
7986         (mark (or mark gnus-dread-mark)))
7987   (while (and (> n 0)
7988               (gnus-summary-mark-article nil mark no-expire)
7989               (zerop (gnus-summary-next-subject 
7990                       (if backward -1 1) gnus-summary-goto-unread)))
7991     (setq n (1- n)))
7992   (if (/= 0 n) (message "No more %sarticles" (if mark "" "unread ")))
7993   (gnus-set-mode-line 'summary)
7994   n))
7995
7996 (defun gnus-summary-mark-article (&optional article mark no-expire)
7997   "Mark ARTICLE with MARK.
7998 MARK can be any character.
7999 Five MARK strings are reserved: ?  (unread), 
8000 ?! (ticked), ?? (dormant), ?D (read), ?E (expirable).
8001 If MARK is nil, then the default character ?D is used.
8002 If ARTICLE is nil, then the article on the current line will be
8003 marked." 
8004   ;; If no mark is given, then we check auto-expiring.
8005   (and (not no-expire)
8006        gnus-newsgroup-auto-expire 
8007        (or (not mark)
8008            (and (numberp mark) (or (= mark gnus-killed-mark)
8009                                    (= mark gnus-dread-mark)
8010                                    (= mark gnus-catchup-mark)
8011                                    (= mark gnus-low-score-mark)
8012                                    (= mark gnus-read-mark))))
8013        (setq mark gnus-expirable-mark))
8014   (let* ((buffer-read-only nil)
8015          (mark (or (and (stringp mark) (aref mark 0)) mark gnus-dread-mark))
8016          (article (or article (gnus-summary-article-number))))
8017     (if (or (= mark gnus-unread-mark) 
8018             (= mark gnus-ticked-mark) 
8019             (= mark gnus-dormant-mark))
8020         (gnus-mark-article-as-unread article mark)
8021       (gnus-mark-article-as-read article mark))
8022     (if (gnus-summary-goto-subject article)
8023         (progn
8024           (gnus-summary-show-thread)
8025           (beginning-of-line)
8026           (and (eq (gnus-summary-article-mark) ?Z)
8027                (forward-line 1))
8028           ;; Fix the mark.
8029           (gnus-summary-update-mark mark 'unread)
8030           t))))
8031
8032 (defun gnus-summary-update-mark (mark type)
8033   (beginning-of-line)
8034   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8035         plist)
8036     (if (not forward)
8037         ()
8038       (forward-char forward)
8039       (setq plist (text-properties-at (point)))
8040       (delete-char 1)
8041       (and (memq 'gnus-mark plist) (setcar (cdr (memq 'gnus-mark plist)) mark))
8042       (insert mark)
8043       (and plist (add-text-properties (1- (point)) (point) plist))
8044       (gnus-summary-update-line (eq mark gnus-unread-mark)))))
8045   
8046 (defun gnus-mark-article-as-read (article &optional mark)
8047   "Enter ARTICLE in the pertinent lists and remove it from others."
8048   ;; Make the article expirable.
8049   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-dread-mark)))
8050     (if (= mark gnus-expirable-mark)
8051         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
8052       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8053     ;; Remove from unread and marked lists.
8054     (setq gnus-newsgroup-unreads
8055           (delq article gnus-newsgroup-unreads))
8056     (setq gnus-newsgroup-marked
8057           (delq article gnus-newsgroup-marked))
8058     (setq gnus-newsgroup-dormant
8059           (delq article gnus-newsgroup-dormant))))
8060
8061 (defun gnus-mark-article-as-unread (article &optional mark)
8062   "Enter ARTICLE in the pertinent lists and remove it from others."
8063   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-ticked-mark)))
8064     ;; Add to unread list.
8065     (or (memq article gnus-newsgroup-unreads)
8066         (setq gnus-newsgroup-unreads
8067               (cons article gnus-newsgroup-unreads)))
8068     ;; If CLEAR-MARK is non-nil, the article must be removed from marked
8069     ;; list.  Otherwise, it must be added to the list.
8070     (setq gnus-newsgroup-marked
8071           (delq article gnus-newsgroup-marked))
8072     (setq gnus-newsgroup-dormant
8073           (delq article gnus-newsgroup-dormant))
8074     (setq gnus-newsgroup-expirable 
8075           (delq article gnus-newsgroup-expirable))
8076     (if (= mark gnus-ticked-mark)
8077         (setq gnus-newsgroup-marked 
8078               (cons article gnus-newsgroup-marked)))
8079     (if (= mark gnus-dormant-mark)
8080         (setq gnus-newsgroup-dormant 
8081               (cons article gnus-newsgroup-dormant)))))
8082
8083 (defalias 'gnus-summary-mark-as-unread-forward 
8084   'gnus-summary-tick-article-forward)
8085 (make-obsolete 'gnus-summary-mark-as-unread-forward 
8086                'gnus-summary-tick-article-forward)
8087 (defun gnus-summary-tick-article-forward (n)
8088   "Tick N articles forwards.
8089 If N is negative, tick backwards instead.
8090 The difference between N and the number of articles ticked is returned."
8091   (interactive "p")
8092   (gnus-summary-mark-forward n gnus-ticked-mark))
8093
8094 (defalias 'gnus-summary-mark-as-unread-backward 
8095   'gnus-summary-tick-article-backward)
8096 (make-obsolete 'gnus-summary-mark-as-unread-backward 
8097                'gnus-summary-tick-article-backward)
8098 (defun gnus-summary-tick-article-backward (n)
8099   "Tick N articles backwards.
8100 The difference between N and the number of articles ticked is returned."
8101   (interactive "p")
8102   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8103
8104 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8105 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8106 (defun gnus-summary-tick-article (&optional article clear-mark)
8107   "Mark current article as unread.
8108 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8109 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8110   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8111                                        gnus-ticked-mark)))
8112
8113 (defun gnus-summary-mark-as-read-forward (n)
8114   "Mark N articles as read forwards.
8115 If N is negative, mark backwards instead.
8116 The difference between N and the actual number of articles marked is
8117 returned."
8118   (interactive "p")
8119   (gnus-summary-mark-forward n gnus-dread-mark t))
8120
8121 (defun gnus-summary-mark-as-read-backward (n)
8122   "Mark the N articles as read backwards.
8123 The difference between N and the actual number of articles marked is
8124 returned."
8125   (interactive "p")
8126   (gnus-summary-mark-forward (- n) gnus-dread-mark t))
8127
8128 (defun gnus-summary-mark-as-read (&optional article mark)
8129   "Mark current article as read.
8130 ARTICLE specifies the article to be marked as read.
8131 MARK specifies a string to be inserted at the beginning of the line."
8132   (gnus-summary-mark-article article mark))
8133
8134 (defun gnus-summary-clear-mark-forward (n)
8135   "Clear marks from N articles forward.
8136 If N is negative, clear backward instead.
8137 The difference between N and the number of marks cleared is returned."
8138   (interactive "p")
8139   (gnus-summary-mark-forward n gnus-unread-mark))
8140
8141 (defun gnus-summary-clear-mark-backward (n)
8142   "Clear marks from N articles backward.
8143 The difference between N and the number of marks cleared is returned."
8144   (interactive "p")
8145   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8146
8147 (defun gnus-summary-mark-unread-as-read ()
8148   "Intended to be used by `gnus-summary-mark-article-hook'."
8149   (or (memq gnus-current-article gnus-newsgroup-marked)
8150       (memq gnus-current-article gnus-newsgroup-dormant)
8151       (memq gnus-current-article gnus-newsgroup-expirable)
8152       (gnus-summary-mark-as-read gnus-current-article gnus-read-mark)))
8153
8154 (defun gnus-summary-mark-region-as-read (point mark all)
8155   "Mark all unread articles between point and mark as read.
8156 If given a prefix, mark all articles between point and mark as read,
8157 even ticked and dormant ones."
8158   (interactive "r\nP")
8159   (save-excursion
8160     (goto-char point)
8161     (beginning-of-line)
8162     (while (and 
8163             (< (point) mark)
8164             (progn
8165               (and
8166                (or all
8167                    (and
8168                     (not (memq (gnus-summary-article-number)
8169                                gnus-newsgroup-marked))
8170                     (not (memq (gnus-summary-article-number)
8171                                gnus-newsgroup-dormant))))
8172                (gnus-summary-mark-article
8173                 (gnus-summary-article-number) gnus-dread-mark))
8174               t)
8175             (zerop (forward-line 1))))))
8176
8177 ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
8178 (defalias 'gnus-summary-delete-marked-as-read 
8179   'gnus-summary-remove-lines-marked-as-read)
8180 (make-obsolete 'gnus-summary-delete-marked-as-read 
8181                'gnus-summary-remove-lines-marked-as-read)
8182 (defun gnus-summary-remove-lines-marked-as-read ()
8183   "Remove lines that are marked as read."
8184   (interactive)
8185   (gnus-summary-remove-lines-marked-with 
8186    (concat (mapconcat
8187             (lambda (char) (char-to-string (symbol-value char)))
8188             '(gnus-dread-mark gnus-read-mark
8189               gnus-killed-mark gnus-kill-file-mark
8190               gnus-low-score-mark gnus-expirable-mark)
8191             ""))))
8192
8193 (defalias 'gnus-summary-delete-marked-with 
8194   'gnus-summary-remove-lines-marked-with)
8195 (make-obsolete 'gnus-summary-delete-marked-with 
8196                'gnus-summary-remove-lines-marked-with)
8197 ;; Rewrite by Daniel Quinlan <quinlan@best.com>.
8198 (defun gnus-summary-remove-lines-marked-with (marks)
8199   "Remove lines that are marked with MARKS (e.g. \"DK\")."
8200   (interactive "sMarks: ")
8201   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
8202   (save-excursion
8203     (set-buffer gnus-summary-buffer)
8204     (let ((buffer-read-only nil)
8205           (marks (concat "^[" marks "]")))
8206       (goto-char (point-min))
8207       (while (search-forward-regexp marks (point-max) t)
8208         (beginning-of-line)
8209         (delete-region (point)
8210                        (progn (forward-line 1) (point)))))
8211     (or (zerop (buffer-size))
8212         (if (eobp)
8213             (gnus-summary-prev-subject 1)
8214           (gnus-summary-position-cursor)))))
8215
8216 (defun gnus-summary-expunge-below (score)
8217   "Remove articles with score less than SCORE."
8218   (interactive "P")
8219   (setq score (if score
8220                   (prefix-numeric-value score)
8221                 (or gnus-summary-default-score 0)))
8222   (save-excursion
8223     (set-buffer gnus-summary-buffer)
8224     (goto-char (point-min))
8225     (let ((buffer-read-only nil)
8226           beg)
8227       (while (not (eobp))
8228         (if (< (gnus-summary-article-score) score)
8229             (progn
8230               (setq beg (point))
8231               (forward-line 1)
8232               (delete-region beg (point)))
8233           (forward-line 1)))
8234       ;; Adjust point.
8235       (or (zerop (buffer-size))
8236           (if (eobp)
8237               (gnus-summary-prev-subject 1)
8238             (gnus-summary-position-cursor))))))
8239
8240 (defun gnus-summary-mark-below (score mark)
8241   "Mark articles with score less than SCORE with MARK."
8242   (interactive "P\ncMark: ")
8243   (setq score (if score
8244                   (prefix-numeric-value score)
8245                 (or gnus-summary-default-score 0)))
8246   (save-excursion
8247     (set-buffer gnus-summary-buffer)
8248     (goto-char (point-min))
8249     (while (not (eobp))
8250       (and (< (gnus-summary-article-score) score)
8251            (gnus-summary-mark-article nil mark))
8252       (forward-line 1))))
8253
8254 (defun gnus-summary-kill-below (score)
8255   "Mark articles with score below SCORE as read."
8256   (interactive "P")
8257   (gnus-summary-mark-below score gnus-killed-mark))
8258
8259 (defun gnus-summary-clear-above (score)
8260   "Clear all marks from articles with score above SCORE."
8261   (interactive "P")
8262   (gnus-summary-mark-above score gnus-unread-mark))
8263
8264 (defun gnus-summary-tick-above (score)
8265   "Tick all articles with score above SCORE."
8266   (interactive "P")
8267   (gnus-summary-mark-above score gnus-ticked-mark))
8268
8269 (defun gnus-summary-mark-above (score mark)
8270   "Mark articles with score over SCORE with MARK."
8271   (interactive "P\ncMark: ")
8272   (setq score (if score
8273                   (prefix-numeric-value score)
8274                 (or gnus-summary-default-score 0)))
8275   (save-excursion
8276     (set-buffer gnus-summary-buffer)
8277     (goto-char (point-min))
8278     (while (not (eobp))
8279       (if (> (gnus-summary-article-score) score)
8280           (progn
8281             (gnus-summary-mark-article nil mark)
8282             (forward-line 1))
8283         (forward-line 1)))))
8284
8285 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
8286 (defun gnus-summary-show-all-expunged ()
8287   "Display all the hidden articles that were expunged for low scores."
8288   (interactive)
8289   (let ((buffer-read-only nil))
8290     (let ((scored gnus-newsgroup-scored)
8291           headers h)
8292       (while scored
8293         (or (gnus-summary-goto-subject (car (car scored)))
8294             (and (setq h (gnus-get-header-by-number (car (car scored))))
8295                  (< (cdr (car scored)) gnus-summary-expunge-below)
8296                  (setq headers (cons h headers))))
8297         (setq scored (cdr scored)))
8298       (or headers (error "No expunged articles hidden."))
8299       (goto-char (point-min))
8300       (save-excursion 
8301         (gnus-summary-prepare-threads (nreverse headers) 0)))
8302     (goto-char (point-min))
8303     (gnus-summary-position-cursor)))
8304
8305 (defun gnus-summary-show-all-dormant ()
8306   "Display all the hidden articles that are marked as dormant."
8307   (interactive)
8308   (let ((buffer-read-only nil))
8309     (goto-char (point-min))
8310     (let ((dormant gnus-newsgroup-dormant)
8311           headers h)
8312       (while dormant
8313         (or (gnus-summary-goto-subject (car dormant))
8314             (and (setq h (gnus-get-header-by-number (car dormant)))
8315                  (setq headers (cons h headers))))
8316         (setq dormant (cdr dormant)))
8317       (or headers (error "No dormant articles hidden."))
8318       (save-excursion (gnus-summary-prepare-threads (nreverse headers) 0)))
8319     (goto-char (point-min))
8320     (gnus-summary-position-cursor)))
8321
8322 (defun gnus-summary-hide-all-dormant ()
8323   "Hide all dormant articles."
8324   (interactive)
8325   (gnus-summary-remove-lines-marked-with (char-to-string gnus-dormant-mark))
8326   (gnus-summary-position-cursor))
8327
8328 (defun gnus-summary-catchup (all &optional quietly to-here)
8329   "Mark all articles not marked as unread in this newsgroup as read.
8330 If prefix argument ALL is non-nil, all articles are marked as read.
8331 If QUIETLY is non-nil, no questions will be asked.
8332 If TO-HERE is non-nil, it should be a point in the buffer. All
8333 articles before this point will be marked as read.
8334 The number of articles marked as read is returned."
8335   (interactive "P")
8336   (prog1
8337       (if (or quietly
8338               (not gnus-interactive-catchup) ;Without confirmation?
8339               gnus-expert-user
8340               (gnus-y-or-n-p
8341                (if all
8342                    "Mark absolutely all articles as read? "
8343                  "Mark all unread articles as read? ")))
8344           (let ((unreads (length gnus-newsgroup-unreads)))
8345             (if (gnus-summary-first-subject (not all))
8346                 (while (and (gnus-summary-mark-as-read nil gnus-catchup-mark)
8347                             (if to-here (< (point) to-here) t)
8348                             (gnus-summary-search-subject nil (not all)))))
8349             (- unreads (length gnus-newsgroup-unreads))
8350             (or to-here
8351                 (setq gnus-newsgroup-unreads gnus-newsgroup-marked))))
8352     (gnus-summary-position-cursor)))
8353
8354 (defun gnus-summary-catchup-to-here (&optional all)
8355   "Mark all unticked articles before the current one as read.
8356 If ALL is non-nil, also mark ticked and dormant articles as read."
8357   (interactive)
8358   (beginning-of-line)
8359   (gnus-summary-catchup all t (point))
8360   (gnus-summary-position-cursor))
8361
8362 (defun gnus-summary-catchup-all (&optional quietly)
8363   "Mark all articles in this newsgroup as read."
8364   (interactive)
8365   (gnus-summary-catchup t quietly))
8366
8367 (defun gnus-summary-catchup-and-exit (all &optional quietly)
8368   "Mark all articles not marked as unread in this newsgroup as read, then exit.
8369 If prefix argument ALL is non-nil, all articles are marked as read."
8370   (interactive "P")
8371   (gnus-summary-catchup all quietly)
8372   ;; Select next newsgroup or exit.
8373   (if (eq gnus-auto-select-next 'quietly)
8374       (gnus-summary-next-group nil)
8375     (gnus-summary-exit)))
8376
8377 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8378   "Mark all articles in this newsgroup as read, and then exit."
8379   (interactive)
8380   (gnus-summary-catchup-and-exit t quietly))
8381
8382 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8383 (defun gnus-summary-catchup-and-goto-next-group (all)
8384   "Mark all articles in this group as read and select the next group.
8385 If given a prefix, mark all articles, unread as well as ticked, as
8386 read." 
8387   (interactive "P")
8388   (gnus-summary-catchup all)
8389   (gnus-summary-next-group))
8390
8391 ;; Thread-based commands.
8392
8393 (defun gnus-summary-toggle-threads (arg)
8394   "Toggle showing conversation threads.
8395 If ARG is positive number, turn showing conversation threads on."
8396   (interactive "P")
8397   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8398     (setq gnus-show-threads
8399           (if (null arg) (not gnus-show-threads)
8400             (> (prefix-numeric-value arg) 0)))
8401     (gnus-summary-prepare)
8402     (gnus-summary-goto-subject current)
8403     (gnus-summary-position-cursor)))
8404
8405 (defun gnus-summary-show-all-threads ()
8406   "Show all threads."
8407   (interactive)
8408   (save-excursion
8409     (let ((buffer-read-only nil))
8410       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8411   (gnus-summary-position-cursor))
8412
8413 (defun gnus-summary-show-thread ()
8414   "Show thread subtrees.
8415 Returns nil if no thread was there to be shown."
8416   (interactive)
8417   (prog1
8418       (save-excursion
8419         (let ((buffer-read-only nil)
8420               ;; first goto end then to beg, to have point at beg after let
8421               (end (progn (end-of-line) (point)))
8422               (beg (progn (beginning-of-line) (point))))
8423           (prog1
8424               ;; Any hidden lines here?
8425               (search-forward "\r" end t)
8426             (subst-char-in-region beg end ?\^M ?\n t))))
8427     (gnus-summary-position-cursor)))
8428
8429 (defun gnus-summary-hide-all-threads ()
8430   "Hide all thread subtrees."
8431   (interactive)
8432   (save-excursion
8433     (goto-char (point-min))
8434     (gnus-summary-hide-thread)
8435     (while (and (not (eobp)) (zerop (forward-line 1)))
8436       (gnus-summary-hide-thread)))
8437   (gnus-summary-position-cursor))
8438
8439 (defun gnus-summary-hide-thread ()
8440   "Hide thread subtrees.
8441 Returns nil if no threads were there to be hidden."
8442   (interactive)
8443   (let ((buffer-read-only nil)
8444         (start (point))
8445         (level (gnus-summary-thread-level))
8446         (end (point)))
8447     ;; Go forward until either the buffer ends or the subthread
8448     ;; ends. 
8449     (if (eobp)
8450         ()
8451       (while (and (zerop (forward-line 1))
8452                   (> (gnus-summary-thread-level) level))
8453         (setq end (point)))
8454       (prog1
8455           (save-excursion
8456             (goto-char end)
8457             (search-backward "\n" start t))
8458         (subst-char-in-region start end ?\n ?\^M t)
8459         (forward-line -1)))))
8460
8461 (defun gnus-summary-go-to-next-thread (&optional previous)
8462   "Go to the same level (or less) next thread.
8463 If PREVIOUS is non-nil, go to previous thread instead.
8464 Return the article number moved to, or nil if moving was impossible."
8465   (let ((level (gnus-summary-thread-level))
8466         (article (gnus-summary-article-number)))
8467     (if previous 
8468         (while (and (zerop (gnus-summary-prev-subject 1))
8469                     (> (gnus-summary-thread-level) level)))
8470       (while (and (zerop (gnus-summary-next-subject 1))
8471                   (> (gnus-summary-thread-level) level))))
8472     (let ((oart (gnus-summary-article-number)))
8473       (and (/= oart article) oart))))
8474
8475 (defun gnus-summary-next-thread (n)
8476   "Go to the same level next N'th thread.
8477 If N is negative, search backward instead.
8478 Returns the difference between N and the number of skips actually
8479 done."
8480   (interactive "p")
8481   (let ((backward (< n 0))
8482         (n (abs n)))
8483   (while (and (> n 0)
8484               (gnus-summary-go-to-next-thread backward))
8485     (setq n (1- n)))
8486   (gnus-summary-position-cursor)
8487   (if (/= 0 n) (message "No more threads"))
8488   n))
8489
8490 (defun gnus-summary-prev-thread (n)
8491   "Go to the same level previous N'th thread.
8492 Returns the difference between N and the number of skips actually
8493 done."
8494   (interactive "p")
8495   (gnus-summary-next-thread (- n)))
8496
8497 (defun gnus-summary-go-down-thread (&optional same)
8498   "Go down one level in the current thread.
8499 If SAME is non-nil, also move to articles of the same level."
8500   (let ((level (gnus-summary-thread-level))
8501         (start (point)))
8502     (if (and (zerop (forward-line 1))
8503              (> (gnus-summary-thread-level) level))
8504         t
8505       (goto-char start)
8506       nil)))
8507
8508 (defun gnus-summary-go-up-thread ()
8509   "Go up one level in the current thread."
8510   (let ((level (gnus-summary-thread-level))
8511         (start (point)))
8512     (while (and (zerop (forward-line -1))
8513                 (>= (gnus-summary-thread-level) level)))
8514     (if (>= (gnus-summary-thread-level) level)
8515         (progn
8516           (goto-char start)
8517           nil)
8518       t)))
8519
8520 (defun gnus-summary-down-thread (n)
8521   "Go down thread N steps.
8522 If N is negative, go up instead.
8523 Returns the difference between N and how many steps down that were
8524 taken."
8525   (interactive "p")
8526   (let ((up (< n 0))
8527         (n (abs n)))
8528   (while (and (> n 0)
8529               (if up (gnus-summary-go-up-thread)
8530                 (gnus-summary-go-down-thread)))
8531     (setq n (1- n)))
8532   (gnus-summary-position-cursor)
8533   (if (/= 0 n) (message "Can't go further"))
8534   n))
8535
8536 (defun gnus-summary-up-thread (n)
8537   "Go up thread N steps.
8538 If N is negative, go up instead.
8539 Returns the difference between N and how many steps down that were
8540 taken."
8541   (interactive "p")
8542   (gnus-summary-down-thread (- n)))
8543
8544 (defun gnus-summary-kill-thread (unmark)
8545   "Mark articles under current thread as read.
8546 If the prefix argument is positive, remove any kinds of marks.
8547 If the prefix argument is negative, tick articles instead."
8548   (interactive "P")
8549   (if unmark
8550       (setq unmark (prefix-numeric-value unmark)))
8551   (let ((killing t)
8552         (level (gnus-summary-thread-level)))
8553     (save-excursion
8554       (while killing
8555         ;; Mark the article...
8556         (cond ((null unmark) (gnus-summary-mark-as-read nil gnus-killed-mark))
8557               ((> unmark 0) (gnus-summary-tick-article nil t))
8558               (t (gnus-summary-tick-article)))
8559         ;; ...and go forward until either the buffer ends or the subtree
8560         ;; ends. 
8561         (if (not (and (zerop (forward-line 1))
8562                       (> (gnus-summary-thread-level) level)))
8563             (setq killing nil))))
8564     ;; Hide killed subtrees.
8565     (and (null unmark)
8566          gnus-thread-hide-killed
8567          (gnus-summary-hide-thread))
8568     ;; If marked as read, go to next unread subject.
8569     (if (null unmark)
8570         ;; Go to next unread subject.
8571         (gnus-summary-next-subject 1 t)))
8572   (gnus-set-mode-line 'summary))
8573
8574 ;; Summary sorting commands
8575
8576 (defun gnus-summary-sort-by-number (reverse)
8577   "Sort summary buffer by article number.
8578 Argument REVERSE means reverse order."
8579   (interactive "P")
8580   (gnus-summary-sort 
8581    (cons 'gnus-summary-article-number 'gnus-thread-sort-by-number) reverse))
8582
8583 (defun gnus-summary-sort-by-author (reverse)
8584   "Sort summary buffer by author name alphabetically.
8585 If case-fold-search is non-nil, case of letters is ignored.
8586 Argument REVERSE means reverse order."
8587   (interactive "P")
8588   (gnus-summary-sort
8589    (cons
8590     (lambda ()
8591       (let ((extract (funcall
8592                       gnus-extract-address-components
8593                       (header-from (gnus-get-header-by-number
8594                                     (gnus-summary-article-number))))))
8595         (or (car extract) (cdr extract))))
8596     'gnus-thread-sort-by-author)
8597    reverse))
8598
8599 (defun gnus-summary-sort-by-subject (reverse)
8600   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
8601 If case-fold-search is non-nil, case of letters is ignored.
8602 Argument REVERSE means reverse order."
8603   (interactive "P")
8604   (gnus-summary-sort
8605    (cons
8606     (lambda ()
8607       (downcase (gnus-simplify-subject (gnus-summary-subject-string))))
8608     'gnus-thread-sort-by-subject)
8609    reverse))
8610
8611 (defun gnus-summary-sort-by-date (reverse)
8612   "Sort summary buffer by date.
8613 Argument REVERSE means reverse order."
8614   (interactive "P")
8615   (gnus-summary-sort
8616    (cons
8617     (lambda ()
8618       (gnus-sortable-date
8619        (header-date 
8620         (gnus-get-header-by-number (gnus-summary-article-number)))))
8621     'gnus-thread-sort-by-date)
8622    reverse))
8623
8624 (defun gnus-summary-sort-by-score (reverse)
8625   "Sort summary buffer by score.
8626 Argument REVERSE means reverse order."
8627   (interactive "P")
8628   (gnus-summary-sort 
8629    (cons 'gnus-summary-article-score 'gnus-thread-sort-by-score)
8630    (not reverse)))
8631
8632 (defvar gnus-summary-already-sorted nil)
8633 (defun gnus-summary-sort (predicate reverse)
8634   ;; Sort summary buffer by PREDICATE.  REVERSE means reverse order. 
8635   (if gnus-summary-already-sorted
8636       ()
8637     (let (buffer-read-only)
8638       (if (not gnus-show-threads)
8639           (progn
8640             (goto-char (point-min))
8641             (sort-subr reverse 'forward-line 'end-of-line (car predicate)))
8642         (let ((gnus-thread-sort-functions (list (cdr predicate)))
8643               (gnus-summary-already-sorted nil))
8644           (gnus-summary-prepare))))))
8645   
8646 (defun gnus-sortable-date (date)
8647   "Make sortable string by string-lessp from DATE.
8648 Timezone package is used."
8649   (let* ((date   (timezone-fix-time date nil nil)) ;[Y M D H M S]
8650          (year   (aref date 0))
8651          (month  (aref date 1))
8652          (day    (aref date 2)))
8653     (timezone-make-sortable-date 
8654      year month day 
8655      (timezone-make-time-string
8656       (aref date 3) (aref date 4) (aref date 5)))))
8657
8658
8659 ;; Summary saving commands.
8660
8661 (defun gnus-summary-save-article (n)
8662   "Save the current article using the default saver function.
8663 If N is a positive number, save the N next articles.
8664 If N is a negative number, save the N previous articles.
8665 If N is nil and any articles have been marked with the process mark,
8666 save those articles instead.
8667 The variable `gnus-default-article-saver' specifies the saver function."
8668   (interactive "P")
8669   (let (articles process)
8670     (if (and n (numberp n))
8671         (let ((backward (< n 0))
8672               (n (abs n)))
8673           (save-excursion
8674             (while (and (> n 0)
8675                         (setq articles (cons (gnus-summary-article-number) 
8676                                              articles))
8677                         (gnus-summary-search-forward nil nil backward))
8678               (setq n (1- n))))
8679           (setq articles (sort articles (function <))))
8680       (if gnus-newsgroup-processable
8681           (progn
8682             (setq articles (setq gnus-newsgroup-processable
8683                                  (nreverse gnus-newsgroup-processable)))
8684             (setq process t))
8685         (setq articles (list (gnus-summary-article-number)))))
8686     (while articles
8687       (let ((header (gnus-gethash (int-to-string (car articles))
8688                                   gnus-newsgroup-headers-hashtb-by-number)))
8689         (if (vectorp header)
8690             (progn
8691               (gnus-summary-select-article t nil nil (car articles))
8692               (or gnus-save-all-headers
8693                   (gnus-article-hide-headers t))
8694               ;; Remove any X-Gnus lines.
8695               (save-excursion
8696                 (save-restriction
8697                   (set-buffer gnus-article-buffer)
8698                   (let ((buffer-read-only nil))
8699                     (goto-char (point-min))
8700                     (narrow-to-region (point) (or (search-forward "\n\n" nil t)
8701                                                   (point-max)))
8702                     (while (re-search-forward "^X-Gnus" nil t)
8703                       (beginning-of-line)
8704                       (delete-region (point)
8705                                      (progn (forward-line 1) (point))))
8706                     (widen))))
8707               (if gnus-default-article-saver
8708                   (funcall gnus-default-article-saver)
8709                 (error "No default saver is defined.")))
8710           (if (assq 'name header)
8711               (gnus-copy-file (cdr (assq 'name header)))
8712             (message "Article %d is unsaveable" (car articles)))))
8713       (if process
8714           (gnus-summary-remove-process-mark (car articles)))
8715       (setq articles (cdr articles)))
8716     (if process (setq gnus-newsgroup-processable 
8717                       (nreverse gnus-newsgroup-processable)))
8718     (gnus-summary-position-cursor)
8719     n))
8720
8721 (defun gnus-summary-pipe-output (arg)
8722   "Pipe the current article to a subprocess.
8723 If N is a positive number, pipe the N next articles.
8724 If N is a negative number, pipe the N previous articles.
8725 If N is nil and any articles have been marked with the process mark,
8726 pipe those articles instead."
8727   (interactive "P")
8728   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8729     (gnus-summary-save-article arg)))
8730
8731 (defun gnus-summary-save-article-mail (arg)
8732   "Append the current article to an mail file.
8733 If N is a positive number, save the N next articles.
8734 If N is a negative number, save the N previous articles.
8735 If N is nil and any articles have been marked with the process mark,
8736 save those articles instead."
8737   (interactive "P")
8738   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8739     (gnus-summary-save-article arg)))
8740
8741 (defun gnus-summary-save-article-rmail (arg)
8742   "Append the current article to an rmail file.
8743 If N is a positive number, save the N next articles.
8744 If N is a negative number, save the N previous articles.
8745 If N is nil and any articles have been marked with the process mark,
8746 save those articles instead."
8747   (interactive "P")
8748   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
8749     (gnus-summary-save-article arg)))
8750
8751 (defun gnus-summary-save-article-file (arg)
8752   "Append the current article to a file.
8753 If N is a positive number, save the N next articles.
8754 If N is a negative number, save the N previous articles.
8755 If N is nil and any articles have been marked with the process mark,
8756 save those articles instead."
8757   (interactive "P")
8758   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8759     (gnus-summary-save-article arg)))
8760
8761 (defun gnus-summary-save-article-folder (arg)
8762   "Append the current article to an mh folder.
8763 If N is a positive number, save the N next articles.
8764 If N is a negative number, save the N previous articles.
8765 If N is nil and any articles have been marked with the process mark,
8766 save those articles instead."
8767   (interactive "P")
8768   (let ((gnus-default-article-saver 'gnus-summary-save-in-folder))
8769     (gnus-summary-save-article arg)))
8770
8771 (defun gnus-read-save-file-name (prompt default-name)
8772   (let ((methods gnus-split-methods)
8773         split-name)
8774     (if (not gnus-split-methods)
8775         ()
8776       (save-excursion
8777         (set-buffer gnus-article-buffer)
8778         (gnus-narrow-to-headers)
8779         (while methods
8780           (goto-char (point-min))
8781           (and (condition-case () 
8782                    (re-search-forward (car (car methods)) nil t)
8783                  (error nil))
8784                (setq split-name (cons (nth 1 (car methods)) split-name)))
8785           (setq methods (cdr methods)))
8786         (widen)))
8787     (cond ((null split-name)
8788            (read-file-name
8789             (concat prompt " (default "
8790                     (file-name-nondirectory default-name) ") ")
8791             (file-name-directory default-name)
8792             default-name))
8793           ((= 1 (length split-name))
8794            (read-file-name
8795             (concat prompt " (default " (car split-name) ") ")
8796             gnus-article-save-directory
8797             (concat gnus-article-save-directory (car split-name))))
8798           (t
8799            (setq split-name (mapcar (lambda (el) (list el))
8800                                     (nreverse split-name)))
8801            (let ((result (completing-read 
8802                           (concat prompt " ")
8803                           split-name nil nil)))
8804              (concat gnus-article-save-directory
8805                      (if (string= result "")
8806                          (car (car split-name))
8807                        result)))))))
8808
8809 (defun gnus-summary-save-in-rmail (&optional filename)
8810   "Append this article to Rmail file.
8811 Optional argument FILENAME specifies file name.
8812 Directory to save to is default to `gnus-article-save-directory' which
8813 is initialized from the SAVEDIR environment variable."
8814   (interactive)
8815   (let ((default-name
8816           (funcall gnus-rmail-save-name gnus-newsgroup-name
8817                    gnus-current-headers gnus-newsgroup-last-rmail)))
8818     (or filename
8819         (setq filename (gnus-read-save-file-name 
8820                         "Save in rmail file:" default-name)))
8821     (gnus-make-directory (file-name-directory filename))
8822     (gnus-eval-in-buffer-window 
8823      gnus-article-buffer
8824      (save-excursion
8825        (save-restriction
8826          (widen)
8827          (gnus-output-to-rmail filename))))
8828     ;; Remember the directory name to save articles.
8829     (setq gnus-newsgroup-last-rmail filename)))
8830
8831 (defun gnus-summary-save-in-mail (&optional filename)
8832   "Append this article to Unix mail file.
8833 Optional argument FILENAME specifies file name.
8834 Directory to save to is default to `gnus-article-save-directory' which
8835 is initialized from the SAVEDIR environment variable."
8836   (interactive)
8837   (let ((default-name
8838           (funcall gnus-mail-save-name gnus-newsgroup-name
8839                    gnus-current-headers gnus-newsgroup-last-mail)))
8840     (or filename
8841         (setq filename (gnus-read-save-file-name 
8842                         "Save in Unix mail file:" default-name)))
8843     (setq filename
8844           (expand-file-name filename
8845                             (and default-name
8846                                  (file-name-directory default-name))))
8847     (gnus-make-directory (file-name-directory filename))
8848     (gnus-eval-in-buffer-window 
8849      gnus-article-buffer
8850      (save-excursion
8851        (save-restriction
8852          (widen)
8853          (if (and (file-readable-p filename) (rmail-file-p filename))
8854              (gnus-output-to-rmail filename)
8855            (rmail-output filename 1 t t)))))
8856     ;; Remember the directory name to save articles.
8857     (setq gnus-newsgroup-last-mail filename)))
8858
8859 (defun gnus-summary-save-in-file (&optional filename)
8860   "Append this article to file.
8861 Optional argument FILENAME specifies file name.
8862 Directory to save to is default to `gnus-article-save-directory' which
8863 is initialized from the SAVEDIR environment variable."
8864   (interactive)
8865   (let ((default-name
8866           (funcall gnus-file-save-name gnus-newsgroup-name
8867                    gnus-current-headers gnus-newsgroup-last-file)))
8868     (or filename
8869         (setq filename (gnus-read-save-file-name 
8870                         "Save in file:" default-name)))
8871     (gnus-make-directory (file-name-directory filename))
8872     (gnus-eval-in-buffer-window 
8873      gnus-article-buffer
8874      (save-excursion
8875        (save-restriction
8876          (widen)
8877          (gnus-output-to-file filename))))
8878     ;; Remember the directory name to save articles.
8879     (setq gnus-newsgroup-last-file filename)))
8880
8881 (defun gnus-summary-save-in-pipe (&optional command)
8882   "Pipe this article to subprocess."
8883   (interactive)
8884   (let ((command (read-string "Shell command on article: "
8885                               gnus-last-shell-command)))
8886     (if (string-equal command "")
8887         (setq command gnus-last-shell-command))
8888     (gnus-eval-in-buffer-window 
8889      gnus-article-buffer
8890      (save-restriction
8891        (widen)
8892        (shell-command-on-region (point-min) (point-max) command nil)))
8893     (setq gnus-last-shell-command command)))
8894
8895 ;; Summary extract commands
8896
8897 (defun gnus-summary-insert-pseudos (pslist)
8898   (let ((buffer-read-only nil)
8899         (article (gnus-summary-article-number))
8900         b)
8901     (or (gnus-summary-goto-subject article)
8902         (error (format "No such article: %d" article)))
8903     (or gnus-newsgroup-headers-hashtb-by-number
8904         (gnus-make-headers-hashtable-by-number))
8905     (gnus-summary-position-cursor)
8906     (if gnus-view-pseudos
8907         (while pslist
8908           (and (assq 'execute (car pslist))
8909                (gnus-execute-command (cdr (assq 'execute (car pslist)))
8910                                      (eq gnus-view-pseudos 'not-confirm)))
8911           (setq pslist (cdr pslist)))
8912       (save-excursion
8913         (while pslist
8914           (gnus-summary-goto-subject (or (cdr (assq 'article (car pslist)))
8915                                          (gnus-summary-article-number)))
8916           (forward-line 1)
8917           (setq b (point))
8918           (insert "          " (file-name-nondirectory 
8919                                 (cdr (assq 'name (car pslist))))
8920                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
8921           (add-text-properties 
8922            b (1+ b) (list 'gnus-subject (cdr (assq 'name (car pslist)))
8923                           'gnus-number gnus-reffed-article-number
8924                           'gnus-mark gnus-unread-mark
8925                           'gnus-pseudo (car pslist)
8926                           'gnus-thread 0))
8927           (forward-line -1)
8928           (gnus-sethash (int-to-string gnus-reffed-article-number)
8929                         (car pslist) gnus-newsgroup-headers-hashtb-by-number)
8930           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
8931           (setq pslist (cdr pslist)))))))
8932
8933 (defun gnus-request-pseudo-article (props)
8934   (cond ((assq 'execute props)
8935          (gnus-execute-command (cdr (assq 'execute props)))))
8936   (let ((gnus-current-article (gnus-summary-article-number)))
8937     (run-hooks 'gnus-mark-article-hook)))
8938
8939 (defun gnus-execute-command (command &optional automatic)
8940   (save-excursion
8941     (gnus-article-setup-buffer)
8942     (set-buffer gnus-article-buffer)
8943     (let ((command (if automatic command (read-string "Command: " command)))
8944           (buffer-read-only nil))
8945       (erase-buffer)
8946       (insert "$ " command "\n\n")
8947       (if gnus-view-pseudo-asynchronously
8948           (start-process "gnus-execute" nil "sh" "-c" command)
8949         (call-process "sh" nil t nil "-c" command)))))
8950
8951 (defun gnus-copy-file (file &optional to)
8952   "Copy FILE to TO."
8953   (interactive
8954    (list (read-file-name "Copy file: " default-directory)
8955          (read-file-name "Copy file to: " default-directory)))
8956   (or to (setq to (read-file-name "Copy file to: " default-directory)))
8957   (and (file-directory-p to) 
8958        (setq to (concat (file-name-as-directory to)
8959                         (file-name-nondirectory file))))
8960   (copy-file file to))
8961
8962 ;; Summary score file commands
8963
8964 ;; Much modification of the kill (ahem, score) code and lots of the
8965 ;; functions are written by Per Abrahamsen <amanda@iesd.auc.dk>.
8966
8967 (defun gnus-summary-header (header)
8968   ;; Return HEADER for current articles, or error.
8969   (let ((article (gnus-summary-article-number)))
8970     (if article
8971         (aref (gnus-get-header-by-number article)
8972               (nth 1 (assoc header gnus-header-index)))
8973       (error "No article on current line"))))
8974
8975 (defun gnus-summary-score-entry (header match type score date &optional prompt)
8976   "Enter score file entry.
8977 HEADER is the header being scored.
8978 MATCH is the string we are looking for.
8979 TYPE is a flag indicating if it is a regexp or substring.
8980 SCORE is the score to add.
8981 DATE is the expire date."
8982   (interactive
8983    (list (completing-read "Header: "
8984                           gnus-header-index
8985                           (lambda (x) (fboundp (nth 2 x)))
8986                           t)
8987          (read-string "Match: ")
8988          (y-or-n-p "Use regexp match? ")
8989          (prefix-numeric-value current-prefix-arg)
8990          (if (y-or-n-p "Expire kill? ")
8991              (current-time-string)
8992            nil)))
8993   (let ((score (gnus-score-default score))
8994         (header (downcase header)))
8995     (and prompt (setq match (read-string 
8996                              (format "Match %s on %s, %s: " 
8997                                      (if date "temp" "permanent") 
8998                                      header
8999                                      (if (< score 0) "lower" "raise"))
9000                              match)))
9001     (and (>= (nth 1 (assoc header gnus-header-index)) 0)
9002          (gnus-summary-score-effect header match type score))
9003     (and (= score gnus-score-interactive-default-score)
9004          (setq score nil))
9005     (let ((new (cond (type
9006                   (list match score (and date (gnus-day-number date)) type))
9007                  (date
9008                   (list match score (gnus-day-number date)))
9009                  (score
9010                   (list match score))
9011                  (t
9012                   (list match))))
9013           (old (gnus-score-get header)))
9014       (gnus-score-set
9015        header
9016        (if old (cons new old) (list new))))
9017     (gnus-score-set 'touched '(t))))
9018
9019 (defun gnus-summary-score-effect (header match type score)
9020   "Simulate the effect of a score file entry.
9021 HEADER is the header being scored.
9022 MATCH is the string we are looking for.
9023 TYPE is a flag indicating if it is a regexp or substring.
9024 SCORE is the score to add."
9025   (interactive (list (completing-read "Header: "
9026                                       gnus-header-index
9027                                       (lambda (x) (fboundp (nth 2 x)))
9028                                       t)
9029                      (read-string "Match: ")
9030                      (y-or-n-p "Use regexp match? ")
9031                      (prefix-numeric-value current-prefix-arg)))
9032   (save-excursion
9033     (or (and (stringp match) (> (length match) 0))
9034       (error "No match"))
9035     (goto-char (point-min))
9036     (let ((regexp (if type
9037                       match
9038                     (concat "\\`.*" (regexp-quote match) ".*\\'"))))
9039       (while (not (eobp))
9040         (let ((content (gnus-summary-header header))
9041               (case-fold-search t))
9042           (and content
9043                (if (string-match regexp content)
9044                    (gnus-summary-raise-score score))))
9045         (beginning-of-line 2)))))
9046
9047 (defun gnus-summary-score-crossposting (score date)
9048    ;; Enter score file entry for current crossposting.
9049    ;; SCORE is the score to add.
9050    ;; DATE is the expire date.
9051    (let ((xref (gnus-summary-header "xref"))
9052          (start 0)
9053          group)
9054      (or xref (error "This article is not crossposted"))
9055      (while (string-match " \\([^ \t]+\\):" xref start)
9056        (setq start (match-end 0))
9057        (if (not (string= 
9058                  (setq group 
9059                        (substring xref (match-beginning 1) (match-end 1)))
9060                  gnus-newsgroup-name))
9061            (gnus-summary-score-entry
9062             "xref" (concat " " group ":") nil score date t)))))
9063
9064 (defun gnus-summary-temporarily-lower-by-subject (level)
9065   "Temporarily lower score by LEVEL for current subject.
9066 See `gnus-score-expiry-days'."
9067   (interactive "P")
9068   (gnus-summary-score-entry
9069    "subject" (gnus-simplify-subject-re (gnus-summary-header "subject"))
9070    nil (- (gnus-score-default level))
9071    (current-time-string) t))
9072
9073 (defun gnus-summary-temporarily-lower-by-author (level)
9074   "Temporarily lower score by LEVEL for current author.
9075 See `gnus-score-expiry-days'."
9076   (interactive "P")
9077   (gnus-summary-score-entry
9078    "from" (gnus-summary-header "from") nil (- (gnus-score-default level)) 
9079    (current-time-string) t))
9080
9081 (defun gnus-summary-temporarily-lower-by-body (level)
9082   "Temporarily lower score by LEVEL for a match on the body of the article.
9083 See `gnus-score-expiry-days'."
9084   (interactive "P")
9085   (gnus-summary-score-entry
9086    "body" "" nil (- (gnus-score-default level)) (current-time-string) t))
9087
9088 (defun gnus-summary-temporarily-lower-by-id (level)
9089   "Temporarily lower score by LEVEL for current message-id.
9090 See `gnus-score-expiry-days'."
9091   (interactive "P")
9092   (gnus-summary-score-entry
9093    "message-id" (gnus-summary-header "message-id") 
9094    nil (- (gnus-score-default level)) 
9095    (current-time-string)))
9096
9097 (defun gnus-summary-temporarily-lower-by-xref (level)
9098   "Temporarily lower score by LEVEL for current xref.
9099 See `gnus-score-expiry-days'."
9100   (interactive "P")
9101   (gnus-summary-score-crossposting 
9102    (- (gnus-score-default level)) (current-time-string)))
9103
9104 (defun gnus-summary-temporarily-lower-by-thread (level)
9105   "Temporarily lower score by LEVEL for current thread.
9106 See `gnus-score-expiry-days'."
9107   (interactive "P")
9108   (gnus-summary-score-entry
9109    "references" (gnus-summary-header "message-id")
9110    nil (- (gnus-score-default level)) (current-time-string)))
9111
9112 (defun gnus-summary-lower-by-subject (level)
9113   "Lower score by LEVEL for current subject."
9114   (interactive "P")
9115   (gnus-summary-score-entry
9116    "subject" (gnus-simplify-subject-re (gnus-summary-header "subject"))
9117    nil (- (gnus-score-default level)) 
9118    nil t))
9119
9120 (defun gnus-summary-lower-by-author (level)
9121   "Lower score by LEVEL for current author."
9122   (interactive "P")
9123   (gnus-summary-score-entry
9124    "from" (gnus-summary-header "from") nil 
9125    (- (gnus-score-default level)) nil t))
9126
9127 (defun gnus-summary-lower-by-body (level)
9128   "Lower score by LEVEL for a match on the body of the article."
9129   (interactive "P")
9130   (gnus-summary-score-entry
9131    "body" "" nil (- (gnus-score-default level)) nil t))
9132
9133 (defun gnus-summary-lower-by-id (level)
9134   "Lower score by LEVEL for current message-id."
9135   (interactive "P")
9136   (gnus-summary-score-entry
9137    "message-id" (gnus-summary-header "message-id") nil 
9138    (- (gnus-score-default level)) nil))
9139
9140 (defun gnus-summary-lower-by-xref (level)
9141   "Lower score by LEVEL for current xref."
9142   (interactive "P")
9143   (gnus-summary-score-crossposting (- (gnus-score-default level)) nil))
9144
9145 (defun gnus-summary-lower-followups-to-author (level)
9146   "Lower score by LEVEL for all followups to the current author."
9147   (interactive "P")
9148   (gnus-summary-raise-followups-to-author
9149    (- (gnus-score-default level))))
9150
9151 (defun gnus-summary-temporarily-raise-by-subject (level)
9152   "Temporarily raise score by LEVEL for current subject.
9153 See `gnus-score-expiry-days'."
9154   (interactive "P")
9155   (gnus-summary-score-entry
9156    "subject" (gnus-simplify-subject-re (gnus-summary-header "subject"))
9157    nil level (current-time-string) t))
9158
9159 (defun gnus-summary-temporarily-raise-by-author (level)
9160   "Temporarily raise score by LEVEL for current author.
9161 See `gnus-score-expiry-days'."
9162   (interactive "P")
9163   (gnus-summary-score-entry
9164    "from" (gnus-summary-header "from") nil level (current-time-string) t))
9165
9166 (defun gnus-summary-temporarily-raise-by-body (level)
9167   "Temporarily raise score by LEVEL for a match on the body of the article.
9168 See `gnus-score-expiry-days'."
9169   (interactive "P")
9170   (gnus-summary-score-entry "body" "" nil level (current-time-string) t))
9171
9172 (defun gnus-summary-temporarily-raise-by-id (level)
9173   "Temporarily raise score by LEVEL for current message-id.
9174 See `gnus-score-expiry-days'."
9175   (interactive "P")
9176   (gnus-summary-score-entry
9177    "message-id" (gnus-summary-header "message-id") 
9178    nil level (current-time-string)))
9179
9180 (defun gnus-summary-temporarily-raise-by-xref (level)
9181   "Temporarily raise score by LEVEL for current xref.
9182 See `gnus-score-expiry-days'."
9183   (interactive "P")
9184   (gnus-summary-score-crossposting level (current-time-string)))
9185
9186 (defun gnus-summary-temporarily-raise-by-thread (level)
9187   "Temporarily raise score by LEVEL for current thread.
9188 See `gnus-score-expiry-days'."
9189   (interactive "P")
9190   (gnus-summary-score-entry
9191    "references" (gnus-summary-header "message-id")
9192    nil level (current-time-string)))
9193
9194 (defun gnus-summary-raise-by-subject (level)
9195   "Raise score by LEVEL for current subject."
9196   (interactive "P")
9197   (gnus-summary-score-entry
9198    "subject" (gnus-simplify-subject-re (gnus-summary-header "subject"))
9199    nil level nil t))
9200
9201 (defun gnus-summary-raise-by-author (level)
9202   "Raise score by LEVEL for current author."
9203   (interactive "P")
9204   (gnus-summary-score-entry
9205    "from" (gnus-summary-header "from") nil level nil t))
9206
9207 (defun gnus-summary-raise-by-body (level)
9208   "Raise score by LEVEL for a match on the body of the article."
9209   (interactive "P")
9210   (gnus-summary-score-entry "body" "" nil level nil t))
9211
9212 (defun gnus-summary-raise-by-id (level)
9213   "Raise score by LEVEL for current message-id."
9214   (interactive "P")
9215   (gnus-summary-score-entry
9216    "message-id" (gnus-summary-header "message-id") nil level nil))
9217
9218 (defun gnus-summary-raise-by-xref (level)
9219   "Raise score by LEVEL for current xref."
9220   (interactive "P")
9221   (gnus-summary-score-crossposting level nil))
9222
9223 (defun gnus-summary-raise-followups-to-author (level)
9224   "Raise score by LEVEL for all followups to the current author."
9225   (interactive "P")
9226   (let ((article (gnus-summary-article-number)))
9227     (if article (setq gnus-current-headers (gnus-get-header-by-number article))
9228       (error "No article on current line")))
9229   (gnus-kill-file-raise-followups-to-author
9230    (gnus-score-default level)))
9231
9232 ;; Summary kill commands.
9233
9234 (defun gnus-summary-edit-global-kill (article)
9235   "Edit the global score file."
9236   (interactive (list (gnus-summary-article-number)))
9237   (gnus-group-edit-global-kill article))
9238
9239 (defun gnus-summary-edit-local-kill ()
9240   "Edit a local score file applied to the current newsgroup."
9241   (interactive)
9242   (setq gnus-current-headers 
9243         (gnus-gethash 
9244          (int-to-string (gnus-summary-article-number))
9245          gnus-newsgroup-headers-hashtb-by-number))
9246   (gnus-set-global-variables)
9247   (gnus-group-edit-local-kill 
9248    (gnus-summary-article-number) gnus-newsgroup-name))
9249
9250 \f
9251 ;;;
9252 ;;; Gnus article mode
9253 ;;;
9254
9255 (put 'gnus-article-mode 'mode-class 'special)
9256
9257 (if gnus-article-mode-map
9258     nil
9259   (setq gnus-article-mode-map (make-keymap))
9260   (suppress-keymap gnus-article-mode-map)
9261   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
9262   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
9263   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
9264   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
9265   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
9266   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
9267   (define-key gnus-article-mode-map "\C-c\C-M" 'gnus-article-mail-with-original)
9268   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
9269   
9270   ;; Duplicate almost all summary keystrokes in the article mode map.
9271   (let ((commands 
9272          (list "#" "\M-#" "\C-c\M-#" "\r" "n" "p"
9273                "N" "P" "\M-\C-n" "\M-\C-p" "." "\M-s" "\M-r"
9274                "<" ">" "l" "j" "^" "\M-^" "-" "u" "U" "d" "D"
9275                "\M-u" "\M-U" "k" "\C-k" "\M-\C-k" "c" "x" "X" 
9276                "\M-\C-x" "\M-\177" "b" "B" "$" "w" "\C-c\C-r"
9277                "t" "\M-t" "a" "f" "F" "C" "S" "r" "R" "\C-c\C-f"
9278                "m" "o" "\C-o" "|" "\M-m" "\M-\C-m" "\M-k" "m" "M"
9279                "V" "\C-c\C-d" "q" "Q")))
9280     (while commands
9281       (define-key gnus-article-mode-map (car commands) 
9282         'gnus-article-summary-command)
9283       (setq commands (cdr commands)))))
9284
9285
9286 (defun gnus-article-mode ()
9287   "Major mode for displaying an article.
9288
9289 All normal editing commands are switched off.
9290
9291 The following commands are available:
9292
9293 \\<gnus-article-mode-map>
9294 \\[gnus-article-next-page]\t Scroll the article one page forwards
9295 \\[gnus-article-prev-page]\t Scroll the article one page backwards
9296 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
9297 \\[gnus-article-show-summary]\t Display the summary buffer
9298 \\[gnus-article-mail]\t Send a reply to the address near point
9299 \\[gnus-article-mail-with-original]\t Send a reply to the address near point; include the original article
9300 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
9301 \\[gnus-info-find-node]\t Go to the Gnus info node"
9302   (interactive)
9303   (if gnus-visual (gnus-article-make-menu-bar))
9304   (kill-all-local-variables)
9305   (setq mode-line-modified "-- ")
9306   (make-local-variable 'mode-line-format)
9307   (setq mode-line-format (copy-sequence mode-line-format))
9308   (and (equal (nth 3 mode-line-format) "   ")
9309        (setcar (nthcdr 3 mode-line-format) ""))
9310   (setq mode-name "Article")
9311   (setq major-mode 'gnus-article-mode)
9312   (make-local-variable 'minor-mode-alist)
9313   (or (assq 'gnus-show-mime minor-mode-alist)
9314       (setq minor-mode-alist
9315             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
9316   (use-local-map gnus-article-mode-map)
9317   (make-local-variable 'page-delimiter)
9318   (setq page-delimiter gnus-page-delimiter)
9319   (buffer-disable-undo (current-buffer))
9320   (setq buffer-read-only t)             ;Disable modification
9321   (run-hooks 'gnus-article-mode-hook))
9322
9323 (defun gnus-article-setup-buffer ()
9324   "Initialize article mode buffer."
9325   (or (get-buffer gnus-article-buffer)
9326       (save-excursion
9327         (set-buffer (get-buffer-create gnus-article-buffer))
9328         (gnus-add-current-to-buffer-list)
9329         (gnus-article-mode))))
9330
9331 (defun gnus-request-article-this-buffer (article &optional group)
9332   "Get an article and insert it into this buffer."
9333   (setq group (or group gnus-newsgroup-name))
9334   ;; Using `gnus-request-article' directly will insert the article into
9335   ;; `nntp-server-buffer' - so we'll save some time by not having to
9336   ;; copy it from the server buffer into the article buffer.
9337
9338   ;; We only request an article by message-id when we do not have the
9339   ;; headers for it, so we'll have to get those.
9340   (and (stringp article) 
9341        (let ((gnus-override-method gnus-refer-article-method))
9342          (gnus-read-header article)))
9343
9344   ;; If the article number is negative, that means that this article
9345   ;; doesn't belong in this newsgroup (possibly), so we find its
9346   ;; message-id and request it by id instead of number.
9347   (if (and (numberp article) (< article 0))
9348       (save-excursion
9349         (set-buffer gnus-summary-buffer)
9350         (let ((header (gnus-gethash (int-to-string article)
9351                                     gnus-newsgroup-headers-hashtb-by-number)))
9352           (if (vectorp header)
9353               ;; It's a real article.
9354               (setq article (header-id header))
9355             ;; It is an extracted pseudo-article.
9356             (setq article nil)
9357             (gnus-request-pseudo-article header)))))
9358   ;; Get the article and into the article buffer.
9359   (if article
9360       (progn
9361        (erase-buffer)
9362        (let ((gnus-override-method 
9363               (and (stringp article) gnus-refer-article-method)))
9364          (and (gnus-request-article article group (current-buffer))
9365               'article)))
9366     'pseudo))
9367
9368 (defun gnus-read-header (id)
9369   "Read the headers of article ID and enter them into the Gnus system."
9370   (or gnus-newsgroup-headers-hashtb-by-number
9371       (gnus-make-headers-hashtable-by-number))
9372   (let (header)
9373     (if (not (setq header 
9374                    (car (if (let ((gnus-nov-is-evil t))
9375                               (gnus-retrieve-headers 
9376                                (list id) gnus-newsgroup-name))
9377                             (gnus-get-newsgroup-headers)))))
9378         nil
9379       (if (stringp id)
9380           (header-set-number header gnus-reffed-article-number))
9381       (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers))
9382       (gnus-sethash (int-to-string (header-number header)) header
9383                     gnus-newsgroup-headers-hashtb-by-number)
9384       (if (stringp id)
9385           (setq gnus-reffed-article-number (1- gnus-reffed-article-number)))
9386       (setq gnus-current-headers header)
9387       header)))
9388
9389 (defun gnus-article-prepare (article &optional all-headers header)
9390   "Prepare ARTICLE in article mode buffer.
9391 ARTICLE should either be an article number or a Message-ID.
9392 If ARTICLE is an id, HEADER should be the article headers.
9393 If ALL-HEADERS is non-nil, no headers are hidden."
9394   (save-excursion
9395     ;; Make sure we start in a summary buffer.
9396     (or (eq major-mode 'gnus-summary-mode)
9397         (set-buffer gnus-summary-buffer))
9398     (setq gnus-summary-buffer (current-buffer))
9399     ;; Make sure the connection to the server is alive.
9400     (or (gnus-server-opened (gnus-find-method-for-group gnus-newsgroup-name))
9401         (progn
9402           (gnus-check-news-server 
9403            (gnus-find-method-for-group gnus-newsgroup-name))
9404           (gnus-request-group gnus-newsgroup-name t)))
9405     (or gnus-newsgroup-headers-hashtb-by-number
9406         (gnus-make-headers-hashtable-by-number))
9407     (let* ((article (if header (header-number header) article))
9408            (summary-buffer (current-buffer))
9409            (internal-hook gnus-article-internal-prepare-hook)
9410            (bookmark (cdr (assq article gnus-newsgroup-bookmarks)))
9411            (group gnus-newsgroup-name)
9412            result)
9413       (save-excursion
9414         (gnus-article-setup-buffer)
9415         (set-buffer gnus-article-buffer)
9416         (let ((buffer-read-only nil))
9417           (if (not (setq result (gnus-request-article-this-buffer 
9418                                  article group)))
9419               ;; There is no such article.
9420               (progn
9421                 (save-excursion
9422                   (set-buffer gnus-summary-buffer)
9423                   (setq gnus-current-article 0)
9424                   (and (numberp article) 
9425                        (gnus-summary-mark-as-read article gnus-canceled-mark))
9426                   (message "No such article (may be canceled)")
9427                   (ding))
9428                 (setq gnus-article-current nil)
9429                 nil)
9430             (if (not (eq result 'article))
9431                 (progn
9432                   (save-excursion
9433                     (set-buffer summary-buffer)
9434                     (setq gnus-last-article gnus-current-article
9435                           gnus-newsgroup-history (cons gnus-current-article
9436                                                        gnus-newsgroup-history)
9437                           gnus-current-article 0
9438                           gnus-current-headers nil
9439                           gnus-article-current nil)
9440                     (gnus-configure-windows 'article)
9441                     (gnus-set-global-variables))
9442                   (gnus-set-mode-line 'article))
9443               ;; The result from the `request' was an actual article -
9444               ;; or at least some text that is now displayed in the
9445               ;; article buffer.
9446               (if (and (numberp article)
9447                        (not (eq article gnus-current-article)))
9448                   ;; Seems like a new article has been selected.
9449                   ;; `gnus-current-article' must be an article number.
9450                   (save-excursion
9451                     (set-buffer summary-buffer)
9452                     (setq gnus-last-article gnus-current-article
9453                           gnus-newsgroup-history (cons gnus-current-article
9454                                                        gnus-newsgroup-history)
9455                           gnus-current-article article
9456                           gnus-current-headers 
9457                           (gnus-get-header-by-number gnus-current-article)
9458                           gnus-article-current 
9459                           (cons gnus-newsgroup-name gnus-current-article))
9460                     (run-hooks 'gnus-mark-article-hook)
9461                     (gnus-set-mode-line 'summary)
9462                     (and gnus-visual 
9463                          (run-hooks 'gnus-visual-mark-article-hook))
9464                     ;; Set the global newsgroup variables here.
9465                     ;; Suggested by Jim Sisolak
9466                     ;; <sisolak@trans4.neep.wisc.edu>.
9467                     (gnus-set-global-variables)))
9468               ;; gnus-have-all-headers must be either T or NIL.
9469               (setq gnus-have-all-headers
9470                     (not (not (or all-headers gnus-show-all-headers))))
9471               ;; Hooks for getting information from the article.
9472               ;; This hook must be called before being narrowed.
9473               (run-hooks 'internal-hook)
9474               (run-hooks 'gnus-article-prepare-hook)
9475               ;; Decode MIME message.
9476               (if (and gnus-show-mime
9477                        (gnus-fetch-field "Mime-Version"))
9478                   (funcall gnus-show-mime-method))
9479               ;; Perform the article display hooks.
9480               (let ((buffer-read-only nil))
9481                 (run-hooks 'gnus-article-display-hook))
9482               ;; Do page break.
9483               (goto-char (point-min))
9484               (and gnus-break-pages (gnus-narrow-to-page))
9485               (gnus-set-mode-line 'article)
9486               (gnus-configure-windows 'article)
9487               (goto-char 1)
9488               (set-window-start 
9489                (get-buffer-window gnus-article-buffer) (point-min))
9490               (if bookmark
9491                   (progn
9492                     (message "Moved to bookmark")
9493                     (search-forward "\n\n" nil t)
9494                     (forward-line bookmark)))
9495               t)))))))
9496
9497 (defun gnus-article-show-all-headers ()
9498   "Show all article headers in article mode buffer."
9499   (save-excursion 
9500     (setq gnus-have-all-headers t)
9501     (gnus-article-setup-buffer)
9502     (set-buffer gnus-article-buffer)
9503     (let ((buffer-read-only nil))
9504       (remove-text-properties 1 (point-max) '(invisible t)))))
9505
9506 (defun gnus-article-hide-headers-if-wanted ()
9507   "Hide unwanted headers if `gnus-have-all-headers' is nil.
9508 Provided for backwards compatability."
9509   (or gnus-have-all-headers
9510       (gnus-article-hide-headers)))
9511
9512 (defun gnus-article-hide-headers (&optional delete)
9513   "Hide unwanted headers and possibly sort them as well."
9514   (interactive "P")
9515   (save-excursion
9516     (set-buffer gnus-article-buffer)
9517     (save-restriction
9518       (let ((sorted gnus-sorted-header-list)
9519             (buffer-read-only nil)
9520             want want-list beg want-l)
9521         ;; First we narrow to just the headers.
9522         (widen)
9523         (goto-char 1)
9524         ;; Hide any "From " lines at the beginning of (mail) articles. 
9525         (while (looking-at rmail-unix-mail-delimiter)
9526           (forward-line 1))
9527         (if (/= (point) 1) 
9528             (add-text-properties 1 (point) '(invisible t)))
9529         ;; Then treat the rest of the header lines.
9530         (narrow-to-region 
9531          (point) 
9532          (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
9533         ;; Then we use the two regular expressions
9534         ;; `gnus-ignored-headers' and `gnus-visible-headers' to
9535         ;; select which header lines is to remain visible in the
9536         ;; article buffer.
9537         (goto-char 1)
9538         (while (re-search-forward "^[^ \t]*:" nil t)
9539           (beginning-of-line)
9540           ;; We add the headers we want to keep to a list and delete
9541           ;; them from the buffer.
9542           (if (or (and (stringp gnus-visible-headers)
9543                        (looking-at gnus-visible-headers))
9544                   (and (not (stringp gnus-visible-headers))
9545                        (stringp gnus-ignored-headers)
9546                        (not (looking-at gnus-ignored-headers))))
9547               (progn
9548                 (setq beg (point))
9549                 (forward-line 1)
9550                 ;; Be sure to get multi-line headers...
9551                 (re-search-forward "^[^ \t]*:" nil t)
9552                 (beginning-of-line)
9553                 (setq want-list 
9554                       (cons (buffer-substring beg (point)) want-list))
9555                 (delete-region beg (point))
9556                 (goto-char beg))
9557             (forward-line 1)))
9558         ;; Next we perform the sorting by looking at
9559         ;; `gnus-sorted-header-list'. 
9560         (goto-char 1)
9561         (while (and sorted want-list)
9562           (setq want-l want-list)
9563           (while (and want-l
9564                       (not (string-match (car sorted) (car want-l))))
9565             (setq want-l (cdr want-l)))
9566           (if want-l 
9567               (progn
9568                 (insert (car want-l))
9569                 (setq want-list (delq (car want-l) want-list))))
9570           (setq sorted (cdr sorted)))
9571         ;; Any headers that were not matched by the sorted list we
9572         ;; just tack on the end of the visible header list.
9573         (while want-list
9574           (insert (car want-list))
9575           (setq want-list (cdr want-list)))
9576         ;; And finally we make the unwanted headers invisible.
9577         (if delete
9578             (delete-region (point) (point-max))
9579           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
9580           (add-text-properties (point) (point-max) '(invisible t)))))))
9581
9582 (defun gnus-article-hide-signature ()
9583   "Hides the signature in an article.
9584 It does this by hiding everyting after \"^-- *$\", which is what all
9585 signatures should be preceded by. Note that this may mean that parts
9586 of an article may disappear if the article has such a line in the
9587 middle of the text."
9588   (interactive)
9589   (save-excursion
9590     (set-buffer gnus-article-buffer)
9591     (let ((buffer-read-only nil))
9592       (goto-char (point-max))
9593       (if (re-search-backward "^-- *$" nil t)
9594           (progn
9595             (add-text-properties (point) (point-max) '(invisible t)))))))
9596
9597 (defun gnus-article-hide-citation ()
9598   "Hide all cited text.
9599 This function uses the famous, extremely intelligent \"shoot in foot\"
9600 algorithm - which is simply deleting all lines that start with
9601 \">\". Your mileage may vary. If you come up with anything better,
9602 please do mail it to me."
9603   (interactive)
9604   (save-excursion
9605     (set-buffer gnus-article-buffer)
9606     (let ((buffer-read-only nil))
9607       (goto-char 1)
9608       (search-forward "\n\n" nil t)
9609       (while (not (eobp))
9610         (if (looking-at ">")
9611             (add-text-properties 
9612              (point) (save-excursion (forward-line 1) (point))
9613              '(invisible t)))
9614         (forward-line 1)))))
9615
9616 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
9617 (defun gnus-article-treat-overstrike ()
9618   "Translate overstrikes into bold text."
9619   (interactive)
9620   (save-excursion
9621     (set-buffer gnus-article-buffer)
9622     (let ((buffer-read-only nil))
9623       (while (search-forward "\b" nil t)
9624         (let ((next (following-char))
9625               (previous (char-after (- (point) 2))))
9626           (cond ((eq next previous)
9627                  (delete-region (- (point) 2) (point))
9628                  (put-text-property (point) (1+ (point))
9629                                     'face 'bold))
9630                 ((eq next ?_)
9631                  (delete-region (1- (point)) (1+ (point)))
9632                  (put-text-property (1- (point)) (point)
9633                                     'face 'underline))
9634                 ((eq previous ?_)
9635                  (delete-region (- (point) 2) (point))
9636                  (put-text-property (point) (1+ (point))
9637                                     'face 'underline))))))))
9638
9639 (defun gnus-article-word-wrap ()
9640   "Format too long lines."
9641   (interactive)
9642   (save-excursion
9643     (set-buffer gnus-article-buffer)
9644     (let ((buffer-read-only nil))
9645       (goto-char 1)
9646       (search-forward "\n\n" nil t)
9647       (end-of-line 1)
9648       (let ((paragraph-start "^\\W"))
9649         (while (not (eobp))
9650           (and (>= (current-column) (window-width))
9651                (/= (preceding-char) ?:)
9652                (fill-paragraph nil))
9653           (end-of-line 2))))))
9654
9655 (defun gnus-article-remove-cr ()
9656   "Remove carriage returns from an article."
9657   (interactive)
9658   (save-excursion
9659     (set-buffer gnus-article-buffer)
9660     (let ((buffer-read-only nil))
9661       (goto-char (point-min))
9662       (while (search-forward "\r" nil t)
9663         (replace-match "")))))
9664
9665 (defun gnus-article-display-x-face ()
9666   "Look for an X-Face header and display it if present."
9667   (interactive)
9668   (save-excursion
9669     (set-buffer gnus-article-buffer)
9670     (goto-char (point-min))
9671     (if (or (not gnus-article-x-face-command)
9672             (not (re-search-forward "^X-Face: " nil t)))
9673         ()
9674       (let ((face
9675              (buffer-substring
9676               (point) 
9677               (progn
9678                 (forward-line 1)
9679                 (while (and (looking-at "[ \t]") 
9680                             (zerop (forward-line 1))))
9681                 (point))))
9682             (command gnus-article-x-face-command))
9683         (if (symbolp command)
9684             (and (or (fboundp command) (error "%s is not a function" command))
9685                  (funcall command face))
9686           (let ((process (start-process "gnus-x-face" nil "sh" "-c" command)))
9687             (if process
9688                 (progn
9689                   (process-send-string process face)
9690                   (process-send-eof process))
9691               (error "Couldn't start process"))))))))
9692               
9693 (defun gnus-article-de-quoted-unreadable ()
9694   "Do a naive translation of a quoted-printable-encoded article.
9695 This is in no way, shape or form meant as a replacement for real MIME
9696 processing, but is simply a stop-gap measure until MIME support is
9697 written."
9698   (interactive)
9699   (save-excursion
9700     (save-restriction
9701       (set-buffer gnus-article-buffer)
9702       (let ((buffer-read-only nil))
9703         (widen)
9704         (goto-char (point-min))
9705         (while (re-search-forward "=[0-9A-F][0-9A-F]" nil t)
9706           (replace-match 
9707            (char-to-string 
9708             (+
9709              (* 16 (gnus-hex-char-to-integer 
9710                     (char-after (1+ (match-beginning 0)))))
9711              (gnus-hex-char-to-integer
9712               (char-after (1- (match-end 0))))))))))))
9713
9714 ;; Taken from hexl.el.
9715 (defun gnus-hex-char-to-integer (character)
9716   "Take a char and return its value as if it was a hex digit."
9717   (if (and (>= character ?0) (<= character ?9))
9718       (- character ?0)
9719     (let ((ch (logior character 32)))
9720       (if (and (>= ch ?a) (<= ch ?f))
9721           (- ch (- ?a 10))
9722         (error (format "Invalid hex digit `%c'." ch))))))
9723
9724 ;; Article savers.
9725
9726 (defun gnus-output-to-rmail (file-name)
9727   "Append the current article to an Rmail file named FILE-NAME."
9728   (require 'rmail)
9729   ;; Most of these codes are borrowed from rmailout.el.
9730   (setq file-name (expand-file-name file-name))
9731   (setq rmail-default-rmail-file file-name)
9732   (let ((artbuf (current-buffer))
9733         (tmpbuf (get-buffer-create " *Gnus-output*")))
9734     (save-excursion
9735       (or (get-file-buffer file-name)
9736           (file-exists-p file-name)
9737           (if (gnus-yes-or-no-p
9738                (concat "\"" file-name "\" does not exist, create it? "))
9739               (let ((file-buffer (create-file-buffer file-name)))
9740                 (save-excursion
9741                   (set-buffer file-buffer)
9742                   (rmail-insert-rmail-file-header)
9743                   (let ((require-final-newline nil))
9744                     (write-region (point-min) (point-max) file-name t 1)))
9745                 (kill-buffer file-buffer))
9746             (error "Output file does not exist")))
9747       (set-buffer tmpbuf)
9748       (buffer-disable-undo (current-buffer))
9749       (erase-buffer)
9750       (insert-buffer-substring artbuf)
9751       (gnus-convert-article-to-rmail)
9752       ;; Decide whether to append to a file or to an Emacs buffer.
9753       (let ((outbuf (get-file-buffer file-name)))
9754         (if (not outbuf)
9755             (append-to-file (point-min) (point-max) file-name)
9756           ;; File has been visited, in buffer OUTBUF.
9757           (set-buffer outbuf)
9758           (let ((buffer-read-only nil)
9759                 (msg (and (boundp 'rmail-current-message)
9760                           rmail-current-message)))
9761             ;; If MSG is non-nil, buffer is in RMAIL mode.
9762             (if msg
9763                 (progn (widen)
9764                        (narrow-to-region (point-max) (point-max))))
9765             (insert-buffer-substring tmpbuf)
9766             (if msg
9767                 (progn
9768                   (goto-char (point-min))
9769                   (widen)
9770                   (search-backward "\^_")
9771                   (narrow-to-region (point) (point-max))
9772                   (goto-char (1+ (point-min)))
9773                   (rmail-count-new-messages t)
9774                   (rmail-show-message msg)))))))
9775     (kill-buffer tmpbuf)))
9776
9777 (defun gnus-output-to-file (file-name)
9778   "Append the current article to a file named FILE-NAME."
9779   (setq file-name (expand-file-name file-name))
9780   (let ((artbuf (current-buffer))
9781         (tmpbuf (get-buffer-create " *Gnus-output*")))
9782     (save-excursion
9783       (set-buffer tmpbuf)
9784       (buffer-disable-undo (current-buffer))
9785       (erase-buffer)
9786       (insert-buffer-substring artbuf)
9787       ;; Append newline at end of the buffer as separator, and then
9788       ;; save it to file.
9789       (goto-char (point-max))
9790       (insert "\n")
9791       (append-to-file (point-min) (point-max) file-name))
9792     (kill-buffer tmpbuf)))
9793
9794 (defun gnus-convert-article-to-rmail ()
9795   "Convert article in current buffer to Rmail message format."
9796   (let ((buffer-read-only nil))
9797     ;; Convert article directly into Babyl format.
9798     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
9799     (goto-char (point-min))
9800     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
9801     (while (search-forward "\n\^_" nil t) ;single char
9802       (replace-match "\n^_"))           ;2 chars: "^" and "_"
9803     (goto-char (point-max))
9804     (insert "\^_")))
9805
9806 (defun gnus-narrow-to-page (&optional arg)
9807   "Make text outside current page invisible except for page delimiter.
9808 A numeric arg specifies to move forward or backward by that many pages,
9809 thus showing a page other than the one point was originally in."
9810   (interactive "P")
9811   (setq arg (if arg (prefix-numeric-value arg) 0))
9812   (save-excursion
9813     (forward-page -1)                   ;Beginning of current page.
9814     (widen)
9815     (if (> arg 0)
9816         (forward-page arg)
9817       (if (< arg 0)
9818           (forward-page (1- arg))))
9819     ;; Find the end of the page.
9820     (forward-page)
9821     ;; If we stopped due to end of buffer, stay there.
9822     ;; If we stopped after a page delimiter, put end of restriction
9823     ;; at the beginning of that line.
9824     ;; These are commented out.
9825     ;;    (if (save-excursion (beginning-of-line)
9826     ;;                  (looking-at page-delimiter))
9827     ;;  (beginning-of-line))
9828     (narrow-to-region (point)
9829                       (progn
9830                         ;; Find the top of the page.
9831                         (forward-page -1)
9832                         ;; If we found beginning of buffer, stay there.
9833                         ;; If extra text follows page delimiter on same line,
9834                         ;; include it.
9835                         ;; Otherwise, show text starting with following line.
9836                         (if (and (eolp) (not (bobp)))
9837                             (forward-line 1))
9838                         (point)))))
9839
9840 (defun gnus-gmt-to-local ()
9841   "Rewrite Date header described in GMT to local in current buffer.
9842 Intended to be used with gnus-article-prepare-hook."
9843   (save-excursion
9844     (save-restriction
9845       (widen)
9846       (goto-char (point-min))
9847       (narrow-to-region (point-min)
9848                         (progn (search-forward "\n\n" nil 'move) (point)))
9849       (goto-char (point-min))
9850       (if (re-search-forward "^Date:[ \t]\\(.*\\)$" nil t)
9851           (let ((buffer-read-only nil)
9852                 (date (buffer-substring (match-beginning 1) (match-end 1))))
9853             (delete-region (match-beginning 1) (match-end 1))
9854             (insert
9855              (timezone-make-date-arpa-standard 
9856               date nil (current-time-zone))))))))
9857
9858
9859 ;; Article mode commands
9860
9861 (defun gnus-article-next-page (lines)
9862   "Show next page of current article.
9863 If end of article, return non-nil. Otherwise return nil.
9864 Argument LINES specifies lines to be scrolled up."
9865   (interactive "P")
9866   (move-to-window-line -1)
9867   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
9868   (if (save-excursion
9869         (end-of-line)
9870         (and (pos-visible-in-window-p)  ;Not continuation line.
9871              (eobp)))
9872       ;; Nothing in this page.
9873       (if (or (not gnus-break-pages)
9874               (save-excursion
9875                 (save-restriction
9876                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
9877           t                             ;Nothing more.
9878         (gnus-narrow-to-page 1)         ;Go to next page.
9879         nil)
9880     ;; More in this page.
9881     (condition-case ()
9882         (scroll-up lines)
9883       (end-of-buffer
9884        ;; Long lines may cause an end-of-buffer error.
9885        (goto-char (point-max))))
9886     nil))
9887
9888 (defun gnus-article-prev-page (lines)
9889   "Show previous page of current article.
9890 Argument LINES specifies lines to be scrolled down."
9891   (interactive "P")
9892   (move-to-window-line 0)
9893   (if (and gnus-break-pages
9894            (bobp)
9895            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
9896       (progn
9897         (gnus-narrow-to-page -1) ;Go to previous page.
9898         (goto-char (point-max))
9899         (recenter -1))
9900     (scroll-down lines)))
9901
9902 (defun gnus-article-refer-article ()
9903   "Read article specified by message-id around point."
9904   (interactive)
9905   (search-forward ">" nil t)    ;Move point to end of "<....>".
9906   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
9907       (let ((message-id
9908              (buffer-substring (match-beginning 1) (match-end 1))))
9909         (set-buffer gnus-summary-buffer)
9910         (gnus-summary-refer-article message-id))
9911     (error "No references around point")))
9912
9913 (defun gnus-article-mail (yank)
9914   "Send a reply to the address near point.
9915 If YANK is non-nil, include the original article."
9916   (interactive "P")
9917   (let ((address 
9918          (buffer-substring
9919           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
9920           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
9921     (and address
9922          (progn
9923            (switch-to-buffer gnus-summary-buffer)
9924            (funcall gnus-mail-reply-method yank address)))))
9925
9926 (defun gnus-article-mail-with-original ()
9927   "Send a reply to the address near point and include the original article."
9928   (interactive)
9929   (gnus-article-mail 'yank))
9930
9931 (defun gnus-article-show-summary ()
9932   "Reconfigure windows to show summary buffer."
9933   (interactive)
9934   (gnus-configure-windows 'article)
9935   (pop-to-buffer gnus-summary-buffer)
9936   (gnus-summary-goto-subject gnus-current-article))
9937
9938 (defun gnus-article-describe-briefly ()
9939   "Describe article mode commands briefly."
9940   (interactive)
9941   (message
9942    (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")))
9943
9944 (defun gnus-article-summary-command ()
9945   "Execute the last keystroke in the summary buffer."
9946   (interactive)
9947   (message "                                                                              ")
9948   (let ((obuf (current-buffer))
9949         (owin (current-window-configuration)))
9950     (switch-to-buffer gnus-summary-buffer 'norecord)
9951     (execute-kbd-macro (this-command-keys))
9952     (set-buffer obuf)
9953     (let ((npoint (point)))
9954       (set-window-configuration owin)
9955       (set-window-start (get-buffer-window (current-buffer)) (point)))))
9956
9957 ;; caesar-region written by phr@prep.ai.mit.edu  Nov 86
9958 ;; Modified by tower@prep Nov 86
9959 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
9960
9961 (defun gnus-caesar-region (&optional n)
9962   "Caesar rotation of region by N, default 13, for decrypting netnews.
9963 ROT47 will be performed for Japanese text in any case."
9964   (interactive (if current-prefix-arg   ; Was there a prefix arg?
9965                    (list (prefix-numeric-value current-prefix-arg))
9966                  (list nil)))
9967   (cond ((not (numberp n)) (setq n 13))
9968         (t (setq n (mod n 26))))        ;canonicalize N
9969   (if (not (zerop n))           ; no action needed for a rot of 0
9970       (progn
9971         (if (or (not (boundp 'caesar-translate-table))
9972                 (not caesar-translate-table)
9973                 (/= (aref caesar-translate-table ?a) (+ ?a n)))
9974             (let ((i 0) 
9975                   (lower "abcdefghijklmnopqrstuvwxyz")
9976                   upper)
9977               (message "Building caesar-translate-table...")
9978               (setq caesar-translate-table (make-vector 256 0))
9979               (while (< i 256)
9980                 (aset caesar-translate-table i i)
9981                 (setq i (1+ i)))
9982               (setq lower (concat lower lower)
9983                     upper (upcase lower)
9984                     i 0)
9985               (while (< i 26)
9986                 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
9987                 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
9988                 (setq i (1+ i)))
9989               ;; ROT47 for Japanese text.
9990               ;; Thanks to ichikawa@flab.fujitsu.junet.
9991               (setq i 161)
9992               (let ((t1 (logior ?O 128))
9993                     (t2 (logior ?! 128))
9994                     (t3 (logior ?~ 128)))
9995                 (while (< i 256)
9996                   (aset caesar-translate-table i
9997                         (let ((v (aref caesar-translate-table i)))
9998                           (if (<= v t1) (if (< v t2) v (+ v 47))
9999                             (if (<= v t3) (- v 47) v))))
10000                   (setq i (1+ i))))
10001               (message "Building caesar-translate-table... done")))
10002         (let ((from (region-beginning))
10003               (to (region-end))
10004               (i 0) str len)
10005           (setq str (buffer-substring from to))
10006           (setq len (length str))
10007           (while (< i len)
10008             (aset str i (aref caesar-translate-table (aref str i)))
10009             (setq i (1+ i)))
10010           (goto-char from)
10011           (delete-region from to)
10012           (insert str)))))
10013
10014 \f
10015 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
10016
10017 ;;;###autoload
10018 (defalias 'gnus-batch-kill 'gnus-batch-score)
10019 ;;;###autoload
10020 (defun gnus-batch-score ()
10021   "Run batched scoring.
10022 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
10023 Newsgroups is a list of strings in Bnews format.  If you want to score
10024 the comp hierarchy, you'd say \"comp.all\". If you would not like to
10025 score the alt hierarchy, you'd say \"!alt.all\"."
10026   (interactive)
10027   (let* ((yes-and-no
10028           (gnus-parse-n-options
10029            (apply (function concat)
10030                   (mapcar (lambda (g) (concat g " "))
10031                           command-line-args-left))))
10032          (gnus-expert-user t)
10033          (nnmail-spool-file nil)
10034          (gnus-use-dribble-file nil)
10035          (yes (car yes-and-no))
10036          (no (cdr yes-and-no))
10037          group subscribed newsrc entry
10038          ;; Disable verbose message.
10039          gnus-novice-user gnus-large-newsgroup)
10040     ;; Eat all arguments.
10041     (setq command-line-args-left nil)
10042     ;; Start Gnus.
10043     (gnus)
10044     ;; Apply kills to specified newsgroups in command line arguments.
10045     (setq newsrc (cdr gnus-newsrc-assoc))
10046     (while newsrc
10047       (setq group (car (car newsrc)))
10048       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
10049       (if (and (<= (nth 1 (car newsrc)) 5)
10050                (and (car entry)
10051                     (or (eq (car entry) t)
10052                         (not (zerop (car entry)))))
10053                (if yes (string-match yes group) t)
10054                (or (null no) (not (string-match no group))))
10055           (progn
10056             (gnus-summary-read-group group nil t)
10057             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
10058                  (gnus-summary-exit))))
10059       (setq newsrc (cdr newsrc)))
10060     ;; Exit Emacs.
10061     (switch-to-buffer gnus-group-buffer)
10062     (gnus-group-save-newsrc)))
10063
10064 (defun gnus-apply-kill-file ()
10065   "Apply a kill file to the current newsgroup.
10066 Returns the number of articles marked as read."
10067   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
10068           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
10069       (gnus-apply-kill-file-internal)
10070     0))
10071
10072 (defun gnus-kill-save-kill-buffer ()
10073   (save-excursion
10074     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
10075       (if (get-file-buffer file)
10076           (progn
10077             (set-buffer (get-file-buffer file))
10078             (and (buffer-modified-p) (save-buffer))
10079             (kill-buffer (current-buffer)))))))
10080
10081 (defvar gnus-kill-file-name "KILL"
10082   "Suffix of the kill files.")
10083
10084 (defun gnus-newsgroup-kill-file (newsgroup)
10085   "Return the name of a kill file name for NEWSGROUP.
10086 If NEWSGROUP is nil, return the global kill file name instead."
10087   (cond ((or (null newsgroup)
10088              (string-equal newsgroup ""))
10089          ;; The global KILL file is placed at top of the directory.
10090          (expand-file-name gnus-kill-file-name
10091                            (or gnus-kill-files-directory "~/News")))
10092         ((gnus-use-long-file-name 'not-kill)
10093          ;; Append ".KILL" to newsgroup name.
10094          (expand-file-name (concat newsgroup "." gnus-kill-file-name)
10095                            (or gnus-kill-files-directory "~/News")))
10096         (t
10097          ;; Place "KILL" under the hierarchical directory.
10098          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
10099                                    "/" gnus-kill-file-name)
10100                            (or gnus-kill-files-directory "~/News")))))
10101
10102 \f
10103 ;;;
10104 ;;; Gnus Score Files
10105 ;;;
10106
10107 ;; All score code written by Per Abrahamsen <abraham@iesd.auc.dk>.
10108
10109 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10110 (defun gnus-score-set-mark-below (score)
10111   "Automatically mark articles with score below SCORE as read."
10112   (interactive 
10113    (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
10114              (string-to-int (read-string "Mark below: ")))))
10115   (setq score (or score gnus-summary-default-score 0))
10116   (gnus-score-set 'mark (list score))
10117   (gnus-score-set 'touched '(t))
10118   (setq gnus-summary-mark-below score)
10119   (gnus-summary-update-lines))
10120
10121 (defun gnus-score-set-expunge-below (score)
10122   "Automatically expunge articles with score below SCORE."
10123   (interactive 
10124    (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
10125              (string-to-int (read-string "Expunge below: ")))))
10126   (setq score (or score gnus-summary-default-score 0))
10127   (gnus-score-set 'expunge (list score))
10128   (gnus-score-set 'touched '(t)))
10129
10130 (defun gnus-score-default (level)
10131   (if level (prefix-numeric-value level) 
10132     gnus-score-interactive-default-score))
10133
10134 (defun gnus-score-set (symbol value &optional alist)
10135   ;; Set SYMBOL to VALUE in ALIST.
10136   (let* ((alist 
10137           (or alist 
10138               gnus-score-alist
10139               (progn
10140                 (gnus-score-load (gnus-score-file-name gnus-newsgroup-name))
10141                 gnus-score-alist)))
10142          (entry (assoc symbol alist)))
10143     (cond ((gnus-score-get 'read-only alist)
10144            ;; This is a read-only score file, so we do nothing.
10145            )
10146           (entry
10147            (setcdr entry value))
10148           ((null alist)
10149            (error "Empty alist"))
10150           (t
10151            (setcdr alist
10152                    (cons (cons symbol value) (cdr alist)))))))
10153
10154 (defun gnus-score-get (symbol &optional alist)
10155   ;; Get SYMBOL's definition in ALIST.
10156   (cdr (assoc symbol 
10157               (or alist 
10158                   gnus-score-alist
10159                   (progn
10160                     (gnus-score-load 
10161                      (gnus-score-file-name gnus-newsgroup-name))
10162                     gnus-score-alist)))))
10163
10164 (defun gnus-score-change-score-file (file)
10165   "Change current score alist."
10166   (interactive
10167    (list (completing-read "Score file: " gnus-score-cache)))
10168   (setq gnus-current-score-file file)
10169   (gnus-score-load-file file)
10170   (gnus-set-mode-line 'summary))
10171
10172 (defun gnus-score-edit-alist (file)
10173   "Edit the current score alist."
10174   (interactive (list gnus-current-score-file))
10175   (and (buffer-name gnus-summary-buffer) (gnus-score-save))
10176   (setq gnus-winconf-edit-score (current-window-configuration))
10177   (gnus-configure-windows 'article)
10178   (pop-to-buffer (find-file-noselect file))
10179   (message (substitute-command-keys 
10180             "\\<gnus-score-mode-map>\\[gnus-score-edit-done] to save edits"))
10181   (gnus-score-mode))
10182   
10183 (defun gnus-score-edit-file (file)
10184   "Edit a score file."
10185   (interactive 
10186    (list (read-file-name "Edit score file: " gnus-kill-files-directory)))
10187   (and (buffer-name gnus-summary-buffer) (gnus-score-save))
10188   (setq gnus-winconf-edit-score (current-window-configuration))
10189   (gnus-configure-windows 'article)
10190   (pop-to-buffer (find-file-noselect file))
10191   (message (substitute-command-keys 
10192             "\\<gnus-score-mode-map>\\[gnus-score-edit-done] to save edits"))
10193   (gnus-score-mode))
10194   
10195 (defun gnus-score-load-file (file)
10196   ;; Load score file FILE.  Returns a list a retrieved score-alists.
10197   (setq gnus-kill-files-directory (or gnus-kill-files-directory "~/News/"))
10198   (let* ((file (expand-file-name 
10199                 (or (and (string-match
10200                           (concat "^" (expand-file-name
10201                                        gnus-kill-files-directory)) 
10202                           (expand-file-name file))
10203                          file)
10204                     (concat gnus-kill-files-directory file))))
10205          (cached (assoc file gnus-score-cache))
10206          (global (member file gnus-internal-global-score-files))
10207          lists alist)
10208     (if cached
10209         ;; The score file was already loaded.
10210         (setq alist (cdr cached))
10211       ;; We load the score file.
10212       (setq gnus-score-alist nil)
10213       (setq alist (gnus-score-load-score-alist file))
10214       ;; We add '(touched) to the alist to signify that it hasn't been
10215       ;; touched (yet). 
10216       (or (assq 'touched alist) (setq alist (cons (list 'touched nil) alist)))
10217       ;; If it is a global score file, we make it read-only.
10218       (and global
10219            (not (assq 'read-only alist))
10220            (setq alist (cons (list 'read-only t) alist)))
10221       ;; Update cache.
10222       (setq gnus-score-cache
10223             (cons (cons file alist) gnus-score-cache)))
10224     ;; If there are actual scores in the alist, we add it to the
10225     ;; return value of this function.
10226     (if (memq t (mapcar (lambda (e) (stringp (car e))) alist))
10227         (setq lists (list alist)))
10228     ;; Treat the other possible atoms in the score alist.
10229     (let ((mark (car (gnus-score-get 'mark alist)))
10230           (expunge (car (gnus-score-get 'expunge alist)))
10231           (mark-and-expunge 
10232            (car (gnus-score-get 'mark-and-expunge alist)))
10233           (read-only (gnus-score-get 'read-only alist))
10234           (files (gnus-score-get 'files alist))
10235           (exclude-files (gnus-score-get 'exclude-files alist))
10236           (orphan (gnus-score-get 'orphan alist))
10237           (eval (gnus-score-get 'eval alist)))
10238       ;; We do not respect eval and files atoms from global score
10239       ;; files. 
10240       (and files (not global)
10241            (setq lists (apply 'append lists
10242                               (mapcar (lambda (file)
10243                                         (gnus-score-load-file file)) 
10244                                       files))))
10245       (and eval (not global) (eval eval))
10246       (setq gnus-scores-exclude-files exclude-files)
10247       (if orphan (setq gnus-orphan-score (car orphan)))
10248       (setq gnus-summary-mark-below 
10249             (or mark mark-and-expunge gnus-summary-mark-below))
10250       (setq gnus-summary-expunge-below 
10251             (or expunge mark-and-expunge gnus-summary-expunge-below)))
10252     (setq gnus-current-score-file file)
10253     (setq gnus-score-alist alist)
10254     lists))
10255
10256 (defun gnus-score-load (file)
10257   ;; Load score FILE.
10258   (let ((cache (assoc file gnus-score-cache)))
10259     (if cache
10260         (setq gnus-score-alist (cdr cache))
10261       (setq gnus-score-alist nil)
10262       (gnus-score-load-score-alist file)
10263       (or gnus-score-alist
10264           (setq gnus-score-alist (copy-alist '((touched nil)))))
10265       (setq gnus-score-cache
10266             (cons (cons file gnus-score-alist) gnus-score-cache)))))
10267
10268 (defun gnus-score-remove-from-cache (file)
10269   (setq gnus-score-cache 
10270         (delq (assoc file gnus-score-cache) gnus-score-cache)))
10271
10272 (defun gnus-score-load-score-alist (file)
10273   (let (alist)
10274     (if (file-readable-p file)
10275         (progn
10276           (save-excursion
10277             (set-buffer (get-buffer-create " *gnus work*"))
10278             (buffer-disable-undo (current-buffer))
10279             (erase-buffer)
10280             (insert-file-contents file)
10281             (goto-char (point-min))
10282             ;; Only do the loading if the score file isn't empty.
10283             (if (save-excursion (re-search-forward "[()0-9a-zA-Z]" nil t))
10284                 (setq alist
10285                       (condition-case ()
10286                           (read (current-buffer))
10287                         (error 
10288                          (progn
10289                            (message "Problem with score file %s" file)
10290                            (ding) 
10291                            (sit-for 2)
10292                            nil))))))
10293           (if (eq (car alist) 'setq)
10294               (setq gnus-score-alist (gnus-score-transform-old-to-new alist))
10295             (setq gnus-score-alist alist))
10296           (setq gnus-score-alist
10297                 (gnus-score-check-syntax gnus-score-alist)))
10298       (setq gnus-score-alist nil))))
10299
10300 (defun gnus-score-check-syntax (alist)
10301   (cond 
10302    ((null alist)
10303     nil)
10304    ((not (consp alist))
10305     (message "Score file is not a list: %s" alist)
10306     (ding)
10307     nil)
10308    (t
10309     (let ((a alist)
10310           err)
10311       (while (and a (not err))
10312         (cond ((not (listp (car a)))
10313                (message "Illegal score element: %s" (car a))
10314                (setq err t))
10315               ((and (stringp (car (car a)))
10316                     (not (listp (nth 1 (car a)))))
10317                (message "Illegal header match: %s" (nth 1 (car a)))
10318                (setq err t))
10319               (t
10320                (setq a (cdr a)))))
10321       (if err
10322           (progn
10323             (ding)
10324             nil)
10325         alist)))))    
10326
10327 (defun gnus-score-transform-old-to-new (alist)
10328   (let* ((alist (nth 2 alist))
10329          out entry)
10330     (if (eq (car alist) 'quote)
10331         (setq alist (nth 1 alist)))
10332     (while alist
10333       (setq entry (car alist))
10334       (if (stringp (car entry))
10335           (let ((scor (cdr entry)))
10336             (setq out (cons entry out))
10337             (while scor
10338               (setcar scor
10339                       (list (car (car scor)) (nth 2 (car scor))
10340                             (and (nth 3 (car scor))
10341                                  (gnus-day-number (nth 3 (car scor))))
10342                             (if (nth 1 (car scor)) 'r 's)))
10343               (setq scor (cdr scor))))
10344         (setq out (cons (if (not (listp (cdr entry))) 
10345                             (list (car entry) (cdr entry))
10346                           entry)
10347                         out)))
10348       (setq alist (cdr alist)))
10349     (cons (list 'touched t) (nreverse out))))
10350   
10351 (defun gnus-score-save ()
10352   ;; Save all SCORE information.
10353   (let ((cache gnus-score-cache))
10354     (save-excursion
10355       (setq gnus-score-alist nil)
10356       (set-buffer (get-buffer-create "*Score*"))
10357       (buffer-disable-undo (current-buffer))
10358       (let (entry score file)
10359         (while cache
10360           (setq entry (car cache)
10361                 cache (cdr cache)
10362                 file (car entry)
10363                 score (cdr entry))
10364           (if (or (not (equal (gnus-score-get 'touched score) '(t)))
10365                   (gnus-score-get 'read-only score)
10366                   (not (file-writable-p file)))
10367               ()
10368             (setq score (delq (assq 'touched score) score))
10369             (erase-buffer)
10370             (let (emacs-lisp-mode-hook)
10371               (pp score (current-buffer)))
10372             (gnus-make-directory (file-name-directory file))
10373             (write-region (point-min) (point-max) file nil 'silent))))
10374       (kill-buffer (current-buffer)))))
10375   
10376 (defun gnus-score-headers ()
10377   ;; Score `gnus-newsgroup-headers'.
10378   (let ((func gnus-score-find-score-files-function)
10379         score-files scores)
10380     (and func (not (listp func))
10381          (setq func (list func)))
10382     ;; Go through all the functions for finding score files (or actual
10383     ;; scores) and add them to a list.
10384     (while func
10385       (and (symbolp (car func))
10386            (fboundp (car func))
10387            (setq score-files 
10388                  (nconc score-files (funcall (car func) gnus-newsgroup-name))))
10389       (setq func (cdr func)))
10390     ;; PLM: probably this is not the best place to clear orphan-score
10391     (setq gnus-orphan-score nil)
10392     ;; Load the SCORE files.
10393     (while score-files
10394       (if (stringp (car score-files))
10395           ;; It is a string, which means that it's a score file name,
10396           ;; so we load the score file and add the score alist to
10397           ;; the list of alists.
10398           (setq scores (nconc (gnus-score-load-file (car score-files)) scores))
10399         ;; It is an alist, so we just add it to the list directly.
10400         (setq scores (nconc (car score-files) scores)))
10401       (setq score-files (cdr score-files)))
10402     ;; Prune the score files that are to be excluded, if any.
10403     (if (not gnus-scores-exclude-files)
10404         ()
10405       (let ((s scores)
10406             c)
10407         (while s
10408           (and (setq c (rassq (car s) gnus-score-cache))
10409                (member (car c) gnus-scores-exclude-files)
10410                (setq scores (delq (car s) scores)))
10411           (setq s (cdr s)))))
10412     (if (not (and gnus-summary-default-score
10413                   scores
10414                   (> (length gnus-newsgroup-headers)
10415                      (length gnus-newsgroup-scored))))
10416         ()
10417       (let* ((entries gnus-header-index)
10418              (now (gnus-day-number (current-time-string)))
10419              (expire (- now gnus-score-expiry-days))
10420              (headers gnus-newsgroup-headers)
10421              entry header)
10422         (message "Scoring...")
10423         ;; Create articles, an alist of the form `(HEADER . SCORE)'.
10424         (while headers
10425           (setq header (car headers)
10426                 headers (cdr headers))
10427           ;; WARNING: The assq makes the function O(N*S) while it could
10428           ;; be written as O(N+S), where N is (length gnus-newsgroup-headers)
10429           ;; and S is (length gnus-newsgroup-scored).
10430           (or (assq (header-number header) gnus-newsgroup-scored)
10431               (setq gnus-scores-articles       ;Total of 2 * N cons-cells used.
10432                     (cons (cons header (or gnus-summary-default-score 0))
10433                           gnus-scores-articles))))
10434
10435         (save-excursion
10436           (set-buffer (get-buffer-create "*Headers*"))
10437           (buffer-disable-undo (current-buffer))
10438           ;; score orphans
10439           (if gnus-orphan-score 
10440               (progn
10441                 (setq gnus-score-index 
10442                       (nth 1 (assoc "references" gnus-header-index)))
10443                 (gnus-score-orphans gnus-orphan-score)))
10444           ;; Run each header through the score process.
10445           (while entries
10446             (setq entry (car entries)
10447                   header (nth 0 entry)
10448                   entries (cdr entries))
10449             (setq gnus-score-index (nth 1 (assoc header gnus-header-index)))
10450             (if (< 0 (apply 'max (mapcar
10451                                   (lambda (score)
10452                                     (length (gnus-score-get header score)))
10453                                   scores)))
10454                 (funcall (nth 2 entry) scores header now expire)))
10455           ;; Remove the buffer.
10456           (kill-buffer (current-buffer)))
10457
10458         ;; Add articles to `gnus-newsgroup-scored'.
10459         (while gnus-scores-articles
10460           (or (= gnus-summary-default-score (cdr (car gnus-scores-articles)))
10461               (setq gnus-newsgroup-scored
10462                     (cons (cons (header-number 
10463                                  (car (car gnus-scores-articles)))
10464                                 (cdr (car gnus-scores-articles)))
10465                           gnus-newsgroup-scored)))
10466           (setq gnus-scores-articles (cdr gnus-scores-articles)))
10467
10468         (message "Scoring...done")))))
10469
10470
10471 (defun gnus-get-new-thread-ids (articles)
10472   (let ((index (nth 1 (assoc "message-id" gnus-header-index)))
10473         (refind gnus-score-index)
10474         id-list art this tref)
10475     (while articles
10476       (setq art (car articles)
10477             this (aref (car art) index)
10478             tref (aref (car art) refind)
10479             articles (cdr articles))
10480       (if (string-equal tref "")        ;no references line
10481           (setq id-list (cons this id-list))))
10482     id-list))
10483
10484 ;; Orphan functions written by plm@atcmp.nl (Peter Mutsaers).
10485 (defun gnus-score-orphans (score)
10486   (let ((new-thread-ids (gnus-get-new-thread-ids gnus-scores-articles))
10487         (index (nth 1 (assoc "references" gnus-header-index)))
10488         alike articles art arts this last this-id)
10489     
10490     (setq gnus-scores-articles (sort gnus-scores-articles 'gnus-score-string<)
10491           articles gnus-scores-articles)
10492
10493     ;;more or less the same as in gnus-score-string
10494     (erase-buffer)
10495     (while articles
10496       (setq art (car articles)
10497             this (aref (car art) gnus-score-index)
10498             articles (cdr articles))
10499       ;;completely skip if this is empty (not a child, so not an orphan)
10500       (if (not (string= this ""))
10501           (if (equal last this)
10502               ;; O(N*H) cons-cells used here, where H is the number of
10503               ;; headers.
10504               (setq alike (cons art alike))
10505             (if last
10506                 (progn
10507                   ;; Insert the line, with a text property on the
10508                   ;; terminating newline refering to the articles with
10509                   ;; this line.
10510                   (insert last ?\n)
10511                   (put-text-property (1- (point)) (point) 'articles alike)))
10512             (setq alike (list art)
10513                   last this))))
10514     (and last                           ; Bwadr, duplicate code.
10515          (progn
10516            (insert last ?\n)                    
10517            (put-text-property (1- (point)) (point) 'articles alike)))
10518
10519     ;; PLM: now delete those lines that contain an entry from new-thread-ids
10520     (while new-thread-ids
10521       (setq this-id (car new-thread-ids)
10522             new-thread-ids (cdr new-thread-ids))
10523       (goto-char (point-min))
10524       (while (search-forward this-id nil t)
10525         ;; found a match. remove this line
10526         (beginning-of-line)
10527         (kill-line 1)))
10528
10529     ;; now for each line: update its articles with score by moving to
10530     ;; every end-of-line in the buffer and read the articles property
10531     (goto-char (point-min))
10532     (while (eq 0 (progn
10533                    (end-of-line)
10534                    (setq arts (get-text-property (point) 'articles))
10535                    (while arts
10536                      (setq art (car arts)
10537                            arts (cdr arts))
10538                      (setcdr art (+ score (cdr art))))
10539                    (forward-line))))))
10540              
10541
10542 (defun gnus-score-integer (scores header now expire)
10543   (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
10544         alike last this art entries alist articles)
10545
10546     ;; Find matches.
10547     (while scores
10548       (setq alist (car scores)
10549             scores (cdr scores)
10550             entries (assoc header alist))
10551       (while (cdr entries)              ;First entry is the header index.
10552         (let* ((rest (cdr entries))             
10553                (kill (car rest))
10554                (match (nth 0 kill))
10555                (type (or (nth 3 kill) '>))
10556                (score (or (nth 1 kill) gnus-score-interactive-default-score))
10557                (date (nth 2 kill))
10558                (found nil)
10559                (match-func (if (or (eq type '>) (eq type '<) (eq type '<=)
10560                                    (eq type '>=) (eq type '=))
10561                                type
10562                              (error "Illegal match type: %s" type)))
10563                (articles gnus-scores-articles)
10564                arts art)
10565           ;; Instead of doing all the clever stuff that
10566           ;; `gnus-score-string' does to minimize searches and stuff,
10567           ;; I will assume that people generally will put so few
10568           ;; matches on numbers that any cleverness will take more
10569           ;; time than one would gain.
10570           (while articles
10571             (and (funcall match-func match 
10572                           (or (aref (car (car articles)) gnus-score-index) 0))
10573                  (progn
10574                    (setq found t)
10575                    (setcdr (car articles) (+ score (cdr (car articles))))))
10576             (setq articles (cdr articles)))
10577           ;; Update expire date
10578           (cond ((null date))           ;Permanent entry.
10579                 (found                  ;Match, update date.
10580                  (gnus-score-set 'touched '(t) alist)
10581                  (setcar (nthcdr 2 kill) now))
10582                 ((< date expire) ;Old entry, remove.
10583                  (gnus-score-set 'touched '(t) alist)
10584                  (setcdr entries (cdr rest))
10585                  (setq rest entries)))
10586           (setq entries rest))))))
10587
10588 (defun gnus-score-date (scores header now expire)
10589   (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
10590         alike last this art entries alist articles)
10591
10592     ;; Find matches.
10593     (while scores
10594       (setq alist (car scores)
10595             scores (cdr scores)
10596             entries (assoc header alist))
10597       (while (cdr entries)              ;First entry is the header index.
10598         (let* ((rest (cdr entries))             
10599                (kill (car rest))
10600                (match (timezone-make-date-sortable (nth 0 kill)))
10601                (type (or (nth 3 kill) 'before))
10602                (score (or (nth 1 kill) gnus-score-interactive-default-score))
10603                (date (nth 2 kill))
10604                (found nil)
10605                (match-func 
10606                 (cond ((eq type 'after) 'string<)
10607                       ((eq type 'before) 'gnus-string>)
10608                       ((eq type 'at) 'string=)
10609                       (t (error "Illegal match type: %s" type))))
10610                (articles gnus-scores-articles)
10611                arts art l)
10612           ;; Instead of doing all the clever stuff that
10613           ;; `gnus-score-string' does to minimize searches and stuff,
10614           ;; I will assume that people generally will put so few
10615           ;; matches on numbers that any cleverness will take more
10616           ;; time than one would gain.
10617           (while articles
10618             (and
10619              (setq l (aref (car (car articles)) gnus-score-index))
10620              (funcall match-func match (timezone-make-date-sortable l))
10621              (progn
10622                (setq found t)
10623                (setcdr (car articles) (+ score (cdr (car articles))))))
10624             (setq articles (cdr articles)))
10625           ;; Update expire date
10626           (cond ((null date))           ;Permanent entry.
10627                 (found                  ;Match, update date.
10628                  (gnus-score-set 'touched '(t) alist)
10629                  (setcar (nthcdr 2 kill) now))
10630                 ((< date expire) ;Old entry, remove.
10631                  (gnus-score-set 'touched '(t) alist)
10632                  (setcdr entries (cdr rest))
10633                  (setq rest entries)))
10634           (setq entries rest))))))
10635
10636 (defun gnus-score-body (scores header now expire)
10637   (save-excursion
10638     (set-buffer nntp-server-buffer)
10639     (save-restriction
10640       (let* ((buffer-read-only nil)
10641              (articles gnus-scores-articles)
10642              (all-scores scores)
10643              (request-func (cond ((string= "head" (downcase header))
10644                                   'gnus-request-head)
10645                                  ((string= "body" (downcase header))
10646                                   'gnus-request-body)
10647                                  (t 'gnus-request-article)))
10648              alike last this art entries alist ofunc article)
10649         ;; Not all backends support partial fetching.  In that case,
10650         ;; we just fetch the entire article.
10651         (or (gnus-check-backend-function request-func gnus-newsgroup-name)
10652             (progn
10653               (setq ofunc request-func)
10654               (setq request-func 'gnus-request-article)))
10655         (while articles
10656           (setq article (header-number (car (car articles))))
10657           (message "Scoring on article %s..." article)
10658           (if (not (funcall request-func article gnus-newsgroup-name))
10659               ()
10660             (widen)
10661             (goto-char (point-min))
10662             ;; If just parts of the article is to be searched, but the
10663             ;; backend didn't support partial fetching, we just narrow
10664             ;; to the relevant parts.
10665             (if ofunc
10666                 (if (eq ofunc 'gnus-request-head)
10667                     (narrow-to-region
10668                      (point)
10669                      (or (search-forward "\n\n" nil t) (point-max)))
10670                   (narrow-to-region
10671                    (or (search-forward "\n\n" nil t) (point))
10672                    (point-max))))
10673             (setq scores all-scores)
10674             ;; Find matches.
10675             (while scores
10676               (setq alist (car scores)
10677                     scores (cdr scores)
10678                     entries (assoc header alist))
10679               (while (cdr entries)      ;First entry is the header index.
10680                 (let* ((rest (cdr entries))             
10681                        (kill (car rest))
10682                        (match (nth 0 kill))
10683                        (type (or (nth 3 kill) 's))
10684                        (score (or (nth 1 kill) 
10685                                   gnus-score-interactive-default-score))
10686                        (date (nth 2 kill))
10687                        (found nil)
10688                        (case-fold-search 
10689                         (not (or (eq type 'R) (eq type 'S)
10690                                  (eq type 'Regexp) (eq type 'String))))
10691                        (search-func 
10692                         (cond ((or (eq type 'r) (eq type 'R)
10693                                    (eq type 'regexp) (eq type 'Regexp))
10694                                're-search-forward)
10695                               ((or (eq type 's) (eq type 'S)
10696                                    (eq type 'string) (eq type 'String))
10697                                'search-forward)
10698                               (t
10699                                (error "Illegal match type: %s" type))))
10700                        arts art)
10701                   (goto-char (point-min))
10702                   (if (funcall search-func match nil t)
10703                       ;; Found a match, update scores.
10704                       (progn
10705                         (setcdr (car articles) (+ score (cdr (car articles))))
10706                         (setq found t)))
10707                   ;; Update expire date
10708                   (cond ((null date))   ;Permanent entry.
10709                         (found          ;Match, update date.
10710                          (gnus-score-set 'touched '(t) alist)
10711                          (setcar (nthcdr 2 kill) now))
10712                         ((< date expire) ;Old entry, remove.
10713                          (gnus-score-set 'touched '(t) alist)
10714                          (setcdr entries (cdr rest))
10715                          (setq rest entries)))
10716                   (setq entries rest)))))
10717           (setq articles (cdr articles)))))))
10718
10719 (defun gnus-score-string (scores header now expire)
10720   ;; Score ARTICLES according to HEADER in SCORES.
10721   ;; Update matches entries to NOW and remove unmatched entried older
10722   ;; than EXPIRE.
10723   
10724   ;; Insert the unique article headers in the buffer.
10725   (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
10726         ;; gnus-score-index is used as a free variable.
10727         alike last this art entries alist articles)
10728
10729     ;; Sorting the articles costs os O(N*log N) but will allow us to
10730     ;; only match with each unique header.  Thus the actual matching
10731     ;; will be O(M*U) where M is the number of strings to match with,
10732     ;; and U is the number of unique headers.  It is assumed (but
10733     ;; untested) this will be a net win because of the large constant
10734     ;; factor involved with string matching.
10735     (setq gnus-scores-articles (sort gnus-scores-articles 'gnus-score-string<)
10736           articles gnus-scores-articles)
10737
10738     (erase-buffer)
10739     (while articles
10740       (setq art (car articles)
10741             this (aref (car art) gnus-score-index)
10742             articles (cdr articles))
10743       (if (equal last this)
10744           ;; O(N*H) cons-cells used here, where H is the number of
10745           ;; headers.
10746           (setq alike (cons art alike))
10747         (if last
10748             (progn
10749               ;; Insert the line, with a text property on the
10750               ;; terminating newline refering to the articles with
10751               ;; this line.
10752               (insert last ?\n)
10753               (put-text-property (1- (point)) (point) 'articles alike)))
10754         (setq alike (list art)
10755               last this)))
10756     (and last                           ; Bwadr, duplicate code.
10757          (progn
10758            (insert last ?\n)                    
10759            (put-text-property (1- (point)) (point) 'articles alike)))
10760   
10761     ;; Find matches.
10762     (while scores
10763       (setq alist (car scores)
10764             scores (cdr scores)
10765             entries (assoc header alist))
10766       (while (cdr entries)              ;First entry is the header index.
10767         (let* ((rest (cdr entries))             
10768                (kill (car rest))
10769                (match (nth 0 kill))
10770                (type (or (nth 3 kill) 's))
10771                (score (or (nth 1 kill) gnus-score-interactive-default-score))
10772                (date (nth 2 kill))
10773                (found nil)
10774                (case-fold-search 
10775                 (not (or (eq type 'R) (eq type 'S)
10776                          (eq type 'Regexp) (eq type 'String))))
10777                (search-func (cond ((or (eq type 'r) (eq type 'R)
10778                                        (eq type 'regexp) (eq type 'Regexp))
10779                                    're-search-forward)
10780                                   ((or (eq type 's) (eq type 'S)
10781                                        (eq type 'string) (eq type 'String))
10782                                    'search-forward)
10783                                   (t
10784                                    (error "Illegal match type: %s" type))))
10785                arts art)
10786           (goto-char (point-min))
10787           (while (funcall search-func match nil t)
10788             (end-of-line 1)
10789             (setq found t
10790                   arts (get-text-property (point) 'articles))
10791             ;; Found a match, update scores.
10792             (while arts
10793               (setq art (car arts)
10794                     arts (cdr arts))
10795               (setcdr art (+ score (cdr art)))))
10796           ;; Update expire date
10797           (cond ((null date))           ;Permanent entry.
10798                 (found                  ;Match, update date.
10799                  (gnus-score-set 'touched '(t) alist)
10800                  (setcar (nthcdr 2 kill) now))
10801                 ((< date expire) ;Old entry, remove.
10802                  (gnus-score-set 'touched '(t) alist)
10803                  (setcdr entries (cdr rest))
10804                  (setq rest entries)))
10805           (setq entries rest))))))
10806
10807 (defun gnus-score-string< (a1 a2)
10808   ;; Compare headers in articles A2 and A2.
10809   ;; The header index used is the free variable `gnus-score-index'.
10810   (string-lessp (aref (car a1) gnus-score-index)
10811                 (aref (car a2) gnus-score-index)))
10812
10813 (defun gnus-score-build-cons (article)
10814   ;; Build a `gnus-newsgroup-scored' type cons from ARTICLE.
10815   (cons (header-number (car article)) (cdr article)))
10816
10817 (defconst gnus-header-index
10818   ;; Name to index alist.
10819   '(("number" 0 gnus-score-integer)
10820     ("subject" 1 gnus-score-string)
10821     ("from" 2 gnus-score-string)
10822     ("date" 3 gnus-score-date)
10823     ("message-id" 4 gnus-score-string) 
10824     ("references" 5 gnus-score-string) 
10825     ("chars" 6 gnus-score-integer) 
10826     ("lines" 7 gnus-score-integer) 
10827     ("xref" 8 gnus-score-string)
10828     ("head" -1 gnus-score-body)
10829     ("body" -1 gnus-score-body)
10830     ("all" -1 gnus-score-body)))
10831
10832 (defun gnus-score-file-name (newsgroup)
10833   "Return the name of a score file for NEWSGROUP."
10834   (cond  ((or (null newsgroup)
10835               (string-equal newsgroup ""))
10836           ;; The global score file is placed at top of the directory.
10837           (expand-file-name gnus-score-file-suffix
10838                             (or gnus-kill-files-directory "~/News")))
10839          ((gnus-use-long-file-name 'not-score)
10840           ;; Append ".SCORE" to newsgroup name.
10841           (expand-file-name (concat newsgroup "." gnus-score-file-suffix)
10842                             (or gnus-kill-files-directory "~/News")))
10843          (t
10844           ;; Place "SCORE" under the hierarchical directory.
10845           (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
10846                                     "/" gnus-score-file-suffix)
10847                             (or gnus-kill-files-directory "~/News")))))
10848
10849 (defun gnus-score-score-files (group)
10850   "Return a list of all possible score files."
10851   (and gnus-global-score-files 
10852        (or gnus-internal-global-score-files
10853            (gnus-score-search-global-directories gnus-global-score-files)))
10854   (setq gnus-kill-files-directory 
10855         (file-name-as-directory
10856          (or gnus-kill-files-directory "~/News/")))
10857   (if (not (file-readable-p gnus-kill-files-directory))
10858       (setq gnus-score-file-list nil)
10859     (if (gnus-use-long-file-name 'not-score)
10860         (if (or (not gnus-score-file-list)
10861                 (gnus-file-newer-than gnus-kill-files-directory
10862                                       (car gnus-score-file-list)))
10863               (setq gnus-score-file-list 
10864                     (cons (nth 5 (file-attributes gnus-kill-files-directory))
10865                           (nreverse 
10866                            (directory-files 
10867                             gnus-kill-files-directory t
10868                             (concat gnus-score-file-suffix "$"))))))
10869       (let ((dir (expand-file-name
10870                   (concat gnus-kill-files-directory
10871                           (gnus-replace-chars-in-string group ?. ?/))))
10872             (mdir (length (expand-file-name gnus-kill-files-directory)))
10873             files)
10874         (if (file-exists-p (concat dir "/" gnus-score-file-suffix))
10875             (setq files (list (concat dir "/" gnus-score-file-suffix))))
10876         (while (>= (1+ (length dir)) mdir)
10877           (and (file-exists-p (concat dir "/all/" gnus-score-file-suffix))
10878                (setq files (cons (concat dir "/all/" gnus-score-file-suffix)
10879                                  files)))
10880           (string-match "/[^/]*$" dir)
10881           (setq dir (substring dir 0 (match-beginning 0))))
10882         (setq gnus-score-file-list 
10883               (cons nil (nreverse files)))))
10884     (cdr gnus-score-file-list)))
10885         
10886 (defun gnus-score-find-single (group)
10887   "Return list containing the score file for GROUP."
10888   (list (gnus-score-file-name group)))
10889
10890 (defun gnus-score-find-hierarchical (group)
10891   "Return list of score files for GROUP.
10892 This includes the score file for the group and all its parents."
10893   (let ((all (copy-sequence '(nil)))
10894         (start 0))
10895     (while (string-match "\\." group (1+ start))
10896       (setq start (match-beginning 0))
10897       (setq all (cons (substring group 0 start) all)))
10898     (setq all (cons group all))
10899     (mapcar 'gnus-score-file-name (nreverse all))))
10900
10901 (defun gnus-score-find-bnews (group)
10902   "Return a list of score files for GROUP.
10903 The score files are those files in the ~/News directory which matches
10904 GROUP using BNews sys file syntax."
10905   (let* ((sfiles (append (gnus-score-score-files group)
10906                          gnus-internal-global-score-files))
10907          (kill-dir (file-name-as-directory 
10908                     (expand-file-name gnus-kill-files-directory)))
10909          (klen (length kill-dir))
10910          ofiles not-match regexp)
10911     (save-excursion
10912       (set-buffer (get-buffer-create "*gnus score files*"))
10913       (buffer-disable-undo (current-buffer))
10914       ;; Go through all score file names and create regexp with them
10915       ;; as the source.  
10916       (while sfiles
10917         (erase-buffer)
10918         (insert (car sfiles))
10919         (goto-char 1)
10920         ;; First remove the suffix itself.
10921         (re-search-forward (concat "." gnus-score-file-suffix "$"))
10922         (replace-match "") 
10923         (goto-char 1)
10924         (if (looking-at (regexp-quote kill-dir))
10925             ;; If the file name was just "SCORE", `klen' is one character
10926             ;; too much.
10927             (delete-char (min (1- (point-max)) klen))
10928           (goto-char (point-max))
10929           (search-backward "/")
10930           (delete-region (1+ (point)) (point-min)))
10931         ;; Translate "all" to ".*".
10932         (while (search-forward "all" nil t)
10933           (replace-match ".*"))
10934         (goto-char 1)
10935         ;; Deal with "not."s.
10936         (if (looking-at "not.")
10937             (progn
10938               (setq not-match t)
10939               (setq regexp (buffer-substring 5 (point-max))))
10940           (setq regexp (buffer-substring 1 (point-max)))
10941           (setq not-match nil))
10942         ;; Finally - if this resulting regexp matches the group name,
10943         ;; we add this score file to the list of score files
10944         ;; applicable to this group.
10945         (if (or (and not-match
10946                      (not (string-match regexp group)))
10947                 (and (not not-match)
10948                      (string-match regexp group)))
10949             (setq ofiles (cons (car sfiles) ofiles)))
10950         (setq sfiles (cdr sfiles)))
10951       (kill-buffer (current-buffer))
10952       ;; Slight kludge here - the last score file returned should be
10953       ;; the local score file, whether it exists or not. This is so
10954       ;; that any score commands the user enters will go to the right
10955       ;; file, and not end up in some global score file.
10956       (let ((localscore
10957              (expand-file-name
10958               (if (gnus-use-long-file-name 'not-score)
10959                   (concat gnus-kill-files-directory group "." 
10960                           gnus-score-file-suffix)
10961                 (concat gnus-kill-files-directory
10962                         (gnus-replace-chars-in-string group ?. ?/)
10963                         "/" gnus-score-file-suffix)))))
10964         (and (member localscore ofiles)
10965              (delete localscore ofiles))
10966         (setq ofiles (cons localscore ofiles)))
10967       (nreverse ofiles))))
10968
10969 (defun gnus-score-search-global-directories (files)
10970   "Scan all global score directories for score files."
10971   ;; Set the variable `gnus-internal-global-score-files' to all
10972   ;; available global score files.
10973   (interactive (list gnus-global-score-files))
10974   (let (out)
10975     (while files
10976       (if (string-match "/$" (car files))
10977           (setq out (nconc (directory-files 
10978                             (car files) t
10979                             (concat gnus-score-file-suffix "$"))))
10980         (setq out (cons (car files) out)))
10981       (setq files (cdr files)))
10982     (setq gnus-internal-global-score-files out)))
10983
10984 (defun gnus-current-score-file-nondirectory (&optional score-file)
10985   (let ((score-file (or score-file gnus-current-score-file)))
10986     (if score-file 
10987         (gnus-short-group-name (file-name-nondirectory score-file))
10988       "none")))
10989
10990 ;;;
10991 ;;; Score mode.
10992 ;;;
10993
10994 (defvar gnus-score-mode-map nil)
10995 (defvar gnus-score-mode-hook nil)
10996
10997 (if gnus-score-mode-map
10998     ()
10999   (setq gnus-score-mode-map (copy-keymap emacs-lisp-mode-map))
11000   (define-key gnus-score-mode-map "\C-c\C-c" 'gnus-score-edit-done)
11001   (define-key gnus-score-mode-map "\C-c\C-d" 'gnus-score-edit-insert-date))
11002
11003 (defun gnus-score-mode ()
11004   "Mode for editing score files.
11005 This mode is an extended emacs-lisp mode.
11006
11007 \\{gnus-score-mode-map}"
11008   (interactive)
11009   (kill-all-local-variables)
11010   (use-local-map gnus-score-mode-map)
11011   (set-syntax-table emacs-lisp-mode-syntax-table)
11012   (setq major-mode 'gnus-score-mode)
11013   (setq mode-name "Score")
11014   (lisp-mode-variables nil)
11015   (run-hooks 'emacs-lisp-mode-hook 'gnus-score-mode-hook))
11016
11017 (defun gnus-score-edit-insert-date ()
11018   "Insert date in numerical format."
11019   (interactive)
11020   (insert (int-to-string (gnus-day-number (current-time-string)))))
11021
11022 (defun gnus-score-edit-done ()
11023   "Save the score file and return to the summary buffer."
11024   (interactive)
11025   (let ((bufnam (buffer-file-name (current-buffer))))
11026     (save-buffer)
11027     (kill-buffer (current-buffer))
11028     (and gnus-winconf-edit-score
11029          (set-window-configuration gnus-winconf-edit-score))
11030     (gnus-score-remove-from-cache bufnam)
11031     (gnus-score-load-file bufnam)))
11032
11033 \f
11034 ;;;
11035 ;;; Gnus Posting Functions
11036 ;;;
11037
11038 (defvar gnus-organization-file "/usr/lib/news/organization"
11039   "*Local news organization file.")
11040
11041 (defvar gnus-post-news-buffer "*post-news*")
11042 (defvar gnus-winconf-post-news nil)
11043
11044 ;;; Post news commands of Gnus group mode and summary mode
11045
11046 (defun gnus-group-post-news ()
11047   "Post an article."
11048   (interactive)
11049   (gnus-set-global-variables)
11050   ;; Save window configuration.
11051   (setq gnus-winconf-post-news (current-window-configuration))
11052   (let ((gnus-newsgroup-name nil))
11053     (unwind-protect
11054         (if gnus-split-window 
11055             (progn
11056               (pop-to-buffer gnus-article-buffer)
11057               (widen)
11058               (split-window-vertically)
11059               (gnus-post-news 'post))
11060           (progn
11061             (pop-to-buffer gnus-article-buffer)
11062             (widen)
11063             (delete-other-windows)
11064             (gnus-post-news 'post))
11065           )
11066       (or (and (eq (current-buffer) (get-buffer gnus-post-news-buffer))
11067                (not (zerop (buffer-size))))
11068           ;; Restore last window configuration.
11069           (and gnus-winconf-post-news
11070                (set-window-configuration gnus-winconf-post-news)))))
11071   ;; We don't want to return to summary buffer nor article buffer later.
11072   (setq gnus-winconf-post-news nil)
11073   (if (get-buffer gnus-summary-buffer)
11074       (bury-buffer gnus-summary-buffer))
11075   (if (get-buffer gnus-article-buffer)
11076       (bury-buffer gnus-article-buffer)))
11077
11078 (defun gnus-summary-post-news ()
11079   "Post an article."
11080   (interactive)
11081   (gnus-set-global-variables)
11082   ;; Save window configuration.
11083   (setq gnus-winconf-post-news (current-window-configuration))
11084   (unwind-protect
11085       (gnus-post-news 'post gnus-newsgroup-name)
11086     (or (and (eq (current-buffer) (get-buffer gnus-post-news-buffer))
11087              (not (zerop (buffer-size))))
11088         ;; Restore last window configuration.
11089         (and gnus-winconf-post-news
11090              (set-window-configuration gnus-winconf-post-news))))
11091   ;; We don't want to return to article buffer later.
11092   (if (get-buffer gnus-article-buffer)
11093       (bury-buffer gnus-article-buffer)))
11094
11095 (defun gnus-summary-followup (yank)
11096   "Compose a followup to an article.
11097 If prefix argument YANK is non-nil, original article is yanked automatically."
11098   (interactive "P")
11099   (gnus-set-global-variables)
11100   (save-window-excursion
11101     (gnus-summary-select-article t))
11102   (let ((headers gnus-current-headers)
11103         (gnus-newsgroup-name gnus-newsgroup-name))
11104     ;; Check Followup-To: poster.
11105     (set-buffer gnus-article-buffer)
11106     (if (and gnus-use-followup-to
11107              (string-equal "poster" (gnus-fetch-field "followup-to"))
11108              (or (not (eq gnus-use-followup-to t))
11109                  (not (gnus-y-or-n-p 
11110                        "Do you want to ignore `Followup-To: poster'? "))))
11111         ;; Mail to the poster.  Gnus is now RFC1036 compliant.
11112         (gnus-summary-reply yank)
11113       ;; Save window configuration.
11114       (setq gnus-winconf-post-news (current-window-configuration))
11115       (unwind-protect
11116           (gnus-post-news nil gnus-newsgroup-name
11117                           headers gnus-article-buffer yank)
11118         (or (and (eq (current-buffer) (get-buffer gnus-post-news-buffer))
11119                  (not (zerop (buffer-size))))
11120             ;; Restore last window configuration.
11121             (and gnus-winconf-post-news
11122                  (set-window-configuration gnus-winconf-post-news))))
11123       ;; We don't want to return to article buffer later.
11124       (bury-buffer gnus-article-buffer)))
11125   (gnus-article-hide-headers-if-wanted))
11126
11127 (defun gnus-summary-followup-with-original ()
11128   "Compose a followup to an article and include the original article."
11129   (interactive)
11130   (gnus-summary-followup t))
11131
11132 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11133 (defun gnus-summary-followup-and-reply (yank)
11134   "Compose a followup and do an auto mail to author."
11135   (interactive "P")
11136   (let ((gnus-auto-mail-to-author t))
11137     (gnus-summary-followup yank)))
11138
11139 (defun gnus-summary-followup-and-reply-with-original ()
11140   "Compose a followup, include the original, and do an auto mail to author."
11141   (interactive)
11142   (gnus-summary-followup-and-reply t))
11143
11144 (defun gnus-summary-cancel-article ()
11145   "Cancel an article you posted."
11146   (interactive)
11147   (gnus-set-global-variables)
11148   (gnus-summary-select-article t)
11149   (gnus-eval-in-buffer-window gnus-article-buffer (gnus-cancel-news))
11150   (gnus-article-hide-headers-if-wanted))
11151
11152 (defun gnus-summary-supersede-article ()
11153   "Compose an article that will supersede a previous article.
11154 This is done simply by taking the old article and adding a Supersedes
11155 header line with the old Message-ID."
11156   (interactive)
11157   (gnus-set-global-variables)
11158   (if (not
11159        (string-equal
11160         (downcase (mail-strip-quoted-names 
11161                    (header-from gnus-current-headers)))
11162         (downcase (mail-strip-quoted-names (gnus-inews-user-name)))))
11163       (error "This article is not yours."))
11164   (gnus-summary-select-article t)
11165   (save-excursion
11166     (set-buffer gnus-article-buffer)
11167     (let ((buffer-read-only nil))
11168       (goto-char (point-min))
11169       (search-forward "\n\n" nil t)
11170       (if (not (re-search-backward "^Message-ID: " nil t))
11171           (error "No Message-ID in this article"))))
11172   (if (gnus-post-news 'post gnus-newsgroup-name)
11173       (progn
11174         (erase-buffer)
11175         (insert-buffer gnus-article-buffer)
11176         (goto-char (point-min))
11177         (search-forward "\n\n" nil t)
11178         (if (not (re-search-backward "^Message-ID: " nil t))
11179             (error "No Message-ID in this article")
11180           (replace-match "Supersedes: "))
11181         (search-forward "\n\n")
11182         (forward-line -1)
11183         (insert mail-header-separator))))
11184
11185 \f
11186 ;;;###autoload
11187 (fset 'sendnews 'gnus-post-news)
11188
11189 ;;;###autoload
11190 (fset 'postnews 'gnus-post-news)
11191
11192 (defun gnus-post-news (post &optional group header article-buffer yank)
11193   "Begin editing a new USENET news article to be posted.
11194 Type \\[describe-mode] in the buffer to get a list of commands."
11195   (interactive (list t))
11196   (if (or (not gnus-novice-user)
11197           gnus-expert-user
11198           (not (eq 'post 
11199                    (nth 1 (assoc 
11200                            (format "%s" (car (gnus-find-method-for-group 
11201                                               gnus-newsgroup-name)))
11202                            gnus-valid-select-methods))))
11203           (assq 'to-address (gnus-find-method-for-group gnus-newsgroup-name))
11204           (gnus-y-or-n-p "Are you sure you want to post to all of USENET? "))
11205       (let ((sumart (if (not post)
11206                         (save-excursion
11207                           (set-buffer gnus-summary-buffer)
11208                           (cons (current-buffer) gnus-current-article))))
11209             (from (and header (header-from header)))
11210             subject follow-to real-group)
11211         (and gnus-interactive-post
11212              (not gnus-expert-user)
11213              post (not group)
11214              (progn
11215                (setq group 
11216                      (completing-read "Group: " gnus-active-hashtb))
11217                (setq subject (read-string "Subject: "))))
11218         (setq mail-reply-buffer article-buffer)
11219
11220         (let ((gnus-newsgroup-name (or group gnus-newsgroup-name "")))
11221           (setq real-group (and group (gnus-group-real-name group)))
11222           (setq gnus-post-news-buffer 
11223                 (gnus-request-post-buffer 
11224                  post real-group subject header article-buffer
11225                  (nth 2 (and group (gnus-gethash group gnus-newsrc-hashtb)))
11226                  (if (and (boundp 'gnus-followup-to-function)
11227                           gnus-followup-to-function)
11228                      (setq follow-to
11229                            (save-excursion
11230                              (set-buffer article-buffer)
11231                              (funcall gnus-followup-to-function group))))
11232                  (eq gnus-use-followup-to t)))
11233           (if post
11234               (progn
11235                 (gnus-configure-windows '(1 0 0))
11236                 (switch-to-buffer gnus-post-news-buffer))
11237             (gnus-configure-windows '(0 1 0))
11238             (if (not yank)
11239                 (progn
11240                   (switch-to-buffer article-buffer)
11241                   (pop-to-buffer gnus-post-news-buffer))
11242               (switch-to-buffer gnus-post-news-buffer)))
11243           (gnus-overload-functions)
11244           (make-local-variable 'gnus-article-reply)
11245           (make-local-variable 'gnus-article-check-size)
11246           (setq gnus-article-reply sumart)
11247           ;; Handle `gnus-auto-mail-to-author'.
11248           ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11249           (let ((to (if (eq gnus-auto-mail-to-author 'ask)
11250                         (and (y-or-n-p "Also send mail to author? ") from)
11251                       (and gnus-auto-mail-to-author from))))
11252             (if to
11253                 (progn
11254                   (if (mail-fetch-field "To")
11255                       (progn
11256                         (beginning-of-line)
11257                         (insert "Cc: " to "\n"))
11258                     (mail-position-on-field "To")
11259                     (insert to)))))
11260           ;; Handle author copy using BCC field.
11261           (if (and gnus-mail-self-blind
11262                    (not (mail-fetch-field "bcc")))
11263               (progn
11264                 (mail-position-on-field "Bcc")
11265                 (insert (if (stringp gnus-mail-self-blind)
11266                             gnus-mail-self-blind
11267                           (user-login-name)))))
11268           ;; Handle author copy using FCC field.
11269           (if gnus-author-copy
11270               (progn
11271                 (mail-position-on-field "Fcc")
11272                 (insert gnus-author-copy)))
11273           (goto-char (point-min))
11274           (if post 
11275               (cond ((not group)
11276                      (re-search-forward "^Newsgroup:" nil t)
11277                      (end-of-line))
11278                     ((not subject)
11279                      (re-search-forward "^Subject:" nil t)
11280                      (end-of-line))
11281                     (t
11282                      (re-search-forward 
11283                       (concat "^" (regexp-quote mail-header-separator) "$"))
11284                      (forward-line 1)))
11285             (re-search-forward 
11286              (concat "^" (regexp-quote mail-header-separator) "$"))
11287             (forward-line 1)
11288             (and yank (save-excursion (news-reply-yank-original nil)))
11289             (if gnus-post-prepare-function
11290                 (funcall gnus-post-prepare-function group))))))
11291   (setq gnus-article-check-size (cons (buffer-size) (gnus-article-checksum)))
11292   (message "")
11293   t)
11294
11295 (defun gnus-inews-news (&optional use-group-method)
11296   "Send a news message.
11297 If given a prefix, and the group is a foreign group, this function
11298 will attempt to use the foreign server to post the article."
11299   (interactive "P")
11300   ;; Check whether the article is a good Net Citizen.
11301   (if (and gnus-article-check-size (not (gnus-inews-check-post)))
11302       ;; Aber nein!
11303       ()
11304     ;; Looks ok, so we do the nasty.
11305     (let* ((case-fold-search nil)
11306            (server-running (gnus-server-opened gnus-select-method))
11307            (reply gnus-article-reply))
11308       (save-excursion
11309         ;; Connect to default NNTP server if necessary.
11310         ;; Suggested by yuki@flab.fujitsu.junet.
11311         (gnus-start-news-server)        ;Use default server.
11312         ;; NNTP server must be opened before current buffer is modified.
11313         (widen)
11314         (goto-char (point-min))
11315         (run-hooks 'news-inews-hook)
11316         (save-restriction
11317           (narrow-to-region
11318            (point-min)
11319            (progn
11320              (goto-char (point-min))
11321              (re-search-forward 
11322               (concat "^" (regexp-quote mail-header-separator) "$"))))
11323
11324           ;; Correct newsgroups field: change sequence of spaces to comma and 
11325           ;; eliminate spaces around commas.  Eliminate imbedded line breaks.
11326           (goto-char (point-min))
11327           (if (search-forward-regexp "^Newsgroups: +" nil t)
11328               (save-restriction
11329                 (narrow-to-region
11330                  (point)
11331                  (if (re-search-forward "^[^ \t]" nil 'end)
11332                      (match-beginning 0)
11333                    (point-max)))
11334                 (goto-char (point-min))
11335                 (replace-regexp "\n[ \t]+" " ") ;No line breaks (too confusing)
11336                 (goto-char (point-min))
11337                 (replace-regexp "[ \t\n]*,[ \t\n]*\\|[ \t]+" ",")))
11338
11339           ;; Added by Per Abrahamsen <abraham@iesd.auc.dk>.
11340           ;; Help save the the world!
11341           (or 
11342            gnus-expert-user
11343            (let ((newsgroups (mail-fetch-field "newsgroups"))
11344                  (followup-to (mail-fetch-field "followup-to"))
11345                  groups to)
11346              (if (and (string-match "," newsgroups) (not followup-to))
11347                  (progn
11348                    (while (string-match "," newsgroups)
11349                      (setq groups
11350                            (cons (list (substring newsgroups
11351                                                   0 (match-beginning 0)))
11352                                  groups))
11353                      (setq newsgroups (substring newsgroups (match-end 0))))
11354                    (setq groups (nreverse (cons (list newsgroups) groups)))
11355
11356                    (setq to
11357                          (completing-read "Followups to: (default all groups) "
11358                                           groups))
11359                    (if (> (length to) 0)
11360                        (progn
11361                          (goto-char (point-min))
11362                          (insert "Followup-To: " to "\n")))))))
11363
11364           ;; Cleanup Followup-To.
11365           (goto-char (point-min))
11366           (if (search-forward-regexp "^Followup-To: +" nil t)
11367               (save-restriction
11368                 (narrow-to-region
11369                  (point)
11370                  (if (re-search-forward "^[^ \t]" nil 'end)
11371                      (match-beginning 0)
11372                    (point-max)))
11373                 (goto-char (point-min))
11374                 (replace-regexp "\n[ \t]+" " ") ;No line breaks (too confusing)
11375                 (goto-char (point-min))
11376                 (replace-regexp "[ \t\n]*,[ \t\n]*\\|[ \t]+" ",")))
11377
11378           ;; Mail the message too if To:, Bcc:. or Cc: exists.
11379           (if (or (mail-fetch-field "to" nil t)
11380                   (mail-fetch-field "bcc" nil t)
11381                   (mail-fetch-field "cc" nil t))
11382               (if gnus-mail-send-method
11383                   (save-excursion
11384                     (save-restriction
11385                       (widen)
11386                       (message "Sending via mail...")
11387                       
11388                       (if gnus-mail-courtesy-message
11389                           (progn
11390                             ;; Insert "courtesy" mail message.
11391                             (goto-char 1)
11392                             (re-search-forward
11393                              (concat "^" (regexp-quote
11394                                           mail-header-separator) "$"))
11395                             (forward-line 1)
11396                             (insert gnus-mail-courtesy-message)
11397                             (funcall gnus-mail-send-method)
11398                             (goto-char 1)
11399                             (search-forward gnus-mail-courtesy-message)
11400                             (replace-match ""))
11401                         (funcall gnus-mail-send-method))
11402
11403                       (message "Sending via mail... done")
11404                       
11405                       (goto-char 1)
11406                       (narrow-to-region
11407                        1 (re-search-forward 
11408                           (concat "^" (regexp-quote 
11409                                        mail-header-separator) "$")))
11410                       (goto-char 1)
11411                       (delete-matching-lines "BCC:.*")))
11412                 (ding)
11413                 (message "No mailer defined.  To: and/or Cc: fields ignored.")
11414                 (sit-for 1))))
11415
11416         ;; Send to NNTP server. 
11417         (message "Posting to USENET...")
11418         (if (gnus-inews-article use-group-method)
11419             (progn
11420               (message "Posting to USENET... done")
11421               (if (and reply
11422                        (get-buffer (car reply))
11423                        (buffer-name (car reply)))
11424                   (progn
11425                     (save-excursion
11426                       (set-buffer gnus-summary-buffer)
11427                       (gnus-summary-mark-article-as-replied 
11428                        (cdr reply))))))
11429           ;; We cannot signal an error.
11430           (ding) (message "Article rejected: %s" 
11431                           (gnus-status-message gnus-select-method)))
11432         (set-buffer-modified-p nil))
11433       ;; If NNTP server is opened by gnus-inews-news, close it by myself.
11434       (or server-running
11435           (gnus-close-server (gnus-find-method-for-group gnus-newsgroup-name)))
11436       (and (fboundp 'bury-buffer) (bury-buffer))
11437       ;; Restore last window configuration.
11438       (and gnus-winconf-post-news
11439            (set-window-configuration gnus-winconf-post-news))
11440       (setq gnus-winconf-post-news nil))))
11441
11442 (defun gnus-inews-check-post ()
11443   "Check whether the post looks ok."
11444   (and 
11445    ;; Check excessive size.
11446    (if (> (buffer-size) 60000)
11447        (gnus-y-or-n-p (format "The article is %d octets long. Really post? "
11448                               (buffer-size)))
11449      t)
11450    ;; Check for commands in Subject.
11451    (save-excursion
11452      (save-restriction
11453        (goto-char (point-min))
11454        (narrow-to-region 
11455         (point) 
11456         (re-search-forward 
11457          (concat "^" (regexp-quote mail-header-separator) "$")))
11458        (if (string-match "^cmsg " (mail-fetch-field "subject"))
11459            (gnus-y-or-n-p
11460             "The control code \"cmsg \" is in the subject. Really post? ")
11461          t)))
11462    ;; Check for control characters.
11463    (save-excursion
11464      (if (re-search-forward "[\000-\007\013\015-\037\200-\237]" nil t)
11465          (gnus-y-or-n-p 
11466           "The article contains control characters. Really post? ")
11467        t))
11468    ;; Check for multiple identical headers.
11469    (let (found)
11470      (save-excursion
11471        (save-restriction
11472          (goto-char (point-min))
11473          (narrow-to-region
11474           (point) 
11475           (re-search-forward 
11476            (concat "^" (regexp-quote mail-header-separator) "$")))
11477          (goto-char (point-min))
11478          (while (and (not found) (re-search-forward "^[^ \t:]+: " nil t))
11479            (save-excursion
11480              (or (re-search-forward 
11481                   (concat "^" (setq found
11482                                     (buffer-substring (match-beginning 0) 
11483                                                       (match-end 0))))
11484                   nil t)
11485                  (setq found nil))))
11486          (if found
11487              (gnus-y-or-n-p 
11488               (format "Multiple %s headers. Really post? " found))
11489            t))))
11490    ;; Check for version and sendsys.
11491    (save-excursion
11492      (save-restriction
11493        (goto-char (point-min))
11494        (narrow-to-region
11495         (point) 
11496         (re-search-forward 
11497          (concat "^" (regexp-quote mail-header-separator) "$")))
11498        (if (re-search-backward "^Sendsys:\\|^Version:" nil t)
11499            (gnus-yes-or-no-p
11500             (format "The article contains a %s command. Really post? "
11501                     (buffer-substring (match-beginning 0) (match-end 0))))
11502          t)))
11503    ;; Check the From header.
11504    (save-excursion
11505      (save-restriction
11506        (goto-char (point-min))
11507        (narrow-to-region
11508         (point)
11509         (re-search-forward
11510          (concat "^" (regexp-quote mail-header-separator) "$")))
11511        (let* ((case-fold-search t)
11512               (from (mail-fetch-field "from")))
11513          (if (and from
11514                   (string-match "@" from)
11515                   (not (string-match "@[^\\.]*\\." from)))
11516              (gnus-yes-or-no-p
11517               (format "The domain looks strange: \"%s\". Really post? "
11518                       from))
11519            t))))
11520    ;; Check for long lines.
11521    (save-excursion
11522      (save-restriction
11523        (goto-char (point-min))
11524        (narrow-to-region
11525         (point)
11526         (re-search-forward
11527          (concat "^" (regexp-quote mail-header-separator) "$")))
11528        (while 
11529            (and
11530             (progn
11531               (end-of-line)
11532               (< (current-column) 80))
11533             (zerop (forward-line 1))))
11534        (or (eobp)
11535            (gnus-yes-or-no-p
11536             (format
11537              "You have lines longer than 79 characters.  Really post? ")))))
11538    ;; Use the (size . checksum) variable to see whether the
11539    ;; article is empty or has only quoted text.
11540    (if (and (= (buffer-size) (car gnus-article-check-size))
11541             (= (gnus-article-checksum) (cdr gnus-article-check-size)))
11542        (gnus-yes-or-no-p
11543         "It looks like there's no new text in your article. Really post? ")
11544      t)))
11545
11546 (defun gnus-article-checksum ()
11547   (let ((sum 0))
11548     (save-excursion
11549       (while (not (eobp))
11550         (setq sum (logxor sum (following-char)))
11551         (forward-char 1)))
11552     sum))
11553
11554 (defun gnus-cancel-news ()
11555   "Cancel an article you posted."
11556   (interactive)
11557   (if (or gnus-expert-user
11558           (gnus-yes-or-no-p "Do you really want to cancel this article? "))
11559       (let ((from nil)
11560             (newsgroups nil)
11561             (message-id nil)
11562             (distribution nil))
11563         (save-excursion
11564           ;; Get header info. from original article.
11565           (save-restriction
11566             (gnus-article-show-all-headers)
11567             (goto-char (point-min))
11568             (search-forward "\n\n" nil 'move)
11569             (narrow-to-region (point-min) (point))
11570             (setq from (mail-fetch-field "from"))
11571             (setq newsgroups (mail-fetch-field "newsgroups"))
11572             (setq message-id (mail-fetch-field "message-id"))
11573             (setq distribution (mail-fetch-field "distribution")))
11574           ;; Verify if the article is absolutely user's by comparing
11575           ;; user id with value of its From: field.
11576           (if (not
11577                (string-equal
11578                 (downcase (mail-strip-quoted-names from))
11579                 (downcase (mail-strip-quoted-names (gnus-inews-user-name)))))
11580               (progn
11581                 (ding) (message "This article is not yours."))
11582             ;; Make control article.
11583             (set-buffer (get-buffer-create " *Gnus-canceling*"))
11584             (buffer-disable-undo (current-buffer))
11585             (erase-buffer)
11586             (insert "Newsgroups: " newsgroups "\n"
11587                     "Subject: cancel " message-id "\n"
11588                     "Control: cancel " message-id "\n"
11589                     mail-header-separator "\n"
11590                     "This is a cancel message from " from ".\n")
11591             ;; Send the control article to NNTP server.
11592             (message "Canceling your article...")
11593             (if (gnus-inews-article)
11594                 (message "Canceling your article... done")
11595               (ding) 
11596               (message "Cancel failed; %s" 
11597                        (gnus-status-message gnus-newsgroup-name)))
11598             ;; Kill the article buffer.
11599             (kill-buffer (current-buffer)))))))
11600
11601 \f
11602 ;;; Lowlevel inews interface
11603
11604 (defun gnus-inews-article (&optional use-group-method)
11605   "Post an article in current buffer using NNTP protocol."
11606   (let ((artbuf (current-buffer))
11607         (tmpbuf (get-buffer-create " *Gnus-posting*")))
11608     (widen)
11609     (goto-char (point-max))
11610     ;; require a newline at the end for inews to append .signature to
11611     (or (= (preceding-char) ?\n)
11612         (insert ?\n))
11613     ;; Prepare article headers.  All message body such as signature
11614     ;; must be inserted before Lines: field is prepared.
11615     (save-restriction
11616       (goto-char (point-min))
11617       (narrow-to-region 
11618        (point-min) 
11619        (save-excursion
11620          (re-search-forward 
11621           (concat "^" (regexp-quote mail-header-separator) "$"))
11622          (match-beginning 0)))
11623       (gnus-inews-insert-headers)
11624       (run-hooks gnus-inews-article-header-hook)
11625       (widen))
11626     (save-excursion
11627       (set-buffer tmpbuf)
11628       (buffer-disable-undo (current-buffer))
11629       (erase-buffer)
11630       (insert-buffer-substring artbuf)
11631       ;; Remove the header separator.
11632       (goto-char (point-min))
11633       (re-search-forward
11634        (concat "^" (regexp-quote mail-header-separator) "$"))
11635       (replace-match "")
11636       ;; This hook may insert a signature.
11637       (run-hooks 'gnus-prepare-article-hook)
11638       ;; Run final inews hooks.  This hook may do FCC.
11639       ;; The article must be saved before being posted because
11640       ;; `gnus-request-post' modifies the buffer.
11641       (run-hooks 'gnus-inews-article-hook)
11642       ;; Post an article to NNTP server.
11643       ;; Return NIL if post failed.
11644       (prog1
11645           (gnus-request-post 
11646            (if use-group-method
11647                (gnus-find-method-for-group gnus-newsgroup-name)
11648              gnus-select-method) use-group-method)
11649         (kill-buffer (current-buffer))))))
11650
11651 (defun gnus-inews-insert-headers ()
11652   "Prepare article headers.
11653 Headers already prepared in the buffer are not modified.
11654 Headers in `gnus-required-headers' will be generated."
11655   (let ((Date (gnus-inews-date))
11656         (Message-ID (gnus-inews-message-id))
11657         (Organization (gnus-inews-organization))
11658         (From (gnus-inews-user-name))
11659         (Path (gnus-inews-path))
11660         (Subject nil)
11661         (Newsgroups nil)
11662         (Distribution nil)
11663         (Lines (gnus-inews-lines))
11664         (X-Newsreader gnus-version)
11665         (headers gnus-required-headers)
11666         (case-fold-search t)
11667         header value elem)
11668     ;; First we remove any old Message-IDs. This might be slightly
11669     ;; fascist, but if the user really wants to generate Message-IDs
11670     ;; by herself, she should remove it from the `gnus-required-list'. 
11671     (goto-char (point-min))
11672     (and (memq 'Message-ID headers)
11673          (re-search-forward "^Message-ID:" nil t)
11674          (delete-region (progn (beginning-of-line) (point))
11675                         (progn (forward-line 1) (point))))
11676     ;; Remove NNTP-posting-host.
11677     (goto-char (point-min))
11678     (and (re-search-forward "^nntp-posting-host:" nil t)
11679          (delete-region (progn (beginning-of-line) (point))
11680                         (progn (forward-line 1) (point))))
11681     ;; Insert new Sender if the From is strange. 
11682     (let ((from (mail-fetch-field "from")))
11683       (if (and from (not (string= (downcase from) (downcase From))))
11684           (progn
11685             (goto-char (point-min))    
11686             (and (re-search-forward "^Sender:" nil t)
11687                  (delete-region (progn (beginning-of-line) (point))
11688                                 (progn (forward-line 1) (point))))
11689             (insert "Sender: " From "\n"))))
11690     ;; If there are References, and no "Re: ", then the thread has
11691     ;; changed name. See Son-of-1036.
11692     (if (and (mail-fetch-field "references")
11693              (get-buffer gnus-article-buffer))
11694         (let ((psubject (gnus-simplify-subject-re
11695                          (mail-fetch-field "subject")))
11696               subject)
11697           (save-excursion
11698             (set-buffer (get-buffer gnus-article-buffer))
11699             (save-restriction
11700               (gnus-narrow-to-headers)
11701               (if (setq subject (mail-fetch-field "subject"))
11702                   (progn
11703                     (and gnus-summary-gather-subject-limit
11704                          (numberp gnus-summary-gather-subject-limit)
11705                          (> (length subject) gnus-summary-gather-subject-limit)
11706                          (setq subject
11707                                (substring subject 0
11708                                           gnus-summary-gather-subject-limit)))
11709                     (setq subject (gnus-simplify-subject-re subject))))))
11710           (or (and psubject subject (string= subject psubject))
11711               (progn
11712                 (string-match "@" Message-ID)
11713                 (setq Message-ID
11714                       (concat (substring Message-ID 0 (match-beginning 0))
11715                               "_-_" 
11716                               (substring Message-ID (match-beginning 0))))))))
11717     ;; Go through all the required headers and see if they are in the
11718     ;; articles already. If they are not, or are empty, they are
11719     ;; inserted automatically - except for Subject, Newsgroups and
11720     ;; Distribution. 
11721     (while headers
11722       (goto-char (point-min))
11723       (setq elem (car headers))
11724       (if (consp elem)
11725           (setq header (car elem))
11726         (setq header elem))
11727       (if (or (not (re-search-forward 
11728                     (concat "^" (downcase (symbol-name header)) ":") nil t))
11729               (progn
11730                 (if (= (following-char) ? ) (forward-char 1) (insert " "))
11731                 (looking-at "[ \t]*$")))
11732           (progn
11733             (setq value 
11734                   (or (if (consp elem)
11735                           ;; The element is a cons.  Either the cdr is
11736                           ;; a string to be inserted verbatim, or it
11737                           ;; is a function, and we insert the value
11738                           ;; returned from this function.
11739                           (or (and (stringp (cdr elem)) (cdr elem))
11740                               (and (fboundp (cdr elem)) (funcall (cdr elem))))
11741                         ;; The element is a symbol.  We insert the
11742                         ;; value of this symbol, if any.
11743                         (and (boundp header) (symbol-value header)))
11744                       ;; We couldn't generate a value for this header,
11745                       ;; so we just ask the user.
11746                       (read-from-minibuffer
11747                        (format "Empty header for %s; enter value: " header))))
11748             (if (bolp)
11749                 (save-excursion
11750                   (goto-char (point-max))
11751                   (insert (symbol-name header) ": " value "\n"))
11752               (replace-match value))))
11753       (setq headers (cdr headers)))))
11754
11755 (defun gnus-inews-insert-signature ()
11756   "Insert a signature file.
11757 If `gnus-signature-function' is bound and returns a string, this
11758 string is used instead of the variable `gnus-signature-file'.
11759 In either case, if the string is a file name, this file is
11760 inserted. If the string is not a file name, the string itself is
11761 inserted. 
11762 If you never want any signature inserted, set both those variables to
11763 nil."
11764   (save-excursion
11765     (let ((signature 
11766            (or (and gnus-signature-function
11767                     (fboundp gnus-signature-function)
11768                     (funcall gnus-signature-function gnus-newsgroup-name))
11769                gnus-signature-file))
11770           b)
11771       (if (and signature
11772                (or (file-exists-p signature)
11773                    (string-match " " signature)
11774                    (not (string-match 
11775                          "^/[^/]+/" (expand-file-name signature)))))
11776           (progn
11777             (goto-char (point-max))
11778             ;; Delete any previous signatures.
11779             (if (and mail-signature (search-backward "\n-- \n" nil t))
11780                 (delete-region (1+ (point)) (point-max)))
11781             (insert "\n-- \n")
11782             (and (< 4 (setq b (count-lines 
11783                                (point)
11784                                (progn
11785                                  (if (file-exists-p signature)
11786                                      (insert-file-contents signature)
11787                                    (insert signature))
11788                                  (goto-char (point-max))
11789                                  (or (bolp) (insert "\n"))
11790                                  (point)))))
11791                  (not gnus-expert-user)
11792                  (not
11793                   (gnus-y-or-n-p
11794                    (format
11795                     "Your .sig is %d lines; it should be max 4. Really post? "
11796                     b)))
11797                  (if (file-exists-p signature)
11798                      (error (format "Edit %s." signature))
11799                    (error "Trim your signature."))))))))
11800
11801 (defun gnus-inews-do-fcc ()
11802   "Process FCC: fields in current article buffer.
11803 Unless the first character of the field is `|', the article is saved
11804 to the specified file using the function specified by the variable
11805 gnus-author-copy-saver.  The default function rmail-output saves in
11806 Unix mailbox format.
11807 If the first character is `|', the contents of the article is send to
11808 a program specified by the rest of the value."
11809   (let ((fcc-list nil)
11810         (fcc-file nil)
11811         (case-fold-search t))           ;Should ignore case.
11812     (save-excursion
11813       (save-restriction
11814         (goto-char (point-min))
11815         (search-forward "\n\n")
11816         (narrow-to-region (point-min) (point))
11817         (goto-char (point-min))
11818         (while (re-search-forward "^FCC:[ \t]*" nil t)
11819           (setq fcc-list
11820                 (cons (buffer-substring
11821                        (point)
11822                        (progn
11823                          (end-of-line)
11824                          (skip-chars-backward " \t")
11825                          (point)))
11826                       fcc-list))
11827           (delete-region (match-beginning 0)
11828                          (progn (forward-line 1) (point))))
11829         ;; Process FCC operations.
11830         (widen)
11831         (while fcc-list
11832           (setq fcc-file (car fcc-list))
11833           (setq fcc-list (cdr fcc-list))
11834           (cond ((string-match "^[ \t]*|[ \t]*\\(.*\\)[ \t]*$" fcc-file)
11835                  (let ((program (substring fcc-file
11836                                            (match-beginning 1) (match-end 1))))
11837                    ;; Suggested by yuki@flab.fujitsu.junet.
11838                    ;; Send article to named program.
11839                    (call-process-region (point-min) (point-max) shell-file-name
11840                                         nil nil nil "-c" program)))
11841                 (t
11842                  ;; Suggested by hyoko@flab.fujitsu.junet.
11843                  ;; Save article in Unix mail format by default.
11844                  (if (and gnus-author-copy-saver
11845                           (not (eq gnus-author-copy-saver 'rmail-output)))
11846                      (funcall gnus-author-copy-saver fcc-file)
11847                    (if (and (file-readable-p fcc-file) (rmail-file-p fcc-file))
11848                        (gnus-output-to-rmail fcc-file)
11849                      (rmail-output fcc-file 1 t t))))))))))
11850
11851 (defun gnus-inews-path ()
11852   "Return uucp path."
11853   (let ((login-name (gnus-inews-login-name)))
11854     (cond ((null gnus-use-generic-path)
11855            (concat (nth 1 gnus-select-method) "!" login-name))
11856           ((stringp gnus-use-generic-path)
11857            ;; Support GENERICPATH.  Suggested by vixie@decwrl.dec.com.
11858            (concat gnus-use-generic-path "!" login-name))
11859           (t login-name))))
11860
11861 (defun gnus-inews-user-name ()
11862   "Return user's network address as \"NAME@DOMAIN (FULL-NAME)\"."
11863   (let ((full-name (gnus-inews-full-name)))
11864     (or gnus-user-from-line
11865         (concat (if (or gnus-user-login-name gnus-use-generic-from
11866                         gnus-local-domain (getenv "DOMAINNAME"))
11867                     (concat (gnus-inews-login-name) "@"
11868                             (gnus-inews-domain-name gnus-use-generic-from))
11869                   user-mail-address)
11870                 ;; User's full name.
11871                 (cond ((string-equal full-name "") "")
11872                       ((string-equal full-name "&") ;Unix hack.
11873                        (concat " (" (user-login-name) ")"))
11874                       (t
11875                        (concat " (" full-name ")")))))))
11876
11877 (defun gnus-inews-login-name ()
11878   "Return login name."
11879   (or gnus-user-login-name (getenv "LOGNAME") (user-login-name)))
11880
11881 (defun gnus-inews-full-name ()
11882   "Return full user name."
11883   (or gnus-user-full-name (getenv "NAME") (user-full-name)))
11884
11885 (defun gnus-inews-domain-name (&optional genericfrom)
11886   "Return user's domain name.
11887 If optional argument GENERICFROM is a string, use it as the domain
11888 name; if it is non-nil, strip off local host name from the domain name.
11889 If the function `system-name' returns full internet name and the
11890 domain is undefined, the domain name is got from it."
11891   (if (or genericfrom gnus-local-domain (getenv "DOMAINNAME"))
11892       (let* ((system-name (system-name))
11893              (domain 
11894               (or (if (stringp genericfrom) genericfrom)
11895                   (getenv "DOMAINNAME")
11896                   gnus-local-domain
11897                   ;; Function `system-name' may return full internet name.
11898                   ;; Suggested by Mike DeCorte <mrd@sun.soe.clarkson.edu>.
11899                   (if (string-match "\\." system-name)
11900                       (substring system-name (match-end 0)))
11901                   (read-string "Domain name (no host): ")))
11902              (host (or (if (string-match "\\." system-name)
11903                            (substring system-name 0 (match-beginning 0)))
11904                        system-name)))
11905         (if (string-equal "." (substring domain 0 1))
11906             (setq domain (substring domain 1)))
11907         ;; Support GENERICFROM as same as standard Bnews system.
11908         ;; Suggested by ohm@kaba.junet and vixie@decwrl.dec.com.
11909         (cond ((null genericfrom)
11910                (concat host "." domain))
11911               ;;((stringp genericfrom) genericfrom)
11912               (t domain)))
11913     (if (string-match "\\." (system-name))
11914         (system-name)
11915       (substring user-mail-address 
11916                  (1+ (string-match "@" user-mail-address))))))
11917
11918 (defun gnus-inews-full-address ()
11919   (let ((domain (gnus-inews-domain-name))
11920         (system (system-name))
11921         (case-fold-search t))
11922     (if (string-match "\\." system) system
11923       (if (string-match (concat "^" (regexp-quote system)) domain) domain
11924         (concat system "." domain)))))
11925
11926 (defun gnus-inews-message-id ()
11927   "Generate unique Message-ID for user."
11928   ;; Message-ID should not contain a slash and should be terminated by
11929   ;; a number.  I don't know the reason why it is so.
11930   (concat "<" (gnus-inews-unique-id) "@" (gnus-inews-full-address) ">"))
11931
11932 (defun gnus-inews-unique-id ()
11933   "Generate unique ID from user name and current time."
11934   (concat (downcase (gnus-inews-login-name))
11935           (mapconcat 
11936            (lambda (num) (gnus-number-base-x num 3 31))
11937            (current-time) "")))
11938
11939 (defun gnus-inews-date ()
11940   "Current time string."
11941   (timezone-make-date-arpa-standard 
11942    (current-time-string) (current-time-zone)))
11943
11944 (defun gnus-inews-organization ()
11945   "Return user's organization.
11946 The ORGANIZATION environment variable is used if defined.
11947 If not, the variable `gnus-local-organization' is used instead.
11948 If it is a function, the function will be called with the current
11949 newsgroup name as the argument.
11950 If this is a file name, the contents of this file will be used as the
11951 organization."
11952   (let* ((organization 
11953           (or (getenv "ORGANIZATION")
11954               (if gnus-local-organization
11955                   (if (and (symbolp gnus-local-organization)
11956                            (fboundp gnus-local-organization))
11957                       (funcall gnus-local-organization gnus-newsgroup-name)
11958                     gnus-local-organization))
11959               gnus-organization-file
11960               "~/.organization")))
11961     (and (stringp organization)
11962          (> (length organization) 0)
11963          (or (file-exists-p organization)
11964              (string-match " " organization)
11965              (not (string-match  "^/[^/]+/" (expand-file-name organization))))
11966          (save-excursion
11967            (set-buffer (get-buffer-create " *Gnus organization*"))
11968            (buffer-disable-undo (current-buffer))
11969            (erase-buffer)
11970            (if (file-exists-p organization)
11971                (insert-file-contents organization)
11972              (insert organization))
11973            (goto-char (point-min))
11974            (while (re-search-forward " *\n *" nil t)
11975              (replace-match " "))
11976            (buffer-substring (point-min) (point-max))))))
11977
11978 (defun gnus-inews-lines ()
11979   "Count the number of lines and return numeric string."
11980   (save-excursion
11981     (save-restriction
11982       (widen)
11983       (goto-char (point-min))
11984       (search-forward "\n\n" nil 'move)
11985       (int-to-string (count-lines (point) (point-max))))))
11986
11987 \f
11988 ;;;
11989 ;;; Gnus Mail Functions 
11990 ;;;
11991
11992 ;;; Mail reply commands of Gnus summary mode
11993
11994 (defun gnus-summary-reply (yank)
11995   "Reply mail to news author.
11996 If prefix argument YANK is non-nil, original article is yanked automatically.
11997 Customize the variable gnus-mail-reply-method to use another mailer."
11998   (interactive "P")
11999   ;; Bug fix by jbw@bigbird.bu.edu (Joe Wells)
12000   ;; Stripping headers should be specified with mail-yank-ignored-headers.
12001   (gnus-set-global-variables)
12002   (setq gnus-winconf-post-news (current-window-configuration))
12003   (gnus-summary-select-article t)
12004   (let ((gnus-newsgroup-name gnus-newsgroup-name))
12005     (bury-buffer gnus-article-buffer)
12006     (funcall gnus-mail-reply-method yank))
12007   (gnus-article-hide-headers-if-wanted))
12008
12009 (defun gnus-summary-reply-with-original ()
12010   "Reply mail to news author with original article.
12011 Customize the variable gnus-mail-reply-method to use another mailer."
12012   (interactive)
12013   (gnus-summary-reply t))
12014
12015 (defun gnus-summary-mail-forward ()
12016   "Forward the current message to another user.
12017 Customize the variable gnus-mail-forward-method to use another mailer."
12018   (interactive)
12019   (gnus-summary-select-article t)
12020   (setq gnus-winconf-post-news (current-window-configuration))
12021   (or gnus-split-window 
12022       (switch-to-buffer gnus-article-buffer))
12023   (widen)
12024   (or gnus-split-window (delete-other-windows))
12025   (or gnus-split-window (bury-buffer gnus-article-buffer))
12026   (let ((gnus-newsgroup-name gnus-newsgroup-name))
12027     (funcall gnus-mail-forward-method))
12028   (gnus-article-hide-headers-if-wanted))
12029
12030 (defun gnus-summary-mail-other-window ()
12031   "Compose mail in other window.
12032 Customize the variable `gnus-mail-other-window-method' to use another
12033 mailer."
12034   (interactive)
12035   (setq gnus-winconf-post-news (current-window-configuration))
12036   (let ((gnus-newsgroup-name gnus-newsgroup-name))
12037     (funcall gnus-mail-other-window-method)))
12038
12039 (defun gnus-mail-reply-using-mail (&optional yank to-address)
12040   (save-excursion
12041     (set-buffer gnus-summary-buffer)
12042     (let ((info (nth 2 (gnus-gethash gnus-newsgroup-name gnus-newsrc-hashtb)))
12043           (group (gnus-group-real-name gnus-newsgroup-name))
12044           (cur (cons (current-buffer) (cdr gnus-article-current)))
12045           from subject date to reply-to message-of
12046           references message-id sender follow-to cc)
12047       (set-buffer (get-buffer-create "*mail*"))
12048       (mail-mode)
12049       (make-local-variable 'gnus-article-reply)
12050       (setq gnus-article-reply cur)
12051       (use-local-map (copy-keymap mail-mode-map))
12052       (local-set-key "\C-c\C-c" 'gnus-mail-send-and-exit)
12053       (if (and (buffer-modified-p)
12054                (> (buffer-size) 0)
12055                (not (gnus-y-or-n-p 
12056                      "Unsent article being composed; erase it? ")))
12057           ()
12058         (erase-buffer)
12059         (save-excursion
12060           (set-buffer gnus-article-buffer)
12061           (let ((buffer-read-only nil))
12062             (goto-char (point-min))
12063             (narrow-to-region (point-min)
12064                               (progn (search-forward "\n\n") (point)))
12065             (add-text-properties (point-min) (point-max) '(invisible nil)))
12066           (if (and (boundp 'gnus-reply-to-function)
12067                    gnus-reply-to-function)
12068               (save-excursion
12069                 (save-restriction
12070                   (gnus-narrow-to-headers)
12071                   (setq follow-to (funcall gnus-reply-to-function group)))))
12072           (setq from (mail-fetch-field "from"))
12073           (setq date (mail-fetch-field "date"))
12074           (and from
12075                (let ((stop-pos 
12076                       (string-match "  *at \\|  *@ \\| *(\\| *<" from)))
12077                  (setq message-of
12078                        (concat (if stop-pos (substring from 0 stop-pos) from)
12079                                "'s message of " date))))
12080           (setq sender (mail-fetch-field "sender"))
12081           (setq subject (or (mail-fetch-field "subject")
12082                             "Re: none"))
12083           (or (string-match "^[Rr][Ee]:" subject)
12084               (setq subject (concat "Re: " subject)))
12085           (setq cc (mail-fetch-field "cc"))
12086           (setq reply-to (mail-fetch-field "reply-to"))
12087           (setq references (mail-fetch-field "references"))
12088           (setq message-id (mail-fetch-field "message-id"))
12089           (widen))
12090         (setq news-reply-yank-from from)
12091         (setq news-reply-yank-message-id message-id)
12092         (mail-setup (or to-address 
12093                         (if (and follow-to (not (stringp follow-to))) ""
12094                           (or follow-to reply-to from sender "")))
12095                     subject message-of nil gnus-article-buffer nil)
12096         (if (and follow-to (listp follow-to))
12097             (progn
12098               (goto-char (point-min))
12099               (while follow-to
12100                 (insert (car (car follow-to)) ": " (cdr (car follow-to)) "\n")
12101                 (setq follow-to (cdr follow-to)))))
12102         ;; Fold long references line to follow RFC1036.
12103         (mail-position-on-field "References")
12104         (let ((begin (- (point) (length "References: ")))
12105               (fill-column 78)
12106               (fill-prefix "\t"))
12107           (if references (insert references))
12108           (if (and references message-id) (insert " "))
12109           (if message-id (insert message-id))
12110           ;; The region must end with a newline to fill the region
12111           ;; without inserting extra newline.
12112           (fill-region-as-paragraph begin (1+ (point))))
12113         (goto-char (point-min))
12114         (re-search-forward
12115          (concat "^" (regexp-quote mail-header-separator) "$"))
12116         (forward-line 1)
12117         (if yank
12118             (let ((last (point)))
12119               (save-excursion
12120                 (mail-yank-original nil))
12121               (run-hooks 'news-reply-header-hook)
12122               (goto-char last))))
12123       (let ((mail (current-buffer)))
12124         (if yank
12125             (progn
12126               (gnus-configure-windows '(0 1 0))
12127               (switch-to-buffer mail))
12128           (gnus-configure-windows '(0 0 1))
12129           (switch-to-buffer-other-window mail))))))
12130
12131 (defun gnus-mail-yank-original ()
12132   (interactive)
12133   (save-excursion
12134    (mail-yank-original nil))
12135   (run-hooks 'news-reply-header-hook))
12136
12137 (defun gnus-mail-send-and-exit ()
12138   (interactive)
12139   (let ((cbuf (current-buffer)))
12140     (mail-send-and-exit nil)
12141     (if (get-buffer gnus-group-buffer)
12142         (progn
12143           (save-excursion
12144             (set-buffer cbuf)
12145             (let ((reply gnus-article-reply))
12146               (if (and reply
12147                        (get-buffer (car reply))
12148                        (buffer-name (car reply)))
12149                   (progn
12150                     (set-buffer (car reply))
12151                     (and (cdr reply)
12152                          (gnus-summary-mark-article-as-replied 
12153                           (cdr reply)))))))
12154           (and gnus-winconf-post-news
12155                (set-window-configuration gnus-winconf-post-news))
12156           (setq gnus-winconf-post-news nil)))))
12157
12158 (defun gnus-mail-forward-using-mail ()
12159   "Forward the current message to another user using mail."
12160   ;; This is almost a carbon copy of rmail-forward in rmail.el.
12161   (let ((forward-buffer (current-buffer))
12162         (subject
12163          (concat "[" (if (memq 'mail (assoc (symbol-name 
12164                                              (car (gnus-find-method-for-group 
12165                                                    gnus-newsgroup-name)))
12166                                             gnus-valid-select-methods))
12167                          (gnus-fetch-field "From")
12168                        gnus-newsgroup-name)
12169                  "] " (or (gnus-fetch-field "Subject") "")))
12170         beg)
12171     ;; If only one window, use it for the mail buffer.
12172     ;; Otherwise, use another window for the mail buffer
12173     ;; so that the Rmail buffer remains visible
12174     ;; and sending the mail will get back to it.
12175     (if (if (one-window-p t)
12176             (mail nil nil subject)
12177           (mail-other-window nil nil subject))
12178         (save-excursion
12179           (use-local-map (copy-keymap emacs-lisp-mode-map))
12180           (local-set-key "\C-c\C-c" 'gnus-mail-send-and-exit)
12181           (setq beg (goto-char (point-max)))
12182           (insert "------- Start of forwarded message -------\n")
12183           (insert-buffer forward-buffer)
12184           (goto-char (point-max))
12185           (insert "------- End of forwarded message -------\n")
12186           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>. 
12187           (goto-char beg)
12188           (while (setq beg (next-single-property-change (point) 'invisible))
12189             (goto-char beg)
12190             (delete-region beg (or (next-single-property-change 
12191                                     (point) 'invisible)
12192                                    (point-max))))
12193           ;; You have a chance to arrange the message.
12194           (run-hooks 'gnus-mail-forward-hook)))))
12195
12196 (defun gnus-mail-other-window-using-mail ()
12197   "Compose mail other window using mail."
12198   (mail-other-window nil nil nil nil nil (get-buffer gnus-article-buffer))
12199   (use-local-map (copy-keymap (current-local-map)))
12200   (local-set-key "\C-c\C-c" 'gnus-mail-send-and-exit))
12201
12202 \f
12203 ;;;
12204 ;;; Dribble file
12205 ;;;
12206
12207 (defvar gnus-dribble-ignore nil)
12208
12209 (defun gnus-dribble-file-name ()
12210   (concat gnus-startup-file "-dribble"))
12211
12212 (defun gnus-dribble-open ()
12213   (save-excursion 
12214     (set-buffer 
12215      (setq gnus-dribble-buffer (find-file-noselect (gnus-dribble-file-name))))
12216     (buffer-disable-undo (current-buffer))
12217     (bury-buffer gnus-dribble-buffer)
12218     (auto-save-mode t)
12219     (goto-char (point-max))))
12220
12221 (defun gnus-dribble-enter (string)
12222   (if (and (not gnus-dribble-ignore)
12223            gnus-dribble-buffer
12224            (buffer-name gnus-dribble-buffer))
12225       (let ((obuf (current-buffer)))
12226         (set-buffer gnus-dribble-buffer)
12227         (insert string "\n")
12228         (set-window-point (get-buffer-window (current-buffer)) (point-max))
12229         (set-buffer obuf))))
12230
12231 (defun gnus-dribble-read-file ()
12232   (let ((dribble-file (gnus-dribble-file-name)))
12233     (save-excursion 
12234       (set-buffer (setq gnus-dribble-buffer 
12235                         (get-buffer-create 
12236                          (file-name-nondirectory dribble-file))))
12237       (gnus-add-current-to-buffer-list)
12238       (erase-buffer)
12239       (set-visited-file-name dribble-file)
12240       (buffer-disable-undo (current-buffer))
12241       (bury-buffer (current-buffer))
12242       (set-buffer-modified-p nil)
12243       (let ((auto (make-auto-save-file-name))
12244             (gnus-dribble-ignore t))
12245         (if (or (file-exists-p auto) (file-exists-p dribble-file))
12246             (progn
12247               (if (file-newer-than-file-p auto dribble-file)
12248                   (setq dribble-file auto))
12249               (insert-file-contents dribble-file)
12250               (if (not (zerop (buffer-size)))
12251                   (set-buffer-modified-p t))
12252               (if (gnus-y-or-n-p 
12253                    "Auto-save file exists. Do you want to read it? ")
12254                   (progn
12255                     (message "Reading %s..." dribble-file) 
12256                     (eval-current-buffer)
12257                     (message "Reading %s...done" dribble-file)))))))))
12258
12259 (defun gnus-dribble-delete-file ()
12260   (if (file-exists-p (gnus-dribble-file-name))
12261       (delete-file (gnus-dribble-file-name)))
12262   (if gnus-dribble-buffer
12263       (save-excursion
12264         (set-buffer gnus-dribble-buffer)
12265         (let ((auto (make-auto-save-file-name)))
12266           (if (file-exists-p auto)
12267               (delete-file auto))
12268           (erase-buffer)
12269           (set-buffer-modified-p nil)))))
12270
12271 (defun gnus-dribble-save ()
12272   (if (and gnus-dribble-buffer
12273            (buffer-name gnus-dribble-buffer))
12274       (save-excursion
12275         (set-buffer gnus-dribble-buffer)
12276         (save-buffer))))
12277
12278 (defun gnus-dribble-clear ()
12279   (save-excursion
12280     (if (and gnus-dribble-buffer
12281              (buffer-name (get-buffer gnus-dribble-buffer)))
12282         (progn
12283           (set-buffer gnus-dribble-buffer)
12284           (erase-buffer)
12285           (set-buffer-modified-p nil)
12286           (setq buffer-saved-size (buffer-size))))))
12287
12288 ;;;
12289 ;;; Server Communication
12290 ;;;
12291
12292 ;; All the Gnus backends have the same interface, and should return
12293 ;; data in a similar format. Below is an overview of what functions
12294 ;; these packages must supply and what results they should return.
12295 ;;
12296 ;; Variables:
12297 ;;
12298 ;; `nntp-server-buffer' - All data should be returned to Gnus in this
12299 ;; buffer. 
12300 ;;
12301 ;; Functions for the imaginary backend `choke':
12302 ;;
12303 ;; `choke-retrieve-headers ARTICLES &optional GROUP SERVER'
12304 ;; Should return all headers for all ARTICLES, or return NOV lines for
12305 ;; the same.
12306 ;;
12307 ;; `choke-request-group GROUP &optional SERVER DISCARD'
12308 ;; Switch to GROUP. If DISCARD is nil, active information on the group
12309 ;; must be returned.
12310 ;;
12311 ;; `choke-close-group GROUP &optional SERVER'
12312 ;; Close group. Most backends won't have to do anything with this
12313 ;; call, but it is an opportunity to clean up, if that is needed. It
12314 ;; is called when Gnus exits a group.
12315 ;;
12316 ;; `choke-request-article ARTICLE &optional GROUP SERVER'
12317 ;; Return ARTICLE, which is either an article number or
12318 ;; message-id. Note that not all backends can return articles based on
12319 ;; message-id. 
12320 ;;
12321 ;; `choke-request-list SERVER'
12322 ;; Return a list of all newsgroups on SERVER.
12323 ;;
12324 ;; `choke-request-list-newsgroups SERVER'
12325 ;; Return a list of descriptions of all newsgroups on SERVER.
12326 ;;
12327 ;; `choke-request-newgroups DATE &optional SERVER'
12328 ;; Return a list of all groups that have arrived after DATE on
12329 ;; SERVER. Note that the date doesn't have to be respected - Gnus will
12330 ;; always check whether the groups are old or not. Backends that do
12331 ;; not store date information may just return the entire list of
12332 ;; groups, although this might not be a good idea in general.
12333 ;;
12334 ;; `choke-request-post-buffer METHOD HEADER ARTICLE-BUFFER GROUP INFO'
12335 ;; Should return a buffer that is suitable for "posting". nnspool and
12336 ;; nntp return a `*post-buffer*', and nnmail return a `*mail*'
12337 ;; buffer. This function should fill out the appropriate headers. 
12338 ;;
12339 ;; `choke-request-post &optional SERVER'
12340 ;; Function that will be called from a buffer to be posted. 
12341 ;;
12342 ;; `choke-open-server SERVER &optional ARGUMENT'
12343 ;; Open a connection to SERVER.
12344 ;;
12345 ;; `choke-close-server &optional SERVER'
12346 ;; Close the connection to SERVER.
12347 ;;
12348 ;; `choke-server-opened &optional SERVER'
12349 ;; Whether the conenction to SERVER is opened or not.
12350 ;;
12351 ;; `choke-server-status &optional SERVER'
12352 ;; Should return a status string (not in the nntp buffer, but as the
12353 ;; result of the function).
12354 ;;
12355 ;; The following functions are optional and apply only to backends
12356 ;; that are able to control the contents of their groups totally
12357 ;; (ie. mail backends.)  Backends that aren't able to do that
12358 ;; shouldn't define these functions at all. Gnus will check for their
12359 ;; presence before attempting to call them.
12360 ;;
12361 ;; `choke-request-expire-articles ARTICLES &optional NEWSGROUP SERVER'
12362 ;; Should expire (according to some aging scheme) all ARTICLES. Most
12363 ;; backends will not be able to expire articles. Should return a list
12364 ;; of all articles that were not expired.
12365 ;;
12366 ;; `choke-request-move-article ARTICLE GROUP SERVER ACCEPT-FORM &optional LAST'
12367 ;; Should move ARTICLE from GROUP on SERVER by using ACCEPT-FORM.
12368 ;; Removes any information it has added to the article (extra headers,
12369 ;; whatever - make it as clean as possible), and then passes the
12370 ;; article on by evaling ACCEPT-FORM, which is normally a call to the
12371 ;; function described below. If the ACCEPT-FORM returns a non-nil
12372 ;; value, the article should then be deleted. If LAST is nil, that
12373 ;; means that there will be further calls to this function. This might
12374 ;; be taken as an advice not to save buffers/internal variables just
12375 ;; yet, but wait until the last call to speed things up.
12376 ;;
12377 ;; `choke-request-accept-article GROUP &optional LAST' 
12378 ;; The contents of the current buffer will be put into GROUP.  There
12379 ;; should, of course, be an article in the current buffer.  This
12380 ;; function is normally only called by the function described above,
12381 ;; and LAST works the same way as in that function.
12382 ;;
12383 ;; `choke-request-replace-article ARTICLE GROUP BUFFER'
12384 ;; Replace ARTICLE in GROUP with the contents of BUFFER.
12385 ;; This provides an easy interface for allowing editing of
12386 ;; articles. Note that even headers may be edited, so the backend has
12387 ;; to update any tables (nov buffers, etc) that it maintains after
12388 ;; replacing the article.
12389 ;;
12390 ;; `choke-request-create-group GROUP &optional SERVER'
12391 ;; Create GROUP on SERVER.  This might be a new, empty group, or it
12392 ;; might be a group that already exists, but hasn't been registered
12393 ;; yet. 
12394 ;;
12395 ;; All these functions must return nil if they couldn't service the
12396 ;; request. If the optional arguments are not supplied, some "current"
12397 ;; or "default" values should be used. In short, one should emulate an
12398 ;; NNTP server, in a way.
12399 ;;
12400 ;; If you want to write a new backend, you just have to supply the
12401 ;; functions listed above. In addition, you must enter the new backend
12402 ;; into the list of valid select methods:
12403 ;; (setq gnus-valid-select-methods 
12404 ;;       (cons '("choke" mail) gnus-valid-select-methods))
12405 ;; The first element in this list is the name of the backend. Other
12406 ;; elemnets may be `mail' (for mail groups),  `post' (for news
12407 ;; groups), `none' (neither), `respool' (for groups that can control
12408 ;; their contents). 
12409
12410 (defun gnus-start-news-server (&optional confirm)
12411   "Open a method for getting news.
12412 If CONFIRM is non-nil, the user will be asked for an NNTP server."
12413   (let (how where)
12414     (if gnus-current-select-method
12415         ;; Stream is already opened.
12416         nil
12417       ;; Open NNTP server.
12418       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
12419       (if confirm
12420           (progn
12421             ;; Read server name with completion.
12422             (setq gnus-nntp-server
12423                   (completing-read "NNTP server: "
12424                                    (mapcar (lambda (server) (list server))
12425                                            (cons (list gnus-nntp-server)
12426                                                  gnus-secondary-servers))
12427                                    nil nil gnus-nntp-server))))
12428
12429       (if (and gnus-nntp-server 
12430                (stringp gnus-nntp-server)
12431                (not (string= gnus-nntp-server "")))
12432           (setq gnus-select-method
12433                 (cond ((or (string= gnus-nntp-server "")
12434                            (string= gnus-nntp-server "::"))
12435                        (list 'nnspool (system-name)))
12436                       ((string-match ":" gnus-nntp-server)
12437                        (list 'nnmh gnus-nntp-server))
12438                       (t
12439                        (list 'nntp gnus-nntp-server)))))
12440
12441       (setq how (car gnus-select-method))
12442       (setq where (car (cdr gnus-select-method)))
12443       (cond ((eq how 'nnspool)
12444              (require 'nnspool)
12445              (message "Looking up local news spool..."))
12446             ((eq how 'nnmh)
12447              (require 'nnmh)
12448              (message "Looking up mh spool..."))
12449             (t
12450              (require 'nntp)))
12451       (setq gnus-current-select-method gnus-select-method)
12452       (run-hooks 'gnus-open-server-hook)
12453       (or 
12454        ;; gnus-open-server-hook might have opened it
12455        (gnus-server-opened gnus-select-method)  
12456        (gnus-open-server gnus-select-method)
12457        (gnus-y-or-n-p
12458         (format
12459          "%s server on %s can't be opened. Continue? "
12460          (car gnus-select-method) (nth 1 gnus-select-method)))
12461        (progn
12462          (message "Couldn't open server on %s" (nth 1 gnus-select-method))
12463          (ding)
12464          nil)))))
12465
12466 (defun gnus-check-news-server (method)
12467   "If the news server is down, start it up again."
12468   (let ((method (if method method gnus-select-method)))
12469     (and (stringp method)
12470          (setq method (gnus-server-to-method method)))
12471     (if (gnus-server-opened method)
12472         ;; Stream is already opened.
12473         t
12474       ;; Open server.
12475       (message "Opening server %s on %s..." (car method) (nth 1 method))
12476       (run-hooks 'gnus-open-server-hook)
12477       (or (gnus-server-opened method)
12478           (gnus-open-server method))
12479       (message ""))))
12480
12481 (defun gnus-nntp-message (&optional message)
12482   "Check the status of the NNTP server.
12483 If the status of the server is clear and MESSAGE is non-nil, MESSAGE
12484 is returned insted of the status string."
12485   (let ((status (gnus-status-message (gnus-find-method-for-group 
12486                                       gnus-newsgroup-name)))
12487         (message (or message "")))
12488     (if (and (stringp status) (> (length status) 0))
12489         status message)))
12490
12491 (defun gnus-get-function (method function)
12492   (and (stringp method)
12493        (setq method (gnus-server-to-method method)))
12494   (let ((func (intern (format "%s-%s" (car method) function))))
12495     (if (not (fboundp func)) 
12496         (progn
12497           (require (car method))
12498           (if (not (fboundp func)) 
12499               (error "No such function: %s" func))))
12500     func))
12501
12502 ;;; Interface functions to the backends.
12503
12504 (defun gnus-open-server (method)
12505   (funcall (gnus-get-function method 'open-server)
12506            (nth 1 method) (nthcdr 2 method)))
12507
12508 (defun gnus-close-server (method)
12509   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
12510
12511 (defun gnus-request-list (method)
12512   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
12513
12514 (defun gnus-request-list-newsgroups (method)
12515   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
12516
12517 (defun gnus-request-newgroups (date method)
12518   (funcall (gnus-get-function method 'request-newgroups) 
12519            date (nth 1 method)))
12520
12521 (defun gnus-server-opened (method)
12522   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
12523
12524 (defun gnus-status-message (method)
12525   (let ((method (if (stringp method) (gnus-find-method-for-group method)
12526                   method)))
12527     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
12528
12529 (defun gnus-request-group (group &optional dont-check)
12530   (let ((method (gnus-find-method-for-group group)))
12531     (funcall (gnus-get-function method 'request-group) 
12532              (gnus-group-real-name group) (nth 1 method) dont-check)))
12533
12534 (defun gnus-list-active-group (group)
12535   (let ((method (gnus-find-method-for-group group))
12536         (func 'list-active-group))
12537     (and (gnus-check-backend-function func group)
12538          (funcall (gnus-get-function method func) 
12539                   (gnus-group-real-name group) (nth 1 method)))))
12540
12541 (defun gnus-request-group-description (group)
12542   (let ((method (gnus-find-method-for-group group))
12543         (func 'request-group-description))
12544     (and (gnus-check-backend-function func group)
12545          (funcall (gnus-get-function method func) 
12546                   (gnus-group-real-name group) (nth 1 method)))))
12547
12548 (defun gnus-close-group (group)
12549   (let ((method (gnus-find-method-for-group group)))
12550     (funcall (gnus-get-function method 'close-group) 
12551              (gnus-group-real-name group) (nth 1 method))))
12552
12553 (defun gnus-retrieve-headers (articles group)
12554   (let ((method (gnus-find-method-for-group group)))
12555     (funcall (gnus-get-function method 'retrieve-headers) 
12556              articles (gnus-group-real-name group) (nth 1 method))))
12557
12558 (defun gnus-request-article (article group &optional buffer)
12559   (let ((method (gnus-find-method-for-group group)))
12560     (funcall (gnus-get-function method 'request-article) 
12561              article (gnus-group-real-name group) (nth 1 method) buffer)))
12562
12563 (defun gnus-request-head (article group)
12564   (let ((method (gnus-find-method-for-group group)))
12565     (funcall (gnus-get-function method 'request-head) 
12566              article (gnus-group-real-name group) (nth 1 method))))
12567
12568 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
12569 (defun gnus-request-post-buffer (post group subject header artbuf
12570                                       info follow-to respect-poster)
12571    (let* ((info (or info (and group (nth 2 (gnus-gethash 
12572                                             group gnus-newsrc-hashtb)))))
12573           (method
12574            (if (and gnus-post-method
12575                     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
12576                     (memq 'post (assoc
12577                                  (format "%s" (car (gnus-find-method-for-group
12578                                                     gnus-newsgroup-name)))
12579                                         gnus-valid-select-methods)))
12580                gnus-post-method
12581              (gnus-find-method-for-group gnus-newsgroup-name))))
12582      (let ((mail-self-blind nil)
12583            (mail-archive-file-name nil))
12584        (funcall (gnus-get-function method 'request-post-buffer) 
12585                 post group subject header artbuf info follow-to
12586                 respect-poster))))
12587
12588 (defun gnus-request-post (method &optional force)
12589   (and (stringp method)
12590        (setq method (gnus-server-to-method method)))
12591   (and (not force) gnus-post-method
12592        (memq 'post (assoc (format "%s" (car method))
12593                           gnus-valid-select-methods))
12594        (setq method gnus-post-method))
12595   (funcall (gnus-get-function method 'request-post) 
12596            (nth 1 method)))
12597
12598 (defun gnus-request-expire-articles (articles group &optional force)
12599   (let ((method (gnus-find-method-for-group group)))
12600     (funcall (gnus-get-function method 'request-expire-articles) 
12601              articles (gnus-group-real-name group) (nth 1 method)
12602              force)))
12603
12604 (defun gnus-request-move-article 
12605   (article group server accept-function &optional last)
12606   (let ((method (gnus-find-method-for-group group)))
12607     (funcall (gnus-get-function method 'request-move-article) 
12608              article (gnus-group-real-name group) 
12609              (nth 1 method) accept-function last)))
12610
12611 (defun gnus-request-accept-article (group &optional last)
12612   (let ((func (if (symbolp group) group
12613                 (car (gnus-find-method-for-group group)))))
12614     (funcall (intern (format "%s-request-accept-article" func))
12615              (if (stringp group) (gnus-group-real-name group) group)
12616              last)))
12617
12618 (defun gnus-request-replace-article (article group buffer)
12619   (let ((func (car (gnus-find-method-for-group group))))
12620     (funcall (intern (format "%s-request-replace-article" func))
12621              article (gnus-group-real-name group) buffer)))
12622
12623 (defun gnus-request-create-group (group)
12624   (let ((method (gnus-find-method-for-group group)))
12625     (funcall (gnus-get-function method 'request-create-group) 
12626              (gnus-group-real-name group) (nth 1 method))))
12627
12628 (defun gnus-find-method-for-group (group)
12629   (or gnus-override-method
12630       (and (not group)
12631            gnus-select-method)
12632       (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
12633             method)
12634         (if (or (not info)
12635                 (not (setq method (nth 4 info))))
12636             (setq method gnus-select-method)
12637           (setq method
12638                 (cond ((stringp method)
12639                        (gnus-server-to-method method))
12640                       ((stringp (car method))
12641                        (gnus-server-extend-method group method))
12642                       (t
12643                        method))))
12644         (let ((method-name (symbol-name (car method))))
12645           (if (and (memq 'address (assoc method-name 
12646                                          gnus-valid-select-methods))
12647                    (not (assq (intern (concat method-name "-address"))
12648                               method)))
12649               (setq method 
12650                     (append method 
12651                             (list (list (intern (concat 
12652                                                  method-name "-address"))
12653                                         (nth 1 method)))))))
12654         method)))
12655
12656
12657 (defun gnus-check-backend-function (func group)
12658   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
12659                  group)))
12660     (fboundp (intern (format "%s-%s" method func)))))
12661
12662 (defun gnus-methods-using (method)
12663   (let ((valids gnus-valid-select-methods)
12664         outs)
12665     (while valids
12666       (if (memq method (car valids)) 
12667           (setq outs (cons (car valids) outs)))
12668       (setq valids (cdr valids)))
12669     outs))
12670
12671 ;;; 
12672 ;;; Active & Newsrc File Handling
12673 ;;;
12674
12675 ;; Newsrc related functions.
12676 ;; Gnus internal format of gnus-newsrc-assoc:
12677 ;; (("alt.general" 3 (1 . 1))
12678 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
12679 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
12680 ;; The first item is the group name; the second is the subscription
12681 ;; level; the third is either a range of a list of ranges of read
12682 ;; articles, the optional fourth element is a list of marked articles,
12683 ;; the optional fifth element is the select method.
12684 ;;
12685 ;; Gnus internal format of gnus-newsrc-hashtb:
12686 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
12687 ;; This is the entry for "alt.misc". The first element is the number
12688 ;; of unread articles in "alt.misc". The cdr of this entry is the
12689 ;; element *before* "alt.misc" in gnus-newsrc-assoc, which makes is
12690 ;; trivial to remove or add new elements into gnus-newsrc-assoc
12691 ;; without scanning the entire list. So, to get the actual information
12692 ;; of "alt.misc", you'd say something like 
12693 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
12694 ;;
12695 ;; Gnus internal format of gnus-active-hashtb:
12696 ;; ((1 . 1))
12697 ;;  (5 . 10))
12698 ;;  (67 . 99)) ...)
12699 ;; The only element in each entry in this hash table is a range of
12700 ;; (possibly) available articles. (Articles in this range may have
12701 ;; been expired or cancelled.)
12702 ;;
12703 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
12704 ;; ("alt.misc" "alt.test" "alt.general" ...)
12705
12706 (defun gnus-setup-news (&optional rawfile level)
12707   "Setup news information.
12708 If RAWFILE is non-nil, the .newsrc file will also be read.
12709 If LEVEL is non-nil, the news will be set up at level LEVEL."
12710   (let ((init (not (and gnus-newsrc-assoc gnus-active-hashtb (not rawfile)))))
12711     ;; Clear some variables to re-initialize news information.
12712     (if init (setq gnus-newsrc-assoc nil gnus-active-hashtb nil))
12713     ;; Read the active file and create `gnus-active-hashtb'.
12714     ;; If `gnus-read-active-file' is nil, then we just create an empty
12715     ;; hash table. The partial filling out of the hash table will be
12716     ;; done in `gnus-get-unread-articles'.
12717     (if (and gnus-read-active-file 
12718              (not level)
12719              (gnus-server-opened gnus-select-method))
12720         (gnus-read-active-file)
12721       (setq gnus-active-hashtb (make-vector 4095 0)))
12722
12723     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
12724     (if init (gnus-read-newsrc-file rawfile))
12725     ;; Find the number of unread articles in each non-dead group.
12726     (gnus-get-unread-articles (or level 6))
12727     ;; Find new newsgroups and treat them.
12728     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level)
12729              (gnus-server-opened gnus-select-method))
12730         (gnus-find-new-newsgroups))
12731     (if (and init gnus-check-bogus-newsgroups 
12732              gnus-read-active-file (not level)
12733              (gnus-server-opened gnus-select-method))
12734         (gnus-check-bogus-newsgroups))))
12735
12736 (defun gnus-find-new-newsgroups ()
12737   "Search for new newsgroups and add them.
12738 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
12739 The `-n' option line from .newsrc is respected."
12740   (interactive)
12741   (or (gnus-check-first-time-used)
12742       (if (or (consp gnus-check-new-newsgroups)
12743               (eq gnus-check-new-newsgroups 'ask-server))
12744           (gnus-ask-server-for-new-groups)
12745         (let ((groups 0)
12746               group new-newsgroups)
12747           (or gnus-have-read-active-file (gnus-read-active-file))
12748           (setq gnus-newsrc-last-checked-date (current-time-string))
12749           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
12750           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
12751           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
12752           (mapatoms
12753            (lambda (sym)
12754              (setq group (symbol-name sym))
12755              (if (or (gnus-gethash group gnus-killed-hashtb)
12756                      (gnus-gethash group gnus-newsrc-hashtb))
12757                  ()
12758                (let ((do-sub (gnus-matches-options-n group)))
12759                  (cond ((eq do-sub 'subscribe)
12760                         (setq groups (1+ groups))
12761                         (gnus-sethash group group gnus-killed-hashtb)
12762                         (funcall 
12763                          gnus-subscribe-options-newsgroup-method group))
12764                        ((eq do-sub 'ignore)
12765                         nil)
12766                        (t
12767                         (setq groups (1+ groups))
12768                         (gnus-sethash group group gnus-killed-hashtb)
12769                         (if gnus-subscribe-hierarchical-interactive
12770                             (setq new-newsgroups (cons group new-newsgroups))
12771                           (funcall gnus-subscribe-newsgroup-method group)))))))
12772            gnus-active-hashtb)
12773           (if new-newsgroups 
12774               (gnus-subscribe-hierarchical-interactive new-newsgroups))
12775           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
12776           (if (> groups 0)
12777               (message "%d new newsgroup%s arrived." 
12778                        groups (if (> groups 1) "s have" " has")))))))
12779
12780 (defun gnus-matches-options-n (group)
12781   ;; Returns `subscribe' if the group is to be uncoditionally
12782   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
12783   ;; no match for the group.
12784
12785   ;; First we check the two user variables.
12786   (cond
12787    ((and gnus-options-not-subscribe
12788          (string-match gnus-options-not-subscribe group))
12789     'subscribe)
12790    ((and gnus-options-subscribe
12791          (string-match gnus-options-subscribe group))
12792     'ignore)
12793    ;; Then we go through the list that was retrieved from the .newsrc
12794    ;; file.  This list has elements on the form 
12795    ;; `(REGEXP . {ignore,subscribe})'. The first match found (the list
12796    ;; is in the reverse order of the options line) is returned.
12797    (t
12798     (let ((regs gnus-newsrc-options-n))
12799       (while (and regs
12800                   (not (string-match (car (car gnus-newsrc-options-n)) group)))
12801         (setq regs (cdr regs)))
12802       (and regs (cdr (car regs)))))))
12803
12804 (defun gnus-ask-server-for-new-groups ()
12805   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
12806          (methods (cons gnus-select-method 
12807                         (append
12808                          (and (consp gnus-check-new-newsgroups)
12809                               gnus-check-new-newsgroups)
12810                          gnus-secondary-select-methods)))
12811          (groups 0)
12812          (new-date (current-time-string))
12813          hashtb group new-newsgroups got-new)
12814     ;; Go thorugh both primary and secondary select methods and
12815     ;; request new newsgroups.  
12816     (while methods
12817       (if (gnus-request-newgroups date (car methods))
12818           (save-excursion
12819             (setq got-new t)
12820             (or hashtb (setq hashtb (gnus-make-hashtable 
12821                                      (count-lines (point-min) (point-max)))))
12822             (set-buffer nntp-server-buffer)
12823             ;; Enter all the new groups in a hashtable.
12824             (gnus-active-to-gnus-format (car methods) hashtb)))
12825       (setq methods (cdr methods)))
12826     (and got-new (setq gnus-newsrc-last-checked-date new-date))
12827     ;; Now all new groups from all select methods are in `hashtb'.
12828     (mapatoms
12829      (lambda (group-sym)
12830        (setq group (symbol-name group-sym))
12831        (if (or (gnus-gethash group gnus-newsrc-hashtb)
12832                (member group gnus-zombie-list)
12833                (member group gnus-killed-list))
12834            ;; The group is already known.
12835            ()
12836          (gnus-sethash group (symbol-value group-sym) gnus-active-hashtb)
12837          (let ((do-sub (gnus-matches-options-n group)))
12838            (cond ((eq do-sub 'subscribe)
12839                   (setq groups (1+ groups))
12840                   (gnus-sethash group group gnus-killed-hashtb)
12841                   (funcall 
12842                    gnus-subscribe-options-newsgroup-method group))
12843                  ((eq do-sub 'ignore)
12844                   nil)
12845                  (t
12846                   (setq groups (1+ groups))
12847                   (gnus-sethash group group gnus-killed-hashtb)
12848                   (if gnus-subscribe-hierarchical-interactive
12849                       (setq new-newsgroups (cons group new-newsgroups))
12850                     (funcall gnus-subscribe-newsgroup-method group)))))))
12851      hashtb)
12852     (if new-newsgroups 
12853         (gnus-subscribe-hierarchical-interactive new-newsgroups))
12854     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
12855     (if (> groups 0)
12856         (message "%d new newsgroup%s arrived." 
12857                  groups (if (> groups 1) "s have" " has")))
12858     got-new))
12859
12860 (defun gnus-check-first-time-used ()
12861   (if (or (> (length gnus-newsrc-assoc) 1)
12862           (file-exists-p gnus-startup-file)
12863           (file-exists-p (concat gnus-startup-file ".el"))
12864           (file-exists-p (concat gnus-startup-file ".eld")))
12865       nil
12866     (message "First time user; subscribing you to default groups")
12867     (or gnus-have-read-active-file (gnus-read-active-file))
12868     (setq gnus-newsrc-last-checked-date (current-time-string))
12869     (let ((groups gnus-default-subscribed-newsgroups)
12870           group)
12871       (if (eq groups t)
12872           nil
12873         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
12874         (mapatoms
12875          (lambda (sym)
12876            (setq group (symbol-name sym))
12877            (let ((do-sub (gnus-matches-options-n group)))
12878              (cond ((eq do-sub 'subscribe)
12879                     (gnus-sethash group group gnus-killed-hashtb)
12880                     (funcall 
12881                      gnus-subscribe-options-newsgroup-method group))
12882                    ((eq do-sub 'ignore)
12883                     nil)
12884                    (t
12885                     (setq gnus-killed-list (cons group gnus-killed-list))))))
12886          gnus-active-hashtb)
12887         (while groups
12888           (if (gnus-gethash (car groups) gnus-active-hashtb)
12889               (gnus-group-change-level (car groups) 3 9))
12890           (setq groups (cdr groups)))
12891         (gnus-group-make-help-group)
12892         (and gnus-novice-user
12893              (message (substitute-command-keys "\\<gnus-group-mode-map>\\[gnus-group-list-killed] to list killed groups")))))))
12894
12895 ;; `gnus-group-change-level' is the fundamental function for changing
12896 ;; subscription levels of newsgroups. This might mean just changing
12897 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
12898 ;; again, which subscribes/unsubscribes a group, which is equally
12899 ;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
12900 ;; from 8-9 to 1-7 means that you remove the group from the list of
12901 ;; killed (or zombie) groups and add them to the (kinda) subscribed
12902 ;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
12903 ;; which is trivial.
12904 ;; ENTRY can either be a string (newsgroup name) or a list (if
12905 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
12906 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
12907 ;; entries. 
12908 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
12909 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
12910 ;; after. 
12911 (defun gnus-group-change-level (entry level &optional oldlevel
12912                                       previous fromkilled)
12913   (let ((pinfo entry)
12914         group info active num)
12915     ;; Glean what info we can from the arguments
12916     (if (consp entry)
12917         (if fromkilled (setq group (nth 1 entry))
12918           (setq group (car (nth 2 entry))))
12919       (setq group entry))
12920     (if (and (stringp entry)
12921              oldlevel 
12922              (< oldlevel 8))
12923         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
12924     (if (and (not oldlevel)
12925              (listp entry))
12926         (setq oldlevel (car (cdr (nth 2 entry)))))
12927     (if (stringp previous)
12928         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
12929
12930     (gnus-dribble-enter
12931      (format "(gnus-group-change-level %S %S %S %S %S)" 
12932              group level oldlevel (car (nth 2 previous)) fromkilled))
12933     
12934     ;; Then we remove the newgroup from any old structures, if needed.
12935     ;; If the group was killed, we remove it from the killed or zombie
12936     ;; list. If not, and it is in fact going to be killed, we remove
12937     ;; it from the newsrc hash table and assoc.
12938     (cond ((>= oldlevel 8)
12939            (if (= oldlevel 8)
12940                (setq gnus-zombie-list (delete group gnus-zombie-list))
12941              (setq gnus-killed-list (delete group gnus-killed-list))))
12942           (t
12943            (if (>= level 8)
12944                (progn
12945                  (gnus-sethash (car (nth 2 entry))
12946                                nil gnus-newsrc-hashtb)
12947                  (if (nth 3 entry)
12948                      (setcdr (gnus-gethash (car (nth 3 entry))
12949                                            gnus-newsrc-hashtb)
12950                              (cdr entry)))
12951                  (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
12952
12953     ;; Finally we enter (if needed) the list where it is supposed to
12954     ;; go, and change the subscription level. If it is to be killed,
12955     ;; we enter it into the killed or zombie list.
12956     (cond ((>= level 8)
12957            (and (string= group (gnus-group-real-name group))
12958                 (if (= level 8)
12959                     (setq gnus-zombie-list (cons group gnus-zombie-list))
12960                   (setq gnus-killed-list (cons group gnus-killed-list)))))
12961           (t
12962            ;; If the list is to be entered into the newsrc assoc, and
12963            ;; it was killed, we have to create an entry in the newsrc
12964            ;; hashtb format and fix the pointers in the newsrc assoc.
12965            (if (>= oldlevel 8)
12966                (progn
12967                  (if (listp entry)
12968                      (progn
12969                        (setq info (cdr entry))
12970                        (setq num (car entry)))
12971                    (setq active (gnus-gethash group gnus-active-hashtb))
12972                    (setq num (if active (- (1+ (cdr active)) (car active)) t))
12973                    ;; Check whether the group is foreign. If so, the
12974                    ;; foreign select method has to be entered into the
12975                    ;; info. 
12976                    (let ((method (gnus-group-method-name group)))
12977                      (if (eq method gnus-select-method)
12978                          (setq info (list group level nil))
12979                        (setq info (list group level nil nil method)))))
12980                  (setq entry (cons info (if previous (cdr (cdr previous))
12981                                           (cdr gnus-newsrc-assoc))))
12982                  (setcdr (if previous (cdr previous) gnus-newsrc-assoc)
12983                          entry)
12984                  (gnus-sethash group (cons num (if previous (cdr previous)
12985                                                  gnus-newsrc-assoc))
12986                                gnus-newsrc-hashtb)
12987                  (if (cdr entry)
12988                      (setcdr (gnus-gethash (car (car (cdr entry)))
12989                                            gnus-newsrc-hashtb)
12990                              entry)))
12991              ;; It was alive, and it is going to stay alive, so we
12992              ;; just change the level and don't change any pointers or
12993              ;; hash table entries.
12994              (setcar (cdr (car (cdr (cdr entry)))) level))))))
12995
12996 (defun gnus-kill-newsgroup (newsgroup)
12997   "Obsolete function. Kills a newsgroup."
12998   (gnus-group-change-level (gnus-gethash newsgroup gnus-newsrc-hashtb) 9))
12999
13000 (defun gnus-check-bogus-newsgroups (&optional confirm)
13001   "Remove bogus newsgroups.
13002 If CONFIRM is non-nil, the user has to confirm the deletion of every
13003 newsgroup." 
13004   (let ((newsrc (cdr gnus-newsrc-assoc))
13005         bogus group)
13006     (message "Checking bogus newsgroups...")
13007     (or gnus-have-read-active-file (gnus-read-active-file))
13008     ;; Find all bogus newsgroup that are subscribed.
13009     (while newsrc
13010       (setq group (car (car newsrc)))
13011       (if (or (gnus-gethash group gnus-active-hashtb)
13012               (nth 4 (car newsrc))
13013               (and confirm
13014                    (not (gnus-y-or-n-p
13015                          (format "Remove bogus newsgroup: %s " group)))))
13016           ;; Active newsgroup.
13017           ()
13018         ;; Found a bogus newsgroup.
13019         (setq bogus (cons group bogus)))
13020       (setq newsrc (cdr newsrc)))
13021     ;; Remove all bogus subscribed groups by first killing them, and
13022     ;; then removing them from the list of killed groups.
13023     (while bogus
13024       (gnus-group-change-level 
13025        (gnus-gethash (car bogus) gnus-newsrc-hashtb) 9)
13026       (setq gnus-killed-list (delete (car bogus) gnus-killed-list))
13027       (setq bogus (cdr bogus)))
13028     ;; Then we remove all bogus groups from the list of killed and
13029     ;; zombie groups. They are are removed without confirmation.
13030     (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
13031           killed)
13032       (while dead-lists
13033         (setq killed (symbol-value (car dead-lists)))
13034         (while killed
13035           (setq group (car killed))
13036           (or (gnus-gethash group gnus-active-hashtb)
13037               ;; The group is bogus.
13038               (set (car dead-lists)
13039                    (delete group (symbol-value (car dead-lists)))))
13040           (setq killed (cdr killed)))
13041         (setq dead-lists (cdr dead-lists))))
13042     (message "Checking bogus newsgroups... done")))
13043
13044 (defun gnus-check-duplicate-killed-groups ()
13045   "Remove duplicates from the list of killed groups."
13046   (interactive)
13047   (let ((killed gnus-killed-list))
13048     (while killed
13049       (message "%d" (length killed))
13050       (setcdr killed (delete (car killed) (cdr killed)))
13051       (setq killed (cdr killed)))))
13052
13053 ;; Go though `gnus-newsrc-assoc' and compare with `gnus-active-hashtb'
13054 ;; and compute how many unread articles there are in each group.
13055 (defun gnus-get-unread-articles (&optional level)
13056   (let ((newsrc (cdr gnus-newsrc-assoc))
13057         (level (or level 6))
13058         info group active virtuals)
13059     (message "Checking new news...")
13060     (while newsrc
13061       (setq info (car newsrc))
13062       (setq group (car info))
13063       (setq active (gnus-gethash group gnus-active-hashtb))
13064
13065       ;; Check newsgroups. If the user doesn't want to check them, or
13066       ;; they can't be checked (for instance, if the news server can't
13067       ;; be reached) we just set the number of unread articles in this
13068       ;; newsgroup to t. This means that Gnus thinks that there are
13069       ;; unread articles, but it has no idea how many.
13070       (if (nth 4 info)
13071           (if (or (and gnus-activate-foreign-newsgroups 
13072                        (not (numberp gnus-activate-foreign-newsgroups)))
13073                   (and (numberp gnus-activate-foreign-newsgroups)
13074                        (<= (nth 1 info) gnus-activate-foreign-newsgroups)
13075                        (<= (nth 1 info) level)))
13076               (if (eq (car (nth 4 info)) 'nnvirtual)
13077                   (setq virtuals (cons info virtuals))
13078                 (setq active (gnus-activate-newsgroup (car info)))))
13079         (if (and (not gnus-read-active-file)
13080                  (<= (nth 1 info) level))
13081             (progn
13082               (setq active (gnus-activate-newsgroup (car info))))))
13083       
13084       (or active (progn (gnus-sethash group nil gnus-active-hashtb)
13085                         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
13086       (and active (gnus-get-unread-articles-in-group info active))
13087       (setq newsrc (cdr newsrc)))
13088
13089     ;; Activate the virtual groups. This has to be done after all the
13090     ;; other groups. 
13091     ;; !!! If one virtual group contains another virtual group, even
13092     ;; doing it this way might cause problems.
13093    (while virtuals
13094       (and (setq active (gnus-activate-newsgroup (car (car virtuals))))
13095            (gnus-get-unread-articles-in-group (car virtuals) active))
13096       (setq virtuals (cdr virtuals)))
13097
13098     (message "Checking new news... done")))
13099
13100 ;; Create a hash table out of the newsrc alist. The `car's of the
13101 ;; alist elements are used as keys.
13102 (defun gnus-make-hashtable-from-newsrc-alist ()
13103   (let ((alist gnus-newsrc-assoc)
13104          prev)
13105     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
13106     (setq alist 
13107           (setq prev (setq gnus-newsrc-assoc 
13108                            (cons (list "dummy.group" 0 nil) alist))))
13109     (while alist
13110       (gnus-sethash (car (car alist)) (cons nil prev) gnus-newsrc-hashtb)
13111       (setq prev alist)
13112       (setq alist (cdr alist)))))
13113
13114 (defun gnus-make-hashtable-from-killed ()
13115   "Create a hash table from the killed and zombie lists."
13116   (let ((lists '(gnus-killed-list gnus-zombie-list))
13117         list)
13118     (setq gnus-killed-hashtb 
13119           (gnus-make-hashtable 
13120            (+ (length gnus-killed-list) (length gnus-zombie-list))))
13121     (while lists
13122       (setq list (symbol-value (car lists)))
13123       (setq lists (cdr lists))
13124       (while list
13125         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
13126         (setq list (cdr list))))))
13127
13128 (defun gnus-get-unread-articles-in-group (info active)
13129   (let* ((range (nth 2 info))
13130          (num 0)
13131          (marked (nth 3 info))
13132          srange lowest group highest)
13133     ;; Modify the list of read articles according to what articles 
13134     ;; are available; then tally the unread articles and add the
13135     ;; number to the group hash table entry.
13136     (cond ((zerop (cdr active))
13137            (setq num 0))
13138           ((not range)
13139            (setq num (- (1+ (cdr active)) (car active))))
13140           ((not (listp (cdr range)))
13141            ;; Fix a single (num . num) range according to the
13142            ;; active hash table.
13143            ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
13144            (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
13145            (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
13146            ;; Compute number of unread articles.
13147            (setq num (max 0 (- (cdr active) 
13148                                (- (1+ (cdr range)) (car range))))))
13149           (t
13150            ;; The read list is a list of ranges. Fix them according to
13151            ;; the active hash table.
13152            ;; First peel off any elements that are below the lower
13153            ;; active limit. 
13154            (while (and (cdr range) 
13155                        (>= (car active) 
13156                            (or (and (atom (car (cdr range))) (car (cdr range)))
13157                                (car (car (cdr range))))))
13158              (if (numberp (car range))
13159                  (setcar range 
13160                          (cons (car range) 
13161                                (or (and (numberp (car (cdr range)))
13162                                         (car (cdr range))) 
13163                                    (cdr (car (cdr range))))))
13164                (setcdr (car range) 
13165                        (or (and (numberp (nth 1 range)) (nth 1 range))
13166                            (cdr (car (cdr range))))))
13167              (setcdr range (cdr (cdr range))))
13168            ;; Adjust the first element to be the same as the lower limit. 
13169            (if (and (not (atom (car range))) 
13170                     (< (cdr (car range)) (car active)))
13171                (setcdr (car range) (1- (car active))))
13172            ;; Then we want to peel off any elements that are higher
13173            ;; than the upper active limit.  
13174            (let ((srange range))
13175              ;; Go past all legal elements.
13176              (while (and (cdr srange) 
13177                          (<= (or (and (atom (car (cdr srange)))
13178                                       (car (cdr srange)))
13179                                  (car (car (cdr srange)))) (cdr active)))
13180                (setq srange (cdr srange)))
13181              (if (cdr srange)
13182                  ;; Nuke all remaining illegal elements.
13183                  (setcdr srange nil))
13184
13185              ;; Adjust the final element.
13186              (if (and (not (atom (car srange)))
13187                       (> (cdr (car srange)) (cdr active)))
13188                  (setcdr (car srange) (cdr active))))
13189            ;; Compute the number of unread articles.
13190            (while range
13191              (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
13192                                          (cdr (car range))))
13193                                  (or (and (atom (car range)) (car range))
13194                                      (car (car range))))))
13195              (setq range (cdr range)))
13196            (setq num (max 0 (- (cdr active) num)))))
13197     (and info
13198          (progn
13199            (and (assq 'tick marked)
13200                 (inline (gnus-remove-illegal-marked-articles
13201                          (assq 'tick marked) (nth 2 info))))
13202            (and (assq 'dormant marked)
13203                 (inline (gnus-remove-illegal-marked-articles
13204                          (assq 'dormant marked) (nth 2 info))))
13205            (setcar
13206             (gnus-gethash (car info) gnus-newsrc-hashtb) 
13207             (setq num (max 0 (- num (length (cdr (assq 'tick marked)))
13208                                 (length (cdr (assq 'dormant marked)))))))))
13209     num))
13210
13211 (defun gnus-remove-illegal-marked-articles (marked ranges)
13212   (let ((m (cdr marked)))
13213     ;; Make sure that all ticked articles are a subset of the unread
13214     ;; articles. 
13215     (while m
13216       (if (gnus-member-of-range (car m) ranges)
13217           (setcdr marked (cdr m))
13218         (setq marked m))
13219       (setq m (cdr m)))))
13220
13221 (defun gnus-activate-newsgroup (group)
13222   (let ((method (gnus-find-method-for-group group))
13223         active)
13224     (and (or (gnus-server-opened method) (gnus-open-server method))
13225          (gnus-request-group group)
13226          (save-excursion
13227            (set-buffer nntp-server-buffer)
13228            (goto-char 1)
13229            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
13230                 (progn
13231                   (goto-char (match-beginning 1))
13232                   (gnus-sethash 
13233                    group (setq active (cons (read (current-buffer))
13234                                             (read (current-buffer))))
13235                    gnus-active-hashtb))
13236                 active)))))
13237
13238 (defun gnus-update-read-articles 
13239   (group unread unselected ticked &optional domarks replied expirable killed
13240          dormant bookmark score)
13241   "Update the list of read and ticked articles in GROUP using the
13242 UNREAD and TICKED lists.
13243 Note: UNSELECTED has to be sorted over `<'.
13244 Returns whether the updating was successful."
13245   (let* ((active (or gnus-newsgroup-active 
13246                      (gnus-gethash group gnus-active-hashtb)))
13247          (entry (gnus-gethash group gnus-newsrc-hashtb))
13248          (number (car entry))
13249          (info (nth 2 entry))
13250          (marked (nth 3 info))
13251          (prev 1)
13252          (unread (sort (copy-sequence unread) (function <)))
13253          last read)
13254     (if (or (not info) (not active))
13255         ;; There is no info on this group if it was, in fact,
13256         ;; killed. Gnus stores no information on killed groups, so
13257         ;; there's nothing to be done. 
13258         ;; One could store the information somewhere temporarily,
13259         ;; perhaps... Hmmm... 
13260         ()
13261       ;; Remove any negative articles numbers.
13262       (while (and unread (< (car unread) 0))
13263         (setq unread (cdr unread)))
13264       (setq unread (sort (append unselected unread) '<))
13265       ;; Set the number of unread articles in gnus-newsrc-hashtb.
13266       (setcar entry (max 0 (- (length unread) (length ticked) 
13267                               (length dormant))))
13268       ;; Compute the ranges of read articles by looking at the list of
13269       ;; unread articles.  
13270       (while unread
13271         (if (/= (car unread) prev)
13272             (setq read (cons (if (= prev (1- (car unread))) prev
13273                                (cons prev (1- (car unread)))) read)))
13274         (setq prev (1+ (car unread)))
13275         (setq unread (cdr unread)))
13276       (if (<= prev (cdr active))
13277           (setq read (cons (cons prev (cdr active)) read)))
13278       ;; Enter this list into the group info.
13279       (setcar (cdr (cdr info)) 
13280               (if (> (length read) 1) (nreverse read) read))
13281       ;; Enter the list of ticked articles.
13282       (gnus-set-marked-articles 
13283        info ticked
13284        (if domarks replied (cdr (assq 'reply marked)))
13285        (if domarks expirable (cdr (assq 'expire marked)))
13286        (if domarks killed (cdr (assq 'killed marked)))
13287        (if domarks dormant (cdr (assq 'dormant marked)))
13288        (if domarks bookmark (cdr (assq 'bookmark marked)))
13289        (if domarks score (cdr (assq 'score marked))))
13290       t)))
13291
13292 (defun gnus-make-articles-unread (group articles)
13293   "Mark ARTICLES in GROUP as unread."
13294   (let ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
13295                          (gnus-gethash (gnus-group-real-name group)
13296                                        gnus-newsrc-hashtb)))))
13297     (setcar (nthcdr 2 info)
13298             (gnus-remove-from-range (nth 2 info) articles))
13299     (gnus-group-update-group group t)))
13300
13301 (defun gnus-read-active-file ()
13302   "Get active file from NNTP server."
13303   (gnus-group-set-mode-line)
13304   (let ((methods (cons gnus-select-method gnus-secondary-select-methods)))
13305     (setq gnus-have-read-active-file nil)
13306     (while methods
13307       (let* ((where (nth 1 (car methods)))
13308              (mesg (format "Reading active file%s via %s..."
13309                            (if (and where (not (zerop (length where))))
13310                                (concat " from " where) "")
13311                            (car (car methods)))))
13312         (message mesg)
13313         (gnus-check-news-server (car methods))
13314         (if (gnus-request-list (car methods)) ; Get active 
13315             (save-excursion
13316               (set-buffer nntp-server-buffer)
13317               (gnus-active-to-gnus-format 
13318                (and gnus-have-read-active-file (car methods)))
13319               (setq gnus-have-read-active-file t)
13320               (message "%s...done" mesg))
13321           (message "Cannot read active file from %s server." 
13322                    (car (car methods)))
13323           (ding)))
13324       (setq methods (cdr methods)))))
13325
13326 ;; rewritten by jwz based on ideas from Rick Sladkey <jrs@world.std.com>
13327 ;; Further rewrites by lmi.
13328 (defun gnus-active-to-gnus-format (method &optional hashtb)
13329   "Convert active file format to internal format.
13330 Lines matching `gnus-ignored-newsgroups' are ignored."
13331   (let ((cur (current-buffer))
13332         (hashtb (or hashtb 
13333                     (if method
13334                         gnus-active-hashtb
13335                       (setq gnus-active-hashtb
13336                             (gnus-make-hashtable 
13337                              (count-lines (point-min) (point-max))))))))
13338     ;; Delete unnecessary lines.
13339     (goto-char (point-min))
13340     (delete-matching-lines gnus-ignored-newsgroups)
13341     (and method (not (eq method gnus-select-method))
13342          (let ((prefix (gnus-group-prefixed-name "" method)))
13343            (goto-char (point-min))
13344            (while (and (not (eobp))
13345                        (null (insert prefix))
13346                        (zerop (forward-line 1))))))
13347     (goto-char (point-min))
13348     ;; Store active file in hashtable.
13349     (save-restriction
13350       (if (or (re-search-forward "\n.\r?$" nil t)
13351               (goto-char (point-max)))
13352           (progn
13353             (beginning-of-line)
13354             (narrow-to-region (point-min) (point))))
13355       (goto-char (point-min))
13356       (if (string-match "%[oO]" gnus-group-line-format)
13357           ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
13358           ;; If we want information on moderated groups, we use this
13359           ;; loop...   
13360           (condition-case ()
13361               (let ((mod-hashtb (make-vector 7 0))
13362                     group max mod)
13363                 (while (not (eobp))
13364                   (setq group (let ((obarray hashtb))
13365                                 (read cur)))
13366                   (setq max (read cur))
13367                   (set group (cons (read cur) max))
13368                   ;; Enter moderated groups into a list.
13369                   (if (string= 
13370                        (symbol-name  (let ((obarray mod-hashtb)) (read cur)))
13371                        "m")
13372                       (setq gnus-moderated-list 
13373                             (cons (symbol-name group) gnus-moderated-list)))
13374                   (forward-line 1)))
13375             (error 
13376              (progn (ding) (message "Possible error in active file."))))
13377         ;; And if we do not care about moderation, we use this loop,
13378         ;; which is faster.
13379         (condition-case ()
13380             (let (group max)
13381               (while (not (eobp))
13382                 ;; group gets set to a symbol interned in the hash table
13383                 ;; (what a hack!!)
13384                 (setq group (let ((obarray hashtb)) (read cur)))
13385                 (setq max (read cur))
13386                 (set group (cons (read cur) max))
13387                 (forward-line 1)))
13388           (error 
13389            (progn (ding) (message "Possible error in active file."))))))))
13390
13391 (defun gnus-read-newsrc-file (&optional force)
13392   "Read startup file.
13393 If FORCE is non-nil, the .newsrc file is read."
13394   (setq gnus-current-startup-file (gnus-make-newsrc-file gnus-startup-file))
13395   ;; Reset variables that might be defined in the .newsrc.eld file.
13396   (let ((variables gnus-variable-list))
13397     (while variables
13398       (set (car variables) nil)
13399       (setq variables (cdr variables))))
13400   (let* ((newsrc-file gnus-current-startup-file)
13401          (quick-file (concat newsrc-file ".el")))
13402     (save-excursion
13403       ;; We always load the .newsrc.eld file. If always contains
13404       ;; much information that can not be gotten from the .newsrc
13405       ;; file (ticked articles, killed groups, foreign methods, etc.)
13406       (gnus-read-newsrc-el-file quick-file)
13407  
13408       (if (or force
13409               (and (file-newer-than-file-p newsrc-file quick-file)
13410                    (file-newer-than-file-p newsrc-file 
13411                                            (concat quick-file "d")))
13412               (not gnus-newsrc-assoc))
13413           ;; We read the .newsrc file. Note that if there if a
13414           ;; .newsrc.eld file exists, it has already been read, and
13415           ;; the `gnus-newsrc-hashtb' has been created. While reading
13416           ;; the .newsrc file, Gnus will only use the information it
13417           ;; can find there for changing the data already read -
13418           ;; ie. reading the .newsrc file will not trash the data
13419           ;; already read (except for read articles).
13420           (save-excursion
13421             (message "Reading %s..." newsrc-file)
13422             (set-buffer (find-file-noselect newsrc-file))
13423             (buffer-disable-undo (current-buffer))
13424             (gnus-newsrc-to-gnus-format)
13425             (kill-buffer (current-buffer))
13426             (message "Reading %s... done" newsrc-file)))
13427       (and gnus-use-dribble-file (gnus-dribble-read-file)))))
13428
13429 (defun gnus-read-newsrc-el-file (file)
13430   (let ((ding-file (concat file "d")))
13431     ;; We always, always read the .eld file.
13432     (message "Reading %s..." ding-file)
13433     (condition-case nil
13434         (load ding-file t t t)
13435       (error nil))
13436     (gnus-uncompress-newsrc-assoc)
13437     (gnus-make-hashtable-from-newsrc-alist)
13438     (if (not (file-newer-than-file-p file ding-file))
13439         ()
13440       ;; Old format quick file
13441       (message "Reading %s..." file)
13442       ;; The .el file is newer than the .eld file, so we read that one
13443       ;; as well. 
13444       (gnus-read-old-newsrc-el-file file))))
13445
13446 ;; Parse the old-style quick startup file
13447 (defun gnus-read-old-newsrc-el-file (file)
13448   (let (newsrc killed marked group g m len info)
13449     (prog1
13450         (let (gnus-killed-assoc gnus-marked-assoc gnus-newsrc-assoc)
13451           (prog1
13452               (condition-case nil
13453                   (load file t t t)
13454                 (error nil))
13455             (setq newsrc gnus-newsrc-assoc
13456                   killed gnus-killed-assoc
13457                   marked gnus-marked-assoc)))
13458       (setq gnus-newsrc-assoc nil)
13459       (while newsrc
13460         (setq group (car newsrc))
13461         (let ((info (nth 2 (gnus-gethash (car group) gnus-newsrc-hashtb))))
13462           (if info
13463               (progn
13464                 (setcar (nthcdr 2 info) (cdr (cdr group)))
13465                 (setcar (cdr info) (if (nth 1 group) 3 6))
13466                 (setq gnus-newsrc-assoc (cons info gnus-newsrc-assoc)))
13467             (setq gnus-newsrc-assoc
13468                   (cons 
13469                    (setq info
13470                          (list (car group)
13471                                (if (nth 1 group) 3 6) (cdr (cdr group))))
13472                    gnus-newsrc-assoc)))
13473           (if (setq m (assoc (car group) marked))
13474             (setcdr (cdr (cdr info)) (cons (list (cons 'tick (cdr m))) nil))))
13475         (setq newsrc (cdr newsrc)))
13476       (setq newsrc killed)
13477       (while newsrc
13478         (setcar newsrc (car (car newsrc)))
13479         (setq newsrc (cdr newsrc)))
13480       (setq gnus-killed-list killed))
13481     (setq gnus-newsrc-assoc (nreverse gnus-newsrc-assoc))
13482     (gnus-make-hashtable-from-newsrc-alist)))
13483       
13484 (defun gnus-make-newsrc-file (file)
13485   "Make server dependent file name by catenating FILE and server host name."
13486   (let* ((file (expand-file-name file nil))
13487          (real-file (concat file "-" (nth 1 gnus-select-method))))
13488     (if (file-exists-p real-file)
13489         real-file file)))
13490
13491 (defun gnus-uncompress-newsrc-assoc ()
13492   ;; Uncompress all lists of marked articles in the newsrc assoc.
13493   (let ((newsrc gnus-newsrc-assoc)
13494         marked)
13495     (while newsrc
13496       (if (not (setq marked (nth 3 (car newsrc))))
13497           ()
13498         (while marked
13499           (or (eq 'score (car (car marked)))
13500               (eq 'bookmark (car (car marked)))
13501               (eq 'killed (car (car marked)))
13502               (setcdr (car marked) (gnus-uncompress-range (cdr (car marked)))))
13503           (setq marked (cdr marked))))
13504       (setq newsrc (cdr newsrc)))))
13505
13506 (defun gnus-compress-newsrc-assoc ()
13507   ;; Compress all lists of marked articles in the newsrc assoc.
13508   (let ((newsrc gnus-newsrc-assoc)
13509         marked)
13510     (while newsrc
13511       (if (not (setq marked (nth 3 (car newsrc))))
13512           ()
13513         (while marked
13514           (or (eq 'score (car (car marked)))
13515               (eq 'bookmark (car (car marked)))
13516               (eq 'killed (car (car marked)))
13517               (setcdr (car marked) 
13518                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
13519           (setq marked (cdr marked))))
13520       (setq newsrc (cdr newsrc)))))
13521
13522 ;; jwz: rewrote this function to be much more efficient, and not be
13523 ;; subject to regexp overflow errors when it encounters very long
13524 ;; lines -- the old behavior was to blow off the rest of the *file*
13525 ;; when a line was encountered that was too long to match!!  Now it
13526 ;; uses only simple looking-at calls, and doesn't create as many
13527 ;; temporary strings.  It also now handles multiple consecutive
13528 ;; options lines (before it only handled the first.)
13529 ;; Tiny rewrite by lmi. 
13530 (defun gnus-newsrc-to-gnus-format ()
13531   "Parse current buffer as .newsrc file."
13532   ;; We have to re-initialize these variables (except for
13533   ;; gnus-killed-list) because the quick startup file may contain bogus
13534   ;; values.
13535   (setq gnus-newsrc-options nil)
13536   (setq gnus-newsrc-options-n nil)
13537   (gnus-parse-options-lines)
13538   (gnus-parse-newsrc-body))
13539
13540 (defun gnus-parse-options-lines ()
13541   ;; newsrc.5 seems to indicate that the options line can come anywhere
13542   ;; in the file, and that there can be any number of them:
13543   ;;
13544   ;;       An  options  line  starts  with  the  word  options (left-
13545   ;;       justified).  Then there are the list of  options  just  as
13546   ;;       they would be on the readnews command line.  For instance:
13547   ;;
13548   ;;       options -n all !net.sf-lovers !mod.human-nets -r
13549   ;;       options -c -r
13550   ;;
13551   ;;       A string of lines beginning with a space or tab after  the
13552   ;;       initial  options  line  will  be  considered  continuation
13553   ;;       lines.
13554   ;;
13555   ;; For now, we only accept it at the beginning of the file.
13556
13557   (goto-char (point-min))
13558   (skip-chars-forward " \t\n")
13559   (setq gnus-newsrc-options nil)
13560   (while (looking-at "^options[ \t]*\\(.*\\)\n")
13561     ;; handle consecutive options lines
13562     (setq gnus-newsrc-options (concat gnus-newsrc-options
13563                                       (if gnus-newsrc-options "\n\t")
13564                                       (buffer-substring (match-beginning 1)
13565                                                         (match-end 1))))
13566     (forward-line 1)
13567     (while (looking-at "[ \t]+\\(.*\\)\n")
13568       ;; handle subsequent continuation lines of this options line
13569       (setq gnus-newsrc-options (concat gnus-newsrc-options "\n\t"
13570                                         (buffer-substring (match-beginning 1)
13571                                                           (match-end 1))))
13572       (forward-line 1)))
13573   ;; Gather all "-n" options lines.
13574   (let ((start 0)
13575         (result nil))
13576     (if gnus-newsrc-options
13577         (while (and (string-match "^[ \t]*-n\\([^\n]*\\)$"
13578                                   gnus-newsrc-options
13579                                   start)
13580                     (setq start (match-end 0)))
13581           (setq result (concat result
13582                                (and result " ")
13583                                (substring gnus-newsrc-options
13584                                           (match-beginning 1)
13585                                           (match-end 1))))))
13586
13587     (and result (gnus-parse-n-options result))
13588     nil))
13589
13590 (defun gnus-parse-newsrc-body ()
13591   ;; Point has been positioned after the options lines.  We shouldn't
13592   ;; see any more in here.
13593
13594   (let ((subscribe nil)
13595         (read-list nil)
13596         (line (1+ (count-lines (point-min) (point))))
13597         newsgroup
13598         p p2)
13599     (save-restriction
13600       (skip-chars-forward " \t")
13601       (while (not (eobp))
13602         (cond
13603          ((= (following-char) ?\n)
13604           ;; skip blank lines
13605           nil)
13606          (t
13607           (setq p (point))
13608           (skip-chars-forward "^:!\n")
13609           (if (= (following-char) ?\n)
13610               (error "line %d is unparsable in %s" line (buffer-name)))
13611           (setq p2 (point))
13612           (skip-chars-backward " \t")
13613
13614           ;; #### note: we could avoid consing a string here by binding obarray
13615           ;; and reading the newsgroup directly into the gnus-newsrc-hashtb,
13616           ;; then setq'ing newsgroup to symbol-name of that, like we do in
13617           ;; gnus-active-to-gnus-format.
13618           (setq newsgroup (buffer-substring p (point)))
13619           (goto-char p2)
13620
13621           (setq subscribe (= (following-char) ?:))
13622           (setq read-list nil)
13623
13624           (forward-char 1)              ; after : or !
13625           (skip-chars-forward " \t")
13626           (while (not (= (following-char) ?\n))
13627             (skip-chars-forward " \t")
13628             (or
13629              (and (cond
13630                    ((looking-at "\\([0-9]+\\)-\\([0-9]+\\)") ; a range
13631                     (setq read-list
13632                           (cons
13633                            (cons
13634                             (progn
13635                               ;; faster that buffer-substring/string-to-int
13636                               (narrow-to-region (point-min) (match-end 1))
13637                               (read (current-buffer)))
13638                             (progn
13639                               (narrow-to-region (point-min) (match-end 2))
13640                               (forward-char) ; skip over "-"
13641                               (prog1
13642                                   (read (current-buffer))
13643                                 (widen))))
13644                            read-list))
13645                     t)
13646                    ((looking-at "[0-9]+")
13647                     ;; faster that buffer-substring/string-to-int
13648                     (narrow-to-region (point-min) (match-end 0))
13649                     (setq p (read (current-buffer)))
13650                     (widen)
13651                     (setq read-list (cons (cons p p) read-list))
13652                     t)
13653                    (t
13654                     ;; bogus chars in ranges
13655                     nil))
13656                   (progn
13657                     (goto-char (match-end 0))
13658                     (skip-chars-forward " \t")
13659                     (cond ((= (following-char) ?,)
13660                            (forward-char 1)
13661                            t)
13662                           ((= (following-char) ?\n)
13663                            t)
13664                           (t
13665                            ;; bogus char after range
13666                            nil))))
13667              ;; if we get here, the parse failed
13668              (progn
13669                (end-of-line)            ; give up on this line
13670                (ding)
13671                (message "Ignoring bogus line %d for %s in %s"
13672                         line newsgroup (buffer-name))
13673                (sleep-for 1))))
13674           ;; We have already read .newsrc.eld, so we gently update the
13675           ;; data in the hash table with the information we have just
13676           ;; read. 
13677           (let ((info (nth 2 (gnus-gethash newsgroup gnus-newsrc-hashtb)))
13678                 level)
13679             (if info
13680                 (progn
13681                   (setcar (nthcdr 2 info) (nreverse read-list))
13682                   ;; We update the level very gently.  In fact, we
13683                   ;; only change it if there's been a status change
13684                   ;; from subscribed to unsubscribed, or vice versa.
13685                   (setq level (nth 1 info))
13686                   (cond ((and (<= level 5) (not subscribe))
13687                          (setq level (if read-list 6 7)))
13688                         ((and (> level 5) subscribe)
13689                          (setq level 3)))
13690                   (setcar (cdr info) level))
13691               (setq gnus-newsrc-assoc
13692                     (cons (list newsgroup (if subscribe 3 (if read-list 6 7))
13693                                 (nreverse read-list))
13694                           gnus-newsrc-assoc))))))
13695         (setq line (1+ line))
13696         (forward-line 1))))
13697   (setq gnus-newsrc-assoc (cdr gnus-newsrc-assoc))
13698   (gnus-make-hashtable-from-newsrc-alist)
13699   nil)
13700
13701 (defun gnus-parse-n-options (options)
13702   "Parse -n NEWSGROUPS options and return a cons of YES and NO regexps."
13703   (let (yes no yes-or-no out newsgroup)
13704     ;; Parse each newsgroup description such as "comp.all".  Commas
13705     ;; and white spaces can be a newsgroup separator.
13706     (while
13707         (string-match "^[ \t\n,]*\\(!?\\)\\([^- \t\n,][^ \t\n,]*\\)" options)
13708       (setq yes-or-no
13709             (substring options (match-beginning 1) (match-end 1)))
13710       (setq newsgroup
13711             (regexp-quote
13712              (substring options
13713                         (match-beginning 2) (match-end 2))))
13714       (setq options (substring options (match-end 2)))
13715       ;; Rewrite "all" to ".+" not ".*".  ".+" requires at least one
13716       ;; character.
13717       (while (string-match "\\(^\\|\\\\[.]\\)all\\(\\\\[.]\\|$\\)" newsgroup)
13718         (setq newsgroup
13719               (concat (substring newsgroup 0 (match-end 1))
13720                       ".+"
13721                       (substring newsgroup (match-beginning 2)))))
13722       ;; It is yes or no.
13723       (setq out (cons (cons (if (string= yes-or-no "!") 'ignore 'subscribe)
13724                             (concat "^" newsgroup "$")) 
13725                       out)))
13726     (setq gnus-newsrc-options-n out)))
13727
13728 (defun gnus-save-newsrc-file ()
13729   "Save .newsrc file."
13730   ;; Note: We cannot save .newsrc file if all newsgroups are removed
13731   ;; from the variable gnus-newsrc-assoc.
13732   (and (or gnus-newsrc-assoc gnus-killed-list)
13733        gnus-current-startup-file
13734        (let ((make-backup-files t)
13735              (version-control nil)
13736              (require-final-newline t)) ;Don't ask even if requested.
13737          ;; You can stop or change version control of backup file.
13738          ;; Suggested by jason@violet.berkeley.edu.
13739          (run-hooks 'gnus-save-newsrc-hook)
13740          (save-excursion
13741            (if (or (not gnus-dribble-buffer)
13742                    (not (buffer-name gnus-dribble-buffer))
13743                    (zerop (save-excursion
13744                             (set-buffer gnus-dribble-buffer)
13745                             (buffer-size))))
13746                (message "(No changes need to be saved)")
13747              (if gnus-save-newsrc-file
13748                  (progn
13749                    (message "Saving %s..." gnus-current-startup-file)
13750                    ;; Make backup file of master newsrc.
13751                    (gnus-gnus-to-newsrc-format)
13752                    (message "Saving %s... done" gnus-current-startup-file)))
13753              ;; Quickly loadable .newsrc.
13754              (set-buffer (get-buffer-create " *Gnus-newsrc*"))
13755              (gnus-add-current-to-buffer-list)
13756              (buffer-disable-undo (current-buffer))
13757              (erase-buffer)
13758              (message "Saving %s.eld..." gnus-current-startup-file)
13759              (gnus-gnus-to-quick-newsrc-format)
13760              (write-region 1 (point-max) 
13761                            (concat gnus-current-startup-file ".eld") 
13762                            nil 'nomesg)
13763              (kill-buffer (current-buffer))
13764              (message "Saving %s.eld... done" gnus-current-startup-file)
13765              (gnus-dribble-delete-file))))))
13766
13767 (defun gnus-gnus-to-quick-newsrc-format ()
13768   "Insert Gnus variables such as gnus-newsrc-assoc in lisp format."
13769   (insert ";; (ding) Gnus startup file.\n")
13770   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
13771   (insert ";; to read .newsrc.\n")
13772   (let ((variables gnus-variable-list)
13773         (gnus-newsrc-assoc (cdr gnus-newsrc-assoc))
13774         variable)
13775     ;; insert lisp expressions.
13776     (gnus-compress-newsrc-assoc)
13777     (while variables
13778       (setq variable (car variables))
13779       (and (boundp variable)
13780            (symbol-value variable)
13781            (or gnus-save-killed-list (not (eq variable 'gnus-killed-list)))
13782            (insert "(setq " (symbol-name variable) " '"
13783                    (prin1-to-string (symbol-value variable))
13784                    ")\n"))
13785       (setq variables (cdr variables)))
13786     (gnus-uncompress-newsrc-assoc)))
13787
13788
13789 (defun gnus-gnus-to-newsrc-format ()
13790   ;; Generate and save the .newsrc file.
13791   (let ((newsrc (cdr gnus-newsrc-assoc))
13792         info ranges range)
13793     (save-excursion
13794       (set-buffer (create-file-buffer gnus-startup-file))
13795       (buffer-disable-undo (current-buffer))
13796       (erase-buffer)
13797       ;; Write options.
13798       (if gnus-newsrc-options (insert "options " gnus-newsrc-options "\n"))
13799       ;; Write subscribed and unsubscribed.
13800       (while newsrc
13801         (setq info (car newsrc))
13802         (if (not (nth 4 info))          ;Don't write foreign groups to .newsrc.
13803             (progn
13804               (insert (car info) (if (>= (nth 1 info) 6) "!" ":"))
13805               (if (setq ranges (nth 2 info))
13806                   (progn
13807                     (insert " ")
13808                     (if (not (listp (cdr ranges)))
13809                         (if (= (car ranges) (cdr ranges))
13810                             (insert (int-to-string (car ranges)))
13811                           (insert (int-to-string (car ranges)) "-" 
13812                                   (int-to-string (cdr ranges))))
13813                       (while ranges
13814                         (setq range (car ranges)
13815                               ranges (cdr ranges))
13816                         (if (or (atom range) (= (car range) (cdr range)))
13817                             (insert (int-to-string 
13818                                      (or (and (atom range) range) 
13819                                          (car range))))
13820                           (insert (int-to-string (car range)) "-"
13821                                   (int-to-string (cdr range))))
13822                         (if ranges (insert ","))))))
13823               (insert "\n")))
13824         (setq newsrc (cdr newsrc)))
13825       (write-region 1 (point-max) gnus-current-startup-file nil 'nomesg)
13826       (kill-buffer (current-buffer)))))
13827
13828 (defun gnus-read-descriptions-file ()
13829   (message "Reading descriptions file...")
13830   (if (not (gnus-request-list-newsgroups gnus-select-method))
13831       (progn
13832         (message "Couldn't read newsgroups descriptions")
13833         nil)
13834     (let (group)
13835       (setq gnus-description-hashtb 
13836             (gnus-make-hashtable (length gnus-active-hashtb)))
13837       (save-excursion
13838         (save-restriction
13839           (set-buffer nntp-server-buffer)
13840           (goto-char (point-min))
13841           (delete-non-matching-lines "^[-\\._+A-Za-z0-9]+[ \t]")
13842           (goto-char (point-min))
13843           (if (or (search-forward "\n.\n" nil t)
13844                   (goto-char (point-max)))
13845               (progn
13846                 (beginning-of-line)
13847                 (narrow-to-region (point-min) (point))))
13848           (goto-char (point-min))
13849           (while (not (eobp))
13850             (setq group (let ((obarray gnus-description-hashtb))
13851                           (read (current-buffer))))
13852             (skip-chars-forward " \t")
13853             (set group (buffer-substring 
13854                         (point) (gnus-point-at-eol)))
13855             (forward-line 1))))
13856       (message "Reading descriptions file...done")
13857       t)))
13858
13859 (defun gnus-group-get-description (group)
13860   ;; Get the description of a group by sending XGTITLE to the server.
13861   (and (gnus-request-group-description group)
13862        (save-excursion
13863          (set-buffer nntp-server-buffer)
13864          (goto-char (point-min))
13865          (and (looking-at "[^ \t]+[ \t]+\\(.*\\)")
13866               (buffer-substring (match-beginning 1) (match-end 1))))))
13867
13868 ;;;
13869 ;;; Server
13870 ;;;
13871
13872 (defvar gnus-server-mode-hook nil
13873   "Hook run in `gnus-server-mode' buffers.")
13874
13875 (defconst gnus-server-line-format "     {%(%h:%w%)}\n"
13876   "Format of server lines.
13877 It works along the same lines as a normal formatting string,
13878 with some simple extensions.")
13879
13880 (defvar gnus-server-mode-line-format "(ding) List of servers"
13881   "The format specification for the server mode line.")
13882
13883 (defconst gnus-server-line-format-alist
13884   (list (list ?h 'how ?s)
13885         (list ?n 'name ?s)
13886         (list ?w 'where ?s)
13887         ))
13888
13889 (defconst gnus-server-mode-line-format-alist 
13890   (list (list ?S 'news-server ?s)
13891         (list ?M 'news-method ?s)
13892         (list ?u 'user-defined ?s)))
13893
13894 (defvar gnus-server-line-format-spec nil)
13895 (defvar gnus-server-mode-line-format-spec nil)
13896 (defvar gnus-server-killed-servers nil)
13897
13898 (defvar gnus-server-mode-map nil)
13899 (put 'gnus-server-mode 'mode-class 'special)
13900
13901 (if gnus-server-mode-map
13902     nil
13903   (setq gnus-server-mode-map (make-sparse-keymap))
13904   (suppress-keymap gnus-server-mode-map)
13905   (define-key gnus-server-mode-map " " 'gnus-server-read-server)
13906   (define-key gnus-server-mode-map "q" 'gnus-server-exit)
13907   (define-key gnus-server-mode-map "l" 'gnus-server-list-servers)
13908   (define-key gnus-server-mode-map "k" 'gnus-server-kill-server)
13909   (define-key gnus-server-mode-map "y" 'gnus-server-yank-server)
13910   (define-key gnus-server-mode-map "c" 'gnus-server-copy-server)
13911   (define-key gnus-server-mode-map "a" 'gnus-server-add-server)
13912   (define-key gnus-server-mode-map "e" 'gnus-server-edit-server))
13913
13914 (defun gnus-server-mode ()
13915   "Major mode for listing and editing servers.
13916
13917 All normal editing commands are switched off.
13918 \\<gnus-server-mode-map>
13919
13920 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
13921
13922 The following commands are available:
13923
13924 \\{gnus-server-mode-map}"
13925   (interactive)
13926 ;  (if gnus-visual (gnus-server-make-menu-bar))
13927   (kill-all-local-variables)
13928   (setq mode-line-modified "-- ")
13929   (make-local-variable 'mode-line-format)
13930   (setq mode-line-format (copy-sequence mode-line-format))
13931   (and (equal (nth 3 mode-line-format) "   ")
13932        (setcar (nthcdr 3 mode-line-format) ""))
13933   (setq major-mode 'gnus-server-mode)
13934   (setq mode-name "Server")
13935 ;  (gnus-group-set-mode-line)
13936   (setq mode-line-process nil)
13937   (use-local-map gnus-server-mode-map)
13938   (buffer-disable-undo (current-buffer))
13939   (setq truncate-lines t)
13940   (setq buffer-read-only t)
13941   (run-hooks 'gnus-server-mode-hook))
13942
13943 (defun gnus-server-insert-server-line (sformat name method)
13944   (let* ((sformat (or sformat gnus-server-line-format-spec))
13945          (how (car method))
13946          (where (nth 1 method))
13947          b)
13948     (beginning-of-line)
13949     (setq b (point))
13950     ;; Insert the text.
13951     (insert (eval sformat))
13952     (add-text-properties 
13953      b (1+ b) (list 'gnus-server (intern name)))))
13954
13955 (defun gnus-server-setup-buffer ()
13956   (if (get-buffer gnus-server-buffer)
13957       ()
13958     (save-excursion
13959       (set-buffer (get-buffer-create gnus-server-buffer))
13960       (gnus-server-mode))))
13961
13962 (defun gnus-server-prepare ()
13963   (setq gnus-server-mode-line-format-spec 
13964         (gnus-parse-format gnus-server-mode-line-format 
13965                            gnus-server-mode-line-format-alist))
13966   (setq gnus-server-line-format-spec 
13967         (gnus-parse-format gnus-server-line-format 
13968                            gnus-server-line-format-alist))
13969   (let ((alist gnus-server-alist)
13970         (buffer-read-only nil))
13971     (erase-buffer)
13972     (while alist
13973       (gnus-server-insert-server-line nil (car (car alist)) (cdr (car alist)))
13974       (setq alist (cdr alist))))
13975   (goto-char (point-min))
13976   (gnus-server-position-cursor))
13977
13978 (defun gnus-server-server-name ()
13979   (let ((server (get-text-property 
13980                  (save-excursion (beginning-of-line) (point)) 'gnus-server)))
13981     (and server (symbol-name server))))
13982
13983 (defalias 'gnus-server-position-cursor 'gnus-goto-colon)
13984
13985 (defvar gnus-winconf-edit-server nil)
13986 (defconst gnus-server-edit-buffer "*Gnus edit server*")
13987
13988 (defun gnus-server-update-server (server)
13989   (save-excursion
13990     (set-buffer gnus-server-buffer)
13991     (let ((buffer-read-only nil)
13992           (info (cdr (assoc server gnus-server-alist))))
13993       (gnus-dribble-enter 
13994        (concat "(gnus-server-set-info \"" server "\" '"
13995                (prin1-to-string info) ")"))
13996       ;; Buffer may be narrowed.
13997       (save-restriction
13998         (widen)
13999         (if (gnus-server-goto-server server)
14000             (delete-region (progn (beginning-of-line) (point))
14001                            (progn (forward-line 1) (point))))
14002         (let ((entry (assoc server gnus-server-alist)))
14003           (gnus-server-insert-server-line nil (car entry) (cdr entry))
14004           (gnus-server-position-cursor))))))
14005
14006 (defun gnus-server-set-info (info)
14007   (gnus-dribble-enter 
14008    (concat "(gnus-server-set-info '"
14009            (prin1-to-string info) ")"))
14010   (let* ((server (nth 1 info))
14011          (entry (assoc server gnus-server-alist)))
14012     (if entry (setcdr entry info)
14013       (setq gnus-server-alist
14014             (nconc gnus-server-alist (list (cons server info)))))))
14015
14016 (defun gnus-server-to-method (server)
14017   (cdr (assoc server gnus-server-alist)))
14018
14019 (defun gnus-server-extend-method (group method)
14020   (let ((entry
14021          (gnus-copy-sequence (cdr (assoc (car method) gnus-server-alist)))))
14022     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
14023     (nconc entry (cdr method))))
14024
14025 (defun gnus-server-get-method (group method)
14026   (cond ((stringp method)
14027          (gnus-server-to-method method))
14028         ((stringp (car method))
14029          (gnus-server-extend-method group method))
14030         (t
14031          method)))
14032
14033 ;;; Interactive server functions.
14034
14035 (defun gnus-server-kill-server (server)
14036   "Kill the server on the current line."
14037   (interactive (list (gnus-server-server-name)))
14038   (or (gnus-server-goto-server server)
14039       (if server (error "No such server: %s" server)
14040         (error "No server on the current line")))
14041   (let ((buffer-read-only nil))
14042     (delete-region (progn (beginning-of-line) (point))
14043                    (progn (forward-line 1) (point))))
14044   (setq gnus-server-killed-servers 
14045         (cons (assoc server gnus-server-alist) gnus-server-killed-servers))
14046   (setq gnus-server-alist (delq (car gnus-server-killed-servers)
14047                                 gnus-server-alist))
14048   (gnus-server-position-cursor))
14049
14050 (defun gnus-server-yank-server ()
14051   "Yank the previously killed server."
14052   (interactive)
14053   (or gnus-server-killed-servers
14054       (error "No killed servers to be yanked"))
14055   (let ((alist gnus-server-alist)
14056         (server (gnus-server-server-name))
14057         (killed (car gnus-server-killed-servers)))
14058     (if (not server) 
14059         (setq gnus-server-alist (nconc gnus-server-alist (list killed)))
14060       (if (string= server (car (car gnus-server-alist)))
14061           (setq gnus-server-alist (cons killed gnus-server-alist))
14062         (while (and (cdr alist)
14063                     (not (string= server (car (car (cdr alist))))))
14064           (setq alist (cdr alist)))
14065         (setcdr alist (cons killed (cdr alist)))))
14066     (gnus-server-update-server (car killed))
14067     (setq gnus-server-killed-servers (cdr gnus-server-killed-servers))
14068     (gnus-server-position-cursor)))
14069
14070 (defun gnus-server-exit ()
14071   "Return to the group buffer."
14072   (interactive)
14073   (kill-buffer (current-buffer))
14074   (switch-to-buffer gnus-group-buffer))
14075
14076 (defun gnus-server-list-servers ()
14077   "List all available servers."
14078   (interactive)
14079   (let ((cur (gnus-server-server-name)))
14080     (gnus-server-prepare)
14081     (if cur (gnus-server-goto-server cur)
14082       (goto-char (point-max))
14083       (forward-line -1))
14084     (gnus-server-position-cursor)))
14085
14086 (defun gnus-server-copy-server (from to)
14087   (interactive
14088    (list
14089     (or (gnus-server-server-name)
14090         (error "No server on the current line"))
14091     (read-string "Copy to: ")))
14092   (or from (error "No server on current line"))
14093   (or (and to (not (string= to ""))) (error "No name to copy to"))
14094   (and (assoc to gnus-server-alist) (error "%s already exists" to))
14095   (or (assoc from gnus-server-alist) 
14096       (error "%s: no such server" from))
14097   (let ((to-entry (gnus-copy-sequence (assoc from gnus-server-alist))))
14098     (setcar to-entry to)
14099     (setcar (nthcdr 2 to-entry) to)
14100     (setq gnus-server-killed-servers 
14101           (cons to-entry gnus-server-killed-servers))
14102     (gnus-server-yank-server)))
14103
14104 (defun gnus-server-add-server (how where)
14105   (interactive 
14106    (list (intern (completing-read "Server method: "
14107                                   gnus-valid-select-methods nil t))
14108          (read-string "Server name: ")))
14109   (setq gnus-server-killed-servers 
14110         (cons (list where how where) gnus-server-killed-servers))
14111   (gnus-server-yank-server))
14112
14113 (defun gnus-server-goto-server (server)
14114   "Jump to a server line."
14115   (interactive
14116    (list (completing-read "Goto server: " gnus-server-alist nil t)))
14117   (let ((to (text-property-any (point-min) (point-max) 
14118                                'gnus-server (intern server))))
14119     (and to
14120          (progn
14121            (goto-char to) 
14122            (gnus-server-position-cursor)))))
14123
14124 (defun gnus-server-edit-server (server)
14125   "Edit the server on the current line."
14126   (interactive (list (gnus-server-server-name)))
14127   (or server
14128       (error "No server on current line"))
14129   (setq gnus-winconf-edit-server (current-window-configuration))
14130   (pop-to-buffer (get-buffer-create gnus-server-edit-buffer))
14131   (gnus-add-current-to-buffer-list)
14132   (emacs-lisp-mode)
14133   (use-local-map (copy-keymap (current-local-map)))
14134   (local-set-key "\C-c\C-c" 'gnus-server-edit-server-done)
14135   (erase-buffer)
14136   (insert ";; Type `C-c C-c' after you have edited the server.\n\n")
14137   (insert "(gnus-server-set-info \n  "
14138           (pp-to-string (list 'quote (cdr (assoc server gnus-server-alist))))
14139           "  )\n"))
14140
14141 (defun gnus-server-edit-server-done ()
14142   (interactive)
14143   (set-buffer (get-buffer-create gnus-server-edit-buffer))
14144   (eval-current-buffer)
14145   (kill-buffer (current-buffer))
14146   (and gnus-winconf-edit-server
14147        (set-window-configuration gnus-winconf-edit-server))
14148   (setq gnus-winconf-edit-server nil)
14149   (set-buffer gnus-server-buffer)
14150   (gnus-server-update-server (gnus-server-server-name))
14151   (gnus-server-position-cursor))
14152
14153
14154 (provide 'gnus)
14155
14156 ;;; gnus.el ends here