*** 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 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, and the optional
55 third element is the \"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-secondary-select-methods nil
80   "A list of secondary methods that will be used for reading news.")
81
82 (defvar gnus-default-nntp-server nil
83   "Specify a default NNTP server.
84 This variable should be defined in paths.el, and should never be set
85 by the user.
86 If you want to change servers, you should use `gnus-select-method'.
87 See the documentation to that variable.")
88
89 (defvar gnus-secondary-servers nil
90   "List of NNTP servers that the user can choose between interactively.
91 To make Gnus query you for a server, you have to give `gnus' a
92 non-numeric prefix - `C-u M-x gnus', in short.")
93
94 (defvar gnus-nntp-server nil
95   "*The name of the host running the NNTP server.
96 This variable is semi-obsolete. Use the `gnus-select-method'
97 variable instead.")
98
99 (defvar gnus-nntp-service "nntp"
100   "NNTP service name (\"nntp\" or 119).
101 This is an obsolete variable, which is scarcely used. If you use an
102 nntp server for your newsgroup and want to change the port number
103 used to 899, you would say something along these lines:
104
105 (setq gnus-select-method '(nntp \"my.nntp.server\" 899))")
106
107 (defvar gnus-startup-file "~/.newsrc"
108   "Your `.newsrc' file.  Use `.newsrc-SERVER' instead if it exists.")
109
110 (defvar gnus-signature-file "~/.signature"
111   "Your signature file.
112 If the variable is a string that doesn't correspond to a file, the
113 string itself is inserted.")
114
115 (defvar gnus-signature-function nil
116   "A function that should return a signature file name.
117 The function will be called with the name of the newsgroup being
118 posted to.
119 If the function returns a string that doesn't correspond to a file, the
120 string itself is inserted.
121 If the function returns nil, the `gnus-signature-file' variable will
122 be used instead.")
123
124 (defvar gnus-init-file "~/.gnus"
125   "Your Gnus elisp startup file.
126 If a file with the .el or .elc suffixes exist, they will be read
127 instead.") 
128
129 (defvar gnus-default-subscribed-newsgroups nil
130   "This variable lists what newsgroups should be susbcribed the first time Gnus is used.
131 It should be a list of strings.
132 If it is `t', Gnus will not do anything special the first time it is
133 started; it'll just use the normal newsgroups subscription methods.")
134
135 (defconst gnus-backup-default-subscribed-newsgroups 
136   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
137   "Default default new newsgroups the first time Gnus is run.")
138
139 (defvar gnus-post-prepare-function nil
140   "Function that is run after a post buffer has been prepared.
141 It is called with the name of the newsgroup that is posted to. It
142 might be use, for instance, for inserting signatures based on the
143 newsgroup name. (In that case, `gnus-signature-file' and
144 `mail-signature' should both be set to nil).")
145
146 (defvar gnus-use-cross-reference t
147   "Non-nil means that cross referenced articles will be marked as read.
148 If nil, ignore cross references.  If t, mark articles as read in
149 subscribed newsgroups. If neither t nor nil, mark as read in all
150 newsgroups.") 
151
152 (defvar gnus-use-dribble-file t
153   "Non-nil means that Gnus will use a dribble file to store user updates.
154 If Emacs should crash without saving the .newsrc files, complete
155 information can be restored from the dribble file.")
156
157 (defvar gnus-use-followup-to 'use
158   "Specifies what to do with Followup-To header.
159 If nil, ignore the header. If it is t, use its value, but ignore 
160 `poster'. If it is neither nil nor t, always use the value.")
161
162 (defvar gnus-followup-to-function nil
163   "A variable that contains a function that returns a followup address.
164 The function will be called in the buffer of the article that is being
165 followed up. The buffer will be narrowed to the headers of the
166 article. To pick header headers, one might use `mail-fetch-field'.  The
167 function will be called with the name of the current newsgroup as the
168 argument.
169
170 Here's an example `gnus-followup-to-function':
171
172 (setq gnus-followup-to-function
173       (lambda (group)
174         (cond ((string= group \"mail.list\")
175                (or (mail-fetch-field \"sender\") 
176                    (mail-fetch-field \"from\")))
177               (t
178                (or (mail-fetch-field \"reply-to\") 
179                    (mail-fetch-field \"from\"))))))")
180
181 (defvar gnus-reply-to-function nil
182   "A variable that contains a function that returns a reply address.
183 See the `gnus-followup-to-function' variable for an explanation of how
184 this variable is used.")
185
186 (defvar gnus-large-newsgroup 200
187   "The number of articles which indicates a large newsgroup.
188 If the number of articles in a newsgroup is greater than the value,
189 confirmation is required for selecting the newsgroup.")
190
191 (defvar gnus-author-copy (getenv "AUTHORCOPY")
192   "Name of the file the article will be saved before it is posted using the FCC header.
193 Initialized from the AUTHORCOPY environment variable.
194
195 Articles are saved using a function specified by the the variable
196 `gnus-author-copy-saver' (`rmail-output' is default) if a file name is
197 given.  Instead, if the first character of the name is `|', the
198 contents of the article is piped out to the named program. It is
199 possible to save an article in an MH folder as follows:
200
201 \(setq gnus-author-copy \"|/usr/local/lib/mh/rcvstore +Article\")")
202
203 (defvar gnus-mail-self-blind nil
204   "Non-nil means insert BCC to self in messages to be sent.
205 This is done when the message is initialized,
206 so you can remove or alter the BCC header to override the default.")
207
208 (defvar gnus-author-copy-saver (function rmail-output)
209   "A function called with a file name to save an author copy to.
210 The default function is `rmail-output' which saves in Unix mailbox format.")
211
212 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
213   "Non-nil means that the default name of a file to save articles in is the newsgroup name.
214 If it's nil, the directory form of the newsgroup name is used instead.")
215
216 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
217   "Name of the directory articles will be saved in (default \"~/News\").
218 Initialized from the SAVEDIR environment variable.")
219
220 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
221   "Name of the directory where kill files will be stored (default \"~/News\").
222 Initialized from the SAVEDIR environment variable.")
223
224 (defvar gnus-kill-expiry-days 7
225   "*Number of days before unused kill file entries are expired.")
226
227 (defvar gnus-default-article-saver (function gnus-summary-save-in-rmail)
228   "A function to save articles in your favorite format.
229 The function must be interactively callable (in other words, it must
230 be an Emacs command).
231
232 Gnus provides the following functions:
233
234 * gnus-summary-save-in-rmail (Rmail format)
235 * gnus-summary-save-in-mail (Unix mail format)
236 * gnus-summary-save-in-folder (MH folder)
237 * gnus-summary-save-in-file (article format).")
238
239 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
240   "A function generating a file name to save articles in Rmail format.
241 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
242
243 (defvar gnus-mail-save-name (function gnus-plain-save-name)
244   "A function generating a file name to save articles in Unix mail format.
245 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
246
247 (defvar gnus-folder-save-name (function gnus-folder-save-name)
248   "A function generating a file name to save articles in MH folder.
249 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
250
251 (defvar gnus-file-save-name (function gnus-numeric-save-name)
252   "A function generating a file name to save articles in article format.
253 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
254
255 (defvar gnus-kill-file-name "KILL"
256   "Suffix of the kill files.")
257
258 (defvar gnus-fetch-old-headers nil
259   "Non-nil means that Gnus will try to build threads by grabbing old headers.
260 If an unread article in the group refers to an older, already read (or
261 just marked as read) article, the old article will not normally be
262 displayed in the Summary buffer. If this variable is non-nil, Gnus
263 will attempt to grab the headers to the old articles, and thereby
264 build complete threads. `gnus-nov-is-evil' has to be nil if this is
265 to work.  If it has the value `some', only enough headers to connect
266 otherwise loose threads will be displayed.")
267
268 (defvar gnus-visual t
269   "*If non-nil, will do various highlighting.
270 If nil, no mouse highlight (or any other) will be performed. This
271 might speed up Gnus some when generating large group and summary
272 buffers.")
273
274 (defvar gnus-novice-user t
275   "*Non-nil means that you are a usenet novice.
276 If non-nil, verbose messages may be displayed and confirmations may be
277 required.")
278
279 (defvar gnus-expert-user nil
280   "*Non-nil means that you will never be asked for confirmation about anything.
281 And that means *anything*.")
282
283 (defvar gnus-keep-same-level nil
284   "Non-nil means that the next newsgroup after the current will be on the same level.
285 When you type, for instance, `n' after reading the last article in the
286 current newsgroup, you will go to the next newsgroup. If this variable
287 is nil, the next newsgroup will be the next from the group
288 buffer. 
289 If this variable is non-nil, Gnus will either put you in the
290 next newsgroup with the same level, or, if no such newsgroup is
291 available, the next newsgroup with the lowest possible level higher
292 than the current level.
293 If this variable is `best', Gnus will make the next newsgroup the one
294 with the best level.")
295
296 (defvar gnus-summary-make-false-root 'adopt
297   "nil means that Gnus won't gather loose threads.
298 If the root of a thread has expired or been read in a previous
299 session, the information necessary to build a complete thread has been
300 lost. Instead of having many small sub-threads from this original thread
301 scattered all over the summary buffer, Gnus can gather them. 
302
303 If non-nil, Gnus will try to gather all loose sub-threads from an
304 original thread into one large thread.
305
306 If this variable is non-nil, it should be one of `none', `adopt',
307 `dummy' or `empty'.
308
309 If this variable is `none', Gnus will not make a false root, but just
310 present the sub-threads after another.
311 If this variable is `dummy', Gnus will create a dummy root that will
312 have all the sub-threads as children.
313 If this variable is `adopt', Gnus will make one of the \"children\"
314 the parent and mark all the step-children as such.
315 If this variable is `empty', the \"children\" are printed with empty
316 subject fields.")
317
318 (defvar gnus-summary-gather-subject-limit nil
319   "*Maximum length of subject to compare when gathering loose threads.
320 Use nil to compare the whole subject.")
321
322 (defvar gnus-check-new-newsgroups t
323   "Non-nil means that Gnus will add new newsgroups at startup.
324 If this variable is `ask-server', Gnus will ask the server for new
325 groups since the last time it checked. This means that the killed list
326 is no longer necessary, so you could set `gnus-save-killed-list' to
327 nil. 
328 A variant is to have this variable be a list of select methods. Then
329 Gnus will use the `ask-server' method on all these select methods to
330 query for new groups from all those servers.
331
332 Eg.
333   (setq gnus-check-new-newsgroups 
334         '((nntp \"some.server\") (nntp \"other.server\")))
335
336 If this variable is nil, then you have to tell Gnus explicitly to
337 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
338
339 (defvar gnus-check-bogus-newsgroups nil
340   "Non-nil means that Gnus will check and remove bogus newsgroup at startup.
341 If this variable is nil, then you have to tell Gnus explicitly to
342 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
343
344 (defvar gnus-read-active-file t
345   "Non-nil means that Gnus will read the entire active file at startup.
346 If this variable is nil, Gnus will only read parts of the active file.")
347
348 (defvar gnus-activate-foreign-newsgroups nil
349   "If nil, Gnus will not check foreign newsgroups at startup.
350 If it is non-nil, it should be a number between one and nine. Foreign
351 newsgroups that have a level lower or equal to this number will be
352 activated on startup. For instance, if you want to active all
353 subscribed newsgroups, but not the rest, you'd set this variable to 5.
354
355 If you subscribe to lots of newsgroups from different servers, startup
356 might take a while. By setting this variable to nil, you'll save time,
357 but you won't be told how many unread articles there are in the
358 newsgroups.")
359
360 (defvar gnus-save-newsrc-file t
361   "Non-nil means that Gnus will save a .newsrc file.
362 Gnus always saves its own startup file, which is called \".newsrc.el\".
363 The file called \".newsrc\" is in a format that can be readily
364 understood by other newsreaders. If you don't plan on using other
365 newsreaders, set this variable to nil to save some time on exit.")
366
367 (defvar gnus-save-killed-list t
368   "If non-nil, save the list of killed groups to the startup file.
369 This will save both time (when starting and quitting) and space (on
370 disk), but it will also mean that Gnus has no record of what
371 newsgroups are new or old, so the automatic new newsgroups
372 subscription methods become meaningless. You should always set
373 `gnus-check-new-newsgroups' to nil if you set this variable to nil.") 
374
375 (defvar gnus-interactive-catchup t
376   "Require your confirmation when catching up a newsgroup if non-nil.")
377
378 (defvar gnus-interactive-post t
379   "Group and subject will be asked for if non-nil.")
380
381 (defvar gnus-interactive-exit t
382   "Require your confirmation when exiting Gnus if non-nil.")
383
384 (defvar gnus-kill-killed nil
385   "If non-nil, Gnus will apply kill files to already killd articles.
386 If it is nil, Gnus will never apply kill files to articles that have
387 already been through the scoring process, which might very well save lots
388 of time.")
389
390 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
391 (defvar gnus-summary-same-subject ""
392   "String indicating that the current article has the same subject as the previous.")
393
394 (defvar gnus-score-interactive-default-score 1000
395   "Scoring commands will raise/lower with this number as the default.")
396
397 (defvar gnus-summary-default-score 0
398   "Default article score level.
399 If this variable is nil, score levels will not be used.")
400
401 (defvar gnus-user-login-name nil
402   "The login name of the user.
403 Got from the function `user-login-name' if undefined.")
404
405 (defvar gnus-user-full-name nil
406   "The full name of the user.
407 Got from the NAME environment variable if undefined.")
408
409 (defvar gnus-show-mime nil
410   "*Show MIME message if non-nil.")
411
412 (defvar gnus-show-threads t
413   "*Show conversation threads in summary mode if non-nil.")
414
415 (defvar gnus-thread-hide-subtree nil
416   "Non-nil means hide thread subtrees initially.
417 If non-nil, you have to run the command `gnus-summary-show-thread' by
418 hand or by using `gnus-select-article-hook' to show hidden threads.")
419
420 (defvar gnus-thread-hide-killed t
421   "Non-nil means hide killed thread subtrees automatically.")
422
423 (defvar gnus-thread-ignore-subject nil
424   "Don't take care of subject differences, but only references if non-nil.
425 If it is non-nil, some commands work with subjects do not work properly.")
426
427 (defvar gnus-thread-indent-level 4
428   "Indentation of thread subtrees.")
429
430 ;; jwz: nuke newsgroups whose name is all digits - that means that
431 ;; some loser has let articles get into the root of the news spool,
432 ;; which is toxic. Lines beginning with whitespace also tend to be
433 ;; toxic.
434 (defvar gnus-ignored-newsgroups
435   (purecopy (mapconcat 'identity
436                        '("^to\\."       ; not "real" groups
437                          "^[0-9. \t]+ " ; all digits in name
438                          "[][\"#'()     ;\\]"   ; bogus characters
439                          )
440                        "\\|"))
441   "A regexp to match uninteresting newsgroups in the active file.
442 Any lines in the active file matching this regular expression are
443 removed from the newsgroup list before anything else is done to it,
444 thus making them effectively non-existant.")
445
446 (defvar gnus-ignored-headers
447   "^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:"
448   "All headers that match this regexp will be hidden.
449 Also see `gnus-visible-headers'.")
450
451 (defvar gnus-visible-headers "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:"
452   "All headers that do not match this regexp will be hidden.
453 Also see `gnus-ignored-headers'.")
454
455 (defvar gnus-sorted-header-list
456   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:" 
457     "^Cc:" "^Date:" "^Organization:")
458   "This variable is a list of regular expressions.
459 If it is non-nil, headers that match the regular expressions will
460 be placed first in the article buffer in the sequence specified by
461 this list.")
462
463 (defvar gnus-required-headers
464   '(From Date Newsgroups Subject Message-ID Organization Lines X-Newsreader)
465   ;; changed by jwz because it's not so nice to do "Lines: 0" by default.
466   ;; and to remove Path, since it's incorrect for Gnus to try
467   ;; and generate that - it is the responsibility of inews or nntpd.
468   "All required headers for articles you post.
469 RFC977 and RFC1036 require From, Date, Newsgroups, Subject, Message-ID
470 and Path headers.  Organization, Lines and X-Newsreader are optional.
471 If you want Gnus not to insert some header, remove it from this
472 variable.")
473
474 (defvar gnus-show-all-headers nil
475   "*Show all headers of an article if non-nil.")
476
477 (defvar gnus-save-all-headers t
478   "*Save all headers of an article if non-nil.")
479
480 (defvar gnus-inhibit-startup-message nil
481   "The startup message will not be displayed if this function is non-nil.")
482
483 (defvar gnus-auto-extend-newsgroup t
484   "Extend visible articles to forward and backward if non-nil.")
485
486 (defvar gnus-auto-select-first t
487   "Select the first unread article automagically if non-nil.
488 If you want to prevent automatic selection of the first unread article
489 in some newsgroups, set the variable to nil in `gnus-select-group-hook'
490 or `gnus-apply-kill-hook'.")
491
492 (defvar gnus-auto-select-next t
493   "Select the next newsgroup automagically if non-nil.
494 If the value is t and the next newsgroup is empty, Gnus will exit
495 summary mode and go back to group mode.  If the value is neither nil
496 nor t, Gnus will select the following unread newsgroup.  Especially, if
497 the value is the symbol `quietly', the next unread newsgroup will be
498 selected without any confirmations.")
499
500 (defvar gnus-auto-select-same nil
501   "Select the next article with the same subject automagically if non-nil.")
502
503 (defvar gnus-auto-center-summary t
504   "*Always center the current summary in Gnus summary window if non-nil.")
505
506 (defvar gnus-auto-mail-to-author nil
507   "Insert `To: author' of the article when following up if non-nil.
508 If this variable is `ask', the user will be prompted.
509 Mail is sent using the function specified by the variable
510 `gnus-mail-send-method'.")
511
512 (defvar gnus-break-pages t
513   "*Break an article into pages if non-nil.
514 Page delimiter is specified by the variable `gnus-page-delimiter'.")
515
516 (defvar gnus-page-delimiter "^\^L"
517   "Regexp describing line-beginnings that separate pages of news article.")
518
519 (defvar gnus-use-full-window t
520   "*Non-nil means to take up the entire screen of Emacs.")
521
522 (defvar gnus-window-configuration
523   '((summary (0 1 0))
524     (newsgroups (1 0 0))
525     (article (0 3 10)))
526   "Specify window configurations for each action.
527 The format of the variable is either a list of (ACTION (G S A)), where
528 G, S, and A are the relative height of group, summary, and article
529 windows, respectively, or a list of (ACTION FUNCTION), where FUNCTION
530 is a function that will be called with ACTION as an argument. ACTION
531 can be `summary', `newsgroups', or `article'.")
532
533 (defvar gnus-show-mime-method (function metamail-buffer)
534   "Function to process a MIME message.
535 The function is expected to process current buffer as a MIME message.")
536
537 (defvar gnus-mail-reply-method
538   (function gnus-mail-reply-using-mail)
539   "Function to compose reply mail.
540 The function `gnus-mail-reply-using-mail' uses usual sendmail mail
541 program.  The function `gnus-mail-reply-using-mhe' uses the MH-E mail
542 program.  You can use yet another program by customizing this variable.")
543
544 (defvar gnus-mail-forward-method
545   (function gnus-mail-forward-using-mail)
546   "Function to forward current message to another user.
547 The function `gnus-mail-reply-using-mail' uses usual sendmail mail
548 program.  You can use yet another program by customizing this variable.")
549
550 (defvar gnus-mail-other-window-method
551   (function gnus-mail-other-window-using-mail)
552   "Function to compose mail in other window.
553 The function `gnus-mail-other-window-using-mail' uses the usual sendmail
554 mail program.  The function `gnus-mail-other-window-using-mhe' uses the MH-E
555 mail program.  You can use yet another program by customizing this variable.")
556
557 (defvar gnus-mail-send-method send-mail-function
558   "Function to mail a message too which is being posted as an article.
559 The message must have To or Cc header.  The default is copied from
560 the variable `send-mail-function'.")
561
562 (defvar gnus-subscribe-newsgroup-method
563   (function gnus-subscribe-zombies)
564   "Function called with a newsgroup name when new newsgroup is found.
565 The function `gnus-subscribe-randomly' inserts a new newsgroup a the
566 beginning of newsgroups.  The function `gnus-subscribe-alphabetically'
567 inserts it in strict alphabetic order.  The function
568 `gnus-subscribe-hierarchically' inserts it in hierarchical newsgroup
569 order.  The function `gnus-subscribe-interactively' asks for your decision.")
570
571 ;; Suggested by a bug report by Hallvard B Furuseth
572 ;; <h.b.furuseth@usit.uio.no>. 
573 (defvar gnus-subscribe-options-newsgroup-method
574   (function gnus-subscribe-alphabetically)
575   "This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
576 If, for instance, you want to subscribe to all newsgroups in the
577 \"no\" and \"alt\" hierarchies, you'd put the following in your
578 .newsrc file:
579
580 options -n no.all alt.all
581
582 Gnus will the subscribe all new newsgroups in these hierarchies with
583 the subscription method in this variable.")
584
585 ;; Mark variables suggested by Thomas Michanek
586 ;; <Thomas.Michanek@telelogic.se>. 
587 (defvar gnus-unread-mark ? 
588   "Mark used for unread articles.")
589 (defvar gnus-ticked-mark ?!
590   "Mark used for ticked articles.")
591 (defvar gnus-dormant-mark ??
592   "Mark used for dormant articles.")
593 (defvar gnus-dread-mark ?D
594   "Mark used for read articles.")
595 (defvar gnus-read-mark ?d
596   "Mark used for read articles.")
597 (defvar gnus-expirable-mark ?E
598   "Mark used for expirable articles.")
599 (defvar gnus-killed-mark ?K
600   "Mark used for killed articles.")
601 (defvar gnus-kill-file-mark ?X
602   "Mark used for articles killed by kill files.")
603 (defvar gnus-low-score-mark ?Y
604   "Mark used for articles with a low score.")
605 (defvar gnus-catchup-mark ?C
606   "Mark used for articles that are caught up.")
607 (defvar gnus-replied-mark ?R
608   "Mark used for articles that have been replied to.")
609 (defvar gnus-process-mark ?#
610   "Mark used for marking articles as processable.")
611 (defvar gnus-ancient-mark ?A
612   "Mark used for ancient articles.")
613 (defvar gnus-canceled-mark ?G
614   "Mark used for cancelled articles.")
615 (defvar gnus-score-over-mark ?+
616   "Score mark used for articles with high scores.")
617 (defvar gnus-score-below-mark ?-
618   "Score mark used for articles with low scores.")
619
620 (defvar gnus-view-pseudo-asynchronously nil
621   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
622
623 (defvar gnus-view-pseudos nil
624   "If `automatic', pseudo-articles will be viewed automatically.
625 If `not-confirm', pseudos will be viewed automatically, and the user
626 will not be asked to confirm the command.")
627
628 (defvar gnus-group-mode-hook nil
629   "A hook for Gnus group mode.")
630
631 (defvar gnus-summary-mode-hook nil
632   "A hook for Gnus summary mode.")
633
634 (defvar gnus-article-mode-hook nil
635   "A hook for Gnus article mode.")
636
637 (defvar gnus-kill-file-mode-hook nil
638   "A hook for Gnus KILL File mode.")
639
640 (defvar gnus-open-server-hook nil
641   "A hook called just before opening connection to news server.")
642
643 (defvar gnus-startup-hook nil
644   "A hook called at startup time.
645 This hook is called after Gnus is connected to the NNTP server. So, it
646 is possible to change the behavior of Gnus according to the selected
647 NNTP server.")
648
649 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
650   "A function that is called to set up the group buffer.
651 The function is called with three arguments: The first is a number;
652 all group with a level less or equal to that number should be listed,
653 if the second is non-nil, empty groups should also be displayed. If
654 the third is non-nil, it is a number. No groups with a level lower
655 than this number should be displayed.")
656
657 (defvar gnus-group-prepare-hook nil
658   "A hook called after the newsgroup list is created in the group buffer.
659 If you want to modify the group buffer, you can use this hook.")
660
661 (defvar gnus-summary-prepare-hook nil
662   "A hook called after summary list is created in the summary buffer.
663 If you want to modify the summary buffer, you can use this hook.")
664
665 (defvar gnus-article-prepare-hook nil
666   "A hook called after an article is prepared in the article buffer.
667 If you want to run a special decoding program like nkf, use this hook.")
668
669 (defvar gnus-article-display-hook nil
670   "A hook called after the article is displayed in the article buffer.
671 The hook is designed to change the contents of the article
672 buffer. Typical functions that this hook may contain are
673 `gnus-article-hide-headers' (hide selected headers),
674 `gnus-article-hide-signature' (hide signature) and
675 `gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
676 (add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
677 (add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
678
679 (defvar gnus-select-group-hook nil
680   "A hook called when a newsgroup is selected.
681
682 If you'd like to simplify subjects like the
683 `gnus-summary-next-same-subject' command does, you can use the
684 following hook:
685
686  (setq gnus-select-group-hook
687       (list
688         (lambda ()
689           (mapcar (lambda (header)
690                      (header-set-subject
691                       header
692                       (gnus-simplify-subject
693                        (header-subject header) 're-only)))
694                   gnus-newsgroup-headers))))
695 ")
696
697 (defvar gnus-select-article-hook
698   '(gnus-summary-show-thread)
699   "A hook called when an article is selected.
700 The default hook shows conversation thread subtrees of the selected
701 article automatically using `gnus-summary-show-thread'.")
702
703 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
704   "A hook called when a newsgroup is selected and summary list is prepared.
705 This hook is intended to apply a kill file to the selected newsgroup.
706 The function `gnus-apply-kill-file' is called by default.
707
708 Since a general kill file is too heavy to use only for a few
709 newsgroups, I recommend you to use a lighter hook function. For
710 example, if you'd like to apply a kill file to articles which contains
711 a string `rmgroup' in subject in newsgroup `control', you can use the
712 following hook:
713
714 \(setq gnus-apply-kill-hook
715       (list
716         (lambda ()
717           (cond ((string-match \"control\" gnus-newsgroup-name)
718                  (gnus-kill \"Subject\" \"rmgroup\")
719                  (gnus-expunge \"X\"))))))")
720
721 (defvar gnus-visual-mark-article-hook 
722   (list 'gnus-visual-highlight-selected-summary)
723   "Hook run after selecting an article in the summary buffer.
724 It is meant to be used for highlighting the article in some way. It is
725 not run if `gnus-visual' is nil.")
726
727 (defvar gnus-prepare-article-hook (list (function gnus-inews-insert-signature))
728   "A hook called after preparing body, but before preparing header headers.
729 The default hook (`gnus-inews-insert-signature') inserts a signature
730 file specified by the variable `gnus-signature-file'.")
731
732 (defvar gnus-inews-article-hook (list (function gnus-inews-do-fcc))
733   "A hook called before finally posting an article.
734 The default hook (`gnus-inews-do-fcc') does FCC processing (save article
735 to a file).")
736
737 (defvar gnus-inews-article-header-hook nil
738   "A hook called after inserting the headers in an article to be posted.
739 The hook is called from the *post-news* buffer, narrowed to the
740 headers.")
741
742 (defvar gnus-exit-group-hook nil
743   "A hook called when exiting (not quitting) summary mode.
744 If your machine is so slow that exiting from summary mode takes very
745 long time, set the variable `gnus-use-cross-reference' to nil. This
746 inhibits marking articles as read using cross-reference information.")
747
748 (defvar gnus-suspend-gnus-hook nil
749   "A hook called when suspending (not exiting) Gnus.")
750
751 (defvar gnus-exit-gnus-hook (list 'nntp-request-close)
752   "A hook called when exiting Gnus.")
753
754 (defvar gnus-save-newsrc-hook nil
755   "A hook called when saving the newsrc file.
756 This hook is called before saving the `.newsrc' file.")
757
758 (defvar gnus-auto-expirable-newsgroups nil
759   "All newsgroups that match this regexp will have all read articles automatically marked as expirable.")
760
761 (defvar gnus-subscribe-hierarchical-interactive nil
762   "If non-nil, Gnus will offer to subscribe hierarchically.
763 When a new hierarchy appears, Gnus will ask the user:
764
765 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
766
767 If the user pressed `d', Gnus will descend the hierarchy, `y' will
768 subscribe to all newsgroups in the hierarchy and `s' will skip this
769 hierarchy in its entirety.")
770
771 (defvar gnus-group-line-format "%M%S%5y: %(%g%)\n"
772   "Format of groups lines.
773 It works along the same lines as a normal formatting string,
774 with some simple extrensions.
775
776 %M    Only marked articles (character, \"*\" or \" \")
777 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
778 %L    Level of subscribedness (integer, 1-9)
779 %N    Number of unread articles (integer)
780 %I    Number of dormant articles (integer)
781 %i    Number of ticked and dormant (integer)
782 %T    Number of ticked articles (integer)
783 %R    Number of read articles (integer)
784 %t    Total number of articles (integer)
785 %y    Number of unread, unticked articles (integer)
786 %G    Group name (string)
787 %g    Qualified group name (string)
788 %D    Group description (string)
789 %s    Select method (string)
790 %o    Moderated group (char, \"m\")
791 %O    Moderated group (string, \"(m)\" or \"\")
792 %n    Select from where (string)
793 %z    A string that look like `<%s:%n>' if a foreign select method is used
794 %u    User defined specifier. The next character in the format string should
795       be a letter.  GNUS will call the function gnus-user-format-function-X,
796       where X is the letter following %u. The function will be passed the
797       current header as argument. The function should return a string, which
798       will be inserted into the summary just like information from any other
799       summary specifier.
800
801 Text between %( and %) will be highlighted with `gnus-mouse-face' when
802 the mouse point move inside the area.  There can only be one such area.
803
804 Note that this format specification is not always respected. For
805 reasons of efficiency, when listing killed groups, this specification
806 is ignored altogether. If the spec is changed considerably, your
807 output may end up looking strange when listing both alive and killed
808 groups.
809
810 If you use %o or %O, reading the active file will be slower and quite
811 a bit of extra memory will be used. %D will also worsen performance.
812 Also note that if you change the format specification to include any
813 of these specs, you must probably re-start Gnus to see them go into
814 effect.") 
815
816 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
817   "The format specification of the lines in the summary buffer.
818
819 It works along the same lines as a normal formatting string,
820 with some simple extensions.
821
822 %N   Article number, left padded with spaces (string)
823 %S   Subject (string)
824 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
825 %n   Name of the poster (string)
826 %A   Address of the poster (string)
827 %L   Number of lines in the article (integer)
828 %c   Number of characters in the article (integer)
829 %D   Date of the article (string)
830 %I   Indentation based on thread level (a string of spaces)
831 %T   A string with two possible values: 80 spaces if the article
832      is on thread level two or larger and 0 spaces on level one
833 %U   Status of this article (character, \"D\", \"K\", \"-\" or \" \") 
834 %[   Opening bracket (character, \"[\" or \"<\")
835 %]   Closing bracket (character, \"]\" or \">\")
836 %>   Spaces of length thread-level (string)
837 %<   Spaces of length (- 20 thread-level) (string)
838 %i   Article score (number)
839 %z   Article zcore (character)
840 %u   User defined specifier. The next character in the format string should
841      be a letter.  GNUS will call the function gnus-user-format-function-X,
842      where X is the letter following %u. The function will be passed the
843      current header as argument. The function should return a string, which
844      will be inserted into the summary just like information from any other
845      summary specifier.
846
847 Text between %( and %) will be highlighted with `gnus-mouse-face'
848 when the mouse point is placed inside the area.  There can only be one
849 such area.
850
851 The %U (status), %R (replied) and %z (zcore) specs have to be handled
852 with care. For reasons of efficiency, Gnus will compute what column
853 these characters will end up in, and "hard-code" that. This means that
854 it is illegal to have these specs after a variable-length spec. Well,
855 you might not be arrested, but your summary buffer will look strange,
856 which is bad enough.
857
858 The smart choice is to have these specs as for to the left as
859 possible. 
860
861 This restriction may disappear in later versions of Gnus.")
862
863 (defconst gnus-summary-dummy-line-format "*   :                          : %S\n"
864   "The format specification for the dummy roots in the summary buffer.
865 It works along the same lines as a normal formatting string,
866 with some simple extensions.
867
868 %S  The subject")
869
870 (defvar gnus-summary-mode-line-format "(ding) %G/%A %Z"
871   "The format specification for the summary mode line.")
872
873 (defvar gnus-article-mode-line-format "(ding) %G/%A %S"
874   "The format specification for the article mode line.")
875
876 (defconst gnus-group-mode-line-format "(ding) List of groups   {%M:%S}"
877   "The format specification for the group mode line.")
878
879
880 \f
881 ;; Site dependent variables. You have to define these variables in
882 ;;  site-init.el, default.el or your .emacs.
883
884 (defvar gnus-local-timezone nil
885   "Local time zone.
886 This value is used only if `current-time-zone' does not work in your Emacs.
887 It specifies the GMT offset, i.e. a decimal integer
888 of the form +-HHMM giving the hours and minutes ahead of (i.e. east of) GMT.
889 For example, +0900 should be used in Japan, since it is 9 hours ahead of GMT.
890
891 For backwards compatibility, it may also be a string like \"JST\",
892 but strings are obsolescent: you should use numeric offsets instead.")
893
894 (defvar gnus-local-domain nil
895   "Local domain name without a host name.
896 The DOMAINNAME environment variable is used instead if it is defined.
897 If the `system-name' function returns the full Internet name, there is
898 no need to set this variable.")
899
900 (defvar gnus-local-organization nil
901   "String with a description of what organization (if any) the user belongs to.
902 The ORGANIZATION environment variable is used instead if it is defined.
903 If this variable contains a function, this function will be called
904 with the current newsgroup name as the argument. The function should
905 return a string.
906 In any case, if the string (either in the variable, in the environment
907 variable, or returned by the function) is a file name, the contents of
908 this file will be used as the organization.")
909
910 (defvar gnus-use-generic-from nil
911   "If nil, the full host name will be the system name prepended to the domain name.
912 If this is a string, the full host name will be this string.
913 If this is non-nil, non-string, the domain name will be used as the
914 full host name.")
915
916 (defvar gnus-use-generic-path nil
917   "If nil, use the NNTP server name in the Path header.
918 If stringp, use this; if non-nil, use no host name (user name only).")
919
920 (defvar gnus-valid-select-methods
921   '(("nntp" post address) ("nnspool" post) ("nnvirtual" none virtual) 
922     ("nnmbox" mail respool) ("nnml" mail respool)
923     ("nnmh" mail respool) ("nndir" none) ("nndigest" none)
924     ("nndoc" none) ("nnbabyl" mail respool)
925     ("nnkiboze" none virtual))
926   "A list of valid select methods.
927 Each element in this list should be a list. The first element of these
928 lists should be a string with the name of the select method. The
929 other elements may be be the category of this method (ie. `post',
930 `mail', `none' or whatever) or other properties that this method has
931  (like being respoolable). 
932 If you implement a new select method, all you should have to change is
933 this variable. I think.")
934
935 (defvar gnus-updated-mode-lines '(group article summary)
936   "This variable is a list of buffers that should keep their mode lines updated.
937 The list may contain the symbols `group', `article' and `summary'. If
938 the corresponding symbol is present, Gnus will keep that mode line
939 updated with information that may be pertinent. 
940 If this variable is nil, screen refresh may be quicker.")
941
942 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
943 (defvar gnus-mouse-face 'highlight
944   "Face used for mouse highlighting in Gnus.
945 No mouse highlights will be done if `gnus-visual' is nil.")
946
947 (defvar gnus-visual-summary-update-hook 
948   (list 'gnus-visual-summary-highlight-line)
949   "A hook called when a summary line is changed.
950 The hook will not be called if `gnus-visual' is nil.
951
952 Point will be at the beginning of the line, and the following free
953 variables can be used for convenience:
954
955 score:   (gnus-summary-article-score)
956 default: gnus-summary-default-score
957 below:   gnus-summary-mark-below
958
959 The default hook `gnus-visual-summary-highlight-line' will highlight the line
960 according to the `gnus-visual-summary-highlight' variable.")
961
962 (defvar gnus-summary-mark-below nil
963   "Score below which articles automatically become marked.
964 This variable is local to each summary buffer and usually set in the
965 score file.")  
966
967 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
968   "List of functions used for thread roots in the summary buffer.
969
970 Each function takes two threads and return non-nil if the first thread
971 should be sorted before the other.  If you use more than one function,
972 list the function you want to act as the primary sort key last.
973
974 Functions you can use are:
975 - gnus-thread-sort-by-number
976 - gnus-thread-sort-by-author
977 - gnus-thread-sort-by-subject
978 - gnus-thread-sort-by-date
979 - gnus-thread-sort-by-score
980 - gnus-thread-sort-by-total-score (see `gnus-thread-score-function').
981
982 The two later only works on articles that have already been scored prior
983 to entering the newsgroup.")
984
985 (defvar gnus-thread-score-function '+
986   "Function used for calculating the total score of a thread.
987
988 The function is called with the scores of the article and each
989 subthread and should then return the score of the thread.
990
991 Some functions you can use are `+', `max', or `min'.")
992
993 (defvar gnus-score-find-score-files-function 'gnus-score-find-bnews
994   "Function used to find SCORE files.
995 The value should be a function that takes a group name as an argument,
996 and return a list of SCORE files that applies to that group.
997
998 Predefined values are:
999
1000 gnus-score-find-single: Only apply the groups own SCORE file.
1001 gnus-score-find-hierarchical: Also apply SCORE files from parent groups.
1002 gnus-score-find-bnews: Apply SCORE files whose names matches.
1003
1004 See the definition of the individual functions for more information.")
1005
1006 (defvar gnus-options-subscribe nil
1007   "All new groups matching this regexp will be subscribed.
1008 Note that this variable deals only with new newsgroups. This variable
1009 does not affect old newsgroups.")
1010
1011 (defvar gnus-options-not-subscribe nil
1012   "All new groups matching this regexp will be ignored.
1013 Note that this variable deals only with new newsgroups. This variable
1014 does not affect old (already subscribed) newsgroups.")
1015
1016 \f
1017 ;; Internal variables
1018
1019 ;; Avoid highlighting in kill files.
1020 (defvar gnus-summary-inhibit-highlight nil)
1021
1022 (defvar caesar-translate-table nil)
1023
1024 (defvar gnus-dribble-buffer nil)
1025 (defvar gnus-headers-retrieved-by nil)
1026
1027 (defvar gnus-article-reply nil)
1028 (defvar gnus-article-check-size nil)
1029
1030 (defvar gnus-score-file-list nil)
1031 (defvar gnus-score-alist nil
1032   "Alist containing score information.
1033 The keys can be symbols or strings.  The following symbols are defined. 
1034
1035 touched: If this alist has been modified.
1036 mark:    Automatically mark articles below this.
1037 expunge: Automatically expunge articles below this.
1038 files:   List of other SCORE files to load when loading this one.
1039 eval:    Sexp to be evaluated when the score file is loaded.
1040
1041 String entries have the form (HEADER (MATCH TYPE SCORE DATE) ...) 
1042 where HEADER is the header being scored, MATCH is the string we are
1043 looking for, TYPE is a flag indicating whether it should use regexp or
1044 substring matching, SCORE is the score to add and DATE is the date
1045 of the last succesful match.")
1046
1047 (defvar gnus-score-cache nil)
1048 ;; Alist containing the content of all loaded SCORE files.
1049 (defvar gnus-scores-lists nil)
1050 (defvar gnus-scores-articles nil)
1051 (defvar gnus-header-index nil)
1052 (defvar gnus-score-index nil)
1053
1054 (defvar gnus-newsgroup-dependencies nil)
1055
1056 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1057
1058 (defvar gnus-default-subscribe-level 2
1059   "Default subscription level.")
1060
1061 (defvar gnus-default-unsubscribe-level 6
1062   "Default unsubscription level.")
1063
1064 (defvar gnus-default-kill-level 9
1065   "Default kill level.")
1066
1067 (defconst gnus-group-line-format-alist
1068   (list (list ?M 'marked ?c)
1069         (list ?S 'subscribed ?c)
1070         (list ?L 'level ?d)
1071         (list ?N 'number ?s)
1072         (list ?I 'number-of-dormant ?d)
1073         (list ?T 'number-of-ticked ?d)
1074         (list ?R 'number-of-read ?s)
1075         (list ?t 'number-total ?d)
1076         (list ?y 'number-of-unread-unticked ?s)
1077         (list ?i 'number-of-ticked-and-dormant ?d)
1078         (list ?g 'group ?s)
1079         (list ?G 'qualified-group ?s)
1080         (list ?D 'newsgroup-description ?s)
1081         (list ?o 'moderated ?c)
1082         (list ?O 'moderated-string ?s)
1083         (list ?s 'news-server ?s)
1084         (list ?n 'news-method ?s)
1085         (list ?z 'news-method-string ?s)
1086         (list ?u 'user-defined ?s)))
1087
1088 (defconst gnus-summary-line-format-alist 
1089   (list (list ?N 'number ?s)
1090         (list ?S 'subject ?s)
1091         (list ?s 'subject-or-nil ?s)
1092         (list ?n 'name ?s)
1093         (list ?A 'address ?s)
1094         (list ?F 'from ?s)
1095         (list ?x (macroexpand '(header-xref header)) ?s)
1096         (list ?D (macroexpand '(header-date header)) ?s)
1097         (list ?M (macroexpand '(header-id header)) ?s)
1098         (list ?r (macroexpand '(header-references header)) ?s)
1099         (list ?c (macroexpand '(header-chars header)) ?d)
1100         (list ?L 'lines ?d)
1101         (list ?I 'indentation ?s)
1102         (list ?T '(if (< level 1) "" (make-string (frame-width) ? )) ?s)
1103         (list ?R 'replied ?c)
1104         (list ?\[ 'opening-bracket ?c)
1105         (list ?\] 'closing-bracket ?c)
1106         (list ?\> '(make-string level ? ) ?s)
1107         (list ?\< '(make-string (max 0 (- 20 level)) ? ) ?s)
1108         (list ?i 'score ?s)
1109         (list ?z 'score-char ?c)
1110         (list ?U 'unread ?c)
1111         (list ?u 'user-defined ?s))
1112   "An alist of format specifications that can appear in summary lines,
1113 and what variables they correspond with, along with the type of the
1114 variable (string, integer, character, etc).")
1115
1116 (defconst gnus-summary-dummy-line-format-alist
1117   (list (list ?S 'subject ?s)
1118         (list ?N 'number ?d)))
1119
1120 (defconst gnus-summary-mode-line-format-alist 
1121   (list (list ?G 'group-name ?s)
1122         (list ?A 'article-number ?d)
1123         (list ?Z 'unread-and-unselected ?s)
1124         (list ?V 'gnus-version ?s)
1125         (list ?U 'unread ?d)
1126         (list ?S 'subject ?s)
1127         (list ?u 'unselected ?d)))
1128
1129 (defconst gnus-group-mode-line-format-alist 
1130   (list (list ?S 'news-server ?s)
1131         (list ?M 'news-method ?s)))
1132
1133 (defvar gnus-have-read-active-file nil)
1134
1135 (defconst gnus-maintainer "Lars Magne Ingebrigtsen <larsi@ifi.uio.no>"
1136   "The mail address of the Gnus maintainer.")
1137
1138 (defconst gnus-version "(ding) Gnus v0.30"
1139   "Version number for this version of Gnus.")
1140
1141 (defvar gnus-info-nodes
1142   '((gnus-group-mode            "(gnus)Group Commands")
1143     (gnus-summary-mode          "(gnus)Summary Commands")
1144     (gnus-article-mode          "(gnus)Article Commands")
1145     (gnus-kill-file-mode        "(gnus)Kill File"))
1146   "Assoc list of major modes and related Info nodes.")
1147
1148 (defvar gnus-documentation-group-file "~/dgnus/lisp/doc.txt"
1149   "The location of the (ding) Gnus documentation group.")
1150
1151 (defvar gnus-group-buffer "*Group*")
1152 (defvar gnus-summary-buffer "*Summary*")
1153 (defvar gnus-article-buffer "*Article*")
1154
1155 (defvar gnus-buffer-list nil
1156   "Gnus buffers that should be killed when exiting.")
1157
1158 (defvar gnus-variable-list
1159   '(gnus-newsrc-options 
1160     gnus-newsrc-options-n-yes gnus-newsrc-options-n-no
1161     gnus-newsrc-last-checked-date
1162     gnus-newsrc-assoc gnus-killed-list gnus-zombie-list)
1163   "Gnus variables saved in the quick startup file.")
1164
1165 (defvar gnus-overload-functions
1166   '((news-inews gnus-inews-news "rnewspost")
1167     (caesar-region gnus-caesar-region "rnews"))
1168   "Functions overloaded by gnus.
1169 It is a list of `(original overload &optional file)'.")
1170
1171 (defvar gnus-newsrc-options nil
1172   "Options line in the .newsrc file.")
1173
1174 (defvar gnus-newsrc-options-n-yes nil
1175   "Regexp representing groups to be subscribed to unconditionally.")
1176
1177 (defvar gnus-newsrc-options-n-no nil
1178   "Regexp representing group to be ignored unconditionally.")
1179
1180 (defvar gnus-newsrc-last-checked-date nil
1181   "Date Gnus last asked server for new newsgroups.")
1182
1183 (defvar gnus-newsrc-assoc nil
1184   "Assoc list of read articles.
1185 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1186
1187 (defvar gnus-newsrc-hashtb nil
1188   "Hashtable of gnus-newsrc-assoc.")
1189
1190 (defvar gnus-killed-list nil
1191   "List of killed newsgroups.")
1192
1193 (defvar gnus-killed-hashtb nil
1194   "Hash table equivalent of gnus-killed-list.")
1195
1196 (defvar gnus-zombie-list nil
1197   "List of almost dead newsgroups.")
1198
1199 (defvar gnus-description-hashtb nil
1200   "Descriptions of newsgroups.")
1201
1202 (defvar gnus-list-of-killed-groups nil
1203   "List of newsgroups that have recently been killed by the user.")
1204
1205 (defvar gnus-active-hashtb nil
1206   "Hashtable of active articles.")
1207
1208 (defvar gnus-moderated-list nil
1209   "List of moderated newsgroups.")
1210
1211 (defvar gnus-current-startup-file nil
1212   "Startup file for the current host.")
1213
1214 (defvar gnus-last-search-regexp nil
1215   "Default regexp for article search command.")
1216
1217 (defvar gnus-last-shell-command nil
1218   "Default shell command on article.")
1219
1220 (defvar gnus-current-select-method nil
1221   "The current method for selecting a newsgroup.")
1222
1223 (defvar gnus-have-all-newsgroups nil)
1224
1225 (defvar gnus-article-internal-prepare-hook nil)
1226
1227 (defvar gnus-newsgroup-name nil)
1228 (defvar gnus-newsgroup-begin nil)
1229 (defvar gnus-newsgroup-end nil)
1230 (defvar gnus-newsgroup-last-rmail nil)
1231 (defvar gnus-newsgroup-last-mail nil)
1232 (defvar gnus-newsgroup-last-folder nil)
1233 (defvar gnus-newsgroup-last-file nil)
1234 (defvar gnus-newsgroup-auto-expire nil
1235   "If non-nil, all read articles will be marked as expirable.")
1236
1237 (defvar gnus-newsgroup-selected-overlay nil)
1238
1239 (defvar gnus-newsgroup-unreads nil
1240   "List of unread articles in the current newsgroup.")
1241
1242 (defvar gnus-newsgroup-unselected nil
1243   "List of unselected unread articles in the current newsgroup.")
1244
1245 (defvar gnus-newsgroup-marked nil
1246   "List of ticked articles in the current newsgroup (a subset of unread art).")
1247
1248 (defvar gnus-newsgroup-killed nil
1249   "List of ranges of articles that have been through the scoring process.")
1250
1251 (defvar gnus-newsgroup-kill-headers nil)
1252
1253 (defvar gnus-newsgroup-replied nil
1254   "List of articles that have been replied to in the current newsgroup.")
1255
1256 (defvar gnus-newsgroup-expirable nil
1257   "List of articles in the current newsgroup that can be expired.")
1258
1259 (defvar gnus-newsgroup-processable nil
1260   "List of articles in the current newsgroup that can be processed.")
1261
1262 (defvar gnus-newsgroup-bookmarks nil
1263   "List of articles in the current newsgroup that have bookmarks.")
1264
1265 (defvar gnus-newsgroup-dormant nil
1266   "List of dormant articles in the current newsgroup.")
1267
1268 (defvar gnus-newsgroup-scored nil
1269   "List of scored articles in the current newsgroup.")
1270
1271 (defvar gnus-newsgroup-headers nil
1272   "List of article headers in the current newsgroup.")
1273 (defvar gnus-newsgroup-headers-hashtb-by-number nil)
1274
1275 (defvar gnus-newsgroup-ancient nil
1276   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1277
1278 (defvar gnus-current-article nil)
1279 (defvar gnus-article-current nil)
1280 (defvar gnus-current-headers nil)
1281 (defvar gnus-have-all-headers nil "Must be either T or NIL.")
1282 (defvar gnus-last-article nil)
1283 (defvar gnus-current-kill-article nil)
1284 (defvar gnus-newsgroup-dormant-subjects nil)
1285 (defvar gnus-newsgroup-expunged-buffer nil)
1286
1287 ;; Save window configuration.
1288 (defvar gnus-winconf-kill-file nil)
1289
1290 (defconst gnus-group-mode-map nil)
1291 (defvar gnus-article-mode-map nil)
1292 (defvar gnus-kill-file-mode-map nil)
1293
1294 ;; Format specs
1295 (defvar gnus-summary-line-format-spec nil)
1296 (defvar gnus-summary-dummy-line-format-spec nil)
1297 (defvar gnus-group-line-format-spec nil)
1298 (defvar gnus-summary-mode-line-format-spec nil)
1299 (defvar gnus-article-mode-line-format-spec nil)
1300 (defvar gnus-group-mode-line-format-spec nil)
1301 (defvar gnus-summary-expunge-below nil)
1302 (defvar gnus-reffed-article-number nil)
1303
1304 (defvar gnus-summary-mark-positions nil)
1305
1306 (defvar rmail-default-file (expand-file-name "~/XMBOX"))
1307 (defvar rmail-default-rmail-file (expand-file-name "~/XNEWS"))
1308
1309 (defconst gnus-summary-local-variables 
1310   '(gnus-newsgroup-name 
1311     gnus-newsgroup-begin gnus-newsgroup-end 
1312     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail 
1313     gnus-newsgroup-last-folder gnus-newsgroup-last-file 
1314     gnus-newsgroup-auto-expire gnus-newsgroup-unreads 
1315     gnus-newsgroup-unselected gnus-newsgroup-marked
1316     gnus-newsgroup-replied gnus-newsgroup-expirable
1317     gnus-newsgroup-processable gnus-newsgroup-killed
1318     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1319     gnus-newsgroup-dormant-subjects gnus-newsgroup-expunged-buffer
1320     gnus-newsgroup-headers gnus-newsgroup-headers-hashtb-by-number
1321     gnus-current-article gnus-current-headers gnus-have-all-headers
1322     gnus-last-article gnus-article-internal-prepare-hook
1323     gnus-newsgroup-selected-overlay gnus-newsgroup-dependencies
1324     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1325     gnus-score-alist gnus-summary-expunge-below 
1326     gnus-summary-mark-below gnus-newsgroup-ancient)
1327   "Variables that are buffer-local to the summary buffers.")
1328
1329 (defvar gnus-mark-article-hook
1330   (list
1331    (lambda ()
1332      (or (memq gnus-current-article gnus-newsgroup-marked)
1333          (memq gnus-current-article gnus-newsgroup-dormant)
1334          (memq gnus-current-article gnus-newsgroup-expirable)
1335          (gnus-summary-mark-as-read gnus-current-article gnus-read-mark))))
1336   "A hook called when an article is selected at the first time.
1337 The hook is intended to mark an article as read (or unread)
1338 automatically when it is selected.
1339
1340 If you'd like to tick articles instead, use the following hook:
1341
1342 \(setq gnus-mark-article-hook
1343       (list
1344         (lambda ()
1345           (gnus-summary-tick-article gnus-current-article))))")
1346
1347 ;; Define some autoload functions Gnus may use.
1348 (eval-and-compile
1349   (autoload 'metamail-buffer "metamail")
1350   (autoload 'Info-goto-node "info")
1351   
1352   (autoload 'timezone-make-date-arpa-standard "timezone")
1353   (autoload 'timezone-fix-time "timezone")
1354   (autoload 'timezone-make-sortable-date "timezone")
1355   (autoload 'timezone-make-time-string "timezone")
1356   
1357   (autoload 'rmail-output "rmailout"
1358     "Append this message to Unix mail file named FILE-NAME." t)
1359   (autoload 'mail-position-on-field "sendmail")
1360   (autoload 'mail-setup "sendmail")
1361
1362   (autoload 'gnus-mail-reply-using-mhe "gnus-mh")
1363   (autoload 'gnus-mail-forward-using-mhe "gnus-mh")
1364   (autoload 'gnus-mail-other-window-using-mhe "gnus-mh")
1365   (autoload 'gnus-summary-save-in-folder "gnus-mh")
1366   (autoload 'gnus-Folder-save-name "gnus-mh")
1367   (autoload 'gnus-folder-save-name "gnus-mh")
1368   
1369   (autoload 'gnus-group-make-menu-bar "gnus-visual")
1370   (autoload 'gnus-summary-make-menu-bar "gnus-visual")
1371   (autoload 'gnus-article-make-menu-bar "gnus-visual")
1372   (autoload 'gnus-visual-highlight-selected-summary "gnus-visual")
1373   (autoload 'gnus-visual-summary-highlight-line "gnus-visual")
1374
1375   (autoload 'gnus-uu-decode-map "gnus-uu" nil nil 'keymap)
1376   (autoload 'gnus-uu-mark-by-regexp "gnus-uu")
1377   (autoload 'gnus-uu-mark-region "gnus-uu")
1378   (autoload 'gnus-uu-mark-thread "gnus-uu")
1379   (autoload 'gnus-uu-mark-sparse "gnus-uu")
1380   (autoload 'gnus-uu-mark-series "gnus-uu")
1381   (autoload 'gnus-uu-mark-all "gnus-uu")
1382   (autoload 'gnus-uu-post-news "gnus-uu")
1383   (autoload 'gnus-uu-digest-and-forward "gnus-uu")
1384
1385   (autoload 'gnus-uu-decode-uu "gnus-uu")
1386   (autoload 'gnus-uu-decode-uu-and-save "gnus-uu")
1387   (autoload 'gnus-uu-decode-unshar "gnus-uu")
1388   (autoload 'gnus-uu-decode-unshar-and-save "gnus-uu")
1389   (autoload 'gnus-uu-decode-save "gnus-uu")
1390   (autoload 'gnus-uu-decode-binhex "gnus-uu")
1391   (autoload 'gnus-uu-decode-uu-view "gnus-uu")
1392   (autoload 'gnus-uu-decode-uu-and-save-view "gnus-uu")
1393   (autoload 'gnus-uu-decode-unshar-view "gnus-uu")
1394   (autoload 'gnus-uu-decode-unshar-and-save-view "gnus-uu")
1395   (autoload 'gnus-uu-decode-save-view "gnus-uu")
1396   (autoload 'gnus-uu-decode-binhex-view "gnus-uu")
1397
1398   (autoload 'pp "pp")
1399   )
1400
1401 (put 'gnus-group-mode 'mode-class 'special)
1402 (put 'gnus-summary-mode 'mode-class 'special)
1403 (put 'gnus-article-mode 'mode-class 'special)
1404
1405 \f
1406
1407 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1408 (defun gnus-summary-position-cursor () nil)
1409 (defun gnus-group-position-cursor () nil)
1410 (fset 'gnus-summary-position-cursor 'gnus-goto-colon)
1411 (fset 'gnus-group-position-cursor 'gnus-goto-colon)
1412
1413 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
1414   "Pop to BUFFER, evaluate FORMS, and then returns to original window."
1415   (` (let ((GnusStartBufferWindow (selected-window)))
1416        (unwind-protect
1417            (progn
1418              (pop-to-buffer (, buffer))
1419              (,@ forms))
1420          (select-window GnusStartBufferWindow)))))
1421
1422 (defun gnus-make-hashtable (&optional hashsize)
1423   "Make a hash table (default and minimum size is 255).
1424 Optional argument HASHSIZE specifies the table size."
1425   (make-vector (if hashsize 
1426                    (max (gnus-create-hash-size hashsize) 255)
1427                  255) 0))
1428
1429 (defmacro gnus-gethash (string hashtable)
1430   "Get hash value of STRING in HASHTABLE."
1431   ;;(` (symbol-value (abbrev-symbol (, string) (, hashtable))))
1432   ;;(` (abbrev-expansion (, string) (, hashtable)))
1433   (` (symbol-value (intern-soft (, string) (, hashtable)))))
1434
1435 (defmacro gnus-sethash (string value hashtable)
1436   "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
1437   ;; We cannot use define-abbrev since it only accepts string as value.
1438                                         ;  (set (intern string hashtable) value))
1439   (` (set (intern (, string) (, hashtable)) (, value))))
1440
1441 (defsubst gnus-buffer-substring (beg end)
1442   (buffer-substring (match-beginning beg) (match-end end)))
1443
1444 (defsubst gnus-simplify-subject-re (subject)
1445   "Remove \"Re:\" from subject lines."
1446   (let ((case-fold-search t))
1447     (if (string-match "^re: *" subject)
1448         (substring subject (match-end 0))
1449       subject)))
1450
1451 (defsubst gnus-goto-char (point)
1452   (and point (goto-char point)))
1453
1454 \f
1455 ;;;
1456 ;;; Gnus Utility Functions
1457 ;;;
1458
1459 (defun gnus-extract-address-components (from)
1460   (let (name address)
1461     (if (string-match "([^)]+)" from)
1462         (setq name (substring from (1+ (match-beginning 0)) 
1463                               (1- (match-end 0)))))
1464     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1465     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
1466         (setq address (substring from (match-beginning 0) (match-end 0))))
1467     (if (and (not name) address)
1468         (if (string-match (concat "<" (regexp-quote address) ">") from)
1469             (setq name (substring from 0 (1- (match-beginning 0))))))
1470     (list (or name from) (or address from))))
1471
1472 (defun gnus-fetch-field (field)
1473   "Return the value of the header FIELD of current article."
1474   (save-excursion
1475     (save-restriction
1476       (gnus-narrow-to-headers)
1477       (mail-fetch-field field))))
1478
1479 (defun gnus-goto-colon ()
1480   (beginning-of-line)
1481   (search-forward ":" (save-excursion (end-of-line) (point)) t))
1482
1483 (defun gnus-narrow-to-headers ()
1484   (widen)
1485   (save-excursion
1486     (goto-char 1)
1487     (if (search-forward "\n\n")
1488         (narrow-to-region 1 (1- (point))))))
1489
1490 ;; Get a number that is suitable for hashing; bigger than MIN
1491 (defun gnus-create-hash-size (min)
1492   (let ((i 1))
1493     (while (< i min)
1494       (setq i (* 2 i)))
1495     (1- i)))
1496
1497 (defun gnus-update-format-specifications ()
1498   (setq gnus-summary-line-format-spec 
1499         (gnus-parse-format
1500          gnus-summary-line-format gnus-summary-line-format-alist))
1501   (gnus-update-summary-mark-positions)
1502   (setq gnus-summary-dummy-line-format-spec 
1503         (gnus-parse-format gnus-summary-dummy-line-format 
1504                            gnus-summary-dummy-line-format-alist))
1505   (setq gnus-group-line-format-spec
1506         (gnus-parse-format 
1507          gnus-group-line-format 
1508          gnus-group-line-format-alist))
1509   (if (and (string-match "%D" gnus-group-line-format)
1510            (not gnus-description-hashtb)
1511            gnus-read-active-file)
1512       (gnus-read-descriptions-file))
1513   (setq gnus-summary-mode-line-format-spec 
1514         (gnus-parse-format gnus-summary-mode-line-format 
1515                            gnus-summary-mode-line-format-alist))
1516   (setq gnus-article-mode-line-format-spec 
1517         (gnus-parse-format gnus-article-mode-line-format 
1518                            gnus-summary-mode-line-format-alist))
1519   (setq gnus-group-mode-line-format-spec 
1520         (gnus-parse-format gnus-group-mode-line-format 
1521                            gnus-group-mode-line-format-alist)))
1522
1523 (defun gnus-update-summary-mark-positions ()
1524   (save-excursion
1525     (let ((gnus-replied-mark 129)
1526           (gnus-score-below-mark 130)
1527           (gnus-score-over-mark 130)
1528           pos)
1529       (set-buffer (get-buffer-create " *gnus work*"))
1530       (buffer-disable-undo (current-buffer))
1531       (erase-buffer)
1532       (gnus-summary-insert-line 
1533        nil ["" "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
1534       (goto-char (point-min))
1535       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
1536                                          (- (point) 2)))))
1537       (goto-char (point-min))
1538       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
1539                                           (- (point) 2))) pos))
1540       (goto-char (point-min))
1541       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
1542                                         (- (point) 2))) pos))
1543       (setq gnus-summary-mark-positions pos)
1544       (kill-buffer (current-buffer)))))
1545
1546 (defun gnus-format-max-width (var length)
1547   (let (result)
1548     (if (> (length (setq result (eval var))) length)
1549         (format "%s" (substring result 0 length))
1550       (format "%s" result))))
1551
1552 (defun gnus-set-mouse-face (string)
1553   ;; Set mouse face property on STRING.
1554   (put-text-property 0 (length string) 'mouse-face gnus-mouse-face string)
1555   string)
1556
1557 (defun gnus-parse-format (format spec-alist)
1558   ;; This function parses the FORMAT string with the help of the
1559   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1560   ;; string.  If the FORMAT string contains the specifiers %( and %)
1561   ;; the text between them will have the mouse-face text property.
1562   (if (string-match "\\`\\(.*\\)%(\\(.*\\)%)\\(.*\n?\\)\\'" format)
1563       (if (and gnus-visual gnus-mouse-face)
1564           (let ((pre (substring format (match-beginning 1) (match-end 1)))
1565                 (button (substring format (match-beginning 2) (match-end 2)))
1566                 (post (substring format (match-beginning 3) (match-end 3))))
1567             (list 'concat
1568                   (gnus-parse-simple-format pre spec-alist)
1569                   (list 'gnus-set-mouse-face
1570                         (gnus-parse-simple-format button spec-alist))
1571                   (gnus-parse-simple-format post spec-alist)))
1572         (gnus-parse-simple-format
1573          (concat (substring format (match-beginning 1) (match-end 1))
1574                  (substring format (match-beginning 2) (match-end 2))
1575                  (substring format (match-beginning 3) (match-end 3)))
1576          spec-alist))
1577     (gnus-parse-simple-format format spec-alist)))
1578
1579 (defun gnus-parse-simple-format (format spec-alist)
1580   ;; This function parses the FORMAT string with the help of the
1581   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1582   ;; string. The list will consist of the symbol `format', a format
1583   ;; specification string, and a list of forms depending on the
1584   ;; SPEC-ALIST.
1585   (let ((max-width 0)
1586         spec flist fstring b newspec max-width elem beg)
1587     (save-excursion
1588       (set-buffer (get-buffer-create "*gnus work*"))
1589       (buffer-disable-undo (current-buffer))
1590       (gnus-add-current-to-buffer-list)
1591       (erase-buffer)
1592       (insert format)
1593       (goto-char 1)
1594       (while (re-search-forward "%[-0-9]*\\(,[0-9]*\\)*\\(.\\)\\(.\\)?" nil t)
1595         (setq spec (string-to-char (buffer-substring (match-beginning 2)
1596                                                      (match-end 2))))
1597         ;; First check if there are any specs that look anything like
1598         ;; "%12,12A", ie. with a "max width specification". These have
1599         ;; to be treated specially.
1600         (if (setq beg (match-beginning 1))
1601             (setq max-width 
1602                   (string-to-int 
1603                    (buffer-substring (1+ (match-beginning 1)) (match-end 1))))
1604           (setq max-width 0)
1605           (setq beg (match-beginning 2)))
1606         ;; Find the specification from `spec-alist'.
1607         (if (not (setq elem (cdr (assq spec spec-alist))))
1608             (setq elem '("*" ?s)))
1609         ;; Treat user defined format specifiers specially
1610         (and (eq (car elem) 'user-defined)
1611              (setq elem
1612                    (list 
1613                     (list (intern (concat "gnus-user-format-function-"
1614                                           (buffer-substring
1615                                            (match-beginning 3)
1616                                            (match-end 3))))
1617                           'header)
1618                     ?s))
1619              (delete-region (match-beginning 3) (match-end 3)))
1620         (if (not (zerop max-width))
1621             (let ((el (car elem)))
1622               (cond ((= (car (cdr elem)) ?c) 
1623                      (setq el (list 'char-to-string el)))
1624                     ((= (car (cdr elem)) ?d)
1625                      (numberp el) (setq el (list 'int-to-string el))))
1626               (setq flist (cons (list 'gnus-format-max-width 
1627                                       el max-width) 
1628                                 flist))
1629               (setq newspec ?s))
1630           (setq flist (cons (car elem) flist))
1631           (setq newspec (car (cdr elem))))
1632         ;; Remove the old specification (and possibly a ",12" string).
1633         (delete-region beg (match-end 2))
1634         ;; Insert the new specification.
1635         (goto-char beg)
1636         (insert newspec))
1637       (setq fstring (buffer-substring 1 (point-max))))
1638     (cons 'format (cons fstring (nreverse flist)))))
1639
1640 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
1641 (defun gnus-read-init-file ()
1642   (and gnus-init-file
1643        (or (file-exists-p gnus-init-file)
1644            (file-exists-p (concat gnus-init-file ".el"))
1645            (file-exists-p (concat gnus-init-file ".elc")))
1646        (load gnus-init-file nil t)))
1647
1648 ;; Article file names when saving.
1649
1650 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
1651   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1652 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
1653 Otherwise, it is like ~/News/news/group/num."
1654   (let ((default
1655           (expand-file-name
1656            (concat (if gnus-use-long-file-name
1657                        (gnus-capitalize-newsgroup newsgroup)
1658                      (gnus-newsgroup-directory-form newsgroup))
1659                    "/" (int-to-string (header-number headers)))
1660            (or gnus-article-save-directory "~/News"))))
1661     (if (and last-file
1662              (string-equal (file-name-directory default)
1663                            (file-name-directory last-file))
1664              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1665         default
1666       (or last-file default))))
1667
1668 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
1669   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1670 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group/num.
1671 Otherwise, it is like ~/News/news/group/num."
1672   (let ((default
1673           (expand-file-name
1674            (concat (if gnus-use-long-file-name
1675                        newsgroup
1676                      (gnus-newsgroup-directory-form newsgroup))
1677                    "/" (int-to-string (header-number headers)))
1678            (or gnus-article-save-directory "~/News"))))
1679     (if (and last-file
1680              (string-equal (file-name-directory default)
1681                            (file-name-directory last-file))
1682              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1683         default
1684       (or last-file default))))
1685
1686 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
1687   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1688 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group.
1689 Otherwise, it is like ~/News/news/group/news."
1690   (or last-file
1691       (expand-file-name
1692        (if gnus-use-long-file-name
1693            (gnus-capitalize-newsgroup newsgroup)
1694          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1695        (or gnus-article-save-directory "~/News"))))
1696
1697 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
1698   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1699 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group.
1700 Otherwise, it is like ~/News/news/group/news."
1701   (or last-file
1702       (expand-file-name
1703        (if gnus-use-long-file-name
1704            newsgroup
1705          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1706        (or gnus-article-save-directory "~/News"))))
1707
1708 ;; For subscribing new newsgroup
1709
1710 (defun gnus-subscribe-hierarchical-interactive (groups)
1711   (let ((groups (sort groups 'string<))
1712         prefixes prefix start rest ans group starts)
1713     (while groups
1714       (setq prefixes (list "^"))
1715       (while (and groups prefixes)
1716         (while (not (string-match (car prefixes) (car groups)))
1717           (setq prefixes (cdr prefixes)))
1718         (setq prefix (car prefixes))
1719         (setq start (1- (length prefix)))
1720         (if (and (string-match "[^\\.]\\." (car groups) start)
1721                  (cdr groups)
1722                  (setq prefix 
1723                        (concat "^" (substring (car groups) 0 (match-end 0))))
1724                  (string-match prefix (car (cdr groups))))
1725             (progn
1726               (setq prefixes (cons prefix prefixes))
1727               (message "Descend hierarchy %s? ([y]nsq): " 
1728                        (substring prefix 1 (1- (length prefix))))
1729               (setq ans (read-char))
1730               (cond ((= ans ?n)
1731                      (while (and groups 
1732                                  (string-match prefix 
1733                                                (setq group (car groups))))
1734                        (setq gnus-killed-list 
1735                              (cons group gnus-killed-list))
1736                        (gnus-sethash group group gnus-killed-hashtb)
1737                        (setq groups (cdr groups)))
1738                      (setq starts (cdr starts)))
1739                     ((= ans ?s)
1740                      (while (and groups 
1741                                  (string-match prefix 
1742                                                (setq group (car groups))))
1743                        (gnus-sethash group group gnus-killed-hashtb)
1744                        (gnus-subscribe-alphabetically (car groups))
1745                        (setq groups (cdr groups)))
1746                      (setq starts (cdr starts)))
1747                     ((= ans ?q)
1748                      (while groups
1749                        (setq group (car groups))
1750                        (setq gnus-killed-list (cons group gnus-killed-list))
1751                        (gnus-sethash group group gnus-killed-hashtb)
1752                        (setq groups (cdr groups))))
1753                     (t nil)))
1754           (message "Subscribe %s? ([n]yq)" (car groups))
1755           (setq ans (read-char))
1756           (setq group (car groups))
1757           (cond ((= ans ?y)
1758                  (gnus-subscribe-alphabetically (car groups))
1759                  (gnus-sethash group group gnus-killed-hashtb))
1760                 ((= ans ?q)
1761                  (while groups
1762                    (setq group (car groups))
1763                    (setq gnus-killed-list (cons group gnus-killed-list))
1764                    (gnus-sethash group group gnus-killed-hashtb)
1765                    (setq groups (cdr groups))))
1766                 (t 
1767                  (setq gnus-killed-list (cons group gnus-killed-list))
1768                  (gnus-sethash group group gnus-killed-hashtb)))
1769           (setq groups (cdr groups)))))))
1770
1771 (defun gnus-subscribe-randomly (newsgroup)
1772   "Subscribe new NEWSGROUP by making it the first newsgroup."
1773   (gnus-subscribe-newsgroup newsgroup))
1774
1775 (defun gnus-subscribe-alphabetically (newgroup)
1776   "Subscribe new NEWSGROUP and insert it in alphabetical order."
1777   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
1778   (let ((groups (cdr gnus-newsrc-assoc))
1779         before)
1780     (while (and (not before) groups)
1781       (if (string< newgroup (car (car groups)))
1782           (setq before (car (car groups)))
1783         (setq groups (cdr groups))))
1784     (gnus-subscribe-newsgroup newgroup before)))
1785
1786 (defun gnus-subscribe-hierarchically (newgroup)
1787   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
1788   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
1789   (save-excursion
1790     (set-buffer (find-file-noselect gnus-current-startup-file))
1791     (let ((groupkey newgroup)
1792           before)
1793       (while (and (not before) groupkey)
1794         (goto-char (point-min))
1795         (let ((groupkey-re
1796                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
1797           (while (and (re-search-forward groupkey-re nil t)
1798                       (progn
1799                         (setq before (buffer-substring
1800                                       (match-beginning 1) (match-end 1)))
1801                         (string< before newgroup)))))
1802         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
1803         (setq groupkey
1804               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
1805                   (substring groupkey (match-beginning 1) (match-end 1)))))
1806       (gnus-subscribe-newsgroup newgroup before))))
1807
1808 (defun gnus-subscribe-interactively (newsgroup)
1809   "Subscribe new NEWSGROUP interactively.
1810 It is inserted in hierarchical newsgroup order if subscribed. If not,
1811 it is killed."
1812   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " newsgroup))
1813       (gnus-subscribe-hierarchically newsgroup)
1814     (setq gnus-killed-list (cons newsgroup gnus-killed-list))))
1815
1816 (defun gnus-subscribe-zombies (newsgroup)
1817   "Make new NEWSGROUP a zombie group."
1818   (setq gnus-zombie-list (cons newsgroup gnus-zombie-list)))
1819
1820 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
1821   "Subscribe new NEWSGROUP.
1822 If NEXT is non-nil, it is inserted before NEXT. Otherwise it is made
1823 the first newsgroup."
1824   ;; We subscribe the group by changing its level to 3.
1825   (gnus-group-change-level 
1826    newsgroup 3 9 (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
1827   (message "Subscribe newsgroup: %s" newsgroup))
1828
1829 ;; For directories
1830
1831 (defun gnus-newsgroup-directory-form (newsgroup)
1832   "Make hierarchical directory name from NEWSGROUP name."
1833   (let ((newsgroup (substring newsgroup 0)) ;Copy string.
1834         (len (length newsgroup))
1835         (idx 0))
1836     ;; Replace all occurrences of `.' with `/'.
1837     (while (< idx len)
1838       (if (= (aref newsgroup idx) ?.)
1839           (aset newsgroup idx ?/))
1840       (setq idx (1+ idx)))
1841     newsgroup
1842     ))
1843
1844 (defun gnus-make-directory (dir)
1845   "Make DIRECTORY recursively."
1846   (let* ((dir (expand-file-name dir default-directory))
1847          dirs)
1848     (if (string-match "/$" dir)
1849         (setq dir (substring dir 0 (match-beginning 0))))
1850     (while (not (file-exists-p dir))
1851       (setq dirs (cons dir dirs))
1852       (string-match "/[^/]+$" dir)
1853       (setq dir (substring dir 0 (match-beginning 0))))
1854     (while dirs
1855       (make-directory (car dirs))
1856       (setq dirs (cdr dirs)))))
1857
1858 (defun gnus-capitalize-newsgroup (newsgroup)
1859   "Capitalize NEWSGROUP name."
1860   (and (not (zerop (length newsgroup)))
1861        (concat (char-to-string (upcase (aref newsgroup 0)))
1862                (substring newsgroup 1))))
1863
1864 ;; Var
1865
1866 (defun gnus-simplify-subject (subject &optional re-only)
1867   "Remove `Re:' and words in parentheses.
1868 If optional argument RE-ONLY is non-nil, strip `Re:' only."
1869   (let ((case-fold-search t))           ;Ignore case.
1870     ;; Remove `Re:' and `Re^N:'.
1871     (if (string-match "^re:[ \t]*" subject)
1872         (setq subject (substring subject (match-end 0))))
1873     ;; Remove words in parentheses from end.
1874     (or re-only
1875         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1876           (setq subject (substring subject 0 (match-beginning 0)))))
1877     ;; Return subject string.
1878     subject
1879     ))
1880
1881 (defun gnus-add-current-to-buffer-list ()
1882   (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list)))
1883
1884 ;; Functions accessing headers.
1885 ;; Functions are more convenient than macros in some case.
1886
1887 (defun gnus-header-number (header)
1888   "Return article number in HEADER."
1889   (header-number header))
1890
1891 (defun gnus-header-subject (header)
1892   "Return subject string in HEADER."
1893   (header-subject header))
1894
1895 (defun gnus-header-from (header)
1896   "Return author string in HEADER."
1897   (header-from header))
1898
1899 (defun gnus-header-xref (header)
1900   "Return xref string in HEADER."
1901   (header-xref header))
1902
1903 (defun gnus-header-lines (header)
1904   "Return lines in HEADER."
1905   (header-lines header))
1906
1907 (defun gnus-header-date (header)
1908   "Return date in HEADER."
1909   (header-date header))
1910
1911 (defun gnus-header-id (header)
1912   "Return Id in HEADER."
1913   (header-id header))
1914
1915 (defun gnus-header-references (header)
1916   "Return references in HEADER."
1917   (header-references header))
1918
1919 (defun gnus-clear-system ()
1920   "Clear all variables and buffers."
1921   ;; Clear Gnus variables.
1922   (let ((variables gnus-variable-list))
1923     (while variables
1924       (set (car variables) nil)
1925       (setq variables (cdr variables))))
1926   ;; Clear other internal variables.
1927   (setq gnus-list-of-killed-groups nil
1928         gnus-have-read-active-file nil
1929         gnus-newsrc-assoc nil
1930         gnus-newsrc-hashtb nil
1931         gnus-killed-list nil
1932         gnus-zombie-list nil
1933         gnus-killed-hashtb nil
1934         gnus-active-hashtb nil
1935         gnus-moderated-list nil
1936         gnus-description-hashtb nil
1937         gnus-newsgroup-headers nil
1938         gnus-score-cache nil
1939         gnus-newsgroup-headers-hashtb-by-number nil
1940         gnus-current-select-method nil)
1941   ;; Kill the startup file.
1942   (and gnus-current-startup-file
1943        (get-file-buffer gnus-current-startup-file)
1944        (kill-buffer (get-file-buffer gnus-current-startup-file)))
1945   (setq gnus-current-startup-file nil)
1946   (gnus-dribble-clear)
1947   ;; Kill global KILL file buffer.
1948   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
1949       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
1950   ;; Kill Gnus buffers.
1951   (while gnus-buffer-list
1952     (if (and (get-buffer (car gnus-buffer-list))
1953              (buffer-name (get-buffer (car gnus-buffer-list))))
1954         (kill-buffer (car gnus-buffer-list)))
1955     (setq gnus-buffer-list (cdr gnus-buffer-list))))
1956
1957 (defun gnus-configure-windows (action &optional force)
1958   "Configure Gnus windows according to the next ACTION.
1959 The ACTION is either a symbol, such as `summary', or a
1960 configuration list such as `(1 1 2)'.  If ACTION is not a list,
1961 configuration list is got from the variable gnus-window-configuration.
1962 If FORCE is non-nil, the updating will be done whether it is necessary
1963 or not."
1964   (let* ((windows
1965           (if (listp action) action 
1966             (if (listp gnus-window-configuration)
1967                 (car (cdr (assq action gnus-window-configuration)))
1968               gnus-window-configuration)))
1969          (grpwin (get-buffer-window gnus-group-buffer))
1970          (subwin (get-buffer-window gnus-summary-buffer))
1971          (artwin (get-buffer-window gnus-article-buffer))
1972          (winsum nil)
1973          (height nil)
1974          (grpheight 0)
1975          (subheight 0)
1976          (artheight 0)
1977
1978          ;; Make split-window-vertically leave focus in upper window.
1979          (split-window-keep-point t))
1980     (if (and (symbolp windows) (fboundp windows))
1981         (funcall windows action)
1982       (if (and (not force)
1983                (or (null windows)               ;No configuration is specified.
1984                    (and (eq (null grpwin)
1985                             (zerop (nth 0 windows)))
1986                         (eq (null subwin)
1987                             (zerop (nth 1 windows)))
1988                         (eq (null artwin)
1989                             (zerop (nth 2 windows))))))
1990           ;; No need to change window configuration.
1991           nil
1992         (select-window (or grpwin subwin artwin (selected-window)))
1993         ;; First of all, compute the height of each window.
1994         (cond (gnus-use-full-window
1995                ;; Take up the entire screen.
1996                (delete-other-windows)
1997                (setq height (window-height (selected-window))))
1998               (t
1999                (setq height (+ (if grpwin (window-height grpwin) 0)
2000                                (if subwin (window-height subwin) 0)
2001                                (if artwin (window-height artwin) 0)))))
2002         ;; The group buffer exits always. So, use it to extend the
2003         ;; group window so as to get enough window space.
2004         (switch-to-buffer gnus-group-buffer 'norecord)
2005         (and (get-buffer gnus-summary-buffer)
2006              (delete-windows-on gnus-summary-buffer))
2007         (and (get-buffer gnus-article-buffer)
2008              (delete-windows-on gnus-article-buffer))
2009         ;; Compute expected window height.
2010         (setq winsum (apply (function +) windows))
2011         (if (not (zerop (nth 0 windows)))
2012             (setq grpheight (max window-min-height
2013                                  (/ (* height (nth 0 windows)) winsum))))
2014         (if (not (zerop (nth 1 windows)))
2015             (setq subheight (max window-min-height
2016                                  (/ (* height (nth 1 windows)) winsum))))
2017         (if (not (zerop (nth 2 windows)))
2018             (setq artheight (max window-min-height
2019                                  (/ (* height (nth 2 windows)) winsum))))
2020         (setq height (+ grpheight subheight artheight))
2021         (enlarge-window (max 0 (- height (window-height (selected-window)))))
2022         ;; Then split the window.
2023         (and (not (zerop artheight))
2024              (or (not (zerop grpheight))
2025                  (not (zerop subheight)))
2026              (split-window-vertically (+ grpheight subheight)))
2027         (and (not (zerop grpheight))
2028              (not (zerop subheight))
2029              (split-window-vertically grpheight))
2030         ;; Then select buffers in each window.
2031         (or (zerop grpheight)
2032             (progn
2033               (switch-to-buffer gnus-group-buffer 'norecord)
2034               (other-window 1)))
2035         (or (zerop subheight)
2036             (progn
2037               (switch-to-buffer gnus-summary-buffer 'norecord)
2038               (other-window 1)))
2039         (or (zerop artheight)
2040             (progn
2041               ;; If article buffer does not exist, it will be created
2042               ;; and initialized.
2043               (gnus-article-setup-buffer)
2044               (switch-to-buffer gnus-article-buffer 'norecord)
2045               (setq buffer-read-only t) ; !!! Why!?! 
2046               (bury-buffer gnus-summary-buffer)
2047               (bury-buffer gnus-group-buffer)))
2048         (or (zerop subheight)
2049             (progn
2050               (pop-to-buffer gnus-summary-buffer)
2051               ;; It seems that some code in this function will set
2052               ;; buffer-read-only to nil. I have absolutely no idea
2053               ;; why. 
2054               (setq buffer-read-only t))) ; !!! Why!?! 
2055         ))))
2056
2057 (defun gnus-window-configuration-split (action)
2058   (switch-to-buffer gnus-group-buffer t)
2059   (delete-other-windows)
2060   (split-window-horizontally)
2061   (cond ((or (eq action 'newsgroups) (eq action 'summary))
2062          (if (and (get-buffer gnus-summary-buffer)
2063                   (buffer-name gnus-summary-buffer))
2064              (switch-to-buffer-other-window gnus-summary-buffer)))
2065         ((eq action 'article)
2066          (switch-to-buffer gnus-summary-buffer t)
2067          (other-window 1)
2068          (gnus-article-setup-buffer)
2069          (switch-to-buffer gnus-article-buffer t))))
2070
2071 (defun gnus-version ()
2072   "Version numbers of this version of Gnus."
2073   (interactive)
2074   (let ((methods gnus-valid-select-methods)
2075         (mess gnus-version)
2076         meth)
2077     ;; Go through all the legal select methods and add their version
2078     ;; numbers to the total version string. Only the backends that are
2079     ;; currently in use will have their message numbers taken into
2080     ;; consideration. 
2081     (while methods
2082       (setq meth (intern (concat (car (car methods)) "-version")))
2083       (and (boundp meth)
2084            (stringp (symbol-value meth))
2085            (setq mess (concat mess "; " (symbol-value meth))))
2086       (setq methods (cdr methods)))
2087     (message mess)))
2088
2089 (defun gnus-info-find-node ()
2090   "Find Info documentation of Gnus."
2091   (interactive)
2092   ;; Enlarge info window if needed.
2093   (cond ((eq major-mode 'gnus-group-mode)
2094          (gnus-configure-windows '(1 0 0)) ;Take all windows.
2095          (pop-to-buffer gnus-group-buffer))
2096         ((eq major-mode 'gnus-summary-mode)
2097          (gnus-configure-windows '(0 1 0)) ;Take all windows.
2098          (pop-to-buffer gnus-summary-buffer)))
2099   (Info-goto-node (car (cdr (assq major-mode gnus-info-nodes)))))
2100
2101 (defun gnus-bug ()
2102   "Send a bug report to the Gnus maintainers."
2103   (interactive)
2104   (pop-to-buffer "*Gnus Bug*")
2105   (erase-buffer)
2106   (mail-setup gnus-maintainer "[Gnus Bug Report] " nil nil nil nil)
2107   (goto-char (point-min))
2108   (search-forward mail-header-separator)
2109   (forward-line 1)
2110   (insert (format "%s\n%s\n\n" (gnus-version) (emacs-version)))
2111   (gnus-debug)
2112   (mail-mode)
2113   (message ""))
2114
2115 (defun gnus-debug ()
2116   "Attemps to go through the Gnus source file and report what variables have been changed.
2117 The source file has to be in the Emacs load path."
2118   (interactive)
2119   (let ((dirs load-path)
2120         file expr olist)
2121     (while dirs
2122       (if (file-exists-p (setq file (concat (car dirs) "/gnus.el")))
2123           (save-excursion
2124             (setq dirs nil)
2125             (set-buffer (get-buffer-create "*gnus bug info*"))
2126             (buffer-disable-undo (current-buffer))
2127             (erase-buffer)
2128             (insert-file-contents file)
2129             (goto-char (point-min))
2130             (or (search-forward "\n;; Internal variables" nil t)
2131                 (error "Malformed sources"))
2132             (narrow-to-region (point-min) (point))
2133             (goto-char (point-min))
2134             (while (setq expr (condition-case () 
2135                                   (read (current-buffer)) (error nil)))
2136               (and (eq (car expr) 'defvar)
2137                    (stringp (nth 3 expr))
2138                    (not (equal (eval (nth 2 expr))
2139                                (symbol-value (nth 1 expr))))
2140                    (setq olist (cons (nth 1 expr) olist))))
2141             (kill-buffer (current-buffer)))
2142         (setq dirs (cdr dirs))))
2143     (while olist
2144       (insert "(setq " (symbol-name (car olist)) " '" 
2145               (prin1-to-string (symbol-value (car olist))) ")\n")
2146       (setq olist (cdr olist)))
2147     (insert "\n\n")))
2148
2149 (defun gnus-overload-functions (&optional overloads)
2150   "Overload functions specified by optional argument OVERLOADS.
2151 If nothing is specified, use the variable gnus-overload-functions."
2152   (let ((defs nil)
2153         (overloads (or overloads gnus-overload-functions)))
2154     (while overloads
2155       (setq defs (car overloads))
2156       (setq overloads (cdr overloads))
2157       ;; Load file before overloading function if necessary.  Make
2158       ;; sure we cannot use `require' always.
2159       (and (not (fboundp (car defs)))
2160            (car (cdr (cdr defs)))
2161            (load (car (cdr (cdr defs))) nil 'nomessage))
2162       (fset (car defs) (car (cdr defs)))
2163       )))
2164
2165 (defun gnus-replace-chars-in-string (string from to)
2166   "Replace characters in STRING from FROM to TO."
2167   (let ((string (substring string 0))   ;Copy string.
2168         (len (length string))
2169         (idx 0))
2170     ;; Replace all occurrences of FROM with TO.
2171     (while (< idx len)
2172       (if (= (aref string idx) from)
2173           (aset string idx to))
2174       (setq idx (1+ idx)))
2175     string))
2176
2177 (defun gnus-days-between (date1 date2)
2178   ;; Return the number of days between date1 and date2.
2179   (let ((d1 (mapcar (lambda (s) (and s (string-to-int s)) )
2180                     (timezone-parse-date date1)))
2181         (d2 (mapcar (lambda (s) (and s (string-to-int s)) )
2182                     (timezone-parse-date date2))))
2183     (- (timezone-absolute-from-gregorian 
2184         (nth 1 d1) (nth 2 d1) (car d1))
2185        (timezone-absolute-from-gregorian 
2186         (nth 1 d2) (nth 2 d2) (car d2)))))
2187
2188 (defun gnus-file-newer-than (file date)
2189   (let ((fdate (nth 5 (file-attributes file))))
2190     (or (> (car fdate) (car date))
2191         (and (= (car fdate) (car date))
2192              (> (nth 1 fdate) (nth 1 date))))))
2193
2194 (defun gnus-y-or-n-p (prompt)
2195   (prog1
2196       (y-or-n-p prompt)
2197     (message "")))
2198
2199 (defun gnus-yes-or-no-p (prompt)
2200   (prog1
2201       (yes-or-no-p prompt)
2202     (message "")))
2203
2204 ;; Return a string of length POS+1 representing NUMber in BASE. The
2205 ;; resulting string will be left padded with zeds.
2206 (defun gnus-number-base-x (num pos base)
2207   (if (< pos 0)
2208       ""
2209     (concat 
2210      (char-to-string
2211       (aref "zyxwvutsrqponmlkjihgfedcba9876543210" (/ num (expt base pos))))
2212      (gnus-number-base-x 
2213       (% num (expt base pos)) (1- pos) base))))
2214
2215 ;; List and range functions
2216
2217 (defun gnus-last-element (list)
2218   "Return last element of LIST."
2219   (while (cdr list)
2220     (setq list (cdr list)))
2221   (car list))
2222
2223 (defun gnus-set-difference (list1 list2)
2224   "Return a list of elements of LIST1 that do not appear in LIST2."
2225   (let ((list1 (copy-sequence list1)))
2226     (while list2
2227       (setq list1 (delq (car list2) list1))
2228       (setq list2 (cdr list2)))
2229     list1
2230     ))
2231
2232 (defun gnus-intersection (list1 list2)      
2233   (let ((result nil))
2234     (while list2
2235       (if (memq (car list2) list1)
2236           (setq result (cons (car list2) result)))
2237       (setq list2 (cdr list2)))
2238     result
2239     ))
2240
2241 (defun gnus-compress-sequence (numbers &optional always-list)
2242   "Convert list of numbers to a list of ranges or a single range.
2243 If ALWAYS-LIST is non-nil, this function will always release a list of
2244 ranges."
2245   (let* ((first (car numbers))
2246          (last (car numbers))
2247          result)
2248     (if (null numbers)
2249         nil
2250       (while numbers
2251         (cond ((= last (car numbers)) nil) ;Omit duplicated number
2252               ((= (1+ last) (car numbers)) ;Still in sequence
2253                (setq last (car numbers)))
2254               (t                                ;End of one sequence
2255                (setq result (cons (cons first last) result))
2256                (setq first (car numbers))
2257                (setq last  (car numbers))))
2258         (setq numbers (cdr numbers)))
2259       (if (and (not always-list) (null result))
2260           (cons first last)
2261         (nreverse (cons (cons first last) result))))))
2262
2263 (defun gnus-uncompress-sequence (ranges)
2264   "Expand a list of ranges into a list of numbers.
2265 RANGES is either a single range on the form `(num . num)' or a list of
2266 these ranges."
2267   (let (first last result)
2268     (if (null ranges)
2269         nil
2270       (if (atom (car ranges))
2271           (progn
2272             (setq first (car ranges))
2273             (setq last (cdr ranges))
2274             (while (<= first last)
2275               (setq result (cons first result))
2276               (setq first (1+ first))))
2277         (while ranges
2278           (setq first (car (car ranges)))
2279           (setq last  (cdr (car ranges)))
2280           (while (<= first last)
2281             (setq result (cons first result))
2282             (setq first (1+ first)))
2283           (setq ranges (cdr ranges))))
2284       (nreverse result))))
2285
2286 (defun gnus-add-to-range (ranges list)
2287   "Return a list of ranges that has all articles from both RANGES and LIST.
2288 Note: LIST has to be sorted over `<'."
2289   (let* ((ranges (if (and ranges (atom (car ranges))) (list ranges) ranges))
2290          (inrange ranges)
2291          did-one
2292          range nranges first last)
2293     (if (not list)
2294         ranges
2295       (if (not ranges)
2296           (gnus-compress-sequence list t)
2297         (and ranges 
2298              (> (car (car ranges)) 1)
2299              (progn
2300                (setq did-one t)
2301                (setq inrange (setq ranges (cons (cons 1 1) ranges)))))
2302         (while (and ranges list)
2303           (setq range (car ranges))
2304           (while (and list (>= (car list) (car range))
2305                       (<= (car list) (cdr range)))
2306             (setq list (cdr list)))
2307           (while (and list (= (1- (car list)) (cdr range)))
2308             (setcdr range (car list))
2309             (setq list (cdr list)))
2310           (if (and list (and (> (car list) (cdr range)) 
2311                              (cdr ranges)
2312                              (< (car list) (car (car (cdr ranges))))))
2313               (setcdr ranges (cons (cons (car list) (car list)) (cdr ranges))))
2314           (setq ranges (cdr ranges)))
2315         (if (and list (not ranges))
2316             (setq inrange (nconc inrange (gnus-compress-sequence list t))))
2317         (if did-one
2318             (if (eq (cdr (car inrange)) 1)
2319                 (setq inrange (cdr inrange))
2320               (setcar (car inrange) 2)))
2321         (setq ranges inrange)
2322         (while ranges
2323           (if (and (cdr ranges) (>= (1+ (cdr (car ranges)))
2324                                     (car (car (cdr ranges)))))
2325               (progn
2326                 (setcdr (car ranges) (cdr (car (cdr ranges))))
2327                 (setcdr ranges (cdr (cdr ranges))))
2328             (setq ranges (cdr ranges))))
2329         (if (not (cdr inrange))
2330             (car inrange)
2331           inrange)))))
2332
2333 (defun gnus-remove-from-range (ranges list)
2334   "Return a list of ranges that has all articles from LIST removed from RANGES.
2335 Note: LIST has to be sorted over `<'."
2336   ;; !!! This function shouldn't look like this, but I've got a headache.
2337   (gnus-compress-sequence 
2338    (gnus-set-difference 
2339     (gnus-uncompress-sequence ranges) list)))
2340
2341 (defun gnus-member-of-range (number ranges)
2342   (let ((not-stop t))
2343     (while (and ranges not-stop)
2344       (if (and (>= number (car (car ranges)))
2345                (<= number (cdr (car ranges))))
2346           (setq not-stop nil))
2347       (setq ranges (cdr ranges)))
2348     (not not-stop)))
2349
2350 \f
2351 ;;;
2352 ;;; Gnus group mode
2353 ;;;
2354
2355 (if gnus-group-mode-map
2356     nil
2357   (setq gnus-group-mode-map (make-keymap))
2358   (suppress-keymap gnus-group-mode-map)
2359   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
2360   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
2361   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
2362   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
2363   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
2364   (define-key gnus-group-mode-map [del] 'gnus-group-prev-unread-group)
2365   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
2366   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
2367   (define-key gnus-group-mode-map "\M-n" 'gnus-group-next-unread-group-same-level)
2368   (define-key gnus-group-mode-map "\M-p" 'gnus-group-prev-unread-group-same-level)
2369   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
2370   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
2371   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
2372   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
2373   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
2374   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
2375   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
2376   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
2377   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
2378   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
2379   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
2380   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
2381   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
2382   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
2383   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
2384   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
2385   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
2386   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
2387   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
2388   (define-key gnus-group-mode-map "\C-c\M-C-a" 'gnus-group-description-apropos)
2389   (define-key gnus-group-mode-map "D" 'gnus-group-make-doc-group)
2390   (define-key gnus-group-mode-map "d" 'gnus-group-make-directory-group)
2391   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
2392   (define-key gnus-group-mode-map "\M-a" 'gnus-group-add-group)
2393   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group)
2394   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
2395   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
2396   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
2397   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
2398   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
2399   (define-key gnus-group-mode-map "\M-z" 'gnus-group-kill-all-zombies)
2400   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
2401   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
2402   (define-key gnus-group-mode-map "\C-c\C-k" 'gnus-group-list-killed)
2403   (define-key gnus-group-mode-map "\C-c\C-z" 'gnus-group-list-zombies)
2404   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
2405   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
2406   (define-key gnus-group-mode-map "V" 'gnus-version)
2407   (define-key gnus-group-mode-map "S" 'gnus-group-set-current-level)
2408   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
2409   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
2410   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
2411   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
2412   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
2413   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
2414   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
2415   (define-key gnus-group-mode-map [mouse-2] 'gnus-mouse-pick-group))
2416
2417 (defun gnus-group-mode ()
2418   "Major mode for reading news.
2419 All normal editing commands are switched off.
2420 The following commands are available:
2421
2422 \\<gnus-group-mode-map>
2423 \\[gnus-group-read-group]\t Choose the current group
2424 \\[gnus-group-select-group]\t Select the current group without selecting the first article
2425 \\[gnus-group-jump-to-group]\t Go to some group
2426 \\[gnus-group-next-unread-group]\t Go to the next unread group
2427 \\[gnus-group-prev-unread-group]\t Go to the previous unread group
2428 \\[gnus-group-next-group]\t Go to the next group
2429 \\[gnus-group-prev-group]\t Go to the previous group
2430 \\[gnus-group-next-unread-group-same-level]\t Go to the next unread group on the same level
2431 \\[gnus-group-prev-unread-group-same-level]\t Go to the previous unread group un the same level
2432 \\[gnus-group-unsubscribe-current-group]\t (Un)subscribe to the current group
2433 \\[gnus-group-unsubscribe-group]\t (Un)subscribe to some group
2434 \\[gnus-group-catchup-current]\t Mark all unread articles in the current group as read
2435 \\[gnus-group-catchup-current-all]\t Mark all alrticles in the current group as read
2436 \\[gnus-group-list-groups]\t List groups that have unread articles
2437 \\[gnus-group-list-all-groups]\t List all groups
2438 \\[gnus-group-mail]\t Compose a mail
2439 \\[gnus-group-get-new-news]\t Look for new news
2440 \\[gnus-group-get-new-news-this-group]\t Look for new news for the current group
2441 \\[gnus-group-restart]\t Restart Gnus
2442 \\[gnus-group-save-newsrc]\t Save the startup file(s)
2443 \\[gnus-group-browse-foreign-server]\t Browse a foreign (NNTP) server
2444 \\[gnus-group-check-bogus-groups]\t Check for and remove bogus newsgroups
2445 \\[gnus-find-new-newsgroups]\t Find new newsgroups
2446 \\[gnus-group-describe-group]\t Describe the current newsgroup
2447 \\[gnus-group-describe-all-groups]\t Describe all newsgroups
2448 \\[gnus-group-post-news]\t Post an article to some newsgroup
2449 \\[gnus-group-add-group]\t Add a newsgroup entry
2450 \\[gnus-group-edit-group]\t Edit a newsgroup entry
2451 \\[gnus-group-make-directory-group]\t Read a directory as a newsgroups
2452 \\[gnus-group-edit-local-kill]\t Edit a local kill file
2453 \\[gnus-group-edit-global-kill]\t Edit the global kill file
2454 \\[gnus-group-kill-group]\t Kill the current newsgroup
2455 \\[gnus-group-yank-group]\t Yank a previously killed newsgroup
2456 \\[gnus-group-kill-region]\t Kill all newsgroups between point and mark
2457 \\[gnus-group-kill-all-zombies]\t Kill all zombie newsgroups
2458 \\[gnus-group-transpose-groups]\t Transpose two newsgroups
2459 \\[gnus-group-list-killed]\t List all killed newsgroups
2460 \\[gnus-group-list-zombies]\t List all zombie newsgroups
2461 \\[gnus-group-expire-articles]\t Expire the expirable articles in the current newsgroup
2462 \\[gnus-group-expire-all-groups]\t Expire expirable articles in all newsgroups
2463 \\[gnus-version]\t Display the current Gnus version
2464 \\[gnus-group-set-current-level]\t Set the level of the current newsgroup
2465 \\[gnus-group-suspend]\t Suspend Gnus
2466 \\[gnus-group-clear-dribble]\t Clear the dribble buffer
2467 \\[gnus-group-exit]\t Stop reading news
2468 \\[gnus-group-quit]\t Stop reading news without saving the startup files
2469 \\[gnus-group-describe-briefly]\t Give a brief description of the current mode
2470 \\[gnus-info-find-node]\t Find the info pages for Gnus
2471 "
2472   (interactive)
2473   (if gnus-visual (gnus-group-make-menu-bar))
2474   (kill-all-local-variables)
2475   (setq mode-line-modified "-- ")
2476   (make-local-variable 'mode-line-format)
2477   (setq mode-line-format (copy-sequence mode-line-format))
2478   (and (equal (nth 3 mode-line-format) "   ")
2479        (setcar (nthcdr 3 mode-line-format) ""))
2480   (setq major-mode 'gnus-group-mode)
2481   (setq mode-name "Group")
2482   (gnus-group-set-mode-line)
2483   (setq mode-line-process nil)
2484   (use-local-map gnus-group-mode-map)
2485   (buffer-disable-undo (current-buffer))
2486   (setq truncate-lines t)
2487   (setq buffer-read-only t)
2488   (run-hooks 'gnus-group-mode-hook))
2489
2490 (defun gnus-mouse-pick-group (e)
2491   (interactive "e")
2492   (mouse-set-point e)
2493   (gnus-group-read-group nil))
2494
2495 ;;;###autoload
2496 (defun gnus-no-server (&optional arg)
2497   "Read network news.
2498 If ARG is a positive number, Gnus will use that as the
2499 startup level. If ARG is nil, Gnus will be started at level 2. 
2500 If ARG is non-nil and not a positive number, Gnus will
2501 prompt the user for the name of an NNTP server to use.
2502 As opposed to `gnus', this command will not connect to the local server."
2503   (interactive "P")
2504   (gnus (or arg 2) t))
2505
2506 (defalias '\(ding\) 'gnus)
2507
2508 ;;;###autoload
2509 (defun gnus (&optional arg dont-connect)
2510   "Read network news.
2511 If ARG is non-nil and a positive number, Gnus will use that as the
2512 startup level. If ARG is non-nil and not a positive number, Gnus will
2513 prompt the user for the name of an NNTP server to use."
2514   (interactive "P")
2515   (if (get-buffer gnus-group-buffer)
2516       (progn
2517         (switch-to-buffer gnus-group-buffer)
2518         (gnus-group-get-new-news))
2519     (gnus-clear-system)
2520     (gnus-read-init-file)
2521     (let ((level (and arg (numberp arg) (> arg 0) arg)))
2522       (unwind-protect
2523           (progn
2524             (switch-to-buffer (get-buffer-create gnus-group-buffer))
2525             (gnus-add-current-to-buffer-list)
2526             (gnus-group-mode)
2527             (or dont-connect (gnus-start-news-server (and arg (not level)))))
2528         (if (and (not dont-connect) 
2529                  (not (gnus-server-opened gnus-select-method)))
2530             (gnus-group-quit)
2531           ;; NNTP server is successfully open. 
2532           (gnus-update-format-specifications)
2533           (let ((buffer-read-only nil))
2534             (erase-buffer)
2535             (if (not gnus-inhibit-startup-message)
2536                 (progn
2537                   (gnus-group-startup-message)
2538                   (sit-for 0))))
2539           (run-hooks 'gnus-startup-hook)
2540           (gnus-setup-news nil level)
2541           (and gnus-use-dribble-file (gnus-dribble-open))
2542           (or t (not gnus-novice-user)
2543               gnus-expert-user
2544               (gnus-group-describe-briefly)) ;Show brief help message.
2545           (gnus-group-list-groups (or level 5)))))))
2546
2547 (defun gnus-group-startup-message (&optional x y)
2548   "Insert startup message in current buffer."
2549   ;; Insert the message.
2550   (erase-buffer)
2551   (insert
2552    (format "
2553 %s
2554        A newsreader 
2555   for GNU Emacs
2556
2557     Based on GNUS 
2558          written by 
2559  Masanobu UMEDA
2560
2561 Lars Ingebrigtsen 
2562   larsi@ifi.uio.no
2563
2564            gnus-version))
2565   ;; And then hack it.
2566   ;; 18 is the longest line.
2567   (indent-rigidly (point-min) (point-max) 
2568                   (/ (max (- (window-width) (or x 22)) 0) 2))
2569   (goto-char (point-min))
2570   ;; +4 is fuzzy factor.
2571   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2)))
2572
2573 (defun gnus-group-setup-buffer ()
2574   (or (get-buffer gnus-group-buffer)
2575       (progn
2576         (switch-to-buffer (get-buffer-create gnus-group-buffer))
2577         (gnus-add-current-to-buffer-list)
2578         (gnus-group-mode))))
2579
2580 (defun gnus-group-list-groups (level &optional unread)
2581   "List newsgroups with level LEVEL or lower that have unread alticles.
2582 Default is 5, which lists all subscribed groups.
2583 If argument UNREAD is non-nil, groups with no unread articles are also listed."
2584   (interactive "P")
2585   (setq level (or level 5))
2586   (gnus-group-setup-buffer)     ;May call from out of group buffer
2587   (let ((case-fold-search nil)
2588         (group (gnus-group-group-name)))
2589     (funcall gnus-group-prepare-function level unread nil)
2590     (if (zerop (buffer-size))
2591         ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
2592         (message "No news is horrible news")
2593       (goto-char (point-min))
2594       (if (not group)
2595           ;; Go to the first group with unread articles.
2596           (gnus-group-search-forward nil nil nil t)
2597         ;; Find the right group to put point on. If the current group
2598         ;; has disapeared in the new listing, try to find the next
2599         ;; one. If no next one can be found, just leave point at the
2600         ;; first newsgroup in the buffer.
2601         (if (not (gnus-goto-char
2602                   (text-property-any (point-min) (point-max) 
2603                                      'gnus-group (intern group))))
2604             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
2605               (while (and newsrc
2606                           (not (gnus-goto-char 
2607                                 (text-property-any 
2608                                  (point-min) (point-max) 'gnus-group 
2609                                  (intern group)))))
2610                 (setq newsrc (cdr newsrc))))))
2611       ;; Adjust cursor point.
2612       (gnus-group-position-cursor))))
2613
2614 (defun gnus-group-prepare-flat (level &optional all lowest) 
2615   "List all newsgroups with unread articles of level LEVEL or lower.
2616 If ALL is non-nil, list groups that have no unread articles.
2617 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher."
2618   (set-buffer gnus-group-buffer)
2619   (let ((buffer-read-only nil)
2620         (newsrc (cdr gnus-newsrc-assoc))
2621         (lowest (or lowest 1))
2622         info clevel unread group)
2623     (erase-buffer)
2624     (if (< lowest 8)
2625         ;; List living groups.
2626         (while newsrc
2627           (setq info (car newsrc)
2628                 group (car info)
2629                 newsrc (cdr newsrc)
2630                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
2631           (and unread ; This group might be bogus
2632                (<= (setq clevel (car (cdr info))) level) 
2633                (>= clevel lowest)
2634                (or all            ; We list all groups?
2635                    (eq unread t)  ; We list unactivated groups
2636                    (> unread 0)   ; We list groups with unread articles
2637                    (cdr (assq 'tick (nth 3 info)))) ; And groups with tickeds
2638                (gnus-group-insert-group-line 
2639                 nil group (car (cdr info)) (nth 3 info) unread (nth 4 info)))))
2640
2641     ;; List dead groups.
2642     (and (>= level 8) (<= lowest 8)
2643          (gnus-group-prepare-flat-list-dead gnus-zombie-list 8 ?Z))
2644     (and (>= level 9) (<= lowest 9)
2645          (gnus-group-prepare-flat-list-dead gnus-killed-list 9 ?K))
2646
2647     (gnus-group-set-mode-line)
2648     (setq gnus-have-all-newsgroups all)
2649     (run-hooks 'gnus-group-prepare-hook)))
2650
2651 (defun gnus-group-prepare-flat-list-dead (groups level mark)
2652   ;; List zombies and killed lists somehwat faster, which was
2653   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>. It does
2654   ;; this by ignoring the group format specification altogether.
2655   (let (group beg)
2656     (while groups
2657       (setq group (car groups)
2658             groups (cdr groups))
2659       (setq beg (point))
2660       (insert (format " %c    *: %s\n" mark group))
2661       (add-text-properties 
2662        beg (1+ beg) 
2663        (list 'gnus-group (intern group)
2664              'gnus-unread t
2665              'gnus-level level)))))
2666
2667 (defun gnus-group-real-name (group)
2668   "Find the real name of a foreign newsgroup."
2669   (if (string-match "^[^:]+:" group)
2670       (substring group (match-end 0))
2671     group))
2672
2673 (defun gnus-group-prefixed-name (group method)
2674   "Return the whole name from GROUP and METHOD."
2675   (concat (format "%s" (car method))
2676           (if (assoc (format "%s" (car method)) (gnus-methods-using 'address))
2677               (concat "+" (nth 1 method)))
2678           ":" group))
2679
2680 (defun gnus-group-real-prefix (group)
2681   "Return the prefix of the current group name."
2682   (if (string-match "^[^:]+:" group)
2683       (substring group 0 (match-end 0))
2684     ""))
2685
2686 (defun gnus-group-method-name (group)
2687   "Return the method used for selecting GROUP."
2688   (let ((prefix (gnus-group-real-prefix group)))
2689     (if (equal prefix "")
2690         gnus-select-method
2691       (if (string-match "^[^\\+]+\\+" prefix)
2692           (list (intern (substring prefix 0 (1- (match-end 0))))
2693                 (substring prefix (match-end 0) (1- (length prefix))))
2694         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
2695
2696 (defun gnus-group-foreign-p (group)
2697   "Return nil if GROUP is native, non-nil if it is foreign."
2698   (string-match ":" group))
2699
2700 (defun gnus-group-set-info (info)
2701   (let ((entry (gnus-gethash (car info) gnus-newsrc-hashtb)))
2702     (if entry
2703         (progn
2704           (setcar (nthcdr 2 entry) info)
2705           (if (and (not (eq (car entry) t)) 
2706                    (gnus-gethash (car info) gnus-active-hashtb))
2707               (let ((marked (nth 3 info)))
2708                 (setcar entry 
2709                         (max 0 (- (length (gnus-list-of-unread-articles 
2710                                            (car info)))
2711                                   (length (cdr (assq 'tick marked)))
2712                                   (length (cdr (assq 'dormant marked)))))))))
2713       (error "No such group: %s" (car info)))))
2714
2715 (defun gnus-group-update-group-line ()
2716   "This function updates the current line in the newsgroup buffer and
2717 moves the point to the colon."
2718   (let* ((buffer-read-only nil)
2719          (group (gnus-group-group-name))
2720          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
2721     (if entry
2722         (gnus-dribble-enter 
2723          (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
2724                  ")")))
2725     (beginning-of-line)
2726     (delete-region (point) (save-excursion (forward-line 1) (point)))
2727     (gnus-group-insert-group-line-info group)
2728     (forward-line -1)
2729     (gnus-group-position-cursor)))
2730
2731 (defun gnus-group-insert-group-line-info (group)
2732   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
2733         active info)
2734     (if entry
2735         (progn
2736           (setq info (nth 2 entry))
2737           (gnus-group-insert-group-line 
2738            nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
2739       (setq active (gnus-gethash group gnus-active-hashtb))
2740       (gnus-group-insert-group-line 
2741        nil group (if (member group gnus-zombie-list) 8 9)
2742        nil (- (1+ (cdr active)) (car active)) nil))))
2743
2744 (defun gnus-group-insert-group-line (gformat group level marked number method)
2745   (let* ((gformat (or gformat gnus-group-line-format-spec))
2746          (active (gnus-gethash group gnus-active-hashtb))
2747          (number-total (if active (1+ (- (cdr active) (car active))) 0))
2748          (number-of-dormant (length (cdr (assq 'dormant marked))))
2749          (number-of-ticked (length (cdr (assq 'tick marked))))
2750          (number-of-ticked-and-dormant
2751           (+ number-of-ticked number-of-dormant))
2752          (number-of-unread-unticked 
2753           (if (numberp number) (int-to-string (max 0 number))
2754             "*"))
2755          (number-of-read
2756           (if (numberp number)
2757               (max 0 (- number-total number))
2758             "*"))
2759          (subscribed (cond ((< level 6) ? )
2760                            ((< level 8) ?U)
2761                            ((= level 8) ?Z)
2762                            (t ?K)))
2763          (qualified-group (gnus-group-real-name group))
2764          (newsgroup-description 
2765           (if gnus-description-hashtb
2766               (or (gnus-gethash group gnus-description-hashtb) "")
2767             ""))
2768          (moderated (if (member group gnus-moderated-list) ?m ? ))
2769          (moderated-string (if (eq moderated ?m) "(m)" ""))
2770          (news-server (or (car (cdr method)) ""))
2771          (news-method (or (car method) ""))
2772          (news-method-string 
2773           (if method (format "(%s:%s)" (car method) (car (cdr method))) ""))
2774          (marked (if (and 
2775                       (numberp number) 
2776                       (zerop number)
2777                       (> number-of-ticked 0))
2778                      ?* ? ))
2779          (number (if (eq number t) "*" (+ number number-of-dormant 
2780                                           number-of-ticked)))
2781          (buffer-read-only nil)
2782          b)
2783     (beginning-of-line)
2784     (setq b (point))
2785     ;; Insert the visible text.
2786     (insert-before-markers (eval gformat))
2787     (add-text-properties 
2788      b (1+ b) (list 'gnus-group (intern group)
2789                     'gnus-unread (if (numberp number)
2790                                      (string-to-int number-of-unread-unticked)
2791                                    t)
2792                     'gnus-marked marked
2793                     'gnus-level level))))
2794
2795 (defun gnus-group-update-group (group &optional visible-only)
2796   "Update newsgroup info of GROUP.
2797 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't already."
2798   (save-excursion
2799     (set-buffer gnus-group-buffer)
2800     (let ((buffer-read-only nil)
2801           (visible nil))
2802       (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
2803         (if entry
2804             (gnus-dribble-enter 
2805              (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
2806                      ")"))))
2807       ;; Buffer may be narrowed.
2808       (save-restriction
2809         (widen)
2810         ;; Search a line to modify.  If the buffer is large, the search
2811         ;; takes long time.  In most cases, current point is on the line
2812         ;; we are looking for.  So, first of all, check current line. 
2813         (if (or (progn
2814                   (beginning-of-line)
2815                   (eq (get-text-property (point) 'gnus-group)
2816                       (intern group)))
2817                 (progn
2818                   (gnus-goto-char 
2819                    (text-property-any 
2820                     (point-min) (point-max) 'gnus-group (intern group)))))
2821             ;; GROUP is listed in current buffer. So, delete old line.
2822             (progn
2823               (setq visible t)
2824               (beginning-of-line)
2825               (delete-region (point) (progn (forward-line 1) (point))))
2826           ;; No such line in the buffer, find out where it's supposed to
2827           ;; go, and insert it there (or at the end of the buffer).
2828           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
2829           (or visible-only
2830               (let ((entry (cdr (gnus-gethash group gnus-newsrc-hashtb))))
2831                 (while (and entry
2832                             (not
2833                              (gnus-goto-char
2834                               (text-property-any
2835                                (point-min) (point-max) 
2836                                'gnus-group (intern (car (car entry)))))))
2837                   (setq entry (cdr entry)))
2838                 (or entry (goto-char (point-max))))))
2839         (if (or visible (not visible-only))
2840             (progn
2841               (gnus-group-insert-group-line-info group)
2842               (forward-line -1)         ; Move point back to the inserted line.
2843               ))))
2844     (gnus-group-set-mode-line)))
2845
2846 (defun gnus-group-set-mode-line ()
2847   (if (memq 'group gnus-updated-mode-lines)
2848       (let* ((gformat (or gnus-group-mode-line-format-spec
2849                           (setq gnus-group-mode-line-format-spec
2850                                 (gnus-parse-format 
2851                                  gnus-group-mode-line-format 
2852                                  gnus-group-mode-line-format-alist))))
2853              (news-server (car (cdr gnus-select-method)))
2854              (news-method (car gnus-select-method))
2855              (mode-string (eval gformat))
2856              (max-len 60))
2857         (if (> (length mode-string) max-len) 
2858             (setq mode-string (substring mode-string 0 (- max-len 4))))
2859         (setq mode-line-buffer-identification mode-string)
2860         (set-buffer-modified-p t))))
2861
2862 (defun gnus-group-group-name ()
2863   "Get the name of the newsgroup on the current line."
2864   (let ((group (get-text-property 
2865                 (save-excursion (beginning-of-line) (point)) 'gnus-group)))
2866     (and group (symbol-name group))))
2867
2868 (defun gnus-group-group-level ()
2869   "Get the level of the newsgroup on the current line."
2870   (get-text-property (save-excursion (beginning-of-line) (point)) 'gnus-level))
2871
2872 (defun gnus-group-search-forward (&optional backward all level first-too)
2873   "Find the next newsgroup with unread articles.
2874 If BACKWARD is non-nil, find the previous newsgroup instead.
2875 If ALL is non-nil, just find any newsgroup.
2876 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
2877 group exists.
2878 If FIRST-TOO, the current line is also eligeble as a target."
2879   (let ((way (if backward -1 1))
2880         (low 10)
2881         (beg (point))
2882         pos found)
2883     (or first-too (forward-line way))
2884     (while (and 
2885             (not (eobp))
2886             (not (setq 
2887                   found 
2888                   (and (or all
2889                            (and
2890                             (let ((unread 
2891                                    (get-text-property (point) 'gnus-unread)))
2892                               (or (eq unread t) (and unread (> unread 0))))
2893                             (> 5 (get-text-property (point) 'gnus-level))))
2894                        (or (not level)
2895                            (let ((lev (get-text-property (point) 'gnus-level)))
2896                              (if (<= lev level)
2897                                  t
2898                                (if (< lev low)
2899                                    (progn
2900                                      (setq low lev)
2901                                      (setq pos (point))))
2902                                nil))))))
2903             (zerop (forward-line way))))
2904     (if found 
2905         (progn (gnus-group-position-cursor) t)
2906       (if pos (goto-char pos) (goto-char beg))
2907       nil)))
2908
2909 ;; Gnus group mode commands
2910
2911 (defun gnus-group-read-group (all &optional no-article group)
2912   "Read news in this newsgroup.
2913 If argument ALL is non-nil, already read articles become readable.
2914 If optional argument NO-ARTICLE is non-nil, no article body is displayed."
2915   (interactive "P")
2916   (let ((group (or group (gnus-group-group-name)))
2917         number active marked entry)
2918     (or group (error "No group on current line"))
2919     (setq marked 
2920           (nth 3 (nth 2 (setq entry (gnus-gethash group gnus-newsrc-hashtb)))))
2921     ;; This group might be a dead group. In that case we have to get
2922     ;; the number of unread articles from `gnus-active-hashtb'.
2923     (if entry
2924         (setq number (car entry))
2925       (if (setq active (gnus-gethash group gnus-active-hashtb))
2926           (setq number (- (1+ (cdr active)) (car active)))))
2927     (gnus-summary-read-group 
2928      group (or all (and (numberp number) 
2929                         (zerop (+ number (length (cdr (assq 'tick marked)))
2930                                   (length (cdr (assq 'dormant marked)))))))
2931      no-article)))
2932
2933 (defun gnus-group-select-group (all)
2934   "Select this newsgroup.
2935 No article is selected automatically.
2936 If argument ALL is non-nil, already read articles become readable."
2937   (interactive "P")
2938   (gnus-group-read-group all t))
2939
2940 (defun gnus-group-jump-to-group (group)
2941   "Jump to newsgroup GROUP."
2942   (interactive
2943    (list 
2944     (completing-read "Group: " gnus-active-hashtb nil t)))
2945   (let (b)
2946     ;; Either go to the line in the group buffer...
2947     (or (and (setq b (text-property-any (point-min) (point-max) 
2948                                         'gnus-group (intern group)))
2949              (goto-char b))
2950         ;; ... or insert the line.
2951         (progn (gnus-group-update-group group)
2952                (goto-char (text-property-any (point-min) (point-max) 
2953                                              'gnus-group (intern group))))))
2954   ;; Adjust cursor point.
2955   (gnus-group-position-cursor))
2956
2957 (defun gnus-group-next-group (n)
2958   "Go to next N'th newsgroup.
2959 If N is negative, search backward instead.
2960 Returns the difference between N and the number of skips actually
2961 done."
2962   (interactive "p")
2963   (gnus-group-next-unread-group n t))
2964
2965 (defun gnus-group-next-unread-group (n &optional all level)
2966   "Go to next N'th unread newsgroup.
2967 If N is negative, search backward instead.
2968 If ALL is non-nil, choose any newsgroup, unread or not.
2969 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
2970 such group can be found, the next group with a level higher than
2971 LEVEL.
2972 Returns the difference between N and the number of skips actually
2973 made."
2974   (interactive "p")
2975   (let ((backward (< n 0))
2976         (n (abs n)))
2977     (while (and (> n 0)
2978                 (gnus-group-search-forward backward all level))
2979       (setq n (1- n)))
2980     (if (/= 0 n) (message "No more%s newsgroups%s" (if all "" " unread")
2981                           (if level " on this level or higher" "")))
2982     n))
2983
2984 (defun gnus-group-prev-group (n)
2985   "Go to previous N'th newsgroup.
2986 Returns the difference between N and the number of skips actually
2987 done."
2988   (interactive "p")
2989   (gnus-group-next-unread-group (- n) t))
2990
2991 (defun gnus-group-prev-unread-group (n)
2992   "Go to previous N'th unread newsgroup.
2993 Returns the difference between N and the number of skips actually
2994 done."  
2995   (interactive "p")
2996   (gnus-group-next-unread-group (- n)))
2997
2998 (defun gnus-group-next-unread-group-same-level (n)
2999   "Go to next N'th unread newsgroup on the same level.
3000 If N is negative, search backward instead.
3001 Returns the difference between N and the number of skips actually
3002 done."
3003   (interactive "p")
3004   (gnus-group-next-unread-group n t (gnus-group-group-level))
3005   (gnus-group-position-cursor))
3006
3007 (defun gnus-group-prev-unread-group-same-level (n)
3008   "Go to next N'th unread newsgroup on the same level.
3009 Returns the difference between N and the number of skips actually
3010 done."
3011   (interactive "p")
3012   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
3013   (gnus-group-position-cursor))
3014
3015 (defun gnus-group-best-unread-group ()
3016   "Go to the group with the highest level."
3017   (interactive)
3018   (goto-char (point-min))
3019   (let ((best 10)
3020         unread best-point)
3021     (while (setq unread (get-text-property (point) 'gnus-unread))
3022       (if (and (numberp unread) (> unread 0))
3023           (progn
3024             (or best-point (setq best-point (point)))
3025             (if (< (get-text-property (point) 'gnus-level) best)
3026                 (progn 
3027                   (setq best (get-text-property (point) 'gnus-level))
3028                   (setq best-point (point))))))
3029       (forward-line 1))
3030     (if best-point (goto-char best-point))
3031     (gnus-summary-position-cursor)
3032     (and best-point (gnus-group-group-name))))
3033
3034 (defun gnus-group-add-group (name method address)
3035   "Add a new newsgroup.
3036 The user will be prompted for a NAME, for a select METHOD, and an
3037 ADDRESS."
3038   (interactive
3039    (list 
3040     (read-string "Group name: ")
3041     (completing-read (format "%s method: " name) 
3042                      gnus-valid-select-methods nil t)
3043     (read-string (format "Get %s by method %s from: " name method))))
3044   (let ((nname (gnus-group-prefixed-name name (list (intern method) address))))
3045     (gnus-group-change-level 
3046      (list t nname 3 nil nil (list (intern method) address))
3047      3 9 (gnus-gethash (or (gnus-group-group-name) "dummy.group")
3048                        gnus-newsrc-hashtb) t)
3049     (gnus-group-insert-group-line-info nname)))
3050
3051 (defun gnus-group-edit-group ()
3052   (interactive)
3053   (let ((group (gnus-group-group-name))
3054         info)
3055     (if group (setq info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
3056       (error "No group on current line"))
3057     (switch-to-buffer (get-buffer-create gnus-group-edit-buffer))
3058     (gnus-add-current-to-buffer-list)
3059     (emacs-lisp-mode)
3060     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3061     (use-local-map (copy-keymap emacs-lisp-mode-map))
3062     (local-set-key "\C-c\C-c" 'gnus-group-edit-group-done)
3063     (erase-buffer)
3064     (insert ";; Type `C-c C-c' after you have edited the newsgroup entry.\n\n")
3065     (insert (format "(gnus-group-set-info\n  '%S)\n" info))))
3066
3067 (defun gnus-group-edit-group-done ()
3068   (interactive)
3069   (set-buffer (get-buffer-create gnus-group-edit-buffer))
3070   (eval-current-buffer)
3071   (kill-buffer (current-buffer))
3072   (set-buffer gnus-group-buffer)
3073   (gnus-group-update-group (gnus-group-group-name))
3074   (gnus-group-position-cursor))
3075
3076 (defun gnus-group-make-doc-group ()
3077   "Create the (ding) Gnus documentation group."
3078   (interactive)
3079   (let ((path load-path))
3080     (while (and path
3081                 (not (file-exists-p (concat (file-name-as-directory (car path))
3082                                             "doc.txt"))))
3083       (setq path (cdr path)))
3084     (or path (error "Couldn't find doc group"))
3085     (gnus-group-add-group 
3086      "gnus-doc" "nndoc" 
3087      (concat (file-name-as-directory (car path)) "doc.txt"))))
3088
3089 (defun gnus-group-make-directory-group (dir)
3090   "Create an nndir group.
3091 The user will be prompted for a directory. The contents of this
3092 directory will be used as a newsgroup. The directory should contain
3093 mail messages or news articles in files that have numeric names."
3094   (interactive
3095    (list (read-file-name "Create group from directory: ")))
3096   (or (file-exists-p dir) (error "No such directory"))
3097   (or (file-directory-p dir) (error "Not a directory"))
3098   (gnus-group-add-group dir "nndir" dir))
3099
3100 (defun gnus-group-catchup-current (n &optional all)
3101   "Mark all articles not marked as unread in current newsgroup as read.
3102 If prefix argument N is numeric, the ARG next newsgroups will be
3103 caught up. If ALL is non-nil, marked articles will also be marked as
3104 read. Cross references (Xref: header) of articles are ignored.
3105 The difference between N and actual number of newsgroups that were
3106 caught up is returned."
3107   (interactive "p")
3108   (if (or (not gnus-interactive-catchup) ;Without confirmation?
3109           gnus-expert-user
3110           (gnus-y-or-n-p
3111            (if all
3112                "Do you really want to mark all articles as read? "
3113              "Mark all unread articles as read? ")))
3114       (progn
3115         (while 
3116             (and (> n 0)
3117                  (progn
3118                    (setq n (1- n))
3119                    (gnus-group-catchup (gnus-group-group-name) all)
3120                    (gnus-group-update-group-line)
3121                    t)
3122                  (zerop (gnus-group-next-unread-group 1))))))
3123   n)
3124
3125 (defun gnus-group-catchup-current-all (n)
3126   "Mark all articles in current newsgroup as read.
3127 Cross references (Xref: header) of articles are ignored."
3128   (interactive "p")
3129   (gnus-group-catchup-current n 'all))
3130
3131 (defun gnus-group-catchup (group &optional all)
3132   "Mark all articles in GROUP as read.
3133 If ALL is non-nil, all articles are marked as read.
3134 The return value is the number of articles that were marked as read,
3135 or nil if no action could be taken."
3136   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
3137          (num (car entry))
3138          (marked (nth 3 (nth 2 entry)))
3139          ticked)
3140     (if (not (numberp (car entry)))
3141         (message "Can't catch up; non-active group")
3142       ;; Do the updating only if the newsgroup isn't killed
3143       (if entry
3144           (progn
3145             (setq ticked (if all nil (cdr (assq 'tick marked))))
3146             (gnus-update-read-articles group ticked nil ticked)
3147             (if (and all marked)
3148                 (setcar (nthcdr 3 (nth 2 entry)) 
3149                         (delq (assq 'dormant marked) marked))))))
3150     num))
3151
3152 (defun gnus-group-expire-articles (newsgroup)
3153   "Expire all expirable articles in the current newsgroup."
3154   (interactive (list (gnus-group-group-name)))
3155   (if (not newsgroup) (error "No current newsgroup"))
3156   (let ((expirable 
3157          (assq 'expire (nth 3 (nth 2 (gnus-gethash newsgroup 
3158                                                    gnus-newsrc-hashtb))))))
3159  (and expirable 
3160       (gnus-check-backend-function 'request-expire-articles newsgroup)
3161       (setcdr expirable
3162               (gnus-request-expire-articles (cdr expirable) newsgroup)))))
3163
3164 (defun gnus-group-expire-all-groups ()
3165   "Expire all expirable articles in all newsgroups."
3166   (interactive)
3167   (message "Expiring...")
3168   (let ((newsrc (cdr gnus-newsrc-assoc)))
3169     (while newsrc
3170       (gnus-group-expire-articles (car (car newsrc)))
3171       (setq newsrc (cdr newsrc))))
3172   (message "Expiring...done"))
3173
3174 (defun gnus-group-set-current-level (n)
3175   "Set the level of the current group to the numeric prefix."
3176   (interactive "P")
3177   (setq n (or n (string-to-int 
3178                  (completing-read 
3179                   "Level: " 
3180                   (mapcar (lambda (n) (list (char-to-string n))) "123456789")
3181                   nil t))))
3182   (let ((group (gnus-group-group-name)))
3183     (if (not group) (error "No newsgroup on current line.")
3184     (if (and (numberp n) (>= n 1) (<= n 9))
3185         (progn
3186           (message "Changed level of %s from %d to %d" 
3187                    group (gnus-group-group-level) n)
3188           (gnus-group-change-level group n (gnus-group-group-level))
3189           (gnus-group-update-group-line))
3190       (error "Illegal level: %s" n))))
3191   (forward-line 1)
3192   (gnus-group-position-cursor))
3193
3194 (defun gnus-group-unsubscribe-current-group (arg)
3195   "Toggle subscribe from/to unsubscribe current group."
3196   (interactive "P")
3197   (let ((group (gnus-group-group-name)))
3198     (or group (error "No newsgroup on current line"))
3199     (or arg (setq arg (if (<= (gnus-group-group-level) 5) 6 3)))
3200     (gnus-group-unsubscribe-group group arg)))
3201
3202 (defun gnus-group-unsubscribe-group (group &optional level)
3203   "Toggle subscribe from/to unsubscribe GROUP.
3204 New newsgroup is added to .newsrc automatically."
3205   (interactive
3206    (list (completing-read "Group: " gnus-active-hashtb nil 
3207                           gnus-have-read-active-file)))
3208   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
3209     (cond (newsrc
3210            ;; Toggle subscription flag.
3211            (gnus-group-change-level 
3212             newsrc (if level level (if (< (nth 1 (nth 2 newsrc)) 6) 6 4)))
3213            (gnus-group-update-group group))
3214           ((and (stringp group)
3215                 (gnus-gethash group gnus-active-hashtb))
3216            ;; Add new newsgroup.
3217            (gnus-group-change-level 
3218             group 
3219             (if level level 3) 
3220             (if (member group gnus-zombie-list) 8 9)
3221             (or (and (gnus-group-group-name)
3222                      (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb))
3223                 (gnus-gethash (car (car gnus-newsrc-assoc)) 
3224                               gnus-newsrc-hashtb)))
3225            (gnus-group-update-group group))
3226           (t (error "No such newsgroup: %s" group)))
3227     (gnus-group-position-cursor)))
3228
3229 (defun gnus-group-transpose-groups (arg)
3230   "Exchange current newsgroup and previous newsgroup.
3231 With argument ARG, takes previous newsgroup and moves it past ARG newsgroup."
3232   (interactive "p")
3233   ;; BUG: last newsgroup and the last but one cannot be transposed
3234   ;; since gnus-group-search-forward does not move forward beyond the
3235   ;; last.  If we instead use forward-line, no problem, but I don't
3236   ;; want to use it for later extension.
3237   (while (> arg 0)
3238     (gnus-group-search-forward t t)
3239     (gnus-group-kill-group 1)
3240     (gnus-group-search-forward nil t)
3241     (gnus-group-yank-group)
3242     (gnus-group-search-forward nil t)
3243     (setq arg (1- arg))))
3244
3245 (defun gnus-group-kill-all-zombies ()
3246   "Kill all zombie newsgroups."
3247   (interactive)
3248   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
3249   (setq gnus-zombie-list nil)
3250   (funcall gnus-group-prepare-function 5 nil nil)
3251   (goto-char (point-min))
3252   (gnus-group-position-cursor))
3253
3254 (defun gnus-group-kill-region (begin end)
3255   "Kill newsgroups in current region (excluding current point).
3256 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
3257   (interactive "r")
3258   (let ((lines
3259          ;; Exclude a line where current point is on.
3260          (1-
3261           ;; Count lines.
3262           (save-excursion
3263             (count-lines
3264              (progn
3265                (goto-char begin)
3266                (beginning-of-line)
3267                (point))
3268              (progn
3269                (goto-char end)
3270                (end-of-line)
3271                (point)))))))
3272     (goto-char begin)
3273     (beginning-of-line)                 ;Important when LINES < 1
3274     (gnus-group-kill-group lines)))
3275
3276 (defun gnus-group-kill-group (n)
3277   "The the next N groups.
3278 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
3279 However, only groups that were alive can be yanked; already killed 
3280 groups or zombie groups can't be yanked.
3281 The return value is the name of the (last) newsgroup that was killed."
3282   (interactive "p")
3283   (let ((buffer-read-only nil)
3284         group entry level)
3285     (while (>= (setq n  (1- n)) 0)
3286       (setq group (gnus-group-group-name))
3287       (or group
3288           (signal 'end-of-buffer nil))
3289       (setq level (gnus-group-group-level))
3290       (beginning-of-line)
3291       (delete-region (point) (progn (forward-line 1) (point)))
3292       (if (setq entry (gnus-gethash group gnus-newsrc-hashtb))
3293           (setq gnus-list-of-killed-groups 
3294                 (cons (cons (car entry) (nth 2 entry)) 
3295                       gnus-list-of-killed-groups)))
3296       (gnus-group-change-level (if entry entry group) 9 (if entry nil level)))
3297     (if (eobp)
3298         (forward-line -1))
3299     (gnus-group-position-cursor)
3300     group))
3301
3302 (defun gnus-group-yank-group (&optional arg)
3303   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
3304 inserting it before the current newsgroup.  The numeric ARG specifies
3305 how many newsgroups are to be yanked.  The name of the (last)
3306 newsgroup yanked is returned."
3307   (interactive "p")
3308   (if (not arg) (setq arg 1))
3309   (let (info group prev)
3310     (while (>= (setq arg (1- arg)) 0)
3311       (if (not (setq info (car gnus-list-of-killed-groups)))
3312           (error "No more newsgroups to yank"))
3313       (setq group (nth 2 info))
3314       ;; Find which newsgroup to insert this one before - search
3315       ;; backward until something suitable is found. If there are no
3316       ;; other newsgroups in this buffer, just make this newsgroup the
3317       ;; first newsgroup.
3318       (while (and (not (setq prev (gnus-group-group-name)))
3319                   (zerop (forward-line -1))))
3320       (if (not prev)
3321           (setq prev (car (car gnus-newsrc-assoc))))
3322       (gnus-group-change-level 
3323        info (nth 2 info) 9 
3324        (gnus-gethash prev gnus-newsrc-hashtb)
3325        t)
3326       (gnus-group-insert-group-line-info (nth 1 info))
3327       (setq gnus-list-of-killed-groups 
3328             (cdr gnus-list-of-killed-groups)))
3329     (forward-line -1)
3330     (gnus-group-position-cursor)
3331     group))
3332       
3333 (defun gnus-group-list-all-groups (arg)
3334   "List all newsgroups with level ARG or lower.
3335 Default is 7, which lists all subscribed and most unsubscribed groups."
3336   (interactive "P")
3337   (setq arg (or arg 7))
3338   (gnus-group-list-groups arg t))
3339
3340 (defun gnus-group-list-killed ()
3341   "List all killed newsgroups in the group buffer."
3342   (interactive)
3343   (if (not gnus-killed-list)
3344       (message "No killed groups")
3345     (funcall gnus-group-prepare-function 9 t 9)
3346     (goto-char (point-min)))
3347   (gnus-group-position-cursor))
3348
3349 (defun gnus-group-list-zombies ()
3350   "List all zombie newsgroups in the group buffer."
3351   (interactive)
3352   (if (not gnus-zombie-list)
3353       (message "No zombie groups")
3354     (funcall gnus-group-prepare-function 8 t 8)
3355     (goto-char (point-min)))
3356   (gnus-group-position-cursor))
3357
3358 (defun gnus-group-get-new-news (&optional arg)
3359   "Get newly arrived articles.
3360 If ARG is non-nil, it should be a number between one and nine to
3361 specify which levels you are interested in re-scanning."
3362   (interactive "P")
3363   (if (and gnus-read-active-file (not arg))
3364       (progn
3365         (gnus-read-active-file)
3366         (gnus-get-unread-articles (or arg 6)))
3367     (let ((gnus-read-active-file nil))
3368       (gnus-get-unread-articles (or arg 6))))
3369   (gnus-group-list-groups 5 gnus-have-all-newsgroups))
3370
3371 (defun gnus-group-get-new-news-this-group (n)
3372   "Check for newly arrived news in the current group (and the N-1 next groups).
3373 The difference between N and the number of newsgroup checked is returned.
3374 If N is negative, this group and the N-1 previous groups will be checked."
3375   (interactive "p")
3376   (let ((way (if (< n 0) -1 1))
3377         (n (abs n))
3378         (w-p (window-start))
3379         group)
3380     (while (and (> n 0)
3381                 (gnus-get-new-news-in-group (gnus-group-group-name))
3382                 (zerop (gnus-group-next-group way)))
3383       (setq n (1- n)))
3384     (if (/= 0 n) (message "No more newsgroups"))
3385     ;; !!! I don't know why the buffer scrolls forward when updating
3386     ;; the first line in the group buffer, but it does. So we set the
3387     ;; window start forcibly.
3388     (set-window-start (get-buffer-window (current-buffer)) w-p)
3389     n))
3390
3391 (defun gnus-get-new-news-in-group (group)
3392   (if (and group (gnus-activate-newsgroup group))
3393       (progn
3394         (gnus-get-unread-articles-in-group 
3395          (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
3396          (gnus-gethash group gnus-active-hashtb))
3397         (gnus-group-update-group-line)))
3398   t)
3399   
3400 (defun gnus-group-describe-group (&optional group)
3401   "Display a description of the current newsgroup."
3402   (interactive)
3403   (let ((group (or group (gnus-group-group-name))))
3404     (if (not group)
3405         (message "No group on current line")
3406       (and (or gnus-description-hashtb
3407                (gnus-read-descriptions-file))
3408            (message
3409             (or (gnus-gethash group gnus-description-hashtb)
3410                 "No description available"))))))
3411
3412 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
3413 (defun gnus-group-describe-all-groups ()
3414   "Pop up a buffer with descriptons of all newsgroups."
3415   (interactive)
3416   (if (not (or gnus-description-hashtb
3417                (gnus-read-descriptions-file)))
3418       (error "Couldn't request descriptions file"))
3419   (let ((buffer-read-only nil)
3420         b)
3421     (erase-buffer)
3422     (mapatoms
3423      (lambda (group)
3424        (setq b (point))
3425        (insert (format "      *: %-20s %s\n" (symbol-name group)
3426                        (symbol-value group)))
3427        (add-text-properties 
3428         b (1+ b) (list 'gnus-group group
3429                        'gnus-unread t 'gnus-marked nil 'gnus-level 6)))
3430      gnus-description-hashtb)
3431     (goto-char (point-min))
3432     (gnus-group-position-cursor)))
3433
3434 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
3435 (defun gnus-group-apropos (regexp &optional search-description)
3436   "List all newsgroups that have names that match a regexp."
3437   (interactive "sGnus apropos (regexp): ")
3438   (let ((prev "")
3439         (obuf (current-buffer))
3440         groups des prev)
3441     ;; Go through all newsgroups that are known to Gnus.
3442     (mapatoms 
3443      (lambda (group)
3444        (and (string-match regexp (symbol-name group))
3445             (setq groups (cons (symbol-name group) groups))))
3446      gnus-active-hashtb)
3447     ;; Go through all descriptions that are known to Gnus. 
3448     (if search-description
3449         (mapatoms 
3450          (lambda (group)
3451            (and (string-match regexp (symbol-value group))
3452                 (gnus-gethash (symbol-name group) gnus-active-hashtb)
3453                 (setq groups (cons (symbol-name group) groups))))
3454          gnus-description-hashtb))
3455     (if (not groups)
3456         (message "No groups matched \"%s\"." regexp)
3457       ;; Print out all the groups.
3458       (save-excursion
3459         (pop-to-buffer (get-buffer-create "*Gnus Help*"))
3460         (buffer-disable-undo (current-buffer))
3461         (erase-buffer)
3462         (setq groups (sort groups 'string<))
3463         (while groups
3464           ;; Groups may be entered twice into the list of groups.
3465           (if (not (string= (car groups) prev))
3466               (progn
3467                 (insert (setq prev (car groups)) "\n")
3468                 (if (and gnus-description-hashtb
3469                          (setq des (gnus-gethash (car groups) 
3470                                                  gnus-description-hashtb)))
3471                     (insert "  " des "\n"))))
3472           (setq groups (cdr groups)))
3473         (goto-char 1)))
3474     (pop-to-buffer obuf)))
3475
3476 (defun gnus-group-description-apropos (regexp)
3477   "List all newsgroups that have names or desccriptions that match a regexp."
3478   (interactive "sGnus description apropos (regexp): ")
3479   (if (not (or gnus-description-hashtb
3480                (gnus-read-descriptions-file)))
3481       (error "Couldn't request descriptions file"))
3482   (gnus-group-apropos regexp t))
3483
3484 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
3485 (defun gnus-group-list-matching (regexp) 
3486   "List all newsgroups with unread articles that match REGEXP."
3487   (interactive "sList newsgroups matching: ")
3488   (set-buffer gnus-group-buffer)
3489   (let ((buffer-read-only nil)
3490         (newsrc (cdr gnus-newsrc-assoc))
3491         (zombie gnus-zombie-list)
3492         (killed gnus-killed-list)
3493         info unread active group)
3494     (erase-buffer)
3495
3496     ;; List alive newsgroups.
3497     (while newsrc
3498       (setq info (car newsrc)
3499             group (car info)
3500             newsrc (cdr newsrc)
3501             unread (car (gnus-gethash group gnus-newsrc-hashtb)))
3502       (if (and unread ; This group might be bogus
3503                (string-match regexp group))
3504           (gnus-group-insert-group-line 
3505            nil group (car (cdr info)) (nth 3 info) unread
3506            (nth 4 info))))
3507
3508     ;; List zombies and killed lists.
3509     (let ((lists (list 'gnus-zombie-list 'gnus-killed-list))
3510           mark b)
3511       (while lists
3512         (if (eq (car lists) 'gnus-zombie-list)
3513             (setq mark ?Z)
3514           (setq mark ?K))
3515         (setq newsrc (set (car lists)
3516                           (sort (symbol-value (car lists)) 
3517                                 (function string<))))
3518         (while newsrc
3519           (setq group (car newsrc)
3520                 newsrc (cdr newsrc))
3521           (if (not (string-match regexp group))
3522               ()
3523             (setq b (point))
3524             (insert (format " %c    *: %s" mark group))
3525             (add-text-properties 
3526              b (1+ b) 
3527              (list 'gnus-group (intern group)
3528                    'gnus-unread t
3529                    'gnus-level (if (= mark ?Z) 8 9)))))
3530         (setq lists (cdr lists))))
3531
3532     (gnus-group-set-mode-line)
3533     (setq gnus-have-all-newsgroups t)
3534     (run-hooks 'gnus-group-prepare-hook))
3535   (goto-char (point-min))
3536   (gnus-group-position-cursor))
3537
3538 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
3539 (defun gnus-group-save-newsrc ()
3540   "Save the Gnus startup files."
3541   (interactive)
3542   (gnus-save-newsrc-file))
3543
3544 (defun gnus-group-restart (&optional arg)
3545   "Force Gnus to read the .newsrc file."
3546   (interactive "P")
3547   (gnus-save-newsrc-file)
3548   (gnus-setup-news 'force)
3549   (gnus-group-list-groups (or arg 5) gnus-have-all-newsgroups))
3550
3551 (defun gnus-group-read-init-file ()
3552   "Read the Gnus elisp init file."
3553   (interactive)
3554   (gnus-read-init-file))
3555
3556 (defun gnus-group-check-bogus-groups ()
3557   "Check bogus newsgroups."
3558   (interactive)
3559   (gnus-check-bogus-newsgroups (not gnus-expert-user))  ;Require confirmation.
3560   (gnus-group-list-groups 5 gnus-have-all-newsgroups))
3561
3562 (defun gnus-group-mail ()
3563   "Start composing a mail."
3564   (interactive)
3565   (mail))
3566
3567 (defun gnus-group-edit-global-kill ()
3568   "Edit a global kill file."
3569   (interactive)
3570   (setq gnus-current-kill-article nil)  ;No articles selected.
3571   (gnus-kill-file-edit-file nil)        ;Nil stands for global KILL file.
3572   (message
3573    (substitute-command-keys
3574     "Editing a global kill file (Type \\[gnus-kill-file-exit] to exit)")))
3575
3576 (defun gnus-group-edit-local-kill ()
3577   "Edit a local kill file."
3578   (interactive)
3579   (setq gnus-current-kill-article nil)  ;No articles selected.
3580   (gnus-kill-file-edit-file (gnus-group-group-name))
3581   (message
3582    (substitute-command-keys
3583     "Editing a local kill file (Type \\[gnus-kill-file-exit] to exit)")))
3584
3585 (defun gnus-group-force-update ()
3586   "Update `.newsrc' file."
3587   (interactive)
3588   (gnus-save-newsrc-file))
3589
3590 (defun gnus-group-suspend ()
3591   "Suspend the current Gnus session.
3592 In fact, cleanup buffers except for group mode buffer.
3593 The hook gnus-suspend-gnus-hook is called before actually suspending."
3594   (interactive)
3595   (run-hooks 'gnus-suspend-gnus-hook)
3596   ;; Kill Gnus buffers except for group mode buffer.
3597   (let ((group-buf (get-buffer gnus-group-buffer)))
3598     (while gnus-buffer-list
3599       (and (not (eq (get-buffer (car gnus-buffer-list)) group-buf))
3600            (get-buffer (car gnus-buffer-list))
3601            (buffer-name (get-buffer (car gnus-buffer-list)))
3602            (kill-buffer (car gnus-buffer-list)))
3603       (setq gnus-buffer-list (cdr gnus-buffer-list)))
3604     (setq gnus-buffer-list (list group-buf))
3605     (bury-buffer group-buf)
3606     (delete-windows-on group-buf t)))
3607
3608 (defun gnus-group-clear-dribble ()
3609   "Clear all information from the dribble buffer."
3610   (interactive)
3611   (gnus-dribble-clear))
3612
3613 (defun gnus-group-exit ()
3614   "Quit reading news after updating .newsrc.eld and .newsrc.
3615 The hook `gnus-exit-gnus-hook' is called before actually exiting."
3616   (interactive)
3617   (if (or noninteractive                ;For gnus-batch-kill
3618           (zerop (buffer-size))         ;No news is good news.
3619           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
3620           (not gnus-interactive-exit)   ;Without confirmation
3621           gnus-expert-user
3622           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
3623       (progn
3624         (run-hooks 'gnus-exit-gnus-hook)
3625         (gnus-save-newsrc-file)
3626         (gnus-clear-system))))
3627
3628 (defun gnus-group-quit ()
3629   "Quit reading news without updating .newsrc.eld or .newsrc.
3630 The hook `gnus-exit-gnus-hook' is called before actually exiting."
3631   (interactive)
3632   (if (or noninteractive                ;For gnus-batch-kill
3633           (zerop (buffer-size))
3634           (not (gnus-server-opened gnus-select-method))
3635           gnus-expert-user
3636           (not gnus-current-startup-file)
3637           (gnus-yes-or-no-p
3638            (format "Quit reading news without saving %s? "
3639                    (file-name-nondirectory gnus-current-startup-file))))
3640       (progn
3641         (run-hooks 'gnus-exit-gnus-hook)
3642         (gnus-dribble-save)
3643         (gnus-clear-system))))
3644
3645 (defun gnus-group-describe-briefly ()
3646   "Give a one line description of the group mode commands."
3647   (interactive)
3648   (message
3649    (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")))
3650
3651 (defun gnus-group-browse-foreign-server (method)
3652   "Browse a foreign news server.
3653 If called interactively, this function will ask for a select method
3654  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
3655 If not, METHOD should be a list where the first element is the method
3656 and the second element is the address."
3657   (interactive
3658    (list (list (intern (completing-read 
3659                         "Select method: "
3660                         gnus-valid-select-methods nil t "nntp"))
3661                ;; Suggested by mapjph@bath.ac.uk.
3662                (completing-read 
3663                 "Server name: " 
3664                 (mapcar (lambda (server) (list server))
3665                         gnus-secondary-servers)))))
3666   (gnus-browse-foreign-server method))
3667
3668 \f
3669 ;;;
3670 ;;; Browse Server Mode
3671 ;;;
3672
3673 (defvar gnus-browse-server-mode-hook nil)
3674 (defvar gnus-browse-server-mode-map nil)
3675
3676 (if gnus-browse-server-mode-map
3677     nil
3678   (setq gnus-browse-server-mode-map (make-keymap))
3679   (suppress-keymap gnus-browse-server-mode-map)
3680   (define-key gnus-browse-server-mode-map " " 'gnus-browse-read-group)
3681   (define-key gnus-browse-server-mode-map "=" 'gnus-browse-read-group)
3682   (define-key gnus-browse-server-mode-map "n" 'gnus-browse-next-group)
3683   (define-key gnus-browse-server-mode-map "p" 'gnus-browse-prev-group)
3684   (define-key gnus-browse-server-mode-map [del] 'gnus-browse-prev-group)
3685   (define-key gnus-browse-server-mode-map "N" 'gnus-browse-next-group)
3686   (define-key gnus-browse-server-mode-map "P" 'gnus-group-prev-group)
3687   (define-key gnus-browse-server-mode-map "\M-n" 'gnus-browse-next-group)
3688   (define-key gnus-browse-server-mode-map "\M-p" 'gnus-browse-prev-group)
3689   (define-key gnus-browse-server-mode-map "\r" 'gnus-browse-read-group)
3690   (define-key gnus-browse-server-mode-map "u" 'gnus-browse-unsubscribe-current-group)
3691   (define-key gnus-browse-server-mode-map "q" 'gnus-browse-exit)
3692   (define-key gnus-browse-server-mode-map "Q" 'gnus-browse-exit)
3693   (define-key gnus-browse-server-mode-map "\C-c\C-c" 'gnus-browse-quit)
3694   (define-key gnus-browse-server-mode-map "?" 'gnus-browse-describe-briefly)
3695   (define-key gnus-browse-server-mode-map "\C-c\C-i" 'gnus-info-find-node)
3696   )
3697
3698 (defvar gnus-browse-current-method nil)
3699
3700 (defun gnus-browse-foreign-server (method)
3701   (setq gnus-browse-current-method method)
3702   (let ((gnus-select-method method)
3703         groups group)
3704     (message "Connecting to %s..." (nth 1 method))
3705     (if (not (gnus-request-list method))
3706         (error "Unable to contact server: " (gnus-status-message method)))
3707     (set-buffer (get-buffer-create "*Gnus Browse Server*"))
3708     (gnus-add-current-to-buffer-list)
3709     (buffer-disable-undo (current-buffer))
3710     (let ((buffer-read-only nil))
3711       (erase-buffer))
3712     (gnus-browse-server-mode)
3713     (setq mode-line-buffer-identification
3714           (format
3715            "(ding) Browse Server {%s:%s}" (car method) (car (cdr method))))
3716     (save-excursion
3717       (set-buffer nntp-server-buffer)
3718       (let ((cur (current-buffer)))
3719         (goto-char 1)
3720         (while (re-search-forward 
3721                 "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
3722           (goto-char (match-end 1))
3723           (setq groups (cons (cons (buffer-substring (match-beginning 1)
3724                                                      (match-end 1))
3725                                    (- (read cur) (read cur)))
3726                              groups)))))
3727     (setq groups (sort groups 
3728                        (lambda (l1 l2)
3729                          (string< (car l1) (car l2)))))
3730     (let ((buffer-read-only nil))
3731       (while groups
3732         (setq group (car groups))
3733         (insert 
3734          (format "K%7d: %s\n" (cdr group) (car group)))
3735         (setq groups (cdr groups))))
3736     (switch-to-buffer (current-buffer))
3737     (goto-char 1)
3738     (gnus-group-position-cursor)))
3739
3740 (defun gnus-browse-server-mode ()
3741   "Major mode for reading network news."
3742   (interactive)
3743   (kill-all-local-variables)
3744   (setq mode-line-modified "-- ")
3745   (make-local-variable 'mode-line-format)
3746   (setq mode-line-format (copy-sequence mode-line-format))
3747   (and (equal (nth 3 mode-line-format) "   ")
3748        (setcar (nthcdr 3 mode-line-format) ""))
3749   (setq major-mode 'gnus-browse-server-mode)
3750   (setq mode-name "Browse Server")
3751   (setq mode-line-process nil)
3752   (use-local-map gnus-browse-server-mode-map)
3753   (buffer-disable-undo (current-buffer))
3754   (setq truncate-lines t)
3755   (setq buffer-read-only t)
3756   (run-hooks 'gnus-browse-server-mode-hook))
3757
3758 (defun gnus-browse-read-group ()
3759   "Not implemented, and will probably never be."
3760   (interactive)
3761   (error "You can't read while browsing"))
3762
3763 (defun gnus-browse-next-group (n)
3764   "Go to the next group."
3765   (interactive "p")
3766   (prog1
3767       (forward-line n)
3768     (gnus-group-position-cursor)))
3769
3770 (defun gnus-browse-prev-group (n)
3771   "Go to the next group."
3772   (interactive "p")
3773   (gnus-browse-next-group (- n)))
3774
3775 (defun gnus-browse-unsubscribe-current-group (arg)
3776   "(Un)subscribe to the next ARG groups."
3777   (interactive "p")
3778   (and (eobp)
3779        (error "No group at current line."))
3780   (let ((ward (if (< arg 0) -1 1))
3781         (arg (abs arg)))
3782     (while (and (> arg 0)
3783                 (not (eobp))
3784                 (gnus-browse-unsubscribe-group)
3785                 (zerop (gnus-browse-next-group ward)))
3786       (setq arg (1- arg)))
3787     (gnus-group-position-cursor)
3788     (if (/= 0 arg) (message "No more newsgroups" ))
3789     arg))
3790   
3791 (defun gnus-browse-unsubscribe-group ()
3792   (let ((sub nil)
3793         (buffer-read-only nil)
3794         group)
3795     (save-excursion
3796       (beginning-of-line)
3797       (if (= (following-char) ?K) (setq sub t))
3798       (re-search-forward ": \\(.*\\)$" nil t)
3799       (setq group (gnus-group-prefixed-name 
3800                    (buffer-substring (match-beginning 1) (match-end 1))
3801                    gnus-browse-current-method))
3802       (beginning-of-line)
3803       (delete-char 1)
3804       (if sub
3805           (progn
3806             (gnus-group-change-level 
3807              (list t group 3 nil nil gnus-browse-current-method) 3 9 
3808              (gnus-gethash (car (nth 1 gnus-newsrc-assoc)) gnus-newsrc-hashtb)
3809              t)
3810             (insert ? ))
3811         (gnus-group-change-level group 9 3)
3812         (insert ?K)))
3813     t))
3814
3815 (defun gnus-browse-exit ()
3816   "Quit browsing and return to the group buffer."
3817   (interactive)
3818   (if (eq major-mode 'gnus-browse-server-mode)
3819       (kill-buffer (current-buffer)))
3820   (switch-to-buffer gnus-group-buffer)
3821   (gnus-group-list-groups 5))
3822
3823 (defun gnus-browse-describe-briefly ()
3824   "Give a one line description of the group mode commands."
3825   (interactive)
3826   (message
3827    (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")))
3828       
3829 \f
3830 ;;;
3831 ;;; Gnus summary mode
3832 ;;;
3833
3834 (defvar gnus-summary-mode-map nil)
3835 (defvar gnus-summary-mark-map nil)
3836 (defvar gnus-summary-send-map nil)
3837 (defvar gnus-summary-extract-map nil)
3838 (defvar gnus-summary-extract-view-map nil)
3839 (defvar gnus-summary-article-map nil)
3840 (defvar gnus-summary-thread-map nil)
3841 (defvar gnus-summary-goto-map nil)
3842 (defvar gnus-summary-exit-map nil)
3843 (defvar gnus-summary-various-map nil)
3844 (defvar gnus-summary-interest-map nil)
3845 (defvar gnus-summary-process-map nil)
3846 (defvar gnus-summary-sort-map nil)
3847 (defvar gnus-summary-mgroup-map nil)
3848 (defvar gnus-summary-vkill-map nil)
3849 (defvar gnus-summary-increase-map nil)
3850 (defvar gnus-summary-inc-subject-map nil)
3851 (defvar gnus-summary-inc-author-map nil)
3852 (defvar gnus-summary-inc-xref-map nil)
3853 (defvar gnus-summary-inc-thread-map nil)
3854 (defvar gnus-summary-inc-fol-map nil)
3855 (defvar gnus-summary-lower-map nil)
3856 (defvar gnus-summary-low-subject-map nil)
3857 (defvar gnus-summary-low-author-map nil)
3858 (defvar gnus-summary-low-xref-map nil)
3859 (defvar gnus-summary-low-thread-map nil)
3860 (defvar gnus-summary-low-fol-map nil)
3861
3862 (if gnus-summary-mode-map
3863     nil
3864   (setq gnus-summary-mode-map (make-keymap))
3865   (suppress-keymap gnus-summary-mode-map)
3866
3867   ;;Non-orthogonal keys
3868
3869   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
3870   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
3871   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
3872   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
3873   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
3874   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
3875   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
3876   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
3877   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
3878   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
3879   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
3880   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
3881   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
3882   (define-key gnus-summary-mode-map "\M-s" 'gnus-summary-search-article-forward)
3883   (define-key gnus-summary-mode-map "\M-r" 'gnus-summary-search-article-backward)
3884   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
3885   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
3886   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-subject)
3887   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
3888   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
3889   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
3890   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
3891   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
3892   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
3893   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
3894   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
3895   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
3896   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
3897   (define-key gnus-summary-mode-map "k" 'gnus-summary-kill-same-subject-and-select)
3898   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
3899   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
3900   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
3901   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
3902   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
3903   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
3904   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
3905   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
3906   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
3907   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
3908   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
3909   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
3910   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
3911   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
3912   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
3913   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
3914   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
3915   (define-key gnus-summary-mode-map "\C-c\M-\C-s" 'gnus-summary-show-all-expunged)
3916   (define-key gnus-summary-mode-map "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
3917   (define-key gnus-summary-mode-map "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
3918   (define-key gnus-summary-mode-map "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
3919   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
3920   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
3921   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
3922   (define-key gnus-summary-mode-map "\C-x\C-s" 'gnus-summary-reselect-current-group)
3923   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
3924   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
3925   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
3926   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
3927   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
3928   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
3929   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
3930   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
3931   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
3932   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
3933   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
3934   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
3935   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
3936   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
3937   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
3938   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
3939   (define-key gnus-summary-mode-map "V" 'gnus-version)
3940   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
3941   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
3942   (define-key gnus-summary-mode-map "Q" 'gnus-summary-quit)
3943   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
3944   (define-key gnus-summary-mode-map [mouse-2] 'gnus-mouse-pick-article)
3945   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
3946   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
3947   (define-key gnus-summary-mode-map "x" 'gnus-summary-delete-marked-as-read)
3948   (define-key gnus-summary-mode-map "X" 'gnus-summary-remove-lines-marked-with)
3949   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
3950   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
3951   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
3952 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
3953   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
3954   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu)
3955
3956
3957   ;; Orthogonal keymap
3958   (define-prefix-command 'gnus-summary-mark-map)
3959   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
3960   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
3961   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
3962   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
3963   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
3964   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
3965   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
3966   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
3967   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
3968   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
3969   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
3970   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
3971   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
3972   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
3973   (define-key gnus-summary-mark-map "\M-r" 'gnus-summary-remove-lines-marked-as-read)
3974   (define-key gnus-summary-mark-map "\M-\C-r" 'gnus-summary-remove-lines-marked-with)
3975   (define-key gnus-summary-mark-map "D" 'gnus-summary-show-all-dormant)
3976   (define-key gnus-summary-mark-map "S" 'gnus-summary-show-all-expunged)
3977   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
3978   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
3979   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
3980   (define-key gnus-summary-mark-map "a" 'gnus-summary-clear-above)
3981   (define-key gnus-summary-mark-map "A" 'gnus-summary-tick-above)
3982
3983   (define-prefix-command 'gnus-summary-process-map)
3984   (define-key gnus-summary-mark-map "p" 'gnus-summary-process-map)
3985   (define-key gnus-summary-process-map "p" 'gnus-summary-mark-as-processable)
3986   (define-key gnus-summary-process-map "u" 'gnus-summary-unmark-as-processable)
3987   (define-key gnus-summary-process-map "U" 'gnus-summary-unmark-all-processable)
3988   (define-key gnus-summary-process-map "s" 'gnus-uu-mark-series)
3989   (define-key gnus-summary-process-map "r" 'gnus-uu-mark-region)
3990   (define-key gnus-summary-process-map "R" 'gnus-uu-mark-by-regexp)
3991   (define-key gnus-summary-process-map "t" 'gnus-uu-mark-thread)
3992   (define-key gnus-summary-process-map "a" 'gnus-uu-mark-all)
3993   (define-key gnus-summary-process-map "S" 'gnus-uu-mark-sparse)
3994   
3995
3996   (define-prefix-command 'gnus-summary-send-map)
3997   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
3998   (define-key gnus-summary-send-map "p" 'gnus-summary-post-news)
3999   (define-key gnus-summary-send-map "f" 'gnus-summary-followup)
4000   (define-key gnus-summary-send-map "F" 'gnus-summary-followup-with-original)
4001   (define-key gnus-summary-send-map "b" 'gnus-summary-followup-and-reply)
4002   (define-key gnus-summary-send-map "B" 'gnus-summary-followup-and-reply-with-original)
4003   (define-key gnus-summary-send-map "c" 'gnus-summary-cancel-article)
4004   (define-key gnus-summary-send-map "s" 'gnus-summary-supersede-article)
4005   (define-key gnus-summary-send-map "r" 'gnus-summary-reply)
4006   (define-key gnus-summary-send-map "R" 'gnus-summary-reply-with-original)
4007   (define-key gnus-summary-send-map "\C-f" 'gnus-summary-mail-forward)
4008   (define-key gnus-summary-send-map "m" 'gnus-summary-mail-other-window)
4009   (define-key gnus-summary-send-map "u" 'gnus-uu-post-news)
4010   (define-key gnus-summary-send-map "\M-f" 'gnus-uu-digest-and-forward)
4011
4012   
4013   (define-prefix-command 'gnus-summary-goto-map)
4014   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
4015   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
4016   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
4017   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
4018   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
4019   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
4020   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
4021   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
4022   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
4023   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
4024   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
4025   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
4026   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
4027
4028
4029   (define-prefix-command 'gnus-summary-thread-map)
4030   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
4031   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
4032   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
4033   (define-key gnus-summary-thread-map "r" 'gnus-summary-raise-thread)
4034   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
4035   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
4036   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
4037   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
4038   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
4039   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
4040   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
4041   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
4042   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
4043   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
4044
4045   
4046   (define-prefix-command 'gnus-summary-exit-map)
4047   (define-key gnus-summary-mode-map "\M-e" 'gnus-summary-exit-map)
4048   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
4049   (define-key gnus-summary-exit-map "\C-c" 'gnus-summary-catchup-all-and-exit)
4050   (define-key gnus-summary-exit-map "q" 'gnus-summary-exit)
4051   (define-key gnus-summary-exit-map "e" 'gnus-summary-exit)
4052   (define-key gnus-summary-exit-map "Q" 'gnus-summary-quit)
4053   (define-key gnus-summary-exit-map "E" 'gnus-summary-quit)
4054
4055
4056   (define-prefix-command 'gnus-summary-article-map)
4057   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
4058   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
4059   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
4060   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
4061   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
4062   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
4063   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
4064   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
4065   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
4066   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
4067   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
4068   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
4069   (define-key gnus-summary-article-map "w" 'gnus-summary-stop-page-breaking)
4070   (define-key gnus-summary-article-map "c" 'gnus-summary-caesar-message)
4071   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
4072   (define-key gnus-summary-article-map "t" 'gnus-summary-toggle-header)
4073   (define-key gnus-summary-article-map "hh" 'gnus-article-hide-headers)
4074   (define-key gnus-summary-article-map "hs" 'gnus-article-hide-signature)
4075   (define-key gnus-summary-article-map "hc" 'gnus-article-hide-citation)
4076   (define-key gnus-summary-article-map "ho" 'gnus-article-treat-overstrike)
4077   (define-key gnus-summary-article-map "hw" 'gnus-article-word-wrap)
4078   (define-key gnus-summary-article-map "hd" 'gnus-article-remove-cr)
4079   (define-key gnus-summary-article-map "hq" 'gnus-article-de-quoted-unreadable)
4080   (define-key gnus-summary-article-map "m" 'gnus-summary-toggle-mime)
4081   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
4082
4083
4084   (define-prefix-command 'gnus-summary-extract-map)
4085   (define-key gnus-summary-mode-map "X" 'gnus-summary-extract-map)
4086 ;  (define-key gnus-summary-extract-map "x" 'gnus-summary-extract-any)
4087 ;  (define-key gnus-summary-extract-map "m" 'gnus-summary-extract-mime)
4088
4089   (define-key gnus-summary-extract-map "u" 'gnus-uu-decode-uu)
4090   (define-key gnus-summary-extract-map "U" 'gnus-uu-decode-uu-and-save)
4091   (define-key gnus-summary-extract-map "s" 'gnus-uu-decode-unshar)
4092   (define-key gnus-summary-extract-map "S" 'gnus-uu-decode-unshar-and-save)
4093   (define-key gnus-summary-extract-map "o" 'gnus-uu-decode-save)
4094   (define-key gnus-summary-extract-map "O" 'gnus-uu-decode-save)
4095   (define-key gnus-summary-extract-map "b" 'gnus-uu-decode-binhex)
4096   (define-key gnus-summary-extract-map "B" 'gnus-uu-decode-binhex)
4097
4098   (define-prefix-command 'gnus-summary-extract-view-map)
4099   (define-key gnus-summary-extract-map "v" 'gnus-summary-extract-view-map)
4100   (define-key gnus-summary-extract-view-map "u" 'gnus-uu-decode-uu-view)
4101   (define-key gnus-summary-extract-view-map "U" 'gnus-uu-decode-uu-and-save-view)
4102   (define-key gnus-summary-extract-view-map "s" 'gnus-uu-decode-unshar-view)
4103   (define-key gnus-summary-extract-view-map "S" 'gnus-uu-decode-unshar-and-save-view)
4104   (define-key gnus-summary-extract-view-map "o" 'gnus-uu-decode-save-view)
4105   (define-key gnus-summary-extract-view-map "O" 'gnus-uu-decode-save-view)
4106   (define-key gnus-summary-extract-view-map "b" 'gnus-uu-decode-binhex-view)
4107   (define-key gnus-summary-extract-view-map "B" 'gnus-uu-decode-binhex-view)
4108   
4109   
4110   (define-prefix-command 'gnus-summary-various-map)
4111   (define-key gnus-summary-mode-map "V" 'gnus-summary-various-map)
4112   (define-key gnus-summary-various-map "u" 'gnus-summary-universal-argument)
4113   (define-key gnus-summary-various-map "\M-s" 'gnus-summary-search-article-forward)
4114   (define-key gnus-summary-various-map "\M-r" 'gnus-summary-search-article-backward)
4115   (define-key gnus-summary-various-map "r" 'gnus-summary-refer-article)
4116   (define-key gnus-summary-various-map "&" 'gnus-summary-execute-command)
4117   (define-key gnus-summary-various-map "\C-t" 'gnus-summary-toggle-truncation)
4118   (define-key gnus-summary-various-map "=" 'gnus-summary-expand-window)
4119   (define-key gnus-summary-various-map "\C-s" 'gnus-summary-reselect-current-group)
4120   (define-key gnus-summary-various-map "g" 'gnus-summary-rescan-group)
4121   (define-key gnus-summary-various-map "o" 'gnus-summary-save-article)
4122   (define-key gnus-summary-various-map "\C-o" 'gnus-summary-save-article-mail)
4123   (define-key gnus-summary-various-map "|" 'gnus-summary-pipe-output)
4124   (define-key gnus-summary-various-map "V" 'gnus-version)
4125   (define-key gnus-summary-various-map "d" 'gnus-summary-describe-group)
4126   (define-key gnus-summary-various-map "?" 'gnus-summary-describe-briefly)
4127   (define-key gnus-summary-various-map "i" 'gnus-info-find-node)
4128   (define-key gnus-summary-various-map "S" 'gnus-summary-set-score)
4129   (define-key gnus-summary-various-map "b" 'gnus-summary-set-mark-below)
4130   (define-key gnus-summary-various-map "D" 'gnus-summary-enter-digest-group)
4131
4132   (define-prefix-command 'gnus-summary-sort-map)
4133   (define-key gnus-summary-various-map "s" 'gnus-summary-sort-map)
4134   (define-key gnus-summary-sort-map "n" 'gnus-summary-sort-by-number)
4135   (define-key gnus-summary-sort-map "a" 'gnus-summary-sort-by-author)
4136   (define-key gnus-summary-sort-map "s" 'gnus-summary-sort-by-subject)
4137   (define-key gnus-summary-sort-map "d" 'gnus-summary-sort-by-date)
4138   (define-key gnus-summary-sort-map "i" 'gnus-summary-sort-by-score)
4139
4140   (define-prefix-command 'gnus-summary-mgroup-map)
4141   (define-key gnus-summary-various-map "m" 'gnus-summary-mgroup-map)
4142   (define-key gnus-summary-mgroup-map "e" 'gnus-summary-expire-articles)
4143   (define-key gnus-summary-mgroup-map "\177" 'gnus-summary-delete-article)
4144   (define-key gnus-summary-mgroup-map "m" 'gnus-summary-move-article)
4145   (define-key gnus-summary-mgroup-map "r" 'gnus-summary-respool-article)
4146   (define-key gnus-summary-mgroup-map "w" 'gnus-summary-edit-article)
4147
4148   (define-prefix-command 'gnus-summary-vkill-map)
4149   (define-key gnus-summary-various-map "k" 'gnus-summary-vkill-map)
4150   (define-key gnus-summary-vkill-map "k" 'gnus-summary-kill-same-subject-and-select)
4151   (define-key gnus-summary-vkill-map "K" 'gnus-summary-kill-same-subject)
4152   (define-key gnus-summary-vkill-map "\M-k" 'gnus-summary-edit-local-kill)
4153   (define-key gnus-summary-vkill-map "\M-K" 'gnus-summary-edit-global-kill)
4154   (define-key gnus-summary-vkill-map "x" 'gnus-kill-file-set-expunge-below)
4155   (define-key gnus-summary-vkill-map "m" 'gnus-kill-file-set-mark-below)
4156
4157
4158
4159   (define-prefix-command 'gnus-summary-increase-map)
4160   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-map)
4161   (define-key gnus-summary-increase-map "i" 'gnus-summary-raise-same-subject-and-select)
4162   (define-key gnus-summary-increase-map "I" 'gnus-summary-raise-same-subject)
4163   (define-key gnus-summary-increase-map "\C-i" 'gnus-summary-raise-score)
4164
4165   (define-prefix-command 'gnus-summary-inc-subject-map)
4166   (define-key gnus-summary-increase-map "s" 'gnus-summary-inc-subject-map)
4167   (define-key gnus-summary-increase-map "S" 'gnus-summary-temporarily-raise-by-subject)
4168   (define-key gnus-summary-inc-subject-map "s" 'gnus-summary-temporarily-raise-by-subject)
4169   (define-key gnus-summary-inc-subject-map "S" 'gnus-summary-raise-by-subject)
4170   (define-key gnus-summary-inc-subject-map "t" 'gnus-summary-temporarily-raise-by-subject)
4171   (define-key gnus-summary-inc-subject-map "p" 'gnus-summary-raise-by-subject)
4172
4173   (define-prefix-command 'gnus-summary-inc-author-map)
4174   (define-key gnus-summary-increase-map "a" 'gnus-summary-inc-author-map)
4175   (define-key gnus-summary-increase-map "A" 'gnus-summary-temporarily-raise-by-author)
4176   (define-key gnus-summary-inc-author-map "a" 'gnus-summary-temporarily-raise-by-author)
4177   (define-key gnus-summary-inc-author-map "A" 'gnus-summary-raise-by-author)
4178   (define-key gnus-summary-inc-author-map "t" 'gnus-summary-temporarily-raise-by-author)
4179   (define-key gnus-summary-inc-author-map "p" 'gnus-summary-raise-by-author)
4180
4181   (define-prefix-command 'gnus-summary-inc-thread-map)
4182   (define-key gnus-summary-increase-map "t" 'gnus-summary-inc-thread-map)
4183   (define-key gnus-summary-increase-map "T" 'gnus-summary-temporarily-raise-by-thread)
4184   (define-key gnus-summary-inc-thread-map "t" 'gnus-summary-temporarily-raise-by-thread)
4185   (define-key gnus-summary-inc-thread-map "T" 'gnus-summary-raise-by-thread)
4186   (define-key gnus-summary-inc-thread-map "t" 'gnus-summary-temporarily-raise-by-thread)
4187   (define-key gnus-summary-inc-thread-map "p" 'gnus-summary-raise-by-thread)
4188
4189   (define-prefix-command 'gnus-summary-inc-xref-map)
4190   (define-key gnus-summary-increase-map "x" 'gnus-summary-inc-xref-map)
4191   (define-key gnus-summary-increase-map "X" 'gnus-summary-temporarily-raise-by-xref)
4192   (define-key gnus-summary-inc-xref-map "x" 'gnus-summary-temporarily-raise-by-xref)
4193   (define-key gnus-summary-inc-xref-map "X" 'gnus-summary-raise-by-xref)
4194   (define-key gnus-summary-inc-xref-map "t" 'gnus-summary-temporarily-raise-by-xref)
4195   (define-key gnus-summary-inc-xref-map "p" 'gnus-summary-raise-by-xref)
4196
4197   (define-prefix-command 'gnus-summary-inc-fol-map)
4198   (define-key gnus-summary-increase-map "f" 'gnus-summary-inc-fol-map)
4199   (define-key gnus-summary-increase-map "F" 'gnus-summary-raise-followups-to-author)
4200   (define-key gnus-summary-inc-fol-map "f" 'gnus-summary-raise-followups-to-author)
4201   (define-key gnus-summary-inc-fol-map "F" 'gnus-summary-raise-followups-to-author)
4202   (define-key gnus-summary-inc-fol-map "t" 'gnus-summary-raise-followups-to-author)
4203   (define-key gnus-summary-inc-fol-map "p" 'gnus-summary-raise-followups-to-author)
4204
4205   (define-prefix-command 'gnus-summary-lower-map)
4206   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-map)
4207   (define-key gnus-summary-lower-map "l" 'gnus-summary-lower-same-subject-and-select)
4208   (define-key gnus-summary-lower-map "L" 'gnus-summary-lower-same-subject)
4209   (define-key gnus-summary-lower-map "\C-l" 'gnus-summary-lower-score)
4210
4211   (define-prefix-command 'gnus-summary-low-subject-map)
4212   (define-key gnus-summary-lower-map "s" 'gnus-summary-low-subject-map)
4213   (define-key gnus-summary-lower-map "S" 'gnus-summary-temporarily-lower-by-subject)
4214   (define-key gnus-summary-low-subject-map "s" 'gnus-summary-temporarily-lower-by-subject)
4215   (define-key gnus-summary-low-subject-map "S" 'gnus-summary-lower-by-subject)
4216   (define-key gnus-summary-low-subject-map "t" 'gnus-summary-temporarily-lower-by-subject)
4217   (define-key gnus-summary-low-subject-map "p" 'gnus-summary-lower-by-subject)
4218
4219   (define-prefix-command 'gnus-summary-low-author-map)
4220   (define-key gnus-summary-lower-map "a" 'gnus-summary-low-author-map)
4221   (define-key gnus-summary-lower-map "A" 'gnus-summary-temporarily-lower-by-author)
4222   (define-key gnus-summary-low-author-map "a" 'gnus-summary-temporarily-lower-by-author)
4223   (define-key gnus-summary-low-author-map "A" 'gnus-summary-lower-by-author)
4224   (define-key gnus-summary-low-author-map "t" 'gnus-summary-temporarily-lower-by-author)
4225   (define-key gnus-summary-low-author-map "p" 'gnus-summary-lower-by-author)
4226
4227   (define-prefix-command 'gnus-summary-low-thread-map)
4228   (define-key gnus-summary-lower-map "t" 'gnus-summary-low-thread-map)
4229   (define-key gnus-summary-lower-map "T" 'gnus-summary-temporarily-lower-by-thread)
4230   (define-key gnus-summary-low-thread-map "t" 'gnus-summary-temporarily-lower-by-thread)
4231   (define-key gnus-summary-low-thread-map "T" 'gnus-summary-lower-by-thread)
4232   (define-key gnus-summary-low-thread-map "t" 'gnus-summary-temporarily-lower-by-thread)
4233   (define-key gnus-summary-low-thread-map "p" 'gnus-summary-lower-by-thread)
4234
4235   (define-prefix-command 'gnus-summary-low-xref-map)
4236   (define-key gnus-summary-lower-map "x" 'gnus-summary-low-xref-map)
4237   (define-key gnus-summary-lower-map "X" 'gnus-summary-temporarily-lower-by-xref)
4238   (define-key gnus-summary-low-xref-map "x" 'gnus-summary-temporarily-lower-by-xref)
4239   (define-key gnus-summary-low-xref-map "X" 'gnus-summary-lower-by-xref)
4240   (define-key gnus-summary-low-xref-map "t" 'gnus-summary-temporarily-lower-by-xref)
4241   (define-key gnus-summary-low-xref-map "p" 'gnus-summary-lower-by-xref)
4242
4243   (define-prefix-command 'gnus-summary-low-fol-map)
4244   (define-key gnus-summary-lower-map "f" 'gnus-summary-low-fol-map)
4245   (define-key gnus-summary-lower-map "F" 'gnus-summary-lower-followups-to-author)
4246   (define-key gnus-summary-low-fol-map "f" 'gnus-summary-lower-followups-to-author)
4247   (define-key gnus-summary-low-fol-map "F" 'gnus-summary-lower-followups-to-author)
4248   (define-key gnus-summary-low-fol-map "t" 'gnus-summary-lower-followups-to-author)
4249   (define-key gnus-summary-low-fol-map "p" 'gnus-summary-lower-followups-to-author)
4250   )
4251
4252
4253 \f
4254
4255 (defun gnus-summary-mode ()
4256   "Major mode for reading articles in this newsgroup.
4257 All normal editing commands are switched off.
4258 The following commands are available:
4259
4260 \\<gnus-summary-mode-map>
4261 \\[gnus-summary-next-page]\t Scroll the article buffer a page forwards
4262 \\[gnus-summary-prev-page]\t Scroll the article buffer a page backwards
4263 \\[gnus-summary-scroll-up]\t Scroll the article buffer one line forwards
4264 \\[gnus-summary-next-unread-article]\t Go to the next unread article
4265 \\[gnus-summary-prev-unread-article]\t Go to the previous unread article
4266 \\[gnus-summary-next-article]\t Go to the next article
4267 \\[gnus-summary-prev-article]\t Go to the previous article
4268 \\[gnus-summary-next-same-subject]\t Go to the next summary line with the same subject
4269 \\[gnus-summary-prev-same-subject]\t Go to the previous summary line with the same subject
4270 \\[gnus-summary-next-subject]\t Go to the next summary line
4271 \\[gnus-summary-prev-subject]\t Go to the previous summary line
4272 \\[gnus-summary-next-unread-subject]\t Go to the next unread summary line
4273 \\[gnus-summary-prev-unread-subject]\t Go to the previous unread summary line
4274 \\[gnus-summary-first-unread-article]\t Go to the first unread article
4275 \\[gnus-summary-best-unread-article]\t Go to the unread article with the highest score
4276 \\[gnus-summary-goto-subject]\t Go to some subject
4277 \\[gnus-summary-goto-last-article]\t Go to the previous article
4278
4279 \\[gnus-summary-beginning-of-article]\t Go to the beginning of the article
4280 \\[gnus-summary-end-of-article]\t Go to the end of the article
4281
4282 \\[gnus-summary-refer-parent-article]\t Get the parent of the current article from the server
4283 \\[gnus-summary-refer-article]\t Request some article by Message-ID from the server
4284
4285 \\[gnus-summary-isearch-article]\t Do an interactive search on the current article
4286 \\[gnus-summary-search-article-forward]\t Search all articles forward for a regular expression
4287 \\[gnus-summary-search-article-backward]\t Search all articles backward for a regular expression
4288
4289 \\[gnus-summary-tick-article-forward]\t Tick current article and move forward
4290 \\[gnus-summary-tick-article-backward]\t Tick current article and move backward
4291 \\[gnus-summary-mark-as-read-forward]\t Mark the current article as read and move forward
4292 \\[gnus-summary-mark-as-read-backward]\t Mark the current article as read and move backward
4293 \\[gnus-summary-clear-mark-forward]\t Clear tick and read marks and move forward
4294 \\[gnus-summary-clear-mark-backward]\t Clear tick and read marks and move backward
4295 \\[gnus-summary-mark-as-processable]\t Set the process mark on the current article
4296 \\[gnus-summary-unmark-as-processable]\t Remove the process mark from the current article
4297 \\[gnus-summary-unmark-all-processable]\t Remove the process mark from all articles
4298
4299 \\[gnus-summary-raise-same-subject-and-select]\t Raise all articles with the current subject and select the next article
4300 \\[gnus-summary-raise-same-subject]\t Raise all articles with the current subject
4301 \\[gnus-summary-lower-same-subject-and-select]\t Lower all articles with the current subject and select the next article
4302 \\[gnus-summary-lower-same-subject]\t Lower all articles with the current subject
4303
4304 \\[gnus-summary-toggle-threads]\t Toggle thread display
4305 \\[gnus-summary-show-thread]\t Show the current thread
4306 \\[gnus-summary-hide-thread]\t Hide the current thread
4307 \\[gnus-summary-next-thread]\t Go to the next thread
4308 \\[gnus-summary-prev-thread]\t Go to the previous thread
4309 \\[gnus-summary-up-thread]\t Go up the current thread
4310 \\[gnus-summary-down-thread]\t Descend the current thread
4311 \\[gnus-summary-raise-thread]\t Raise the current thread
4312 \\[gnus-summary-lower-thread]\t Lower the current thread
4313 \\[gnus-summary-mark-as-expirable]\t Mark the current artivles as expirable
4314 \\[gnus-summary-remove-lines-marked-as-read]\t Remove all articles that are marked as read
4315 \\[gnus-summary-remove-lines-marked-with]\t Remove all articles that have some mark
4316
4317 \\[gnus-summary-execute-command]\t Execute a command
4318 \\[gnus-summary-catchup-and-exit]\t Mark all unread articles as read and exit
4319 \\[gnus-summary-toggle-truncation]\t Toggle truncation of summary lines
4320 \\[gnus-summary-expand-window]\t Expand the summary window
4321 \\[gnus-summary-universal-argument]\t Run a command on all articles with the process mark
4322
4323 \\[gnus-summary-sort-by-number]\t Sort the summary buffer by article number
4324 \\[gnus-summary-sort-by-author]\t Sort the summary buffer by author
4325 \\[gnus-summary-sort-by-subject]\t Sort the summary buffer by subject
4326 \\[gnus-summary-sort-by-date]\t Sort the summary buffer by date
4327
4328 \\[gnus-summary-reselect-current-group]\t Exit and reselect the current group
4329 \\[gnus-summary-rescan-group]\t Exit, get new articles and reselect the group
4330 \\[gnus-summary-stop-page-breaking]\t Stop page breaking of the current article
4331 \\[gnus-summary-caesar-message]\t Caesar rotate (rot13) the current article
4332 \\[gnus-summary-show-article]\t Reselect the current article
4333 \\[gnus-summary-toggle-header]\t Toggle header display
4334 \\[gnus-summary-toggle-mime]\t Toggle whether to use MIME
4335 \\[gnus-summary-enter-digest-group]\t Enter a newsgroup based on the current digest
4336 \\[gnus-summary-post-news]\t Post an article to the current group
4337 \\[gnus-summary-followup]\t Post a followup to the current article
4338 \\[gnus-summary-followup-with-original]\t Post a followup and include the original article
4339 \\[gnus-summary-cancel-article]\t Cancel the current article
4340 \\[gnus-summary-supersede-article]\t Supersede the current article
4341 \\[gnus-summary-reply]\t Mail a reply to the author of the current article
4342 \\[gnus-summary-reply-with-original]\t Mail a reply and include the current article
4343 \\[gnus-summary-mail-forward]\t Forward the current article
4344 \\[gnus-summary-mail-other-window]\t Mail in the other window
4345 \\[gnus-summary-save-article]\t Save the current article
4346 \\[gnus-summary-save-article-mail]\t Save the current article in rmail format
4347 \\[gnus-summary-pipe-output]\t Pipe the current article to a process
4348 \\[gnus-summary-move-article]\t Move the article to a different newsgroup
4349 \\[gnus-summary-respool-article]\t Respool the article
4350 \\[gnus-summary-edit-local-kill]\t Edit the local kill file
4351 \\[gnus-summary-edit-global-kill]\t Edit the global kill file
4352 \\[gnus-version]\t Display the current Gnus version
4353 \\[gnus-summary-exit]\t Exit the summary buffer 
4354 \\[gnus-summary-quit]\t Exit the summary buffer without saving any changes
4355 \\[gnus-summary-describe-group]\t Describe the current newsgroup
4356 \\[gnus-summary-describe-briefly]\t Give a brief key overview
4357 \\[gnus-info-find-node]\t Go to the Gnus info node
4358
4359 \\[gnus-kill-file-set-expunge-below]    Automatically expunge articles below LEVEL.
4360
4361 \\[gnus-kill-file-set-mark-below]       Automatically mark articles below LEVEL.
4362 \\[gnus-summary-temporarily-raise-by-subject]\t Temporarily raise score for articles with the current subject
4363 \\[gnus-summary-temporarily-raise-by-author]\t Temporarily raise score for articles from the current author
4364 \\[gnus-summary-temporarily-raise-by-xref]\t Temporarily raise score for articles with the current cross-posting
4365 \\[gnus-summary-raise-by-subject]\t Permanently raise score for articles with the current subject
4366 \\[gnus-summary-raise-by-author]\t Permanently raise score for articles from the current author
4367 \\[gnus-summary-raise-followups-to-author]\t Permanently raise score for followups to the current author
4368 \\[gnus-summary-raise-by-xref]\t Permanently raise score for articles with the current cross-posting
4369 \\[gnus-summary-temporarily-lower-by-subject]\t Temporarily lower score for articles with the current subject
4370 \\[gnus-summary-temporarily-lower-by-author]\t Temporarily lower score for articles from the current author
4371 \\[gnus-summary-temporarily-lower-by-xref]\t Temporarily lower score for articles with the current cross-posting
4372 \\[gnus-summary-lower-by-subject]\t Permanently lower score for articles with the current subject
4373 \\[gnus-summary-lower-by-author]\t Permanently lower score for articles from the current author
4374 \\[gnus-summary-lower-followups-to-author]\t Permanently lower score for followups to the current author
4375 \\[gnus-summary-lower-by-thread]\t Permanently lower score for articles in the current thread
4376 \\[gnus-summary-lower-by-xref]\t Permanently lower score for articles with the current cross-posting
4377 "
4378   (interactive)
4379   (if gnus-visual (gnus-summary-make-menu-bar))
4380   (kill-all-local-variables)
4381   (let ((locals gnus-summary-local-variables))
4382     (while locals
4383       (if (consp (car locals))
4384           (progn
4385             (make-local-variable (car (car locals)))
4386             (set (car (car locals)) (eval (cdr (car locals)))))
4387         (make-local-variable (car locals))
4388         (set (car locals) nil))
4389       (setq locals (cdr locals))))
4390   (gnus-update-format-specifications)
4391   (setq mode-line-modified "-- ")
4392   (make-local-variable 'mode-line-format)
4393   (setq mode-line-format (copy-sequence mode-line-format))
4394   (and (equal (nth 3 mode-line-format) "   ")
4395        (setcar (nthcdr 3 mode-line-format) ""))
4396   (setq major-mode 'gnus-summary-mode)
4397   (setq mode-name "Summary")
4398   (make-local-variable 'minor-mode-alist)
4399 ;  (or (assq 'gnus-show-threads minor-mode-alist)
4400 ;      (setq minor-mode-alist
4401 ;           (cons (list 'gnus-show-threads " Thread") minor-mode-alist)))
4402   (gnus-set-mode-line 'summary)
4403   (use-local-map gnus-summary-mode-map)
4404   (buffer-disable-undo (current-buffer))
4405   (setq buffer-read-only t)             ;Disable modification
4406   (setq truncate-lines t)
4407   (setq selective-display t)
4408   (setq selective-display-ellipses t)   ;Display `...'
4409   (run-hooks 'gnus-summary-mode-hook))
4410
4411 (defun gnus-mouse-pick-article (e)
4412   (interactive "e")
4413   (mouse-set-point e)
4414   (gnus-summary-next-page nil t))
4415
4416 (defun gnus-summary-setup-buffer (group)
4417   "Initialize summary buffer."
4418   (let ((buffer (concat "*Summary " group "*")))
4419     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
4420     (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
4421     (gnus-add-current-to-buffer-list)
4422     (gnus-summary-mode)))
4423
4424 (defun gnus-set-global-variables ()
4425   ;; Set the global equivalents of the summary buffer-local variables
4426   ;; to the latest values they had. These reflect the summary buffer
4427   ;; that was in action when the last article was fetched.
4428   (if (eq major-mode 'gnus-summary-mode) 
4429       (progn
4430         (setq gnus-summary-buffer (current-buffer))
4431         (let ((name gnus-newsgroup-name)
4432               (marked gnus-newsgroup-marked)
4433               (unread gnus-newsgroup-unreads)
4434               (headers gnus-current-headers))
4435           (save-excursion
4436             (set-buffer gnus-group-buffer)
4437             (setq gnus-newsgroup-name name)
4438             (setq gnus-newsgroup-marked marked)
4439             (setq gnus-newsgroup-unreads unread)
4440             (setq gnus-current-headers headers))))))
4441
4442 (defun gnus-summary-insert-dummy-line (sformat subject number)
4443   (if (not sformat) 
4444       (setq sformat gnus-summary-dummy-line-format-spec))
4445   (let (b)
4446     (beginning-of-line)
4447     (setq b (point))
4448     (insert (eval sformat))
4449     (add-text-properties
4450      b (1+ b)
4451      (list 'gnus-subject (gnus-simplify-subject-re subject)
4452            'gnus-number number
4453            'gnus-mark ?Z
4454            'gnus-thread 0))))
4455
4456 (defun gnus-summary-insert-line 
4457   (sformat header level current unread replied expirable subject-or-nil
4458            &optional dummy score)
4459   (or sformat (setq sformat gnus-summary-line-format-spec))
4460   (let* ((indentation 
4461           (make-string (* level gnus-thread-indent-level) ? ))
4462          (lines (or (header-lines header) 0))
4463          (score (or score gnus-summary-default-score 0))
4464          (score-char (if (= score gnus-summary-default-score) ? 
4465                        (if (< score gnus-summary-default-score) 
4466                            gnus-score-below-mark gnus-score-over-mark)))
4467          (replied (if replied gnus-replied-mark ? ))
4468          (from (header-from header))
4469          (name-address (gnus-extract-address-components from))
4470          (address (car (cdr name-address)))
4471          (name (or (car name-address) (car (cdr name-address))))
4472          (number (header-number header))
4473          (subject (header-subject header))
4474          (buffer-read-only nil)
4475          (opening-bracket (if dummy ?\< ?\[))
4476          (closing-bracket (if dummy ?\> ?\]))
4477          b)
4478     ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
4479     (if (not (numberp lines)) (setq lines 0))
4480     (beginning-of-line)
4481     (setq b (point))
4482     (insert-before-markers (eval sformat))
4483     (add-text-properties
4484      b (1+ b)
4485      (list 'gnus-subject (gnus-simplify-subject-re subject)
4486            'gnus-number number
4487            'gnus-mark (or unread gnus-unread-mark ? )
4488            'gnus-thread level))))
4489
4490 (defun gnus-summary-update-line ()
4491   ;; Update summary line after change.
4492   (or (not gnus-summary-default-score)
4493       gnus-summary-inhibit-highlight
4494       (save-excursion
4495         (beginning-of-line 1)
4496         (let ((score (gnus-summary-article-score))
4497               (default gnus-summary-default-score)
4498               (below gnus-summary-mark-below))
4499           (save-excursion
4500             (if (< score below)
4501                 (if (eq (following-char) gnus-unread-mark)
4502                     (gnus-summary-mark-article nil gnus-low-score-mark))
4503               (if (eq (following-char) gnus-low-score-mark)
4504                   (gnus-summary-mark-article nil gnus-unread-mark))))
4505           (if  gnus-visual
4506               (run-hooks 'gnus-visual-summary-update-hook))))))
4507
4508 (defun gnus-summary-update-lines ()
4509   ;; Rehighlight summary buffer according to `gnus-summary-highlight'.
4510   (if (and gnus-visual gnus-visual-summary-update-hook)
4511       (save-excursion
4512         (set-buffer gnus-summary-buffer)
4513         (goto-char (point-min))
4514         (while (not (eobp))
4515           (gnus-summary-update-line)
4516           (forward-line 1)))))
4517
4518 (defun gnus-summary-read-group (group &optional show-all no-article kill-buffer)
4519   "Start reading news in newsgroup GROUP.
4520 If SHOW-ALL is non-nil, already read articles are also listed.
4521 If NO-ARTICLE is non-nil, no article is selected initially."
4522   (message "Retrieving newsgroup: %s..." group)
4523   (gnus-summary-setup-buffer group)
4524   (if (gnus-select-newsgroup group show-all)
4525       (progn
4526         ;; You can change the subjects in this hook.
4527         (run-hooks 'gnus-select-group-hook)
4528         ;; Do Score Processing.
4529         (gnus-score-headers)
4530         ;; Update the format specifiers.
4531         (gnus-update-format-specifications)
4532         (gnus-summary-prepare)
4533         (if (and (zerop (buffer-size))
4534                  gnus-newsgroup-dormant)
4535             (gnus-summary-show-all-dormant))
4536         (gnus-set-global-variables)
4537         ;; Function `gnus-apply-kill-file' must be called in this hook.
4538         (run-hooks 'gnus-apply-kill-hook)
4539         (if (zerop (buffer-size))
4540             (progn
4541               ;; This newsgroup is empty.
4542               (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
4543               (message "No unread news"))
4544           ;; Hide conversation thread subtrees.  We cannot do this in
4545           ;; gnus-summary-prepare-hook since kill processing may not
4546           ;; work with hidden articles.
4547           (and gnus-show-threads
4548                gnus-thread-hide-subtree
4549                (gnus-summary-hide-all-threads))
4550           ;; Show first unread article if requested.
4551           (goto-char (point-min))
4552           (if (and (not no-article)
4553                    gnus-auto-select-first
4554                    (gnus-summary-first-unread-article))
4555               (gnus-configure-windows 'article)
4556             (gnus-configure-windows 'summary))
4557           (pop-to-buffer gnus-summary-buffer)
4558           (gnus-set-mode-line 'summary)
4559           (gnus-summary-position-cursor)
4560           (if (and kill-buffer
4561                    (get-buffer kill-buffer)
4562                    (buffer-name (get-buffer kill-buffer)))
4563               (kill-buffer kill-buffer))))
4564     ;; Cannot select newsgroup GROUP.
4565     (message "Couldn't select newsgroup")
4566     (and (eq major-mode 'gnus-summary-mode)
4567          (kill-buffer (current-buffer)))
4568     (switch-to-buffer gnus-group-buffer)))
4569
4570 (defun gnus-summary-prepare ()
4571   "Prepare summary list of current newsgroup in summary buffer."
4572   (let ((buffer-read-only nil))
4573     (erase-buffer)
4574     (gnus-summary-prepare-threads 
4575      (if gnus-show-threads
4576          (gnus-gather-threads (gnus-sort-threads (gnus-make-threads)))
4577        gnus-newsgroup-headers)
4578      0)
4579     (gnus-summary-remove-dormant-lines)
4580     ;; Erase header retrieval message.
4581     (message "")
4582     ;; Call hooks for modifying summary buffer.
4583     ;; Suggested by sven@tde.LTH.Se (Sven Mattisson).
4584     (goto-char (point-min))
4585     (run-hooks 'gnus-summary-prepare-hook)))
4586
4587 (defun gnus-summary-remove-dormant-lines ()
4588   (let ((int gnus-newsgroup-dormant)
4589         (buffer-read-only nil)
4590         beg cur-level)
4591     (while int
4592       (if (gnus-summary-goto-subject (car int))
4593           (progn
4594             (beginning-of-line)
4595             (setq cur-level (gnus-summary-thread-level))
4596             (setq beg (point))
4597             (re-search-forward "[\n\r]")
4598             (if (<= (gnus-summary-thread-level) cur-level)
4599                 ;; If the level of the next article is greater than the
4600                 ;; level of this article, then it has to be the child of this
4601                 ;; article, so we do not delete this article.
4602                 (progn
4603                   (setq gnus-newsgroup-dormant-subjects
4604                         (cons (cons (car int) (buffer-substring beg (point)))
4605                               gnus-newsgroup-dormant-subjects))
4606                   (delete-region beg (point))))))
4607       (setq int (cdr int)))))
4608
4609 (defun gnus-gather-threads (threads)
4610   "Gather threads that have lost their roots."
4611   (if (not gnus-summary-make-false-root)
4612       threads 
4613     (let ((hashtb (gnus-make-hashtable 1023))
4614           (prev threads)
4615           (result threads)
4616           thread subject hthread unre-subject whole-subject)
4617       (while threads
4618         (setq subject (header-subject (car (car threads)))
4619               whole-subject subject)
4620         (and gnus-summary-gather-subject-limit
4621              (> (length subject) gnus-summary-gather-subject-limit)
4622              (setq subject
4623                    (substring subject 0 gnus-summary-gather-subject-limit)))
4624         (if (setq hthread 
4625                   (gnus-gethash 
4626                    (setq unre-subject (gnus-simplify-subject-re subject))
4627                    hashtb))
4628             (progn
4629               (or (stringp (car (car hthread)))
4630                   (setcar hthread (list whole-subject (car hthread))))
4631               (setcdr (car hthread) (nconc (cdr (car hthread)) 
4632                                            (list (car threads))))
4633               (setcdr prev (cdr threads))
4634               (setq threads prev))
4635           (gnus-sethash unre-subject threads hashtb))
4636         (setq prev threads)
4637         (setq threads (cdr threads)))
4638       result)))
4639
4640 (defun gnus-make-threads ()
4641   ;; This function takes the dependencies already made by 
4642   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
4643   ;; through the dependecies in the hash table and finds all the
4644   ;; roots. Roots do not refer back to any valid articles.
4645   (let (roots)
4646     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
4647          (gnus-build-old-threads))
4648     (mapatoms
4649      (lambda (refs)
4650        (if (not (car (symbol-value refs)))
4651            (setq roots (append (cdr (symbol-value refs)) roots))
4652          ;; Ok, these refer back to valid articles, but if
4653          ;; `gnus-thread-ignore-subject' is nil, we have to check that
4654          ;; the root has the same subject as its children. The children
4655          ;; that do not are made into roots and removed from the list
4656          ;; of children. 
4657          (or gnus-thread-ignore-subject
4658              (let* ((prev (symbol-value refs))
4659                     (subject (gnus-simplify-subject-re 
4660                               (header-subject (car prev))))
4661                     (headers (cdr prev)))
4662                (while headers
4663                  (if (not (string= subject
4664                                    (gnus-simplify-subject-re 
4665                                     (header-subject (car headers)))))
4666                      (progn
4667                        (setq roots (cons (car headers) roots))
4668                        (setcdr prev (cdr headers)))
4669                    (setq prev headers))
4670                  (setq headers (cdr headers)))))))
4671      gnus-newsgroup-dependencies)
4672
4673     (mapcar (lambda (root) (gnus-trim-thread (gnus-make-sub-thread root)))
4674             roots)))
4675
4676 (defun gnus-trim-thread (thread)
4677   (if (and (eq gnus-fetch-old-headers 'some)
4678            (memq (header-number (car thread)) gnus-newsgroup-ancient)
4679            (= (length thread) 2))
4680       (gnus-trim-thread (nth 1 thread))
4681     thread))
4682
4683 (defun gnus-make-sub-thread (root)
4684   ;; This function makes a sub-tree for a node in the tree.
4685   (let ((children (reverse (cdr (gnus-gethash (downcase (header-id root))
4686                                               gnus-newsgroup-dependencies)))))
4687     (cons root (mapcar 'gnus-make-sub-thread children))))
4688
4689 (defun gnus-build-old-threads ()
4690   ;; Look at all the articles that refer back to old articles, and
4691   ;; fetch the headers for the articles that aren't there. This will
4692   ;; build complete threads - if the roots haven't been expired by the
4693   ;; server, that is.
4694   (let (id heads)
4695     (mapatoms
4696      (lambda (refs)
4697        (if (not (car (symbol-value refs)))
4698            (progn
4699              (setq heads (cdr (symbol-value refs)))
4700              (while heads
4701                (if (not (memq (header-number (car heads))
4702                               gnus-newsgroup-dormant))
4703                    (progn
4704                      (setq id (symbol-name refs))
4705                      (while (and (setq id (gnus-build-get-header id))
4706                                  (not (car (gnus-gethash 
4707                                             id gnus-newsgroup-dependencies)))))
4708                      (setq heads nil))
4709                  (setq heads (cdr heads)))))))
4710      gnus-newsgroup-dependencies)))
4711
4712 (defun gnus-build-get-header (id)
4713   ;; Look through the buffer of NOV lines and find the header to
4714   ;; ID. Enter this line into the dependencies hash table, and return
4715   ;; the id of the parent article (if any).
4716   (let ((deps gnus-newsgroup-dependencies)
4717         found header)
4718     (prog1
4719         (save-excursion
4720           (set-buffer nntp-server-buffer)
4721           (goto-char (point-min))
4722           (while (and (not found) (search-forward id nil t))
4723             (beginning-of-line)
4724             (setq found (looking-at (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4725                                             (regexp-quote id))))
4726             (or found (beginning-of-line 2)))
4727           (if found
4728               (let (ref)
4729                 (beginning-of-line)
4730                 (and
4731                  (setq header (gnus-nov-parse-line 
4732                                (read (current-buffer)) deps))
4733                  (setq ref (header-references header))
4734                  (string-match "\\(<[^>]+>\\) *$" ref)
4735                  (substring ref (match-beginning 1) (match-end 1))))))
4736       (and header
4737            (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers)
4738                  gnus-newsgroup-ancient (cons (header-number header)
4739                                               gnus-newsgroup-ancient))))))
4740
4741 (defun gnus-sort-threads (threads)
4742   ;; Sort threads as specified in `gnus-thread-sort-functions'.
4743   (let ((fun gnus-thread-sort-functions))
4744     (while fun
4745       (setq threads (sort threads (car fun))
4746             fun (cdr fun))))
4747   threads)
4748
4749 (defun gnus-thread-header (thread)
4750   ;; Return header of first article in THREAD.
4751   (if (consp thread)
4752       (if (stringp (car thread))
4753           (car (car (cdr thread)))
4754         (car thread))
4755     thread))
4756
4757 (defun gnus-thread-sort-by-number (h1 h2)
4758   "Sort threads by root article number."
4759   (let ((h1 (gnus-thread-header h1))
4760         (h2 (gnus-thread-header h2)))
4761     (< (header-number h1) (header-number h2))))
4762
4763 (defun gnus-thread-sort-by-author (h1 h2)
4764   "Sort threads by root author."
4765   (let ((h1 (gnus-thread-header h1))
4766         (h2 (gnus-thread-header h2)))
4767     (string-lessp
4768      (let ((extract (gnus-extract-address-components (header-from h1))))
4769        (or (car extract) (cdr extract)))
4770      (let ((extract (gnus-extract-address-components (header-from h2))))
4771        (or (car extract) (cdr extract))))))
4772
4773 (defun gnus-thread-sort-by-subject (h1 h2)
4774   "Sort threads by root subject."
4775   (let ((h1 (gnus-thread-header h1))
4776         (h2 (gnus-thread-header h2)))
4777     (string-lessp
4778      (downcase (gnus-simplify-subject (header-subject h1)))
4779      (downcase (gnus-simplify-subject (header-subject h2))))))
4780
4781 (defun gnus-thread-sort-by-date (h1 h2)
4782   "Sort threads by root article date."
4783   (let ((h1 (gnus-thread-header h1))
4784         (h2 (gnus-thread-header h2)))
4785     (string-lessp
4786      (gnus-sortable-date (header-date h1))
4787      (gnus-sortable-date (header-date h2)))))
4788
4789 (defun gnus-thread-sort-by-score (h1 h2)
4790   "Sort threads by root article score.
4791 Unscored articles will be counted as havin a score of zero."
4792   (let ((h1 (gnus-thread-header h1))
4793         (h2 (gnus-thread-header h2)))
4794     (let ((s1 (assq (header-number h1) gnus-newsgroup-scored))
4795           (s2 (assq (header-number h2) gnus-newsgroup-scored)))
4796       (> (or (cdr s1) gnus-summary-default-score 0)
4797          (or (cdr s2) gnus-summary-default-score 0)))))
4798
4799 (defun gnus-thread-sort-by-total-score (h1 h2)
4800   "Sort threads by the sum of all scores in the thread.
4801 Unscored articles will be counted as havin a score of zero."
4802   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4803
4804 (defun gnus-thread-total-score (thread)
4805   ;;  This function find the total score of  THREAD.
4806   (if (consp thread)
4807       (if (stringp (car thread))
4808           (apply gnus-thread-score-function 0
4809                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4810         (gnus-thread-total-score-1 thread))
4811     (gnus-thread-total-score-1 (list thread))))
4812
4813 (defun gnus-thread-total-score-1 (root)
4814   ;; This function find the total score of the thread below ROOT.
4815   (setq root (car root))
4816   (apply gnus-thread-score-function
4817          (or (cdr (assq (header-number root) gnus-newsgroup-scored))
4818              gnus-summary-default-score 0)
4819          (mapcar 'gnus-thread-total-score
4820                  (cdr (gnus-gethash (downcase (header-id root))
4821                                     gnus-newsgroup-dependencies)))))
4822
4823 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4824 (defvar gnus-tmp-prev-subject "")
4825 (defvar gnus-tmp-prev-dormant nil)
4826
4827 ;; Basic ideas by Paul Dworkin <paul@media-lab.media.mit.edu>.
4828 (defun gnus-summary-prepare-threads 
4829   (threads level &optional not-child no-subject)
4830   "Prepare summary buffer from THREADS and indentation LEVEL.  
4831 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
4832 or a straight list of headers."
4833   (let (thread header number subject clevel)
4834     (while threads
4835       (setq thread (car threads)
4836             threads (cdr threads))
4837       ;; If `thread' is a cons, hierarchical threads are used.  If not,
4838       ;; `thread' is the header.
4839       (if (consp thread)
4840           (setq header (car thread))
4841         (setq header thread))
4842       (if (stringp header)
4843           ;; The header is a dummy root.
4844           (progn
4845             (cond ((eq gnus-summary-make-false-root 'adopt)
4846                    ;; We let the first article adopt the rest.
4847                    (gnus-summary-prepare-threads (list (car (cdr thread))) 0)
4848                    (setq thread (cdr (cdr thread)))
4849                    (while thread
4850                      (gnus-summary-prepare-threads (list (car thread)) 1 t)
4851                      (setq thread (cdr thread))))
4852                   ((eq gnus-summary-make-false-root 'dummy)
4853                    ;; We output a dummy root.
4854                    (gnus-summary-insert-dummy-line 
4855                     nil header (header-number (car (car (cdr thread)))))
4856                    (setq clevel 1))
4857                   ((eq gnus-summary-make-false-root 'empty)
4858                    ;; We print the articles with empty subject fields. 
4859                    (gnus-summary-prepare-threads (list (car (cdr thread))) 0)
4860                    (setq thread (cdr (cdr thread)))
4861                    (while thread
4862                      (gnus-summary-prepare-threads (list (car thread)) 0 nil
4863                                                    (not gnus-tmp-prev-dormant))
4864                      (setq thread (cdr thread))))
4865                   (t
4866                    ;; We do not make a root for the gathered
4867                    ;; sub-threads at all.  
4868                    (setq clevel 0)))
4869             ;; Print the sub-threads.
4870             (and (consp thread) (cdr thread)
4871                  (gnus-summary-prepare-threads (cdr thread) clevel)))
4872         ;; The header is a real article.
4873         (setq number (header-number header)
4874               subject (header-subject header)
4875               gnus-tmp-prev-dormant (memq number gnus-newsgroup-dormant))
4876         (gnus-summary-insert-line
4877          nil header level nil 
4878          (cond ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
4879                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
4880                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
4881                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
4882                (t gnus-ancient-mark))
4883          (memq number gnus-newsgroup-replied)
4884          (memq number gnus-newsgroup-expirable)
4885          (if no-subject gnus-summary-same-subject
4886            (if (or (zerop level)
4887                    (and gnus-thread-ignore-subject
4888                         (not (string= 
4889                               (gnus-simplify-subject-re gnus-tmp-prev-subject)
4890                               (gnus-simplify-subject-re subject)))))
4891                subject
4892              gnus-summary-same-subject))
4893          not-child
4894          (cdr (assq number gnus-newsgroup-scored)))
4895         (setq gnus-tmp-prev-subject subject)
4896         ;; Recursively print subthreads.
4897         (and (consp thread) (cdr thread)
4898              (gnus-summary-prepare-threads (cdr thread) (1+ level)))))))
4899
4900 (defun gnus-select-newsgroup (group &optional read-all)
4901   "Select newsgroup GROUP.
4902 If READ-ALL is non-nil, all articles in the group are selected."
4903   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4904          (info (nth 2 entry))
4905          articles header-marks)
4906     (and (eq (car entry) t)
4907          (or (gnus-activate-newsgroup (car info))
4908              (progn
4909                (kill-buffer (current-buffer))
4910                (error "Couldn't request newsgroup %s" group))))
4911     (setq gnus-current-select-method (or (nth 4 info) gnus-select-method))
4912     (gnus-check-news-server (nth 4 info))
4913     (or (gnus-request-group group t)
4914         (progn
4915           (kill-buffer (current-buffer))
4916           (error "Couldn't request newsgroup %s" group)))
4917
4918     ;; Initialize the buffer that holds lines that have been removed
4919     ;; from the summary buffer.
4920     (save-excursion
4921       (set-buffer
4922        (setq gnus-newsgroup-expunged-buffer 
4923              (get-buffer-create (format " *gnus expunge %s*" group))))
4924       (buffer-disable-undo (current-buffer))
4925       (erase-buffer)
4926       (gnus-add-current-to-buffer-list))
4927     
4928     (setq gnus-newsgroup-name group)
4929     (setq gnus-newsgroup-unselected nil)
4930     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4931
4932     (and info
4933          (let (marked)
4934            (gnus-adjust-marked-articles info)
4935            (setq gnus-newsgroup-marked 
4936                  (cdr (assq 'tick (setq marked (nth 3 info)))))
4937            (setq gnus-newsgroup-replied (cdr (assq 'reply marked)))
4938            (setq gnus-newsgroup-expirable (cdr (assq 'expire marked)))
4939            (setq gnus-newsgroup-killed (cdr (assq 'killed marked)))
4940            (setq gnus-newsgroup-bookmarks (cdr (assq 'bookmark marked)))
4941            (setq gnus-newsgroup-dormant (cdr (assq 'dormant marked)))
4942            (setq gnus-newsgroup-scored (cdr (assq 'score marked)))
4943            (setq gnus-newsgroup-processable nil)))
4944
4945     (if (not (setq articles (gnus-articles-to-read group read-all)))
4946         nil
4947       ;; Init the dependencies hash table.
4948       (setq gnus-newsgroup-dependencies 
4949             (gnus-make-hashtable (length gnus-newsgroup-unreads)))
4950       ;; Retrieve the headers and read them in.
4951       (setq gnus-newsgroup-headers 
4952             (if (eq 'nov (setq gnus-headers-retrieved-by
4953                                (gnus-retrieve-headers 
4954                                 (if gnus-fetch-old-headers 
4955                                     (cons 1 articles) articles) 
4956                                 gnus-newsgroup-name)))
4957                 (progn
4958                   (gnus-get-newsgroup-headers-xover articles))
4959               (gnus-get-newsgroup-headers)))
4960       ;; If we were to fetch old headers, but the backend didn't
4961       ;; support XOVER, then it is possible we fetched one article
4962       ;; that we shouldn't have. If that's the case, we pop it off the
4963       ;; list of headers.
4964       (and (not (eq gnus-headers-retrieved-by 'nov))
4965            gnus-fetch-old-headers
4966            gnus-newsgroup-headers
4967            (/= (header-number (car gnus-newsgroup-headers)) (car articles))
4968            (setq gnus-newsgroup-headers (cdr gnus-newsgroup-headers)))
4969       ;; Remove cancelled articles from the list of unread articles.
4970       (setq gnus-newsgroup-unreads
4971             (gnus-intersection 
4972              gnus-newsgroup-unreads
4973              (mapcar (lambda (headers) (header-number headers))
4974                      gnus-newsgroup-headers)))
4975       ;; Check whether auto-expire is to be done in this group.
4976       (setq gnus-newsgroup-auto-expire
4977             (and (stringp gnus-auto-expirable-newsgroups)
4978                  (string-match gnus-auto-expirable-newsgroups 
4979                                (gnus-group-real-name group))))
4980       ;; First and last article in this newsgroup.
4981       (and gnus-newsgroup-headers
4982            (setq gnus-newsgroup-begin 
4983                  (header-number (car gnus-newsgroup-headers)))
4984            (setq gnus-newsgroup-end
4985                  (header-number (gnus-last-element gnus-newsgroup-headers))))
4986       (setq gnus-reffed-article-number -1)
4987       ;; GROUP is successfully selected.
4988       (or gnus-newsgroup-headers t))))
4989
4990 (defun gnus-articles-to-read (group read-all)
4991   ;; Find out what articles the user wants to read.
4992   (let* ((articles
4993           ;; Select all articles if `read-all' is non-nil, or if all the
4994           ;; unread articles are dormant articles.
4995           (if (or read-all
4996                   (= (length gnus-newsgroup-unreads) 
4997                      (length gnus-newsgroup-scored)))
4998               (gnus-uncompress-sequence 
4999                (gnus-gethash group gnus-active-hashtb))
5000             gnus-newsgroup-unreads))
5001          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5002          (scored (length scored-list))
5003          (number (length articles))
5004          (marked (+ (length gnus-newsgroup-marked)
5005                     (length gnus-newsgroup-dormant)))
5006          (select
5007           (condition-case ()
5008               (cond ((and (or (<= scored marked)
5009                               (= scored number))
5010                           (numberp gnus-large-newsgroup)
5011                           (> number gnus-large-newsgroup))
5012                      (let ((input
5013                             (read-string
5014                              (format
5015                               "How many articles from %s (default %d): "
5016                               gnus-newsgroup-name number))))
5017                        (if (string-equal input "")
5018                            number input)))
5019                     ((and (> scored marked) (< scored number))
5020                      (let ((input
5021                             (read-string
5022                              (format 
5023                               "%s %s (%d scored, %d total, %d default): "
5024                               "How many articles from"
5025                               group scored number scored))))
5026                        (if (string-equal input "")
5027                            scored input)))
5028                     (t number))
5029             (quit 0))))
5030     (setq select (if (numberp select) select (string-to-number select)))
5031     (if (zerop select)
5032         ()
5033       (if (and (not (zerop scored)) (<= (abs select) scored))
5034           (progn
5035             (setq articles (sort scored-list '<))
5036             (setq number (length articles)))
5037         (setq articles (copy-sequence articles)))
5038
5039       (if (< (abs select) number)
5040           (if (< select 0) 
5041               ;; Select the N oldest articles.
5042               (setcdr (nthcdr (1- (abs select)) articles) nil)
5043             ;; Select the N most recent articles.
5044             (setq articles (nthcdr (- number select) articles))))
5045       (setq gnus-newsgroup-unselected
5046             (gnus-set-difference gnus-newsgroup-unreads articles))
5047       articles)))
5048
5049 (defun gnus-killed-articles (killed articles)
5050   (let (out)
5051     (while articles
5052       (if (inline (gnus-member-of-range (car articles) killed))
5053           (setq out (cons (car articles) out)))
5054       (setq articles (cdr articles)))
5055     out))
5056
5057 (defun gnus-adjust-marked-articles (info)
5058   "Remove all marked articles that are no longer legal."
5059   (let ((marked-lists (nth 3 info))
5060         (active (gnus-gethash (car info) gnus-active-hashtb))
5061         marked m prev)
5062     ;; There are four types of marked articles - ticked, replied,
5063     ;; expirable and dormant.  
5064     (while marked-lists
5065       (setq m (cdr (setq prev (car marked-lists))))
5066       (cond ((or (eq 'tick (car prev)) (eq 'dormant (car prev)))
5067              ;; Make sure that all ticked articles are a subset of the
5068              ;; unread/unselected articles.
5069              (while m
5070                (if (or (memq (car m) gnus-newsgroup-unreads)
5071                        (memq (car m) gnus-newsgroup-unselected))
5072                    (setq prev m)
5073                  (setcdr prev (cdr m)))
5074                (setq m (cdr m))))
5075             ((eq 'score (car prev))
5076              ;; Scored articles should be a subset of
5077              ;; unread/unselected articles. 
5078              (while m
5079                (if (or (memq (car (car m)) gnus-newsgroup-unreads)
5080                        (memq (car (car m)) gnus-newsgroup-unreads))
5081                    (setq prev m)
5082                  (setcdr prev (cdr m)))
5083                (setq m (cdr m))))
5084             ((eq 'bookmark (car prev))
5085              ;; Bookmarks should be a subset of active articles.
5086              (while m
5087                (if (< (car (car m)) (car active))
5088                    (setcdr prev (cdr m))
5089                  (setq prev m))
5090                (setq m (cdr m))))
5091             ((eq 'killed (car prev))
5092              ;; Articles that have been through the kill process are
5093              ;; to be a subset of active articles.
5094              (while (and m (< (cdr (car m)) (car active)))
5095                (setcdr prev (cdr m))
5096                (setq m (cdr m)))
5097              (if (and m (< (car (car m)) (car active))) 
5098                  (setcar (car m) (car active))))
5099             ((or (eq 'reply (car marked)) (eq 'expire (car marked)))
5100              ;; The replied and expirable articles have to be articles
5101              ;; that are active. 
5102              (while m
5103                (if (< (car m) (car active))
5104                    (setcdr prev (cdr m))
5105                  (setq prev m))
5106                (setq m (cdr m)))))
5107       (setq marked-lists (cdr marked-lists)))
5108     ;; Remove all lists that are empty.
5109     (setq marked-lists (nth 3 info))
5110     (if marked-lists
5111         (progn
5112           (while (= 1 (length (car marked-lists)))
5113             (setq marked-lists (cdr marked-lists)))
5114           (setq m (cdr (setq prev marked-lists)))
5115           (while m
5116             (if (= 1 (length (car m)))
5117                 (setcdr prev (cdr m))
5118               (setq prev m))
5119             (setq m (cdr m)))
5120           (setcar (nthcdr 3 info) marked-lists)))
5121     ;; Finally, if there are no marked lists at all left, and if there
5122     ;; are no elements after the lists in the info list, we just chop
5123     ;; the info list off before the marked lists.
5124     (if (and (null marked-lists) (not (nthcdr 4 info)))
5125         (setcdr (nthcdr 2 info) nil)))
5126   info)
5127
5128 (defun gnus-set-marked-articles 
5129   (info ticked replied expirable killed dormant bookmark score) 
5130   "Enter the various lists of marked articles into the newsgroup info list."
5131   (let (newmarked)
5132     (and ticked (setq newmarked (cons (cons 'tick ticked) nil)))
5133     (and replied (setq newmarked (cons (cons 'reply replied) newmarked)))
5134     (and expirable (setq newmarked (cons (cons 'expire expirable) 
5135                                          newmarked)))
5136     (and killed (setq newmarked (cons (cons 'killed killed) newmarked)))
5137     (and dormant (setq newmarked (cons (cons 'dormant dormant) newmarked)))
5138     (and bookmark (setq newmarked (cons (cons 'bookmark bookmark) 
5139                                         newmarked)))
5140     (and score (setq newmarked (cons (cons 'score score) newmarked)))
5141     (if (nthcdr 3 info)
5142         (if newmarked
5143             (setcar (nthcdr 3 info) newmarked)
5144           (if (not (nthcdr 4 info))
5145               (setcdr (nthcdr 2 info) nil)
5146             (setcar (nthcdr 3 info) nil)))
5147       (if newmarked
5148           (setcdr (nthcdr 2 info) (cons newmarked nil))))))
5149
5150 (defun gnus-add-marked-articles (group type articles &optional info force)
5151   ;; Add ARTICLES of TYPE to the info of GROUP.
5152   ;; If INFO is non-nil, use that info. If FORCE is non-nil, don't
5153   ;; add, but replace this marked articles of TYPE with ARTICLES.
5154   (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
5155         marked m)
5156     (or (not info)
5157         (and (not (setq marked (nthcdr 3 info)))
5158              (setcdr (nthcdr 2 info) (list (list (cons type articles)))))
5159         (and (not (setq m (assq type (car marked))))
5160              (setcar marked (cons (cons type articles) (car marked))))
5161         (if force
5162             (setcdr m articles)
5163           (nconc m articles)))))
5164          
5165 (defun gnus-set-mode-line (where)
5166   "This function sets the mode line of the article or summary buffers.
5167 If WHERE is `summary', the summary mode line format will be used."
5168   (if (memq where gnus-updated-mode-lines)
5169       (let (mode-string)
5170         (save-excursion
5171           (set-buffer gnus-summary-buffer)
5172           (let* ((mformat (if (eq where 'article) 
5173                               gnus-article-mode-line-format-spec
5174                             gnus-summary-mode-line-format-spec))
5175                  (group-name gnus-newsgroup-name)
5176                  (article-number (or gnus-current-article 0))
5177                  (unread (- (length gnus-newsgroup-unreads)
5178                             (length gnus-newsgroup-dormant)))
5179                  (unselected (length gnus-newsgroup-unselected))
5180                  (unread-and-unselected
5181                   (cond ((and (zerop unread) (zerop unselected)) "")
5182                         ((zerop unselected) (format "{%d more}" unread))
5183                         (t (format "{%d(+%d) more}" unread unselected))))
5184                  (subject
5185                   (if gnus-current-headers
5186                       (header-subject gnus-current-headers) ""))
5187                  (max-len (if (eq where 'summary) 59 59)))
5188             (setq mode-string (eval mformat))
5189             (if (> (length mode-string) max-len) 
5190                 (setq mode-string 
5191                       (concat (substring mode-string 0 (- max-len 3)) "...")))
5192             (setq mode-string (format (format "%%-%ds" max-len 5)
5193                                       mode-string))))
5194         (setq mode-line-buffer-identification mode-string)
5195         (set-buffer-modified-p t))))
5196
5197 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5198   "Go through the HEADERS list and add all Xrefs to a hash table.
5199 The resulting hash table is returned, or nil if no Xrefs were found."
5200   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
5201          (prefix (if (and 
5202                       (gnus-group-foreign-p from-newsgroup)
5203                       (not (memq 'virtual 
5204                                  (assoc (symbol-name (car from-method))
5205                                         gnus-valid-select-methods))))
5206                      (gnus-group-real-prefix from-newsgroup)))
5207          (xref-hashtb (make-vector 63 0))
5208          start group entry number xrefs header)
5209     (while headers
5210       (setq header (car headers))
5211       (if (and (setq xrefs (header-xref header))
5212                (not (memq (header-number header) unreads)))
5213           (progn
5214             (setq start 0)
5215             (while (string-match "\\([^ ]+\\):\\([0-9]+\\)" xrefs start)
5216               (setq start (match-end 0))
5217               (setq group (concat prefix (substring xrefs (match-beginning 1) 
5218                                                     (match-end 1))))
5219               (setq number 
5220                     (string-to-int (substring xrefs (match-beginning 2) 
5221                                               (match-end 2))))
5222               (if (setq entry (gnus-gethash group xref-hashtb))
5223                   (setcdr entry (cons number (cdr entry)))
5224                 (gnus-sethash group (cons number nil) xref-hashtb)))))
5225       (setq headers (cdr headers)))
5226     (if start xref-hashtb nil)))
5227
5228 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads expirable)
5229   "Look through all the headers and mark the Xrefs as read."
5230   (let (name entry read info xref-hashtb idlist active num range exps method)
5231     (set-buffer gnus-group-buffer)
5232     (if (setq xref-hashtb 
5233               (gnus-create-xref-hashtb from-newsgroup headers unreads))
5234         (mapatoms 
5235          (lambda (group)
5236            (if (string= from-newsgroup (setq name (symbol-name group)))
5237                ()
5238              (setq idlist (symbol-value group))
5239              ;; Dead groups are not updated.
5240              (if (and (setq entry (gnus-gethash name gnus-newsrc-hashtb))
5241                       ;; Only do the xrefs if the group has the same
5242                       ;; select method as the group we have just read.
5243                       (or (and (not (nth 4 (setq info (nth 2 entry))))
5244                                (eq (gnus-find-method-for-group from-newsgroup)
5245                                    gnus-select-method))
5246                           (memq 'virtual 
5247                                 (assoc (symbol-name 
5248                                         (car (gnus-find-method-for-group 
5249                                               from-newsgroup)))
5250                                        gnus-valid-select-methods))
5251                           (equal (nth 4 info) 
5252                                  (setq method (gnus-find-method-for-group 
5253                                                from-newsgroup)))
5254                           (and (equal (car (nth 4 info)) (car method))
5255                                (equal (nth 1 (nth 4 info)) (nth 1 method))))
5256                       gnus-use-cross-reference
5257                       (or (not (eq gnus-use-cross-reference t))
5258                           (<= (nth 1 info) 5)))
5259                  (progn
5260                    (setq num 0)
5261                    ;; Set the new list of read articles in this group.
5262                    (setq active (gnus-gethash name gnus-active-hashtb))
5263                    ;; First peel off all illegal article numbers.
5264                    (if active
5265                        (let ((ids idlist)
5266                              (ticked (cdr (memq 'tick (nth 3 info))))
5267                              (dormant (cdr (memq 'dormant (nth 3 info))))
5268                              id)
5269                          (setq exps nil)
5270                          (while ids
5271                            (setq id (car ids))
5272                            (if (or (> id (cdr active))
5273                                    (< id (car active))
5274                                    (memq id ticked)
5275                                    (memq id dormant))
5276                                (setq idlist (delq id idlist)))
5277                            (and (memq id expirable)
5278                                 (setq exps (cons id exps)))
5279                            (setq ids (cdr ids)))))
5280                    ;; Update expirable articles.
5281                    (gnus-add-marked-articles nil 'expirable exps info)
5282                    (setcar (nthcdr 2 info)
5283                            (setq range
5284                                  (gnus-add-to-range 
5285                                   (nth 2 info) 
5286                                   (setq idlist (sort idlist '<)))))
5287                    ;; Then we have to re-compute how many unread
5288                    ;; articles there are in this group.
5289                    (if active
5290                        (progn
5291                          (if (atom (car range))
5292                              (if (not range)
5293                                  (setq num (- (1+ (cdr active)) (car active)))
5294                                (setq num (- (cdr active) (- (1+ (cdr range)) 
5295                                                             (car range)))))
5296                            (while range
5297                              (setq num (+ num (- (1+ (cdr (car range))) 
5298                                                  (car (car range)))))
5299                              (setq range (cdr range)))
5300                            (setq num (- (cdr active) num)))
5301                          ;; Update the number of unread articles.
5302                          (setcar 
5303                           entry 
5304                           (max 0 (- num (length 
5305                                          (cdr (memq 'tick (nth 3 info))) )
5306                                     (length (cdr (memq 'dormant 
5307                                                        (nth 3 info)))))))
5308                          ;; Update the group buffer.
5309                          (gnus-group-update-group name t)))))))
5310          xref-hashtb))))
5311
5312 (defsubst gnus-header-value ()
5313   (buffer-substring (match-end 0) (save-excursion (end-of-line) (point))))
5314
5315 (defun gnus-get-newsgroup-headers ()
5316   (setq gnus-article-internal-prepare-hook nil)
5317   (let ((cur nntp-server-buffer)
5318         (dependencies gnus-newsgroup-dependencies)
5319         (none-id 0)
5320         headers char article id dep end)
5321     (save-excursion
5322       (set-buffer nntp-server-buffer)
5323       (goto-char 1)
5324       ;; Search to the beginning of the next header. Error messages
5325       ;; do not begin with 2 or 3.
5326       (while (re-search-forward "^[23][0-9]+ " nil t)
5327         (let ((header (make-vector 9 nil))
5328               (c (following-char))
5329               (case-fold-search t)
5330               (p (point))
5331               from subject in-reply-to references ref)
5332           (setq id nil
5333                 ref nil
5334                 references nil
5335                 subject nil
5336                 from nil)
5337           (header-set-number header (setq article (read cur)))
5338           ;; This implementation of this function, with nine
5339           ;; search-forwards instead of the one re-search-forward and
5340           ;; a case (which basically was the old function) is actually
5341           ;; about twice as fast, even though it looks messier. You
5342           ;; can't have everything, I guess. Speed and elegance
5343           ;; doesn't always come hand in hand.
5344           (save-restriction
5345             (narrow-to-region (point) (save-excursion 
5346                                         (search-forward "\n.\n" nil t)))
5347             (if (search-forward "\nfrom: " nil t)
5348                 (header-set-from header (gnus-header-value))
5349               (header-set-from header "(nobody)"))
5350             (goto-char p)
5351             (if (search-forward "\nsubject: " nil t)
5352                 (header-set-subject header (gnus-header-value))
5353               (header-set-subject header "(none)"))
5354             (goto-char p)
5355             (and (search-forward "\nxref: " nil t)
5356                  (header-set-xref header (gnus-header-value)))
5357             (goto-char p)
5358             (and (search-forward "\nlines: " nil t)
5359                  (header-set-lines header (read cur)))
5360             (goto-char p)
5361             (and (search-forward "\ndate: " nil t)
5362                  (header-set-date header (gnus-header-value)))
5363             (goto-char p)
5364             (if (search-forward "\nmessage-id: " nil t)
5365                 (header-set-id header (setq id (gnus-header-value)))
5366               ;; If there was no message-id, we just fake one to make
5367               ;; subsequent routines simpler.
5368               (header-set-id 
5369                header 
5370                (setq id (concat "none+" (int-to-string 
5371                                          (setq none-id (1+ none-id)))))))
5372             (goto-char p)
5373             (if (search-forward "\nreferences: " nil t)
5374                 (progn
5375                   (header-set-references header (gnus-header-value))
5376                   (setq end (match-end 0))
5377                   (save-excursion
5378                     (setq ref 
5379                           (downcase
5380                            (buffer-substring
5381                             (progn 
5382                               (end-of-line)
5383                               (search-backward ">" end t)
5384                               (1+ (point)))
5385                             (progn
5386                               (search-backward "<" end t)
5387                               (point)))))))
5388               ;; Get the references from the in-reply-to header if there
5389               ;; ware no references and the in-reply-to header looks
5390               ;; promising. 
5391               (if (and (search-forward "\nin-reply-to: " nil t)
5392                        (setq in-reply-to (gnus-header-value))
5393                        (string-match "<[^>]+>" in-reply-to))
5394                   (progn
5395                     (header-set-references 
5396                      header 
5397                      (setq ref (substring in-reply-to (match-beginning 0)
5398                                           (match-end 0))))
5399                     (setq ref (downcase ref)))
5400                 (setq ref "none")))
5401             ;; We do some threading while we read the headers. The
5402             ;; message-id and the last reference are both entered into
5403             ;; the same hash table. Some tippy-toeing around has to be
5404             ;; done in case an article has arrived before the article
5405             ;; which it refers to.
5406             (if (boundp (setq dep (intern (downcase id) dependencies)))
5407                 (if (car (symbol-value dep))
5408                     ;; An article with this Message-ID has already
5409                     ;; been seen, so we ignore this one, except we add
5410                     ;; any additional Xrefs (in case the two articles
5411                     ;; came from different servers.
5412                     (progn
5413                       (header-set-xref 
5414                        (car (symbol-value dep))
5415                        (concat (or (header-xref (car (symbol-value dep))) "")
5416                                (or (header-xref header) "")))
5417                       (setq header nil))
5418                   (setcar (symbol-value dep) header))
5419               (set dep (list header)))
5420             (if header
5421                 (progn
5422                   (if (boundp (setq dep (intern ref dependencies)))
5423                       (setcdr (symbol-value dep) 
5424                               (cons header (cdr (symbol-value dep))))
5425                     (set dep (list nil header)))
5426                   (setq headers (cons header headers))))
5427             (goto-char (point-max))))))
5428     (nreverse headers)))
5429
5430 ;; The following macros and functions were written by Felix Lee
5431 ;; <flee@cse.psu.edu>. 
5432
5433 ;; This is almost 4x faster than (string-to-int (buffer-substring ... ))
5434 ;; primarily because of garbage collection.  -jwz
5435 (defmacro gnus-read-integer (&optional point move-p)
5436   (` ((, (if move-p 'progn 'save-excursion))
5437       (,@ (if point (list (list 'goto-char point))))
5438       (if (and (<= (following-char) ?9)
5439                (>= (following-char) ?0))
5440           (read (current-buffer))
5441         0))))
5442
5443 (defmacro gnus-nov-skip-field ()
5444   '(search-forward "\t" eol 'end))
5445
5446 (defmacro gnus-nov-field ()
5447   '(buffer-substring
5448     (point)
5449     (progn (gnus-nov-skip-field) (1- (point)))))
5450
5451 ;; Goes through the xover lines and returns a list of vectors
5452 (defun gnus-get-newsgroup-headers-xover (sequence)
5453   "Parse the news overview data in the server buffer, and return a
5454 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
5455   ;; Get the Xref when the users reads the articles since most/some
5456   ;; NNTP servers do not include Xrefs when using XOVER.
5457   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5458   (let ((cur nntp-server-buffer)
5459         (dependencies gnus-newsgroup-dependencies)
5460         (none 0)
5461         number headers header)
5462     (save-excursion
5463       (set-buffer nntp-server-buffer)
5464       (goto-char (point-min))
5465       (while (and sequence (not (eobp)))
5466         (setq number (read cur))
5467         (while (and sequence (< (car sequence) number))
5468           (setq sequence (cdr sequence)))
5469         (and sequence 
5470              (eq number (car sequence))
5471              (progn
5472                (setq sequence (cdr sequence))
5473                (if (setq header 
5474                          (inline (gnus-nov-parse-line number dependencies)))
5475                    (setq headers (cons header headers)))))
5476         (forward-line 1))
5477       (setq headers (nreverse headers)))
5478     headers))
5479
5480 (defun gnus-nov-parse-line (number dependencies)
5481   "Point has to be after the number on the beginning of the line."
5482   (let ((none 0)
5483         header eol ref id dep)
5484     (save-excursion
5485       (end-of-line)
5486       (setq eol (point)))
5487     (forward-char)
5488     ;; overview: [num subject from date id refs chars lines misc]
5489     (setq header
5490           (vector 
5491            number                       ; number
5492            (gnus-nov-field)             ; subject
5493            (gnus-nov-field)             ; from
5494            (gnus-nov-field)             ; date
5495            (setq id (gnus-nov-field))   ; id
5496            (progn
5497              (save-excursion
5498                (let ((beg (point)))
5499                  (search-forward "\t" eol)
5500                  (if (search-backward ">" beg t)
5501                      (setq ref 
5502                            (downcase 
5503                             (buffer-substring 
5504                              (1+ (point))
5505                              (progn
5506                                (search-backward "<" beg t)
5507                                (point)))))
5508                    (setq ref nil))))
5509              (gnus-nov-field))          ; refs
5510            (read (current-buffer))      ; chars
5511            (read (current-buffer))      ; lines
5512            (if (/= (following-char) ?\t)
5513                nil
5514              (forward-char 1)
5515              (gnus-nov-field))          ; misc
5516            ))
5517     ;; We build the thread tree.
5518     (if (boundp 
5519          (setq dep 
5520                (intern 
5521                 (downcase 
5522                  (or id (concat "none+"
5523                                 (int-to-string 
5524                                  (setq none (1+ none))))))
5525                 dependencies)))
5526         (if (car (symbol-value dep))
5527             ;; An article with this Message-ID has already been seen,
5528             ;; so we ignore this one, except we add any additional
5529             ;; Xrefs (in case the two articles came from different
5530             ;; servers.
5531             (progn
5532               (header-set-xref 
5533                (car (symbol-value dep))
5534                (concat (or (header-xref (car (symbol-value dep))) "")
5535                        (or (header-xref header) "")))
5536               (setq header nil))
5537           (setcar (symbol-value dep) header))
5538       (set dep (list header)))
5539     (if header
5540         (progn
5541           (if (boundp (setq dep (intern (or ref "none") 
5542                                         dependencies)))
5543               (setcdr (symbol-value dep) 
5544                       (cons header (cdr (symbol-value dep))))
5545             (set dep (list nil header)))))
5546     header))
5547
5548 (defun gnus-article-get-xrefs ()
5549   "Fill in the Xref value in `gnus-current-headers', if necessary.
5550 This is meant to be called in `gnus-article-internal-prepare-hook'."
5551   (or (not gnus-use-cross-reference)
5552       (header-xref gnus-current-headers)
5553       (let ((case-fold-search t)
5554             xref)
5555         (save-restriction
5556           (gnus-narrow-to-headers)
5557           (goto-char (point-min))
5558           (if (or (and (eq (downcase (following-char)) ?x)
5559                        (looking-at "Xref:"))
5560                   (search-forward "\nXref:" nil t))
5561               (progn
5562                 (goto-char (1+ (match-end 0)))
5563                 (setq xref (buffer-substring (point) 
5564                                              (progn (end-of-line) (point))))
5565                 (save-excursion
5566                   (set-buffer gnus-summary-buffer)
5567                   (header-set-xref gnus-current-headers xref))))))))
5568
5569 (defalias 'gnus-find-header-by-number 'gnus-get-header-by-number)
5570 (make-obsolete 'gnus-find-header-by-number 'gnus-get-header-by-number)
5571
5572 ;; Return a header specified by a NUMBER.
5573 (defun gnus-get-header-by-number (number)
5574   (save-excursion
5575     (set-buffer gnus-summary-buffer)
5576     (or gnus-newsgroup-headers-hashtb-by-number
5577         (gnus-make-headers-hashtable-by-number))
5578     (gnus-gethash (int-to-string number)
5579                   gnus-newsgroup-headers-hashtb-by-number)))
5580
5581 (defun gnus-make-headers-hashtable-by-number ()
5582   "Make hashtable for the variable gnus-newsgroup-headers by number."
5583   (save-excursion
5584     (set-buffer gnus-summary-buffer)
5585     (let ((headers gnus-newsgroup-headers)
5586           header)
5587       (setq gnus-newsgroup-headers-hashtb-by-number
5588             (gnus-make-hashtable (length headers)))
5589       (while headers
5590         (setq header (car headers))
5591         (gnus-sethash (int-to-string (header-number header))
5592                       header gnus-newsgroup-headers-hashtb-by-number)
5593         (setq headers (cdr headers))))))
5594
5595 (defun gnus-more-header-backward ()
5596   "Find new header backward."
5597   (let ((first (car (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
5598         (artnum gnus-newsgroup-begin)
5599         (header nil))
5600     (while (and (not header)
5601                 (> artnum first))
5602       (setq artnum (1- artnum))
5603       (setq header (gnus-read-header artnum)))
5604     header))
5605
5606 (defun gnus-more-header-forward ()
5607   "Find new header forward."
5608   (let ((last (cdr (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
5609         (artnum gnus-newsgroup-end)
5610         (header nil))
5611     (while (and (not header)
5612                 (< artnum last))
5613       (setq artnum (1+ artnum))
5614       (setq header (gnus-read-header artnum)))
5615     header))
5616
5617 (defun gnus-extend-newsgroup (header &optional backward)
5618   "Extend newsgroup selection with HEADER.
5619 Optional argument BACKWARD means extend toward backward."
5620   (if header
5621       (let ((artnum (header-number header)))
5622         (setq gnus-newsgroup-headers
5623               (if backward
5624                   (cons header gnus-newsgroup-headers)
5625                 (nconc gnus-newsgroup-headers (list header))))
5626         (setq gnus-newsgroup-unselected
5627               (delq artnum gnus-newsgroup-unselected))
5628         (setq gnus-newsgroup-begin (min gnus-newsgroup-begin artnum))
5629         (setq gnus-newsgroup-end (max gnus-newsgroup-end artnum)))))
5630
5631 (defun gnus-summary-work-articles (n)
5632   "Return a list of articles to be worked upon. The prefix argument,
5633 the list of process marked articles, and the current article will be
5634 taken into consideration."
5635   (let (articles)
5636     (if (and n (numberp n))
5637         (let ((backward (< n 0))
5638               (n (abs n)))
5639           (save-excursion
5640             (while (and (> n 0)
5641                         (setq articles (cons (gnus-summary-article-number) 
5642                                              articles))
5643                         (gnus-summary-search-forward nil nil backward))
5644               (setq n (1- n))))
5645           (sort articles (function <)))
5646       (or (reverse gnus-newsgroup-processable)
5647           (list (gnus-summary-article-number))))))
5648
5649 (defun gnus-summary-search-group (&optional backward use-level)
5650   "Search for next unread newsgroup.
5651 If optional argument BACKWARD is non-nil, search backward instead."
5652   (save-excursion
5653     (set-buffer gnus-group-buffer)
5654     (save-excursion
5655       ;; We don't want to alter current point of group mode buffer.
5656       (if (gnus-group-search-forward 
5657            backward nil
5658            (if use-level (gnus-group-group-level) nil))
5659           (gnus-group-group-name)))))
5660
5661 (defun gnus-summary-best-group ()
5662   "Find the name of the best unread group."
5663   (save-excursion
5664     (set-buffer gnus-group-buffer)
5665     (save-excursion
5666       (gnus-group-best-unread-group))))
5667
5668 (defun gnus-summary-search-subject (&optional backward unread subject)
5669   "Search for article forward.
5670 If BACKWARD is non-nil, search backward.
5671 If UNREAD is non-nil, only unread articles are selected.
5672 If SUBJECT is non-nil, the article which has the same subject will be
5673 searched for." 
5674   (let ((func (if backward 'previous-single-property-change
5675                 'next-single-property-change))
5676         (beg (point))
5677         (did t)
5678         pos)
5679     (beginning-of-line)
5680     (forward-char (if backward (if (bobp) 0 -1) (if (eobp) 0 1)))
5681     (while (and (setq pos (funcall func (point) 'gnus-number))
5682                 (goto-char (if backward (1- pos) pos))
5683                 (setq did
5684                       (not (and (or (not unread)
5685                                     (eq (get-text-property (point) 'gnus-mark) 
5686                                         gnus-unread-mark))
5687                                 (or (not subject)
5688                                     (equal (gnus-simplify-subject-re 
5689                                             subject)
5690                                            (gnus-simplify-subject-re
5691                                             (get-text-property 
5692                                              (point) 
5693                                              'gnus-subject)))))))
5694                 (if backward (if (bobp) nil (forward-char -1) t)
5695                   (if (eobp) nil (forward-char 1) t))))
5696     (if did
5697         (progn (goto-char beg) nil)
5698       (prog1
5699           (get-text-property (point) 'gnus-number)
5700         (gnus-summary-position-cursor)))))
5701
5702 (defun gnus-summary-search-forward (&optional unread subject backward)
5703   "Search for article forward.
5704 If UNREAD is non-nil, only unread articles are selected.
5705 If SUBJECT is non-nil, the article which has the same subject will be
5706 searched for. 
5707 If BACKWARD is non-nil, the search will be performed backwards instead."
5708   (gnus-summary-search-subject backward unread subject))
5709
5710 (defun gnus-summary-search-backward (&optional unread subject)
5711   "Search for article backward.
5712 If 1st optional argument UNREAD is non-nil, only unread article is selected.
5713 If 2nd optional argument SUBJECT is non-nil, the article which has
5714 the same subject will be searched for."
5715   (gnus-summary-search-forward unread subject t))
5716
5717 (defun gnus-summary-article-number (&optional number-or-nil)
5718   "The article number of the article on the current line.
5719 If there isn's an article number here, then we return the current
5720 article number."
5721   (let ((number (get-text-property (save-excursion (beginning-of-line) (point))
5722                                    'gnus-number)))
5723     (if number-or-nil number (or number gnus-current-article))))
5724
5725 (defun gnus-summary-thread-level ()
5726   "The thread level of the article on the current line."
5727   (or (get-text-property (save-excursion (beginning-of-line) (point))
5728                          'gnus-thread)
5729       0))
5730
5731 (defun gnus-summary-pseudo-article ()
5732   "The thread level of the article on the current line."
5733   (get-text-property (save-excursion (beginning-of-line) (point)) 
5734                      'gnus-pseudo))
5735
5736 (defun gnus-summary-article-mark ()
5737   "The mark on the current line."
5738   (get-text-property (save-excursion (beginning-of-line) (point))
5739                      'gnus-mark))
5740
5741 (defun gnus-summary-subject-string ()
5742   "Return current subject string or nil if nothing."
5743   (get-text-property (save-excursion (beginning-of-line) (point))
5744                      'gnus-subject))
5745
5746 (defalias 'gnus-summary-score 'gnus-summary-article-score)
5747 (make-obsolete 'gnus-summary-score 'gnus-summary-article-score)
5748 (defun gnus-summary-article-score ()
5749   "Return current article score."
5750   (or (cdr (assq (gnus-summary-article-number) gnus-newsgroup-scored))
5751       gnus-summary-default-score))
5752
5753 (defun gnus-summary-recenter ()
5754   "Center point in summary window."
5755   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5756   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
5757   (let ((half (/ (- (window-height) 2) 2)))
5758     (and 
5759      ;; It has to be wanted,
5760      gnus-auto-center-summary 
5761      ;; the article buffer must be displayed,
5762      (get-buffer-window gnus-article-buffer)
5763      ;; there must be lines left to scroll forward,
5764      (zerop (save-excursion (forward-line (1+ half))))
5765      ;; so we recenter.
5766      (recenter half))))
5767
5768 (defun gnus-summary-jump-to-group (newsgroup)
5769   "Move point to NEWSGROUP in group mode buffer."
5770   ;; Keep update point of group mode buffer if visible.
5771   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5772       (save-window-excursion
5773         ;; Take care of tree window mode.
5774         (if (get-buffer-window gnus-group-buffer)
5775             (pop-to-buffer gnus-group-buffer))
5776         (gnus-group-jump-to-group newsgroup))
5777     (save-excursion
5778       ;; Take care of tree window mode.
5779       (if (get-buffer-window gnus-group-buffer)
5780           (pop-to-buffer gnus-group-buffer)
5781         (set-buffer gnus-group-buffer))
5782       (gnus-group-jump-to-group newsgroup))))
5783
5784 ;; This function returns a list of article numbers based on the
5785 ;; difference between the ranges of read articles in this group and
5786 ;; the range of active articles.
5787 (defun gnus-list-of-unread-articles (group)
5788   (let* ((read (nth 2 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
5789          (active (gnus-gethash group gnus-active-hashtb))
5790          (last (cdr active))
5791          unread first nlast unread)
5792     ;; If none are read, then all are unread. 
5793     (if (not read)
5794           (setq first (car active))
5795       ;; If the range of read articles is a single range, then the
5796       ;; first unread article is the article after the last read
5797       ;; article. Sounds logical, doesn't it?
5798       (if (atom (car read))
5799           (setq first (1+ (cdr read)))
5800         ;; `read' is a list of ranges.
5801         (while read
5802           (if first 
5803               (while (< first nlast)
5804                 (setq unread (cons first unread))
5805                 (setq first (1+ first))))
5806           (setq first (1+ (cdr (car read))))
5807           (setq nlast (car (car (cdr read))))
5808           (setq read (cdr read)))))
5809     ;; And add the last unread articles.
5810     (while (<= first last)
5811       (setq unread (cons first unread))
5812       (setq first (1+ first)))
5813     ;; Return the list of unread articles.
5814     (nreverse unread)))
5815
5816
5817 ;; Various summary commands
5818
5819 (defun gnus-summary-universal-argument ()
5820   "Perform any operation on all articles marked with the process mark."
5821   (interactive)
5822   (gnus-set-global-variables)
5823   (let ((articles (reverse gnus-newsgroup-processable))
5824         key func)
5825     (or articles (error "No articles marked"))
5826     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
5827         (error "Undefined key"))
5828     (while articles
5829       (gnus-summary-goto-subject (car articles))
5830       (command-execute func)
5831       (gnus-summary-remove-process-mark (car articles))
5832       (setq articles (cdr articles)))))
5833
5834 (defun gnus-summary-toggle-truncation (arg)
5835   "Toggle truncation of summary lines.
5836 With arg, turn line truncation on iff arg is positive."
5837   (interactive "P")
5838   (setq truncate-lines
5839         (if (null arg) (not truncate-lines)
5840           (> (prefix-numeric-value arg) 0)))
5841   (redraw-display))
5842
5843 (defun gnus-summary-reselect-current-group (show-all)
5844   "Once exit and then reselect the current newsgroup.
5845 Prefix argument SHOW-ALL means to select all articles."
5846   (interactive "P")
5847   (gnus-set-global-variables)
5848   (let ((current-subject (gnus-summary-article-number)))
5849     (gnus-summary-exit t)
5850     ;; We have to adjust the point of group mode buffer because the
5851     ;; current point was moved to the next unread newsgroup by
5852     ;; exiting.
5853     (gnus-summary-jump-to-group gnus-newsgroup-name)
5854     (gnus-group-read-group show-all t)
5855     (gnus-summary-goto-subject current-subject)
5856     ))
5857
5858 (defun gnus-summary-rescan-group (all)
5859   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5860   (interactive "P")
5861   (gnus-set-global-variables)
5862   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
5863   (let ((group gnus-newsgroup-name))
5864     (gnus-summary-exit t)
5865     (gnus-summary-jump-to-group group)
5866     (save-excursion
5867       (set-buffer gnus-group-buffer)
5868       (gnus-group-get-new-news-this-group 1))
5869     (gnus-summary-jump-to-group group)
5870     (gnus-group-read-group all)))
5871
5872 (defun gnus-summary-exit (&optional temporary)
5873   "Exit reading current newsgroup, and then return to group selection mode.
5874 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5875   (interactive)
5876   (gnus-set-global-variables)
5877   (gnus-kill-save-kill-buffer)
5878   (let* ((group gnus-newsgroup-name)
5879          (quit-buffer (cdr (assoc 'quit-buffer (gnus-find-method-for-group
5880                                                 gnus-newsgroup-name))))
5881          (mode major-mode)
5882          (method (car (gnus-find-method-for-group group)))
5883          (buf (current-buffer)))
5884     (if gnus-newsgroup-kill-headers
5885         (setq gnus-newsgroup-killed
5886               (gnus-compress-sequence
5887                (nconc
5888                 (gnus-intersection
5889                  (gnus-uncompress-sequence gnus-newsgroup-killed)
5890                  (setq gnus-newsgroup-unselected
5891                        (sort gnus-newsgroup-unselected '<)))
5892                 (setq gnus-newsgroup-unreads
5893                       (sort gnus-newsgroup-unreads '<))))))
5894     (or (listp (cdr gnus-newsgroup-killed))
5895         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5896     (let ((updated nil)
5897           (headers gnus-newsgroup-headers))
5898       (gnus-close-group group)
5899       (run-hooks 'gnus-exit-group-hook)
5900       (gnus-score-save)
5901       (and gnus-newsgroup-expunged-buffer 
5902            (buffer-name gnus-newsgroup-expunged-buffer)
5903            (kill-buffer gnus-newsgroup-expunged-buffer))
5904       (gnus-update-read-articles 
5905        group gnus-newsgroup-unreads gnus-newsgroup-unselected 
5906        gnus-newsgroup-marked
5907        t gnus-newsgroup-replied gnus-newsgroup-expirable
5908        gnus-newsgroup-killed gnus-newsgroup-dormant
5909        gnus-newsgroup-bookmarks gnus-newsgroup-scored)
5910       ;; t means ignore unsubscribed newsgroups.
5911       (and gnus-use-cross-reference
5912            (gnus-mark-xrefs-as-read 
5913             group headers gnus-newsgroup-unreads gnus-newsgroup-expirable))
5914       ;; Do not switch windows but change the buffer to work.
5915       (set-buffer gnus-group-buffer)
5916       (or (eq 'nndigest method)
5917           (gnus-group-update-group group)))
5918     ;; Make sure where I was, and go to next newsgroup.
5919     (if (eq method 'nndigest)
5920         ()
5921       (gnus-group-jump-to-group group)
5922       (gnus-group-next-unread-group 1))
5923     (if temporary
5924         ;; If exiting temporary, caller should adjust group mode
5925         ;; buffer point by itself.
5926         nil                             ;Nothing to do.
5927       ;; Return to group mode buffer. 
5928       (gnus-configure-windows 'newsgroups t)
5929       (and (get-buffer buf) 
5930            (eq mode 'gnus-summary-mode)
5931            (kill-buffer buf))
5932       (if (get-buffer gnus-article-buffer)
5933           (bury-buffer gnus-article-buffer))
5934       (setq gnus-current-select-method gnus-select-method)
5935       (pop-to-buffer gnus-group-buffer)
5936       (if (and quit-buffer (buffer-name quit-buffer))
5937           (progn
5938             (switch-to-buffer quit-buffer)
5939             (gnus-set-global-variables)
5940             (gnus-configure-windows 'summary))))))
5941
5942 (defun gnus-summary-quit (&optional no-questions)
5943   "Quit reading current newsgroup without updating read article info."
5944   (interactive)
5945   (let ((group gnus-newsgroup-name)
5946         (quit-buffer (cdr (assoc 'quit-buffer (gnus-find-method-for-group
5947                                                gnus-newsgroup-name)))))
5948     (if (or no-questions
5949             gnus-expert-user
5950             (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
5951         (progn
5952           (gnus-close-group group)
5953           ;; Return to group selection mode.
5954           (and gnus-newsgroup-expunged-buffer 
5955                (buffer-name gnus-newsgroup-expunged-buffer)
5956                (kill-buffer gnus-newsgroup-expunged-buffer))
5957           (gnus-configure-windows 'newsgroups)
5958           (if (get-buffer gnus-summary-buffer)
5959               (kill-buffer gnus-summary-buffer))
5960           (if (get-buffer gnus-article-buffer)
5961               (bury-buffer gnus-article-buffer))
5962           (pop-to-buffer gnus-group-buffer)
5963           (gnus-group-jump-to-group gnus-newsgroup-name)
5964           (gnus-group-next-group 1)
5965           (if (and quit-buffer (buffer-name quit-buffer))
5966               (progn
5967                 (switch-to-buffer quit-buffer)
5968                 (gnus-configure-windows 'summary)))))))
5969
5970 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5971 (defun gnus-summary-describe-group ()
5972   "Describe the current newsgroup."
5973   (interactive)
5974   (gnus-group-describe-group gnus-newsgroup-name))
5975
5976 (defun gnus-summary-describe-briefly ()
5977   "Describe summary mode commands briefly."
5978   (interactive)
5979   (message
5980     (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")))
5981
5982 ;; Walking around group mode buffer from summary mode.
5983
5984 (defun gnus-summary-next-group (&optional no-article group)
5985   "Exit current newsgroup and then select next unread newsgroup.
5986 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5987   (interactive "P")
5988   (gnus-set-global-variables)
5989   (let ((ingroup gnus-newsgroup-name)
5990         (sumbuf (current-buffer)))
5991     (gnus-summary-exit t)               ;Update all information.
5992     (gnus-group-jump-to-group ingroup)
5993     (let ((group (or group (gnus-summary-search-group)))
5994           (buf gnus-summary-buffer))
5995       (if (null group)
5996           (gnus-summary-quit t)
5997         (message "Selecting %s..." group)
5998         ;; We are now in group mode buffer.
5999         ;; Make sure group mode buffer point is on GROUP.
6000         (gnus-group-jump-to-group group)
6001         (unwind-protect
6002             (gnus-summary-read-group group nil no-article buf)
6003           (and (string= gnus-newsgroup-name ingroup)
6004                (progn
6005                  (set-buffer sumbuf)
6006                  (gnus-summary-quit t))))))))
6007
6008 (defun gnus-summary-prev-group (no-article)
6009   "Exit current newsgroup and then select previous unread newsgroup.
6010 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6011   (interactive "P")
6012   ;; Make sure group mode buffer point is on current newsgroup.
6013   (gnus-summary-jump-to-group gnus-newsgroup-name)
6014   (let ((group (gnus-summary-search-group t)))
6015     (if (null group)
6016         (progn
6017           (message "Exiting %s..." gnus-newsgroup-name)  
6018           (gnus-summary-exit)
6019           (message ""))
6020       (message "Selecting %s..." group)
6021       (gnus-summary-exit t)             ;Exit summary mode temporary.
6022       ;; We are now in group mode buffer.
6023       ;; We have to adjust point of group mode buffer because current
6024       ;; point is moved to next unread newsgroup by exiting.
6025       (gnus-summary-jump-to-group group)
6026       (gnus-summary-read-group group nil no-article)
6027       (or (eq (current-buffer)
6028               (get-buffer gnus-summary-buffer))
6029           (eq gnus-auto-select-next t)
6030           ;; Expected newsgroup has nothing to read since the articles
6031           ;; are marked as read by cross-referencing. So, try next
6032           ;; newsgroup. (Make sure we are in group mode buffer now.)
6033           (and (eq (current-buffer)
6034                    (get-buffer gnus-group-buffer))
6035                (gnus-summary-search-group t)
6036                (gnus-summary-read-group
6037                 (gnus-summary-search-group t) nil no-article))
6038           )
6039       )))
6040
6041 ;; Walking around summary lines.
6042
6043 (defun gnus-summary-first-subject (unread)
6044   "Go to the first unread subject.
6045 If UNREAD is non-nil, go to the first unread article.
6046 Returns nil if there are no unread articles."
6047   (let ((begin (point)))
6048     (if unread
6049         (if (not (gnus-goto-char 
6050                   (text-property-any (point-min) (point-max)
6051                                      'gnus-mark gnus-unread-mark)))
6052             (progn
6053               ;; If there is no unread articles, stay where you are.
6054               (goto-char begin)
6055               (message "No more unread articles")
6056               nil)
6057           t)
6058       (goto-char (point-min)))))
6059
6060 (defun gnus-summary-next-subject (n &optional unread)
6061   "Go to next N'th summary line.
6062 If N is negative, go to the previous N'th subject line.
6063 If UNREAD is non-nil, only unread articles are selected.
6064 The difference between N and the actual number of steps taken is
6065 returned."
6066   (interactive "p")
6067   (let ((backward (< n 0))
6068         (n (abs n)))
6069   (while (and (> n 0)
6070               (gnus-summary-search-forward unread nil backward))
6071     (setq n (1- n)))
6072   (gnus-summary-recenter)
6073   (if (/= 0 n) (message "No more%s articles" (if unread " unread" "")))
6074   (gnus-summary-position-cursor)
6075   n))
6076
6077 (defun gnus-summary-next-unread-subject (n)
6078   "Go to next N'th unread summary line."
6079   (interactive "p")
6080   (gnus-summary-next-subject n t))
6081
6082 (defun gnus-summary-prev-subject (n &optional unread)
6083   "Go to previous N'th summary line.
6084 If optional argument UNREAD is non-nil, only unread article is selected."
6085   (interactive "p")
6086   (gnus-summary-next-subject (- n) unread))
6087
6088 (defun gnus-summary-prev-unread-subject (n)
6089   "Go to previous N'th unread summary line."
6090   (interactive "p")
6091   (gnus-summary-next-subject (- n) t))
6092
6093 (defun gnus-summary-goto-subject (article)
6094   "Go the subject line of ARTICLE."
6095   (interactive
6096    (list
6097     (string-to-int
6098      (completing-read "Article number: "
6099                       (mapcar
6100                        (lambda (headers)
6101                          (list
6102                           (int-to-string (header-number headers))))
6103                        gnus-newsgroup-headers)
6104                       nil 'require-match))))
6105   (or article (error "No article number"))
6106   (if (or (eq article (gnus-summary-article-number t))
6107           (gnus-goto-char
6108            (text-property-any
6109             (point-min) (point-max) 'gnus-number article)))
6110       article))
6111
6112 ;; Walking around summary lines with displaying articles.
6113
6114 (defun gnus-summary-expand-window ()
6115   "Expand summary window to show headers full window."
6116   (interactive)
6117   (gnus-set-global-variables)
6118   (gnus-configure-windows 'summary)
6119   (pop-to-buffer gnus-summary-buffer))
6120
6121 (defun gnus-summary-display-article (article &optional all-header)
6122   "Display ARTICLE in article buffer."
6123   (gnus-set-global-variables)
6124   (if (null article)
6125       nil
6126     (gnus-article-prepare article all-header)
6127     (if (= (gnus-summary-article-mark) ?Z) 
6128         (progn
6129           (forward-line 1)
6130           (gnus-summary-position-cursor)))
6131     (run-hooks 'gnus-select-article-hook)
6132     (gnus-summary-recenter)
6133 ;    (set-window-point (get-buffer-window (current-buffer)) (point-max))
6134     (sit-for 0)
6135     (gnus-summary-goto-subject article)
6136     ;; Successfully display article.
6137     t))
6138
6139 (defun gnus-summary-select-article (&optional all-headers force pseudo)
6140   "Select the current article.
6141 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6142 non-nil, the article will be re-fetched even if it already present in
6143 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6144 be displayed."
6145   (and (not pseudo) (gnus-summary-pseudo-article)
6146        (error "This is a pseudo-article."))
6147   (let ((article (gnus-summary-article-number))
6148         (all-headers (not (not all-headers)))) ;Must be T or NIL.
6149     (if (or (null gnus-current-article)
6150             (null gnus-article-current)
6151             (/= article (cdr gnus-article-current))
6152             (not (equal (car gnus-article-current) gnus-newsgroup-name))
6153             force)
6154         ;; The requested article is different from the current article.
6155         (progn
6156           (gnus-summary-display-article article all-headers)
6157           article)
6158       (if all-headers (gnus-article-show-all-headers))
6159       (gnus-configure-windows 'article)
6160       (pop-to-buffer gnus-summary-buffer)
6161       nil)))
6162
6163 (defun gnus-summary-set-current-mark (&optional current-mark)
6164   "Obsolete function."
6165   nil)
6166
6167 (defun gnus-summary-next-article (unread &optional subject)
6168   "Select the article after the current one.
6169 If UNREAD is non-nil, only unread articles are selected."
6170   (interactive "P")
6171   (let ((header nil)
6172         (method (car (gnus-find-method-for-group gnus-newsgroup-name))))
6173     (cond ((gnus-summary-display-article
6174             (gnus-summary-search-forward unread subject)))
6175           ((and subject
6176                 gnus-auto-select-same
6177                 (gnus-set-difference gnus-newsgroup-unreads
6178                                      (append gnus-newsgroup-marked
6179                                              gnus-newsgroup-dormant))
6180                 (memq this-command
6181                       '(gnus-summary-next-unread-article
6182                         gnus-summary-next-page
6183                         gnus-summary-kill-same-subject-and-select
6184                         ;;gnus-summary-next-article
6185                         ;;gnus-summary-next-same-subject
6186                         ;;gnus-summary-next-unread-same-subject
6187                         )))
6188            ;; Wrap article pointer if there are unread articles.
6189            (let ((buffer (current-buffer))
6190                  (last-point (point)))
6191              ;; No more articles with same subject, so jump to the first
6192              ;; unread article.
6193              (gnus-summary-first-unread-article)
6194              ;;(and (eq buffer (current-buffer))
6195              ;; (= (point) last-point)
6196              ;; ;; Ignore given SUBJECT, and try again.
6197              ;; (gnus-summary-next-article unread nil))
6198              (and (eq buffer (current-buffer))
6199                   (< (point) last-point)
6200                   (message "Wrapped"))
6201              ))
6202           ((and gnus-auto-extend-newsgroup
6203                 (not unread)            ;Not unread only
6204                 (not subject)           ;Only if subject is not specified.
6205                 (setq header (gnus-more-header-forward)))
6206            ;; Extend to next article if possible.
6207            ;; Basic ideas by himacdonald@watdragon.waterloo.edu
6208            (gnus-extend-newsgroup header nil)
6209            ;; Threads feature must be turned off.
6210            (let ((buffer-read-only nil))
6211              (goto-char (point-max))
6212              (gnus-summary-prepare-threads (list header) 0))
6213            (gnus-summary-goto-article gnus-newsgroup-end))
6214           (t
6215            ;; Select next newsgroup automatically if requested.
6216            (gnus-summary-jump-to-group gnus-newsgroup-name)
6217            (let ((cmd (aref (this-command-keys) 0))
6218                  (group (if (eq gnus-keep-same-level 'best)
6219                             (gnus-summary-best-group)
6220                           (gnus-summary-search-group 
6221                            nil gnus-keep-same-level)))
6222                  (auto-select
6223                   (and gnus-auto-select-next
6224                        ;;(null (gnus-set-difference gnus-newsgroup-unreads
6225                        ;;                               gnus-newsgroup-marked))
6226                        (memq this-command
6227                              '(gnus-summary-next-unread-article
6228                                gnus-summary-next-article
6229                                gnus-summary-next-page
6230                                gnus-summary-next-same-subject
6231                                gnus-summary-next-unread-same-subject
6232                                gnus-summary-kill-same-subject
6233                                gnus-summary-kill-same-subject-and-select
6234                                ))
6235                        ;; Ignore characters typed ahead.
6236                        (not (input-pending-p))
6237                        )))
6238              ;; Keep just the event type of CMD.
6239              (if (listp cmd)
6240                  (setq cmd (car cmd)))
6241              (message "No more%s articles%s"
6242                       (if unread " unread" "")
6243                       (if (and auto-select
6244                                (not (eq gnus-auto-select-next 'quietly)))
6245                           (if (and group (not (eq method 'nndigest)))
6246                               (format " (Type %s for %s [%s])"
6247                                       (single-key-description cmd)
6248                                       group
6249                                       (car (gnus-gethash 
6250                                             group gnus-newsrc-hashtb)))
6251                             (format " (Type %s to exit %s)"
6252                                     (single-key-description cmd)
6253                                     gnus-newsgroup-name))
6254                         ""))
6255              ;; Select next unread newsgroup automagically.
6256              (cond ((and auto-select
6257                          (eq gnus-auto-select-next 'quietly))
6258                     ;; Select quietly.
6259                     (if (eq method 'nndigest)
6260                         (gnus-summary-exit)
6261                       (gnus-summary-next-group)))
6262                    (auto-select
6263                     ;; Confirm auto selection.
6264                     (let* ((event (read-event))
6265                            (type
6266                             (if (listp event)
6267                                 (car event)
6268                               event)))
6269                       (if (and (eq event type) (eq event cmd))
6270                           (if (eq method 'nndigest)
6271                               (gnus-summary-exit)
6272                             (gnus-summary-next-group))
6273                         (setq unread-command-events (list event)))))
6274                    )
6275              ))
6276           )))
6277
6278 (defun gnus-summary-next-unread-article ()
6279   "Select unread article after current one."
6280   (interactive)
6281   (gnus-summary-next-article t (and gnus-auto-select-same
6282                                     (gnus-summary-subject-string)))
6283   (gnus-summary-position-cursor))
6284
6285 (defun gnus-summary-prev-article (unread &optional subject)
6286   "Select the article after the current one.
6287 If UNREAD is non-nil, only unread articles are selected."
6288   (interactive "P")
6289   (let ((header nil))
6290     (cond ((gnus-summary-display-article
6291             (gnus-summary-search-backward unread subject)))
6292           ((and subject
6293                 gnus-auto-select-same
6294                 (gnus-set-difference gnus-newsgroup-unreads
6295                                      (append gnus-newsgroup-marked
6296                                              gnus-newsgroup-dormant))
6297                 (memq this-command
6298                       '(gnus-summary-prev-unread-article
6299                         gnus-summary-prev-page)))
6300            ;; Wrap article pointer if there are unread articles.
6301            (let ((buffer (current-buffer))
6302                  (last-point (point)))
6303              ;; No more articles with same subject, so jump to the first
6304              ;; unread article.
6305              (gnus-summary-first-unread-article)
6306              (and (eq buffer (current-buffer))
6307                   (< (point) last-point)
6308                   (message "Wrapped"))
6309              ))
6310           ((and gnus-auto-extend-newsgroup
6311                 (not unread)            ;Not unread only
6312                 (not subject)           ;Only if subject is not specified.
6313                 (setq header (gnus-more-header-backward)))
6314            ;; Extend to next article if possible.
6315            ;; Basic ideas by himacdonald@watdragon.waterloo.edu
6316            (gnus-extend-newsgroup header t)
6317            ;; Threads feature must be turned off.
6318            (let ((buffer-read-only nil))
6319              (goto-char (point-min))
6320              (gnus-summary-prepare-threads (list header) 0))
6321            (gnus-summary-goto-article gnus-newsgroup-begin))
6322           (t
6323            ;; Select prev newsgroup automatically if requested.
6324            (gnus-summary-jump-to-group gnus-newsgroup-name)
6325            (let ((cmd (aref (this-command-keys) 0))
6326                  (group (if (eq gnus-keep-same-level 'best)
6327                             (gnus-summary-best-group)
6328                           (gnus-summary-search-group 
6329                            t gnus-keep-same-level)))
6330                  (auto-select
6331                   (and gnus-auto-select-next
6332                        (memq this-command
6333                              '(gnus-summary-prev-unread-article
6334                                gnus-summary-prev-article
6335                                gnus-summary-prev-page))
6336                        ;; Ignore characters typed ahead.
6337                        (not (input-pending-p)))))
6338              ;; Keep just the event type of CMD.
6339              (if (listp cmd)
6340                  (setq cmd (car cmd)))
6341              (message "No more%s articles%s"
6342                       (if unread " unread" "")
6343                       (if (and auto-select
6344                                (not (eq gnus-auto-select-next 'quietly)))
6345                           (if group
6346                               (format " (Type %s for %s [%s])"
6347                                       (single-key-description cmd)
6348                                       group
6349                                       (car (gnus-gethash 
6350                                             group gnus-newsrc-hashtb)))
6351                             (format " (Type %s to exit %s)"
6352                                     (single-key-description cmd)
6353                                     gnus-newsgroup-name))
6354                         ""))
6355              ;; Select next unread newsgroup automagically.
6356              (cond ((and auto-select
6357                          (eq gnus-auto-select-next 'quietly))
6358                     ;; Select quietly.
6359                     (gnus-summary-prev-group 1))
6360                    (auto-select
6361                     ;; Confirm auto selection.
6362                     (let* ((event (read-event))
6363                            (type
6364                             (if (listp event)
6365                                 (car event)
6366                               event)))
6367                       (if (and (eq event type) (eq event cmd))
6368                           (gnus-summary-prev-group 1)
6369                         (setq unread-command-events (list event)))))
6370                    )
6371              ))
6372           )))
6373
6374 (defun gnus-summary-prev-unread-article ()
6375   "Select unred article before current one."
6376   (interactive)
6377   (gnus-summary-prev-article t (and gnus-auto-select-same
6378                                     (gnus-summary-subject-string))))
6379
6380 (defun gnus-summary-next-page (lines &optional circular)
6381   "Show next page of selected article.
6382 If end of article, select next article.
6383 Argument LINES specifies lines to be scrolled up.
6384 If CIRCULAR is non-nil, go to the start of the article instead of 
6385 instead of selecting the next article when reaching the end of the
6386 current article." 
6387   (interactive "P")
6388   (setq gnus-summary-buffer (current-buffer))
6389   (let ((article (gnus-summary-article-number))
6390         (endp nil))
6391     (if (or (null gnus-current-article)
6392             (null gnus-article-current)
6393             (/= article (cdr gnus-article-current))
6394             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6395         ;; Selected subject is different from current article's.
6396         (gnus-summary-display-article article)
6397       (gnus-configure-windows 'article)
6398       (pop-to-buffer gnus-summary-buffer)
6399       (gnus-eval-in-buffer-window
6400        gnus-article-buffer
6401        (setq endp (gnus-article-next-page lines)))
6402       (if endp
6403           (cond (circular
6404                  (gnus-summary-beginning-of-article))
6405                 (lines
6406                  (message "End of message"))
6407                 ((null lines)
6408                  (gnus-summary-next-unread-article))))))
6409   (gnus-summary-position-cursor))
6410
6411 (defun gnus-summary-prev-page (lines)
6412   "Show previous page of selected article.
6413 Argument LINES specifies lines to be scrolled down."
6414   (interactive "P")
6415   (let ((article (gnus-summary-article-number)))
6416     (if (or (null gnus-current-article)
6417             (null gnus-article-current)
6418             (/= article (cdr gnus-article-current))
6419             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6420         ;; Selected subject is different from current article's.
6421         (gnus-summary-display-article article)
6422       (gnus-configure-windows 'article)
6423       (pop-to-buffer gnus-summary-buffer)
6424       (gnus-eval-in-buffer-window gnus-article-buffer
6425         (gnus-article-prev-page lines))))
6426   (gnus-summary-position-cursor))
6427
6428 (defun gnus-summary-scroll-up (lines)
6429   "Scroll up (or down) one line current article.
6430 Argument LINES specifies lines to be scrolled up (or down if negative)."
6431   (interactive "p")
6432   (or (gnus-summary-select-article nil nil 'pseudo)
6433       (gnus-eval-in-buffer-window 
6434        gnus-article-buffer
6435        (cond ((> lines 0)
6436               (if (gnus-article-next-page lines)
6437                   (message "End of message")))
6438              ((< lines 0)
6439               (gnus-article-prev-page (- lines))))))
6440   (gnus-summary-position-cursor))
6441
6442 (defun gnus-summary-next-same-subject ()
6443   "Select next article which has the same subject as current one."
6444   (interactive)
6445   (gnus-summary-next-article nil (gnus-summary-subject-string)))
6446
6447 (defun gnus-summary-prev-same-subject ()
6448   "Select previous article which has the same subject as current one."
6449   (interactive)
6450   (gnus-summary-prev-article nil (gnus-summary-subject-string)))
6451
6452 (defun gnus-summary-next-unread-same-subject ()
6453   "Select next unread article which has the same subject as current one."
6454   (interactive)
6455   (gnus-summary-next-article t (gnus-summary-subject-string)))
6456
6457 (defun gnus-summary-prev-unread-same-subject ()
6458   "Select previous unread article which has the same subject as current one."
6459   (interactive)
6460   (gnus-summary-prev-article t (gnus-summary-subject-string)))
6461
6462 (defun gnus-summary-first-unread-article ()
6463   "Select the first unread article. 
6464 Return nil if there are no unread articles."
6465   (interactive)
6466   (prog1
6467       (if (gnus-summary-first-subject t)
6468           (gnus-summary-display-article (gnus-summary-article-number)))
6469     (gnus-summary-position-cursor)))
6470
6471 (defun gnus-summary-best-unread-article ()
6472   "Select the unread article with the highest score."
6473   (interactive)
6474   (gnus-set-global-variables)
6475   (let ((scored gnus-newsgroup-scored)
6476         (best -1000000)
6477         article art)
6478     (while scored
6479       (or (> best (cdr (car scored)))
6480           (and (memq (setq art (car (car scored))) gnus-newsgroup-unreads)
6481                (not (memq art gnus-newsgroup-marked))
6482                (not (memq art gnus-newsgroup-dormant))
6483                (if (= best (cdr (car scored)))
6484                    (setq article (min art article))
6485                  (setq article art)
6486                  (setq best (cdr (car scored))))))
6487       (setq scored (cdr scored)))
6488     (if article 
6489         (gnus-summary-goto-article article)
6490       (gnus-summary-first-unread-article))
6491     (gnus-summary-position-cursor)))
6492
6493 (defun gnus-summary-goto-article (article &optional all-headers)
6494   "Fetch ARTICLE and display it if it exists.
6495 If ALL-HEADERS is non-nil, no header lines are hidden."
6496   (interactive
6497    (list
6498     (string-to-int
6499      (completing-read 
6500       "Article number: "
6501       (mapcar (lambda (headers) (list (int-to-string (header-number headers))))
6502               gnus-newsgroup-headers) 
6503       nil 'require-match))))
6504   (if (gnus-summary-goto-subject article)
6505       (gnus-summary-display-article article all-headers))
6506   (gnus-summary-position-cursor))
6507
6508 (defun gnus-summary-goto-last-article ()
6509   "Go to the last article."
6510   (interactive)
6511   (if gnus-last-article
6512       (gnus-summary-goto-article gnus-last-article))
6513   (gnus-summary-position-cursor))
6514
6515 ;; Summary article oriented commands
6516
6517 (defun gnus-summary-refer-parent-article (n)
6518   "Refer parent article N times.
6519 The difference between N and the number of articles fetched is returned."
6520   (interactive "p")
6521   (gnus-set-global-variables)
6522   (while 
6523       (and 
6524        (> n 0)
6525        (let ((ref (header-references (gnus-get-header-by-number
6526                                       (gnus-summary-article-number)))))
6527          (if (and ref (not (equal ref ""))
6528                   (string-match "<[^<>]*>[ \t]*$" ref))
6529              (gnus-summary-refer-article 
6530               (substring ref (match-beginning 0) (match-end 0))))))
6531     (setq n (1- n)))
6532   (or (zerop n) (message "No references in article or expired article."))
6533   (gnus-summary-position-cursor)
6534   n)
6535     
6536 (defun gnus-summary-refer-article (message-id)
6537   "Refer article specified by MESSAGE-ID.
6538 NOTE: This command only works with newsgroup that use NNTP."
6539   (interactive "sMessage-ID: ")
6540   (if (or (not (stringp message-id))
6541           (zerop (length message-id)))
6542       ()
6543     ;; Construct the correct Message-ID if necessary.
6544     ;; Suggested by tale@pawl.rpi.edu.
6545     (or (string-match "^<" message-id)
6546         (setq message-id (concat "<" message-id)))
6547     (or (string-match ">$" message-id)
6548         (setq message-id (concat message-id ">")))
6549     (let ((header (car (gnus-gethash message-id gnus-newsgroup-dependencies))))
6550       (if header
6551           (gnus-summary-goto-article (header-number header))
6552         (if (gnus-article-prepare message-id nil (gnus-read-header message-id))
6553             (progn
6554               (gnus-summary-insert-line 
6555                nil gnus-current-headers 0 nil gnus-read-mark nil nil 
6556                (header-subject gnus-current-headers))
6557               (forward-line -1)
6558               (gnus-summary-position-cursor)
6559               (gnus-summary-update-line)
6560               message-id)
6561           (message "No such references")
6562           nil)))))
6563
6564 (defun gnus-summary-enter-digest-group ()
6565   "Enter a digest group based on the current article."
6566   (interactive)
6567   (gnus-summary-select-article)
6568   (let ((name (format "%s/%d" 
6569                       (gnus-group-prefixed-name 
6570                        gnus-newsgroup-name (list 'nndigest "")) 
6571                       gnus-current-article))
6572         (buf (current-buffer)))
6573     (set-buffer gnus-group-buffer)
6574     (gnus-sethash 
6575      name 
6576      (list t nil (list name 3 nil nil 
6577                        (list 'nndigest gnus-article-buffer
6578                              (cons 'quit-buffer buf))))
6579      gnus-newsrc-hashtb)
6580     (gnus-group-read-group t nil name)))
6581   
6582 (defun gnus-summary-isearch-article ()
6583   "Do incremental search forward on current article."
6584   (interactive)
6585   (gnus-summary-select-article)
6586   (gnus-eval-in-buffer-window gnus-article-buffer
6587                               (isearch-forward)))
6588
6589 (defun gnus-summary-search-article-forward (regexp)
6590   "Search for an article containing REGEXP forward.
6591 gnus-select-article-hook is not called during the search."
6592   (interactive
6593    (list (read-string
6594           (concat "Search forward (regexp): "
6595                   (if gnus-last-search-regexp
6596                       (concat "(default " gnus-last-search-regexp ") "))))))
6597   (if (string-equal regexp "")
6598       (setq regexp (or gnus-last-search-regexp ""))
6599     (setq gnus-last-search-regexp regexp))
6600   (if (gnus-summary-search-article regexp nil)
6601       (gnus-eval-in-buffer-window gnus-article-buffer
6602         (recenter 0)
6603         ;;(sit-for 1)
6604         )
6605     (error "Search failed: \"%s\"" regexp)
6606     ))
6607
6608 (defun gnus-summary-search-article-backward (regexp)
6609   "Search for an article containing REGEXP backward.
6610 gnus-select-article-hook is not called during the search."
6611   (interactive
6612    (list (read-string
6613           (concat "Search backward (regexp): "
6614                   (if gnus-last-search-regexp
6615                       (concat "(default " gnus-last-search-regexp ") "))))))
6616   (if (string-equal regexp "")
6617       (setq regexp (or gnus-last-search-regexp ""))
6618     (setq gnus-last-search-regexp regexp))
6619   (if (gnus-summary-search-article regexp t)
6620       (gnus-eval-in-buffer-window gnus-article-buffer
6621         (recenter 0)
6622         ;;(sit-for 1)
6623         )
6624     (error "Search failed: \"%s\"" regexp)
6625     ))
6626
6627 (defun gnus-summary-search-article (regexp &optional backward)
6628   "Search for an article containing REGEXP.
6629 Optional argument BACKWARD means do search for backward.
6630 gnus-select-article-hook is not called during the search."
6631   (let ((gnus-select-article-hook nil)  ;Disable hook.
6632         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6633         (re-search
6634          (if backward
6635              (function re-search-backward) (function re-search-forward)))
6636         (found nil)
6637         (last nil))
6638     ;; Hidden thread subtrees must be searched for ,too.
6639     (gnus-summary-show-all-threads)
6640     (if (eobp) (forward-line -1))
6641     ;; First of all, search current article.
6642     ;; We don't want to read article again from NNTP server nor reset
6643     ;; current point.
6644     (gnus-summary-select-article)
6645     (message "Searching article: %d..." gnus-current-article)
6646     (setq last gnus-current-article)
6647     (gnus-eval-in-buffer-window gnus-article-buffer
6648       (save-restriction
6649         (widen)
6650         ;; Begin search from current point.
6651         (setq found (funcall re-search regexp nil t))))
6652     ;; Then search next articles.
6653     (while (and (not found)
6654                 (gnus-summary-display-article 
6655                  (gnus-summary-search-subject backward nil nil)))
6656       (message "Searching article: %d..." gnus-current-article)
6657       (gnus-eval-in-buffer-window gnus-article-buffer
6658         (save-restriction
6659           (widen)
6660           (goto-char (if backward (point-max) (point-min)))
6661           (setq found (funcall re-search regexp nil t)))
6662         ))
6663     (message "")
6664     ;; Adjust article pointer.
6665     (or (eq last gnus-current-article)
6666         (setq gnus-last-article last))
6667     ;; Return T if found such article.
6668     found
6669     ))
6670
6671 (defun gnus-summary-execute-command (field regexp command &optional backward)
6672   "If FIELD of article header matches REGEXP, execute a COMMAND string.
6673 If FIELD is an empty string (or nil), entire article body is searched for.
6674 If optional (prefix) argument BACKWARD is non-nil, do backward instead."
6675   (interactive
6676    (list (let ((completion-ignore-case t))
6677            (completing-read "Field name: "
6678                             '(("Number")("Subject")("From")
6679                               ("Lines")("Date")("Id")
6680                               ("Xref")("References"))
6681                             nil 'require-match))
6682          (read-string "Regexp: ")
6683          (read-key-sequence "Command: ")
6684          current-prefix-arg))
6685   ;; Hidden thread subtrees must be searched for ,too.
6686   (gnus-summary-show-all-threads)
6687   ;; We don't want to change current point nor window configuration.
6688   (save-excursion
6689     (save-window-excursion
6690       (message "Executing %s..." (key-description command))
6691       ;; We'd like to execute COMMAND interactively so as to give arguments.
6692       (gnus-execute field regexp
6693                     (` (lambda ()
6694                          (call-interactively '(, (key-binding command)))))
6695                     backward)
6696       (message "Executing %s... done" (key-description command)))))
6697
6698 (defun gnus-summary-beginning-of-article ()
6699   "Scroll the article back to the beginning."
6700   (interactive)
6701   (gnus-summary-select-article)
6702   (gnus-eval-in-buffer-window gnus-article-buffer
6703     (widen)
6704     (goto-char (point-min))
6705     (if gnus-break-pages
6706         (gnus-narrow-to-page))
6707     ))
6708
6709 (defun gnus-summary-end-of-article ()
6710   "Scroll to the end of the article."
6711   (interactive)
6712   (gnus-summary-select-article)
6713   (gnus-eval-in-buffer-window gnus-article-buffer
6714     (widen)
6715     (goto-char (point-max))
6716     (if gnus-break-pages
6717         (gnus-narrow-to-page))
6718     ))
6719
6720 (defun gnus-summary-show-article ()
6721   "Force re-fetching of the current article."
6722   (interactive)
6723   (gnus-summary-select-article gnus-have-all-headers t t))
6724
6725 (defun gnus-summary-toggle-header (arg)
6726   "Show the headers if they are hidden, or hide them if they are shown.
6727 If ARG is a positive number, show the entire header.
6728 If ARG is a negative number, hide the unwanted header lines."
6729   (interactive "P")
6730   (gnus-set-global-variables)
6731   (save-excursion
6732     (set-buffer gnus-article-buffer)
6733     (let ((buffer-read-only nil))
6734       (if (numberp arg) 
6735           (if (> arg 0) (remove-text-properties 1 (point-max) '(invisible t))
6736             (if (< arg 0) (run-hooks 'gnus-article-display-hook)))
6737         (if (text-property-any 1 (point-max) 'invisible t)
6738             (remove-text-properties 1 (point-max) '(invisible t))
6739           (run-hooks 'gnus-article-display-hook))))))
6740
6741 (defun gnus-summary-show-all-headers ()
6742   "Make all header lines visible."
6743   (interactive)
6744   (gnus-article-show-all-headers))
6745
6746 (defun gnus-summary-toggle-mime (arg)
6747   "Toggle MIME processing.
6748 If ARG is a positive number, turn MIME processing on."
6749   (interactive "P")
6750   (setq gnus-show-mime
6751         (if (null arg) (not gnus-show-mime)
6752           (> (prefix-numeric-value arg) 0)))
6753   (gnus-summary-select-article t 'force))
6754
6755 (defun gnus-summary-caesar-message (rotnum)
6756   "Caesar rotates all letters of current message by 13/47 places.
6757 With prefix arg, specifies the number of places to rotate each letter forward.
6758 Caesar rotates Japanese letters by 47 places in any case."
6759   (interactive "P")
6760   (gnus-summary-select-article)
6761   (let ((mail-header-separator "")) ; !!! Is this necessary?
6762     (gnus-overload-functions)
6763     (gnus-eval-in-buffer-window 
6764      gnus-article-buffer
6765      (save-restriction
6766        (widen)
6767        ;; We don't want to jump to the beginning of the message.
6768        ;; `save-excursion' does not do its job.
6769        (move-to-window-line 0)
6770        (let ((last (point)))
6771          (news-caesar-buffer-body rotnum)
6772          (goto-char last)
6773          (recenter 0))))))
6774
6775 (defun gnus-summary-stop-page-breaking ()
6776   "Stop page breaking in the current article."
6777   (interactive)
6778   (gnus-summary-select-article)
6779   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
6780
6781 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
6782
6783 (defun gnus-summary-move-article (n &optional to-newsgroup select-method)
6784   "Move the current article to a different newsgroup.
6785 If N is a positive number, move the N next articles.
6786 If N is a negative number, move the N previous articles.
6787 If N is nil and any articles have been marked with the process mark,
6788 move those articles instead.
6789 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
6790 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
6791 re-spool using this method.
6792 For this function to work, both the current newsgroup and the
6793 newsgroup that you want to move to have to support the `request-move'
6794 and `request-accept' functions. (Ie. mail newsgroups at present.)"
6795   (interactive "P")
6796   (gnus-set-global-variables)
6797   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
6798       (error "The current newsgroup does not support article moving"))
6799   (let ((articles (gnus-summary-work-articles n))
6800         art-group)
6801     (if (and (not to-newsgroup) (not select-method))
6802         (setq to-newsgroup
6803               (completing-read 
6804                (format "Where do you want to move %s? "
6805                        (if (> (length articles) 1)
6806                            (format "these %d articles" (length articles))
6807                          "this article"))
6808                gnus-active-hashtb nil t)))
6809     (or (gnus-check-backend-function 'request-accept-article 
6810                                      (or select-method to-newsgroup))
6811         (error "%s does not support article moving" to-newsgroup))
6812     (message "Moving to %s: %s..." (or select-method to-newsgroup) articles)
6813     (while articles
6814       (if (setq art-group
6815                 (gnus-request-move-article 
6816                  (car articles)
6817                  gnus-newsgroup-name 
6818                  (nth 1 (gnus-find-method-for-group gnus-newsgroup-name))
6819                  (list 'gnus-request-accept-article 
6820                        (if select-method
6821                            (quote select-method)
6822                          to-newsgroup))))
6823           (let* ((buffer-read-only nil)
6824                  (entry 
6825                   (or
6826                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
6827                    (gnus-gethash 
6828                     (gnus-group-prefixed-name 
6829                      (car art-group) 
6830                      (if select-method (list select-method "")
6831                        (gnus-find-method-for-group to-newsgroup)))
6832                     gnus-newsrc-hashtb)))
6833                  (info (nth 2 entry))
6834                  (article (car articles))
6835                  (marked (nth 3 info)))
6836             (gnus-summary-goto-subject article)
6837             (delete-region (progn (beginning-of-line) (point))
6838                            (progn (forward-line 1) (point)))
6839             (if (not (memq article gnus-newsgroup-unreads))
6840                 (setcar (cdr (cdr info))
6841                         (gnus-add-to-range (nth 2 info) 
6842                                            (list (cdr art-group)))))
6843             ;; !!! Here one should copy all the marks over to the new
6844             ;; newsgroup, but I couldn't be bothered. nth on that!
6845             )
6846         (message "Couldn't move article %s" (car articles)))
6847       (gnus-summary-remove-process-mark (car articles))
6848       (setq articles (cdr articles)))))
6849
6850 (defun gnus-summary-respool-article (n &optional respool-method)
6851   "Respool the current article.
6852 The article will be squeezed through the mail spooling process again,
6853 which means that it will be put in some mail newsgroup or other
6854 depending on `nnmail-split-methods'.
6855 If N is a positive number, respool the N next articles.
6856 If N is a negative number, respool the N previous articles.
6857 If N is nil and any articles have been marked with the process mark,
6858 respool those articles instead.
6859 For this function to work, both the current newsgroup and the
6860 newsgroup that you want to move to have to support the `request-move'
6861 and `request-accept' functions. (Ie. mail newsgroups at present.)"
6862   (interactive "P")
6863   (gnus-set-global-variables)
6864   (or respool-method
6865       (setq respool-method
6866             (completing-read
6867              "What method do you want to use when respooling? "
6868              (gnus-methods-using 'respool) nil t)))
6869   (gnus-summary-move-article n nil (intern respool-method)))
6870
6871 ;; Summary score commands.
6872
6873 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
6874
6875 (defun gnus-summary-raise-score (n)
6876   "Raise the score of the current article by N."
6877   (interactive "p")
6878   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
6879
6880 (defun gnus-summary-lower-score (n)
6881   "Lower the score of the current article by N."
6882   (interactive "p")
6883   (gnus-summary-raise-score (- n)))
6884
6885 (defun gnus-summary-set-score (n)
6886   "Set the score of the current article to N."
6887   (interactive "p")
6888   ;; Skip dummy header line.
6889   (save-excursion
6890     (if (= (gnus-summary-article-mark) ?Z) (forward-line 1))
6891     (let ((buffer-read-only nil))
6892       ;; Set score.
6893       (gnus-summary-update-mark
6894        (if (= n gnus-summary-default-score) ? 
6895          (if (< n gnus-summary-default-score) 
6896              gnus-score-below-mark gnus-score-over-mark)) 'score))
6897     (let* ((article (gnus-summary-article-number))
6898            (score (assq article gnus-newsgroup-scored)))
6899       (if score (setcdr score n)
6900         (setq gnus-newsgroup-scored 
6901               (cons (cons article n) gnus-newsgroup-scored))))
6902     (gnus-summary-update-line)))
6903
6904 (defmacro gnus-raise (field expression level)
6905   (` (gnus-kill (, field) (, expression)
6906                 (function (gnus-summary-raise-score (, level))) t)))
6907
6908 (defmacro gnus-lower (field expression level)
6909   (` (gnus-kill (, field) (, expression)
6910                 (function (gnus-summary-raise-score (- (, level)))) t)))
6911
6912 ;; Summary marking commands.
6913
6914 (defun gnus-summary-raise-same-subject-and-select (score)
6915   "Raise articles which has the same subject with SCORE and select the next."
6916   (interactive "p")
6917   (let ((subject (gnus-summary-subject-string)))
6918     (gnus-summary-raise-score score)
6919     (while (gnus-summary-search-subject nil nil subject)
6920       (gnus-summary-raise-score score))
6921     (gnus-summary-next-article t)))
6922
6923 (defun gnus-summary-raise-same-subject (score)
6924   "Raise articles which has the same subject with SCORE."
6925   (interactive "p")
6926   (let ((subject (gnus-summary-subject-string)))
6927     (gnus-summary-raise-score score)
6928     (while (gnus-summary-search-subject nil nil subject)
6929       (gnus-summary-raise-score score))
6930     (gnus-summary-next-subject 1 t)))
6931
6932 (defun gnus-summary-raise-thread (score)
6933   "Raise articles under current thread with SCORE."
6934   (interactive "p")
6935   (let (e)
6936     (save-excursion
6937       (let ((level (gnus-summary-thread-level)))
6938         (gnus-summary-raise-score score)
6939         (while (and (zerop (gnus-summary-next-subject 1))
6940                     (> (gnus-summary-thread-level) level))
6941           (gnus-summary-raise-score score))
6942         (setq e (point))))
6943     (or (zerop (gnus-summary-next-subject 1 t))
6944         (goto-char e)))
6945   (gnus-summary-position-cursor)
6946   (gnus-set-mode-line 'summary))
6947
6948 (defun gnus-summary-lower-same-subject-and-select (score)
6949   "Raise articles which has the same subject with SCORE and select the next."
6950   (interactive "p")
6951   (gnus-summary-raise-same-subject-and-select (- score)))
6952
6953 (defun gnus-summary-lower-same-subject (score)
6954   "Raise articles which has the same subject with SCORE."
6955   (interactive "p")
6956   (gnus-summary-raise-same-subject (- score)))
6957
6958 (defun gnus-summary-lower-thread (score)
6959   "Raise articles under current thread with SCORE."
6960   (interactive "p")
6961   (gnus-summary-raise-thread (- score)))
6962
6963 (defun gnus-summary-kill-same-subject-and-select (unmark)
6964   "Mark articles which has the same subject as read, and then select the next.
6965 If UNMARK is positive, remove any kind of mark.
6966 If UNMARK is negative, tick articles."
6967   (interactive "P")
6968   (if unmark
6969       (setq unmark (prefix-numeric-value unmark)))
6970   (let ((count
6971          (gnus-summary-mark-same-subject
6972           (gnus-summary-subject-string) unmark)))
6973     ;; Select next unread article. If auto-select-same mode, should
6974     ;; select the first unread article.
6975     (gnus-summary-next-article t (and gnus-auto-select-same
6976                                       (gnus-summary-subject-string)))
6977     (message "%d articles are marked as %s"
6978              count (if unmark "unread" "read"))
6979     ))
6980
6981 (defun gnus-summary-kill-same-subject (unmark)
6982   "Mark articles which has the same subject as read. 
6983 If UNMARK is positive, remove any kind of mark.
6984 If UNMARK is negative, tick articles."
6985   (interactive "P")
6986   (if unmark
6987       (setq unmark (prefix-numeric-value unmark)))
6988   (let ((count
6989          (gnus-summary-mark-same-subject
6990           (gnus-summary-subject-string) unmark)))
6991     ;; If marked as read, go to next unread subject.
6992     (if (null unmark)
6993         ;; Go to next unread subject.
6994         (gnus-summary-next-subject 1 t))
6995     (message "%d articles are marked as %s"
6996              count (if unmark "unread" "read"))
6997     ))
6998
6999 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7000   "Mark articles with same SUBJECT as read, and return marked number.
7001 If optional argument UNMARK is positive, remove any kinds of marks.
7002 If optional argument UNMARK is negative, mark articles as unread instead."
7003   (let ((count 1))
7004     (save-excursion
7005       (cond ((null unmark)
7006              (gnus-summary-mark-as-read nil gnus-killed-mark))
7007             ((> unmark 0)
7008              (gnus-summary-tick-article nil t))
7009             (t
7010              (gnus-summary-tick-article)))
7011       (while (and subject
7012                   (gnus-summary-search-forward nil subject))
7013         (cond ((null unmark)
7014                (gnus-summary-mark-as-read nil gnus-killed-mark))
7015               ((> unmark 0)
7016                (gnus-summary-tick-article nil t))
7017               (t
7018                (gnus-summary-tick-article)))
7019         (setq count (1+ count))
7020         ))
7021     ;; Hide killed thread subtrees.  Does not work properly always.
7022     ;;(and (null unmark)
7023     ;;     gnus-thread-hide-killed
7024     ;;     (gnus-summary-hide-thread))
7025     ;; Return number of articles marked as read.
7026     count
7027     ))
7028
7029 (defun gnus-summary-mark-as-processable (n &optional unmark)
7030   "Set the process mark on the next N articles.
7031 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7032 the process mark instead.  The difference between N and the actual
7033 number of articles marked is returned."
7034   (interactive "p")
7035   (let ((backward (< n 0))
7036         (n (abs n)))
7037   (while (and 
7038           (> n 0)
7039           (if unmark
7040               (gnus-summary-remove-process-mark (gnus-summary-article-number))
7041             (gnus-summary-set-process-mark (gnus-summary-article-number)))
7042           (zerop (gnus-summary-next-subject (if backward -1 1))))
7043     (setq n (1- n)))
7044   (if (/= 0 n) (message "No more articles"))
7045   n))
7046
7047 (defun gnus-summary-unmark-as-processable (n)
7048   "Remove the process mark from the next N articles.
7049 If N is negative, mark backward instead.  The difference between N and
7050 the actual number of articles marked is returned."
7051   (interactive "p")
7052   (gnus-summary-mark-as-processable n t))
7053
7054 (defun gnus-summary-unmark-all-processable ()
7055   "Remove the process mark from all articles."
7056   (interactive)
7057   (save-excursion
7058     (while gnus-newsgroup-processable
7059       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7060   (gnus-summary-position-cursor))
7061
7062 (defun gnus-summary-mark-as-expirable (n)
7063   "Mark N articles forward as expirable.
7064 If N is negative, mark backward instead. The difference between N and
7065 the actual number of articles marked is returned."
7066   (interactive "p")
7067   (gnus-summary-mark-forward n gnus-expirable-mark))
7068
7069 (defun gnus-summary-expire-articles ()
7070   "Expire all articles that are marked as expirable in the current group."
7071   (interactive)
7072   (if (and gnus-newsgroup-expirable
7073            (gnus-check-backend-function 
7074             'request-expire-articles gnus-newsgroup-name))
7075       (let ((expirable gnus-newsgroup-expirable))
7076         ;; The list of articles that weren't expired is returned.
7077         (setq gnus-newsgroup-expirable 
7078               (gnus-request-expire-articles gnus-newsgroup-expirable
7079                                             gnus-newsgroup-name))
7080         ;; We go through the old list of expirable, and mark all
7081         ;; really expired articles as non-existant.
7082         (while expirable
7083           (or (memq (car expirable) gnus-newsgroup-expirable)
7084               (gnus-summary-mark-as-read (car expirable) "%"))
7085           (setq expirable (cdr expirable))))))
7086
7087 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7088 (defun gnus-summary-delete-article (n)
7089   "Delete the N next (mail) articles.
7090 This command actually deletes articles. This is not a marking
7091 command. The article will disappear forever from you life, never to
7092 return. 
7093 If N is negative, delete backwards.
7094 If N is nil and articles have been marked with the process mark,
7095 delete these instead."
7096   (interactive "P")
7097   (or (gnus-check-backend-function 'request-expire-articles 
7098                                    gnus-newsgroup-name)
7099       (error "The current newsgroup does not support article deletion."))
7100   ;; Compute the list of articles to delete.
7101   (let (articles)
7102     (if (and n (numberp n))
7103         (let ((backward (< n 0))
7104               (n (abs n)))
7105           (save-excursion
7106             (while (and (> n 0)
7107                         (setq articles (cons (gnus-summary-article-number) 
7108                                              articles))
7109                         (gnus-summary-search-forward nil nil backward))
7110               (setq n (1- n))))
7111           (setq articles (sort articles (function <))))
7112       (setq articles (or (setq gnus-newsgroup-processable
7113                                (sort gnus-newsgroup-processable (function <)))
7114                          (list (gnus-summary-article-number)))))
7115     (if (and gnus-novice-user
7116              (not (gnus-y-or-n-p 
7117                    (format "Do you really want to delete %s forever? "
7118                            (if (> (length articles) 1) "these articles"
7119                              "this article")))))
7120         ()
7121       ;; Delete the articles.
7122       (setq gnus-newsgroup-expirable 
7123             (gnus-request-expire-articles 
7124              articles gnus-newsgroup-name 'force))
7125       (while articles
7126         (gnus-summary-mark-as-read (car articles) gnus-canceled-mark)
7127         (setq articles (cdr articles))))))
7128
7129 (defun gnus-summary-edit-article ()
7130   "Enter into a buffer and edit the current article.
7131 This will have permanent effect only in mail groups."
7132   (interactive)
7133   (or (gnus-check-backend-function 
7134        'request-replace-article gnus-newsgroup-name)
7135       (error "The current newsgroup does not support article editing."))
7136   (gnus-summary-select-article t)
7137   (other-window 1)
7138   (message "C-c C-c to end edits")
7139   (setq buffer-read-only nil)
7140   (text-mode)
7141   (use-local-map (copy-keymap (current-local-map)))
7142   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
7143   (goto-char 1)
7144   (search-forward "\n\n" nil t))
7145
7146 (defun gnus-summary-edit-article-done ()
7147   "Make edits to the current article permanent."
7148   (interactive)
7149   (if (not (gnus-request-replace-article 
7150             (cdr gnus-article-current) (car gnus-article-current) 
7151             (current-buffer)))
7152       (error "Couldn't replace article.")
7153     (gnus-article-mode)
7154     (use-local-map gnus-article-mode-map)
7155     (setq buffer-read-only t)
7156     (pop-to-buffer gnus-summary-buffer)))
7157
7158 (defun gnus-summary-mark-article-as-replied (article)
7159   "Mark ARTICLE replied and update the summary line."
7160   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
7161   (let ((buffer-read-only nil))
7162     (if (gnus-summary-goto-subject article)
7163         (progn
7164           (gnus-summary-update-mark gnus-replied-mark 'replied)
7165           t))))
7166
7167 (defun gnus-summary-set-bookmark (article)
7168   "Set a bookmark in current article."
7169   (interactive (list (gnus-summary-article-number)))
7170   (if (or (not (get-buffer gnus-article-buffer))
7171           (not gnus-current-article)
7172           (not gnus-article-current)
7173           (not (equal gnus-newsgroup-name (car gnus-article-current))))
7174       (error "No current article selected"))
7175   ;; Remove old bookmark, if one exists.
7176   (let ((old (assq article gnus-newsgroup-bookmarks)))
7177     (if old (setq gnus-newsgroup-bookmarks 
7178                   (delq old gnus-newsgroup-bookmarks))))
7179   ;; Set the new bookmark, which is on the form 
7180   ;; (article-number . line-number-in-body).
7181   (setq gnus-newsgroup-bookmarks 
7182         (cons 
7183          (cons article 
7184                (save-excursion
7185                  (set-buffer gnus-article-buffer)
7186                  (count-lines
7187                   (min (point)
7188                        (save-excursion
7189                          (goto-char 1)
7190                          (search-forward "\n\n" nil t)
7191                          (point)))
7192                   (point))))
7193          gnus-newsgroup-bookmarks))
7194   (message "A bookmark has been added to the current article."))
7195
7196 (defun gnus-summary-remove-bookmark (article)
7197   "Remove the bookmark from the current article."
7198   (interactive (list (gnus-summary-article-number)))
7199   ;; Remove old bookmark, if one exists.
7200   (let ((old (assq article gnus-newsgroup-bookmarks)))
7201     (if old 
7202         (progn
7203           (setq gnus-newsgroup-bookmarks 
7204                 (delq old gnus-newsgroup-bookmarks))
7205           (message "Removed bookmark."))
7206       (message "No bookmark in current article."))))
7207
7208 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7209 (defun gnus-summary-mark-as-dormant (n)
7210   "Mark N articles forward as dormant.
7211 If N is negative, mark backward instead.  The difference between N and
7212 the actual number of articles marked is returned."
7213   (interactive "p")
7214   (gnus-summary-mark-forward n gnus-dormant-mark))
7215
7216 (defun gnus-summary-set-process-mark (article)
7217   "Set the process mark on ARTICLE and update the summary line."
7218   (setq gnus-newsgroup-processable (cons article gnus-newsgroup-processable))
7219   (let ((buffer-read-only nil))
7220     (if (gnus-summary-goto-subject article)
7221         (progn
7222           (if (= (gnus-summary-article-mark) ?Z) (forward-line 1))
7223           (gnus-summary-update-mark gnus-process-mark 'replied)
7224           t))))
7225
7226 (defun gnus-summary-remove-process-mark (article)
7227   "Remove the process mark from ARTICLE and update the summary line."
7228   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7229   (let ((buffer-read-only nil))
7230     (if (gnus-summary-goto-subject article)
7231         (progn
7232           (and (= (gnus-summary-article-mark) ?Z) (forward-line 1))
7233           (gnus-summary-update-mark ?  'replied)
7234           (if (memq article gnus-newsgroup-replied) 
7235               (gnus-summary-update-mark gnus-replied-mark 'replied))
7236           t))))
7237
7238 (defun gnus-summary-mark-forward (n &optional mark)
7239   "Mark N articles as read forwards.
7240 If N is negative, mark backwards instead.
7241 Mark with MARK. If MARK is ? , ?! or ??, articles will be
7242 marked as unread. 
7243 The difference between N and the actual number of articles marked is
7244 returned."
7245   (interactive "p")
7246   (gnus-set-global-variables)
7247   (let ((backward (< n 0))
7248         (n (abs n))
7249         (mark (or mark gnus-dread-mark)))
7250   (while (and (> n 0)
7251               (gnus-summary-mark-article nil mark)
7252               (zerop (gnus-summary-next-subject (if backward -1 1))))
7253     (setq n (1- n)))
7254   (if (/= 0 n) (message "No more %sarticles" (if mark "" "unread ")))
7255   (gnus-set-mode-line 'summary)
7256   n))
7257
7258 (defun gnus-summary-mark-article (&optional article mark)
7259   "Mark ARTICLE with MARK.
7260 MARK can be any character.
7261 Five MARK strings are reserved: ?  (unread), 
7262 ?! (ticked), ?? (dormant), ?D (read), ?E (expirable).
7263 If MARK is nil, then the default character ?D is used.
7264 If ARTICLE is nil, then the article on the current line will be
7265 marked." 
7266   ;; If no mark is given, then we check auto-expiring.
7267   (and (or (not mark)
7268            (and (numberp mark) (or (= mark gnus-killed-mark)
7269                                    (= mark gnus-read-mark))))
7270        (and gnus-newsgroup-auto-expire (setq mark gnus-expirable-mark)))
7271   (let* ((buffer-read-only nil)
7272          (mark (or (and (stringp mark) (aref mark 0)) mark gnus-dread-mark))
7273          (article (or article (gnus-summary-article-number))))
7274     (if (or (= mark gnus-unread-mark) 
7275             (= mark gnus-ticked-mark) 
7276             (= mark gnus-dormant-mark))
7277         (gnus-mark-article-as-unread article mark)
7278       (gnus-mark-article-as-read article mark))
7279     (if (gnus-summary-goto-subject article)
7280         (progn
7281           (gnus-summary-show-thread)
7282           (beginning-of-line)
7283           (if (= (gnus-summary-article-mark) ?Z) (forward-line 1))
7284           ;; Fix the mark.
7285           (gnus-summary-update-mark mark 'unread)
7286           t))))
7287
7288 (defun gnus-summary-update-mark (mark type)
7289   (beginning-of-line)
7290   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
7291         plist)
7292     (if (not forward)
7293         ()
7294       (forward-char forward)
7295       (setq plist (text-properties-at (point)))
7296       (delete-char 1)
7297       (and (memq 'gnus-mark plist) (setcar (cdr (memq 'gnus-mark plist)) mark))
7298       (insert mark)
7299       (and plist (add-text-properties (1- (point)) (point) plist)))))
7300   
7301 (defun gnus-mark-article-as-read (article &optional mark)
7302   "Enter ARTICLE in the pertinent lists and remove it from others."
7303   ;; Make the article expirable.
7304   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-dread-mark)))
7305     (if (= mark gnus-expirable-mark)
7306         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
7307       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
7308     ;; Remove from unread and marked lists.
7309     (setq gnus-newsgroup-unreads
7310           (delq article gnus-newsgroup-unreads))
7311     (setq gnus-newsgroup-marked
7312           (delq article gnus-newsgroup-marked))
7313     (setq gnus-newsgroup-dormant
7314           (delq article gnus-newsgroup-dormant))))
7315
7316 (defun gnus-mark-article-as-unread (article &optional mark)
7317   "Enter ARTICLE in the pertinent lists and remove it from others."
7318   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-ticked-mark)))
7319     ;; Add to unread list.
7320     (or (memq article gnus-newsgroup-unreads)
7321         (setq gnus-newsgroup-unreads
7322               (cons article gnus-newsgroup-unreads)))
7323     ;; If CLEAR-MARK is non-nil, the article must be removed from marked
7324     ;; list.  Otherwise, it must be added to the list.
7325     (setq gnus-newsgroup-marked
7326           (delq article gnus-newsgroup-marked))
7327     (setq gnus-newsgroup-dormant
7328           (delq article gnus-newsgroup-dormant))
7329     (setq gnus-newsgroup-expirable 
7330           (delq article gnus-newsgroup-expirable))
7331     (if (= mark gnus-ticked-mark)
7332         (setq gnus-newsgroup-marked 
7333               (cons article gnus-newsgroup-marked)))
7334     (if (= mark gnus-dormant-mark)
7335         (setq gnus-newsgroup-dormant 
7336               (cons article gnus-newsgroup-dormant)))))
7337
7338 (defalias 'gnus-summary-mark-as-unread-forward 
7339   'gnus-summary-tick-article-forward)
7340 (make-obsolete 'gnus-summary-mark-as-unread-forward 
7341                'gnus-summary-tick-article--forward)
7342 (defun gnus-summary-tick-article-forward (n)
7343   "Tick N articles forwards.
7344 If N is negative, tick backwards instead.
7345 The difference between N and the number of articles ticked is returned."
7346   (interactive "p")
7347   (gnus-summary-mark-forward n gnus-ticked-mark))
7348
7349 (defalias 'gnus-summary-mark-as-unread-backward 'gnus-summary-tick-article-backward)
7350 (make-obsolete 'gnus-summary-mark-as-unread-backward 'gnus-summary-tick-article-backward)
7351 (defun gnus-summary-tick-article-backward (n)
7352   "Tick N articles backwards.
7353 The difference between N and the number of articles ticked is returned."
7354   (interactive "p")
7355   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
7356
7357 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
7358 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
7359 (defun gnus-summary-tick-article (&optional article clear-mark)
7360   "Mark current article as unread.
7361 Optional 1st argument ARTICLE specifies article number to be marked as unread.
7362 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
7363   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
7364                                        gnus-ticked-mark)))
7365
7366 (defun gnus-summary-mark-as-read-forward (n)
7367   "Mark N articles as read forwards.
7368 If N is negative, mark backwards instead.
7369 The difference between N and the actual number of articles marked is
7370 returned."
7371   (interactive "p")
7372   (gnus-summary-mark-forward n))
7373
7374 (defun gnus-summary-mark-as-read-backward (n)
7375   "Mark the N articles as read backwards.
7376 The difference between N and the actual number of articles marked is
7377 returned."
7378   (interactive "p")
7379   (gnus-summary-mark-forward (- n)))
7380
7381 (defun gnus-summary-mark-as-read (&optional article mark)
7382   "Mark current article as read.
7383 ARTICLE specifies the article to be marked as read.
7384 MARK specifies a string to be inserted at the beginning of the line.
7385 Any kind of string (length 1) except for a space and `-' is ok."
7386   (gnus-summary-mark-article article mark))
7387
7388 (defun gnus-summary-clear-mark-forward (n)
7389   "Clear marks from N articles forward.
7390 If N is negative, clear backward instead.
7391 The difference between N and the number of marks cleared is returned."
7392   (interactive "p")
7393   (gnus-summary-mark-forward n gnus-unread-mark))
7394
7395 (defun gnus-summary-clear-mark-backward (n)
7396   "Clear marks from N articles backward.
7397 The difference between N and the number of marks cleared is returned."
7398   (interactive "p")
7399   (gnus-summary-mark-forward (- n) gnus-unread-mark))
7400
7401 ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
7402 (defalias 'gnus-summary-delete-marked-as-read 
7403   'gnus-summary-remove-lines-marked-as-read)
7404 (make-obsolete 'gnus-summary-delete-marked-as-read 
7405                'gnus-summary-remove-lines-marked-as-read)
7406 (defun gnus-summary-remove-lines-marked-as-read ()
7407   "Remove lines that are marked as read."
7408   (interactive)
7409   (gnus-summary-remove-lines-marked-with 
7410    (concat (mapconcat
7411             (lambda (char) (char-to-string (symbol-value char)))
7412             '(gnus-dread-mark gnus-read-mark
7413               gnus-killed-mark gnus-kill-file-mark
7414               gnus-low-score-mark gnus-expirable-mark)
7415             ""))))
7416
7417 (defalias 'gnus-summary-delete-marked-with 
7418   'gnus-summary-remove-lines-marked-with)
7419 (make-obsolete 'gnus-summary-delete-marked-with 
7420                'gnus-summary-remove-lines-marked-with)
7421 ;; Rewrite by Daniel Quinlan <quinlan@best.com>.
7422 (defun gnus-summary-remove-lines-marked-with (marks)
7423   "Remove lines that are marked with MARKS (e.g. \"DK\")."
7424   (interactive "sMarks: ")
7425   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
7426   (save-excursion
7427     (set-buffer gnus-summary-buffer)
7428     (let ((buffer-read-only nil)
7429           (marks (concat "^[" marks "]"))
7430           beg)
7431       (goto-char (point-min))
7432       (while (search-forward-regexp marks (point-max) t)
7433         (progn
7434           (move-to-column 0)
7435           (setq beg (point))
7436           (forward-line 1)
7437           ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>
7438           (append-to-buffer gnus-newsgroup-expunged-buffer beg (point))
7439           (delete-region beg (point)))))
7440     (or (zerop (buffer-size))
7441         (if (eobp)
7442             (gnus-summary-prev-subject 1)
7443           (gnus-summary-position-cursor)))))
7444
7445 (defun gnus-summary-expunge-below (score)
7446   "Remove articles with score less than SCORE."
7447   (interactive "P")
7448   (setq score (if score
7449                   (prefix-numeric-value score)
7450                 gnus-summary-default-score))
7451   (save-excursion
7452     (set-buffer gnus-summary-buffer)
7453     (goto-char (point-min))
7454     (let ((buffer-read-only nil)
7455           beg)
7456       (while (not (eobp))
7457         (if (< (gnus-summary-article-score) score)
7458             (progn
7459               (setq beg (point))
7460               (forward-line 1)
7461               (append-to-buffer gnus-newsgroup-expunged-buffer beg (point))
7462               (delete-region beg (point)))
7463           (forward-line 1)))
7464       ;; Adjust point.
7465       (or (zerop (buffer-size))
7466           (if (eobp)
7467               (gnus-summary-prev-subject 1)
7468             (gnus-summary-position-cursor))))))
7469
7470 (defun gnus-summary-mark-below (score mark)
7471   "Mark articles with score less than SCORE with MARK."
7472   (interactive "P\ncMark: ")
7473   (setq score (if score
7474                   (prefix-numeric-value score)
7475                 gnus-summary-default-score))
7476   (save-excursion
7477     (set-buffer gnus-summary-buffer)
7478     (goto-char (point-min))
7479     (while (not (eobp))
7480       (if (< (gnus-summary-article-score) score)
7481           (progn
7482             (gnus-summary-mark-article nil (char-to-string mark))
7483             (forward-line 1))
7484         (forward-line 1)))))
7485
7486 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
7487 (defun gnus-summary-set-mark-below (score)
7488   "Automatically mark articles with score below SCORE as read."
7489   (interactive "P")
7490   (setq score (if score
7491                   (prefix-numeric-value score)
7492                 gnus-summary-default-score))
7493   (setq gnus-summary-mark-below score)
7494   (gnus-summary-update-lines))
7495
7496 (defun gnus-summary-kill-below (score)
7497   "Mark articles with score below SCORE as read."
7498   (interactive "P")
7499   (gnus-summary-mark-below score gnus-killed-mark))
7500
7501 (defun gnus-summary-clear-above (score)
7502   "Clear all marks from articles with score above SCORE."
7503   (interactive "P")
7504   (gnus-summary-mark-above score gnus-unread-mark))
7505
7506 (defun gnus-summary-tick-above (score)
7507   "Tick all articles with score above SCORE."
7508   (interactive "P")
7509   (gnus-summary-mark-above score gnus-ticked-mark))
7510
7511 (defun gnus-summary-mark-above (score mark)
7512   "Mark articles with score less than SCORE with MARK."
7513   (interactive "P\ncMark: ")
7514   (setq score (if score
7515                   (prefix-numeric-value score)
7516                 gnus-summary-default-score))
7517   (save-excursion
7518     (set-buffer gnus-summary-buffer)
7519     (goto-char (point-min))
7520     (while (not (eobp))
7521       (if (> (gnus-summary-article-score) score)
7522           (progn
7523             (gnus-summary-mark-article nil mark)
7524             (forward-line 1))
7525         (forward-line 1)))))
7526
7527 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
7528 (defun gnus-summary-show-all-expunged ()
7529   "Show all previously expunge articles."
7530   (interactive)
7531   (let ((buffer-read-only nil))
7532     (save-excursion
7533       (if (and gnus-newsgroup-expunged-buffer
7534                (progn
7535                  (set-buffer gnus-newsgroup-expunged-buffer)
7536                  (not (zerop (buffer-size)))))
7537           (progn
7538             (append-to-buffer gnus-summary-buffer (point-min) (point-max))
7539             (erase-buffer))
7540         (error "No lines expunged")))))
7541
7542 (defun gnus-summary-show-all-dormant ()
7543   "Display all the hidden articles that are marked as dormant."
7544   (interactive)
7545   (let ((int gnus-newsgroup-dormant-subjects)
7546         (buffer-read-only nil))
7547     (if (not int)
7548         (error "No dormant articles hidden."))
7549     (goto-char (point-min))
7550     (save-excursion
7551       (while int
7552         (insert (cdr (car int)))
7553         (setq int (cdr int))))
7554     (gnus-summary-position-cursor)
7555     (setq gnus-newsgroup-dormant-subjects nil)))
7556
7557 (defun gnus-summary-catchup (all &optional quietly to-here)
7558   "Mark all articles not marked as unread in this newsgroup as read.
7559 If prefix argument ALL is non-nil, all articles are marked as read.
7560 If QUIETLY is non-nil, no questions will be asked.
7561 If TO-HERE is non-nil, it should be a point in the buffer. All
7562 articles before this point will be marked as read.
7563 The number of articles marked as read is returned."
7564   (interactive "P")
7565   (if (or quietly
7566           (not gnus-interactive-catchup) ;Without confirmation?
7567           gnus-expert-user
7568           (gnus-y-or-n-p
7569            (if all
7570                "Mark absolutely all articles as read? "
7571              "Mark all unread articles as read? ")))
7572       (let ((unreads (length gnus-newsgroup-unreads)))
7573         (if (gnus-summary-first-subject (not all))
7574             (while (and (gnus-summary-mark-as-read nil gnus-catchup-mark)
7575                         (if to-here (< (point) to-here) t)
7576                         (gnus-summary-search-subject nil (not all)))))
7577         (- unreads (length gnus-newsgroup-unreads))))
7578   (gnus-summary-position-cursor))
7579
7580 (defun gnus-summary-catchup-to-here (&optional all)
7581   "Mark all unticked articles before the current one as read.
7582 If ALL is non-nil, also mark ticked and dormant articles as read."
7583   (interactive)
7584   (beginning-of-line)
7585   (gnus-summary-catchup all nil (point))
7586   (gnus-summary-position-cursor))
7587
7588 (defun gnus-summary-catchup-all (&optional quietly)
7589   "Mark all articles in this newsgroup as read."
7590   (interactive)
7591   (gnus-summary-catchup t quietly))
7592
7593 (defun gnus-summary-catchup-and-exit (all &optional quietly)
7594   "Mark all articles not marked as unread in this newsgroup as read, then exit.
7595 If prefix argument ALL is non-nil, all articles are marked as read."
7596   (interactive "P")
7597   (gnus-summary-catchup all quietly)
7598   ;; Select next newsgroup or exit.
7599   (if (eq gnus-auto-select-next 'quietly)
7600       (gnus-summary-next-group nil)
7601     (gnus-summary-exit)))
7602
7603 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
7604   "Mark all articles in this newsgroup as read, and then exit."
7605   (interactive)
7606   (gnus-summary-catchup-and-exit t quietly))
7607
7608 ;; Thread-based commands.
7609
7610 (defun gnus-summary-toggle-threads (arg)
7611   "Toggle showing conversation threads.
7612 If ARG is positive number, turn showing conversation threads on."
7613   (interactive "P")
7614   (let ((current (gnus-summary-article-number)))
7615     (setq gnus-show-threads
7616           (if (null arg) (not gnus-show-threads)
7617             (> (prefix-numeric-value arg) 0)))
7618     (gnus-summary-prepare)
7619     (gnus-summary-goto-subject current)))
7620
7621 (defun gnus-summary-show-all-threads ()
7622   "Show all threads."
7623   (interactive)
7624   (save-excursion
7625     (let ((buffer-read-only nil))
7626       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
7627   (gnus-summary-position-cursor))
7628
7629 (defun gnus-summary-show-thread ()
7630   "Show thread subtrees.
7631 Returns nil if no thread was there to be shown."
7632   (interactive)
7633   (prog1
7634       (save-excursion
7635         (let ((buffer-read-only nil)
7636               (beg (progn (beginning-of-line) (point)))
7637               (end (save-excursion (end-of-line) (point))))
7638           (prog1
7639               ;; Any hidden lines here?
7640               (search-forward "\r" end t)
7641             (subst-char-in-region beg end ?\^M ?\n t))))
7642     (gnus-summary-position-cursor)))
7643
7644 (defun gnus-summary-hide-all-threads ()
7645   "Hide all thread subtrees."
7646   (interactive)
7647   (save-excursion
7648     (goto-char (point-min))
7649     (gnus-summary-hide-thread)
7650     (while (and (not (eobp)) (zerop (forward-line 1)))
7651       (gnus-summary-hide-thread))
7652     )
7653   (gnus-summary-position-cursor))
7654
7655 (defun gnus-summary-hide-thread ()
7656   "Hide thread subtrees.
7657 Returns nil if no threads were there to be hidden."
7658   (interactive)
7659   (let ((buffer-read-only nil)
7660         (start (point))
7661         (level (gnus-summary-thread-level))
7662         (end (point)))
7663     ;; Go forward until either the buffer ends or the subthread
7664     ;; ends. 
7665     (if (eobp)
7666         ()
7667       (while (and (zerop (forward-line 1))
7668                   (> (gnus-summary-thread-level) level))
7669         (setq end (point)))
7670       (prog1
7671           (save-excursion
7672             (goto-char end)
7673             (search-backward "\n" start t))
7674         (subst-char-in-region start end ?\n ?\^M t)
7675         (forward-line -1)))))
7676
7677 (defun gnus-summary-go-to-next-thread (&optional previous)
7678   "Go to the same level (or less) next thread.
7679 If PREVIOUS is non-nil, go to previous thread instead.
7680 Return the article number moved to, or nil if moving was impossible."
7681   (let ((level (gnus-summary-thread-level))
7682         (article (gnus-summary-article-number)))
7683     (if previous 
7684         (while (and (zerop (gnus-summary-prev-subject 1))
7685                     (> (gnus-summary-thread-level) level)))
7686       (while (and (zerop (gnus-summary-next-subject 1))
7687                   (> (gnus-summary-thread-level) level))))
7688     (let ((oart (gnus-summary-article-number)))
7689       (and (/= oart article) oart))))
7690
7691 (defun gnus-summary-next-thread (n)
7692   "Go to the same level next N'th thread.
7693 If N is negative, search backward instead.
7694 Returns the difference between N and the number of skips actually
7695 done."
7696   (interactive "p")
7697   (let ((backward (< n 0))
7698         (n (abs n)))
7699   (while (and (> n 0)
7700               (gnus-summary-go-to-next-thread backward))
7701     (setq n (1- n)))
7702   (gnus-summary-position-cursor)
7703   (if (/= 0 n) (message "No more threads" ))
7704   n))
7705
7706 (defun gnus-summary-prev-thread (n)
7707   "Go to the same level previous N'th thread.
7708 Returns the difference between N and the number of skips actually
7709 done."
7710   (interactive "p")
7711   (gnus-summary-next-thread (- n)))
7712
7713 (defun gnus-summary-go-down-thread (&optional same)
7714   "Go down one level in the current thread.
7715 If SAME is non-nil, also move to articles of the same level."
7716   (let ((level (gnus-summary-thread-level))
7717         (start (point)))
7718     (if (and (zerop (forward-line 1))
7719              (> (gnus-summary-thread-level) level))
7720         t
7721       (goto-char start)
7722       nil)))
7723
7724 (defun gnus-summary-go-up-thread ()
7725   "Go up one level in the current thread."
7726   (let ((level (gnus-summary-thread-level))
7727         (start (point)))
7728     (while (and (zerop (forward-line -1))
7729                 (>= (gnus-summary-thread-level) level)))
7730     (if (>= (gnus-summary-thread-level) level)
7731         (progn
7732           (goto-char start)
7733           nil)
7734       t)))
7735
7736 (defun gnus-summary-down-thread (n)
7737   "Go down thread N steps.
7738 If N is negative, go up instead.
7739 Returns the difference between N and how many steps down that were
7740 taken."
7741   (interactive "p")
7742   (let ((up (< n 0))
7743         (n (abs n)))
7744   (while (and (> n 0)
7745               (if up (gnus-summary-go-up-thread)
7746                 (gnus-summary-go-down-thread)))
7747     (setq n (1- n)))
7748   (gnus-summary-position-cursor)
7749   (if (/= 0 n) (message "Can't go further" ))
7750   n))
7751
7752 (defun gnus-summary-up-thread (n)
7753   "Go up thread N steps.
7754 If N is negative, go up instead.
7755 Returns the difference between N and how many steps down that were
7756 taken."
7757   (interactive "p")
7758   (gnus-summary-down-thread (- n)))
7759
7760 (defun gnus-summary-kill-thread (unmark)
7761   "Mark articles under current thread as read.
7762 If the prefix argument is positive, remove any kinds of marks.
7763 If the prefix argument is negative, tick articles instead."
7764   (interactive "P")
7765   (if unmark
7766       (setq unmark (prefix-numeric-value unmark)))
7767   (let ((killing t)
7768         (level (gnus-summary-thread-level)))
7769     (save-excursion
7770       (while killing
7771         ;; Mark the article...
7772         (cond ((null unmark) (gnus-summary-mark-as-read nil gnus-killed-mark))
7773               ((> unmark 0) (gnus-summary-tick-article nil t))
7774               (t (gnus-summary-tick-article)))
7775         ;; ...and go forward until either the buffer ends or the subtree
7776         ;; ends. 
7777         (if (not (and (zerop (forward-line 1))
7778                       (> (gnus-summary-thread-level) level)))
7779             (setq killing nil))))
7780     ;; Hide killed subtrees.
7781     (and (null unmark)
7782          gnus-thread-hide-killed
7783          (gnus-summary-hide-thread))
7784     ;; If marked as read, go to next unread subject.
7785     (if (null unmark)
7786         ;; Go to next unread subject.
7787         (gnus-summary-next-subject 1 t)))
7788   (gnus-set-mode-line 'summary))
7789
7790 ;; Summary sorting commands
7791
7792 (defun gnus-summary-sort-by-number (reverse)
7793   "Sort summary buffer by article number.
7794 Argument REVERSE means reverse order."
7795   (interactive "P")
7796   (gnus-summary-sort 'gnus-summary-article-number reverse))
7797
7798 (defun gnus-summary-sort-by-author (reverse)
7799   "Sort summary buffer by author name alphabetically.
7800 If case-fold-search is non-nil, case of letters is ignored.
7801 Argument REVERSE means reverse order."
7802   (interactive "P")
7803   (gnus-summary-sort
7804    (lambda ()
7805      (let ((extract (gnus-extract-address-components
7806                      (header-from (gnus-get-header-by-number
7807                                    (gnus-summary-article-number))))))
7808        (or (car extract) (cdr extract))))
7809    reverse))
7810
7811 (defun gnus-summary-sort-by-subject (reverse)
7812   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
7813 If case-fold-search is non-nil, case of letters is ignored.
7814 Argument REVERSE means reverse order."
7815   (interactive "P")
7816   (gnus-summary-sort
7817    (lambda ()
7818      (downcase (gnus-simplify-subject (gnus-summary-subject-string))))
7819    reverse))
7820
7821 (defun gnus-summary-sort-by-date (reverse)
7822   "Sort summary buffer by date.
7823 Argument REVERSE means reverse order."
7824   (interactive "P")
7825   (gnus-summary-sort
7826    (lambda ()
7827      (gnus-sortable-date
7828       (header-date (gnus-get-header-by-number (gnus-summary-article-number)))))
7829    reverse))
7830
7831 (defun gnus-summary-sort-by-score (reverse)
7832   "Sort summary buffer by score.
7833 Argument REVERSE means reverse order."
7834   (interactive "P")
7835   (gnus-summary-sort 'gnus-summary-article-score (not reverse)))
7836
7837 (defun gnus-summary-sort (predicate reverse)
7838   ;; Sort summary buffer by PREDICATE.  REVERSE means reverse order. 
7839   (let (buffer-read-only)
7840     (goto-char (point-min))
7841     (sort-subr reverse 'forward-line 'end-of-line predicate)))
7842
7843 (defun gnus-sortable-date (date)
7844   "Make sortable string by string-lessp from DATE.
7845 Timezone package is used."
7846   (let* ((date   (timezone-fix-time date nil nil)) ;[Y M D H M S]
7847          (year   (aref date 0))
7848          (month  (aref date 1))
7849          (day    (aref date 2)))
7850     (timezone-make-sortable-date year month day 
7851                                  (timezone-make-time-string
7852                                   (aref date 3) (aref date 4) (aref date 5)))
7853     ))
7854
7855
7856 ;; Summary saving commands.
7857
7858 (defun gnus-summary-save-article (n)
7859   "Save the current article using the default saver function.
7860 If N is a positive number, save the N next articles.
7861 If N is a negative number, save the N previous articles.
7862 If N is nil and any articles have been marked with the process mark,
7863 save those articles instead.
7864 The variable `gnus-default-article-saver' specifies the saver function."
7865   (interactive "P")
7866   (let (articles process)
7867     (if (and n (numberp n))
7868         (let ((backward (< n 0))
7869               (n (abs n)))
7870           (save-excursion
7871             (while (and (> n 0)
7872                         (setq articles (cons (gnus-summary-article-number) 
7873                                              articles))
7874                         (gnus-summary-search-forward nil nil backward))
7875               (setq n (1- n))))
7876           (setq articles (sort articles (function <))))
7877       (if gnus-newsgroup-processable
7878           (progn
7879             (setq articles (setq gnus-newsgroup-processable
7880                                  (nreverse gnus-newsgroup-processable)))
7881             (setq process t))
7882         (setq articles (list (gnus-summary-article-number)))))
7883     (while articles
7884       (let ((header (gnus-gethash (int-to-string (car articles))
7885                                   gnus-newsgroup-headers-hashtb-by-number)))
7886         (if (vectorp header)
7887             (progn
7888               (gnus-summary-display-article (car articles) t)
7889               (if (not gnus-save-all-headers)
7890                   (gnus-article-hide-headers t))
7891               (if gnus-default-article-saver
7892                   (funcall gnus-default-article-saver)
7893                 (error "No default saver is defined.")))
7894           (if (assq 'name header)
7895               (gnus-copy-file (cdr (assq 'name header)))
7896             (message "Article %d is unsaveable" (car articles)))))
7897       (if process
7898           (gnus-summary-remove-process-mark (car articles)))
7899       (setq articles (cdr articles)))
7900     (if process (setq gnus-newsgroup-processable 
7901                       (nreverse gnus-newsgroup-processable)))
7902     (gnus-summary-position-cursor)
7903     n))
7904
7905 (defun gnus-summary-pipe-output (arg)
7906   "Pipe the current article to a subprocess.
7907 If N is a positive number, pipe the N next articles.
7908 If N is a negative number, pipe the N previous articles.
7909 If N is nil and any articles have been marked with the process mark,
7910 pipe those articles instead."
7911   (interactive "P")
7912   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
7913     (gnus-summary-save-article arg)))
7914
7915 (defun gnus-summary-save-article-mail (arg)
7916   "Append the current article to an mail file.
7917 If N is a positive number, save the N next articles.
7918 If N is a negative number, save the N previous articles.
7919 If N is nil and any articles have been marked with the process mark,
7920 save those articles instead."
7921   (interactive "P")
7922   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
7923     (gnus-summary-save-article arg)))
7924
7925 (defun gnus-summary-save-in-rmail (&optional filename)
7926   "Append this article to Rmail file.
7927 Optional argument FILENAME specifies file name.
7928 Directory to save to is default to `gnus-article-save-directory' which
7929 is initialized from the SAVEDIR environment variable."
7930   (interactive)
7931   (let ((default-name
7932           (funcall gnus-rmail-save-name gnus-newsgroup-name
7933                    gnus-current-headers gnus-newsgroup-last-rmail)))
7934     (or filename
7935         (setq filename
7936               (read-file-name
7937                (concat "Save article in rmail file: (default "
7938                        (file-name-nondirectory default-name) ") ")
7939                (file-name-directory default-name)
7940                default-name)))
7941     (gnus-make-directory (file-name-directory filename))
7942     (gnus-eval-in-buffer-window 
7943      gnus-article-buffer
7944      (save-excursion
7945        (save-restriction
7946          (widen)
7947          (gnus-output-to-rmail filename))))
7948     ;; Remember the directory name to save articles.
7949     (setq gnus-newsgroup-last-rmail filename)))
7950
7951 (defun gnus-summary-save-in-mail (&optional filename)
7952   "Append this article to Unix mail file.
7953 Optional argument FILENAME specifies file name.
7954 Directory to save to is default to `gnus-article-save-directory' which
7955 is initialized from the SAVEDIR environment variable."
7956   (interactive)
7957   (let ((default-name
7958           (funcall gnus-mail-save-name gnus-newsgroup-name
7959                    gnus-current-headers gnus-newsgroup-last-mail)))
7960     (or filename
7961         (setq filename
7962               (read-file-name
7963                (concat "Save article in Unix mail file: (default "
7964                        (file-name-nondirectory default-name) ") ")
7965                (file-name-directory default-name)
7966                default-name)))
7967     (setq filename
7968           (expand-file-name filename
7969                             (and default-name
7970                                  (file-name-directory default-name))))
7971     (gnus-make-directory (file-name-directory filename))
7972     (gnus-eval-in-buffer-window 
7973      gnus-article-buffer
7974      (save-excursion
7975        (save-restriction
7976          (widen)
7977          (if (and (file-readable-p filename) (rmail-file-p filename))
7978              (gnus-output-to-rmail filename)
7979            (rmail-output filename 1 t t)))))
7980     ;; Remember the directory name to save articles.
7981     (setq gnus-newsgroup-last-mail filename)))
7982
7983 (defun gnus-summary-save-in-file (&optional filename)
7984   "Append this article to file.
7985 Optional argument FILENAME specifies file name.
7986 Directory to save to is default to `gnus-article-save-directory' which
7987 is initialized from the SAVEDIR environment variable."
7988   (interactive)
7989   (let ((default-name
7990           (funcall gnus-file-save-name gnus-newsgroup-name
7991                    gnus-current-headers gnus-newsgroup-last-file)))
7992     (or filename
7993         (setq filename
7994               (read-file-name
7995                (concat "Save article in file: (default "
7996                        (file-name-nondirectory default-name) ") ")
7997                (file-name-directory default-name)
7998                default-name)))
7999     (gnus-make-directory (file-name-directory filename))
8000     (gnus-eval-in-buffer-window 
8001      gnus-article-buffer
8002      (save-excursion
8003        (save-restriction
8004          (widen)
8005          (gnus-output-to-file filename))))
8006     ;; Remember the directory name to save articles.
8007     (setq gnus-newsgroup-last-file filename)))
8008
8009 (defun gnus-summary-save-in-pipe (&optional command)
8010   "Pipe this article to subprocess."
8011   (interactive)
8012   (let ((command (read-string "Shell command on article: "
8013                               gnus-last-shell-command)))
8014     (if (string-equal command "")
8015         (setq command gnus-last-shell-command))
8016     (gnus-eval-in-buffer-window 
8017      gnus-article-buffer
8018      (save-restriction
8019        (widen)
8020        (shell-command-on-region (point-min) (point-max) command nil)))
8021     (setq gnus-last-shell-command command)))
8022
8023 ;; Summary extract commands
8024
8025 (defun gnus-summary-insert-pseudos (pslist)
8026   (let ((buffer-read-only nil)
8027         (article (gnus-summary-article-number))
8028         b)
8029     (or (gnus-summary-goto-subject article)
8030         (error (format "No such article: %d" article)))
8031     (gnus-summary-position-cursor)
8032     (if (eq gnus-view-pseudos 'automatic)
8033         (while pslist
8034           (and (assq 'execute (car pslist))
8035                (gnus-execute-command (cdr (assq 'execute (car pslist)))))
8036           (setq pslist (cdr pslist)))
8037       (save-excursion
8038         (while pslist
8039           (gnus-summary-goto-subject (or (cdr (assq 'article (car pslist)))
8040                                          (gnus-summary-article-number)))
8041           (forward-line 1)
8042           (setq b (point))
8043           (insert "          " (file-name-nondirectory 
8044                                 (cdr (assq 'name (car pslist))))
8045                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
8046           (add-text-properties 
8047            b (1+ b) (list 'gnus-subject (cdr (assq 'name (car pslist)))
8048                           'gnus-number gnus-reffed-article-number
8049                           'gnus-mark gnus-unread-mark
8050                           'gnus-pseudo (car pslist)
8051                           'gnus-thread 0))
8052           (gnus-sethash (int-to-string gnus-reffed-article-number)
8053                         (car pslist) gnus-newsgroup-headers-hashtb-by-number)
8054           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
8055           (setq pslist (cdr pslist)))))))
8056
8057 (defun gnus-request-pseudo-article (props)
8058   (cond ((assq 'execute props)
8059          (gnus-execute-command (cdr (assq 'execute props))))
8060         ((assq 'digest props)
8061          )
8062         )
8063   (let ((gnus-current-article (gnus-summary-article-number)))
8064     (run-hooks 'gnus-mark-article-hook)))
8065
8066 (defun gnus-execute-command (command &optional automatic)
8067   (save-excursion
8068     (gnus-article-setup-buffer)
8069     (set-buffer gnus-article-buffer)
8070     (let ((command (if automatic command (read-string "Command: " command)))
8071           (buffer-read-only nil))
8072       (erase-buffer)
8073       (insert "$ " command "\n\n")
8074       (if gnus-view-pseudo-asynchronously
8075           (start-process "gnus-execute" nil "sh" "-c" command)
8076         (call-process "sh" nil t nil "-c" command)))))
8077
8078 (defun gnus-copy-file (file &optional to)
8079   "Copy FILE to TO."
8080   (interactive
8081    (list (read-file-name "Copy file: " default-directory)
8082          (read-file-name "Copy file to: " default-directory)))
8083   (or to (setq to (read-file-name "Copy file to: " default-directory)))
8084   (and (file-directory-p to) 
8085        (setq to (concat (file-name-as-directory to)
8086                         (file-name-nondirectory file))))
8087   (copy-file file to))
8088
8089 ;; Summary score file commands
8090
8091 ;; Much modification of the kill (ahem, score) code and lots of the
8092 ;; functions are written by Per Abrahamsen <amanda@iesd.auc.dk>.
8093
8094 (defun gnus-summary-header (header)
8095   ;; Return HEADER for current articles, or error.
8096   (let ((article (gnus-summary-article-number)))
8097     (if article
8098         (aref (gnus-get-header-by-number article)
8099               (nth 1 (assoc header gnus-header-index)))
8100       (error "No article on current line"))))
8101
8102 (defun gnus-summary-score-entry (header match type score date &optional prompt)
8103   "Enter score file entry.
8104 HEADER is the header being scored.
8105 MATCH is the string we are looking for.
8106 TYPE is a flag indicating if it is a regexp or substring.
8107 SCORE is the score to add.
8108 DATE is the expire date."
8109   (interactive (list (completing-read "Header: "
8110                                       gnus-header-index
8111                                       (lambda (x) (fboundp (nth 2 x)))
8112                                       t)
8113                      (read-string "Match: ")
8114                      (y-or-n-p "Use regexp match? ")
8115                      (prefix-numeric-value current-prefix-arg)
8116                      (if (y-or-n-p "Expire kill? ")
8117                          (current-time-string)
8118                        nil)))
8119   (and prompt (setq match (read-string "Match: " match)))
8120   (let ((score (gnus-score-default score)))
8121     (gnus-summary-score-effect header match type score)
8122   
8123     (gnus-summary-score-effect header match type score)
8124     (gnus-score-set header
8125                     (cons (list match type score date) 
8126                           (gnus-score-get header) ))
8127     (gnus-score-set 'touched t)))
8128
8129 (defun gnus-summary-score-effect (header match type score)
8130   "Simulate the effect of a score file entry.
8131 HEADER is the header being scored.
8132 MATCH is the string we are looking for.
8133 TYPE is a flag indicating if it is a regexp or substring.
8134 SCORE is the score to add."
8135   (interactive (list (completing-read "Header: "
8136                                       gnus-header-index
8137                                       (lambda (x) (fboundp (nth 2 x)))
8138                                       t)
8139                      (read-string "Match: ")
8140                      (y-or-n-p "Use regexp match? ")
8141                      (prefix-numeric-value current-prefix-arg)))
8142   (save-excursion
8143     (or (and (stringp match) (> (length match) 0))
8144       (error "No match"))
8145     (goto-char (point-min))
8146     (let ((regexp (if type
8147                       match
8148                     (concat "\\`.*" (regexp-quote match) ".*\\'"))))
8149       (while (not (eobp))
8150         (let ((content (gnus-summary-header header))
8151               (case-fold-search t))
8152           (and content
8153                (if (string-match regexp content)
8154                    (gnus-summary-raise-score score))))
8155         (beginning-of-line 2)))))
8156
8157 (defun gnus-summary-score-crossposting (score date)
8158    ;; Enter score file entry for current crossposting.
8159    ;; SCORE is the score to add.
8160    ;; DATE is the expire date.
8161    (let ((xref (gnus-summary-header "xref"))
8162          (start 0)
8163          group)
8164      (or xref (error "This article is not crossposted"))
8165      (while (string-match " \\([^ \t]+\\):" xref start)
8166        (setq start (match-end 0))
8167        (if (not (string= 
8168                  (setq group 
8169                        (substring xref (match-beginning 1) (match-end 1)))
8170                  gnus-newsgroup-name))
8171            (gnus-summary-score-entry
8172             "xref" (concat " " group ":") nil score date t)))))
8173
8174 (defun gnus-summary-temporarily-lower-by-subject (level)
8175   "Temporarily lower score by LEVEL for current subject.
8176 See `gnus-kill-expiry-days'."
8177   (interactive "P")
8178   (gnus-summary-score-entry
8179    "subject" (gnus-simplify-subject-re (gnus-summary-header "subject"))
8180    nil (- (gnus-score-default level) )
8181    (current-time-string) t))
8182
8183 (defun gnus-summary-temporarily-lower-by-author (level)
8184   "Temporarily lower score by LEVEL for current author.
8185 See `gnus-kill-expiry-days'."
8186   (interactive "P")
8187   (gnus-summary-score-entry
8188    "from" (gnus-summary-header "from") nil (- (gnus-score-default level)) 
8189    (current-time-string) t))
8190
8191 (defun gnus-summary-temporarily-lower-by-xref (level)
8192   "Temporarily lower score by LEVEL for current xref.
8193 See `gnus-kill-expiry-days'."
8194   (interactive "P")
8195   (gnus-summary-score-crossposting (- (gnus-score-default level)) (current-time-string)))
8196
8197 (defun gnus-summary-temporarily-lower-by-thread (level)
8198   "Temporarily lower score by LEVEL for current thread.
8199 See `gnus-kill-expiry-days'."
8200   (interactive "P")
8201   (gnus-summary-score-entry
8202    "references" (gnus-summary-header "id")
8203    nil (- (gnus-score-default level)) (current-time-string)))
8204
8205 (defun gnus-summary-lower-by-subject (level)
8206   "Lower score by LEVEL for current subject."
8207   (interactive "P")
8208   (gnus-summary-score-entry
8209    "subject" (gnus-simplify-subject-re (gnus-summary-header "subject"))
8210    nil (- (gnus-score-default level)) 
8211    nil t))
8212
8213 (defun gnus-summary-lower-by-author (level)
8214   "Lower score by LEVEL for current author."
8215   (interactive "P")
8216   (gnus-summary-score-entry
8217    "from" (gnus-summary-header "from") nil (- (gnus-score-default level)) nil t))
8218
8219 (defun gnus-summary-lower-by-xref (level)
8220   "Lower score by LEVEL for current xref."
8221   (interactive "P")
8222   (gnus-summary-score-crossposting (- (gnus-score-default level)) nil))
8223
8224 (defun gnus-summary-lower-followups-to-author (level)
8225   "Lower score by LEVEL for all followups to the current author."
8226   (interactive "P")
8227   (gnus-kill-file-lower-followups-to-author
8228    (gnus-score-default level)
8229    (let ((article (gnus-summary-article-number)))
8230      (if article (gnus-get-header-by-number article)
8231        (error "No article on current line")))))
8232
8233 (defun gnus-summary-temporarily-raise-by-subject (level)
8234   "Temporarily raise score by LEVEL for current subject.
8235 See `gnus-kill-expiry-days'."
8236   (interactive "P")
8237   (gnus-summary-score-entry
8238    "subject" (gnus-simplify-subject-re (gnus-summary-header "subject"))
8239    nil level (current-time-string) t))
8240
8241 (defun gnus-summary-temporarily-raise-by-author (level)
8242   "Temporarily raise score by LEVEL for current author.
8243 See `gnus-kill-expiry-days'."
8244   (interactive "P")
8245   (gnus-summary-score-entry
8246    "from" (gnus-summary-header "from") nil level (current-time-string) t))
8247
8248 (defun gnus-summary-temporarily-raise-by-xref (level)
8249   "Temporarily raise score by LEVEL for current xref.
8250 See `gnus-kill-expiry-days'."
8251   (interactive "P")
8252   (gnus-summary-score-crossposting level (current-time-string)))
8253
8254 (defun gnus-summary-temporarily-raise-by-thread (level)
8255   "Temporarily raise score by LEVEL for current thread.
8256 See `gnus-kill-expiry-days'."
8257   (interactive "P")
8258   (gnus-summary-score-entry
8259    "references" (gnus-summary-header "id")
8260    nil level (current-time-string)))
8261
8262 (defun gnus-summary-raise-by-subject (level)
8263   "Raise score by LEVEL for current subject."
8264   (interactive "P")
8265   (gnus-summary-score-entry
8266    "subject" (gnus-simplify-subject-re (gnus-summary-header "subject"))
8267    nil level nil t))
8268
8269 (defun gnus-summary-raise-by-author (level)
8270   "Raise score by LEVEL for current author."
8271   (interactive "P")
8272   (gnus-summary-score-entry
8273    "from" (gnus-summary-header "from") nil level nil t))
8274
8275 (defun gnus-summary-raise-by-xref (level)
8276   "Raise score by LEVEL for current xref."
8277   (interactive "P")
8278   (gnus-summary-score-crossposting level nil))
8279
8280 (defun gnus-summary-edit-global-kill ()
8281   "Edit a global score file."
8282   (interactive)
8283   (setq gnus-current-kill-article (gnus-summary-article-number))
8284   (gnus-kill-file-edit-file nil)        ;Nil stands for global score file.
8285   (message
8286    (substitute-command-keys
8287     "Editing a global score file (Type \\[gnus-kill-file-exit] to exit)")))
8288
8289 (defun gnus-summary-raise-followups-to-author (level)
8290   "Raise score by LEVEL for all followups to the current author."
8291   (interactive "P")
8292   (gnus-kill-file-raise-followups-to-author
8293    (gnus-score-default level)
8294    (let ((article (gnus-summary-article-number)))
8295      (if article (gnus-get-header-by-number article)
8296        (error "No article on current line")))))
8297
8298 (defun gnus-summary-edit-local-kill ()
8299   "Edit a local score file applied to the current newsgroup."
8300   (interactive)
8301   (setq gnus-current-kill-article (gnus-summary-article-number))
8302   (gnus-kill-file-edit-file gnus-newsgroup-name)
8303   (message
8304    (substitute-command-keys
8305     "Editing a local score file (Type \\[gnus-kill-file-exit] to exit)")))
8306
8307
8308 \f
8309 ;;;
8310 ;;; Gnus article mode
8311 ;;;
8312
8313 (if gnus-article-mode-map
8314     nil
8315   (setq gnus-article-mode-map (make-keymap))
8316   (suppress-keymap gnus-article-mode-map)
8317   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
8318   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
8319   (define-key gnus-article-mode-map "\C-x^" 'gnus-article-refer-article)
8320   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
8321   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
8322   (define-key gnus-article-mode-map "\C-xm" 'gnus-article-mail)
8323   (define-key gnus-article-mode-map "\C-xM" 'gnus-article-mail-with-original)
8324   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
8325   
8326   ;; Duplicate almost all summary keystrokes in the article mode map.
8327   (let ((commands 
8328          (list "#" "\M-#" "\C-c\M-#" "\r" "n" "p"
8329                "N" "P" "\M-\C-n" "\M-\C-p" "." "\M-s" "\M-r"
8330                "<" ">" "l" "j" "^" "\M-^" "-" "u" "U" "d" "D"
8331                "\M-u" "\M-U" "k" "\C-k" "\M-\C-k" "c" "x" "X" 
8332                "\M-\C-x" "\M-\177" "b" "B" "$" "w" "\C-c\C-r"
8333                "t" "\M-t" "a" "f" "F" "C" "S" "r" "R" "\C-c\C-f"
8334                "m" "o" "\C-o" "|" "\M-m" "\M-\C-m" "\M-k" "m" "M"
8335                "V" "\C-c\C-d" "q" "Q")))
8336     (while commands
8337       (define-key gnus-article-mode-map (car commands) 
8338         'gnus-article-summary-command)
8339       (setq commands (cdr commands)))))
8340
8341
8342 (defun gnus-article-mode ()
8343   "Major mode for reading an article.
8344 All normal editing commands are switched off.
8345 The following commands are available:
8346
8347 \\<gnus-article-mode-map>
8348 \\[gnus-article-next-page]\t Scroll the article one page forwards
8349 \\[gnus-article-prev-page]\t Scroll the article one page backwards
8350 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
8351 \\[gnus-article-show-summary]\t Display the summary buffer
8352 \\[gnus-article-mail]\t Send a reply to the address near point
8353 \\[gnus-article-mail-with-original]\t Send a reply to the address near point; include the original article
8354 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
8355 \\[gnus-info-find-node]\t Go to the Gnus info node
8356
8357 "
8358   (interactive)
8359   (if gnus-visual (gnus-article-make-menu-bar))
8360   (kill-all-local-variables)
8361   (setq mode-line-modified "-- ")
8362   (make-local-variable 'mode-line-format)
8363   (setq mode-line-format (copy-sequence mode-line-format))
8364   (and (equal (nth 3 mode-line-format) "   ")
8365        (setcar (nthcdr 3 mode-line-format) ""))
8366   (setq mode-name "Article")
8367   (setq major-mode 'gnus-article-mode)
8368   (make-local-variable 'minor-mode-alist)
8369   (or (assq 'gnus-show-mime minor-mode-alist)
8370       (setq minor-mode-alist
8371             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
8372   (use-local-map gnus-article-mode-map)
8373   (make-local-variable 'page-delimiter)
8374   (setq page-delimiter gnus-page-delimiter)
8375   (buffer-disable-undo (current-buffer))
8376   (setq buffer-read-only t)             ;Disable modification
8377   (run-hooks 'gnus-article-mode-hook))
8378
8379 (defun gnus-article-setup-buffer ()
8380   "Initialize article mode buffer."
8381   (or (get-buffer gnus-article-buffer)
8382       (save-excursion
8383         (set-buffer (get-buffer-create gnus-article-buffer))
8384         (gnus-add-current-to-buffer-list)
8385         (gnus-article-mode))))
8386
8387 (defun gnus-request-article-this-buffer (article &optional group)
8388   "Get an article and insert it into this buffer."
8389   (setq group (or group gnus-newsgroup-name))
8390   ;; Using `gnus-request-article' directly will insert the article into
8391   ;; `nntp-server-buffer' - so we'll save some time by not having to
8392   ;; copy it from the server buffer into the article buffer.
8393
8394   ;; We only request an article by message-id when we do not have the
8395   ;; headers for it, so we'll have to get those.
8396   (and (stringp article) (gnus-read-header article))
8397
8398   ;; If the article number is negative, that means that this article
8399   ;; doesn't belong in this newsgroup (possibly), so we find its
8400   ;; message-id and request it by id instead of number.
8401   (if (and (numberp article) (< article 0))
8402       (save-excursion
8403         (set-buffer gnus-summary-buffer)
8404         (let ((header (gnus-gethash (int-to-string article)
8405                                     gnus-newsgroup-headers-hashtb-by-number)))
8406           (if (vectorp header)
8407               ;; It's a real article.
8408               (setq article (header-id header))
8409             ;; It is an extracted pseudo-article.
8410             (setq article nil)
8411             (gnus-request-pseudo-article header)))))
8412   ;; Get the article and into the article buffer.
8413   (if article
8414       (progn
8415        (erase-buffer)
8416        (and (gnus-request-article article group (current-buffer))
8417             'article))
8418     'pseudo))
8419
8420 (defun gnus-read-header (id)
8421   "Read the headers of article ID and enter them into the Gnus system."
8422   (or gnus-newsgroup-headers-hashtb-by-number
8423       (gnus-make-headers-hashtable-by-number))
8424   (let (header)
8425     (if (not (setq header 
8426                    (car (if (let ((gnus-nov-is-evil t))
8427                               (gnus-retrieve-headers 
8428                                (list id) gnus-newsgroup-name))
8429                             (gnus-get-newsgroup-headers)))))
8430         nil
8431       (if (stringp id)
8432           (header-set-number header gnus-reffed-article-number))
8433       (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers))
8434       (gnus-sethash (int-to-string (header-number header)) header
8435                     gnus-newsgroup-headers-hashtb-by-number)
8436       (if (stringp id)
8437           (setq gnus-reffed-article-number (1- gnus-reffed-article-number)))
8438       (setq gnus-current-headers header)
8439       header)))
8440
8441 (defun gnus-article-prepare (article &optional all-headers header)
8442   "Prepare ARTICLE in article mode buffer.
8443 ARTICLE should either be an article number or a Message-ID.
8444 If ARTICLE is an id, HEADER should be the article headers.
8445 If ALL-HEADERS is non-nil, no headers are hidden."
8446   (save-excursion
8447     ;; Make sure we start in a summary buffer.
8448     (or (eq major-mode 'gnus-summary-mode)
8449         (set-buffer gnus-summary-buffer))
8450     (setq gnus-summary-buffer (current-buffer))
8451     ;; Make sure the connection to the server is alive.
8452     (or (gnus-server-opened (gnus-find-method-for-group gnus-newsgroup-name))
8453         (progn
8454           (gnus-check-news-server 
8455            (gnus-find-method-for-group gnus-newsgroup-name))
8456           (gnus-request-group gnus-newsgroup-name t)))
8457     (or gnus-newsgroup-headers-hashtb-by-number
8458         (gnus-make-headers-hashtable-by-number))
8459     (let* ((article (if header (header-number header) article))
8460            (summary-buffer (current-buffer))
8461            (internal-hook gnus-article-internal-prepare-hook)
8462            (bookmark (cdr (assq article gnus-newsgroup-bookmarks)))
8463            (group gnus-newsgroup-name)
8464            result)
8465       (save-excursion
8466         (gnus-article-setup-buffer)
8467         (set-buffer gnus-article-buffer)
8468         (let ((buffer-read-only nil))
8469           (if (not (setq result (gnus-request-article-this-buffer 
8470                                  article group)))
8471               ;; There is no such article.
8472               (progn
8473                 (save-excursion
8474                   (set-buffer gnus-summary-buffer)
8475                   (setq gnus-current-article 0)
8476                   (and (numberp article) 
8477                        (gnus-summary-mark-as-read article gnus-canceled-mark))
8478                   (message "No such article (may be canceled)")
8479                   (ding) )
8480                 (setq gnus-article-current nil)
8481                 nil)
8482             (if (not (eq result 'article))
8483                 (progn
8484                   (save-excursion
8485                     (set-buffer summary-buffer)
8486                     (setq gnus-last-article gnus-current-article
8487                           gnus-current-article 0
8488                           gnus-current-headers nil
8489                           gnus-article-current nil)
8490                     (gnus-configure-windows 'article)
8491                     (gnus-set-mode-line 'summary)
8492                     (gnus-set-global-variables))
8493                   (gnus-set-mode-line 'article))
8494               ;; The result from the `request' was an actual article -
8495               ;; or at least some text that is now displayed in the
8496               ;; article buffer.
8497               (if (and (numberp article)
8498                        (not (eq article gnus-current-article)))
8499                   ;; Seems like a new article has been selected.
8500                   ;; `gnus-current-article' must be an article number.
8501                   (save-excursion
8502                     (set-buffer summary-buffer)
8503                     (setq gnus-last-article gnus-current-article)
8504                     (setq gnus-current-article article)
8505                     (setq gnus-current-headers 
8506                           (gnus-get-header-by-number gnus-current-article))
8507                     (setq gnus-article-current 
8508                           (cons gnus-newsgroup-name gnus-current-article))
8509                     (gnus-set-mode-line 'summary)
8510                     (run-hooks 'gnus-mark-article-hook)
8511                     (and gnus-visual 
8512                          (run-hooks 'gnus-visual-mark-article-hook))
8513                     ;; Set the global newsgroup variables here.
8514                     ;; Suggested by Jim Sisolak
8515                     ;; <sisolak@trans4.neep.wisc.edu>.
8516                     (gnus-set-global-variables)))
8517               ;; gnus-have-all-headers must be either T or NIL.
8518               (setq gnus-have-all-headers
8519                     (not (not (or all-headers gnus-show-all-headers))))
8520               ;; Hooks for getting information from the article.
8521               ;; This hook must be called before being narrowed.
8522               (run-hooks 'internal-hook)
8523               (run-hooks 'gnus-article-prepare-hook)
8524               ;; Decode MIME message.
8525               (if (and gnus-show-mime
8526                        (gnus-fetch-field "Mime-Version"))
8527                   (funcall gnus-show-mime-method))
8528               ;; Perform the article display hooks.
8529               (let ((buffer-read-only nil))
8530                 (run-hooks 'gnus-article-display-hook))
8531               ;; Do page break.
8532               (goto-char (point-min))
8533               (and gnus-break-pages (gnus-narrow-to-page))
8534               (gnus-set-mode-line 'article)
8535               (gnus-configure-windows 'article)
8536               (goto-char 1)
8537               (set-window-start 
8538                (get-buffer-window gnus-article-buffer) (point-min))
8539               (if bookmark
8540                   (progn
8541                     (message "Moved to bookmark")
8542                     (search-forward "\n\n" nil t)
8543                     (forward-line bookmark)))
8544               t)))))))
8545
8546 (defun gnus-article-show-all-headers ()
8547   "Show all article headers in article mode buffer."
8548   (save-excursion 
8549     (setq gnus-have-all-headers t)
8550     (gnus-article-setup-buffer)
8551     (set-buffer gnus-article-buffer)
8552     (let ((buffer-read-only nil))
8553       (remove-text-properties 1 (point-max) '(invisible t)))))
8554
8555 (defun gnus-article-hide-headers-if-wanted ()
8556   "Hide unwanted headers if `gnus-have-all-headers' is nil.
8557 Provided for backwards compatability."
8558   (or gnus-have-all-headers
8559       (gnus-article-hide-headers)))
8560
8561 (defun gnus-article-hide-headers (&optional delete)
8562   "Hide unwanted headers and possibly sort them as well."
8563   (interactive "P")
8564   (save-excursion
8565     (set-buffer gnus-article-buffer)
8566     (save-restriction
8567       (let ((sorted gnus-sorted-header-list)
8568             (buffer-read-only nil)
8569             want want-list beg want-l)
8570         ;; First we narrow to just the headers.
8571         (widen)
8572         (goto-char 1)
8573         ;; Hide any "From " lines at the beginning of (mail) articles. 
8574         (while (looking-at rmail-unix-mail-delimiter)
8575           (forward-line 1))
8576         (if (/= (point) 1) 
8577             (add-text-properties 1 (point) '(invisible t)))
8578         ;; Then treat the rest of the header lines.
8579         (narrow-to-region 
8580          (point) 
8581          (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
8582         ;; Then we use the two regular expressions
8583         ;; `gnus-ignored-headers' and `gnus-visible-headers' to
8584         ;; select which header lines is to remain visible in the
8585         ;; article buffer.
8586         (goto-char 1)
8587         (while (re-search-forward "^[^ \t]*:" nil t)
8588           (beginning-of-line)
8589           ;; We add the headers we want to keep to a list and delete
8590           ;; them from the buffer.
8591           (if (or (and (stringp gnus-visible-headers)
8592                        (looking-at gnus-visible-headers))
8593                   (and (not (stringp gnus-visible-headers))
8594                        (stringp gnus-ignored-headers)
8595                        (not (looking-at gnus-ignored-headers))))
8596               (progn
8597                 (setq beg (point))
8598                 (forward-line 1)
8599                 ;; Be sure to get multi-line headers...
8600                 (re-search-forward "^[^ \t]*:" nil t)
8601                 (beginning-of-line)
8602                 (setq want-list 
8603                       (cons (buffer-substring beg (point)) want-list))
8604                 (delete-region beg (point))
8605                 (goto-char beg))
8606             (forward-line 1)))
8607         ;; Next we perform the sorting by looking at
8608         ;; `gnus-sorted-header-list'. 
8609         (goto-char 1)
8610         (while (and sorted want-list)
8611           (setq want-l want-list)
8612           (while (and want-l
8613                       (not (string-match (car sorted) (car want-l))))
8614             (setq want-l (cdr want-l)))
8615           (if want-l 
8616               (progn
8617                 (insert (car want-l))
8618                 (setq want-list (delq (car want-l) want-list))))
8619           (setq sorted (cdr sorted)))
8620         ;; Any headers that were not matched by the sorted list we
8621         ;; just tack on the end of the visible header list.
8622         (while want-list
8623           (insert (car want-list))
8624           (setq want-list (cdr want-list)))
8625         ;; And finally we make the unwanted headers invisible.
8626         (if delete
8627             (delete-region (point) (point-max))
8628           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
8629           (add-text-properties (point) (point-max) '(invisible t)))))))
8630
8631 (defun gnus-article-hide-signature ()
8632   "Hides the signature in an article.
8633 It does this by hiding everyting after \"^-- *$\", which is what all
8634 signatures should be preceded by. Note that this may mean that parts
8635 of an article may disappear if the article has such a line in the
8636 middle of the text."
8637   (interactive)
8638   (save-excursion
8639     (set-buffer gnus-article-buffer)
8640     (let ((buffer-read-only nil))
8641       (goto-char (point-max))
8642       (if (re-search-backward "^-- *$" nil t)
8643           (progn
8644             (add-text-properties (point) (point-max) '(invisible t)))))))
8645
8646 (defun gnus-article-hide-citation ()
8647   "Hide all cited text.
8648 This function uses the famous, extremely intelligent \"shoot in foot\"
8649 algorithm - which is simply deleting all lines that start with
8650 \">\". Your mileage may vary. If you come up with anything better,
8651 please do mail it to me."
8652   (interactive)
8653   (save-excursion
8654     (set-buffer gnus-article-buffer)
8655     (let ((buffer-read-only nil))
8656       (goto-char 1)
8657       (search-forward "\n\n" nil t)
8658       (while (not (eobp))
8659         (if (looking-at ">")
8660             (add-text-properties 
8661              (point) (save-excursion (forward-line 1) (point))
8662              '(invisible t)))
8663         (forward-line 1)))))
8664
8665 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
8666 (defun gnus-article-treat-overstrike ()
8667   ;; Prepare article for overstrike commands.
8668   (interactive)
8669   (save-excursion
8670     (set-buffer gnus-article-buffer)
8671     (let ((buffer-read-only nil))
8672       (while (search-forward "\b" nil t)
8673         (let ((next (following-char))
8674               (previous (char-after (- (point) 2))))
8675           (cond ((eq next previous)
8676                  (delete-region (- (point) 2) (point))
8677                  (put-text-property (point) (1+ (point))
8678                                     'face 'bold))
8679                 ((eq next ?_)
8680                  (delete-region (1- (point)) (1+ (point)))
8681                  (put-text-property (1- (point)) (point)
8682                                     'face 'underline))
8683                 ((eq previous ?_)
8684                  (delete-region (- (point) 2) (point))
8685                  (put-text-property (point) (1+ (point))
8686                                     'face 'underline))))))))
8687
8688 (defun gnus-article-word-wrap ()
8689   "Format too long lines."
8690   (interactive)
8691   (save-excursion
8692     (set-buffer gnus-article-buffer)
8693     (let ((buffer-read-only nil))
8694       (goto-char 1)
8695       (search-forward "\n\n" nil t)
8696       (end-of-line 1)
8697       (let ((paragraph-start "^\\W"))
8698         (while (not (eobp))
8699           (and (>= (current-column) (window-width))
8700                (/= (preceding-char) ?:)
8701                (fill-paragraph nil))
8702           (end-of-line 2))))))
8703
8704 (defun gnus-article-remove-cr ()
8705   (interactive)
8706   (save-excursion
8707     (let ((buffer-read-only nil))
8708       (set-buffer gnus-article-buffer)
8709       (while (search-forward "\r" nil t)
8710         (replace-match "")))))
8711
8712 (defun gnus-article-de-quoted-unreadable ()
8713   (interactive)
8714   (save-excursion
8715     (save-restriction
8716       (set-buffer gnus-article-buffer)
8717       (let ((buffer-read-only nil))
8718         (widen)
8719         (goto-char (point-min))
8720         (while (re-search-forward "=[0-9A-F][0-9A-F]" nil t)
8721           (replace-match 
8722            (char-to-string 
8723             (+
8724              (* 16 (gnus-hex-char-to-integer 
8725                     (char-after (1+ (match-beginning 0)))))
8726              (gnus-hex-char-to-integer
8727               (char-after (1- (match-end 0))))))))))))
8728
8729 ;; Taken from hexl.el.
8730 (defun gnus-hex-char-to-integer (character)
8731   "Take a char and return its value as if it was a hex digit."
8732   (if (and (>= character ?0) (<= character ?9))
8733       (- character ?0)
8734     (let ((ch (logior character 32)))
8735       (if (and (>= ch ?a) (<= ch ?f))
8736           (- ch (- ?a 10))
8737         (error (format "Invalid hex digit `%c'." ch))))))
8738
8739 ;; Article savers.
8740
8741 (defun gnus-output-to-rmail (file-name)
8742   "Append the current article to an Rmail file named FILE-NAME."
8743   (require 'rmail)
8744   ;; Most of these codes are borrowed from rmailout.el.
8745   (setq file-name (expand-file-name file-name))
8746   (setq rmail-default-rmail-file file-name)
8747   (let ((artbuf (current-buffer))
8748         (tmpbuf (get-buffer-create " *Gnus-output*")))
8749     (save-excursion
8750       (or (get-file-buffer file-name)
8751           (file-exists-p file-name)
8752           (if (gnus-yes-or-no-p
8753                (concat "\"" file-name "\" does not exist, create it? "))
8754               (let ((file-buffer (create-file-buffer file-name)))
8755                 (save-excursion
8756                   (set-buffer file-buffer)
8757                   (rmail-insert-rmail-file-header)
8758                   (let ((require-final-newline nil))
8759                     (write-region (point-min) (point-max) file-name t 1)))
8760                 (kill-buffer file-buffer))
8761             (error "Output file does not exist")))
8762       (set-buffer tmpbuf)
8763       (buffer-disable-undo (current-buffer))
8764       (erase-buffer)
8765       (insert-buffer-substring artbuf)
8766       (gnus-convert-article-to-rmail)
8767       ;; Decide whether to append to a file or to an Emacs buffer.
8768       (let ((outbuf (get-file-buffer file-name)))
8769         (if (not outbuf)
8770             (append-to-file (point-min) (point-max) file-name)
8771           ;; File has been visited, in buffer OUTBUF.
8772           (set-buffer outbuf)
8773           (let ((buffer-read-only nil)
8774                 (msg (and (boundp 'rmail-current-message)
8775                           rmail-current-message)))
8776             ;; If MSG is non-nil, buffer is in RMAIL mode.
8777             (if msg
8778                 (progn (widen)
8779                        (narrow-to-region (point-max) (point-max))))
8780             (insert-buffer-substring tmpbuf)
8781             (if msg
8782                 (progn
8783                   (goto-char (point-min))
8784                   (widen)
8785                   (search-backward "\^_")
8786                   (narrow-to-region (point) (point-max))
8787                   (goto-char (1+ (point-min)))
8788                   (rmail-count-new-messages t)
8789                   (rmail-show-message msg))))))
8790       )
8791     (kill-buffer tmpbuf)
8792     ))
8793
8794 (defun gnus-output-to-file (file-name)
8795   "Append the current article to a file named FILE-NAME."
8796   (setq file-name (expand-file-name file-name))
8797   (let ((artbuf (current-buffer))
8798         (tmpbuf (get-buffer-create " *Gnus-output*")))
8799     (save-excursion
8800       (set-buffer tmpbuf)
8801       (buffer-disable-undo (current-buffer))
8802       (erase-buffer)
8803       (insert-buffer-substring artbuf)
8804       ;; Append newline at end of the buffer as separator, and then
8805       ;; save it to file.
8806       (goto-char (point-max))
8807       (insert "\n")
8808       (append-to-file (point-min) (point-max) file-name))
8809     (kill-buffer tmpbuf)
8810     ))
8811
8812 (defun gnus-convert-article-to-rmail ()
8813   "Convert article in current buffer to Rmail message format."
8814   (let ((buffer-read-only nil))
8815     ;; Convert article directly into Babyl format.
8816     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
8817     (goto-char (point-min))
8818     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
8819     (while (search-forward "\n\^_" nil t) ;single char
8820       (replace-match "\n^_"))           ;2 chars: "^" and "_"
8821     (goto-char (point-max))
8822     (insert "\^_")))
8823
8824 (defun gnus-narrow-to-page (&optional arg)
8825   "Make text outside current page invisible except for page delimiter.
8826 A numeric arg specifies to move forward or backward by that many pages,
8827 thus showing a page other than the one point was originally in."
8828   (interactive "P")
8829   (setq arg (if arg (prefix-numeric-value arg) 0))
8830   (save-excursion
8831     (forward-page -1)                   ;Beginning of current page.
8832     (widen)
8833     (if (> arg 0)
8834         (forward-page arg)
8835       (if (< arg 0)
8836           (forward-page (1- arg))))
8837     ;; Find the end of the page.
8838     (forward-page)
8839     ;; If we stopped due to end of buffer, stay there.
8840     ;; If we stopped after a page delimiter, put end of restriction
8841     ;; at the beginning of that line.
8842     ;; These are commented out.
8843     ;;    (if (save-excursion (beginning-of-line)
8844     ;;                  (looking-at page-delimiter))
8845     ;;  (beginning-of-line))
8846     (narrow-to-region (point)
8847                       (progn
8848                         ;; Find the top of the page.
8849                         (forward-page -1)
8850                         ;; If we found beginning of buffer, stay there.
8851                         ;; If extra text follows page delimiter on same line,
8852                         ;; include it.
8853                         ;; Otherwise, show text starting with following line.
8854                         (if (and (eolp) (not (bobp)))
8855                             (forward-line 1))
8856                         (point)))
8857     ))
8858
8859 (defun gnus-gmt-to-local ()
8860   "Rewrite Date: field described in GMT to local in current buffer.
8861 The variable gnus-local-timezone is used for local time zone.
8862 Intended to be used with gnus-article-prepare-hook."
8863   (save-excursion
8864     (save-restriction
8865       (widen)
8866       (goto-char (point-min))
8867       (narrow-to-region (point-min)
8868                         (progn (search-forward "\n\n" nil 'move) (point)))
8869       (goto-char (point-min))
8870       (if (re-search-forward "^Date:[ \t]\\(.*\\)$" nil t)
8871           (let ((buffer-read-only nil)
8872                 (date (buffer-substring (match-beginning 1) (match-end 1))))
8873             (delete-region (match-beginning 1) (match-end 1))
8874             (insert
8875              (timezone-make-date-arpa-standard date nil gnus-local-timezone))
8876             ))
8877       )))
8878
8879
8880 ;; Article mode commands
8881
8882 (defun gnus-article-next-page (lines)
8883   "Show next page of current article.
8884 If end of article, return non-nil. Otherwise return nil.
8885 Argument LINES specifies lines to be scrolled up."
8886   (interactive "P")
8887   (move-to-window-line -1)
8888   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
8889   (if (save-excursion
8890         (end-of-line)
8891         (and (pos-visible-in-window-p)  ;Not continuation line.
8892              (eobp)))
8893       ;; Nothing in this page.
8894       (if (or (not gnus-break-pages)
8895               (save-excursion
8896                 (save-restriction
8897                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
8898           t                             ;Nothing more.
8899         (gnus-narrow-to-page 1)         ;Go to next page.
8900         nil
8901         )
8902     ;; More in this page.
8903     (condition-case ()
8904         (scroll-up lines)
8905       (end-of-buffer
8906        ;; Long lines may cause an end-of-buffer error.
8907        (goto-char (point-max))))
8908     nil
8909     ))
8910
8911 (defun gnus-article-prev-page (lines)
8912   "Show previous page of current article.
8913 Argument LINES specifies lines to be scrolled down."
8914   (interactive "P")
8915   (move-to-window-line 0)
8916   (if (and gnus-break-pages
8917            (bobp)
8918            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
8919       (progn
8920         (gnus-narrow-to-page -1) ;Go to previous page.
8921         (goto-char (point-max))
8922         (recenter -1))
8923     (scroll-down lines)))
8924
8925 (defun gnus-article-refer-article ()
8926   "Read article specified by message-id around point."
8927   (interactive)
8928   (search-forward ">" nil t)    ;Move point to end of "<....>".
8929   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
8930       (let ((message-id
8931              (buffer-substring (match-beginning 1) (match-end 1))))
8932         (set-buffer gnus-summary-buffer)
8933         (gnus-summary-refer-article message-id))
8934     (error "No references around point")))
8935
8936 (defun gnus-article-mail (yank)
8937   "Send a reply to the address near point.
8938 If YANK is non-nil, include the original article."
8939   (interactive "P")
8940   (let ((address 
8941          (buffer-substring
8942           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
8943           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
8944     (and address
8945          (progn
8946            (switch-to-buffer gnus-summary-buffer)
8947            (funcall gnus-mail-reply-method yank address)))))
8948
8949 (defun gnus-article-mail-with-original ()
8950   "Send a reply to the address near point and include the original article."
8951   (interactive)
8952   (gnus-article-mail 'yank))
8953
8954 (defun gnus-article-show-summary ()
8955   "Reconfigure windows to show summary buffer."
8956   (interactive)
8957   (gnus-configure-windows 'article)
8958   (pop-to-buffer gnus-summary-buffer)
8959   (gnus-summary-goto-subject gnus-current-article))
8960
8961 (defun gnus-article-describe-briefly ()
8962   "Describe article mode commands briefly."
8963   (interactive)
8964   (message
8965    (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")))
8966
8967 (defun gnus-article-summary-command ()
8968   "Execute the last keystroke in the summary buffer."
8969   (interactive)
8970   (message "                                                                              ")
8971   (let ((obuf (current-buffer))
8972         (owin (current-window-configuration)))
8973     (switch-to-buffer gnus-summary-buffer 'norecord)
8974     (execute-kbd-macro (this-command-keys))
8975     (set-buffer obuf)
8976     (let ((npoint (point)))
8977       (set-window-configuration owin)
8978       (set-window-start (get-buffer-window (current-buffer)) (point)))))
8979
8980 ;; caesar-region written by phr@prep.ai.mit.edu  Nov 86
8981 ;; Modified by tower@prep Nov 86
8982 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
8983
8984 (defun gnus-caesar-region (&optional n)
8985   "Caesar rotation of region by N, default 13, for decrypting netnews.
8986 ROT47 will be performed for Japanese text in any case."
8987   (interactive (if current-prefix-arg   ; Was there a prefix arg?
8988                    (list (prefix-numeric-value current-prefix-arg))
8989                  (list nil)))
8990   (cond ((not (numberp n)) (setq n 13))
8991         (t (setq n (mod n 26))))        ;canonicalize N
8992   (if (not (zerop n))           ; no action needed for a rot of 0
8993       (progn
8994         (if (or (not (boundp 'caesar-translate-table))
8995                 (not caesar-translate-table)
8996                 (/= (aref caesar-translate-table ?a) (+ ?a n)))
8997             (let ((i 0) (lower "abcdefghijklmnopqrstuvwxyz") upper)
8998               (message "Building caesar-translate-table...")
8999               (setq caesar-translate-table (make-vector 256 0))
9000               (while (< i 256)
9001                 (aset caesar-translate-table i i)
9002                 (setq i (1+ i)))
9003               (setq lower (concat lower lower) upper (upcase lower) i 0)
9004               (while (< i 26)
9005                 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
9006                 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
9007                 (setq i (1+ i)))
9008               ;; ROT47 for Japanese text.
9009               ;; Thanks to ichikawa@flab.fujitsu.junet.
9010               (setq i 161)
9011               (let ((t1 (logior ?O 128))
9012                     (t2 (logior ?! 128))
9013                     (t3 (logior ?~ 128)))
9014                 (while (< i 256)
9015                   (aset caesar-translate-table i
9016                         (let ((v (aref caesar-translate-table i)))
9017                           (if (<= v t1) (if (< v t2) v (+ v 47))
9018                             (if (<= v t3) (- v 47) v))))
9019                   (setq i (1+ i))))
9020               (message "Building caesar-translate-table... done")))
9021         (let ((from (region-beginning))
9022               (to (region-end))
9023               (i 0) str len)
9024           (setq str (buffer-substring from to))
9025           (setq len (length str))
9026           (while (< i len)
9027             (aset str i (aref caesar-translate-table (aref str i)))
9028             (setq i (1+ i)))
9029           (goto-char from)
9030           (delete-region from to)
9031           (insert str)))))
9032
9033 \f
9034 ;;;
9035 ;;; Gnus Score File Mode
9036 ;;;
9037
9038 (if gnus-kill-file-mode-map
9039     nil
9040   (setq gnus-kill-file-mode-map (copy-keymap emacs-lisp-mode-map))
9041   (define-key gnus-kill-file-mode-map "\C-c\C-x"
9042     'gnus-kill-file-set-expunge-below)
9043   (define-key gnus-kill-file-mode-map "\C-c@"
9044     'gnus-kill-file-set-mark-below)
9045   (define-key gnus-kill-file-mode-map "\C-c\C-k\C-s"
9046     'gnus-kill-file-temporarily-lower-by-subject)
9047   (define-key gnus-kill-file-mode-map "\C-c\C-k\C-a"
9048     'gnus-kill-file-temporarily-lower-by-author)
9049   (define-key gnus-kill-file-mode-map "\C-c\C-k\C-x"
9050     'gnus-kill-file-temporarily-lower-by-xref)
9051   (define-key gnus-kill-file-mode-map "\C-c\C-ks"
9052     'gnus-kill-file-lower-by-subject)
9053   (define-key gnus-kill-file-mode-map "\C-c\C-ka"
9054     'gnus-kill-file-lower-by-author)
9055   (define-key gnus-kill-file-mode-map "\C-c\C-kt"
9056     'gnus-kill-file-lower-by-thread)
9057   (define-key gnus-kill-file-mode-map "\C-c\C-kx"
9058     'gnus-kill-file-lower-by-xref)
9059   (define-key gnus-kill-file-mode-map "\C-c\C-kf"
9060     'gnus-kill-file-lower-followups-to-author)
9061   (define-key gnus-kill-file-mode-map "\C-c\C-i\C-s"
9062     'gnus-kill-file-temporarily-raise-by-subject)
9063   (define-key gnus-kill-file-mode-map "\C-c\C-i\C-a"
9064     'gnus-kill-file-temporarily-raise-by-author)
9065   (define-key gnus-kill-file-mode-map "\C-c\C-i\C-t"
9066     'gnus-kill-file-temporarily-raise-by-thread)
9067   (define-key gnus-kill-file-mode-map "\C-c\C-i\C-x"
9068     'gnus-kill-file-temporarily-raise-by-xref)
9069   (define-key gnus-kill-file-mode-map "\C-c\C-is"
9070     'gnus-kill-file-raise-by-subject)
9071   (define-key gnus-kill-file-mode-map "\C-c\C-ia"
9072     'gnus-kill-file-raise-by-author)
9073   (define-key gnus-kill-file-mode-map "\C-c\C-ix"
9074     'gnus-kill-file-raise-by-xref)
9075   (define-key gnus-kill-file-mode-map "\C-c\C-if"
9076     'gnus-kill-file-raise-followups-to-author)
9077   (define-key gnus-kill-file-mode-map "\C-c\C-a" 'gnus-kill-file-apply-buffer)
9078   (define-key gnus-kill-file-mode-map "\C-c\C-e" 'gnus-kill-file-apply-last-sexp)
9079   (define-key gnus-kill-file-mode-map "\C-c\C-c" 'gnus-kill-file-exit)
9080   (define-key gnus-kill-file-mode-map "\C-c\C-i" 'gnus-info-find-node))
9081
9082 (defun gnus-kill-file-mode ()
9083   "Major mode for editing score files.
9084
9085 In addition to Emacs-Lisp mode, the following commands are available:
9086
9087 \\[gnus-kill-file-set-expunge-below]    Automatically expunge articles below LEVEL.
9088 \\[gnus-kill-file-set-mark-below]       Automatically mark articles below LEVEL.
9089 \\[gnus-kill-file-temporarily-lower-by-author]  Insert temporary lower command for current author.
9090 \\[gnus-kill-file-temporarily-lower-by-thread]  Insert temporary lower command for current thread.
9091 \\[gnus-kill-file-temporarily-lower-by-xref]            Insert temporary lower command for current cross-posting.
9092 \\[gnus-kill-file-lower-by-subject]     Insert permanent lower command for current subject.
9093 \\[gnus-kill-file-lower-by-author]      Insert permanent lower command for current author.
9094 \\[gnus-kill-file-lower-followups-to-author]    Insert permanent lower command for followups to the current author.
9095 \\[gnus-kill-file-lower-by-xref]                Insert permanent lower command for current cross-posting.
9096 \\[gnus-kill-file-temporarily-raise-by-subject] Insert temporary raise command for current subject.
9097 \\[gnus-kill-file-temporarily-raise-by-author]  Insert temporary raise command for current author.
9098 \\[gnus-kill-file-temporarily-raise-by-thread]  Insert temporary raise command for current thread.
9099 \\[gnus-kill-file-temporarily-raise-by-xref]            Insert temporary raise command for current cross-posting.
9100 \\[gnus-kill-file-raise-by-subject]     Insert permanent raise command for current subject.
9101 \\[gnus-kill-file-raise-by-author]      Insert permanent raise command for current author.
9102 \\[gnus-kill-file-raise-followups-to-author]    Insert permanent raise command for followups to the current author.
9103 \\[gnus-kill-file-raise-by-xref]                Insert permanent raise command for current cross-posting.
9104 \\[gnus-kill-file-apply-buffer] Apply current buffer to selected newsgroup.
9105 \\[gnus-kill-file-apply-last-sexp]      Apply sexp before point to selected newsgroup.
9106 \\[gnus-kill-file-exit] Save file and exit editing score file.
9107 \\[gnus-info-find-node] Read Info about score files.
9108
9109   A score file contains Lisp expressions to be applied to a selected
9110 newsgroup.  The purpose is to mark articles as read on the basis of
9111 some set of regexps.  A global score file is applied to every
9112 newsgroup, and a local score file is applied to a specified newsgroup.
9113 Since a global score file is applied to every newsgroup, for better
9114 performance use a local one.
9115
9116   A score file can contain any kind of Emacs Lisp expressions expected
9117 to be evaluated in the summary buffer.  Writing Lisp programs for this
9118 purpose is not so easy because the internal working of Gnus must be
9119 well-known.  For this reason, Gnus provides a general function which
9120 does this easily for non-Lisp programmers.
9121
9122   The `gnus-kill' function executes commands available in summary mode
9123 by their key sequences. `gnus-kill' should be called with FIELD,
9124 REGEXP and optional COMMAND and ALL.  FIELD is a string representing
9125 the header field or an empty string.  If FIELD is an empty string, the
9126 entire article body is searched for.  REGEXP is a string which is
9127 compared with FIELD value. COMMAND is a string representing a valid
9128 key sequence in summary mode or Lisp expression. COMMAND defaults to
9129 '(gnus-summary-mark-as-read nil \"X\").  Make sure that COMMAND is
9130 executed in the summary buffer.  If the second optional argument ALL
9131 is non-nil, the COMMAND is applied to articles which are already
9132 marked as read or unread.  Articles which are marked are skipped over
9133 by default.
9134
9135   For example, if you want to mark articles of which subjects contain
9136 the string `AI' as read, a possible score file may look like:
9137
9138         (gnus-kill \"Subject\" \"AI\")
9139
9140   If you want to mark articles with `D' instead of `X', you can use
9141 the following expression:
9142
9143         (gnus-kill \"Subject\" \"AI\" \"d\")
9144
9145 In this example it is assumed that the command
9146 `gnus-summary-mark-as-read-forward' is assigned to `d' in summary mode.
9147
9148   It is possible to remove unnecessary headers which are marked with
9149 `X' in a score file as follows:
9150
9151         (gnus-expunge \"X\")
9152
9153   If the summary buffer is empty after applying score files, Gnus will
9154 exit the selected newsgroup normally.  If headers which are marked
9155 with `D' are deleted in a score file, it is impossible to read articles
9156 which are marked as read in the previous Gnus sessions.  Marks other
9157 than `D' should be used for articles which should really be deleted.
9158
9159 Entry to this mode calls emacs-lisp-mode-hook and
9160 gnus-kill-file-mode-hook with no arguments, if that value is non-nil."
9161   (interactive)
9162   (kill-all-local-variables)
9163   (use-local-map gnus-kill-file-mode-map)
9164   (set-syntax-table emacs-lisp-mode-syntax-table)
9165   (setq major-mode 'gnus-kill-file-mode)
9166   (setq mode-name "score-file")
9167   (lisp-mode-variables nil)
9168   (run-hooks 'emacs-lisp-mode-hook 'gnus-kill-file-mode-hook))
9169
9170 (defun gnus-kill-file-edit-file (newsgroup)
9171   "Begin editing a score file for NEWSGROUP.
9172 If NEWSGROUP is nil, the global score file is selected."
9173   (interactive "sNewsgroup: ")
9174   (let ((file (gnus-newsgroup-kill-file newsgroup)))
9175     (gnus-make-directory (file-name-directory file))
9176     ;; Save current window configuration if this is first invocation.
9177     (or (and (get-file-buffer file)
9178              (get-buffer-window (get-file-buffer file)))
9179         (setq gnus-winconf-kill-file (current-window-configuration)))
9180     ;; Hack windows.
9181     (let ((buffer (find-file-noselect file)))
9182       (cond ((get-buffer-window buffer)
9183              (pop-to-buffer buffer))
9184             ((eq major-mode 'gnus-group-mode)
9185              (gnus-configure-windows '(1 0 0)) ;Take all windows.
9186              (pop-to-buffer gnus-group-buffer)
9187              ;; Fix by sachs@SLINKY.CS.NYU.EDU (Jay Sachs).
9188              (let ((gnus-summary-buffer buffer))
9189                (gnus-configure-windows '(1 1 0))) ;Split into two.
9190              (pop-to-buffer buffer))
9191             ((eq major-mode 'gnus-summary-mode)
9192              (gnus-configure-windows 'article)
9193              (pop-to-buffer gnus-article-buffer)
9194              (bury-buffer gnus-article-buffer)
9195              (switch-to-buffer buffer))
9196             (t                          ;No good rules.
9197              (find-file-other-window file))
9198             ))
9199     (gnus-kill-file-mode)
9200     ))
9201
9202 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
9203 (defun gnus-kill-set-kill-buffer ()
9204   (let* ((file (gnus-newsgroup-kill-file gnus-newsgroup-name))
9205          (buffer (find-file-noselect file)))
9206     (set-buffer buffer)
9207     (gnus-kill-file-mode)
9208     (bury-buffer buffer)))
9209
9210 (defun gnus-kill-save-kill-buffer ()
9211   (save-excursion
9212     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
9213       (if (get-file-buffer file)
9214           (progn
9215             (set-buffer (get-file-buffer file))
9216             (and (buffer-modified-p) (save-buffer))
9217             (kill-buffer (current-buffer)))))))
9218
9219 (defun gnus-article-fetch-field (field)
9220   (save-excursion
9221     (set-buffer gnus-article-buffer)
9222     (save-restriction
9223       (widen)
9224       (goto-char 1)
9225       (narrow-to-region 1 (save-excursion 
9226                             (search-forward "\n\n" nil t) (point)))
9227       (goto-char 1)
9228       (prog1
9229           (mail-fetch-field field)
9230         (widen)))))
9231
9232 (defun gnus-kill-file-enter-kill (field regexp level date edit)
9233   ;; Enter score file entry.
9234   ;; FIELD: String containing the name of the header field to score.
9235   ;; REGEXP: The string to score.
9236   ;; LEVEL: How much to raise the score by.
9237   ;; DATE: A date string for expire score or nil for permanent kills.
9238   ;; EDIT: Allow the user to edit REGEXP iff non-nil.
9239   (save-excursion
9240     (gnus-kill-set-kill-buffer)
9241     (goto-char (point-min))
9242     (let ((regexp 
9243            (if edit (read-string 
9244                      (format "Add %d to articles with %s matching: " 
9245                              level (downcase field))
9246                      regexp)
9247              regexp))
9248           entry string kill beg)
9249       (setq entry (if date (cons regexp date) regexp)
9250             string (format "(gnus-raise %S (quote %S) %S)\n"
9251                            field entry level))
9252       (while (and (setq beg (point))
9253                   (condition-case nil
9254                       (setq kill (read (current-buffer)))
9255                     (error nil))
9256                   (or (not (eq (nth 0 kill) 'gnus-raise))
9257                       (not (string= (downcase (nth 1 kill)) (downcase field)))
9258                       (not (eq (nth 3 kill) level))))
9259         (setq kill nil))
9260       (if (not kill)
9261           (progn
9262             (goto-char (point-min))
9263             (insert string))
9264         (let ((list (nth 2 kill)))
9265           (if (and (listp list) (eq 'quote (car list)))
9266               (setq list (car (cdr list))))
9267           (setcar 
9268            (nthcdr 2 kill) 
9269            (if (and (listp list) (listp (cdr list)))
9270                (list 'quote (cons entry list))
9271              (list 'quote (list entry list)))))
9272         (delete-region beg (point))
9273         (insert (gnus-pp-gnus-kill kill)))
9274       (gnus-kill-file-apply-string string))
9275     ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
9276     (or edit 
9277         (message "Added kill file entry %s: %s" (downcase field) regexp))))
9278     
9279 (defun gnus-kill-file-set-variable (symbol value)
9280    ;; Set SYMBOL to VALUE in the score file.
9281    (save-excursion
9282      (gnus-kill-set-kill-buffer)
9283      (goto-char (point-min))
9284      (let ((string (format "(setq %S %S)\n" symbol value))
9285            kill beg)
9286        (while (and (setq beg (point))
9287                    (condition-case nil
9288                        (setq kill (read (current-buffer)))
9289                      (error nil))
9290                    (or (not (eq (nth 0 kill) 'setq))
9291                        (not (eq (nth 1 kill) symbol))))
9292          (setq kill nil))
9293        (if (not kill)
9294            (progn
9295              (goto-char (point-min))
9296              (insert string))
9297          (delete-region beg (point))
9298          (insert string)))))
9299     
9300 (defun gnus-kill-file-set-expunge-below (level)
9301    "Automatically expunge articles with score below LEVEL."
9302    (interactive "P")
9303    (setq level (if level
9304                   (prefix-numeric-value level)
9305                 gnus-summary-default-score))
9306    (if (eq major-mode 'gnus-summary-mode)
9307        (progn
9308          (gnus-score-set 'expunge level)
9309          (gnus-score-set 'touched t))
9310      (gnus-kill-file-set-variable 'expunge-below level))
9311    (message "Set expunge below level to %d." level))
9312
9313  (defun gnus-kill-file-set-mark-below (level)
9314    "Automatically mark articles with score below LEVEL as read."
9315    (interactive "P")
9316    (setq level (if level
9317                    (prefix-numeric-value level)
9318                  gnus-summary-default-score))
9319    (if (eq major-mode 'gnus-summary-mode)
9320        (progn
9321          (gnus-score-set 'mark level)
9322          (gnus-score-set 'touched t)
9323          (gnus-summary-set-mark-below level))
9324      (gnus-kill-file-set-variable 'mark-below level))
9325    (message "Set mark below level to %d." level))
9326  
9327  (defun gnus-kill-file-temporarily-raise-by-subject (level &optional header)
9328    "Temporarily raise score by LEVEL for current subject.
9329  See `gnus-kill-expiry-days'."
9330    (interactive "p")
9331    (gnus-kill-file-raise-by-subject level header (current-time-string)))
9332   
9333  (defun gnus-kill-file-temporarily-raise-by-author (level &optional header)
9334    "Temporarily raise score by LEVEL for current author.
9335  See `gnus-kill-expiry-days'."
9336    (interactive "p")
9337    (gnus-kill-file-raise-by-author level header (current-time-string)))
9338   
9339  (defun gnus-kill-file-temporarily-raise-by-thread (level &optional header)
9340    "Temporarily raise score by LEVEL for current thread.
9341  See `gnus-kill-expiry-days'."
9342    (interactive "p")
9343    (gnus-kill-file-enter-kill 
9344     "References"
9345     (regexp-quote (header-id (or header gnus-current-headers)))
9346     level
9347     (current-time-string)
9348     nil))
9349   
9350  (defun gnus-kill-file-temporarily-raise-by-xref (level &optional header)
9351    "Insert temporary score commands for articles that have been crossposted.
9352  By default use the current crossposted groups.
9353  See `gnus-kill-expiry-days'."
9354    (interactive "p")
9355    (gnus-kill-file-raise-by-xref level header (current-time-string)))
9356   
9357  (defun gnus-kill-file-raise-by-subject (level &optional header date)
9358    "Raise score by LEVEL for current subject."
9359    (interactive "p")
9360    (gnus-kill-file-enter-kill
9361     "Subject"
9362     (regexp-quote 
9363      (gnus-simplify-subject 
9364       (header-subject (or header gnus-current-headers))))
9365     level
9366     date
9367     t))
9368   
9369  (defun gnus-kill-file-raise-by-author (level &optional header date)
9370    "Raise score by LEVEL for current author."
9371    (interactive "p")
9372    (gnus-kill-file-enter-kill
9373     "From"
9374     (regexp-quote (header-from (or header gnus-current-headers)))
9375     level
9376     date
9377     t))
9378  
9379  (defun gnus-kill-file-raise-by-xref (level &optional header date)
9380    "Raise score by LEVEL for articles that have been crossposted.
9381  By default use the current crossposted groups."
9382    (interactive "p")
9383    (let ((xref (header-xref (or header gnus-current-headers)))
9384          (start 0)
9385          group)
9386      (if xref
9387          (while (string-match " \\([^ \t]+\\):" xref start)
9388            (setq start (match-end 0))
9389            (if (not (string= 
9390                      (setq group 
9391                            (substring xref (match-beginning 1) (match-end 1)))
9392                      gnus-newsgroup-name))
9393                (gnus-kill-file-enter-kill 
9394                 "Xref"
9395                 (concat " " (regexp-quote group) ":")
9396                 level
9397                 date
9398                 t))))))
9399
9400 (defun gnus-kill-file-raise-followups-to-author
9401   (level &optional header)
9402   "Raise score for all followups to the current author."
9403   (interactive)
9404   (let ((name (header-from (or header gnus-current-headers)))
9405         (string))
9406     (save-excursion
9407       (gnus-kill-set-kill-buffer)
9408       (goto-char (point-min))
9409       (setq name (read-string (concat "Add " level
9410                                       " to followup articles to: ")
9411                               (regexp-quote name)))
9412       (setq string
9413             (format "(gnus-kill %S %S '(gnus-summary-temporarily-raise-by-thread %S))\n"
9414                     "From" name level))
9415       (insert string)
9416       (gnus-kill-file-apply-string string))
9417     (message "Added permanent score file entry for followups to %s." name)))
9418
9419 (defun gnus-kill-file-temporarily-lower-by-subject (level &optional header)
9420   "Temporarily lower score by LEVEL for current subject.
9421 See `gnus-kill-expiry-days'."
9422   (interactive "p")
9423   (gnus-kill-file-lower-by-subject level header (current-time-string)))
9424
9425 (defun gnus-kill-file-temporarily-lower-by-author (level &optional header)
9426   "Temporarily lower score by LEVEL for current author.
9427 See `gnus-kill-expiry-days'."
9428   (interactive "p")
9429   (gnus-kill-file-lower-by-author level header (current-time-string)))
9430
9431 (defun gnus-kill-file-temporarily-lower-by-thread (level &optional header)
9432   "Temporarily lower score by LEVEL for current thread.
9433 See `gnus-kill-expiry-days'."
9434   (interactive "p")
9435   (gnus-kill-file-temporarily-raise-by-thread (- (gnus-score-default level)) header))
9436
9437 (defun gnus-kill-file-temporarily-lower-by-xref (level &optional header)
9438   "Insert temporary score commands for articles that have been crossposted.
9439 By default use the current crossposted groups.
9440 See `gnus-kill-expiry-days'."
9441   (interactive "p")
9442   (gnus-kill-file-lower-by-xref level header (current-time-string)))
9443
9444 (defun gnus-kill-file-lower-by-subject (level &optional header date)
9445     "Lower score by LEVEL for current subject."
9446   (interactive "p")
9447   (gnus-kill-file-raise-by-subject (- (gnus-score-default level)) header date))
9448
9449 (defun gnus-kill-file-lower-by-author (level &optional header date)
9450   "Lower score by LEVEL for current author."
9451   (interactive "p")
9452   (gnus-kill-file-raise-by-author (- (gnus-score-default level)) header date))
9453
9454 (defun gnus-kill-file-lower-by-xref (level &optional header date)
9455   "Lower score by LEVEL for articles that have been crossposted.
9456 By default use the current crossposted groups."
9457   (gnus-kill-file-raise-by-xref (- (gnus-score-default level)) header date))
9458
9459 (defun gnus-kill-file-lower-followups-to-author
9460   (level &optional header)
9461   "Lower score for all followups to the current author."
9462   (interactive "p")
9463   (gnus-kill-file-raise-followups-to-author (- (gnus-score-default level)) header))
9464
9465 (defun gnus-kill-file-apply-buffer ()
9466   "Apply current buffer to current newsgroup."
9467   (interactive)
9468   (if (and gnus-current-kill-article
9469            (get-buffer gnus-summary-buffer))
9470       ;; Assume newsgroup is selected.
9471       (gnus-kill-file-apply-string (buffer-string))
9472     (ding) (message "No newsgroup is selected.")))
9473
9474 (defun gnus-kill-file-apply-string (string)
9475   "Apply STRING to current newsgroup."
9476   (interactive)
9477   (let ((string (concat "(progn \n" string "\n)" )))
9478     (save-excursion
9479       (save-window-excursion
9480         (pop-to-buffer gnus-summary-buffer)
9481         (eval (car (read-from-string string)))))))
9482
9483 (defun gnus-kill-file-apply-last-sexp ()
9484   "Apply sexp before point in current buffer to current newsgroup."
9485   (interactive)
9486   (if (and gnus-current-kill-article
9487            (get-buffer gnus-summary-buffer))
9488       ;; Assume newsgroup is selected.
9489       (let ((string
9490              (buffer-substring
9491               (save-excursion (forward-sexp -1) (point)) (point))))
9492         (save-excursion
9493           (save-window-excursion
9494             (pop-to-buffer gnus-summary-buffer)
9495             (eval (car (read-from-string string))))))
9496     (ding) (message "No newsgroup is selected.")))
9497
9498 (defun gnus-kill-file-exit ()
9499   "Save a score file, then return to the previous buffer."
9500   (interactive)
9501   (save-buffer)
9502   (let ((killbuf (current-buffer)))
9503     ;; We don't want to return to article buffer.
9504     (and (get-buffer gnus-article-buffer)
9505          (bury-buffer gnus-article-buffer))
9506     ;; Delete the KILL file windows.
9507     (delete-windows-on killbuf)
9508     ;; Restore last window configuration if available.
9509     (and gnus-winconf-kill-file
9510          (set-window-configuration gnus-winconf-kill-file))
9511     (setq gnus-winconf-kill-file nil)
9512     ;; Kill the KILL file buffer.  Suggested by tale@pawl.rpi.edu.
9513     (kill-buffer killbuf)))
9514
9515 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
9516
9517 (defalias 'gnus-batch-kill 'gnus-batch-score)
9518 ;;;###autoload
9519 (defun gnus-batch-score ()
9520   "Run batched scoring.
9521 Usage: emacs -batch -l gnus -f gnus-batch-kill <newsgroups> ...
9522 Newsgroups is a list of strings in Bnews format.  If you want to score
9523 the comp hierarchy, you'd say \"comp.all\". If you would not like to
9524 score the alt hierarchy, you'd say \"!alt.all\"."
9525   (interactive)
9526   (let* ((yes-and-no
9527           (gnus-parse-n-options
9528            (apply (function concat)
9529                   (mapcar (lambda (g) (concat g " "))
9530                           command-line-args-left))))
9531          (gnus-expert-user t)
9532          (nnmail-spool-file nil)
9533          (gnus-use-dribble-file nil)
9534          (yes (car yes-and-no))
9535          (no (cdr yes-and-no))
9536          group subscribed newsrc entry
9537          ;; Disable verbose message.
9538          gnus-novice-user gnus-large-newsgroup)
9539     ;; Eat all arguments.
9540     (setq command-line-args-left nil)
9541     ;; Start Gnus.
9542     (gnus)
9543     ;; Apply kills to specified newsgroups in command line arguments.
9544     (setq newsrc (cdr gnus-newsrc-assoc))
9545     (while newsrc
9546       (setq group (car (car newsrc)))
9547       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
9548       (if (and (<= (nth 1 (car newsrc)) 5)
9549                (and (car entry)
9550                     (or (eq (car entry) t)
9551                         (not (zerop (car entry)))))
9552                (if yes (string-match yes group) t)
9553                (or (null no) (not (string-match no group))))
9554           (progn
9555             (gnus-summary-read-group group nil t)
9556             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
9557                  (gnus-summary-exit))))
9558       (setq newsrc (cdr newsrc)))
9559     ;; Exit Emacs.
9560     (switch-to-buffer gnus-group-buffer)
9561     (gnus-group-save-newsrc)))
9562
9563 ;; For score files
9564
9565 (defun gnus-Newsgroup-kill-file (newsgroup)
9566   "Return the name of a score file for NEWSGROUP.
9567 If NEWSGROUP is nil, return the global score file instead."
9568   (cond ((or (null newsgroup)
9569              (string-equal newsgroup ""))
9570          ;; The global score file is placed at top of the directory.
9571          (expand-file-name gnus-kill-file-name
9572                            (or gnus-kill-files-directory "~/News")))
9573         (gnus-use-long-file-name
9574          ;; Append ".KILL" to capitalized newsgroup name.
9575          (expand-file-name (concat (gnus-capitalize-newsgroup newsgroup)
9576                                    "." gnus-kill-file-name)
9577                            (or gnus-kill-files-directory "~/News")))
9578         (t
9579          ;; Place "KILL" under the hierarchical directory.
9580          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
9581                                    "/" gnus-kill-file-name)
9582                            (or gnus-kill-files-directory "~/News")))
9583         ))
9584
9585 (defun gnus-newsgroup-kill-file (newsgroup)
9586   "Return the name of a score file name for NEWSGROUP.
9587 If NEWSGROUP is nil, return the global score file name instead."
9588   (cond ((or (null newsgroup)
9589              (string-equal newsgroup ""))
9590          ;; The global KILL file is placed at top of the directory.
9591          (expand-file-name gnus-kill-file-name
9592                            (or gnus-kill-files-directory "~/News")))
9593         (gnus-use-long-file-name
9594          ;; Append ".KILL" to newsgroup name.
9595          (expand-file-name (concat newsgroup "." gnus-kill-file-name)
9596                            (or gnus-kill-files-directory "~/News")))
9597         (t
9598          ;; Place "KILL" under the hierarchical directory.
9599          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
9600                                    "/" gnus-kill-file-name)
9601                            (or gnus-kill-files-directory "~/News")))
9602         ))
9603
9604
9605 (defalias 'gnus-expunge 'gnus-summary-remove-lines-marked-with)
9606
9607 (defun gnus-apply-kill-file ()
9608   "Apply a score file to the current newsgroup.
9609 Returns the number of articles marked as read."
9610   (let* ((kill-files (list (gnus-newsgroup-kill-file nil)
9611                            (gnus-newsgroup-kill-file gnus-newsgroup-name)))
9612          (unreads (length gnus-newsgroup-unreads))
9613          (gnus-summary-inhibit-highlight t)
9614          (mark-below (or gnus-summary-mark-below gnus-summary-default-score))
9615          (expunge-below gnus-summary-expunge-below)
9616          form beg)
9617     (setq gnus-newsgroup-kill-headers nil)
9618     (or gnus-newsgroup-headers-hashtb-by-number
9619         (gnus-make-headers-hashtable-by-number))
9620     ;; If there are any previously scored articles, we remove these
9621     ;; from the `gnus-newsgroup-headers' list that the score functions
9622     ;; will see. This is probably pretty wasteful when it comes to
9623     ;; conses, but is, I think, faster than having to assq in every
9624     ;; single score funtion.
9625     (let ((files kill-files))
9626       (while files
9627         (if (file-exists-p (car files))
9628             (let ((headers gnus-newsgroup-headers))
9629               (if gnus-kill-killed
9630                   (setq gnus-newsgroup-kill-headers
9631                         (mapcar (lambda (header) (header-number header))
9632                                 headers))
9633                 (while headers
9634                   (or (gnus-member-of-range 
9635                        (header-number (car headers)) 
9636                        gnus-newsgroup-killed)
9637                       (setq gnus-newsgroup-kill-headers 
9638                             (cons (header-number (car headers))
9639                                   gnus-newsgroup-kill-headers)))
9640                   (setq headers (cdr headers))))
9641               (setq files nil))
9642           (setq files (cdr files)))))
9643     (if gnus-newsgroup-kill-headers
9644         (save-excursion
9645           (while kill-files
9646             (if (file-exists-p (car kill-files))
9647                 (progn
9648                   (message "Processing kill file %s..." (car kill-files))
9649                   (find-file (car kill-files))
9650                   (goto-char (point-min))
9651                   (while (progn
9652                            (setq beg (point))
9653                            (setq form (condition-case nil 
9654                                           (read (current-buffer)) 
9655                                         (error nil))))
9656                     (if (or (eq (car form) 'gnus-kill)
9657                             (eq (car form) 'gnus-raise)
9658                             (eq (car form) 'gnus-lower))
9659                         (progn
9660                           (delete-region beg (point))
9661                           (insert (or (eval form) "")))
9662                       (eval form)))
9663                   (and (buffer-modified-p) (save-buffer))
9664                   (message "Processing kill file %s...done" (car kill-files))))
9665             (setq kill-files (cdr kill-files)))))
9666     (if expunge-below (gnus-summary-expunge-below expunge-below))
9667     (let (gnus-summary-inhibit-highlight)
9668       (gnus-summary-set-mark-below mark-below))
9669     (if beg
9670         (let ((nunreads (- unreads (length gnus-newsgroup-unreads))))
9671           (or (eq nunreads 0)
9672               (message "Marked %d articles as read" nunreads))
9673           nunreads)
9674       0)))
9675
9676 ;; Kill changes and new format by suggested by JWZ and Sudish Joseph
9677 ;; <joseph@cis.ohio-state.edu>.  
9678 (defun gnus-kill (field regexp &optional exe-command all)
9679   "If FIELD of an article matches REGEXP, execute COMMAND.
9680 Optional 1st argument COMMAND is default to
9681         (gnus-summary-mark-as-read nil \"X\").
9682 If optional 2nd argument ALL is non-nil, articles marked are also applied to.
9683 If FIELD is an empty string (or nil), entire article body is searched for.
9684 COMMAND must be a lisp expression or a string representing a key sequence."
9685   ;; We don't want to change current point nor window configuration.
9686   (save-excursion
9687     (save-window-excursion
9688       ;; Selected window must be summary buffer to execute keyboard
9689       ;; macros correctly. See command_loop_1.
9690       (switch-to-buffer gnus-summary-buffer 'norecord)
9691       (goto-char (point-min))           ;From the beginning.
9692       (let ((kill-list regexp)
9693             (date (current-time-string))
9694             (command (or exe-command '(gnus-summary-mark-as-read 
9695                                        nil gnus-kill-file-mark)))
9696             kill kdate prev)
9697         (if (listp kill-list)
9698             ;; It is a list.
9699             (if (not (consp (cdr kill-list)))
9700                 ;; It's on the form (regexp . date).
9701                 (if (zerop (gnus-execute field (car kill-list) 
9702                                          command nil (not all)))
9703                     (if (> (gnus-days-between date (cdr kill-list))
9704                            gnus-kill-expiry-days)
9705                         (setq regexp nil))
9706                   (setcdr kill-list date))
9707               (while (setq kill (car kill-list))
9708                 (if (consp kill)
9709                     ;; It's a temporary kill.
9710                     (progn
9711                       (setq kdate (cdr kill))
9712                       (if (zerop (gnus-execute 
9713                                   field (car kill) command nil (not all)))
9714                           (if (> (gnus-days-between date kdate)
9715                                  gnus-kill-expiry-days)
9716                               ;; Time limit has been exceeded, so we
9717                               ;; remove the match.
9718                               (if prev
9719                                   (setcdr prev (cdr kill-list))
9720                                 (setq regexp (cdr regexp))))
9721                         ;; Successful kill. Set the date to today.
9722                         (setcdr kill date)))
9723                   ;; It's a permanent kill.
9724                   (gnus-execute field kill command nil (not all)))
9725                 (setq prev kill-list)
9726                 (setq kill-list (cdr kill-list))))
9727           (gnus-execute field kill-list command nil (not all)))
9728         )))
9729   (if regexp
9730       (gnus-pp-gnus-kill
9731        (nconc (list 'gnus-kill field 
9732                     (if (consp regexp) (list 'quote regexp) regexp))
9733               (if (or exe-command all) (list (list 'quote exe-command)))
9734               (if all (list t) nil)))))
9735
9736 (defun gnus-pp-gnus-kill (object)
9737   (if (or (not (consp (nth 2 object)))
9738           (not (consp (cdr (nth 2 object))))
9739           (and (eq 'quote (car (nth 2 object)))
9740                (not (consp (cdr (car (cdr (nth 2 object))))))))
9741       (concat "\n" (prin1-to-string object))
9742     (save-excursion
9743       (set-buffer (get-buffer-create "*Gnus PP*"))
9744       (buffer-disable-undo (current-buffer))
9745       (erase-buffer)
9746       (insert (format "\n(%S %S\n  '(" (nth 0 object) (nth 1 object)))
9747       (let ((klist (car (cdr (nth 2 object))))
9748             (first t))
9749         (while klist
9750           (insert (if first (progn (setq first nil) "")  "\n    ")
9751                   (prin1-to-string (car klist)))
9752           (setq klist (cdr klist))))
9753       (insert ")")
9754       (and (nth 3 object)
9755            (insert "\n  " 
9756                    (if (and (consp (nth 3 object))
9757                             (not (eq 'quote (car (nth 3 object))))) 
9758                        "'" "")
9759                    (prin1-to-string (nth 3 object))))
9760       (and (nth 4 object)
9761            (insert "\n  t"))
9762       (insert ")")
9763       (prog1
9764           (buffer-substring (point-min) (point-max))
9765         (kill-buffer (current-buffer))))))
9766
9767 (defun gnus-execute-1 (function regexp form header)
9768   (save-excursion
9769     (let (did-kill)
9770       (if (null header)
9771           nil                           ;Nothing to do.
9772         (if function
9773             ;; Compare with header field.
9774             (let (value)
9775               (and header
9776                    (progn
9777                      (setq value (funcall function header))
9778                      ;; Number (Lines:) or symbol must be converted to string.
9779                      (or (stringp value)
9780                          (setq value (prin1-to-string value)))
9781                      (setq did-kill (string-match regexp value)))
9782                    (if (stringp form)   ;Keyboard macro.
9783                        (execute-kbd-macro form)
9784                      (funcall form))))
9785           ;; Search article body.
9786           (let ((gnus-current-article nil) ;Save article pointer.
9787                 (gnus-last-article nil)
9788                 (gnus-break-pages nil)  ;No need to break pages.
9789                 (gnus-mark-article-hook nil)) ;Inhibit marking as read.
9790             (message "Searching for article: %d..." (header-number header))
9791             (gnus-article-setup-buffer)
9792             (gnus-article-prepare (header-number header) t)
9793             (if (save-excursion
9794                   (set-buffer gnus-article-buffer)
9795                   (goto-char (point-min))
9796                   (setq did-kill (re-search-forward regexp nil t)))
9797                 (if (stringp form)      ;Keyboard macro.
9798                     (execute-kbd-macro form)
9799                   (funcall form))))))
9800       did-kill)))
9801
9802 (defun gnus-execute (field regexp form &optional backward ignore-marked)
9803   "If FIELD of article header matches REGEXP, execute lisp FORM (or a string).
9804 If FIELD is an empty string (or nil), entire article body is searched for.
9805 If optional 1st argument BACKWARD is non-nil, do backward instead.
9806 If optional 2nd argument IGNORE-MARKED is non-nil, articles which are
9807 marked as read or ticked are ignored."
9808   (save-excursion
9809     (let ((killed-no 0)
9810           function header article)
9811       (if (or (null field) (string-equal field ""))
9812           (setq field nil)
9813         ;; Get access function of header filed.
9814         (setq function (intern-soft (concat "gnus-header-" (downcase field))))
9815         (if (and function (fboundp function))
9816             (setq function (symbol-function function))
9817           (error "Unknown header field: \"%s\"" field))
9818         ;; Make FORM funcallable.
9819         (if (and (listp form) (not (eq (car form) 'lambda)))
9820             (setq form (list 'lambda nil form)))
9821         ;; Starting from the current article.
9822         (while (or (and (not article)
9823                         (setq article (gnus-summary-article-number))
9824                         t)
9825                    (setq article 
9826                          (gnus-summary-search-subject 
9827                           backward (not ignore-marked))))
9828           (and (memq article gnus-newsgroup-kill-headers)
9829                (gnus-execute-1 function regexp form 
9830                                (gnus-get-header-by-number article))
9831                (setq killed-no (1+ killed-no)))))
9832       killed-no)))
9833
9834 \f
9835 ;;; Gnus Score File
9836
9837 ;; All score code written by Per Abrahamsen <abraham@iesd.auc.dk>.
9838
9839 (defun gnus-score-default (level)
9840   (if level (prefix-numeric-value level) 
9841     gnus-score-interactive-default-score))
9842
9843 (defun gnus-score-set (symbol value &optional alist)
9844   ;; Set SYMBOL to VALUE in ALIST.
9845   (let* ((local-score (gnus-score-file-name gnus-newsgroup-name))
9846          (alist 
9847           (or alist 
9848               (setq gnus-score-alist 
9849                     (cdr (assoc local-score gnus-score-cache)))
9850               (progn
9851                 (gnus-score-load local-score)
9852                 gnus-score-alist)))
9853          (entry (assoc symbol alist)))
9854     (cond (entry
9855            (setcdr entry value))
9856           ((null alist)
9857            (error "Empty alist"))
9858           (t
9859            (setcdr alist (cons (cons symbol value) (cdr alist)))))))
9860
9861 (defun gnus-score-get (symbol &optional alist)
9862   ;; Get SYMBOL's definition in ALIST.
9863   (cdr (assoc symbol 
9864               (or alist 
9865 ;                 (setq gnus-score-alist 
9866 ;                       (cdr (assoc (gnus-score-file-name gnus-newsgroup-name)
9867 ;                                   gnus-score-cache))
9868                   
9869                   (progn
9870                     (gnus-score-load 
9871                      (gnus-score-file-name gnus-newsgroup-name))
9872                     gnus-score-alist)))))
9873
9874 (defun gnus-score-edit-file (group)
9875   "Edit score file for GROUP."
9876   (interactive (list (read-string "Edit SCORE file for: "
9877                                   (cons (or gnus-newsgroup-name "") 1))))
9878   (and (get-buffer gnus-summary-buffer) (gnus-score-save))
9879   (find-file (gnus-score-file-name group))
9880   (emacs-lisp-mode))
9881
9882 (defun gnus-score-load-file (file)
9883   ;; Load score file FILE. Updates `gnus-scores-lists'.
9884   (let ((cache (assoc file gnus-score-cache)))
9885     (if cache
9886         (progn
9887           (setq gnus-score-alist (cdr cache))
9888           (if (memq t (mapcar (lambda (e) (stringp (car e))) gnus-score-alist))
9889               (setq gnus-scores-lists 
9890                     (cons gnus-score-alist gnus-scores-lists))))
9891       (setq gnus-score-alist nil)
9892       (load file t nil t)
9893       (if (memq t (mapcar (lambda (e) (stringp (car e))) gnus-score-alist))
9894           (setq gnus-scores-lists (cons gnus-score-alist gnus-scores-lists)))
9895       (or gnus-score-alist
9896           (setq gnus-score-alist (copy-alist '((touched . nil)))))
9897       (setq gnus-score-cache
9898             (cons (cons file gnus-score-alist) gnus-score-cache))))
9899   (let ((mark (gnus-score-get 'mark))
9900         (expunge (gnus-score-get 'expunge))
9901         (files (gnus-score-get 'files))
9902         (eval (gnus-score-get 'eval)))
9903     (if files (mapcar (lambda (file) (gnus-score-load-file file)) files))
9904     (if eval (eval eval))
9905     (if mark (setq gnus-summary-mark-below mark))
9906     (if expunge (setq gnus-summary-expunge-below expunge))))
9907
9908 (defun gnus-score-load (file)
9909   ;; Load score FILE.
9910   ;; Updates free variables `gnus-score-alist' and `scores'.
9911
9912   (let ((cache (assoc file gnus-score-cache)))
9913     (if cache
9914         (setq gnus-score-alist (cdr cache))
9915       (setq gnus-score-alist nil)
9916       (load file t nil t)
9917       (or gnus-score-alist
9918           (setq gnus-score-alist (copy-alist '((touched . nil)))))
9919       (setq gnus-score-cache
9920             (cons (cons file gnus-score-alist) gnus-score-cache)))))
9921   
9922 (defun gnus-score-save ()
9923   ;; Save all SCORE information.
9924   (let (cache)
9925     (save-excursion
9926       (set-buffer gnus-summary-buffer)
9927       (setq cache gnus-score-cache
9928             gnus-score-cache nil))
9929     (save-excursion
9930       (setq gnus-score-alist nil)
9931       (set-buffer (get-buffer-create "*Score*"))
9932       (buffer-disable-undo (current-buffer))
9933       (let (entry score file)
9934         (while cache
9935           (setq entry (car cache)
9936                 cache (cdr cache)
9937                 file (car entry)
9938                 score (cdr entry))
9939           (if (null (gnus-score-get 'touched score))
9940               ()
9941             (gnus-score-set 'touched nil score)
9942             (erase-buffer)
9943             (let (emacs-lisp-mode-hook)
9944               (pp (list 'setq 'gnus-score-alist (list 'quote score))
9945                   (current-buffer)))
9946             (make-directory (file-name-directory file) t)
9947               (write-region (point-min) (point-max) file nil 'silent))))
9948       (kill-buffer (current-buffer)))))
9949   
9950 (defun gnus-score-headers ()
9951   ;; Score `gnus-newsgroup-headers'.
9952   (let ((score-files (and gnus-score-find-score-files-function
9953                           (fboundp gnus-score-find-score-files-function)
9954                           (funcall gnus-score-find-score-files-function
9955                                    gnus-newsgroup-name)))
9956         scores)
9957     ;; Load the SCORE files.
9958     (while score-files
9959       (gnus-score-load-file (car score-files))
9960       (setq score-files (cdr score-files)))
9961     (setq scores gnus-scores-lists
9962           gnus-scores-lists nil)
9963     (if (not (and gnus-summary-default-score
9964                   scores
9965                   (> (length gnus-newsgroup-headers)
9966                      (length gnus-newsgroup-scored))))
9967         ()
9968       (let* ((entries gnus-header-index)
9969              (now (current-time-string))
9970              (expire (- (gnus-useful-date now) gnus-kill-expiry-days))
9971              (headers gnus-newsgroup-headers)
9972              entry header)
9973         (message "Scoring...")
9974         ;; Create articles, an alist of the form `(HEADER . SCORE)'.
9975         (while headers
9976           (setq header (car headers)
9977                 headers (cdr headers))
9978           ;; WARNING: The assq makes the function O(N*S) while it could
9979           ;; be written as O(N+S), where N is (length gnus-newsgroup-headers)
9980           ;; and S is (length gnus-newsgroup-scored).
9981           (or (assq (header-number header) gnus-newsgroup-scored)
9982               (setq gnus-scores-articles       ;Total of 2 * N cons-cells used.
9983                     (cons (cons header gnus-summary-default-score)
9984                           gnus-scores-articles))))
9985   
9986         (save-excursion
9987           (set-buffer (get-buffer-create "*Headers*"))
9988           (buffer-disable-undo (current-buffer))
9989           ;; Run each header through the score process.
9990           (while entries
9991             (setq entry (car entries)
9992                   header (nth 0 entry)
9993                   entries (cdr entries))
9994             (setq gnus-score-index (nth 1 (assoc header gnus-header-index)))
9995             (if (< 0 (apply 'max (mapcar
9996                                   (lambda (score)
9997                                     (length (gnus-score-get header score)))
9998                                   scores)))
9999                 (funcall (nth 2 entry) scores header now expire)))
10000           ;; Remove the buffer.
10001           (kill-buffer (current-buffer)))
10002
10003         (message "Scoring...")
10004         ;; Add articles to `gnus-newsgroup-scored'.
10005         (while gnus-scores-articles
10006           (or (= gnus-summary-default-score (cdr (car gnus-scores-articles)))
10007               (setq gnus-newsgroup-scored
10008                     (cons (cons (header-number 
10009                                  (car (car gnus-scores-articles)))
10010                                 (cdr (car gnus-scores-articles)))
10011                           gnus-newsgroup-scored)))
10012           (setq gnus-scores-articles (cdr gnus-scores-articles)))
10013
10014         (message "Scoring...done")))))
10015
10016 ;(defun gnus-score-integer (scores header now expire)
10017 ;  )
10018
10019 ;(defun gnus-score-date (scores header now expire)
10020 ;  )
10021
10022 (defun gnus-score-string (scores header now expire)
10023   ;; Score ARTICLES according to HEADER in SCORES.
10024   ;; Update matches entries to NOW and remove unmatched entried older
10025   ;; than EXPIRE.
10026   
10027   ;; Insert the unique article headers in the buffer.
10028   (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
10029         ;; gnus-score-index is used as a free variable.
10030         alike last this art entries alist articles)
10031
10032     ;; Sorting the articles costs os O(N*log N) but will allow us to
10033     ;; only match with each unique header.  Thus the actual matching
10034     ;; will be O(M*U) where M is the number of strings to match with,
10035     ;; and U is the number of unique headers.  It is assumed (but
10036     ;; untested) this will be a net win because of the large constant
10037     ;; factor involved with string matching.
10038     (setq gnus-scores-articles (sort gnus-scores-articles 'gnus-score-string<)
10039           articles gnus-scores-articles)
10040
10041     (erase-buffer)
10042     (while articles
10043       (setq art (car articles)
10044             this (aref (car art) gnus-score-index)
10045             articles (cdr articles))
10046       (if (equal last this)
10047           ;; O(N*H) cons-cells used here, where H is the number of
10048           ;; headers.
10049           (setq alike (cons art alike))
10050         (if last
10051             (progn
10052               ;; Insert the line, with a text property on the
10053               ;; terminating newline refering to the articles with
10054               ;; this line.
10055               (insert last ?\n)
10056               (put-text-property (1- (point)) (point) 'articles alike)))
10057         (setq alike (list art)
10058               last this)))
10059     (and last                           ; Bwadr, duplicate code.
10060          (progn
10061            (insert last ?\n)                    
10062            (put-text-property (1- (point)) (point) 'articles alike)))
10063   
10064     ;; Find matches.
10065     (while scores
10066       (setq alist (car scores)
10067             scores (cdr scores)
10068             entries (assoc header alist))
10069       (while (cdr entries)              ;First entry is the header index.
10070         (let* ((rest (cdr entries))             
10071                (kill (car rest))
10072                (match (nth 0 kill))
10073                (type (nth 1 kill))
10074                (score (nth 2 kill))
10075                (date (nth 3 kill))
10076                (found nil)
10077                (case-fold-search t)
10078                arts art)
10079           (goto-char (point-min))
10080           (while (if type
10081                      (re-search-forward match nil t)
10082                    (search-forward match nil t))
10083             (end-of-line 1)
10084             (setq found t
10085                   arts (get-text-property (point) 'articles))
10086             ;; Found a match, update scores.
10087             (while arts
10088               (setq art (car arts)
10089                     arts (cdr arts))
10090               (setcdr art (+ score (cdr art)))))
10091           ;; Update expire date
10092           (cond ((null date))           ;Permanent entry.
10093                 (found                  ;Match, update date.
10094                  (gnus-score-set 'touched t alist)
10095                  (setcar (nthcdr 3 kill) now))
10096                 ((< (gnus-useful-date date) expire) ;Old entry, remove.
10097                  (gnus-score-set 'touched t alist)
10098                  (setcdr entries (cdr rest))
10099                  (setq rest entries)))
10100           (setq entries rest))))))
10101
10102 (defun gnus-score-string< (a1 a2)
10103   ;; Compare headers in articles A2 and A2.
10104   ;; The header index used is the free variable `gnus-score-index'.
10105   (string-lessp (aref (car a1) gnus-score-index)
10106                 (aref (car a2) gnus-score-index)))
10107
10108 (defun gnus-useful-date (date)
10109   ;; Return the numeric day corresponding to the DATE string.
10110   (let ((d (mapcar (lambda (s) (and s (string-to-int s)) )
10111                    (timezone-parse-date date))))
10112     (timezone-absolute-from-gregorian (nth 1 d) (nth 2 d) (car d))))
10113
10114 (defun gnus-score-build-cons (article)
10115   ;; Build a `gnus-newsgroup-scored' type cons from ARTICLE.
10116   (cons (header-number (car article)) (cdr article)))
10117
10118 (defconst gnus-header-index
10119   ;; Name to index alist.
10120   '(("number" 0 gnus-score-integer)
10121     ("subject" 1 gnus-score-string)
10122     ("from" 2 gnus-score-string)
10123     ("date" 3 gnus-score-date)
10124     ("id" 4 gnus-score-string) 
10125     ("references" 5 gnus-score-string) 
10126     ("chars" 6 gnus-score-integer) 
10127     ("lines" 7 gnus-score-integer) 
10128     ("xref" 8 gnus-score-string)))
10129
10130 (defvar gnus-score-file-suffix "SCORE"
10131   "Suffix of the score files.")
10132
10133 (defun gnus-score-file-name (newsgroup)
10134   "Return the name of a score file for NEWSGROUP."
10135   (cond  ((or (null newsgroup)
10136               (string-equal newsgroup ""))
10137           ;; The global score file is placed at top of the directory.
10138           (expand-file-name gnus-score-file-suffix
10139                             (or gnus-kill-files-directory "~/News")))
10140          (gnus-use-long-file-name
10141           ;; Append ".SCORE" to newsgroup name.
10142           (expand-file-name (concat newsgroup "." gnus-score-file-suffix)
10143                             (or gnus-kill-files-directory "~/News")))
10144          (t
10145           ;; Place "KILL" under the hierarchical directory.
10146           (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
10147                                     "/" gnus-score-file-suffix)
10148                             (or gnus-kill-files-directory "~/News")))))
10149
10150 (defun gnus-score-score-files (group)
10151   "Return a list of all possible score files."
10152   (or gnus-kill-files-directory (setq gnus-kill-files-directory "~/News/"))
10153   (if (not (file-exists-p gnus-kill-files-directory))
10154       (setq gnus-score-file-list nil)
10155     (if gnus-use-long-file-name
10156         (if (or (not gnus-score-file-list)
10157                 (gnus-file-newer-than gnus-kill-files-directory
10158                                       (car gnus-score-file-list)))
10159             (setq gnus-score-file-list
10160                   (cons (nth 5 (file-attributes gnus-kill-files-directory))
10161                         (directory-files
10162                          gnus-kill-files-directory t
10163                          (concat gnus-score-file-suffix "$")))))
10164       (let ((dir (expand-file-name
10165                   (concat gnus-kill-files-directory
10166                           (gnus-replace-chars-in-string group ?. ?/))))
10167             (mdir (length (expand-file-name gnus-kill-files-directory)))
10168             files)
10169         (if (file-exists-p (concat dir "/" gnus-score-file-suffix))
10170             (setq files (list (concat dir "/" gnus-score-file-suffix))))
10171         (while (>= (1+ (length dir)) mdir)
10172           (and (file-exists-p (concat dir "/all/" gnus-score-file-suffix))
10173                (setq files (cons (concat dir "/all/" gnus-score-file-suffix)
10174                                  files)))
10175           (string-match "/[^/]*$" dir)
10176           (setq dir (substring dir (match-beginning 0))))
10177         (setq gnus-score-file-list (cons nil files)))))
10178   (cdr gnus-score-file-list))
10179         
10180 (defun gnus-score-find-single (group)
10181   "Return list containing the score file for GROUP."
10182   (list (gnus-score-file-name group)))
10183
10184 (defun gnus-score-find-hierarchical (group)
10185   "Return list of score files for GROUP.
10186 This includes the score file for the group and all its parents."
10187   (let ((all (copy-sequence '(nil)))
10188         (start 0))
10189     (while (string-match "\\." group (1+ start))
10190       (setq start (match-beginning 0))
10191       (setq all (cons (substring group 0 start) all)))
10192     (setq all (cons group all))
10193     (mapcar 'gnus-score-file-name (nreverse all))))
10194
10195 (defun gnus-score-find-bnews (group)
10196   "Return a list of score files for GROUP.
10197 The score files are those files in the ~/News directory which matches
10198 GROUP using BNews sys file syntax."
10199   (let ((sfiles (gnus-score-score-files group))
10200         (klen (length (expand-file-name gnus-kill-files-directory)))
10201         ofiles not-match regexp)
10202     (save-excursion
10203       (set-buffer (get-buffer-create "*gnus score files*"))
10204       (buffer-disable-undo (current-buffer))
10205       (while sfiles
10206         (erase-buffer)
10207         (insert (car sfiles))
10208         (goto-char 1)
10209         (re-search-forward (concat "." gnus-score-file-suffix "$"))
10210         (replace-match "") 
10211         (goto-char 1)
10212         (delete-char klen)
10213         (while (search-forward "all" nil t)
10214           (replace-match ".+"))
10215         (goto-char 1)
10216         (if (looking-at "not.")
10217             (progn
10218               (setq not-match t)
10219               (setq regexp (buffer-substring 5 (point-max))))
10220           (setq regexp (buffer-substring 1 (point-max)))
10221           (setq not-match nil))
10222         (if (or (and not-match
10223                      (not (string-match regexp group)))
10224                 (and (not not-match)
10225                      (string-match regexp group)))
10226             (setq ofiles (cons (car sfiles) ofiles)))
10227         (setq sfiles (cdr sfiles)))
10228       (kill-buffer (current-buffer))
10229       ofiles)))
10230
10231
10232 \f
10233 ;;; Gnus Posting Functions
10234 ;;;
10235
10236 (defvar gnus-organization-file "/usr/lib/news/organization"
10237   "*Local news organization file.")
10238
10239 (defvar gnus-post-news-buffer "*post-news*")
10240 (defvar gnus-winconf-post-news nil)
10241
10242 (autoload 'news-reply-mode "rnewspost")
10243
10244 ;;; Post news commands of Gnus group mode and summary mode
10245
10246 (defun gnus-group-post-news ()
10247   "Post an article."
10248   (interactive)
10249   (gnus-set-global-variables)
10250   ;; Save window configuration.
10251   (setq gnus-winconf-post-news (current-window-configuration))
10252   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
10253   (or gnus-newsgroup-name (setq gnus-newsgroup-name (gnus-group-group-name)))
10254   (unwind-protect
10255       (gnus-post-news 'post nil)
10256     (or (and (eq (current-buffer) (get-buffer gnus-post-news-buffer))
10257              (not (zerop (buffer-size))))
10258         ;; Restore last window configuration.
10259         (and gnus-winconf-post-news
10260              (set-window-configuration gnus-winconf-post-news))))
10261   ;; We don't want to return to summary buffer nor article buffer later.
10262   (setq gnus-winconf-post-news nil)
10263   (if (get-buffer gnus-summary-buffer)
10264       (bury-buffer gnus-summary-buffer))
10265   (if (get-buffer gnus-article-buffer)
10266       (bury-buffer gnus-article-buffer)))
10267
10268 (defun gnus-summary-post-news ()
10269   "Post an article."
10270   (interactive)
10271   (gnus-set-global-variables)
10272   ;; Save window configuration.
10273   (setq gnus-winconf-post-news (current-window-configuration))
10274   (unwind-protect
10275       (gnus-post-news 'post gnus-newsgroup-name)
10276     (or (and (eq (current-buffer) (get-buffer gnus-post-news-buffer))
10277              (not (zerop (buffer-size))))
10278         ;; Restore last window configuration.
10279         (and gnus-winconf-post-news
10280              (set-window-configuration gnus-winconf-post-news))))
10281   ;; We don't want to return to article buffer later.
10282   (if (get-buffer gnus-article-buffer)
10283       (bury-buffer gnus-article-buffer)))
10284
10285 (defun gnus-summary-followup (yank)
10286   "Compose a followup to an article.
10287 If prefix argument YANK is non-nil, original article is yanked automatically."
10288   (interactive "P")
10289   (gnus-set-global-variables)
10290   (gnus-summary-select-article t)
10291   (let ((headers gnus-current-headers)
10292         (gnus-newsgroup-name gnus-newsgroup-name))
10293     ;; Check Followup-To: poster.
10294     (set-buffer gnus-article-buffer)
10295     (if (and gnus-use-followup-to
10296              (string-equal "poster" (gnus-fetch-field "followup-to"))
10297              (or (not (eq gnus-use-followup-to t))
10298                  (not (gnus-y-or-n-p 
10299                        "Do you want to ignore `Followup-To: poster'? "))))
10300         ;; Mail to the poster.  Gnus is now RFC1036 compliant.
10301         (gnus-summary-reply yank)
10302       ;; Save window configuration.
10303       (setq gnus-winconf-post-news (current-window-configuration))
10304       (unwind-protect
10305           (gnus-post-news 'followup headers gnus-article-buffer yank)
10306         (or (and (eq (current-buffer) (get-buffer gnus-post-news-buffer))
10307                  (not (zerop (buffer-size))))
10308             ;; Restore last window configuration.
10309             (and gnus-winconf-post-news
10310                  (set-window-configuration gnus-winconf-post-news))))
10311       ;; We don't want to return to article buffer later.
10312       (bury-buffer gnus-article-buffer))))
10313
10314 (defun gnus-summary-followup-with-original ()
10315   "Compose a followup to an article and include the original article."
10316   (interactive)
10317   (gnus-summary-followup t))
10318
10319 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10320 (defun gnus-summary-followup-and-reply (yank)
10321   "Compose a followup and do an auto mail to author."
10322   (interactive "P")
10323   (let ((gnus-auto-mail-to-author t))
10324     (gnus-summary-followup yank)))
10325
10326 (defun gnus-summary-followup-and-reply-with-original ()
10327   "Compose a followup, include the original, and do an auto mail to author."
10328   (interactive)
10329   (gnus-summary-followup-and-reply t))
10330
10331 (defun gnus-summary-cancel-article ()
10332   "Cancel an article you posted."
10333   (interactive)
10334   (gnus-set-global-variables)
10335   (gnus-summary-select-article t)
10336   (gnus-eval-in-buffer-window gnus-article-buffer
10337                               (gnus-cancel-news)))
10338
10339 (defun gnus-summary-supersede-article ()
10340   "Compose an article that will supersede a previous article.
10341 This is done simply by taking the old article and adding a Supersedes
10342 header line with the old Message-ID."
10343   (interactive)
10344   (gnus-set-global-variables)
10345   (if (not
10346        (string-equal
10347         (downcase (mail-strip-quoted-names 
10348                    (header-from gnus-current-headers)))
10349         (downcase (mail-strip-quoted-names (gnus-inews-user-name)))))
10350       (error "This article is not yours."))
10351   (gnus-summary-select-article t)
10352   (save-excursion
10353     (set-buffer gnus-article-buffer)
10354     (let ((buffer-read-only nil))
10355       (goto-char (point-min))
10356       (search-forward "\n\n" nil t)
10357       (if (not (re-search-backward "^Message-ID: " nil t))
10358           (error "No Message-ID in this article"))))
10359   (if (gnus-post-news 'post gnus-newsgroup-name)
10360       (progn
10361         (erase-buffer)
10362         (insert-buffer gnus-article-buffer)
10363         (goto-char (point-min))
10364         (search-forward "\n\n" nil t)
10365         (if (not (re-search-backward "^Message-ID: " nil t))
10366             (error "No Message-ID in this article")
10367           (replace-match "Supersedes: "))
10368         (search-forward "\n\n")
10369         (forward-line -1)
10370         (insert mail-header-separator))))
10371
10372 \f
10373 ;;; Post a News using NNTP
10374
10375 ;;;###autoload
10376 (fset 'sendnews 'gnus-post-news)
10377
10378 ;;;###autoload
10379 (fset 'postnews 'gnus-post-news)
10380
10381 (defun gnus-post-news (method &optional header article-buffer yank)
10382   "Begin editing a new USENET news article to be posted.
10383 Type \\[describe-mode] in the buffer to get a list of commands."
10384   (interactive)
10385   (if (or (not gnus-novice-user)
10386           gnus-expert-user
10387           (not (eq 'post 
10388                    (nth 1 (assoc 
10389                            (format "%s" (car (gnus-find-method-for-group 
10390                                               gnus-newsgroup-name)))
10391                            gnus-valid-select-methods))))
10392           (gnus-y-or-n-p "Are you sure you want to post to all of USENET? "))
10393       (let ((sumart (if (eq method 'followup)
10394                         (save-excursion
10395                           (set-buffer gnus-summary-buffer)
10396                           (cons (current-buffer) gnus-current-article))))
10397             post-buf)
10398         (if (and gnus-interactive-post
10399                  (not gnus-expert-user)
10400                  (eq method 'post)
10401                  (not header))
10402             (setq header 
10403                   (completing-read "Group: " gnus-active-hashtb nil t)))
10404         (setq mail-reply-buffer article-buffer)
10405         (setq gnus-post-news-buffer 
10406               (setq post-buf
10407                     (gnus-request-post-buffer 
10408                      method (if (stringp header) 
10409                                 (gnus-group-real-name header) header)
10410                      article-buffer)))
10411         (if (eq method 'post)
10412             (progn
10413               (delete-other-windows)
10414               (switch-to-buffer post-buf))
10415           (delete-other-windows)
10416           (if (not yank)
10417               (progn
10418                 (switch-to-buffer article-buffer)
10419                 (pop-to-buffer post-buf))
10420             (switch-to-buffer post-buf)))
10421         (gnus-overload-functions)
10422         (make-local-variable 'gnus-article-reply)
10423         (make-local-variable 'gnus-article-check-size)
10424         (setq gnus-article-reply sumart)
10425         ;; Handle author copy using BCC field.
10426         (if (and gnus-mail-self-blind
10427                  (not (mail-fetch-field "bcc")))
10428             (progn
10429               (mail-position-on-field "BCC")
10430               (insert (if (stringp gnus-mail-self-blind)
10431                           gnus-mail-self-blind
10432                         (user-login-name)))))
10433         ;; Handle author copy using FCC field.
10434         (if gnus-author-copy
10435             (progn
10436               (mail-position-on-field "FCC")
10437               (insert gnus-author-copy)))
10438         (goto-char (point-min))
10439         (if (and (eq method 'post) (not header))
10440             (end-of-line)
10441           (search-forward (concat "\n" mail-header-separator "\n"))
10442           (if yank 
10443               (save-excursion
10444                 (run-hooks 'news-reply-header-hook)
10445                 (mail-yank-original nil)))
10446           (if gnus-post-prepare-function
10447               (funcall gnus-post-prepare-function 
10448                        (if (stringp header) header gnus-newsgroup-name))))))
10449   (setq gnus-article-check-size (cons (buffer-size) (gnus-article-checksum)))
10450   (message "")
10451   t)
10452
10453 (defun gnus-inews-news (&optional use-group-method)
10454   "Send a news message.
10455 If given a prefix, and the group is a foreign group, this function
10456 will attempt to use the foreign server to post the article."
10457   (interactive "P")
10458   ;; Check whether the article is a good Net Citizen.
10459   (if (and gnus-article-check-size (not (gnus-inews-check-post)))
10460       ;; Aber nein!
10461       ()
10462     ;; Looks ok, so we do the nasty.
10463     (let* ((case-fold-search nil)
10464            (server-running (gnus-server-opened gnus-select-method))
10465            (reply gnus-article-reply))
10466       (save-excursion
10467         ;; Connect to default NNTP server if necessary.
10468         ;; Suggested by yuki@flab.fujitsu.junet.
10469         (gnus-start-news-server)        ;Use default server.
10470         ;; NNTP server must be opened before current buffer is modified.
10471         (widen)
10472         (goto-char (point-min))
10473         (run-hooks 'news-inews-hook)
10474         (save-restriction
10475           (narrow-to-region
10476            (point-min)
10477            (progn
10478              (goto-char (point-min))
10479              (search-forward (concat "\n" mail-header-separator "\n"))
10480              (point)))
10481
10482           ;; Correct newsgroups field: change sequence of spaces to comma and 
10483           ;; eliminate spaces around commas.  Eliminate imbedded line breaks.
10484           (goto-char (point-min))
10485           (if (search-forward-regexp "^Newsgroups: +" nil t)
10486               (save-restriction
10487                 (narrow-to-region
10488                  (point)
10489                  (if (re-search-forward "^[^ \t]" nil 'end)
10490                      (match-beginning 0)
10491                    (point-max)))
10492                 (goto-char (point-min))
10493                 (replace-regexp "\n[ \t]+" " ") ;No line breaks (too confusing)
10494                 (goto-char (point-min))
10495                 (replace-regexp "[ \t\n]*,[ \t\n]*\\|[ \t]+" ",")))
10496
10497           ;; Added by Per Abrahamsen <abraham@iesd.auc.dk>.
10498           ;; Help save the the world!
10499           (or 
10500            gnus-expert-user
10501            (let ((newsgroups (mail-fetch-field "newsgroups"))
10502                  (followup-to (mail-fetch-field "followup-to"))
10503                  groups to)
10504              (if (and (string-match "," newsgroups) (not followup-to))
10505                  (progn
10506                    (while (string-match "," newsgroups)
10507                      (setq groups
10508                            (cons (list (substring newsgroups
10509                                                   0 (match-beginning 0)))
10510                                  groups))
10511                      (setq newsgroups (substring newsgroups (match-end 0))))
10512                    (setq groups (nreverse (cons (list newsgroups) groups)))
10513
10514                    (setq to
10515                          (completing-read "Followups to: (default all groups) "
10516                                           groups))
10517                    (if (> (length to) 0)
10518                        (progn
10519                          (goto-char (point-min))
10520                          (insert "Followup-To: " to "\n")))))))
10521
10522           ;; Cleanup Followup-To.
10523           (goto-char (point-min))
10524           (if (search-forward-regexp "^Followup-To: +" nil t)
10525               (save-restriction
10526                 (narrow-to-region
10527                  (point)
10528                  (if (re-search-forward "^[^ \t]" nil 'end)
10529                      (match-beginning 0)
10530                    (point-max)))
10531                 (goto-char (point-min))
10532                 (replace-regexp "\n[ \t]+" " ") ;No line breaks (too confusing)
10533                 (goto-char (point-min))
10534                 (replace-regexp "[ \t\n]*,[ \t\n]*\\|[ \t]+" ",")))
10535
10536           ;; Mail the message too if To:, Bcc:. or Cc: exists.
10537           (if (or (mail-fetch-field "to" nil t)
10538                   (mail-fetch-field "bcc" nil t)
10539                   (mail-fetch-field "cc" nil t))
10540               (if gnus-mail-send-method
10541                   (progn
10542                     (message "Sending via mail...")
10543                     (widen)
10544                     (funcall gnus-mail-send-method)
10545                     (message "Sending via mail... done")
10546                     (save-excursion
10547                       (save-restriction
10548                         (goto-char 1)
10549                         (narrow-to-region
10550                          1 (re-search-forward mail-header-separator))
10551                         (goto-char 1)
10552                         (delete-matching-lines "BCC:.*"))))
10553                 (ding)
10554                 (message "No mailer defined.  To: and/or Cc: fields ignored.")
10555                 (sit-for 1))))
10556
10557         ;; Send to NNTP server. 
10558         (message "Posting to USENET...")
10559         (if (gnus-inews-article use-group-method)
10560             (progn
10561               (message "Posting to USENET... done")
10562               (if (and reply
10563                        (get-buffer (car reply))
10564                        (buffer-name (car reply)))
10565                   (progn
10566                     (save-excursion
10567                       (set-buffer gnus-summary-buffer)
10568                       (gnus-summary-mark-article-as-replied 
10569                        (cdr reply))))))
10570           ;; We cannot signal an error.
10571           (ding) (message "Article rejected: %s" 
10572                           (gnus-status-message gnus-select-method)))
10573         (set-buffer-modified-p nil))
10574       ;; If NNTP server is opened by gnus-inews-news, close it by myself.
10575       (or server-running
10576           (gnus-close-server (gnus-find-method-for-group gnus-newsgroup-name)))
10577       (and (fboundp 'bury-buffer) (bury-buffer))
10578       ;; Restore last window configuration.
10579       (and gnus-winconf-post-news
10580            (set-window-configuration gnus-winconf-post-news))
10581       (setq gnus-winconf-post-news nil))))
10582
10583 (defun gnus-inews-check-post ()
10584   "Check whether the post looks ok."
10585   (and 
10586    ;; Check excessive size.
10587    (if (> (buffer-size) 60000)
10588        (gnus-y-or-n-p (format "The article is %d octets long. Really post? "
10589                               (buffer-size)))
10590      t)
10591    ;; Check for commands in Subject.
10592    (save-excursion
10593      (save-restriction
10594        (goto-char (point-min))
10595        (narrow-to-region (point) (search-forward mail-header-separator))
10596        (if (string-match "^cmsg " (mail-fetch-field "subject"))
10597            (gnus-y-or-n-p
10598             "The control code \"cmsg \" is in the subject. Really post? ")
10599          t)))
10600    ;; Check for control characters.
10601    (save-excursion
10602      (if (re-search-forward "[\000-\007\013\015-\037\200-\237]" nil t)
10603          (gnus-y-or-n-p 
10604           "The article contains control characters. Really post? ")
10605        t))
10606    ;; Check for multiple identical headers.
10607    (let (found)
10608      (save-excursion
10609        (save-restriction
10610          (goto-char (point-min))
10611          (narrow-to-region (point) (search-forward mail-header-separator))
10612          (goto-char (point-min))
10613          (while (and (not found) (re-search-forward "^[^ \t:]+: " nil t))
10614            (save-excursion
10615              (or (re-search-forward 
10616                   (concat "^" (setq found
10617                                     (buffer-substring (match-beginning 0) 
10618                                                       (match-end 0))))
10619                   nil t)
10620                  (setq found nil))))
10621          (if found
10622              (gnus-y-or-n-p 
10623               (format "Multiple %s headers. Really post? " found))
10624            t))))
10625    ;; Check for version and sendsys.
10626    (save-excursion
10627      (save-restriction
10628        (goto-char (point-min))
10629        (narrow-to-region (point) (search-forward mail-header-separator))
10630        (if (re-search-backward "^Sendsys:\\|^Version:" nil t)
10631            (gnus-yes-or-no-p
10632             (format "The article contains a %s command. Really post? "
10633                     (buffer-substring (match-beginning 0) (match-end 0))))
10634          t)))
10635    (save-excursion
10636      (save-restriction
10637        (goto-char (point-min))
10638        (narrow-to-region (point) (search-forward mail-header-separator))
10639        (let* ((case-fold-search t)
10640               (from (mail-fetch-field "from")))
10641          (if (and from
10642                   (string-match "@" from)
10643                   (not (string-match "@[^\\.]*\\." from)))
10644              (gnus-yes-or-no-p
10645               (format "The domain looks strange: \"%s\". Really post? "
10646                       from))
10647            t))))
10648    ;; Use the (size . checksum) variable to see whether the
10649    ;; article is empty or has only quoted text.
10650    (if (and (= (buffer-size) (car gnus-article-check-size))
10651             (= (gnus-article-checksum) (cdr gnus-article-check-size)))
10652        (gnus-yes-or-no-p
10653         "It looks like there's no new text in your article. Really post? ")
10654      t)))
10655
10656 (defun gnus-article-checksum ()
10657   (let ((sum 0))
10658     (save-excursion
10659       (while (not (eobp))
10660         (setq sum (logxor sum (following-char)))
10661         (forward-char 1)))
10662     sum))
10663
10664 (defun gnus-cancel-news ()
10665   "Cancel an article you posted."
10666   (interactive)
10667   (if (gnus-yes-or-no-p "Do you really want to cancel this article? ")
10668       (let ((from nil)
10669             (newsgroups nil)
10670             (message-id nil)
10671             (distribution nil))
10672         (save-excursion
10673           ;; Get header info. from original article.
10674           (save-restriction
10675             (gnus-article-show-all-headers)
10676             (goto-char (point-min))
10677             (search-forward "\n\n" nil 'move)
10678             (narrow-to-region (point-min) (point))
10679             (setq from (mail-fetch-field "from"))
10680             (setq newsgroups (mail-fetch-field "newsgroups"))
10681             (setq message-id (mail-fetch-field "message-id"))
10682             (setq distribution (mail-fetch-field "distribution")))
10683           ;; Verify if the article is absolutely user's by comparing
10684           ;; user id with value of its From: field.
10685           (if (not
10686                (string-equal
10687                 (downcase (mail-strip-quoted-names from))
10688                 (downcase (mail-strip-quoted-names (gnus-inews-user-name)))))
10689               (progn
10690                 (ding) (message "This article is not yours."))
10691             ;; Make control article.
10692             (set-buffer (get-buffer-create " *Gnus-canceling*"))
10693             (buffer-disable-undo (current-buffer))
10694             (erase-buffer)
10695             (insert "Newsgroups: " newsgroups "\n"
10696                     "Subject: cancel " message-id "\n"
10697                     "Control: cancel " message-id "\n"
10698                     mail-header-separator "\n"
10699                     "This is a cancel message from " from ".\n")
10700             ;; Send the control article to NNTP server.
10701             (message "Canceling your article...")
10702             (if (gnus-inews-article)
10703                 (message "Canceling your article... done")
10704               (ding) (message "Failed to cancel your article"))
10705             ;; Kill the article buffer.
10706             (kill-buffer (current-buffer)))))))
10707
10708 \f
10709 ;;; Lowlevel inews interface
10710
10711 (defun gnus-inews-article (&optional use-group-method)
10712   "Post an article in current buffer using NNTP protocol."
10713   (let ((artbuf (current-buffer))
10714         (tmpbuf (get-buffer-create " *Gnus-posting*")))
10715     (widen)
10716     (goto-char (point-max))
10717     ;; require a newline at the end for inews to append .signature to
10718     (or (= (preceding-char) ?\n)
10719         (insert ?\n))
10720     ;; Prepare article headers.  All message body such as signature
10721     ;; must be inserted before Lines: field is prepared.
10722     (save-restriction
10723       (goto-char (point-min))
10724       (narrow-to-region 
10725        (point-min) 
10726        (save-excursion
10727          (search-forward (concat "\n" mail-header-separator "\n")) 
10728          (forward-line -1) 
10729          (point)))
10730       (gnus-inews-insert-headers)
10731       (run-hooks gnus-inews-article-header-hook)
10732       (widen))
10733     (save-excursion
10734       (set-buffer tmpbuf)
10735       (buffer-disable-undo (current-buffer))
10736       (erase-buffer)
10737       (insert-buffer-substring artbuf)
10738       ;; Remove the header separator.
10739       (goto-char (point-min))
10740       (search-forward (concat "\n" mail-header-separator "\n"))
10741       (replace-match "\n\n")
10742       ;; This hook may insert a signature.
10743       (run-hooks 'gnus-prepare-article-hook)
10744       ;; Run final inews hooks.  This hook may do FCC.
10745       ;; The article must be saved before being posted because
10746       ;; `gnus-request-post' modifies the buffer.
10747       (run-hooks 'gnus-inews-article-hook)
10748       ;; Post an article to NNTP server.
10749       ;; Return NIL if post failed.
10750       (prog1
10751           (gnus-request-post 
10752            (if use-group-method
10753                (gnus-find-method-for-group gnus-newsgroup-name)
10754              gnus-select-method) use-group-method)
10755         (kill-buffer (current-buffer))))))
10756
10757 (defun gnus-inews-insert-headers ()
10758   "Prepare article headers.
10759 Headers already prepared in the buffer are not modified.
10760 Headers in `gnus-required-headers' will be generated."
10761   (let ((Date (gnus-inews-date))
10762         (Message-ID (gnus-inews-message-id))
10763         (Organization (gnus-inews-organization))
10764         (From (gnus-inews-user-name))
10765         (Path (gnus-inews-path))
10766         (Subject nil)
10767         (Newsgroups nil)
10768         (Distribution nil)
10769         (Lines (gnus-inews-lines))
10770         (X-Newsreader gnus-version)
10771         (headers gnus-required-headers)
10772         (case-fold-search t)
10773         header value)
10774     ;; First we remove any old Message-IDs. This might be slightly
10775     ;; fascist, but if the user really wants to generate Message-IDs
10776     ;; by herself, she should remove it from the `gnus-required-list'. 
10777     (goto-char (point-min))
10778     (and (memq 'Message-ID headers)
10779          (re-search-forward "^Message-ID:" nil t)
10780          (delete-region (progn (beginning-of-line) (point))
10781                         (progn (forward-line 1) (point))))
10782     ;; Remove NNTP-posting-host.
10783     (goto-char (point-min))
10784     (and (re-search-forward "nntp-posting-host^:" nil t)
10785          (delete-region (progn (beginning-of-line) (point))
10786                         (progn (forward-line 1) (point))))
10787     ;; Insert new Sender if the From is strange. 
10788     (let ((from (mail-fetch-field "from")))
10789       (if (and from (not (string= (downcase from) (downcase From))))
10790           (progn
10791             (goto-char (point-min))    
10792             (and (re-search-forward "^Sender:" nil t)
10793                  (delete-region (progn (beginning-of-line) (point))
10794                                 (progn (forward-line 1) (point))))
10795             (insert "Sender: " From "\n"))))
10796     ;; If there are References, and no "Re: ", then the thread has
10797     ;; changed name. See Son-of-1036.
10798     (if (and (mail-fetch-field "references")
10799              (get-buffer gnus-article-buffer))
10800         (let ((psubject (gnus-simplify-subject-re
10801                          (mail-fetch-field "subject")))
10802               subject)
10803           (save-excursion
10804             (set-buffer (get-buffer gnus-article-buffer))
10805             (save-restriction
10806               (gnus-narrow-to-headers)
10807               (if (setq subject (mail-fetch-field "subject"))
10808                   (progn
10809                     (and gnus-summary-gather-subject-limit
10810                          (> (length subject) gnus-summary-gather-subject-limit)
10811                          (setq subject
10812                                (substring subject 0
10813                                           gnus-summary-gather-subject-limit)))
10814                     (setq subject (gnus-simplify-subject-re subject))))))
10815           (or (and psubject subject (string= subject psubject))
10816               (progn
10817                 (string-match "@" Message-ID)
10818                 (setq Message-ID
10819                       (concat (substring Message-ID 0 (match-beginning 0))
10820                               "_-_" 
10821                               (substring Message-ID (match-beginning 0))))))))
10822     ;; Go through all the required headers and see if they are in the
10823     ;; articles already. If they are not, or are empty, they are
10824     ;; inserted automatically - except for Subject, Newsgroups and
10825     ;; Distribution. 
10826     (while headers
10827       (goto-char (point-min))
10828       (setq header (car headers))
10829       (if (or (not (re-search-forward 
10830                     (concat "^" (downcase (symbol-name header)) ":") nil t))
10831               (progn
10832                 (if (= (following-char) ? ) (forward-char 1) (insert " "))
10833                 (looking-at "[ \t]*$")))
10834           (progn
10835             (setq value (or (and (boundp header) (symbol-value header))
10836                             (read-from-minibuffer
10837                              (format "Empty header for %s; enter value: " 
10838                                      header))))
10839             (if (bolp)
10840                 (save-excursion
10841                   (goto-char (point-max))
10842                   (insert (symbol-name header) ": " value "\n"))
10843               (replace-match value))))
10844       (setq headers (cdr headers)))))
10845
10846 (defun gnus-inews-insert-signature ()
10847   "Insert a signature file.
10848 If `gnus-signature-function' is bound and returns a string, this
10849 string is used instead of the variable `gnus-signature-file'.
10850 In either case, if the string is a file name, this file is
10851 inserted. If the string is not a file name, the string itself is
10852 inserted. 
10853 If you never want any signature inserted, set both those variables to
10854 nil."
10855   (save-excursion
10856     (let ((signature 
10857            (or (and gnus-signature-function
10858                     (fboundp gnus-signature-function)
10859                     (funcall gnus-signature-function gnus-newsgroup-name))
10860                gnus-signature-file))
10861           b)
10862       (if (and signature
10863                (or (file-exists-p signature)
10864                    (string-match " " signature)
10865                    (not (string-match 
10866                          "^/[^/]+/" (expand-file-name signature)))))
10867           (progn
10868             (goto-char (point-max))
10869             ;; Delete any previous signatures.
10870             (if (and mail-signature (search-backward "\n-- \n" nil t))
10871                 (delete-region (1+ (point)) (point-max)))
10872             (insert "\n-- \n")
10873             (and (< 4 (setq b (count-lines 
10874                                (point)
10875                                (progn
10876                                  (if (file-exists-p signature)
10877                                      (insert-file-contents signature)
10878                                    (insert signature))
10879                                  (goto-char (point-max))
10880                                  (or (bolp) (insert "\n"))
10881                                  (point)))))
10882                  (not gnus-expert-user)
10883                  (not
10884                   (gnus-y-or-n-p
10885                    (format
10886                     "Your .sig is %d lines; it should be max 4. Really post? "
10887                     b)))
10888                  (if (file-exists-p signature)
10889                      (error (format "Edit %s." signature))
10890                    (error "Trim your signature."))))))))
10891
10892 (defun gnus-inews-do-fcc ()
10893   "Process FCC: fields in current article buffer.
10894 Unless the first character of the field is `|', the article is saved
10895 to the specified file using the function specified by the variable
10896 gnus-author-copy-saver.  The default function rmail-output saves in
10897 Unix mailbox format.
10898 If the first character is `|', the contents of the article is send to
10899 a program specified by the rest of the value."
10900   (let ((fcc-list nil)
10901         (fcc-file nil)
10902         (case-fold-search t))           ;Should ignore case.
10903     (save-excursion
10904       (save-restriction
10905         (goto-char (point-min))
10906         (search-forward "\n\n")
10907         (narrow-to-region (point-min) (point))
10908         (goto-char (point-min))
10909         (while (re-search-forward "^FCC:[ \t]*" nil t)
10910           (setq fcc-list
10911                 (cons (buffer-substring
10912                        (point)
10913                        (progn
10914                          (end-of-line)
10915                          (skip-chars-backward " \t")
10916                          (point)))
10917                       fcc-list))
10918           (delete-region (match-beginning 0)
10919                          (progn (forward-line 1) (point))))
10920         ;; Process FCC operations.
10921         (widen)
10922         (while fcc-list
10923           (setq fcc-file (car fcc-list))
10924           (setq fcc-list (cdr fcc-list))
10925           (cond ((string-match "^[ \t]*|[ \t]*\\(.*\\)[ \t]*$" fcc-file)
10926                  (let ((program (substring fcc-file
10927                                            (match-beginning 1) (match-end 1))))
10928                    ;; Suggested by yuki@flab.fujitsu.junet.
10929                    ;; Send article to named program.
10930                    (call-process-region (point-min) (point-max) shell-file-name
10931                                         nil nil nil "-c" program)
10932                    ))
10933                 (t
10934                  ;; Suggested by hyoko@flab.fujitsu.junet.
10935                  ;; Save article in Unix mail format by default.
10936                  (if (and gnus-author-copy-saver
10937                           (not (eq gnus-author-copy-saver 'rmail-output)))
10938                      (funcall gnus-author-copy-saver fcc-file)
10939                    (if (and (file-readable-p fcc-file) (rmail-file-p fcc-file))
10940                        (gnus-output-to-rmail fcc-file)
10941                      (rmail-output fcc-file 1 t t)))
10942                  ))
10943           )
10944         ))
10945     ))
10946
10947 (defun gnus-inews-path ()
10948   "Return uucp path."
10949   (let ((login-name (gnus-inews-login-name)))
10950     (cond ((null gnus-use-generic-path)
10951            (concat (nth 1 gnus-select-method) "!" login-name))
10952           ((stringp gnus-use-generic-path)
10953            ;; Support GENERICPATH.  Suggested by vixie@decwrl.dec.com.
10954            (concat gnus-use-generic-path "!" login-name))
10955           (t login-name))
10956     ))
10957
10958 (defun gnus-inews-user-name ()
10959   "Return user's network address as \"NAME@DOMAIN (FULL-NAME)\"."
10960   (let ((full-name (gnus-inews-full-name)))
10961     (concat (if (or gnus-user-login-name gnus-use-generic-from
10962                     gnus-local-domain (getenv "DOMAINNAME"))
10963                 (concat (gnus-inews-login-name) "@"
10964                         (gnus-inews-domain-name gnus-use-generic-from))
10965               user-mail-address)
10966             ;; User's full name.
10967             (cond ((string-equal full-name "") "")
10968                   ((string-equal full-name "&") ;Unix hack.
10969                    (concat " (" (user-login-name) ")"))
10970                   (t
10971                    (concat " (" full-name ")"))))))
10972
10973 (defun gnus-inews-login-name ()
10974   "Return login name."
10975   (or gnus-user-login-name (getenv "LOGNAME") (user-login-name)))
10976
10977 (defun gnus-inews-full-name ()
10978   "Return full user name."
10979   (or gnus-user-full-name (getenv "NAME") (user-full-name)))
10980
10981 (defun gnus-inews-domain-name (&optional genericfrom)
10982   "Return user's domain name.
10983 If optional argument GENERICFROM is a string, use it as the domain
10984 name; if it is non-nil, strip off local host name from the domain name.
10985 If the function `system-name' returns full internet name and the
10986 domain is undefined, the domain name is got from it."
10987   (if (or genericfrom gnus-local-domain (getenv "DOMAINNAME"))
10988       (let ((domain 
10989              (or (if (stringp genericfrom) genericfrom)
10990                  (getenv "DOMAINNAME")
10991                  gnus-local-domain
10992                  ;; Function `system-name' may return full internet name.
10993                  ;; Suggested by Mike DeCorte <mrd@sun.soe.clarkson.edu>.
10994                  (if (string-match "\\." (system-name))
10995                      (substring (system-name) (match-end 0)))
10996                  (read-string "Domain name (no host): ")))
10997             (host (or (if (string-match "\\." (system-name))
10998                           (substring (system-name) 0 (match-beginning 0)))
10999                       (system-name))))
11000         (if (string-equal "." (substring domain 0 1))
11001             (setq domain (substring domain 1)))
11002         ;; Support GENERICFROM as same as standard Bnews system.
11003         ;; Suggested by ohm@kaba.junet and vixie@decwrl.dec.com.
11004         (cond ((null genericfrom)
11005                (concat host "." domain))
11006               ;;((stringp genericfrom) genericfrom)
11007               (t domain)))
11008     (substring user-mail-address (1+ (string-match "@" user-mail-address)))))
11009
11010 (defun gnus-inews-full-address ()
11011   (let ((domain (gnus-inews-domain-name))
11012         (system (system-name))
11013         (case-fold-search t))
11014     (if (string-match "\\." system) system
11015       (if (string-match (concat "^" (regexp-quote system)) domain) domain
11016         (concat system "." domain)))))
11017
11018 (defun gnus-inews-message-id ()
11019   "Generate unique Message-ID for user."
11020   ;; Message-ID should not contain a slash and should be terminated by
11021   ;; a number.  I don't know the reason why it is so.
11022   (concat "<" (gnus-inews-unique-id) "@" (gnus-inews-full-address) ">"))
11023
11024 (defun gnus-inews-unique-id ()
11025   "Generate unique ID from user name and current time."
11026   (concat (downcase (gnus-inews-login-name))
11027           (mapconcat 
11028            (lambda (num) (gnus-number-base-x num 3 31))
11029            (current-time) "")))
11030
11031 (defun gnus-inews-date ()
11032   "Current time string."
11033   (timezone-make-date-arpa-standard (current-time-string)))
11034
11035 (defun gnus-inews-organization ()
11036   "Return user's organization.
11037 The ORGANIZATION environment variable is used if defined.
11038 If not, the variable `gnus-local-organization' is used instead.
11039 If it is a function, the function will be called with the current
11040 newsgroup name as the argument.
11041 If this is a file name, the contents of this file will be used as the
11042 organization."
11043   (let* ((organization 
11044           (or (getenv "ORGANIZATION")
11045               (if gnus-local-organization
11046                   (if (and (symbolp gnus-local-organization)
11047                            (fboundp gnus-local-organization))
11048                       (funcall gnus-local-organization gnus-newsgroup-name)
11049                     gnus-local-organization))
11050               gnus-organization-file
11051               "~/.organization")))
11052     (and (stringp organization)
11053          (> (length organization) 0)
11054          (or (file-exists-p organization)
11055              (string-match " " organization)
11056              (not (string-match  "^/[^/]+/" (expand-file-name organization))))
11057          (save-excursion
11058            (set-buffer (get-buffer-create " *Gnus organization*"))
11059            (buffer-disable-undo (current-buffer))
11060            (erase-buffer)
11061            (if (file-exists-p organization)
11062                (insert-file-contents organization)
11063              (insert organization))
11064            (goto-char (point-min))
11065            (while (re-search-forward " *\n *" nil t)
11066              (replace-match " "))
11067            (buffer-substring (point-min) (point-max))))))
11068
11069 (defun gnus-inews-lines ()
11070   "Count the number of lines and return numeric string."
11071   (save-excursion
11072     (save-restriction
11073       (widen)
11074       (goto-char (point-min))
11075       (search-forward "\n\n" nil 'move)
11076       (int-to-string (count-lines (point) (point-max))))))
11077
11078 \f
11079 ;;;
11080 ;;; Gnus Mail Functions 
11081 ;;;
11082
11083 (autoload 'news-mail-reply "rnewspost")
11084 (autoload 'news-mail-other-window "rnewspost")
11085
11086 ;;; Mail reply commands of Gnus summary mode
11087
11088 (defun gnus-summary-reply (yank)
11089   "Reply mail to news author.
11090 If prefix argument YANK is non-nil, original article is yanked automatically.
11091 Customize the variable gnus-mail-reply-method to use another mailer."
11092   (interactive "P")
11093   ;; Bug fix by jbw@bigbird.bu.edu (Joe Wells)
11094   ;; Stripping headers should be specified with mail-yank-ignored-headers.
11095   (gnus-summary-select-article t)
11096   (setq gnus-winconf-post-news (current-window-configuration))
11097   (let ((gnus-newsgroup-name gnus-newsgroup-name))
11098     (bury-buffer gnus-article-buffer)
11099     (funcall gnus-mail-reply-method yank)))
11100
11101 (defun gnus-summary-reply-with-original ()
11102   "Reply mail to news author with original article.
11103 Customize the variable gnus-mail-reply-method to use another mailer."
11104   (interactive)
11105   (gnus-summary-reply t))
11106
11107 (defun gnus-summary-mail-forward ()
11108   "Forward the current message to another user.
11109 Customize the variable gnus-mail-forward-method to use another mailer."
11110   (interactive)
11111   (gnus-summary-select-article t)
11112   (setq gnus-winconf-post-news (current-window-configuration))
11113   (set-buffer gnus-article-buffer)
11114   (let ((gnus-newsgroup-name gnus-newsgroup-name))
11115     (funcall gnus-mail-forward-method)))
11116
11117 (defun gnus-summary-mail-other-window ()
11118   "Compose mail in other window.
11119 Customize the variable `gnus-mail-other-window-method' to use another
11120 mailer."
11121   (interactive)
11122   (setq gnus-winconf-post-news (current-window-configuration))
11123   (let ((gnus-newsgroup-name gnus-newsgroup-name))
11124     (funcall gnus-mail-other-window-method)))
11125
11126 (defun gnus-mail-reply-using-mail (&optional yank to-address)
11127   (save-excursion
11128     (set-buffer gnus-summary-buffer)
11129     (let ((info (nth 2 (gnus-gethash gnus-newsgroup-name gnus-newsrc-hashtb)))
11130           (group (gnus-group-real-name gnus-newsgroup-name))
11131           (cur (cons (current-buffer) (cdr gnus-article-current)))
11132           from subject date to reply-to message-of
11133           references message-id sender follow-to cc)
11134       (set-buffer (get-buffer-create "*mail*"))
11135       (mail-mode)
11136       (make-local-variable 'gnus-article-reply)
11137       (setq gnus-article-reply cur)
11138       (local-set-key "\C-c\C-c" 'gnus-mail-send-and-exit)
11139       (if (and (buffer-modified-p)
11140                (> (buffer-size) 0)
11141                (not (gnus-y-or-n-p 
11142                      "Unsent article being composed; erase it? ")))
11143           ()
11144         (erase-buffer)
11145         (save-excursion
11146           (set-buffer gnus-article-buffer)
11147           (let ((buffer-read-only nil))
11148             (goto-char (point-min))
11149             (narrow-to-region (point-min)
11150                               (progn (search-forward "\n\n") (point)))
11151             (add-text-properties (point-min) (point-max) '(invisible nil)))
11152           (if (and (boundp 'gnus-reply-to-function)
11153                    gnus-reply-to-function)
11154               (save-excursion
11155                 (save-restriction
11156                   (gnus-narrow-to-headers)
11157                   (setq follow-to (funcall gnus-reply-to-function group)))))
11158           (setq from (mail-fetch-field "from"))
11159           (setq date (mail-fetch-field "date"))
11160           (and from
11161                (let ((stop-pos 
11162                       (string-match "  *at \\|  *@ \\| *(\\| *<" from)))
11163                  (setq message-of
11164                        (concat (if stop-pos (substring from 0 stop-pos) from)
11165                                "'s message of " date))))
11166           (setq sender (mail-fetch-field "sender"))
11167           (setq subject (or (mail-fetch-field "subject")
11168                             "Re: none"))
11169           (or (string-match "^[Rr][Ee]:" subject)
11170               (setq subject (concat "Re: " subject)))
11171           (setq cc (mail-fetch-field "cc"))
11172           (setq reply-to (mail-fetch-field "reply-to"))
11173           (setq references (mail-fetch-field "references"))
11174           (setq message-id (mail-fetch-field "message-id"))
11175           (widen))
11176         (setq news-reply-yank-from from)
11177         (setq news-reply-yank-message-id message-id)
11178         (mail-setup (or to-address follow-to reply-to from sender "") 
11179                     subject message-of nil gnus-article-buffer nil)
11180         ;; Fold long references line to follow RFC1036.
11181         (mail-position-on-field "References")
11182         (let ((begin (- (point) (length "References: ")))
11183               (fill-column 78)
11184               (fill-prefix "\t"))
11185           (if references (insert references))
11186           (if (and references message-id) (insert " "))
11187           (if message-id (insert message-id))
11188           ;; The region must end with a newline to fill the region
11189           ;; without inserting extra newline.
11190           (fill-region-as-paragraph begin (1+ (point))))
11191         (goto-char (point-min))
11192         (search-forward (concat "\n" mail-header-separator "\n"))
11193         (if yank
11194             (let ((last (point)))
11195               (run-hooks 'news-reply-header-hook)
11196               (mail-yank-original nil)
11197               (goto-char last))))
11198       (if (not yank)
11199           (let ((mail (current-buffer)))
11200             (switch-to-buffer gnus-article-buffer)
11201             (delete-other-windows)
11202             (switch-to-buffer-other-window mail))
11203         (delete-other-windows)
11204         (switch-to-buffer (current-buffer))))))
11205
11206 (defun gnus-mail-yank-original ()
11207   (interactive)
11208   (run-hooks 'news-reply-header-hook)
11209   (mail-yank-original nil))
11210
11211 (defun gnus-mail-send-and-exit ()
11212   (interactive)
11213   (let ((cbuf (current-buffer)))
11214     (mail-send-and-exit nil)
11215     (if (get-buffer gnus-group-buffer)
11216         (progn
11217           (save-excursion
11218             (set-buffer cbuf)
11219             (let ((reply gnus-article-reply))
11220               (if (and reply
11221                        (get-buffer (car reply))
11222                        (buffer-name (car reply)))
11223                   (progn
11224                     (set-buffer (car reply))
11225                     (and (cdr reply)
11226                          (gnus-summary-mark-article-as-replied 
11227                           (cdr reply)))))))
11228           (and gnus-winconf-post-news
11229                (set-window-configuration gnus-winconf-post-news))
11230           (setq gnus-winconf-post-news nil)))))
11231
11232 (defun gnus-mail-forward-using-mail ()
11233   "Forward the current message to another user using mail."
11234   ;; This is almost a carbon copy of rmail-forward in rmail.el.
11235   (let ((forward-buffer (current-buffer))
11236         (subject
11237          (concat "[" gnus-newsgroup-name "] "
11238                  (or (gnus-fetch-field "Subject") "")))
11239         beg)
11240     ;; If only one window, use it for the mail buffer.
11241     ;; Otherwise, use another window for the mail buffer
11242     ;; so that the Rmail buffer remains visible
11243     ;; and sending the mail will get back to it.
11244     (if (if (one-window-p t)
11245             (mail nil nil subject)
11246           (mail-other-window nil nil subject))
11247         (save-excursion
11248           (setq beg (goto-char (point-max)))
11249           (insert "------- Start of forwarded message -------\n")
11250           (insert-buffer forward-buffer)
11251           (goto-char (point-max))
11252           (insert "------- End of forwarded message -------\n")
11253           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>. 
11254           (goto-char beg)
11255           (while (setq beg (next-single-property-change (point) 'invisible))
11256             (goto-char beg)
11257             (delete-region beg (or (next-single-property-change 
11258                                     (point) 'invisible)
11259                                    (point-max))))
11260           ;; You have a chance to arrange the message.
11261           (run-hooks 'gnus-mail-forward-hook)))))
11262
11263 (defun gnus-mail-other-window-using-mail ()
11264   "Compose mail other window using mail."
11265   (news-mail-other-window)
11266   (gnus-overload-functions))
11267
11268 \f
11269 ;;;
11270 ;;; Dribble file
11271 ;;;
11272
11273 (defvar gnus-dribble-ignore nil)
11274
11275 (defun gnus-dribble-file-name ()
11276   (concat gnus-startup-file "-dribble"))
11277
11278 (defun gnus-dribble-open ()
11279   (save-excursion 
11280     (set-buffer 
11281      (setq gnus-dribble-buffer (find-file-noselect (gnus-dribble-file-name))))
11282     (buffer-disable-undo (current-buffer))
11283     (bury-buffer gnus-dribble-buffer)
11284     (auto-save-mode t)
11285     (goto-char (point-max))))
11286
11287 (defun gnus-dribble-enter (string)
11288   (if (and (not gnus-dribble-ignore)
11289            gnus-dribble-buffer
11290            (buffer-name gnus-dribble-buffer))
11291       (let ((obuf (current-buffer)))
11292         (set-buffer gnus-dribble-buffer)
11293         (insert string "\n")
11294         (set-window-point (get-buffer-window (current-buffer)) (point-max))
11295         (set-buffer obuf))))
11296
11297 (defun gnus-dribble-read-file ()
11298   (let ((dribble-file (gnus-dribble-file-name)))
11299     (save-excursion 
11300       (set-buffer (setq gnus-dribble-buffer 
11301                         (get-buffer-create 
11302                          (file-name-nondirectory dribble-file))))
11303       (gnus-add-current-to-buffer-list)
11304       (erase-buffer)
11305       (set-visited-file-name dribble-file)
11306       (buffer-disable-undo (current-buffer))
11307       (bury-buffer (current-buffer))
11308       (set-buffer-modified-p nil)
11309       (let ((auto (make-auto-save-file-name))
11310             (gnus-dribble-ignore t))
11311         (if (or (file-exists-p auto) (file-exists-p dribble-file))
11312             (progn
11313               (if (file-newer-than-file-p auto dribble-file)
11314                   (setq dribble-file auto))
11315               (insert-file-contents dribble-file)
11316               (if (not (zerop (buffer-size)))
11317                   (set-buffer-modified-p t))
11318               (if (gnus-y-or-n-p 
11319                    "Auto-save file exists. Do you want to read it? ")
11320                   (progn
11321                     (message "Reading %s..." dribble-file) 
11322                     (eval-current-buffer)
11323                     (message "Reading %s...done" dribble-file)))))))))
11324
11325 (defun gnus-dribble-delete-file ()
11326   (if (file-exists-p (gnus-dribble-file-name))
11327       (delete-file (gnus-dribble-file-name)))
11328   (if gnus-dribble-buffer
11329       (save-excursion
11330         (set-buffer gnus-dribble-buffer)
11331         (let ((auto (make-auto-save-file-name)))
11332           (if (file-exists-p auto)
11333               (delete-file auto))
11334           (erase-buffer)
11335           (set-buffer-modified-p nil)))))
11336
11337 (defun gnus-dribble-save ()
11338   (if (and gnus-dribble-buffer
11339            (buffer-name gnus-dribble-buffer))
11340       (save-excursion
11341         (set-buffer gnus-dribble-buffer)
11342         (save-buffer))))
11343
11344 (defun gnus-dribble-clear ()
11345   (save-excursion
11346     (if (and gnus-dribble-buffer
11347              (buffer-name (get-buffer gnus-dribble-buffer)))
11348         (progn
11349           (set-buffer gnus-dribble-buffer)
11350           (erase-buffer)
11351           (set-buffer-modified-p nil)
11352           (setq buffer-saved-size (buffer-size))))))
11353
11354 ;;;
11355 ;;; Server Communication
11356 ;;;
11357
11358 (defun gnus-start-news-server (&optional confirm)
11359   "Open a method for getting news.
11360 If CONFIRM is non-nil, the user will be asked for an NNTP server."
11361   (let (how where)
11362     (if gnus-current-select-method
11363         ;; Stream is already opened.
11364         nil
11365       ;; Open NNTP server.
11366       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
11367       (if confirm
11368           (progn
11369             ;; Read server name with completion.
11370             (setq gnus-nntp-server
11371                   (completing-read "NNTP server: "
11372                                    (mapcar (lambda (server) (list server))
11373                                            (cons (list gnus-nntp-server)
11374                                                  gnus-secondary-servers))
11375                                    nil nil gnus-nntp-server))))
11376
11377       (if (and gnus-nntp-server 
11378                (stringp gnus-nntp-server)
11379                (not (string= gnus-nntp-server "")))
11380           (setq gnus-select-method
11381                 (cond ((or (string= gnus-nntp-server "")
11382                            (string= gnus-nntp-server "::"))
11383                        (list 'nnspool (system-name)))
11384                       ((string-match ":" gnus-nntp-server)
11385                        (list 'nnmh gnus-nntp-server))
11386                       (t
11387                        (list 'nntp gnus-nntp-server)))))
11388
11389       (setq how (car gnus-select-method))
11390       (setq where (car (cdr gnus-select-method)))
11391       (cond ((eq how 'nnspool)
11392              (require 'nnspool)
11393              (message "Looking up local news spool..."))
11394             ((eq how 'nnmh)
11395              (require 'nnmh)
11396              (message "Looking up mh spool..."))
11397             (t
11398              (require 'nntp)))
11399       (setq gnus-current-select-method gnus-select-method)
11400       (run-hooks 'gnus-open-server-hook)
11401       (or 
11402        ;; gnus-open-server-hook might have opened it
11403        (gnus-server-opened gnus-select-method)  
11404        (gnus-open-server gnus-select-method)
11405        (error "%s" (gnus-nntp-message 
11406                     (format "Cannot open NNTP server on %s" 
11407                             where))))
11408       gnus-select-method)))
11409
11410 (defun gnus-check-news-server (method)
11411   "If the news server is down, start it up again."
11412   (let ((method (if method method gnus-select-method)))
11413     (if (gnus-server-opened method)
11414         ;; Stream is already opened.
11415         t
11416       ;; Open server.
11417       (message "Opening server %s on %s..." (car method) (nth 1 method))
11418       (run-hooks 'gnus-open-server-hook)
11419       (or (gnus-server-opened method)
11420           (gnus-open-server method))
11421       (message ""))))
11422
11423 (defun gnus-nntp-message (&optional message)
11424   "Check the status of the NNTP server.
11425 If the status of the server is clear and MESSAGE is non-nil, MESSAGE
11426 is returned insted of the status string."
11427   (let ((status (gnus-status-message (gnus-find-method-for-group 
11428                                       gnus-newsgroup-name)))
11429         (message (or message "")))
11430     (if (and (stringp status) (> (length status) 0))
11431         status message)))
11432
11433 (defun gnus-get-function (method function)
11434   (let ((func (intern (format "%s-%s" (car method) function))))
11435     (if (not (fboundp func)) 
11436         (progn
11437           (require (car method))
11438           (if (not (fboundp func)) 
11439               (error "No such function: %s" func))))
11440     func))
11441
11442 ;; Specifying port number suggested by Stephane Laveau <laveau@corse.inria.fr>.
11443 (defun gnus-open-server (method)
11444   (apply (gnus-get-function method 'open-server) (cdr method)))
11445
11446 (defun gnus-close-server (method)
11447   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
11448
11449 (defun gnus-request-list (method)
11450   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
11451
11452 (defun gnus-request-list-newsgroups (method)
11453   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
11454
11455 (defun gnus-request-newgroups (date method)
11456   (funcall (gnus-get-function method 'request-newgroups) 
11457            date (nth 1 method)))
11458
11459 (defun gnus-server-opened (method)
11460   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
11461
11462 (defun gnus-status-message (method)
11463   (funcall (gnus-get-function method 'status-message) (nth 1 method)))
11464
11465 (defun gnus-request-group (group &optional dont-check)
11466   (let ((method (gnus-find-method-for-group group)))
11467     (funcall (gnus-get-function method 'request-group) 
11468              (gnus-group-real-name group) (nth 1 method) dont-check)))
11469
11470 (defun gnus-close-group (group)
11471   (let ((method (gnus-find-method-for-group group)))
11472     (funcall (gnus-get-function method 'close-group) 
11473              (gnus-group-real-name group) (nth 1 method))))
11474
11475 (defun gnus-retrieve-headers (articles group)
11476   (let ((method (gnus-find-method-for-group group)))
11477     (funcall (gnus-get-function method 'retrieve-headers) 
11478              articles (gnus-group-real-name group) (nth 1 method))))
11479
11480 (defun gnus-request-article (article group buffer)
11481   (let ((method (gnus-find-method-for-group group)))
11482     (funcall (gnus-get-function method 'request-article) 
11483              article (gnus-group-real-name group) (nth 1 method) buffer)))
11484
11485 (defun gnus-request-head (article group)
11486   (let ((method (gnus-find-method-for-group group)))
11487     (funcall (gnus-get-function method 'request-head) 
11488              article (gnus-group-real-name group) (nth 1 method))))
11489
11490 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11491 (defun gnus-request-post-buffer (post header artbuf)
11492    (let* ((group gnus-newsgroup-name)
11493           (info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
11494           (method
11495            (if (and gnus-post-method
11496                     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11497                     (memq 'post (assoc
11498                                  (format "%s" (car (gnus-find-method-for-group
11499                                                     gnus-newsgroup-name)))
11500                                         gnus-valid-select-methods)))
11501                gnus-post-method
11502              (gnus-find-method-for-group gnus-newsgroup-name))))
11503     (funcall (gnus-get-function method 'request-post-buffer) 
11504              post header artbuf (gnus-group-real-name group) info)))
11505
11506 (defun gnus-request-post (method &optional force)
11507   (and (not force) gnus-post-method
11508        (memq 'post (assoc (format "%s" (car method))
11509                           gnus-valid-select-methods))
11510        (setq method gnus-post-method))
11511   (funcall (gnus-get-function method 'request-post) 
11512            (nth 1 method)))
11513
11514 (defun gnus-request-expire-articles (articles group &optional force)
11515   (let ((method (gnus-find-method-for-group group)))
11516     (funcall (gnus-get-function method 'request-expire-articles) 
11517              articles (gnus-group-real-name group) (nth 1 method)
11518              force)))
11519
11520 (defun gnus-request-move-article (article group server accept-function)
11521   (let ((method (gnus-find-method-for-group group)))
11522     (funcall (gnus-get-function method 'request-move-article) 
11523              article (gnus-group-real-name group) 
11524              (nth 1 method) accept-function)))
11525
11526 (defun gnus-request-accept-article (group)
11527   (let ((func (if (symbolp group) group
11528                 (car (gnus-find-method-for-group group)))))
11529     (funcall (intern (format "%s-request-accept-article" func))
11530              (if (stringp group) (gnus-group-real-name group)
11531                group))))
11532
11533 (defun gnus-request-replace-article (article group buffer)
11534   (let ((func (car (gnus-find-method-for-group group))))
11535     (funcall (intern (format "%s-request-replace-article" func))
11536              article (gnus-group-real-name group) buffer)))
11537
11538 (defun gnus-find-method-for-group (group)
11539   (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
11540     (if (or (not info)
11541             (not (nth 4 info)))
11542         gnus-select-method
11543       (nth 4 info))))
11544
11545 (defun gnus-check-backend-function (func group)
11546   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
11547                  group)))
11548     (fboundp (intern (format "%s-%s" method func)))))
11549
11550 (defun gnus-methods-using (method)
11551   (let ((valids gnus-valid-select-methods)
11552         outs)
11553     (while valids
11554       (if (memq method (car valids)) 
11555           (setq outs (cons (car valids) outs)))
11556       (setq valids (cdr valids)))
11557     outs))
11558
11559 ;;; 
11560 ;;; Active & Newsrc File Handling
11561 ;;;
11562
11563 ;; Newsrc related functions.
11564 ;; Gnus internal format of gnus-newsrc-assoc:
11565 ;; (("alt.general" 3 (1 . 1))
11566 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
11567 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
11568 ;; The first item is the group name; the second is the subscription
11569 ;; level; the third is either a range of a list of ranges of read
11570 ;; articles, the optional fourth element is a list of marked articles,
11571 ;; the optional fifth element is the select method.
11572 ;;
11573 ;; Gnus internal format of gnus-newsrc-hashtb:
11574 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
11575 ;; This is the entry for "alt.misc". The first element is the number
11576 ;; of unread articles in "alt.misc". The cdr of this entry is the
11577 ;; element *before* "alt.misc" in gnus-newsrc-assoc, which makes is
11578 ;; trivial to remove or add new elements into gnus-newsrc-assoc
11579 ;; without scanning the entire list. So, to get the actual information
11580 ;; of "alt.misc", you'd say something like 
11581 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
11582 ;;
11583 ;; Gnus internal format of gnus-active-hashtb:
11584 ;; ((1 . 1))
11585 ;;  (5 . 10))
11586 ;;  (67 . 99)) ...)
11587 ;; The only element in each entry in this hash table is a range of
11588 ;; (possibly) available articles. (Articles in this range may have
11589 ;; been expired or cancelled.)
11590 ;;
11591 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
11592 ;; ("alt.misc" "alt.test" "alt.general" ...)
11593
11594 (defun gnus-setup-news (&optional rawfile level)
11595   "Setup news information.
11596 If RAWFILE is non-nil, the .newsrc file will also be read.
11597 If LEVEL is non-nil, the news will be set up at level LEVEL."
11598   (let ((init (not (and gnus-newsrc-assoc gnus-active-hashtb (not rawfile)))))
11599     ;; Clear some variables to re-initialize news information.
11600     (if init (setq gnus-newsrc-assoc nil gnus-active-hashtb nil))
11601     ;; Read the active file and create `gnus-active-hashtb'.
11602     ;; If `gnus-read-active-file' is nil, then we just create an empty
11603     ;; hash table. The partial filling out of the hash table will be
11604     ;; done in `gnus-get-unread-articles'.
11605     (if (and gnus-read-active-file (not level))
11606         (gnus-read-active-file)
11607       (setq gnus-active-hashtb (make-vector 4095 0)))
11608
11609     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
11610     (if init (gnus-read-newsrc-file rawfile))
11611     ;; Find the number of unread articles in each non-dead group.
11612     (gnus-get-unread-articles (or level 6))
11613     ;; Find new newsgroups and treat them.
11614     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level))
11615         (gnus-find-new-newsgroups))
11616     (if (and init gnus-check-bogus-newsgroups 
11617              gnus-read-active-file (not level))
11618         (gnus-check-bogus-newsgroups))))
11619
11620 (defun gnus-find-new-newsgroups ()
11621   "Search for new newsgroups and add them.
11622 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
11623 The `-n' option line from .newsrc is respected."
11624   (interactive)
11625   (or (gnus-check-first-time-used)
11626       (if (or (consp gnus-check-new-newsgroups)
11627               (eq gnus-check-new-newsgroups 'ask-server))
11628           (gnus-ask-server-for-new-groups)
11629         (let ((groups 0)
11630               group new-newsgroups)
11631           (or gnus-have-read-active-file (gnus-read-active-file))
11632           (setq gnus-newsrc-last-checked-date (current-time-string))
11633           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
11634           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
11635           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
11636           (mapatoms
11637            (lambda (sym)
11638              (setq group (symbol-name sym))
11639              (if (or (gnus-gethash group gnus-killed-hashtb)
11640                      (gnus-gethash group gnus-newsrc-hashtb))
11641                  ()
11642                (if (and gnus-newsrc-options-n-yes
11643                         (string-match gnus-newsrc-options-n-yes group))
11644                    (progn
11645                      (setq groups (1+ groups))
11646                      (gnus-sethash group group gnus-killed-hashtb)
11647                      (funcall gnus-subscribe-options-newsgroup-method group))
11648                  (if (or (null gnus-newsrc-options-n-no)
11649                          (not (string-match gnus-newsrc-options-n-no group)))
11650                      ;; Add this group.
11651                      (progn
11652                        (setq groups (1+ groups))
11653                        (gnus-sethash group group gnus-killed-hashtb)
11654                        (if gnus-subscribe-hierarchical-interactive
11655                            (setq new-newsgroups (cons group new-newsgroups))
11656                          (funcall gnus-subscribe-newsgroup-method group)))))))
11657            gnus-active-hashtb)
11658           (if new-newsgroups 
11659               (gnus-subscribe-hierarchical-interactive new-newsgroups))
11660           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11661           (if (> groups 0)
11662               (message "%d new newsgroup%s arrived." 
11663                        groups (if (> groups 1) "s have" " has")))))))
11664
11665 (defun gnus-ask-server-for-new-groups ()
11666   (let* ((date (timezone-parse-date (or gnus-newsrc-last-checked-date
11667                                         (current-time-string))))
11668          (methods (cons gnus-select-method 
11669                         (append
11670                          (and (consp gnus-check-new-newsgroups)
11671                               gnus-check-new-newsgroups)
11672                          gnus-secondary-select-methods)))
11673          (time-string
11674           (format "%s%02d%02d %s%s%s"
11675                   (substring (aref date 0) 2) (string-to-int (aref date 1)) 
11676                   (string-to-int (aref date 2)) (substring (aref date 3) 0 2)
11677                   (substring (aref date 3) 3 5) (substring (aref date 3) 6 8)))
11678          (groups 0)
11679          (new-date (current-time-string))
11680          hashtb group new-newsgroups)
11681     ;; Go thorugh both primary and secondary select methods and
11682     ;; request new newsgroups.  
11683     (while methods
11684       (if (gnus-request-newgroups time-string (car methods))
11685           (save-excursion
11686             (or hashtb (setq hashtb (gnus-make-hashtable 
11687                                      (count-lines (point-min) (point-max)))))
11688             (set-buffer nntp-server-buffer)
11689             ;; Enter all the new groups in a hashtable.
11690             (gnus-active-to-gnus-format (car methods) hashtb)))
11691       (setq methods (cdr methods)))
11692     ;; Now all new groups from all select methods are in `hashtb'.
11693     (mapatoms
11694      (lambda (group-sym)
11695        (setq group (symbol-name group-sym))
11696        (if (gnus-gethash group gnus-active-hashtb)
11697            ()
11698          (gnus-sethash group (symbol-value group-sym) gnus-active-hashtb)
11699          (if (and gnus-newsrc-options-n-yes
11700                   (string-match gnus-newsrc-options-n-yes group))
11701              (progn
11702                (setq groups (1+ groups))
11703                (funcall gnus-subscribe-options-newsgroup-method group))
11704            (if (or (null gnus-newsrc-options-n-no)
11705                    (not (string-match gnus-newsrc-options-n-no group)))
11706                ;; Add this group.
11707                (progn
11708                  (setq groups (1+ groups))
11709                  (if gnus-subscribe-hierarchical-interactive
11710                      (setq new-newsgroups (cons group new-newsgroups))
11711                    (funcall gnus-subscribe-newsgroup-method group)))))))
11712      hashtb)
11713     (if new-newsgroups 
11714         (gnus-subscribe-hierarchical-interactive new-newsgroups))
11715     (setq gnus-newsrc-last-checked-date new-date)
11716     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11717     (if (> groups 0)
11718         (message "%d new newsgroup%s arrived." 
11719                  groups (if (> groups 1) "s have" " has")))))
11720
11721 (defun gnus-check-first-time-used ()
11722   (if (or (file-exists-p gnus-startup-file)
11723           (file-exists-p (concat gnus-startup-file ".el"))
11724           (file-exists-p (concat gnus-startup-file ".eld")))
11725       nil
11726     (message "First time user; subscribing you to default groups")
11727     (or gnus-have-read-active-file (gnus-read-active-file))
11728     (setq gnus-newsrc-last-checked-date (current-time-string))
11729     (let ((groups gnus-default-subscribed-newsgroups)
11730           group)
11731       (if (eq groups t)
11732           nil
11733         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
11734         (mapatoms
11735          (lambda (sym)
11736            (setq group (symbol-name sym))
11737            (if (and gnus-newsrc-options-n-yes
11738                     (string-match gnus-newsrc-options-n-yes group))
11739                (funcall gnus-subscribe-options-newsgroup-method group)
11740              (and (or (null gnus-newsrc-options-n-no)
11741                       (not (string-match gnus-newsrc-options-n-no group)))
11742                   (setq gnus-killed-list (cons group gnus-killed-list)))))
11743          gnus-active-hashtb)
11744         (while groups
11745           (if (gnus-gethash (car groups) gnus-active-hashtb)
11746               (gnus-group-change-level (car groups) 3 9))
11747           (setq groups (cdr groups)))
11748         (gnus-group-make-doc-group)
11749         (message
11750          (substitute-command-keys "\\<gnus-group-mode-map>\\[gnus-group-list-killed] to list killed groups"))))))
11751
11752 ;; `gnus-group-change-level' is the fundamental function for changing
11753 ;; subscription levels of newsgroups. This might mean just changing
11754 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
11755 ;; again, which subscribes/unsubscribes a group, which is equally
11756 ;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
11757 ;; from 8-9 to 1-7 means that you remove the group from the list of
11758 ;; killed (or zombie) groups and add them to the (kinda) subscribed
11759 ;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
11760 ;; which is trivial.
11761 ;; ENTRY can either be a string (newsgroup name) or a list (if
11762 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
11763 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
11764 ;; entries. 
11765 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
11766 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
11767 ;; after. 
11768 (defun gnus-group-change-level (entry level &optional oldlevel
11769                                       previous fromkilled)
11770   (let (group info active num)
11771     ;; Glean what info we can from the arguments
11772     (if (consp entry)
11773         (if fromkilled (setq group (nth 1 entry))
11774           (setq group (car (nth 2 entry))))
11775       (setq group entry))
11776     (if (and (stringp entry)
11777              oldlevel 
11778              (< oldlevel 8))
11779         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
11780     (if (and (not oldlevel)
11781              (listp entry))
11782         (setq oldlevel (car (cdr (nth 2 entry)))))
11783     (if (stringp previous)
11784         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
11785
11786     (gnus-dribble-enter
11787      (format "(gnus-group-change-level %S %S %S %S %S)" 
11788              group level oldlevel (car (nth 2 previous)) fromkilled))
11789     
11790     ;; Then we remove the newgroup from any old structures, if needed.
11791     ;; If the group was killed, we remove it from the killed or zombie
11792     ;; list. If not, and it is in fact going to be killed, we remove
11793     ;; it from the newsrc hash table and assoc.
11794     (cond ((>= oldlevel 8)
11795            (if (= oldlevel 8)
11796                (setq gnus-zombie-list (delete group gnus-zombie-list))
11797              (setq gnus-killed-list (delete group gnus-killed-list))))
11798           (t
11799            (if (>= level 8)
11800                (progn
11801                  (gnus-sethash (car (nth 2 entry))
11802                                nil gnus-newsrc-hashtb)
11803                  (if (nth 3 entry)
11804                      (setcdr (gnus-gethash (car (nth 3 entry))
11805                                            gnus-newsrc-hashtb)
11806                              (cdr entry)))
11807                  (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
11808
11809     ;; Finally we enter (if needed) the list where it is supposed to
11810     ;; go, and change the subscription level. If it is to be killed,
11811     ;; we enter it into the killed or zombie list.
11812     (cond ((>= level 8)
11813            (if (= level 8)
11814                (setq gnus-zombie-list (cons group gnus-zombie-list))
11815              (setq gnus-killed-list (cons group gnus-killed-list))))
11816           (t
11817            ;; If the list is to be entered into the newsrc assoc, and
11818            ;; it was killed, we have to create an entry in the newsrc
11819            ;; hashtb format and fix the pointers in the newsrc assoc.
11820            (if (>= oldlevel 8)
11821                (progn
11822                  (if (listp entry)
11823                      (progn
11824                        (setq info (cdr entry))
11825                        (setq num (car entry)))
11826                    (setq active (gnus-gethash group gnus-active-hashtb))
11827                    (setq num (- (1+ (cdr active)) (car active)))
11828                    ;; Check whether the group is foreign. If so, the
11829                    ;; foreign select method has to be entered into the
11830                    ;; info. 
11831                    (let ((method (gnus-group-method-name group)))
11832                      (if (eq method gnus-select-method)
11833                          (setq info (list group level 
11834                                           (cons 1 (1- (car active)))))
11835                        (setq info (list group level (cons 1 (1- (car active)))
11836                                         nil method)))))
11837                  (setq entry (cons info (if previous (cdr (cdr previous))
11838                                           (cdr gnus-newsrc-assoc))))
11839                  (setcdr (if previous (cdr previous) gnus-newsrc-assoc)
11840                          entry)
11841                  (gnus-sethash group (cons num (if previous (cdr previous)
11842                                                  gnus-newsrc-assoc))
11843                                gnus-newsrc-hashtb)
11844                  (if (cdr entry)
11845                      (setcdr (gnus-gethash (car (car (cdr entry)))
11846                                            gnus-newsrc-hashtb)
11847                              entry)))
11848              ;; It was alive, and it is going to stay alive, so we
11849              ;; just change the level and don't change any pointers or
11850              ;; hash table entries.
11851              (setcar (cdr (car (cdr (cdr entry)))) level))))))
11852
11853 (defun gnus-kill-newsgroup (newsgroup)
11854   "Obsolete function. Kills a newsgroup."
11855   (gnus-group-change-level (gnus-gethash newsgroup gnus-newsrc-hashtb) 9))
11856
11857 (defun gnus-check-bogus-newsgroups (&optional confirm)
11858   "Remove bogus newsgroups.
11859 If CONFIRM is non-nil, the user has to confirm the deletion of every
11860 newsgroup." 
11861   (let ((newsrc (cdr gnus-newsrc-assoc))
11862         bogus group)
11863     (message "Checking bogus newsgroups...")
11864     (or gnus-have-read-active-file (gnus-read-active-file))
11865     ;; Find all bogus newsgroup that are subscribed.
11866     (while newsrc
11867       (setq group (car (car newsrc)))
11868       (if (or (gnus-gethash group gnus-active-hashtb)
11869               (nth 4 (car newsrc))
11870               (and confirm
11871                    (not (gnus-y-or-n-p
11872                          (format "Remove bogus newsgroup: %s " group)))))
11873           ;; Active newsgroup.
11874           ()
11875         ;; Found a bogus newsgroup.
11876         (setq bogus (cons group bogus)))
11877       (setq newsrc (cdr newsrc)))
11878     ;; Remove all bogus subscribed groups by first killing them, and
11879     ;; then removing them from the list of killed groups.
11880     (while bogus
11881       (gnus-group-change-level 
11882        (gnus-gethash (car bogus) gnus-newsrc-hashtb) 9)
11883       (setq gnus-killed-list (delete (car bogus) gnus-killed-list))
11884       (setq bogus (cdr bogus)))
11885     ;; Then we remove all bogus groups from the list of killed and
11886     ;; zombie groups. They are are removed without confirmation.
11887     (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
11888           killed)
11889       (while dead-lists
11890         (setq killed (symbol-value (car dead-lists)))
11891         (while killed
11892           (setq group (car killed))
11893           (or (gnus-gethash group gnus-active-hashtb)
11894               ;; The group is bogus.
11895               (set (car dead-lists)
11896                    (delete group (symbol-value (car dead-lists)))))
11897           (setq killed (cdr killed)))
11898         (setq dead-lists (cdr dead-lists))))
11899     ;; While we're at it, we check the killed list for duplicates.
11900     ;; This has nothing to do with bogosity, but it's a convenient
11901     ;; place to put the check.
11902     (let ((killed gnus-killed-list))
11903       (while killed
11904         (message "%d" (length killed))
11905         (setcdr killed (delete (car killed) (cdr killed)))
11906         (setq killed (cdr killed))))
11907     (message "Checking bogus newsgroups... done")))
11908
11909 ;; Go though `gnus-newsrc-assoc' and compare with `gnus-active-hashtb'
11910 ;; and compute how many unread articles there are in each group.
11911 (defun gnus-get-unread-articles (&optional level)
11912   (let ((newsrc (cdr gnus-newsrc-assoc))
11913         (level (or level 6))
11914         info group active)
11915     (message "Checking new news...")
11916     (while newsrc
11917       (setq info (car newsrc))
11918       (setq group (car info))
11919       (setq active (gnus-gethash group gnus-active-hashtb))
11920
11921       ;; Check newsgroups. If the user doesn't want to check them, or
11922       ;; they can't be checked (for instance, if the news server can't
11923       ;; be reached) we just set the number of unread articles in this
11924       ;; newsgroup to t. This means that Gnus thinks that there are
11925       ;; unread articles, but it has no idea how many.
11926       (if (nth 4 info)
11927           (if (or (and gnus-activate-foreign-newsgroups 
11928                        (not (numberp gnus-activate-foreign-newsgroups)))
11929                   (and (numberp gnus-activate-foreign-newsgroups)
11930                        (<= (nth 1 info) gnus-activate-foreign-newsgroups)
11931                        (<= (nth 1 info) level)))
11932               (or (eq (car (nth 4 info)) 'nnvirtual)
11933                   (setq active (gnus-activate-newsgroup (car info)))))
11934         (if (and (not gnus-read-active-file)
11935                  (<= (nth 1 info) level))
11936             (progn
11937               (setq active (gnus-activate-newsgroup (car info))))))
11938       
11939       (or active (progn (gnus-sethash group nil gnus-active-hashtb)
11940                         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
11941       (and active (gnus-get-unread-articles-in-group info active))
11942       (setq newsrc (cdr newsrc)))
11943     (message "Checking new news... done")))
11944
11945 ;; Create a hash table out of the newsrc alist. The `car's of the
11946 ;; alist elements are used as keys.
11947 (defun gnus-make-hashtable-from-newsrc-alist ()
11948   (let ((alist gnus-newsrc-assoc)
11949          prev)
11950     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
11951     (setq alist 
11952           (setq prev (setq gnus-newsrc-assoc 
11953                            (cons (list "dummy.group" 0 (cons 0 0)) alist))))
11954     (while alist
11955       (gnus-sethash (car (car alist)) (cons nil prev) gnus-newsrc-hashtb)
11956       (setq prev alist)
11957       (setq alist (cdr alist)))))
11958
11959 (defun gnus-make-hashtable-from-killed ()
11960   "Create a hash table from the killed and zombie lists."
11961   (let ((lists '(gnus-killed-list gnus-zombie-list))
11962         list)
11963     (setq gnus-killed-hashtb 
11964           (gnus-make-hashtable 
11965            (+ (length gnus-killed-list) (length gnus-zombie-list))))
11966     (while lists
11967       (setq list (symbol-value (car lists)))
11968       (setq lists (cdr lists))
11969       (while list
11970         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
11971         (setq list (cdr list))))))
11972
11973 (defun gnus-get-unread-articles-in-group (info active)
11974   (let* ((range (nth 2 info))
11975          (num 0)
11976          (marked (nth 3 info))
11977          srange lowest group)
11978     ;; Modify the list of read articles according to what articles 
11979     ;; are available; then tally the unread articles and add the
11980     ;; number to the group hash table entry.
11981     (cond ((zerop (cdr active))
11982            (setq num 0))
11983           ((not range)
11984            (setq num (- (1+ (cdr active)) (car active))))
11985           ((atom (car range))
11986            ;; Fix a single (num . num) range according to the
11987            ;; active hash table.
11988            (if (< (cdr range) (car active)) (setcdr range (car active)))
11989            ;; Compute number of unread articles.
11990            (setq num (max 0 (- (cdr active) 
11991                                (- (1+ (cdr range)) (car range))))))
11992           (t
11993            ;; The read list is a list of ranges. Fix them according to
11994            ;; the active hash table.
11995            (setq srange range)
11996            (setq lowest (1- (car active)))
11997            (while (and (< (cdr (car srange)) lowest))
11998              (if (and (cdr srange)
11999                       (<= (cdr (car srange)) (1+ lowest)))
12000                  (progn
12001                    (setcdr (car srange) (cdr (car (cdr srange))))
12002                    (setcdr srange (cdr (cdr srange))))
12003                (setcdr (car srange) lowest)))
12004            ;; Compute the number of unread articles.
12005            (while range
12006              (setq num (+ num (- (1+ (cdr (car range))) 
12007                                  (car (car range)))))
12008              (setq range (cdr range)))
12009            (setq num (max 0 (- (cdr active) num)))))
12010     (and info
12011          (setcar (gnus-gethash (car info) gnus-newsrc-hashtb) 
12012                  (max 0 (- num (length (cdr (assq 'tick marked)))
12013                            (length (cdr (assq 'dormant marked)))))))
12014     num))
12015
12016 (defun gnus-activate-newsgroup (group)
12017   (let ((method (gnus-find-method-for-group group))
12018         active)
12019     (or (gnus-server-opened method) (gnus-open-server method))
12020     (and (gnus-request-group group)
12021          (save-excursion
12022            (set-buffer nntp-server-buffer)
12023            (goto-char 1)
12024            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
12025                 (progn
12026                   (goto-char (match-beginning 1))
12027                   (gnus-sethash 
12028                    group (setq active (cons (read (current-buffer))
12029                                             (read (current-buffer))))
12030                    gnus-active-hashtb)))))
12031     active))
12032
12033 (defun gnus-update-read-articles 
12034   (group unread unselected ticked &optional domarks replied expirable killed
12035          dormant bookmark score)
12036   "Update the list of read and ticked articles in GROUP using the
12037 UNREAD and TICKED lists.
12038 Note: UNSELECTED has to be sorted over `<'.
12039 Returns whether the updating was successful."
12040   (let* ((active (gnus-gethash group gnus-active-hashtb))
12041          (entry (gnus-gethash group gnus-newsrc-hashtb))
12042          (number (car entry))
12043          (info (nth 2 entry))
12044          (marked (nth 3 info))
12045          (prev 1)
12046          (unread (sort (copy-sequence unread) (function <)))
12047          last read)
12048     (if (or (not info) (not active))
12049         ;; There is no info on this group if it was, in fact,
12050         ;; killed. Gnus stores no information on killed groups, so
12051         ;; there's nothing to be done. 
12052         ;; One could store the information somewhere temporarily,
12053         ;; perhaps... Hmmm... 
12054         ()
12055       ;; Remove any negative articles numbers.
12056       (while (and unread (< (car unread) 0))
12057         (setq unread (cdr unread)))
12058       (if (not (and (numberp number) (zerop number)))
12059           (setq unread (nconc unselected unread)))
12060       ;; Set the number of unread articles in gnus-newsrc-hashtb.
12061       (setcar entry (max 0 (- (length unread) (length ticked) 
12062                               (length dormant))))
12063       ;; Compute the ranges of read articles by looking at the list of
12064       ;; unread articles.  
12065       (while unread
12066         (if (/= (car unread) prev)
12067             (setq read (cons (cons prev (1- (car unread))) read)))
12068         (setq prev (1+ (car unread)))
12069         (setq unread (cdr unread)))
12070       (if (<= prev (cdr active))
12071           (setq read (cons (cons prev (cdr active)) read)))
12072       ;; Enter this list into the group info.
12073       (setcar (cdr (cdr info)) 
12074               (if (> (length read) 1) (nreverse read) (car read)))
12075       ;; Enter the list of ticked articles.
12076       (gnus-set-marked-articles 
12077        info ticked
12078        (if domarks replied (cdr (assq 'reply marked)))
12079        (if domarks expirable (cdr (assq 'expire marked)))
12080        (if domarks killed (cdr (assq 'killed marked)))
12081        (if domarks dormant (cdr (assq 'dormant marked)))
12082        (if domarks bookmark (cdr (assq 'bookmark marked)))
12083        (if domarks score (cdr (assq 'score marked))))
12084       t)))
12085
12086 (defun gnus-make-articles-unread (group articles)
12087   "Mark ARTICLES in GROUP as unread."
12088   (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
12089     (setcar (nthcdr 2 info)
12090             (gnus-remove-from-range (nth 2 info) articles))
12091     (gnus-group-update-group group t)))
12092
12093 (defun gnus-read-active-file ()
12094   "Get active file from NNTP server."
12095   (gnus-group-set-mode-line)
12096   (let ((methods (cons gnus-select-method gnus-secondary-select-methods)))
12097     (setq gnus-have-read-active-file nil)
12098     (while methods
12099       (let* ((where (nth 1 (car methods)))
12100              (mesg (format "Reading active file%s via %s..."
12101                            (if (and where (not (zerop (length where))))
12102                                (concat " from " where) "")
12103                            (car (car methods)))))
12104         (message mesg)
12105         (if (gnus-request-list (car methods)) ; Get active 
12106             (save-excursion
12107               (set-buffer nntp-server-buffer)
12108               (gnus-active-to-gnus-format 
12109                (and gnus-have-read-active-file (car methods)))
12110               (setq gnus-have-read-active-file t)
12111               (message "%s...done" mesg))
12112           (message "Cannot read active file from %s server." 
12113                    (car (car methods)))
12114           (ding)))
12115       (setq methods (cdr methods)))))
12116
12117 ;; rewritten by jwz based on ideas from Rick Sladkey <jrs@world.std.com>
12118 ;; Further rewrites by lmi.
12119 (defun gnus-active-to-gnus-format (method &optional hashtb)
12120   "Convert active file format to internal format.
12121 Lines matching `gnus-ignored-newsgroups' are ignored."
12122   (let ((cur (current-buffer))
12123         (hashtb (or hashtb 
12124                     (if method
12125                         gnus-active-hashtb
12126                       (setq gnus-active-hashtb
12127                             (gnus-make-hashtable 
12128                              (count-lines (point-min) (point-max))))))))
12129     ;; Delete unnecessary lines.
12130     (goto-char (point-min))
12131     (delete-matching-lines gnus-ignored-newsgroups)
12132     (and method (not (eq method gnus-select-method))
12133          (let ((prefix (gnus-group-prefixed-name "" method)))
12134            (goto-char (point-min))
12135            (while (and (not (eobp))
12136                        (null (insert prefix))
12137                        (zerop (forward-line 1))))))
12138     (goto-char (point-min))
12139     ;; Store active file in hashtable.
12140     (save-restriction
12141       (if (or (re-search-forward "\n.\r?$" nil t)
12142               (goto-char (point-max)))
12143           (progn
12144             (beginning-of-line)
12145             (narrow-to-region (point-min) (point))))
12146       (goto-char (point-min))
12147       (if (string-match "%[oO]" gnus-group-line-format)
12148           ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
12149           ;; If we want information on moderated groups, we use this
12150           ;; loop...   
12151           (condition-case ()
12152               (let ((mod-hashtb (make-vector 7 0))
12153                     group max mod)
12154                 (while (not (eobp))
12155                   (setq group (let ((obarray hashtb))
12156                                 (read cur)))
12157                   (setq max (read cur))
12158                   (set group (cons (read cur) max))
12159                   ;; Enter moderated groups into a list.
12160                   (if (string= 
12161                        (symbol-name  (let ((obarray mod-hashtb)) (read cur)))
12162                        "m")
12163                       (setq gnus-moderated-list 
12164                             (cons (symbol-name group) gnus-moderated-list)))
12165                   (forward-line 1)))
12166             (error 
12167              (progn (ding) (message "Possible error in active file."))))
12168         ;; And if we do not care about moderation, we use this loop,
12169         ;; which is faster.
12170         (condition-case ()
12171             (let (group max)
12172               (while (not (eobp))
12173                 ;; group gets set to a symbol interned in the hash table
12174                 ;; (what a hack!!)
12175                 (setq group (let ((obarray hashtb)) (read cur)))
12176                 (setq max (read cur))
12177                 (set group (cons (read cur) max))
12178                 (forward-line 1)))
12179           (error 
12180            (progn (ding) (message "Possible error in active file."))))))))
12181
12182 (defun gnus-read-newsrc-file (&optional force)
12183   "Read startup file.
12184 If FORCE is non-nil, the .newsrc file is read."
12185   (setq gnus-current-startup-file (gnus-make-newsrc-file gnus-startup-file))
12186   ;; Reset variables that might be defined in the .newsrc.eld file.
12187   (let ((variables gnus-variable-list))
12188     (while variables
12189       (set (car variables) nil)
12190       (setq variables (cdr variables))))
12191   (let* ((newsrc-file gnus-current-startup-file)
12192          (quick-file (concat newsrc-file ".el")))
12193     (save-excursion
12194       ;; We always load the .newsrc.eld file. If always contains
12195       ;; much information that can not be gotten from the .newsrc
12196       ;; file (ticked articles, killed groups, foreign methods, etc.)
12197       (gnus-read-newsrc-el-file quick-file)
12198  
12199       (if (or force
12200               (and (file-newer-than-file-p newsrc-file quick-file)
12201                    (file-newer-than-file-p newsrc-file 
12202                                            (concat quick-file "d")))
12203               (not gnus-newsrc-assoc))
12204           ;; We read the .newsrc file. Note that if there if a
12205           ;; .newsrc.eld file exists, it has already been read, and
12206           ;; the `gnus-newsrc-hashtb' has been created. While reading
12207           ;; the .newsrc file, Gnus will only use the information it
12208           ;; can find there for changing the data already read -
12209           ;; ie. reading the .newsrc file will not trash the data
12210           ;; already read (except for read articles).
12211           (save-excursion
12212             (message "Reading %s..." newsrc-file)
12213             (set-buffer (find-file-noselect newsrc-file))
12214             (buffer-disable-undo (current-buffer))
12215             (gnus-newsrc-to-gnus-format)
12216             (kill-buffer (current-buffer))
12217             (message "Reading %s... done" newsrc-file)))
12218       (and gnus-use-dribble-file (gnus-dribble-read-file)))))
12219
12220 (defun gnus-read-newsrc-el-file (file)
12221   (let ((ding-file (concat file "d")))
12222     ;; We always, always read the .eld file.
12223     (message "Reading %s..." ding-file)
12224     (condition-case nil
12225         (load ding-file t t t)
12226       (error nil))
12227     (gnus-make-hashtable-from-newsrc-alist)
12228     (if (not (file-newer-than-file-p file ding-file))
12229         ()
12230       ;; Old format quick file
12231       (message "Reading %s..." file)
12232       ;; The .el file is newer than the .eld file, so we read that one
12233       ;; as well. 
12234       (gnus-read-old-newsrc-el-file file))))
12235
12236 ;; Parse the old-style quick startup file
12237 (defun gnus-read-old-newsrc-el-file (file)
12238   (let (newsrc killed marked group g m len info)
12239     (prog1
12240         (let (gnus-killed-assoc gnus-marked-assoc gnus-newsrc-assoc)
12241           (prog1
12242               (condition-case nil
12243                   (load file t t t)
12244                 (error nil))
12245             (setq newsrc gnus-newsrc-assoc
12246                   killed gnus-killed-assoc
12247                   marked gnus-marked-assoc)))
12248       (setq gnus-newsrc-assoc nil)
12249       (while newsrc
12250         (setq group (car newsrc))
12251         (let ((info (nth 2 (gnus-gethash (car group) gnus-newsrc-hashtb))))
12252           (if info
12253               (progn
12254                 (setcar (nthcdr 2 info) (cdr (cdr group)))
12255                 (setcar (cdr info) (if (nth 1 group) 3 6))
12256                 (setq gnus-newsrc-assoc (cons info gnus-newsrc-assoc)))
12257             (setq gnus-newsrc-assoc
12258                   (cons 
12259                    (setq info
12260                          (list (car group)
12261                                (if (nth 1 group) 3 6) (cdr (cdr group))))
12262                    gnus-newsrc-assoc)))
12263           (if (setq m (assoc (car group) marked))
12264             (setcdr (cdr (cdr info)) (cons (list (cons 'tick (cdr m))) nil))))
12265         (setq newsrc (cdr newsrc)))
12266       (setq newsrc killed)
12267       (while newsrc
12268         (setcar newsrc (car (car newsrc)))
12269         (setq newsrc (cdr newsrc)))
12270       (setq gnus-killed-list killed))
12271     (setq gnus-newsrc-assoc (nreverse gnus-newsrc-assoc))
12272     (gnus-make-hashtable-from-newsrc-alist)))
12273       
12274 (defun gnus-make-newsrc-file (file)
12275   "Make server dependent file name by catenating FILE and server host name."
12276   (let* ((file (expand-file-name file nil))
12277          (real-file (concat file "-" (nth 1 gnus-select-method))))
12278     (if (file-exists-p real-file)
12279         real-file file)
12280     ))
12281
12282 ;; jwz: rewrote this function to be much more efficient, and not be subject
12283 ;; to regexp overflow errors when it encounters very long lines -- the old
12284 ;; behavior was to blow off the rest of the *file* when a line was encountered
12285 ;; that was too long to match!!  Now it uses only simple looking-at calls, and
12286 ;; doesn't create as many temporary strings.  It also now handles multiple
12287 ;; consecutive options lines (before it only handled the first.)
12288 ;; Tiny rewrite by lmi. 
12289 (defun gnus-newsrc-to-gnus-format ()
12290   "Parse current buffer as .newsrc file."
12291   ;; We have to re-initialize these variables (except for
12292   ;; gnus-killed-list) because the quick startup file may contain bogus
12293   ;; values.
12294   (setq gnus-newsrc-options nil)
12295   (setq gnus-newsrc-options-n-yes nil)
12296   (setq gnus-newsrc-options-n-no nil)
12297   (setq gnus-newsrc-assoc nil)
12298   (gnus-parse-options-lines)
12299   (gnus-parse-newsrc-body))
12300
12301 (defun gnus-parse-options-lines ()
12302   ;; newsrc.5 seems to indicate that the options line can come anywhere
12303   ;; in the file, and that there can be any number of them:
12304   ;;
12305   ;;       An  options  line  starts  with  the  word  options (left-
12306   ;;       justified).  Then there are the list of  options  just  as
12307   ;;       they would be on the readnews command line.  For instance:
12308   ;;
12309   ;;       options -n all !net.sf-lovers !mod.human-nets -r
12310   ;;       options -c -r
12311   ;;
12312   ;;       A string of lines beginning with a space or tab after  the
12313   ;;       initial  options  line  will  be  considered  continuation
12314   ;;       lines.
12315   ;;
12316   ;; For now, we only accept it at the beginning of the file.
12317
12318   (goto-char (point-min))
12319   (skip-chars-forward " \t\n")
12320   (setq gnus-newsrc-options nil)
12321   (while (looking-at "^options[ \t]*\\(.*\\)\n")
12322     ;; handle consecutive options lines
12323     (setq gnus-newsrc-options (concat gnus-newsrc-options
12324                                       (if gnus-newsrc-options "\n\t")
12325                                       (buffer-substring (match-beginning 1)
12326                                                         (match-end 1))))
12327     (forward-line 1)
12328     (while (looking-at "[ \t]+\\(.*\\)\n")
12329       ;; handle subsequent continuation lines of this options line
12330       (setq gnus-newsrc-options (concat gnus-newsrc-options "\n\t"
12331                                         (buffer-substring (match-beginning 1)
12332                                                           (match-end 1))))
12333       (forward-line 1)))
12334   ;; Gather all "-n" options lines.
12335   (let ((start 0)
12336         (result nil))
12337     (if gnus-newsrc-options
12338         (while (and (string-match "^[ \t]*-n\\([^\n]*\\)$"
12339                                   gnus-newsrc-options
12340                                   start)
12341                     (setq start (match-end 0)))
12342           (setq result (concat result
12343                                (and result " ")
12344                                (substring gnus-newsrc-options
12345                                           (match-beginning 1)
12346                                           (match-end 1))))))
12347     (let ((yes-and-no (and result (gnus-parse-n-options result))))
12348       (and (or gnus-options-subscribe (car yes-and-no))
12349            (setq gnus-newsrc-options-n-yes 
12350                  (concat (or gnus-options-subscribe "") 
12351                          (or (car yes-and-no) ""))))
12352       (and (or gnus-options-not-subscribe (cdr yes-and-no))
12353            (setq gnus-newsrc-options-n-no 
12354                  (concat (or gnus-options-not-subscribe "") 
12355                          (or (cdr yes-and-no) "")))))
12356     nil))
12357
12358 (defun gnus-parse-newsrc-body ()
12359   ;; Point has been positioned after the options lines.  We shouldn't
12360   ;; see any more in here.
12361
12362   (let ((subscribe nil)
12363         (read-list nil)
12364         (line (1+ (count-lines (point-min) (point))))
12365         newsgroup
12366         p p2)
12367     (save-restriction
12368       (skip-chars-forward " \t")
12369       (while (not (eobp))
12370         (cond
12371          ((= (following-char) ?\n)
12372           ;; skip blank lines
12373           nil)
12374          (t
12375           (setq p (point))
12376           (skip-chars-forward "^:!\n")
12377           (if (= (following-char) ?\n)
12378               (error "line %d is unparsable in %s" line (buffer-name)))
12379           (setq p2 (point))
12380           (skip-chars-backward " \t")
12381
12382           ;; #### note: we could avoid consing a string here by binding obarray
12383           ;; and reading the newsgroup directly into the gnus-newsrc-hashtb,
12384           ;; then setq'ing newsgroup to symbol-name of that, like we do in
12385           ;; gnus-active-to-gnus-format.
12386           (setq newsgroup (buffer-substring p (point)))
12387           (goto-char p2)
12388
12389           (setq subscribe (= (following-char) ?:))
12390           (setq read-list nil)
12391
12392           (forward-char 1)              ; after : or !
12393           (skip-chars-forward " \t")
12394           (while (not (= (following-char) ?\n))
12395             (skip-chars-forward " \t")
12396             (or
12397              (and (cond
12398                    ((looking-at "\\([0-9]+\\)-\\([0-9]+\\)") ; a range
12399                     (setq read-list
12400                           (cons
12401                            (cons
12402                             (progn
12403                               ;; faster that buffer-substring/string-to-int
12404                               (narrow-to-region (point-min) (match-end 1))
12405                               (read (current-buffer)))
12406                             (progn
12407                               (narrow-to-region (point-min) (match-end 2))
12408                               (forward-char) ; skip over "-"
12409                               (prog1
12410                                   (read (current-buffer))
12411                                 (widen))))
12412                            read-list))
12413                     t)
12414                    ((looking-at "[0-9]+")
12415                     ;; faster that buffer-substring/string-to-int
12416                     (narrow-to-region (point-min) (match-end 0))
12417                     (setq p (read (current-buffer)))
12418                     (widen)
12419                     (setq read-list (cons (cons p p) read-list))
12420                     t)
12421                    (t
12422                     ;; bogus chars in ranges
12423                     nil))
12424                   (progn
12425                     (goto-char (match-end 0))
12426                     (skip-chars-forward " \t")
12427                     (cond ((= (following-char) ?,)
12428                            (forward-char 1)
12429                            t)
12430                           ((= (following-char) ?\n)
12431                            t)
12432                           (t
12433                            ;; bogus char after range
12434                            nil))))
12435              ;; if we get here, the parse failed
12436              (progn
12437                (end-of-line)            ; give up on this line
12438                (ding)
12439                (message "Ignoring bogus line %d for %s in %s"
12440                         line newsgroup (buffer-name))
12441                (sleep-for 1))))
12442           ;; We have already read .newsrc.eld, so we gently update the
12443           ;; data in the hash table with the information we have just
12444           ;; read. 
12445           (let ((info (nth 2 (gnus-gethash newsgroup gnus-newsrc-hashtb))))
12446             (if info
12447                 (progn
12448                   (setcar (nthcdr 2 info) (nreverse read-list))
12449                   (setcar (cdr info) (if subscribe 3 (if read-list 6 7)))
12450                   (setq gnus-newsrc-assoc (cons info gnus-newsrc-assoc)))
12451               (setq gnus-newsrc-assoc
12452                     (cons (list newsgroup (if subscribe 3 (if read-list 6 7))
12453                                 (nreverse read-list))
12454                           gnus-newsrc-assoc))))))
12455         (setq line (1+ line))
12456         (forward-line 1))))
12457   (setq gnus-newsrc-assoc (nreverse gnus-newsrc-assoc))
12458   (gnus-make-hashtable-from-newsrc-alist)
12459   nil)
12460
12461 (defun gnus-parse-n-options (options)
12462   "Parse -n NEWSGROUPS options and return a cons of YES and NO regexps."
12463   (let ((yes nil)
12464         (no nil)
12465         (yes-or-no nil)                 ;`!' or not.
12466         (newsgroup nil))
12467     ;; Parse each newsgroup description such as "comp.all".  Commas
12468     ;; and white spaces can be a newsgroup separator.
12469     (while
12470         (string-match "^[ \t\n,]*\\(!?\\)\\([^- \t\n,][^ \t\n,]*\\)" options)
12471       (setq yes-or-no
12472             (substring options (match-beginning 1) (match-end 1)))
12473       (setq newsgroup
12474             (regexp-quote
12475              (substring options
12476                         (match-beginning 2) (match-end 2))))
12477       (setq options (substring options (match-end 2)))
12478       ;; Rewrite "all" to ".+" not ".*".  ".+" requires at least one
12479       ;; character.
12480       (while (string-match "\\(^\\|\\\\[.]\\)all\\(\\\\[.]\\|$\\)" newsgroup)
12481         (setq newsgroup
12482               (concat (substring newsgroup 0 (match-end 1))
12483                       ".+"
12484                       (substring newsgroup (match-beginning 2)))))
12485       ;; It is yes or no.
12486       (cond ((string-equal yes-or-no "!")
12487              (setq no (cons newsgroup no)))
12488             ((string-equal newsgroup ".+")) ;Ignore `all'.
12489             (t
12490              (setq yes (cons newsgroup yes))))
12491       )
12492     ;; Make a cons of regexps from parsing result.
12493     ;; We have to append \(\.\|$\) to prevent matching substring of
12494     ;; newsgroup.  For example, "jp.net" should not match with
12495     ;; "jp.network".
12496     ;; Fixes for large regexp problems are from yonezu@nak.math.keio.ac.jp.
12497     (cons (if yes
12498               (concat "^\\("
12499                       (apply (function concat)
12500                              (mapcar
12501                               (lambda (newsgroup)
12502                                 (concat newsgroup "\\|"))
12503                               (cdr yes)))
12504                       (car yes) "\\)\\(\\.\\|$\\)"))
12505           (if no
12506               (concat "^\\("
12507                       (apply (function concat)
12508                              (mapcar
12509                               (lambda (newsgroup)
12510                                 (concat newsgroup "\\|"))
12511                               (cdr no)))
12512                       (car no) "\\)\\(\\.\\|$\\)")))
12513     ))
12514
12515 (defun gnus-save-newsrc-file ()
12516   "Save .newsrc file."
12517   ;; Note: We cannot save .newsrc file if all newsgroups are removed
12518   ;; from the variable gnus-newsrc-assoc.
12519   (and (or gnus-newsrc-assoc gnus-killed-list)
12520        gnus-current-startup-file
12521        (save-excursion
12522          (if (zerop (save-excursion
12523                     (set-buffer gnus-dribble-buffer)
12524                     (buffer-size)))
12525              (message "(No changes need to be saved)")
12526            (if gnus-save-newsrc-file
12527                (let ((make-backup-files t)
12528                      (version-control nil)
12529                      (require-final-newline t)) ;Don't ask even if requested.
12530                  (message "Saving %s..." gnus-current-startup-file)
12531                  ;; Make backup file of master newsrc.
12532                  ;; You can stop or change version control of backup file.
12533                  ;; Suggested by jason@violet.berkeley.edu.
12534                  (run-hooks 'gnus-save-newsrc-hook)
12535                  (gnus-gnus-to-newsrc-format)
12536                  (message "Saving %s... done" gnus-current-startup-file)))
12537            ;; Quickly loadable .newsrc.
12538            (set-buffer (get-buffer-create " *Gnus-newsrc*"))
12539            (gnus-add-current-to-buffer-list)
12540            (buffer-disable-undo (current-buffer))
12541            (erase-buffer)
12542            (message "Saving %s.eld..." gnus-current-startup-file)
12543            (gnus-gnus-to-quick-newsrc-format)
12544            (let ((make-backup-files nil)
12545                  (version-control nil)
12546                  (require-final-newline t)) ;Don't ask even if requested.
12547              (write-region 1 (point-max) 
12548                            (concat gnus-current-startup-file ".eld") 
12549                            nil 'nomesg))
12550            (kill-buffer (current-buffer))
12551            (message "Saving %s.eld... done" gnus-current-startup-file)
12552            (gnus-dribble-delete-file)))))
12553
12554 (defun gnus-gnus-to-quick-newsrc-format ()
12555   "Insert Gnus variables such as gnus-newsrc-assoc in lisp format."
12556   (insert ";; (ding) Gnus startup file.\n")
12557   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
12558   (insert ";; to read .newsrc.\n")
12559   (let ((variables gnus-variable-list)
12560         (gnus-newsrc-assoc (cdr gnus-newsrc-assoc))
12561         variable)
12562     ;; insert lisp expressions.
12563     (while variables
12564       (setq variable (car variables))
12565       (and (boundp variable)
12566            (symbol-value variable)
12567            (or gnus-save-killed-list (not (eq variable 'gnus-killed-list)))
12568            (insert "(setq " (symbol-name variable) " '"
12569                    (prin1-to-string (symbol-value variable))
12570                    ")\n"))
12571       (setq variables (cdr variables)))))
12572
12573 (defun gnus-gnus-to-newsrc-format ()
12574   ;; Generate and save the .newsrc file.
12575   (let ((newsrc (cdr gnus-newsrc-assoc))
12576         info ranges range)
12577     (save-excursion
12578       (set-buffer (create-file-buffer gnus-startup-file))
12579       (buffer-disable-undo (current-buffer))
12580       (erase-buffer)
12581       ;; Write options.
12582       (if gnus-newsrc-options (insert "options " gnus-newsrc-options "\n"))
12583       ;; Write subscribed and unsubscribed.
12584       (while newsrc
12585         (setq info (car newsrc))
12586         (if (not (nth 4 info))          ;Don't write foreign groups to .newsrc.
12587             (progn
12588               (insert (car info) (if (>= (nth 1 info) 6) "!" ":"))
12589               (if (setq ranges (nth 2 info))
12590                   (progn
12591                     (insert " ")
12592                     (if (atom (car ranges))
12593                         (if (= (car ranges) (cdr ranges))
12594                             (insert (int-to-string (car ranges)))
12595                           (insert (int-to-string (car ranges)) "-" 
12596                                   (int-to-string (cdr ranges))))
12597                       (while ranges
12598                         (setq range (car ranges)
12599                               ranges (cdr ranges))
12600                         (if (= (car range) (cdr range))
12601                             (insert (int-to-string (car range)))
12602                           (insert (int-to-string (car range)) "-"
12603                                   (int-to-string (cdr range))))
12604                         (if ranges (insert ","))))))
12605               (insert "\n")))
12606         (setq newsrc (cdr newsrc)))
12607       (write-region 1 (point-max) gnus-current-startup-file nil 'nomesg)
12608       (kill-buffer (current-buffer)))))
12609
12610 (defun gnus-read-descriptions-file ()
12611   (message "Reading descriptions file...")
12612   (if (not (gnus-request-list-newsgroups gnus-select-method))
12613       (progn
12614         (message "Couldn't read newsgroups descriptions")
12615         nil)
12616     (let (group)
12617       (setq gnus-description-hashtb 
12618             (gnus-make-hashtable (length gnus-active-hashtb)))
12619       (save-excursion
12620         (save-restriction
12621           (set-buffer nntp-server-buffer)
12622           (goto-char (point-min))
12623           (delete-non-matching-lines "^[a-zA-Z\\.0-9]+[ \t]")
12624           (goto-char (point-min))
12625           (if (or (search-forward "\n.\n" nil t)
12626                   (goto-char (point-max)))
12627               (progn
12628                 (beginning-of-line)
12629                 (narrow-to-region (point-min) (point))))
12630           (goto-char (point-min))
12631           (while (not (eobp))
12632             (setq group (let ((obarray gnus-description-hashtb))
12633                           (read (current-buffer))))
12634             (skip-chars-forward " \t")
12635             (set group (buffer-substring 
12636                         (point) (save-excursion (end-of-line) (point))))
12637             (forward-line 1))))
12638       (message "Reading descriptions file...done")
12639       t)))
12640
12641 (provide 'gnus)
12642
12643 ;;; gnus.el ends here