*** empty log message ***
[gnus] / lisp / gnus.el
1 ;;; (ding) Gnus --- a newsreader for GNU Emacs
2 ;; Copyright (C) 1987,88,89,90,93,94,95 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;; Although (ding) Gnus looks suspiciously like GNUS, it isn't quite
27 ;; the same beast. Most internal structures have been changed. If you
28 ;; have written packages that depend on any of the hash tables,
29 ;; `gnus-newsrc-assoc', `gnus-killed-assoc', marked lists, the .newsrc
30 ;; buffer, or internal knowledge of the `nntp-header-' macros, or
31 ;; dependence on the buffers having a certain format, your code will
32 ;; fail.
33
34 ;;; Code:
35
36 (require 'mail-utils)
37 (require 'timezone)
38 (require 'rnews)
39 (require 'rmail)
40
41 (require 'nnheader)
42
43 ;; Customization variables
44
45 (defvar gnus-select-method 
46   (list 'nntp (or (getenv "NNTPSERVER") 
47                   (if (and gnus-default-nntp-server
48                            (not (string= gnus-default-nntp-server "")))
49                       gnus-default-nntp-server)
50                   (system-name))
51         "nntp")
52   "Default method for selecting a newsgroup.
53 This variable should be a list, where the first element is how the
54 news is to be fetched, the second is the address.  An optional third
55 element can be included to specify a port number if nntp is used.
56
57 For instance, if you want to get your news via NNTP from
58 \"flab.flab.edu\" on port 23, you could say:
59
60 (setq gnus-select-method '(nntp \"flab.flab.edu\" 23))
61
62 If you want to use your local spool, say:
63
64 (setq gnus-select-method (list 'nnspool (system-name)))
65
66 If you use this variable, you must set `gnus-nntp-server' to nil.")
67
68 ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
69 (defvar gnus-post-method nil
70   "Preferred method for posting USENET news.
71 If this variable is nil, Gnus will use the current method to decide
72 which method to use when posting.  If it is non-nil, it will override
73 the current method. This method will not be used in mail groups and
74 the like, only in \"real\" newsgroups.
75
76 The value must be a valid method as discussed in the documentation of
77 `gnus-select-method'.")
78
79 (defvar gnus-refer-article-method nil
80   "Preferred method for fetching an article by Message-ID.
81 If you are reading news from the local spool (with nnspool), fetching
82 articles by Message-ID is painfully slow. By setting this method to an
83 nntp method, you might get acceptable results.
84
85 The value of this variable must be a valid select method as discussed
86 in the documentation of `gnus-select-method'")
87
88 (defvar gnus-secondary-select-methods nil
89   "A list of secondary methods that will be used for reading news.
90 This is a list where each element is a complete select methdod (see
91 `gnus-select-method').  
92
93 If, for instance, you want to read your mail with the nnml backend,
94 you could set this variable:
95
96 (setq gnus-secondary-select-methods '((nnml \"\"))")
97
98 (defvar gnus-secondary-servers nil
99   "List of NNTP servers that the user can choose between interactively.
100 To make Gnus query you for a server, you have to give `gnus' a
101 non-numeric prefix - `C-u M-x gnus', in short.")
102
103 (defvar gnus-nntp-server nil
104   "The name of the host running the NNTP server.
105 This variable is semi-obsolete. Use the `gnus-select-method'
106 variable instead.")
107
108 (defvar gnus-nntp-service "nntp"
109   "NNTP service name (\"nntp\" or 119).
110 This is an obsolete variable, which is scarcely used. If you use an
111 nntp server for your newsgroup and want to change the port number
112 used to 899, you would say something along these lines:
113
114  (setq gnus-select-method '(nntp \"my.nntp.server\" 899))")
115
116 (defvar gnus-startup-file "~/.newsrc"
117   "Your `.newsrc' file.
118 `.newsrc-SERVER' will be used instead if that exists.")
119
120 (defvar gnus-signature-file "~/.signature"
121   "Your signature file.
122 If the variable is a string that doesn't correspond to a file, the
123 string itself is inserted.")
124
125 (defvar gnus-signature-function nil
126   "A function that should return a signature file name.
127 The function will be called with the name of the newsgroup being
128 posted to.
129 If the function returns a string that doesn't correspond to a file, the
130 string itself is inserted.
131 If the function returns nil, the `gnus-signature-file' variable will
132 be used instead.")
133
134 (defvar gnus-init-file "~/.gnus"
135   "Your Gnus elisp startup file.
136 If a file with the .el or .elc suffixes exist, it will be read
137 instead.") 
138
139 (defvar gnus-group-faq-directory
140   "/anonymous@rtfm.mit.edu:/pub/usenet-by-group/"
141   "Directory where the group FAQs are stored.
142 This will most commonly be on a remote machine, and the file will be
143 fetched by ange-ftp.")
144
145 (defvar gnus-group-archive-directory
146   "/ftp@sina.tcamc.uh.edu:/pub/emacs/ding-list/" 
147   "The address of the (ding) archives.")
148
149 (defvar gnus-default-subscribed-newsgroups nil
150   "This variable lists what newsgroups should be susbcribed the first time Gnus is used.
151 It should be a list of strings.
152 If it is `t', Gnus will not do anything special the first time it is
153 started; it'll just use the normal newsgroups subscription methods.")
154
155 (defvar gnus-post-prepare-function nil
156   "Function that is run after a post buffer has been prepared.
157 It is called with the name of the newsgroup that is posted to. It
158 might be used, for instance, for inserting signatures based on the
159 newsgroup name. (In that case, `gnus-signature-file' and
160 `mail-signature' should both be set to nil).")
161
162 (defvar gnus-use-cross-reference t
163   "Non-nil means that cross referenced articles will be marked as read.
164 If nil, ignore cross references.  If t, mark articles as read in
165 subscribed newsgroups. If neither t nor nil, mark as read in all
166 newsgroups.") 
167
168 (defvar gnus-use-dribble-file t
169   "Non-nil means that Gnus will use a dribble file to store user updates.
170 If Emacs should crash without saving the .newsrc files, complete
171 information can be restored from the dribble file.")
172
173 (defvar gnus-use-followup-to 'use
174   "Specifies what to do with Followup-To header.
175 If nil, ignore the header. If it is t, use its value, but ignore 
176 `poster'. If it is neither nil nor t, which is the default, always use
177 the value.") 
178
179 (defvar gnus-followup-to-function nil
180   "A variable that contains a function that returns a followup address.
181 The function will be called in the buffer of the article that is being
182 followed up. The buffer will be narrowed to the headers of the
183 article. To pick header headers, one might use `mail-fetch-field'.  The
184 function will be called with the name of the current newsgroup as the
185 argument.
186
187 Here's an example `gnus-followup-to-function':
188
189 (setq gnus-followup-to-function
190       (lambda (group)
191         (cond ((string= group \"mail.list\")
192                (or (mail-fetch-field \"sender\") 
193                    (mail-fetch-field \"from\")))
194               (t
195                (or (mail-fetch-field \"reply-to\") 
196                    (mail-fetch-field \"from\"))))))")
197
198 (defvar gnus-reply-to-function nil
199   "A variable that contains a function that returns a reply address.
200 See the `gnus-followup-to-function' variable for an explanation of how
201 this variable is used.
202
203 This function should return a string that will be used to fill in the
204 header.  This function may also return a list.  In that case, every
205 list element should be a cons where the first car should be a string
206 with the header name, and the cdr should be a string with the header
207 value.")
208
209 (defvar gnus-large-newsgroup 200
210   "The number of articles which indicates a large newsgroup.
211 If the number of articles in a newsgroup is greater than this value,
212 confirmation is required for selecting the newsgroup.")
213
214 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
215 (defvar gnus-no-groups-message "No news is horrible news"
216   "Message displayed by Gnus when no groups are available.")
217
218 (defvar gnus-author-copy (getenv "AUTHORCOPY")
219   "Save outgoing articles in this file.
220 Initialized from the AUTHORCOPY environment variable.
221
222 If this variable begins with the character \"|\", outgoing articles
223 will be piped to the named program. It is possible to save an article
224 in an MH folder as follows:
225
226 \(setq gnus-author-copy \"|/usr/local/lib/mh/rcvstore +Article\")
227
228 If the first character is not a pipe, articles are saved using the
229 function specified by the `gnus-author-copy-saver' variable.")
230
231 (defvar gnus-mail-self-blind nil
232   "Non-nil means insert a BCC header in all outgoing articles.
233 This will result in having a copy of the article mailed to yourself.
234 The BCC header is inserted when the post buffer is initialized, so you
235 can remove or alter the BCC header to override the default.")
236
237 (defvar gnus-author-copy-saver (function rmail-output)
238   "A function called to save outgoing articles.
239 This function will be called with the same of the file to store the
240 article in. The default function is `rmail-output' which saves in Unix
241 mailbox format.")
242
243 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
244   "Non-nil means that the default name of a file to save articles in is the group name.
245 If it's nil, the directory form of the group name is used instead.
246
247 If this variable is a list, and the list contains the element
248 `not-score', long file names will not be used for score files, if it
249 contains the element `not-save', long file names will not be used for
250 saving, and if it contains the element `not-kill', long file names
251 will not be used for kill files.")
252
253 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
254   "Name of the directory articles will be saved in (default \"~/News\").
255 Initialized from the SAVEDIR environment variable.")
256
257 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
258   "Name of the directory where kill files will be stored (default \"~/News\").
259 Initialized from the SAVEDIR environment variable.")
260
261 (defvar gnus-score-expiry-days 7
262   "*Number of days before unused score file entries are expired.")
263
264 (defvar gnus-default-article-saver (function gnus-summary-save-in-rmail)
265   "A function to save articles in your favorite format.
266 The function must be interactively callable (in other words, it must
267 be an Emacs command).
268
269 Gnus provides the following functions:
270
271 * gnus-summary-save-in-rmail (Rmail format)
272 * gnus-summary-save-in-mail (Unix mail format)
273 * gnus-summary-save-in-folder (MH folder)
274 * gnus-summary-save-in-file (article format).")
275
276 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
277   "A function generating a file name to save articles in Rmail format.
278 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
279
280 (defvar gnus-mail-save-name (function gnus-plain-save-name)
281   "A function generating a file name to save articles in Unix mail format.
282 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
283
284 (defvar gnus-folder-save-name (function gnus-folder-save-name)
285   "A function generating a file name to save articles in MH folder.
286 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
287
288 (defvar gnus-file-save-name (function gnus-numeric-save-name)
289   "A function generating a file name to save articles in article format.
290 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
291
292 (defvar gnus-score-file-suffix "SCORE"
293   "Suffix of the score files.")
294
295 (defvar gnus-fetch-old-headers nil
296   "Non-nil means that Gnus will try to build threads by grabbing old headers.
297 If an unread article in the group refers to an older, already read (or
298 just marked as read) article, the old article will not normally be
299 displayed in the Summary buffer.  If this variable is non-nil, Gnus
300 will attempt to grab the headers to the old articles, and thereby
301 build complete threads.  If it has the value `some', only enough
302 headers to connect otherwise loose threads will be displayed.
303
304 The server has to support XOVER for any of this to work.")
305
306 (defvar gnus-visual t
307   "*If non-nil, will do various highlighting.
308 If nil, no mouse highlights (or any other highlights) will be
309 performed.  This might speed up Gnus some when generating large group
310 and summary buffers.")
311
312 (defvar gnus-novice-user t
313   "*Non-nil means that you are a usenet novice.
314 If non-nil, verbose messages may be displayed and confirmations may be
315 required.")
316
317 (defvar gnus-expert-user nil
318   "*Non-nil means that you will never be asked for confirmation about anything.
319 And that means *anything*.")
320
321 (defvar gnus-keep-same-level nil
322   "Non-nil means that the next newsgroup after the current will be on the same level.
323 When you type, for instance, `n' after reading the last article in the
324 current newsgroup, you will go to the next newsgroup. If this variable
325 is nil, the next newsgroup will be the next from the group
326 buffer. 
327 If this variable is non-nil, Gnus will either put you in the
328 next newsgroup with the same level, or, if no such newsgroup is
329 available, the next newsgroup with the lowest possible level higher
330 than the current level.
331 If this variable is `best', Gnus will make the next newsgroup the one
332 with the best level.")
333
334 (defvar gnus-summary-make-false-root 'adopt
335   "nil means that Gnus won't gather loose threads.
336 If the root of a thread has expired or been read in a previous
337 session, the information necessary to build a complete thread has been
338 lost. Instead of having many small sub-threads from this original thread
339 scattered all over the summary buffer, Gnus can gather them. 
340
341 If non-nil, Gnus will try to gather all loose sub-threads from an
342 original thread into one large thread.
343
344 If this variable is non-nil, it should be one of `none', `adopt',
345 `dummy' or `empty'.
346
347 If this variable is `none', Gnus will not make a false root, but just
348 present the sub-threads after another.
349 If this variable is `dummy', Gnus will create a dummy root that will
350 have all the sub-threads as children.
351 If this variable is `adopt', Gnus will make one of the \"children\"
352 the parent and mark all the step-children as such.
353 If this variable is `empty', the \"children\" are printed with empty
354 subject fields.  (Or rather, they will be printed with a string
355 given by the `gnus-summary-same-subject' variable.)")
356
357 (defvar gnus-summary-gather-subject-limit nil
358   "*Maximum length of subject comparisons when gathering loose threads.
359 Use nil to compare full subjects.  Setting this variable to a low
360 number will help gather threads that have been corrupted by
361 newsreaders chopping off subject lines, but it might also mean that
362 unrelated articles that have subject that happen to begin with the
363 same few characters will be incorrectly gathered.
364
365 If this variable is `fuzzy', Gnus will use a fuzzy algortihm when
366 comparing subjects.")
367
368 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
369 (defvar gnus-summary-same-subject ""
370   "String indicating that the current article has the same subject as the previous.
371 This variable will only be used if the value of
372 `gnus-summary-make-false-root' is `empty'.")
373
374 (defvar gnus-summary-goto-unread nil
375   "If non-nil, marking commands will go to the next unread article.")
376
377 (defvar gnus-check-new-newsgroups t
378   "Non-nil means that Gnus will add new newsgroups at startup.
379 If this variable is `ask-server', Gnus will ask the server for new
380 groups since the last time it checked. This means that the killed list
381 is no longer necessary, so you could set `gnus-save-killed-list' to
382 nil. 
383
384 A variant is to have this variable be a list of select methods. Gnus
385 will then use the `ask-server' method on all these select methods to
386 query for new groups from all those servers.
387
388 Eg.
389   (setq gnus-check-new-newsgroups 
390         '((nntp \"some.server\") (nntp \"other.server\")))
391
392 If this variable is nil, then you have to tell Gnus explicitly to
393 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
394
395 (defvar gnus-check-bogus-newsgroups nil
396   "Non-nil means that Gnus will check and remove bogus newsgroup at startup.
397 If this variable is nil, then you have to tell Gnus explicitly to
398 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
399
400 (defvar gnus-read-active-file t
401   "Non-nil means that Gnus will read the entire active file at startup.
402 If this variable is nil, Gnus will only know about the groups in your
403 `.newsrc' file.
404
405 If you set this variable to nil, you probably still want to be told
406 about new newsgroups that arrive.  To do that, set
407 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
408 properly with all servers.") 
409
410 (defvar gnus-activate-foreign-newsgroups nil
411   "If nil, Gnus will not check foreign newsgroups at startup.
412 If it is non-nil, it should be a number between one and nine. Foreign
413 newsgroups that have a level lower or equal to this number will be
414 activated on startup. For instance, if you want to active all
415 subscribed newsgroups, but not the rest, you'd set this variable to 5.
416
417 If you subscribe to lots of newsgroups from different servers, startup
418 might take a while. By setting this variable to nil, you'll save time,
419 but you won't be told how many unread articles there are in the
420 groups.")
421
422 (defvar gnus-save-newsrc-file t
423   "Non-nil means that Gnus will save the `.newsrc' file.
424 Gnus always saves its own startup file, which is called
425 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
426 be readily understood by other newsreaders.  If you don't plan on
427 using other newsreaders, set this variable to nil to save some time on
428 exit.")
429
430 (defvar gnus-save-killed-list t
431   "If non-nil, save the list of killed groups to the startup file.
432 This will save both time (when starting and quitting) and space (both
433 memory and disk), but it will also mean that Gnus has no record of
434 which groups are new and which are old, so the automatic new
435 newsgroups subscription methods become meaningless. You should always
436 set `gnus-check-new-newsgroups' to `ask-server' or nil if you set this
437 variable to nil.")
438
439 (defvar gnus-interactive-catchup t
440   "If non-nil, require your confirmation when catching up a group.")
441
442 (defvar gnus-interactive-post t
443   "If non-nil, group name will be asked for when posting.")
444
445 (defvar gnus-interactive-exit t
446   "If non-nil, require your confirmation when exiting Gnus.")
447
448 (defvar gnus-kill-killed nil
449   "If non-nil, Gnus will apply kill files to already killed articles.
450 If it is nil, Gnus will never apply kill files to articles that have
451 already been through the scoring process, which might very well save lots
452 of time.")
453
454 (defvar gnus-extract-address-components 'gnus-extract-address-components
455   "Function for extracting address components from a From header.
456 Two pre-defined function exist: `gnus-extract-address-components',
457 which is the default, quite fast, and too simplistic solution, and
458 `mail-extract-address-components', which works much better, but is
459 slower.")
460
461 (defvar gnus-score-interactive-default-score 1000
462   "Scoring commands will raise/lower the score with this number as the default.")
463
464 (defvar gnus-save-score nil
465   "If non-nil, save group scoring info.")
466
467 (defvar gnus-global-score-files nil
468   "List of global score files and directories.
469 Set this variable if you want to use people's score files.  One entry
470 for each score file or each score file directory.  Gnus will decide
471 by itself what score files are applicable to which group.
472
473 Say you want to use the single score file
474 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
475 score files in the \"/ftp.some-where:/pub/score\" directory.
476
477  (setq gnus-global-score-files
478        '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
479          \"/ftp.some-where:/pub/score\"))")
480
481 (defvar gnus-summary-default-score 0
482   "Default article score level.
483 If this variable is nil, scoring will be disabled.")
484
485 (defvar gnus-group-default-list-level 5
486   "Default listing level.")
487
488 (defvar gnus-user-login-name nil
489   "The login name of the user.
490 Got from the function `user-login-name' if undefined.")
491
492 (defvar gnus-user-full-name nil
493   "The full name of the user.
494 Got from the NAME environment variable if undefined.")
495
496 (defvar gnus-user-from-line nil
497   "Your full, complete e-mail address.  
498 Overrides the other Gnus variables if it is non-nil.
499
500 Here are two example values of this variable:
501
502  \"Lars Magne Ingebrigtsen <larsi@ifi.uio.no>\"
503
504 and
505
506  \"larsi@ifi.uio.no (Lars Magne Ingebrigtsen)\"
507
508 The first version is recommended, but the name has to be quoted if it
509 contains non-alphanumerical characters.")
510
511 (defvar gnus-show-mime nil
512   "*If non-ni, do mime processing of articles.
513 The articles will simply be fed to the function given by
514 `gnus-show-mime-method'.")
515
516 (defvar gnus-show-mime-method (function metamail-buffer)
517   "Function to process a MIME message.
518 The function is called from the article buffer.")
519
520 (defvar gnus-show-threads t
521   "*If non-nil, display threads in summary mode.")
522
523 (defvar gnus-thread-hide-subtree nil
524   "If non-nil, hide all threads initially.
525 If threads are hidden, you have to run the command
526 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
527 to expose hidden threads.")
528
529 (defvar gnus-thread-hide-killed t
530   "If non-nil, hide killed threads automatically.")
531
532 (defvar gnus-thread-ignore-subject nil
533   "If non-nil, ignore subjects and do all threading based on the Reference header.
534 If nil, which is the default, articles that have different subjects
535 from their parents will start separate threads.")
536
537 (defvar gnus-thread-indent-level 4
538   "Number that says how much each sub-thread should be indented.")
539
540 ;; jwz: nuke newsgroups whose name is all digits - that means that
541 ;; some loser has let articles get into the root of the news spool,
542 ;; which is toxic. Lines beginning with whitespace also tend to be
543 ;; toxic.
544 (defvar gnus-ignored-newsgroups
545   (purecopy (mapconcat 'identity
546                        '("^to\\."       ; not "real" groups
547                          "^[0-9. \t]+ " ; all digits in name
548                          "[][\"#'()     ;\\]"   ; bogus characters
549                          )
550                        "\\|"))
551   "A regexp to match uninteresting newsgroups in the active file.
552 Any lines in the active file matching this regular expression are
553 removed from the newsgroup list before anything else is done to it,
554 thus making them effectively non-existant.")
555
556 (defvar gnus-ignored-headers
557   "^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:"
558   "All headers that match this regexp will be hidden.
559 Also see `gnus-visible-headers'.")
560
561 (defvar gnus-visible-headers "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:"
562   "All headers that do not match this regexp will be hidden.
563 Also see `gnus-ignored-headers'.")
564
565 (defvar gnus-sorted-header-list
566   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:" 
567     "^Cc:" "^Date:" "^Organization:")
568   "This variable is a list of regular expressions.
569 If it is non-nil, headers that match the regular expressions will
570 be placed first in the article buffer in the sequence specified by
571 this list.")
572
573 (defvar gnus-required-headers
574   '(From Date Newsgroups Subject Message-ID Organization Lines X-Newsreader)
575   "Headers to be generated or prompted for when posting an article.
576 RFC977 and RFC1036 require From, Date, Newsgroups, Subject, Message-ID
577 and Path headers.  Organization, Lines and X-Newsreader are optional.
578 If you want Gnus not to insert some header, remove it from this
579 list.") 
580
581 (defvar gnus-show-all-headers nil
582   "*If non-nil, don't hide any headers.")
583
584 (defvar gnus-save-all-headers t
585   "*If non-nil, don't remove any headers before saving.")
586
587 (defvar gnus-inhibit-startup-message nil
588   "If non-nil, the startup message will not be displayed.")
589
590 (defvar gnus-auto-extend-newsgroup t
591   "If non-nil, extend newsgroup forward and backward when requested.")
592
593 (defvar gnus-auto-select-first t
594   "If non-nil, select the first unread article when entering a group.
595 If you want to prevent automatic selection of the first unread article
596 in some newsgroups, set the variable to nil in
597 `gnus-select-group-hook'.") 
598
599 (defvar gnus-auto-select-next t
600   "If non-nil, offer to go to the next group from the end of the previous.
601 If the value is t and the next newsgroup is empty, Gnus will exit
602 summary mode and go back to group mode.  If the value is neither nil
603 nor t, Gnus will select the following unread newsgroup.  In
604 particular, if the value is the symbol `quietly', the next unread
605 newsgroup will be selected without any confirmations.")
606
607 (defvar gnus-auto-select-same nil
608   "If non-nil, select the next article with the same subject.")
609
610 (defvar gnus-auto-center-summary t
611   "*If non-nil, always center the current summary buffer.")
612
613 (defvar gnus-auto-mail-to-author nil
614   "*If non-nil, mail the authors of articles a copy of your follow-ups.
615 If this variable is `ask', the user will be prompted for whether to
616 mail a copy.  The string given by `gnus-mail-courtesy-message' will be
617 inserted at the beginning of the mail copy.
618
619 Mail is sent using the function specified by the
620 `gnus-mail-send-method' variable.")
621
622 ;; Added by Ethan Bradford <ethanb@ptolemy.astro.washington.edu>.
623 (defvar gnus-mail-courtesy-message
624   "The following message is a courtesy copy of an article\nthat has been posted as well.\n\n"
625   "This is inserted at the start of a mailed copy of a posted message.
626 If this variable is nil, no such courtesy message will be added.")
627
628 (defvar gnus-break-pages t
629   "*If non-nil, do page breaking on articles.
630 The page delimiter is specified by the `gnus-page-delimiter'
631 variable.")
632
633 (defvar gnus-page-delimiter "^\^L"
634   "Regexp describing what to use as article page delimiters.
635 The default value is \"^\^L\", which is a form linefeed at the
636 beginning of a line.")
637
638 (defvar gnus-use-full-window t
639   "*If non-nil, use the entire Emacs screen.")
640
641 (defvar gnus-window-configuration
642   '((summary (0 1 0))
643     (newsgroups (1 0 0))
644     (article (0 3 10)))
645   "Specify window configurations for each action.
646 The format of the variable is either a list of (ACTION (G S A)), where
647 G, S, and A are the relative height of group, summary, and article
648 windows, respectively, or a list of (ACTION FUNCTION), where FUNCTION
649 is a function that will be called with ACTION as an argument. ACTION
650 can be `summary', `newsgroups', or `article'.")
651
652 (defvar gnus-mail-reply-method (function gnus-mail-reply-using-mail)
653   "Function to compose a reply.
654 Two pre-made functions are `gnus-mail-reply-using-mail' (sendmail) and
655 `gnus-mail-reply-using-mhe' (MH-E).")
656
657 (defvar gnus-mail-forward-method (function gnus-mail-forward-using-mail)
658   "Function to forward the current message to another user.
659 Two pre-made functions are `gnus-mail-forward-using-mail' (sendmail)
660 and `gnus-mail-forward-using-mhe' (MH-E).") 
661
662 (defvar gnus-mail-other-window-method 'gnus-mail-other-window-using-mail
663   "Function to compose mail in the other window.
664 Two pre-made functions are `gnus-mail-other-window-using-mail'
665 (sendmail) and `gnus-mail-other-window-using-mhe' (MH-E).")
666
667 (defvar gnus-mail-send-method send-mail-function
668   "Function to mail a message which is also being posted as an article.
669 The message must have To or Cc header.  The default is copied from
670 the variable `send-mail-function'.")
671
672 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
673   "Function called with a group name when new group is detected.
674 A few pre-made functions are supplied: `gnus-subscribe-randomly'
675 inserts new groups at the beginning of the list of groups;
676 `gnus-subscribe-alphabetically' inserts new groups in strict
677 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
678 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
679 for your decision.")
680
681 ;; Suggested by a bug report by Hallvard B Furuseth.
682 ;; <h.b.furuseth@usit.uio.no>. 
683 (defvar gnus-subscribe-options-newsgroup-method
684   (function gnus-subscribe-alphabetically)
685   "This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
686 If, for instance, you want to subscribe to all newsgroups in the
687 \"no\" and \"alt\" hierarchies, you'd put the following in your
688 .newsrc file:
689
690 options -n no.all alt.all
691
692 Gnus will the subscribe all new newsgroups in these hierarchies with
693 the subscription method in this variable.")
694
695 (defvar gnus-subscribe-hierarchical-interactive nil
696   "If non-nil, Gnus will offer to subscribe hierarchically.
697 When a new hierarchy appears, Gnus will ask the user:
698
699 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
700
701 If the user pressed `d', Gnus will descend the hierarchy, `y' will
702 subscribe to all newsgroups in the hierarchy and `s' will skip this
703 hierarchy in its entirety.")
704
705 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
706   "Function used for sorting the group buffer.
707 This function will be called with group info entries as the arguments
708 for the groups to be sorted.  Pre-made functions include
709 `gnus-sort-by-alphabet', `gnus-sort-by-unread' and
710 `gnus-sort-by-level'")
711
712 ;; Mark variables suggested by Thomas Michanek
713 ;; <Thomas.Michanek@telelogic.se>. 
714 (defvar gnus-unread-mark ? 
715   "Mark used for unread articles.")
716 (defvar gnus-ticked-mark ?!
717   "Mark used for ticked articles.")
718 (defvar gnus-dormant-mark ??
719   "Mark used for dormant articles.")
720 (defvar gnus-dread-mark ?D
721   "Mark used for read articles.")
722 (defvar gnus-read-mark ?d
723   "Mark used for read articles.")
724 (defvar gnus-expirable-mark ?E
725   "Mark used for expirable articles.")
726 (defvar gnus-killed-mark ?K
727   "Mark used for killed articles.")
728 (defvar gnus-kill-file-mark ?X
729   "Mark used for articles killed by kill files.")
730 (defvar gnus-low-score-mark ?Y
731   "Mark used for articles with a low score.")
732 (defvar gnus-catchup-mark ?C
733   "Mark used for articles that are caught up.")
734 (defvar gnus-replied-mark ?R
735   "Mark used for articles that have been replied to.")
736 (defvar gnus-process-mark ?# 
737   "Process mark.")
738 (defvar gnus-ancient-mark ?A
739   "Mark used for ancient articles.")
740 (defvar gnus-canceled-mark ?G
741   "Mark used for cancelled articles.")
742 (defvar gnus-score-over-mark ?+
743   "Score mark used for articles with high scores.")
744 (defvar gnus-score-below-mark ?-
745   "Score mark used for articles with low scores.")
746
747 (defvar gnus-view-pseudo-asynchronously nil
748   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
749
750 (defvar gnus-view-pseudos nil
751   "If `automatic', pseudo-articles will be viewed automatically.
752 If `not-confirm', pseudos will be viewed automatically, and the user
753 will not be asked to confirm the command.")
754
755 (defvar gnus-group-line-format "%M%S%5y: %(%g%)\n"
756   "Format of groups lines.
757 It works along the same lines as a normal formatting string,
758 with some simple extensions.
759
760 %M    Only marked articles (character, \"*\" or \" \")
761 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
762 %L    Level of subscribedness (integer, 1-9)
763 %N    Number of unread articles (integer)
764 %I    Number of dormant articles (integer)
765 %i    Number of ticked and dormant (integer)
766 %T    Number of ticked articles (integer)
767 %R    Number of read articles (integer)
768 %t    Total number of articles (integer)
769 %y    Number of unread, unticked articles (integer)
770 %G    Group name (string)
771 %g    Qualified group name (string)
772 %D    Group description (string)
773 %s    Select method (string)
774 %o    Moderated group (char, \"m\")
775 %O    Moderated group (string, \"(m)\" or \"\")
776 %n    Select from where (string)
777 %z    A string that look like `<%s:%n>' if a foreign select method is used
778 %u    User defined specifier. The next character in the format string should
779       be a letter.  Gnus will call the function gnus-user-format-function-X,
780       where X is the letter following %u. The function will be passed the
781       current header as argument. The function should return a string, which
782       will be inserted into the buffer just like information from any other
783       group specifier.
784
785 Text between %( and %) will be highlighted with `gnus-mouse-face' when
786 the mouse point move inside the area.  There can only be one such area.
787
788 Note that this format specification is not always respected. For
789 reasons of efficiency, when listing killed groups, this specification
790 is ignored altogether. If the spec is changed considerably, your
791 output may end up looking strange when listing both alive and killed
792 groups.
793
794 If you use %o or %O, reading the active file will be slower and quite
795 a bit of extra memory will be used. %D will also worsen performance.
796 Also note that if you change the format specification to include any
797 of these specs, you must probably re-start Gnus to see them go into
798 effect.") 
799
800 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
801   "The format specification of the lines in the summary buffer.
802
803 It works along the same lines as a normal formatting string,
804 with some simple extensions.
805
806 %N   Article number, left padded with spaces (string)
807 %S   Subject (string)
808 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
809 %n   Name of the poster (string)
810 %A   Address of the poster (string)
811 %L   Number of lines in the article (integer)
812 %c   Number of characters in the article (integer)
813 %D   Date of the article (string)
814 %I   Indentation based on thread level (a string of spaces)
815 %T   A string with two possible values: 80 spaces if the article
816      is on thread level two or larger and 0 spaces on level one
817 %U   Status of this article (character, \"D\", \"K\", \"-\" or \" \") 
818 %[   Opening bracket (character, \"[\" or \"<\")
819 %]   Closing bracket (character, \"]\" or \">\")
820 %>   Spaces of length thread-level (string)
821 %<   Spaces of length (- 20 thread-level) (string)
822 %i   Article score (number)
823 %z   Article zcore (character)
824 %t   Number of articles under the current thread.
825 %u   User defined specifier. The next character in the format string should
826      be a letter.  Gnus will call the function gnus-user-format-function-X,
827      where X is the letter following %u. The function will be passed the
828      current header as argument. The function should return a string, which
829      will be inserted into the summary just like information from any other
830      summary specifier.
831
832 Text between %( and %) will be highlighted with `gnus-mouse-face'
833 when the mouse point is placed inside the area.  There can only be one
834 such area.
835
836 The %U (status), %R (replied) and %z (zcore) specs have to be handled
837 with care. For reasons of efficiency, Gnus will compute what column
838 these characters will end up in, and \"hard-code\" that. This means that
839 it is illegal to have these specs after a variable-length spec. Well,
840 you might not be arrested, but your summary buffer will look strange,
841 which is bad enough.
842
843 The smart choice is to have these specs as for to the left as
844 possible. 
845
846 This restriction may disappear in later versions of Gnus.")
847
848 (defvar gnus-summary-dummy-line-format "*   :                          : %S\n"
849   "The format specification for the dummy roots in the summary buffer.
850 It works along the same lines as a normal formatting string,
851 with some simple extensions.
852
853 %S  The subject")
854
855 (defvar gnus-summary-mode-line-format "(ding) %G/%A %Z"
856   "The format specification for the summary mode line.")
857
858 (defvar gnus-article-mode-line-format "(ding) %G/%A %S"
859   "The format specification for the article mode line.")
860
861 (defvar gnus-group-mode-line-format "(ding) List of groups   {%M:%S}  "
862   "The format specification for the group mode line.")
863
864 (defvar gnus-valid-select-methods
865   '(("nntp" post address prompt-address)
866     ("nnspool" post) ("nnvirtual" none virtual prompt-address) 
867     ("nnmbox" mail respool) ("nnml" mail respool)
868     ("nnmh" mail respool) ("nndir" none prompt-address) ("nndigest" none)
869     ("nndoc" none prompt-address) ("nnbabyl" mail respool)
870     ("nnkiboze" none virtual) ("nnfolder" mail respool))
871   "An alist of valid select methods.
872 The first element of each list lists should be a string with the name
873 of the select method. The other elements may be be the category of
874 this method (ie. `post', `mail', `none' or whatever) or other
875 properties that this method has (like being respoolable).
876 If you implement a new select method, all you should have to change is
877 this variable. I think.")
878
879 (defvar gnus-updated-mode-lines '(group article summary)
880   "List of buffers that should update their mode lines.
881 The list may contain the symbols `group', `article' and `summary'. If
882 the corresponding symbol is present, Gnus will keep that mode line
883 updated with information that may be pertinent. 
884 If this variable is nil, screen refresh may be quicker.")
885
886 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
887 (defvar gnus-mode-non-string-length 21
888   "Max length of mode-line non-string contents buffer contents.")
889
890 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
891 (defvar gnus-mouse-face 'highlight
892   "Face used for mouse highlighting in Gnus.
893 No mouse highlights will be done if `gnus-visual' is nil.")
894
895 (defvar gnus-summary-mark-below nil
896   "Mark all articles with a score below this variable as read.
897 This variable is local to each summary buffer and usually set by the
898 score file.")  
899
900 (defvar gnus-orphan-score nil
901   "All orphans get this score added. Set in the score file.")
902
903 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
904   "List of functions used for sorting threads in the summary buffer.
905 By default, threads are sorted by article number.
906
907 Each function takes two threads and return non-nil if the first thread
908 should be sorted before the other.  If you use more than one function,
909 the primary sort function should be the last.
910
911 Ready-mady functions include `gnus-thread-sort-by-number',
912 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
913 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
914 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
915
916 The latter two only work on threads that have been scored prior to
917 entering the newsgroup.")
918
919 (defvar gnus-thread-score-function '+
920   "Function used for calculating the total score of a thread.
921
922 The function is called with the scores of the article and each
923 subthread and should then return the score of the thread.
924
925 Some functions you can use are `+', `max', or `min'.")
926
927 (defvar gnus-score-find-score-files-function 'gnus-score-find-bnews
928   "Function used to find SCORE files.
929 The function will be called with the group name as the argument, and
930 should return a list of score files to apply to that group.  The score
931 files do not actually have to exist.
932
933 Predefined values are:
934
935 gnus-score-find-single: Only apply the group's own SCORE file.
936 gnus-score-find-hierarchical: Also apply SCORE files from parent groups.
937 gnus-score-find-bnews: Apply SCORE files whose names matches.
938
939 See the documentation to these functions for more information.
940
941 This variable can also be a list of functions to be called.  Each
942 function should either return a list of score files, or the functions
943 may also return lists of score alists.")
944
945 (defvar gnus-options-subscribe nil
946   "All new groups matching this regexp will be subscribed unconditionally.
947 Note that this variable deals only with new newsgroups.  This variable
948 does not affect old newsgroups.")
949
950 (defvar gnus-options-not-subscribe nil
951   "All new groups matching this regexp will be ignored.
952 Note that this variable deals only with new newsgroups.  This variable
953 does not affect old (already subscribed) newsgroups.")
954
955 (defvar gnus-auto-expirable-newsgroups nil
956   "Groups in which to automatically mark read articles as expirable.
957 If non-nil, this should be a regexp that should match all groups in
958 which to perform auto-expiry. This only makes sense for mail groups.")
959
960
961 ;; Hooks.
962
963 (defvar gnus-group-mode-hook nil
964   "A hook for Gnus group mode.")
965
966 (defvar gnus-summary-mode-hook nil
967   "A hook for Gnus summary mode.")
968
969 (defvar gnus-article-mode-hook nil
970   "A hook for Gnus article mode.")
971
972 (defvar gnus-open-server-hook nil
973   "A hook called just before opening connection to the news server.")
974
975 (defvar gnus-startup-hook nil
976   "A hook called at startup.
977 This hook is called after Gnus is connected to the NNTP server.")
978
979 (defvar gnus-get-new-news-hook nil
980   "A hook run just before Gnus checks for new news.")
981
982 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
983   "A function that is called to generate the group buffer.
984 The function is called with three arguments: The first is a number;
985 all group with a level less or equal to that number should be listed,
986 if the second is non-nil, empty groups should also be displayed. If
987 the third is non-nil, it is a number. No groups with a level lower
988 than this number should be displayed.
989
990 The only current function implemented is `gnus-group-prepare-flat'.")
991
992 (defvar gnus-group-prepare-hook nil
993   "A hook called after the group buffer has been generated.
994 If you want to modify the group buffer, you can use this hook.")
995
996 (defvar gnus-summary-prepare-hook nil
997   "A hook called after the summary buffer has been generated.
998 If you want to modify the summary buffer, you can use this hook.")
999
1000 (defvar gnus-article-prepare-hook nil
1001   "A hook called after an article has been prepared in the article buffer.
1002 If you want to run a special decoding program like nkf, use this hook.")
1003
1004 (defvar gnus-article-display-hook nil
1005   "A hook called after the article is displayed in the article buffer.
1006 The hook is designed to change the contents of the article
1007 buffer. Typical functions that this hook may contain are
1008 `gnus-article-hide-headers' (hide selected headers),
1009 `gnus-article-hide-signature' (hide signature) and
1010 `gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1011 (add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1012 (add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1013
1014 (defvar gnus-select-group-hook nil
1015   "A hook called when a newsgroup is selected.
1016
1017 If you'd like to simplify subjects like the
1018 `gnus-summary-next-same-subject' command does, you can use the
1019 following hook:
1020
1021  (setq gnus-select-group-hook
1022       (list
1023         (lambda ()
1024           (mapcar (lambda (header)
1025                      (header-set-subject
1026                       header
1027                       (gnus-simplify-subject
1028                        (header-subject header) 're-only)))
1029                   gnus-newsgroup-headers))))")
1030
1031 (defvar gnus-select-article-hook
1032   '(gnus-summary-show-thread)
1033   "A hook called when an article is selected.
1034 The default hook shows conversation thread subtrees of the selected
1035 article automatically using `gnus-summary-show-thread'.")
1036
1037 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1038   "A hook called to apply kill files to a group.
1039 This hook is intended to apply a kill file to the selected newsgroup.
1040 The function `gnus-apply-kill-file' is called by default.
1041
1042 Since a general kill file is too heavy to use only for a few
1043 newsgroups, I recommend you to use a lighter hook function. For
1044 example, if you'd like to apply a kill file to articles which contains
1045 a string `rmgroup' in subject in newsgroup `control', you can use the
1046 following hook:
1047
1048 \(setq gnus-apply-kill-hook
1049       (list
1050         (lambda ()
1051           (cond ((string-match \"control\" gnus-newsgroup-name)
1052                  (gnus-kill \"Subject\" \"rmgroup\")
1053                  (gnus-expunge \"X\"))))))")
1054
1055 (defvar gnus-visual-mark-article-hook 
1056   (list 'gnus-visual-highlight-selected-summary)
1057   "Hook run after selecting an article in the summary buffer.
1058 It is meant to be used for highlighting the article in some way.  It
1059 is not run if `gnus-visual' is nil.")
1060
1061 (defvar gnus-prepare-article-hook (list 'gnus-inews-insert-signature)
1062   "A hook called after preparing body, but before preparing header headers.
1063 The default hook (`gnus-inews-insert-signature') inserts a signature
1064 file specified by the variable `gnus-signature-file'.")
1065
1066 (defvar gnus-inews-article-hook (list 'gnus-inews-do-fcc)
1067   "A hook called before finally posting an article.
1068 The default hook (`gnus-inews-do-fcc') does FCC processing (ie. saves
1069 the article to a file).")
1070
1071 (defvar gnus-inews-article-header-hook nil
1072   "A hook called after inserting the headers in an article to be posted.
1073 The hook is called from the *post-news* buffer, narrowed to the
1074 headers.")
1075
1076 (defvar gnus-exit-group-hook nil
1077   "A hook called when exiting (not quitting) summary mode.")
1078
1079 (defvar gnus-suspend-gnus-hook nil
1080   "A hook called when suspending (not exiting) Gnus.")
1081
1082 (defvar gnus-exit-gnus-hook nil
1083   "A hook called when exiting Gnus.")
1084
1085 (defvar gnus-save-newsrc-hook nil
1086   "A hook called when saving the newsrc file.")
1087
1088 (defvar gnus-visual-summary-update-hook 
1089   (list 'gnus-visual-summary-highlight-line)
1090   "A hook called when a summary line is changed.
1091 The hook will not be called if `gnus-visual' is nil.
1092
1093 The default function `gnus-visual-summary-highlight-line' will
1094 highlight the line according to the `gnus-visual-summary-highlight'
1095 variable.")
1096
1097 (defvar gnus-mark-article-hook (list 'gnus-summary-mark-unread-as-read)
1098   "A hook called when an article is selected for the first time.
1099 The hook is intended to mark an article as read (or unread)
1100 automatically when it is selected.")
1101
1102 ;; Site dependent variables. These variables should be defined in
1103 ;; paths.el.
1104
1105 (defvar gnus-default-nntp-server nil
1106   "Specify a default NNTP server.
1107 This variable should be defined in paths.el, and should never be set
1108 by the user.
1109 If you want to change servers, you should use `gnus-select-method'.
1110 See the documentation to that variable.")
1111
1112 (defconst gnus-backup-default-subscribed-newsgroups 
1113   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
1114   "Default default new newsgroups the first time Gnus is run.
1115 Should be set in paths.el, and shouldn't be touched by the user.")
1116
1117 (defvar gnus-local-domain nil
1118   "Local domain name without a host name.
1119 The DOMAINNAME environment variable is used instead if it is defined.
1120 If the `system-name' function returns the full Internet name, there is
1121 no need to set this variable.")
1122
1123 (defvar gnus-local-organization nil
1124   "String with a description of what organization (if any) the user belongs to.
1125 The ORGANIZATION environment variable is used instead if it is defined.
1126 If this variable contains a function, this function will be called
1127 with the current newsgroup name as the argument. The function should
1128 return a string.
1129 In any case, if the string (either in the variable, in the environment
1130 variable, or returned by the function) is a file name, the contents of
1131 this file will be used as the organization.")
1132
1133 (defvar gnus-use-generic-from nil
1134   "If nil, the full host name will be the system name prepended to the domain name.
1135 If this is a string, the full host name will be this string.
1136 If this is non-nil, non-string, the domain name will be used as the
1137 full host name.")
1138
1139 (defvar gnus-use-generic-path nil
1140   "If nil, use the NNTP server name in the Path header.
1141 If stringp, use this; if non-nil, use no host name (user name only).")
1142
1143 \f
1144 ;; Internal variables
1145
1146 ;; Avoid highlighting in kill files.
1147 (defvar gnus-summary-inhibit-highlight nil)
1148 (defvar gnus-newsgroup-selected-overlay nil)
1149
1150 (defvar gnus-article-mode-map nil)
1151 (defvar caesar-translate-table nil)
1152 (defvar gnus-dribble-buffer nil)
1153 (defvar gnus-headers-retrieved-by nil)
1154 (defvar gnus-article-reply nil)
1155 (defvar gnus-override-method nil)
1156 (defvar gnus-article-check-size nil)
1157 (defvar gnus-score-file-list nil)
1158 (defvar gnus-internal-global-score-files nil)
1159 (defvar gnus-current-score-file nil)
1160
1161 (defvar gnus-current-move-group nil)
1162
1163 (defvar gnus-score-alist nil
1164   "Alist containing score information.
1165 The keys can be symbols or strings.  The following symbols are defined. 
1166
1167 touched: If this alist has been modified.
1168 mark:    Automatically mark articles below this.
1169 expunge: Automatically expunge articles below this.
1170 files:   List of other SCORE files to load when loading this one.
1171 eval:    Sexp to be evaluated when the score file is loaded.
1172
1173 String entries have the form (HEADER (MATCH TYPE SCORE DATE) ...) 
1174 where HEADER is the header being scored, MATCH is the string we are
1175 looking for, TYPE is a flag indicating whether it should use regexp or
1176 substring matching, SCORE is the score to add and DATE is the date
1177 of the last succesful match.")
1178
1179 (defvar gnus-score-cache nil)
1180 (defvar gnus-scores-articles nil)
1181 (defvar gnus-header-index nil)
1182 (defvar gnus-score-index nil)
1183
1184 (defvar gnus-newsgroup-dependencies nil)
1185 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1186 (defvar gnus-default-subscribe-level 2)
1187 (defvar gnus-default-unsubscribe-level 6)
1188 (defvar gnus-default-kill-level 9)
1189
1190 (defconst gnus-group-line-format-alist
1191   (list (list ?M 'marked ?c)
1192         (list ?S 'subscribed ?c)
1193         (list ?L 'level ?d)
1194         (list ?N 'number ?s)
1195         (list ?I 'number-of-dormant ?d)
1196         (list ?T 'number-of-ticked ?d)
1197         (list ?R 'number-of-read ?s)
1198         (list ?t 'number-total ?d)
1199         (list ?y 'number-of-unread-unticked ?s)
1200         (list ?i 'number-of-ticked-and-dormant ?d)
1201         (list ?g 'group ?s)
1202         (list ?G 'qualified-group ?s)
1203         (list ?D 'newsgroup-description ?s)
1204         (list ?o 'moderated ?c)
1205         (list ?O 'moderated-string ?s)
1206         (list ?s 'news-server ?s)
1207         (list ?n 'news-method ?s)
1208         (list ?z 'news-method-string ?s)
1209         (list ?u 'user-defined ?s)))
1210
1211 (defconst gnus-summary-line-format-alist 
1212   (list (list ?N 'number ?s)
1213         (list ?S 'subject ?s)
1214         (list ?s 'subject-or-nil ?s)
1215         (list ?n 'name ?s)
1216         (list ?A 'address ?s)
1217         (list ?F 'from ?s)
1218         (list ?x (macroexpand '(header-xref header)) ?s)
1219         (list ?D (macroexpand '(header-date header)) ?s)
1220         (list ?M (macroexpand '(header-id header)) ?s)
1221         (list ?r (macroexpand '(header-references header)) ?s)
1222         (list ?c (macroexpand '(header-chars header)) ?d)
1223         (list ?L 'lines ?d)
1224         (list ?I 'indentation ?s)
1225         (list ?T '(if (< level 1) "" (make-string (frame-width) ? )) ?s)
1226         (list ?R 'replied ?c)
1227         (list ?\[ 'opening-bracket ?c)
1228         (list ?\] 'closing-bracket ?c)
1229         (list ?\> '(make-string level ? ) ?s)
1230         (list ?\< '(make-string (max 0 (- 20 level)) ? ) ?s)
1231         (list ?i 'score ?s)
1232         (list ?z 'score-char ?c)
1233         (list ?U 'unread ?c)
1234         (list ?t '(gnus-summary-number-of-articles-in-thread thread) ?d)
1235         (list ?u 'user-defined ?s))
1236   "An alist of format specifications that can appear in summary lines,
1237 and what variables they correspond with, along with the type of the
1238 variable (string, integer, character, etc).")
1239
1240 (defconst gnus-summary-dummy-line-format-alist
1241   (list (list ?S 'subject ?s)
1242         (list ?N 'number ?d)
1243         (list ?u 'user-defined ?s)))
1244
1245 (defconst gnus-summary-mode-line-format-alist 
1246   (list (list ?G 'group-name ?s)
1247         (list ?g '(gnus-short-group-name group-name))
1248         (list ?A 'article-number ?d)
1249         (list ?Z 'unread-and-unselected ?s)
1250         (list ?V 'gnus-version ?s)
1251         (list ?U 'unread ?d)
1252         (list ?S 'subject ?s)
1253         (list ?e 'unselected ?d)
1254         (list ?u 'user-defined ?s)
1255         (list ?s 'gnus-current-score-file ?s)))
1256
1257 (defconst gnus-group-mode-line-format-alist 
1258   (list (list ?S 'news-server ?s)
1259         (list ?M 'news-method ?s)
1260         (list ?u 'user-defined ?s)))
1261
1262 (defvar gnus-have-read-active-file nil)
1263
1264 (defconst gnus-maintainer "Lars Magne Ingebrigtsen <larsi@ifi.uio.no>"
1265   "The mail address of the Gnus maintainer.")
1266
1267 (defconst gnus-version "(ding) Gnus v0.50"
1268   "Version number for this version of Gnus.")
1269
1270 (defvar gnus-info-nodes
1271   '((gnus-group-mode            "(gnus)The Group Buffer")
1272     (gnus-summary-mode          "(gnus)The Summary Buffer")
1273     (gnus-article-mode          "(gnus)The Article Buffer"))
1274   "Assoc list of major modes and related Info nodes.")
1275
1276 (defvar gnus-documentation-group-file "~/dgnus/lisp/doc.txt"
1277   "The location of the (ding) Gnus documentation group.")
1278
1279 (defvar gnus-group-buffer "*Group*")
1280 (defvar gnus-summary-buffer "*Summary*")
1281 (defvar gnus-article-buffer "*Article*")
1282
1283 (defvar gnus-buffer-list nil
1284   "Gnus buffers that should be killed on exit.")
1285
1286 (defvar gnus-variable-list
1287   '(gnus-newsrc-options gnus-newsrc-options-n
1288     gnus-newsrc-last-checked-date
1289     gnus-newsrc-assoc gnus-killed-list gnus-zombie-list)
1290   "Gnus variables saved in the quick startup file.")
1291
1292 (defvar gnus-overload-functions
1293   '((news-inews gnus-inews-news "rnewspost")
1294     (caesar-region gnus-caesar-region "rnews"))
1295   "Functions overloaded by gnus.
1296 It is a list of `(original overload &optional file)'.")
1297
1298 (defvar gnus-newsrc-options nil
1299   "Options line in the .newsrc file.")
1300
1301 (defvar gnus-newsrc-options-n nil
1302   "List of regexps representing groups to be subscribed/ignored unconditionally.") 
1303
1304 (defvar gnus-newsrc-last-checked-date nil
1305   "Date Gnus last asked server for new newsgroups.")
1306
1307 (defvar gnus-newsrc-assoc nil
1308   "Assoc list of read articles.
1309 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1310
1311 (defvar gnus-newsrc-hashtb nil
1312   "Hashtable of gnus-newsrc-assoc.")
1313
1314 (defvar gnus-killed-list nil
1315   "List of killed newsgroups.")
1316
1317 (defvar gnus-killed-hashtb nil
1318   "Hash table equivalent of gnus-killed-list.")
1319
1320 (defvar gnus-zombie-list nil
1321   "List of almost dead newsgroups.")
1322
1323 (defvar gnus-description-hashtb nil
1324   "Descriptions of newsgroups.")
1325
1326 (defvar gnus-list-of-killed-groups nil
1327   "List of newsgroups that have recently been killed by the user.")
1328
1329 (defvar gnus-active-hashtb nil
1330   "Hashtable of active articles.")
1331
1332 (defvar gnus-moderated-list nil
1333   "List of moderated newsgroups.")
1334
1335 (defvar gnus-current-startup-file nil
1336   "Startup file for the current host.")
1337
1338 (defvar gnus-last-search-regexp nil
1339   "Default regexp for article search command.")
1340
1341 (defvar gnus-last-shell-command nil
1342   "Default shell command on article.")
1343
1344 (defvar gnus-current-select-method nil
1345   "The current method for selecting a newsgroup.")
1346
1347 (defvar gnus-have-all-newsgroups nil)
1348
1349 (defvar gnus-article-internal-prepare-hook nil)
1350
1351 (defvar gnus-newsgroup-name nil)
1352 (defvar gnus-newsgroup-begin nil)
1353 (defvar gnus-newsgroup-end nil)
1354 (defvar gnus-newsgroup-last-rmail nil)
1355 (defvar gnus-newsgroup-last-mail nil)
1356 (defvar gnus-newsgroup-last-folder nil)
1357 (defvar gnus-newsgroup-last-file nil)
1358 (defvar gnus-newsgroup-auto-expire nil)
1359
1360 (defvar gnus-newsgroup-unreads nil
1361   "List of unread articles in the current newsgroup.")
1362
1363 (defvar gnus-newsgroup-unselected nil
1364   "List of unselected unread articles in the current newsgroup.")
1365
1366 (defvar gnus-newsgroup-marked nil
1367   "List of ticked articles in the current newsgroup (a subset of unread art).")
1368
1369 (defvar gnus-newsgroup-killed nil
1370   "List of ranges of articles that have been through the scoring process.")
1371
1372 (defvar gnus-newsgroup-kill-headers nil)
1373
1374 (defvar gnus-newsgroup-replied nil
1375   "List of articles that have been replied to in the current newsgroup.")
1376
1377 (defvar gnus-newsgroup-expirable nil
1378   "List of articles in the current newsgroup that can be expired.")
1379
1380 (defvar gnus-newsgroup-processable nil
1381   "List of articles in the current newsgroup that can be processed.")
1382
1383 (defvar gnus-newsgroup-bookmarks nil
1384   "List of articles in the current newsgroup that have bookmarks.")
1385
1386 (defvar gnus-newsgroup-dormant nil
1387   "List of dormant articles in the current newsgroup.")
1388
1389 (defvar gnus-newsgroup-scored nil
1390   "List of scored articles in the current newsgroup.")
1391
1392 (defvar gnus-newsgroup-headers nil
1393   "List of article headers in the current newsgroup.")
1394 (defvar gnus-newsgroup-headers-hashtb-by-number nil)
1395
1396 (defvar gnus-newsgroup-ancient nil
1397   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1398
1399 (defvar gnus-current-article nil)
1400 (defvar gnus-article-current nil)
1401 (defvar gnus-current-headers nil)
1402 (defvar gnus-have-all-headers nil)
1403 (defvar gnus-last-article nil)
1404 (defvar gnus-newsgroup-history nil)
1405 (defvar gnus-current-kill-article nil)
1406
1407 ;; Save window configuration.
1408 (defvar gnus-winconf-edit-group nil)
1409 (defvar gnus-winconf-edit-score nil)
1410
1411 ;; Format specs
1412 (defvar gnus-summary-line-format-spec nil)
1413 (defvar gnus-summary-dummy-line-format-spec nil)
1414 (defvar gnus-group-line-format-spec nil)
1415 (defvar gnus-summary-mode-line-format-spec nil)
1416 (defvar gnus-article-mode-line-format-spec nil)
1417 (defvar gnus-group-mode-line-format-spec nil)
1418 (defvar gnus-summary-mark-positions nil)
1419
1420 (defvar gnus-summary-expunge-below nil)
1421 (defvar gnus-reffed-article-number nil)
1422
1423 (defvar rmail-default-file (expand-file-name "~/XMBOX"))
1424 (defvar rmail-default-rmail-file (expand-file-name "~/XNEWS"))
1425
1426 (defconst gnus-summary-local-variables 
1427   '(gnus-newsgroup-name 
1428     gnus-newsgroup-begin gnus-newsgroup-end 
1429     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail 
1430     gnus-newsgroup-last-folder gnus-newsgroup-last-file 
1431     gnus-newsgroup-auto-expire gnus-newsgroup-unreads 
1432     gnus-newsgroup-unselected gnus-newsgroup-marked
1433     gnus-newsgroup-replied gnus-newsgroup-expirable
1434     gnus-newsgroup-processable gnus-newsgroup-killed
1435     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1436     gnus-newsgroup-headers gnus-newsgroup-headers-hashtb-by-number
1437     gnus-current-article gnus-current-headers gnus-have-all-headers
1438     gnus-last-article gnus-article-internal-prepare-hook
1439     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1440     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1441     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below 
1442     gnus-summary-mark-below
1443     gnus-newsgroup-history gnus-newsgroup-ancient)
1444   "Variables that are buffer-local to the summary buffers.")
1445
1446 ;;; End of variables.
1447
1448 ;; Define some autoload functions Gnus might use.
1449 (eval-and-compile
1450   (autoload 'metamail-buffer "metamail")
1451   (autoload 'Info-goto-node "info")
1452   
1453   (autoload 'timezone-make-date-arpa-standard "timezone")
1454   (autoload 'timezone-fix-time "timezone")
1455   (autoload 'timezone-make-sortable-date "timezone")
1456   (autoload 'timezone-make-time-string "timezone")
1457   
1458   (autoload 'rmail-output "rmailout")
1459   (autoload 'mail-position-on-field "sendmail")
1460   (autoload 'mail-setup "sendmail")
1461   (autoload 'news-mail-other-window "rnewspost")
1462
1463   (autoload 'gnus-mail-reply-using-mhe "gnus-mh")
1464   (autoload 'gnus-mail-forward-using-mhe "gnus-mh")
1465   (autoload 'gnus-mail-other-window-using-mhe "gnus-mh")
1466   (autoload 'gnus-summary-save-in-folder "gnus-mh")
1467   (autoload 'gnus-Folder-save-name "gnus-mh")
1468   (autoload 'gnus-folder-save-name "gnus-mh")
1469   
1470   (autoload 'gnus-group-make-menu-bar "gnus-visual")
1471   (autoload 'gnus-summary-make-menu-bar "gnus-visual")
1472   (autoload 'gnus-article-make-menu-bar "gnus-visual")
1473   (autoload 'gnus-visual-highlight-selected-summary "gnus-visual")
1474   (autoload 'gnus-visual-summary-highlight-line "gnus-visual")
1475
1476   (autoload 'gnus-uu-mark-by-regexp "gnus-uu")
1477   (autoload 'gnus-uu-mark-region "gnus-uu")
1478   (autoload 'gnus-uu-mark-thread "gnus-uu")
1479   (autoload 'gnus-uu-mark-sparse "gnus-uu")
1480   (autoload 'gnus-uu-mark-series "gnus-uu")
1481   (autoload 'gnus-uu-mark-all "gnus-uu")
1482   (autoload 'gnus-uu-post-news "gnus-uu")
1483   (autoload 'gnus-uu-digest-and-forward "gnus-uu")
1484
1485   (autoload 'gnus-uu-decode-uu "gnus-uu")
1486   (autoload 'gnus-uu-decode-uu-and-save "gnus-uu")
1487   (autoload 'gnus-uu-decode-unshar "gnus-uu")
1488   (autoload 'gnus-uu-decode-unshar-and-save "gnus-uu")
1489   (autoload 'gnus-uu-decode-save "gnus-uu")
1490   (autoload 'gnus-uu-decode-binhex "gnus-uu")
1491   (autoload 'gnus-uu-decode-uu-view "gnus-uu")
1492   (autoload 'gnus-uu-decode-uu-and-save-view "gnus-uu")
1493   (autoload 'gnus-uu-decode-unshar-view "gnus-uu")
1494   (autoload 'gnus-uu-decode-unshar-and-save-view "gnus-uu")
1495   (autoload 'gnus-uu-decode-save-view "gnus-uu")
1496   (autoload 'gnus-uu-decode-binhex-view "gnus-uu")
1497
1498   (autoload 'gnus-kill "gnus-kill")
1499   (autoload 'gnus-apply-kill-file-internal "gnus-kill")
1500   (autoload 'gnus-kill-file-edit-file "gnus-kill")
1501   (autoload 'gnus-kill-file-raise-followups-to-author "gnus-kill")
1502   (autoload 'gnus-execute "gnus-kill")
1503
1504   (autoload 'pp "pp")
1505   (autoload 'pp-to-string "pp")
1506   (autoload 'mail-extract-address-components "mail-extr")
1507   )
1508
1509 \f
1510
1511 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1512 ;; If you want the cursor to go somewhere else, set these two
1513 ;; functions in some startup hook to whatever you want.
1514 (defalias 'gnus-summary-position-cursor 'gnus-goto-colon)
1515 (defalias 'gnus-group-position-cursor 'gnus-goto-colon)
1516
1517 ;; Cruft to make Gnus work under GNU XEmacs.
1518 (defvar gnus-xemacs nil
1519   "Non-nil if Gnus is running under GNU XEmacs.")
1520
1521 (if (not (string-match "XEmacs\\|Lucid" emacs-version))
1522     ()
1523   (setq gnus-xemacs t)
1524   (eval
1525    '((or (memq 'underline (list-faces))
1526          (make-face 'underline))
1527      (or (face-differs-from-default-p 'underline)
1528          (set-face-underline-p 'underline t))
1529      
1530      (defun set-text-properties (start end props &optional buffer)
1531        (if props
1532            (put-text-property start end (car props) (cadr props) buffer)
1533          (remove-text-properties start end ()))))))
1534
1535
1536 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
1537   "Pop to BUFFER, evaluate FORMS, and then returns to original window."
1538   (` (let ((GnusStartBufferWindow (selected-window)))
1539        (unwind-protect
1540            (progn
1541              (pop-to-buffer (, buffer))
1542              (,@ forms))
1543          (select-window GnusStartBufferWindow)))))
1544
1545 (defun gnus-make-hashtable (&optional hashsize)
1546   "Make a hash table (default and minimum size is 255).
1547 Optional argument HASHSIZE specifies the table size."
1548   (make-vector (if hashsize 
1549                    (max (gnus-create-hash-size hashsize) 255)
1550                  255) 0))
1551
1552 (defmacro gnus-gethash (string hashtable)
1553   "Get hash value of STRING in HASHTABLE."
1554   ;;(` (symbol-value (abbrev-symbol (, string) (, hashtable))))
1555   ;;(` (abbrev-expansion (, string) (, hashtable)))
1556   (` (symbol-value (intern-soft (, string) (, hashtable)))))
1557
1558 (defmacro gnus-sethash (string value hashtable)
1559   "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
1560   ;; We cannot use define-abbrev since it only accepts string as value.
1561                                         ;  (set (intern string hashtable) value))
1562   (` (set (intern (, string) (, hashtable)) (, value))))
1563
1564 (defsubst gnus-buffer-substring (beg end)
1565   (buffer-substring (match-beginning beg) (match-end end)))
1566
1567 (defsubst gnus-simplify-subject-re (subject)
1568   "Remove \"Re:\" from subject lines."
1569   (let ((case-fold-search t))
1570     (if (string-match "^re: *" subject)
1571         (substring subject (match-end 0))
1572       subject)))
1573
1574 (defsubst gnus-goto-char (point)
1575   (and point (goto-char point)))
1576
1577 \f
1578 ;;;
1579 ;;; Gnus Utility Functions
1580 ;;;
1581
1582 (defun gnus-extract-address-components (from)
1583   (let (name address)
1584     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
1585         (setq address (substring from (match-beginning 0) (match-end 0))))
1586     (and address
1587          (string-match (concat "<" (regexp-quote address) ">") from)
1588          (setq name (substring from 0 (1- (match-beginning 0)))))
1589     (or name
1590         (and (string-match "(.+)" from)
1591              (setq name (substring from (1+ (match-beginning 0)) 
1592                                    (1- (match-end 0))))))
1593     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1594     (list (or name from) (or address from))))
1595
1596 (defun gnus-fetch-field (field)
1597   "Return the value of the header FIELD of current article."
1598   (save-excursion
1599     (save-restriction
1600       (gnus-narrow-to-headers)
1601       (mail-fetch-field field))))
1602
1603 (defun gnus-goto-colon ()
1604   (beginning-of-line)
1605   (search-forward ":" (save-excursion (end-of-line) (point)) t))
1606
1607 (defun gnus-narrow-to-headers ()
1608   (widen)
1609   (save-excursion
1610     (goto-char 1)
1611     (if (search-forward "\n\n")
1612         (narrow-to-region 1 (1- (point))))))
1613
1614 ;; Get a number that is suitable for hashing; bigger than MIN
1615 (defun gnus-create-hash-size (min)
1616   (let ((i 1))
1617     (while (< i min)
1618       (setq i (* 2 i)))
1619     (1- i)))
1620
1621 (defun gnus-update-format-specifications ()
1622   (setq gnus-summary-line-format-spec 
1623         (gnus-parse-format
1624          gnus-summary-line-format gnus-summary-line-format-alist))
1625   (gnus-update-summary-mark-positions)
1626   (setq gnus-summary-dummy-line-format-spec 
1627         (gnus-parse-format gnus-summary-dummy-line-format 
1628                            gnus-summary-dummy-line-format-alist))
1629   (setq gnus-group-line-format-spec
1630         (gnus-parse-format 
1631          gnus-group-line-format 
1632          gnus-group-line-format-alist))
1633   (if (and (string-match "%D" gnus-group-line-format)
1634            (not gnus-description-hashtb)
1635            gnus-read-active-file)
1636       (gnus-read-descriptions-file))
1637   (setq gnus-summary-mode-line-format-spec 
1638         (gnus-parse-format gnus-summary-mode-line-format 
1639                            gnus-summary-mode-line-format-alist))
1640   (setq gnus-article-mode-line-format-spec 
1641         (gnus-parse-format gnus-article-mode-line-format 
1642                            gnus-summary-mode-line-format-alist))
1643   (setq gnus-group-mode-line-format-spec 
1644         (gnus-parse-format gnus-group-mode-line-format 
1645                            gnus-group-mode-line-format-alist)))
1646
1647 (defun gnus-update-summary-mark-positions ()
1648   (save-excursion
1649     (let ((gnus-replied-mark 129)
1650           (gnus-score-below-mark 130)
1651           (gnus-score-over-mark 130)
1652           pos)
1653       (set-buffer (get-buffer-create " *gnus work*"))
1654       (buffer-disable-undo (current-buffer))
1655       (erase-buffer)
1656       (gnus-summary-insert-line 
1657        nil [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
1658       (goto-char (point-min))
1659       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
1660                                          (- (point) 2)))))
1661       (goto-char (point-min))
1662       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
1663                                           (- (point) 2))) pos))
1664       (goto-char (point-min))
1665       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
1666                                         (- (point) 2))) pos))
1667       (setq gnus-summary-mark-positions pos)
1668       (kill-buffer (current-buffer)))))
1669
1670 (defun gnus-format-max-width (var length)
1671   (let (result)
1672     (if (> (length (setq result (eval var))) length)
1673         (format "%s" (substring result 0 length))
1674       (format "%s" result))))
1675
1676 (defun gnus-set-mouse-face (string)
1677   ;; Set mouse face property on STRING.
1678   (or gnus-xemacs
1679       (put-text-property 0 (length string) 'mouse-face gnus-mouse-face string))
1680   string)
1681
1682 (defun gnus-parse-format (format spec-alist)
1683   ;; This function parses the FORMAT string with the help of the
1684   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1685   ;; string.  If the FORMAT string contains the specifiers %( and %)
1686   ;; the text between them will have the mouse-face text property.
1687   (if (string-match "\\`\\(.*\\)%(\\(.*\\)%)\\(.*\n?\\)\\'" format)
1688       (if (and gnus-visual gnus-mouse-face)
1689           (let ((pre (substring format (match-beginning 1) (match-end 1)))
1690                 (button (substring format (match-beginning 2) (match-end 2)))
1691                 (post (substring format (match-beginning 3) (match-end 3))))
1692             (list 'concat
1693                   (gnus-parse-simple-format pre spec-alist)
1694                   (list 'gnus-set-mouse-face
1695                         (gnus-parse-simple-format button spec-alist))
1696                   (gnus-parse-simple-format post spec-alist)))
1697         (gnus-parse-simple-format
1698          (concat (substring format (match-beginning 1) (match-end 1))
1699                  (substring format (match-beginning 2) (match-end 2))
1700                  (substring format (match-beginning 3) (match-end 3)))
1701          spec-alist))
1702     (gnus-parse-simple-format format spec-alist)))
1703
1704 (defun gnus-parse-simple-format (format spec-alist)
1705   ;; This function parses the FORMAT string with the help of the
1706   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1707   ;; string. The list will consist of the symbol `format', a format
1708   ;; specification string, and a list of forms depending on the
1709   ;; SPEC-ALIST.
1710   (let ((max-width 0)
1711         spec flist fstring b newspec max-width elem beg)
1712     (save-excursion
1713       (set-buffer (get-buffer-create " *gnus work*"))
1714       (buffer-disable-undo (current-buffer))
1715       (gnus-add-current-to-buffer-list)
1716       (erase-buffer)
1717       (insert format)
1718       (goto-char 1)
1719       (while (re-search-forward "%[-0-9]*\\(,[0-9]*\\)*\\(.\\)\\(.\\)?" nil t)
1720         (setq spec (string-to-char (buffer-substring (match-beginning 2)
1721                                                      (match-end 2))))
1722         ;; First check if there are any specs that look anything like
1723         ;; "%12,12A", ie. with a "max width specification". These have
1724         ;; to be treated specially.
1725         (if (setq beg (match-beginning 1))
1726             (setq max-width 
1727                   (string-to-int 
1728                    (buffer-substring (1+ (match-beginning 1)) (match-end 1))))
1729           (setq max-width 0)
1730           (setq beg (match-beginning 2)))
1731         ;; Find the specification from `spec-alist'.
1732         (if (not (setq elem (cdr (assq spec spec-alist))))
1733             (setq elem '("*" ?s)))
1734         ;; Treat user defined format specifiers specially
1735         (and (eq (car elem) 'user-defined)
1736              (setq elem
1737                    (list 
1738                     (list (intern (concat "gnus-user-format-function-"
1739                                           (buffer-substring
1740                                            (match-beginning 3)
1741                                            (match-end 3))))
1742                           'header)
1743                     ?s))
1744              (delete-region (match-beginning 3) (match-end 3)))
1745         (if (not (zerop max-width))
1746             (let ((el (car elem)))
1747               (cond ((= (car (cdr elem)) ?c) 
1748                      (setq el (list 'char-to-string el)))
1749                     ((= (car (cdr elem)) ?d)
1750                      (numberp el) (setq el (list 'int-to-string el))))
1751               (setq flist (cons (list 'gnus-format-max-width 
1752                                       el max-width) 
1753                                 flist))
1754               (setq newspec ?s))
1755           (setq flist (cons (car elem) flist))
1756           (setq newspec (car (cdr elem))))
1757         ;; Remove the old specification (and possibly a ",12" string).
1758         (delete-region beg (match-end 2))
1759         ;; Insert the new specification.
1760         (goto-char beg)
1761         (insert newspec))
1762       (setq fstring (buffer-substring 1 (point-max))))
1763     (cons 'format (cons fstring (nreverse flist)))))
1764
1765 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
1766 (defun gnus-read-init-file ()
1767   (and gnus-init-file
1768        (or (and (file-exists-p gnus-init-file) 
1769                 ;; Don't try to load a directory.
1770                 (not (file-directory-p gnus-init-file)))
1771            (file-exists-p (concat gnus-init-file ".el"))
1772            (file-exists-p (concat gnus-init-file ".elc")))
1773        (load gnus-init-file nil t)))
1774
1775 ;; Article file names when saving.
1776
1777 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
1778   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1779 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
1780 Otherwise, it is like ~/News/news/group/num."
1781   (let ((default
1782           (expand-file-name
1783            (concat (if (gnus-use-long-file-name 'not-save)
1784                        (gnus-capitalize-newsgroup newsgroup)
1785                      (gnus-newsgroup-directory-form newsgroup))
1786                    "/" (int-to-string (header-number headers)))
1787            (or gnus-article-save-directory "~/News"))))
1788     (if (and last-file
1789              (string-equal (file-name-directory default)
1790                            (file-name-directory last-file))
1791              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1792         default
1793       (or last-file default))))
1794
1795 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
1796   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1797 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group/num.
1798 Otherwise, it is like ~/News/news/group/num."
1799   (let ((default
1800           (expand-file-name
1801            (concat (if (gnus-use-long-file-name 'not-save)
1802                        newsgroup
1803                      (gnus-newsgroup-directory-form newsgroup))
1804                    "/" (int-to-string (header-number headers)))
1805            (or gnus-article-save-directory "~/News"))))
1806     (if (and last-file
1807              (string-equal (file-name-directory default)
1808                            (file-name-directory last-file))
1809              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1810         default
1811       (or last-file default))))
1812
1813 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
1814   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1815 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group.
1816 Otherwise, it is like ~/News/news/group/news."
1817   (or last-file
1818       (expand-file-name
1819        (if (gnus-use-long-file-name 'not-save)
1820            (gnus-capitalize-newsgroup newsgroup)
1821          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1822        (or gnus-article-save-directory "~/News"))))
1823
1824 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
1825   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1826 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group.
1827 Otherwise, it is like ~/News/news/group/news."
1828   (or last-file
1829       (expand-file-name
1830        (if (gnus-use-long-file-name 'not-save)
1831            newsgroup
1832          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1833        (or gnus-article-save-directory "~/News"))))
1834
1835 ;; For subscribing new newsgroup
1836
1837 (defun gnus-subscribe-hierarchical-interactive (groups)
1838   (let ((groups (sort groups 'string<))
1839         prefixes prefix start rest ans group starts)
1840     (while groups
1841       (setq prefixes (list "^"))
1842       (while (and groups prefixes)
1843         (while (not (string-match (car prefixes) (car groups)))
1844           (setq prefixes (cdr prefixes)))
1845         (setq prefix (car prefixes))
1846         (setq start (1- (length prefix)))
1847         (if (and (string-match "[^\\.]\\." (car groups) start)
1848                  (cdr groups)
1849                  (setq prefix 
1850                        (concat "^" (substring (car groups) 0 (match-end 0))))
1851                  (string-match prefix (car (cdr groups))))
1852             (progn
1853               (setq prefixes (cons prefix prefixes))
1854               (message "Descend hierarchy %s? ([y]nsq): " 
1855                        (substring prefix 1 (1- (length prefix))))
1856               (setq ans (read-char))
1857               (cond ((= ans ?n)
1858                      (while (and groups 
1859                                  (string-match prefix 
1860                                                (setq group (car groups))))
1861                        (setq gnus-killed-list 
1862                              (cons group gnus-killed-list))
1863                        (gnus-sethash group group gnus-killed-hashtb)
1864                        (setq groups (cdr groups)))
1865                      (setq starts (cdr starts)))
1866                     ((= ans ?s)
1867                      (while (and groups 
1868                                  (string-match prefix 
1869                                                (setq group (car groups))))
1870                        (gnus-sethash group group gnus-killed-hashtb)
1871                        (gnus-subscribe-alphabetically (car groups))
1872                        (setq groups (cdr groups)))
1873                      (setq starts (cdr starts)))
1874                     ((= ans ?q)
1875                      (while groups
1876                        (setq group (car groups))
1877                        (setq gnus-killed-list (cons group gnus-killed-list))
1878                        (gnus-sethash group group gnus-killed-hashtb)
1879                        (setq groups (cdr groups))))
1880                     (t nil)))
1881           (message "Subscribe %s? ([n]yq)" (car groups))
1882           (setq ans (read-char))
1883           (setq group (car groups))
1884           (cond ((= ans ?y)
1885                  (gnus-subscribe-alphabetically (car groups))
1886                  (gnus-sethash group group gnus-killed-hashtb))
1887                 ((= ans ?q)
1888                  (while groups
1889                    (setq group (car groups))
1890                    (setq gnus-killed-list (cons group gnus-killed-list))
1891                    (gnus-sethash group group gnus-killed-hashtb)
1892                    (setq groups (cdr groups))))
1893                 (t 
1894                  (setq gnus-killed-list (cons group gnus-killed-list))
1895                  (gnus-sethash group group gnus-killed-hashtb)))
1896           (setq groups (cdr groups)))))))
1897
1898 (defun gnus-subscribe-randomly (newsgroup)
1899   "Subscribe new NEWSGROUP by making it the first newsgroup."
1900   (gnus-subscribe-newsgroup newsgroup))
1901
1902 (defun gnus-subscribe-alphabetically (newgroup)
1903   "Subscribe new NEWSGROUP and insert it in alphabetical order."
1904   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
1905   (let ((groups (cdr gnus-newsrc-assoc))
1906         before)
1907     (while (and (not before) groups)
1908       (if (string< newgroup (car (car groups)))
1909           (setq before (car (car groups)))
1910         (setq groups (cdr groups))))
1911     (gnus-subscribe-newsgroup newgroup before)))
1912
1913 (defun gnus-subscribe-hierarchically (newgroup)
1914   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
1915   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
1916   (save-excursion
1917     (set-buffer (find-file-noselect gnus-current-startup-file))
1918     (let ((groupkey newgroup)
1919           before)
1920       (while (and (not before) groupkey)
1921         (goto-char (point-min))
1922         (let ((groupkey-re
1923                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
1924           (while (and (re-search-forward groupkey-re nil t)
1925                       (progn
1926                         (setq before (buffer-substring
1927                                       (match-beginning 1) (match-end 1)))
1928                         (string< before newgroup)))))
1929         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
1930         (setq groupkey
1931               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
1932                   (substring groupkey (match-beginning 1) (match-end 1)))))
1933       (gnus-subscribe-newsgroup newgroup before))))
1934
1935 (defun gnus-subscribe-interactively (newsgroup)
1936   "Subscribe new NEWSGROUP interactively.
1937 It is inserted in hierarchical newsgroup order if subscribed. If not,
1938 it is killed."
1939   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " newsgroup))
1940       (gnus-subscribe-hierarchically newsgroup)
1941     (setq gnus-killed-list (cons newsgroup gnus-killed-list))))
1942
1943 (defun gnus-subscribe-zombies (newsgroup)
1944   "Make new NEWSGROUP a zombie group."
1945   (setq gnus-zombie-list (cons newsgroup gnus-zombie-list)))
1946
1947 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
1948   "Subscribe new NEWSGROUP.
1949 If NEXT is non-nil, it is inserted before NEXT. Otherwise it is made
1950 the first newsgroup."
1951   ;; We subscribe the group by changing its level to 3.
1952   (gnus-group-change-level 
1953    newsgroup 3 9 (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
1954   (message "Subscribe newsgroup: %s" newsgroup))
1955
1956 ;; For directories
1957
1958 (defun gnus-newsgroup-directory-form (newsgroup)
1959   "Make hierarchical directory name from NEWSGROUP name."
1960   (let ((newsgroup (substring newsgroup 0)) ;Copy string.
1961         (len (length newsgroup))
1962         (idx 0))
1963     ;; Replace all occurrences of `.' with `/'.
1964     (while (< idx len)
1965       (if (= (aref newsgroup idx) ?.)
1966           (aset newsgroup idx ?/))
1967       (setq idx (1+ idx)))
1968     newsgroup
1969     ))
1970
1971 (defun gnus-make-directory (dir)
1972   "Make DIRECTORY recursively."
1973   (let* ((dir (expand-file-name dir default-directory))
1974          dirs)
1975     (if (string-match "/$" dir)
1976         (setq dir (substring dir 0 (match-beginning 0))))
1977     (while (not (file-exists-p dir))
1978       (setq dirs (cons dir dirs))
1979       (string-match "/[^/]+$" dir)
1980       (setq dir (substring dir 0 (match-beginning 0))))
1981     (while dirs
1982       (make-directory (car dirs))
1983       (setq dirs (cdr dirs)))))
1984
1985 (defun gnus-capitalize-newsgroup (newsgroup)
1986   "Capitalize NEWSGROUP name."
1987   (and (not (zerop (length newsgroup)))
1988        (concat (char-to-string (upcase (aref newsgroup 0)))
1989                (substring newsgroup 1))))
1990
1991 ;; Var
1992
1993 (defun gnus-simplify-subject (subject &optional re-only)
1994   "Remove `Re:' and words in parentheses.
1995 If optional argument RE-ONLY is non-nil, strip `Re:' only."
1996   (let ((case-fold-search t))           ;Ignore case.
1997     ;; Remove `Re:' and `Re^N:'.
1998     (if (string-match "^re:[ \t]*" subject)
1999         (setq subject (substring subject (match-end 0))))
2000     ;; Remove words in parentheses from end.
2001     (or re-only
2002         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2003           (setq subject (substring subject 0 (match-beginning 0)))))
2004     ;; Return subject string.
2005     subject))
2006
2007 (defun gnus-simplify-subject-fuzzy (subject)
2008   (let ((case-fold-search t))
2009     (and (string-match "^re:[ \t]*" subject)
2010          (setq subject (substring subject (match-end 0))))
2011     (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2012       (setq subject (substring subject 0 (match-beginning 0))))
2013     (let ((beg 0)
2014           (osubject ""))
2015       (while (string-match "[ \t]+" subject beg)
2016         (setq osubject
2017               (concat osubject (substring 
2018                                 subject beg (match-beginning 0)) " ")
2019               beg (match-end 0)))
2020       (setq osubject (concat osubject (substring subject beg)))
2021       (and (string-match " \\'" osubject) 
2022            (setq osubject (substring osubject 0 (match-beginning 0))))
2023       osubject)))
2024
2025 (defun gnus-add-current-to-buffer-list ()
2026   (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list)))
2027
2028 (defun gnus-string> (s1 s2)
2029   (not (or (string< s1 s2)
2030            (string= s1 s2))))
2031
2032 ;; Functions accessing headers.
2033 ;; Functions are more convenient than macros in some cases.
2034
2035 (defun gnus-header-number (header)
2036   "Return article number in HEADER."
2037   (header-number header))
2038
2039 (defun gnus-header-subject (header)
2040   "Return subject string in HEADER."
2041   (header-subject header))
2042
2043 (defun gnus-header-from (header)
2044   "Return author string in HEADER."
2045   (header-from header))
2046
2047 (defun gnus-header-xref (header)
2048   "Return xref string in HEADER."
2049   (header-xref header))
2050
2051 (defun gnus-header-lines (header)
2052   "Return lines in HEADER."
2053   (header-lines header))
2054
2055 (defun gnus-header-date (header)
2056   "Return date in HEADER."
2057   (header-date header))
2058
2059 (defun gnus-header-id (header)
2060   "Return Id in HEADER."
2061   (header-id header))
2062
2063 (defun gnus-header-references (header)
2064   "Return references in HEADER."
2065   (header-references header))
2066
2067 (defun gnus-clear-system ()
2068   "Clear all variables and buffers."
2069   ;; Clear Gnus variables.
2070   (let ((variables gnus-variable-list))
2071     (while variables
2072       (set (car variables) nil)
2073       (setq variables (cdr variables))))
2074   ;; Clear other internal variables.
2075   (setq gnus-list-of-killed-groups nil
2076         gnus-have-read-active-file nil
2077         gnus-newsrc-assoc nil
2078         gnus-newsrc-hashtb nil
2079         gnus-killed-list nil
2080         gnus-zombie-list nil
2081         gnus-killed-hashtb nil
2082         gnus-active-hashtb nil
2083         gnus-moderated-list nil
2084         gnus-description-hashtb nil
2085         gnus-newsgroup-headers nil
2086         gnus-score-cache nil
2087         gnus-newsgroup-headers-hashtb-by-number nil
2088         gnus-newsgroup-name nil
2089         gnus-internal-global-score-files nil
2090         gnus-current-select-method nil)
2091   ;; Kill the startup file.
2092   (and gnus-current-startup-file
2093        (get-file-buffer gnus-current-startup-file)
2094        (kill-buffer (get-file-buffer gnus-current-startup-file)))
2095   (gnus-dribble-clear)
2096   ;; Kill global KILL file buffer.
2097   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
2098       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
2099   ;; Kill Gnus buffers.
2100   (while gnus-buffer-list
2101     (if (and (get-buffer (car gnus-buffer-list))
2102              (buffer-name (get-buffer (car gnus-buffer-list))))
2103         (kill-buffer (car gnus-buffer-list)))
2104     (setq gnus-buffer-list (cdr gnus-buffer-list))))
2105
2106 (defun gnus-configure-windows (action &optional force)
2107   "Configure Gnus windows according to the next ACTION.
2108 The ACTION is either a symbol, such as `summary', or a
2109 configuration list such as `(1 1 2)'.  If ACTION is not a list,
2110 configuration list is got from the variable gnus-window-configuration.
2111 If FORCE is non-nil, the updating will be done whether it is necessary
2112 or not."
2113   (let* ((windows
2114           (if (listp action) action 
2115             (if (listp gnus-window-configuration)
2116                 (car (cdr (assq action gnus-window-configuration)))
2117               gnus-window-configuration)))
2118          (grpwin (get-buffer-window gnus-group-buffer))
2119          (subwin (get-buffer-window gnus-summary-buffer))
2120          (artwin (get-buffer-window gnus-article-buffer))
2121          (winsum nil)
2122          (height nil)
2123          (grpheight 0)
2124          (subheight 0)
2125          (artheight 0)
2126
2127          ;; Make split-window-vertically leave focus in upper window.
2128          (split-window-keep-point t))
2129     (if (and (symbolp windows) (fboundp windows))
2130         (funcall windows action)
2131       (if (and (not force)
2132                (or (null windows)               ;No configuration is specified.
2133                    (and (eq (null grpwin)
2134                             (zerop (nth 0 windows)))
2135                         (eq (null subwin)
2136                             (zerop (nth 1 windows)))
2137                         (eq (null artwin)
2138                             (zerop (nth 2 windows))))))
2139           ;; No need to change window configuration.
2140           nil
2141         (select-window (or grpwin subwin artwin (selected-window)))
2142         ;; First of all, compute the height of each window.
2143         (cond (gnus-use-full-window
2144                ;; Take up the entire screen.
2145                (delete-other-windows)
2146                (setq height (window-height (selected-window))))
2147               (t
2148                (setq height (+ (if grpwin (window-height grpwin) 0)
2149                                (if subwin (window-height subwin) 0)
2150                                (if artwin (window-height artwin) 0)))))
2151         ;; The group buffer exits always. So, use it to extend the
2152         ;; group window so as to get enough window space.
2153         (switch-to-buffer gnus-group-buffer 'norecord)
2154         (and (get-buffer gnus-summary-buffer)
2155              (delete-windows-on gnus-summary-buffer))
2156         (and (get-buffer gnus-article-buffer)
2157              (delete-windows-on gnus-article-buffer))
2158         ;; Compute expected window height.
2159         (setq winsum (apply (function +) windows))
2160         (if (not (zerop (nth 0 windows)))
2161             (setq grpheight (max window-min-height
2162                                  (/ (* height (nth 0 windows)) winsum))))
2163         (if (not (zerop (nth 1 windows)))
2164             (setq subheight (max window-min-height
2165                                  (/ (* height (nth 1 windows)) winsum))))
2166         (if (not (zerop (nth 2 windows)))
2167             (setq artheight (max window-min-height
2168                                  (/ (* height (nth 2 windows)) winsum))))
2169         (setq height (+ grpheight subheight artheight))
2170         (enlarge-window (max 0 (- height (window-height (selected-window)))))
2171         ;; Then split the window.
2172         (and (not (zerop artheight))
2173              (or (not (zerop grpheight))
2174                  (not (zerop subheight)))
2175              (split-window-vertically (+ grpheight subheight)))
2176         (and (not (zerop grpheight))
2177              (not (zerop subheight))
2178              (split-window-vertically grpheight))
2179         ;; Then select buffers in each window.
2180         (or (zerop grpheight)
2181             (progn
2182               (switch-to-buffer gnus-group-buffer 'norecord)
2183               (other-window 1)))
2184         (or (zerop subheight)
2185             (progn
2186               (switch-to-buffer gnus-summary-buffer 'norecord)
2187               (other-window 1)))
2188         (or (zerop artheight)
2189             (progn
2190               ;; If article buffer does not exist, it will be created
2191               ;; and initialized.
2192               (gnus-article-setup-buffer)
2193               (switch-to-buffer gnus-article-buffer 'norecord)
2194               (setq buffer-read-only t) ; !!! Why!?! 
2195               (bury-buffer gnus-summary-buffer)
2196               (bury-buffer gnus-group-buffer)))
2197         (or (zerop subheight)
2198             (progn
2199               (pop-to-buffer gnus-summary-buffer)
2200               ;; It seems that some code in this function will set
2201               ;; buffer-read-only to nil. I have absolutely no idea
2202               ;; why. 
2203               (setq buffer-read-only t))))))) ; !!! Why!?! 
2204
2205 (defun gnus-window-configuration-split (action)
2206   (switch-to-buffer gnus-group-buffer t)
2207   (delete-other-windows)
2208   (split-window-horizontally)
2209   (cond ((or (eq action 'newsgoups) (eq action 'summary))
2210          (if (and (get-buffer gnus-summary-buffer)
2211                   (buffer-name gnus-summary-buffer))
2212              (switch-to-buffer-other-window gnus-summary-buffer)))
2213         ((eq action 'article)
2214          (switch-to-buffer gnus-summary-buffer t)
2215          (other-window 1)
2216          (gnus-article-setup-buffer)
2217          (switch-to-buffer gnus-article-buffer t))))
2218
2219 (defun gnus-version ()
2220   "Version numbers of this version of Gnus."
2221   (interactive)
2222   (let ((methods gnus-valid-select-methods)
2223         (mess gnus-version)
2224         meth)
2225     ;; Go through all the legal select methods and add their version
2226     ;; numbers to the total version string. Only the backends that are
2227     ;; currently in use will have their message numbers taken into
2228     ;; consideration. 
2229     (while methods
2230       (setq meth (intern (concat (car (car methods)) "-version")))
2231       (and (boundp meth)
2232            (stringp (symbol-value meth))
2233            (setq mess (concat mess "; " (symbol-value meth))))
2234       (setq methods (cdr methods)))
2235     (message mess)))
2236
2237 (defun gnus-info-find-node ()
2238   "Find Info documentation of Gnus."
2239   (interactive)
2240   ;; Enlarge info window if needed.
2241   (cond ((eq major-mode 'gnus-group-mode)
2242          (gnus-configure-windows '(1 0 0)) ;Take all windows.
2243          (pop-to-buffer gnus-group-buffer))
2244         ((eq major-mode 'gnus-summary-mode)
2245          (gnus-configure-windows '(0 1 0)) ;Take all windows.
2246          (pop-to-buffer gnus-summary-buffer)))
2247   (Info-goto-node (car (cdr (assq major-mode gnus-info-nodes)))))
2248
2249 (defun gnus-bug ()
2250   "Send a bug report to the Gnus maintainers."
2251   (interactive)
2252   (pop-to-buffer "*Gnus Bug*")
2253   (erase-buffer)
2254   (mail-setup gnus-maintainer "[Gnus Bug Report] " nil nil nil nil)
2255   (goto-char (point-min))
2256   (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
2257   (forward-line 1)
2258   (insert (format "%s\n%s\n\n" (gnus-version) (emacs-version)))
2259   (gnus-debug)
2260   (mail-mode)
2261   (message ""))
2262
2263 (defun gnus-debug ()
2264   "Attemps to go through the Gnus source file and report what variables have been changed.
2265 The source file has to be in the Emacs load path."
2266   (interactive)
2267   (let ((dirs load-path)
2268         file expr olist)
2269     (while dirs
2270       (if (file-exists-p (setq file (concat (car dirs) "/gnus.el")))
2271           (save-excursion
2272             (setq dirs nil)
2273             (set-buffer (get-buffer-create "*gnus bug info*"))
2274             (buffer-disable-undo (current-buffer))
2275             (erase-buffer)
2276             (insert-file-contents file)
2277             (goto-char (point-min))
2278             (or (search-forward "\n;; Internal variables" nil t)
2279                 (error "Malformed sources"))
2280             (narrow-to-region (point-min) (point))
2281             (goto-char (point-min))
2282             (while (setq expr (condition-case () 
2283                                   (read (current-buffer)) (error nil)))
2284               (and (eq (car expr) 'defvar)
2285                    (stringp (nth 3 expr))
2286                    (not (equal (eval (nth 2 expr))
2287                                (and (boundp (nth 1 expr))
2288                                     (symbol-value (nth 1 expr)))))
2289                    (setq olist (cons (nth 1 expr) olist))))
2290             (kill-buffer (current-buffer)))
2291         (setq dirs (cdr dirs))))
2292     (while olist
2293       (insert "(setq " (symbol-name (car olist)) " '" 
2294               (prin1-to-string (symbol-value (car olist))) ")\n")
2295       (setq olist (cdr olist)))
2296     (insert "\n\n")))
2297
2298 (defun gnus-overload-functions (&optional overloads)
2299   "Overload functions specified by optional argument OVERLOADS.
2300 If nothing is specified, use the variable gnus-overload-functions."
2301   (let ((defs nil)
2302         (overloads (or overloads gnus-overload-functions)))
2303     (while overloads
2304       (setq defs (car overloads))
2305       (setq overloads (cdr overloads))
2306       ;; Load file before overloading function if necessary.  Make
2307       ;; sure we cannot use `require' always.
2308       (and (not (fboundp (car defs)))
2309            (car (cdr (cdr defs)))
2310            (load (car (cdr (cdr defs))) nil 'nomessage))
2311       (fset (car defs) (car (cdr defs))))))
2312
2313 (defun gnus-replace-chars-in-string (string from to)
2314   "Replace characters in STRING from FROM to TO."
2315   (let ((string (substring string 0))   ;Copy string.
2316         (len (length string))
2317         (idx 0))
2318     ;; Replace all occurrences of FROM with TO.
2319     (while (< idx len)
2320       (if (= (aref string idx) from)
2321           (aset string idx to))
2322       (setq idx (1+ idx)))
2323     string))
2324
2325 (defun gnus-days-between (date1 date2)
2326   ;; Return the number of days between date1 and date2.
2327   (let ((d1 (mapcar (lambda (s) (and s (string-to-int s)) )
2328                     (timezone-parse-date date1)))
2329         (d2 (mapcar (lambda (s) (and s (string-to-int s)) )
2330                     (timezone-parse-date date2))))
2331     (- (timezone-absolute-from-gregorian 
2332         (nth 1 d1) (nth 2 d1) (car d1))
2333        (timezone-absolute-from-gregorian 
2334         (nth 1 d2) (nth 2 d2) (car d2)))))
2335
2336 (defun gnus-day-number (date)
2337   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
2338                      (timezone-parse-date date))))
2339     (timezone-absolute-from-gregorian 
2340      (nth 1 dat) (nth 2 dat) (car dat))))
2341
2342 (defun gnus-file-newer-than (file date)
2343   (let ((fdate (nth 5 (file-attributes file))))
2344     (or (> (car fdate) (car date))
2345         (and (= (car fdate) (car date))
2346              (> (nth 1 fdate) (nth 1 date))))))
2347
2348 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
2349 ;; the echo area.
2350 (defun gnus-y-or-n-p (prompt)
2351   (prog1
2352       (y-or-n-p prompt)
2353     (message "")))
2354
2355 (defun gnus-yes-or-no-p (prompt)
2356   (prog1
2357       (yes-or-no-p prompt)
2358     (message "")))
2359
2360 ;; Return a string of length POS+1 representing NUMber in BASE. The
2361 ;; resulting string will be left padded with zeds.
2362 (defun gnus-number-base-x (num pos base)
2363   (if (< pos 0)
2364       ""
2365     (concat 
2366      (char-to-string
2367       (aref "zyxwvutsrqponmlkjihgfedcba9876543210" (/ num (expt base pos))))
2368      (gnus-number-base-x 
2369       (% num (expt base pos)) (1- pos) base))))
2370
2371 ;; Check whether to use long file names.
2372 (defun gnus-use-long-file-name (symbol)
2373   (and gnus-use-long-file-name
2374        (or (not (listp gnus-use-long-file-name))
2375            (not (memq 'symbol gnus-use-long-file-name)))))
2376
2377 ;; List and range functions
2378
2379 (defun gnus-last-element (list)
2380   "Return last element of LIST."
2381   (while (cdr list)
2382     (setq list (cdr list)))
2383   (car list))
2384
2385 (defun gnus-copy-sequence (list)
2386   "Do a complete, total copy of a list."
2387   (mapcar (lambda (elem) (if (consp elem) 
2388                              (if (consp (cdr elem))
2389                                  (gnus-copy-sequence elem)
2390                                (cons (car elem) (cdr elem)))
2391                            elem))
2392           list))
2393
2394 (defun gnus-set-difference (list1 list2)
2395   "Return a list of elements of LIST1 that do not appear in LIST2."
2396   (let ((list1 (copy-sequence list1)))
2397     (while list2
2398       (setq list1 (delq (car list2) list1))
2399       (setq list2 (cdr list2)))
2400     list1))
2401
2402 (defun gnus-sorted-complement (list1 list2)
2403   "Return a list of elements of LIST1 that do not appear in LIST2.
2404 Both lists have to be sorted over <."
2405   (let (out)
2406     (while (and list1 list2)
2407       (cond ((= (car list1) (car list2))
2408              (setq list1 (cdr list1)
2409                    list2 (cdr list2)))
2410             ((< (car list1) (car list2))
2411              (setq out (cons (car list1) out))
2412              (setq list1 (cdr list1)))
2413             (t
2414              (setq out (cons (car list2) out))
2415              (setq list2 (cdr list2)))))
2416     (nreverse (append (or list1 list2) out))))
2417
2418 (defun gnus-intersection (list1 list2)      
2419   (let ((result nil))
2420     (while list2
2421       (if (memq (car list2) list1)
2422           (setq result (cons (car list2) result)))
2423       (setq list2 (cdr list2)))
2424     result))
2425
2426 (defun gnus-sorted-intersection (list1 list2)
2427   ;; LIST1 and LIST2 have to be sorted over <.
2428   (let (out)
2429     (while (and list1 list2)
2430       (cond ((= (car list1) (car list2))
2431              (setq out (cons (car list1) out)
2432                    list1 (cdr list1)
2433                    list2 (cdr list2)))
2434             ((< (car list1) (car list2))
2435              (setq list1 (cdr list1)))
2436             (t
2437              (setq list2 (cdr list2)))))
2438     (nreverse out)))
2439
2440 (defun gnus-set-sorted-intersection (list1 list2)
2441   ;; LIST1 and LIST2 have to be sorted over <.
2442   ;; This function modifies LIST1.
2443   (let* ((top (cons nil list1))
2444          (prev top))
2445   (while (and list1 list2)
2446     (cond ((= (car list1) (car list2))
2447            (setq prev list1
2448                  list1 (cdr list1)
2449                  list2 (cdr list2)))
2450           ((< (car list1) (car list2))
2451            (setcdr prev (cdr list1))
2452            (setq list1 (cdr list1)))
2453           (t
2454            (setq list2 (cdr list2)))))
2455   (setcdr prev nil)
2456   (cdr top)))
2457
2458 (defun gnus-compress-sequence (numbers &optional always-list)
2459   "Convert list of numbers to a list of ranges or a single range.
2460 If ALWAYS-LIST is non-nil, this function will always release a list of
2461 ranges."
2462   (let* ((first (car numbers))
2463          (last (car numbers))
2464          result)
2465     (if (null numbers)
2466         nil
2467       (if (not (listp (cdr numbers)))
2468           numbers
2469         (while numbers
2470           (cond ((= last (car numbers)) nil) ;Omit duplicated number
2471                 ((= (1+ last) (car numbers)) ;Still in sequence
2472                  (setq last (car numbers)))
2473                 (t                      ;End of one sequence
2474                  (setq result 
2475                        (cons (if (= first last) first (cons first last)) result))
2476                  (setq first (car numbers))
2477                  (setq last  (car numbers))))
2478           (setq numbers (cdr numbers)))
2479         (if (and (not always-list) (null result))
2480             (if (= first last) first (cons first last))
2481           (nreverse (cons (if (= first last) first (cons first last))
2482                           result)))))))
2483
2484 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
2485 (defun gnus-uncompress-range (ranges)
2486   "Expand a list of ranges into a list of numbers.
2487 RANGES is either a single range on the form `(num . num)' or a list of
2488 these ranges."
2489   (let (first last result)
2490     (cond 
2491      ((null ranges)
2492       nil)
2493      ((not (listp (cdr ranges)))
2494       (setq first (car ranges))
2495       (setq last (cdr ranges))
2496       (while (<= first last)
2497         (setq result (cons first result))
2498         (setq first (1+ first)))
2499       (nreverse result))
2500      (t
2501       (while ranges
2502         (if (atom (car ranges))
2503             (if (numberp (car ranges))
2504                 (setq result (cons (car ranges) result)))
2505           (setq first (car (car ranges)))
2506           (setq last  (cdr (car ranges)))
2507           (while (<= first last)
2508             (setq result (cons first result))
2509             (setq first (1+ first))))
2510         (setq ranges (cdr ranges)))
2511       (nreverse result)))))
2512
2513 (defun gnus-add-to-range (ranges list)
2514   "Return a list of ranges that has all articles from both RANGES and LIST.
2515 Note: LIST has to be sorted over `<'."
2516   (if (not ranges)
2517       (gnus-compress-sequence list t)
2518     (setq list (copy-sequence list))
2519     (or (listp (cdr ranges))
2520         (setq ranges (list ranges)))
2521     (let ((out ranges)
2522           ilist lowest highest temp)
2523       (while (and ranges list)
2524         (setq ilist list)
2525         (setq lowest (or (and (atom (car ranges)) (car ranges))
2526                          (car (car ranges))))
2527         (while (and list (cdr list) (< (car (cdr list)) lowest))
2528           (setq list (cdr list)))
2529         (if (< (car ilist) lowest)
2530             (progn
2531               (setq temp list)
2532               (setq list (cdr list))
2533               (setcdr temp nil)
2534               (setq out (nconc (gnus-compress-sequence ilist t) out))))
2535         (setq highest (or (and (atom (car ranges)) (car ranges))
2536                           (cdr (car ranges))))
2537         (while (and list (<= (car list) highest))
2538           (setq list (cdr list)))
2539         (setq ranges (cdr ranges)))
2540       (if list
2541           (setq out (nconc (gnus-compress-sequence list t) out)))
2542       (setq out (sort out (lambda (r1 r2) 
2543                             (< (or (and (atom r1) r1) (car r1))
2544                                (or (and (atom r2) r2) (car r2))))))
2545       (setq ranges out)
2546       (while ranges
2547         (if (atom (car ranges))
2548             (if (cdr ranges)
2549                 (if (atom (car (cdr ranges)))
2550                     (if (= (1+ (car ranges)) (car (cdr ranges)))
2551                         (progn
2552                           (setcar ranges (cons (car ranges) 
2553                                                (car (cdr ranges))))
2554                           (setcdr ranges (cdr (cdr ranges)))))
2555                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
2556                       (progn
2557                         (setcar (car (cdr ranges)) (car ranges))
2558                         (setcar ranges (car (cdr ranges)))
2559                         (setcdr ranges (cdr (cdr ranges)))))))
2560           (if (cdr ranges)
2561               (if (atom (car (cdr ranges)))
2562                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
2563                       (progn
2564                         (setcdr (car ranges) (car (cdr ranges)))
2565                         (setcdr ranges (cdr (cdr ranges)))))
2566                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
2567                     (progn
2568                       (setcdr (car ranges) (cdr (car (cdr ranges))))
2569                       (setcdr ranges (cdr (cdr ranges))))))))
2570         (setq ranges (cdr ranges)))
2571       out)))
2572
2573 (defun gnus-add-to-range-new (ranges list)
2574   (gnus-compress-sequence 
2575    (sort (nconc (gnus-uncompress-range ranges) list) '<)))     
2576
2577 (defun gnus-add-to-range-old (ranges list)
2578   "Return a list of ranges that has all articles from both RANGES and LIST.
2579 Note: LIST has to be sorted over `<'."
2580   (let* ((ranges (if (and ranges (not (listp (cdr ranges))))
2581                      (list ranges) ranges))
2582          (inrange ranges)
2583          did-one
2584          range nranges first last)
2585     (if (not list)
2586         ranges
2587       (if (not ranges)
2588           (gnus-compress-sequence list t)
2589         (and ranges 
2590              (> (car (car ranges)) 1)
2591              (progn
2592                (setq did-one t)
2593                (setq inrange (setq ranges (cons (cons 1 1) ranges)))))
2594         (while (and ranges list)
2595           (setq range (car ranges))
2596           (while (and list (>= (car list) (car range))
2597                       (<= (car list) (cdr range)))
2598             (setq list (cdr list)))
2599           (while (and list (= (1- (car list)) (cdr range)))
2600             (setcdr range (car list))
2601             (setq list (cdr list)))
2602           (if (and list (and (> (car list) (cdr range)) 
2603                              (cdr ranges)
2604                              (< (car list) (car (car (cdr ranges))))))
2605               (setcdr ranges (cons (cons (car list) (car list)) (cdr ranges))))
2606           (setq ranges (cdr ranges)))
2607         (if (and list (not ranges))
2608             (setq inrange (nconc inrange (gnus-compress-sequence list t))))
2609         (if did-one
2610             (if (eq (cdr (car inrange)) 1)
2611                 (setq inrange (cdr inrange))
2612               (setcar (car inrange) 2)))
2613         (setq ranges inrange)
2614         (while ranges
2615           (if (and (cdr ranges) (>= (1+ (cdr (car ranges)))
2616                                     (car (car (cdr ranges)))))
2617               (progn
2618                 (setcdr (car ranges) (cdr (car (cdr ranges))))
2619                 (setcdr ranges (cdr (cdr ranges))))
2620             (setq ranges (cdr ranges))))
2621         (if (not (cdr inrange))
2622             (car inrange)
2623           inrange)))))
2624
2625 (defun gnus-remove-from-range (ranges list)
2626   "Return a list of ranges that has all articles from LIST removed from RANGES.
2627 Note: LIST has to be sorted over `<'."
2628   ;; !!! This function shouldn't look like this, but I've got a headache.
2629   (gnus-compress-sequence 
2630    (gnus-sorted-complement
2631     (gnus-uncompress-range ranges) list)))
2632
2633 (defun gnus-member-of-range (number ranges)
2634   (if (not (listp (cdr ranges)))
2635       (and (>= number (car ranges)) 
2636            (<= number (cdr ranges)))
2637     (let ((not-stop t))
2638       (while (and ranges 
2639                   (if (numberp (car ranges))
2640                       (>= number (car ranges))
2641                     (>= number (car (car ranges))))
2642                   not-stop)
2643         (if (if (numberp (car ranges))
2644                 (= number (car ranges))
2645               (and (>= number (car (car ranges)))
2646                    (<= number (cdr (car ranges)))))
2647             (setq not-stop nil))
2648         (setq ranges (cdr ranges)))
2649       (not not-stop))))
2650
2651 \f
2652 ;;;
2653 ;;; Gnus group mode
2654 ;;;
2655
2656 (defvar gnus-group-mode-map nil)
2657 (defvar gnus-group-make-map nil)
2658 (defvar gnus-group-list-map nil)
2659 (defvar gnus-group-sub-map nil)
2660 (put 'gnus-group-mode 'mode-class 'special)
2661
2662 (if gnus-group-mode-map
2663     nil
2664   (setq gnus-group-mode-map (make-keymap))
2665   (suppress-keymap gnus-group-mode-map)
2666   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
2667   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
2668   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
2669   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
2670   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
2671   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
2672   (define-key gnus-group-mode-map "\177" 'gnus-group-prev-unread-group)
2673   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
2674   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
2675   (define-key gnus-group-mode-map "\M-n" 'gnus-group-next-unread-group-same-level)
2676   (define-key gnus-group-mode-map "\M-p" 'gnus-group-prev-unread-group-same-level)
2677   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
2678   (define-key gnus-group-mode-map "." 'gnus-group-first-unread-group)
2679   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
2680   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
2681   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
2682   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
2683   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
2684   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
2685   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
2686   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
2687   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
2688   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
2689   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
2690   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
2691   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
2692   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
2693   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
2694   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
2695   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
2696   (define-key gnus-group-mode-map "\C-c\M-C-a" 'gnus-group-description-apropos)
2697   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
2698   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
2699   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
2700   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
2701   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
2702   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
2703   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
2704   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
2705   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
2706   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
2707   (define-key gnus-group-mode-map "V" 'gnus-version)
2708   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
2709   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
2710   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
2711   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
2712   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
2713   (define-key gnus-group-mode-map "\M-f" 'gnus-group-fetch-faq)
2714   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
2715   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
2716   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group)
2717   (define-key gnus-group-mode-map
2718     (if gnus-xemacs [button2] [mouse-2]) 'gnus-mouse-pick-group)
2719
2720   (define-prefix-command 'gnus-group-make-map)
2721   (define-key gnus-group-mode-map "M" 'gnus-group-make-map)
2722   (define-key gnus-group-make-map "d" 'gnus-group-make-directory-group)
2723   (define-key gnus-group-make-map "h" 'gnus-group-make-help-group)
2724   (define-key gnus-group-make-map "a" 'gnus-group-make-archive-group)
2725   (define-key gnus-group-make-map "k" 'gnus-group-make-kiboze-group)
2726   (define-key gnus-group-make-map "m" 'gnus-group-make-group)
2727   (define-key gnus-group-make-map "e" 'gnus-group-edit-group)
2728
2729   (define-prefix-command 'gnus-group-list-map)
2730   (define-key gnus-group-mode-map "G" 'gnus-group-list-map)
2731   (define-key gnus-group-list-map "k" 'gnus-group-list-killed)
2732   (define-key gnus-group-list-map "z" 'gnus-group-list-zombies)
2733   (define-key gnus-group-list-map "s" 'gnus-group-list-groups)
2734   (define-key gnus-group-list-map "u" 'gnus-group-list-all-groups)
2735   (define-key gnus-group-list-map "a" 'gnus-group-apropos)
2736   (define-key gnus-group-list-map "d" 'gnus-group-description-apropos)
2737   (define-key gnus-group-list-map "m" 'gnus-group-list-matching)
2738   (define-key gnus-group-list-map "M" 'gnus-group-list-all-matching)
2739
2740   (define-prefix-command 'gnus-group-sub-map)
2741   (define-key gnus-group-mode-map "S" 'gnus-group-sub-map)
2742   (define-key gnus-group-sub-map "l" 'gnus-group-set-current-level)
2743   (define-key gnus-group-sub-map "t" 'gnus-group-unsubscribe-current-group)
2744   (define-key gnus-group-sub-map "s" 'gnus-group-unsubscribe-group)
2745   (define-key gnus-group-sub-map "k" 'gnus-group-kill-group)
2746   (define-key gnus-group-sub-map "y" 'gnus-group-yank-group)
2747   (define-key gnus-group-sub-map "w" 'gnus-group-kill-region)
2748   (define-key gnus-group-sub-map "z" 'gnus-group-kill-all-zombies))
2749
2750 (defun gnus-group-mode ()
2751   "Major mode for reading news.
2752
2753 All normal editing commands are switched off.
2754 \\<gnus-group-mode-map>
2755 The group buffer lists (some of) the groups available.  For instance,
2756 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
2757 lists all zombie groups. 
2758
2759 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe 
2760 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'. 
2761
2762 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
2763
2764 The following commands are available:
2765
2766 \\{gnus-group-mode-map}"
2767   (interactive)
2768   (if gnus-visual (gnus-group-make-menu-bar))
2769   (kill-all-local-variables)
2770   (setq mode-line-modified "-- ")
2771   (make-local-variable 'mode-line-format)
2772   (setq mode-line-format (copy-sequence mode-line-format))
2773   (and (equal (nth 3 mode-line-format) "   ")
2774        (setcar (nthcdr 3 mode-line-format) ""))
2775   (setq major-mode 'gnus-group-mode)
2776   (setq mode-name "Group")
2777   (gnus-group-set-mode-line)
2778   (setq mode-line-process nil)
2779   (use-local-map gnus-group-mode-map)
2780   (buffer-disable-undo (current-buffer))
2781   (setq truncate-lines t)
2782   (setq buffer-read-only t)
2783   (run-hooks 'gnus-group-mode-hook))
2784
2785 (defun gnus-mouse-pick-group (e)
2786   (interactive "e")
2787   (mouse-set-point e)
2788   (gnus-group-read-group nil))
2789
2790 ;;;###autoload
2791 (defun gnus-no-server (&optional arg)
2792   "Read network news.
2793 If ARG is a positive number, Gnus will use that as the
2794 startup level. If ARG is nil, Gnus will be started at level 2. 
2795 If ARG is non-nil and not a positive number, Gnus will
2796 prompt the user for the name of an NNTP server to use.
2797 As opposed to `gnus', this command will not connect to the local server."
2798   (interactive "P")
2799   (gnus (or arg 2) t))
2800
2801 (defalias '\(ding\) 'gnus)
2802
2803 ;;;###autoload
2804 (defun gnus (&optional arg dont-connect)
2805   "Read network news.
2806 If ARG is non-nil and a positive number, Gnus will use that as the
2807 startup level. If ARG is non-nil and not a positive number, Gnus will
2808 prompt the user for the name of an NNTP server to use."
2809   (interactive "P")
2810   (if (get-buffer gnus-group-buffer)
2811       (progn
2812         (switch-to-buffer gnus-group-buffer)
2813         (gnus-group-get-new-news))
2814     (gnus-clear-system)
2815     (gnus-read-init-file)
2816     (let ((level (and arg (numberp arg) (> arg 0) arg))
2817           did-connect)
2818       (unwind-protect
2819           (progn
2820             (switch-to-buffer (get-buffer-create gnus-group-buffer))
2821             (gnus-add-current-to-buffer-list)
2822             (gnus-group-mode)
2823             (or dont-connect 
2824                 (setq did-connect
2825                       (gnus-start-news-server (and arg (not level))))))
2826         (if (and (not dont-connect) 
2827                  (not did-connect))
2828             (gnus-group-quit)
2829           (run-hooks 'gnus-startup-hook)
2830           ;; NNTP server is successfully open. 
2831           (gnus-update-format-specifications)
2832           (let ((buffer-read-only nil))
2833             (erase-buffer)
2834             (if (not gnus-inhibit-startup-message)
2835                 (progn
2836                   (gnus-group-startup-message)
2837                   (sit-for 0))))
2838           (gnus-setup-news nil level)
2839           (and gnus-use-dribble-file (gnus-dribble-open))
2840           (or t (not gnus-novice-user)
2841               gnus-expert-user
2842               (gnus-group-describe-briefly)) ;Show brief help message.
2843           (gnus-group-list-groups level))))))
2844
2845 (defun gnus-group-startup-message (&optional x y)
2846   "Insert startup message in current buffer."
2847   ;; Insert the message.
2848   (erase-buffer)
2849   (insert
2850    (format "
2851     %s
2852            A newsreader 
2853       for GNU Emacs
2854
2855         Based on GNUS 
2856              written by 
2857      Masanobu UMEDA
2858
2859     Lars Magne 
2860          Ingebrigtsen 
2861       larsi@ifi.uio.no
2862
2863            gnus-version))
2864   ;; And then hack it.
2865   ;; 18 is the longest line.
2866   (indent-rigidly (point-min) (point-max) 
2867                   (/ (max (- (window-width) (or x 28)) 0) 2))
2868   (goto-char (point-min))
2869   ;; +4 is fuzzy factor.
2870   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2)))
2871
2872 (defun gnus-group-setup-buffer ()
2873   (or (get-buffer gnus-group-buffer)
2874       (progn
2875         (switch-to-buffer (get-buffer-create gnus-group-buffer))
2876         (gnus-add-current-to-buffer-list)
2877         (gnus-group-mode))))
2878
2879 (defun gnus-group-list-groups (level &optional unread)
2880   "List newsgroups with level LEVEL or lower that have unread alticles.
2881 Default is 5, which lists all subscribed groups.
2882 If argument UNREAD is non-nil, groups with no unread articles are also listed."
2883   (interactive "P")
2884   (setq level (or level gnus-group-default-list-level 5))
2885   (gnus-group-setup-buffer)     ;May call from out of group buffer
2886   (let ((case-fold-search nil)
2887         (group (gnus-group-group-name)))
2888     (funcall gnus-group-prepare-function level unread nil)
2889     (if (zerop (buffer-size))
2890         (message gnus-no-groups-message)
2891       (goto-char (point-min))
2892       (if (not group)
2893           ;; Go to the first group with unread articles.
2894           (gnus-group-search-forward nil nil nil t)
2895         ;; Find the right group to put point on. If the current group
2896         ;; has disapeared in the new listing, try to find the next
2897         ;; one. If no next one can be found, just leave point at the
2898         ;; first newsgroup in the buffer.
2899         (if (not (gnus-goto-char
2900                   (text-property-any (point-min) (point-max) 
2901                                      'gnus-group (intern group))))
2902             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
2903               (while (and newsrc
2904                           (not (gnus-goto-char 
2905                                 (text-property-any 
2906                                  (point-min) (point-max) 'gnus-group 
2907                                  (intern group)))))
2908                 (setq newsrc (cdr newsrc))))))
2909       ;; Adjust cursor point.
2910       (gnus-group-position-cursor))))
2911
2912 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
2913   "List all newsgroups with unread articles of level LEVEL or lower.
2914 If ALL is non-nil, list groups that have no unread articles.
2915 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
2916 If REGEXP, only list groups matching REGEXP."
2917   (set-buffer gnus-group-buffer)
2918   (let ((buffer-read-only nil)
2919         (newsrc (cdr gnus-newsrc-assoc))
2920         (lowest (or lowest 1))
2921         info clevel unread group)
2922     (erase-buffer)
2923     (if (< lowest 8)
2924         ;; List living groups.
2925         (while newsrc
2926           (setq info (car newsrc)
2927                 group (car info)
2928                 newsrc (cdr newsrc)
2929                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
2930           (and unread ; This group might be bogus
2931                (or (not regexp)
2932                    (string-match regexp group))
2933                (<= (setq clevel (car (cdr info))) level) 
2934                (>= clevel lowest)
2935                (or all            ; We list all groups?
2936                    (eq unread t)  ; We list unactivated groups
2937                    (> unread 0)   ; We list groups with unread articles
2938                    (cdr (assq 'tick (nth 3 info)))) ; And groups with tickeds
2939                (gnus-group-insert-group-line 
2940                 nil group (car (cdr info)) (nth 3 info) unread (nth 4 info)))))
2941
2942     ;; List dead groups.
2943     (and (>= level 8) (<= lowest 8)
2944          (gnus-group-prepare-flat-list-dead 
2945           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 8 ?Z
2946           regexp))
2947     (and (>= level 9) (<= lowest 9)
2948          (gnus-group-prepare-flat-list-dead 
2949           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 9 ?K
2950           regexp))
2951
2952     (gnus-group-set-mode-line)
2953     (setq gnus-have-all-newsgroups all)
2954     (run-hooks 'gnus-group-prepare-hook)))
2955
2956 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
2957   ;; List zombies and killed lists somehwat faster, which was
2958   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>. It does
2959   ;; this by ignoring the group format specification altogether.
2960   (let (group beg)
2961     (while groups
2962       (setq group (car groups)
2963             groups (cdr groups))
2964       (if (or (not regexp)
2965               (string-match regexp group))
2966           (progn
2967             (setq beg (point))
2968             (insert (format " %c    *: %s\n" mark group))
2969             (add-text-properties 
2970              beg (1+ beg) 
2971              (list 'gnus-group (intern group)
2972                    'gnus-unread t
2973                    'gnus-level level)))))))
2974
2975 (defun gnus-group-real-name (group)
2976   "Find the real name of a foreign newsgroup."
2977   (if (string-match "^[^:]+:" group)
2978       (substring group (match-end 0))
2979     group))
2980
2981 (defun gnus-group-prefixed-name (group method)
2982   "Return the whole name from GROUP and METHOD."
2983   (concat (format "%s" (car method))
2984           (if (assoc (format "%s" (car method)) (gnus-methods-using 'address))
2985               (concat "+" (nth 1 method)))
2986           ":" group))
2987
2988 (defun gnus-group-real-prefix (group)
2989   "Return the prefix of the current group name."
2990   (if (string-match "^[^:]+:" group)
2991       (substring group 0 (match-end 0))
2992     ""))
2993
2994 (defun gnus-group-method-name (group)
2995   "Return the method used for selecting GROUP."
2996   (let ((prefix (gnus-group-real-prefix group)))
2997     (if (equal prefix "")
2998         gnus-select-method
2999       (if (string-match "^[^\\+]+\\+" prefix)
3000           (list (intern (substring prefix 0 (1- (match-end 0))))
3001                 (substring prefix (match-end 0) (1- (length prefix))))
3002         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
3003
3004 (defun gnus-group-foreign-p (group)
3005   "Return nil if GROUP is native, non-nil if it is foreign."
3006   (string-match ":" group))
3007
3008 (defun gnus-group-set-info (info)
3009   (let ((entry (gnus-gethash (car info) gnus-newsrc-hashtb)))
3010     (let (marked)
3011       (if (not (setq marked (nth 3 info)))
3012           ()
3013         (while marked
3014           (or (eq 'score (car (car marked)))
3015               (eq 'bookmark (car (car marked)))
3016               (eq 'killed (car (car marked)))
3017               (setcdr (car marked) 
3018                       (gnus-uncompress-range (cdr (car marked)))))
3019           (setq marked (cdr marked)))))
3020     (if entry
3021         ()
3022       (save-excursion
3023         (set-buffer gnus-group-buffer)
3024         (if (nth 4 info)
3025             (gnus-group-make-group 
3026              (gnus-group-real-name (car info))
3027              (prin1-to-string (car (nth 4 info)))
3028              (nth 1 (nth 4 info)))
3029           (gnus-group-make-group
3030            (car info)
3031            (prin1-to-string (car gnus-select-method))
3032            (nth 1 gnus-select-method)))
3033         (message "Note: New group created")
3034         (setq entry 
3035               (gnus-gethash (gnus-group-prefixed-name 
3036                              (gnus-group-real-name (car info))
3037                              (or (nth 4 info) gnus-select-method))
3038                             gnus-newsrc-hashtb))))
3039     (if entry
3040         (progn
3041           (setcar (nthcdr 2 entry) info)
3042           (if (and (not (eq (car entry) t)) 
3043                    (gnus-gethash (car info) gnus-active-hashtb))
3044               (let ((marked (nth 3 info)))
3045                 (setcar entry 
3046                         (max 0 (- (length (gnus-list-of-unread-articles 
3047                                            (car info)))
3048                                   (length (cdr (assq 'tick marked)))
3049                                   (length (cdr (assq 'dormant marked)))))))))
3050       (error "No such group: %s" (car info)))))
3051
3052 (defun gnus-group-update-group-line ()
3053   "This function updates the current line in the newsgroup buffer and
3054 moves the point to the colon."
3055   (let* ((buffer-read-only nil)
3056          (group (gnus-group-group-name))
3057          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
3058     (if entry
3059         (gnus-dribble-enter 
3060          (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3061                  ")")))
3062     (beginning-of-line)
3063     (delete-region (point) (save-excursion (forward-line 1) (point)))
3064     (gnus-group-insert-group-line-info group)
3065     (forward-line -1)
3066     (gnus-group-position-cursor)))
3067
3068 (defun gnus-group-insert-group-line-info (group)
3069   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
3070         active info)
3071     (if entry
3072         (progn
3073           (setq info (nth 2 entry))
3074           (gnus-group-insert-group-line 
3075            nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
3076       (setq active (gnus-gethash group gnus-active-hashtb))
3077       (gnus-group-insert-group-line 
3078        nil group (if (member group gnus-zombie-list) 8 9)
3079        nil (if active (- (1+ (cdr active)) (car active)) 0) nil))))
3080
3081 (defun gnus-group-insert-group-line (gformat group level marked number method)
3082   (let* ((gformat (or gformat gnus-group-line-format-spec))
3083          (active (gnus-gethash group gnus-active-hashtb))
3084          (number-total (if active (1+ (- (cdr active) (car active))) 0))
3085          (number-of-dormant (length (cdr (assq 'dormant marked))))
3086          (number-of-ticked (length (cdr (assq 'tick marked))))
3087          (number-of-ticked-and-dormant
3088           (+ number-of-ticked number-of-dormant))
3089          (number-of-unread-unticked 
3090           (if (numberp number) (int-to-string (max 0 number))
3091             "*"))
3092          (number-of-read
3093           (if (numberp number)
3094               (max 0 (- number-total number))
3095             "*"))
3096          (subscribed (cond ((< level 6) ? )
3097                            ((< level 8) ?U)
3098                            ((= level 8) ?Z)
3099                            (t ?K)))
3100          (qualified-group (gnus-group-real-name group))
3101          (newsgroup-description 
3102           (if gnus-description-hashtb
3103               (or (gnus-gethash group gnus-description-hashtb) "")
3104             ""))
3105          (moderated (if (member group gnus-moderated-list) ?m ? ))
3106          (moderated-string (if (eq moderated ?m) "(m)" ""))
3107          (news-server (or (car (cdr method)) ""))
3108          (news-method (or (car method) ""))
3109          (news-method-string 
3110           (if method (format "(%s:%s)" (car method) (car (cdr method))) ""))
3111          (marked (if (and 
3112                       (numberp number) 
3113                       (zerop number)
3114                       (> number-of-ticked 0))
3115                      ?* ? ))
3116          (number (if (eq number t) "*" (+ number number-of-dormant 
3117                                           number-of-ticked)))
3118          (buffer-read-only nil)
3119          b)
3120     (beginning-of-line)
3121     (setq b (point))
3122     ;; Insert the text.
3123     (insert (eval gformat))
3124
3125     (add-text-properties 
3126      b (1+ b) (list 'gnus-group (intern group)
3127                     'gnus-unread (if (numberp number)
3128                                      (string-to-int number-of-unread-unticked)
3129                                    t)
3130                     'gnus-marked marked
3131                     'gnus-level level))))
3132
3133 (defun gnus-group-update-group (group &optional visible-only)
3134   "Update newsgroup info of GROUP.
3135 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't already."
3136   (save-excursion
3137     (set-buffer gnus-group-buffer)
3138     (let ((buffer-read-only nil)
3139           visible)
3140       (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
3141         (if entry
3142             (gnus-dribble-enter 
3143              (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3144                      ")"))))
3145       ;; Buffer may be narrowed.
3146       (save-restriction
3147         (widen)
3148         ;; Search a line to modify.  If the buffer is large, the search
3149         ;; takes long time.  In most cases, current point is on the line
3150         ;; we are looking for.  So, first of all, check current line. 
3151         (if (or (progn
3152                   (beginning-of-line)
3153                   (eq (get-text-property (point) 'gnus-group)
3154                       (intern group)))
3155                 (progn
3156                   (gnus-goto-char 
3157                    (text-property-any 
3158                     (point-min) (point-max) 'gnus-group (intern group)))))
3159             ;; GROUP is listed in current buffer. So, delete old line.
3160             (progn
3161               (setq visible t)
3162               (beginning-of-line)
3163               (delete-region (point) (progn (forward-line 1) (point))))
3164           ;; No such line in the buffer, find out where it's supposed to
3165           ;; go, and insert it there (or at the end of the buffer).
3166           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
3167           (or visible-only
3168               (let ((entry (cdr (gnus-gethash group gnus-newsrc-hashtb))))
3169                 (while (and entry
3170                             (not
3171                              (gnus-goto-char
3172                               (text-property-any
3173                                (point-min) (point-max) 
3174                                'gnus-group (intern (car (car entry)))))))
3175                   (setq entry (cdr entry)))
3176                 (or entry (goto-char (point-max)))))))
3177       (if (or visible (not visible-only))
3178           (gnus-group-insert-group-line-info group))
3179       (gnus-group-set-mode-line))))
3180
3181 (defun gnus-group-set-mode-line ()
3182   (if (memq 'group gnus-updated-mode-lines)
3183       (let* ((gformat (or gnus-group-mode-line-format-spec
3184                           (setq gnus-group-mode-line-format-spec
3185                                 (gnus-parse-format 
3186                                  gnus-group-mode-line-format 
3187                                  gnus-group-mode-line-format-alist))))
3188              (news-server (car (cdr gnus-select-method)))
3189              (news-method (car gnus-select-method))
3190              (mode-string (eval gformat))
3191              (max-len 60))
3192         (if (> (length mode-string) max-len) 
3193             (setq mode-string (substring mode-string 0 (- max-len 4))))
3194         (setq mode-line-buffer-identification mode-string)
3195         (set-buffer-modified-p t))))
3196
3197 (defun gnus-group-group-name ()
3198   "Get the name of the newsgroup on the current line."
3199   (let ((group (get-text-property 
3200                 (save-excursion (beginning-of-line) (point)) 'gnus-group)))
3201     (and group (symbol-name group))))
3202
3203 (defun gnus-group-group-level ()
3204   "Get the level of the newsgroup on the current line."
3205   (get-text-property (save-excursion (beginning-of-line) (point)) 'gnus-level))
3206
3207 (defun gnus-group-search-forward (&optional backward all level first-too)
3208   "Find the next newsgroup with unread articles.
3209 If BACKWARD is non-nil, find the previous newsgroup instead.
3210 If ALL is non-nil, just find any newsgroup.
3211 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
3212 group exists.
3213 If FIRST-TOO, the current line is also eligeble as a target."
3214   (let ((way (if backward -1 1))
3215         (low 10)
3216         (beg (point))
3217         pos found)
3218     (or first-too (forward-line way))
3219     (while (and 
3220             (not (eobp))
3221             (not (setq 
3222                   found 
3223                   (and (or all
3224                            (and
3225                             (let ((unread 
3226                                    (get-text-property (point) 'gnus-unread)))
3227                               (or (eq unread t) (and unread (> unread 0))))
3228                             (let ((lev (get-text-property (point) 'gnus-level)))
3229                               (and lev (< (get-text-property (point) 'gnus-level) 6)))))
3230                        (or (not level)
3231                            (let ((lev (get-text-property (point) 'gnus-level)))
3232                              (if (and lev (<= lev level))
3233                                  t
3234                                (if (< lev low)
3235                                    (progn
3236                                      (setq low lev)
3237                                      (setq pos (point))))
3238                                nil))))))
3239             (zerop (forward-line way))))
3240     (if found 
3241         (progn (gnus-group-position-cursor) t)
3242       (if pos (goto-char pos) (goto-char beg))
3243       nil)))
3244
3245 ;; Gnus group mode commands
3246
3247 (defun gnus-group-read-group (all &optional no-article group)
3248   "Read news in this newsgroup.
3249 If argument ALL is non-nil, already read articles become readable.
3250 If optional argument NO-ARTICLE is non-nil, no article body is displayed."
3251   (interactive "P")
3252   (let ((group (or group (gnus-group-group-name)))
3253         number active marked entry)
3254     (or group (error "No group on current line"))
3255     (setq marked 
3256           (nth 3 (nth 2 (setq entry (gnus-gethash group gnus-newsrc-hashtb)))))
3257     ;; This group might be a dead group. In that case we have to get
3258     ;; the number of unread articles from `gnus-active-hashtb'.
3259     (if entry
3260         (setq number (car entry))
3261       (if (setq active (gnus-gethash group gnus-active-hashtb))
3262           (setq number (- (1+ (cdr active)) (car active)))))
3263     (gnus-summary-read-group 
3264      group (or all (and (numberp number) 
3265                         (zerop (+ number (length (cdr (assq 'tick marked)))
3266                                   (length (cdr (assq 'dormant marked)))))))
3267      no-article)))
3268
3269 (defun gnus-group-select-group (all)
3270   "Select this newsgroup.
3271 No article is selected automatically.
3272 If argument ALL is non-nil, already read articles become readable."
3273   (interactive "P")
3274   (gnus-group-read-group all t))
3275
3276 (defun gnus-group-jump-to-group (group)
3277   "Jump to newsgroup GROUP."
3278   (interactive (list (completing-read "Group: " gnus-active-hashtb nil
3279                                       (not (not gnus-read-active-file)))))
3280
3281   (if (equal group "")
3282       (error "empty group name"))
3283
3284   (let ((b (text-property-any (point-min) (point-max) 
3285                               'gnus-group (intern group))))
3286     (if b
3287         ;; Either go to the line in the group buffer...
3288         (goto-char b)
3289       ;; ... or insert the line.
3290       (or
3291        (gnus-gethash group gnus-active-hashtb)
3292        (gnus-activate-newsgroup group)
3293        (error "%s error: %s" group (gnus-status-message group)))
3294
3295       (gnus-group-update-group group)
3296       (goto-char (text-property-any (point-min) (point-max) 
3297                                     'gnus-group (intern group)))))
3298   ;; Adjust cursor point.
3299   (gnus-group-position-cursor))
3300
3301 (defun gnus-group-next-group (n)
3302   "Go to next N'th newsgroup.
3303 If N is negative, search backward instead.
3304 Returns the difference between N and the number of skips actually
3305 done."
3306   (interactive "p")
3307   (gnus-group-next-unread-group n t))
3308
3309 (defun gnus-group-next-unread-group (n &optional all level)
3310   "Go to next N'th unread newsgroup.
3311 If N is negative, search backward instead.
3312 If ALL is non-nil, choose any newsgroup, unread or not.
3313 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
3314 such group can be found, the next group with a level higher than
3315 LEVEL.
3316 Returns the difference between N and the number of skips actually
3317 made."
3318   (interactive "p")
3319   (let ((backward (< n 0))
3320         (n (abs n)))
3321     (while (and (> n 0)
3322                 (gnus-group-search-forward backward all level))
3323       (setq n (1- n)))
3324     (if (/= 0 n) (message "No more%s newsgroups%s" (if all "" " unread")
3325                           (if level " on this level or higher" "")))
3326     n))
3327
3328 (defun gnus-group-prev-group (n)
3329   "Go to previous N'th newsgroup.
3330 Returns the difference between N and the number of skips actually
3331 done."
3332   (interactive "p")
3333   (gnus-group-next-unread-group (- n) t))
3334
3335 (defun gnus-group-prev-unread-group (n)
3336   "Go to previous N'th unread newsgroup.
3337 Returns the difference between N and the number of skips actually
3338 done."  
3339   (interactive "p")
3340   (gnus-group-next-unread-group (- n)))
3341
3342 (defun gnus-group-next-unread-group-same-level (n)
3343   "Go to next N'th unread newsgroup on the same level.
3344 If N is negative, search backward instead.
3345 Returns the difference between N and the number of skips actually
3346 done."
3347   (interactive "p")
3348   (gnus-group-next-unread-group n t (gnus-group-group-level))
3349   (gnus-group-position-cursor))
3350
3351 (defun gnus-group-prev-unread-group-same-level (n)
3352   "Go to next N'th unread newsgroup on the same level.
3353 Returns the difference between N and the number of skips actually
3354 done."
3355   (interactive "p")
3356   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
3357   (gnus-group-position-cursor))
3358
3359 (defun gnus-group-best-unread-group (&optional exclude-group)
3360   "Go to the group with the highest level.
3361 If EXCLUDE-GROUP, do not go to that group."
3362   (interactive)
3363   (goto-char (point-min))
3364   (let ((best 10)
3365         unread best-point)
3366     (while (setq unread (get-text-property (point) 'gnus-unread))
3367       (if (and (numberp unread) (> unread 0))
3368           (progn
3369             (or best-point (setq best-point (point)))
3370             (if (and (< (get-text-property (point) 'gnus-level) best)
3371                      (or (not exclude-group)
3372                          (not (equal exclude-group (gnus-group-group-name)))))
3373                 (progn 
3374                   (setq best (get-text-property (point) 'gnus-level))
3375                   (setq best-point (point))))))
3376       (forward-line 1))
3377     (if best-point (goto-char best-point))
3378     (gnus-summary-position-cursor)
3379     (and best-point (gnus-group-group-name))))
3380
3381 (defun gnus-group-first-unread-group ()
3382   "Go to the first group with unread articles."
3383   (interactive)
3384   (goto-char (point-min))
3385   (gnus-group-next-unread-group 1))
3386
3387 (defun gnus-group-make-group (name method address)
3388   "Add a new newsgroup.
3389 The user will be prompted for a NAME, for a select METHOD, and an
3390 ADDRESS."
3391   (interactive
3392    (cons 
3393     (read-string "Group name: ")
3394     (let ((method
3395            (completing-read 
3396             "Method: " gnus-valid-select-methods nil t)))
3397       (list method
3398             (if (memq 'prompt-address
3399                       (assoc method gnus-valid-select-methods))
3400                 (read-string "Address: ")
3401               "")))))
3402   (let ((nname (gnus-group-prefixed-name name (list (intern method) address)))
3403         info)
3404     (and (gnus-gethash nname gnus-active-hashtb)
3405          (error "Group %s already exists" nname))
3406     (gnus-group-change-level 
3407      (setq info (list t nname 3 nil nil (list (intern method) address)))
3408      3 9 (gnus-gethash (or (gnus-group-group-name) "dummy.group")
3409                        gnus-newsrc-hashtb) t)
3410     (gnus-sethash nname '(0 . 0) gnus-active-hashtb)
3411     (gnus-dribble-enter 
3412      (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")"))
3413     (gnus-group-insert-group-line-info nname)
3414
3415     (and (gnus-check-backend-function 'request-create-group nname)
3416          (gnus-request-create-group nname))))
3417
3418 (defun gnus-group-edit-group (group)
3419   (interactive (list (gnus-group-group-name)))
3420   (let (info)
3421     (if group (setq info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
3422       (error "No group on current line"))
3423     (setq gnus-winconf-edit-group (current-window-configuration))
3424     (pop-to-buffer (get-buffer-create gnus-group-edit-buffer))
3425     (gnus-add-current-to-buffer-list)
3426     (emacs-lisp-mode)
3427     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3428     (use-local-map (copy-keymap emacs-lisp-mode-map))
3429     (local-set-key "\C-c\C-c" 'gnus-group-edit-group-done)
3430     (erase-buffer)
3431     (insert ";; Type `C-c C-c' after you have edited the newsgroup entry.\n\n")
3432     (let ((cinfo (gnus-copy-sequence info))
3433           marked)
3434       (if (not (setq marked (nth 3 cinfo)))
3435           ()
3436         (while marked
3437           (or (eq 'score (car (car marked)))
3438               (eq 'bookmark (car (car marked)))
3439               (eq 'killed (car (car marked)))
3440               (setcdr (car marked) 
3441                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
3442           (setq marked (cdr marked))))
3443       (insert (pp-to-string (list 'gnus-group-set-info
3444                                   (list 'quote cinfo)))))))
3445
3446 (defun gnus-group-edit-group-done ()
3447   (interactive)
3448   (set-buffer (get-buffer-create gnus-group-edit-buffer))
3449   (eval-current-buffer)
3450   (kill-buffer (current-buffer))
3451   (and gnus-winconf-edit-group
3452        (set-window-configuration gnus-winconf-edit-group))
3453   (setq gnus-winconf-edit-group nil)
3454   (set-buffer gnus-group-buffer)
3455   (gnus-group-update-group (gnus-group-group-name))
3456   (gnus-group-position-cursor))
3457
3458 (defun gnus-group-make-help-group ()
3459   "Create the (ding) Gnus documentation group."
3460   (interactive)
3461   (and (gnus-gethash (gnus-group-prefixed-name "gnus-help" '(nndoc ""))
3462                      gnus-newsrc-hashtb)
3463        (error "Documentation group already exists"))
3464   (let ((path load-path))
3465     (while (and path
3466                 (not (file-exists-p (concat (file-name-as-directory (car path))
3467                                             "doc.txt"))))
3468       (setq path (cdr path)))
3469     (or path (error "Couldn't find doc group"))
3470     (gnus-group-make-group 
3471      "gnus-help" "nndoc" 
3472      (concat (file-name-as-directory (car path)) "doc.txt"))
3473     (gnus-group-position-cursor)))
3474
3475 (defun gnus-group-make-archive-group ()
3476   "Create the (ding) Gnus archive group."
3477   (interactive)
3478   (and (gnus-gethash (gnus-group-prefixed-name "ding.archives" '(nndir ""))
3479                      gnus-newsrc-hashtb)
3480        (error "Archive group already exists"))
3481   (gnus-group-make-group "ding.archives" "nndir" gnus-group-archive-directory)
3482   (gnus-group-position-cursor))
3483
3484 (defun gnus-group-make-directory-group (dir)
3485   "Create an nndir group.
3486 The user will be prompted for a directory. The contents of this
3487 directory will be used as a newsgroup. The directory should contain
3488 mail messages or news articles in files that have numeric names."
3489   (interactive
3490    (list (read-file-name "Create group from directory: ")))
3491   (or (file-exists-p dir) (error "No such directory"))
3492   (or (file-directory-p dir) (error "Not a directory"))
3493   (gnus-group-make-group dir "nndir" dir)
3494   (gnus-group-position-cursor))
3495
3496 (defun gnus-group-make-kiboze-group (group address scores)
3497   "Create an nnkiboze group.
3498 The user will be prompted for a name, a regexp to match groups, and
3499 score file entries for articles to include in the group."
3500   (interactive
3501    (list
3502     (read-string "nnkiboze group name: ")
3503     (read-string "Source groups (regexp): ")
3504     (let ((headers (mapcar (lambda (group) (list group))
3505                            '("subject" "from" "number" "date" "message-id"
3506                              "references" "chars" "lines" "xref")))
3507           scores header regexp regexps)
3508       (while (not (equal "" (setq header (completing-read 
3509                                           "Match on header: " headers nil t))))
3510         (setq regexps nil)
3511         (while (not (equal "" (setq regexp (read-string 
3512                                             (format "Match on %s (string): "
3513                                                     header)))))
3514           (setq regexps (cons (list regexp nil 1000 nil) regexps)))
3515         (setq scores (cons (cons header regexps) scores)))
3516       (car scores))))
3517   (gnus-group-make-group group "nnkiboze" address)
3518   (save-excursion
3519     (set-buffer (get-buffer-create " *gnus work*"))
3520     (buffer-disable-undo (current-buffer))
3521     (let (emacs-lisp-mode-hook)
3522       (pp (list 'setq 'gnus-score-alist 
3523                 (list 'quote (list scores)))
3524           (current-buffer)))
3525     (write-region (point-min) (point-max) 
3526                   (concat (or gnus-kill-files-directory "~/News")
3527                           "nnkiboze:" group "." gnus-score-file-suffix))
3528     (kill-buffer (current-buffer)))
3529   (gnus-group-position-cursor))
3530
3531 ;; Group sorting commands
3532 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
3533
3534 (defun gnus-group-sort-groups ()
3535   "Sort the group buffer using `gnus-group-sort-function'."
3536   (interactive)
3537   (setq gnus-newsrc-assoc 
3538         (sort (cdr gnus-newsrc-assoc) gnus-group-sort-function))
3539   (gnus-make-hashtable-from-newsrc-alist)
3540   (gnus-get-unread-articles 6)
3541   (gnus-group-list-groups nil))
3542
3543 (defun gnus-group-sort-by-alphabet (info1 info2)
3544   (string< (car info1) (car info2)))
3545
3546 (defun gnus-group-sort-by-unread (info1 info2)
3547   (let ((n1 (car (gnus-gethash (car info1) gnus-newsrc-hashtb)))
3548         (n2 (car (gnus-gethash (car info2) gnus-newsrc-hashtb))))
3549     (< (or (and (numberp n1) n1) 0)
3550        (or (and (numberp n2) n2) 0))))
3551
3552 (defun gnus-group-sort-by-level (info1 info2)
3553   (< (nth 1 info1) (nth 1 info2)))
3554
3555 ;; Group catching up.
3556
3557 (defun gnus-group-catchup-current (n &optional all)
3558   "Mark all articles not marked as unread in current newsgroup as read.
3559 If prefix argument N is numeric, the ARG next newsgroups will be
3560 caught up. If ALL is non-nil, marked articles will also be marked as
3561 read. Cross references (Xref: header) of articles are ignored.
3562 The difference between N and actual number of newsgroups that were
3563 caught up is returned."
3564   (interactive "p")
3565   (if (or (not gnus-interactive-catchup) ;Without confirmation?
3566           gnus-expert-user
3567           (gnus-y-or-n-p
3568            (if all
3569                "Do you really want to mark all articles as read? "
3570              "Mark all unread articles as read? ")))
3571       (progn
3572         (while 
3573             (and (> n 0)
3574                  (progn
3575                    (setq n (1- n))
3576                    (gnus-group-catchup (gnus-group-group-name) all)
3577                    (gnus-group-update-group-line)
3578                    t)
3579                  (zerop (gnus-group-next-unread-group 1))))))
3580   n)
3581
3582 (defun gnus-group-catchup-current-all (n)
3583   "Mark all articles in current newsgroup as read.
3584 Cross references (Xref: header) of articles are ignored."
3585   (interactive "p")
3586   (gnus-group-catchup-current n 'all))
3587
3588 (defun gnus-group-catchup (group &optional all)
3589   "Mark all articles in GROUP as read.
3590 If ALL is non-nil, all articles are marked as read.
3591 The return value is the number of articles that were marked as read,
3592 or nil if no action could be taken."
3593   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
3594          (num (car entry))
3595          (marked (nth 3 (nth 2 entry)))
3596          ticked)
3597     (if (not (numberp (car entry)))
3598         (message "Can't catch up; non-active group")
3599       ;; Do the updating only if the newsgroup isn't killed
3600       (if entry
3601           (progn
3602             (setq ticked (if all nil (cdr (assq 'tick marked))))
3603             (gnus-update-read-articles group ticked nil ticked)
3604             (if (and all marked)
3605                 (setcar (nthcdr 3 (nth 2 entry)) 
3606                         (delq (assq 'dormant marked) marked))))))
3607     num))
3608
3609 (defun gnus-group-expire-articles (newsgroup)
3610   "Expire all expirable articles in the current newsgroup."
3611   (interactive (list (gnus-group-group-name)))
3612   (if (not newsgroup) (error "No current newsgroup"))
3613   (let ((expirable 
3614          (assq 'expire (nth 3 (nth 2 (gnus-gethash newsgroup 
3615                                                    gnus-newsrc-hashtb))))))
3616  (and expirable 
3617       (gnus-check-backend-function 'request-expire-articles newsgroup)
3618       (setcdr expirable
3619               (gnus-request-expire-articles (cdr expirable) newsgroup)))))
3620
3621 (defun gnus-group-expire-all-groups ()
3622   "Expire all expirable articles in all newsgroups."
3623   (interactive)
3624   (message "Expiring...")
3625   (let ((newsrc (cdr gnus-newsrc-assoc)))
3626     (while newsrc
3627       (gnus-group-expire-articles (car (car newsrc)))
3628       (setq newsrc (cdr newsrc))))
3629   (message "Expiring...done"))
3630
3631 (defun gnus-group-set-current-level (n)
3632   "Set the level of the current group to the numeric prefix."
3633   (interactive "P")
3634   (setq n (or n (string-to-int 
3635                  (completing-read 
3636                   "Level: " 
3637                   (mapcar (lambda (n) (list (char-to-string n))) "123456789")
3638                   nil t))))
3639   (let ((group (gnus-group-group-name)))
3640     (if (not group) (error "No newsgroup on current line.")
3641     (if (and (numberp n) (>= n 1) (<= n 9))
3642         (progn
3643           (message "Changed level of %s from %d to %d" 
3644                    group (gnus-group-group-level) n)
3645           (gnus-group-change-level group n (gnus-group-group-level))
3646           (gnus-group-update-group-line))
3647       (error "Illegal level: %s" n))))
3648   (forward-line 1)
3649   (gnus-group-position-cursor))
3650
3651 (defun gnus-group-unsubscribe-current-group (arg)
3652   "Toggle subscribe from/to unsubscribe current group."
3653   (interactive "P")
3654   (let ((group (gnus-group-group-name)))
3655     (or group (error "No newsgroup on current line"))
3656     (or arg (setq arg (if (<= (gnus-group-group-level) 5) 6 3)))
3657     (gnus-group-unsubscribe-group group arg)
3658     (gnus-group-next-group 1)))
3659
3660 (defun gnus-group-unsubscribe-group (group &optional level)
3661   "Toggle subscribe from/to unsubscribe GROUP.
3662 New newsgroup is added to .newsrc automatically."
3663   (interactive
3664    (list (completing-read "Group: " gnus-active-hashtb nil 
3665                           gnus-have-read-active-file)))
3666   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
3667     (cond (newsrc
3668            ;; Toggle subscription flag.
3669            (gnus-group-change-level 
3670             newsrc (if level level (if (< (nth 1 (nth 2 newsrc)) 6) 6 4)))
3671            (gnus-group-update-group group))
3672           ((and (stringp group)
3673                 (or (not gnus-have-read-active-file)
3674                     (gnus-gethash group gnus-active-hashtb)))
3675            ;; Add new newsgroup.
3676            (gnus-group-change-level 
3677             group 
3678             (if level level 3) 
3679             (or (and (member group gnus-zombie-list) 8) 9)
3680             (or (and (gnus-group-group-name)
3681                      (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb))
3682                 (gnus-gethash (car (car gnus-newsrc-assoc)) 
3683                               gnus-newsrc-hashtb)))
3684            (gnus-group-update-group group))
3685           (t (error "No such newsgroup: %s" group)))
3686     (gnus-group-position-cursor)))
3687
3688 (defun gnus-group-transpose-groups (arg)
3689   "Exchange current newsgroup and previous newsgroup.
3690 With argument ARG, takes previous newsgroup and moves it past ARG newsgroup."
3691   (interactive "p")
3692   ;; BUG: last newsgroup and the last but one cannot be transposed
3693   ;; since gnus-group-search-forward does not move forward beyond the
3694   ;; last.  If we instead use forward-line, no problem, but I don't
3695   ;; want to use it for later extension.
3696   (while (> arg 0)
3697     (gnus-group-search-forward t t)
3698     (gnus-group-kill-group 1)
3699     (gnus-group-search-forward nil t)
3700     (gnus-group-yank-group)
3701     (gnus-group-search-forward nil t)
3702     (setq arg (1- arg))))
3703
3704 (defun gnus-group-kill-all-zombies ()
3705   "Kill all zombie newsgroups."
3706   (interactive)
3707   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
3708   (setq gnus-zombie-list nil)
3709   (funcall gnus-group-prepare-function 5 nil nil)
3710   (goto-char (point-min))
3711   (gnus-group-position-cursor))
3712
3713 (defun gnus-group-kill-region (begin end)
3714   "Kill newsgroups in current region (excluding current point).
3715 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
3716   (interactive "r")
3717   (let ((lines
3718          ;; Exclude a line where current point is on.
3719          (1-
3720           ;; Count lines.
3721           (save-excursion
3722             (count-lines
3723              (progn
3724                (goto-char begin)
3725                (beginning-of-line)
3726                (point))
3727              (progn
3728                (goto-char end)
3729                (end-of-line)
3730                (point)))))))
3731     (goto-char begin)
3732     (beginning-of-line)                 ;Important when LINES < 1
3733     (gnus-group-kill-group lines)))
3734
3735 (defun gnus-group-kill-group (n)
3736   "The the next N groups.
3737 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
3738 However, only groups that were alive can be yanked; already killed 
3739 groups or zombie groups can't be yanked.
3740 The return value is the name of the (last) newsgroup that was killed."
3741   (interactive "p")
3742   (let ((buffer-read-only nil)
3743         group entry level)
3744     (while (>= (setq n  (1- n)) 0)
3745       (setq group (gnus-group-group-name))
3746       (or group
3747           (signal 'end-of-buffer nil))
3748       (setq level (gnus-group-group-level))
3749       (beginning-of-line)
3750       (delete-region (point) (progn (forward-line 1) (point)))
3751       (if (setq entry (gnus-gethash group gnus-newsrc-hashtb))
3752           (setq gnus-list-of-killed-groups 
3753                 (cons (cons (car entry) (nth 2 entry)) 
3754                       gnus-list-of-killed-groups)))
3755       (gnus-group-change-level (if entry entry group) 9 (if entry nil level)))
3756     (if (eobp)
3757         (forward-line -1))
3758     (gnus-group-position-cursor)
3759     group))
3760
3761 (defun gnus-group-yank-group (&optional arg)
3762   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
3763 inserting it before the current newsgroup.  The numeric ARG specifies
3764 how many newsgroups are to be yanked.  The name of the (last)
3765 newsgroup yanked is returned."
3766   (interactive "p")
3767   (if (not arg) (setq arg 1))
3768   (let (info group prev)
3769     (while (>= (setq arg (1- arg)) 0)
3770       (if (not (setq info (car gnus-list-of-killed-groups)))
3771           (error "No more newsgroups to yank"))
3772       (setq group (nth 2 info))
3773       ;; Find which newsgroup to insert this one before - search
3774       ;; backward until something suitable is found. If there are no
3775       ;; other newsgroups in this buffer, just make this newsgroup the
3776       ;; first newsgroup.
3777       (while (and (not (setq prev (gnus-group-group-name)))
3778                   (zerop (forward-line -1))))
3779       (if (not prev)
3780           (setq prev (car (car gnus-newsrc-assoc))))
3781       (gnus-group-change-level 
3782        info (nth 2 info) 9 
3783        (gnus-gethash prev gnus-newsrc-hashtb)
3784        t)
3785       (gnus-group-insert-group-line-info (nth 1 info))
3786       (setq gnus-list-of-killed-groups 
3787             (cdr gnus-list-of-killed-groups)))
3788     (forward-line -1)
3789     (gnus-group-position-cursor)
3790     group))
3791       
3792 (defun gnus-group-list-all-groups (arg)
3793   "List all newsgroups with level ARG or lower.
3794 Default is 7, which lists all subscribed and most unsubscribed groups."
3795   (interactive "P")
3796   (setq arg (or arg 7))
3797   (gnus-group-list-groups arg t))
3798
3799 (defun gnus-group-list-killed ()
3800   "List all killed newsgroups in the group buffer."
3801   (interactive)
3802   (if (not gnus-killed-list)
3803       (message "No killed groups")
3804     (funcall gnus-group-prepare-function 9 t 9)
3805     (goto-char (point-min)))
3806   (gnus-group-position-cursor))
3807
3808 (defun gnus-group-list-zombies ()
3809   "List all zombie newsgroups in the group buffer."
3810   (interactive)
3811   (if (not gnus-zombie-list)
3812       (message "No zombie groups")
3813     (funcall gnus-group-prepare-function 8 t 8)
3814     (goto-char (point-min)))
3815   (gnus-group-position-cursor))
3816
3817 (defun gnus-group-get-new-news (&optional arg)
3818   "Get newly arrived articles.
3819 If ARG is non-nil, it should be a number between one and nine to
3820 specify which levels you are interested in re-scanning."
3821   (interactive "P")
3822   (run-hooks 'gnus-get-new-news-hook)
3823   (if (and gnus-read-active-file (not arg))
3824       (progn
3825         (gnus-read-active-file)
3826         (gnus-get-unread-articles (or arg 6)))
3827     (let ((gnus-read-active-file nil))
3828       (gnus-get-unread-articles (or arg 6))))
3829   (gnus-group-list-groups nil gnus-have-all-newsgroups))
3830
3831 (defun gnus-group-get-new-news-this-group (n)
3832   "Check for newly arrived news in the current group (and the N-1 next groups).
3833 The difference between N and the number of newsgroup checked is returned.
3834 If N is negative, this group and the N-1 previous groups will be checked."
3835   (interactive "p")
3836   (let ((way (if (< n 0) -1 1))
3837         (n (abs n))
3838         (w-p (window-start))
3839         group)
3840     (while (and (> n 0)
3841                 (progn
3842                   (or (gnus-get-new-news-in-group
3843                        (setq group (gnus-group-group-name)))
3844                       (progn 
3845                         (ding) 
3846                         (message "%s error: %s" 
3847                                  group (gnus-status-message group))))
3848                   t)
3849                 (zerop (gnus-group-next-group way)))
3850       (setq n (1- n)))
3851     (if (/= 0 n) (message "No more newsgroups"))
3852     ;; !!! I don't know why the buffer scrolls forward when updating
3853     ;; the first line in the group buffer, but it does. So we set the
3854     ;; window start forcibly.
3855     (set-window-start (get-buffer-window (current-buffer)) w-p)
3856     n))
3857
3858 (defun gnus-get-new-news-in-group (group)
3859   (and group 
3860        (gnus-activate-newsgroup group)
3861        (progn
3862          (gnus-get-unread-articles-in-group 
3863           (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
3864           (gnus-gethash group gnus-active-hashtb))
3865          (gnus-group-update-group-line)
3866          t)))
3867
3868 (defun gnus-group-fetch-faq (group)
3869   "Fetch the FAQ for the current group."
3870   (interactive (list (gnus-group-group-name)))
3871   (or group (error "No group name given"))
3872   (let ((file (concat gnus-group-faq-directory group))) 
3873     (if (not (file-exists-p file))
3874         (error "No such file: %s" file)
3875       (find-file file))))
3876   
3877 (defun gnus-group-describe-group (force &optional group)
3878   "Display a description of the current newsgroup."
3879   (interactive "P")
3880   (and force (setq gnus-description-hashtb nil))
3881   (let ((group (or group (gnus-group-group-name)))
3882         desc)
3883     (or group (message "No group name given"))
3884     (and (or gnus-description-hashtb
3885              (setq desc (gnus-group-get-description group))
3886              (gnus-read-descriptions-file))
3887          (message
3888           (or desc (gnus-gethash group gnus-description-hashtb)
3889               "No description available")))))
3890
3891 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
3892 (defun gnus-group-describe-all-groups (force)
3893   "Pop up a buffer with descriptons of all newsgroups."
3894   (interactive "P")
3895   (and force (setq gnus-description-hashtb nil))
3896   (if (not (or gnus-description-hashtb
3897                (gnus-read-descriptions-file)))
3898       (error "Couldn't request descriptions file"))
3899   (let ((buffer-read-only nil)
3900         b)
3901     (erase-buffer)
3902     (mapatoms
3903      (lambda (group)
3904        (setq b (point))
3905        (insert (format "      *: %-20s %s\n" (symbol-name group)
3906                        (symbol-value group)))
3907        (add-text-properties 
3908         b (1+ b) (list 'gnus-group group
3909                        'gnus-unread t 'gnus-marked nil 'gnus-level 6)))
3910      gnus-description-hashtb)
3911     (goto-char (point-min))
3912     (gnus-group-position-cursor)))
3913
3914 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
3915 (defun gnus-group-apropos (regexp &optional search-description)
3916   "List all newsgroups that have names that match a regexp."
3917   (interactive "sGnus apropos (regexp): ")
3918   (let ((prev "")
3919         (obuf (current-buffer))
3920         groups des prev)
3921     ;; Go through all newsgroups that are known to Gnus.
3922     (mapatoms 
3923      (lambda (group)
3924        (and (string-match regexp (symbol-name group))
3925             (setq groups (cons (symbol-name group) groups))))
3926      gnus-active-hashtb)
3927     ;; Go through all descriptions that are known to Gnus. 
3928     (if search-description
3929         (mapatoms 
3930          (lambda (group)
3931            (and (string-match regexp (symbol-value group))
3932                 (gnus-gethash (symbol-name group) gnus-active-hashtb)
3933                 (setq groups (cons (symbol-name group) groups))))
3934          gnus-description-hashtb))
3935     (if (not groups)
3936         (message "No groups matched \"%s\"." regexp)
3937       ;; Print out all the groups.
3938       (save-excursion
3939         (pop-to-buffer (get-buffer-create "*Gnus Help*"))
3940         (buffer-disable-undo (current-buffer))
3941         (erase-buffer)
3942         (setq groups (sort groups 'string<))
3943         (while groups
3944           ;; Groups may be entered twice into the list of groups.
3945           (if (not (string= (car groups) prev))
3946               (progn
3947                 (insert (setq prev (car groups)) "\n")
3948                 (if (and gnus-description-hashtb
3949                          (setq des (gnus-gethash (car groups) 
3950                                                  gnus-description-hashtb)))
3951                     (insert "  " des "\n"))))
3952           (setq groups (cdr groups)))
3953         (goto-char 1)))
3954     (pop-to-buffer obuf)))
3955
3956 (defun gnus-group-description-apropos (regexp)
3957   "List all newsgroups that have names or desccriptions that match a regexp."
3958   (interactive "sGnus description apropos (regexp): ")
3959   (if (not (or gnus-description-hashtb
3960                (gnus-read-descriptions-file)))
3961       (error "Couldn't request descriptions file"))
3962   (gnus-group-apropos regexp t))
3963
3964 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
3965 (defun gnus-group-list-matching (level regexp &optional all lowest) 
3966   "List all groups with unread articles that match REGEXP.
3967 If the prefix LEVEL is non-nil, it should be a number that says which
3968 level to cut off listing groups. 
3969 If ALL, also list groups with no unread articles.
3970 If LOWEST, don't list groups with level lower than LOWEST."
3971   (interactive "P\nsList newsgroups matching: ")
3972   (gnus-group-prepare-flat (or level 5) all (or lowest 1) regexp)
3973   (goto-char (point-min))
3974   (gnus-group-position-cursor))
3975
3976 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
3977   "List all groups that match REGEXP.
3978 If the prefix LEVEL is non-nil, it should be a number that says which
3979 level to cut off listing groups. 
3980 If LOWEST, don't list groups with level lower than LOWEST."
3981   (interactive "P\nsList newsgroups matching: ")
3982   (gnus-group-list-matching (or level 9) regexp t lowest))
3983
3984 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
3985 (defun gnus-group-save-newsrc ()
3986   "Save the Gnus startup files."
3987   (interactive)
3988   (gnus-save-newsrc-file))
3989
3990 (defun gnus-group-restart (&optional arg)
3991   "Force Gnus to read the .newsrc file."
3992   (interactive "P")
3993   (gnus-save-newsrc-file)
3994   (gnus-setup-news 'force)
3995   (gnus-group-list-groups arg gnus-have-all-newsgroups))
3996
3997 (defun gnus-group-read-init-file ()
3998   "Read the Gnus elisp init file."
3999   (interactive)
4000   (gnus-read-init-file))
4001
4002 (defun gnus-group-check-bogus-groups ()
4003   "Check bogus newsgroups."
4004   (interactive)
4005   (gnus-check-bogus-newsgroups (not gnus-expert-user))  ;Require confirmation.
4006   (gnus-group-list-groups nil gnus-have-all-newsgroups))
4007
4008 (defun gnus-group-mail ()
4009   "Start composing a mail."
4010   (interactive)
4011   (mail))
4012
4013 (defun gnus-group-edit-global-kill (article &optional group)
4014   "Edit the global kill file.
4015 If GROUP, edit that local kill file instead."
4016   (interactive "P")
4017   (setq gnus-current-kill-article article)
4018   (gnus-kill-file-edit-file group)
4019   (message
4020    (substitute-command-keys
4021     "Editing a global kill file (Type \\[gnus-kill-file-exit] to exit)")))
4022
4023 (defun gnus-group-edit-local-kill (article group)
4024   "Edit a local kill file."
4025   (interactive (list nil (gnus-group-group-name)))
4026   (gnus-group-edit-global-kill article group))
4027
4028 (defun gnus-group-force-update ()
4029   "Update `.newsrc' file."
4030   (interactive)
4031   (gnus-save-newsrc-file))
4032
4033 (defun gnus-group-suspend ()
4034   "Suspend the current Gnus session.
4035 In fact, cleanup buffers except for group mode buffer.
4036 The hook gnus-suspend-gnus-hook is called before actually suspending."
4037   (interactive)
4038   (run-hooks 'gnus-suspend-gnus-hook)
4039   ;; Kill Gnus buffers except for group mode buffer.
4040   (let ((group-buf (get-buffer gnus-group-buffer)))
4041     (while gnus-buffer-list
4042       (and (not (eq (get-buffer (car gnus-buffer-list)) group-buf))
4043            (not (eq (get-buffer (car gnus-buffer-list)) gnus-dribble-buffer))
4044            (get-buffer (car gnus-buffer-list))
4045            (buffer-name (get-buffer (car gnus-buffer-list)))
4046            (kill-buffer (car gnus-buffer-list)))
4047       (setq gnus-buffer-list (cdr gnus-buffer-list)))
4048     (setq gnus-buffer-list (list group-buf))
4049     (bury-buffer group-buf)
4050     (delete-windows-on group-buf t)))
4051
4052 (defun gnus-group-clear-dribble ()
4053   "Clear all information from the dribble buffer."
4054   (interactive)
4055   (gnus-dribble-clear))
4056
4057 (defun gnus-group-exit ()
4058   "Quit reading news after updating .newsrc.eld and .newsrc.
4059 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4060   (interactive)
4061   (if (or noninteractive                ;For gnus-batch-kill
4062           (zerop (buffer-size))         ;No news is good news.
4063           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
4064           (not gnus-interactive-exit)   ;Without confirmation
4065           gnus-expert-user
4066           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
4067       (progn
4068         (run-hooks 'gnus-exit-gnus-hook)
4069         (gnus-save-newsrc-file)
4070         (gnus-close-backends)
4071         (gnus-clear-system))))
4072
4073 (defun gnus-close-backends ()
4074   ;; Send a close request to all backends that support such a request. 
4075   (let ((methods gnus-valid-select-methods)
4076         func)
4077     (while methods
4078       (if (fboundp (setq func (intern (concat (car (car methods))
4079                                               "-request-close"))))
4080           (funcall func))
4081       (setq methods (cdr methods)))))
4082
4083 (defun gnus-group-quit ()
4084   "Quit reading news without updating .newsrc.eld or .newsrc.
4085 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4086   (interactive)
4087   (if (or noninteractive                ;For gnus-batch-kill
4088           (zerop (buffer-size))
4089           (not (gnus-server-opened gnus-select-method))
4090           gnus-expert-user
4091           (not gnus-current-startup-file)
4092           (gnus-yes-or-no-p
4093            (format "Quit reading news without saving %s? "
4094                    (file-name-nondirectory gnus-current-startup-file))))
4095       (progn
4096         (run-hooks 'gnus-exit-gnus-hook)
4097         (gnus-dribble-save)
4098         (gnus-close-backends)
4099         (gnus-clear-system))))
4100
4101 (defun gnus-group-describe-briefly ()
4102   "Give a one line description of the group mode commands."
4103   (interactive)
4104   (message
4105    (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")))
4106
4107 (defun gnus-group-browse-foreign-server (method)
4108   "Browse a foreign news server.
4109 If called interactively, this function will ask for a select method
4110  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
4111 If not, METHOD should be a list where the first element is the method
4112 and the second element is the address."
4113   (interactive
4114    (list (list (intern (completing-read 
4115                         "Select method: "
4116                         gnus-valid-select-methods nil t "nntp"))
4117                ;; Suggested by mapjph@bath.ac.uk.
4118                (completing-read 
4119                 "Server name: " 
4120                 (mapcar (lambda (server) (list server))
4121                         gnus-secondary-servers)))))
4122   (gnus-browse-foreign-server method))
4123
4124 \f
4125 ;;;
4126 ;;; Browse Server Mode
4127 ;;;
4128
4129 (defvar gnus-browse-server-mode-hook nil)
4130 (defvar gnus-browse-server-mode-map nil)
4131 (put 'gnus-browse-server-mode 'mode-class 'special)
4132
4133 (if gnus-browse-server-mode-map
4134     nil
4135   (setq gnus-browse-server-mode-map (make-keymap))
4136   (suppress-keymap gnus-browse-server-mode-map)
4137   (define-key gnus-browse-server-mode-map " " 'gnus-browse-read-group)
4138   (define-key gnus-browse-server-mode-map "=" 'gnus-browse-read-group)
4139   (define-key gnus-browse-server-mode-map "n" 'gnus-browse-next-group)
4140   (define-key gnus-browse-server-mode-map "p" 'gnus-browse-prev-group)
4141   (define-key gnus-browse-server-mode-map "\177" 'gnus-browse-prev-group)
4142   (define-key gnus-browse-server-mode-map "N" 'gnus-browse-next-group)
4143   (define-key gnus-browse-server-mode-map "P" 'gnus-browse-prev-group)
4144   (define-key gnus-browse-server-mode-map "\M-n" 'gnus-browse-next-group)
4145   (define-key gnus-browse-server-mode-map "\M-p" 'gnus-browse-prev-group)
4146   (define-key gnus-browse-server-mode-map "\r" 'gnus-browse-read-group)
4147   (define-key gnus-browse-server-mode-map "u" 'gnus-browse-unsubscribe-current-group)
4148   (define-key gnus-browse-server-mode-map "l" 'gnus-browse-exit)
4149   (define-key gnus-browse-server-mode-map "L" 'gnus-browse-exit)
4150   (define-key gnus-browse-server-mode-map "q" 'gnus-browse-exit)
4151   (define-key gnus-browse-server-mode-map "Q" 'gnus-browse-exit)
4152   (define-key gnus-browse-server-mode-map "\C-c\C-c" 'gnus-browse-exit)
4153   (define-key gnus-browse-server-mode-map "?" 'gnus-browse-describe-briefly)
4154   (define-key gnus-browse-server-mode-map "\C-c\C-i" 'gnus-info-find-node)
4155   )
4156
4157 (defvar gnus-browse-current-method nil)
4158
4159 (defun gnus-browse-foreign-server (method)
4160   (setq gnus-browse-current-method method)
4161   (let ((gnus-select-method method)
4162         groups group)
4163     (message "Connecting to %s..." (nth 1 method))
4164     (or (gnus-server-opened method)
4165         (gnus-open-server method)
4166         (error "Unable to contact server: %s" (gnus-status-message method)))
4167     (or (gnus-request-list method)
4168         (error "Couldn't request list: %s" (gnus-status-message method)))
4169     (set-buffer (get-buffer-create "*Gnus Browse Server*"))
4170     (gnus-add-current-to-buffer-list)
4171     (buffer-disable-undo (current-buffer))
4172     (let ((buffer-read-only nil))
4173       (erase-buffer))
4174     (gnus-browse-server-mode)
4175     (setq mode-line-buffer-identification
4176           (format
4177            "(ding) Browse Server {%s:%s}" (car method) (car (cdr method))))
4178     (save-excursion
4179       (set-buffer nntp-server-buffer)
4180       (let ((cur (current-buffer)))
4181         (goto-char 1)
4182         (delete-matching-lines gnus-ignored-newsgroups)
4183         (while (re-search-forward 
4184                 "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
4185           (goto-char (match-end 1))
4186           (setq groups (cons (cons (buffer-substring (match-beginning 1)
4187                                                      (match-end 1))
4188                                    (- (read cur) (read cur)))
4189                              groups)))))
4190     (setq groups (sort groups 
4191                        (lambda (l1 l2)
4192                          (string< (car l1) (car l2)))))
4193     (let ((buffer-read-only nil))
4194       (while groups
4195         (setq group (car groups))
4196         (insert 
4197          (format "K%7d: %s\n" (cdr group) (car group)))
4198         (setq groups (cdr groups))))
4199     (switch-to-buffer (current-buffer))
4200     (goto-char 1)
4201     (gnus-group-position-cursor)))
4202
4203 (defun gnus-browse-server-mode ()
4204   "Major mode for browsing a foreign server."
4205   (interactive)
4206   (kill-all-local-variables)
4207   (setq mode-line-modified "-- ")
4208   (make-local-variable 'mode-line-format)
4209   (setq mode-line-format (copy-sequence mode-line-format))
4210   (and (equal (nth 3 mode-line-format) "   ")
4211        (setcar (nthcdr 3 mode-line-format) ""))
4212   (setq major-mode 'gnus-browse-server-mode)
4213   (setq mode-name "Browse Server")
4214   (setq mode-line-process nil)
4215   (use-local-map gnus-browse-server-mode-map)
4216   (buffer-disable-undo (current-buffer))
4217   (setq truncate-lines t)
4218   (setq buffer-read-only t)
4219   (run-hooks 'gnus-browse-server-mode-hook))
4220
4221 (defun gnus-browse-read-group ()
4222   "Not implemented, and will probably never be."
4223   (interactive)
4224   (error "You can't read while browsing"))
4225
4226 (defun gnus-browse-next-group (n)
4227   "Go to the next group."
4228   (interactive "p")
4229   (prog1
4230       (forward-line n)
4231     (gnus-group-position-cursor)))
4232
4233 (defun gnus-browse-prev-group (n)
4234   "Go to the next group."
4235   (interactive "p")
4236   (gnus-browse-next-group (- n)))
4237
4238 (defun gnus-browse-unsubscribe-current-group (arg)
4239   "(Un)subscribe to the next ARG groups."
4240   (interactive "p")
4241   (and (eobp)
4242        (error "No group at current line."))
4243   (let ((ward (if (< arg 0) -1 1))
4244         (arg (abs arg)))
4245     (while (and (> arg 0)
4246                 (not (eobp))
4247                 (gnus-browse-unsubscribe-group)
4248                 (zerop (gnus-browse-next-group ward)))
4249       (setq arg (1- arg)))
4250     (gnus-group-position-cursor)
4251     (if (/= 0 arg) (message "No more newsgroups"))
4252     arg))
4253   
4254 (defun gnus-browse-unsubscribe-group ()
4255   (let ((sub nil)
4256         (buffer-read-only nil)
4257         group)
4258     (save-excursion
4259       (beginning-of-line)
4260       (if (= (following-char) ?K) (setq sub t))
4261       (re-search-forward ": \\(.*\\)$" nil t)
4262       (setq group (gnus-group-prefixed-name 
4263                    (buffer-substring (match-beginning 1) (match-end 1))
4264                    gnus-browse-current-method))
4265       (beginning-of-line)
4266       (delete-char 1)
4267       (if sub
4268           (progn
4269             (gnus-group-change-level 
4270              (list t group 3 nil nil gnus-browse-current-method) 3 9 
4271              (gnus-gethash (car (nth 1 gnus-newsrc-assoc)) gnus-newsrc-hashtb)
4272              t)
4273             (insert ? ))
4274         (gnus-group-change-level group 9 3)
4275         (insert ?K)))
4276     t))
4277
4278 (defun gnus-browse-exit ()
4279   "Quit browsing and return to the group buffer."
4280   (interactive)
4281   (if (eq major-mode 'gnus-browse-server-mode)
4282       (kill-buffer (current-buffer)))
4283   (switch-to-buffer gnus-group-buffer)
4284   (gnus-group-list-groups nil))
4285
4286 (defun gnus-browse-describe-briefly ()
4287   "Give a one line description of the group mode commands."
4288   (interactive)
4289   (message
4290    (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")))
4291       
4292 \f
4293 ;;;
4294 ;;; Gnus summary mode
4295 ;;;
4296
4297 (defvar gnus-summary-mode-map nil)
4298 (defvar gnus-summary-mark-map nil)
4299 (defvar gnus-summary-mscore-map nil)
4300 (defvar gnus-summary-send-map nil)
4301 (defvar gnus-summary-extract-map nil)
4302 (defvar gnus-summary-extract-view-map nil)
4303 (defvar gnus-summary-article-map nil)
4304 (defvar gnus-summary-thread-map nil)
4305 (defvar gnus-summary-goto-map nil)
4306 (defvar gnus-summary-exit-map nil)
4307 (defvar gnus-summary-various-map nil)
4308 (defvar gnus-summary-interest-map nil)
4309 (defvar gnus-summary-process-map nil)
4310 (defvar gnus-summary-score-map nil)
4311 (defvar gnus-summary-sort-map nil)
4312 (defvar gnus-summary-mgroup-map nil)
4313 (defvar gnus-summary-vsave-map nil)
4314 (defvar gnus-summary-increase-map nil)
4315 (defvar gnus-summary-inc-subject-map nil)
4316 (defvar gnus-summary-inc-author-map nil)
4317 (defvar gnus-summary-inc-body-map nil)
4318 (defvar gnus-summary-inc-id-map nil)
4319 (defvar gnus-summary-inc-xref-map nil)
4320 (defvar gnus-summary-inc-thread-map nil)
4321 (defvar gnus-summary-inc-fol-map nil)
4322 (defvar gnus-summary-lower-map nil)
4323 (defvar gnus-summary-low-subject-map nil)
4324 (defvar gnus-summary-low-author-map nil)
4325 (defvar gnus-summary-low-body-map nil)
4326 (defvar gnus-summary-low-id-map nil)
4327 (defvar gnus-summary-low-xref-map nil)
4328 (defvar gnus-summary-low-thread-map nil)
4329 (defvar gnus-summary-low-fol-map nil)
4330 (put 'gnus-summary-mode 'mode-class 'special)
4331
4332 (if gnus-summary-mode-map
4333     nil
4334   (setq gnus-summary-mode-map (make-keymap))
4335   (suppress-keymap gnus-summary-mode-map)
4336
4337   ;; Non-orthogonal keys
4338
4339   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
4340   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
4341   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
4342   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
4343   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
4344   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
4345   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
4346   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
4347   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
4348   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
4349   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
4350   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
4351   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
4352   (define-key gnus-summary-mode-map "\M-s" 'gnus-summary-search-article-forward)
4353   (define-key gnus-summary-mode-map "\M-r" 'gnus-summary-search-article-backward)
4354   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
4355   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
4356   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-subject)
4357   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
4358   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
4359   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
4360   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
4361   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
4362   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
4363   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
4364   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
4365   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
4366   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
4367   (define-key gnus-summary-mode-map "k" 'gnus-summary-kill-same-subject-and-select)
4368   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
4369   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
4370   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
4371   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
4372   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
4373   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
4374   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
4375   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
4376   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
4377   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
4378   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
4379   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
4380   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
4381   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
4382   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
4383   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
4384   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
4385   (define-key gnus-summary-mode-map "\C-c\M-\C-s" 'gnus-summary-show-all-expunged)
4386   (define-key gnus-summary-mode-map "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
4387   (define-key gnus-summary-mode-map "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
4388   (define-key gnus-summary-mode-map "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
4389   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
4390   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
4391   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
4392   (define-key gnus-summary-mode-map "\C-x\C-s" 'gnus-summary-reselect-current-group)
4393   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
4394   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
4395   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
4396   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
4397   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
4398   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
4399   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
4400   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
4401   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
4402   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
4403   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
4404   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
4405   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
4406   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
4407   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
4408   (define-key gnus-summary-mode-map "V" 'gnus-version)
4409   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
4410   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
4411   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
4412   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
4413   (define-key gnus-summary-mode-map
4414     (if gnus-xemacs [button2] [mouse-2]) 'gnus-mouse-pick-article)
4415   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
4416   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
4417   (define-key gnus-summary-mode-map "x" 'gnus-summary-delete-marked-as-read)
4418 ; (define-key gnus-summary-mode-map "X" 'gnus-summary-remove-lines-marked-with)
4419   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
4420   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
4421   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
4422 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
4423   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
4424   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu-view)
4425   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
4426
4427
4428   ;; Sort of orthogonal keymap
4429   (define-prefix-command 'gnus-summary-mark-map)
4430   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
4431   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
4432   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
4433   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
4434   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
4435   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
4436   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
4437   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
4438   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
4439   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
4440   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
4441   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
4442   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
4443   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
4444   (define-key gnus-summary-mark-map "\M-r" 'gnus-summary-remove-lines-marked-as-read)
4445   (define-key gnus-summary-mark-map "\M-\C-r" 'gnus-summary-remove-lines-marked-with)
4446   (define-key gnus-summary-mark-map "D" 'gnus-summary-show-all-dormant)
4447   (define-key gnus-summary-mark-map "\M-D" 'gnus-summary-hide-all-dormant)
4448   (define-key gnus-summary-mark-map "S" 'gnus-summary-show-all-expunged)
4449   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
4450   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
4451   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
4452   (define-key gnus-summary-mark-map "k" 'gnus-summary-kill-same-subject-and-select)
4453   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
4454
4455   (define-prefix-command 'gnus-summary-mscore-map)
4456   (define-key gnus-summary-mark-map "s" 'gnus-summary-mscore-map)
4457   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
4458   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
4459   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
4460   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
4461
4462   (define-prefix-command 'gnus-summary-process-map)
4463   (define-key gnus-summary-mark-map "p" 'gnus-summary-process-map)
4464   (define-key gnus-summary-process-map "p" 'gnus-summary-mark-as-processable)
4465   (define-key gnus-summary-process-map "u" 'gnus-summary-unmark-as-processable)
4466   (define-key gnus-summary-process-map "U" 'gnus-summary-unmark-all-processable)
4467   (define-key gnus-summary-process-map "s" 'gnus-uu-mark-series)
4468   (define-key gnus-summary-process-map "r" 'gnus-uu-mark-region)
4469   (define-key gnus-summary-process-map "R" 'gnus-uu-mark-by-regexp)
4470   (define-key gnus-summary-process-map "t" 'gnus-uu-mark-thread)
4471   (define-key gnus-summary-process-map "a" 'gnus-uu-mark-all)
4472   (define-key gnus-summary-process-map "S" 'gnus-uu-mark-sparse)
4473   
4474
4475   (define-prefix-command 'gnus-summary-send-map)
4476   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
4477   (define-key gnus-summary-send-map "p" 'gnus-summary-post-news)
4478   (define-key gnus-summary-send-map "f" 'gnus-summary-followup)
4479   (define-key gnus-summary-send-map "F" 'gnus-summary-followup-with-original)
4480   (define-key gnus-summary-send-map "b" 'gnus-summary-followup-and-reply)
4481   (define-key gnus-summary-send-map "B" 'gnus-summary-followup-and-reply-with-original)
4482   (define-key gnus-summary-send-map "c" 'gnus-summary-cancel-article)
4483   (define-key gnus-summary-send-map "s" 'gnus-summary-supersede-article)
4484   (define-key gnus-summary-send-map "r" 'gnus-summary-reply)
4485   (define-key gnus-summary-send-map "R" 'gnus-summary-reply-with-original)
4486   (define-key gnus-summary-send-map "\C-f" 'gnus-summary-mail-forward)
4487   (define-key gnus-summary-send-map "m" 'gnus-summary-mail-other-window)
4488   (define-key gnus-summary-send-map "u" 'gnus-uu-post-news)
4489   (define-key gnus-summary-send-map "\M-f" 'gnus-uu-digest-and-forward)
4490
4491   
4492   (define-prefix-command 'gnus-summary-goto-map)
4493   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
4494   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
4495   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
4496   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
4497   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
4498   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
4499   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
4500   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
4501   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
4502   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
4503   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
4504   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
4505   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
4506   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
4507
4508
4509   (define-prefix-command 'gnus-summary-thread-map)
4510   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
4511   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
4512   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
4513   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
4514   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
4515   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
4516   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
4517   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
4518   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
4519   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
4520   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
4521   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
4522   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
4523   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
4524
4525   
4526   (define-prefix-command 'gnus-summary-exit-map)
4527   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
4528   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
4529   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
4530   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
4531   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
4532   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
4533   (define-key gnus-summary-exit-map "n" 'gnus-summary-catchup-and-goto-next-group)
4534
4535
4536   (define-prefix-command 'gnus-summary-article-map)
4537   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
4538   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
4539   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
4540   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
4541   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
4542   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
4543   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
4544   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
4545   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
4546   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
4547   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
4548   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
4549   (define-key gnus-summary-article-map "w" 'gnus-summary-stop-page-breaking)
4550   (define-key gnus-summary-article-map "c" 'gnus-summary-caesar-message)
4551   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
4552   (define-key gnus-summary-article-map "t" 'gnus-summary-toggle-header)
4553   (define-key gnus-summary-article-map "hh" 'gnus-article-hide-headers)
4554   (define-key gnus-summary-article-map "hs" 'gnus-article-hide-signature)
4555   (define-key gnus-summary-article-map "hc" 'gnus-article-hide-citation)
4556   (define-key gnus-summary-article-map "ho" 'gnus-article-treat-overstrike)
4557   (define-key gnus-summary-article-map "hw" 'gnus-article-word-wrap)
4558   (define-key gnus-summary-article-map "hd" 'gnus-article-remove-cr)
4559   (define-key gnus-summary-article-map "hq" 'gnus-article-de-quoted-unreadable)
4560   (define-key gnus-summary-article-map "m" 'gnus-summary-toggle-mime)
4561   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
4562
4563
4564   (define-prefix-command 'gnus-summary-extract-map)
4565   (define-key gnus-summary-mode-map "X" 'gnus-summary-extract-map)
4566 ;  (define-key gnus-summary-extract-map "x" 'gnus-summary-extract-any)
4567 ;  (define-key gnus-summary-extract-map "m" 'gnus-summary-extract-mime)
4568   (define-key gnus-summary-extract-map "u" 'gnus-uu-decode-uu)
4569   (define-key gnus-summary-extract-map "U" 'gnus-uu-decode-uu-and-save)
4570   (define-key gnus-summary-extract-map "s" 'gnus-uu-decode-unshar)
4571   (define-key gnus-summary-extract-map "S" 'gnus-uu-decode-unshar-and-save)
4572   (define-key gnus-summary-extract-map "o" 'gnus-uu-decode-save)
4573   (define-key gnus-summary-extract-map "O" 'gnus-uu-decode-save)
4574   (define-key gnus-summary-extract-map "b" 'gnus-uu-decode-binhex)
4575   (define-key gnus-summary-extract-map "B" 'gnus-uu-decode-binhex)
4576
4577   (define-prefix-command 'gnus-summary-extract-view-map)
4578   (define-key gnus-summary-extract-map "v" 'gnus-summary-extract-view-map)
4579   (define-key gnus-summary-extract-view-map "u" 'gnus-uu-decode-uu-view)
4580   (define-key gnus-summary-extract-view-map "U" 'gnus-uu-decode-uu-and-save-view)
4581   (define-key gnus-summary-extract-view-map "s" 'gnus-uu-decode-unshar-view)
4582   (define-key gnus-summary-extract-view-map "S" 'gnus-uu-decode-unshar-and-save-view)
4583   (define-key gnus-summary-extract-view-map "o" 'gnus-uu-decode-save-view)
4584   (define-key gnus-summary-extract-view-map "O" 'gnus-uu-decode-save-view)
4585   (define-key gnus-summary-extract-view-map "b" 'gnus-uu-decode-binhex-view)
4586   (define-key gnus-summary-extract-view-map "B" 'gnus-uu-decode-binhex-view)
4587   
4588   
4589   (define-prefix-command 'gnus-summary-various-map)
4590   (define-key gnus-summary-mode-map "V" 'gnus-summary-various-map)
4591   (define-key gnus-summary-various-map "u" 'gnus-summary-universal-argument)
4592   (define-key gnus-summary-various-map "\C-s" 'gnus-summary-search-article-forward)
4593   (define-key gnus-summary-various-map "\C-r" 'gnus-summary-search-article-backward)
4594   (define-key gnus-summary-various-map "r" 'gnus-summary-refer-article)
4595   (define-key gnus-summary-various-map "&" 'gnus-summary-execute-command)
4596   (define-key gnus-summary-various-map "T" 'gnus-summary-toggle-truncation)
4597   (define-key gnus-summary-various-map "e" 'gnus-summary-expand-window)
4598   (define-key gnus-summary-various-map "S" 'gnus-summary-reselect-current-group)
4599   (define-key gnus-summary-various-map "g" 'gnus-summary-rescan-group)
4600   (define-key gnus-summary-various-map "V" 'gnus-version)
4601   (define-key gnus-summary-various-map "f" 'gnus-summary-fetch-faq)
4602   (define-key gnus-summary-various-map "d" 'gnus-summary-describe-group)
4603   (define-key gnus-summary-various-map "?" 'gnus-summary-describe-briefly)
4604   (define-key gnus-summary-various-map "i" 'gnus-info-find-node)
4605   (define-key gnus-summary-various-map "D" 'gnus-summary-enter-digest-group)
4606   (define-key gnus-summary-various-map "k" 'gnus-summary-edit-local-kill)
4607   (define-key gnus-summary-various-map "K" 'gnus-summary-edit-global-kill)
4608
4609   (define-prefix-command 'gnus-summary-vsave-map)
4610   (define-key gnus-summary-various-map "o" 'gnus-summary-vsave-map)
4611   (define-key gnus-summary-vsave-map "o" 'gnus-summary-save-article)
4612   (define-key gnus-summary-vsave-map "m" 'gnus-summary-save-article-mail)
4613   (define-key gnus-summary-vsave-map "r" 'gnus-summary-save-article-rmail)
4614   (define-key gnus-summary-vsave-map "f" 'gnus-summary-save-article-file)
4615   (define-key gnus-summary-vsave-map "h" 'gnus-summary-save-article-folder)
4616   (define-key gnus-summary-vsave-map "p" 'gnus-summary-pipe-output)
4617
4618   (define-prefix-command 'gnus-summary-score-map)
4619   (define-key gnus-summary-various-map "S" 'gnus-summary-score-map)
4620   (define-key gnus-summary-score-map "s" 'gnus-summary-set-score)
4621   (define-key gnus-summary-score-map "c" 'gnus-score-change-score-file)
4622   (define-key gnus-summary-score-map "m" 'gnus-score-set-mark-below)
4623   (define-key gnus-summary-score-map "x" 'gnus-score-set-expunge-below)
4624   (define-key gnus-summary-score-map "e" 'gnus-score-edit-alist)
4625   (define-key gnus-summary-score-map "f" 'gnus-score-edit-file)
4626
4627   (define-prefix-command 'gnus-summary-sort-map)
4628   (define-key gnus-summary-various-map "s" 'gnus-summary-sort-map)
4629   (define-key gnus-summary-sort-map "n" 'gnus-summary-sort-by-number)
4630   (define-key gnus-summary-sort-map "a" 'gnus-summary-sort-by-author)
4631   (define-key gnus-summary-sort-map "s" 'gnus-summary-sort-by-subject)
4632   (define-key gnus-summary-sort-map "d" 'gnus-summary-sort-by-date)
4633   (define-key gnus-summary-sort-map "i" 'gnus-summary-sort-by-score)
4634
4635   (define-prefix-command 'gnus-summary-mgroup-map)
4636   (define-key gnus-summary-various-map "m" 'gnus-summary-mgroup-map)
4637   (define-key gnus-summary-mgroup-map "e" 'gnus-summary-expire-articles)
4638   (define-key gnus-summary-mgroup-map "\177" 'gnus-summary-delete-article)
4639   (define-key gnus-summary-mgroup-map "m" 'gnus-summary-move-article)
4640   (define-key gnus-summary-mgroup-map "r" 'gnus-summary-respool-article)
4641   (define-key gnus-summary-mgroup-map "w" 'gnus-summary-edit-article)
4642   (define-key gnus-summary-mgroup-map "c" 'gnus-summary-copy-article)
4643
4644
4645   (define-prefix-command 'gnus-summary-increase-map)
4646   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-map)
4647   (define-key gnus-summary-increase-map "i" 'gnus-summary-raise-same-subject-and-select)
4648   (define-key gnus-summary-increase-map "I" 'gnus-summary-raise-same-subject)
4649   (define-key gnus-summary-increase-map "\C-i" 'gnus-summary-raise-score)
4650
4651   (define-prefix-command 'gnus-summary-inc-subject-map)
4652   (define-key gnus-summary-increase-map "s" 'gnus-summary-inc-subject-map)
4653   (define-key gnus-summary-increase-map "S" 'gnus-summary-temporarily-raise-by-subject)
4654   (define-key gnus-summary-inc-subject-map "s" 'gnus-summary-temporarily-raise-by-subject)
4655   (define-key gnus-summary-inc-subject-map "S" 'gnus-summary-raise-by-subject)
4656   (define-key gnus-summary-inc-subject-map "t" 'gnus-summary-temporarily-raise-by-subject)
4657   (define-key gnus-summary-inc-subject-map "p" 'gnus-summary-raise-by-subject)
4658
4659   (define-prefix-command 'gnus-summary-inc-author-map)
4660   (define-key gnus-summary-increase-map "a" 'gnus-summary-inc-author-map)
4661   (define-key gnus-summary-increase-map "A" 'gnus-summary-temporarily-raise-by-author)
4662   (define-key gnus-summary-inc-author-map "a" 'gnus-summary-temporarily-raise-by-author)
4663   (define-key gnus-summary-inc-author-map "A" 'gnus-summary-raise-by-author)
4664   (define-key gnus-summary-inc-author-map "t" 'gnus-summary-temporarily-raise-by-author)
4665   (define-key gnus-summary-inc-author-map "p" 'gnus-summary-raise-by-author)
4666
4667   (define-prefix-command 'gnus-summary-inc-body-map)
4668   (define-key gnus-summary-increase-map "b" 'gnus-summary-inc-body-map)
4669   (define-key gnus-summary-increase-map "B" 'gnus-summary-temporarily-raise-by-body)
4670   (define-key gnus-summary-inc-body-map "b" 'gnus-summary-temporarily-raise-by-body)
4671   (define-key gnus-summary-inc-body-map "B" 'gnus-summary-raise-by-body)
4672   (define-key gnus-summary-inc-body-map "t" 'gnus-summary-temporarily-raise-by-body)
4673   (define-key gnus-summary-inc-body-map "p" 'gnus-summary-raise-by-body)
4674
4675   (define-prefix-command 'gnus-summary-inc-id-map)
4676   (define-key gnus-summary-increase-map "i" 'gnus-summary-inc-id-map)
4677   (define-key gnus-summary-increase-map "I" 'gnus-summary-temporarily-raise-by-id)
4678   (define-key gnus-summary-inc-id-map "i" 'gnus-summary-temporarily-raise-by-id)
4679   (define-key gnus-summary-inc-id-map "I" 'gnus-summary-raise-by-id)
4680   (define-key gnus-summary-inc-id-map "t" 'gnus-summary-temporarily-raise-by-id)
4681   (define-key gnus-summary-inc-id-map "p" 'gnus-summary-raise-by-id)
4682
4683   (define-prefix-command 'gnus-summary-inc-thread-map)
4684   (define-key gnus-summary-increase-map "t" 'gnus-summary-inc-thread-map)
4685   (define-key gnus-summary-increase-map "T" 'gnus-summary-temporarily-raise-by-thread)
4686   (define-key gnus-summary-inc-thread-map "t" 'gnus-summary-temporarily-raise-by-thread)
4687   (define-key gnus-summary-inc-thread-map "T" 'gnus-summary-raise-by-thread)
4688   (define-key gnus-summary-inc-thread-map "t" 'gnus-summary-temporarily-raise-by-thread)
4689   (define-key gnus-summary-inc-thread-map "p" 'gnus-summary-raise-by-thread)
4690
4691   (define-prefix-command 'gnus-summary-inc-xref-map)
4692   (define-key gnus-summary-increase-map "x" 'gnus-summary-inc-xref-map)
4693   (define-key gnus-summary-increase-map "X" 'gnus-summary-temporarily-raise-by-xref)
4694   (define-key gnus-summary-inc-xref-map "x" 'gnus-summary-temporarily-raise-by-xref)
4695   (define-key gnus-summary-inc-xref-map "X" 'gnus-summary-raise-by-xref)
4696   (define-key gnus-summary-inc-xref-map "t" 'gnus-summary-temporarily-raise-by-xref)
4697   (define-key gnus-summary-inc-xref-map "p" 'gnus-summary-raise-by-xref)
4698
4699   (define-prefix-command 'gnus-summary-inc-fol-map)
4700   (define-key gnus-summary-increase-map "f" 'gnus-summary-inc-fol-map)
4701   (define-key gnus-summary-increase-map "F" 'gnus-summary-raise-followups-to-author)
4702   (define-key gnus-summary-inc-fol-map "f" 'gnus-summary-raise-followups-to-author)
4703   (define-key gnus-summary-inc-fol-map "F" 'gnus-summary-raise-followups-to-author)
4704   (define-key gnus-summary-inc-fol-map "t" 'gnus-summary-raise-followups-to-author)
4705   (define-key gnus-summary-inc-fol-map "p" 'gnus-summary-raise-followups-to-author)
4706
4707   (define-prefix-command 'gnus-summary-lower-map)
4708   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-map)
4709   (define-key gnus-summary-lower-map "l" 'gnus-summary-lower-same-subject-and-select)
4710   (define-key gnus-summary-lower-map "L" 'gnus-summary-lower-same-subject)
4711   (define-key gnus-summary-lower-map "\C-l" 'gnus-summary-lower-score)
4712
4713   (define-prefix-command 'gnus-summary-low-subject-map)
4714   (define-key gnus-summary-lower-map "s" 'gnus-summary-low-subject-map)
4715   (define-key gnus-summary-lower-map "S" 'gnus-summary-temporarily-lower-by-subject)
4716   (define-key gnus-summary-low-subject-map "s" 'gnus-summary-temporarily-lower-by-subject)
4717   (define-key gnus-summary-low-subject-map "S" 'gnus-summary-lower-by-subject)
4718   (define-key gnus-summary-low-subject-map "t" 'gnus-summary-temporarily-lower-by-subject)
4719   (define-key gnus-summary-low-subject-map "p" 'gnus-summary-lower-by-subject)
4720
4721   (define-prefix-command 'gnus-summary-low-body-map)
4722   (define-key gnus-summary-lower-map "b" 'gnus-summary-low-body-map)
4723   (define-key gnus-summary-lower-map "B" 'gnus-summary-temporarily-lower-by-body)
4724   (define-key gnus-summary-low-body-map "b" 'gnus-summary-temporarily-lower-by-body)
4725   (define-key gnus-summary-low-body-map "B" 'gnus-summary-lower-by-body)
4726   (define-key gnus-summary-low-body-map "t" 'gnus-summary-temporarily-lower-by-body)
4727   (define-key gnus-summary-low-body-map "p" 'gnus-summary-lower-by-body)
4728
4729   (define-prefix-command 'gnus-summary-low-author-map)
4730   (define-key gnus-summary-lower-map "a" 'gnus-summary-low-author-map)
4731   (define-key gnus-summary-lower-map "A" 'gnus-summary-temporarily-lower-by-author)
4732   (define-key gnus-summary-low-author-map "a" 'gnus-summary-temporarily-lower-by-author)
4733   (define-key gnus-summary-low-author-map "A" 'gnus-summary-lower-by-author)
4734   (define-key gnus-summary-low-author-map "t" 'gnus-summary-temporarily-lower-by-author)
4735   (define-key gnus-summary-low-author-map "p" 'gnus-summary-lower-by-author)
4736
4737   (define-prefix-command 'gnus-summary-low-id-map)
4738   (define-key gnus-summary-lower-map "i" 'gnus-summary-low-id-map)
4739   (define-key gnus-summary-lower-map "I" 'gnus-summary-temporarily-lower-by-id)
4740   (define-key gnus-summary-low-id-map "i" 'gnus-summary-temporarily-lower-by-id)
4741   (define-key gnus-summary-low-id-map "I" 'gnus-summary-lower-by-id)
4742   (define-key gnus-summary-low-id-map "t" 'gnus-summary-temporarily-lower-by-id)
4743   (define-key gnus-summary-low-id-map "p" 'gnus-summary-lower-by-id)
4744
4745   (define-prefix-command 'gnus-summary-low-thread-map)
4746   (define-key gnus-summary-lower-map "t" 'gnus-summary-low-thread-map)
4747   (define-key gnus-summary-lower-map "T" 'gnus-summary-temporarily-lower-by-thread)
4748   (define-key gnus-summary-low-thread-map "t" 'gnus-summary-temporarily-lower-by-thread)
4749   (define-key gnus-summary-low-thread-map "T" 'gnus-summary-lower-by-thread)
4750   (define-key gnus-summary-low-thread-map "t" 'gnus-summary-temporarily-lower-by-thread)
4751   (define-key gnus-summary-low-thread-map "p" 'gnus-summary-lower-by-thread)
4752
4753   (define-prefix-command 'gnus-summary-low-xref-map)
4754   (define-key gnus-summary-lower-map "x" 'gnus-summary-low-xref-map)
4755   (define-key gnus-summary-lower-map "X" 'gnus-summary-temporarily-lower-by-xref)
4756   (define-key gnus-summary-low-xref-map "x" 'gnus-summary-temporarily-lower-by-xref)
4757   (define-key gnus-summary-low-xref-map "X" 'gnus-summary-lower-by-xref)
4758   (define-key gnus-summary-low-xref-map "t" 'gnus-summary-temporarily-lower-by-xref)
4759   (define-key gnus-summary-low-xref-map "p" 'gnus-summary-lower-by-xref)
4760
4761   (define-prefix-command 'gnus-summary-low-fol-map)
4762   (define-key gnus-summary-lower-map "f" 'gnus-summary-low-fol-map)
4763   (define-key gnus-summary-lower-map "F" 'gnus-summary-lower-followups-to-author)
4764   (define-key gnus-summary-low-fol-map "f" 'gnus-summary-lower-followups-to-author)
4765   (define-key gnus-summary-low-fol-map "F" 'gnus-summary-lower-followups-to-author)
4766   (define-key gnus-summary-low-fol-map "t" 'gnus-summary-lower-followups-to-author)
4767   (define-key gnus-summary-low-fol-map "p" 'gnus-summary-lower-followups-to-author)
4768   )
4769
4770
4771 \f
4772
4773 (defun gnus-summary-mode ()
4774   "Major mode for reading articles.
4775
4776 All normal editing commands are switched off.
4777 \\<gnus-summary-mode-map>
4778 Each line in this buffer represents one article.  To read an
4779 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
4780 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
4781 respectively.
4782
4783 You can also post articles and send mail from this buffer.  To 
4784 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
4785 of an article, type `\\[gnus-summary-reply]'.
4786
4787 There are approx. one gazillion commands you can execute in this 
4788 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
4789
4790 The following commands are available:
4791
4792 \\{gnus-summary-mode-map}"
4793   (interactive)
4794   (if gnus-visual (gnus-summary-make-menu-bar))
4795   (kill-all-local-variables)
4796   (let ((locals gnus-summary-local-variables))
4797     (while locals
4798       (if (consp (car locals))
4799           (progn
4800             (make-local-variable (car (car locals)))
4801             (set (car (car locals)) (eval (cdr (car locals)))))
4802         (make-local-variable (car locals))
4803         (set (car locals) nil))
4804       (setq locals (cdr locals))))
4805   (gnus-update-format-specifications)
4806   (setq mode-line-modified "-- ")
4807   (make-local-variable 'mode-line-format)
4808   (setq mode-line-format (copy-sequence mode-line-format))
4809   (and (equal (nth 3 mode-line-format) "   ")
4810        (setcar (nthcdr 3 mode-line-format) ""))
4811   (setq major-mode 'gnus-summary-mode)
4812   (setq mode-name "Summary")
4813   (make-local-variable 'minor-mode-alist)
4814   (gnus-set-mode-line 'summary)
4815   (use-local-map gnus-summary-mode-map)
4816   (buffer-disable-undo (current-buffer))
4817   (setq buffer-read-only t)             ;Disable modification
4818   (setq truncate-lines t)
4819   (setq selective-display t)
4820   (setq selective-display-ellipses t)   ;Display `...'
4821   (run-hooks 'gnus-summary-mode-hook))
4822
4823 (defun gnus-summary-clear-local-variables ()
4824   (let ((locals gnus-summary-local-variables))
4825     (while locals
4826       (if (consp (car locals))
4827           (set (car (car locals)) nil)
4828         (set (car locals) nil))
4829       (setq locals (cdr locals)))))
4830
4831 (defun gnus-mouse-pick-article (e)
4832   (interactive "e")
4833   (mouse-set-point e)
4834   (gnus-summary-next-page nil t))
4835
4836 (defun gnus-summary-setup-buffer (group)
4837   "Initialize summary buffer."
4838   (let ((buffer (concat "*Summary " group "*")))
4839     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
4840     (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
4841     (gnus-add-current-to-buffer-list)
4842     (gnus-summary-mode)))
4843
4844 (defun gnus-set-global-variables ()
4845   ;; Set the global equivalents of the summary buffer-local variables
4846   ;; to the latest values they had. These reflect the summary buffer
4847   ;; that was in action when the last article was fetched.
4848   (if (eq major-mode 'gnus-summary-mode) 
4849       (progn
4850         (setq gnus-summary-buffer (current-buffer))
4851         (let ((name gnus-newsgroup-name)
4852               (marked gnus-newsgroup-marked)
4853               (unread gnus-newsgroup-unreads)
4854               (headers gnus-current-headers))
4855           (save-excursion
4856             (set-buffer gnus-group-buffer)
4857             (setq gnus-newsgroup-name name)
4858             (setq gnus-newsgroup-marked marked)
4859             (setq gnus-newsgroup-unreads unread)
4860             (setq gnus-current-headers headers))))))
4861
4862 (defun gnus-summary-insert-dummy-line (sformat subject number)
4863   (if (not sformat) 
4864       (setq sformat gnus-summary-dummy-line-format-spec))
4865   (let (b)
4866     (beginning-of-line)
4867     (setq b (point))
4868     (insert (eval sformat))
4869     (add-text-properties
4870      b (1+ b)
4871      (list 'gnus-subject (gnus-simplify-subject-re subject)
4872            'gnus-number number
4873            'gnus-mark ?Z
4874            'gnus-thread 0))))
4875
4876 (defun gnus-summary-insert-line 
4877   (sformat header level current unread replied expirable subject-or-nil
4878            &optional dummy score)
4879   (or sformat (setq sformat gnus-summary-line-format-spec))
4880   (let* ((indentation 
4881           (make-string (* level gnus-thread-indent-level) ? ))
4882          (lines (or (header-lines header) 0))
4883          (score (or score gnus-summary-default-score 0))
4884          (score-char (if (or (null gnus-summary-default-score)
4885                              (= score gnus-summary-default-score)) ? 
4886                        (if (< score gnus-summary-default-score) 
4887                            gnus-score-below-mark gnus-score-over-mark)))
4888          (replied (if replied gnus-replied-mark ? ))
4889          (from (header-from header))
4890          (name-address (funcall gnus-extract-address-components from))
4891          (address (car (cdr name-address)))
4892          (name (or (car name-address) (car (cdr name-address))))
4893          (number (header-number header))
4894          (subject (header-subject header))
4895          (buffer-read-only nil)
4896          (opening-bracket (if dummy ?\< ?\[))
4897          (closing-bracket (if dummy ?\> ?\]))
4898          b)
4899     ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
4900     (if (not (numberp lines)) (setq lines 0))
4901     (beginning-of-line)
4902     (setq b (point))
4903     (insert (eval sformat))
4904     (add-text-properties
4905      b (1+ b)
4906      (list 'gnus-subject (gnus-simplify-subject-re subject)
4907            'gnus-number number
4908            'gnus-mark (or unread gnus-unread-mark ? )
4909            'gnus-thread level))))
4910
4911 (defun gnus-summary-update-line (&optional dont-update)
4912   ;; Update summary line after change.
4913   (or (not gnus-summary-default-score)
4914       gnus-summary-inhibit-highlight
4915       (let ((gnus-summary-inhibit-highlight t)
4916             (article (gnus-summary-article-number)))
4917         (progn
4918           (or dont-update
4919               (if (and gnus-summary-mark-below
4920                        (< (gnus-summary-article-score)
4921                           gnus-summary-mark-below))
4922                   (and (not (memq article gnus-newsgroup-marked))
4923                        (not (memq article gnus-newsgroup-dormant))
4924                        (memq article gnus-newsgroup-unreads)
4925                        (gnus-summary-mark-article nil gnus-low-score-mark))
4926                 (and (eq (gnus-summary-article-mark) gnus-low-score-mark)
4927                      (gnus-summary-mark-article nil gnus-unread-mark))))
4928           (and gnus-visual
4929                (run-hooks 'gnus-visual-summary-update-hook))))))
4930
4931 (defun gnus-summary-update-lines ()
4932   ;; Rehighlight summary buffer according to `gnus-summary-highlight'.
4933   (and (save-excursion
4934          (set-buffer gnus-summary-buffer)
4935          (goto-char (point-min))
4936          (while (not (eobp))
4937            (gnus-summary-update-line)
4938            (forward-line 1)))))
4939
4940 (defun gnus-summary-number-of-articles-in-thread (thread)
4941   ;; Sum up all elements (and sub-elements) in a list.
4942   (if (listp thread) 
4943       (apply '+ (mapcar 'gnus-summary-number-of-articles-in-thread thread))
4944     1))
4945
4946 (defun gnus-summary-read-group (group &optional show-all no-article kill-buffer)
4947   "Start reading news in newsgroup GROUP.
4948 If SHOW-ALL is non-nil, already read articles are also listed.
4949 If NO-ARTICLE is non-nil, no article is selected initially."
4950   (message "Retrieving newsgroup: %s..." group)
4951   (gnus-summary-setup-buffer group)
4952   (if (gnus-select-newsgroup group show-all)
4953       (progn
4954         (gnus-set-global-variables)
4955         ;; You can change the subjects in this hook.
4956         (run-hooks 'gnus-select-group-hook)
4957         ;; Do Score Processing.
4958         (gnus-score-headers)
4959         ;; Update the format specifiers.
4960         (gnus-update-format-specifications)
4961         (gnus-summary-prepare)
4962         (if (zerop (buffer-size))
4963             (cond (gnus-newsgroup-dormant
4964                    (gnus-summary-show-all-dormant))
4965                   ((and gnus-newsgroup-scored show-all)
4966                    (gnus-summary-show-all-expunged))))
4967         ;; Function `gnus-apply-kill-file' must be called in this hook.
4968         (run-hooks 'gnus-apply-kill-hook)
4969         (if (zerop (buffer-size))
4970             (progn
4971               ;; This newsgroup is empty.
4972               (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
4973               (message "No unread news")
4974               (and kill-buffer
4975                    (get-buffer kill-buffer)
4976                    (buffer-name (get-buffer kill-buffer))
4977                    (kill-buffer kill-buffer)))
4978           (save-excursion
4979             (if kill-buffer
4980                 (let ((gnus-summary-buffer kill-buffer))
4981                   (gnus-configure-windows 'newsgroups t))))
4982           ;; Hide conversation thread subtrees.  We cannot do this in
4983           ;; gnus-summary-prepare-hook since kill processing may not
4984           ;; work with hidden articles.
4985           (and gnus-show-threads
4986                gnus-thread-hide-subtree
4987                (gnus-summary-hide-all-threads))
4988           ;; Show first unread article if requested.
4989           (goto-char (point-min))
4990           (if (and (not no-article)
4991                    gnus-auto-select-first
4992                    (gnus-summary-first-unread-article))
4993               (gnus-configure-windows 'article)
4994             (gnus-configure-windows 'summary))
4995           (pop-to-buffer gnus-summary-buffer)
4996           (gnus-set-mode-line 'summary)
4997           (gnus-summary-position-cursor)
4998           (if (and kill-buffer
4999                    (get-buffer kill-buffer)
5000                    (buffer-name (get-buffer kill-buffer)))
5001               (kill-buffer kill-buffer))))
5002     ;; Cannot select newsgroup GROUP.
5003     (message "Couldn't select newsgroup")
5004     (and (eq major-mode 'gnus-summary-mode)
5005          (kill-buffer (current-buffer)))
5006     (switch-to-buffer gnus-group-buffer)))
5007
5008 (defun gnus-summary-prepare ()
5009   "Prepare summary list of current newsgroup in summary buffer."
5010   (let ((buffer-read-only nil))
5011     (erase-buffer)
5012     (gnus-summary-prepare-threads 
5013      (if gnus-show-threads
5014          (gnus-gather-threads 
5015           (gnus-sort-threads 
5016            (if gnus-summary-expunge-below
5017                (gnus-make-threads-and-expunge)
5018              (gnus-make-threads))))
5019        gnus-newsgroup-headers)
5020      0 nil nil t)
5021     ;; Erase header retrieval message.
5022     (gnus-summary-update-lines)
5023     (message "")
5024     ;; Call hooks for modifying summary buffer.
5025     ;; Suggested by sven@tde.LTH.Se (Sven Mattisson).
5026     (goto-char (point-min))
5027     (run-hooks 'gnus-summary-prepare-hook)))
5028
5029 (defun gnus-gather-threads (threads)
5030   "Gather threads that have lost their roots."
5031   (if (not gnus-summary-make-false-root)
5032       threads 
5033     (let ((hashtb (gnus-make-hashtable 1023))
5034           (prev threads)
5035           (result threads)
5036           thread subject hthread unre-subject whole-subject)
5037       (while threads
5038         (setq subject (header-subject (car (car threads)))
5039               whole-subject subject)
5040         (and gnus-summary-gather-subject-limit
5041              (or (and (numberp gnus-summary-gather-subject-limit)
5042                       (> (length subject) gnus-summary-gather-subject-limit)
5043                       (setq subject
5044                             (substring subject 0 
5045                                        gnus-summary-gather-subject-limit)))
5046                  (and (eq 'fuzzy gnus-summary-gather-subject-limit)
5047                       (setq subject (gnus-simplify-subject-fuzzy subject)))))
5048         (if (setq hthread 
5049                   (gnus-gethash 
5050                    (setq unre-subject (gnus-simplify-subject-re subject))
5051                    hashtb))
5052             (progn
5053               (or (stringp (car (car hthread)))
5054                   (setcar hthread (list whole-subject (car hthread))))
5055               (setcdr (car hthread) (nconc (cdr (car hthread)) 
5056                                            (list (car threads))))
5057               (setcdr prev (cdr threads))
5058               (setq threads prev))
5059           (gnus-sethash unre-subject threads hashtb))
5060         (setq prev threads)
5061         (setq threads (cdr threads)))
5062       result)))
5063
5064 (defun gnus-make-threads ()
5065   ;; This function takes the dependencies already made by 
5066   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5067   ;; through the dependecies in the hash table and finds all the
5068   ;; roots. Roots do not refer back to any valid articles.
5069   (let (roots)
5070     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5071          (gnus-build-old-threads))
5072     (mapatoms
5073      (lambda (refs)
5074        (if (not (car (symbol-value refs)))
5075            (setq roots (append (cdr (symbol-value refs)) roots))
5076          ;; Ok, these refer back to valid articles, but if
5077          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5078          ;; the root has the same subject as its children. The children
5079          ;; that do not are made into roots and removed from the list
5080          ;; of children. 
5081          (or gnus-thread-ignore-subject
5082              (let* ((prev (symbol-value refs))
5083                     (subject (gnus-simplify-subject-re 
5084                               (header-subject (car prev))))
5085                     (headers (cdr prev)))
5086                (while headers
5087                  (if (not (string= subject
5088                                    (gnus-simplify-subject-re 
5089                                     (header-subject (car headers)))))
5090                      (progn
5091                        (setq roots (cons (car headers) roots))
5092                        (setcdr prev (cdr headers)))
5093                    (setq prev headers))
5094                  (setq headers (cdr headers)))))))
5095      gnus-newsgroup-dependencies)
5096     
5097     (mapcar 'gnus-trim-thread
5098             (apply 'append
5099                    (mapcar 'gnus-cut-thread
5100                            (mapcar 'gnus-make-sub-thread roots))))))
5101   
5102 (defun gnus-make-threads-and-expunge ()
5103   ;; This function takes the dependencies already made by 
5104   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5105   ;; through the dependecies in the hash table and finds all the
5106   ;; roots. Roots do not refer back to any valid articles.
5107   (let (roots)
5108     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5109          (gnus-build-old-threads))
5110     (mapatoms
5111      (lambda (refs)
5112        (if (not (car (symbol-value refs)))
5113            (if (and gnus-summary-expunge-below
5114                     (not gnus-fetch-old-headers))
5115                (let ((headers (cdr (symbol-value refs))))
5116                  (while headers
5117                   (if (not (< (or (cdr (assq (header-number (car headers))
5118                                              gnus-newsgroup-scored))
5119                                   gnus-summary-default-score 0)
5120                               gnus-summary-expunge-below))
5121                       (setq roots (cons (car headers) roots))
5122                     (setq gnus-newsgroup-unreads
5123                           (delq (header-number (car headers))
5124                                 gnus-newsgroup-unreads)))
5125                   (setq headers (cdr headers))))
5126              (setq roots (append (cdr (symbol-value refs)) roots)))
5127          ;; Ok, these refer back to valid articles, but if
5128          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5129          ;; the root has the same subject as its children. The children
5130          ;; that do not are made into roots and removed from the list
5131          ;; of children. 
5132          (or gnus-thread-ignore-subject
5133              (let* ((prev (symbol-value refs))
5134                     (subject (gnus-simplify-subject-re 
5135                               (header-subject (car prev))))
5136                     (headers (cdr prev)))
5137                (while headers
5138                  (if (not (string= subject
5139                                    (gnus-simplify-subject-re 
5140                                     (header-subject (car headers)))))
5141                      (progn
5142                        (setq roots (cons (car headers) roots))
5143                        (setcdr prev (cdr headers)))
5144                    (setq prev headers))
5145                  (setq headers (cdr headers)))))
5146          (and gnus-summary-expunge-below
5147               (not gnus-fetch-old-headers)
5148               (let* ((prev (symbol-value refs))
5149                      (headers (cdr prev))
5150                      id)
5151                 (while headers
5152                   (if (not (< (or (cdr (assq (header-number (car headers))
5153                                              gnus-newsgroup-scored))
5154                                   gnus-summary-default-score 0)
5155                               gnus-summary-expunge-below))
5156                       (setq prev (cdr prev))
5157                     (setq gnus-newsgroup-unreads 
5158                           (delq (header-number (car headers))
5159                                 gnus-newsgroup-unreads))
5160                     (setcdr prev (cdr headers))
5161                     (setq id (gnus-gethash (header-id (car headers))
5162                                            gnus-newsgroup-dependencies))
5163                     (let ((h (cdr id)))
5164                       (while h
5165                         (if (not (< (or (cdr (assq (header-number (car h))
5166                                                    gnus-newsgroup-scored))
5167                                         gnus-summary-default-score 0)
5168                                     gnus-summary-expunge-below))
5169                             (setq roots (cons (car h) roots)))
5170                         (setq h (cdr h)))))
5171                   (setq headers (cdr headers)))))))
5172      gnus-newsgroup-dependencies)
5173     
5174     (mapcar 'gnus-trim-thread
5175             (apply 'append
5176                    (mapcar 'gnus-cut-thread
5177                            (mapcar 'gnus-make-sub-thread roots))))))
5178   
5179 (defun gnus-cut-thread (thread)
5180   ;; Remove leaf dormant or ancient articles from THREAD.
5181   (let ((head (car thread))
5182         (tail (apply 'append (mapcar 'gnus-cut-thread (cdr thread)))))
5183     (if (and (null tail)
5184              (let ((number (header-number head)))
5185                (or (memq number gnus-newsgroup-ancient)
5186                    (memq number gnus-newsgroup-dormant)
5187                    (and gnus-summary-expunge-below
5188                         (eq gnus-fetch-old-headers 'some)
5189                         (< (or (cdr (assq number gnus-newsgroup-scored))
5190                                gnus-summary-default-score 0)
5191                            gnus-summary-expunge-below)
5192                         (progn
5193                           (setq gnus-newsgroup-unreads
5194                                 (delq number gnus-newsgroup-unreads))
5195                           t)))))
5196         nil
5197       (list (cons head tail)))))
5198
5199 (defun gnus-trim-thread (thread)
5200   ;; Remove root ancient articles with only one child from THREAD.
5201   (if (and (eq gnus-fetch-old-headers 'some)
5202            (memq (header-number (car thread)) gnus-newsgroup-ancient)
5203            (= (length thread) 2))
5204       (gnus-trim-thread (nth 1 thread))
5205     thread))
5206
5207 (defun gnus-make-sub-thread (root)
5208   ;; This function makes a sub-tree for a node in the tree.
5209   (let ((children (reverse (cdr (gnus-gethash (downcase (header-id root))
5210                                               gnus-newsgroup-dependencies)))))
5211     (cons root (mapcar 'gnus-make-sub-thread children))))
5212
5213 (defun gnus-build-old-threads ()
5214   ;; Look at all the articles that refer back to old articles, and
5215   ;; fetch the headers for the articles that aren't there. This will
5216   ;; build complete threads - if the roots haven't been expired by the
5217   ;; server, that is.
5218   (let (id heads)
5219     (mapatoms
5220      (lambda (refs)
5221        (if (not (car (symbol-value refs)))
5222            (progn
5223              (setq heads (cdr (symbol-value refs)))
5224              (while heads
5225                (if (not (memq (header-number (car heads))
5226                               gnus-newsgroup-dormant))
5227                    (progn
5228                      (setq id (symbol-name refs))
5229                      (while (and (setq id (gnus-build-get-header id))
5230                                  (not (car (gnus-gethash 
5231                                             id gnus-newsgroup-dependencies)))))
5232                      (setq heads nil))
5233                  (setq heads (cdr heads)))))))
5234      gnus-newsgroup-dependencies)))
5235
5236 (defun gnus-build-get-header (id)
5237   ;; Look through the buffer of NOV lines and find the header to
5238   ;; ID. Enter this line into the dependencies hash table, and return
5239   ;; the id of the parent article (if any).
5240   (let ((deps gnus-newsgroup-dependencies)
5241         found header)
5242     (prog1
5243         (save-excursion
5244           (set-buffer nntp-server-buffer)
5245           (goto-char (point-min))
5246           (while (and (not found) (search-forward id nil t))
5247             (beginning-of-line)
5248             (setq found (looking-at (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
5249                                             (regexp-quote id))))
5250             (or found (beginning-of-line 2)))
5251           (if found
5252               (let (ref)
5253                 (beginning-of-line)
5254                 (and
5255                  (setq header (gnus-nov-parse-line 
5256                                (read (current-buffer)) deps))
5257                  (setq ref (header-references header))
5258                  (string-match "\\(<[^>]+>\\) *$" ref)
5259                  (substring ref (match-beginning 1) (match-end 1))))))
5260       (and header
5261            (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers)
5262                  gnus-newsgroup-ancient (cons (header-number header)
5263                                               gnus-newsgroup-ancient))))))
5264
5265 (defun gnus-sort-threads (threads)
5266   ;; Sort threads as specified in `gnus-thread-sort-functions'.
5267   (let ((fun gnus-thread-sort-functions))
5268     (while fun
5269       (setq threads (sort threads (car fun))
5270             fun (cdr fun))))
5271   threads)
5272
5273 (defun gnus-thread-header (thread)
5274   ;; Return header of first article in THREAD.
5275   (if (consp thread)
5276       (if (stringp (car thread))
5277           (car (car (cdr thread)))
5278         (car thread))
5279     thread))
5280
5281 (defun gnus-thread-sort-by-number (h1 h2)
5282   "Sort threads by root article number."
5283   (let ((h1 (gnus-thread-header h1))
5284         (h2 (gnus-thread-header h2)))
5285     (< (header-number h1) (header-number h2))))
5286
5287 (defun gnus-thread-sort-by-author (h1 h2)
5288   "Sort threads by root author."
5289   (let ((h1 (gnus-thread-header h1))
5290         (h2 (gnus-thread-header h2)))
5291     (string-lessp
5292      (let ((extract (funcall 
5293                      gnus-extract-address-components (header-from h1))))
5294        (or (car extract) (cdr extract)))
5295      (let ((extract (funcall
5296                      gnus-extract-address-components (header-from h2))))
5297        (or (car extract) (cdr extract))))))
5298
5299 (defun gnus-thread-sort-by-subject (h1 h2)
5300   "Sort threads by root subject."
5301   (let ((h1 (gnus-thread-header h1))
5302         (h2 (gnus-thread-header h2)))
5303     (string-lessp
5304      (downcase (gnus-simplify-subject (header-subject h1)))
5305      (downcase (gnus-simplify-subject (header-subject h2))))))
5306
5307 (defun gnus-thread-sort-by-date (h1 h2)
5308   "Sort threads by root article date."
5309   (let ((h1 (gnus-thread-header h1))
5310         (h2 (gnus-thread-header h2)))
5311     (string-lessp
5312      (gnus-sortable-date (header-date h1))
5313      (gnus-sortable-date (header-date h2)))))
5314
5315 (defun gnus-thread-sort-by-score (h1 h2)
5316   "Sort threads by root article score.
5317 Unscored articles will be counted as havin a score of zero."
5318   (let ((h1 (gnus-thread-header h1))
5319         (h2 (gnus-thread-header h2)))
5320     (let ((s1 (assq (header-number h1) gnus-newsgroup-scored))
5321           (s2 (assq (header-number h2) gnus-newsgroup-scored)))
5322       (> (or (cdr s1) gnus-summary-default-score 0)
5323          (or (cdr s2) gnus-summary-default-score 0)))))
5324
5325 (defun gnus-thread-sort-by-total-score (h1 h2)
5326   "Sort threads by the sum of all scores in the thread.
5327 Unscored articles will be counted as havin a score of zero."
5328   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
5329
5330 (defun gnus-thread-total-score (thread)
5331   ;;  This function find the total score of  THREAD.
5332   (if (consp thread)
5333       (if (stringp (car thread))
5334           (apply gnus-thread-score-function 0
5335                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
5336         (gnus-thread-total-score-1 thread))
5337     (gnus-thread-total-score-1 (list thread))))
5338
5339 (defun gnus-thread-total-score-1 (root)
5340   ;; This function find the total score of the thread below ROOT.
5341   (setq root (car root))
5342   (apply gnus-thread-score-function
5343          (or (cdr (assq (header-number root) gnus-newsgroup-scored))
5344              gnus-summary-default-score 0)
5345          (mapcar 'gnus-thread-total-score
5346                  (cdr (gnus-gethash (downcase (header-id root))
5347                                     gnus-newsgroup-dependencies)))))
5348
5349 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5350 (defvar gnus-tmp-prev-subject "")
5351
5352 ;; Basic ideas by Paul Dworkin <paul@media-lab.media.mit.edu>.
5353 (defun gnus-summary-prepare-threads 
5354   (threads level &optional not-child no-subject cull)
5355   "Prepare summary buffer from THREADS and indentation LEVEL.  
5356 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
5357 or a straight list of headers."
5358   (let (thread header number subject clevel)
5359     (while threads
5360       (setq thread (car threads)
5361             threads (cdr threads))
5362       ;; If `thread' is a cons, hierarchical threads are used.  If not,
5363       ;; `thread' is the header.
5364       (if (consp thread)
5365           (setq header (car thread))
5366         (setq header thread)
5367         (and cull
5368              (or (memq (setq number (header-number header))
5369                        gnus-newsgroup-dormant)
5370                  (and gnus-summary-expunge-below
5371                       (< (or (cdr (assq number gnus-newsgroup-scored))
5372                              gnus-summary-default-score 0)
5373                          gnus-summary-expunge-below)))
5374              (progn
5375                (setq header nil)
5376                (setq gnus-newsgroup-unreads 
5377                      (delq number gnus-newsgroup-unreads)))))
5378       (cond 
5379        ((stringp header)
5380         ;; The header is a dummy root.
5381         (cond ((eq gnus-summary-make-false-root 'adopt)
5382                ;; We let the first article adopt the rest.
5383                (gnus-summary-prepare-threads (list (car (cdr thread))) 0)
5384                (setq thread (cdr (cdr thread)))
5385                (while thread
5386                  (gnus-summary-prepare-threads (list (car thread)) 1 t)
5387                  (setq thread (cdr thread))))
5388               ((eq gnus-summary-make-false-root 'dummy)
5389                ;; We output a dummy root.
5390                (gnus-summary-insert-dummy-line 
5391                 nil header (header-number (car (car (cdr thread)))))
5392                (setq clevel 1))
5393               ((eq gnus-summary-make-false-root 'empty)
5394                ;; We print the articles with empty subject fields. 
5395                (gnus-summary-prepare-threads (list (car (cdr thread))) 0)
5396                (setq thread (cdr (cdr thread)))
5397                (while thread
5398                  (gnus-summary-prepare-threads 
5399                   (list (car thread)) 0 nil
5400                   (not (and (eq gnus-summary-gather-subject-limit 'fuzzy)
5401                             (not (string=  
5402                                   (gnus-simplify-subject-re 
5403                                    (header-subject (car (car thread))))
5404                                   (gnus-simplify-subject-re header))))))
5405                  (setq thread (cdr thread))))
5406               (t
5407                ;; We do not make a root for the gathered
5408                ;; sub-threads at all.  
5409                (setq clevel 0)))
5410         ;; Print the sub-threads.
5411         (and (consp thread) (cdr thread)
5412              (gnus-summary-prepare-threads (cdr thread) clevel)))
5413        ;; The header is a real article.
5414        (header
5415         (setq number (header-number header)
5416               subject (header-subject header))
5417         (gnus-summary-insert-line
5418          nil header level nil 
5419          (cond ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
5420                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
5421                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
5422                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
5423                (t gnus-ancient-mark))
5424          (memq number gnus-newsgroup-replied)
5425          (memq number gnus-newsgroup-expirable)
5426          (if no-subject 
5427              gnus-summary-same-subject
5428            (if (or (zerop level)
5429                    (and gnus-thread-ignore-subject
5430                         (not (string= 
5431                               (gnus-simplify-subject-re gnus-tmp-prev-subject)
5432                               (gnus-simplify-subject-re subject)))))
5433                subject
5434              gnus-summary-same-subject))
5435          not-child
5436          (cdr (assq number gnus-newsgroup-scored)))
5437         (setq gnus-tmp-prev-subject subject)
5438         ;; Recursively print subthreads.
5439         (and (consp thread) (cdr thread)
5440              (gnus-summary-prepare-threads (cdr thread) (1+ level))))))))
5441
5442 (defun gnus-select-newsgroup (group &optional read-all)
5443   "Select newsgroup GROUP.
5444 If READ-ALL is non-nil, all articles in the group are selected."
5445   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5446          (info (nth 2 entry))
5447          articles header-marks)
5448     (and (eq (car entry) t)
5449          (or (gnus-activate-newsgroup (car info))
5450              (progn
5451                (kill-buffer (current-buffer))
5452                (error "Couldn't request group %s: %s" 
5453                       group (gnus-status-message group)))))
5454     (setq gnus-current-select-method (or (nth 4 info) gnus-select-method))
5455     (gnus-check-news-server (nth 4 info))
5456     (or (gnus-request-group group t)
5457         (progn
5458           (kill-buffer (current-buffer))
5459           (error "Couldn't request group %s: %s" 
5460                  group (gnus-status-message group))))
5461
5462     (setq gnus-newsgroup-name group)
5463     (setq gnus-newsgroup-unselected nil)
5464     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5465
5466     (if (not (setq articles (gnus-articles-to-read group read-all)))
5467         nil
5468       ;; Init the dependencies hash table.
5469       (setq gnus-newsgroup-dependencies 
5470             (gnus-make-hashtable (length articles)))
5471       ;; Retrieve the headers and read them in.
5472       (setq gnus-newsgroup-headers 
5473             (if (eq 'nov (setq gnus-headers-retrieved-by
5474                                (gnus-retrieve-headers 
5475                                 (if gnus-fetch-old-headers 
5476                                     (cons 1 articles) articles) 
5477                                 gnus-newsgroup-name)))
5478                 (progn
5479                   (gnus-get-newsgroup-headers-xover articles))
5480               (gnus-get-newsgroup-headers)))
5481       ;; If we were to fetch old headers, but the backend didn't
5482       ;; support XOVER, then it is possible we fetched one article
5483       ;; that we shouldn't have. If that's the case, we pop it off the
5484       ;; list of headers.
5485       (and (not (eq gnus-headers-retrieved-by 'nov))
5486            gnus-fetch-old-headers
5487            gnus-newsgroup-headers
5488            (/= (header-number (car gnus-newsgroup-headers)) (car articles))
5489            (setq gnus-newsgroup-headers (cdr gnus-newsgroup-headers)))
5490       ;; Remove cancelled articles from the list of unread articles.
5491       (setq gnus-newsgroup-unreads
5492             (gnus-set-sorted-intersection 
5493              gnus-newsgroup-unreads
5494              (mapcar (lambda (headers) (header-number headers))
5495                      gnus-newsgroup-headers)))
5496       ;; Adjust and set lists of article marks.
5497       (and info
5498            (let (marked)
5499              (gnus-adjust-marked-articles info)
5500              (setq gnus-newsgroup-marked 
5501                    (cdr (assq 'tick (setq marked (nth 3 info)))))
5502              (setq gnus-newsgroup-replied (cdr (assq 'reply marked)))
5503              (setq gnus-newsgroup-expirable (cdr (assq 'expire marked)))
5504              (setq gnus-newsgroup-killed (cdr (assq 'killed marked)))
5505              (setq gnus-newsgroup-bookmarks (cdr (assq 'bookmark marked)))
5506              (setq gnus-newsgroup-dormant (cdr (assq 'dormant marked)))
5507              (setq gnus-newsgroup-scored (cdr (assq 'score marked)))
5508              (setq gnus-newsgroup-processable nil)))
5509       ;; Check whether auto-expire is to be done in this group.
5510       (setq gnus-newsgroup-auto-expire
5511             (or (and (stringp gnus-auto-expirable-newsgroups)
5512                      (string-match gnus-auto-expirable-newsgroups group))
5513                 (memq 'auto-expire gnus-current-select-method)))
5514       ;; First and last article in this newsgroup.
5515       (and gnus-newsgroup-headers
5516            (setq gnus-newsgroup-begin 
5517                  (header-number (car gnus-newsgroup-headers)))
5518            (setq gnus-newsgroup-end
5519                  (header-number (gnus-last-element gnus-newsgroup-headers))))
5520       (setq gnus-reffed-article-number -1)
5521       ;; GROUP is successfully selected.
5522       (or gnus-newsgroup-headers t))))
5523
5524 (defun gnus-articles-to-read (group read-all)
5525   ;; Find out what articles the user wants to read.
5526   (let* ((articles
5527           ;; Select all articles if `read-all' is non-nil, or if all the
5528           ;; unread articles are dormant articles.
5529           (if (or read-all
5530                   (= (length gnus-newsgroup-unreads) 
5531                      (length gnus-newsgroup-dormant)))
5532               (gnus-uncompress-range 
5533                (gnus-gethash group gnus-active-hashtb))
5534             gnus-newsgroup-unreads))
5535          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5536          (scored (length scored-list))
5537          (number (length articles))
5538          (marked (+ (length gnus-newsgroup-marked)
5539                     (length gnus-newsgroup-dormant)))
5540          (select
5541           (condition-case ()
5542               (cond ((and (or (<= scored marked)
5543                               (= scored number))
5544                           (numberp gnus-large-newsgroup)
5545                           (> number gnus-large-newsgroup))
5546                      (let ((input
5547                             (read-string
5548                              (format
5549                               "How many articles from %s (default %d): "
5550                               gnus-newsgroup-name number))))
5551                        (if (string-equal input "")
5552                            number input)))
5553                     ((and (> scored marked) (< scored number))
5554                      (let ((input
5555                             (read-string
5556                              (format 
5557                               "%s %s (%d scored, %d total): "
5558                               "How many articles from"
5559                               group scored number))))
5560                        (if (string-equal input "")
5561                            number input)))
5562                     (t number))
5563             (quit 0)))
5564          total-articles)
5565     (setq select (if (numberp select) select (string-to-number select)))
5566     (if (zerop select)
5567         ()
5568       (if (and (not (zerop scored)) (<= (abs select) scored))
5569           (progn
5570             (setq articles (sort scored-list '<))
5571             (setq number (length articles)))
5572         (setq articles (copy-sequence articles)))
5573
5574       (setq total-articles articles)
5575       
5576       (if (< (abs select) number)
5577           (if (< select 0) 
5578               ;; Select the N oldest articles.
5579               (setcdr (nthcdr (1- (abs select)) articles) nil)
5580             ;; Select the N most recent articles.
5581             (setq articles (nthcdr (- number select) articles))))
5582       (setq gnus-newsgroup-unselected
5583             (gnus-sorted-intersection
5584              gnus-newsgroup-unreads
5585              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
5586       articles)))
5587
5588 (defun gnus-killed-articles (killed articles)
5589   (let (out)
5590     (while articles
5591       (if (inline (gnus-member-of-range (car articles) killed))
5592           (setq out (cons (car articles) out)))
5593       (setq articles (cdr articles)))
5594     out))
5595
5596 (defun gnus-adjust-marked-articles (info &optional active)
5597   "Remove all marked articles that are no longer legal."
5598   (let ((marked-lists (nth 3 info))
5599         (active (or active (gnus-gethash (car info) gnus-active-hashtb)))
5600         marked m prev)
5601     ;; There are many types of marked articles.
5602     (while marked-lists
5603       (setq m (cdr (setq prev (car marked-lists))))
5604       (cond ((or (eq 'tick (car prev)) (eq 'dormant (car prev)))
5605              ;; Make sure that all ticked articles are a subset of the
5606              ;; unread/unselected articles.
5607              (while m
5608                (if (or (memq (car m) gnus-newsgroup-unreads)
5609                        (memq (car m) gnus-newsgroup-unselected))
5610                    (setq prev m)
5611                  (setcdr prev (cdr m)))
5612                (setq m (cdr m))))
5613             ((eq 'score (car prev))
5614              ;; Scored articles should be a subset of
5615              ;; unread/unselected articles. 
5616              (while m
5617                (if (or (memq (car (car m)) gnus-newsgroup-unreads)
5618                        (memq (car (car m)) gnus-newsgroup-unreads))
5619                    (setq prev m)
5620                  (setcdr prev (cdr m)))
5621                (setq m (cdr m))))
5622             ((eq 'bookmark (car prev))
5623              ;; Bookmarks should be a subset of active articles.
5624              (while m
5625                (if (< (car (car m)) (car active))
5626                    (setcdr prev (cdr m))
5627                  (setq prev m))
5628                (setq m (cdr m))))
5629             ((eq 'killed (car prev))
5630              ;; Articles that have been through the kill process are
5631              ;; to be a subset of active articles.
5632              (while (and m (< (or (and (numberp (car m)) (car m))
5633                                   (cdr (car m)))
5634                               (car active)))
5635                (setcdr prev (cdr m))
5636                (setq m (cdr m)))
5637              (if (and m (< (or (and (numberp (car m)) (car m))
5638                                (car (car m)))
5639                            (car active))) 
5640                  (setcar (if (numberp (car m)) m (car m)) (car active))))
5641             ((or (eq 'reply (car prev)) (eq 'expire (car prev)))
5642              ;; The replied and expirable articles have to be articles
5643              ;; that are active. 
5644              (while m
5645                (if (< (car m) (car active))
5646                    (setcdr prev (cdr m))
5647                  (setq prev m))
5648                (setq m (cdr m)))))
5649       (setq marked-lists (cdr marked-lists)))
5650     ;; Remove all lists that are empty.
5651     (setq marked-lists (nth 3 info))
5652     (if marked-lists
5653         (progn
5654           (while (= 1 (length (car marked-lists)))
5655             (setq marked-lists (cdr marked-lists)))
5656           (setq m (cdr (setq prev marked-lists)))
5657           (while m
5658             (if (= 1 (length (car m)))
5659                 (setcdr prev (cdr m))
5660               (setq prev m))
5661             (setq m (cdr m)))
5662           (setcar (nthcdr 3 info) marked-lists)))
5663     ;; Finally, if there are no marked lists at all left, and if there
5664     ;; are no elements after the lists in the info list, we just chop
5665     ;; the info list off before the marked lists.
5666     (and (null marked-lists) 
5667          (not (nthcdr 4 info))
5668          (setcdr (nthcdr 2 info) nil)))
5669   info)
5670
5671 (defun gnus-set-marked-articles 
5672   (info ticked replied expirable killed dormant bookmark score) 
5673   "Enter the various lists of marked articles into the newsgroup info list."
5674   (let (newmarked)
5675     (and ticked (setq newmarked (cons (cons 'tick ticked) nil)))
5676     (and replied (setq newmarked (cons (cons 'reply replied) newmarked)))
5677     (and expirable (setq newmarked (cons (cons 'expire expirable) 
5678                                          newmarked)))
5679     (and killed (setq newmarked (cons (cons 'killed killed) newmarked)))
5680     (and dormant (setq newmarked (cons (cons 'dormant dormant) newmarked)))
5681     (and bookmark (setq newmarked (cons (cons 'bookmark bookmark) 
5682                                         newmarked)))
5683     (and score (setq newmarked (cons (cons 'score score) newmarked)))
5684     (if (nthcdr 3 info)
5685         (if newmarked
5686             (setcar (nthcdr 3 info) newmarked)
5687           (if (not (nthcdr 4 info))
5688               (setcdr (nthcdr 2 info) nil)
5689             (setcar (nthcdr 3 info) nil)))
5690       (if newmarked
5691           (setcdr (nthcdr 2 info) (cons newmarked nil))))))
5692
5693 (defun gnus-add-marked-articles (group type articles &optional info force)
5694   ;; Add ARTICLES of TYPE to the info of GROUP.
5695   ;; If INFO is non-nil, use that info. If FORCE is non-nil, don't
5696   ;; add, but replace marked articles of TYPE with ARTICLES.
5697   (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
5698         marked m)
5699     (or (not info)
5700         (and (not (setq marked (nthcdr 3 info)))
5701              (setcdr (nthcdr 2 info) (list (list (cons type articles)))))
5702         (and (not (setq m (assq type (car marked))))
5703              (setcar marked (cons (cons type articles) (car marked))))
5704         (if force
5705             (setcdr m articles)
5706           (nconc m articles)))))
5707          
5708 (defun gnus-set-mode-line (where)
5709   "This function sets the mode line of the article or summary buffers.
5710 If WHERE is `summary', the summary mode line format will be used."
5711   (if (memq where gnus-updated-mode-lines)
5712       (let (mode-string)
5713         (save-excursion
5714           (set-buffer gnus-summary-buffer)
5715           (let* ((mformat (if (eq where 'article) 
5716                               gnus-article-mode-line-format-spec
5717                             gnus-summary-mode-line-format-spec))
5718                  (group-name gnus-newsgroup-name)
5719                  (article-number (or gnus-current-article 0))
5720                  (unread (- (length gnus-newsgroup-unreads)
5721                             (length gnus-newsgroup-dormant)))
5722                  (unread-and-unticked 
5723                   (- unread (length gnus-newsgroup-marked)))
5724                  (unselected (length gnus-newsgroup-unselected))
5725                  (unread-and-unselected
5726                   (cond ((and (zerop unread-and-unticked)
5727                               (zerop unselected)) "")
5728                         ((zerop unselected) 
5729                          (format "{%d more}" unread-and-unticked))
5730                         (t (format "{%d(+%d) more}"
5731                                    unread-and-unticked unselected))))
5732                  (subject
5733                   (if gnus-current-headers
5734                       (header-subject gnus-current-headers) ""))
5735                  (max-len (- (frame-width) gnus-mode-non-string-length)))
5736             (setq mode-string (eval mformat))
5737             (if (> (length mode-string) max-len) 
5738                 (setq mode-string 
5739                       (concat (substring mode-string 0 (- max-len 3)) "...")))
5740             (setq mode-string (format (format "%%-%ds" max-len 5)
5741                                       mode-string))))
5742         (setq mode-line-buffer-identification mode-string)
5743         (set-buffer-modified-p t))))
5744
5745 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5746   "Go through the HEADERS list and add all Xrefs to a hash table.
5747 The resulting hash table is returned, or nil if no Xrefs were found."
5748   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
5749          (prefix (if (and 
5750                       (gnus-group-foreign-p from-newsgroup)
5751                       (not (memq 'virtual 
5752                                  (assoc (symbol-name (car from-method))
5753                                         gnus-valid-select-methods))))
5754                      (gnus-group-real-prefix from-newsgroup)))
5755          (xref-hashtb (make-vector 63 0))
5756          start group entry number xrefs header)
5757     (while headers
5758       (setq header (car headers))
5759       (if (and (setq xrefs (header-xref header))
5760                (not (memq (header-number header) unreads)))
5761           (progn
5762             (setq start 0)
5763             (while (string-match "\\([^ ]+\\):\\([0-9]+\\)" xrefs start)
5764               (setq start (match-end 0))
5765               (setq group (concat prefix (substring xrefs (match-beginning 1) 
5766                                                     (match-end 1))))
5767               (setq number 
5768                     (string-to-int (substring xrefs (match-beginning 2) 
5769                                               (match-end 2))))
5770               (if (setq entry (gnus-gethash group xref-hashtb))
5771                   (setcdr entry (cons number (cdr entry)))
5772                 (gnus-sethash group (cons number nil) xref-hashtb)))))
5773       (setq headers (cdr headers)))
5774     (if start xref-hashtb nil)))
5775
5776 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads expirable)
5777   "Look through all the headers and mark the Xrefs as read."
5778   (let ((virtual (memq 'virtual 
5779                        (assoc (symbol-name (car (gnus-find-method-for-group 
5780                                                  from-newsgroup)))
5781                               gnus-valid-select-methods)))
5782         name entry read info xref-hashtb idlist active num range exps method)
5783     (save-excursion
5784       (set-buffer gnus-group-buffer)
5785       (if (setq xref-hashtb 
5786                 (gnus-create-xref-hashtb from-newsgroup headers unreads))
5787           (mapatoms 
5788            (lambda (group)
5789              (if (string= from-newsgroup (setq name (symbol-name group)))
5790                  ()
5791                (setq idlist (symbol-value group))
5792                ;; Dead groups are not updated.
5793                (if (and (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5794                               info (nth 2 entry))
5795                         ;; Only do the xrefs if the group has the same
5796                         ;; select method as the group we have just read.
5797                         (or (gnus-methods-equal-p 
5798                              (nth 4 info)
5799                              (gnus-find-method-for-group from-newsgroup))
5800                             virtual
5801                             (equal (nth 4 info) 
5802                                    (setq method (gnus-find-method-for-group 
5803                                                  from-newsgroup)))
5804                             (and (equal (car (nth 4 info)) (car method))
5805                                  (equal (nth 1 (nth 4 info)) (nth 1 method))))
5806                         gnus-use-cross-reference
5807                         (or (not (eq gnus-use-cross-reference t))
5808                             virtual
5809                             ;; Only do cross-references on subscribed
5810                             ;; groups, if that is what is wanted.  
5811                             (<= (nth 1 info) 5)))
5812                    (progn
5813                      (setq num 0)
5814                      ;; Set the new list of read articles in this group.
5815                      (setq active (gnus-gethash name gnus-active-hashtb))
5816                      ;; First peel off all illegal article numbers.
5817                      (if active
5818                          (let ((ids idlist)
5819                                (ticked (cdr (assq 'tick (nth 3 info))))
5820                                (dormant (cdr (assq 'dormant (nth 3 info))))
5821                                id)
5822                            (setq exps nil)
5823                            (while ids
5824                              (setq id (car ids))
5825                              (if (or (> id (cdr active))
5826                                      (< id (car active))
5827                                      (memq id ticked)
5828                                      (memq id dormant))
5829                                  (setq idlist (delq id idlist)))
5830                              (and (memq id expirable)
5831                                   (setq exps (cons id exps)))
5832                              (setq ids (cdr ids)))))
5833                      ;; Update expirable articles.
5834                      (gnus-add-marked-articles nil 'expirable exps info)
5835                      (and (null (nth 2 info))
5836                           (> (car active) 1)
5837                           (setcar (nthcdr 2 info) (cons 1 (1- (car active)))))
5838                      (setcar (nthcdr 2 info)
5839                              (setq range
5840                                    (gnus-add-to-range 
5841                                     (nth 2 info) 
5842                                     (setq idlist (sort idlist '<)))))
5843                      ;; Then we have to re-compute how many unread
5844                      ;; articles there are in this group.
5845                      (if active
5846                          (progn
5847                            (cond 
5848                             ((not range)
5849                              (setq num (- (1+ (cdr active)) (car active))))
5850                             ((not (listp (cdr range)))
5851                              (setq num (- (cdr active) (- (1+ (cdr range)) 
5852                                                           (car range)))))
5853                             (t
5854                              (while range
5855                                (if (numberp (car range))
5856                                    (setq num (1+ num))
5857                                  (setq num (+ num (- (1+ (cdr (car range)))
5858                                                      (car (car range))))))
5859                                (setq range (cdr range)))
5860                              (setq num (- (cdr active) num))))
5861                            ;; Update the number of unread articles.
5862                            (setcar 
5863                             entry 
5864                             (max 0 (- num 
5865                                       (length (cdr (assq 'tick (nth 3 info))))
5866                                       (length 
5867                                        (cdr (assq 'dormant (nth 3 info)))))))
5868                            ;; Update the group buffer.
5869                            (gnus-group-update-group name t)))))))
5870            xref-hashtb)))))
5871
5872 (defun gnus-methods-equal-p (m1 m2)
5873   (let ((m1 (or m1 gnus-select-method))
5874         (m2 (or m2 gnus-select-method)))
5875     (or (equal m1 m2)
5876         (and (eq (car m1) (car m2))
5877              (or (not (memq 'address (assoc (symbol-name (car m1))
5878                                             gnus-valid-select-methods)))
5879                  (equal (nth 1 m1) (nth 1 m2)))))))
5880
5881 (defsubst gnus-header-value ()
5882   (buffer-substring (match-end 0) (save-excursion (end-of-line) (point))))
5883
5884 (defun gnus-get-newsgroup-headers ()
5885   (setq gnus-article-internal-prepare-hook nil)
5886   (let ((cur nntp-server-buffer)
5887         (dependencies gnus-newsgroup-dependencies)
5888         (none-id 0)
5889         headers char article id dep end)
5890     (save-excursion
5891       (set-buffer nntp-server-buffer)
5892       (goto-char 1)
5893       ;; Search to the beginning of the next header. Error messages
5894       ;; do not begin with 2 or 3.
5895       (while (re-search-forward "^[23][0-9]+ " nil t)
5896         (let ((header (make-vector 9 nil))
5897               (c (following-char))
5898               (case-fold-search t)
5899               (p (point))
5900               from subject in-reply-to references ref)
5901           (setq id nil
5902                 ref nil
5903                 references nil
5904                 subject nil
5905                 from nil)
5906           (header-set-number header (setq article (read cur)))
5907           ;; This implementation of this function, with nine
5908           ;; search-forwards instead of the one re-search-forward and
5909           ;; a case (which basically was the old function) is actually
5910           ;; about twice as fast, even though it looks messier. You
5911           ;; can't have everything, I guess. Speed and elegance
5912           ;; doesn't always come hand in hand.
5913           (save-restriction
5914             (narrow-to-region (point) (save-excursion 
5915                                         (search-forward "\n.\n" nil t)))
5916             (if (search-forward "\nfrom: " nil t)
5917                 (header-set-from header (gnus-header-value))
5918               (header-set-from header "(nobody)"))
5919             (goto-char p)
5920             (if (search-forward "\nsubject: " nil t)
5921                 (header-set-subject header (gnus-header-value))
5922               (header-set-subject header "(none)"))
5923             (goto-char p)
5924             (and (search-forward "\nxref: " nil t)
5925                  (header-set-xref header (gnus-header-value)))
5926             (goto-char p)
5927             (and (search-forward "\nlines: " nil t)
5928                  (header-set-lines header (read cur)))
5929             (goto-char p)
5930             (and (search-forward "\ndate: " nil t)
5931                  (header-set-date header (gnus-header-value)))
5932             (goto-char p)
5933             (if (search-forward "\nmessage-id: " nil t)
5934                 (header-set-id header (setq id (gnus-header-value)))
5935               ;; If there was no message-id, we just fake one to make
5936               ;; subsequent routines simpler.
5937               (header-set-id 
5938                header 
5939                (setq id (concat "none+" (int-to-string 
5940                                          (setq none-id (1+ none-id)))))))
5941             (goto-char p)
5942             (if (search-forward "\nreferences: " nil t)
5943                 (progn
5944                   (header-set-references header (gnus-header-value))
5945                   (setq end (match-end 0))
5946                   (save-excursion
5947                     (setq ref 
5948                           (downcase
5949                            (buffer-substring
5950                             (progn 
5951                               (end-of-line)
5952                               (search-backward ">" end t)
5953                               (1+ (point)))
5954                             (progn
5955                               (search-backward "<" end t)
5956                               (point)))))))
5957               ;; Get the references from the in-reply-to header if there
5958               ;; ware no references and the in-reply-to header looks
5959               ;; promising. 
5960               (if (and (search-forward "\nin-reply-to: " nil t)
5961                        (setq in-reply-to (gnus-header-value))
5962                        (string-match "<[^>]+>" in-reply-to))
5963                   (progn
5964                     (header-set-references 
5965                      header 
5966                      (setq ref (substring in-reply-to (match-beginning 0)
5967                                           (match-end 0))))
5968                     (setq ref (downcase ref)))
5969                 (setq ref "none")))
5970             ;; We do some threading while we read the headers. The
5971             ;; message-id and the last reference are both entered into
5972             ;; the same hash table. Some tippy-toeing around has to be
5973             ;; done in case an article has arrived before the article
5974             ;; which it refers to.
5975             (if (boundp (setq dep (intern (downcase id) dependencies)))
5976                 (if (car (symbol-value dep))
5977                     ;; An article with this Message-ID has already
5978                     ;; been seen, so we ignore this one, except we add
5979                     ;; any additional Xrefs (in case the two articles
5980                     ;; came from different servers.
5981                     (progn
5982                       (header-set-xref 
5983                        (car (symbol-value dep))
5984                        (concat (or (header-xref (car (symbol-value dep))) "")
5985                                (or (header-xref header) "")))
5986                       (setq header nil))
5987                   (setcar (symbol-value dep) header))
5988               (set dep (list header)))
5989             (if header
5990                 (progn
5991                   (if (boundp (setq dep (intern ref dependencies)))
5992                       (setcdr (symbol-value dep) 
5993                               (cons header (cdr (symbol-value dep))))
5994                     (set dep (list nil header)))
5995                   (setq headers (cons header headers))))
5996             (goto-char (point-max))))))
5997     (nreverse headers)))
5998
5999 ;; The following macros and functions were written by Felix Lee
6000 ;; <flee@cse.psu.edu>. 
6001
6002 ;; This is almost 4x faster than (string-to-int (buffer-substring ... ))
6003 ;; primarily because of garbage collection.  -jwz
6004 (defmacro gnus-read-integer (&optional point move-p)
6005   (` ((, (if move-p 'progn 'save-excursion))
6006       (,@ (if point (list (list 'goto-char point))))
6007       (if (and (<= (following-char) ?9)
6008                (>= (following-char) ?0))
6009           (read (current-buffer))
6010         0))))
6011
6012 (defmacro gnus-nov-skip-field ()
6013   '(search-forward "\t" eol 'end))
6014
6015 (defmacro gnus-nov-field ()
6016   '(buffer-substring
6017     (point)
6018     (progn (gnus-nov-skip-field) (1- (point)))))
6019
6020 ;; Goes through the xover lines and returns a list of vectors
6021 (defun gnus-get-newsgroup-headers-xover (sequence)
6022   "Parse the news overview data in the server buffer, and return a
6023 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
6024   ;; Get the Xref when the users reads the articles since most/some
6025   ;; NNTP servers do not include Xrefs when using XOVER.
6026   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6027   (let ((cur nntp-server-buffer)
6028         (dependencies gnus-newsgroup-dependencies)
6029         (none 0)
6030         number headers header)
6031     (save-excursion
6032       (set-buffer nntp-server-buffer)
6033       (goto-char (point-min))
6034       (while (and sequence (not (eobp)))
6035         (setq number (read cur))
6036         (while (and sequence (< (car sequence) number))
6037           (setq sequence (cdr sequence)))
6038         (and sequence 
6039              (eq number (car sequence))
6040              (progn
6041                (setq sequence (cdr sequence))
6042                (if (setq header 
6043                          (inline (gnus-nov-parse-line number dependencies)))
6044                    (setq headers (cons header headers)))))
6045         (forward-line 1))
6046       (setq headers (nreverse headers)))
6047     headers))
6048
6049 (defun gnus-nov-parse-line (number dependencies)
6050   "Point has to be after the number on the beginning of the line."
6051   (let ((none 0)
6052         header eol ref id dep)
6053     (save-excursion
6054       (end-of-line)
6055       (setq eol (point)))
6056     (forward-char)
6057     ;; overview: [num subject from date id refs chars lines misc]
6058     (save-restriction
6059       (narrow-to-region (point) eol)
6060       (condition-case nil
6061           (setq header
6062                 (vector 
6063                  number                 ; number
6064                  (gnus-nov-field)       ; subject
6065                  (gnus-nov-field)       ; from
6066                  (gnus-nov-field)       ; date
6067                  (setq id (or (gnus-nov-field)
6068                               (concat "none+"
6069                                      (int-to-string 
6070                                       (setq none (1+ none)))))) ; id
6071                  (progn
6072                    (save-excursion
6073                      (let ((beg (point)))
6074                        (search-forward "\t" eol)
6075                        (if (search-backward ">" beg t)
6076                            (setq ref 
6077                                  (downcase 
6078                                   (buffer-substring 
6079                                    (1+ (point))
6080                                    (progn
6081                                      (search-backward "<" beg t)
6082                                      (point)))))
6083                          (setq ref nil))))
6084                    (gnus-nov-field))    ; refs
6085                  (read (current-buffer)) ; chars
6086                  (read (current-buffer)) ; lines
6087                  (if (/= (following-char) ?\t)
6088                      nil
6089                    (forward-char 1)
6090                    (gnus-nov-field))    ; misc
6091                  ))
6092         (error (progn 
6093                  (setq header nil)
6094                  (goto-char eol)))))
6095     ;; We build the thread tree.
6096     (and header
6097          (if (boundp (setq dep (intern (downcase id) dependencies)))
6098              (if (car (symbol-value dep))
6099                  ;; An article with this Message-ID has already been seen,
6100                  ;; so we ignore this one, except we add any additional
6101                  ;; Xrefs (in case the two articles came from different
6102                  ;; servers.
6103                  (progn
6104                    (header-set-xref 
6105                     (car (symbol-value dep))
6106                     (concat (or (header-xref (car (symbol-value dep))) "")
6107                             (or (header-xref header) "")))
6108                    (setq header nil))
6109                (setcar (symbol-value dep) header))
6110            (set dep (list header))))
6111     (if header
6112         (progn
6113           (if (boundp (setq dep (intern (or ref "none") 
6114                                         dependencies)))
6115               (setcdr (symbol-value dep) 
6116                       (cons header (cdr (symbol-value dep))))
6117             (set dep (list nil header)))))
6118     header))
6119
6120 (defun gnus-article-get-xrefs ()
6121   "Fill in the Xref value in `gnus-current-headers', if necessary.
6122 This is meant to be called in `gnus-article-internal-prepare-hook'."
6123   (or (not gnus-use-cross-reference)
6124       (header-xref gnus-current-headers)
6125       (let ((case-fold-search t)
6126             xref)
6127         (save-restriction
6128           (gnus-narrow-to-headers)
6129           (goto-char (point-min))
6130           (if (or (and (eq (downcase (following-char)) ?x)
6131                        (looking-at "Xref:"))
6132                   (search-forward "\nXref:" nil t))
6133               (progn
6134                 (goto-char (1+ (match-end 0)))
6135                 (setq xref (buffer-substring (point) 
6136                                              (progn (end-of-line) (point))))
6137                 (save-excursion
6138                   (set-buffer gnus-summary-buffer)
6139                   (header-set-xref gnus-current-headers xref))))))))
6140
6141 (defalias 'gnus-find-header-by-number 'gnus-get-header-by-number)
6142 (make-obsolete 'gnus-find-header-by-number 'gnus-get-header-by-number)
6143
6144 ;; Return a header specified by a NUMBER.
6145 (defun gnus-get-header-by-number (number)
6146   (save-excursion
6147     (set-buffer gnus-summary-buffer)
6148     (or gnus-newsgroup-headers-hashtb-by-number
6149         (gnus-make-headers-hashtable-by-number))
6150     (gnus-gethash (int-to-string number)
6151                   gnus-newsgroup-headers-hashtb-by-number)))
6152
6153 (defun gnus-make-headers-hashtable-by-number ()
6154   "Make hashtable for the variable gnus-newsgroup-headers by number."
6155   (save-excursion
6156     (set-buffer gnus-summary-buffer)
6157     (let ((headers gnus-newsgroup-headers)
6158           header)
6159       (setq gnus-newsgroup-headers-hashtb-by-number
6160             (gnus-make-hashtable (length headers)))
6161       (while headers
6162         (setq header (car headers))
6163         (gnus-sethash (int-to-string (header-number header))
6164                       header gnus-newsgroup-headers-hashtb-by-number)
6165         (setq headers (cdr headers))))))
6166
6167 (defun gnus-more-header-backward ()
6168   "Find new header backward."
6169   (let ((first (car (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6170         (artnum gnus-newsgroup-begin)
6171         (header nil))
6172     (while (and (not header)
6173                 (> artnum first))
6174       (setq artnum (1- artnum))
6175       (setq header (gnus-read-header artnum)))
6176     header))
6177
6178 (defun gnus-more-header-forward (&optional backward)
6179   "Find new header forward.
6180 If BACKWARD, find new header backward instead."
6181   (if backward
6182       (gnus-more-header-backward)
6183     (let ((last (cdr (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6184           (artnum gnus-newsgroup-end)
6185           (header nil))
6186       (while (and (not header)
6187                   (< artnum last))
6188         (setq artnum (1+ artnum))
6189         (setq header (gnus-read-header artnum)))
6190       header)))
6191
6192 (defun gnus-extend-newsgroup (header &optional backward)
6193   "Extend newsgroup selection with HEADER.
6194 Optional argument BACKWARD means extend toward backward."
6195   (if header
6196       (let ((artnum (header-number header)))
6197         (setq gnus-newsgroup-headers
6198               (if backward
6199                   (cons header gnus-newsgroup-headers)
6200                 (nconc gnus-newsgroup-headers (list header))))
6201         (setq gnus-newsgroup-unselected
6202               (delq artnum gnus-newsgroup-unselected))
6203         (setq gnus-newsgroup-begin (min gnus-newsgroup-begin artnum))
6204         (setq gnus-newsgroup-end (max gnus-newsgroup-end artnum)))))
6205
6206 (defun gnus-summary-work-articles (n)
6207   "Return a list of articles to be worked upon. The prefix argument,
6208 the list of process marked articles, and the current article will be
6209 taken into consideration."
6210   (let (articles)
6211     (if (and n (numberp n))
6212         (let ((backward (< n 0))
6213               (n (abs n)))
6214           (save-excursion
6215             (while (and (> n 0)
6216                         (setq articles (cons (gnus-summary-article-number) 
6217                                              articles))
6218                         (gnus-summary-search-forward nil nil backward))
6219               (setq n (1- n))))
6220           (sort articles (function <)))
6221       (or (reverse gnus-newsgroup-processable)
6222           (list (gnus-summary-article-number))))))
6223
6224 (defun gnus-summary-search-group (&optional backward use-level)
6225   "Search for next unread newsgroup.
6226 If optional argument BACKWARD is non-nil, search backward instead."
6227   (save-excursion
6228     (set-buffer gnus-group-buffer)
6229     (save-excursion
6230       ;; We don't want to alter current point of group mode buffer.
6231       (if (gnus-group-search-forward 
6232            backward nil
6233            (if use-level (gnus-group-group-level) nil))
6234           (gnus-group-group-name)))))
6235
6236 (defun gnus-summary-best-group (&optional exclude-group)
6237   "Find the name of the best unread group.
6238 If EXCLUDE-GROUP, do not go to this group."
6239   (save-excursion
6240     (set-buffer gnus-group-buffer)
6241     (save-excursion
6242       (gnus-group-best-unread-group exclude-group))))
6243
6244 (defun gnus-summary-search-subject (&optional backward unread subject)
6245   "Search for article forward.
6246 If BACKWARD is non-nil, search backward.
6247 If UNREAD is non-nil, only unread articles are selected.
6248 If SUBJECT is non-nil, the article which has the same subject will be
6249 searched for." 
6250   (let ((func (if backward 'previous-single-property-change
6251                 'next-single-property-change))
6252         (beg (point))
6253         (did t)
6254         pos)
6255     (beginning-of-line)
6256     (forward-char (if backward (if (bobp) 0 -1) (if (eobp) 0 1)))
6257     (while (and (setq pos (funcall func (point) 'gnus-number))
6258                 (goto-char (if backward (1- pos) pos))
6259                 (setq did
6260                       (not (and (or (not unread)
6261                                     (eq (get-text-property (point) 'gnus-mark) 
6262                                         gnus-unread-mark))
6263                                 (or (not subject)
6264                                     (equal (gnus-simplify-subject-re 
6265                                             subject)
6266                                            (gnus-simplify-subject-re
6267                                             (get-text-property 
6268                                              (point) 
6269                                              'gnus-subject)))))))
6270                 (if backward (if (bobp) nil (forward-char -1) t)
6271                   (if (eobp) nil (forward-char 1) t))))
6272     (if did
6273         (progn (goto-char beg) nil)
6274       (prog1
6275           (get-text-property (point) 'gnus-number)
6276         (gnus-summary-position-cursor)))))
6277
6278 (defun gnus-summary-search-forward (&optional unread subject backward)
6279   "Search for article forward.
6280 If UNREAD is non-nil, only unread articles are selected.
6281 If SUBJECT is non-nil, the article which has the same subject will be
6282 searched for. 
6283 If BACKWARD is non-nil, the search will be performed backwards instead."
6284   (gnus-summary-search-subject backward unread subject))
6285
6286 (defun gnus-summary-search-backward (&optional unread subject)
6287   "Search for article backward.
6288 If 1st optional argument UNREAD is non-nil, only unread article is selected.
6289 If 2nd optional argument SUBJECT is non-nil, the article which has
6290 the same subject will be searched for."
6291   (gnus-summary-search-forward unread subject t))
6292
6293 (defun gnus-summary-article-number (&optional number-or-nil)
6294   "The article number of the article on the current line.
6295 If there isn's an article number here, then we return the current
6296 article number."
6297   (let* ((p (point))
6298          (number (get-text-property 
6299                   (progn (beginning-of-line) (prog1 (point) (goto-char p)))
6300                   'gnus-number)))
6301     (if number-or-nil number (or number gnus-current-article))))
6302
6303 (defun gnus-summary-thread-level ()
6304   "The thread level of the article on the current line."
6305   (or (get-text-property 
6306        (save-excursion (beginning-of-line) (point)) 'gnus-thread)
6307       0))
6308
6309 (defun gnus-summary-pseudo-article ()
6310   "The thread level of the article on the current line."
6311   (get-text-property 
6312    (save-excursion (beginning-of-line) (point)) 'gnus-pseudo))
6313
6314 (defun gnus-summary-article-mark ()
6315   "The mark on the current line."
6316   (get-text-property (save-excursion (beginning-of-line) (point)) 'gnus-mark))
6317
6318 (defun gnus-summary-subject-string ()
6319   "Return current subject string or nil if nothing."
6320   (get-text-property 
6321    (save-excursion (beginning-of-line) (point)) 'gnus-subject))
6322
6323 (defalias 'gnus-summary-score 'gnus-summary-article-score)
6324 (make-obsolete 'gnus-summary-score 'gnus-summary-article-score)
6325 (defun gnus-summary-article-score ()
6326   "Return current article score."
6327   (or (cdr (assq (gnus-summary-article-number) gnus-newsgroup-scored))
6328       gnus-summary-default-score 0))
6329
6330 (defun gnus-summary-recenter ()
6331   "Center point in the summary window.
6332 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6333 displayed, no centering will be performed." 
6334   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6335   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
6336   (let ((top (cond ((< (window-height) 4) 0)
6337                    ((< (window-height) 6) 1)
6338                    (t 2))))
6339     (and 
6340      ;; The user has to want it,
6341      gnus-auto-center-summary 
6342      ;; the article buffer must be displayed,
6343      (get-buffer-window gnus-article-buffer)
6344      ;; there must be lines left to scroll forward,
6345      (zerop (save-excursion (forward-line (- (window-height) 1 top))))
6346      ;; so we recenter.
6347      (set-window-start 
6348       (get-buffer-window (current-buffer)) 
6349       (save-excursion (forward-line (- top)) (point))))))
6350
6351 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
6352 (defun gnus-short-group-name (newsgroup)
6353   "Convert a long group name to an initialized form.
6354 The last part of the name is left intact: \"rec.arts.anime\" becomes
6355 \"r.a.anime\"."
6356   (let ((name ""))
6357     (while newsgroup
6358       (if (string-match "\\." newsgroup)
6359           (progn
6360             (setq name (concat name (substring newsgroup 0 1))
6361                   newsgroup (substring newsgroup (match-end 0))
6362                   name (concat name ".")))
6363           (setq name (concat name newsgroup)
6364                 newsgroup nil)))
6365     name))
6366
6367
6368 (defun gnus-summary-jump-to-group (newsgroup)
6369   "Move point to NEWSGROUP in group mode buffer."
6370   ;; Keep update point of group mode buffer if visible.
6371   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6372       (save-window-excursion
6373         ;; Take care of tree window mode.
6374         (if (get-buffer-window gnus-group-buffer)
6375             (pop-to-buffer gnus-group-buffer))
6376         (gnus-group-jump-to-group newsgroup))
6377     (save-excursion
6378       ;; Take care of tree window mode.
6379       (if (get-buffer-window gnus-group-buffer)
6380           (pop-to-buffer gnus-group-buffer)
6381         (set-buffer gnus-group-buffer))
6382       (gnus-group-jump-to-group newsgroup))))
6383
6384 ;; This function returns a list of article numbers based on the
6385 ;; difference between the ranges of read articles in this group and
6386 ;; the range of active articles.
6387 (defun gnus-list-of-unread-articles (group)
6388   (let* ((read (nth 2 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6389          (active (gnus-gethash group gnus-active-hashtb))
6390          (last (cdr active))
6391          unread first nlast unread)
6392     ;; If none are read, then all are unread. 
6393     (if (not read)
6394         (setq first (car active))
6395       ;; If the range of read articles is a single range, then the
6396       ;; first unread article is the article after the last read
6397       ;; article. Sounds logical, doesn't it?
6398       (if (not (listp (cdr read)))
6399           (setq first (1+ (cdr read)))
6400         ;; `read' is a list of ranges.
6401         (if (/= (setq nlast (or (and (numberp (car read)) (car read)) 
6402                                 (car (car read)))) 1)
6403             (setq first 1))
6404         (while read
6405           (if first 
6406               (while (< first nlast)
6407                 (setq unread (cons first unread))
6408                 (setq first (1+ first))))
6409           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
6410           (setq nlast (if (atom (car (cdr read))) 
6411                           (car (cdr read))
6412                         (car (car (cdr read)))))
6413           (setq read (cdr read)))))
6414     ;; And add the last unread articles.
6415     (while (<= first last)
6416       (setq unread (cons first unread))
6417       (setq first (1+ first)))
6418     ;; Return the list of unread articles.
6419     (nreverse unread)))
6420
6421
6422 ;; Various summary commands
6423
6424 (defun gnus-summary-universal-argument ()
6425   "Perform any operation on all articles marked with the process mark."
6426   (interactive)
6427   (gnus-set-global-variables)
6428   (let ((articles (reverse gnus-newsgroup-processable))
6429         key func)
6430     (or articles (error "No articles marked"))
6431     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
6432         (error "Undefined key"))
6433     (while articles
6434       (gnus-summary-goto-subject (car articles))
6435       (command-execute func)
6436       (gnus-summary-remove-process-mark (car articles))
6437       (setq articles (cdr articles)))))
6438
6439 (defun gnus-summary-toggle-truncation (arg)
6440   "Toggle truncation of summary lines.
6441 With arg, turn line truncation on iff arg is positive."
6442   (interactive "P")
6443   (setq truncate-lines
6444         (if (null arg) (not truncate-lines)
6445           (> (prefix-numeric-value arg) 0)))
6446   (redraw-display))
6447
6448 (defun gnus-summary-reselect-current-group (all)
6449   "Once exit and then reselect the current newsgroup.
6450 The prefix argument ALL means to select all articles."
6451   (interactive "P")
6452   (gnus-set-global-variables)
6453   (let ((current-subject (gnus-summary-article-number)))
6454     (gnus-summary-exit t)
6455     ;; We have to adjust the point of group mode buffer because the
6456     ;; current point was moved to the next unread newsgroup by
6457     ;; exiting.
6458     (gnus-summary-jump-to-group gnus-newsgroup-name)
6459     (gnus-group-read-group all t)
6460     (gnus-summary-goto-subject current-subject)))
6461
6462 (defun gnus-summary-rescan-group (all)
6463   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6464   (interactive "P")
6465   (gnus-set-global-variables)
6466   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
6467   (let ((group gnus-newsgroup-name))
6468     (gnus-summary-exit t)
6469     (gnus-summary-jump-to-group group)
6470     (save-excursion
6471       (set-buffer gnus-group-buffer)
6472       (gnus-group-get-new-news-this-group 1))
6473     (gnus-summary-jump-to-group group)
6474     (gnus-group-read-group all)))
6475
6476 (defun gnus-summary-exit (&optional temporary)
6477   "Exit reading current newsgroup, and then return to group selection mode.
6478 gnus-exit-group-hook is called with no arguments if that value is non-nil."
6479   (interactive)
6480   (gnus-set-global-variables)
6481   (gnus-kill-save-kill-buffer)
6482   (let* ((group gnus-newsgroup-name)
6483          (quit-buffer (cdr (assoc 'quit-buffer (gnus-find-method-for-group
6484                                                 gnus-newsgroup-name))))
6485          (mode major-mode)
6486          (method (car (gnus-find-method-for-group group)))
6487          (buf (current-buffer)))
6488     (if gnus-newsgroup-kill-headers
6489         (setq gnus-newsgroup-killed
6490               (gnus-compress-sequence
6491                (nconc
6492                 (gnus-set-sorted-intersection
6493                  (gnus-uncompress-range gnus-newsgroup-killed)
6494                  (setq gnus-newsgroup-unselected
6495                        (sort gnus-newsgroup-unselected '<)))
6496                 (setq gnus-newsgroup-unreads
6497                       (sort gnus-newsgroup-unreads '<))) t)))
6498     (or (listp (cdr gnus-newsgroup-killed))
6499         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6500     (let ((updated nil)
6501           (headers gnus-newsgroup-headers))
6502       (gnus-close-group group)
6503       (run-hooks 'gnus-exit-group-hook)
6504       (gnus-score-save)
6505       (gnus-update-read-articles 
6506        group gnus-newsgroup-unreads gnus-newsgroup-unselected 
6507        gnus-newsgroup-marked
6508        t gnus-newsgroup-replied gnus-newsgroup-expirable
6509        gnus-newsgroup-killed gnus-newsgroup-dormant
6510        gnus-newsgroup-bookmarks 
6511        (and gnus-save-score gnus-newsgroup-scored))
6512       (and gnus-use-cross-reference
6513            (gnus-mark-xrefs-as-read 
6514             group headers gnus-newsgroup-unreads gnus-newsgroup-expirable))
6515       ;; Do not switch windows but change the buffer to work.
6516       (set-buffer gnus-group-buffer)
6517       (or (eq 'nndigest method)
6518           (gnus-group-update-group group)))
6519     ;; Make sure where I was, and go to next newsgroup.
6520     (if (eq method 'nndigest)
6521         ()
6522       (gnus-group-jump-to-group group)
6523       (gnus-group-next-unread-group 1))
6524     (if temporary
6525         ;; If exiting temporary, caller should adjust group mode
6526         ;; buffer point by itself.
6527         nil                             ;Nothing to do.
6528       ;; We set all buffer-local variables to nil. It is unclear why
6529       ;; this is needed, but if we don't, buffer-local variables are
6530       ;; not garbage-collected, it seems. This would the lead to en
6531       ;; ever-growing Emacs.
6532       (set-buffer buf)
6533       (gnus-summary-clear-local-variables)
6534       ;; We clear the global counterparts of the buffer-local
6535       ;; variables as well, just to be on the safe side.
6536       (set-buffer gnus-group-buffer)
6537       (gnus-summary-clear-local-variables)
6538       (gnus-configure-windows 'newsgroups t)
6539       ;; Return to group mode buffer. 
6540       (and (get-buffer buf) 
6541            (eq mode 'gnus-summary-mode)
6542            (kill-buffer buf))
6543       (if (get-buffer gnus-article-buffer)
6544           (bury-buffer gnus-article-buffer))
6545       (setq gnus-current-select-method gnus-select-method)
6546       (pop-to-buffer gnus-group-buffer)
6547       (if (and quit-buffer (buffer-name quit-buffer))
6548           (progn
6549             (switch-to-buffer quit-buffer)
6550             (gnus-set-global-variables)
6551             (gnus-configure-windows 'summary))))))
6552
6553 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6554 (defun gnus-summary-exit-no-update (&optional no-questions)
6555   "Quit reading current newsgroup without updating read article info."
6556   (interactive)
6557   (let* ((group gnus-newsgroup-name)
6558          (quit-buffer (cdr (assoc 'quit-buffer 
6559                                   (gnus-find-method-for-group group)))))
6560     (if (or no-questions
6561             gnus-expert-user
6562             (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
6563         (progn
6564           (gnus-close-group group)
6565           (gnus-summary-clear-local-variables)
6566           (set-buffer gnus-group-buffer)
6567           (gnus-summary-clear-local-variables)
6568           ;; Return to group selection mode.
6569           (gnus-configure-windows 'newsgroups)
6570           (if (get-buffer gnus-summary-buffer)
6571               (kill-buffer gnus-summary-buffer))
6572           (if (get-buffer gnus-article-buffer)
6573               (bury-buffer gnus-article-buffer))
6574           (pop-to-buffer gnus-group-buffer)
6575           (gnus-group-jump-to-group group)
6576           (gnus-group-next-group 1)
6577           (if (and quit-buffer (buffer-name quit-buffer))
6578               (progn
6579                 (switch-to-buffer quit-buffer)
6580                 (gnus-configure-windows 'summary)))))))
6581
6582 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6583 (defun gnus-summary-fetch-faq (group)
6584   "Fetch the FAQ for the current group."
6585   (interactive (list gnus-newsgroup-name))
6586   (gnus-configure-windows 'article)
6587   (pop-to-buffer gnus-article-buffer)
6588   (find-file (concat gnus-group-faq-directory group)))
6589
6590 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6591 (defun gnus-summary-describe-group (force)
6592   "Describe the current newsgroup."
6593   (interactive "P")
6594   (gnus-group-describe-group force gnus-newsgroup-name))
6595
6596 (defun gnus-summary-describe-briefly ()
6597   "Describe summary mode commands briefly."
6598   (interactive)
6599   (message
6600     (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")))
6601
6602 ;; Walking around group mode buffer from summary mode.
6603
6604 (defun gnus-summary-next-group (&optional no-article group backward)
6605   "Exit current newsgroup and then select next unread newsgroup.
6606 If prefix argument NO-ARTICLE is non-nil, no article is selected initially.
6607 If BACKWARD, go to previous group instead."
6608   (interactive "P")
6609   (gnus-set-global-variables)
6610   (let ((ingroup gnus-newsgroup-name)
6611         (sumbuf (current-buffer))
6612         num)
6613     (gnus-summary-exit t)               ;Update all information.
6614     (if (and group
6615              (or (and (numberp (setq num (car (gnus-gethash
6616                                                group gnus-newsrc-hashtb))))
6617                       (< num 1))
6618                  (null num)))
6619         (progn
6620           (gnus-group-jump-to-group group)
6621           (setq group nil))
6622       (gnus-group-jump-to-group ingroup))
6623     (let ((group (or group (gnus-summary-search-group backward)))
6624           (buf gnus-summary-buffer))
6625       (if (null group)
6626           (gnus-summary-exit-no-update t)
6627         (message "Selecting %s..." group)
6628         ;; We are now in group mode buffer.
6629         ;; Make sure group mode buffer point is on GROUP.
6630         (gnus-group-jump-to-group group)
6631         (unwind-protect
6632             (gnus-summary-read-group group nil no-article buf)
6633           (and (string= gnus-newsgroup-name ingroup)
6634                (bufferp sumbuf) (buffer-name sumbuf)
6635                (progn
6636                  (set-buffer (setq gnus-summary-buffer sumbuf))
6637                  (gnus-summary-exit-no-update t))))))))
6638
6639 (defun gnus-summary-prev-group (no-article)
6640   "Exit current newsgroup and then select previous unread newsgroup.
6641 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6642   (interactive "P")
6643   (gnus-summary-next-group no-article nil t))
6644
6645 ;; Walking around summary lines.
6646
6647 (defun gnus-summary-first-subject (unread)
6648   "Go to the first unread subject.
6649 If UNREAD is non-nil, go to the first unread article.
6650 Returns nil if there are no unread articles."
6651   (interactive "P")
6652   (let ((begin (point)))
6653     (if unread
6654         (if (not (gnus-goto-char 
6655                   (text-property-any (point-min) (point-max)
6656                                      'gnus-mark gnus-unread-mark)))
6657             (progn
6658               ;; If there is no unread articles, stay where you are.
6659               (goto-char begin)
6660               (message "No more unread articles")
6661               nil)
6662           t)
6663       (goto-char (point-min)))))
6664
6665 (defun gnus-summary-next-subject (n &optional unread)
6666   "Go to next N'th summary line.
6667 If N is negative, go to the previous N'th subject line.
6668 If UNREAD is non-nil, only unread articles are selected.
6669 The difference between N and the actual number of steps taken is
6670 returned."
6671   (interactive "p")
6672   (let ((backward (< n 0))
6673         (n (abs n)))
6674   (while (and (> n 0)
6675               (gnus-summary-search-forward unread nil backward))
6676     (setq n (1- n)))
6677   (gnus-summary-recenter)
6678   (if (/= 0 n) (message "No more%s articles" (if unread " unread" "")))
6679   (gnus-summary-position-cursor)
6680   n))
6681
6682 (defun gnus-summary-next-unread-subject (n)
6683   "Go to next N'th unread summary line."
6684   (interactive "p")
6685   (gnus-summary-next-subject n t))
6686
6687 (defun gnus-summary-prev-subject (n &optional unread)
6688   "Go to previous N'th summary line.
6689 If optional argument UNREAD is non-nil, only unread article is selected."
6690   (interactive "p")
6691   (gnus-summary-next-subject (- n) unread))
6692
6693 (defun gnus-summary-prev-unread-subject (n)
6694   "Go to previous N'th unread summary line."
6695   (interactive "p")
6696   (gnus-summary-next-subject (- n) t))
6697
6698 (defun gnus-summary-goto-subject (article)
6699   "Go the subject line of ARTICLE."
6700   (interactive
6701    (list
6702     (string-to-int
6703      (completing-read "Article number: "
6704                       (mapcar
6705                        (lambda (headers)
6706                          (list
6707                           (int-to-string (header-number headers))))
6708                        gnus-newsgroup-headers)
6709                       nil 'require-match))))
6710   (or article (error "No article number"))
6711   (if (or (eq article (gnus-summary-article-number t))
6712           (gnus-goto-char
6713            (text-property-any
6714             (point-min) (point-max) 'gnus-number article)))
6715       article))
6716
6717 ;; Walking around summary lines with displaying articles.
6718
6719 (defun gnus-summary-expand-window ()
6720   "Expand summary window to show headers full window."
6721   (interactive)
6722   (gnus-set-global-variables)
6723   (gnus-configure-windows 'summary)
6724   (pop-to-buffer gnus-summary-buffer))
6725
6726 (defun gnus-summary-display-article (article &optional all-header)
6727   "Display ARTICLE in article buffer."
6728   (gnus-set-global-variables)
6729   (if (null article)
6730       nil
6731     (gnus-article-prepare article all-header)
6732     (if (eq (gnus-summary-article-mark) ?Z)
6733         (progn
6734           (forward-line 1)
6735           (gnus-summary-position-cursor)))
6736     (run-hooks 'gnus-select-article-hook)
6737     (gnus-summary-recenter)
6738 ;    (set-window-point (get-buffer-window (current-buffer)) (point-max))
6739 ;    (sit-for 0)
6740     (gnus-summary-goto-subject article)
6741     ;; Successfully display article.
6742     (gnus-summary-update-line)
6743     t))
6744
6745 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6746   "Select the current article.
6747 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6748 non-nil, the article will be re-fetched even if it already present in
6749 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6750 be displayed."
6751   (and (not pseudo) (gnus-summary-pseudo-article)
6752        (error "This is a pseudo-article."))
6753   (let ((article (or article (gnus-summary-article-number)))
6754         (all-headers (not (not all-headers)))) ;Must be T or NIL.
6755     (if (or (null gnus-current-article)
6756             (null gnus-article-current)
6757             (/= article (cdr gnus-article-current))
6758             (not (equal (car gnus-article-current) gnus-newsgroup-name))
6759             force)
6760         ;; The requested article is different from the current article.
6761         (progn
6762           (gnus-summary-display-article article all-headers)
6763           article)
6764       (if all-headers (gnus-article-show-all-headers))
6765       (gnus-configure-windows 'article)
6766       (pop-to-buffer gnus-summary-buffer)
6767       nil)))
6768
6769 (defun gnus-summary-set-current-mark (&optional current-mark)
6770   "Obsolete function."
6771   nil)
6772
6773 (defun gnus-summary-next-article (unread &optional subject backward)
6774   "Select the next article.
6775 If UNREAD, only unread articles are selected.
6776 If SUBJECT, only articles with SUBJECT are selected.
6777 If BACKWARD, the previous article is selected instead of the next."
6778   (interactive "P")
6779   (let ((opoint (point))
6780         (method (car (gnus-find-method-for-group gnus-newsgroup-name)))
6781         header)
6782     (cond
6783      ;; Is there such an article?
6784      ((gnus-summary-display-article 
6785        (gnus-summary-search-forward unread subject backward))
6786       (gnus-summary-position-cursor))
6787      ;; If not, we try the first unread, if that is wanted.
6788      ((and subject
6789            gnus-auto-select-same
6790            (gnus-summary-first-unread-article))
6791       (message "Wrapped"))
6792      ;; Try to get next/previous article not displayed in this group.
6793      ((and gnus-auto-extend-newsgroup
6794            (not unread) (not subject)
6795            (setq header (gnus-more-header-forward backward)))
6796       (gnus-extend-newsgroup header backward)
6797       (let ((buffer-read-only nil))
6798         (goto-char (if backward (point-min) (point-max)))
6799         (gnus-summary-prepare-threads (list header) 0))
6800       (gnus-summary-goto-article (if backward gnus-newsgroup-begin
6801                                    gnus-newsgroup-end)))
6802      ;; Go to next/previous group.
6803      (t
6804       (gnus-summary-jump-to-group gnus-newsgroup-name)
6805       (let ((cmd (aref (this-command-keys) 0))
6806             (group 
6807              (if (eq gnus-keep-same-level 'best) 
6808                  (gnus-summary-best-group gnus-newsgroup-name)
6809                (gnus-summary-search-group backward gnus-keep-same-level))))
6810         ;; Keep just the event type of CMD.
6811         (and (listp cmd) (setq cmd (car cmd)))
6812         ;; Select next unread newsgroup automagically.
6813         (cond 
6814          ((not gnus-auto-select-next)
6815           (message "No more%s articles" (if unread " unread" "")))
6816          ((eq gnus-auto-select-next 'quietly)
6817           ;; Select quietly.
6818           (if (eq method 'nndigest)
6819               (gnus-summary-exit)
6820             (message "No more%s articles (%s)..."
6821                      (if unread " unread" "") 
6822                      (if group (concat "selecting " group)
6823                        "exiting"))
6824             (gnus-summary-next-group nil group backward)))
6825          (t
6826           (let ((keystrokes '(?\C-n ?\C-p))
6827                 key)
6828             (while (or (null key) (memq key keystrokes))
6829               (message 
6830                "No more%s articles%s" (if unread " unread" "")
6831                (if (and group (not (eq method 'nndigest)))
6832                    (format " (Type %s for %s [%s])"
6833                            (single-key-description cmd) group
6834                            (car (gnus-gethash group gnus-newsrc-hashtb)))
6835                  (format " (Type %s to exit %s)"
6836                          (single-key-description cmd)
6837                          gnus-newsgroup-name)))
6838               ;; Confirm auto selection.
6839               (let* ((event (read-event)))
6840                 (setq key (if (listp event) (car event) event))
6841                 (if (member key keystrokes)
6842                     (let ((obuf (current-buffer)))
6843                       (switch-to-buffer gnus-group-buffer)
6844                       (gnus-group-jump-to-group group)
6845                       (execute-kbd-macro (char-to-string key))
6846                       (setq group (gnus-group-group-name))
6847                       (switch-to-buffer obuf)))))
6848             (if (eq key cmd)
6849                 (if (or (not group) (eq method 'nndigest))
6850                     (gnus-summary-exit)
6851                   (gnus-summary-next-group nil group backward))
6852               (setq unread-command-events (list key)))))))))))
6853
6854 (defun gnus-summary-next-unread-article ()
6855   "Select unread article after current one."
6856   (interactive)
6857   (gnus-summary-next-article t (and gnus-auto-select-same
6858                                     (gnus-summary-subject-string))))
6859
6860 (defun gnus-summary-prev-article (unread &optional subject)
6861   "Select the article after the current one.
6862 If UNREAD is non-nil, only unread articles are selected."
6863   (interactive "P")
6864   (gnus-summary-next-article unread subject t))
6865
6866 (defun gnus-summary-prev-unread-article ()
6867   "Select unred article before current one."
6868   (interactive)
6869   (gnus-summary-prev-article t (and gnus-auto-select-same
6870                                     (gnus-summary-subject-string))))
6871
6872 (defun gnus-summary-next-page (lines &optional circular)
6873   "Show next page of selected article.
6874 If end of article, select next article.
6875 Argument LINES specifies lines to be scrolled up.
6876 If CIRCULAR is non-nil, go to the start of the article instead of 
6877 instead of selecting the next article when reaching the end of the
6878 current article." 
6879   (interactive "P")
6880   (setq gnus-summary-buffer (current-buffer))
6881   (let ((article (gnus-summary-article-number))
6882         (endp nil))
6883     (if (or (null gnus-current-article)
6884             (null gnus-article-current)
6885             (/= article (cdr gnus-article-current))
6886             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6887         ;; Selected subject is different from current article's.
6888         (gnus-summary-display-article article)
6889       (gnus-configure-windows 'article)
6890       (pop-to-buffer gnus-summary-buffer)
6891       (gnus-eval-in-buffer-window
6892        gnus-article-buffer
6893        (setq endp (gnus-article-next-page lines)))
6894       (if endp
6895           (cond (circular
6896                  (gnus-summary-beginning-of-article))
6897                 (lines
6898                  (message "End of message"))
6899                 ((null lines)
6900                  (gnus-summary-next-unread-article))))))
6901   (gnus-summary-position-cursor))
6902
6903 (defun gnus-summary-prev-page (lines)
6904   "Show previous page of selected article.
6905 Argument LINES specifies lines to be scrolled down."
6906   (interactive "P")
6907   (let ((article (gnus-summary-article-number)))
6908     (if (or (null gnus-current-article)
6909             (null gnus-article-current)
6910             (/= article (cdr gnus-article-current))
6911             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6912         ;; Selected subject is different from current article's.
6913         (gnus-summary-display-article article)
6914       (gnus-configure-windows 'article)
6915       (pop-to-buffer gnus-summary-buffer)
6916       (gnus-eval-in-buffer-window gnus-article-buffer
6917         (gnus-article-prev-page lines))))
6918   (gnus-summary-position-cursor))
6919
6920 (defun gnus-summary-scroll-up (lines)
6921   "Scroll up (or down) one line current article.
6922 Argument LINES specifies lines to be scrolled up (or down if negative)."
6923   (interactive "p")
6924   (or (gnus-summary-select-article nil nil 'pseudo)
6925       (gnus-eval-in-buffer-window 
6926        gnus-article-buffer
6927        (cond ((> lines 0)
6928               (if (gnus-article-next-page lines)
6929                   (message "End of message")))
6930              ((< lines 0)
6931               (gnus-article-prev-page (- lines))))))
6932   (gnus-summary-position-cursor))
6933
6934 (defun gnus-summary-next-same-subject ()
6935   "Select next article which has the same subject as current one."
6936   (interactive)
6937   (gnus-summary-next-article nil (gnus-summary-subject-string)))
6938
6939 (defun gnus-summary-prev-same-subject ()
6940   "Select previous article which has the same subject as current one."
6941   (interactive)
6942   (gnus-summary-prev-article nil (gnus-summary-subject-string)))
6943
6944 (defun gnus-summary-next-unread-same-subject ()
6945   "Select next unread article which has the same subject as current one."
6946   (interactive)
6947   (gnus-summary-next-article t (gnus-summary-subject-string)))
6948
6949 (defun gnus-summary-prev-unread-same-subject ()
6950   "Select previous unread article which has the same subject as current one."
6951   (interactive)
6952   (gnus-summary-prev-article t (gnus-summary-subject-string)))
6953
6954 (defun gnus-summary-first-unread-article ()
6955   "Select the first unread article. 
6956 Return nil if there are no unread articles."
6957   (interactive)
6958   (prog1
6959       (if (gnus-summary-first-subject t)
6960           (gnus-summary-display-article (gnus-summary-article-number)))
6961     (gnus-summary-position-cursor)))
6962
6963 (defun gnus-summary-best-unread-article ()
6964   "Select the unread article with the highest score."
6965   (interactive)
6966   (gnus-set-global-variables)
6967   (let ((scored gnus-newsgroup-scored)
6968         (best -1000000)
6969         article art)
6970     (while scored
6971       (or (> best (cdr (car scored)))
6972           (and (memq (setq art (car (car scored))) gnus-newsgroup-unreads)
6973                (not (memq art gnus-newsgroup-marked))
6974                (not (memq art gnus-newsgroup-dormant))
6975                (if (= best (cdr (car scored)))
6976                    (setq article (min art article))
6977                  (setq article art)
6978                  (setq best (cdr (car scored))))))
6979       (setq scored (cdr scored)))
6980     (if article 
6981         (gnus-summary-goto-article article)
6982       (gnus-summary-first-unread-article))
6983     (gnus-summary-position-cursor)))
6984
6985 (defun gnus-summary-goto-article (article &optional all-headers)
6986   "Fetch ARTICLE and display it if it exists.
6987 If ALL-HEADERS is non-nil, no header lines are hidden."
6988   (interactive
6989    (list
6990     (string-to-int
6991      (completing-read 
6992       "Article number: "
6993       (mapcar (lambda (headers) (list (int-to-string (header-number headers))))
6994               gnus-newsgroup-headers) 
6995       nil 'require-match))))
6996   (if (gnus-summary-goto-subject article)
6997       (gnus-summary-display-article article all-headers))
6998   (gnus-summary-position-cursor))
6999
7000 (defun gnus-summary-goto-last-article ()
7001   "Go to the last article."
7002   (interactive)
7003   (if gnus-last-article
7004       (gnus-summary-goto-article gnus-last-article))
7005   (gnus-summary-position-cursor))
7006
7007 (defun gnus-summary-pop-article (number)
7008   "Pop one article off the history and go to the previous.
7009 NUMBER articles will be popped off."
7010   (interactive "p")
7011   (let (to)
7012     (setq gnus-newsgroup-history
7013           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7014     (if to
7015         (gnus-summary-goto-article (car to))
7016       (error "Article history empty")))
7017   (gnus-summary-position-cursor))
7018
7019 ;; Summary article oriented commands
7020
7021 (defun gnus-summary-refer-parent-article (n)
7022   "Refer parent article N times.
7023 The difference between N and the number of articles fetched is returned."
7024   (interactive "p")
7025   (gnus-set-global-variables)
7026   (while 
7027       (and 
7028        (> n 0)
7029        (let ((ref (header-references (gnus-get-header-by-number
7030                                       (gnus-summary-article-number)))))
7031          (if (and ref (not (equal ref ""))
7032                   (string-match "<[^<>]*>[ \t]*$" ref))
7033              (gnus-summary-refer-article 
7034               (substring ref (match-beginning 0) (match-end 0))))))
7035     (setq n (1- n)))
7036   (or (zerop n) (message "No references in article or expired article."))
7037   (gnus-summary-position-cursor)
7038   n)
7039     
7040 (defun gnus-summary-refer-article (message-id)
7041   "Refer article specified by MESSAGE-ID.
7042 NOTE: This command only works with newsgroup that use NNTP."
7043   (interactive "sMessage-ID: ")
7044   (if (or (not (stringp message-id))
7045           (zerop (length message-id)))
7046       ()
7047     ;; Construct the correct Message-ID if necessary.
7048     ;; Suggested by tale@pawl.rpi.edu.
7049     (or (string-match "^<" message-id)
7050         (setq message-id (concat "<" message-id)))
7051     (or (string-match ">$" message-id)
7052         (setq message-id (concat message-id ">")))
7053     (let ((header (car (gnus-gethash (downcase message-id)
7054                                      gnus-newsgroup-dependencies))))
7055       (if header
7056           (gnus-summary-goto-article (header-number header))
7057         (let ((gnus-override-method gnus-refer-article-method))
7058           (if (gnus-article-prepare 
7059                message-id nil (gnus-read-header message-id))
7060               (progn
7061                 (gnus-summary-insert-line 
7062                  nil gnus-current-headers 0 nil gnus-read-mark nil nil 
7063                  (header-subject gnus-current-headers))
7064                 (forward-line -1)
7065                 (gnus-summary-position-cursor)
7066                 (gnus-summary-update-line)
7067                 message-id)
7068             (message "No such references")
7069             nil))))))
7070
7071 (defun gnus-summary-enter-digest-group ()
7072   "Enter a digest group based on the current article."
7073   (interactive)
7074   (gnus-summary-select-article)
7075   (let ((name (format "%s/%d" 
7076                       (gnus-group-prefixed-name 
7077                        gnus-newsgroup-name (list 'nndigest "")) 
7078                       gnus-current-article))
7079         (buf (current-buffer)))
7080     (set-buffer gnus-group-buffer)
7081     (gnus-sethash 
7082      name 
7083      (list t nil (list name 3 nil nil 
7084                        (list 'nndigest gnus-article-buffer
7085                              (cons 'quit-buffer buf))))
7086      gnus-newsrc-hashtb)
7087     (gnus-group-read-group t nil name)))
7088   
7089 (defun gnus-summary-isearch-article ()
7090   "Do incremental search forward on current article."
7091   (interactive)
7092   (gnus-summary-select-article)
7093   (gnus-eval-in-buffer-window gnus-article-buffer
7094                               (isearch-forward)))
7095
7096 (defun gnus-summary-search-article-forward (regexp)
7097   "Search for an article containing REGEXP forward.
7098 gnus-select-article-hook is not called during the search."
7099   (interactive
7100    (list (read-string
7101           (concat "Search forward (regexp): "
7102                   (if gnus-last-search-regexp
7103                       (concat "(default " gnus-last-search-regexp ") "))))))
7104   (if (string-equal regexp "")
7105       (setq regexp (or gnus-last-search-regexp ""))
7106     (setq gnus-last-search-regexp regexp))
7107   (if (gnus-summary-search-article regexp nil)
7108       (gnus-eval-in-buffer-window 
7109        gnus-article-buffer
7110        (recenter 0))
7111     (error "Search failed: \"%s\"" regexp)))
7112
7113 (defun gnus-summary-search-article-backward (regexp)
7114   "Search for an article containing REGEXP backward.
7115 gnus-select-article-hook is not called during the search."
7116   (interactive
7117    (list (read-string
7118           (concat "Search backward (regexp): "
7119                   (if gnus-last-search-regexp
7120                       (concat "(default " gnus-last-search-regexp ") "))))))
7121   (if (string-equal regexp "")
7122       (setq regexp (or gnus-last-search-regexp ""))
7123     (setq gnus-last-search-regexp regexp))
7124   (if (gnus-summary-search-article regexp t)
7125       (gnus-eval-in-buffer-window
7126        gnus-article-buffer
7127        (recenter 0))
7128     (error "Search failed: \"%s\"" regexp)))
7129
7130 (defun gnus-summary-search-article (regexp &optional backward)
7131   "Search for an article containing REGEXP.
7132 Optional argument BACKWARD means do search for backward.
7133 gnus-select-article-hook is not called during the search."
7134   (let ((gnus-select-article-hook nil)  ;Disable hook.
7135         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7136         (re-search
7137          (if backward
7138              (function re-search-backward) (function re-search-forward)))
7139         (found nil)
7140         (last nil))
7141     ;; Hidden thread subtrees must be searched for ,too.
7142     (gnus-summary-show-all-threads)
7143     (if (eobp) (forward-line -1))
7144     ;; First of all, search current article.
7145     ;; We don't want to read article again from NNTP server nor reset
7146     ;; current point.
7147     (gnus-summary-select-article)
7148     (message "Searching article: %d..." gnus-current-article)
7149     (setq last gnus-current-article)
7150     (gnus-eval-in-buffer-window gnus-article-buffer
7151       (save-restriction
7152         (widen)
7153         ;; Begin search from current point.
7154         (setq found (funcall re-search regexp nil t))))
7155     ;; Then search next articles.
7156     (while (and (not found)
7157                 (gnus-summary-display-article 
7158                  (gnus-summary-search-subject backward nil nil)))
7159       (message "Searching article: %d..." gnus-current-article)
7160       (gnus-eval-in-buffer-window gnus-article-buffer
7161         (save-restriction
7162           (widen)
7163           (goto-char (if backward (point-max) (point-min)))
7164           (setq found (funcall re-search regexp nil t)))))
7165     (message "")
7166     ;; Adjust article pointer.
7167     (or (eq last gnus-current-article)
7168         (setq gnus-last-article last))
7169     ;; Return T if found such article.
7170     found))
7171
7172 (defun gnus-summary-execute-command (header regexp command &optional backward)
7173   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7174 If HEADER is an empty string (or nil), the match is done on the entire
7175 article. If BACKWARD (the prefix) is non-nil, search backward instead."
7176   (interactive
7177    (list (let ((completion-ignore-case t))
7178            (completing-read 
7179             "Header name: "
7180             (mapcar (string) (list string)
7181                     '("Number" "Subject" "From" "Lines" "Date"
7182                       "Message-ID" "Xref" "References")
7183                     nil 'require-match)))
7184          (read-string "Regexp: ")
7185          (read-key-sequence "Command: ")
7186          current-prefix-arg))
7187   ;; Hidden thread subtrees must be searched as well.
7188   (gnus-summary-show-all-threads)
7189   ;; We don't want to change current point nor window configuration.
7190   (save-excursion
7191     (save-window-excursion
7192       (message "Executing %s..." (key-description command))
7193       ;; We'd like to execute COMMAND interactively so as to give arguments.
7194       (gnus-execute header regexp
7195                     (` (lambda ()
7196                          (call-interactively '(, (key-binding command)))))
7197                     backward)
7198       (message "Executing %s... done" (key-description command)))))
7199
7200 (defun gnus-summary-beginning-of-article ()
7201   "Scroll the article back to the beginning."
7202   (interactive)
7203   (gnus-summary-select-article)
7204   (gnus-eval-in-buffer-window
7205    gnus-article-buffer
7206    (widen)
7207    (goto-char (point-min))
7208    (and gnus-break-pages (gnus-narrow-to-page))))
7209
7210 (defun gnus-summary-end-of-article ()
7211   "Scroll to the end of the article."
7212   (interactive)
7213   (gnus-summary-select-article)
7214   (gnus-eval-in-buffer-window 
7215    gnus-article-buffer
7216    (widen)
7217    (goto-char (point-max))
7218    (and gnus-break-pages (gnus-narrow-to-page))))
7219
7220 (defun gnus-summary-show-article ()
7221   "Force re-fetching of the current article."
7222   (interactive)
7223   (gnus-summary-select-article gnus-have-all-headers t t))
7224
7225 (defun gnus-summary-toggle-header (arg)
7226   "Show the headers if they are hidden, or hide them if they are shown.
7227 If ARG is a positive number, show the entire header.
7228 If ARG is a negative number, hide the unwanted header lines."
7229   (interactive "P")
7230   (gnus-set-global-variables)
7231   (save-excursion
7232     (set-buffer gnus-article-buffer)
7233     (let ((buffer-read-only nil))
7234       (if (numberp arg) 
7235           (if (> arg 0) (remove-text-properties 1 (point-max) '(invisible t))
7236             (if (< arg 0) (run-hooks 'gnus-article-display-hook)))
7237         (if (text-property-any 1 (point-max) 'invisible t)
7238             (remove-text-properties 1 (point-max) '(invisible t))
7239           (let ((gnus-have-all-headers nil))
7240             (run-hooks 'gnus-article-display-hook))))
7241       (set-window-point (get-buffer-window (current-buffer)) 1))))
7242
7243 (defun gnus-summary-show-all-headers ()
7244   "Make all header lines visible."
7245   (interactive)
7246   (gnus-article-show-all-headers))
7247
7248 (defun gnus-summary-toggle-mime (arg)
7249   "Toggle MIME processing.
7250 If ARG is a positive number, turn MIME processing on."
7251   (interactive "P")
7252   (setq gnus-show-mime
7253         (if (null arg) (not gnus-show-mime)
7254           (> (prefix-numeric-value arg) 0)))
7255   (gnus-summary-select-article t 'force))
7256
7257 (defun gnus-summary-caesar-message (rotnum)
7258   "Caesar rotates all letters of current message by 13/47 places.
7259 With prefix arg, specifies the number of places to rotate each letter forward.
7260 Caesar rotates Japanese letters by 47 places in any case."
7261   (interactive "P")
7262   (gnus-summary-select-article)
7263   (let ((mail-header-separator "")) ; !!! Is this necessary?
7264     (gnus-overload-functions)
7265     (gnus-eval-in-buffer-window 
7266      gnus-article-buffer
7267      (save-restriction
7268        (widen)
7269        ;; We don't want to jump to the beginning of the message.
7270        ;; `save-excursion' does not do its job.
7271        (move-to-window-line 0)
7272        (let ((last (point)))
7273          (news-caesar-buffer-body rotnum)
7274          (goto-char last)
7275          (recenter 0))))))
7276
7277 (defun gnus-summary-stop-page-breaking ()
7278   "Stop page breaking in the current article."
7279   (interactive)
7280   (gnus-summary-select-article)
7281   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
7282
7283 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
7284
7285 (defun gnus-summary-move-article (n &optional to-newsgroup select-method)
7286   "Move the current article to a different newsgroup.
7287 If N is a positive number, move the N next articles.
7288 If N is a negative number, move the N previous articles.
7289 If N is nil and any articles have been marked with the process mark,
7290 move those articles instead.
7291 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
7292 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
7293 re-spool using this method.
7294 For this function to work, both the current newsgroup and the
7295 newsgroup that you want to move to have to support the `request-move'
7296 and `request-accept' functions. (Ie. mail newsgroups at present.)"
7297   (interactive "P")
7298   (gnus-set-global-variables)
7299   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
7300       (error "The current newsgroup does not support article moving"))
7301   (let ((articles (gnus-summary-work-articles n))
7302         art-group)
7303     (if (and (not to-newsgroup) (not select-method))
7304         (setq to-newsgroup
7305               (completing-read 
7306                (format "Where do you want to move %s? %s"
7307                        (if (> (length articles) 1)
7308                            (format "these %d articles" (length articles))
7309                          "this article")
7310                        (if gnus-current-move-group
7311                            (format "(%s default) " gnus-current-move-group)
7312                          ""))
7313                gnus-active-hashtb nil nil
7314                (gnus-group-real-prefix gnus-newsgroup-name))))
7315     (if (string= to-newsgroup "")
7316         (setq to-newsgroup (or gnus-current-move-group "")))
7317     (or (gnus-gethash to-newsgroup gnus-active-hashtb)
7318         (error "No such group: %s" to-newsgroup))
7319     (setq gnus-current-move-group to-newsgroup)
7320     (or (gnus-check-backend-function 'request-accept-article 
7321                                      (or select-method to-newsgroup))
7322         (error "%s does not support article moving" to-newsgroup))
7323     (message "Moving to %s: %s..." (or select-method to-newsgroup) articles)
7324     (while articles
7325       (if (setq art-group
7326                 (gnus-request-move-article 
7327                  (car articles)                   ; Article to move
7328                  gnus-newsgroup-name              ; From newsgrouo
7329                  (nth 1 (gnus-find-method-for-group 
7330                          gnus-newsgroup-name))    ; Server
7331                  (list 'gnus-request-accept-article 
7332                        (if select-method
7333                            (quote select-method)
7334                          to-newsgroup)
7335                        (not (cdr articles)))     ; Accept form
7336                  (not (cdr articles))))          ; Only save nov last time
7337           (let* ((buffer-read-only nil)
7338                  (entry 
7339                   (or
7340                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
7341                    (gnus-gethash 
7342                     (gnus-group-prefixed-name 
7343                      (car art-group) 
7344                      (if select-method (list select-method "")
7345                        (gnus-find-method-for-group to-newsgroup)))
7346                     gnus-newsrc-hashtb)))
7347                  (info (nth 2 entry))
7348                  (article (car articles))
7349                  (marked (nth 3 info)))
7350             (gnus-summary-goto-subject article)
7351             (delete-region (progn (beginning-of-line) (point))
7352                            (progn (forward-line 1) (point)))
7353             (if (not (memq article gnus-newsgroup-unreads))
7354                 (setcar (cdr (cdr info))
7355                         (gnus-add-to-range (nth 2 info) 
7356                                            (list (cdr art-group)))))
7357             ;; Copy any marks over to the new group.
7358             (let ((marks '((tick . gnus-newsgroup-marked)
7359                            (dormant . gnus-newsgroup-dormant)
7360                            (expire . gnus-newsgroup-expirable)
7361                            (bookmark . gnus-newsgroup-bookmarks)
7362                         ;   (score . gnus-newsgroup-scored)
7363                            (reply . gnus-newsgroup-replied)))
7364                   (to-article (cdr art-group)))
7365               (while marks
7366                 (if (memq article (symbol-value (cdr (car marks))))
7367                     (gnus-add-marked-articles 
7368                      (car info) (car (car marks)) (list to-article) info))
7369                 (setq marks (cdr marks))))
7370             (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7371             (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7372             (setq gnus-newsgroup-dormant
7373                   (delq article gnus-newsgroup-dormant)))
7374         (message "Couldn't move article %s" (car articles)))
7375       (gnus-summary-remove-process-mark (car articles))
7376       (setq articles (cdr articles)))))
7377
7378 (defun gnus-summary-respool-article (n &optional respool-method)
7379   "Respool the current article.
7380 The article will be squeezed through the mail spooling process again,
7381 which means that it will be put in some mail newsgroup or other
7382 depending on `nnmail-split-methods'.
7383 If N is a positive number, respool the N next articles.
7384 If N is a negative number, respool the N previous articles.
7385 If N is nil and any articles have been marked with the process mark,
7386 respool those articles instead.
7387 For this function to work, both the current newsgroup and the
7388 newsgroup that you want to move to have to support the `request-move'
7389 and `request-accept' functions. (Ie. mail newsgroups at present.)"
7390   (interactive "P")
7391   (gnus-set-global-variables)
7392   (or respool-method
7393       (setq respool-method
7394             (completing-read
7395              "What method do you want to use when respooling? "
7396              (gnus-methods-using 'respool) nil t)))
7397   (gnus-summary-move-article n nil (intern respool-method)))
7398
7399 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
7400 (defun gnus-summary-copy-article (n &optional to-newsgroup select-method)
7401   "Move the current article to a different newsgroup.
7402 If N is a positive number, move the N next articles.
7403 If N is a negative number, move the N previous articles.
7404 If N is nil and any articles have been marked with the process mark,
7405 move those articles instead.
7406 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
7407 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
7408 re-spool using this method.
7409 For this function to work, the newsgroup that you want to move to have
7410 to support the `request-move' and `request-accept'
7411 functions. (Ie. mail newsgroups at present.)"
7412   (interactive "P")
7413   (gnus-set-global-variables)
7414   (let ((articles (gnus-summary-work-articles n))
7415         (copy-buf (get-buffer-create "*copy work*"))
7416         art-group)
7417     (buffer-disable-undo copy-buf)
7418     (if (and (not to-newsgroup) (not select-method))
7419         (setq to-newsgroup
7420               (completing-read 
7421                (format "Where do you want to copy %s? %s"
7422                        (if (> (length articles) 1)
7423                            (format "these %d articles" (length articles))
7424                          "this article")
7425                        (if gnus-current-move-group
7426                            (format "(%s default) " gnus-current-move-group)
7427                          ""))
7428                gnus-active-hashtb nil nil
7429                (gnus-group-real-prefix gnus-newsgroup-name))))
7430     (if (string= to-newsgroup "")
7431         (setq to-newsgroup (or gnus-current-move-group "")))
7432     (or (gnus-gethash to-newsgroup gnus-active-hashtb)
7433         (error "No such group: %s" to-newsgroup))
7434     (setq gnus-current-move-group to-newsgroup)
7435     (or (gnus-check-backend-function 'request-accept-article 
7436                                      (or select-method to-newsgroup))
7437         (error "%s does not support article copying" to-newsgroup))
7438     (message "Moving to %s: %s..." (or select-method to-newsgroup) articles)
7439     (while articles
7440       (if (setq art-group
7441                 (save-excursion
7442                   (set-buffer copy-buf)
7443                   (gnus-request-article-this-buffer
7444                    (car articles) gnus-newsgroup-name)
7445                   (gnus-request-accept-article
7446                    (if select-method (quote select-method) to-newsgroup)
7447                    (not (cdr articles)))))
7448           (let* ((entry 
7449                   (or
7450                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
7451                    (gnus-gethash 
7452                     (gnus-group-prefixed-name 
7453                      (car art-group) 
7454                      (if select-method (list select-method "")
7455                        (gnus-find-method-for-group to-newsgroup)))
7456                     gnus-newsrc-hashtb)))
7457                  (info (nth 2 entry))
7458                  (article (car articles))
7459                  (marked (nth 3 info)))
7460             (if (not (memq article gnus-newsgroup-unreads))
7461                 (setcar (cdr (cdr info))
7462                         (gnus-add-to-range (nth 2 info) 
7463                                            (list (cdr art-group)))))
7464             ;; Copy any marks over to the new group.
7465             (let ((marks '((tick . gnus-newsgroup-marked)
7466                            (dormant . gnus-newsgroup-dormant)
7467                            (expire . gnus-newsgroup-expirable)
7468                            (bookmark . gnus-newsgroup-bookmarks)
7469                         ;   (score . gnus-newsgroup-scored)
7470                            (reply . gnus-newsgroup-replied)))
7471                   (to-article (cdr art-group)))
7472               (while marks
7473                 (if (memq article (symbol-value (cdr (car marks))))
7474                     (gnus-add-marked-articles 
7475                      (car info) (car (car marks)) (list to-article) info))
7476                 (setq marks (cdr marks)))))
7477         (message "Couldn't copy article %s" (car articles)))
7478       (gnus-summary-remove-process-mark (car articles))
7479       (setq articles (cdr articles)))
7480     (kill-buffer copy-buf)))
7481
7482 (defun gnus-summary-expire-articles ()
7483   "Expire all articles that are marked as expirable in the current group."
7484   (interactive)
7485   (if (and gnus-newsgroup-expirable
7486            (gnus-check-backend-function 
7487             'request-expire-articles gnus-newsgroup-name))
7488       (let ((expirable gnus-newsgroup-expirable))
7489         ;; The list of articles that weren't expired is returned.
7490         (setq gnus-newsgroup-expirable 
7491               (gnus-request-expire-articles gnus-newsgroup-expirable
7492                                             gnus-newsgroup-name))
7493         ;; We go through the old list of expirable, and mark all
7494         ;; really expired articles as non-existant.
7495         (while expirable
7496           (or (memq (car expirable) gnus-newsgroup-expirable)
7497               (gnus-summary-mark-as-read (car expirable) "%"))
7498           (setq expirable (cdr expirable))))))
7499
7500 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7501 (defun gnus-summary-delete-article (n)
7502   "Delete the N next (mail) articles.
7503 This command actually deletes articles. This is not a marking
7504 command. The article will disappear forever from you life, never to
7505 return. 
7506 If N is negative, delete backwards.
7507 If N is nil and articles have been marked with the process mark,
7508 delete these instead."
7509   (interactive "P")
7510   (or (gnus-check-backend-function 'request-expire-articles 
7511                                    gnus-newsgroup-name)
7512       (error "The current newsgroup does not support article deletion."))
7513   ;; Compute the list of articles to delete.
7514   (let ((articles (gnus-summary-work-articles n)))
7515     (if (and gnus-novice-user
7516              (not (gnus-y-or-n-p 
7517                    (format "Do you really want to delete %s forever? "
7518                            (if (> (length articles) 1) "these articles"
7519                              "this article")))))
7520         ()
7521       ;; Delete the articles.
7522       (setq gnus-newsgroup-expirable 
7523             (gnus-request-expire-articles 
7524              articles gnus-newsgroup-name 'force))
7525       (while articles
7526         (gnus-summary-remove-process-mark (car articles))       
7527         ;; The backend might not have been able to delete the article
7528         ;; after all.  
7529         (or (memq (car articles) gnus-newsgroup-expirable)
7530             (gnus-summary-mark-as-read (car articles) gnus-canceled-mark))
7531         (setq articles (cdr articles))))))
7532
7533 (defun gnus-summary-edit-article ()
7534   "Enter into a buffer and edit the current article.
7535 This will have permanent effect only in mail groups."
7536   (interactive)
7537   (or (gnus-check-backend-function 
7538        'request-replace-article gnus-newsgroup-name)
7539       (error "The current newsgroup does not support article editing."))
7540   (gnus-summary-select-article t)
7541   (other-window 1)
7542   (message "C-c C-c to end edits")
7543   (setq buffer-read-only nil)
7544   (text-mode)
7545   (use-local-map (copy-keymap (current-local-map)))
7546   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
7547   (goto-char 1)
7548   (search-forward "\n\n" nil t))
7549
7550 (defun gnus-summary-edit-article-done ()
7551   "Make edits to the current article permanent."
7552   (interactive)
7553   (if (not (gnus-request-replace-article 
7554             (cdr gnus-article-current) (car gnus-article-current) 
7555             (current-buffer)))
7556       (error "Couldn't replace article.")
7557     (gnus-article-mode)
7558     (use-local-map gnus-article-mode-map)
7559     (setq buffer-read-only t)
7560     (pop-to-buffer gnus-summary-buffer)))      
7561
7562 ;; Summary score commands.
7563
7564 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
7565
7566 (defun gnus-summary-raise-score (n)
7567   "Raise the score of the current article by N."
7568   (interactive "p")
7569   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
7570
7571 (defun gnus-summary-lower-score (n)
7572   "Lower the score of the current article by N."
7573   (interactive "p")
7574   (gnus-summary-raise-score (- n)))
7575
7576 (defun gnus-summary-set-score (n)
7577   "Set the score of the current article to N."
7578   (interactive "p")
7579   ;; Skip dummy header line.
7580   (save-excursion
7581     (if (eq (gnus-summary-article-mark) ?Z)
7582         (forward-line 1))
7583     (let ((buffer-read-only nil))
7584       ;; Set score.
7585       (gnus-summary-update-mark
7586        (if (= n (or gnus-summary-default-score 0)) ? 
7587          (if (< n (or gnus-summary-default-score 0)) 
7588              gnus-score-below-mark gnus-score-over-mark)) 'score))
7589     (let* ((article (gnus-summary-article-number))
7590            (score (assq article gnus-newsgroup-scored)))
7591       (if score (setcdr score n)
7592         (setq gnus-newsgroup-scored 
7593               (cons (cons article n) gnus-newsgroup-scored))))
7594     (gnus-summary-update-line)))
7595
7596 ;; Summary marking commands.
7597
7598 (defun gnus-summary-raise-same-subject-and-select (score)
7599   "Raise articles which has the same subject with SCORE and select the next."
7600   (interactive "p")
7601   (let ((subject (gnus-summary-subject-string)))
7602     (gnus-summary-raise-score score)
7603     (while (gnus-summary-search-subject nil nil subject)
7604       (gnus-summary-raise-score score))
7605     (gnus-summary-next-article t)))
7606
7607 (defun gnus-summary-raise-same-subject (score)
7608   "Raise articles which has the same subject with SCORE."
7609   (interactive "p")
7610   (let ((subject (gnus-summary-subject-string)))
7611     (gnus-summary-raise-score score)
7612     (while (gnus-summary-search-subject nil nil subject)
7613       (gnus-summary-raise-score score))
7614     (gnus-summary-next-subject 1 t)))
7615
7616 (defun gnus-summary-raise-thread (score)
7617   "Raise articles under current thread with SCORE."
7618   (interactive "p")
7619   (let (e)
7620     (save-excursion
7621       (let ((level (gnus-summary-thread-level)))
7622         (gnus-summary-raise-score score)
7623         (while (and (zerop (gnus-summary-next-subject 1))
7624                     (> (gnus-summary-thread-level) level))
7625           (gnus-summary-raise-score score))
7626         (setq e (point))))
7627     (or (zerop (gnus-summary-next-subject 1 t))
7628         (goto-char e)))
7629   (gnus-summary-position-cursor)
7630   (gnus-set-mode-line 'summary))
7631
7632 (defun gnus-summary-lower-same-subject-and-select (score)
7633   "Raise articles which has the same subject with SCORE and select the next."
7634   (interactive "p")
7635   (gnus-summary-raise-same-subject-and-select (- score)))
7636
7637 (defun gnus-summary-lower-same-subject (score)
7638   "Raise articles which has the same subject with SCORE."
7639   (interactive "p")
7640   (gnus-summary-raise-same-subject (- score)))
7641
7642 (defun gnus-summary-lower-thread (score)
7643   "Raise articles under current thread with SCORE."
7644   (interactive "p")
7645   (gnus-summary-raise-thread (- score)))
7646
7647 (defun gnus-summary-kill-same-subject-and-select (unmark)
7648   "Mark articles which has the same subject as read, and then select the next.
7649 If UNMARK is positive, remove any kind of mark.
7650 If UNMARK is negative, tick articles."
7651   (interactive "P")
7652   (if unmark
7653       (setq unmark (prefix-numeric-value unmark)))
7654   (let ((count
7655          (gnus-summary-mark-same-subject
7656           (gnus-summary-subject-string) unmark)))
7657     ;; Select next unread article. If auto-select-same mode, should
7658     ;; select the first unread article.
7659     (gnus-summary-next-article t (and gnus-auto-select-same
7660                                       (gnus-summary-subject-string)))
7661     (message "%d articles are marked as %s"
7662              count (if unmark "unread" "read"))))
7663
7664 (defun gnus-summary-kill-same-subject (unmark)
7665   "Mark articles which has the same subject as read. 
7666 If UNMARK is positive, remove any kind of mark.
7667 If UNMARK is negative, tick articles."
7668   (interactive "P")
7669   (if unmark
7670       (setq unmark (prefix-numeric-value unmark)))
7671   (let ((count
7672          (gnus-summary-mark-same-subject
7673           (gnus-summary-subject-string) unmark)))
7674     ;; If marked as read, go to next unread subject.
7675     (if (null unmark)
7676         ;; Go to next unread subject.
7677         (gnus-summary-next-subject 1 t))
7678     (message "%d articles are marked as %s"
7679              count (if unmark "unread" "read"))))
7680
7681 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7682   "Mark articles with same SUBJECT as read, and return marked number.
7683 If optional argument UNMARK is positive, remove any kinds of marks.
7684 If optional argument UNMARK is negative, mark articles as unread instead."
7685   (let ((count 1))
7686     (save-excursion
7687       (cond ((null unmark)
7688              (gnus-summary-mark-as-read nil gnus-killed-mark))
7689             ((> unmark 0)
7690              (gnus-summary-tick-article nil t))
7691             (t
7692              (gnus-summary-tick-article)))
7693       (while (and subject
7694                   (gnus-summary-search-forward nil subject))
7695         (cond ((null unmark)
7696                (gnus-summary-mark-as-read nil gnus-killed-mark))
7697               ((> unmark 0)
7698                (gnus-summary-tick-article nil t))
7699               (t
7700                (gnus-summary-tick-article)))
7701         (setq count (1+ count))))
7702     ;; Hide killed thread subtrees.  Does not work properly always.
7703     ;;(and (null unmark)
7704     ;;     gnus-thread-hide-killed
7705     ;;     (gnus-summary-hide-thread))
7706     ;; Return number of articles marked as read.
7707     count))
7708
7709 (defun gnus-summary-mark-as-processable (n &optional unmark)
7710   "Set the process mark on the next N articles.
7711 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7712 the process mark instead.  The difference between N and the actual
7713 number of articles marked is returned."
7714   (interactive "p")
7715   (let ((backward (< n 0))
7716         (n (abs n)))
7717   (while (and 
7718           (> n 0)
7719           (if unmark
7720               (gnus-summary-remove-process-mark (gnus-summary-article-number))
7721             (gnus-summary-set-process-mark (gnus-summary-article-number)))
7722           (zerop (gnus-summary-next-subject (if backward -1 1))))
7723     (setq n (1- n)))
7724   (if (/= 0 n) (message "No more articles"))
7725   n))
7726
7727 (defun gnus-summary-unmark-as-processable (n)
7728   "Remove the process mark from the next N articles.
7729 If N is negative, mark backward instead.  The difference between N and
7730 the actual number of articles marked is returned."
7731   (interactive "p")
7732   (gnus-summary-mark-as-processable n t))
7733
7734 (defun gnus-summary-unmark-all-processable ()
7735   "Remove the process mark from all articles."
7736   (interactive)
7737   (save-excursion
7738     (while gnus-newsgroup-processable
7739       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7740   (gnus-summary-position-cursor))
7741
7742 (defun gnus-summary-mark-as-expirable (n)
7743   "Mark N articles forward as expirable.
7744 If N is negative, mark backward instead. The difference between N and
7745 the actual number of articles marked is returned."
7746   (interactive "p")
7747   (gnus-summary-mark-forward n gnus-expirable-mark))
7748
7749 (defun gnus-summary-mark-article-as-replied (article)
7750   "Mark ARTICLE replied and update the summary line."
7751   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
7752   (let ((buffer-read-only nil))
7753     (if (gnus-summary-goto-subject article)
7754         (progn
7755           (gnus-summary-update-mark gnus-replied-mark 'replied)
7756           t))))
7757
7758 (defun gnus-summary-set-bookmark (article)
7759   "Set a bookmark in current article."
7760   (interactive (list (gnus-summary-article-number)))
7761   (if (or (not (get-buffer gnus-article-buffer))
7762           (not gnus-current-article)
7763           (not gnus-article-current)
7764           (not (equal gnus-newsgroup-name (car gnus-article-current))))
7765       (error "No current article selected"))
7766   ;; Remove old bookmark, if one exists.
7767   (let ((old (assq article gnus-newsgroup-bookmarks)))
7768     (if old (setq gnus-newsgroup-bookmarks 
7769                   (delq old gnus-newsgroup-bookmarks))))
7770   ;; Set the new bookmark, which is on the form 
7771   ;; (article-number . line-number-in-body).
7772   (setq gnus-newsgroup-bookmarks 
7773         (cons 
7774          (cons article 
7775                (save-excursion
7776                  (set-buffer gnus-article-buffer)
7777                  (count-lines
7778                   (min (point)
7779                        (save-excursion
7780                          (goto-char 1)
7781                          (search-forward "\n\n" nil t)
7782                          (point)))
7783                   (point))))
7784          gnus-newsgroup-bookmarks))
7785   (message "A bookmark has been added to the current article."))
7786
7787 (defun gnus-summary-remove-bookmark (article)
7788   "Remove the bookmark from the current article."
7789   (interactive (list (gnus-summary-article-number)))
7790   ;; Remove old bookmark, if one exists.
7791   (let ((old (assq article gnus-newsgroup-bookmarks)))
7792     (if old 
7793         (progn
7794           (setq gnus-newsgroup-bookmarks 
7795                 (delq old gnus-newsgroup-bookmarks))
7796           (message "Removed bookmark."))
7797       (message "No bookmark in current article."))))
7798
7799 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7800 (defun gnus-summary-mark-as-dormant (n)
7801   "Mark N articles forward as dormant.
7802 If N is negative, mark backward instead.  The difference between N and
7803 the actual number of articles marked is returned."
7804   (interactive "p")
7805   (gnus-summary-mark-forward n gnus-dormant-mark))
7806
7807 (defun gnus-summary-set-process-mark (article)
7808   "Set the process mark on ARTICLE and update the summary line."
7809   (setq gnus-newsgroup-processable (cons article gnus-newsgroup-processable))
7810   (let ((buffer-read-only nil))
7811     (if (gnus-summary-goto-subject article)
7812         (progn
7813           (and (eq (gnus-summary-article-mark) ?Z)
7814                (forward-line 1))
7815           (gnus-summary-update-mark gnus-process-mark 'replied)
7816           t))))
7817
7818 (defun gnus-summary-remove-process-mark (article)
7819   "Remove the process mark from ARTICLE and update the summary line."
7820   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7821   (let ((buffer-read-only nil))
7822     (if (gnus-summary-goto-subject article)
7823         (progn
7824           (and (eq (gnus-summary-article-mark) ?Z)
7825                (forward-line 1))
7826           (gnus-summary-update-mark ?  'replied)
7827           (if (memq article gnus-newsgroup-replied) 
7828               (gnus-summary-update-mark gnus-replied-mark 'replied))
7829           t))))
7830
7831 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7832   "Mark N articles as read forwards.
7833 If N is negative, mark backwards instead.
7834 Mark with MARK. If MARK is ? , ?! or ??, articles will be
7835 marked as unread. 
7836 The difference between N and the actual number of articles marked is
7837 returned."
7838   (interactive "p")
7839   (gnus-set-global-variables)
7840   (let ((backward (< n 0))
7841         (n (abs n))
7842         (mark (or mark gnus-dread-mark)))
7843   (while (and (> n 0)
7844               (gnus-summary-mark-article nil mark no-expire)
7845               (zerop (gnus-summary-next-subject 
7846                       (if backward -1 1) gnus-summary-goto-unread)))
7847     (setq n (1- n)))
7848   (if (/= 0 n) (message "No more %sarticles" (if mark "" "unread ")))
7849   (gnus-set-mode-line 'summary)
7850   n))
7851
7852 (defun gnus-summary-mark-article (&optional article mark no-expire)
7853   "Mark ARTICLE with MARK.
7854 MARK can be any character.
7855 Five MARK strings are reserved: ?  (unread), 
7856 ?! (ticked), ?? (dormant), ?D (read), ?E (expirable).
7857 If MARK is nil, then the default character ?D is used.
7858 If ARTICLE is nil, then the article on the current line will be
7859 marked." 
7860   ;; If no mark is given, then we check auto-expiring.
7861   (and (not no-expire)
7862        gnus-newsgroup-auto-expire 
7863        (or (not mark)
7864            (and (numberp mark) (or (= mark gnus-killed-mark)
7865                                    (= mark gnus-dread-mark)
7866                                    (= mark gnus-catchup-mark)
7867                                    (= mark gnus-low-score-mark)
7868                                    (= mark gnus-read-mark))))
7869        (setq mark gnus-expirable-mark))
7870   (let* ((buffer-read-only nil)
7871          (mark (or (and (stringp mark) (aref mark 0)) mark gnus-dread-mark))
7872          (article (or article (gnus-summary-article-number))))
7873     (if (or (= mark gnus-unread-mark) 
7874             (= mark gnus-ticked-mark) 
7875             (= mark gnus-dormant-mark))
7876         (gnus-mark-article-as-unread article mark)
7877       (gnus-mark-article-as-read article mark))
7878     (if (gnus-summary-goto-subject article)
7879         (progn
7880           (gnus-summary-show-thread)
7881           (beginning-of-line)
7882           (and (eq (gnus-summary-article-mark) ?Z)
7883                (forward-line 1))
7884           ;; Fix the mark.
7885           (gnus-summary-update-mark mark 'unread)
7886           t))))
7887
7888 (defun gnus-summary-update-mark (mark type)
7889   (beginning-of-line)
7890   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
7891         plist)
7892     (if (not forward)
7893         ()
7894       (forward-char forward)
7895       (setq plist (text-properties-at (point)))
7896       (delete-char 1)
7897       (and (memq 'gnus-mark plist) (setcar (cdr (memq 'gnus-mark plist)) mark))
7898       (insert mark)
7899       (and plist (add-text-properties (1- (point)) (point) plist))
7900       (gnus-summary-update-line (eq mark gnus-unread-mark)))))
7901   
7902 (defun gnus-mark-article-as-read (article &optional mark)
7903   "Enter ARTICLE in the pertinent lists and remove it from others."
7904   ;; Make the article expirable.
7905   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-dread-mark)))
7906     (if (= mark gnus-expirable-mark)
7907         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
7908       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
7909     ;; Remove from unread and marked lists.
7910     (setq gnus-newsgroup-unreads
7911           (delq article gnus-newsgroup-unreads))
7912     (setq gnus-newsgroup-marked
7913           (delq article gnus-newsgroup-marked))
7914     (setq gnus-newsgroup-dormant
7915           (delq article gnus-newsgroup-dormant))))
7916
7917 (defun gnus-mark-article-as-unread (article &optional mark)
7918   "Enter ARTICLE in the pertinent lists and remove it from others."
7919   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-ticked-mark)))
7920     ;; Add to unread list.
7921     (or (memq article gnus-newsgroup-unreads)
7922         (setq gnus-newsgroup-unreads
7923               (cons article gnus-newsgroup-unreads)))
7924     ;; If CLEAR-MARK is non-nil, the article must be removed from marked
7925     ;; list.  Otherwise, it must be added to the list.
7926     (setq gnus-newsgroup-marked
7927           (delq article gnus-newsgroup-marked))
7928     (setq gnus-newsgroup-dormant
7929           (delq article gnus-newsgroup-dormant))
7930     (setq gnus-newsgroup-expirable 
7931           (delq article gnus-newsgroup-expirable))
7932     (if (= mark gnus-ticked-mark)
7933         (setq gnus-newsgroup-marked 
7934               (cons article gnus-newsgroup-marked)))
7935     (if (= mark gnus-dormant-mark)
7936         (setq gnus-newsgroup-dormant 
7937               (cons article gnus-newsgroup-dormant)))))
7938
7939 (defalias 'gnus-summary-mark-as-unread-forward 
7940   'gnus-summary-tick-article-forward)
7941 (make-obsolete 'gnus-summary-mark-as-unread-forward 
7942                'gnus-summary-tick-article-forward)
7943 (defun gnus-summary-tick-article-forward (n)
7944   "Tick N articles forwards.
7945 If N is negative, tick backwards instead.
7946 The difference between N and the number of articles ticked is returned."
7947   (interactive "p")
7948   (gnus-summary-mark-forward n gnus-ticked-mark))
7949
7950 (defalias 'gnus-summary-mark-as-unread-backward 
7951   'gnus-summary-tick-article-backward)
7952 (make-obsolete 'gnus-summary-mark-as-unread-backward 
7953                'gnus-summary-tick-article-backward)
7954 (defun gnus-summary-tick-article-backward (n)
7955   "Tick N articles backwards.
7956 The difference between N and the number of articles ticked is returned."
7957   (interactive "p")
7958   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
7959
7960 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
7961 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
7962 (defun gnus-summary-tick-article (&optional article clear-mark)
7963   "Mark current article as unread.
7964 Optional 1st argument ARTICLE specifies article number to be marked as unread.
7965 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
7966   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
7967                                        gnus-ticked-mark)))
7968
7969 (defun gnus-summary-mark-as-read-forward (n)
7970   "Mark N articles as read forwards.
7971 If N is negative, mark backwards instead.
7972 The difference between N and the actual number of articles marked is
7973 returned."
7974   (interactive "p")
7975   (gnus-summary-mark-forward n gnus-dread-mark t))
7976
7977 (defun gnus-summary-mark-as-read-backward (n)
7978   "Mark the N articles as read backwards.
7979 The difference between N and the actual number of articles marked is
7980 returned."
7981   (interactive "p")
7982   (gnus-summary-mark-forward (- n) gnus-dread-mark t))
7983
7984 (defun gnus-summary-mark-as-read (&optional article mark)
7985   "Mark current article as read.
7986 ARTICLE specifies the article to be marked as read.
7987 MARK specifies a string to be inserted at the beginning of the line."
7988   (gnus-summary-mark-article article mark))
7989
7990 (defun gnus-summary-clear-mark-forward (n)
7991   "Clear marks from N articles forward.
7992 If N is negative, clear backward instead.
7993 The difference between N and the number of marks cleared is returned."
7994   (interactive "p")
7995   (gnus-summary-mark-forward n gnus-unread-mark))
7996
7997 (defun gnus-summary-clear-mark-backward (n)
7998   "Clear marks from N articles backward.
7999 The difference between N and the number of marks cleared is returned."
8000   (interactive "p")
8001   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8002
8003 (defun gnus-summary-mark-unread-as-read ()
8004   "Intended to be used by `gnus-summary-mark-article-hook'."
8005   (or (memq gnus-current-article gnus-newsgroup-marked)
8006       (memq gnus-current-article gnus-newsgroup-dormant)
8007       (memq gnus-current-article gnus-newsgroup-expirable)
8008       (gnus-summary-mark-as-read gnus-current-article gnus-read-mark)))
8009
8010 ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
8011 (defalias 'gnus-summary-delete-marked-as-read 
8012   'gnus-summary-remove-lines-marked-as-read)
8013 (make-obsolete 'gnus-summary-delete-marked-as-read 
8014                'gnus-summary-remove-lines-marked-as-read)
8015 (defun gnus-summary-remove-lines-marked-as-read ()
8016   "Remove lines that are marked as read."
8017   (interactive)
8018   (gnus-summary-remove-lines-marked-with 
8019    (concat (mapconcat
8020             (lambda (char) (char-to-string (symbol-value char)))
8021             '(gnus-dread-mark gnus-read-mark
8022               gnus-killed-mark gnus-kill-file-mark
8023               gnus-low-score-mark gnus-expirable-mark)
8024             ""))))
8025
8026 (defalias 'gnus-summary-delete-marked-with 
8027   'gnus-summary-remove-lines-marked-with)
8028 (make-obsolete 'gnus-summary-delete-marked-with 
8029                'gnus-summary-remove-lines-marked-with)
8030 ;; Rewrite by Daniel Quinlan <quinlan@best.com>.
8031 (defun gnus-summary-remove-lines-marked-with (marks)
8032   "Remove lines that are marked with MARKS (e.g. \"DK\")."
8033   (interactive "sMarks: ")
8034   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
8035   (save-excursion
8036     (set-buffer gnus-summary-buffer)
8037     (let ((buffer-read-only nil)
8038           (marks (concat "^[" marks "]")))
8039       (goto-char (point-min))
8040       (while (search-forward-regexp marks (point-max) t)
8041         (delete-region (progn (beginning-of-line) (point))
8042                        (progn (forward-line 1) (point)))))
8043     (or (zerop (buffer-size))
8044         (if (eobp)
8045             (gnus-summary-prev-subject 1)
8046           (gnus-summary-position-cursor)))))
8047
8048 (defun gnus-summary-expunge-below (score)
8049   "Remove articles with score less than SCORE."
8050   (interactive "P")
8051   (setq score (if score
8052                   (prefix-numeric-value score)
8053                 (or gnus-summary-default-score 0)))
8054   (save-excursion
8055     (set-buffer gnus-summary-buffer)
8056     (goto-char (point-min))
8057     (let ((buffer-read-only nil)
8058           beg)
8059       (while (not (eobp))
8060         (if (< (gnus-summary-article-score) score)
8061             (progn
8062               (setq beg (point))
8063               (forward-line 1)
8064               (delete-region beg (point)))
8065           (forward-line 1)))
8066       ;; Adjust point.
8067       (or (zerop (buffer-size))
8068           (if (eobp)
8069               (gnus-summary-prev-subject 1)
8070             (gnus-summary-position-cursor))))))
8071
8072 (defun gnus-summary-mark-below (score mark)
8073   "Mark articles with score less than SCORE with MARK."
8074   (interactive "P\ncMark: ")
8075   (setq score (if score
8076                   (prefix-numeric-value score)
8077                 (or gnus-summary-default-score 0)))
8078   (save-excursion
8079     (set-buffer gnus-summary-buffer)
8080     (goto-char (point-min))
8081     (while (not (eobp))
8082       (and (< (gnus-summary-article-score) score)
8083            (gnus-summary-mark-article nil mark))
8084       (forward-line 1))))
8085
8086 (defun gnus-summary-kill-below (score)
8087   "Mark articles with score below SCORE as read."
8088   (interactive "P")
8089   (gnus-summary-mark-below score gnus-killed-mark))
8090
8091 (defun gnus-summary-clear-above (score)
8092   "Clear all marks from articles with score above SCORE."
8093   (interactive "P")
8094   (gnus-summary-mark-above score gnus-unread-mark))
8095
8096 (defun gnus-summary-tick-above (score)
8097   "Tick all articles with score above SCORE."
8098   (interactive "P")
8099   (gnus-summary-mark-above score gnus-ticked-mark))
8100
8101 (defun gnus-summary-mark-above (score mark)
8102   "Mark articles with score over SCORE with MARK."
8103   (interactive "P\ncMark: ")
8104   (setq score (if score
8105                   (prefix-numeric-value score)
8106                 (or gnus-summary-default-score 0)))
8107   (save-excursion
8108     (set-buffer gnus-summary-buffer)
8109     (goto-char (point-min))
8110     (while (not (eobp))
8111       (if (> (gnus-summary-article-score) score)
8112           (progn
8113             (gnus-summary-mark-article nil mark)
8114             (forward-line 1))
8115         (forward-line 1)))))
8116
8117 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
8118 (defun gnus-summary-show-all-expunged ()
8119   "Display all the hidden articles that were expunged for low scores."
8120   (interactive)
8121   (let ((buffer-read-only nil))
8122     (let ((scored gnus-newsgroup-scored)
8123           headers h)
8124       (while scored
8125         (or (gnus-summary-goto-subject (car (car scored)))
8126             (and (setq h (gnus-get-header-by-number (car (car scored))))
8127                  (< (cdr (car scored)) gnus-summary-expunge-below)
8128                  (setq headers (cons h headers))))
8129         (setq scored (cdr scored)))
8130       (or headers (error "No expunged articles hidden."))
8131       (goto-char (point-min))
8132       (save-excursion 
8133         (gnus-summary-prepare-threads (nreverse headers) 0)))
8134     (goto-char (point-min))
8135     (gnus-summary-position-cursor)))
8136
8137 (defun gnus-summary-show-all-dormant ()
8138   "Display all the hidden articles that are marked as dormant."
8139   (interactive)
8140   (let ((buffer-read-only nil))
8141     (goto-char (point-min))
8142     (let ((dormant gnus-newsgroup-dormant)
8143           headers h)
8144       (while dormant
8145         (or (gnus-summary-goto-subject (car dormant))
8146             (and (setq h (gnus-get-header-by-number (car dormant)))
8147                  (setq headers (cons h headers))))
8148         (setq dormant (cdr dormant)))
8149       (or headers (error "No dormant articles hidden."))
8150       (save-excursion (gnus-summary-prepare-threads (nreverse headers) 0)))
8151     (goto-char (point-min))
8152     (gnus-summary-position-cursor)))
8153
8154 (defun gnus-summary-hide-all-dormant ()
8155   "Hide all dormant articles."
8156   (interactive)
8157   (gnus-summary-remove-lines-marked-with (char-to-string gnus-dormant-mark))
8158   (gnus-summary-position-cursor))
8159
8160 (defun gnus-summary-catchup (all &optional quietly to-here)
8161   "Mark all articles not marked as unread in this newsgroup as read.
8162 If prefix argument ALL is non-nil, all articles are marked as read.
8163 If QUIETLY is non-nil, no questions will be asked.
8164 If TO-HERE is non-nil, it should be a point in the buffer. All
8165 articles before this point will be marked as read.
8166 The number of articles marked as read is returned."
8167   (interactive "P")
8168   (prog1
8169       (if (or quietly
8170               (not gnus-interactive-catchup) ;Without confirmation?
8171               gnus-expert-user
8172               (gnus-y-or-n-p
8173                (if all
8174                    "Mark absolutely all articles as read? "
8175                  "Mark all unread articles as read? ")))
8176           (let ((unreads (length gnus-newsgroup-unreads)))
8177             (if (gnus-summary-first-subject (not all))
8178                 (while (and (gnus-summary-mark-as-read nil gnus-catchup-mark)
8179                             (if to-here (< (point) to-here) t)
8180                             (gnus-summary-search-subject nil (not all)))))
8181             (- unreads (length gnus-newsgroup-unreads))
8182             (or to-here
8183                 (setq gnus-newsgroup-unreads gnus-newsgroup-marked))))
8184     (gnus-summary-position-cursor)))
8185
8186 (defun gnus-summary-catchup-to-here (&optional all)
8187   "Mark all unticked articles before the current one as read.
8188 If ALL is non-nil, also mark ticked and dormant articles as read."
8189   (interactive)
8190   (beginning-of-line)
8191   (gnus-summary-catchup all t (point))
8192   (gnus-summary-position-cursor))
8193
8194 (defun gnus-summary-catchup-all (&optional quietly)
8195   "Mark all articles in this newsgroup as read."
8196   (interactive)
8197   (gnus-summary-catchup t quietly))
8198
8199 (defun gnus-summary-catchup-and-exit (all &optional quietly)
8200   "Mark all articles not marked as unread in this newsgroup as read, then exit.
8201 If prefix argument ALL is non-nil, all articles are marked as read."
8202   (interactive "P")
8203   (gnus-summary-catchup all quietly)
8204   ;; Select next newsgroup or exit.
8205   (if (eq gnus-auto-select-next 'quietly)
8206       (gnus-summary-next-group nil)
8207     (gnus-summary-exit)))
8208
8209 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8210   "Mark all articles in this newsgroup as read, and then exit."
8211   (interactive)
8212   (gnus-summary-catchup-and-exit t quietly))
8213
8214 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8215 (defun gnus-summary-catchup-and-goto-next-group (all)
8216   "Mark all articles in this group as read and select the next group.
8217 If given a prefix, mark all articles, unread as well as ticked, as
8218 read." 
8219   (interactive "P")
8220   (gnus-summary-catchup all)
8221   (gnus-summary-next-group))
8222
8223 ;; Thread-based commands.
8224
8225 (defun gnus-summary-toggle-threads (arg)
8226   "Toggle showing conversation threads.
8227 If ARG is positive number, turn showing conversation threads on."
8228   (interactive "P")
8229   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8230     (setq gnus-show-threads
8231           (if (null arg) (not gnus-show-threads)
8232             (> (prefix-numeric-value arg) 0)))
8233     (gnus-summary-prepare)
8234     (gnus-summary-goto-subject current)
8235     (gnus-summary-position-cursor)))
8236
8237 (defun gnus-summary-show-all-threads ()
8238   "Show all threads."
8239   (interactive)
8240   (save-excursion
8241     (let ((buffer-read-only nil))
8242       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8243   (gnus-summary-position-cursor))
8244
8245 (defun gnus-summary-show-thread ()
8246   "Show thread subtrees.
8247 Returns nil if no thread was there to be shown."
8248   (interactive)
8249   (prog1
8250       (save-excursion
8251         (let ((buffer-read-only nil)
8252               (beg (progn (beginning-of-line) (point)))
8253               (end (save-excursion (end-of-line) (point))))
8254           (prog1
8255               ;; Any hidden lines here?
8256               (search-forward "\r" end t)
8257             (subst-char-in-region beg end ?\^M ?\n t))))
8258     (gnus-summary-position-cursor)))
8259
8260 (defun gnus-summary-hide-all-threads ()
8261   "Hide all thread subtrees."
8262   (interactive)
8263   (save-excursion
8264     (goto-char (point-min))
8265     (gnus-summary-hide-thread)
8266     (while (and (not (eobp)) (zerop (forward-line 1)))
8267       (gnus-summary-hide-thread)))
8268   (gnus-summary-position-cursor))
8269
8270 (defun gnus-summary-hide-thread ()
8271   "Hide thread subtrees.
8272 Returns nil if no threads were there to be hidden."
8273   (interactive)
8274   (let ((buffer-read-only nil)
8275         (start (point))
8276         (level (gnus-summary-thread-level))
8277         (end (point)))
8278     ;; Go forward until either the buffer ends or the subthread
8279     ;; ends. 
8280     (if (eobp)
8281         ()
8282       (while (and (zerop (forward-line 1))
8283                   (> (gnus-summary-thread-level) level))
8284         (setq end (point)))
8285       (prog1
8286           (save-excursion
8287             (goto-char end)
8288             (search-backward "\n" start t))
8289         (subst-char-in-region start end ?\n ?\^M t)
8290         (forward-line -1)))))
8291
8292 (defun gnus-summary-go-to-next-thread (&optional previous)
8293   "Go to the same level (or less) next thread.
8294 If PREVIOUS is non-nil, go to previous thread instead.
8295 Return the article number moved to, or nil if moving was impossible."
8296   (let ((level (gnus-summary-thread-level))
8297         (article (gnus-summary-article-number)))
8298     (if previous 
8299         (while (and (zerop (gnus-summary-prev-subject 1))
8300                     (> (gnus-summary-thread-level) level)))
8301       (while (and (zerop (gnus-summary-next-subject 1))
8302                   (> (gnus-summary-thread-level) level))))
8303     (let ((oart (gnus-summary-article-number)))
8304       (and (/= oart article) oart))))
8305
8306 (defun gnus-summary-next-thread (n)
8307   "Go to the same level next N'th thread.
8308 If N is negative, search backward instead.
8309 Returns the difference between N and the number of skips actually
8310 done."
8311   (interactive "p")
8312   (let ((backward (< n 0))
8313         (n (abs n)))
8314   (while (and (> n 0)
8315               (gnus-summary-go-to-next-thread backward))
8316     (setq n (1- n)))
8317   (gnus-summary-position-cursor)
8318   (if (/= 0 n) (message "No more threads"))
8319   n))
8320
8321 (defun gnus-summary-prev-thread (n)
8322   "Go to the same level previous N'th thread.
8323 Returns the difference between N and the number of skips actually
8324 done."
8325   (interactive "p")
8326   (gnus-summary-next-thread (- n)))
8327
8328 (defun gnus-summary-go-down-thread (&optional same)
8329   "Go down one level in the current thread.
8330 If SAME is non-nil, also move to articles of the same level."
8331   (let ((level (gnus-summary-thread-level))
8332         (start (point)))
8333     (if (and (zerop (forward-line 1))
8334              (> (gnus-summary-thread-level) level))
8335         t
8336       (goto-char start)
8337       nil)))
8338
8339 (defun gnus-summary-go-up-thread ()
8340   "Go up one level in the current thread."
8341   (let ((level (gnus-summary-thread-level))
8342         (start (point)))
8343     (while (and (zerop (forward-line -1))
8344                 (>= (gnus-summary-thread-level) level)))
8345     (if (>= (gnus-summary-thread-level) level)
8346         (progn
8347           (goto-char start)
8348           nil)
8349       t)))
8350
8351 (defun gnus-summary-down-thread (n)
8352   "Go down thread N steps.
8353 If N is negative, go up instead.
8354 Returns the difference between N and how many steps down that were
8355 taken."
8356   (interactive "p")
8357   (let ((up (< n 0))
8358         (n (abs n)))
8359   (while (and (> n 0)
8360               (if up (gnus-summary-go-up-thread)
8361                 (gnus-summary-go-down-thread)))
8362     (setq n (1- n)))
8363   (gnus-summary-position-cursor)
8364   (if (/= 0 n) (message "Can't go further"))
8365   n))
8366
8367 (defun gnus-summary-up-thread (n)
8368   "Go up thread N steps.
8369 If N is negative, go up instead.
8370 Returns the difference between N and how many steps down that were
8371 taken."
8372   (interactive "p")
8373   (gnus-summary-down-thread (- n)))
8374
8375 (defun gnus-summary-kill-thread (unmark)
8376   "Mark articles under current thread as read.
8377 If the prefix argument is positive, remove any kinds of marks.
8378 If the prefix argument is negative, tick articles instead."
8379   (interactive "P")
8380   (if unmark
8381       (setq unmark (prefix-numeric-value unmark)))
8382   (let ((killing t)
8383         (level (gnus-summary-thread-level)))
8384     (save-excursion
8385       (while killing
8386         ;; Mark the article...
8387         (cond ((null unmark) (gnus-summary-mark-as-read nil gnus-killed-mark))
8388               ((> unmark 0) (gnus-summary-tick-article nil t))
8389               (t (gnus-summary-tick-article)))
8390         ;; ...and go forward until either the buffer ends or the subtree
8391         ;; ends. 
8392         (if (not (and (zerop (forward-line 1))
8393                       (> (gnus-summary-thread-level) level)))
8394             (setq killing nil))))
8395     ;; Hide killed subtrees.
8396     (and (null unmark)
8397          gnus-thread-hide-killed
8398          (gnus-summary-hide-thread))
8399     ;; If marked as read, go to next unread subject.
8400     (if (null unmark)
8401         ;; Go to next unread subject.
8402         (gnus-summary-next-subject 1 t)))
8403   (gnus-set-mode-line 'summary))
8404
8405 ;; Summary sorting commands
8406
8407 (defun gnus-summary-sort-by-number (reverse)
8408   "Sort summary buffer by article number.
8409 Argument REVERSE means reverse order."
8410   (interactive "P")
8411   (gnus-summary-sort 
8412    (cons 'gnus-summary-article-number 'gnus-thread-sort-by-number) reverse))
8413
8414 (defun gnus-summary-sort-by-author (reverse)
8415   "Sort summary buffer by author name alphabetically.
8416 If case-fold-search is non-nil, case of letters is ignored.
8417 Argument REVERSE means reverse order."
8418   (interactive "P")
8419   (gnus-summary-sort
8420    (cons
8421     (lambda ()
8422       (let ((extract (funcall
8423                       gnus-extract-address-components
8424                       (header-from (gnus-get-header-by-number
8425                                     (gnus-summary-article-number))))))
8426         (or (car extract) (cdr extract))))
8427     'gnus-thread-sort-by-author)
8428    reverse))
8429
8430 (defun gnus-summary-sort-by-subject (reverse)
8431   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
8432 If case-fold-search is non-nil, case of letters is ignored.
8433 Argument REVERSE means reverse order."
8434   (interactive "P")
8435   (gnus-summary-sort
8436    (cons
8437     (lambda ()
8438       (downcase (gnus-simplify-subject (gnus-summary-subject-string))))
8439     'gnus-thread-sort-by-subject)
8440    reverse))
8441
8442 (defun gnus-summary-sort-by-date (reverse)
8443   "Sort summary buffer by date.
8444 Argument REVERSE means reverse order."
8445   (interactive "P")
8446   (gnus-summary-sort
8447    (cons
8448     (lambda ()
8449       (gnus-sortable-date
8450        (header-date 
8451         (gnus-get-header-by-number (gnus-summary-article-number)))))
8452     'gnus-thread-sort-by-date)
8453    reverse))
8454
8455 (defun gnus-summary-sort-by-score (reverse)
8456   "Sort summary buffer by score.
8457 Argument REVERSE means reverse order."
8458   (interactive "P")
8459   (gnus-summary-sort 
8460    (cons 'gnus-summary-article-score 'gnus-thread-sort-by-score)
8461    (not reverse)))
8462
8463 (defvar gnus-summary-already-sorted nil)
8464 (defun gnus-summary-sort (predicate reverse)
8465   ;; Sort summary buffer by PREDICATE.  REVERSE means reverse order. 
8466   (if gnus-summary-already-sorted
8467       ()
8468     (let (buffer-read-only)
8469       (if (not gnus-show-threads)
8470           (progn
8471             (goto-char (point-min))
8472             (sort-subr reverse 'forward-line 'end-of-line (car predicate)))
8473         (let ((gnus-thread-sort-functions (list (cdr predicate)))
8474               (gnus-summary-already-sorted nil))
8475           (gnus-summary-prepare))))))
8476   
8477 (defun gnus-sortable-date (date)
8478   "Make sortable string by string-lessp from DATE.
8479 Timezone package is used."
8480   (let* ((date   (timezone-fix-time date nil nil)) ;[Y M D H M S]
8481          (year   (aref date 0))
8482          (month  (aref date 1))
8483          (day    (aref date 2)))
8484     (timezone-make-sortable-date 
8485      year month day 
8486      (timezone-make-time-string
8487       (aref date 3) (aref date 4) (aref date 5)))))
8488
8489
8490 ;; Summary saving commands.
8491
8492 (defun gnus-summary-save-article (n)
8493   "Save the current article using the default saver function.
8494 If N is a positive number, save the N next articles.
8495 If N is a negative number, save the N previous articles.
8496 If N is nil and any articles have been marked with the process mark,
8497 save those articles instead.
8498 The variable `gnus-default-article-saver' specifies the saver function."
8499   (interactive "P")
8500   (let (articles process)
8501     (if (and n (numberp n))
8502         (let ((backward (< n 0))
8503               (n (abs n)))
8504           (save-excursion
8505             (while (and (> n 0)
8506                         (setq articles (cons (gnus-summary-article-number) 
8507                                              articles))
8508                         (gnus-summary-search-forward nil nil backward))
8509               (setq n (1- n))))
8510           (setq articles (sort articles (function <))))
8511       (if gnus-newsgroup-processable
8512           (progn
8513             (setq articles (setq gnus-newsgroup-processable
8514                                  (nreverse gnus-newsgroup-processable)))
8515             (setq process t))
8516         (setq articles (list (gnus-summary-article-number)))))
8517     (while articles
8518       (let ((header (gnus-gethash (int-to-string (car articles))
8519                                   gnus-newsgroup-headers-hashtb-by-number)))
8520         (if (vectorp header)
8521             (progn
8522               (gnus-summary-select-article t nil nil (car articles))
8523               (or gnus-save-all-headers
8524                   (gnus-article-hide-headers t))
8525               ;; Remove any X-Gnus lines.
8526               (save-excursion
8527                 (save-restriction
8528                   (set-buffer gnus-article-buffer)
8529                   (let ((buffer-read-only nil))
8530                     (goto-char (point-min))
8531                     (narrow-to-region (point) (or (search-forward "\n\n" nil t)
8532                                                   (point-max)))
8533                     (while (re-search-forward "^X-Gnus" nil t)
8534                       (delete-region (progn (beginning-of-line) (point))
8535                                      (progn (forward-line 1) (point))))
8536                     (widen))))
8537               (if gnus-default-article-saver
8538                   (funcall gnus-default-article-saver)
8539                 (error "No default saver is defined.")))
8540           (if (assq 'name header)
8541               (gnus-copy-file (cdr (assq 'name header)))
8542             (message "Article %d is unsaveable" (car articles)))))
8543       (if process
8544           (gnus-summary-remove-process-mark (car articles)))
8545       (setq articles (cdr articles)))
8546     (if process (setq gnus-newsgroup-processable 
8547                       (nreverse gnus-newsgroup-processable)))
8548     (gnus-summary-position-cursor)
8549     n))
8550
8551 (defun gnus-summary-pipe-output (arg)
8552   "Pipe the current article to a subprocess.
8553 If N is a positive number, pipe the N next articles.
8554 If N is a negative number, pipe the N previous articles.
8555 If N is nil and any articles have been marked with the process mark,
8556 pipe those articles instead."
8557   (interactive "P")
8558   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8559     (gnus-summary-save-article arg)))
8560
8561 (defun gnus-summary-save-article-mail (arg)
8562   "Append the current article to an mail file.
8563 If N is a positive number, save the N next articles.
8564 If N is a negative number, save the N previous articles.
8565 If N is nil and any articles have been marked with the process mark,
8566 save those articles instead."
8567   (interactive "P")
8568   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8569     (gnus-summary-save-article arg)))
8570
8571 (defun gnus-summary-save-article-rmail (arg)
8572   "Append the current article to an rmail file.
8573 If N is a positive number, save the N next articles.
8574 If N is a negative number, save the N previous articles.
8575 If N is nil and any articles have been marked with the process mark,
8576 save those articles instead."
8577   (interactive "P")
8578   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
8579     (gnus-summary-save-article arg)))
8580
8581 (defun gnus-summary-save-article-file (arg)
8582   "Append the current article to a file.
8583 If N is a positive number, save the N next articles.
8584 If N is a negative number, save the N previous articles.
8585 If N is nil and any articles have been marked with the process mark,
8586 save those articles instead."
8587   (interactive "P")
8588   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8589     (gnus-summary-save-article arg)))
8590
8591 (defun gnus-summary-save-article-folder (arg)
8592   "Append the current article to an mh folder.
8593 If N is a positive number, save the N next articles.
8594 If N is a negative number, save the N previous articles.
8595 If N is nil and any articles have been marked with the process mark,
8596 save those articles instead."
8597   (interactive "P")
8598   (let ((gnus-default-article-saver 'gnus-summary-save-in-folder))
8599     (gnus-summary-save-article arg)))
8600
8601 (defun gnus-summary-save-in-rmail (&optional filename)
8602   "Append this article to Rmail file.
8603 Optional argument FILENAME specifies file name.
8604 Directory to save to is default to `gnus-article-save-directory' which
8605 is initialized from the SAVEDIR environment variable."
8606   (interactive)
8607   (let ((default-name
8608           (funcall gnus-rmail-save-name gnus-newsgroup-name
8609                    gnus-current-headers gnus-newsgroup-last-rmail)))
8610     (or filename
8611         (setq filename
8612               (read-file-name
8613                (concat "Save in rmail file: (default "
8614                        (file-name-nondirectory default-name) ") ")
8615                (file-name-directory default-name)
8616                default-name)))
8617     (gnus-make-directory (file-name-directory filename))
8618     (gnus-eval-in-buffer-window 
8619      gnus-article-buffer
8620      (save-excursion
8621        (save-restriction
8622          (widen)
8623          (gnus-output-to-rmail filename))))
8624     ;; Remember the directory name to save articles.
8625     (setq gnus-newsgroup-last-rmail filename)))
8626
8627 (defun gnus-summary-save-in-mail (&optional filename)
8628   "Append this article to Unix mail file.
8629 Optional argument FILENAME specifies file name.
8630 Directory to save to is default to `gnus-article-save-directory' which
8631 is initialized from the SAVEDIR environment variable."
8632   (interactive)
8633   (let ((default-name
8634           (funcall gnus-mail-save-name gnus-newsgroup-name
8635                    gnus-current-headers gnus-newsgroup-last-mail)))
8636     (or filename
8637         (setq filename
8638               (read-file-name
8639                (concat "Save in Unix mail file: (default "
8640                        (file-name-nondirectory default-name) ") ")
8641                (file-name-directory default-name)
8642                default-name)))
8643     (setq filename
8644           (expand-file-name filename
8645                             (and default-name
8646                                  (file-name-directory default-name))))
8647     (gnus-make-directory (file-name-directory filename))
8648     (gnus-eval-in-buffer-window 
8649      gnus-article-buffer
8650      (save-excursion
8651        (save-restriction
8652          (widen)
8653          (if (and (file-readable-p filename) (rmail-file-p filename))
8654              (gnus-output-to-rmail filename)
8655            (rmail-output filename 1 t t)))))
8656     ;; Remember the directory name to save articles.
8657     (setq gnus-newsgroup-last-mail filename)))
8658
8659 (defun gnus-summary-save-in-file (&optional filename)
8660   "Append this article to file.
8661 Optional argument FILENAME specifies file name.
8662 Directory to save to is default to `gnus-article-save-directory' which
8663 is initialized from the SAVEDIR environment variable."
8664   (interactive)
8665   (let ((default-name
8666           (funcall gnus-file-save-name gnus-newsgroup-name
8667                    gnus-current-headers gnus-newsgroup-last-file)))
8668     (or filename
8669         (setq filename
8670               (read-file-name
8671                (concat "Save in file: (default "
8672                        (file-name-nondirectory default-name) ") ")
8673                (file-name-directory default-name)
8674                default-name)))
8675     (gnus-make-directory (file-name-directory filename))
8676     (gnus-eval-in-buffer-window 
8677      gnus-article-buffer
8678      (save-excursion
8679        (save-restriction
8680          (widen)
8681          (gnus-output-to-file filename))))
8682     ;; Remember the directory name to save articles.
8683     (setq gnus-newsgroup-last-file filename)))
8684
8685 (defun gnus-summary-save-in-pipe (&optional command)
8686   "Pipe this article to subprocess."
8687   (interactive)
8688   (let ((command (read-string "Shell command on article: "
8689                               gnus-last-shell-command)))
8690     (if (string-equal command "")
8691         (setq command gnus-last-shell-command))
8692     (gnus-eval-in-buffer-window 
8693      gnus-article-buffer
8694      (save-restriction
8695        (widen)
8696        (shell-command-on-region (point-min) (point-max) command nil)))
8697     (setq gnus-last-shell-command command)))
8698
8699 ;; Summary extract commands
8700
8701 (defun gnus-summary-insert-pseudos (pslist)
8702   (let ((buffer-read-only nil)
8703         (article (gnus-summary-article-number))
8704         b)
8705     (or (gnus-summary-goto-subject article)
8706         (error (format "No such article: %d" article)))
8707     (or gnus-newsgroup-headers-hashtb-by-number
8708         (gnus-make-headers-hashtable-by-number))
8709     (gnus-summary-position-cursor)
8710     (if gnus-view-pseudos
8711         (while pslist
8712           (and (assq 'execute (car pslist))
8713                (gnus-execute-command (cdr (assq 'execute (car pslist)))
8714                                      (eq gnus-view-pseudos 'not-confirm)))
8715           (setq pslist (cdr pslist)))
8716       (save-excursion
8717         (while pslist
8718           (gnus-summary-goto-subject (or (cdr (assq 'article (car pslist)))
8719                                          (gnus-summary-article-number)))
8720           (forward-line 1)
8721           (setq b (point))
8722           (insert "          " (file-name-nondirectory 
8723                                 (cdr (assq 'name (car pslist))))
8724                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
8725           (add-text-properties 
8726            b (1+ b) (list 'gnus-subject (cdr (assq 'name (car pslist)))
8727                           'gnus-number gnus-reffed-article-number
8728                           'gnus-mark gnus-unread-mark
8729                           'gnus-pseudo (car pslist)
8730                           'gnus-thread 0))
8731           (gnus-sethash (int-to-string gnus-reffed-article-number)
8732                         (car pslist) gnus-newsgroup-headers-hashtb-by-number)
8733           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
8734           (setq pslist (cdr pslist)))))))
8735
8736 (defun gnus-request-pseudo-article (props)
8737   (cond ((assq 'execute props)
8738          (gnus-execute-command (cdr (assq 'execute props)))))
8739   (let ((gnus-current-article (gnus-summary-article-number)))
8740     (run-hooks 'gnus-mark-article-hook)))
8741
8742 (defun gnus-execute-command (command &optional automatic)
8743   (save-excursion
8744     (gnus-article-setup-buffer)
8745     (set-buffer gnus-article-buffer)
8746     (let ((command (if automatic command (read-string "Command: " command)))
8747           (buffer-read-only nil))
8748       (erase-buffer)
8749       (insert "$ " command "\n\n")
8750       (if gnus-view-pseudo-asynchronously
8751           (start-process "gnus-execute" nil "sh" "-c" command)
8752         (call-process "sh" nil t nil "-c" command)))))
8753
8754 (defun gnus-copy-file (file &optional to)
8755   "Copy FILE to TO."
8756   (interactive
8757    (list (read-file-name "Copy file: " default-directory)
8758          (read-file-name "Copy file to: " default-directory)))
8759   (or to (setq to (read-file-name "Copy file to: " default-directory)))
8760   (and (file-directory-p to) 
8761        (setq to (concat (file-name-as-directory to)
8762                         (file-name-nondirectory file))))
8763   (copy-file file to))
8764
8765 ;; Summary score file commands
8766
8767 ;; Much modification of the kill (ahem, score) code and lots of the
8768 ;; functions are written by Per Abrahamsen <amanda@iesd.auc.dk>.
8769
8770 (defun gnus-summary-header (header)
8771   ;; Return HEADER for current articles, or error.
8772   (let ((article (gnus-summary-article-number)))
8773     (if article
8774         (aref (gnus-get-header-by-number article)
8775               (nth 1 (assoc header gnus-header-index)))
8776       (error "No article on current line"))))
8777
8778 (defun gnus-summary-score-entry (header match type score date &optional prompt)
8779   "Enter score file entry.
8780 HEADER is the header being scored.
8781 MATCH is the string we are looking for.
8782 TYPE is a flag indicating if it is a regexp or substring.
8783 SCORE is the score to add.
8784 DATE is the expire date."
8785   (interactive
8786    (list (completing-read "Header: "
8787                           gnus-header-index
8788                           (lambda (x) (fboundp (nth 2 x)))
8789                           t)
8790          (read-string "Match: ")
8791          (y-or-n-p "Use regexp match? ")
8792          (prefix-numeric-value current-prefix-arg)
8793          (if (y-or-n-p "Expire kill? ")
8794              (current-time-string)
8795            nil)))
8796   (let ((score (gnus-score-default score))
8797         (header (downcase header)))
8798     (and prompt (setq match (read-string 
8799                              (format "Match %s on %s, %s: " 
8800                                      (if date "temp" "permanent") 
8801                                      header
8802                                      (if (< score 0) "lower" "raise"))
8803                              match)))
8804     (and (>= (nth 1 (assoc header gnus-header-index)) 0)
8805          (gnus-summary-score-effect header match type score))
8806     (and (= score gnus-score-interactive-default-score)
8807          (setq score nil))
8808     (let ((new (cond (type
8809                   (list match score (and date (gnus-day-number date)) type))
8810                  (date
8811                   (list match score (gnus-day-number date)))
8812                  (score
8813                   (list match score))
8814                  (t
8815                   (list match))))
8816           (old (gnus-score-get header)))
8817       (gnus-score-set
8818        header
8819        (if old (cons new old) (list new))))
8820     (gnus-score-set 'touched '(t))))
8821
8822 (defun gnus-summary-score-effect (header match type score)
8823   "Simulate the effect of a score file entry.
8824 HEADER is the header being scored.
8825 MATCH is the string we are looking for.
8826 TYPE is a flag indicating if it is a regexp or substring.
8827 SCORE is the score to add."
8828   (interactive (list (completing-read "Header: "
8829                                       gnus-header-index
8830                                       (lambda (x) (fboundp (nth 2 x)))
8831                                       t)
8832                      (read-string "Match: ")
8833                      (y-or-n-p "Use regexp match? ")
8834                      (prefix-numeric-value current-prefix-arg)))
8835   (save-excursion
8836     (or (and (stringp match) (> (length match) 0))
8837       (error "No match"))
8838     (goto-char (point-min))
8839     (let ((regexp (if type
8840                       match
8841                     (concat "\\`.*" (regexp-quote match) ".*\\'"))))
8842       (while (not (eobp))
8843         (let ((content (gnus-summary-header header))
8844               (case-fold-search t))
8845           (and content
8846                (if (string-match regexp content)
8847                    (gnus-summary-raise-score score))))
8848         (beginning-of-line 2)))))
8849
8850 (defun gnus-summary-score-crossposting (score date)
8851    ;; Enter score file entry for current crossposting.
8852    ;; SCORE is the score to add.
8853    ;; DATE is the expire date.
8854    (let ((xref (gnus-summary-header "xref"))
8855          (start 0)
8856          group)
8857      (or xref (error "This article is not crossposted"))
8858      (while (string-match " \\([^ \t]+\\):" xref start)
8859        (setq start (match-end 0))
8860        (if (not (string= 
8861                  (setq group 
8862                        (substring xref (match-beginning 1) (match-end 1)))
8863                  gnus-newsgroup-name))
8864            (gnus-summary-score-entry
8865             "xref" (concat " " group ":") nil score date t)))))
8866
8867 (defun gnus-summary-temporarily-lower-by-subject (level)
8868   "Temporarily lower score by LEVEL for current subject.
8869 See `gnus-score-expiry-days'."
8870   (interactive "P")
8871   (gnus-summary-score-entry
8872    "subject" (gnus-simplify-subject-re (gnus-summary-header "subject"))
8873    nil (- (gnus-score-default level))
8874    (current-time-string) t))
8875
8876 (defun gnus-summary-temporarily-lower-by-author (level)
8877   "Temporarily lower score by LEVEL for current author.
8878 See `gnus-score-expiry-days'."
8879   (interactive "P")
8880   (gnus-summary-score-entry
8881    "from" (gnus-summary-header "from") nil (- (gnus-score-default level)) 
8882    (current-time-string) t))
8883
8884 (defun gnus-summary-temporarily-lower-by-body (level)
8885   "Temporarily lower score by LEVEL for a match on the body of the article.
8886 See `gnus-score-expiry-days'."
8887   (interactive "P")
8888   (gnus-summary-score-entry
8889    "body" "" nil (- (gnus-score-default level)) (current-time-string) t))
8890
8891 (defun gnus-summary-temporarily-lower-by-id (level)
8892   "Temporarily lower score by LEVEL for current message-id.
8893 See `gnus-score-expiry-days'."
8894   (interactive "P")
8895   (gnus-summary-score-entry
8896    "message-id" (gnus-summary-header "message-id") 
8897    nil (- (gnus-score-default level)) 
8898    (current-time-string)))
8899
8900 (defun gnus-summary-temporarily-lower-by-xref (level)
8901   "Temporarily lower score by LEVEL for current xref.
8902 See `gnus-score-expiry-days'."
8903   (interactive "P")
8904   (gnus-summary-score-crossposting 
8905    (- (gnus-score-default level)) (current-time-string)))
8906
8907 (defun gnus-summary-temporarily-lower-by-thread (level)
8908   "Temporarily lower score by LEVEL for current thread.
8909 See `gnus-score-expiry-days'."
8910   (interactive "P")
8911   (gnus-summary-score-entry
8912    "references" (gnus-summary-header "message-id")
8913    nil (- (gnus-score-default level)) (current-time-string)))
8914
8915 (defun gnus-summary-lower-by-subject (level)
8916   "Lower score by LEVEL for current subject."
8917   (interactive "P")
8918   (gnus-summary-score-entry
8919    "subject" (gnus-simplify-subject-re (gnus-summary-header "subject"))
8920    nil (- (gnus-score-default level)) 
8921    nil t))
8922
8923 (defun gnus-summary-lower-by-author (level)
8924   "Lower score by LEVEL for current author."
8925   (interactive "P")
8926   (gnus-summary-score-entry
8927    "from" (gnus-summary-header "from") nil 
8928    (- (gnus-score-default level)) nil t))
8929
8930 (defun gnus-summary-lower-by-body (level)
8931   "Lower score by LEVEL for a match on the body of the article."
8932   (interactive "P")
8933   (gnus-summary-score-entry
8934    "body" "" nil (- (gnus-score-default level)) nil t))
8935
8936 (defun gnus-summary-lower-by-id (level)
8937   "Lower score by LEVEL for current message-id."
8938   (interactive "P")
8939   (gnus-summary-score-entry
8940    "message-id" (gnus-summary-header "message-id") nil 
8941    (- (gnus-score-default level)) nil))
8942
8943 (defun gnus-summary-lower-by-xref (level)
8944   "Lower score by LEVEL for current xref."
8945   (interactive "P")
8946   (gnus-summary-score-crossposting (- (gnus-score-default level)) nil))
8947
8948 (defun gnus-summary-lower-followups-to-author (level)
8949   "Lower score by LEVEL for all followups to the current author."
8950   (interactive "P")
8951   (gnus-summary-raise-followups-to-author
8952    (- (gnus-score-default level))))
8953
8954 (defun gnus-summary-temporarily-raise-by-subject (level)
8955   "Temporarily raise score by LEVEL for current subject.
8956 See `gnus-score-expiry-days'."
8957   (interactive "P")
8958   (gnus-summary-score-entry
8959    "subject" (gnus-simplify-subject-re (gnus-summary-header "subject"))
8960    nil level (current-time-string) t))
8961
8962 (defun gnus-summary-temporarily-raise-by-author (level)
8963   "Temporarily raise score by LEVEL for current author.
8964 See `gnus-score-expiry-days'."
8965   (interactive "P")
8966   (gnus-summary-score-entry
8967    "from" (gnus-summary-header "from") nil level (current-time-string) t))
8968
8969 (defun gnus-summary-temporarily-raise-by-body (level)
8970   "Temporarily raise score by LEVEL for a match on the body of the article.
8971 See `gnus-score-expiry-days'."
8972   (interactive "P")
8973   (gnus-summary-score-entry "body" "" nil level (current-time-string) t))
8974
8975 (defun gnus-summary-temporarily-raise-by-id (level)
8976   "Temporarily raise score by LEVEL for current message-id.
8977 See `gnus-score-expiry-days'."
8978   (interactive "P")
8979   (gnus-summary-score-entry
8980    "message-id" (gnus-summary-header "message-id") 
8981    nil level (current-time-string)))
8982
8983 (defun gnus-summary-temporarily-raise-by-xref (level)
8984   "Temporarily raise score by LEVEL for current xref.
8985 See `gnus-score-expiry-days'."
8986   (interactive "P")
8987   (gnus-summary-score-crossposting level (current-time-string)))
8988
8989 (defun gnus-summary-temporarily-raise-by-thread (level)
8990   "Temporarily raise score by LEVEL for current thread.
8991 See `gnus-score-expiry-days'."
8992   (interactive "P")
8993   (gnus-summary-score-entry
8994    "references" (gnus-summary-header "message-id")
8995    nil level (current-time-string)))
8996
8997 (defun gnus-summary-raise-by-subject (level)
8998   "Raise score by LEVEL for current subject."
8999   (interactive "P")
9000   (gnus-summary-score-entry
9001    "subject" (gnus-simplify-subject-re (gnus-summary-header "subject"))
9002    nil level nil t))
9003
9004 (defun gnus-summary-raise-by-author (level)
9005   "Raise score by LEVEL for current author."
9006   (interactive "P")
9007   (gnus-summary-score-entry
9008    "from" (gnus-summary-header "from") nil level nil t))
9009
9010 (defun gnus-summary-raise-by-body (level)
9011   "Raise score by LEVEL for a match on the body of the article."
9012   (interactive "P")
9013   (gnus-summary-score-entry "body" "" nil level nil t))
9014
9015 (defun gnus-summary-raise-by-id (level)
9016   "Raise score by LEVEL for current message-id."
9017   (interactive "P")
9018   (gnus-summary-score-entry
9019    "message-id" (gnus-summary-header "message-id") nil level nil))
9020
9021 (defun gnus-summary-raise-by-xref (level)
9022   "Raise score by LEVEL for current xref."
9023   (interactive "P")
9024   (gnus-summary-score-crossposting level nil))
9025
9026 (defun gnus-summary-raise-followups-to-author (level)
9027   "Raise score by LEVEL for all followups to the current author."
9028   (interactive "P")
9029   (let ((article (gnus-summary-article-number)))
9030     (if article (setq gnus-current-headers (gnus-get-header-by-number article))
9031       (error "No article on current line")))
9032   (gnus-kill-file-raise-followups-to-author
9033    (gnus-score-default level)))
9034
9035 ;; Summary kill commands.
9036
9037 (defun gnus-summary-edit-global-kill (article)
9038   "Edit the global score file."
9039   (interactive (list (gnus-summary-article-number)))
9040   (gnus-group-edit-global-kill article))
9041
9042 (defun gnus-summary-edit-local-kill ()
9043   "Edit a local score file applied to the current newsgroup."
9044   (interactive)
9045   (setq gnus-current-headers 
9046         (gnus-gethash 
9047          (int-to-string (gnus-summary-article-number))
9048          gnus-newsgroup-headers-hashtb-by-number))
9049   (gnus-set-global-variables)
9050   (gnus-group-edit-local-kill 
9051    (gnus-summary-article-number) gnus-newsgroup-name))
9052
9053 \f
9054 ;;;
9055 ;;; Gnus article mode
9056 ;;;
9057
9058 (put 'gnus-article-mode 'mode-class 'special)
9059
9060 (if gnus-article-mode-map
9061     nil
9062   (setq gnus-article-mode-map (make-keymap))
9063   (suppress-keymap gnus-article-mode-map)
9064   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
9065   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
9066   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
9067   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
9068   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
9069   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
9070   (define-key gnus-article-mode-map "\C-c\C-M" 'gnus-article-mail-with-original)
9071   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
9072   
9073   ;; Duplicate almost all summary keystrokes in the article mode map.
9074   (let ((commands 
9075          (list "#" "\M-#" "\C-c\M-#" "\r" "n" "p"
9076                "N" "P" "\M-\C-n" "\M-\C-p" "." "\M-s" "\M-r"
9077                "<" ">" "l" "j" "^" "\M-^" "-" "u" "U" "d" "D"
9078                "\M-u" "\M-U" "k" "\C-k" "\M-\C-k" "c" "x" "X" 
9079                "\M-\C-x" "\M-\177" "b" "B" "$" "w" "\C-c\C-r"
9080                "t" "\M-t" "a" "f" "F" "C" "S" "r" "R" "\C-c\C-f"
9081                "m" "o" "\C-o" "|" "\M-m" "\M-\C-m" "\M-k" "m" "M"
9082                "V" "\C-c\C-d" "q" "Q")))
9083     (while commands
9084       (define-key gnus-article-mode-map (car commands) 
9085         'gnus-article-summary-command)
9086       (setq commands (cdr commands)))))
9087
9088
9089 (defun gnus-article-mode ()
9090   "Major mode for displaying an article.
9091
9092 All normal editing commands are switched off.
9093
9094 The following commands are available:
9095
9096 \\<gnus-article-mode-map>
9097 \\[gnus-article-next-page]\t Scroll the article one page forwards
9098 \\[gnus-article-prev-page]\t Scroll the article one page backwards
9099 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
9100 \\[gnus-article-show-summary]\t Display the summary buffer
9101 \\[gnus-article-mail]\t Send a reply to the address near point
9102 \\[gnus-article-mail-with-original]\t Send a reply to the address near point; include the original article
9103 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
9104 \\[gnus-info-find-node]\t Go to the Gnus info node"
9105   (interactive)
9106   (if gnus-visual (gnus-article-make-menu-bar))
9107   (kill-all-local-variables)
9108   (setq mode-line-modified "-- ")
9109   (make-local-variable 'mode-line-format)
9110   (setq mode-line-format (copy-sequence mode-line-format))
9111   (and (equal (nth 3 mode-line-format) "   ")
9112        (setcar (nthcdr 3 mode-line-format) ""))
9113   (setq mode-name "Article")
9114   (setq major-mode 'gnus-article-mode)
9115   (make-local-variable 'minor-mode-alist)
9116   (or (assq 'gnus-show-mime minor-mode-alist)
9117       (setq minor-mode-alist
9118             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
9119   (use-local-map gnus-article-mode-map)
9120   (make-local-variable 'page-delimiter)
9121   (setq page-delimiter gnus-page-delimiter)
9122   (buffer-disable-undo (current-buffer))
9123   (setq buffer-read-only t)             ;Disable modification
9124   (run-hooks 'gnus-article-mode-hook))
9125
9126 (defun gnus-article-setup-buffer ()
9127   "Initialize article mode buffer."
9128   (or (get-buffer gnus-article-buffer)
9129       (save-excursion
9130         (set-buffer (get-buffer-create gnus-article-buffer))
9131         (gnus-add-current-to-buffer-list)
9132         (gnus-article-mode))))
9133
9134 (defun gnus-request-article-this-buffer (article &optional group)
9135   "Get an article and insert it into this buffer."
9136   (setq group (or group gnus-newsgroup-name))
9137   ;; Using `gnus-request-article' directly will insert the article into
9138   ;; `nntp-server-buffer' - so we'll save some time by not having to
9139   ;; copy it from the server buffer into the article buffer.
9140
9141   ;; We only request an article by message-id when we do not have the
9142   ;; headers for it, so we'll have to get those.
9143   (and (stringp article) 
9144        (let ((gnus-override-method gnus-refer-article-method))
9145          (gnus-read-header article)))
9146
9147   ;; If the article number is negative, that means that this article
9148   ;; doesn't belong in this newsgroup (possibly), so we find its
9149   ;; message-id and request it by id instead of number.
9150   (if (and (numberp article) (< article 0))
9151       (save-excursion
9152         (set-buffer gnus-summary-buffer)
9153         (let ((header (gnus-gethash (int-to-string article)
9154                                     gnus-newsgroup-headers-hashtb-by-number)))
9155           (if (vectorp header)
9156               ;; It's a real article.
9157               (setq article (header-id header))
9158             ;; It is an extracted pseudo-article.
9159             (setq article nil)
9160             (gnus-request-pseudo-article header)))))
9161   ;; Get the article and into the article buffer.
9162   (if article
9163       (progn
9164        (erase-buffer)
9165        (let ((gnus-override-method 
9166               (and (stringp article) gnus-refer-article-method)))
9167          (and (gnus-request-article article group (current-buffer))
9168               'article)))
9169     'pseudo))
9170
9171 (defun gnus-read-header (id)
9172   "Read the headers of article ID and enter them into the Gnus system."
9173   (or gnus-newsgroup-headers-hashtb-by-number
9174       (gnus-make-headers-hashtable-by-number))
9175   (let (header)
9176     (if (not (setq header 
9177                    (car (if (let ((gnus-nov-is-evil t))
9178                               (gnus-retrieve-headers 
9179                                (list id) gnus-newsgroup-name))
9180                             (gnus-get-newsgroup-headers)))))
9181         nil
9182       (if (stringp id)
9183           (header-set-number header gnus-reffed-article-number))
9184       (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers))
9185       (gnus-sethash (int-to-string (header-number header)) header
9186                     gnus-newsgroup-headers-hashtb-by-number)
9187       (if (stringp id)
9188           (setq gnus-reffed-article-number (1- gnus-reffed-article-number)))
9189       (setq gnus-current-headers header)
9190       header)))
9191
9192 (defun gnus-article-prepare (article &optional all-headers header)
9193   "Prepare ARTICLE in article mode buffer.
9194 ARTICLE should either be an article number or a Message-ID.
9195 If ARTICLE is an id, HEADER should be the article headers.
9196 If ALL-HEADERS is non-nil, no headers are hidden."
9197   (save-excursion
9198     ;; Make sure we start in a summary buffer.
9199     (or (eq major-mode 'gnus-summary-mode)
9200         (set-buffer gnus-summary-buffer))
9201     (setq gnus-summary-buffer (current-buffer))
9202     ;; Make sure the connection to the server is alive.
9203     (or (gnus-server-opened (gnus-find-method-for-group gnus-newsgroup-name))
9204         (progn
9205           (gnus-check-news-server 
9206            (gnus-find-method-for-group gnus-newsgroup-name))
9207           (gnus-request-group gnus-newsgroup-name t)))
9208     (or gnus-newsgroup-headers-hashtb-by-number
9209         (gnus-make-headers-hashtable-by-number))
9210     (let* ((article (if header (header-number header) article))
9211            (summary-buffer (current-buffer))
9212            (internal-hook gnus-article-internal-prepare-hook)
9213            (bookmark (cdr (assq article gnus-newsgroup-bookmarks)))
9214            (group gnus-newsgroup-name)
9215            result)
9216       (save-excursion
9217         (gnus-article-setup-buffer)
9218         (set-buffer gnus-article-buffer)
9219         (let ((buffer-read-only nil))
9220           (if (not (setq result (gnus-request-article-this-buffer 
9221                                  article group)))
9222               ;; There is no such article.
9223               (progn
9224                 (save-excursion
9225                   (set-buffer gnus-summary-buffer)
9226                   (setq gnus-current-article 0)
9227                   (and (numberp article) 
9228                        (gnus-summary-mark-as-read article gnus-canceled-mark))
9229                   (message "No such article (may be canceled)")
9230                   (ding))
9231                 (setq gnus-article-current nil)
9232                 nil)
9233             (if (not (eq result 'article))
9234                 (progn
9235                   (save-excursion
9236                     (set-buffer summary-buffer)
9237                     (setq gnus-last-article gnus-current-article
9238                           gnus-newsgroup-history (cons gnus-current-article
9239                                                        gnus-newsgroup-history)
9240                           gnus-current-article 0
9241                           gnus-current-headers nil
9242                           gnus-article-current nil)
9243                     (gnus-configure-windows 'article)
9244                     (gnus-set-global-variables))
9245                   (gnus-set-mode-line 'article))
9246               ;; The result from the `request' was an actual article -
9247               ;; or at least some text that is now displayed in the
9248               ;; article buffer.
9249               (if (and (numberp article)
9250                        (not (eq article gnus-current-article)))
9251                   ;; Seems like a new article has been selected.
9252                   ;; `gnus-current-article' must be an article number.
9253                   (save-excursion
9254                     (set-buffer summary-buffer)
9255                     (setq gnus-last-article gnus-current-article
9256                           gnus-newsgroup-history (cons gnus-current-article
9257                                                        gnus-newsgroup-history)
9258                           gnus-current-article article
9259                           gnus-current-headers 
9260                           (gnus-get-header-by-number gnus-current-article)
9261                           gnus-article-current 
9262                           (cons gnus-newsgroup-name gnus-current-article))
9263                     (run-hooks 'gnus-mark-article-hook)
9264                     (gnus-set-mode-line 'summary)
9265                     (and gnus-visual 
9266                          (run-hooks 'gnus-visual-mark-article-hook))
9267                     ;; Set the global newsgroup variables here.
9268                     ;; Suggested by Jim Sisolak
9269                     ;; <sisolak@trans4.neep.wisc.edu>.
9270                     (gnus-set-global-variables)))
9271               ;; gnus-have-all-headers must be either T or NIL.
9272               (setq gnus-have-all-headers
9273                     (not (not (or all-headers gnus-show-all-headers))))
9274               ;; Hooks for getting information from the article.
9275               ;; This hook must be called before being narrowed.
9276               (run-hooks 'internal-hook)
9277               (run-hooks 'gnus-article-prepare-hook)
9278               ;; Decode MIME message.
9279               (if (and gnus-show-mime
9280                        (gnus-fetch-field "Mime-Version"))
9281                   (funcall gnus-show-mime-method))
9282               ;; Perform the article display hooks.
9283               (let ((buffer-read-only nil))
9284                 (run-hooks 'gnus-article-display-hook))
9285               ;; Do page break.
9286               (goto-char (point-min))
9287               (and gnus-break-pages (gnus-narrow-to-page))
9288               (gnus-set-mode-line 'article)
9289               (gnus-configure-windows 'article)
9290               (goto-char 1)
9291               (set-window-start 
9292                (get-buffer-window gnus-article-buffer) (point-min))
9293               (if bookmark
9294                   (progn
9295                     (message "Moved to bookmark")
9296                     (search-forward "\n\n" nil t)
9297                     (forward-line bookmark)))
9298               t)))))))
9299
9300 (defun gnus-article-show-all-headers ()
9301   "Show all article headers in article mode buffer."
9302   (save-excursion 
9303     (setq gnus-have-all-headers t)
9304     (gnus-article-setup-buffer)
9305     (set-buffer gnus-article-buffer)
9306     (let ((buffer-read-only nil))
9307       (remove-text-properties 1 (point-max) '(invisible t)))))
9308
9309 (defun gnus-article-hide-headers-if-wanted ()
9310   "Hide unwanted headers if `gnus-have-all-headers' is nil.
9311 Provided for backwards compatability."
9312   (or gnus-have-all-headers
9313       (gnus-article-hide-headers)))
9314
9315 (defun gnus-article-hide-headers (&optional delete)
9316   "Hide unwanted headers and possibly sort them as well."
9317   (interactive "P")
9318   (save-excursion
9319     (set-buffer gnus-article-buffer)
9320     (save-restriction
9321       (let ((sorted gnus-sorted-header-list)
9322             (buffer-read-only nil)
9323             want want-list beg want-l)
9324         ;; First we narrow to just the headers.
9325         (widen)
9326         (goto-char 1)
9327         ;; Hide any "From " lines at the beginning of (mail) articles. 
9328         (while (looking-at rmail-unix-mail-delimiter)
9329           (forward-line 1))
9330         (if (/= (point) 1) 
9331             (add-text-properties 1 (point) '(invisible t)))
9332         ;; Then treat the rest of the header lines.
9333         (narrow-to-region 
9334          (point) 
9335          (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
9336         ;; Then we use the two regular expressions
9337         ;; `gnus-ignored-headers' and `gnus-visible-headers' to
9338         ;; select which header lines is to remain visible in the
9339         ;; article buffer.
9340         (goto-char 1)
9341         (while (re-search-forward "^[^ \t]*:" nil t)
9342           (beginning-of-line)
9343           ;; We add the headers we want to keep to a list and delete
9344           ;; them from the buffer.
9345           (if (or (and (stringp gnus-visible-headers)
9346                        (looking-at gnus-visible-headers))
9347                   (and (not (stringp gnus-visible-headers))
9348                        (stringp gnus-ignored-headers)
9349                        (not (looking-at gnus-ignored-headers))))
9350               (progn
9351                 (setq beg (point))
9352                 (forward-line 1)
9353                 ;; Be sure to get multi-line headers...
9354                 (re-search-forward "^[^ \t]*:" nil t)
9355                 (beginning-of-line)
9356                 (setq want-list 
9357                       (cons (buffer-substring beg (point)) want-list))
9358                 (delete-region beg (point))
9359                 (goto-char beg))
9360             (forward-line 1)))
9361         ;; Next we perform the sorting by looking at
9362         ;; `gnus-sorted-header-list'. 
9363         (goto-char 1)
9364         (while (and sorted want-list)
9365           (setq want-l want-list)
9366           (while (and want-l
9367                       (not (string-match (car sorted) (car want-l))))
9368             (setq want-l (cdr want-l)))
9369           (if want-l 
9370               (progn
9371                 (insert (car want-l))
9372                 (setq want-list (delq (car want-l) want-list))))
9373           (setq sorted (cdr sorted)))
9374         ;; Any headers that were not matched by the sorted list we
9375         ;; just tack on the end of the visible header list.
9376         (while want-list
9377           (insert (car want-list))
9378           (setq want-list (cdr want-list)))
9379         ;; And finally we make the unwanted headers invisible.
9380         (if delete
9381             (delete-region (point) (point-max))
9382           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
9383           (add-text-properties (point) (point-max) '(invisible t)))))))
9384
9385 (defun gnus-article-hide-signature ()
9386   "Hides the signature in an article.
9387 It does this by hiding everyting after \"^-- *$\", which is what all
9388 signatures should be preceded by. Note that this may mean that parts
9389 of an article may disappear if the article has such a line in the
9390 middle of the text."
9391   (interactive)
9392   (save-excursion
9393     (set-buffer gnus-article-buffer)
9394     (let ((buffer-read-only nil))
9395       (goto-char (point-max))
9396       (if (re-search-backward "^-- *$" nil t)
9397           (progn
9398             (add-text-properties (point) (point-max) '(invisible t)))))))
9399
9400 (defun gnus-article-hide-citation ()
9401   "Hide all cited text.
9402 This function uses the famous, extremely intelligent \"shoot in foot\"
9403 algorithm - which is simply deleting all lines that start with
9404 \">\". Your mileage may vary. If you come up with anything better,
9405 please do mail it to me."
9406   (interactive)
9407   (save-excursion
9408     (set-buffer gnus-article-buffer)
9409     (let ((buffer-read-only nil))
9410       (goto-char 1)
9411       (search-forward "\n\n" nil t)
9412       (while (not (eobp))
9413         (if (looking-at ">")
9414             (add-text-properties 
9415              (point) (save-excursion (forward-line 1) (point))
9416              '(invisible t)))
9417         (forward-line 1)))))
9418
9419 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
9420 (defun gnus-article-treat-overstrike ()
9421   ;; Prepare article for overstrike commands.
9422   (interactive)
9423   (save-excursion
9424     (set-buffer gnus-article-buffer)
9425     (let ((buffer-read-only nil))
9426       (while (search-forward "\b" nil t)
9427         (let ((next (following-char))
9428               (previous (char-after (- (point) 2))))
9429           (cond ((eq next previous)
9430                  (delete-region (- (point) 2) (point))
9431                  (put-text-property (point) (1+ (point))
9432                                     'face 'bold))
9433                 ((eq next ?_)
9434                  (delete-region (1- (point)) (1+ (point)))
9435                  (put-text-property (1- (point)) (point)
9436                                     'face 'underline))
9437                 ((eq previous ?_)
9438                  (delete-region (- (point) 2) (point))
9439                  (put-text-property (point) (1+ (point))
9440                                     'face 'underline))))))))
9441
9442 (defun gnus-article-word-wrap ()
9443   "Format too long lines."
9444   (interactive)
9445   (save-excursion
9446     (set-buffer gnus-article-buffer)
9447     (let ((buffer-read-only nil))
9448       (goto-char 1)
9449       (search-forward "\n\n" nil t)
9450       (end-of-line 1)
9451       (let ((paragraph-start "^\\W"))
9452         (while (not (eobp))
9453           (and (>= (current-column) (window-width))
9454                (/= (preceding-char) ?:)
9455                (fill-paragraph nil))
9456           (end-of-line 2))))))
9457
9458 (defun gnus-article-remove-cr ()
9459   (interactive)
9460   (save-excursion
9461     (set-buffer gnus-article-buffer)
9462     (let ((buffer-read-only nil))
9463       (goto-char (point-min))
9464       (while (search-forward "\r" nil t)
9465         (replace-match "")))))
9466
9467 (defun gnus-article-de-quoted-unreadable ()
9468   (interactive)
9469   (save-excursion
9470     (save-restriction
9471       (set-buffer gnus-article-buffer)
9472       (let ((buffer-read-only nil))
9473         (widen)
9474         (goto-char (point-min))
9475         (while (re-search-forward "=[0-9A-F][0-9A-F]" nil t)
9476           (replace-match 
9477            (char-to-string 
9478             (+
9479              (* 16 (gnus-hex-char-to-integer 
9480                     (char-after (1+ (match-beginning 0)))))
9481              (gnus-hex-char-to-integer
9482               (char-after (1- (match-end 0))))))))))))
9483
9484 ;; Taken from hexl.el.
9485 (defun gnus-hex-char-to-integer (character)
9486   "Take a char and return its value as if it was a hex digit."
9487   (if (and (>= character ?0) (<= character ?9))
9488       (- character ?0)
9489     (let ((ch (logior character 32)))
9490       (if (and (>= ch ?a) (<= ch ?f))
9491           (- ch (- ?a 10))
9492         (error (format "Invalid hex digit `%c'." ch))))))
9493
9494 ;; Article savers.
9495
9496 (defun gnus-output-to-rmail (file-name)
9497   "Append the current article to an Rmail file named FILE-NAME."
9498   (require 'rmail)
9499   ;; Most of these codes are borrowed from rmailout.el.
9500   (setq file-name (expand-file-name file-name))
9501   (setq rmail-default-rmail-file file-name)
9502   (let ((artbuf (current-buffer))
9503         (tmpbuf (get-buffer-create " *Gnus-output*")))
9504     (save-excursion
9505       (or (get-file-buffer file-name)
9506           (file-exists-p file-name)
9507           (if (gnus-yes-or-no-p
9508                (concat "\"" file-name "\" does not exist, create it? "))
9509               (let ((file-buffer (create-file-buffer file-name)))
9510                 (save-excursion
9511                   (set-buffer file-buffer)
9512                   (rmail-insert-rmail-file-header)
9513                   (let ((require-final-newline nil))
9514                     (write-region (point-min) (point-max) file-name t 1)))
9515                 (kill-buffer file-buffer))
9516             (error "Output file does not exist")))
9517       (set-buffer tmpbuf)
9518       (buffer-disable-undo (current-buffer))
9519       (erase-buffer)
9520       (insert-buffer-substring artbuf)
9521       (gnus-convert-article-to-rmail)
9522       ;; Decide whether to append to a file or to an Emacs buffer.
9523       (let ((outbuf (get-file-buffer file-name)))
9524         (if (not outbuf)
9525             (append-to-file (point-min) (point-max) file-name)
9526           ;; File has been visited, in buffer OUTBUF.
9527           (set-buffer outbuf)
9528           (let ((buffer-read-only nil)
9529                 (msg (and (boundp 'rmail-current-message)
9530                           rmail-current-message)))
9531             ;; If MSG is non-nil, buffer is in RMAIL mode.
9532             (if msg
9533                 (progn (widen)
9534                        (narrow-to-region (point-max) (point-max))))
9535             (insert-buffer-substring tmpbuf)
9536             (if msg
9537                 (progn
9538                   (goto-char (point-min))
9539                   (widen)
9540                   (search-backward "\^_")
9541                   (narrow-to-region (point) (point-max))
9542                   (goto-char (1+ (point-min)))
9543                   (rmail-count-new-messages t)
9544                   (rmail-show-message msg)))))))
9545     (kill-buffer tmpbuf)))
9546
9547 (defun gnus-output-to-file (file-name)
9548   "Append the current article to a file named FILE-NAME."
9549   (setq file-name (expand-file-name file-name))
9550   (let ((artbuf (current-buffer))
9551         (tmpbuf (get-buffer-create " *Gnus-output*")))
9552     (save-excursion
9553       (set-buffer tmpbuf)
9554       (buffer-disable-undo (current-buffer))
9555       (erase-buffer)
9556       (insert-buffer-substring artbuf)
9557       ;; Append newline at end of the buffer as separator, and then
9558       ;; save it to file.
9559       (goto-char (point-max))
9560       (insert "\n")
9561       (append-to-file (point-min) (point-max) file-name))
9562     (kill-buffer tmpbuf)))
9563
9564 (defun gnus-convert-article-to-rmail ()
9565   "Convert article in current buffer to Rmail message format."
9566   (let ((buffer-read-only nil))
9567     ;; Convert article directly into Babyl format.
9568     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
9569     (goto-char (point-min))
9570     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
9571     (while (search-forward "\n\^_" nil t) ;single char
9572       (replace-match "\n^_"))           ;2 chars: "^" and "_"
9573     (goto-char (point-max))
9574     (insert "\^_")))
9575
9576 (defun gnus-narrow-to-page (&optional arg)
9577   "Make text outside current page invisible except for page delimiter.
9578 A numeric arg specifies to move forward or backward by that many pages,
9579 thus showing a page other than the one point was originally in."
9580   (interactive "P")
9581   (setq arg (if arg (prefix-numeric-value arg) 0))
9582   (save-excursion
9583     (forward-page -1)                   ;Beginning of current page.
9584     (widen)
9585     (if (> arg 0)
9586         (forward-page arg)
9587       (if (< arg 0)
9588           (forward-page (1- arg))))
9589     ;; Find the end of the page.
9590     (forward-page)
9591     ;; If we stopped due to end of buffer, stay there.
9592     ;; If we stopped after a page delimiter, put end of restriction
9593     ;; at the beginning of that line.
9594     ;; These are commented out.
9595     ;;    (if (save-excursion (beginning-of-line)
9596     ;;                  (looking-at page-delimiter))
9597     ;;  (beginning-of-line))
9598     (narrow-to-region (point)
9599                       (progn
9600                         ;; Find the top of the page.
9601                         (forward-page -1)
9602                         ;; If we found beginning of buffer, stay there.
9603                         ;; If extra text follows page delimiter on same line,
9604                         ;; include it.
9605                         ;; Otherwise, show text starting with following line.
9606                         (if (and (eolp) (not (bobp)))
9607                             (forward-line 1))
9608                         (point)))))
9609
9610 (defun gnus-gmt-to-local ()
9611   "Rewrite Date header described in GMT to local in current buffer.
9612 Intended to be used with gnus-article-prepare-hook."
9613   (save-excursion
9614     (save-restriction
9615       (widen)
9616       (goto-char (point-min))
9617       (narrow-to-region (point-min)
9618                         (progn (search-forward "\n\n" nil 'move) (point)))
9619       (goto-char (point-min))
9620       (if (re-search-forward "^Date:[ \t]\\(.*\\)$" nil t)
9621           (let ((buffer-read-only nil)
9622                 (date (buffer-substring (match-beginning 1) (match-end 1))))
9623             (delete-region (match-beginning 1) (match-end 1))
9624             (insert
9625              (timezone-make-date-arpa-standard 
9626               date nil (current-time-zone))))))))
9627
9628
9629 ;; Article mode commands
9630
9631 (defun gnus-article-next-page (lines)
9632   "Show next page of current article.
9633 If end of article, return non-nil. Otherwise return nil.
9634 Argument LINES specifies lines to be scrolled up."
9635   (interactive "P")
9636   (move-to-window-line -1)
9637   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
9638   (if (save-excursion
9639         (end-of-line)
9640         (and (pos-visible-in-window-p)  ;Not continuation line.
9641              (eobp)))
9642       ;; Nothing in this page.
9643       (if (or (not gnus-break-pages)
9644               (save-excursion
9645                 (save-restriction
9646                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
9647           t                             ;Nothing more.
9648         (gnus-narrow-to-page 1)         ;Go to next page.
9649         nil)
9650     ;; More in this page.
9651     (condition-case ()
9652         (scroll-up lines)
9653       (end-of-buffer
9654        ;; Long lines may cause an end-of-buffer error.
9655        (goto-char (point-max))))
9656     nil))
9657
9658 (defun gnus-article-prev-page (lines)
9659   "Show previous page of current article.
9660 Argument LINES specifies lines to be scrolled down."
9661   (interactive "P")
9662   (move-to-window-line 0)
9663   (if (and gnus-break-pages
9664            (bobp)
9665            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
9666       (progn
9667         (gnus-narrow-to-page -1) ;Go to previous page.
9668         (goto-char (point-max))
9669         (recenter -1))
9670     (scroll-down lines)))
9671
9672 (defun gnus-article-refer-article ()
9673   "Read article specified by message-id around point."
9674   (interactive)
9675   (search-forward ">" nil t)    ;Move point to end of "<....>".
9676   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
9677       (let ((message-id
9678              (buffer-substring (match-beginning 1) (match-end 1))))
9679         (set-buffer gnus-summary-buffer)
9680         (gnus-summary-refer-article message-id))
9681     (error "No references around point")))
9682
9683 (defun gnus-article-mail (yank)
9684   "Send a reply to the address near point.
9685 If YANK is non-nil, include the original article."
9686   (interactive "P")
9687   (let ((address 
9688          (buffer-substring
9689           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
9690           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
9691     (and address
9692          (progn
9693            (switch-to-buffer gnus-summary-buffer)
9694            (funcall gnus-mail-reply-method yank address)))))
9695
9696 (defun gnus-article-mail-with-original ()
9697   "Send a reply to the address near point and include the original article."
9698   (interactive)
9699   (gnus-article-mail 'yank))
9700
9701 (defun gnus-article-show-summary ()
9702   "Reconfigure windows to show summary buffer."
9703   (interactive)
9704   (gnus-configure-windows 'article)
9705   (pop-to-buffer gnus-summary-buffer)
9706   (gnus-summary-goto-subject gnus-current-article))
9707
9708 (defun gnus-article-describe-briefly ()
9709   "Describe article mode commands briefly."
9710   (interactive)
9711   (message
9712    (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")))
9713
9714 (defun gnus-article-summary-command ()
9715   "Execute the last keystroke in the summary buffer."
9716   (interactive)
9717   (message "                                                                              ")
9718   (let ((obuf (current-buffer))
9719         (owin (current-window-configuration)))
9720     (switch-to-buffer gnus-summary-buffer 'norecord)
9721     (execute-kbd-macro (this-command-keys))
9722     (set-buffer obuf)
9723     (let ((npoint (point)))
9724       (set-window-configuration owin)
9725       (set-window-start (get-buffer-window (current-buffer)) (point)))))
9726
9727 ;; caesar-region written by phr@prep.ai.mit.edu  Nov 86
9728 ;; Modified by tower@prep Nov 86
9729 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
9730
9731 (defun gnus-caesar-region (&optional n)
9732   "Caesar rotation of region by N, default 13, for decrypting netnews.
9733 ROT47 will be performed for Japanese text in any case."
9734   (interactive (if current-prefix-arg   ; Was there a prefix arg?
9735                    (list (prefix-numeric-value current-prefix-arg))
9736                  (list nil)))
9737   (cond ((not (numberp n)) (setq n 13))
9738         (t (setq n (mod n 26))))        ;canonicalize N
9739   (if (not (zerop n))           ; no action needed for a rot of 0
9740       (progn
9741         (if (or (not (boundp 'caesar-translate-table))
9742                 (not caesar-translate-table)
9743                 (/= (aref caesar-translate-table ?a) (+ ?a n)))
9744             (let ((i 0) 
9745                   (lower "abcdefghijklmnopqrstuvwxyz")
9746                   upper)
9747               (message "Building caesar-translate-table...")
9748               (setq caesar-translate-table (make-vector 256 0))
9749               (while (< i 256)
9750                 (aset caesar-translate-table i i)
9751                 (setq i (1+ i)))
9752               (setq lower (concat lower lower)
9753                     upper (upcase lower)
9754                     i 0)
9755               (while (< i 26)
9756                 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
9757                 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
9758                 (setq i (1+ i)))
9759               ;; ROT47 for Japanese text.
9760               ;; Thanks to ichikawa@flab.fujitsu.junet.
9761               (setq i 161)
9762               (let ((t1 (logior ?O 128))
9763                     (t2 (logior ?! 128))
9764                     (t3 (logior ?~ 128)))
9765                 (while (< i 256)
9766                   (aset caesar-translate-table i
9767                         (let ((v (aref caesar-translate-table i)))
9768                           (if (<= v t1) (if (< v t2) v (+ v 47))
9769                             (if (<= v t3) (- v 47) v))))
9770                   (setq i (1+ i))))
9771               (message "Building caesar-translate-table... done")))
9772         (let ((from (region-beginning))
9773               (to (region-end))
9774               (i 0) str len)
9775           (setq str (buffer-substring from to))
9776           (setq len (length str))
9777           (while (< i len)
9778             (aset str i (aref caesar-translate-table (aref str i)))
9779             (setq i (1+ i)))
9780           (goto-char from)
9781           (delete-region from to)
9782           (insert str)))))
9783
9784 \f
9785 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
9786
9787 ;;;###autoload
9788 (defalias 'gnus-batch-kill 'gnus-batch-score)
9789 ;;;###autoload
9790 (defun gnus-batch-score ()
9791   "Run batched scoring.
9792 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
9793 Newsgroups is a list of strings in Bnews format.  If you want to score
9794 the comp hierarchy, you'd say \"comp.all\". If you would not like to
9795 score the alt hierarchy, you'd say \"!alt.all\"."
9796   (interactive)
9797   (let* ((yes-and-no
9798           (gnus-parse-n-options
9799            (apply (function concat)
9800                   (mapcar (lambda (g) (concat g " "))
9801                           command-line-args-left))))
9802          (gnus-expert-user t)
9803          (nnmail-spool-file nil)
9804          (gnus-use-dribble-file nil)
9805          (yes (car yes-and-no))
9806          (no (cdr yes-and-no))
9807          group subscribed newsrc entry
9808          ;; Disable verbose message.
9809          gnus-novice-user gnus-large-newsgroup)
9810     ;; Eat all arguments.
9811     (setq command-line-args-left nil)
9812     ;; Start Gnus.
9813     (gnus)
9814     ;; Apply kills to specified newsgroups in command line arguments.
9815     (setq newsrc (cdr gnus-newsrc-assoc))
9816     (while newsrc
9817       (setq group (car (car newsrc)))
9818       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
9819       (if (and (<= (nth 1 (car newsrc)) 5)
9820                (and (car entry)
9821                     (or (eq (car entry) t)
9822                         (not (zerop (car entry)))))
9823                (if yes (string-match yes group) t)
9824                (or (null no) (not (string-match no group))))
9825           (progn
9826             (gnus-summary-read-group group nil t)
9827             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
9828                  (gnus-summary-exit))))
9829       (setq newsrc (cdr newsrc)))
9830     ;; Exit Emacs.
9831     (switch-to-buffer gnus-group-buffer)
9832     (gnus-group-save-newsrc)))
9833
9834 (defun gnus-apply-kill-file ()
9835   "Apply a kill file to the current newsgroup.
9836 Returns the number of articles marked as read."
9837   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
9838           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
9839       (gnus-apply-kill-file-internal)
9840     0))
9841
9842 (defun gnus-kill-save-kill-buffer ()
9843   (save-excursion
9844     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
9845       (if (get-file-buffer file)
9846           (progn
9847             (set-buffer (get-file-buffer file))
9848             (and (buffer-modified-p) (save-buffer))
9849             (kill-buffer (current-buffer)))))))
9850
9851 (defvar gnus-kill-file-name "KILL"
9852   "Suffix of the kill files.")
9853
9854 (defun gnus-newsgroup-kill-file (newsgroup)
9855   "Return the name of a kill file name for NEWSGROUP.
9856 If NEWSGROUP is nil, return the global kill file name instead."
9857   (cond ((or (null newsgroup)
9858              (string-equal newsgroup ""))
9859          ;; The global KILL file is placed at top of the directory.
9860          (expand-file-name gnus-kill-file-name
9861                            (or gnus-kill-files-directory "~/News")))
9862         ((gnus-use-long-file-name 'not-kill)
9863          ;; Append ".KILL" to newsgroup name.
9864          (expand-file-name (concat newsgroup "." gnus-kill-file-name)
9865                            (or gnus-kill-files-directory "~/News")))
9866         (t
9867          ;; Place "KILL" under the hierarchical directory.
9868          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
9869                                    "/" gnus-kill-file-name)
9870                            (or gnus-kill-files-directory "~/News")))))
9871
9872 \f
9873 ;;;
9874 ;;; Gnus Score Files
9875 ;;;
9876
9877 ;; All score code written by Per Abrahamsen <abraham@iesd.auc.dk>.
9878
9879 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9880 (defun gnus-score-set-mark-below (score)
9881   "Automatically mark articles with score below SCORE as read."
9882   (interactive 
9883    (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
9884              (string-to-int (read-string "Mark below: ")))))
9885   (setq score (or score gnus-summary-default-score 0))
9886   (gnus-score-set 'mark (list score))
9887   (gnus-score-set 'touched '(t))
9888   (setq gnus-summary-mark-below score)
9889   (gnus-summary-update-lines))
9890
9891 (defun gnus-score-set-expunge-below (score)
9892   "Automatically expunge articles with score below SCORE."
9893   (interactive 
9894    (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
9895              (string-to-int (read-string "Expunge below: ")))))
9896   (setq score (or score gnus-summary-default-score 0))
9897   (gnus-score-set 'expunge (list score))
9898   (gnus-score-set 'touched '(t)))
9899
9900 (defun gnus-score-default (level)
9901   (if level (prefix-numeric-value level) 
9902     gnus-score-interactive-default-score))
9903
9904 (defun gnus-score-set (symbol value &optional alist)
9905   ;; Set SYMBOL to VALUE in ALIST.
9906   (let* ((alist 
9907           (or alist 
9908               gnus-score-alist
9909               (progn
9910                 (gnus-score-load (gnus-score-file-name gnus-newsgroup-name))
9911                 gnus-score-alist)))
9912          (entry (assoc symbol alist)))
9913     (cond ((gnus-score-get 'read-only alist)
9914            ;; This is a read-only score file, so we do nothing.
9915            )
9916           (entry
9917            (setcdr entry value))
9918           ((null alist)
9919            (error "Empty alist"))
9920           (t
9921            (setcdr alist
9922                    (cons (cons symbol value) (cdr alist)))))))
9923
9924 (defun gnus-score-get (symbol &optional alist)
9925   ;; Get SYMBOL's definition in ALIST.
9926   (cdr (assoc symbol 
9927               (or alist 
9928                   gnus-score-alist
9929                   (progn
9930                     (gnus-score-load 
9931                      (gnus-score-file-name gnus-newsgroup-name))
9932                     gnus-score-alist)))))
9933
9934 (defun gnus-score-change-score-file (file)
9935   "Change current score alist."
9936   (interactive
9937    (list (completing-read "Score file: " gnus-score-cache)))
9938   (setq gnus-current-score-file file)
9939   (gnus-score-load-file file))
9940
9941 (defun gnus-score-edit-alist (file)
9942   "Edit the current score alist."
9943   (interactive (list gnus-current-score-file))
9944   (and (buffer-name gnus-summary-buffer) (gnus-score-save))
9945   (setq gnus-winconf-edit-score (current-window-configuration))
9946   (gnus-configure-windows 'article)
9947   (pop-to-buffer (find-file-noselect file))
9948   (message (substitute-command-keys 
9949             "\\<gnus-score-mode-map>\\[gnus-score-edit-done] to save edits"))
9950   (gnus-score-mode))
9951   
9952 (defun gnus-score-edit-file (file)
9953   "Edit a score file."
9954   (interactive 
9955    (list (read-file-name "Edit score file: " gnus-kill-files-directory)))
9956   (and (buffer-name gnus-summary-buffer) (gnus-score-save))
9957   (setq gnus-winconf-edit-score (current-window-configuration))
9958   (gnus-configure-windows 'article)
9959   (pop-to-buffer (find-file-noselect file))
9960   (message (substitute-command-keys 
9961             "\\<gnus-score-mode-map>\\[gnus-score-edit-done] to save edits"))
9962   (gnus-score-mode))
9963   
9964 (defun gnus-score-load-file (file)
9965   ;; Load score file FILE.  Returns a list a retrieved score-alists.
9966   (setq gnus-kill-files-directory (or gnus-kill-files-directory "~/News/"))
9967   (let* ((file (expand-file-name 
9968                 (or (and (string-match
9969                           (concat "^" (expand-file-name
9970                                        gnus-kill-files-directory)) 
9971                           (expand-file-name file))
9972                          file)
9973                     (concat gnus-kill-files-directory file))))
9974          (cached (assoc file gnus-score-cache))
9975          (global (member file gnus-internal-global-score-files))
9976          lists alist)
9977     (if cached
9978         ;; The score file was already loaded.
9979         (setq alist (cdr cached))
9980       ;; We load the score file.
9981       (setq gnus-score-alist nil)
9982       (setq alist (gnus-score-load-score-alist file))
9983       ;; We add '(touched) to the alist to signify that it hasn't been
9984       ;; touched (yet). 
9985       (or (assq 'touched alist) (setq alist (cons (list 'touched nil) alist)))
9986       ;; If it is a global score file, we make it read-only.
9987       (and global
9988            (not (assq 'read-only alist))
9989            (setq alist (cons (list 'read-only t) alist)))
9990       ;; Update cache.
9991       (setq gnus-score-cache
9992             (cons (cons file alist) gnus-score-cache)))
9993     ;; If there are actual scores in the alist, we add it to the
9994     ;; return value of this function.
9995     (if (memq t (mapcar (lambda (e) (stringp (car e))) alist))
9996         (setq lists (list alist)))
9997     ;; Treat the other possible atoms in the score alist.
9998     (let ((mark (car (gnus-score-get 'mark alist)))
9999           (expunge (car (gnus-score-get 'expunge alist)))
10000           (mark-and-expunge 
10001            (car (gnus-score-get 'mark-and-expunge alist)))
10002           (read-only (gnus-score-get 'read-only alist))
10003           (files (gnus-score-get 'files alist))
10004           (exclude-files (gnus-score-get 'exclude-files alist))
10005           (orphan (gnus-score-get 'orphan alist))
10006           (eval (gnus-score-get 'eval alist)))
10007       ;; We do not respect eval and files atoms from global score
10008       ;; files. 
10009       (and files (not global)
10010            (setq lists (apply 'append lists
10011                               (mapcar (lambda (file)
10012                                         (gnus-score-load-file file)) 
10013                                       files))))
10014       (and eval (not global) (eval eval))
10015       (if orphan (setq gnus-orphan-score (car orphan)))
10016       (setq gnus-summary-mark-below 
10017             (or mark mark-and-expunge gnus-summary-mark-below))
10018       (setq gnus-summary-expunge-below 
10019             (or expunge mark-and-expunge gnus-summary-expunge-below)))
10020     (setq gnus-current-score-file file)
10021     (setq gnus-score-alist alist)
10022     lists))
10023
10024 (defun gnus-score-load (file)
10025   ;; Load score FILE.
10026   (let ((cache (assoc file gnus-score-cache)))
10027     (if cache
10028         (setq gnus-score-alist (cdr cache))
10029       (setq gnus-score-alist nil)
10030       (gnus-score-load-score-alist file)
10031       (or gnus-score-alist
10032           (setq gnus-score-alist (copy-alist '((touched nil)))))
10033       (setq gnus-score-cache
10034             (cons (cons file gnus-score-alist) gnus-score-cache)))))
10035
10036 (defun gnus-score-remove-from-cache (file)
10037   (setq gnus-score-cache 
10038         (delq (assoc file gnus-score-cache) gnus-score-cache)))
10039
10040 (defun gnus-score-load-score-alist (file)
10041   (let (alist)
10042     (if (file-readable-p file)
10043         (progn
10044           (save-excursion
10045             (set-buffer (get-buffer-create " *gnus work*"))
10046             (buffer-disable-undo (current-buffer))
10047             (erase-buffer)
10048             (insert-file-contents file)
10049             (goto-char (point-min))
10050             ;; Only do the loading if the score file isn't empty.
10051             (if (save-excursion (re-search-forward "[()0-9a-zA-Z]" nil t))
10052                 (setq alist
10053                       (condition-case ()
10054                           (read (current-buffer))
10055                         (error 
10056                          (progn
10057                            (message "Problem with score file %s" file)
10058                            (ding) 
10059                            (sit-for 2)
10060                            nil))))))
10061           (if (eq (car alist) 'setq)
10062               (setq gnus-score-alist (gnus-score-transform-old-to-new alist))
10063             (setq gnus-score-alist alist))
10064           (setq gnus-score-alist
10065                 (gnus-score-check-syntax gnus-score-alist)))
10066       (setq gnus-score-alist nil))))
10067
10068 (defun gnus-score-check-syntax (alist)
10069   (cond 
10070    ((null alist)
10071     nil)
10072    ((not (consp alist))
10073     (message "Score file is not a list: %s" alist)
10074     (ding)
10075     nil)
10076    (t
10077     (let ((a alist)
10078           err)
10079       (while (and a (not err))
10080         (cond ((not (listp (car a)))
10081                (message "Illegal score element: %s" (car a))
10082                (setq err t))
10083               ((and (stringp (car (car a)))
10084                     (not (listp (nth 1 (car a)))))
10085                (message "Illegal header match: %s" (nth 1 (car a)))
10086                (setq err t))
10087               (t
10088                (setq a (cdr a)))))
10089       (if err
10090           (progn
10091             (ding)
10092             nil)
10093         alist)))))    
10094
10095 (defun gnus-score-transform-old-to-new (alist)
10096   (let* ((alist (nth 2 alist))
10097          out entry)
10098     (if (eq (car alist) 'quote)
10099         (setq alist (nth 1 alist)))
10100     (while alist
10101       (setq entry (car alist))
10102       (if (stringp (car entry))
10103           (let ((scor (cdr entry)))
10104             (setq out (cons entry out))
10105             (while scor
10106               (setcar scor
10107                       (list (car (car scor)) (nth 2 (car scor))
10108                             (and (nth 3 (car scor))
10109                                  (gnus-day-number (nth 3 (car scor))))
10110                             (if (nth 1 (car scor)) 'r 's)))
10111               (setq scor (cdr scor))))
10112         (setq out (cons (if (not (listp (cdr entry))) 
10113                             (list (car entry) (cdr entry))
10114                           entry)
10115                         out)))
10116       (setq alist (cdr alist)))
10117     (cons (list 'touched t) (nreverse out))))
10118   
10119 (defun gnus-score-save ()
10120   ;; Save all SCORE information.
10121   (let ((cache gnus-score-cache))
10122     (save-excursion
10123       (setq gnus-score-alist nil)
10124       (set-buffer (get-buffer-create "*Score*"))
10125       (buffer-disable-undo (current-buffer))
10126       (let (entry score file)
10127         (while cache
10128           (setq entry (car cache)
10129                 cache (cdr cache)
10130                 file (car entry)
10131                 score (cdr entry))
10132           (if (or (not (equal (gnus-score-get 'touched score) '(t)))
10133                   (gnus-score-get 'read-only score)
10134                   (not (file-writable-p file)))
10135               ()
10136             (setq score (delq (assq 'touched score) score))
10137             (erase-buffer)
10138             (let (emacs-lisp-mode-hook)
10139               (pp score (current-buffer)))
10140             (gnus-make-directory (file-name-directory file))
10141             (write-region (point-min) (point-max) file nil 'silent))))
10142       (kill-buffer (current-buffer)))))
10143   
10144 (defun gnus-score-headers ()
10145   ;; Score `gnus-newsgroup-headers'.
10146   (let ((func gnus-score-find-score-files-function)
10147         score-files scores)
10148     (and func (not (listp func))
10149          (setq func (list func)))
10150     ;; Go through all the functions for finding score files (or actual
10151     ;; scores) and add them to a list.
10152     (while func
10153       (and (symbolp (car func))
10154            (fboundp (car func))
10155            (setq score-files 
10156                  (nconc score-files (funcall (car func) gnus-newsgroup-name))))
10157       (setq func (cdr func)))
10158     ;; PLM: probably this is not the best place to clear orphan-score
10159     (setq gnus-orphan-score nil)
10160     ;; Load the SCORE files.
10161     (while score-files
10162       (if (stringp (car score-files))
10163           (setq scores (nconc (gnus-score-load-file (car score-files)) scores))
10164         (setq scores (nconc (car score-files) scores)))
10165       (setq score-files (cdr score-files)))
10166     (if (not (and gnus-summary-default-score
10167                   scores
10168                   (> (length gnus-newsgroup-headers)
10169                      (length gnus-newsgroup-scored))))
10170         ()
10171       (let* ((entries gnus-header-index)
10172              (now (gnus-day-number (current-time-string)))
10173              (expire (- now gnus-score-expiry-days))
10174              (headers gnus-newsgroup-headers)
10175              entry header)
10176         (message "Scoring...")
10177         ;; Create articles, an alist of the form `(HEADER . SCORE)'.
10178         (while headers
10179           (setq header (car headers)
10180                 headers (cdr headers))
10181           ;; WARNING: The assq makes the function O(N*S) while it could
10182           ;; be written as O(N+S), where N is (length gnus-newsgroup-headers)
10183           ;; and S is (length gnus-newsgroup-scored).
10184           (or (assq (header-number header) gnus-newsgroup-scored)
10185               (setq gnus-scores-articles       ;Total of 2 * N cons-cells used.
10186                     (cons (cons header (or gnus-summary-default-score 0))
10187                           gnus-scores-articles))))
10188
10189         (save-excursion
10190           (set-buffer (get-buffer-create "*Headers*"))
10191           (buffer-disable-undo (current-buffer))
10192           ;; score orphans
10193           (if gnus-orphan-score 
10194               (progn
10195                 (setq gnus-score-index 
10196                       (nth 1 (assoc "references" gnus-header-index)))
10197                 (gnus-score-orphans gnus-orphan-score)))
10198           ;; Run each header through the score process.
10199           (while entries
10200             (setq entry (car entries)
10201                   header (nth 0 entry)
10202                   entries (cdr entries))
10203             (setq gnus-score-index (nth 1 (assoc header gnus-header-index)))
10204             (if (< 0 (apply 'max (mapcar
10205                                   (lambda (score)
10206                                     (length (gnus-score-get header score)))
10207                                   scores)))
10208                 (funcall (nth 2 entry) scores header now expire)))
10209           ;; Remove the buffer.
10210           (kill-buffer (current-buffer)))
10211
10212         ;; Add articles to `gnus-newsgroup-scored'.
10213         (while gnus-scores-articles
10214           (or (= gnus-summary-default-score (cdr (car gnus-scores-articles)))
10215               (setq gnus-newsgroup-scored
10216                     (cons (cons (header-number 
10217                                  (car (car gnus-scores-articles)))
10218                                 (cdr (car gnus-scores-articles)))
10219                           gnus-newsgroup-scored)))
10220           (setq gnus-scores-articles (cdr gnus-scores-articles)))
10221
10222         (message "Scoring...done")))))
10223
10224
10225 (defun gnus-get-new-thread-ids (articles)
10226   (let ((index (nth 1 (assoc "message-id" gnus-header-index)))
10227         (refind gnus-score-index)
10228         id-list art this tref)
10229     (while articles
10230       (setq art (car articles)
10231             this (aref (car art) index)
10232             tref (aref (car art) refind)
10233             articles (cdr articles))
10234       (if (string-equal tref "")        ;no references line
10235           (setq id-list (cons this id-list))))
10236     id-list))
10237
10238 ;; Orphan functions written by plm@atcmp.nl (Peter Mutsaers).
10239 (defun gnus-score-orphans (score)
10240   (let ((new-thread-ids (gnus-get-new-thread-ids gnus-scores-articles))
10241         (index (nth 1 (assoc "references" gnus-header-index)))
10242         alike articles art arts this last this-id)
10243     
10244     (setq gnus-scores-articles (sort gnus-scores-articles 'gnus-score-string<)
10245           articles gnus-scores-articles)
10246
10247     ;;more or less the same as in gnus-score-string
10248     (erase-buffer)
10249     (while articles
10250       (setq art (car articles)
10251             this (aref (car art) gnus-score-index)
10252             articles (cdr articles))
10253       ;;completely skip if this is empty (not a child, so not an orphan)
10254       (if (not (string= this ""))
10255           (if (equal last this)
10256               ;; O(N*H) cons-cells used here, where H is the number of
10257               ;; headers.
10258               (setq alike (cons art alike))
10259             (if last
10260                 (progn
10261                   ;; Insert the line, with a text property on the
10262                   ;; terminating newline refering to the articles with
10263                   ;; this line.
10264                   (insert last ?\n)
10265                   (put-text-property (1- (point)) (point) 'articles alike)))
10266             (setq alike (list art)
10267                   last this))))
10268     (and last                           ; Bwadr, duplicate code.
10269          (progn
10270            (insert last ?\n)                    
10271            (put-text-property (1- (point)) (point) 'articles alike)))
10272
10273     ;; PLM: now delete those lines that contain an entry from new-thread-ids
10274     (while new-thread-ids
10275       (setq this-id (car new-thread-ids)
10276             new-thread-ids (cdr new-thread-ids))
10277       (goto-char (point-min))
10278       (while (search-forward this-id nil t)
10279         ;; found a match. remove this line
10280         (progn
10281           (beginning-of-line)
10282           (kill-line 1))))
10283
10284     ;; now for each line: update its articles with score by moving to
10285     ;; every end-of-line in the buffer and read the articles property
10286     (goto-char (point-min))
10287     (while (eq 0 (progn
10288                    (end-of-line)
10289                    (setq arts (get-text-property (point) 'articles))
10290                    (while arts
10291                      (setq art (car arts)
10292                            arts (cdr arts))
10293                      (setcdr art (+ score (cdr art))))
10294                    (forward-line))))))
10295              
10296
10297 (defun gnus-score-integer (scores header now expire)
10298   (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
10299         alike last this art entries alist articles)
10300
10301     ;; Find matches.
10302     (while scores
10303       (setq alist (car scores)
10304             scores (cdr scores)
10305             entries (assoc header alist))
10306       (while (cdr entries)              ;First entry is the header index.
10307         (let* ((rest (cdr entries))             
10308                (kill (car rest))
10309                (match (nth 0 kill))
10310                (type (or (nth 3 kill) '>))
10311                (score (or (nth 1 kill) gnus-score-interactive-default-score))
10312                (date (nth 2 kill))
10313                (found nil)
10314                (match-func (if (or (eq type '>) (eq type '<) (eq type '<=)
10315                                    (eq type '>=) (eq type '=))
10316                                type
10317                              (error "Illegal match type: %s" type)))
10318                (articles gnus-scores-articles)
10319                arts art)
10320           ;; Instead of doing all the clever stuff that
10321           ;; `gnus-score-string' does to minimize searches and stuff,
10322           ;; I will assume that people generally will put so few
10323           ;; matches on numbers that any cleverness will take more
10324           ;; time than one would gain.
10325           (while articles
10326             (and (funcall match-func match 
10327                           (or (aref (car (car articles)) gnus-score-index) 0))
10328                  (progn
10329                    (setq found t)
10330                    (setcdr (car articles) (+ score (cdr (car articles))))))
10331             (setq articles (cdr articles)))
10332           ;; Update expire date
10333           (cond ((null date))           ;Permanent entry.
10334                 (found                  ;Match, update date.
10335                  (gnus-score-set 'touched '(t) alist)
10336                  (setcar (nthcdr 2 kill) now))
10337                 ((< date expire) ;Old entry, remove.
10338                  (gnus-score-set 'touched '(t) alist)
10339                  (setcdr entries (cdr rest))
10340                  (setq rest entries)))
10341           (setq entries rest))))))
10342
10343 (defun gnus-score-date (scores header now expire)
10344   (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
10345         alike last this art entries alist articles)
10346
10347     ;; Find matches.
10348     (while scores
10349       (setq alist (car scores)
10350             scores (cdr scores)
10351             entries (assoc header alist))
10352       (while (cdr entries)              ;First entry is the header index.
10353         (let* ((rest (cdr entries))             
10354                (kill (car rest))
10355                (match (timezone-make-date-sortable (nth 0 kill)))
10356                (type (or (nth 3 kill) 'before))
10357                (score (or (nth 1 kill) gnus-score-interactive-default-score))
10358                (date (nth 2 kill))
10359                (found nil)
10360                (match-func 
10361                 (cond ((eq type 'after) 'string<)
10362                       ((eq type 'before) 'gnus-string>)
10363                       ((eq type 'at) 'string=)
10364                       (t (error "Illegal match type: %s" type))))
10365                (articles gnus-scores-articles)
10366                arts art l)
10367           ;; Instead of doing all the clever stuff that
10368           ;; `gnus-score-string' does to minimize searches and stuff,
10369           ;; I will assume that people generally will put so few
10370           ;; matches on numbers that any cleverness will take more
10371           ;; time than one would gain.
10372           (while articles
10373             (and
10374              (setq l (aref (car (car articles)) gnus-score-index))
10375              (funcall match-func match (timezone-make-date-sortable l))
10376              (progn
10377                (setq found t)
10378                (setcdr (car articles) (+ score (cdr (car articles))))))
10379             (setq articles (cdr articles)))
10380           ;; Update expire date
10381           (cond ((null date))           ;Permanent entry.
10382                 (found                  ;Match, update date.
10383                  (gnus-score-set 'touched '(t) alist)
10384                  (setcar (nthcdr 2 kill) now))
10385                 ((< date expire) ;Old entry, remove.
10386                  (gnus-score-set 'touched '(t) alist)
10387                  (setcdr entries (cdr rest))
10388                  (setq rest entries)))
10389           (setq entries rest))))))
10390
10391 (defun gnus-score-body (scores header now expire)
10392   (save-excursion
10393     (set-buffer nntp-server-buffer)
10394     (save-restriction
10395       (let* ((buffer-read-only nil)
10396              (articles gnus-scores-articles)
10397              (all-scores scores)
10398              (request-func (cond ((string= "head" (downcase header))
10399                                   'gnus-request-head)
10400                                  ((string= "body" (downcase header))
10401                                   'gnus-request-body)
10402                                  (t 'gnus-request-article)))
10403              alike last this art entries alist ofunc article)
10404         ;; Not all backends support partial fetching.  In that case,
10405         ;; we just fetch the entire article.
10406         (or (gnus-check-backend-function request-func gnus-newsgroup-name)
10407             (progn
10408               (setq ofunc request-func)
10409               (setq request-func 'gnus-request-article)))
10410         (while articles
10411           (setq article (header-number (car (car articles))))
10412           (message "Scoring on article %s..." article)
10413           (if (not (funcall request-func article gnus-newsgroup-name))
10414               ()
10415             (widen)
10416             (goto-char (point-min))
10417             ;; If just parts of the article is to be searched, but the
10418             ;; backend didn't support partial fetching, we just narrow
10419             ;; to the relevant parts.
10420             (if ofunc
10421                 (if (eq ofunc 'gnus-request-head)
10422                     (narrow-to-region
10423                      (point)
10424                      (or (search-forward "\n\n" nil t) (point-max)))
10425                   (narrow-to-region
10426                    (or (search-forward "\n\n" nil t) (point))
10427                    (point-max))))
10428             (setq scores all-scores)
10429             ;; Find matches.
10430             (while scores
10431               (setq alist (car scores)
10432                     scores (cdr scores)
10433                     entries (assoc header alist))
10434               (while (cdr entries)      ;First entry is the header index.
10435                 (let* ((rest (cdr entries))             
10436                        (kill (car rest))
10437                        (match (nth 0 kill))
10438                        (type (or (nth 3 kill) 's))
10439                        (score (or (nth 1 kill) 
10440                                   gnus-score-interactive-default-score))
10441                        (date (nth 2 kill))
10442                        (found nil)
10443                        (case-fold-search 
10444                         (not (or (eq type 'R) (eq type 'S)
10445                                  (eq type 'Regexp) (eq type 'String))))
10446                        (search-func 
10447                         (cond ((or (eq type 'r) (eq type 'R)
10448                                    (eq type 'regexp) (eq type 'Regexp))
10449                                're-search-forward)
10450                               ((or (eq type 's) (eq type 'S)
10451                                    (eq type 'string) (eq type 'String))
10452                                'search-forward)
10453                               (t
10454                                (error "Illegal match type: %s" type))))
10455                        arts art)
10456                   (goto-char (point-min))
10457                   (if (funcall search-func match nil t)
10458                       ;; Found a match, update scores.
10459                       (progn
10460                         (setcdr (car articles) (+ score (cdr (car articles))))
10461                         (setq found t)))
10462                   ;; Update expire date
10463                   (cond ((null date))   ;Permanent entry.
10464                         (found          ;Match, update date.
10465                          (gnus-score-set 'touched '(t) alist)
10466                          (setcar (nthcdr 2 kill) now))
10467                         ((< date expire) ;Old entry, remove.
10468                          (gnus-score-set 'touched '(t) alist)
10469                          (setcdr entries (cdr rest))
10470                          (setq rest entries)))
10471                   (setq entries rest)))))
10472           (setq articles (cdr articles)))))))
10473
10474 (defun gnus-score-string (scores header now expire)
10475   ;; Score ARTICLES according to HEADER in SCORES.
10476   ;; Update matches entries to NOW and remove unmatched entried older
10477   ;; than EXPIRE.
10478   
10479   ;; Insert the unique article headers in the buffer.
10480   (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
10481         ;; gnus-score-index is used as a free variable.
10482         alike last this art entries alist articles)
10483
10484     ;; Sorting the articles costs os O(N*log N) but will allow us to
10485     ;; only match with each unique header.  Thus the actual matching
10486     ;; will be O(M*U) where M is the number of strings to match with,
10487     ;; and U is the number of unique headers.  It is assumed (but
10488     ;; untested) this will be a net win because of the large constant
10489     ;; factor involved with string matching.
10490     (setq gnus-scores-articles (sort gnus-scores-articles 'gnus-score-string<)
10491           articles gnus-scores-articles)
10492
10493     (erase-buffer)
10494     (while articles
10495       (setq art (car articles)
10496             this (aref (car art) gnus-score-index)
10497             articles (cdr articles))
10498       (if (equal last this)
10499           ;; O(N*H) cons-cells used here, where H is the number of
10500           ;; headers.
10501           (setq alike (cons art alike))
10502         (if last
10503             (progn
10504               ;; Insert the line, with a text property on the
10505               ;; terminating newline refering to the articles with
10506               ;; this line.
10507               (insert last ?\n)
10508               (put-text-property (1- (point)) (point) 'articles alike)))
10509         (setq alike (list art)
10510               last this)))
10511     (and last                           ; Bwadr, duplicate code.
10512          (progn
10513            (insert last ?\n)                    
10514            (put-text-property (1- (point)) (point) 'articles alike)))
10515   
10516     ;; Find matches.
10517     (while scores
10518       (setq alist (car scores)
10519             scores (cdr scores)
10520             entries (assoc header alist))
10521       (while (cdr entries)              ;First entry is the header index.
10522         (let* ((rest (cdr entries))             
10523                (kill (car rest))
10524                (match (nth 0 kill))
10525                (type (or (nth 3 kill) 's))
10526                (score (or (nth 1 kill) gnus-score-interactive-default-score))
10527                (date (nth 2 kill))
10528                (found nil)
10529                (case-fold-search 
10530                 (not (or (eq type 'R) (eq type 'S)
10531                          (eq type 'Regexp) (eq type 'String))))
10532                (search-func (cond ((or (eq type 'r) (eq type 'R)
10533                                        (eq type 'regexp) (eq type 'Regexp))
10534                                    're-search-forward)
10535                                   ((or (eq type 's) (eq type 'S)
10536                                        (eq type 'string) (eq type 'String))
10537                                    'search-forward)
10538                                   (t
10539                                    (error "Illegal match type: %s" type))))
10540                arts art)
10541           (goto-char (point-min))
10542           (while (funcall search-func match nil t)
10543             (end-of-line 1)
10544             (setq found t
10545                   arts (get-text-property (point) 'articles))
10546             ;; Found a match, update scores.
10547             (while arts
10548               (setq art (car arts)
10549                     arts (cdr arts))
10550               (setcdr art (+ score (cdr art)))))
10551           ;; Update expire date
10552           (cond ((null date))           ;Permanent entry.
10553                 (found                  ;Match, update date.
10554                  (gnus-score-set 'touched '(t) alist)
10555                  (setcar (nthcdr 2 kill) now))
10556                 ((< date expire) ;Old entry, remove.
10557                  (gnus-score-set 'touched '(t) alist)
10558                  (setcdr entries (cdr rest))
10559                  (setq rest entries)))
10560           (setq entries rest))))))
10561
10562 (defun gnus-score-string< (a1 a2)
10563   ;; Compare headers in articles A2 and A2.
10564   ;; The header index used is the free variable `gnus-score-index'.
10565   (string-lessp (aref (car a1) gnus-score-index)
10566                 (aref (car a2) gnus-score-index)))
10567
10568 (defun gnus-score-build-cons (article)
10569   ;; Build a `gnus-newsgroup-scored' type cons from ARTICLE.
10570   (cons (header-number (car article)) (cdr article)))
10571
10572 (defconst gnus-header-index
10573   ;; Name to index alist.
10574   '(("number" 0 gnus-score-integer)
10575     ("subject" 1 gnus-score-string)
10576     ("from" 2 gnus-score-string)
10577     ("date" 3 gnus-score-date)
10578     ("message-id" 4 gnus-score-string) 
10579     ("references" 5 gnus-score-string) 
10580     ("chars" 6 gnus-score-integer) 
10581     ("lines" 7 gnus-score-integer) 
10582     ("xref" 8 gnus-score-string)
10583     ("head" -1 gnus-score-body)
10584     ("body" -1 gnus-score-body)
10585     ("all" -1 gnus-score-body)))
10586
10587 (defun gnus-score-file-name (newsgroup)
10588   "Return the name of a score file for NEWSGROUP."
10589   (cond  ((or (null newsgroup)
10590               (string-equal newsgroup ""))
10591           ;; The global score file is placed at top of the directory.
10592           (expand-file-name gnus-score-file-suffix
10593                             (or gnus-kill-files-directory "~/News")))
10594          ((gnus-use-long-file-name 'not-score)
10595           ;; Append ".SCORE" to newsgroup name.
10596           (expand-file-name (concat newsgroup "." gnus-score-file-suffix)
10597                             (or gnus-kill-files-directory "~/News")))
10598          (t
10599           ;; Place "SCORE" under the hierarchical directory.
10600           (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
10601                                     "/" gnus-score-file-suffix)
10602                             (or gnus-kill-files-directory "~/News")))))
10603
10604 (defun gnus-score-score-files (group)
10605   "Return a list of all possible score files."
10606   (and gnus-global-score-files 
10607        (or gnus-internal-global-score-files
10608            (gnus-score-search-global-directories gnus-global-score-files)))
10609   (setq gnus-kill-files-directory 
10610         (file-name-as-directory
10611          (or gnus-kill-files-directory "~/News/")))
10612   (if (not (file-readable-p gnus-kill-files-directory))
10613       (setq gnus-score-file-list nil)
10614     (if (gnus-use-long-file-name 'not-score)
10615         (if (or (not gnus-score-file-list)
10616                 (gnus-file-newer-than gnus-kill-files-directory
10617                                       (car gnus-score-file-list)))
10618               (setq gnus-score-file-list 
10619                     (cons (nth 5 (file-attributes gnus-kill-files-directory))
10620                           (nreverse 
10621                            (directory-files 
10622                             gnus-kill-files-directory t
10623                             (concat gnus-score-file-suffix "$"))))))
10624       (let ((dir (expand-file-name
10625                   (concat gnus-kill-files-directory
10626                           (gnus-replace-chars-in-string group ?. ?/))))
10627             (mdir (length (expand-file-name gnus-kill-files-directory)))
10628             files)
10629         (if (file-exists-p (concat dir "/" gnus-score-file-suffix))
10630             (setq files (list (concat dir "/" gnus-score-file-suffix))))
10631         (while (>= (1+ (length dir)) mdir)
10632           (and (file-exists-p (concat dir "/all/" gnus-score-file-suffix))
10633                (setq files (cons (concat dir "/all/" gnus-score-file-suffix)
10634                                  files)))
10635           (string-match "/[^/]*$" dir)
10636           (setq dir (substring dir 0 (match-beginning 0))))
10637         (setq gnus-score-file-list 
10638               (cons nil (nreverse files)))))
10639     (cdr gnus-score-file-list)))
10640         
10641 (defun gnus-score-find-single (group)
10642   "Return list containing the score file for GROUP."
10643   (list (gnus-score-file-name group)))
10644
10645 (defun gnus-score-find-hierarchical (group)
10646   "Return list of score files for GROUP.
10647 This includes the score file for the group and all its parents."
10648   (let ((all (copy-sequence '(nil)))
10649         (start 0))
10650     (while (string-match "\\." group (1+ start))
10651       (setq start (match-beginning 0))
10652       (setq all (cons (substring group 0 start) all)))
10653     (setq all (cons group all))
10654     (mapcar 'gnus-score-file-name (nreverse all))))
10655
10656 (defun gnus-score-find-bnews (group)
10657   "Return a list of score files for GROUP.
10658 The score files are those files in the ~/News directory which matches
10659 GROUP using BNews sys file syntax."
10660   (let* ((sfiles (append (gnus-score-score-files group)
10661                          gnus-internal-global-score-files))
10662          (kill-dir (file-name-as-directory 
10663                     (expand-file-name gnus-kill-files-directory)))
10664          (klen (length kill-dir))
10665          ofiles not-match regexp)
10666     (save-excursion
10667       (set-buffer (get-buffer-create "*gnus score files*"))
10668       (buffer-disable-undo (current-buffer))
10669       ;; Go through all score file names and create regexp with them
10670       ;; as the source.  
10671       (while sfiles
10672         (erase-buffer)
10673         (insert (car sfiles))
10674         (goto-char 1)
10675         ;; First remove the suffix itself.
10676         (re-search-forward (concat "." gnus-score-file-suffix "$"))
10677         (replace-match "") 
10678         (goto-char 1)
10679         (if (looking-at (regexp-quote kill-dir))
10680             ;; If the file name was just "SCORE", `klen' is one character
10681             ;; too much.
10682             (delete-char (min (1- (point-max)) klen))
10683           (goto-char (point-max))
10684           (search-backward "/")
10685           (delete-region (1+ (point)) (point-min)))
10686         ;; Translate "all" to ".*".
10687         (while (search-forward "all" nil t)
10688           (replace-match ".*"))
10689         (goto-char 1)
10690         ;; Deal with "not."s.
10691         (if (looking-at "not.")
10692             (progn
10693               (setq not-match t)
10694               (setq regexp (buffer-substring 5 (point-max))))
10695           (setq regexp (buffer-substring 1 (point-max)))
10696           (setq not-match nil))
10697         ;; Finally - if this resulting regexp matches the group name,
10698         ;; we add this score file to the list of score files
10699         ;; applicable to this group.
10700         (if (or (and not-match
10701                      (not (string-match regexp group)))
10702                 (and (not not-match)
10703                      (string-match regexp group)))
10704             (setq ofiles (cons (car sfiles) ofiles)))
10705         (setq sfiles (cdr sfiles)))
10706       (kill-buffer (current-buffer))
10707       ;; Slight kludge here - the last score file returned should be
10708       ;; the local score file, whether it exists or not. This is so
10709       ;; that any score commands the user enters will go to the right
10710       ;; file, and not end up in some global score file.
10711       (let ((localscore
10712              (expand-file-name
10713               (if (gnus-use-long-file-name 'not-score)
10714                   (concat gnus-kill-files-directory group "." 
10715                           gnus-score-file-suffix)
10716                 (concat gnus-kill-files-directory
10717                         (gnus-replace-chars-in-string group ?. ?/)
10718                         "/" gnus-score-file-suffix)))))
10719         (and (member localscore ofiles)
10720              (delete localscore ofiles))
10721         (setq ofiles (cons localscore ofiles)))
10722       (nreverse ofiles))))
10723
10724 (defun gnus-score-search-global-directories (files)
10725   "Scan all global score directories for score files."
10726   ;; Set the variable `gnus-internal-global-score-files' to all
10727   ;; available global score files.
10728   (interactive (list gnus-global-score-files))
10729   (let (out)
10730     (while files
10731       (if (string-match "/$" (car files))
10732           (setq out (nconc (directory-files 
10733                             (car files) t
10734                             (concat gnus-score-file-suffix "$"))))
10735         (setq out (cons (car files) out)))
10736       (setq files (cdr files)))
10737     (setq gnus-internal-global-score-files out)))
10738
10739 ;;;
10740 ;;; Score mode.
10741 ;;;
10742
10743 (defvar gnus-score-mode-map nil)
10744 (defvar gnus-score-mode-hook nil)
10745
10746 (if gnus-score-mode-map
10747     ()
10748   (setq gnus-score-mode-map (copy-keymap emacs-lisp-mode-map))
10749   (define-key gnus-score-mode-map "\C-c\C-c" 'gnus-score-edit-done)
10750   (define-key gnus-score-mode-map "\C-c\C-d" 'gnus-score-edit-insert-date))
10751
10752 (defun gnus-score-mode ()
10753   "Mode for editing score files.
10754 This mode is an extended emacs-lisp mode.
10755
10756 \\{gnus-score-mode-map}"
10757   (interactive)
10758   (kill-all-local-variables)
10759   (use-local-map gnus-score-mode-map)
10760   (set-syntax-table emacs-lisp-mode-syntax-table)
10761   (setq major-mode 'gnus-score-mode)
10762   (setq mode-name "Score")
10763   (lisp-mode-variables nil)
10764   (run-hooks 'emacs-lisp-mode-hook 'gnus-score-mode-hook))
10765
10766 (defun gnus-score-edit-insert-date ()
10767   "Insert date in numerical format."
10768   (interactive)
10769   (insert (int-to-string (gnus-day-number (current-time-string)))))
10770
10771 (defun gnus-score-edit-done ()
10772   "Save the score file and return to the summary buffer."
10773   (interactive)
10774   (let ((bufnam (buffer-file-name (current-buffer))))
10775     (save-buffer)
10776     (kill-buffer (current-buffer))
10777     (and gnus-winconf-edit-score
10778          (set-window-configuration gnus-winconf-edit-score))
10779     (gnus-score-remove-from-cache bufnam)
10780     (gnus-score-load-file bufnam)))
10781
10782 \f
10783 ;;;
10784 ;;; Gnus Posting Functions
10785 ;;;
10786
10787 (defvar gnus-organization-file "/usr/lib/news/organization"
10788   "*Local news organization file.")
10789
10790 (defvar gnus-post-news-buffer "*post-news*")
10791 (defvar gnus-winconf-post-news nil)
10792
10793 ;;; Post news commands of Gnus group mode and summary mode
10794
10795 (defun gnus-group-post-news ()
10796   "Post an article."
10797   (interactive)
10798   (gnus-set-global-variables)
10799   ;; Save window configuration.
10800   (setq gnus-winconf-post-news (current-window-configuration))
10801   (let ((gnus-newsgroup-name nil))
10802     (unwind-protect
10803         (gnus-post-news 'post)
10804       (or (and (eq (current-buffer) (get-buffer gnus-post-news-buffer))
10805                (not (zerop (buffer-size))))
10806           ;; Restore last window configuration.
10807           (and gnus-winconf-post-news
10808                (set-window-configuration gnus-winconf-post-news)))))
10809   ;; We don't want to return to summary buffer nor article buffer later.
10810   (setq gnus-winconf-post-news nil)
10811   (if (get-buffer gnus-summary-buffer)
10812       (bury-buffer gnus-summary-buffer))
10813   (if (get-buffer gnus-article-buffer)
10814       (bury-buffer gnus-article-buffer)))
10815
10816 (defun gnus-summary-post-news ()
10817   "Post an article."
10818   (interactive)
10819   (gnus-set-global-variables)
10820   ;; Save window configuration.
10821   (setq gnus-winconf-post-news (current-window-configuration))
10822   (unwind-protect
10823       (gnus-post-news 'post gnus-newsgroup-name)
10824     (or (and (eq (current-buffer) (get-buffer gnus-post-news-buffer))
10825              (not (zerop (buffer-size))))
10826         ;; Restore last window configuration.
10827         (and gnus-winconf-post-news
10828              (set-window-configuration gnus-winconf-post-news))))
10829   ;; We don't want to return to article buffer later.
10830   (if (get-buffer gnus-article-buffer)
10831       (bury-buffer gnus-article-buffer)))
10832
10833 (defun gnus-summary-followup (yank)
10834   "Compose a followup to an article.
10835 If prefix argument YANK is non-nil, original article is yanked automatically."
10836   (interactive "P")
10837   (gnus-set-global-variables)
10838   (save-window-excursion
10839     (gnus-summary-select-article t))
10840   (let ((headers gnus-current-headers)
10841         (gnus-newsgroup-name gnus-newsgroup-name))
10842     ;; Check Followup-To: poster.
10843     (set-buffer gnus-article-buffer)
10844     (if (and gnus-use-followup-to
10845              (string-equal "poster" (gnus-fetch-field "followup-to"))
10846              (or (not (eq gnus-use-followup-to t))
10847                  (not (gnus-y-or-n-p 
10848                        "Do you want to ignore `Followup-To: poster'? "))))
10849         ;; Mail to the poster.  Gnus is now RFC1036 compliant.
10850         (gnus-summary-reply yank)
10851       ;; Save window configuration.
10852       (setq gnus-winconf-post-news (current-window-configuration))
10853       (unwind-protect
10854           (gnus-post-news nil gnus-newsgroup-name
10855                           headers gnus-article-buffer yank)
10856         (or (and (eq (current-buffer) (get-buffer gnus-post-news-buffer))
10857                  (not (zerop (buffer-size))))
10858             ;; Restore last window configuration.
10859             (and gnus-winconf-post-news
10860                  (set-window-configuration gnus-winconf-post-news))))
10861       ;; We don't want to return to article buffer later.
10862       (bury-buffer gnus-article-buffer)))
10863   (gnus-article-hide-headers-if-wanted))
10864
10865 (defun gnus-summary-followup-with-original ()
10866   "Compose a followup to an article and include the original article."
10867   (interactive)
10868   (gnus-summary-followup t))
10869
10870 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10871 (defun gnus-summary-followup-and-reply (yank)
10872   "Compose a followup and do an auto mail to author."
10873   (interactive "P")
10874   (let ((gnus-auto-mail-to-author t))
10875     (gnus-summary-followup yank)))
10876
10877 (defun gnus-summary-followup-and-reply-with-original ()
10878   "Compose a followup, include the original, and do an auto mail to author."
10879   (interactive)
10880   (gnus-summary-followup-and-reply t))
10881
10882 (defun gnus-summary-cancel-article ()
10883   "Cancel an article you posted."
10884   (interactive)
10885   (gnus-set-global-variables)
10886   (gnus-summary-select-article t)
10887   (gnus-eval-in-buffer-window gnus-article-buffer (gnus-cancel-news))
10888   (gnus-article-hide-headers-if-wanted))
10889
10890 (defun gnus-summary-supersede-article ()
10891   "Compose an article that will supersede a previous article.
10892 This is done simply by taking the old article and adding a Supersedes
10893 header line with the old Message-ID."
10894   (interactive)
10895   (gnus-set-global-variables)
10896   (if (not
10897        (string-equal
10898         (downcase (mail-strip-quoted-names 
10899                    (header-from gnus-current-headers)))
10900         (downcase (mail-strip-quoted-names (gnus-inews-user-name)))))
10901       (error "This article is not yours."))
10902   (gnus-summary-select-article t)
10903   (save-excursion
10904     (set-buffer gnus-article-buffer)
10905     (let ((buffer-read-only nil))
10906       (goto-char (point-min))
10907       (search-forward "\n\n" nil t)
10908       (if (not (re-search-backward "^Message-ID: " nil t))
10909           (error "No Message-ID in this article"))))
10910   (if (gnus-post-news 'post gnus-newsgroup-name)
10911       (progn
10912         (erase-buffer)
10913         (insert-buffer gnus-article-buffer)
10914         (goto-char (point-min))
10915         (search-forward "\n\n" nil t)
10916         (if (not (re-search-backward "^Message-ID: " nil t))
10917             (error "No Message-ID in this article")
10918           (replace-match "Supersedes: "))
10919         (search-forward "\n\n")
10920         (forward-line -1)
10921         (insert mail-header-separator))))
10922
10923 \f
10924 ;;;###autoload
10925 (fset 'sendnews 'gnus-post-news)
10926
10927 ;;;###autoload
10928 (fset 'postnews 'gnus-post-news)
10929
10930 (defun gnus-post-news (post &optional group header article-buffer yank)
10931   "Begin editing a new USENET news article to be posted.
10932 Type \\[describe-mode] in the buffer to get a list of commands."
10933   (interactive (list t))
10934   (if (or (not gnus-novice-user)
10935           gnus-expert-user
10936           (not (eq 'post 
10937                    (nth 1 (assoc 
10938                            (format "%s" (car (gnus-find-method-for-group 
10939                                               gnus-newsgroup-name)))
10940                            gnus-valid-select-methods))))
10941           (assq 'to-address (gnus-find-method-for-group gnus-newsgroup-name))
10942           (gnus-y-or-n-p "Are you sure you want to post to all of USENET? "))
10943       (let ((sumart (if (not post)
10944                         (save-excursion
10945                           (set-buffer gnus-summary-buffer)
10946                           (cons (current-buffer) gnus-current-article))))
10947             (from (and header (header-from header)))
10948             subject follow-to real-group)
10949         (and gnus-interactive-post
10950              (not gnus-expert-user)
10951              post (not group)
10952              (progn
10953                (setq group 
10954                      (completing-read "Group: " gnus-active-hashtb nil t))
10955                (setq subject (read-string "Subject: "))))
10956         (setq mail-reply-buffer article-buffer)
10957
10958         (let ((gnus-newsgroup-name (or group gnus-newsgroup-name "")))
10959           (setq real-group (and group (gnus-group-real-name group)))
10960           (setq gnus-post-news-buffer 
10961                 (gnus-request-post-buffer 
10962                  post real-group subject header article-buffer
10963                  (nth 2 (and group (gnus-gethash group gnus-newsrc-hashtb)))
10964                  (if (and (boundp 'gnus-followup-to-function)
10965                           gnus-followup-to-function)
10966                      (setq follow-to
10967                            (save-excursion
10968                              (set-buffer article-buffer)
10969                              (funcall gnus-followup-to-function group))))
10970                  (eq gnus-use-followup-to t)))
10971           (if post
10972               (progn
10973                 (gnus-configure-windows '(1 0 0))
10974                 (switch-to-buffer gnus-post-news-buffer))
10975             (gnus-configure-windows '(0 1 0))
10976             (if (not yank)
10977                 (progn
10978                   (switch-to-buffer article-buffer)
10979                   (pop-to-buffer gnus-post-news-buffer))
10980               (switch-to-buffer gnus-post-news-buffer)))
10981           (gnus-overload-functions)
10982           (make-local-variable 'gnus-article-reply)
10983           (make-local-variable 'gnus-article-check-size)
10984           (setq gnus-article-reply sumart)
10985           ;; Handle `gnus-auto-mail-to-author'.
10986           ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10987           (let ((to (if (eq gnus-auto-mail-to-author 'ask)
10988                         (and (y-or-n-p "Also send mail to author? ") from)
10989                       (and gnus-auto-mail-to-author from))))
10990             (if to
10991                 (progn
10992                   (if (mail-fetch-field "To")
10993                       (progn
10994                         (beginning-of-line)
10995                         (insert "Cc: " to "\n"))
10996                     (mail-position-on-field "To")
10997                     (insert to)))))
10998           ;; Handle author copy using BCC field.
10999           (if (and gnus-mail-self-blind
11000                    (not (mail-fetch-field "bcc")))
11001               (progn
11002                 (mail-position-on-field "Bcc")
11003                 (insert (if (stringp gnus-mail-self-blind)
11004                             gnus-mail-self-blind
11005                           (user-login-name)))))
11006           ;; Handle author copy using FCC field.
11007           (if gnus-author-copy
11008               (progn
11009                 (mail-position-on-field "Fcc")
11010                 (insert gnus-author-copy)))
11011           (goto-char (point-min))
11012           (if post 
11013               (cond ((not group)
11014                      (re-search-forward "^Newsgroup:" nil t)
11015                      (end-of-line))
11016                     ((not subject)
11017                      (re-search-forward "^Subject:" nil t)
11018                      (end-of-line))
11019                     (t
11020                      (re-search-forward 
11021                       (concat "^" (regexp-quote mail-header-separator) "$"))
11022                      (forward-line 1)))
11023             (re-search-forward 
11024              (concat "^" (regexp-quote mail-header-separator) "$"))
11025             (forward-line 1)
11026             (if yank 
11027                 (save-excursion
11028                   (run-hooks 'news-reply-header-hook)
11029                   (mail-yank-original nil)))
11030             (if gnus-post-prepare-function
11031                 (funcall gnus-post-prepare-function group))))))
11032   (setq gnus-article-check-size (cons (buffer-size) (gnus-article-checksum)))
11033   (message "")
11034   t)
11035
11036 (defun gnus-inews-news (&optional use-group-method)
11037   "Send a news message.
11038 If given a prefix, and the group is a foreign group, this function
11039 will attempt to use the foreign server to post the article."
11040   (interactive "P")
11041   ;; Check whether the article is a good Net Citizen.
11042   (if (and gnus-article-check-size (not (gnus-inews-check-post)))
11043       ;; Aber nein!
11044       ()
11045     ;; Looks ok, so we do the nasty.
11046     (let* ((case-fold-search nil)
11047            (server-running (gnus-server-opened gnus-select-method))
11048            (reply gnus-article-reply))
11049       (save-excursion
11050         ;; Connect to default NNTP server if necessary.
11051         ;; Suggested by yuki@flab.fujitsu.junet.
11052         (gnus-start-news-server)        ;Use default server.
11053         ;; NNTP server must be opened before current buffer is modified.
11054         (widen)
11055         (goto-char (point-min))
11056         (run-hooks 'news-inews-hook)
11057         (save-restriction
11058           (narrow-to-region
11059            (point-min)
11060            (progn
11061              (goto-char (point-min))
11062              (re-search-forward 
11063               (concat "^" (regexp-quote mail-header-separator) "$"))))
11064
11065           ;; Correct newsgroups field: change sequence of spaces to comma and 
11066           ;; eliminate spaces around commas.  Eliminate imbedded line breaks.
11067           (goto-char (point-min))
11068           (if (search-forward-regexp "^Newsgroups: +" nil t)
11069               (save-restriction
11070                 (narrow-to-region
11071                  (point)
11072                  (if (re-search-forward "^[^ \t]" nil 'end)
11073                      (match-beginning 0)
11074                    (point-max)))
11075                 (goto-char (point-min))
11076                 (replace-regexp "\n[ \t]+" " ") ;No line breaks (too confusing)
11077                 (goto-char (point-min))
11078                 (replace-regexp "[ \t\n]*,[ \t\n]*\\|[ \t]+" ",")))
11079
11080           ;; Added by Per Abrahamsen <abraham@iesd.auc.dk>.
11081           ;; Help save the the world!
11082           (or 
11083            gnus-expert-user
11084            (let ((newsgroups (mail-fetch-field "newsgroups"))
11085                  (followup-to (mail-fetch-field "followup-to"))
11086                  groups to)
11087              (if (and (string-match "," newsgroups) (not followup-to))
11088                  (progn
11089                    (while (string-match "," newsgroups)
11090                      (setq groups
11091                            (cons (list (substring newsgroups
11092                                                   0 (match-beginning 0)))
11093                                  groups))
11094                      (setq newsgroups (substring newsgroups (match-end 0))))
11095                    (setq groups (nreverse (cons (list newsgroups) groups)))
11096
11097                    (setq to
11098                          (completing-read "Followups to: (default all groups) "
11099                                           groups))
11100                    (if (> (length to) 0)
11101                        (progn
11102                          (goto-char (point-min))
11103                          (insert "Followup-To: " to "\n")))))))
11104
11105           ;; Cleanup Followup-To.
11106           (goto-char (point-min))
11107           (if (search-forward-regexp "^Followup-To: +" nil t)
11108               (save-restriction
11109                 (narrow-to-region
11110                  (point)
11111                  (if (re-search-forward "^[^ \t]" nil 'end)
11112                      (match-beginning 0)
11113                    (point-max)))
11114                 (goto-char (point-min))
11115                 (replace-regexp "\n[ \t]+" " ") ;No line breaks (too confusing)
11116                 (goto-char (point-min))
11117                 (replace-regexp "[ \t\n]*,[ \t\n]*\\|[ \t]+" ",")))
11118
11119           ;; Mail the message too if To:, Bcc:. or Cc: exists.
11120           (if (or (mail-fetch-field "to" nil t)
11121                   (mail-fetch-field "bcc" nil t)
11122                   (mail-fetch-field "cc" nil t))
11123               (if gnus-mail-send-method
11124                   (save-excursion
11125                     (save-restriction
11126                       (widen)
11127                       (message "Sending via mail...")
11128                       
11129                       (if gnus-mail-courtesy-message
11130                           (progn
11131                             ;; Insert "courtesy" mail message.
11132                             (goto-char 1)
11133                             (re-search-forward
11134                              (concat "^" (regexp-quote
11135                                           mail-header-separator) "$"))
11136                             (forward-line 1)
11137                             (insert gnus-mail-courtesy-message)
11138                             (funcall gnus-mail-send-method)
11139                             (goto-char 1)
11140                             (search-forward gnus-mail-courtesy-message)
11141                             (replace-match ""))
11142                         (funcall gnus-mail-send-method))
11143
11144                       (message "Sending via mail... done")
11145                       
11146                       (goto-char 1)
11147                       (narrow-to-region
11148                        1 (re-search-forward 
11149                           (concat "^" (regexp-quote 
11150                                        mail-header-separator) "$")))
11151                       (goto-char 1)
11152                       (delete-matching-lines "BCC:.*")))
11153                 (ding)
11154                 (message "No mailer defined.  To: and/or Cc: fields ignored.")
11155                 (sit-for 1))))
11156
11157         ;; Send to NNTP server. 
11158         (message "Posting to USENET...")
11159         (if (gnus-inews-article use-group-method)
11160             (progn
11161               (message "Posting to USENET... done")
11162               (if (and reply
11163                        (get-buffer (car reply))
11164                        (buffer-name (car reply)))
11165                   (progn
11166                     (save-excursion
11167                       (set-buffer gnus-summary-buffer)
11168                       (gnus-summary-mark-article-as-replied 
11169                        (cdr reply))))))
11170           ;; We cannot signal an error.
11171           (ding) (message "Article rejected: %s" 
11172                           (gnus-status-message gnus-select-method)))
11173         (set-buffer-modified-p nil))
11174       ;; If NNTP server is opened by gnus-inews-news, close it by myself.
11175       (or server-running
11176           (gnus-close-server (gnus-find-method-for-group gnus-newsgroup-name)))
11177       (and (fboundp 'bury-buffer) (bury-buffer))
11178       ;; Restore last window configuration.
11179       (and gnus-winconf-post-news
11180            (set-window-configuration gnus-winconf-post-news))
11181       (setq gnus-winconf-post-news nil))))
11182
11183 (defun gnus-inews-check-post ()
11184   "Check whether the post looks ok."
11185   (and 
11186    ;; Check excessive size.
11187    (if (> (buffer-size) 60000)
11188        (gnus-y-or-n-p (format "The article is %d octets long. Really post? "
11189                               (buffer-size)))
11190      t)
11191    ;; Check for commands in Subject.
11192    (save-excursion
11193      (save-restriction
11194        (goto-char (point-min))
11195        (narrow-to-region 
11196         (point) 
11197         (re-search-forward 
11198          (concat "^" (regexp-quote mail-header-separator) "$")))
11199        (if (string-match "^cmsg " (mail-fetch-field "subject"))
11200            (gnus-y-or-n-p
11201             "The control code \"cmsg \" is in the subject. Really post? ")
11202          t)))
11203    ;; Check for control characters.
11204    (save-excursion
11205      (if (re-search-forward "[\000-\007\013\015-\037\200-\237]" nil t)
11206          (gnus-y-or-n-p 
11207           "The article contains control characters. Really post? ")
11208        t))
11209    ;; Check for multiple identical headers.
11210    (let (found)
11211      (save-excursion
11212        (save-restriction
11213          (goto-char (point-min))
11214          (narrow-to-region
11215           (point) 
11216           (re-search-forward 
11217            (concat "^" (regexp-quote mail-header-separator) "$")))
11218          (goto-char (point-min))
11219          (while (and (not found) (re-search-forward "^[^ \t:]+: " nil t))
11220            (save-excursion
11221              (or (re-search-forward 
11222                   (concat "^" (setq found
11223                                     (buffer-substring (match-beginning 0) 
11224                                                       (match-end 0))))
11225                   nil t)
11226                  (setq found nil))))
11227          (if found
11228              (gnus-y-or-n-p 
11229               (format "Multiple %s headers. Really post? " found))
11230            t))))
11231    ;; Check for version and sendsys.
11232    (save-excursion
11233      (save-restriction
11234        (goto-char (point-min))
11235        (narrow-to-region
11236         (point) 
11237         (re-search-forward 
11238          (concat "^" (regexp-quote mail-header-separator) "$")))
11239        (if (re-search-backward "^Sendsys:\\|^Version:" nil t)
11240            (gnus-yes-or-no-p
11241             (format "The article contains a %s command. Really post? "
11242                     (buffer-substring (match-beginning 0) (match-end 0))))
11243          t)))
11244    ;; Check the From header.
11245    (save-excursion
11246      (save-restriction
11247        (goto-char (point-min))
11248        (narrow-to-region
11249         (point)
11250         (re-search-forward
11251          (concat "^" (regexp-quote mail-header-separator) "$")))
11252        (let* ((case-fold-search t)
11253               (from (mail-fetch-field "from")))
11254          (if (and from
11255                   (string-match "@" from)
11256                   (not (string-match "@[^\\.]*\\." from)))
11257              (gnus-yes-or-no-p
11258               (format "The domain looks strange: \"%s\". Really post? "
11259                       from))
11260            t))))
11261    ;; Check for long lines.
11262    (save-excursion
11263      (save-restriction
11264        (goto-char (point-min))
11265        (narrow-to-region
11266         (point)
11267         (re-search-forward
11268          (concat "^" (regexp-quote mail-header-separator) "$")))
11269        (while 
11270            (and
11271             (progn
11272               (end-of-line)
11273               (< (current-column) 80))
11274             (zerop (forward-line 1))))
11275        (or (eobp)
11276            (gnus-yes-or-no-p
11277             (format
11278              "You have lines longer than 79 characters.  Really post? ")))))
11279    ;; Use the (size . checksum) variable to see whether the
11280    ;; article is empty or has only quoted text.
11281    (if (and (= (buffer-size) (car gnus-article-check-size))
11282             (= (gnus-article-checksum) (cdr gnus-article-check-size)))
11283        (gnus-yes-or-no-p
11284         "It looks like there's no new text in your article. Really post? ")
11285      t)))
11286
11287 (defun gnus-article-checksum ()
11288   (let ((sum 0))
11289     (save-excursion
11290       (while (not (eobp))
11291         (setq sum (logxor sum (following-char)))
11292         (forward-char 1)))
11293     sum))
11294
11295 (defun gnus-cancel-news ()
11296   "Cancel an article you posted."
11297   (interactive)
11298   (if (gnus-yes-or-no-p "Do you really want to cancel this article? ")
11299       (let ((from nil)
11300             (newsgroups nil)
11301             (message-id nil)
11302             (distribution nil))
11303         (save-excursion
11304           ;; Get header info. from original article.
11305           (save-restriction
11306             (gnus-article-show-all-headers)
11307             (goto-char (point-min))
11308             (search-forward "\n\n" nil 'move)
11309             (narrow-to-region (point-min) (point))
11310             (setq from (mail-fetch-field "from"))
11311             (setq newsgroups (mail-fetch-field "newsgroups"))
11312             (setq message-id (mail-fetch-field "message-id"))
11313             (setq distribution (mail-fetch-field "distribution")))
11314           ;; Verify if the article is absolutely user's by comparing
11315           ;; user id with value of its From: field.
11316           (if (not
11317                (string-equal
11318                 (downcase (mail-strip-quoted-names from))
11319                 (downcase (mail-strip-quoted-names (gnus-inews-user-name)))))
11320               (progn
11321                 (ding) (message "This article is not yours."))
11322             ;; Make control article.
11323             (set-buffer (get-buffer-create " *Gnus-canceling*"))
11324             (buffer-disable-undo (current-buffer))
11325             (erase-buffer)
11326             (insert "Newsgroups: " newsgroups "\n"
11327                     "Subject: cancel " message-id "\n"
11328                     "Control: cancel " message-id "\n"
11329                     mail-header-separator "\n"
11330                     "This is a cancel message from " from ".\n")
11331             ;; Send the control article to NNTP server.
11332             (message "Canceling your article...")
11333             (if (gnus-inews-article)
11334                 (message "Canceling your article... done")
11335               (ding) (message "Failed to cancel your article"))
11336             ;; Kill the article buffer.
11337             (kill-buffer (current-buffer)))))))
11338
11339 \f
11340 ;;; Lowlevel inews interface
11341
11342 (defun gnus-inews-article (&optional use-group-method)
11343   "Post an article in current buffer using NNTP protocol."
11344   (let ((artbuf (current-buffer))
11345         (tmpbuf (get-buffer-create " *Gnus-posting*")))
11346     (widen)
11347     (goto-char (point-max))
11348     ;; require a newline at the end for inews to append .signature to
11349     (or (= (preceding-char) ?\n)
11350         (insert ?\n))
11351     ;; Prepare article headers.  All message body such as signature
11352     ;; must be inserted before Lines: field is prepared.
11353     (save-restriction
11354       (goto-char (point-min))
11355       (narrow-to-region 
11356        (point-min) 
11357        (save-excursion
11358          (re-search-forward 
11359           (concat "^" (regexp-quote mail-header-separator) "$"))
11360          (match-beginning 0)))
11361       (gnus-inews-insert-headers)
11362       (run-hooks gnus-inews-article-header-hook)
11363       (widen))
11364     (save-excursion
11365       (set-buffer tmpbuf)
11366       (buffer-disable-undo (current-buffer))
11367       (erase-buffer)
11368       (insert-buffer-substring artbuf)
11369       ;; Remove the header separator.
11370       (goto-char (point-min))
11371       (re-search-forward
11372        (concat "^" (regexp-quote mail-header-separator) "$"))
11373       (replace-match "\n\n")
11374       ;; This hook may insert a signature.
11375       (run-hooks 'gnus-prepare-article-hook)
11376       ;; Run final inews hooks.  This hook may do FCC.
11377       ;; The article must be saved before being posted because
11378       ;; `gnus-request-post' modifies the buffer.
11379       (run-hooks 'gnus-inews-article-hook)
11380       ;; Post an article to NNTP server.
11381       ;; Return NIL if post failed.
11382       (prog1
11383           (gnus-request-post 
11384            (if use-group-method
11385                (gnus-find-method-for-group gnus-newsgroup-name)
11386              gnus-select-method) use-group-method)
11387         (kill-buffer (current-buffer))))))
11388
11389 (defun gnus-inews-insert-headers ()
11390   "Prepare article headers.
11391 Headers already prepared in the buffer are not modified.
11392 Headers in `gnus-required-headers' will be generated."
11393   (let ((Date (gnus-inews-date))
11394         (Message-ID (gnus-inews-message-id))
11395         (Organization (gnus-inews-organization))
11396         (From (gnus-inews-user-name))
11397         (Path (gnus-inews-path))
11398         (Subject nil)
11399         (Newsgroups nil)
11400         (Distribution nil)
11401         (Lines (gnus-inews-lines))
11402         (X-Newsreader gnus-version)
11403         (headers gnus-required-headers)
11404         (case-fold-search t)
11405         header value)
11406     ;; First we remove any old Message-IDs. This might be slightly
11407     ;; fascist, but if the user really wants to generate Message-IDs
11408     ;; by herself, she should remove it from the `gnus-required-list'. 
11409     (goto-char (point-min))
11410     (and (memq 'Message-ID headers)
11411          (re-search-forward "^Message-ID:" nil t)
11412          (delete-region (progn (beginning-of-line) (point))
11413                         (progn (forward-line 1) (point))))
11414     ;; Remove NNTP-posting-host.
11415     (goto-char (point-min))
11416     (and (re-search-forward "nntp-posting-host^:" nil t)
11417          (delete-region (progn (beginning-of-line) (point))
11418                         (progn (forward-line 1) (point))))
11419     ;; Insert new Sender if the From is strange. 
11420     (let ((from (mail-fetch-field "from")))
11421       (if (and from (not (string= (downcase from) (downcase From))))
11422           (progn
11423             (goto-char (point-min))    
11424             (and (re-search-forward "^Sender:" nil t)
11425                  (delete-region (progn (beginning-of-line) (point))
11426                                 (progn (forward-line 1) (point))))
11427             (insert "Sender: " From "\n"))))
11428     ;; If there are References, and no "Re: ", then the thread has
11429     ;; changed name. See Son-of-1036.
11430     (if (and (mail-fetch-field "references")
11431              (get-buffer gnus-article-buffer))
11432         (let ((psubject (gnus-simplify-subject-re
11433                          (mail-fetch-field "subject")))
11434               subject)
11435           (save-excursion
11436             (set-buffer (get-buffer gnus-article-buffer))
11437             (save-restriction
11438               (gnus-narrow-to-headers)
11439               (if (setq subject (mail-fetch-field "subject"))
11440                   (progn
11441                     (and gnus-summary-gather-subject-limit
11442                          (numberp gnus-summary-gather-subject-limit)
11443                          (> (length subject) gnus-summary-gather-subject-limit)
11444                          (setq subject
11445                                (substring subject 0
11446                                           gnus-summary-gather-subject-limit)))
11447                     (setq subject (gnus-simplify-subject-re subject))))))
11448           (or (and psubject subject (string= subject psubject))
11449               (progn
11450                 (string-match "@" Message-ID)
11451                 (setq Message-ID
11452                       (concat (substring Message-ID 0 (match-beginning 0))
11453                               "_-_" 
11454                               (substring Message-ID (match-beginning 0))))))))
11455     ;; Go through all the required headers and see if they are in the
11456     ;; articles already. If they are not, or are empty, they are
11457     ;; inserted automatically - except for Subject, Newsgroups and
11458     ;; Distribution. 
11459     (while headers
11460       (goto-char (point-min))
11461       (setq header (car headers))
11462       (if (or (not (re-search-forward 
11463                     (concat "^" (downcase (symbol-name header)) ":") nil t))
11464               (progn
11465                 (if (= (following-char) ? ) (forward-char 1) (insert " "))
11466                 (looking-at "[ \t]*$")))
11467           (progn
11468             (setq value (or (and (boundp header) (symbol-value header))
11469                             (read-from-minibuffer
11470                              (format "Empty header for %s; enter value: " 
11471                                      header))))
11472             (if (bolp)
11473                 (save-excursion
11474                   (goto-char (point-max))
11475                   (insert (symbol-name header) ": " value "\n"))
11476               (replace-match value))))
11477       (setq headers (cdr headers)))))
11478
11479 (defun gnus-inews-insert-signature ()
11480   "Insert a signature file.
11481 If `gnus-signature-function' is bound and returns a string, this
11482 string is used instead of the variable `gnus-signature-file'.
11483 In either case, if the string is a file name, this file is
11484 inserted. If the string is not a file name, the string itself is
11485 inserted. 
11486 If you never want any signature inserted, set both those variables to
11487 nil."
11488   (save-excursion
11489     (let ((signature 
11490            (or (and gnus-signature-function
11491                     (fboundp gnus-signature-function)
11492                     (funcall gnus-signature-function gnus-newsgroup-name))
11493                gnus-signature-file))
11494           b)
11495       (if (and signature
11496                (or (file-exists-p signature)
11497                    (string-match " " signature)
11498                    (not (string-match 
11499                          "^/[^/]+/" (expand-file-name signature)))))
11500           (progn
11501             (goto-char (point-max))
11502             ;; Delete any previous signatures.
11503             (if (and mail-signature (search-backward "\n-- \n" nil t))
11504                 (delete-region (1+ (point)) (point-max)))
11505             (insert "\n-- \n")
11506             (and (< 4 (setq b (count-lines 
11507                                (point)
11508                                (progn
11509                                  (if (file-exists-p signature)
11510                                      (insert-file-contents signature)
11511                                    (insert signature))
11512                                  (goto-char (point-max))
11513                                  (or (bolp) (insert "\n"))
11514                                  (point)))))
11515                  (not gnus-expert-user)
11516                  (not
11517                   (gnus-y-or-n-p
11518                    (format
11519                     "Your .sig is %d lines; it should be max 4. Really post? "
11520                     b)))
11521                  (if (file-exists-p signature)
11522                      (error (format "Edit %s." signature))
11523                    (error "Trim your signature."))))))))
11524
11525 (defun gnus-inews-do-fcc ()
11526   "Process FCC: fields in current article buffer.
11527 Unless the first character of the field is `|', the article is saved
11528 to the specified file using the function specified by the variable
11529 gnus-author-copy-saver.  The default function rmail-output saves in
11530 Unix mailbox format.
11531 If the first character is `|', the contents of the article is send to
11532 a program specified by the rest of the value."
11533   (let ((fcc-list nil)
11534         (fcc-file nil)
11535         (case-fold-search t))           ;Should ignore case.
11536     (save-excursion
11537       (save-restriction
11538         (goto-char (point-min))
11539         (search-forward "\n\n")
11540         (narrow-to-region (point-min) (point))
11541         (goto-char (point-min))
11542         (while (re-search-forward "^FCC:[ \t]*" nil t)
11543           (setq fcc-list
11544                 (cons (buffer-substring
11545                        (point)
11546                        (progn
11547                          (end-of-line)
11548                          (skip-chars-backward " \t")
11549                          (point)))
11550                       fcc-list))
11551           (delete-region (match-beginning 0)
11552                          (progn (forward-line 1) (point))))
11553         ;; Process FCC operations.
11554         (widen)
11555         (while fcc-list
11556           (setq fcc-file (car fcc-list))
11557           (setq fcc-list (cdr fcc-list))
11558           (cond ((string-match "^[ \t]*|[ \t]*\\(.*\\)[ \t]*$" fcc-file)
11559                  (let ((program (substring fcc-file
11560                                            (match-beginning 1) (match-end 1))))
11561                    ;; Suggested by yuki@flab.fujitsu.junet.
11562                    ;; Send article to named program.
11563                    (call-process-region (point-min) (point-max) shell-file-name
11564                                         nil nil nil "-c" program)))
11565                 (t
11566                  ;; Suggested by hyoko@flab.fujitsu.junet.
11567                  ;; Save article in Unix mail format by default.
11568                  (if (and gnus-author-copy-saver
11569                           (not (eq gnus-author-copy-saver 'rmail-output)))
11570                      (funcall gnus-author-copy-saver fcc-file)
11571                    (if (and (file-readable-p fcc-file) (rmail-file-p fcc-file))
11572                        (gnus-output-to-rmail fcc-file)
11573                      (rmail-output fcc-file 1 t t))))))))))
11574
11575 (defun gnus-inews-path ()
11576   "Return uucp path."
11577   (let ((login-name (gnus-inews-login-name)))
11578     (cond ((null gnus-use-generic-path)
11579            (concat (nth 1 gnus-select-method) "!" login-name))
11580           ((stringp gnus-use-generic-path)
11581            ;; Support GENERICPATH.  Suggested by vixie@decwrl.dec.com.
11582            (concat gnus-use-generic-path "!" login-name))
11583           (t login-name))))
11584
11585 (defun gnus-inews-user-name ()
11586   "Return user's network address as \"NAME@DOMAIN (FULL-NAME)\"."
11587   (let ((full-name (gnus-inews-full-name)))
11588     (or gnus-user-from-line
11589         (concat (if (or gnus-user-login-name gnus-use-generic-from
11590                         gnus-local-domain (getenv "DOMAINNAME"))
11591                     (concat (gnus-inews-login-name) "@"
11592                             (gnus-inews-domain-name gnus-use-generic-from))
11593                   user-mail-address)
11594                 ;; User's full name.
11595                 (cond ((string-equal full-name "") "")
11596                       ((string-equal full-name "&") ;Unix hack.
11597                        (concat " (" (user-login-name) ")"))
11598                       (t
11599                        (concat " (" full-name ")")))))))
11600
11601 (defun gnus-inews-login-name ()
11602   "Return login name."
11603   (or gnus-user-login-name (getenv "LOGNAME") (user-login-name)))
11604
11605 (defun gnus-inews-full-name ()
11606   "Return full user name."
11607   (or gnus-user-full-name (getenv "NAME") (user-full-name)))
11608
11609 (defun gnus-inews-domain-name (&optional genericfrom)
11610   "Return user's domain name.
11611 If optional argument GENERICFROM is a string, use it as the domain
11612 name; if it is non-nil, strip off local host name from the domain name.
11613 If the function `system-name' returns full internet name and the
11614 domain is undefined, the domain name is got from it."
11615   (if (or genericfrom gnus-local-domain (getenv "DOMAINNAME"))
11616       (let* ((system-name (system-name))
11617              (domain 
11618               (or (if (stringp genericfrom) genericfrom)
11619                   (getenv "DOMAINNAME")
11620                   gnus-local-domain
11621                   ;; Function `system-name' may return full internet name.
11622                   ;; Suggested by Mike DeCorte <mrd@sun.soe.clarkson.edu>.
11623                   (if (string-match "\\." system-name)
11624                       (substring system-name (match-end 0)))
11625                   (read-string "Domain name (no host): ")))
11626              (host (or (if (string-match "\\." system-name)
11627                            (substring system-name 0 (match-beginning 0)))
11628                        system-name)))
11629         (if (string-equal "." (substring domain 0 1))
11630             (setq domain (substring domain 1)))
11631         ;; Support GENERICFROM as same as standard Bnews system.
11632         ;; Suggested by ohm@kaba.junet and vixie@decwrl.dec.com.
11633         (cond ((null genericfrom)
11634                (concat host "." domain))
11635               ;;((stringp genericfrom) genericfrom)
11636               (t domain)))
11637     (if (string-match "\\." (system-name))
11638         (system-name)
11639       (substring user-mail-address 
11640                  (1+ (string-match "@" user-mail-address))))))
11641
11642 (defun gnus-inews-full-address ()
11643   (let ((domain (gnus-inews-domain-name))
11644         (system (system-name))
11645         (case-fold-search t))
11646     (if (string-match "\\." system) system
11647       (if (string-match (concat "^" (regexp-quote system)) domain) domain
11648         (concat system "." domain)))))
11649
11650 (defun gnus-inews-message-id ()
11651   "Generate unique Message-ID for user."
11652   ;; Message-ID should not contain a slash and should be terminated by
11653   ;; a number.  I don't know the reason why it is so.
11654   (concat "<" (gnus-inews-unique-id) "@" (gnus-inews-full-address) ">"))
11655
11656 (defun gnus-inews-unique-id ()
11657   "Generate unique ID from user name and current time."
11658   (concat (downcase (gnus-inews-login-name))
11659           (mapconcat 
11660            (lambda (num) (gnus-number-base-x num 3 31))
11661            (current-time) "")))
11662
11663 (defun gnus-inews-date ()
11664   "Current time string."
11665   (timezone-make-date-arpa-standard 
11666    (current-time-string) (current-time-zone)))
11667
11668 (defun gnus-inews-organization ()
11669   "Return user's organization.
11670 The ORGANIZATION environment variable is used if defined.
11671 If not, the variable `gnus-local-organization' is used instead.
11672 If it is a function, the function will be called with the current
11673 newsgroup name as the argument.
11674 If this is a file name, the contents of this file will be used as the
11675 organization."
11676   (let* ((organization 
11677           (or (getenv "ORGANIZATION")
11678               (if gnus-local-organization
11679                   (if (and (symbolp gnus-local-organization)
11680                            (fboundp gnus-local-organization))
11681                       (funcall gnus-local-organization gnus-newsgroup-name)
11682                     gnus-local-organization))
11683               gnus-organization-file
11684               "~/.organization")))
11685     (and (stringp organization)
11686          (> (length organization) 0)
11687          (or (file-exists-p organization)
11688              (string-match " " organization)
11689              (not (string-match  "^/[^/]+/" (expand-file-name organization))))
11690          (save-excursion
11691            (set-buffer (get-buffer-create " *Gnus organization*"))
11692            (buffer-disable-undo (current-buffer))
11693            (erase-buffer)
11694            (if (file-exists-p organization)
11695                (insert-file-contents organization)
11696              (insert organization))
11697            (goto-char (point-min))
11698            (while (re-search-forward " *\n *" nil t)
11699              (replace-match " "))
11700            (buffer-substring (point-min) (point-max))))))
11701
11702 (defun gnus-inews-lines ()
11703   "Count the number of lines and return numeric string."
11704   (save-excursion
11705     (save-restriction
11706       (widen)
11707       (goto-char (point-min))
11708       (search-forward "\n\n" nil 'move)
11709       (int-to-string (count-lines (point) (point-max))))))
11710
11711 \f
11712 ;;;
11713 ;;; Gnus Mail Functions 
11714 ;;;
11715
11716 ;;; Mail reply commands of Gnus summary mode
11717
11718 (defun gnus-summary-reply (yank)
11719   "Reply mail to news author.
11720 If prefix argument YANK is non-nil, original article is yanked automatically.
11721 Customize the variable gnus-mail-reply-method to use another mailer."
11722   (interactive "P")
11723   ;; Bug fix by jbw@bigbird.bu.edu (Joe Wells)
11724   ;; Stripping headers should be specified with mail-yank-ignored-headers.
11725   (gnus-set-global-variables)
11726   (gnus-summary-select-article t)
11727   (setq gnus-winconf-post-news (current-window-configuration))
11728   (let ((gnus-newsgroup-name gnus-newsgroup-name))
11729     (bury-buffer gnus-article-buffer)
11730     (funcall gnus-mail-reply-method yank))
11731   (gnus-article-hide-headers-if-wanted))
11732
11733 (defun gnus-summary-reply-with-original ()
11734   "Reply mail to news author with original article.
11735 Customize the variable gnus-mail-reply-method to use another mailer."
11736   (interactive)
11737   (gnus-summary-reply t))
11738
11739 (defun gnus-summary-mail-forward ()
11740   "Forward the current message to another user.
11741 Customize the variable gnus-mail-forward-method to use another mailer."
11742   (interactive)
11743   (gnus-summary-select-article t)
11744   (setq gnus-winconf-post-news (current-window-configuration))
11745   (set-buffer gnus-article-buffer)
11746   (let ((gnus-newsgroup-name gnus-newsgroup-name))
11747     (funcall gnus-mail-forward-method))
11748   (gnus-article-hide-headers-if-wanted))
11749
11750 (defun gnus-summary-mail-other-window ()
11751   "Compose mail in other window.
11752 Customize the variable `gnus-mail-other-window-method' to use another
11753 mailer."
11754   (interactive)
11755   (setq gnus-winconf-post-news (current-window-configuration))
11756   (let ((gnus-newsgroup-name gnus-newsgroup-name))
11757     (funcall gnus-mail-other-window-method)))
11758
11759 (defun gnus-mail-reply-using-mail (&optional yank to-address)
11760   (save-excursion
11761     (set-buffer gnus-summary-buffer)
11762     (let ((info (nth 2 (gnus-gethash gnus-newsgroup-name gnus-newsrc-hashtb)))
11763           (group (gnus-group-real-name gnus-newsgroup-name))
11764           (cur (cons (current-buffer) (cdr gnus-article-current)))
11765           from subject date to reply-to message-of
11766           references message-id sender follow-to cc)
11767       (set-buffer (get-buffer-create "*mail*"))
11768       (mail-mode)
11769       (make-local-variable 'gnus-article-reply)
11770       (setq gnus-article-reply cur)
11771       (use-local-map (copy-keymap mail-mode-map))
11772       (local-set-key "\C-c\C-c" 'gnus-mail-send-and-exit)
11773       (if (and (buffer-modified-p)
11774                (> (buffer-size) 0)
11775                (not (gnus-y-or-n-p 
11776                      "Unsent article being composed; erase it? ")))
11777           ()
11778         (erase-buffer)
11779         (save-excursion
11780           (set-buffer gnus-article-buffer)
11781           (let ((buffer-read-only nil))
11782             (goto-char (point-min))
11783             (narrow-to-region (point-min)
11784                               (progn (search-forward "\n\n") (point)))
11785             (add-text-properties (point-min) (point-max) '(invisible nil)))
11786           (if (and (boundp 'gnus-reply-to-function)
11787                    gnus-reply-to-function)
11788               (save-excursion
11789                 (save-restriction
11790                   (gnus-narrow-to-headers)
11791                   (setq follow-to (funcall gnus-reply-to-function group)))))
11792           (setq from (mail-fetch-field "from"))
11793           (setq date (mail-fetch-field "date"))
11794           (and from
11795                (let ((stop-pos 
11796                       (string-match "  *at \\|  *@ \\| *(\\| *<" from)))
11797                  (setq message-of
11798                        (concat (if stop-pos (substring from 0 stop-pos) from)
11799                                "'s message of " date))))
11800           (setq sender (mail-fetch-field "sender"))
11801           (setq subject (or (mail-fetch-field "subject")
11802                             "Re: none"))
11803           (or (string-match "^[Rr][Ee]:" subject)
11804               (setq subject (concat "Re: " subject)))
11805           (setq cc (mail-fetch-field "cc"))
11806           (setq reply-to (mail-fetch-field "reply-to"))
11807           (setq references (mail-fetch-field "references"))
11808           (setq message-id (mail-fetch-field "message-id"))
11809           (widen))
11810         (setq news-reply-yank-from from)
11811         (setq news-reply-yank-message-id message-id)
11812         (mail-setup (or to-address 
11813                         (if (and follow-to (not (stringp follow-to))) ""
11814                           (or follow-to reply-to from sender "")))
11815                     subject message-of nil gnus-article-buffer nil)
11816         (if (and follow-to (listp follow-to))
11817             (progn
11818               (goto-char (point-min))
11819               (while follow-to
11820                 (insert (car (car follow-to)) ": " (cdr (car follow-to)) "\n")
11821                 (setq follow-to (cdr follow-to)))))
11822         ;; Fold long references line to follow RFC1036.
11823         (mail-position-on-field "References")
11824         (let ((begin (- (point) (length "References: ")))
11825               (fill-column 78)
11826               (fill-prefix "\t"))
11827           (if references (insert references))
11828           (if (and references message-id) (insert " "))
11829           (if message-id (insert message-id))
11830           ;; The region must end with a newline to fill the region
11831           ;; without inserting extra newline.
11832           (fill-region-as-paragraph begin (1+ (point))))
11833         (goto-char (point-min))
11834         (re-search-forward
11835          (concat "^" (regexp-quote mail-header-separator) "$"))
11836         (forward-line 1)
11837         (if yank
11838             (let ((last (point)))
11839               (run-hooks 'news-reply-header-hook)
11840               (mail-yank-original nil)
11841               (goto-char last))))
11842       (if (not yank)
11843           (let ((mail (current-buffer)))
11844             (gnus-configure-windows '(0 0 1))
11845             (switch-to-buffer-other-window mail))
11846         (gnus-configure-windows '(0 1 0))
11847         (switch-to-buffer (current-buffer))))))
11848
11849 (defun gnus-mail-yank-original ()
11850   (interactive)
11851   (run-hooks 'news-reply-header-hook)
11852   (mail-yank-original nil))
11853
11854 (defun gnus-mail-send-and-exit ()
11855   (interactive)
11856   (let ((cbuf (current-buffer)))
11857     (mail-send-and-exit nil)
11858     (if (get-buffer gnus-group-buffer)
11859         (progn
11860           (save-excursion
11861             (set-buffer cbuf)
11862             (let ((reply gnus-article-reply))
11863               (if (and reply
11864                        (get-buffer (car reply))
11865                        (buffer-name (car reply)))
11866                   (progn
11867                     (set-buffer (car reply))
11868                     (and (cdr reply)
11869                          (gnus-summary-mark-article-as-replied 
11870                           (cdr reply)))))))
11871           (and gnus-winconf-post-news
11872                (set-window-configuration gnus-winconf-post-news))
11873           (setq gnus-winconf-post-news nil)))))
11874
11875 (defun gnus-mail-forward-using-mail ()
11876   "Forward the current message to another user using mail."
11877   ;; This is almost a carbon copy of rmail-forward in rmail.el.
11878   (let ((forward-buffer (current-buffer))
11879         (subject
11880          (concat "[" (if (memq 'mail (assoc (symbol-name 
11881                                              (car (gnus-find-method-for-group 
11882                                                    gnus-newsgroup-name)))
11883                                             gnus-valid-select-methods))
11884                          (gnus-fetch-field "From")
11885                        gnus-newsgroup-name)
11886                  "] " (or (gnus-fetch-field "Subject") "")))
11887         beg)
11888     ;; If only one window, use it for the mail buffer.
11889     ;; Otherwise, use another window for the mail buffer
11890     ;; so that the Rmail buffer remains visible
11891     ;; and sending the mail will get back to it.
11892     (if (if (one-window-p t)
11893             (mail nil nil subject)
11894           (mail-other-window nil nil subject))
11895         (save-excursion
11896           (use-local-map (copy-keymap emacs-lisp-mode-map))
11897           (local-set-key "\C-c\C-c" 'gnus-mail-send-and-exit)
11898           (setq beg (goto-char (point-max)))
11899           (insert "------- Start of forwarded message -------\n")
11900           (insert-buffer forward-buffer)
11901           (goto-char (point-max))
11902           (insert "------- End of forwarded message -------\n")
11903           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>. 
11904           (goto-char beg)
11905           (while (setq beg (next-single-property-change (point) 'invisible))
11906             (goto-char beg)
11907             (delete-region beg (or (next-single-property-change 
11908                                     (point) 'invisible)
11909                                    (point-max))))
11910           ;; You have a chance to arrange the message.
11911           (run-hooks 'gnus-mail-forward-hook)))))
11912
11913 (defun gnus-mail-other-window-using-mail ()
11914   "Compose mail other window using mail."
11915   (mail-other-window nil nil nil nil nil (get-buffer gnus-article-buffer))
11916   (use-local-map (copy-keymap (current-local-map)))
11917   (local-set-key "\C-c\C-c" 'gnus-mail-send-and-exit))
11918
11919 \f
11920 ;;;
11921 ;;; Dribble file
11922 ;;;
11923
11924 (defvar gnus-dribble-ignore nil)
11925
11926 (defun gnus-dribble-file-name ()
11927   (concat gnus-startup-file "-dribble"))
11928
11929 (defun gnus-dribble-open ()
11930   (save-excursion 
11931     (set-buffer 
11932      (setq gnus-dribble-buffer (find-file-noselect (gnus-dribble-file-name))))
11933     (buffer-disable-undo (current-buffer))
11934     (bury-buffer gnus-dribble-buffer)
11935     (auto-save-mode t)
11936     (goto-char (point-max))))
11937
11938 (defun gnus-dribble-enter (string)
11939   (if (and (not gnus-dribble-ignore)
11940            gnus-dribble-buffer
11941            (buffer-name gnus-dribble-buffer))
11942       (let ((obuf (current-buffer)))
11943         (set-buffer gnus-dribble-buffer)
11944         (insert string "\n")
11945         (set-window-point (get-buffer-window (current-buffer)) (point-max))
11946         (set-buffer obuf))))
11947
11948 (defun gnus-dribble-read-file ()
11949   (let ((dribble-file (gnus-dribble-file-name)))
11950     (save-excursion 
11951       (set-buffer (setq gnus-dribble-buffer 
11952                         (get-buffer-create 
11953                          (file-name-nondirectory dribble-file))))
11954       (gnus-add-current-to-buffer-list)
11955       (erase-buffer)
11956       (set-visited-file-name dribble-file)
11957       (buffer-disable-undo (current-buffer))
11958       (bury-buffer (current-buffer))
11959       (set-buffer-modified-p nil)
11960       (let ((auto (make-auto-save-file-name))
11961             (gnus-dribble-ignore t))
11962         (if (or (file-exists-p auto) (file-exists-p dribble-file))
11963             (progn
11964               (if (file-newer-than-file-p auto dribble-file)
11965                   (setq dribble-file auto))
11966               (insert-file-contents dribble-file)
11967               (if (not (zerop (buffer-size)))
11968                   (set-buffer-modified-p t))
11969               (if (gnus-y-or-n-p 
11970                    "Auto-save file exists. Do you want to read it? ")
11971                   (progn
11972                     (message "Reading %s..." dribble-file) 
11973                     (eval-current-buffer)
11974                     (message "Reading %s...done" dribble-file)))))))))
11975
11976 (defun gnus-dribble-delete-file ()
11977   (if (file-exists-p (gnus-dribble-file-name))
11978       (delete-file (gnus-dribble-file-name)))
11979   (if gnus-dribble-buffer
11980       (save-excursion
11981         (set-buffer gnus-dribble-buffer)
11982         (let ((auto (make-auto-save-file-name)))
11983           (if (file-exists-p auto)
11984               (delete-file auto))
11985           (erase-buffer)
11986           (set-buffer-modified-p nil)))))
11987
11988 (defun gnus-dribble-save ()
11989   (if (and gnus-dribble-buffer
11990            (buffer-name gnus-dribble-buffer))
11991       (save-excursion
11992         (set-buffer gnus-dribble-buffer)
11993         (save-buffer))))
11994
11995 (defun gnus-dribble-clear ()
11996   (save-excursion
11997     (if (and gnus-dribble-buffer
11998              (buffer-name (get-buffer gnus-dribble-buffer)))
11999         (progn
12000           (set-buffer gnus-dribble-buffer)
12001           (erase-buffer)
12002           (set-buffer-modified-p nil)
12003           (setq buffer-saved-size (buffer-size))))))
12004
12005 ;;;
12006 ;;; Server Communication
12007 ;;;
12008
12009 ;; All the Gnus backends have the same interface, and should return
12010 ;; data in a similar format. Below is an overview of what functions
12011 ;; these packages must supply and what results they should return.
12012 ;;
12013 ;; Variables:
12014 ;;
12015 ;; `nntp-server-buffer' - All data should be returned to Gnus in this
12016 ;; buffer. 
12017 ;;
12018 ;; Functions for the imaginary backend `choke':
12019 ;;
12020 ;; `choke-retrieve-headers ARTICLES &optional GROUP SERVER'
12021 ;; Should return all headers for all ARTICLES, or return NOV lines for
12022 ;; the same.
12023 ;;
12024 ;; `choke-request-group GROUP &optional SERVER DISCARD'
12025 ;; Switch to GROUP. If DISCARD is nil, active information on the group
12026 ;; must be returned.
12027 ;;
12028 ;; `choke-close-group GROUP &optional SERVER'
12029 ;; Close group. Most backends won't have to do anything with this
12030 ;; call, but it is an opportunity to clean up, if that is needed. It
12031 ;; is called when Gnus exits a group.
12032 ;;
12033 ;; `choke-request-article ARTICLE &optional GROUP SERVER'
12034 ;; Return ARTICLE, which is either an article number or
12035 ;; message-id. Note that not all backends can return articles based on
12036 ;; message-id. 
12037 ;;
12038 ;; `choke-request-list SERVER'
12039 ;; Return a list of all newsgroups on SERVER.
12040 ;;
12041 ;; `choke-request-list-newsgroups SERVER'
12042 ;; Return a list of descriptions of all newsgroups on SERVER.
12043 ;;
12044 ;; `choke-request-newgroups DATE &optional SERVER'
12045 ;; Return a list of all groups that have arrived after DATE on
12046 ;; SERVER. Note that the date doesn't have to be respected - Gnus will
12047 ;; always check whether the groups are old or not. Backends that do
12048 ;; not store date information may just return the entire list of
12049 ;; groups, although this might not be a good idea in general.
12050 ;;
12051 ;; `choke-request-post-buffer METHOD HEADER ARTICLE-BUFFER GROUP INFO'
12052 ;; Should return a buffer that is suitable for "posting". nnspool and
12053 ;; nntp return a `*post-buffer*', and nnmail return a `*mail*'
12054 ;; buffer. This function should fill out the appropriate headers. 
12055 ;;
12056 ;; `choke-request-post &optional SERVER'
12057 ;; Function that will be called from a buffer to be posted. 
12058 ;;
12059 ;; `choke-open-server SERVER &optional ARGUMENT'
12060 ;; Open a connection to SERVER.
12061 ;;
12062 ;; `choke-close-server &optional SERVER'
12063 ;; Close the connection to SERVER.
12064 ;;
12065 ;; `choke-server-opened &optional SERVER'
12066 ;; Whether the conenction to SERVER is opened or not.
12067 ;;
12068 ;; `choke-server-status &optional SERVER'
12069 ;; Should return a status string (not in the nntp buffer, but as the
12070 ;; result of the function).
12071 ;;
12072 ;; The following functions are optional and apply only to backends
12073 ;; that are able to control the contents of their groups totally
12074 ;; (ie. mail backends.)  Backends that aren't able to do that
12075 ;; shouldn't define these functions at all. Gnus will check for their
12076 ;; presence before attempting to call them.
12077 ;;
12078 ;; `choke-request-expire-articles ARTICLES &optional NEWSGROUP SERVER'
12079 ;; Should expire (according to some aging scheme) all ARTICLES. Most
12080 ;; backends will not be able to expire articles. Should return a list
12081 ;; of all articles that were not expired.
12082 ;;
12083 ;; `choke-request-move-article ARTICLE GROUP SERVER ACCEPT-FORM &optional LAST'
12084 ;; Should move ARTICLE from GROUP on SERVER by using ACCEPT-FORM.
12085 ;; Removes any information it has added to the article (extra headers,
12086 ;; whatever - make it as clean as possible), and then passes the
12087 ;; article on by evaling ACCEPT-FORM, which is normally a call to the
12088 ;; function described below. If the ACCEPT-FORM returns a non-nil
12089 ;; value, the article should then be deleted. If LAST is nil, that
12090 ;; means that there will be further calls to this function. This might
12091 ;; be taken as an advice not to save buffers/internal variables just
12092 ;; yet, but wait until the last call to speed things up.
12093 ;;
12094 ;; `choke-request-accept-article GROUP &optional LAST' 
12095 ;; The contents of the current buffer will be put into GROUP.  There
12096 ;; should, of course, be an article in the current buffer.  This
12097 ;; function is normally only called by the function described above,
12098 ;; and LAST works the same way as in that function.
12099 ;;
12100 ;; `choke-request-replace-article ARTICLE GROUP BUFFER'
12101 ;; Replace ARTICLE in GROUP with the contents of BUFFER.
12102 ;; This provides an easy interface for allowing editing of
12103 ;; articles. Note that even headers may be edited, so the backend has
12104 ;; to update any tables (nov buffers, etc) that it maintains after
12105 ;; replacing the article.
12106 ;;
12107 ;; `choke-request-create-group GROUP &optional SERVER'
12108 ;; Create GROUP on SERVER.  This might be a new, empty group, or it
12109 ;; might be a group that already exists, but hasn't been registered
12110 ;; yet. 
12111 ;;
12112 ;; All these functions must return nil if they couldn't service the
12113 ;; request. If the optional arguments are not supplied, some "current"
12114 ;; or "default" values should be used. In short, one should emulate an
12115 ;; NNTP server, in a way.
12116 ;;
12117 ;; If you want to write a new backend, you just have to supply the
12118 ;; functions listed above. In addition, you must enter the new backend
12119 ;; into the list of valid select methods:
12120 ;; (setq gnus-valid-select-methods 
12121 ;;       (cons '("choke" mail) gnus-valid-select-methods))
12122 ;; The first element in this list is the name of the backend. Other
12123 ;; elemnets may be `mail' (for mail groups),  `post' (for news
12124 ;; groups), `none' (neither), `respool' (for groups that can control
12125 ;; their contents). 
12126
12127 (defun gnus-start-news-server (&optional confirm)
12128   "Open a method for getting news.
12129 If CONFIRM is non-nil, the user will be asked for an NNTP server."
12130   (let (how where)
12131     (if gnus-current-select-method
12132         ;; Stream is already opened.
12133         nil
12134       ;; Open NNTP server.
12135       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
12136       (if confirm
12137           (progn
12138             ;; Read server name with completion.
12139             (setq gnus-nntp-server
12140                   (completing-read "NNTP server: "
12141                                    (mapcar (lambda (server) (list server))
12142                                            (cons (list gnus-nntp-server)
12143                                                  gnus-secondary-servers))
12144                                    nil nil gnus-nntp-server))))
12145
12146       (if (and gnus-nntp-server 
12147                (stringp gnus-nntp-server)
12148                (not (string= gnus-nntp-server "")))
12149           (setq gnus-select-method
12150                 (cond ((or (string= gnus-nntp-server "")
12151                            (string= gnus-nntp-server "::"))
12152                        (list 'nnspool (system-name)))
12153                       ((string-match ":" gnus-nntp-server)
12154                        (list 'nnmh gnus-nntp-server))
12155                       (t
12156                        (list 'nntp gnus-nntp-server)))))
12157
12158       (setq how (car gnus-select-method))
12159       (setq where (car (cdr gnus-select-method)))
12160       (cond ((eq how 'nnspool)
12161              (require 'nnspool)
12162              (message "Looking up local news spool..."))
12163             ((eq how 'nnmh)
12164              (require 'nnmh)
12165              (message "Looking up mh spool..."))
12166             (t
12167              (require 'nntp)))
12168       (setq gnus-current-select-method gnus-select-method)
12169       (run-hooks 'gnus-open-server-hook)
12170       (or 
12171        ;; gnus-open-server-hook might have opened it
12172        (gnus-server-opened gnus-select-method)  
12173        (gnus-open-server gnus-select-method)
12174        (gnus-y-or-n-p
12175         (format
12176          "%s server on %s can't be opened. Continue? "
12177          (car gnus-select-method) (nth 1 gnus-select-method)))
12178        (progn
12179          (message "Couldn't open server on %s" (nth 1 gnus-select-method))
12180          (ding)
12181          nil)))))
12182
12183 (defun gnus-check-news-server (method)
12184   "If the news server is down, start it up again."
12185   (let ((method (if method method gnus-select-method)))
12186     (if (gnus-server-opened method)
12187         ;; Stream is already opened.
12188         t
12189       ;; Open server.
12190       (message "Opening server %s on %s..." (car method) (nth 1 method))
12191       (run-hooks 'gnus-open-server-hook)
12192       (or (gnus-server-opened method)
12193           (gnus-open-server method))
12194       (message ""))))
12195
12196 (defun gnus-nntp-message (&optional message)
12197   "Check the status of the NNTP server.
12198 If the status of the server is clear and MESSAGE is non-nil, MESSAGE
12199 is returned insted of the status string."
12200   (let ((status (gnus-status-message (gnus-find-method-for-group 
12201                                       gnus-newsgroup-name)))
12202         (message (or message "")))
12203     (if (and (stringp status) (> (length status) 0))
12204         status message)))
12205
12206 (defun gnus-get-function (method function)
12207   (let ((func (intern (format "%s-%s" (car method) function))))
12208     (if (not (fboundp func)) 
12209         (progn
12210           (require (car method))
12211           (if (not (fboundp func)) 
12212               (error "No such function: %s" func))))
12213     func))
12214
12215 ;; Specifying port number suggested by Stephane Laveau <laveau@corse.inria.fr>.
12216 (defun gnus-open-server (method)
12217   (apply (gnus-get-function method 'open-server) (cdr method)))
12218
12219 (defun gnus-close-server (method)
12220   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
12221
12222 (defun gnus-request-list (method)
12223   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
12224
12225 (defun gnus-request-list-newsgroups (method)
12226   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
12227
12228 (defun gnus-request-newgroups (date method)
12229   (funcall (gnus-get-function method 'request-newgroups) 
12230            date (nth 1 method)))
12231
12232 (defun gnus-server-opened (method)
12233   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
12234
12235 (defun gnus-status-message (method)
12236   (let ((method (if (stringp method) (gnus-find-method-for-group method)
12237                   method)))
12238     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
12239
12240 (defun gnus-request-group (group &optional dont-check)
12241   (let ((method (gnus-find-method-for-group group)))
12242     (funcall (gnus-get-function method 'request-group) 
12243              (gnus-group-real-name group) (nth 1 method) dont-check)))
12244
12245 (defun gnus-list-active-group (group)
12246   (let ((method (gnus-find-method-for-group group))
12247         (func 'list-active-group))
12248     (and (gnus-check-backend-function func group)
12249          (funcall (gnus-get-function method func) 
12250                   (gnus-group-real-name group) (nth 1 method)))))
12251
12252 (defun gnus-request-group-description (group)
12253   (let ((method (gnus-find-method-for-group group))
12254         (func 'request-group-description))
12255     (and (gnus-check-backend-function func group)
12256          (funcall (gnus-get-function method func) 
12257                   (gnus-group-real-name group) (nth 1 method)))))
12258
12259 (defun gnus-close-group (group)
12260   (let ((method (gnus-find-method-for-group group)))
12261     (funcall (gnus-get-function method 'close-group) 
12262              (gnus-group-real-name group) (nth 1 method))))
12263
12264 (defun gnus-retrieve-headers (articles group)
12265   (let ((method (gnus-find-method-for-group group)))
12266     (funcall (gnus-get-function method 'retrieve-headers) 
12267              articles (gnus-group-real-name group) (nth 1 method))))
12268
12269 (defun gnus-request-article (article group &optional buffer)
12270   (let ((method (gnus-find-method-for-group group)))
12271     (funcall (gnus-get-function method 'request-article) 
12272              article (gnus-group-real-name group) (nth 1 method) buffer)))
12273
12274 (defun gnus-request-head (article group)
12275   (let ((method (gnus-find-method-for-group group)))
12276     (funcall (gnus-get-function method 'request-head) 
12277              article (gnus-group-real-name group) (nth 1 method))))
12278
12279 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
12280 (defun gnus-request-post-buffer (post group subject header artbuf
12281                                       info follow-to respect-poster)
12282    (let* ((info (or info (and group (nth 2 (gnus-gethash 
12283                                             group gnus-newsrc-hashtb)))))
12284           (method
12285            (if (and gnus-post-method
12286                     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
12287                     (memq 'post (assoc
12288                                  (format "%s" (car (gnus-find-method-for-group
12289                                                     gnus-newsgroup-name)))
12290                                         gnus-valid-select-methods)))
12291                gnus-post-method
12292              (gnus-find-method-for-group gnus-newsgroup-name))))
12293      (let ((mail-self-blind nil)
12294            (mail-archive-file-name nil))
12295        (funcall (gnus-get-function method 'request-post-buffer) 
12296                 post group subject header artbuf info follow-to
12297                 respect-poster))))
12298
12299 (defun gnus-request-post (method &optional force)
12300   (and (not force) gnus-post-method
12301        (memq 'post (assoc (format "%s" (car method))
12302                           gnus-valid-select-methods))
12303        (setq method gnus-post-method))
12304   (funcall (gnus-get-function method 'request-post) 
12305            (nth 1 method)))
12306
12307 (defun gnus-request-expire-articles (articles group &optional force)
12308   (let ((method (gnus-find-method-for-group group)))
12309     (funcall (gnus-get-function method 'request-expire-articles) 
12310              articles (gnus-group-real-name group) (nth 1 method)
12311              force)))
12312
12313 (defun gnus-request-move-article 
12314   (article group server accept-function &optional last)
12315   (let ((method (gnus-find-method-for-group group)))
12316     (funcall (gnus-get-function method 'request-move-article) 
12317              article (gnus-group-real-name group) 
12318              (nth 1 method) accept-function last)))
12319
12320 (defun gnus-request-accept-article (group &optional last)
12321   (let ((func (if (symbolp group) group
12322                 (car (gnus-find-method-for-group group)))))
12323     (funcall (intern (format "%s-request-accept-article" func))
12324              (if (stringp group) (gnus-group-real-name group) group)
12325              last)))
12326
12327 (defun gnus-request-replace-article (article group buffer)
12328   (let ((func (car (gnus-find-method-for-group group))))
12329     (funcall (intern (format "%s-request-replace-article" func))
12330              article (gnus-group-real-name group) buffer)))
12331
12332 (defun gnus-request-create-group (group)
12333   (let ((method (gnus-find-method-for-group group)))
12334     (funcall (gnus-get-function method 'request-create-group) 
12335              (gnus-group-real-name group) (nth 1 method))))
12336
12337
12338 (defun gnus-find-method-for-group (group)
12339   (or gnus-override-method
12340       (and (not group)
12341            gnus-select-method)
12342       (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
12343         (if (or (not info)
12344                 (not (nth 4 info)))
12345             gnus-select-method
12346           (nth 4 info)))))
12347
12348 (defun gnus-check-backend-function (func group)
12349   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
12350                  group)))
12351     (fboundp (intern (format "%s-%s" method func)))))
12352
12353 (defun gnus-methods-using (method)
12354   (let ((valids gnus-valid-select-methods)
12355         outs)
12356     (while valids
12357       (if (memq method (car valids)) 
12358           (setq outs (cons (car valids) outs)))
12359       (setq valids (cdr valids)))
12360     outs))
12361
12362 ;;; 
12363 ;;; Active & Newsrc File Handling
12364 ;;;
12365
12366 ;; Newsrc related functions.
12367 ;; Gnus internal format of gnus-newsrc-assoc:
12368 ;; (("alt.general" 3 (1 . 1))
12369 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
12370 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
12371 ;; The first item is the group name; the second is the subscription
12372 ;; level; the third is either a range of a list of ranges of read
12373 ;; articles, the optional fourth element is a list of marked articles,
12374 ;; the optional fifth element is the select method.
12375 ;;
12376 ;; Gnus internal format of gnus-newsrc-hashtb:
12377 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
12378 ;; This is the entry for "alt.misc". The first element is the number
12379 ;; of unread articles in "alt.misc". The cdr of this entry is the
12380 ;; element *before* "alt.misc" in gnus-newsrc-assoc, which makes is
12381 ;; trivial to remove or add new elements into gnus-newsrc-assoc
12382 ;; without scanning the entire list. So, to get the actual information
12383 ;; of "alt.misc", you'd say something like 
12384 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
12385 ;;
12386 ;; Gnus internal format of gnus-active-hashtb:
12387 ;; ((1 . 1))
12388 ;;  (5 . 10))
12389 ;;  (67 . 99)) ...)
12390 ;; The only element in each entry in this hash table is a range of
12391 ;; (possibly) available articles. (Articles in this range may have
12392 ;; been expired or cancelled.)
12393 ;;
12394 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
12395 ;; ("alt.misc" "alt.test" "alt.general" ...)
12396
12397 (defun gnus-setup-news (&optional rawfile level)
12398   "Setup news information.
12399 If RAWFILE is non-nil, the .newsrc file will also be read.
12400 If LEVEL is non-nil, the news will be set up at level LEVEL."
12401   (let ((init (not (and gnus-newsrc-assoc gnus-active-hashtb (not rawfile)))))
12402     ;; Clear some variables to re-initialize news information.
12403     (if init (setq gnus-newsrc-assoc nil gnus-active-hashtb nil))
12404     ;; Read the active file and create `gnus-active-hashtb'.
12405     ;; If `gnus-read-active-file' is nil, then we just create an empty
12406     ;; hash table. The partial filling out of the hash table will be
12407     ;; done in `gnus-get-unread-articles'.
12408     (if (and gnus-read-active-file 
12409              (not level)
12410              (gnus-server-opened gnus-select-method))
12411         (gnus-read-active-file)
12412       (setq gnus-active-hashtb (make-vector 4095 0)))
12413
12414     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
12415     (if init (gnus-read-newsrc-file rawfile))
12416     ;; Find the number of unread articles in each non-dead group.
12417     (gnus-get-unread-articles (or level 6))
12418     ;; Find new newsgroups and treat them.
12419     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level)
12420              (gnus-server-opened gnus-select-method))
12421         (gnus-find-new-newsgroups))
12422     (if (and init gnus-check-bogus-newsgroups 
12423              gnus-read-active-file (not level)
12424              (gnus-server-opened gnus-select-method))
12425         (gnus-check-bogus-newsgroups))))
12426
12427 (defun gnus-find-new-newsgroups ()
12428   "Search for new newsgroups and add them.
12429 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
12430 The `-n' option line from .newsrc is respected."
12431   (interactive)
12432   (or (gnus-check-first-time-used)
12433       (if (or (consp gnus-check-new-newsgroups)
12434               (eq gnus-check-new-newsgroups 'ask-server))
12435           (gnus-ask-server-for-new-groups)
12436         (let ((groups 0)
12437               group new-newsgroups)
12438           (or gnus-have-read-active-file (gnus-read-active-file))
12439           (setq gnus-newsrc-last-checked-date (current-time-string))
12440           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
12441           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
12442           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
12443           (mapatoms
12444            (lambda (sym)
12445              (setq group (symbol-name sym))
12446              (if (or (gnus-gethash group gnus-killed-hashtb)
12447                      (gnus-gethash group gnus-newsrc-hashtb))
12448                  ()
12449                (let ((do-sub (gnus-matches-options-n group)))
12450                  (cond ((eq do-sub 'subscribe)
12451                         (setq groups (1+ groups))
12452                         (gnus-sethash group group gnus-killed-hashtb)
12453                         (funcall 
12454                          gnus-subscribe-options-newsgroup-method group))
12455                        ((eq do-sub 'ignore)
12456                         nil)
12457                        (t
12458                         (setq groups (1+ groups))
12459                         (gnus-sethash group group gnus-killed-hashtb)
12460                         (if gnus-subscribe-hierarchical-interactive
12461                             (setq new-newsgroups (cons group new-newsgroups))
12462                           (funcall gnus-subscribe-newsgroup-method group)))))))
12463            gnus-active-hashtb)
12464           (if new-newsgroups 
12465               (gnus-subscribe-hierarchical-interactive new-newsgroups))
12466           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
12467           (if (> groups 0)
12468               (message "%d new newsgroup%s arrived." 
12469                        groups (if (> groups 1) "s have" " has")))))))
12470
12471 (defun gnus-matches-options-n (group)
12472   ;; Returns `subscribe' if the group is to be uncoditionally
12473   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
12474   ;; no match for the group.
12475
12476   ;; First we check the two user variables.
12477   (cond
12478    ((and gnus-options-not-subscribe
12479          (string-match gnus-options-not-subscribe group))
12480     'subscribe)
12481    ((and gnus-options-subscribe
12482          (string-match gnus-options-subscribe group))
12483     'ignore)
12484    ;; Then we go through the list that was retrieved from the .newsrc
12485    ;; file.  This list has elements on the form 
12486    ;; `(REGEXP . {ignore,subscribe})'. The first match found (the list
12487    ;; is in the reverse order of the options line) is returned.
12488    (t
12489     (let ((regs gnus-newsrc-options-n))
12490       (while (and regs
12491                   (not (string-match (car (car gnus-newsrc-options-n)) group)))
12492         (setq regs (cdr regs)))
12493       (and regs (cdr (car regs)))))))
12494
12495 (defun gnus-ask-server-for-new-groups ()
12496   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
12497          (methods (cons gnus-select-method 
12498                         (append
12499                          (and (consp gnus-check-new-newsgroups)
12500                               gnus-check-new-newsgroups)
12501                          gnus-secondary-select-methods)))
12502          (groups 0)
12503          (new-date (current-time-string))
12504          hashtb group new-newsgroups got-new)
12505     ;; Go thorugh both primary and secondary select methods and
12506     ;; request new newsgroups.  
12507     (while methods
12508       (if (gnus-request-newgroups date (car methods))
12509           (save-excursion
12510             (setq got-new t)
12511             (or hashtb (setq hashtb (gnus-make-hashtable 
12512                                      (count-lines (point-min) (point-max)))))
12513             (set-buffer nntp-server-buffer)
12514             ;; Enter all the new groups in a hashtable.
12515             (gnus-active-to-gnus-format (car methods) hashtb)))
12516       (setq methods (cdr methods)))
12517     (and got-new (setq gnus-newsrc-last-checked-date new-date))
12518     ;; Now all new groups from all select methods are in `hashtb'.
12519     (mapatoms
12520      (lambda (group-sym)
12521        (setq group (symbol-name group-sym))
12522        (if (or (gnus-gethash group gnus-newsrc-hashtb)
12523                (member group gnus-zombie-list)
12524                (member group gnus-killed-list))
12525            ;; The group is already known.
12526            ()
12527          (gnus-sethash group (symbol-value group-sym) gnus-active-hashtb)
12528          (let ((do-sub (gnus-matches-options-n group)))
12529            (cond ((eq do-sub 'subscribe)
12530                   (setq groups (1+ groups))
12531                   (gnus-sethash group group gnus-killed-hashtb)
12532                   (funcall 
12533                    gnus-subscribe-options-newsgroup-method group))
12534                  ((eq do-sub 'ignore)
12535                   nil)
12536                  (t
12537                   (setq groups (1+ groups))
12538                   (gnus-sethash group group gnus-killed-hashtb)
12539                   (if gnus-subscribe-hierarchical-interactive
12540                       (setq new-newsgroups (cons group new-newsgroups))
12541                     (funcall gnus-subscribe-newsgroup-method group)))))))
12542      hashtb)
12543     (if new-newsgroups 
12544         (gnus-subscribe-hierarchical-interactive new-newsgroups))
12545     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
12546     (if (> groups 0)
12547         (message "%d new newsgroup%s arrived." 
12548                  groups (if (> groups 1) "s have" " has")))
12549     got-new))
12550
12551 (defun gnus-check-first-time-used ()
12552   (if (or (> (length gnus-newsrc-assoc) 1)
12553           (file-exists-p gnus-startup-file)
12554           (file-exists-p (concat gnus-startup-file ".el"))
12555           (file-exists-p (concat gnus-startup-file ".eld")))
12556       nil
12557     (message "First time user; subscribing you to default groups")
12558     (or gnus-have-read-active-file (gnus-read-active-file))
12559     (setq gnus-newsrc-last-checked-date (current-time-string))
12560     (let ((groups gnus-default-subscribed-newsgroups)
12561           group)
12562       (if (eq groups t)
12563           nil
12564         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
12565         (mapatoms
12566          (lambda (sym)
12567            (setq group (symbol-name sym))
12568            (let ((do-sub (gnus-matches-options-n group)))
12569              (cond ((eq do-sub 'subscribe)
12570                     (gnus-sethash group group gnus-killed-hashtb)
12571                     (funcall 
12572                      gnus-subscribe-options-newsgroup-method group))
12573                    ((eq do-sub 'ignore)
12574                     nil)
12575                    (t
12576                     (setq gnus-killed-list (cons group gnus-killed-list))))))
12577          gnus-active-hashtb)
12578         (while groups
12579           (if (gnus-gethash (car groups) gnus-active-hashtb)
12580               (gnus-group-change-level (car groups) 3 9))
12581           (setq groups (cdr groups)))
12582         (gnus-group-make-help-group)
12583         (and gnus-novice-user
12584              (message (substitute-command-keys "\\<gnus-group-mode-map>\\[gnus-group-list-killed] to list killed groups")))))))
12585
12586 ;; `gnus-group-change-level' is the fundamental function for changing
12587 ;; subscription levels of newsgroups. This might mean just changing
12588 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
12589 ;; again, which subscribes/unsubscribes a group, which is equally
12590 ;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
12591 ;; from 8-9 to 1-7 means that you remove the group from the list of
12592 ;; killed (or zombie) groups and add them to the (kinda) subscribed
12593 ;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
12594 ;; which is trivial.
12595 ;; ENTRY can either be a string (newsgroup name) or a list (if
12596 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
12597 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
12598 ;; entries. 
12599 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
12600 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
12601 ;; after. 
12602 (defun gnus-group-change-level (entry level &optional oldlevel
12603                                       previous fromkilled)
12604   (let ((pinfo entry)
12605         group info active num)
12606     ;; Glean what info we can from the arguments
12607     (if (consp entry)
12608         (if fromkilled (setq group (nth 1 entry))
12609           (setq group (car (nth 2 entry))))
12610       (setq group entry))
12611     (if (and (stringp entry)
12612              oldlevel 
12613              (< oldlevel 8))
12614         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
12615     (if (and (not oldlevel)
12616              (listp entry))
12617         (setq oldlevel (car (cdr (nth 2 entry)))))
12618     (if (stringp previous)
12619         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
12620
12621     (gnus-dribble-enter
12622      (format "(gnus-group-change-level %S %S %S %S %S)" 
12623              group level oldlevel (car (nth 2 previous)) fromkilled))
12624     
12625     ;; Then we remove the newgroup from any old structures, if needed.
12626     ;; If the group was killed, we remove it from the killed or zombie
12627     ;; list. If not, and it is in fact going to be killed, we remove
12628     ;; it from the newsrc hash table and assoc.
12629     (cond ((>= oldlevel 8)
12630            (if (= oldlevel 8)
12631                (setq gnus-zombie-list (delete group gnus-zombie-list))
12632              (setq gnus-killed-list (delete group gnus-killed-list))))
12633           (t
12634            (if (>= level 8)
12635                (progn
12636                  (gnus-sethash (car (nth 2 entry))
12637                                nil gnus-newsrc-hashtb)
12638                  (if (nth 3 entry)
12639                      (setcdr (gnus-gethash (car (nth 3 entry))
12640                                            gnus-newsrc-hashtb)
12641                              (cdr entry)))
12642                  (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
12643
12644     ;; Finally we enter (if needed) the list where it is supposed to
12645     ;; go, and change the subscription level. If it is to be killed,
12646     ;; we enter it into the killed or zombie list.
12647     (cond ((>= level 8)
12648            (and (string= group (gnus-group-real-name group))
12649                 (if (= level 8)
12650                     (setq gnus-zombie-list (cons group gnus-zombie-list))
12651                   (setq gnus-killed-list (cons group gnus-killed-list)))))
12652           (t
12653            ;; If the list is to be entered into the newsrc assoc, and
12654            ;; it was killed, we have to create an entry in the newsrc
12655            ;; hashtb format and fix the pointers in the newsrc assoc.
12656            (if (>= oldlevel 8)
12657                (progn
12658                  (if (listp entry)
12659                      (progn
12660                        (setq info (cdr entry))
12661                        (setq num (car entry)))
12662                    (setq active (gnus-gethash group gnus-active-hashtb))
12663                    (setq num (if active (- (1+ (cdr active)) (car active)) t))
12664                    ;; Check whether the group is foreign. If so, the
12665                    ;; foreign select method has to be entered into the
12666                    ;; info. 
12667                    (let ((method (gnus-group-method-name group)))
12668                      (if (eq method gnus-select-method)
12669                          (setq info (list group level nil))
12670                        (setq info (list group level nil nil method)))))
12671                  (setq entry (cons info (if previous (cdr (cdr previous))
12672                                           (cdr gnus-newsrc-assoc))))
12673                  (setcdr (if previous (cdr previous) gnus-newsrc-assoc)
12674                          entry)
12675                  (gnus-sethash group (cons num (if previous (cdr previous)
12676                                                  gnus-newsrc-assoc))
12677                                gnus-newsrc-hashtb)
12678                  (if (cdr entry)
12679                      (setcdr (gnus-gethash (car (car (cdr entry)))
12680                                            gnus-newsrc-hashtb)
12681                              entry)))
12682              ;; It was alive, and it is going to stay alive, so we
12683              ;; just change the level and don't change any pointers or
12684              ;; hash table entries.
12685              (setcar (cdr (car (cdr (cdr entry)))) level))))))
12686
12687 (defun gnus-kill-newsgroup (newsgroup)
12688   "Obsolete function. Kills a newsgroup."
12689   (gnus-group-change-level (gnus-gethash newsgroup gnus-newsrc-hashtb) 9))
12690
12691 (defun gnus-check-bogus-newsgroups (&optional confirm)
12692   "Remove bogus newsgroups.
12693 If CONFIRM is non-nil, the user has to confirm the deletion of every
12694 newsgroup." 
12695   (let ((newsrc (cdr gnus-newsrc-assoc))
12696         bogus group)
12697     (message "Checking bogus newsgroups...")
12698     (or gnus-have-read-active-file (gnus-read-active-file))
12699     ;; Find all bogus newsgroup that are subscribed.
12700     (while newsrc
12701       (setq group (car (car newsrc)))
12702       (if (or (gnus-gethash group gnus-active-hashtb)
12703               (nth 4 (car newsrc))
12704               (and confirm
12705                    (not (gnus-y-or-n-p
12706                          (format "Remove bogus newsgroup: %s " group)))))
12707           ;; Active newsgroup.
12708           ()
12709         ;; Found a bogus newsgroup.
12710         (setq bogus (cons group bogus)))
12711       (setq newsrc (cdr newsrc)))
12712     ;; Remove all bogus subscribed groups by first killing them, and
12713     ;; then removing them from the list of killed groups.
12714     (while bogus
12715       (gnus-group-change-level 
12716        (gnus-gethash (car bogus) gnus-newsrc-hashtb) 9)
12717       (setq gnus-killed-list (delete (car bogus) gnus-killed-list))
12718       (setq bogus (cdr bogus)))
12719     ;; Then we remove all bogus groups from the list of killed and
12720     ;; zombie groups. They are are removed without confirmation.
12721     (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
12722           killed)
12723       (while dead-lists
12724         (setq killed (symbol-value (car dead-lists)))
12725         (while killed
12726           (setq group (car killed))
12727           (or (gnus-gethash group gnus-active-hashtb)
12728               ;; The group is bogus.
12729               (set (car dead-lists)
12730                    (delete group (symbol-value (car dead-lists)))))
12731           (setq killed (cdr killed)))
12732         (setq dead-lists (cdr dead-lists))))
12733     (message "Checking bogus newsgroups... done")))
12734
12735 (defun gnus-check-duplicate-killed-groups ()
12736   "Remove duplicates from the list of killed groups."
12737   (interactive)
12738   (let ((killed gnus-killed-list))
12739     (while killed
12740       (message "%d" (length killed))
12741       (setcdr killed (delete (car killed) (cdr killed)))
12742       (setq killed (cdr killed)))))
12743
12744 ;; Go though `gnus-newsrc-assoc' and compare with `gnus-active-hashtb'
12745 ;; and compute how many unread articles there are in each group.
12746 (defun gnus-get-unread-articles (&optional level)
12747   (let ((newsrc (cdr gnus-newsrc-assoc))
12748         (level (or level 6))
12749         info group active virtuals)
12750     (message "Checking new news...")
12751     (while newsrc
12752       (setq info (car newsrc))
12753       (setq group (car info))
12754       (setq active (gnus-gethash group gnus-active-hashtb))
12755
12756       ;; Check newsgroups. If the user doesn't want to check them, or
12757       ;; they can't be checked (for instance, if the news server can't
12758       ;; be reached) we just set the number of unread articles in this
12759       ;; newsgroup to t. This means that Gnus thinks that there are
12760       ;; unread articles, but it has no idea how many.
12761       (if (nth 4 info)
12762           (if (or (and gnus-activate-foreign-newsgroups 
12763                        (not (numberp gnus-activate-foreign-newsgroups)))
12764                   (and (numberp gnus-activate-foreign-newsgroups)
12765                        (<= (nth 1 info) gnus-activate-foreign-newsgroups)
12766                        (<= (nth 1 info) level)))
12767               (if (eq (car (nth 4 info)) 'nnvirtual)
12768                   (setq virtuals (cons info virtuals))
12769                 (setq active (gnus-activate-newsgroup (car info)))))
12770         (if (and (not gnus-read-active-file)
12771                  (<= (nth 1 info) level))
12772             (progn
12773               (setq active (gnus-activate-newsgroup (car info))))))
12774       
12775       (or active (progn (gnus-sethash group nil gnus-active-hashtb)
12776                         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
12777       (and active (gnus-get-unread-articles-in-group info active))
12778       (setq newsrc (cdr newsrc)))
12779
12780     ;; Activate the virtual groups. This has to be done after all the
12781     ;; other groups. 
12782     ;; !!! If one virtual group contains another virtual group, even
12783     ;; doing it this way might cause problems.
12784    (while virtuals
12785       (and (setq active (gnus-activate-newsgroup (car (car virtuals))))
12786            (gnus-get-unread-articles-in-group (car virtuals) active))
12787       (setq virtuals (cdr virtuals)))
12788
12789     (message "Checking new news... done")))
12790
12791 ;; Create a hash table out of the newsrc alist. The `car's of the
12792 ;; alist elements are used as keys.
12793 (defun gnus-make-hashtable-from-newsrc-alist ()
12794   (let ((alist gnus-newsrc-assoc)
12795          prev)
12796     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
12797     (setq alist 
12798           (setq prev (setq gnus-newsrc-assoc 
12799                            (cons (list "dummy.group" 0 nil) alist))))
12800     (while alist
12801       (gnus-sethash (car (car alist)) (cons nil prev) gnus-newsrc-hashtb)
12802       (setq prev alist)
12803       (setq alist (cdr alist)))))
12804
12805 (defun gnus-make-hashtable-from-killed ()
12806   "Create a hash table from the killed and zombie lists."
12807   (let ((lists '(gnus-killed-list gnus-zombie-list))
12808         list)
12809     (setq gnus-killed-hashtb 
12810           (gnus-make-hashtable 
12811            (+ (length gnus-killed-list) (length gnus-zombie-list))))
12812     (while lists
12813       (setq list (symbol-value (car lists)))
12814       (setq lists (cdr lists))
12815       (while list
12816         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
12817         (setq list (cdr list))))))
12818
12819 (defun gnus-get-unread-articles-in-group (info active)
12820   (let* ((range (nth 2 info))
12821          (num 0)
12822          (marked (nth 3 info))
12823          srange lowest group highest)
12824     ;; Modify the list of read articles according to what articles 
12825     ;; are available; then tally the unread articles and add the
12826     ;; number to the group hash table entry.
12827     (cond ((zerop (cdr active))
12828            (setq num 0))
12829           ((not range)
12830            (setq num (- (1+ (cdr active)) (car active))))
12831           ((not (listp (cdr range)))
12832            ;; Fix a single (num . num) range according to the
12833            ;; active hash table.
12834            ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
12835            (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
12836            (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
12837            ;; Compute number of unread articles.
12838            (setq num (max 0 (- (cdr active) 
12839                                (- (1+ (cdr range)) (car range))))))
12840           (t
12841            ;; The read list is a list of ranges. Fix them according to
12842            ;; the active hash table.
12843            ;; First peel off any elements that are below the lower
12844            ;; active limit. 
12845            (while (and (cdr range) 
12846                        (>= (car active) 
12847                            (or (and (atom (car (cdr range))) (car (cdr range)))
12848                                (car (car (cdr range))))))
12849              (if (numberp (car range))
12850                  (setcar range 
12851                          (cons (car range) 
12852                                (or (and (numberp (car (cdr range)))
12853                                         (car (cdr range))) 
12854                                    (cdr (car (cdr range))))))
12855                (setcdr (car range) 
12856                        (or (and (numberp (nth 1 range)) (nth 1 range))
12857                            (cdr (car (cdr range))))))
12858              (setcdr range (cdr (cdr range))))
12859            ;; Adjust the first element to be the same as the lower limit. 
12860            (if (and (not (atom (car range))) 
12861                     (< (cdr (car range)) (car active)))
12862                (setcdr (car range) (1- (car active))))
12863            ;; Then we want to peel off any elements that are higher
12864            ;; than the upper active limit.  
12865            (let ((srange range))
12866              ;; Go past all legal elements.
12867              (while (and (cdr srange) 
12868                          (<= (or (and (atom (car (cdr srange)))
12869                                       (car (cdr srange)))
12870                                  (car (car (cdr srange)))) (cdr active)))
12871                (setq srange (cdr srange)))
12872              (if (cdr srange)
12873                  ;; Nuke all remaining illegal elements.
12874                  (setcdr srange nil))
12875
12876              ;; Adjust the final element.
12877              (if (and (not (atom (car srange)))
12878                       (> (cdr (car srange)) (cdr active)))
12879                  (setcdr (car srange) (cdr active))))
12880            ;; Compute the number of unread articles.
12881            (while range
12882              (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
12883                                          (cdr (car range))))
12884                                  (or (and (atom (car range)) (car range))
12885                                      (car (car range))))))
12886              (setq range (cdr range)))
12887            (setq num (max 0 (- (cdr active) num)))))
12888     (and info
12889          (progn
12890            (and (assq 'tick marked)
12891                 (inline (gnus-remove-illegal-marked-articles
12892                          (assq 'tick marked) (nth 2 info))))
12893            (and (assq 'dormant marked)
12894                 (inline (gnus-remove-illegal-marked-articles
12895                          (assq 'dormant marked) (nth 2 info))))
12896            (setcar
12897             (gnus-gethash (car info) gnus-newsrc-hashtb) 
12898             (setq num (max 0 (- num (length (cdr (assq 'tick marked)))
12899                                 (length (cdr (assq 'dormant marked)))))))))
12900     num))
12901
12902 (defun gnus-remove-illegal-marked-articles (marked ranges)
12903   (let ((m (cdr marked)))
12904     ;; Make sure that all ticked articles are a subset of the unread
12905     ;; articles. 
12906     (while m
12907       (if (gnus-member-of-range (car m) ranges)
12908           (setcdr marked (cdr m))
12909         (setq marked m))
12910       (setq m (cdr m)))))
12911
12912 (defun gnus-activate-newsgroup (group)
12913   (let ((method (gnus-find-method-for-group group))
12914         active)
12915     (and (or (gnus-server-opened method) (gnus-open-server method))
12916          (gnus-request-group group)
12917          (save-excursion
12918            (set-buffer nntp-server-buffer)
12919            (goto-char 1)
12920            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
12921                 (progn
12922                   (goto-char (match-beginning 1))
12923                   (gnus-sethash 
12924                    group (setq active (cons (read (current-buffer))
12925                                             (read (current-buffer))))
12926                    gnus-active-hashtb))
12927                 active)))))
12928
12929 (defun gnus-update-read-articles 
12930   (group unread unselected ticked &optional domarks replied expirable killed
12931          dormant bookmark score)
12932   "Update the list of read and ticked articles in GROUP using the
12933 UNREAD and TICKED lists.
12934 Note: UNSELECTED has to be sorted over `<'.
12935 Returns whether the updating was successful."
12936   (let* ((active (gnus-gethash group gnus-active-hashtb))
12937          (entry (gnus-gethash group gnus-newsrc-hashtb))
12938          (number (car entry))
12939          (info (nth 2 entry))
12940          (marked (nth 3 info))
12941          (prev 1)
12942          (unread (sort (copy-sequence unread) (function <)))
12943          last read)
12944     (if (or (not info) (not active))
12945         ;; There is no info on this group if it was, in fact,
12946         ;; killed. Gnus stores no information on killed groups, so
12947         ;; there's nothing to be done. 
12948         ;; One could store the information somewhere temporarily,
12949         ;; perhaps... Hmmm... 
12950         ()
12951       ;; Remove any negative articles numbers.
12952       (while (and unread (< (car unread) 0))
12953         (setq unread (cdr unread)))
12954       (setq unread (sort (append unselected unread) '<))
12955       ;; Set the number of unread articles in gnus-newsrc-hashtb.
12956       (setcar entry (max 0 (- (length unread) (length ticked) 
12957                               (length dormant))))
12958       ;; Compute the ranges of read articles by looking at the list of
12959       ;; unread articles.  
12960       (while unread
12961         (if (/= (car unread) prev)
12962             (setq read (cons (if (= prev (1- (car unread))) prev
12963                                (cons prev (1- (car unread)))) read)))
12964         (setq prev (1+ (car unread)))
12965         (setq unread (cdr unread)))
12966       (if (<= prev (cdr active))
12967           (setq read (cons (cons prev (cdr active)) read)))
12968       ;; Enter this list into the group info.
12969       (setcar (cdr (cdr info)) 
12970               (if (> (length read) 1) (nreverse read) read))
12971       ;; Enter the list of ticked articles.
12972       (gnus-set-marked-articles 
12973        info ticked
12974        (if domarks replied (cdr (assq 'reply marked)))
12975        (if domarks expirable (cdr (assq 'expire marked)))
12976        (if domarks killed (cdr (assq 'killed marked)))
12977        (if domarks dormant (cdr (assq 'dormant marked)))
12978        (if domarks bookmark (cdr (assq 'bookmark marked)))
12979        (if domarks score (cdr (assq 'score marked))))
12980       t)))
12981
12982 (defun gnus-make-articles-unread (group articles)
12983   "Mark ARTICLES in GROUP as unread."
12984   (let ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
12985                          (gnus-gethash (gnus-group-real-name group)
12986                                        gnus-newsrc-hashtb)))))
12987     (setcar (nthcdr 2 info)
12988             (gnus-remove-from-range (nth 2 info) articles))
12989     (gnus-group-update-group group t)))
12990
12991 (defun gnus-read-active-file ()
12992   "Get active file from NNTP server."
12993   (gnus-group-set-mode-line)
12994   (let ((methods (cons gnus-select-method gnus-secondary-select-methods)))
12995     (setq gnus-have-read-active-file nil)
12996     (while methods
12997       (let* ((where (nth 1 (car methods)))
12998              (mesg (format "Reading active file%s via %s..."
12999                            (if (and where (not (zerop (length where))))
13000                                (concat " from " where) "")
13001                            (car (car methods)))))
13002         (message mesg)
13003         (gnus-check-news-server (car methods))
13004         (if (gnus-request-list (car methods)) ; Get active 
13005             (save-excursion
13006               (set-buffer nntp-server-buffer)
13007               (gnus-active-to-gnus-format 
13008                (and gnus-have-read-active-file (car methods)))
13009               (setq gnus-have-read-active-file t)
13010               (message "%s...done" mesg))
13011           (message "Cannot read active file from %s server." 
13012                    (car (car methods)))
13013           (ding)))
13014       (setq methods (cdr methods)))))
13015
13016 ;; rewritten by jwz based on ideas from Rick Sladkey <jrs@world.std.com>
13017 ;; Further rewrites by lmi.
13018 (defun gnus-active-to-gnus-format (method &optional hashtb)
13019   "Convert active file format to internal format.
13020 Lines matching `gnus-ignored-newsgroups' are ignored."
13021   (let ((cur (current-buffer))
13022         (hashtb (or hashtb 
13023                     (if method
13024                         gnus-active-hashtb
13025                       (setq gnus-active-hashtb
13026                             (gnus-make-hashtable 
13027                              (count-lines (point-min) (point-max))))))))
13028     ;; Delete unnecessary lines.
13029     (goto-char (point-min))
13030     (delete-matching-lines gnus-ignored-newsgroups)
13031     (and method (not (eq method gnus-select-method))
13032          (let ((prefix (gnus-group-prefixed-name "" method)))
13033            (goto-char (point-min))
13034            (while (and (not (eobp))
13035                        (null (insert prefix))
13036                        (zerop (forward-line 1))))))
13037     (goto-char (point-min))
13038     ;; Store active file in hashtable.
13039     (save-restriction
13040       (if (or (re-search-forward "\n.\r?$" nil t)
13041               (goto-char (point-max)))
13042           (progn
13043             (beginning-of-line)
13044             (narrow-to-region (point-min) (point))))
13045       (goto-char (point-min))
13046       (if (string-match "%[oO]" gnus-group-line-format)
13047           ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
13048           ;; If we want information on moderated groups, we use this
13049           ;; loop...   
13050           (condition-case ()
13051               (let ((mod-hashtb (make-vector 7 0))
13052                     group max mod)
13053                 (while (not (eobp))
13054                   (setq group (let ((obarray hashtb))
13055                                 (read cur)))
13056                   (setq max (read cur))
13057                   (set group (cons (read cur) max))
13058                   ;; Enter moderated groups into a list.
13059                   (if (string= 
13060                        (symbol-name  (let ((obarray mod-hashtb)) (read cur)))
13061                        "m")
13062                       (setq gnus-moderated-list 
13063                             (cons (symbol-name group) gnus-moderated-list)))
13064                   (forward-line 1)))
13065             (error 
13066              (progn (ding) (message "Possible error in active file."))))
13067         ;; And if we do not care about moderation, we use this loop,
13068         ;; which is faster.
13069         (condition-case ()
13070             (let (group max)
13071               (while (not (eobp))
13072                 ;; group gets set to a symbol interned in the hash table
13073                 ;; (what a hack!!)
13074                 (setq group (let ((obarray hashtb)) (read cur)))
13075                 (setq max (read cur))
13076                 (set group (cons (read cur) max))
13077                 (forward-line 1)))
13078           (error 
13079            (progn (ding) (message "Possible error in active file."))))))))
13080
13081 (defun gnus-read-newsrc-file (&optional force)
13082   "Read startup file.
13083 If FORCE is non-nil, the .newsrc file is read."
13084   (setq gnus-current-startup-file (gnus-make-newsrc-file gnus-startup-file))
13085   ;; Reset variables that might be defined in the .newsrc.eld file.
13086   (let ((variables gnus-variable-list))
13087     (while variables
13088       (set (car variables) nil)
13089       (setq variables (cdr variables))))
13090   (let* ((newsrc-file gnus-current-startup-file)
13091          (quick-file (concat newsrc-file ".el")))
13092     (save-excursion
13093       ;; We always load the .newsrc.eld file. If always contains
13094       ;; much information that can not be gotten from the .newsrc
13095       ;; file (ticked articles, killed groups, foreign methods, etc.)
13096       (gnus-read-newsrc-el-file quick-file)
13097  
13098       (if (or force
13099               (and (file-newer-than-file-p newsrc-file quick-file)
13100                    (file-newer-than-file-p newsrc-file 
13101                                            (concat quick-file "d")))
13102               (not gnus-newsrc-assoc))
13103           ;; We read the .newsrc file. Note that if there if a
13104           ;; .newsrc.eld file exists, it has already been read, and
13105           ;; the `gnus-newsrc-hashtb' has been created. While reading
13106           ;; the .newsrc file, Gnus will only use the information it
13107           ;; can find there for changing the data already read -
13108           ;; ie. reading the .newsrc file will not trash the data
13109           ;; already read (except for read articles).
13110           (save-excursion
13111             (message "Reading %s..." newsrc-file)
13112             (set-buffer (find-file-noselect newsrc-file))
13113             (buffer-disable-undo (current-buffer))
13114             (gnus-newsrc-to-gnus-format)
13115             (kill-buffer (current-buffer))
13116             (message "Reading %s... done" newsrc-file)))
13117       (and gnus-use-dribble-file (gnus-dribble-read-file)))))
13118
13119 (defun gnus-read-newsrc-el-file (file)
13120   (let ((ding-file (concat file "d")))
13121     ;; We always, always read the .eld file.
13122     (message "Reading %s..." ding-file)
13123     (condition-case nil
13124         (load ding-file t t t)
13125       (error nil))
13126     (gnus-uncompress-newsrc-assoc)
13127     (gnus-make-hashtable-from-newsrc-alist)
13128     (if (not (file-newer-than-file-p file ding-file))
13129         ()
13130       ;; Old format quick file
13131       (message "Reading %s..." file)
13132       ;; The .el file is newer than the .eld file, so we read that one
13133       ;; as well. 
13134       (gnus-read-old-newsrc-el-file file))))
13135
13136 ;; Parse the old-style quick startup file
13137 (defun gnus-read-old-newsrc-el-file (file)
13138   (let (newsrc killed marked group g m len info)
13139     (prog1
13140         (let (gnus-killed-assoc gnus-marked-assoc gnus-newsrc-assoc)
13141           (prog1
13142               (condition-case nil
13143                   (load file t t t)
13144                 (error nil))
13145             (setq newsrc gnus-newsrc-assoc
13146                   killed gnus-killed-assoc
13147                   marked gnus-marked-assoc)))
13148       (setq gnus-newsrc-assoc nil)
13149       (while newsrc
13150         (setq group (car newsrc))
13151         (let ((info (nth 2 (gnus-gethash (car group) gnus-newsrc-hashtb))))
13152           (if info
13153               (progn
13154                 (setcar (nthcdr 2 info) (cdr (cdr group)))
13155                 (setcar (cdr info) (if (nth 1 group) 3 6))
13156                 (setq gnus-newsrc-assoc (cons info gnus-newsrc-assoc)))
13157             (setq gnus-newsrc-assoc
13158                   (cons 
13159                    (setq info
13160                          (list (car group)
13161                                (if (nth 1 group) 3 6) (cdr (cdr group))))
13162                    gnus-newsrc-assoc)))
13163           (if (setq m (assoc (car group) marked))
13164             (setcdr (cdr (cdr info)) (cons (list (cons 'tick (cdr m))) nil))))
13165         (setq newsrc (cdr newsrc)))
13166       (setq newsrc killed)
13167       (while newsrc
13168         (setcar newsrc (car (car newsrc)))
13169         (setq newsrc (cdr newsrc)))
13170       (setq gnus-killed-list killed))
13171     (setq gnus-newsrc-assoc (nreverse gnus-newsrc-assoc))
13172     (gnus-make-hashtable-from-newsrc-alist)))
13173       
13174 (defun gnus-make-newsrc-file (file)
13175   "Make server dependent file name by catenating FILE and server host name."
13176   (let* ((file (expand-file-name file nil))
13177          (real-file (concat file "-" (nth 1 gnus-select-method))))
13178     (if (file-exists-p real-file)
13179         real-file file)))
13180
13181 (defun gnus-uncompress-newsrc-assoc ()
13182   ;; Uncompress all lists of marked articles in the newsrc assoc.
13183   (let ((newsrc gnus-newsrc-assoc)
13184         marked)
13185     (while newsrc
13186       (if (not (setq marked (nth 3 (car newsrc))))
13187           ()
13188         (while marked
13189           (or (eq 'score (car (car marked)))
13190               (eq 'bookmark (car (car marked)))
13191               (eq 'killed (car (car marked)))
13192               (setcdr (car marked) (gnus-uncompress-range (cdr (car marked)))))
13193           (setq marked (cdr marked))))
13194       (setq newsrc (cdr newsrc)))))
13195
13196 (defun gnus-compress-newsrc-assoc ()
13197   ;; Compress all lists of marked articles in the newsrc assoc.
13198   (let ((newsrc gnus-newsrc-assoc)
13199         marked)
13200     (while newsrc
13201       (if (not (setq marked (nth 3 (car newsrc))))
13202           ()
13203         (while marked
13204           (or (eq 'score (car (car marked)))
13205               (eq 'bookmark (car (car marked)))
13206               (eq 'killed (car (car marked)))
13207               (setcdr (car marked) 
13208                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
13209           (setq marked (cdr marked))))
13210       (setq newsrc (cdr newsrc)))))
13211
13212 ;; jwz: rewrote this function to be much more efficient, and not be
13213 ;; subject to regexp overflow errors when it encounters very long
13214 ;; lines -- the old behavior was to blow off the rest of the *file*
13215 ;; when a line was encountered that was too long to match!!  Now it
13216 ;; uses only simple looking-at calls, and doesn't create as many
13217 ;; temporary strings.  It also now handles multiple consecutive
13218 ;; options lines (before it only handled the first.)
13219 ;; Tiny rewrite by lmi. 
13220 (defun gnus-newsrc-to-gnus-format ()
13221   "Parse current buffer as .newsrc file."
13222   ;; We have to re-initialize these variables (except for
13223   ;; gnus-killed-list) because the quick startup file may contain bogus
13224   ;; values.
13225   (setq gnus-newsrc-options nil)
13226   (setq gnus-newsrc-options-n nil)
13227   (gnus-parse-options-lines)
13228   (gnus-parse-newsrc-body))
13229
13230 (defun gnus-parse-options-lines ()
13231   ;; newsrc.5 seems to indicate that the options line can come anywhere
13232   ;; in the file, and that there can be any number of them:
13233   ;;
13234   ;;       An  options  line  starts  with  the  word  options (left-
13235   ;;       justified).  Then there are the list of  options  just  as
13236   ;;       they would be on the readnews command line.  For instance:
13237   ;;
13238   ;;       options -n all !net.sf-lovers !mod.human-nets -r
13239   ;;       options -c -r
13240   ;;
13241   ;;       A string of lines beginning with a space or tab after  the
13242   ;;       initial  options  line  will  be  considered  continuation
13243   ;;       lines.
13244   ;;
13245   ;; For now, we only accept it at the beginning of the file.
13246
13247   (goto-char (point-min))
13248   (skip-chars-forward " \t\n")
13249   (setq gnus-newsrc-options nil)
13250   (while (looking-at "^options[ \t]*\\(.*\\)\n")
13251     ;; handle consecutive options lines
13252     (setq gnus-newsrc-options (concat gnus-newsrc-options
13253                                       (if gnus-newsrc-options "\n\t")
13254                                       (buffer-substring (match-beginning 1)
13255                                                         (match-end 1))))
13256     (forward-line 1)
13257     (while (looking-at "[ \t]+\\(.*\\)\n")
13258       ;; handle subsequent continuation lines of this options line
13259       (setq gnus-newsrc-options (concat gnus-newsrc-options "\n\t"
13260                                         (buffer-substring (match-beginning 1)
13261                                                           (match-end 1))))
13262       (forward-line 1)))
13263   ;; Gather all "-n" options lines.
13264   (let ((start 0)
13265         (result nil))
13266     (if gnus-newsrc-options
13267         (while (and (string-match "^[ \t]*-n\\([^\n]*\\)$"
13268                                   gnus-newsrc-options
13269                                   start)
13270                     (setq start (match-end 0)))
13271           (setq result (concat result
13272                                (and result " ")
13273                                (substring gnus-newsrc-options
13274                                           (match-beginning 1)
13275                                           (match-end 1))))))
13276
13277     (and result (gnus-parse-n-options result))
13278     nil))
13279
13280 (defun gnus-parse-newsrc-body ()
13281   ;; Point has been positioned after the options lines.  We shouldn't
13282   ;; see any more in here.
13283
13284   (let ((subscribe nil)
13285         (read-list nil)
13286         (line (1+ (count-lines (point-min) (point))))
13287         newsgroup
13288         p p2)
13289     (save-restriction
13290       (skip-chars-forward " \t")
13291       (while (not (eobp))
13292         (cond
13293          ((= (following-char) ?\n)
13294           ;; skip blank lines
13295           nil)
13296          (t
13297           (setq p (point))
13298           (skip-chars-forward "^:!\n")
13299           (if (= (following-char) ?\n)
13300               (error "line %d is unparsable in %s" line (buffer-name)))
13301           (setq p2 (point))
13302           (skip-chars-backward " \t")
13303
13304           ;; #### note: we could avoid consing a string here by binding obarray
13305           ;; and reading the newsgroup directly into the gnus-newsrc-hashtb,
13306           ;; then setq'ing newsgroup to symbol-name of that, like we do in
13307           ;; gnus-active-to-gnus-format.
13308           (setq newsgroup (buffer-substring p (point)))
13309           (goto-char p2)
13310
13311           (setq subscribe (= (following-char) ?:))
13312           (setq read-list nil)
13313
13314           (forward-char 1)              ; after : or !
13315           (skip-chars-forward " \t")
13316           (while (not (= (following-char) ?\n))
13317             (skip-chars-forward " \t")
13318             (or
13319              (and (cond
13320                    ((looking-at "\\([0-9]+\\)-\\([0-9]+\\)") ; a range
13321                     (setq read-list
13322                           (cons
13323                            (cons
13324                             (progn
13325                               ;; faster that buffer-substring/string-to-int
13326                               (narrow-to-region (point-min) (match-end 1))
13327                               (read (current-buffer)))
13328                             (progn
13329                               (narrow-to-region (point-min) (match-end 2))
13330                               (forward-char) ; skip over "-"
13331                               (prog1
13332                                   (read (current-buffer))
13333                                 (widen))))
13334                            read-list))
13335                     t)
13336                    ((looking-at "[0-9]+")
13337                     ;; faster that buffer-substring/string-to-int
13338                     (narrow-to-region (point-min) (match-end 0))
13339                     (setq p (read (current-buffer)))
13340                     (widen)
13341                     (setq read-list (cons (cons p p) read-list))
13342                     t)
13343                    (t
13344                     ;; bogus chars in ranges
13345                     nil))
13346                   (progn
13347                     (goto-char (match-end 0))
13348                     (skip-chars-forward " \t")
13349                     (cond ((= (following-char) ?,)
13350                            (forward-char 1)
13351                            t)
13352                           ((= (following-char) ?\n)
13353                            t)
13354                           (t
13355                            ;; bogus char after range
13356                            nil))))
13357              ;; if we get here, the parse failed
13358              (progn
13359                (end-of-line)            ; give up on this line
13360                (ding)
13361                (message "Ignoring bogus line %d for %s in %s"
13362                         line newsgroup (buffer-name))
13363                (sleep-for 1))))
13364           ;; We have already read .newsrc.eld, so we gently update the
13365           ;; data in the hash table with the information we have just
13366           ;; read. 
13367           (let ((info (nth 2 (gnus-gethash newsgroup gnus-newsrc-hashtb))))
13368             (if info
13369                 (progn
13370                   (setcar (nthcdr 2 info) (nreverse read-list))
13371                   (setcar (cdr info) (if subscribe 3 (if read-list 6 7))))
13372               (setq gnus-newsrc-assoc
13373                     (cons (list newsgroup (if subscribe 3 (if read-list 6 7))
13374                                 (nreverse read-list))
13375                           gnus-newsrc-assoc))))))
13376         (setq line (1+ line))
13377         (forward-line 1))))
13378   (setq gnus-newsrc-assoc (cdr gnus-newsrc-assoc))
13379   (gnus-make-hashtable-from-newsrc-alist)
13380   nil)
13381
13382 (defun gnus-parse-n-options (options)
13383   "Parse -n NEWSGROUPS options and return a cons of YES and NO regexps."
13384   (let (yes no yes-or-no out newsgroup)
13385     ;; Parse each newsgroup description such as "comp.all".  Commas
13386     ;; and white spaces can be a newsgroup separator.
13387     (while
13388         (string-match "^[ \t\n,]*\\(!?\\)\\([^- \t\n,][^ \t\n,]*\\)" options)
13389       (setq yes-or-no
13390             (substring options (match-beginning 1) (match-end 1)))
13391       (setq newsgroup
13392             (regexp-quote
13393              (substring options
13394                         (match-beginning 2) (match-end 2))))
13395       (setq options (substring options (match-end 2)))
13396       ;; Rewrite "all" to ".+" not ".*".  ".+" requires at least one
13397       ;; character.
13398       (while (string-match "\\(^\\|\\\\[.]\\)all\\(\\\\[.]\\|$\\)" newsgroup)
13399         (setq newsgroup
13400               (concat (substring newsgroup 0 (match-end 1))
13401                       ".+"
13402                       (substring newsgroup (match-beginning 2)))))
13403       ;; It is yes or no.
13404       (setq out (cons (cons (if (string= yes-or-no "!") 'ignore 'subscribe)
13405                             (concat "^" newsgroup "$")) 
13406                       out)))
13407     (setq gnus-newsrc-options-n out)))
13408
13409 (defun gnus-save-newsrc-file ()
13410   "Save .newsrc file."
13411   ;; Note: We cannot save .newsrc file if all newsgroups are removed
13412   ;; from the variable gnus-newsrc-assoc.
13413   (and (or gnus-newsrc-assoc gnus-killed-list)
13414        gnus-current-startup-file
13415        (save-excursion
13416          (if (or (not gnus-dribble-buffer)
13417                  (not (buffer-name gnus-dribble-buffer))
13418                  (zerop (save-excursion
13419                           (set-buffer gnus-dribble-buffer)
13420                           (buffer-size))))
13421              (message "(No changes need to be saved)")
13422            (if gnus-save-newsrc-file
13423                (let ((make-backup-files t)
13424                      (version-control nil)
13425                      (require-final-newline t)) ;Don't ask even if requested.
13426                  (message "Saving %s..." gnus-current-startup-file)
13427                  ;; Make backup file of master newsrc.
13428                  ;; You can stop or change version control of backup file.
13429                  ;; Suggested by jason@violet.berkeley.edu.
13430                  (run-hooks 'gnus-save-newsrc-hook)
13431                  (gnus-gnus-to-newsrc-format)
13432                  (message "Saving %s... done" gnus-current-startup-file)))
13433            ;; Quickly loadable .newsrc.
13434            (set-buffer (get-buffer-create " *Gnus-newsrc*"))
13435            (gnus-add-current-to-buffer-list)
13436            (buffer-disable-undo (current-buffer))
13437            (erase-buffer)
13438            (message "Saving %s.eld..." gnus-current-startup-file)
13439            (gnus-gnus-to-quick-newsrc-format)
13440            (let ((make-backup-files nil)
13441                  (version-control nil)
13442                  (require-final-newline t)) ;Don't ask even if requested.
13443              (write-region 1 (point-max) 
13444                            (concat gnus-current-startup-file ".eld") 
13445                            nil 'nomesg))
13446            (kill-buffer (current-buffer))
13447            (message "Saving %s.eld... done" gnus-current-startup-file)
13448            (gnus-dribble-delete-file)))))
13449
13450 (defun gnus-gnus-to-quick-newsrc-format ()
13451   "Insert Gnus variables such as gnus-newsrc-assoc in lisp format."
13452   (insert ";; (ding) Gnus startup file.\n")
13453   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
13454   (insert ";; to read .newsrc.\n")
13455   (let ((variables gnus-variable-list)
13456         (gnus-newsrc-assoc (cdr gnus-newsrc-assoc))
13457         variable)
13458     ;; insert lisp expressions.
13459     (gnus-compress-newsrc-assoc)
13460     (while variables
13461       (setq variable (car variables))
13462       (and (boundp variable)
13463            (symbol-value variable)
13464            (or gnus-save-killed-list (not (eq variable 'gnus-killed-list)))
13465            (insert "(setq " (symbol-name variable) " '"
13466                    (prin1-to-string (symbol-value variable))
13467                    ")\n"))
13468       (setq variables (cdr variables)))
13469     (gnus-uncompress-newsrc-assoc)))
13470
13471
13472 (defun gnus-gnus-to-newsrc-format ()
13473   ;; Generate and save the .newsrc file.
13474   (let ((newsrc (cdr gnus-newsrc-assoc))
13475         info ranges range)
13476     (save-excursion
13477       (set-buffer (create-file-buffer gnus-startup-file))
13478       (buffer-disable-undo (current-buffer))
13479       (erase-buffer)
13480       ;; Write options.
13481       (if gnus-newsrc-options (insert "options " gnus-newsrc-options "\n"))
13482       ;; Write subscribed and unsubscribed.
13483       (while newsrc
13484         (setq info (car newsrc))
13485         (if (not (nth 4 info))          ;Don't write foreign groups to .newsrc.
13486             (progn
13487               (insert (car info) (if (>= (nth 1 info) 6) "!" ":"))
13488               (if (setq ranges (nth 2 info))
13489                   (progn
13490                     (insert " ")
13491                     (if (not (listp (cdr ranges)))
13492                         (if (= (car ranges) (cdr ranges))
13493                             (insert (int-to-string (car ranges)))
13494                           (insert (int-to-string (car ranges)) "-" 
13495                                   (int-to-string (cdr ranges))))
13496                       (while ranges
13497                         (setq range (car ranges)
13498                               ranges (cdr ranges))
13499                         (if (or (atom range) (= (car range) (cdr range)))
13500                             (insert (int-to-string 
13501                                      (or (and (atom range) range) 
13502                                          (car range))))
13503                           (insert (int-to-string (car range)) "-"
13504                                   (int-to-string (cdr range))))
13505                         (if ranges (insert ","))))))
13506               (insert "\n")))
13507         (setq newsrc (cdr newsrc)))
13508       (write-region 1 (point-max) gnus-current-startup-file nil 'nomesg)
13509       (kill-buffer (current-buffer)))))
13510
13511 (defun gnus-read-descriptions-file ()
13512   (message "Reading descriptions file...")
13513   (if (not (gnus-request-list-newsgroups gnus-select-method))
13514       (progn
13515         (message "Couldn't read newsgroups descriptions")
13516         nil)
13517     (let (group)
13518       (setq gnus-description-hashtb 
13519             (gnus-make-hashtable (length gnus-active-hashtb)))
13520       (save-excursion
13521         (save-restriction
13522           (set-buffer nntp-server-buffer)
13523           (goto-char (point-min))
13524           (delete-non-matching-lines "^[-\\._+A-Za-z0-9]+[ \t]")
13525           (goto-char (point-min))
13526           (if (or (search-forward "\n.\n" nil t)
13527                   (goto-char (point-max)))
13528               (progn
13529                 (beginning-of-line)
13530                 (narrow-to-region (point-min) (point))))
13531           (goto-char (point-min))
13532           (while (not (eobp))
13533             (setq group (let ((obarray gnus-description-hashtb))
13534                           (read (current-buffer))))
13535             (skip-chars-forward " \t")
13536             (set group (buffer-substring 
13537                         (point) (save-excursion (end-of-line) (point))))
13538             (forward-line 1))))
13539       (message "Reading descriptions file...done")
13540       t)))
13541
13542 (defun gnus-group-get-description (group)
13543   ;; Get the description of a group by sending XGTITLE to the server.
13544   (and (gnus-request-group-description group)
13545        (save-excursion
13546          (set-buffer nntp-server-buffer)
13547          (goto-char (point-min))
13548          (and (looking-at "[^ \t]+[ \t]+\\(.*\\)")
13549               (buffer-substring (match-beginning 1) (match-end 1))))))
13550
13551 (provide 'gnus)
13552
13553 ;;; gnus.el ends here